openssl icon indicating copy to clipboard operation
openssl copied to clipboard

TLS 1.3 server certificate selection ignores RSA leaf Key Usage restrictions

Open LiD0209 opened this issue 3 months ago • 2 comments

TLS 1.3 server certificate selection ignores RSA leaf Key Usage restrictions

Summary

OpenSSL can complete a TLS 1.3 server-authenticated handshake with an RSA end-entity certificate whose Key Usage extension is present but does not include digitalSignature. The server still selects that certificate and produces a CertificateVerify signature with rsa_pss_rsae_sha256.

This is the same root cause previously described by the reports for req-a450bc0c604e2ae88ec0, req-d567083032095f769a80, and req-4c12a40e287c7496667a. Those requirements are different extractions of the same RFC 8446 server certificate selection rule, so this report treats them as one finding.

Standard Requirement

  • Official standard: RFC 8446
  • Main section: 4.4.2.2. Server Certificate Selection
  • Supporting sections: 4.2.3. Signature Algorithms, 4.4.3. Certificate Verify
The server's end-entity certificate's public key (and associated
restrictions) MUST be compatible with the selected authentication
algorithm from the client's "signature_algorithms" extension
(currently RSA, ECDSA, or EdDSA).

The certificate MUST allow the key to be used for signing (i.e.,
the digitalSignature bit MUST be set if the Key Usage extension is
present) with a signature scheme indicated in the client's
"signature_algorithms"/"signature_algorithms_cert" extensions

signature_algorithms applies to TLS CertificateVerify signatures, while signature_algorithms_cert applies to signatures in certificates. If signature_algorithms_cert is absent, signature_algorithms also constrains certificate signatures. This distinction does not weaken the end-entity certificate rule: a TLS 1.3 server certificate used for authentication signing must itself permit signing, so a present Key Usage extension must include digitalSignature.

Relevant Source Code

The server state machine reaches signature/certificate selection before continuing the TLS 1.3 handshake:

ssl/statem/statem_srvr.c:2536-2540

