Skip to content

Encryption failure during handshake when using libssh2 and wolfssl #1299

Closed

Description

@brucsc

I'm getting a -44 error code and message of "Unable to ask for ssh-userauth service" when using wolfssl as the encryption engine during ssh handshake. I've tracked the error to this code on line 584 of openssl.c:

if(IS_LAST(firstlast)) {
  /* This is the last block.
     encrypt: compute tag, if applicable
     decrypt: verify tag, if applicable
     in!=NULL is equivalent to EVP_CipherUpdate
     in==NULL is equivalent to EVP_CipherFinal */
#ifdef HAVE_OPAQUE_STRUCTS
  ret = EVP_Cipher(*ctx, NULL, NULL, 0); /* final */
#else
  ret = EVP_Cipher(ctx, NULL, NULL, 0); /* final */
#endif

The comment indicates the expectation that EVP_CipherFinal should be equivalent to EVP_Cipher when finalizing the encryption. The EVP_Cipher method resolves to wolfSSL_EVP_Cipher on line 7080 of the wolfssl/wolfcrypt/src/evp.c file. There a check of NULL on the incoming buffers returns a WOLFSSL_FATAL_ERROR. After modifying the code to follow the comment, EVP_CipherFinal is used instead of EVP_Cipher, then the encryption seems to work as expected:

int final_length = 0;
ret = EVP_CipherFinal(*ctx, buf + aadlen, &final_length); /* final */

I do not know enough about these internals to know if this is the correct solution or not and would appreciate any assistance in getting this fixed. The comment suggests a break in the interface expectations with wolfssl.

To Reproduce
Extract libssh2 1.11.0, wolfssl 5.6.4. Build wolfssl. Build libssh2 to use wolfssl. Attempt to connect to an ssh server. In my case OpenSSH 8.9.

Expected behavior
For the encryption to work and the ssh handshake to complete.

Version

  • OS and version: Linux Ubuntu 22.04.3 LTS
  • libssh2 version: 1.11.0
  • crypto backend and version: wolfSSL 5.6.4

Additional context
Add any other context about the problem here.

Activity

  1. vszakats commented on Dec 26, 2023

    @vszakats
    Member

    initial report: #1020

  2. vszakats commented on Apr 5, 2024

    @vszakats
    Member

    @brucsc: Can you make PR out of your fix proposal?

    You can also find earlier discussions about this in #1020, with
    the patch necessary to re-enable CI tests for wolfSSL, in this comment:
    #1020 (comment)

  3. added 2 commits that reference this issue on May 24, 2024
    6acf5a8
    8ea4ced
  4. kareem-wolfssl commented on May 24, 2024

    @kareem-wolfssl

    This should be fixed with my colleague's PR here: wolfSSL/wolfssl#7143
    This PR was not merged until wolfSSL 5.7.0, please try updating to 5.7.0 and try again.

  5. vszakats commented on May 24, 2024

    @vszakats
    Member

    @kareem-wolfssl Can you confirm this is the same issue as #1020, or are they different?

  6. kareem-wolfssl commented on May 24, 2024

    @kareem-wolfssl

    @vszakats as best as I can tell this is the same issue, #1020 was also caused by the line EVP_Cipher(*ctx, NULL, NULL, 0);. So the linked PR/5.7.0 should resolve both issues.

  7. vszakats commented on May 24, 2024

    @vszakats
    Member

    Thanks, also for the info about the fix!

    I prefer @MichaelBuckley's PR, which replaces that API call with one that works with all wolfSSL versions. The Linux CI runner is still at 5.2.0.

  8. brucsc commented on May 24, 2024

    @brucsc
    Author

    Great, I was very uncomfortable with what I presented in the initial question since it seemed like I was covering over an issue instead of fixing it.

  9. added a commit that references this issue on Jun 24, 2024
    a5b0fac
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions