Skip to content

libssh2_session_handshake returns -44 with OpenSSL crypto backend #463

Closed

Description

@SX-80

The function libssh2_session_handshake returns -44 "Unable to ask for ssh-userauth service" in code using OpenSSL crypto backend.

    // ... initialize socket ...

    LIBSSH2_SESSION* session = libssh2_session_init();
    int res = libssh2_session_handshake( session, socket );

    char const* str = nullptr;
    int len = 0;
    libssh2_session_last_error( session, &str, &len, false );

    std::cout << "res: " << res << ", error: " << str << std::endl;

Error is found in function ''_libssh2_cipher_crypt" (openssl.c: 416)
The problem appears after commit in OpenSSL:
openssl/openssl@f7397f0

Version

  • OS: Windows 7
  • libssh2 version [1.9.0_DEV] - master on 2020-04-27
  • openssl version [3.0.0-alpha2-dev] - master on 2020-04-27

Activity

  1. willco007 commented on Apr 27, 2020

    @willco007
    Member

    Have you tried a shipping version of OpenSSL? Seems like a non-zero chance this is a bug in the alpha release of OpenSSL you're using.

  2. SX-80 commented on Apr 27, 2020

    @SX-80
    Author

    Corrected version

    int
    _libssh2_cipher_crypt(_libssh2_cipher_ctx * ctx,
                          _libssh2_cipher_type(algo),
                          int encrypt, unsigned char *block, size_t blocksize)
    {
        unsigned char buf[EVP_MAX_BLOCK_LENGTH];
        int ret;
        (void) algo;
        (void) encrypt;
    
    #ifdef HAVE_OPAQUE_STRUCTS
        ret = EVP_Cipher(*ctx, buf, block, blocksize);
    #else
        ret = EVP_Cipher(ctx, buf, block, blocksize);
    #endif
        if(0 < ret) {
            memcpy(block, buf, blocksize);
        }
        return 0 < ret ? 0 : 1;
    }
    
  3. SX-80 commented on Apr 27, 2020

    @SX-80
    Author

    The changes in OpenSSL library
    изображение

    Now returns 'outl' or '-1'

  4. SX-80 commented on Apr 27, 2020

    @SX-80
    Author

    изображение

  5. willco007 commented on Apr 27, 2020

    @willco007
    Member

    Thanks for tracking this down, I'll drop in a fix.

  6. willco007 commented on Apr 27, 2020

    @willco007
    Member

    Looking at the git commit for OpenSSL it seems like 0 <= ret is success, not just 0 < ret.

  7. willco007 commented on Apr 27, 2020

    @willco007
    Member

    Furthermore, there is this mentioned in their commit:

    EVP_Cipher() returns the amount of encrypted / decrypted bytes, or -1 on failure, if the flag B<EVP_CIPH_FLAG_CUSTOM_CIPHER> is set for the cipher. EVP_Cipher() returns 1 on success or 0 on failure, if the flag B<EVP_CIPH_FLAG_CUSTOM_CIPHER> is not set for the cipher.

    To my knowledge we're not setting this flag so it should be returning 1, not bytes encrypted.

  8. willco007 commented on Apr 27, 2020

    @willco007
    Member

    Looks like this is set by the cipher inside of OpenSSL implicitly. Their release notes: New flag in ciphers: EVP_CIPH_FLAG_CUSTOM_CIPHER. This means the underlying do_cipher function handles all cipher semantics itself including padding and finalisation. Our API call is going to have to check which version of OpenSSL libssh2 is using to handle the possible return code values since 1.1/1.0/3.0 aren't compatible with each other.

  9. SX-80 commented on Apr 28, 2020

    @SX-80
    Author

    Looking at the git commit for OpenSSL it seems like 0 <= ret is success, not just 0 < ret.

    You are right, but "0 < ret" decision works with both versions of OpenSSL with no other code.
    Correct me if I am wrong, but I expect that libssh2 does not call _libssh2_cipher_crypt with blockssize == 0.
    This is one of the simplest solution.

    Our API call is going to have to check which version of OpenSSL libssh2

    Of course the correct solution is to CHECK MACRO and use special code for every possible case.

  10. SX-80 commented on Apr 28, 2020

    @SX-80
    Author

    To my knowledge we're not setting this flag so it should be returning 1

    I am not setting this flag too. But function EVP_Cipher in my application calls ctx->cipher->ccipher.

    изображение

  11. SX-80 commented on Apr 28, 2020

    @SX-80
    Author

    but "0 < ret" decision works with both versions of OpenSSL with no other code

    and always works whether the function calls ctx->cipher->ccipher, ctx->cipher->cupdate or ctx->cipher->cfinal.
    (assumption: libssh2 does not call _libssh2_cipher_crypt with blockssize == 0)

  12. stale commented on Aug 28, 2020

    @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.

  13. stale commented on Jan 2, 2021

    @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.

  14. jmarchil commented on Feb 11, 2021

    @jmarchil

    Is there any follow-up on this issue. I am integrating with openssl 3.0 alpha 11 and I am seeing this issue.

  15. willco007 commented on May 8, 2021

    @willco007
    Member

    A fix was pushed with #463; could all you who are running into this verify this is fixed?

  16. jmarchil commented on May 26, 2021

    @jmarchil

    A fix was pushed with #463; could all you who are running into this verify this is fixed?

    I verified using the latest openssl 3.0 alpha 17 and the problem is resolved

  17. willco007 commented on May 26, 2021

    @willco007
    Member

    @jmarchil Great thanks! I'm going to close this issue.

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