if (!s->hit) {
    if (!tls_choose_sigalg(s, 1)) {
        /* SSLfatal already called */
        goto err;
    }

For TLS 1.3, tls_choose_sigalg() delegates to find_sig_alg() and then records the selected certificate and signature algorithm:

ssl/t1_lib.c:4779-4795

int tls_choose_sigalg(SSL_CONNECTION *s, int fatalerrs)
{
    const SIGALG_LOOKUP *lu = NULL;
    int sig_idx = -1;

    s->s3.tmp.cert = NULL;
    s->s3.tmp.sigalg = NULL;

    if (SSL_CONNECTION_IS_TLS13(s)) {
        lu = find_sig_alg(s, NULL, NULL);
        if (lu == NULL) {
            if (!fatalerrs)
                return 1;
            SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
                SSL_R_NO_SUITABLE_SIGNATURE_ALGORITHM);
            return 0;
        }

ssl/t1_lib.c:4915-4920

if (sig_idx == -1)
    sig_idx = lu->sig_idx;
s->s3.tmp.cert = &s->cert->pkeys[sig_idx];
s->cert->key = s->s3.tmp.cert;
s->s3.tmp.sigalg = lu;
return 1;

find_sig_alg() filters TLS 1.3 RSA-PSS candidates for algorithm compatibility and key size, but not for the leaf certificate's Key Usage:

ssl/t1_lib.c:4728-4758

for (i = 0; i < s->shared_sigalgslen; i++) {
    lu = s->shared_sigalgs[i];
    if (lu->hash == NID_sha1
        || lu->hash == NID_sha224
        || lu->sig == EVP_PKEY_DSA
        || lu->sig == EVP_PKEY_RSA
        || !tls_sigalg_compat(s, lu))
        continue;

    if (!tls1_lookup_md(sctx, lu, NULL))
        continue;
    if ((pkey == NULL && !has_usable_cert(s, lu, -1))
        || (pkey != NULL && !is_cert_usable(s, lu, x, pkey)))
        continue;

    tmppkey = (pkey != NULL) ? pkey
                             : s->cert->pkeys[lu->sig_idx].privatekey;

    if (lu->sig == EVP_PKEY_EC) {
        ...
    } else if (lu->sig == EVP_PKEY_RSA_PSS) {
        if (!rsa_pss_check_min_key_size(sctx, tmppkey, lu))
            continue;
    }
    break;
}

The selected-certificate usability check validates private-key signing support for the digest and optional signature_algorithms_cert restrictions on the certificate's issuer signature. It does not check whether the leaf certificate Key Usage permits signing:

ssl/t1_lib.c:4623-4673

static int check_cert_usable(SSL_CONNECTION *s, const SIGALG_LOOKUP *sig,
    X509 *x, EVP_PKEY *pkey)
{
    ...
    supported = EVP_PKEY_digestsign_supports_digest(pkey, sctx->libctx,
        mdname,
        sctx->propq);
    if (supported <= 0)
        return 0;

    if (s->s3.tmp.peer_cert_sigalgs != NULL) {
        if (!X509_get_signature_info(x, &mdnid, &pknid, NULL, NULL))
            return 0;
        ...
        if (mdnid == lu->hash && pknid == lu->sig)
            return 1;
        ...
        return 0;
    }

    return 1;
}

The local certificate parameter check also does not fill the gap for RSA certificates; it returns success for non-EC keys:

ssl/t1_lib.c:1942-1951

static int tls1_check_cert_param(SSL_CONNECTION *s, X509 *x, int check_ee_md)
{
    uint16_t group_id;
    EVP_PKEY *pkey;
    pkey = X509_get0_pubkey(x);
    if (pkey == NULL)
        return 0;
    /* If not EC nothing to do */
    if (!EVP_PKEY_is_a(pkey, "EC"))
        return 1;

OpenSSL's generic X.509 server-purpose check accepts keyEncipherment as sufficient for a TLS/SSL server certificate. That generic policy is broader than the TLS 1.3 signing-specific requirement:

crypto/x509/v3_purp.c:898-919

/*
 * Key usage needed for TLS/SSL server: digital signature, encipherment or
 * key agreement. The ssl code can check this more thoroughly for individual
 * key types.
 */
#define KU_TLS \
    KU_DIGITAL_SIGNATURE | KU_KEY_ENCIPHERMENT | KU_KEY_AGREEMENT

static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
    int non_leaf)
{
    ...
    if (ku_reject(x, KU_TLS))
        return 0;

    return 1;
}

The later CertificateVerify construction uses the previously selected s->s3.tmp.cert and s->s3.tmp.sigalg directly:

ssl/statem/statem_lib.c:324-332

const SIGALG_LOOKUP *lu = s->s3.tmp.sigalg;

if (lu == NULL || s->s3.tmp.cert == NULL) {
    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
    goto err;
}
pkey = s->s3.tmp.cert->privatekey;

Implementation Behavior

For an RSA TLS 1.3 server certificate, OpenSSL accepts the certificate when:

  • the private key can produce a signature for the selected digest;
  • the RSA-PSS key is large enough;
  • any signature_algorithms_cert restriction is satisfied for the certificate's issuer signature;
  • generic server-purpose validation accepts the certificate, which can happen with keyEncipherment alone.

None of those checks require the selected end-entity certificate to include digitalSignature when Key Usage is present. The implementation therefore permits a certificate that can pass broad TLS server-purpose validation but is not permitted for the specific TLS 1.3 signing operation.

Inconsistency Reason

RFC 8446 requires the selected TLS 1.3 server certificate to allow signing with the selected client-indicated authentication algorithm. When Key Usage is present, that means digitalSignature must be set.

OpenSSL instead selects the RSA certificate based on key/signature compatibility and broad server certificate usability, then signs CertificateVerify with the associated private key. An RSA leaf certificate with keyUsage=keyEncipherment and no digitalSignature can therefore be used to generate rsa_pss_rsae_sha256, which the certificate's own restrictions do not authorize.

Runtime Evidence

The issue was reproduced against:

OpenSSL 4.1.0-dev  (Library: OpenSSL 4.1.0-dev )

The reproducer generated a CA certificate and an RSA server leaf certificate in memory, then passed them to s_server and s_client through shell variables and process substitution. No certificate or key files were written for the reproducer.

The server leaf certificate had these relevant properties:

Signature Algorithm: sha256WithRSAEncryption
X509v3 Basic Constraints: critical
    CA:FALSE
X509v3 Key Usage: critical
    Key Encipherment
X509v3 Extended Key Usage:
    TLS Web Server Authentication
X509v3 Subject Alternative Name:
    DNS:localhost
Signature Algorithm: sha256WithRSAEncryption

The client advertised rsa_pss_rsae_sha256:rsa_pkcs1_sha256. This isolates the Key Usage issue: rsa_pkcs1_sha256 permits the certificate's issuer signature, while the TLS 1.3 CertificateVerify signature still uses RSA-PSS.

Observed client result:

s_client exit: 0
CONNECTION ESTABLISHED
Protocol version: TLSv1.3
Ciphersuite: TLS_AES_256_GCM_SHA384
Peer certificate: CN=localhost
Signature type: rsa_pss_rsae_sha256
Verification: OK

This confirms that OpenSSL completed a TLS 1.3 handshake and verified the chain successfully even though the selected server leaf certificate had Key Usage present without digitalSignature.

Impact

The impact is primarily standards compliance and certificate-usage policy enforcement. A server configured with an RSA certificate that is not permitted for signing can still authenticate TLS 1.3 handshakes with that certificate. This can defeat deployments that rely on Key Usage separation, for example separating RSA key-encipherment certificates from RSA signing certificates, and can create interoperability problems with stricter clients or PKI policies.

Fix Direction

Add a TLS 1.3 server-side end-entity certificate restriction check before accepting a certificate for a selected signing authentication algorithm. For X.509 certificates, when Key Usage is present, the selected certificate should require X509_get_key_usage(x) & X509v3_KU_DIGITAL_SIGNATURE before it can be used for CertificateVerify.

The check should cover RSA/RSA-PSS and EdDSA paths as well as ECDSA, and should be placed on the certificate usability/selection path used by tls_choose_sigalg() so an incompatible certificate is skipped or the handshake fails with no suitable signature algorithm.

LiD0209 avatar Aug 06 '26 10:08 LiD0209

This has little practical relevance. It is the user's responsibility to field sensibly usable certificates. A certificate with a keyUsage that omits digitalSignature should not be configured as a TLS server or client certificate. The receiving peer is likely to object in practice. We might add a check (with care to not break the RPK use-case), but this is not IMHO a substantive issue.

vdukhovni avatar Aug 12 '26 07:08 vdukhovni

Agreed on the limited practical impact. I’m not arguing that this is a high-severity security issue; the report is primarily about RFC 8446 compliance and enforcing the restrictions carried by the configured X.509 certificate.

The specific mismatch is that, when Key Usage is present, §4.4.2.2 requires digitalSignature for the end-entity certificate selected for TLS 1.3 authentication, while the current selection path can accept keyEncipherment alone and subsequently use the key for CertificateVerify.

Also agreed regarding RPK: this check should apply only to X.509 certificate authentication and must not affect raw public keys.

So I think a narrow X.509-only check in the certificate usability/selection path would address the issue.

LiD0209 avatar Aug 13 '26 03:08 LiD0209