Repository navigation
Conversation
There was a problem hiding this comment.
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_bloband public-key derivation helpers tossh2_pub_privkey_file/ssh2_pub_privkey_blob. - Renames PEM parsing helpers to
ssh2_pem_parse_FILE/ssh2_pem_parse_bloband OpenSSH PEM parsing tossh2_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.
| 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) |
There was a problem hiding this comment.
Yes, it's not a beauty but _FILE means FILE *, vs _file, where a filename is passed.
| int ssh2_openssh_pem_parse_FILE(LIBSSH2_SESSION *session, | ||
| const char *passphrase, | ||
| FILE *fp, | ||
| struct string_buf **decrypted_buf) |
| "Computing public key from private key.")); | ||
|
|
||
| bp = BIO_new_mem_buf(privatekeydata, (int)privatekeydata_len); | ||
| bp = BIO_new_mem_buf(privkeyblob, (int)privkeyblob_len); |
There was a problem hiding this comment.
Pre-existing, perhaps worthy to consider in a separate PR.
There was a problem hiding this comment.
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_lenissize_t, butBIO_new_mem_buftakes anintlength. Add anINT_MAXbounds 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)
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
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
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
e.g. privatekeydata → privkeyblob