Creating pub key from private key using ssh - ssh

I have executed below command for creating public key from private key, it shows me an error and asking for passphrase.
~# ssh-keygen -y -f key.pem > mykey.pub
###########################################################
# WARNING: UNPROTECTED PRIVATE KEY FILE! #
###########################################################
Permissions 0644 for 'key.pem' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
bad permissions: ignore key: key.pem
Enter passphrase:
###########################################################
# WARNING: UNPROTECTED PRIVATE KEY FILE! #
###########################################################
Permissions 0644 for 'key.pem' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
bad permissions: ignore key: key.pem
load failed
Thanks(in advance)

ssh checks if your private key file could be read by other users. Since it is the case, it refuses to go further.
just chmod 600 key.pem and run the tool again (read-write rights only for current user)

Quick bash-only hack:
ssh-keygen -y -f <(cat key.pem) > mykey.pub
The process substition empolyed here should create a named pipe that has limited permissions.

Related

How can I determine the format of an ssh private key file?

I am developing an application that uses ssh keys (JSch), and it won't accept some ssh keys. I want to make guidelines to users so they know which keys they can use and which one they can't. Hence the question: how can I determine the format of an ssh private key file, going beyond the RSA/etc and the number of bits?
For instance, at the end of this question is a 3072 bit RSA key that was generated using ssh-keygen with no parameters on android/termux. it works in ssh but both JSch and JuiceSSH refuse to even open it. openssl rsa -text -noout -in file says:
unable to load Private Key
1991864336:error:0909006C:PEM routines:get_name:no start line:../crypto/pem/pem_lib.c:745:Expecting: ANY PRIVATE KEY
I can fix this by running ssh-keygen -p -m PEM -f file, and this might be a workaround for some users, but it would help to know in advance which formats work and which don't.
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn
NhAAAAAwEAAQAAAYEAxeXW062gz3y29H9nni0pz+IDetDpMWxOsyLXAIJuyRrXNqmcsLTz
f2qMP4ua3ENkqjoa+wwv95Ft+X4eb920FoKCL4vzpSWgFH8MfCL8Szs1y0/vgzVEipBMCh
FwIXqtnFaTvVS69vgZKARspIEod9z/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X4wkJtTx71BJFg5
6FlKmYZKu6JSIoIzDKR6Uaqb8s0HOgj9a9bf7kXBk2CWfYz1BU777NqQdZAJdJtSH52fXg
Rlk/d8DHNpBVrPAAAAEXUwX2ExNzFAbG9jYWxob3N0AQ==
-----END OPENSSH PRIVATE KEY-----

PAM module that uses SSH

Is there a PAM module that can authenticate a user with an SSH challenge-response?
I'm trying to write a PAM aware application that essentially has the same authentication process as an SSH connection. I have been unable to find a PAM module that already does this (the pam_ssh module is still password-based), so I have been attempting to write my own.
However, I cannot find an API that will allow me to access SSH in this way. Even with a PAM module for sshd, there doesn't seem to be a clean way of verifying an SSH public key.
To be more explicit, if I am given an SSH public key for a given user on a machine (consider localhost for simplicity), I want to send a challenge to that user on that machine to test if they own that SSH key. Does anyone know a way of doing this?
Thank you!
You could encrypt some random data using the public key, send it to the user, and assert that they are able to decrypt it with the private key. If you're using RSA keys, you can perform the encryption/decryption with OpenSSL. For example,
# server
$ ssh-keygen -e -m PKCS8 -f ~/.ssh/id_rsa.pub >pub.pem
$ openssl rsautl -pubin -inkey pub.pem -encrypt -pkcs -in random -out encrypted
# client
$ openssl rsautl -inkey ~/.ssh/id_rsa -decrypt -in encrypted -out random
and verify that the client is able to reply with the correct contents of random.
These commands are inspired by jass, which you could use instead of ssh-keygen+openssl.
Unfortunately, this doesn't work with Ed25519 keys; OpenSSH has its own custom format and doesn't have a way to convert them.

Check my private key from created ssh keys

