Skip to content

crypto: make ssh2_bn_from_bin() init the bignum, drop ssh2_bn_init_from_bin() - #2594

Closed
vszakats wants to merge 10 commits into
libssh2:masterfrom
vszakats:bn-from-bin
Closed

vszakats wants to merge 10 commits into
libssh2:masterfrom
vszakats:bn-from-bin

Conversation

@vszakats

@vszakats vszakats commented Sep 7, 2026 •

Copy link
Copy Markdown
Member

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)

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.

🟡 Changes recommended

Fixed-group key exchanges always fail, and two backends mishandle allocation failure.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Updates bignum binary conversion to initialize destinations consistently across crypto backends.

Changes:

  • Changes ssh2_bn_from_bin() to accept ssh2_bn **.
  • Removes call sites’ explicit initialization.
  • Adds backend-specific conversion implementations.
File summaries
File Description
src/wincng.c Updates Windows CNG conversion.
src/os400qc3.c Migrates IBM i bignum callers.
src/openssl.h Removes conversion macro.
src/openssl.c Adds OpenSSL conversion function.
src/mbedtls.h Removes conversion macro.
src/mbedtls.c Adds mbedTLS conversion function.
src/libgcrypt.h Updates conversion macro indirection.
src/kex.c Migrates key-exchange bignum initialization.
src/crypto.h Updates the shared conversion API.
Review details

Suppressed comments (3)

src/kex.c:868

  • Because key_state->p now starts as NULL, this condition never calls ssh2_bn_from_bin() and group14 key exchange always fails. The conversion call itself should perform allocation and report any failure.
        else if(!key_state->p ||
                ssh2_bn_from_bin(&key_state->p, p_value, 256)) {

src/kex.c:978

  • The preceding key_state->p = NULL makes this condition unconditionally true before ssh2_bn_from_bin() can run, so group16 key exchange always fails. Test the conversion result without the pre-allocation null check.
        if(!key_state->p || ssh2_bn_from_bin(&key_state->p, p_value, 512)) {

src/kex.c:1111

  • key_state->p is initialized to NULL, so this || short-circuits and group18 key exchange always takes the error path. Call ssh2_bn_from_bin() directly so it can initialize p.
        else if(!key_state->p ||
                ssh2_bn_from_bin(&key_state->p, p_value, 1024)) {
  • Files reviewed: 9/9 changed files
  • Comments generated: 4
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/kex.c Resolved
Comment thread src/openssl.c Resolved
Comment thread src/mbedtls.c Resolved
Comment thread src/crypto.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.

🟡 Changes recommended

OS/400 wrappers can falsely report success and leak allocations when passed NULL output bignums.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details

Suppressed comments (2)

src/os400qc3.c:1322

  • pub is passed by value, so taking &pub allows ssh2_bn_from_bin() to allocate only into the local copy. If the caller's public-bignum allocation failed (for example at kex.c:429), this now returns success, leaks the new bignum, and leaves the caller's output NULL. Preserve the output contract by rejecting NULL before conversion (or change the interface to propagate allocation).
    return ssh2_bn_from_bin(&pub, (unsigned char *)pubkey, pubkeylen);

src/os400qc3.c:1372

  • Taking the address of this by-value secret parameter means the converter can allocate a bignum that is never propagated to the caller. On a NULL destination this returns success and leaks memory, while the caller still has NULL; reject NULL before conversion or make the output parameter indirect throughout the interface.
    return ssh2_bn_from_bin(&secret, (unsigned char *)secretbuf, secretbuflen);
  • Files reviewed: 10/10 changed files
  • Comments generated: 3
  • Review effort level: Balanced

Comment thread src/libgcrypt.h Resolved
Comment thread src/os400qc3.c Resolved
Comment thread src/HACKING-CRYPTO.md Resolved
`bn` is a by-value parameter here. If it is NULL, the new converter
allocates through the address of this local copy, returns success, and
leaks the allocation without updating the caller; previously this path
returned an error. Reject a NULL destination before calling the
allocating API.

This issue also appears in the following locations of the same file:
- line 1322
- line 1372

libssh2#2594 (comment)

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.

🟡 Changes recommended

The documented allocation condition contradicts the implemented pointer-to-pointer contract.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details
  • Files reviewed: 10/10 changed files
  • Comments generated: 1
  • Review effort level: Balanced

Comment thread src/HACKING-CRYPTO.md 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.

🟢 Approval recommended

The backend implementations, callers, and documentation consistently follow the revised bignum initialization contract.

Review details
  • Files reviewed: 10/10 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

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