Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why do I need public key for auth? #350

Closed
T3rm1 opened this issue Aug 16, 2017 · 11 comments
Closed

Why do I need public key for auth? #350

T3rm1 opened this issue Aug 16, 2017 · 11 comments

Comments

@T3rm1
Copy link

T3rm1 commented Aug 16, 2017

Isn't it possible to derive the public key from the private key? Why do I need to set the public key in the KeyProvier? It would be easier to only set the private key.

@hierynomus
Copy link
Owner

hierynomus commented Aug 16, 2017 via email

@T3rm1
Copy link
Author

T3rm1 commented Aug 17, 2017

sshClient.loadKeys(keypair) requires a KeyPair. In order to auth I have to set the private key and the public key of it. If I'm not mistaken the public key can be derived from the private key. If you do that in your library the user wouldn't need to load the public key first and it would be enough to set only the private key.

@hierynomus
Copy link
Owner

@T3rm1, if you look at the methods to authenticate, there are actually multiple ways that handle this exact usecase for you...

  • SSHClient.authPublicKey(String username): Uses the default named keys in the users' home-dir
  • SSHClient.authPublicKey(String username, Iterable<KeyProvider>): Takes a KeyProvider which can be instantiated with SSHClient.loadKeys(...)

The SSHClient.loadKeys() method has a number of alternative interesting overloads:

  • loadKeys(String location): Takes the location of an unencrypted private key file
  • loadKeys(String location, PasswordFinder/char[]/String password): Takes the location of an encrypted private key file
  • loadKeys(String privKey, String pubKey, PasswordFinder): Takes the location of both the private and public key and the password needed to unlock the encrypted private key.
  • loadKeys(KeyPair kp): Takes an already loaded keypair to use.

As you see there are a number of methods which do all the magic for you, adn there is one that allows you to override the defaults. So I really do no see any problem or something that is missing.

@T3rm1
Copy link
Author

T3rm1 commented Aug 18, 2017

I was talking about the publickey auth mechanism. Sorry if that wasn't obvious. You named some methods suitable for that method. All of the method require that you load the public and the private key.
Can you explain to me, why in any of these methods, it is required to specify the public key?

@hierynomus
Copy link
Owner

I'm not so sure what you mean still. Most of the methods I mentioned (which all do public key auth) do not require you to load the public key. It will take care of that for you.

Public keys are sometimes easily guessable from the private key, but this is only in specific cases. More generally, most private key encodings actually also contain the public key in an encoded format (so no need to do the mathematical guessing game). But this is not for all and always the case.

@T3rm1
Copy link
Author

T3rm1 commented Aug 22, 2017

Ok, check out this link
I have my private key in a file that is not located at the default location. So I need to use either loadKeys(KeyPair kp) or SSHClient.authPublicKey(String username, Iterable<KeyProvider>). Both methods require a public key! This doesn't make sense. Why did you design these methods to require a public key when, in reality, only the private key is really needed?

@hierynomus
Copy link
Owner

hierynomus commented Aug 22, 2017 via email

@T3rm1
Copy link
Author

T3rm1 commented Aug 23, 2017

Oh okay, I didn't know that. Since OpenSSH is the defacto standard implementation of SSH, what other kind of keys do you support?

@hierynomus
Copy link
Owner

We support a number of key files:

  • OpenSSHv1
  • OpenSSH
  • PKCS#5
  • PKCS#8
  • Putty

But also be aware (I forgot that was possible) that you may set the second parameter of loadKeys to null (ie. the publicKey), which means there is no separate public key. It will then try to use the encoded key if possible.

@dixon961
Copy link

When I try to connect by SSH to server i can input in terminal:
ssh -i ./private_key root@IP (IP == ip of my server)
where private_key is a file where only private key is written (i created it by myself, just paste known private key)

Using your library i want to connect to server only having serverIp and private key as String.
I can use OpenSSHKeyFile and my private_key file and it works well. How can I use only String without creating a file? Want to write something like:
ssh.authPrivateKey(String username, String privateKey)

@hierynomus
Copy link
Owner

@dixon961 You can implement a new type of KeyProvider which could take the privatekey as a byte array or base64 encoded string.

Please note that htis issue was closed. If you want to raise a feature request, please submit a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants