Skip to content

openssl: replace ossl_*_openssh_priv_new() functions with ossl_key_from_openssh() - #2396

Closed
vszakats wants to merge 7 commits into
libssh2:masterfrom
vszakats:pemmerge4
Closed

vszakats wants to merge 7 commits into
libssh2:masterfrom
vszakats:pemmerge4

Conversation

@vszakats

@vszakats vszakats commented Jul 22, 2026 •

Copy link
Copy Markdown
Member

Merging ossl_*_openssh_priv_new() functions and syncing the result up
with ossl_key_from_openssh() ends up with an almost identical function
(with the newly merged ones supporting filenames, not blobs). Therefore
replace these calls with the latter, and drop the redundant code.

This also syncs up recognized keys loaded from filenames with those
loaded from memory buffers.

Also:

Follow-up to 3bb068e #2394

Copilot AI review requested due to automatic review settings July 22, 2026 15:39

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 refactors the OpenSSL backend’s OpenSSH private-key loading by consolidating the per-algorithm ossl_*_openssh_priv_new() helpers into a single shared ossl_key_from_openssh_file() implementation, reducing duplicated parsing logic across RSA/DSA/ECDSA/ED25519 key loaders.

Changes:

  • Introduces ossl_key_from_openssh_file() and routes RSA/DSA/ECDSA private-key-from-file fallbacks through it.
  • Simplifies ssh2_ed25519_new_priv() control flow to use the shared file loader and a common cleanup section.
  • Removes the now-redundant per-algorithm ossl_*_openssh_priv_new() helpers.

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

Comment thread src/openssl.c Resolved
Comment thread src/openssl.c Resolved
Comment thread src/openssl.c Resolved
Copilot AI review requested due to automatic review settings July 22, 2026 15:56

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

Comments suppressed due to low confidence (1)

src/openssl.c:3456

  • ossl_key_from_openssh_file() applies want_method filtering for most key types, but the ECDSA security-key branch ([email protected]) ignores want_method. That can allow a caller that expects ssh-ecdsa to successfully parse a sk-ecdsa-* private key (algorithm mismatch).
    if(!strcmp("[email protected]", (const char *)buf))

Comment thread src/openssl.c Resolved
Comment thread src/openssl.c Resolved
Copilot AI review requested due to automatic review settings July 22, 2026 16:01

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

Comments suppressed due to low confidence (3)

src/openssl.c:1981

  • ossl_key_from_openssh() expects a void ** output parameter. Passing (void **)ctx passes a NULL/invalid address, so the parsed ED25519 key context is not returned (and can lead to returning success with *ed_ctx == NULL). Pass &ctx so the callee can store the allocated key into the local ctx that is later published in cleanup:.
    if(filename)
        rc = ossl_key_from_openssh(session, (void **)ctx, "ssh-ed25519",
                                   NULL, NULL, NULL,
                                   filename, NULL, 0, passphrase);
    else {

src/openssl.c:2015

  • On the OpenSSH-blob fallback path, the key is currently parsed into ed_ctx, but cleanup: then overwrites *ed_ctx with the local ctx (still NULL here), discarding the successfully parsed key and leaking it. Parse into local ctx (like the PEM path) so cleanup: publishes the correct value.
        rc = ossl_key_from_openssh(session, (void **)ed_ctx, "ssh-ed25519",
                                   NULL, NULL, NULL,
                                   NULL, blob, blob_len, passphrase);
    }

cleanup:

    if(ed_ctx)
        *ed_ctx = ctx;
    else
        ssh2_ed25519_free(ctx);

src/openssl.c:2901

  • ssh2_ecdsa_new_priv() now falls back to ossl_key_from_openssh(..., want_method="ssh-ecdsa", ...), but ossl_key_from_openssh() accepts [email protected] without checking want_method. That means an OpenSSH ECDSA-SK key can be returned here as a plain ssh2_ecdsa_ctx (without the SK application/key_handle needed for signing), causing a later signing failure after this function reports success. ossl_key_from_openssh() should gate the SK-ECDSA branch the same way it gates other key types (only when want_method is NULL or matches the SK method).
    if(!*ec_ctx)
        rc = ossl_key_from_openssh(session, (void **)ec_ctx, "ssh-ecdsa",
                                   NULL, NULL, NULL,
                                   filename, blob, blob_len, passphrase);

@vszakats vszakats changed the title openssl: unify ossl_*_openssh_priv_new() functions openssl: replace ossl_*_openssh_priv_new() functions with ossl_key_from_openssh() Jul 22, 2026
Copilot AI review requested due to automatic review settings July 22, 2026 16:12

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

Comments suppressed due to low confidence (1)

src/openssl.c:2007

  • In the blob path, ossl_key_from_openssh() is called with (void **)ed_ctx, but the following cleanup: block always overwrites the caller output with local ctx (which is still NULL on this path). This discards a successfully parsed OpenSSH key and can leak the allocated key context.
        rc = ossl_key_from_openssh(session, (void **)ed_ctx, "ssh-ed25519",
                                   NULL, NULL, NULL,
                                   NULL, blob, blob_len, passphrase);

Copilot AI review requested due to automatic review settings July 22, 2026 16:30

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 1 out of 1 changed files in this pull request and generated 1 comment.

Comment thread src/openssl.c Resolved
Copilot AI review requested due to automatic review settings July 22, 2026 16:34

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

@vszakats vszakats closed this in ac9a71a Jul 22, 2026
@vszakats
vszakats deleted the pemmerge4 branch July 22, 2026 18:00
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