Repository navigation
Conversation
There was a problem hiding this comment.
Pull request overview
This PR consolidates PEM private key parsing from either a file (FILE*) or an in-memory blob into a single implementation, reducing duplicated logic across backends.
Changes:
- Replaces
ssh2_pem_parse_FILE()/ssh2_pem_parse_blob()with a unifiedssh2_pem_parse()API. - Adds an internal helper to load a
FILE*into a blob buffer for parsing. - Updates WinCNG, OS/400 QC3, and libgcrypt backends to call the unified PEM parser.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/pem.c | Introduces ssh2_pem_parse() and refactors file/blob PEM parsing into a unified path. |
| src/libssh2_priv.h | Updates internal header prototypes to expose only the new unified PEM parsing API. |
| src/wincng.c | Switches WinCNG private key loading to call ssh2_pem_parse(). |
| src/os400qc3.c | Switches OS/400 PEM parsing call sites to ssh2_pem_parse(). |
| src/libgcrypt.c | Switches libgcrypt RSA/DSA private key loading to ssh2_pem_parse() (but currently has an uninitialized FILE *fp bug). |
💡 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/libgcrypt.c:263
fpmay be uninitialized whenfilename == NULL, but it is passed tossh2_pem_parse()and later tested inif(fp) fclose(fp);. This is undefined behavior and can lead to crashes or closing an invalid pointer. Initializefpto NULL so the blob codepath is safe.
ret = ssh2_pem_parse(session, PEM_RSA_HEADER, PEM_RSA_FOOTER,
fp, blob, blob_len, passphrase,
&data, &datalen);
if(fp)
fclose(fp);
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
src/os400qc3.c:1993
try_pem_load()updatesblob_offsetincorrectly:ssh2_pem_parse()returns an offset relative to the provided slice (blob + blob_offset), but the code overwrites the absoluteblob_offsetwith that relative value. This can cause the loop to re-parse the same region or skip incorrectly when multiple PEM objects are concatenated.
while(blob_offset < blob_len) {
ret = ssh2_pem_parse(session, header, trailer,
NULL, blob + blob_offset, blob_len - blob_offset,
passphrase,
&data, &datalen, &blob_offset);
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (3)
src/os400qc3.c:1995
try_pem_load()usesblob_leftfor the loop condition, but callsssh2_pem_parse()withblob_remainand decrementsblob_remainafterwards.blob_remainis not declared here, which will not compile; even if renamed later, the loop would never make progress unless the same variable used in the condition is decremented.
while(blob_left > 0) {
ret = ssh2_pem_parse(session, header, trailer,
NULL, blob_pos, blob_remain,
passphrase,
&data, &datalen, &blob_offset);
src/pem.c:85
ssh2_file_to_blob()returns-1whenssh2_fopen()fails but does not set a session error. This loses the specific failure reason for all new callers that rely onssh2_file_to_blob()/ssh2_*pem_parse()(and some call sites may overwrite it with a misleading error).
fp = ssh2_fopen(filename, "rb");
if(!fp) {
ret = ssh2_err(session, LIBSSH2_ERROR_FILE,
src/openssl.c:3537
- On
ssh2_openssh_pem_parse()failure this block unconditionally overwrites the session error with "Not an OpenSSH key file". With the new filename/blob merge, failures can now include file open/IO/alloc errors; overwriting them makes diagnosing failures harder and can be misleading.
if(rc) {
ssh2_err(session, LIBSSH2_ERROR_FILE, "Not an OpenSSH key file");
return rc;
}
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 7 out of 7 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
src/os400qc3.c:1995
try_pem_load()usesblob_remain(undefined) instead ofblob_left, which will not compile and also prevents the remaining-length counter from being updated correctly. This loop should pass the current remaining length tossh2_pem_parse(), decrement the same variable after advancingblob_pos, and (defensively) break ifblob_offsetis 0 to avoid a potential infinite loop. Also freedataon the early-success return to avoid leaking the decoded PEM buffer.
while(blob_left > 0) {
ret = ssh2_pem_parse(session, header, trailer,
NULL, blob_pos, blob_remain,
passphrase,
&data, &datalen, &blob_offset);
| #else | ||
| ssh2_err(session, LIBSSH2_ERROR_ALGO_UNSUPPORTED, | ||
| "Unable to decrypt PEM, MD5 not enabled"); | ||
| ret = -1; | ||
| goto out; |
When processing encrypted PEM files. Also: make sure to initialize returned pointer/length on fail. Reported by Copilot Bug: #2388 (comment) Closes #2390
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 7 out of 7 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (2)
src/pem.c:173
ssh2_pem_parse()can be called with bothfilename == NULLandblob == NULL/blob_len == 0, but it currently dereferencesblobviapem_readline()without validating inputs. Adding an early guard (similar tossh2_openssh_pem_parse()) avoids a NULL dereference and ensures a consistent error is reported.
*data = NULL;
*datalen = 0;
if(filename) {
ret = ssh2_file_to_blob(session, filename, &filedata, &filedata_len);
src/pem.c:354
- In the
LIBSSH2_MD5_PEMdisabled branch,ssh2_err(...)is called but its return value is not assigned toret, so the function returns-1instead of the specific error code. Assigning it keeps return codes consistent with other error paths in this function (e.g. unsupported algorithm).
#else
ssh2_err(session, LIBSSH2_ERROR_ALGO_UNSUPPORTED,
"Unable to decrypt PEM, MD5 not enabled");
goto out;
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (4)
src/pem.c:371
filedataholds the raw private-key file contents (loaded viassh2_file_to_blob()), but it is freed without being scrubbed. This can leave key material in heap memory after parsing succeeds/fails.
if(filedata)
SSH2_FREE(session, filedata);
src/pem.c:797
- When
ssh2_openssh_pem_parse()loads a key fromfilename,filedatais freed without being scrubbed. Since this buffer contains raw private key material, it should be explicitly zeroed before freeing.
if(filedata)
SSH2_FREE(session, filedata);
src/os400qc3.c:2062
blobcontains the full private-key file contents (PEM/DER) and is freed without being scrubbed. Since this is sensitive material, explicitly zero the buffer (including the extra NUL terminator added byssh2_file_to_blob()) before freeing.
SSH2_FREE(session, blob);
src/pem.c:180
ssh2_pem_parse()can be called with either afilenameor a non-NULLblob+blob_len, but unlikessh2_openssh_pem_parse()it does not validate that at least one source is provided. If it is ever called withfilename == NULLandblob_len > 0butblob == NULL, it will dereference a NULL pointer inpem_readline(). Add an explicit guard like the OpenSSH parser has.
*data = NULL;
*datalen = 0;
if(filename) {
ret = ssh2_file_to_blob(session, filename, &filedata, &filedata_len);
if(ret)
goto out;
blob = filedata;
blob_len = filedata_len;
}
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 7 out of 7 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
src/pem.c:176
ssh2_pem_parse()dereferencesblobinsidepem_readline()without validating that a non-NULL blob was provided whenfilenameis NULL. If a caller accidentally passesblob_len > 0withblob == NULL(or otherwise violates the filename/blob contract), this will crash via a NULL dereference.ssh2_openssh_pem_parse()already defends against this case; it would be safer forssh2_pem_parse()to do the same and return a proper libssh2 error instead of crashing.
*data = NULL;
*datalen = 0;
if(filename) {
To further merge file/blob codepaths/behavior, and to reduce branching
at callers.
Also:
ssh2_file_to_blob()to load a (PEM) file intomemory. Use it internally.
Make sure to null-terminate the returned blob, primarily for mbedTLS.
ssh2_file_to_blob().LIBSSH2_ERROR_ALGO_UNSUPPORTEDinssh2_pem_parse()in no-MD5 builds (was: -1).try_pem_load().Note:
try_pem_load()has unique behavior of allowing multipleinstances 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 frompem.c. Italso 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 unifyos400_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
e.g. in
ssh2_rsa_new_priv(). → Separate PR.