Skip to content

src: open key files in binary mode - #1883

Closed
vszakats wants to merge 1 commit into
libssh2:masterfrom
vszakats:fopenbin
Closed

vszakats wants to merge 1 commit into
libssh2:masterfrom
vszakats:fopenbin

Conversation

@vszakats

@vszakats vszakats commented Apr 24, 2026 •

Copy link
Copy Markdown
Member

To fix ftell() returning the size in bytes in _libssh2_pem_parse().

Ref: #1880 (comment)
Follow-up to ef6eaad #235

To fix `ftell()` returning the size in bytes.

Follow-up to ef6eaad libssh2#235

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 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 .pub files in binary mode in userauth.
  • 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 uses readline_memory() in src/pem.c, which currently treats \r\n as 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 the Proc-Type: 4,ENCRYPTED line and the parse fails. Consider either fixing readline_memory() to consume CRLF as a single newline (or normalizing \r\n to \n after fread) 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() in src/pem.c does not currently consume \r\n as a single newline, which can break parsing of encrypted PEM keys (it may miss the Proc-Type: 4,ENCRYPTED line and then try to base64-decode it). Recommend updating the PEM line reader to handle CRLF properly (or normalizing line endings after fread) 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()'s readline_memory() currently treats \r\n as two separators, which can break encrypted PEM parsing. If switching to binary mode is required to make ftell()/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 vszakats closed this in 0ca33b6 Apr 24, 2026
@vszakats
vszakats deleted the fopenbin branch April 24, 2026 07:28
vszakats added a commit to vszakats/libssh2 that referenced this pull request Apr 25, 2026
vszakats added a commit that referenced this pull request Apr 30, 2026
To make `ftell()` return a byte offset.

Also:
- handle `ftell()` returning -1 when loading DER format.

Follow-up to 0ca33b6 #1883

Closes #1884
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