Skip to content

Support RSA SHA2 cert-based authentication (rsa-sha2-512_cert and rsa-sha2-256_cert) - #1314

Merged
vszakats merged 41 commits into
libssh2:masterfrom
Tejaswikandula:master
Apr 3, 2024
Merged

vszakats merged 41 commits into
libssh2:masterfrom
Tejaswikandula:master

Conversation

@Tejaswikandula

@Tejaswikandula Tejaswikandula commented Jan 24, 2024 •

Copy link
Copy Markdown
Contributor

Replicating OpenSSH's behavior to handle RSA certificate authentication differently based on the remote server version.

  1. For OpenSSH versions >= 7.8, ascertain server's support for RSA Cert types by checking if the certificate's signature type is present in the 'server-sig-algs'.
  2. For OpenSSH versions < 7.8, Set the "SSH_BUG_SIGTYPE" flag when the RSA key in question is a certificate to ignore "server-sig-algs" and only offer ssh-rsa signature algorithm for RSA certs.
    This arises from the fact that OpenSSH versions up to 7.7 accept RSA-SHA2 keys but not RSA-SHA2 certificate types. Although OpenSSH <=7.7 includes RSA-SHA2 keys in the server-sig-algs, versions <=7.7 do not actually support RSA certs. Therefore, server sending RSA-SHA2 keys in server-sig-algs should not be interpreted as indicating support for RSA-SHA2 certs. So, server-sig-algs are ignored when the RSA key in question is a cert, and the remote server version is 7.7 or below.

Relevant sections of the OpenSSH source code:

https://github.com/openssh/openssh-portable/blob/V_8_9_P1/sshconnect2.c#L1191-L1197
https://github.com/openssh/openssh-portable/blob/master/compat.c#L43

sshconnect2.c
compat.c

Comment thread tests/gen_keys.sh Resolved
…he supported RSA cert based algs in server-sig-algs extension, fallback to default othwerwise
@Tejaswikandula

Copy link
Copy Markdown
Contributor Author

@willco007 Did changes to allow algo upgrades for SSH RSA certs if the server also enumerates the supported RSA cert based algs in server-sig-algs extension, fallback to default otherwise. Thoughts/Suggestions?

@willco007

Copy link
Copy Markdown
Member

@Tejaswikandula No, the only types it supports are rsa-sha2-256 and rsa-sha2-512. At the time, these were the only upgrades OpenSSH supported, I'm not sure if OpenSSH now supports cert upgrades as well.

@Tejaswikandula

Copy link
Copy Markdown
Contributor Author

@Tejaswikandula No, the only types it supports are rsa-sha2-256 and rsa-sha2-512. At the time, these were the only upgrades OpenSSH supported, I'm not sure if OpenSSH now supports cert upgrades as well.

RFC 8332 mentions allowing for interoperable use of existing and new RSA keys with SHA-256 and SHA-512. Is this what you are referring to when you say "these were the only upgrades OpenSSH supported"?

While RFC 8332 doesn't explicitly discuss RSA certificates, RSA certs align with the hashing types used in RSA keys right? So, in case of certs, even though OpenSSH doesn't explicitly announce RSA certs in the server-sig-algs extension for flexible negotiation during key exchange process, can we infer that the support for RSA SHA-2 certificate-based authentications in newer OpenSSH versions indicates the potential for certificate upgrades?

@willco007

Copy link
Copy Markdown
Member

RFC 8332 mentions allowing for interoperable use of existing and new RSA keys with SHA-256 and SHA-512. Is this what > you are referring to when you say "these were the only upgrades OpenSSH supported"?

Correct.

...can we infer that the support for RSA SHA-2 certificate-based authentications in newer OpenSSH versions indicates the potential for certificate upgrades?

No, we can't infer this. When reviewing the OpenSSH server code while implementing this, it did not have RSA cert support. New versions may have support, but that would need to be confirmed.

@willco007

willco007 commented Feb 5, 2024 •

Copy link
Copy Markdown
Member

This is what comes back from 9.4:

Key Ex: Server to Client extension server-sig-algs: ssh-ed25519,[email protected],ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,[email protected],[email protected],ssh-dss,ssh-rsa,rsa-sha2-256,rsa-sha2-512.
I see no mention of cert support.

@Tejaswikandula

Copy link
Copy Markdown
Contributor Author

RFC 8332 mentions allowing for interoperable use of existing and new RSA keys with SHA-256 and SHA-512. Is this what > you are referring to when you say "these were the only upgrades OpenSSH supported"?

Correct.

...can we infer that the support for RSA SHA-2 certificate-based authentications in newer OpenSSH versions indicates the potential for certificate upgrades?

No, we can't infer this. When reviewing the OpenSSH server code while implementing this, it did not have RSA cert support. New versions may have support, but that would need to be confirmed.

OpenSSH do support RSA certs from a long time and newer versions do also support RSA SHA2 certs. OpenSSH deprecated support for SHA1 certs along with the keys. Please refer to this PR - https://github.com/openssh/openssh-portable/commit/2344750250247111a6c3c6a4fe84ed583a61cc11. I'm able to successfully test using certs against different OpenSSH versions

@Tejaswikandula

Copy link
Copy Markdown
Contributor Author

This is what comes back from 9.4:

Key Ex: Server to Client extension server-sig-algs: ssh-ed25519,[email protected],ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,[email protected],[email protected],ssh-dss,ssh-rsa,rsa-sha2-256,rsa-sha2-512. I see no mention of cert support.

That's true. Though OpenSSH supports cert based authentication, it's not listing the same in server-sig-algs

@willco007

Copy link
Copy Markdown
Member

Yes, that is true, but I believe you're conflating server-sig-algs list and normal key auth. The server-sig-algs list is used to upgrade insecure keys while certs are used for normal key auth which is what you have implemented in this MR.

@Tejaswikandula

Copy link
Copy Markdown
Contributor Author

@vszakats Requesting review again on this one. Also, the build is failing with docker error unrelated to my changes (probably flaky), can you help with restarting the build?

@vszakats
vszakats merged commit 3a6ab70 into libssh2:master Apr 3, 2024
@vszakats

vszakats commented Apr 3, 2024

Copy link
Copy Markdown
Member

Thank you @Tejaswikandula for this, also for your patience. To @willco007 for all the help.
Merged now!

agreppin pushed a commit to agreppin/libssh2 that referenced this pull request Jul 14, 2024
…-sha2-256_cert) (libssh2#1314)

Replicating OpenSSH's behavior to handle RSA certificate authentication
differently based on the remote server version.

1. For OpenSSH versions >= 7.8, ascertain server's support for RSA Cert
   types by checking if the certificate's signature type is present in
   the `server-sig-algs`.

2. For OpenSSH versions < 7.8, Set the "SSH_BUG_SIGTYPE" flag when the
   RSA key in question is a certificate to ignore `server-sig-algs` and
   only offer ssh-rsa signature algorithm for RSA certs.

This arises from the fact that OpenSSH versions up to 7.7 accept
RSA-SHA2 keys but not RSA-SHA2 certificate types. Although OpenSSH <=7.7
includes RSA-SHA2 keys in the `server-sig-algs`, versions <=7.7 do not
actually support RSA certs. Therefore, server sending RSA-SHA2 keys in
`server-sig-algs` should not be interpreted as indicating support for
RSA-SHA2 certs. So, `server-sig-algs` are ignored when the RSA key in
question is a cert, and the remote server version is 7.7 or below.

Relevant sections of the OpenSSH source code:
                                                                                          
<https://github.com/openssh/openssh-portable/blob/V_8_9_P1/sshconnect2.c#L1191-L1197>     
<https://github.com/openssh/openssh-portable/blob/master/compat.c#L43>                    
                                                                                          
Assisted-by: Will Cosgrove                                                                
Reviewed-by: Viktor Szakats
vszakats added a commit to vszakats/libssh2 that referenced this pull request Sep 13, 2025
@vszakats vszakats mentioned this pull request Sep 13, 2025
vszakats added a commit that referenced this pull request Sep 14, 2025
- userauth: fix NULL dereference when out-of-memory.
  Also fix indentation.
  Follow-up to 3a6ab70 #1314

- openssl: drop redundant NULL check and logic.
  Follow-up to ed439a2 #698

Pointed out by CodeQL

Closes #1656
vszakats added a commit to vszakats/libssh2 that referenced this pull request Apr 21, 2026
Comment thread src/userauth.c
/* Set "SSH_BUG_SIGTYPE" flag when the remote server version is OpenSSH 7.7
or lower and when the RSA key in question is a certificate to ignore
"server-sig-algs" and only offer ssh-rsa signature algorithm for
RSA certs */

@vszakats vszakats Apr 22, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@Tejaswikandula Do you recall what this section of code is meant to do?

The comment (and linked openssh code) refers to SSH_BUG_SIGTYPE as a flag, but here it's a local variable that's set then checked right away, and never read again. Is this how it's intended?

Also, is the memcmp() call accurate? shouldn't it be compared to zero for a positive match? (there are two memcmp() calls like this)

vszakats added a commit to vszakats/libssh2 that referenced this pull request Apr 23, 2026
vszakats added a commit that referenced this pull request Apr 24, 2026
- dedupe constant, and replace magic number with `sizeof()`.
- drop useless check for non-NULL `filtered_algs` on exit.
- fix leaking `filtered_algs` on an error path.
- fix to pass `*key_method` to `memcmp()` (was: `key_method`).
- fix to compare `memcmp()` against `0` (was: non-zero).
- add missing NULL-check before using `*key_method`.
- drop redundant `*key_method` NULL-check after use.
- scope variables.
- avoid camelcase in variable name.
- fix formatting.

Ref: #1314 (comment)
Ref: #1877 (comment)
Ref: #1877 (comment)
Follow-up to 3a6ab70 #1314

Closes #1881
vszakats added a commit that referenced this pull request Apr 25, 2026
- size the `filtered-algs` buffer by the server list rather than the
  supported list, since the output is always a subset of the server list
  byte-for-byte.
  Report-and-patch-by: Kit Knox

- use `strttol()` to extract minor version too.
  Pointed out by GitHub Code Quality.

Also:
- rename a variable.
- simplify an expression.
- inline an `if` block.
- drop redundant parentheses.

Follow-up to ba2a7ff #1881
Follow-up to 3a6ab70 #1314

Closes #1885
@vszakats
vszakats requested a review from Copilot July 8, 2026 09:28

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

Adds support for RSA-SHA2 certificate-based public key authentication ([email protected], [email protected]), aiming to mirror OpenSSH’s version-dependent behavior around server-sig-algs and older OpenSSH servers.

Changes:

  • Extends algorithm handling to recognize RSA-SHA2 cert method names and (attempt to) upgrade signing methods accordingly.
  • Adds RSA-SHA2 cert hostkey method registrations.
  • Introduces a Docker test plus new test keys/certs and key generation script updates.

Reviewed changes

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

Show a summary per file
File Description
tests/test_auth_pubkey_ok_rsa_sha2_256_signed.c New Docker test covering RSA-SHA2 cert authentication flow.
tests/Makefile.inc Wires the new Docker test and distributes new key fixtures.
tests/key_rsa_sha2_256_signed.pub Adds RSA public key fixture for the new test.
tests/key_rsa_sha2_256_signed-cert.pub Adds RSA certificate public key fixture for the new test.
tests/key_rsa_sha2_256_signed Adds RSA private key fixture for the new test.
tests/gen_keys.sh Updates key generation to create the new RSA-SHA2 cert fixtures.
src/userauth.c Implements RSA-SHA2 cert method handling and OpenSSH version-based behavior (contains critical logic/memory issues).
src/openssl.c Extends RSA-SHA2 upgrade support to include [email protected] for the OpenSSL backend.
src/hostkey.c Registers RSA-SHA2 cert hostkey methods.

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

Comment thread src/userauth.c
Comment on lines +1267 to +1274
if(!strncmp("[email protected]",
method,
method_len) ||
!strncmp("[email protected]",
method,
method_len)) {
return 12;
}
Comment thread src/userauth.c Resolved
Comment thread src/userauth.c Resolved
Comment thread src/openssl.c
Comment on lines +5137 to +5142
if((key_method_len == 7 &&
memcmp(key_method, "ssh-rsa", key_method_len) == 0) ||
(key_method_len == 28 &&
memcmp(key_method, "[email protected]",
key_method_len) == 0)
) {

@vszakats vszakats Jul 8, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

→ PR #2252 (failed attempt)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

→ PR #2483

Comment thread src/userauth.c Resolved
vszakats added a commit that referenced this pull request Jul 8, 2026
They were shared between all crypto backends, except openssl, which
supports an extra key method. Retain this exception by using the
`LIBSSH2_OPENSSL` guards. This is a workaround, but I deem it fine, due
to already existing evidence for per-crypto-backend logic in non-crypto
code, and the codebase already making this exception in tests.

A better option would be introducing a crypto-feature macro flag, maybe
in a future commit. Perhaps also worth a check whether the exception is
necessary.

Ref: 3a6ab70 #1314
Follow-up to 82ffc1b #2248

Closes #2249
vszakats added a commit to vszakats/libssh2 that referenced this pull request Aug 1, 2026
Follow-up to 3a6ab70 libssh2#1314
vszakats added a commit to vszakats/libssh2 that referenced this pull request Aug 1, 2026
Follow-up to 3a6ab70 libssh2#1314
@vszakats
vszakats requested a review from Copilot August 2, 2026 08:46

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 no new comments.

Suppressed comments (4)

src/userauth.c:1331

  • is_version_less_than_78() still only derives the minor version from a single character ((endptr + 1)[0] - '0') and doesn’t validate that it’s a digit. This can misclassify versions like "7.10" (or banners with non-digit suffixes) and incorrectly set SSH_BUG_SIGTYPE.
    major = strtol(version, &endptr, 10);
    if(!endptr || *endptr != '.')
        return 0; /* Not a valid number */
    minor = (endptr + 1)[0] - '0';
    if((major >= 1 && major <= 6) ||

src/userauth.c:1405

  • The SSH_BUG_SIGTYPE early-return check is using memcmp(key_method, ...) where key_method is an unsigned char** (should be *key_method) and the memcmp result isn’t compared to 0, so the condition triggers for non-certs. Also, returning here leaks filtered_algs.
            SSH_BUG_SIGTYPE = is_version_less_than_78(remote_ver);
            if(SSH_BUG_SIGTYPE && *key_method_len == 28 &&
               memcmp(key_method, "[email protected]",
                      *key_method_len)) {
                return LIBSSH2_ERROR_NONE;

src/userauth.c:1481

  • The cert-upgrade block in _libssh2_key_sign_algorithm() has multiple correctness bugs: it compares against the cert method using memcmp(key_method, ...) instead of memcmp(*key_method, ...) and doesn’t check == 0, so it upgrades the wrong cases. It also checks allocation failure with if(!key_method) instead of if(!*key_method).
        if(*key_method_len == 28 &&
        memcmp(key_method, "[email protected]", *key_method_len)) {
            if(*key_method)
                LIBSSH2_FREE(session, *key_method);
            *key_method = LIBSSH2_ALLOC(session, match_len + suffix_len);

tests/gen_keys.sh:42

  • The newly generated "key_rsa_sha2_256_signed-cert" is intended to be SHA256-signed, but the ssh-keygen signing command doesn’t specify a certificate hashalg. That can result in a cert that isn’t actually RSA-SHA2-256-signed, reducing the value of the new SHA2-256-specific test vectors.
ssh-keygen -t rsa     -b 4096 -N ''                 -C 'key_rsa_sha2_256_signed' -f 'key_rsa_sha2_256_signed'
ssh-keygen                    -I "${id}" -n "${pr}" -s 'openssh_server/ca_rsa'      'key_rsa_sha2_256_signed.pub'

vszakats added a commit that referenced this pull request Aug 2, 2026
The committed key was in reality `rsa-sha2-512`, not `rsa-sha2-256`, as
the filenames suggested.

Also set the type explicitly to `rsa-sha2-512`, to avoid relying on
ssh-keygen's default (`rsa-sha2-512`).

```shell
$ grep -E -o ' .+ ' id_rsa_sha2_512_signed-cert.pub | base64 -d | strings | grep -i rsa
```

Bug: #2474
Follow-up to 3a6ab70 #1314

Closes #2479
vszakats added a commit that referenced this pull request Aug 2, 2026
…userauth_supported_key_sign_algs()`, fix tests

The PR implementing this did not add anything OpenSSL-specific, yet it
gated the feature OpenSSL only. Turns out the reason for this is that
the test RSA keys are in OpenSSH format, which is only supported by
OpenSSL (and wolfSSL) in libssh2.

Fix by converting the two test keys to PEM format, making them
compatible with all backends. (RSA OpenSSH key format is already tested
separately.)

Also: this also fixes outdated comment fields present in these keys.

Bug: #1314 (comment)
Ref: #2252 (initial fix attempt)
Follow-up to 1410efa #2249
Follow-up to 3a6ab70 #1314

Closes #2483
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants