You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Can't distinguish wrong key from wrong passphrase #608
Describe the bug
When you authenticate using libssh2_userauth_publickey_fromfile it fails with LIBSSH2_ERROR_FILE when the passphrase provided for an encrypted private key is wrong, and also for many other reasons. This makes it impossible to build a UI that tells the user what to do. If the file is malformed, for example, there's no point prompting them to enter a passphrase.
To Reproduce
Call libssh2_userauth_publickey_fromfile giving it the path to an OpenSSH-formatted private key that is encrypted with a passphrase along with an empty passphrase. Note that it returns LIBSSH2_ERROR_FILE.
Then call it with the path to a malformed private key file (like /etc/passwd). Note that it returns LIBSSH2_ERROR_FILE.
Expected behavior
There should be a distinct error code for when the passphrase is wrong so that the application can prompt the user to enter a correct one.
For an OpenSSH-format private key, there is no simple way to tell before authenticating whether the private key is encrypted. Therefore it is undecidable whether to prompt the user for a passphrase.
Version (please complete the following information):
OS: macOS 11.4
libssh2 version 1.9.0
Additional context _libssh2_pub_priv_openssh_keyfile returns LIBSSH2_ERROR_KEYFILE_AUTH_FAILEDbut _libssh2_pub_priv_keyfile changes the return code to LIBSSH2_ERROR_FILE which is used for many other purposes, making it impossible to tell whether the user should re-enter a passphrase.
Note that this is especially a problem specifically for OpenSSH-format private keys. Other kinds of private key indicate that they are encrypted in easier-to-parse ways.
IIRC this is because OpenSSL returns a pretty generic error code when decoding OpenSSL style keys in a variety of situations. So to keep the API consistent it returns LIBSSH2_ERROR_FILE. In my client application, we do a bit of key pre-processing to determine if the key can be decoded, is encrypted, etc before passing it into the libssh2 API.
Clients should not be asked to parse a key file, base64-decode it, ASN.1-decode it, and so on to determine if a key is encrypted. This code already exists in libssh2 and is unlikely to be reimplemented correctly by its users.
I actually wrote a function to guess if a key is encrypted, but I completely missed the case of OpenSSH-style keys. This proves that at least I am incapable of doing this right, and the result is a bug that's been in my app for probably years.
Based on what I see of _libssh2_pub_priv_keyfile it will either use the result of PEM_read_bio_PrivateKey or else _libssh2_openssh_pem_parse_data.
PEM_read_bio_PrivateKey doesn't seem to return a useful error, but it's also easy to tell if a PEM file has an encrypted private key: it uses -----BEGIN ENCRYPTED PRIVATE KEY-----. or a header like Proc-Type: 4,ENCRYPTED. Maybe there's more to this than I realize, though? I'm definitely not an expert on key formats.
It looks like you're on the right track. There is a decent amount of logic parsing key types, if it's encrypted, etc and which types are supported with which backend, etc. I'm not too keen on moving this into the library, but if you're game for it I'd happily review a PR for any changes you'd like to make.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Activity
Assume openssh private keys are encrypted since it's impossible to te…
willco007 commentedon Jun 2, 2021
IIRC this is because OpenSSL returns a pretty generic error code when decoding OpenSSL style keys in a variety of situations. So to keep the API consistent it returns
LIBSSH2_ERROR_FILE. In my client application, we do a bit of key pre-processing to determine if the key can be decoded, is encrypted, etc before passing it into the libssh2 API.gnachman commentedon Jun 3, 2021
Clients should not be asked to parse a key file, base64-decode it, ASN.1-decode it, and so on to determine if a key is encrypted. This code already exists in libssh2 and is unlikely to be reimplemented correctly by its users.
I actually wrote a function to guess if a key is encrypted, but I completely missed the case of OpenSSH-style keys. This proves that at least I am incapable of doing this right, and the result is a bug that's been in my app for probably years.
Based on what I see of
_libssh2_pub_priv_keyfileit will either use the result ofPEM_read_bio_PrivateKeyor else_libssh2_openssh_pem_parse_data.PEM_read_bio_PrivateKeydoesn't seem to return a useful error, but it's also easy to tell if a PEM file has an encrypted private key: it uses-----BEGIN ENCRYPTED PRIVATE KEY-----. or a header likeProc-Type: 4,ENCRYPTED. Maybe there's more to this than I realize, though? I'm definitely not an expert on key formats.willco007 commentedon Nov 18, 2021
It looks like you're on the right track. There is a decent amount of logic parsing key types, if it's encrypted, etc and which types are supported with which backend, etc. I'm not too keen on moving this into the library, but if you're game for it I'd happily review a PR for any changes you'd like to make.
stale commentedon Apr 16, 2022
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
vszakats commentedon Apr 25, 2023
This PR addresses this issue: #789
openssl: fix misleading error message if wrong passphrase (libssh2#789)
openssl: fix misleading error message if wrong passphrase (libssh2#789)