Repository navigation
Description
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
tests: workaround for intermittent first test failures
tests: workaround for intermittent first test failures
tests: workaround for intermittent first test failures
tests: workaround for intermittent first test failures (#832)
ci: speed up static-only build tests on AppVeyor
ci: speed up static-only build tests on AppVeyor
ci: speed up static-only build tests on AppVeyor
ci: speed up static-only build tests on AppVeyor (#868)
vszakats commentedon Apr 18, 2023
Here's a live run reproducing this failure, with trace enabled: https://ci.appveyor.com/project/libssh2org/libssh2/builds/46822855/job/c3fw767k5bm3sgjf?fullLog=true#L19325
Compared to a good run of the same test:
https://ci.appveyor.com/project/libssh2org/libssh2/builds/46821039/job/c3suvs0hu2le8eq8?fullLog=true#L18662
ci: enable FIXTURE_TRACE_ALL_CONNECT for WinCNG tests
mback2k commentedon Apr 19, 2023
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.
vszakats commentedon Apr 19, 2023
@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 withSTATUS_INVALID_SIGNATURE.)Do you have idea for a fix?
mback2k commentedon Apr 19, 2023
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_ALGORITHMto 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.vszakats commentedon Apr 19, 2023
Agreed on the value of Vista-compatibility. This issue is happening also with the Visual Studio 2022 AppVeyor image, with Windows Server 2019 (Windows 10), what could be an explanation?
Three such runs with some logging details (I've since added logging for
BCRYPT_DH_ALGORITHMdetection):https://ci.appveyor.com/project/libssh2org/libssh2/builds/46828624/job/0pto6u8d1hbxokb9
https://ci.appveyor.com/project/libssh2org/libssh2/builds/46827388/job/4e1ivdrjeepp1g8c
https://ci.appveyor.com/project/libssh2org/libssh2/builds/46826769/job/v4rrlp9vvsfhcioy
12 remaining items
ci: speed up static-only build tests on AppVeyor (libssh2#868)
ci: enable FIXTURE_TRACE_ALL_CONNECT for WinCNG tests
ci: add Windows Server 2016 into the test mix
tests: retry KEX failures when using the WinCNG backend
tests: Retry session initialization on key exchange failure (WinCNG)
runtests: fix test key format for libssh2 WinCNG (and others)
runtests: fix test key format for libssh2 WinCNG (and others)
vszakats commentedon Jun 30, 2026
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."
via #1370 (comment)
mback2k commentedon Jul 1, 2026
Good find, but finding the root cause would still be interesting.
wincng: normalize fixed-width DH values before mpint encoding
GHA: test WinCNG SCP/SFTP via curl runtests
tests: drop WinCNG retry workarounds
tests: drop WinCNG retry workarounds