Skip to content

EVP_CipherUpdate breaks support for private keys encrypted with aes256-cbc #764

Closed

Description

@jakob

Describe the bug
OpenSSH private keys encrypted with aes256-cbc no longer work

To Reproduce

  1. Create an ed25519 private key encrypted with aes256-cbc
ssh-keygen -t ed25519 -Z aes256-cbc -f id_ed25519_aes256-cbc -N secretpassphrase
  1. Try to authenticate with that private key:
int status = libssh2_userauth_publickey_fromfile_ex(ssh_session, user, strlen(user), NULL, "id_ed25519_aes256-cbc", "secretpassphrase")

libssh2 fails with status = LIBSSH2_ERROR_FILE and the following error message:

Unable to extract public key from private key file: Wrong passphrase or invalid/unrecognized private key file format

Expected behavior
I expect libssh2 to try to authenticate with the provided key, but it does not work. When I revert commit 17c9c1f it works again.

Keys encrypted with the aes256-ctr (=default for ssh-keygen) work fine.

Version (please complete the following information):

  • OS and version: macOS 12.4
  • libssh2 version: 1.10.1-dev (0ba6e74)
  • crypto backend: OpenSSL (openssl-1.1.1q)

Additional context
Possibly related to #739?

Activity

  1. willco007 commented on Oct 28, 2022

    @willco007
    Member

    @haydenroche5 looks like #629 (comment) is having some fallout given this and #739 both are pointing to that change. Any chance you'd be willing to tweak this and ensure it works with wolfSSL while keeping the original EVP_Cipher call?

  2. jakob commented on Oct 28, 2022

    @jakob
    ContributorAuthor

    If I'm reading the docs correctly, it would seem that EVP_CipherUpdate needs to be followed by EVP_CipherFinal_ex, but I'm not sure where that should go in the code.

  3. willco007 commented on Oct 28, 2022

    @willco007
    Member

    Calling final means you will no longer call update which doesn't seem like the correct solution here. Looking at the OpenSSL code, both EVP_Cipher() and EVP_CipherUpdate() eventually call into ctx->cipher->do_cipher(). The later doing a lot more legwork before calling do_cipher(). It seems like there may be a slight incompatibility in how they work.

  4. willco007 commented on Oct 28, 2022

    @willco007
    Member

    Using EVP_CipherUpdate() requires a call to EVP_CipherFinal() but the way libssh2 is currently built there is no concept of final function on the LIBSSH2_CRYPT_METHOD struct so using the modern API will not work as a drop in replacement.

  5. haydenroche5 commented on Oct 31, 2022

    @haydenroche5
    Contributor

    @haydenroche5 looks like #629 (comment) is having some fallout given this and #739 both are pointing to that change. Any chance you'd be willing to tweak this and ensure it works with wolfSSL while keeping the original EVP_Cipher call?

    Sure. I will look at this this week.

  6. willco007 commented on Oct 31, 2022

    @willco007
    Member

    @haydenroche5 I went ahead and submitted a PR here 3fdcff2, please make sure it works for you with wolfSSL.

  7. ionutbaltariu commented on Nov 2, 2022

    @ionutbaltariu

    I'm sorry if I came here without fully understanding the context.
    It seems that an issue about using ed25519 keys is propagating to a Python module (ssh2-python) that is based on libssh2. ParallelSSH/ssh2-python#178 - here it mentions that the issue might be related to libssh2.

    Can someone tell me if it is somehow related to this issue or if a new issue has to be created? As i mentioned earlier, the technical details might be over my knowledge, especially when C code is involved.

  8. willco007 commented on Nov 2, 2022

    @willco007
    Member

    @xeno-john you're probably looking for the fix in 3fdcff2

  9. ionutbaltariu commented on Nov 3, 2022

    @ionutbaltariu

    @willco007 "This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository."

  10. willco007 commented on Nov 3, 2022

    @willco007
    Member

    @xeno-john That's because I landed it on master shortly after I replied to you and deleted the MR branch.

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