Skip to content

crypto: de-duplicate/merge most file/blob codepaths - #2379

Closed
vszakats wants to merge 49 commits into
libssh2:masterfrom
vszakats:mergemore3
Closed

vszakats wants to merge 49 commits into
libssh2:masterfrom
vszakats:mergemore3

Conversation

@vszakats

@vszakats vszakats commented Jul 21, 2026 •

Copy link
Copy Markdown
Member

Replace separate codepath for loading private keys from filenames vs.
memory buffers (blobs), with unified functions that handle both. Either
filename or blob pointer + size must be passed to these functions.

To reduce code duplication and small/large differences and bugs between
these codepaths. Also to improve readability and ease maintenance.

Also fix:

  • mbedtls: do not pass redundant null-terminator to
    mbed_parse_openssh_key() on in ECDSA blob codepath. (benign)
  • libgcrypt: add support for RSA/DSA blobs.
  • openssl: error handling code now applies to from-file codepath.
    Reported-and-alternate-patch-by: Martin Nowak
    Fixes ensure to clear openssl3 PEM reading errors #1652
  • openssl: add file support for ssh2_sk_pubkey().
    (not exposed via public API at this time.)
  • openssl: fix to call OSSL_INIT_IF_NEEDED() from
    ssh2_pub_privkey(). (for wolfSSL-debug enabled builds.)
  • openssl: fix error code returned by ssh2_sk_pubkey().
    (to be consistent with rest of code.)
  • openssl: fix potential NULL deref in ssh2_ed25519_new_priv() blob
    codepaths.
  • openssl: fix potential dangling pointer in RSA, DSA, ECDSA codepaths
    (both file and blob) if BIO init failed.
  • os400qc3: fix uninitialized structs in os400_pub_privkey_*().
  • os400qc3: drop unused local variable.

Notes:

  • os400: use wrappers without actually merging the code.
    They are very different and difficult to read, also not possible to
    build test.
  • openssl: ed25519 loader code has outlier code where the file/blob code
    is different (both rather short.)
  • openssl: there is a file/blob function pair left to merge.
  • pem: there remain two pairs of file/blob internal APIs.

Follow-up to 6a95116 #2362
Follow-up to fe9442b #2359
Follow-up to 5b94157 #2354


https://github.com/libssh2/libssh2/pull/2379/files?w=1

Copilot AI review requested due to automatic review settings July 21, 2026 15:06
@vszakats
vszakats marked this pull request as draft July 21, 2026 15:06
@vszakats vszakats changed the title src: de-duplicate/merge most file/blob codepaths crypto: de-duplicate/merge most file/blob codepaths Jul 21, 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 11 out of 11 changed files in this pull request and generated 9 comments.

Comment thread src/openssl.c Resolved
Comment thread src/openssl.c Resolved
Comment thread src/openssl.c Resolved
Comment thread src/wincng.c Resolved
Comment thread src/mbedtls.c Resolved
Comment thread src/mbedtls.c Resolved
Comment thread src/libgcrypt.c Resolved
Comment thread src/libgcrypt.c Resolved
Comment thread src/os400qc3.c
Comment on lines +2216 to +2225
if(privatekey)
return os400_pub_privkey_file(session, method, method_len,
pubkeydata, pubkeydata_len,
privatekey,
passphrase);
else
return os400_pub_privkey_blob(session, method, method_len,
pubkeydata, pubkeydata_len,
privkeyblob, privkeyblob_len,
passphrase);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hyperactive annoying LLM.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This class of issues is all preexisting unless I'm missing something.
Won't be tackling it here.

Copilot AI review requested due to automatic review settings July 21, 2026 16:05

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 11 out of 11 changed files in this pull request and generated 4 comments.

Comment thread src/openssl.c Resolved
Comment thread src/openssl.c Resolved
Comment thread src/openssl.c Resolved
Comment thread src/wincng.c Resolved
Copilot AI review requested due to automatic review settings July 21, 2026 16:52
@vszakats

Copy link
Copy Markdown
Member Author

Automatic review is somewhat annoying.

@vszakats
vszakats marked this pull request as ready for review July 21, 2026 16:55

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 11 out of 11 changed files in this pull request and generated 1 comment.

Comment thread src/openssl.c Resolved
Copilot AI review requested due to automatic review settings July 21, 2026 17:06

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 11 out of 11 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (1)

src/openssl.c:3927

  • If PEM_read_bio_PrivateKey() fails but the OpenSSH fallback (ossl_key_from_openssh_file/blob) succeeds, OpenSSL errors produced by the failed PEM read can remain on the thread-local error queue. This is the same class of issue as #1652 (stale errors after successfully loading an OpenSSH key). Consider clearing the remaining OpenSSL error queue on the !pk path after capturing sslError (when applicable) so a successful return leaves no OpenSSL errors behind.
    (void)BIO_reset(bp);
    pk = PEM_read_bio_PrivateKey(bp, NULL, NULL, SSH2_UNCONST(passphrase));
#ifdef HAVE_SSLERROR_BAD_DECRYPT
    sslError = ERR_get_error();
#endif

@vszakats vszakats closed this in b560cb4 Jul 21, 2026
@vszakats
vszakats deleted the mergemore3 branch July 21, 2026 17:34
vszakats added a commit that referenced this pull request Jul 22, 2026
To further merge file/blob codepaths/behavior, and to reduce branching
at callers.

Also:
- pem: add internal API `ssh2_file_to_blob()` to load a (PEM) file into
  memory. Use it internally.
  Make sure to null-terminate the returned blob, primarily for mbedTLS.
- mbedtls, os400qc3: use `ssh2_file_to_blob()`.
- pem: fix to return `LIBSSH2_ERROR_ALGO_UNSUPPORTED` in
  `ssh2_pem_parse()` in no-MD5 builds (was: -1).
- os400qc3: fix to scrub data before free in `try_pem_load()`.

Note:
- os400qc3: `try_pem_load()` has unique behavior of allowing multiple
  instances of private keys within a single file, but only when loading
  from a file. I could not positively confirm this to be valid/standard
  private file format, nor find a comment or commit message explaining
  the behavior. I've added (untested) support for this using the blob
  codepath, because the `FILE *` method is now gone from `pem.c`. It
  also seems one contributing factor why the file (vs blob) tracks are
  much different in this backend. After this patch, both could use
  `ssh2_pem_parse()` and possibly unify `os400_pub_privkey_file/blob()`
  and `os400_rsa_new_priv_from_file/blob()`.

Follow-up to b560cb4 #2379
Follow-up to 6a95116 #2362
Follow-up to fe9442b #2359
Follow-up to 5b94157 #2354

Closes #2388
vszakats added a commit that referenced this pull request Jul 22, 2026
vszakats added a commit that referenced this pull request Jul 22, 2026
…_from_openssh()`

Merging `ossl_*_openssh_priv_new()` functions and syncing the result up
with `ossl_key_from_openssh()` ends up with an almost identical function
(with the newly merged ones supporting filenames, not blobs). Therefore
replace these calls with the latter, and drop the redundant code.

This also syncs up recognized keys loaded from filenames with those
loaded from memory buffers.

Also:
- sync `ssh2_ed25519_new_priv()` with sibling functions, also merging
  its file/blob codepaths. It means the filename codepath now uses
  OpenSSL's built-in loader first, and resorts to libssh2's own loader
  if OpenSSL's failed.
  Follow-up to b560cb4 #2379

Follow-up to 3bb068e #2394

Closes #2396
vszakats added a commit that referenced this pull request Aug 28, 2026
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