Skip to content

src: rename file/blob key import/parse functions for consistency - #2362

Closed
vszakats wants to merge 25 commits into
libssh2:masterfrom
vszakats:shorten1
Closed

vszakats wants to merge 25 commits into
libssh2:masterfrom
vszakats:shorten1

Conversation

@vszakats

@vszakats vszakats commented Jul 19, 2026 •

Copy link
Copy Markdown
Member

To have the same root name and same length, to match terms used
elsewhere in the code. To make it easy to identify them in pairs and by
suffix.

Also: move passphrase arg after key source, where missing.


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

  • align arg names with userauth.c.
    e.g. privatekeydata → privkeyblob

@vszakats
vszakats requested a review from Copilot July 19, 2026 17:13
@vszakats vszakats changed the title crypto: rename file/blob key import function src: rename file/blob key import/parse functions Jul 19, 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

This PR standardizes crypto-backend helper naming for importing/parsing private keys and deriving public keys, using consistent “file” vs “blob” terminology across backends and call sites to make related function pairs easier to spot.

Changes:

  • Renames crypto backend key-loading functions to *_priv_from_file / *_priv_from_blob and public-key derivation helpers to ssh2_pub_privkey_file / ssh2_pub_privkey_blob.
  • Renames PEM parsing helpers to ssh2_pem_parse_FILE / ssh2_pem_parse_blob and OpenSSH PEM parsing to ssh2_openssh_pem_parse_FILE / ssh2_openssh_pem_parse_blob, updating all call sites.
  • Updates internal documentation (HACKING-CRYPTO.md) and internal prototypes (crypto.h, libssh2_priv.h) accordingly.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/wincng.c Updates WinCNG backend to new file/blob key parsing/derivation names.
src/userauth.c Updates userauth flows to call renamed pubkey-from-privkey helpers (file/blob + SK).
src/pem.c Renames PEM/OpenSSH PEM parsing entry points and updates internal delegation to blob parsing.
src/os400qc3.c Updates OS/400 QC3 backend to renamed PEM and key helper functions.
src/openssl.h Updates LIBSSH2_KEY_SK comment to reflect renamed SK helper.
src/openssl.c Updates OpenSSL backend key import/parse paths and SK helper naming.
src/misc.c Updates stub SK helper implementation name for non-SK builds.
src/mbedtls.c Updates mbedTLS backend to renamed key import and pubkey derivation helpers.
src/libssh2_priv.h Updates internal PEM/OpenSSH PEM parsing prototypes.
src/libgcrypt.c Updates libgcrypt backend stubs/implementations to renamed key import + pubkey derivation helpers.
src/hostkey.c Updates hostkey initialization to call renamed backend key import helpers.
src/HACKING-CRYPTO.md Updates backend interface documentation examples to match new names.
src/crypto.h Updates internal crypto-backend interface prototypes to new naming scheme.

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

Comment thread src/pem.c
Comment on lines +99 to +104
int ssh2_pem_parse_FILE(LIBSSH2_SESSION *session,
const char *headerbegin,
const char *headerend,
const char *passphrase,
FILE *fp,
unsigned char **data, size_t *datalen)

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.

Yes, it's not a beauty but _FILE means FILE *, vs _file, where a filename is passed.

Comment thread src/pem.c
Comment on lines +717 to +720
int ssh2_openssh_pem_parse_FILE(LIBSSH2_SESSION *session,
const char *passphrase,
FILE *fp,
struct string_buf **decrypted_buf)

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.

Same.

@vszakats vszakats changed the title src: rename file/blob key import/parse functions src: rename file/blob key import/parse functions for consistency Jul 19, 2026
@vszakats
vszakats requested a review from Copilot July 19, 2026 23:20

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

Comment thread src/crypto.h Resolved
Comment thread src/wincng.c Resolved
Comment thread src/wincng.c Resolved
Comment thread src/openssl.c
"Computing public key from private key."));

bp = BIO_new_mem_buf(privatekeydata, (int)privatekeydata_len);
bp = BIO_new_mem_buf(privkeyblob, (int)privkeyblob_len);

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.

Pre-existing, perhaps worthy to consider in a separate PR.

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.

Pull request overview

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

Comments suppressed due to low confidence (1)

src/openssl.c:4020

  • privkeyblob_len is size_t, but BIO_new_mem_buf takes an int length. Add an INT_MAX bounds check before casting to avoid truncation/negative lengths on large inputs.
int ssh2_pub_privkey_blob(LIBSSH2_SESSION *session,
                          char **method, size_t *method_len,
                          unsigned char **pubkeydata, size_t *pubkeydata_len,
                          const char *privkeyblob,
                          size_t privkeyblob_len,
                          const char *passphrase)

Comment thread src/openssl.c Resolved
Comment thread src/openssl.c Resolved
Comment thread src/openssl.c Resolved
Comment thread src/openssl.c Resolved
@vszakats vszakats closed this in 6a95116 Jul 20, 2026
@vszakats
vszakats deleted the shorten1 branch July 20, 2026 06:35
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