Repository navigation
Conversation
To fix `ftell()` returning the size in bytes. Follow-up to ef6eaad libssh2#235
There was a problem hiding this comment.
Pull request overview
This PR changes how key files are opened so that _libssh2_pem_parse() can rely on ftell()/fread() operating on raw bytes (avoiding text-mode newline translation discrepancies).
Changes:
- Open PEM/private key files in binary mode (
"rb") in the WinCNG backend. - Open public key
.pubfiles in binary mode inuserauth. - Open RSA/DSA private key files in binary mode in the libgcrypt backend.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/wincng.c |
Switch PEM key file opens to "rb" before calling _libssh2_pem_parse(). |
src/userauth.c |
Switch public key file open to "rb" for deterministic byte reads. |
src/libgcrypt.c |
Switch RSA/DSA private key file opens to "rb" before PEM parsing. |
Comments suppressed due to low confidence (3)
src/wincng.c:897
- Opening PEM key files in binary mode keeps CRLF sequences intact.
_libssh2_pem_parse()reads the whole file and then usesreadline_memory()insrc/pem.c, which currently treats\r\nas two line breaks (consumes only one byte), causing an empty line right after the BEGIN header. For passphrase-protected PEM keys this prevents detection of theProc-Type: 4,ENCRYPTEDline and the parse fails. Consider either fixingreadline_memory()to consume CRLF as a single newline (or normalizing\r\nto\nafterfread) before switching to binary mode here.
fp = fopen(filename, "rb");
if(!fp) {
return -1;
}
ret = _libssh2_pem_parse(session, headerbegin, headerend,
passphrase,
fp, data, datalen);
src/libgcrypt.c:337
- Switching this fopen to binary mode will feed
_libssh2_pem_parse()a buffer containing CRLF on Windows (or on checkouts with autocrlf).readline_memory()insrc/pem.cdoes not currently consume\r\nas a single newline, which can break parsing of encrypted PEM keys (it may miss theProc-Type: 4,ENCRYPTEDline and then try to base64-decode it). Recommend updating the PEM line reader to handle CRLF properly (or normalizing line endings afterfread) to avoid regressions.
fp = fopen(filename, "rb");
if(!fp) {
return -1;
}
ret = _libssh2_pem_parse(session,
"-----BEGIN RSA PRIVATE KEY-----",
"-----END RSA PRIVATE KEY-----",
passphrase,
fp, &data, &datalen);
src/libgcrypt.c:457
- Same concern as above: binary-mode reads preserve CRLF, but
_libssh2_pem_parse_memory()'sreadline_memory()currently treats\r\nas two separators, which can break encrypted PEM parsing. If switching to binary mode is required to makeftell()/fread()agree, the PEM in-memory line reader should be made CRLF-aware.
fp = fopen(filename, "rb");
if(!fp) {
return -1;
}
ret = _libssh2_pem_parse(session,
"-----BEGIN DSA PRIVATE KEY-----",
"-----END DSA PRIVATE KEY-----",
passphrase,
fp, &data, &datalen);
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
vszakats
added a commit
to vszakats/libssh2
that referenced
this pull request
Apr 25, 2026
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.
To fix
ftell()returning the size in bytes in_libssh2_pem_parse().Ref: #1880 (comment)
Follow-up to ef6eaad #235