Skip to content

src: disable SHA1-based hostkey methods and signatures by default, fix fallouts - #2501

Closed
vszakats wants to merge 21 commits into
libssh2:masterfrom
vszakats:rsa-sha1
Closed

vszakats wants to merge 21 commits into
libssh2:masterfrom
vszakats:rsa-sha1

Conversation

@vszakats

@vszakats vszakats commented Aug 3, 2026 •

Copy link
Copy Markdown
Member

They are ssh-rsa and [email protected], and insecure due
to their reliance on SHA1 hashes.

Also:

  • hostkey: fix to accept all RSA hostkey methods in common
    hostkey_method_ssh_rsa_init().
  • mbedtls: add missing RSA SHA1/SHA2 guards.
  • mbedtls: cleanup on error in mbed_pub_priv_key().
  • os400qc3: add missing RSA SHA1/SHA2 guards. (untested)
  • wincng: add missing RSA SHA1/SHA2 guards.
  • crypto.h: add missing #endif comments.
  • GHA: move stray -DLIBSSH2_DSA_ENABLE to its place.
    Follow-up to b7ab0fa disable DSA by default #1435
  • tests: fix to guard RSA tests with LIBSSH2_RSA, replacing
    LIBSSH2_RSA_SHA1 to keep test coverage. Keep using the latter for
    testing SHA1-based signatures in test_auth_pubkey_ok_rsa_signed.

Follow-up to 3e14d67 #2444
Follow-up to 99f9353 #2443


https://github.com/libssh2/libssh2/pull/2501/files?w=1

@vszakats vszakats changed the title [WIP] try disabling SHA1-based hostkeys by default hostkey: disable SHA1-based hostkeys by default Aug 3, 2026
@vszakats vszakats changed the title hostkey: disable SHA1-based hostkeys by default hostkey: disable SHA1-based methods by default Aug 3, 2026
@vszakats vszakats changed the title hostkey: disable SHA1-based methods by default hostkey: disable SHA1-based methods by default, fix fallouts Aug 3, 2026
@vszakats
vszakats requested a review from Copilot August 3, 2026 13:20

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 hardens libssh2’s RSA hostkey handling by disabling SHA1-based RSA hostkey methods (ssh-rsa, [email protected]) by default, while adding/adjusting build guards across multiple crypto backends to prevent fallout when SHA1 (and/or RSA) is disabled.

Changes:

  • Disable SHA1-based RSA hostkey methods by default, with a new opt-in build define (LIBSSH2_RSA_SHA1_ENABLE) and updated release notes/CI coverage.
  • Make hostkey_method_ssh_rsa_init() accept all relevant RSA key header types in common code paths.
  • Add missing RSA/SHA1 and RSA/SHA2 compile guards across several crypto backends (mbedTLS, OS/400 QC3, Windows CNG) and tidy #endif comments.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/wincng.c Add LIBSSH2_RSA_SHA1 / LIBSSH2_RSA_SHA2 guards around RSA sign entrypoints in the Windows CNG backend.
src/os400qc3.h Add missing RSA / SHA1 / SHA2 guards for OS/400 QC3 RSA macros and declarations.
src/os400qc3.c Add missing RSA guard(s) and SHA1/SHA2 guards for OS/400 QC3 RSA verify functions.
src/mbedtls.c Wrap RSA-related functions with LIBSSH2_RSA guards in the mbedTLS backend.
src/hostkey.c Simplify RSA hostkey init header-type acceptance to handle all RSA variants in common code.
src/crypto.h Clarify #endif comments for RSA/DSA blocks.
src/crypto_config.h Switch to opt-in LIBSSH2_RSA_SHA1_ENABLE to keep SHA1 RSA hostkey methods disabled by default.
RELEASE-NOTES Document the default disablement and the opt-in flag for SHA1-based RSA hostkey methods.
.github/workflows/ci.yml Ensure legacy-options CI runs explicitly opt back into RSA-SHA1 (and place DSA enable flag appropriately).
Suppressed comments (3)

