Skip to content

Can't distinguish wrong key from wrong passphrase #608

Closed

Description

@gnachman

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.

Activity

  1. willco007 commented on Jun 2, 2021

    @willco007
    Member

    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.

  2. gnachman commented on Jun 3, 2021

    @gnachman
    Author

    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.

  3. willco007 commented on Nov 18, 2021

    @willco007
    Member

    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.

  4. stale commented on Apr 16, 2022

    @stale

    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.

  5. vszakats commented on Apr 25, 2023

    @vszakats
    Member

    This PR addresses this issue: #789

  6. added a commit that references this issue on Mar 28, 2024
    097c8f0
  7. added a commit that references this issue on Jul 14, 2024
    271392a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions