Repository navigation
Conversation
vszakats
marked this pull request as draft
July 19, 2026 03:23
Member
Author
vszakats
added a commit
that referenced
this pull request
Jul 19, 2026
- fix off by one error when loading public keys with no id. - make sure that sp_len is positive and avoid overflows. These were fixed in `userauth_read_file_pubkey()` earlier, but missed from the sibling code loading pubkey from memory buffer (as opposed to file). Sibling code made it into the codebase after the first fix, but it was based code copy-pasted from a pre-fix revision. Bug: #2354 (comment) Follow-up to 4820556 #386 Follow-up to 18cfec8 Follow-up to 3e47ca8 Closes #2358
There was a problem hiding this comment.
Pull request overview
This PR reduces duplicated key-loading logic in the userauth/hostkey areas by unifying “from file” and “from memory” codepaths, and by collapsing the hostkey PEM initialization interface into a single entry point that can handle either source.
Changes:
- Merge public-key parsing into a single helper (
userauth_read_pubkey) that supports both filename and in-memory blobs. - Merge private-key loading and signing callbacks into unified codepaths (
userauth_read_privkey+userauth_sign) to avoid divergence between file/memory auth flows. - Simplify the internal hostkey backend interface by replacing
initPEM+initPEMFromMemorywith a singleinitPEMthat accepts either a filename or a blob.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/userauth.c |
Consolidates public/private key reading and signing paths to remove file-vs-memory duplication. |
src/libssh2_priv.h |
Updates the internal hostkey_method interface to a unified PEM init entry point. |
src/hostkey.c |
Refactors RSA/DSA/ECDSA/Ed25519 hostkey method implementations to use the unified PEM init signature and removes the separate “from memory” init. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
src/userauth.c:713
- userauth_read_privkey() selects the hostkey_method by doing a prefix match on the method name (strncmp(..., method_len)). This can incorrectly pick a different algorithm whose name shares the same prefix (e.g., selecting "ssh-ed25519-cert-..." when method is "ssh-ed25519"), because method_len is derived from the first token in the public key data. Use an exact match (length + memcmp) to avoid ambiguous handler selection.
while(*hostkey_methods_avail && (*hostkey_methods_avail)->name) {
if((*hostkey_methods_avail)->initPEM &&
!strncmp((*hostkey_methods_avail)->name, (const char *)method,
method_len)) {
*hostkey_method = *hostkey_methods_avail;
```
/home/runner/work/libssh2/libssh2/src/userauth.c:1005:12: error: Branch condition evaluates to a garbage value [clang-analyzer-core.uninitialized.Branch,-warnings-as-errors]
1005 | if(privkeyobj && privkeyobj->signv &&
| ^~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/userauth.c:900:8: note: Assuming field 'userauth_host_state' is equal to ssh2_NB_state_idle
900 | if(session->userauth_host_state == ssh2_NB_state_idle) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/userauth.c:900:5: note: Taking true branch
900 | if(session->userauth_host_state == ssh2_NB_state_idle) {
| ^
/home/runner/work/libssh2/libssh2/src/userauth.c:901:9: note: 'privkeyobj' declared without an initial value
901 | const struct hostkey_method *privkeyobj;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/userauth.c:914:12: note: Assuming 'publickey' is non-null
914 | if(publickey) {
| ^~~~~~~~~
/home/runner/work/libssh2/libssh2/src/userauth.c:914:9: note: Taking true branch
914 | if(publickey) {
| ^
/home/runner/work/libssh2/libssh2/src/userauth.c:920:16: note: Assuming 'rc' is 0
920 | if(rc)
| ^~
/home/runner/work/libssh2/libssh2/src/userauth.c:920:13: note: Taking false branch
920 | if(rc)
| ^
/home/runner/work/libssh2/libssh2/src/userauth.c:934:12: note: Assuming the condition is false
934 | if(username_len > MAX_INPUT_LEN ||
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/userauth.c:934:12: note: Left side of '||' is false
/home/runner/work/libssh2/libssh2/src/userauth.c:935:12: note: Assuming the condition is false
935 | session->userauth_host_method_len > MAX_INPUT_LEN ||
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/userauth.c:934:12: note: Left side of '||' is false
934 | if(username_len > MAX_INPUT_LEN ||
| ^
/home/runner/work/libssh2/libssh2/src/userauth.c:936:12: note: Assuming the condition is false
936 | hostname_len > MAX_INPUT_LEN ||
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/userauth.c:934:12: note: Left side of '||' is false
934 | if(username_len > MAX_INPUT_LEN ||
| ^
/home/runner/work/libssh2/libssh2/src/userauth.c:937:12: note: Assuming the condition is false
937 | local_username_len > MAX_INPUT_LEN ||
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/userauth.c:934:12: note: Left side of '||' is false
934 | if(username_len > MAX_INPUT_LEN ||
| ^
/home/runner/work/libssh2/libssh2/src/userauth.c:938:12: note: Assuming the condition is false
938 | pubkeydata_len > MAX_INPUT_LEN) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/userauth.c:934:9: note: Taking false branch
934 | if(username_len > MAX_INPUT_LEN ||
| ^
/home/runner/work/libssh2/libssh2/src/userauth.c:965:12: note: Assuming field 'userauth_host_packet' is non-null
965 | if(!session->userauth_host_packet) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/userauth.c:965:9: note: Taking false branch
965 | if(!session->userauth_host_packet) {
| ^
/home/runner/work/libssh2/libssh2/src/userauth.c:985:14: note: Calling 'userauth_read_privkey'
985 | rc = userauth_read_privkey(session, &privkeyobj, &abstract,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
986 | session->userauth_host_method,
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
987 | session->userauth_host_method_len,
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
988 | privatekey, NULL, 0, passphrase);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/userauth.c:700:8: note: Assuming 'privkeyfile' is null
700 | if(privkeyfile)
| ^~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/userauth.c:700:5: note: Taking false branch
700 | if(privkeyfile)
| ^
/home/runner/work/libssh2/libssh2/src/userauth.c:703:14: note: 'privkeyblob' is null
703 | else if(!privkeyblob || !privkeyblob_len)
| ^~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/userauth.c:703:26: note: Left side of '||' is true
703 | else if(!privkeyblob || !privkeyblob_len)
| ^
/home/runner/work/libssh2/libssh2/src/userauth.c:704:9: note: Returning without writing to '*hostkey_method'
704 | return ssh2_err(session, LIBSSH2_ERROR_FILE,
| ^
/home/runner/work/libssh2/libssh2/src/userauth.c:704:9: note: Returning value, which participates in a condition later
704 | return ssh2_err(session, LIBSSH2_ERROR_FILE,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
705 | "Missing private key blob");
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/userauth.c:985:14: note: Returning from 'userauth_read_privkey'
985 | rc = userauth_read_privkey(session, &privkeyobj, &abstract,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
986 | session->userauth_host_method,
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
987 | session->userauth_host_method_len,
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
988 | privatekey, NULL, 0, passphrase);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/runner/work/libssh2/libssh2/src/userauth.c:989:12: note: Assuming 'rc' is 0
989 | if(rc) {
| ^~
/home/runner/work/libssh2/libssh2/src/userauth.c:989:9: note: Taking false branch
989 | if(rc) {
| ^
/home/runner/work/libssh2/libssh2/src/userauth.c:997:9: note: Loop condition is false. Exiting loop
997 | ssh2_prepare_iovec(datavec, 4);
| ^
/home/runner/work/libssh2/libssh2/src/crypto.h:62:39: note: expanded from macro 'ssh2_prepare_iovec'
62 | #define ssh2_prepare_iovec(vec, len) do {} while(0)
| ^
/home/runner/work/libssh2/libssh2/src/userauth.c:1005:12: note: Branch condition evaluates to a garbage value
1005 | if(privkeyobj && privkeyobj->signv &&
| ^~~~~~~~~~
```
https://github.com/libssh2/libssh2/actions/runs/29690160065/job/88201368184?pr=2354
vszakats
added a commit
that referenced
this pull request
Jul 21, 2026
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 #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 Closes #2379
1 task done
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In libssh2 for what looks like historical reasons, the codepaths for
reading keys are split for reading them from a file or reading them from
a memory buffer. This split is causing many, otherwise identical code to
be duplicated and living their own lives. It also means getting updates
or fixes separately at times, and generally causing duplicate
maintenance and arguably code bloat. The duplication makes its way into
the internal crypto interface (
crypto.h) and with it, into eachbackend, causing further code duplication and subtly or completely
different behavior (and bugs) depending on sourcing the same key from
file or memory buffer.
These effects are undesired both for using and maintaining libssh2.
Loading files via a distinct code may make sense for large files and/or
on devices with constrained memory, but in practice key files are tiny,
expected to remain so, and many (most?) codepaths already preload the
whole file into a memory buffer, and process it as such anyway.
To avoid code duplication and different behavior, this patch aims to
merge these codepaths, in the hostkey and userauth end of the code.