Skip to content

libgcrypt, mbedtls, openssl: validate DH public value - #2239

Closed
vszakats wants to merge 47 commits into
libssh2:masterfrom
vszakats:pubisvalid
Closed

vszakats wants to merge 47 commits into
libssh2:masterfrom
vszakats:pubisvalid

Conversation

@vszakats
vszakats marked this pull request as draft July 6, 2026 22:54
@vszakats
vszakats force-pushed the pubisvalid branch 2 times, most recently from c7860e8 to c35962c Compare July 8, 2026 13:03
@vszakats vszakats changed the title mbedtls, openssl: try validating DH public value mbedtls, openssl: validate DH public value Jul 8, 2026
@vszakats
vszakats marked this pull request as ready for review July 8, 2026 13:19
@vszakats
vszakats requested a review from Copilot July 8, 2026 13:22

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds validation for Diffie-Hellman public values in the OpenSSL and mbedTLS backends (mitigating GHSA-m9rg-h9jv-9gvh) and introduces a simple unit test to exercise the new validation helper.

Changes:

  • Add ssh2_dh_is_valid() helper for OpenSSL and mbedTLS and enforce it in ssh2_dh_secret().
  • Add unit coverage in tests/test_simple.c for DH public-value validation error cases.
  • Expose ssh2_dh_is_valid() in src/crypto.h and annotate an internal header used by unit tests.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
tests/test_simple.c Adds unit test cases for DH public-value validation across supported crypto backends.
src/userauth_kbd_packet.h Adds a comment clarifying the function is used by unit tests.
src/openssl.c Implements DH public-value validation for OpenSSL/WolfSSL and enforces it before computing the shared secret.
src/mbedtls.c Implements DH public-value validation for mbedTLS and enforces it before computing the shared secret.
src/crypto.h Declares ssh2_dh_is_valid() for internal use (tests + backend code path).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/test_simple.c Resolved
Comment thread src/mbedtls.c Resolved
Comment thread src/mbedtls.c Resolved
Comment thread src/mbedtls.c Resolved
Comment thread src/crypto.h Resolved
Comment thread src/openssl.c Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.

Comment thread tests/test_simple.c Resolved
Comment thread tests/test_simple.c Resolved
Comment thread tests/test_simple.c Resolved
Comment thread src/userauth_kbd_packet.h Resolved
Comment thread src/crypto.h Resolved
@vszakats
vszakats requested a review from Copilot July 8, 2026 16:54
@vszakats vszakats changed the title mbedtls, openssl: validate DH public value libgcrypt, mbedtls, openssl: validate DH public value Jul 8, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Comment thread tests/test_simple.c Resolved
Comment thread tests/test_simple.c Resolved
Comment thread src/crypto.h Resolved
Comment thread src/userauth_kbd_packet.h Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Comment thread src/mbedtls.c Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 7 comments.

Comment thread tests/test_simple.c Resolved
Comment thread src/userauth_kbd_packet.h Resolved
Comment thread src/crypto.h Resolved
Comment thread src/openssl.c Resolved
Comment thread src/openssl.c Resolved
Comment thread src/mbedtls.c Resolved
Comment thread src/libgcrypt.c Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Comment thread src/openssl.c Resolved
Comment thread src/crypto.h Resolved
Comment thread src/openssl.c Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Comment thread src/crypto.h Resolved
@vszakats vszakats closed this in fcf8307 Jul 8, 2026
@vszakats
vszakats deleted the pubisvalid branch July 8, 2026 20:24
vszakats added a commit to vszakats/libssh2 that referenced this pull request Jul 23, 2026
To reflect that the return value is not a boolean.

Reported-by: Patrick Monnerat
Bug: libssh2#2402 (comment)
Follow-up to fcf8307 libssh2#2239
vszakats added a commit that referenced this pull request Jul 23, 2026
To reflect that the return value is not a boolean.

Reported-by: Patrick Monnerat
Bug: #2402 (comment)
Follow-up to fcf8307 #2239

Closes #2406
vszakats added a commit that referenced this pull request Jul 23, 2026
Fixing:
```
tests/test_simple.c:84:56: error: operand of ? changes signedness: 'int' to 'unsigned long' [clang-diagnostic-sign-conversion]
   84 |         gcry_mpi_t f = gcry_mpi_set_ui(NULL, fi >= 0 ? fi : -fi);
      |                        ~~~~~~~~~~~~~~~                 ^~
tests/test_simple.c:84:61: error: operand of ? changes signedness: 'int' to 'unsigned long' [clang-diagnostic-sign-conversion]
   84 |         gcry_mpi_t f = gcry_mpi_set_ui(NULL, fi >= 0 ? fi : -fi);
      |                        ~~~~~~~~~~~~~~~                      ^~~
tests/test_simple.c:85:46: error: implicit conversion changes signedness: 'int' to 'unsigned long' [clang-diagnostic-sign-conversion]
   85 |         gcry_mpi_t p = gcry_mpi_set_ui(NULL, atoi(tests[i].p));
      |                        ~~~~~~~~~~~~~~~       ^~~~~~~~~~~~~~~~
```
Ref: https://github.com/libssh2/libssh2/actions/runs/30043871895/job/89330114459#step:24:124

Also add intermediate current test holder var to keep code readable.

Follow-up to 76d30e5 #2417
Follow-up to fcf8307 #2239

Closes #2418
vszakats added a commit that referenced this pull request Jul 27, 2026
vszakats pushed a commit that referenced this pull request Jul 27, 2026
We consider parameters as valid if 2 <= f <= p-2 and if f has at least 4
bits set.

I had to undo some tidy-up that broke compilation on os400:

`((ptr)->connect)(...)` instead of `(ptr)->connect(...)` is intentional
since` connect()` is a macro and extra-parentheses avoid its expansion.
The same applies to `send()` and `recv()`.

OS400 has no `uintptr_t`.

Ref: GHSA-m9rg-h9jv-9gvh
Follow-up to fcf8307 #2239

Closes #2402
vszakats added a commit that referenced this pull request Sep 7, 2026
Omit direct backend-specific API use. Drop one test using negative
input, that's impossible with the internal crypto API. Using
backend-specific code in tests is highly undesired. It's also
discouraged to special-case any, in this case WinCNG had to be because
it does not implement the DH validation function.

Also:
- mbedtls: fix an internal API type difference with a cast.
- unfold a line.

Follow-up to fcf8307 #2239

Closes #2595
vszakats pushed a commit that referenced this pull request Oct 4, 2026
ssh2_dh_secret() in the Windows CNG backend uses the server's
Diffie-Hellman public value f without the range check the other four
backends apply through ssh2_dh_validate(), so a malicious server can
send f of 0, 1 or p-1 and push the shared secret into a trivial subgroup
that openssl, libgcrypt, mbedtls and os400qc3 all reject with
LIBSSH2_ERROR_PROTO. This adds a WinCNG ssh2_dh_validate() that enforces
1 < f < p-1 and the same minimum-set-bits check on the big-endian value,
and calls it at the top of ssh2_dh_secret() like the other backends do.
The shared test_ssh2_dh_validate() in tests/test_simple.c was stubbed
out on WinCNG only because the function did not exist, so it now runs
against the real implementation too.

Reported-by: yaotushaozhu on github
Ref: GHSA-m9rg-h9jv-9gvh
Follow-up to fcf8307 #2239

Closes #2645
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants