Commit 50290898 authored by MIDAS NEXT's avatar MIDAS NEXT Committed by jan.koester
Browse files

tls: don't let a reused ssl object's prior connection leak into sigAlg checks

Independent review (HIGH): resetTLS() never cleared peer_rsa/peer_ec_pub/
peer_ec_curve/peer_ec_pub_raw, and the RSA branch of the TLS1.3 Certificate
handler never cleared peer_ec_curve either. On an ssl object reused across
two connect() calls (e.g. A with a P-384 cert, then close+connect to B
with an RSA cert), the CertificateVerify dispatch checked sigAlg only
against peer_ec_curve/peer_rsa -- fields that could still hold connection
A's values if some clearing step were missed, letting a mismatched
sigAlg slip through.

Extracts the dispatch's matching logic into tls::sigAlgMatchesLeafKey(),
gated on server_cert_is_ecdsa (always set fresh from *this* handshake's
own Certificate message) rather than trusting the cached key fields
alone -- so the check holds even if some field isn't cleared. resetTLS()
and the RSA/EC Certificate branches now clear the peer state anyway, as
defense in depth.

Co-Authored-By: default avatarClaude Sonnet 5 <noreply@anthropic.com>
parent 6edca0d5
Loading
Loading
Loading
Loading
+35 −13
Changes for src/crypto/tls.cpp: 35 added lines, 13 removed lines.
Original line number Diff line number Diff line
@@ -2206,6 +2206,7 @@ namespace netplus {
                            throwSSL(NetException::Error, "TLS1.3: could not parse leaf certificate");
                        if (leafCert.isECKey()) {
                            server_cert_is_ecdsa = true;
                            peer_rsa = netplus::rsa(); // clear a stale RSA key from a previous connection
                            std::vector<uint8_t> pubRaw;
                            if (!leafCert.extractECPublicKey(pubRaw))
                                throwSSL(NetException::Error, "TLS1.3: failed to extract EC public key from leaf certificate");
@@ -2228,6 +2229,12 @@ namespace netplus {
                            }
                        } else {
                            server_cert_is_ecdsa = false;
                            // Clear a stale EC curve/key from a previous handshake on a reused
                            // ssl object -- without this, peer_ec_curve could still read e.g.
                            // CurveP384 from a prior connection while this one is RSA.
                            peer_ec_curve.clear();
                            peer_ec_pub_raw.clear();
                            peer_ec_pub = netplus::P256Point();
                            if (!leafCert.extractPublicKey(peer_rsa))
                                throwSSL(NetException::Error, "TLS1.3: failed to extract RSA public key from leaf certificate");
                        }
@@ -2291,19 +2298,15 @@ namespace netplus {
                    toSign.insert(toSign.end(), cv_th.begin(), cv_th.end());

                    bool sigOk;
                    if (sigAlg == 0x0804) { // rsa_pss_rsae_sha256
                        sigOk = tls::verify_rsa_pss_sha256(toSign, sig, peer_rsa);
                    } else if (sigAlg == 0x0403) { // ecdsa_secp256r1_sha256
                        // The signature scheme must match the leaf key's actual curve -- a
                        // P-384 key signing as if it were P-256 (or vice versa) is rejected
                        // rather than fed to the wrong verifier.
                        if (peer_ec_curve != netplus::CurveP256)
                            throwSSL(NetException::Error, "TLS1.3 CertificateVerify: ecdsa_secp256r1_sha256 does not match leaf key's curve");
                        sigOk = tls::verify_ecdsa_sha256(toSign, sig, peer_ec_pub);
                    } else if (sigAlg == 0x0503) { // ecdsa_secp384r1_sha384
                        if (peer_ec_curve != netplus::CurveP384)
                            throwSSL(NetException::Error, "TLS1.3 CertificateVerify: ecdsa_secp384r1_sha384 does not match leaf key's curve");
                        sigOk = tls::verify_ecdsa_sha384(toSign, sig, peer_ec_pub_raw);
                    if (sigAlg == 0x0804 || sigAlg == 0x0403 || sigAlg == 0x0503) {
                        // Must match this handshake's own leaf key type/curve -- see
                        // sigAlgMatchesLeafKey's doc comment for why that's gated on
                        // server_cert_is_ecdsa rather than just the cached key fields.
                        if (!sigAlgMatchesLeafKey(sigAlg))
                            throwSSL(NetException::Error, "TLS1.3 CertificateVerify: signature algorithm does not match leaf key's type/curve");
                        if (sigAlg == 0x0804) sigOk = tls::verify_rsa_pss_sha256(toSign, sig, peer_rsa); // rsa_pss_rsae_sha256
                        else if (sigAlg == 0x0403) sigOk = tls::verify_ecdsa_sha256(toSign, sig, peer_ec_pub); // ecdsa_secp256r1_sha256
                        else sigOk = tls::verify_ecdsa_sha384(toSign, sig, peer_ec_pub_raw); // ecdsa_secp384r1_sha384
                    } else {
                        throwSSL(NetException::Error, "TLS1.3 CertificateVerify: unsupported signature algorithm");
                        sigOk = false; // unreachable, throwSSL always throws
@@ -3698,6 +3701,13 @@ namespace netplus {
        return x_val == r_val;
    }

    bool tls::sigAlgMatchesLeafKey(uint16_t sigAlg) const {
        if (sigAlg == 0x0804) return !server_cert_is_ecdsa; // rsa_pss_rsae_sha256
        if (sigAlg == 0x0403) return server_cert_is_ecdsa && peer_ec_curve == netplus::CurveP256; // ecdsa_secp256r1_sha256
        if (sigAlg == 0x0503) return server_cert_is_ecdsa && peer_ec_curve == netplus::CurveP384; // ecdsa_secp384r1_sha384
        return false;
    }

    bool tls::verify_ecdsa_sha384(
        const std::vector<uint8_t>& data,
        const std::vector<uint8_t>& derSignature,
@@ -3807,6 +3817,18 @@ namespace netplus {
        tls13_hs_recv_seq = 0;
        tls13_app_send_seq = 0;
        tls13_app_recv_seq = 0;

        // -------------------------
        // Peer certificate/key state (client side) -- a reused ssl object must not let
        // the next handshake's CertificateVerify check see the previous connection's
        // leaf key type/curve/bytes.
        // -------------------------
        server_cert_is_ecdsa = false;
        peer_rsa = netplus::rsa();
        peer_ec_pub = netplus::P256Point();
        peer_ec_curve.clear();
        peer_ec_pub_raw.clear();
        peer_presented_certs.clear();
    }

    // =================================================================
+13 −0
Changes for src/crypto/tls.h: 13 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1544,6 +1544,19 @@ namespace netplus {
        netplus::P256Point peer_ec_pub; /**< Client side (TLS 1.3): the server's EC public key, extracted from its leaf certificate when server_cert_is_ecdsa and the key is on P-256, used to verify CertificateVerify. */
        std::string peer_ec_curve; /**< Client side (TLS 1.3): the server's leaf key's curve OID (CurveP256 or CurveP384, see ecdsa_verify.h) -- which signature algorithm CertificateVerify may use is pinned to this. */
        std::vector<uint8_t> peer_ec_pub_raw; /**< Client side (TLS 1.3): the server's EC public key as the raw uncompressed point (0x04||X||Y), set alongside peer_ec_curve when it's P-384 (verify_ecdsa_sha384 takes raw bytes, not a dedicated point type -- see its declaration). */

        /**
         * @brief True iff @p sigAlg (a TLS 1.3 CertificateVerify SignatureScheme) is
         * consistent with the current handshake's own leaf key type/curve
         * (server_cert_is_ecdsa/peer_ec_curve, set when this handshake's Certificate
         * message was parsed) -- the binding the CLI_TLS13_WAIT_CERT_VERIFY dispatch
         * enforces before trusting peer_rsa/peer_ec_pub/peer_ec_pub_raw. Gating on
         * server_cert_is_ecdsa (always set fresh per handshake) rather than only on
         * peer_ec_curve/peer_rsa matters on a reused ssl object: it rejects a mismatch
         * even if some other field was left over from a previous connection instead of
         * being cleared. Pulled out of the dispatch so it can be unit-tested directly.
         */
        bool sigAlgMatchesLeafKey(uint16_t sigAlg) const;
        std::vector<std::vector<uint8_t>> peer_presented_certs; /**< Client side (TLS 1.3): every certificate the server presented, leaf first, DER-encoded, in presentation order. */

        /** @brief Client-side peer certificate verification policy (hostname/CA-chain/pinning); see cert_verify.h. Default-constructed = verify (secure by default) -- consumers connecting to a self-signed/internal-CA peer must opt out explicitly (verifyPeer=false) or pin its fingerprint. */