Skip to content

WinCNG test failures #804

Closed
Closed

Description

@dfandrich

Most runs, the WinCNG openssh test runs on Appveyor fail at least one test. The actual test changes (e.g. test_password_auth_fails_with_wrong_username, test_keyboard_interactive_auth_fails_with_wrong_response, test_hostkey) and sometimes the WinCNG runs complete without failures, but it's pretty common that at least one fails. It's often on the
VS2015, WinCNG, Shared, x86_64 build (but maybe because that's one of the first WinCNG builds and the tests stop after the first error), but it happens on other WinCNG builds, too, including VS2013, Static and x86 builds, but always with WinCNG.

The tests fail with the error 9: libssh2_session_handshake failed (-8): Unable to exchange encryption keys. Running the tests with FIXTURE_TRACE_ALL=1 showed this log at the time of failure:

[00:05:13] 5: [libssh2] 0.951417 Transport: Packet type 33 received, length=1078
[00:05:13] 5: [libssh2] 0.951417 Transport: Looking for packet of type: 33
[00:05:13] 5: [libssh2] 0.951417 Key Ex: Server's MD5 Fingerprint: 0c:0e:d1:a5:bb:10:27:5f:76:92:4c:e1:87:ce:5c:5e
[00:05:13] 5: [libssh2] 0.951417 Key Ex: Server's SHA1 Fingerprint: f3:cd:59:e2:91:3f:44:22:b8:0f:7b:0a:82:b2:b8:9e:ae:44:93:87
[00:05:13] 5: [libssh2] 0.951417 Key Ex: Server's SHA256 Fingerprint: kuPaSd88f5moKPUF7YI5OXpdH2KRRFl2D4ePdRD1Y6M=
[00:05:13] 5: [libssh2] 1.045165 Failure Event: -11 - Unable to verify hostkey signature
[00:05:13] 5: [libssh2] 1.045165 Failure Event: -8 - Unrecoverable error exchanging keys
[00:05:13] 5: [libssh2] 1.045165 Failure Event: -8 - Unable to exchange encryption keys
[00:05:13] 5: libssh2_session_handshake failed (-8): Unable to exchange encryption keys
[00:05:13] 5: [libssh2] 1.045165 Transport: Disconnecting: reason=11, desc=test ended, lang=
[00:05:13] 5: => libssh2_transport_write plain (23 bytes)
[00:05:13] 5: 0000: 01 00 00 00 0B 00 00 00  0A 74 65 73 74 20 65 6E : .........test en
[00:05:13] 5: 0010: 64 65 64 00 00 00 00                             : ded....

I wouldn't expect to see this kind of problem (hostkey verification problem) if it were a timing issue. Could it be a reliance on uninitialized memory? It would be interesting to run this on the Windows equivalent to Valgrind or ASAN/UBSAN to see if something shows up.

Activity

  1. mback2k commented on Apr 19, 2023

    @mback2k
    Member

    I have observed this before and think the root cause is the fact that we use random bytes to generate the DH session key and then stuff those bytes which are not necessarily an actual prime into the RSA function to perform the modular exponentiation. I based my implementation of the random bignum function for WinCNG on other backends like mbedTLS which do it similarly.

  2. vszakats commented on Apr 19, 2023

    @vszakats
    Member

    @mback2k Looks like a good lead. After eyeballing these functions closely, I could not find an obvious coding error and the issue applies not just to rsa-sha2, but also to rsa-sha1 (confirming this isn't a fresh regression.)

    (Ultimately BCryptVerifySignature() fails with STATUS_INVALID_SIGNATURE.)

    Do you have idea for a fix?

  3. mback2k commented on Apr 19, 2023

    @mback2k
    Member

    Do you have idea for a fix?

    Unfortunately, no, I don't have an idea for a fix unless we figure out a way to better test the random bytes for actual "prime'ness" or a better way to generate such primes. One idea could be to retry the DH key exchange if it fails with this error, but I am not sure if this would have a negative effect on the security of the key exchange or is even possible from a protocol point of view.

    Since the introduction of BCRYPT_DH_ALGORITHM to WinCNG and libssh2 this problem should at least not affect Windows 8.1+. But of course we purposely test also older versions with AppVeyor and I wanted to keep the old way for backwards compatibility going back to Windows Vista which introduced WinCNG.

  4. vszakats commented on Apr 19, 2023

    @vszakats
    Member
  5. 12 remaining items

  6. vszakats commented on Jun 30, 2026

    @vszakats
    Member

    Potentially related server-side workaround by setting KexAlgorithms -diffie-hellman-group-exchange-sha25:
    logmanoriginal/lvssh2@ae7c1aa

    "tests: Fix unable to exchange encryption keys
    There appears to be a bug in libssh2 when using the WINCNG crypto
    backend, which results in error -8 "Unable to exchange encryption
    keys" during key exchange. The error happens during diffie-hellman
    key-pair generation ("dh key pair generation failed") with method
    'diffie-hellman-group-exchange-sha256'.

    This appears to be a known issue that may be addressed in future
    versions of libssh2: #1370

    The workaround is to disable the offending method in sshd_config."

    diff --git a/tests/docker/openssh-server/config/ssh_host_keys/sshd_config b/tests/docker/openssh-server/config/ssh_host_keys/sshd_config
    index 2eba339..85ce834 100644
    --- a/tests/docker/openssh-server/config/ssh_host_keys/sshd_config
    +++ b/tests/docker/openssh-server/config/ssh_host_keys/sshd_config
    @@ -119,3 +119,8 @@ Subsystem	sftp	internal-sftp
     #	AllowTcpForwarding no
     #	PermitTTY no
     #	ForceCommand cvs server
    +
    +# Disable diffie-hellman-group-exchange-sha256 to prevent
    +# error "dh key pair generation failed" when using libssh2
    +# with WINCNG. This is a workaround for libssh2 bug #1370
    +KexAlgorithms -diffie-hellman-group-exchange-sha256
    Key Ex: Agreed on KEX method: diffie-hellman-group-exchange-sha256
    ...
    Failure Event: -5 - dh key pair generation failed
    Failure Event: -8 - Unrecoverable error exchanging keys
    Failure Event: -8 - Unable to exchange encryption keys
    

    via #1370 (comment)

  7. mback2k commented on Jul 1, 2026

    @mback2k
    Member

    Good find, but finding the root cause would still be interesting.

  8. added
    Windows
    on Sep 2, 2026
  9. added 4 commits that reference this issue on Sep 8, 2026
    f216bbe
    bb8e095
    13efc6d
    5bfa574
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

    Windowsbughelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions