Skip to content

[libssh 1.10.1_dev] Error "Invalid MAC received" when building without HAVE_EVP_AES_128_CTR #739

Closed

Description

@jeroen

Using libgit2 + libssh2 + openssl 1.1.1 compiled with mingw-w64. After upgrading from 1.10.0 to 1.10.1_dev (master branch) cloning git+ssh repos from GitHub fails with:

Error in libgit2::git_clone : failed to start SSH session: Invalid MAC received

Reverting to libssh2 1.10.1 fixes the problem, so there seems to be a regression. Afaict, the issue only appears on Windows. What recent changes could be causing this?

Activity

  1. jeroen commented on Sep 6, 2022

    @jeroen
    ContributorAuthor

    Did some debugging and it indeed fails here with 2 completely different mac addresses:

    libssh2/src/transport.c

    Lines 191 to 194 in 6c59eea

    if(memcmp(macbuf, p->payload + session->fullpacket_payload_len,
    session->remote.mac->mac_len)) {
    session->fullpacket_macstate = LIBSSH2_MAC_INVALID;
    }

    One thing I noticed comparing the (working) 1.10.0 build with the dev version is that the value of fullpacket_payload_len is different. In the stable branch it is always 27, but in the dev version it is much larger, I have seen 59 or 539 depending the server.

  2. jeroen commented on Sep 6, 2022

    @jeroen
    ContributorAuthor

    Bisected to 17c9c1f (pr #629).
    cc @haydenroche5 @bagder perhaps the change to EVP_CipherUpdate was not without side effects?

  3. willco007 commented on Sep 6, 2022

    @willco007
    Member

    @jeroen if you revert the EVP_CipherUpdate change does it work for you? We do run tests on Windows + OpenSSL which should catch a regression in transport like this unless it's some odd interplay with mingw.

  4. jeroen commented on Sep 6, 2022

    @jeroen
    ContributorAuthor

    Yes after reverting the EVP_CipherUpdate change it works again. Do the tests actually connect to an ssh server? I am also surprised to see this problem only on Windows; we specifically build with gcc and openssl to get the same behaviour as on mac/linux.

  5. willco007 commented on Sep 6, 2022

    @willco007
    Member

    If I'm reading this correctly, the API only appears to used with arcfour128 cipher which is disabled in modern OpenSSH and isn't hit with the unit tests.

  6. jeroen commented on Sep 6, 2022

    @jeroen
    ContributorAuthor

    GitHub and other git servers seem to be running: OpenSSH_8.6p1, LibreSSL 3.3.6. I wasn't able to connect to any of them using the master branch.

  7. willco007 commented on Sep 6, 2022

    @willco007
    Member

    Are you specifying arcfour128 explicitly? Have you reverted that entire commit, or just that one line? Assuredly the GitHub servers aren't using it by default. Could you attach a trace from a failed connection and a successful one? Thanks!

  8. jeroen commented on Sep 6, 2022

    @jeroen
    ContributorAuthor

    One thing that makes this difficult to debug is that the LIBSSH2_SESSION is started by libgit2, so I don't have much control over it. Is there an envvar I can set to get a trace?

    I am not setting arcfour128 but perhaps libgit2 does things (though I don't see where this would be happening).

  9. willco007 commented on Sep 6, 2022

    @willco007
    Member

    On the session you call libssh2_trace with say, these flags LIBSSH2_TRACE_KEX | LIBSSH2_TRACE_AUTH | LIBSSH2_TRACE_PUBLICKEY | LIBSSH2_TRACE_ERROR and then set a trace handler with libssh2_trace_sethandler and log the output some place.

  10. jeroen commented on Sep 7, 2022

    @jeroen
    ContributorAuthor

    OK so I am testing by testing the first part of example/ssh2.c on github.com ./ssh2.exe 140.82.121.4.

    With the master branch I get:

    [libssh2] 0.441534 Key Ex: Sent KEX: curve25519-sha256,[email protected],ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1,diffie-hellman-group-exchange-sha1,ext-info-c
    [libssh2] 0.457138 Key Ex: Sent HOSTKEY: ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,[email protected],[email protected],[email protected],ssh-ed25519,rsa-sha2-512,rsa-sha2-256,ssh-rsa,[email protected],ssh-dss
    [libssh2] 0.457138 Key Ex: Sent CRYPT_CS: aes128-ctr,aes192-ctr,aes256-ctr,aes256-cbc,[email protected],aes192-cbc,aes128-cbc,blowfish-cbc,arcfour128,arcfour,cast128-cbc,3des-cbc
    [libssh2] 0.457138 Key Ex: Sent CRYPT_SC: aes128-ctr,aes192-ctr,aes256-ctr,aes256-cbc,[email protected],aes192-cbc,aes128-cbc,blowfish-cbc,arcfour128,arcfour,cast128-cbc,3des-cbc
    [libssh2] 0.457138 Key Ex: Sent MAC_CS: hmac-sha2-256,hmac-sha2-512,hmac-sha1,hmac-sha1-96,hmac-md5,hmac-md5-96,hmac-ripemd160,[email protected]
    [libssh2] 0.457138 Key Ex: Sent MAC_SC: hmac-sha2-256,hmac-sha2-512,hmac-sha1,hmac-sha1-96,hmac-md5,hmac-md5-96,hmac-ripemd160,[email protected]
    [libssh2] 0.457138 Key Ex: Sent COMP_CS: none
    [libssh2] 0.457138 Key Ex: Sent COMP_SC: none
    [libssh2] 0.457138 Key Ex: Sent LANG_CS:
    [libssh2] 0.457138 Key Ex: Sent LANG_SC:
    [libssh2] 0.457138 Key Ex: Agreed on KEX method: curve25519-sha256
    [libssh2] 0.457138 Key Ex: Agreed on HOSTKEY method: ecdsa-sha2-nistp256
    [libssh2] 0.457138 Key Ex: Agreed on CRYPT_CS method: aes128-ctr
    [libssh2] 0.457138 Key Ex: Agreed on CRYPT_SC method: aes128-ctr
    [libssh2] 0.457138 Key Ex: Agreed on MAC_CS method: hmac-sha2-256
    [libssh2] 0.457138 Key Ex: Agreed on MAC_SC method: hmac-sha2-256
    [libssh2] 0.457138 Key Ex: Agreed on COMP_CS method: none
    [libssh2] 0.457138 Key Ex: Agreed on COMP_SC method: none
    [libssh2] 0.457138 Key Ex: Initiating curve25519 SHA2
    [libssh2] 0.581967 Key Ex: Server's MD5 Fingerprint: 7b:99:81:1e:4c:91:a5:0d:5a:2e:2e:80:13:3f:24:ca
    [libssh2] 0.581967 Key Ex: Server's SHA1 Fingerprint: 33:58:ab:5d:d3:e3:06:c4:61:c8:40:f7:48:7e:93:b6:97:e3:06:00
    [libssh2] 0.581967 Key Ex: Server's SHA256 Fingerprint: p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM=
    [libssh2] 0.581967 Key Ex: Received NEWKEYS message
    [libssh2] 0.581967 Key Ex: session_id calculated
    [libssh2] 0.581967 Key Ex: Client to Server IV and Key calculated
    [libssh2] 0.581967 Key Ex: Server to Client IV and Key calculated
    [libssh2] 0.581967 Key Ex: Client to Server HMAC Key calculated
    [libssh2] 0.581967 Key Ex: Server to Client HMAC Key calculated
    [libssh2] 0.581967 Key Ex: Client to Server compression initialized
    [libssh2] 0.597570 Key Ex: Server to Client compression initialized
    [libssh2] 0.691192 Failure Event: -4 - Invalid MAC received
    

    And after reverting 17c9c1f we get:

    [libssh2] 0.805358 Key Ex: Sent KEX: curve25519-sha256,[email protected],ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1,diffie-hellman-group-exchange-sha1,ext-info-c
    [libssh2] 0.805358 Key Ex: Sent HOSTKEY: ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,[email protected],[email protected],[email protected],ssh-ed25519,rsa-sha2-512,rsa-sha2-256,ssh-rsa,[email protected],ssh-dss
    [libssh2] 0.805358 Key Ex: Sent CRYPT_CS: aes128-ctr,aes192-ctr,aes256-ctr,aes256-cbc,[email protected],aes192-cbc,aes128-cbc,blowfish-cbc,arcfour128,arcfour,cast128-cbc,3des-cbc
    [libssh2] 0.805358 Key Ex: Sent CRYPT_SC: aes128-ctr,aes192-ctr,aes256-ctr,aes256-cbc,[email protected],aes192-cbc,aes128-cbc,blowfish-cbc,arcfour128,arcfour,cast128-cbc,3des-cbc
    [libssh2] 0.805358 Key Ex: Sent MAC_CS: hmac-sha2-256,hmac-sha2-512,hmac-sha1,hmac-sha1-96,hmac-md5,hmac-md5-96,hmac-ripemd160,[email protected]
    [libssh2] 0.805358 Key Ex: Sent MAC_SC: hmac-sha2-256,hmac-sha2-512,hmac-sha1,hmac-sha1-96,hmac-md5,hmac-md5-96,hmac-ripemd160,[email protected]
    [libssh2] 0.805358 Key Ex: Sent COMP_CS: none
    [libssh2] 0.805358 Key Ex: Sent COMP_SC: none
    [libssh2] 0.805358 Key Ex: Sent LANG_CS:
    [libssh2] 0.805358 Key Ex: Sent LANG_SC:
    [libssh2] 0.805358 Key Ex: Agreed on KEX method: curve25519-sha256
    [libssh2] 0.805358 Key Ex: Agreed on HOSTKEY method: ecdsa-sha2-nistp256
    [libssh2] 0.805358 Key Ex: Agreed on CRYPT_CS method: aes128-ctr
    [libssh2] 0.805358 Key Ex: Agreed on CRYPT_SC method: aes128-ctr
    [libssh2] 0.805358 Key Ex: Agreed on MAC_CS method: hmac-sha2-256
    [libssh2] 0.805358 Key Ex: Agreed on MAC_SC method: hmac-sha2-256
    [libssh2] 0.805358 Key Ex: Agreed on COMP_CS method: none
    [libssh2] 0.805358 Key Ex: Agreed on COMP_SC method: none
    [libssh2] 0.805358 Key Ex: Initiating curve25519 SHA2
    [libssh2] 0.945790 Key Ex: Server's MD5 Fingerprint: 7b:99:81:1e:4c:91:a5:0d:5a:2e:2e:80:13:3f:24:ca
    [libssh2] 0.945790 Key Ex: Server's SHA1 Fingerprint: 33:58:ab:5d:d3:e3:06:c4:61:c8:40:f7:48:7e:93:b6:97:e3:06:00
    [libssh2] 0.945790 Key Ex: Server's SHA256 Fingerprint: p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM=
    [libssh2] 0.945790 Key Ex: Received NEWKEYS message
    [libssh2] 0.945790 Key Ex: session_id calculated
    [libssh2] 0.945790 Key Ex: Client to Server IV and Key calculated
    [libssh2] 0.945790 Key Ex: Server to Client IV and Key calculated
    [libssh2] 0.945790 Key Ex: Client to Server HMAC Key calculated
    [libssh2] 0.945790 Key Ex: Server to Client HMAC Key calculated
    [libssh2] 0.945790 Key Ex: Client to Server compression initialized
    [libssh2] 0.945790 Key Ex: Server to Client compression initialized
    [libssh2] 1.055015 Key Ex: Server to Client extension server-sig-algs: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],ssh-ed25519,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,ecdsa-sha2-nistp256,rsa-sha2-512,rsa-sha2-256,ssh-rsa
    Fingerprint: 33 58 AB 5D D3 E3 06 C4 61 C8 40 F7 48 7E 93 B6 97 E3 06 00
    ...
    
  11. willco007 commented on Sep 7, 2022

    @willco007
    Member

    Could you check the values for HAVE_OPAQUE_STRUCTS, LIBSSH2_WOLFSSL and LIBSSH2_OPENSSL? Maybe they aren't being defined correctly for whatever reason.

  12. jeroen commented on Sep 8, 2022

    @jeroen
    ContributorAuthor

    Did some more debugging, was able to reproduce the issue with some systems and not others. It turned out that on broken systems, HAVE_EVP_AES_128_CTR is not set in src/libssh2_config, probably because these have a static build of openssl.

    So I guess the problem is now as follows:

    • autoconf is not properly detecting EVP_aes_128_ctr when openssl is only available as static library
    • the libssh2 fallback method for EVP_aes_128_ctr is broken
    • somehow this triggers a problem in EVP_CipherUpdate that wasn't there before 17c9c1f

    I think you should be able to repro the issue on other systems by unsetting HAVE_EVP_AES_128_CTR in libssh2_config

  13. jeroen commented on Sep 9, 2022

    @jeroen
    ContributorAuthor

    I think the best solution here is to remove the option to build without HAVE_EVP_AES_128_CTR. I think all openssl versions have this function, the only reason why it is not set is that autoconf fails to detect it.

  14. willco007 commented on Sep 9, 2022

    @willco007
    Member

    Are you overriding the location of OpenSSL and/or have you verified it's finding the correct version? If you build using CMake does it configure it correctly?

  15. jeroen commented on Sep 9, 2022

    @jeroen
    ContributorAuthor

    We build with autoconf, which fails the feature test for HAVE_EVP_AES_128_CTR. The reason is that a static openssl requires extra linking flags on Windows:

    # pkg-config --libs-only-l --static openssl
    -lssl -lcrypto -lws2_32 -lgdi32 -lcrypt32 -lws2_32

    However, your configure script is not consulting pkg-config and assuming only libssl and libcrypto are needed. Therefore it fails the feature test for EVP_AES_128_CTR with some linking errors, even though EVP_aes_128_ctr is really there.

    The resulting configuration then has OpenSSL (AES-CTR: no) like this:

      version:          1.10.1_DEV
      Host type:        i686-w64-mingw32
      Install prefix:   /mingw32
      Compiler:         gcc
      Compiler flags:   -march=pentium4 -mtune=generic -O2 -pipe -DLIBSSH2_WIN32
      Library types:    Shared=no, Static=yes
      Crypto library:   OpenSSL (AES-CTR: no)
      Clear memory:     unsupported
      Debug build:      no
      Build examples:   yes
      Path to sshd:     /usr/bin/sshd (only for self-tests)
      zlib compression: yes
    

    I suppose the Crypto library: OpenSSL (AES-CTR: no) part means that it will use openssl but not EVP_aes_128_ctr, but this implementation somehow seems broken.

    I would suggest to just remove the feature test and assume EVP_aes_128_ctr is available when openssl is used. I haven't come across a version of openssl that does not have this.

    As a workaround, I now build libssh2 like so:

    export LIBS=$(pkg-config --libs-only-l --static openssl)
    ./configure --with-crypto=openssl

    And that results in a working configuration with

      Compiler flags:   -march=x86-64 -mtune=generic -O2 -pipe -DLIBSSH2_WIN32
      Library types:    Shared=no, Static=yes
      Crypto library:   OpenSSL (AES-CTR: yes)
    ...
    
    
  16. 4 remaining items

  17. vszakats commented on Mar 19, 2023

    @vszakats
    Member

    HAVE_EVP_AES_128_CTR detection was fixed in 31fb886 PR #811.

    That said, I agree we should make this a requirement, without detection. This was available at least back in OpenSSL 1.0.2.

  18. added 4 commits that reference this issue on Mar 19, 2023
    bb7f986
    76899c4
    1bb303e
    480d6f9
  19. vszakats commented on Mar 19, 2023

    @vszakats
    Member

    Opened PR to always enable this option without detection, for all build methods: #860

  20. added 3 commits that reference this issue on Mar 19, 2023
    fb2600c
    d224493
    00fd5f2
  21. added a commit that references this issue on Mar 28, 2024
    ec0a51d
  22. added a commit that references this issue on Jul 14, 2024
    4037032
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