Repository navigation
Support RSA SHA2 cert-based authentication (rsa-sha2-512_cert and rsa-sha2-256_cert) - #1314
Conversation
…he supported RSA cert based algs in server-sig-algs extension, fallback to default othwerwise
|
@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? |
|
@Tejaswikandula No, the only types it supports are |
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? |
Correct.
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. |
|
This is what comes back from 9.4:
|
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 |
That's true. Though OpenSSH supports cert based authentication, it's not listing the same in server-sig-algs |
|
Yes, that is true, but I believe you're conflating |
…on differently based on the remote server version
…on differently based on the remote server version
|
@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? |
|
Thank you @Tejaswikandula for this, also for your patience. To @willco007 for all the help. |
…-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
Follow-up to 3a6ab70 libssh2#1314
| /* 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 */ |
There was a problem hiding this comment.
@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)
Follow-up to 3a6ab70 libssh2#1314
- 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
- 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
There was a problem hiding this comment.
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.
| if(!strncmp("[email protected]", | ||
| method, | ||
| method_len) || | ||
| !strncmp("[email protected]", | ||
| method, | ||
| method_len)) { | ||
| return 12; | ||
| } |
| 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) | ||
| ) { |
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
Follow-up to 3a6ab70 libssh2#1314
Follow-up to 3a6ab70 libssh2#1314
There was a problem hiding this comment.
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'
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
…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
Replicating OpenSSH's behavior to handle RSA certificate authentication differently based on the remote server version.
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