Skip to content

userauth: merge file/blob sibling functions into userauth_publickey() - #2359

Closed
vszakats wants to merge 3 commits into
libssh2:masterfrom
vszakats:mergemore
Closed

vszakats wants to merge 3 commits into
libssh2:masterfrom
vszakats:mergemore

Conversation

@vszakats

Copy link
Copy Markdown
Member

Follow-up to 5b94157 #2354

@vszakats vszakats changed the title userauth: merge file/blob subling functions into userauth_publickey() userauth: merge file/blob sibling functions into userauth_publickey() Jul 19, 2026
@vszakats
vszakats requested a review from Copilot July 19, 2026 15:44

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 refactors public-key user authentication by merging the previously separate “from file” and “from memory/blob” helper paths into a single internal helper (userauth_publickey()), aligning with the broader effort in #2354 to reduce duplicated key-loading logic and backend divergence.

Changes:

  • Consolidates userauth_publickey_fromfile() and userauth_publickey_frommemory() into a unified userauth_publickey() helper that supports file or blob inputs.
  • Simplifies error propagation in hostbased auth setup by using a single post-branch rc check with a consistent comment.
  • Updates internal comments to reflect the new consolidated helper name and behavior.

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

@vszakats vszakats closed this in fe9442b Jul 19, 2026
@vszakats
vszakats deleted the mergemore branch July 19, 2026 16:32
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
@vszakats vszakats mentioned this pull request Jul 22, 2026
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
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