Repository navigation
Conversation
vszakats
left a comment
There was a problem hiding this comment.
Can you say how did you stress test connections locally?
|
Thanks for the review. I pushed
Local checks after that change:
For the connection stress testing, I used a local Docker OpenSSH server reached over loopback from a small Windows handshake driver linked to the MSVC WinCNG build. Every attempt created a fresh TCP socket and The unpatched WinCNG control failed intermittently in both serial and one-session-per-process runs, so concurrency was not required. Normalizing only the shared secret still left 41 failures in 10,000 handshakes; instrumentation on those remaining failures showed a redundant leading zero in the exported client public value. With both the public value and shared secret normalized, the runs completed as follows:
The same local server path was also clean with the Windows OpenSSH client (2,000/2,000), and the WSL/OpenSSL control completed its tested KEX variants without failures. The new upstream CI run is in progress. Please let me know if you would prefer the stress harness to be reduced into a repository test in a follow-up. |
|
Thanks for the fixes! Just curious, are you real person or am I talking I think this is the moment when libssh2 would need to have WinCNG If you have suggestions for a WnCNG CI test, I'd be useful to see it, yes. Refs: @mback2k: Your thoughts on this fix proposal? |
|
I'm a real person and the owner of this account. I'm a native Korean speaker and not fluent in English, so I use an LLM to translate my comments into English. The investigation and patch were AI-assisted. The builds, stress runs, hashes, and CI results I reported are outputs from commands actually executed in my environment, not generated estimates. I make sure I understand the root cause and the purpose of each change, verify the technical claims with reproducible tests, and take responsibility for the submission. I'd be happy to help restore WinCNG runtime coverage in CI. Since you already have an idea and may have strong preferences about the design, could you outline the topology and constraints you have in mind, such as the runner and SSH server arrangement, minimum KEX coverage, acceptable runtime, and whether retries should remain only as a temporary comparison? I'll review those constraints and the references you shared before preparing a focused follow-up proposal or writing code. |
Short history: We traditionally had WinCNG tests running in AppVeyor CI, Preferably WinCNG tests should reuse the existing test infrastructure [ A long shot, and this isn't the goal within this PR, but I'd much prefer As for my idea on running tests: In curl we do run libssh2 tests via curl's |
There was a problem hiding this comment.
🟡 Changes recommended
The fallback DH path must also normalize successful results before encoding.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Normalizes fixed-width WinCNG DH/ECDH values before SSH mpint serialization.
Changes:
- Adds in-place bignum normalization.
- Normalizes native DH/ECDH outputs.
- Adds regression coverage for leading-zero handling.
File summaries
| File | Review |
|---|---|
tests/test_simple.c |
Test does not exercise the affected WinCNG output path. |
src/wincng.c |
Fallback DH public values remain unnormalized. |
Review details
Suppressed comments (2)
src/wincng.c:3230
- This handles only successful
BCRYPT_KDF_RAW_SECRETderivation. On systems where that KDF returnsSTATUS_NOT_SUPPORTED(documented above as pre-Windows 10), execution jumps to the fallback atwincng.c:3249-3251, which returns the fixed-widthwcng_bn_mod_exp()result without normalization. The original intermittent non-canonical shared-secret mpint therefore remains on the supported fallback path; normalize successful fallback results as well.
wcng_bn_normalize(secret);
tests/test_simple.c:305
- This test does not exercise the reported regression: the previous
ssh2_bn_from_bin()implementation already removed leading zero bytes, so the test still passes if all three new normalization calls are reverted. Please cover a fixed-width value produced by the WinCNG DH/ECDH output path (including the updated logical length and cleared tail), so the test fails without this fix.
if(ssh2_bn_from_bin(bn, fixed_width, sizeof(fixed_width))) {
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| memcpy(pub->bignum, (unsigned char *)dh_key_blob + | ||
| sizeof(*dh_key_blob) + 2 * dh_key_blob->cbKey, | ||
| dh_key_blob->cbKey); | ||
| wcng_bn_normalize(pub); |
mback2k
left a comment
There was a problem hiding this comment.
This PR itself looks good to me and may finally fix the random issues we had, but of course this will require longer testing as the removed part in ssh2_bn_from_bin had the same purpose which is now replaced and generalized.
| if(!bn || !bn->bignum || !bn->length) | ||
| return; | ||
|
|
||
| while(offset + 1 < bn->length && !bn->bignum[offset]) |
There was a problem hiding this comment.
I think the conditions here should be switched around as the bignum being zero is the primary condition.
mback2k
left a comment
There was a problem hiding this comment.
Please implement the requested changes.
|
I also agree with @vszakats regarding the long-term wishes for a better Windows testsuite, although I would really like to fix the AppVeyor CI, I currently have no time or clue how to do that. WSL could also be an option on GHA to run the Windows tests against a proper Linux-based OpenSSH server as the Windows-version is not completely compatible. |
|
Thanks, @vszakats and @mback2k. I've pushed
The native DH and P-256 ECDH shared-secret tests use fixed vectors. The native DH public-value check compares a fresh key with its CNG export, so detecting omitted normalization there depends on a leading-zero sample. The test itself does not retry or require such a sample. In local mutation checks, six omissions were detected on the first run; the native public-value omission was detected on the 33rd sample. All 92 existing CI checks for this commit passed. The WinCNG SSH runtime job we discussed has not been added yet. For that job, I propose building a pinned curl revision against this PR's WinCNG libssh2 on a GHA Windows x64 runner and running curl's SCP/SFTP tests. I'd first check the WSL/Linux OpenSSH option on the same runner against curl's harness and your constraints on setup complexity, third-party Actions, and PowerShell. Would you prefer this CI work in this PR, or as a follow-up PR? |
Just to clarify, the curl runtests method does not require or use WSL. (edit: it might use it, but it seems to be a deep rabbit hole, which
I think it'd be easier to handle in a separate PR, then rebase this one, |
| rc |= test_ssh2_dh_native_normalization(); | ||
| #if LIBSSH2_ECDSA | ||
| rc |= test_ssh2_ecdh_normalization(session); | ||
| #endif |
There was a problem hiding this comment.
Can we avoid the backend-specific (esp. Windows-specific) code in tests?
Either test the primitives for all backends, or drop things from there that are
WinCNG-specific.
I think the major issue with AppVeyor CI is that it makes the PR push cycles GHA works much better in this regard, by running jobs in parallel, spinning To emulate pre-10 Windows versions (only offered by AppVeyor CI) we can |
|
Testing the curl runtests idea here: #2590 I suspect this was the WinCNG flaky fail replicated, as spotted in the 2nd run: https://github.com/libssh2/libssh2/actions/runs/33995660051/job/101385680240#step:12:2364 |
|
With this PR applied as of e95a1ee, Runs fine on the next run: |
|
Thanks for testing this in #2590. I've pushed |
…_from_bin()` To better hide backend-specific differences and t bring the init sub-steps (1. allocation, 2. conversion) close to each other. Also: - HACKING_CRYPTO.md: fix docs for `ssh2_bn_from_bin()` and `ssh2_bn_set_word()`. Ref: #2583 (comment) Closes #2594
| ssh2_bn *bn = ssh2_bn_init_from_bin(); | ||
|
|
||
| #ifndef LIBSSH2_LIBGCRYPT | ||
| if(!bn) | ||
| return 1; | ||
| #endif | ||
| if(ssh2_bn_from_bin(bn, tests[i].input, tests[i].length) || |
There was a problem hiding this comment.
| ssh2_bn *bn = ssh2_bn_init_from_bin(); | |
| #ifndef LIBSSH2_LIBGCRYPT | |
| if(!bn) | |
| return 1; | |
| #endif | |
| if(ssh2_bn_from_bin(bn, tests[i].input, tests[i].length) || | |
| ssh2_bn *bn = NULL; | |
| if(ssh2_bn_from_bin(&bn, tests[i].input, tests[i].length) || |
Maybe this, to sync with master, aligning platforms.
| } | ||
| } | ||
|
|
||
| int ssh2_bn_from_bin(ssh2_bn *bn, const unsigned char *bin, size_t len) |
There was a problem hiding this comment.
| int ssh2_bn_from_bin(ssh2_bn *bn, const unsigned char *bin, size_t len) | |
| int ssh2_bn_from_bin(ssh2_bn **bn, const unsigned char *bin, size_t len) |
Needs a rebase on master for this and related changes.
There was a problem hiding this comment.
I've rebased onto the latest master and updated the WinCNG implementation and tests for the new ssh2_bn_from_bin() API in 7eaca5dd. The special initializer and libgcrypt-specific exception are removed.
All 92 CI checks passed. Thanks for simplifying the initialization API.
Trim redundant leading zero bytes from WinCNG DH public values and DH/ECDH shared secrets before SSH mpint serialization. Add a Windows regression test for normalization and zeroization.
Keep the WinCNG helper file-local and cover leading-zero normalization through the common bignum conversion API so the regression runs across crypto backends.
Apply leading-zero normalization to successful RSA-backed DH fallback results before mpint encoding. Put the zero-byte condition first in the normalization loop as requested in review. Expand the common bignum conversion cases and check cleared buffer tails. Exercise native DH and P-256 ECDH with fixed shared-secret vectors, and compare fresh DH public values with their CNG exports. Validation: test_simple passed with warnings as errors on MSVC WinCNG with ECDSA off/on and on WSL OpenSSL. Seven individual normalization or clearing mutations were detected; the native public-value mutation needed a leading-zero random sample. checksrc also passed.
Keep the common bignum conversion cases and remove Windows-specific fixtures, internal-buffer checks and zero-representation cases from the shared test file, as requested in review. Preserve libgcrypt initialization handling and leave the WinCNG normalization implementation unchanged.
Start the common conversion test with a NULL bignum and pass its address to the new ssh2_bn_from_bin() API. Let the backend perform allocation and report failure, removing the special initializer and libgcrypt guard. Keep the size comparison aligned with the upstream size_t return type. Co-Authored-By: OpenAI Codex <[email protected]>
fb1261e to
7eaca5d
Compare
|
@mback2k Your change request has been addressed, is that correct? |
|
Thanks @Lv2dev, merged now! |
The WinCNG backend has been missing runtime tests for a while. To restore some level of testing, this PR make use of curl's regression test suite (`runtests`) to run SCP/SFTP-specific tests with the master/PR libssh2 revision at hand. Some tests need to be disabled when using the built-in Windows OpenSSH server. The current CI jobs reproduce the long-running WinCNG flaky failure reported in #804 reliability. Note, if these CI jobs make libssh2 CI a pain to use, they will be removed. This has a reasonably fair chance to happen, based on the experience with curl CI with Windows. Also: - test both ECDH and non-ECDH builds. - test build on x64 and ARM64. - test with both Windows built-in OpenSSH port and MSYS2's sshd. - enable debug logging in libssh2 while running the tests. - for extra stress, run each of the 56-57 tests 6 times. This may be lowered in a future commit to avoid flakiness and reduce CI load and job times. Caveats: - as seen in curl CI, runtests may result in hangs due to broken pipes and error 2304 for undiscovered reasons. Discussion: curl/curl#14854 - MSBuild has an undisablable "smart" feature that sometimes marks a successful run a failure. Refs: curl/curl@a477789 curl/curl#18301 - these tests are different in scope than libssh2's own. - tests sometime fail flaky with: ``` Failure Event: -43 - Failed getting banner ``` Ref: https://github.com/libssh2/libssh2/actions/runs/34134974899/job/101783681252?pr=2590 - running on modern runner machines, the Win10-introduced DH algo is always enabled. Ref: #2049 Ref: #2583 Closes #2590
Should no longer be necessary after fixing the root cause. (Though we do not run these tests in CI at the moment.) Ref: libssh2#804 Follow-up to f216bbe libssh2#2583 Follow-up to bc120a3 libssh2#1023
In an attempt to improve stability / reduce flakiness of these tests. Especially the ECDSA-enable ones were prone to mass fail. ``` 22:49:30.110000 * Failure establishing ssh session: -43, Failed getting banner ``` Ref: https://github.com/libssh2/libssh2/actions/runs/38480041149/job/115580107883#step:11:1034 This is a different failure than the long-chased flaky -8 kex failures, which was fixed earlier and did not reoccur since then. Follow-up to 5bfa574 #2603 Follow-up to f216bbe #2583 Follow-up to bb8e095 #2590 Closes #2666
Summary
mpintserializationProblem
WinCNG returns DH public values and raw DH/ECDH shared secrets in fixed-width
buffers. The WinCNG bignum retained that buffer width as its numeric length.
When a value started with
0x00, libssh2 included the redundant byte in theexchange hash even though RFC 4251 requires a canonical
mpintrepresentation.The peer hashes the normalized integer, so the exchange hashes differ and a
valid server host-key signature is rejected. This appeared intermittently as
LIBSSH2_ERROR_KEY_EXCHANGE_FAILUREbecause it depends on the generated value.The helper introduced here moves significant bytes to the start of the existing
allocation, explicitly clears the removed tail, and updates only the logical
length. It does not add an allocation or a new failure path.
Validation
scripts/checksrc-all.plENABLE_ECDSA_WINCNG=ONtest_simplein both configurationstest_simplepassestest_simplepasses on each backendgroup exchange, group14, group16, and independent-process cases
These connection runs were not repeated for the latest base update.
Reported-by: Dan Fandrich
Fixes #804
Ref: #2590 (tests)