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

tls: verify ECDSA P-384 CertificateVerify, offer it in signature_algorithms

The ClientHello never offered ecdsa_secp384r1_sha384 (0x0503), and even if
a server picked it anyway the client's CertificateVerify check was
hardcoded to a P-256 point (tls::verify_ecdsa_sha256) -- a P-384 leaf
certificate's TLS 1.3 handshake had no way to succeed. Adds
tls::verify_ecdsa_sha384, delegating to the curve-generic
netplus::ecdsa_verify_digest that x509cert::verifySignedBy already uses
for P-384 chain signatures, instead of duplicating P-384 point arithmetic.
sigAlg is now pinned to the leaf key's actual curve (0x0403 only for
P-256, 0x0503 only for P-384) before 0x0503 is added to the ClientHello.
TLS 1.2's ServerKeyExchange path already dispatches through the same
curve-generic verifier and needed no change.

Co-Authored-By: default avatarClaude Sonnet 5 <noreply@anthropic.com>
parent 67451249
Loading
Loading
Loading
Loading
+41 −5
Changes for src/crypto/tls.cpp: 41 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -2207,11 +2207,25 @@ namespace netplus {
                        if (leafCert.isECKey()) {
                            server_cert_is_ecdsa = true;
                            std::vector<uint8_t> pubRaw;
                            if (!leafCert.extractECPublicKey(pubRaw) ||
                                !P256Point::decodeTls(peer_ec_pub, pubRaw.data(), pubRaw.size()) ||
                            if (!leafCert.extractECPublicKey(pubRaw))
                                throwSSL(NetException::Error, "TLS1.3: failed to extract EC public key from leaf certificate");

                            peer_ec_curve = leafCert.getECCurveOID();
                            if (peer_ec_curve.empty()) peer_ec_curve = netplus::CurveP256; // no parameters: as before

                            if (peer_ec_curve == netplus::CurveP384) {
                                // Sanity-check the encoding (0x04 || X(48) || Y(48)) now; the
                                // actual point-on-curve check happens inside
                                // ecdsa_verify_digest when CertificateVerify is checked.
                                if (pubRaw.size() != 97 || pubRaw[0] != 0x04)
                                    throwSSL(NetException::Error, "TLS1.3: malformed P-384 public key in leaf certificate");
                                peer_ec_pub_raw = std::move(pubRaw);
                            } else {
                                if (!P256Point::decodeTls(peer_ec_pub, pubRaw.data(), pubRaw.size()) ||
                                    !peer_ec_pub.isOnCurve()) {
                                    throwSSL(NetException::Error, "TLS1.3: failed to extract EC public key from leaf certificate");
                                }
                            }
                        } else {
                            server_cert_is_ecdsa = false;
                            if (!leafCert.extractPublicKey(peer_rsa))
@@ -2280,7 +2294,16 @@ namespace netplus {
                    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);
                    } else {
                        throwSSL(NetException::Error, "TLS1.3 CertificateVerify: unsupported signature algorithm");
                        sigOk = false; // unreachable, throwSSL always throws
@@ -3675,6 +3698,14 @@ namespace netplus {
        return x_val == r_val;
    }

    bool tls::verify_ecdsa_sha384(
        const std::vector<uint8_t>& data,
        const std::vector<uint8_t>& derSignature,
        const std::vector<uint8_t>& pubKeyRaw
    ) {
        return netplus::ecdsa_verify_digest(netplus::CurveP384, pubKeyRaw, sha384_hash(data), derSignature);
    }

    void tls::resetTLS() {
        std::lock_guard<std::recursive_mutex> lk(mutex_);
        // MUST be false so next handshake is treated as fresh
@@ -5240,14 +5271,19 @@ namespace netplus {
        // 4) signature_algorithms extension
        {
            // rsa_pss_rsae_sha256 (0x0804), rsa_pkcs1_sha256 (0x0401),
            // ecdsa_secp256r1_sha256 (0x0403), rsa_pss_rsae_sha384 (0x0805)
            // ecdsa_secp256r1_sha256 (0x0403), rsa_pss_rsae_sha384 (0x0805),
            // ecdsa_secp384r1_sha384 (0x0503) -- added once CertificateVerify could
            // actually verify it (tls::verify_ecdsa_sha384, see above); offering it
            // without that support would just move a P-384 server's handshake failure
            // from "no common signature algorithm" to "unsupported algorithm" here.
            exts.push_back(0x00); exts.push_back(0x0d); // type
            exts.push_back(0x00); exts.push_back(0x0a); // length = 10
            exts.push_back(0x00); exts.push_back(0x08); // sig_alg_list length = 8
            exts.push_back(0x00); exts.push_back(0x0c); // length = 12
            exts.push_back(0x00); exts.push_back(0x0a); // sig_alg_list length = 10
            exts.push_back(0x08); exts.push_back(0x04); // rsa_pss_rsae_sha256
            exts.push_back(0x04); exts.push_back(0x01); // rsa_pkcs1_sha256
            exts.push_back(0x04); exts.push_back(0x03); // ecdsa_secp256r1_sha256
            exts.push_back(0x08); exts.push_back(0x05); // rsa_pss_rsae_sha384
            exts.push_back(0x05); exts.push_back(0x03); // ecdsa_secp384r1_sha384
        }

        if (!client_tls12_only) {
+21 −1
Changes for src/crypto/tls.h: 21 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -740,6 +740,24 @@ namespace netplus {
            const netplus::P256Point& pubKey
        );

        /**
         * @brief Verify an ECDSA-P384-SHA384 signature (`ecdsa_secp384r1_sha384`), the
         * CertificateVerify counterpart to @ref verify_ecdsa_sha256 for a P-384 leaf key.
         * Takes the public key as the raw uncompressed point (0x04||X||Y, 97 bytes) rather
         * than a dedicated point type -- P-384 point arithmetic already lives in
         * ecdsa_verify.cpp (netplus::ecdsa_verify_digest), which this delegates to instead
         * of duplicating it here.
         * @param data The content that was signed.
         * @param derSignature DER-encoded SEQUENCE{INTEGER r, INTEGER s} signature.
         * @param pubKeyRaw P-384 public key as the raw uncompressed point.
         * @return true iff the signature is valid for @p data under @p pubKeyRaw.
         */
        static bool verify_ecdsa_sha384(
            const std::vector<uint8_t>& data,
            const std::vector<uint8_t>& derSignature,
            const std::vector<uint8_t>& pubKeyRaw
        );

        // --- TLS 1.2 Record Layer ---

        /**
@@ -1523,7 +1541,9 @@ namespace netplus {

        // --- Peer RSA key (client stores server's public key here) ---
        netplus::rsa peer_rsa;    /**< Client side: the server's RSA public key, extracted from its certificate, used for RSA key-exchange encryption or signature verification. */
        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, used to verify CertificateVerify. */
        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). */
        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. */