src/mbedtls.c:585

  • mbed_gen_publickey_from_rsa() is now inside #if LIBSSH2_RSA, but it is still called unconditionally from mbed_pub_priv_key() (e.g., line 612). When building with LIBSSH2_NO_RSA this will result in an undefined reference at link time. Either keep mbed_gen_publickey_from_rsa() available in non-RSA builds (stub that returns NULL / sets an error) or extend the #if LIBSSH2_RSA guard to cover the callers as well.
                             size_t *pubkeydata_len,
                             mbedtls_pk_context *pkey)
{

src/os400qc3.c:2403

  • These RSA verify helpers are guarded by LIBSSH2_RSA_SHA2 / LIBSSH2_RSA_SHA1 only. Because the backend headers define those as 1 by default, a LIBSSH2_NO_RSA build can still compile this RSA code even though RSA is disabled, potentially causing dead code and missing-prototype warnings. Consider also gating these blocks with LIBSSH2_RSA (and ideally the surrounding RSA-only helpers, including ssh2_rsa_new_priv() / ssh2_os400qc3_rsa_signv()).
#if LIBSSH2_RSA_SHA2
int ssh2_rsa_sha2_verify(ssh2_rsa_ctx *rsa, size_t hash_len,
                         const unsigned char *sig, size_t sig_len,
                         const unsigned char *m, size_t m_len)
{

src/os400qc3.c:2438

  • Same as the SHA2 block above: this SHA1 verify wrapper should also be gated with LIBSSH2_RSA so RSA-disabled builds don't compile RSA-only code just because LIBSSH2_RSA_SHA1 defaults to 1 in the backend header.
int ssh2_rsa_sha1_verify(ssh2_rsa_ctx *rsa,
                         const unsigned char *sig, size_t sig_len,
                         const unsigned char *m, size_t m_len)
{

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

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

Comment thread src/mbedtls.c Outdated
Comment on lines 592 to 598
#if LIBSSH2_RSA
if(mbedtls_pk_get_type(pkey) != MBEDTLS_PK_RSA)
#endif
{
mbedtls_pk_free(pkey);
return ssh2_err(session, LIBSSH2_ERROR_FILE, "Key type not supported");
}

@vszakats vszakats Aug 3, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not like this, but sure. No-RSA builds are very low priority, the LIBSSH2_RSA and LIBSSH2_RSA_SHA2 guards are rather informational, than something we put focus on.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ownership issue is real, fixing in separate PR after merging this.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

→ PR #2502

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

Suppressed comments (1)

RELEASE-NOTES:8

  • This item is labeled as “hostkey methods”, but LIBSSH2_RSA_SHA1 is also used for RSA/SHA1 signatures in publickey auth (e.g. userauth_supported_key_sign_algs() conditionally includes ,ssh-rsa). Consider broadening the wording so it’s clear this option affects RSA/SHA1 signature methods beyond hostkey negotiation.
  - SHA1-based hostkey methods: `ssh-rsa`, `[email protected]`
    You can enable them with `-DLIBSSH2_RSA_SHA1_ENABLE`.

Comment thread src/mbedtls.c Outdated
Comment on lines 594 to 597
{
mbedtls_pk_free(pkey);
return ssh2_err(session, LIBSSH2_ERROR_FILE, "Key type not supported");
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, sure. In another PR.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

→ PR #2502

@vszakats vszakats changed the title hostkey: disable SHA1-based methods by default, fix fallouts hostkey: disable SHA1-based methods and signature by default, fix fallouts Aug 3, 2026
@vszakats vszakats changed the title hostkey: disable SHA1-based methods and signature by default, fix fallouts hostkey: disable SHA1-based methods and signatures by default, fix fallouts Aug 3, 2026
vszakats added a commit that referenced this pull request Aug 3, 2026
"`mbed_pub_priv_key()` frees `pkey` on the unsupported-key path, but the
caller (`ssh2_pub_privkey()`) unconditionally calls
`mbedtls_pk_free(&pkey)` after this helper returns. That makes this a
double-free on the error path (memory corruption). The helper should not
free `pkey`; leave ownership/cleanup to the caller."

Reported by Copilot
Bug: #2501 (comment)
Bug: #2501 (comment)
Follow-up to 186f1a2 #132

Closes #2502

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

@vszakats vszakats changed the title hostkey: disable SHA1-based methods and signatures by default, fix fallouts src: disable SHA1-based hostkey methods and signatures by default, fix fallouts Aug 3, 2026
@vszakats vszakats closed this in 079eeb8 Aug 3, 2026
@vszakats
vszakats deleted the rsa-sha1 branch August 3, 2026 14:44
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