I use ssh-keygen -t rsa to generate ssh keys, I learned that my private key is saved to the id_rsa file in the .ssh directory, I wonder what mac ox command line that I should use to see what my private keys is? Thank you in advance.
cat ~/.ssh/id_rsa
or
open ~/.ssh/id_rsa
Works the same for id_rsa.pub

'Not a private Key' error when trying to convert pem to ppk

I'm trying to access my friend's AWS server via ssh. He's given me the host name as well as a pem file. I'm relatively new to this and am trying to use putty to connect to the login. For him on Windows, he uses the PuttyGen to convert his pem files into ppk files. But on Ubuntu I couldn't find a GUI version for puttygen and tried looking up on converting the pem file to a ppk file but with no luck. Tried converting it using
puttygen myFile.pem -o newFile.ppk
but get an error
puttygen: unable to load file `myFile.pem': not a private key
I've tried the same command with the -O private option at the end but with no luck either. Even if I try to use ssh with the -i option with the pem file itself, it asks me for a passphrase which I don't know and my friend says there is no passphrase.
My friend gave me his ppk file which I tried to use with Putty but it didn't work.
You can tell if the private key is encrypted or not by printing the public key
encrypted key:
$ ssh-keygen -y -f ~/.ssh/test_key_with_passphrase
Enter passphrase:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC5kB+0TPMDH/OQ6t/ps67DDJvZzAFF4QyqTYFS3K86bmEz5DeIBtB8kgi3a7ecft6/ooeq+WnuyGvInwNY9GqrO3WDbP4joAqAc6waolEIcs8Nb6iNK+Zhv3O0BfAeXnb5aAztGFfzoBKz6MFGw6Haod3BkZiC40/owG11rjwvb7p4mlHsGrjPpBOGMZ66zPBPuEoFDcCDUnpgh9tNww4Wrzcp+jgZM1MP5ylRCiQE/ssgu3G0zZ3H+5YwRN/XNChomXW74W/yBnp5gAqJZNhiuxTaZBDANXAyiqwrysfzYFgzvTDfyf03aysPAMWkWucmMHxnHz5C649ikSLAOK+h
unencrypted key:
$ ssh-keygen -y -f ~/.ssh/test_key_in_the_clear.pem
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCxNX8DHatQQYw05PHcEvwHwKaqDvZUQWG23uUxQSxEiq1crf2j9dPCfzigKcPxIYarTIJ6yvdP6Kl/ONb+OKM7j6dd8ljO5LOs7dsgA5Tr2gaWyjrjmg767VCN2PB6BJZ9xz+p3SGTdFWtUXYtaEPAGocRx09N9kofpecRbRMlnbfHotyK8canGYzzRfimzk/uDAC/CcpeG3YLphj7zhpRaXhgdu/FKcdiTryqgktlZreJEbefeq3CEBM9kmxvr2uDc+QSVnhbcdutTJ4u4DEop0ZuTREZ2tH2HoAVruiJQ7Nd/VP8jz3SD5ySFBzPGiPcNMQ2mOP0cffm55+3CTwT
Once you've worked that out, then ssh with -i to get on to the server, if you are using ubuntu there's no need to bother with putty.
EDIT
Here's what a pem file would normally look like:
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC,0E109A1A0F7582B0D8B5AAAFDAB18C2A
wzvkuSuXTFTSyKvkmU7bytC6xhNGybkcc9FKH6oCFL802ZnJIUWnqIV7x4vXuDxg
zQFggRxnEqJIPv+gNUCdBMqlKal9XqAVAXw6I2bDKJIymg/UB0ODVzqzxhD1w+fD
a1nArpapqfO2Vgfeb0ABv3yNHXNcsLGpv21wR6JY/SskQQflt59MJ3pdc5nFQ3nE
O0AMiI9YH6dLPOtk5HMGR1s55b6SgHkfiaDoIAqRJd8J9Zrg7qDV0oncQmxYT+A/
ye+5OmXVoRP1SskhhmMFbb84GZ5YSpeIXqFCVaH+Uvifzhxc0La53sZEdJw1QUf/
f1ZIgg+Kysir+HX3Cul6LPz60tzjkZrlhGzPBmwcrM1zM1ehzQqU5dKzp3uAjtP0
NUHMAg5TG8dLv7eaKQAPis6f2vYebjzd538sA5O0EpMuNuWk1/CrchrKxS7ii8Sj
LkD/3hIrpXC4UO1AVJq1+OmbXh3LOCiTh2i+hKhFBS2q8V4cCaW5VbQfup8gyn5K
jOstbx+oCH3OsfoG3Tcr75+TKl1m4jXZmFACzEIQeuiWTA58nbFQDDSQJo9AdC6n
eMnT2vLvZQo2Og1fKfaaHSsh/sfqIqDxQhqCZCT7QB4SAB+CtD5QnzaWkHAqMNHv
RjW9rDdM3/oQxXba0eiCa2XBcI+W9ZU1ItjyqfbP7PJIge4rICTA0EmgZiK8uEjl
opckNqCfCVuAJlFDPJKFA/hh1cA1q+HHNY+YsKu0nj3uWtQb1ihaXTvUbIZk/rub
rNwHT9pxf59lFaIQ6/bTTxEN29m4ocPLmbzLUsAILl5vGKKwqnORypvcQZFPxC8y
xdsSsFwV43EQUpKPlewFa2tj+KDCOtWtFzj7SJrISi5j3/85g2i61eYVSJb28Cl5
cxbKDuDNok9JbA4XUS10zSpJc5y674XBt3VsZB3O4Oap4SpyuobcrvTd9jnhs7eS
KMm+qgtMhzvNs9b5RZAsGyRCThOJX0LNI8AINaF75eThmtqCcW7/Xrij/ZWt0Fs+
p74fqh6YBCfyRe5rwtNEQ+clYivdbkkUqep/6suV8KU/e6SzcmjbMHrrwtgcjji1
1IS8f858IsDoSWtij/gyWcNI945GmN7DbqjwpwajyidGrY0iYm3lzJOG3lR61LCU
hVgt017BZQc5El67RYifMBFgHRUL5W5FgBbOlLBrdYSvvCoTfSvpH1KccY6GcqYJ
mWQd0bfhAkb7s4KSndNUakVNo6FXO2f9o9uRJfLcgyW6WQkxGSpXYySOjY0SpRUS
ep7Zv2/fdjtcXMR9kFV1Hj/qGal9HQ+DE9j9aO9TZ7a3PotMwglPpAgi01EwqVlz
6wLmrWAXc9zdEjk/xCxWfgaFtRYzyVX+2aPsKoMCnVggEsk2z2jSJx8dh3tFsRl7
s2cgtFaAQdatTj3xOyNOP/8q8XBGXEu27QLqjdJOxT8Ngl+OjXt02d+rFVQwWvyn
sXpNz7VQJM4Irh2/ZOs4ySfLmzu46yDp75h3WaAasKVV5PzEQad4Sr8CziKF9WQu
KnJA3jJ0Nu746xDMMHhFibNZ/Day95LRrAi97c4nNPgk8JV6mEHKBRcWFyRmpFo2
-----END RSA PRIVATE KEY-----
If the file doesn't look like that then you dont have a pem file.

DSA vs RSA: how can you tell

Does anyone have a tool to use in order to differentiate an RSA public key vs a DSA public key? I have two SSH .pub files and i need to know if they're RSA or DSA.
As noted in the other answer, since the file is in SSH.COM format, you can convert to openssh format and just open the file to check for ssh-dsa or ssh-rsa:
To convert your SSH.COM key to OpenSSH format use:
ssh-keygen -i -f ssh_key.pub
From the ssh-keygen manpage
-i This option will read an unencrypted private (or public) key
file in SSH2-compatible format and print an OpenSSH
compatible private (or public) key to stdout. ssh-keygen
also reads the `SECSH Public Key File Format'. This option
allows importing keys from several commercial SSH
implementations.
-f Specifies the filename of the key file.
Source: http://landru.uwaterloo.ca/cgi-bin/wiki.pl?OpenSSH_-_SSH.Com_Interoperability
You can use ssh-keygen to get a fingerprint and type out of a .pub file:
ssh-keygen -lf id_rsa.pub