Loading src/crypto/cert_verify.h +4 −3 Changes for src/crypto/cert_verify.h: 4 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -7,10 +7,11 @@ namespace netplus { // Trust configuration for a single TLS client connection (netplus::tls::trust_policy). // Default-constructed means "verify nothing" -- the pre-existing behavior of every consumer // that doesn't opt in, so adding this never changes behavior for callers that don't set it. // Secure by default: a default-constructed policy verifies the peer (hostname + CA chain, // or a pinned fingerprint) against the platform's trust store. Set verifyPeer = false // explicitly to opt back out (e.g. known-self-signed endpoints not worth pinning). struct TlsTrustPolicy { bool verifyPeer = false; bool verifyPeer = true; // System CA bundle used when the peer's leaf certificate isn't pinned (see below). // Empty (the default) means "use the platform default": on Windows, the system ROOT Loading src/quic.cpp +174 −4 Changes for src/quic.cpp: 174 added lines, 4 removed lines. Original line number Diff line number Diff line Loading @@ -6039,12 +6039,13 @@ void quic::processTLSMessages() { case 0x0b: // Certificate // Add to transcript (client receiving from server) _tls_transcript.insert(_tls_transcript.end(), msg.begin(), msg.end()); // Verify server certificate (simplified) processServerCertificate(msg); break; case 0x0f: // CertificateVerify // Add to transcript (client receiving from server) _tls_transcript.insert(_tls_transcript.end(), msg.begin(), msg.end()); // Verify signature (simplified) // RFC 8446 §4.4.3: CertificateVerify's signature covers the // transcript *before* this message, so verify before adding // msg to it (processServerCertificateVerify does both, in order). processServerCertificateVerify(msg); break; case 0x14: // Finished processFinished(msg); Loading Loading @@ -6302,6 +6303,175 @@ void quic::parseTransportParameters(const uint8_t* data, size_t len) { } } // ============================================================================ // TLS 1.3: Process server Certificate (Client side, RFC 9001 §4.4 / RFC 8446 // §4.4.2) -- parses the presented chain, extracts the leaf's public key // (needed by processServerCertificateVerify()), and runs the chain-of-trust // check (hostname/CA-chain/pinning). verifyPeerChain() is a no-op unless the // caller opted in via setTrustPolicy(); the CertificateVerify signature check // that follows is independent of it either way and always runs. // ============================================================================ void quic::processServerCertificate(const std::vector<uint8_t>& msg) { _peer_presented_certs.clear(); size_t off = 4; // Skip type(1) + length(3) if (off >= msg.size()) { NetException e; e[NetException::Error] << "quic: TLS1.3 Certificate message truncated (context)"; throw e; } uint8_t ctxLen = msg[off++]; if (off + ctxLen > msg.size()) { NetException e; e[NetException::Error] << "quic: TLS1.3 Certificate message truncated (context)"; throw e; } off += ctxLen; // certificate_request_context, empty for server certs if (off + 3 > msg.size()) { NetException e; e[NetException::Error] << "quic: TLS1.3 Certificate message truncated (list length)"; throw e; } uint32_t listLen = (uint32_t(msg[off]) << 16) | (uint32_t(msg[off + 1]) << 8) | uint32_t(msg[off + 2]); off += 3; size_t listEnd = off + listLen; if (listEnd > msg.size()) { NetException e; e[NetException::Error] << "quic: TLS1.3 Certificate list truncated"; throw e; } while (off + 3 <= listEnd) { uint32_t certLen = (uint32_t(msg[off]) << 16) | (uint32_t(msg[off + 1]) << 8) | uint32_t(msg[off + 2]); off += 3; if (off + certLen > listEnd) { NetException e; e[NetException::Error] << "quic: TLS1.3 Certificate entry truncated"; throw e; } _peer_presented_certs.emplace_back(msg.begin() + off, msg.begin() + off + certLen); off += certLen; if (off + 2 > listEnd) { NetException e; e[NetException::Error] << "quic: TLS1.3 Certificate entry extensions truncated"; throw e; } uint16_t extLen = (uint16_t(msg[off]) << 8) | uint16_t(msg[off + 1]); off += 2; if (off + extLen > listEnd) { NetException e; e[NetException::Error] << "quic: TLS1.3 Certificate entry extensions truncated"; throw e; } off += extLen; } if (_peer_presented_certs.empty()) { NetException e; e[NetException::Error] << "quic: TLS1.3 Certificate message contained no certificates"; throw e; } // Extract the leaf's public key -- needed to verify the CertificateVerify // message that follows. { x509cert leafCert; if (!leafCert.loadFromBuffer(_peer_presented_certs[0])) { NetException e; e[NetException::Error] << "quic: TLS1.3 could not parse leaf certificate"; throw e; } if (leafCert.isECKey()) { _peer_cert_is_ecdsa = true; std::vector<uint8_t> pubRaw; if (!leafCert.extractECPublicKey(pubRaw) || !P256Point::decodeTls(_peer_ec_pub, pubRaw.data(), pubRaw.size()) || !_peer_ec_pub.isOnCurve()) { NetException e; e[NetException::Error] << "quic: TLS1.3 failed to extract EC public key from leaf certificate"; throw e; } } else { _peer_cert_is_ecdsa = false; if (!leafCert.extractPublicKey(_peer_rsa)) { NetException e; e[NetException::Error] << "quic: TLS1.3 failed to extract RSA public key from leaf certificate"; throw e; } } } // Full chain-of-trust check (hostname/CA-chain/pinning) -- no-op unless // the caller opted in via _trust_policy.verifyPeer (see cert_verify.h). // Independent of the CertificateVerify signature check below: this proves // the leaf is who it claims to be; CertificateVerify proves the peer holds // that leaf's private key. verifyPeerChain(_peer_presented_certs, _trust_policy); } // ============================================================================ // TLS 1.3: Process server CertificateVerify (Client side, RFC 8446 §4.4.3) -- // checks the server's signature over the handshake transcript so far using // the leaf public key processServerCertificate() extracted. Only the two // signature algorithms this client's ClientHello advertises (buildClientHello()) // are accepted; anything else is a protocol violation from a compliant peer. // ============================================================================ void quic::processServerCertificateVerify(const std::vector<uint8_t>& msg) { if (msg.size() < 8) { NetException e; e[NetException::Error] << "quic: TLS1.3 CertificateVerify too short"; throw e; } uint16_t sigAlg = (uint16_t(msg[4]) << 8) | uint16_t(msg[5]); uint16_t sigLen = (uint16_t(msg[6]) << 8) | uint16_t(msg[7]); if (size_t(8) + sigLen > msg.size()) { NetException e; e[NetException::Error] << "quic: TLS1.3 CertificateVerify signature truncated"; throw e; } std::vector<uint8_t> sig(msg.begin() + 8, msg.begin() + 8 + sigLen); // Content signed: 64 spaces + context string + 0x00 + transcript hash, // per RFC 8446 §4.4.3. The transcript hash (of everything received/sent // so far, NOT including this message) must use the negotiated cipher // suite's hash function. std::vector<uint8_t> cv_th = (_selected_cipher == 0x1302) ? sha384_hash(_tls_transcript) : sha256_hash(_tls_transcript); std::vector<uint8_t> toSign; toSign.reserve(64 + 34 + 1 + cv_th.size()); toSign.resize(64, 0x20); { static const char* ctx = "TLS 1.3, server CertificateVerify"; toSign.insert(toSign.end(), ctx, ctx + strlen(ctx)); } toSign.push_back(0x00); toSign.insert(toSign.end(), cv_th.begin(), cv_th.end()); bool sigOk; if (sigAlg == 0x0804) { // rsa_pss_rsae_sha256 sigOk = rsa_pss_sha256::verify(_peer_rsa, sig, toSign); } else if (sigAlg == 0x0403) { // ecdsa_secp256r1_sha256 sigOk = tls::verify_ecdsa_sha256(toSign, sig, _peer_ec_pub); } else { NetException e; e[NetException::Error] << "quic: TLS1.3 CertificateVerify unsupported signature algorithm"; throw e; } if (!sigOk) { NetException e; e[NetException::Error] << "quic: TLS1.3 CertificateVerify signature invalid"; throw e; } // Add to transcript only after a successful check -- the signature // covers everything received *before* this message. _tls_transcript.insert(_tls_transcript.end(), msg.begin(), msg.end()); } // ============================================================================ // TLS: Process EncryptedExtensions (Client side - contains transport params) // ============================================================================ Loading src/socket.h +31 −0 Changes for src/socket.h: 31 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -740,6 +740,16 @@ namespace netplus { // Cipher suite information uint16_t getSelectedCipher() const { return _selected_cipher; } // Trust policy applied to the server's certificate chain during the // TLS 1.3 handshake (client role only; a no-op on the server role, // mirroring netplus::ssl::getTls().trust_policy for the TCP path). // Must be set before connect() to take effect. Default-constructed // policy verifies against the platform CA store (see // netplus::TlsTrustPolicy, crypto/cert_verify.h) but skips the // hostname check until expectedHostname is set explicitly. void setTrustPolicy(const netplus::TlsTrustPolicy& policy) { _trust_policy = policy; } const netplus::TlsTrustPolicy& getTrustPolicy() const { return _trust_policy; } // RFC 9000 §8.1.3: a client-side address-validation token issued by // the server after a successful handshake (via NEW_TOKEN). Save it // with getStoredToken() and hand it to setToken() on a later, separate Loading Loading @@ -1175,6 +1185,13 @@ namespace netplus { std::vector<uint8_t> buildClientHello(); std::vector<uint8_t> buildTransportParameters(); void processTLSMessages(); // Client-only, TLS 1.3 peer authentication (RFC 9001 §4.4): parses // the server's Certificate message into _peer_presented_certs, // extracts the leaf's public key, and runs verifyPeerChain() against // _trust_policy; processServerCertificateVerify() then checks the // server's CertificateVerify signature against that extracted key. void processServerCertificate(const std::vector<uint8_t>& msg); void processServerCertificateVerify(const std::vector<uint8_t>& msg); void processFinished(const std::vector<uint8_t>& msg); void sendClientFinished(); void sendHandshakeDone(); Loading Loading @@ -1225,6 +1242,20 @@ namespace netplus { bool _sigalg_rsa_pss_sha256 = true; // Safe default if extension missing bool _sigalg_rsa_pss_sha384 = false; // Client-only, TLS 1.3 peer authentication (RFC 9001 §4.4 / RFC 8446 // §4.4.2-4.4.3): every certificate the server presented in its // Certificate message, DER-encoded, leaf first, plus the leaf's // public key extracted from it -- used to verify the CertificateVerify // signature that follows (processTLSMessages(), cases 0x0b/0x0f). std::vector<std::vector<uint8_t>> _peer_presented_certs; netplus::rsa _peer_rsa; // leaf's RSA public key, if not ECDSA netplus::P256Point _peer_ec_pub; // leaf's EC public key, if ECDSA bool _peer_cert_is_ecdsa = false; // Trust policy consulted by the Certificate-message handling above // (see setTrustPolicy()/getTrustPolicy()); verifyPeerChain() is a // no-op unless the caller opted in via _trust_policy.verifyPeer. netplus::TlsTrustPolicy _trust_policy; // Connection IDs std::vector<uint8_t> _local_cid; std::vector<uint8_t> _remote_cid; Loading Loading
src/crypto/cert_verify.h +4 −3 Changes for src/crypto/cert_verify.h: 4 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -7,10 +7,11 @@ namespace netplus { // Trust configuration for a single TLS client connection (netplus::tls::trust_policy). // Default-constructed means "verify nothing" -- the pre-existing behavior of every consumer // that doesn't opt in, so adding this never changes behavior for callers that don't set it. // Secure by default: a default-constructed policy verifies the peer (hostname + CA chain, // or a pinned fingerprint) against the platform's trust store. Set verifyPeer = false // explicitly to opt back out (e.g. known-self-signed endpoints not worth pinning). struct TlsTrustPolicy { bool verifyPeer = false; bool verifyPeer = true; // System CA bundle used when the peer's leaf certificate isn't pinned (see below). // Empty (the default) means "use the platform default": on Windows, the system ROOT Loading
src/quic.cpp +174 −4 Changes for src/quic.cpp: 174 added lines, 4 removed lines. Original line number Diff line number Diff line Loading @@ -6039,12 +6039,13 @@ void quic::processTLSMessages() { case 0x0b: // Certificate // Add to transcript (client receiving from server) _tls_transcript.insert(_tls_transcript.end(), msg.begin(), msg.end()); // Verify server certificate (simplified) processServerCertificate(msg); break; case 0x0f: // CertificateVerify // Add to transcript (client receiving from server) _tls_transcript.insert(_tls_transcript.end(), msg.begin(), msg.end()); // Verify signature (simplified) // RFC 8446 §4.4.3: CertificateVerify's signature covers the // transcript *before* this message, so verify before adding // msg to it (processServerCertificateVerify does both, in order). processServerCertificateVerify(msg); break; case 0x14: // Finished processFinished(msg); Loading Loading @@ -6302,6 +6303,175 @@ void quic::parseTransportParameters(const uint8_t* data, size_t len) { } } // ============================================================================ // TLS 1.3: Process server Certificate (Client side, RFC 9001 §4.4 / RFC 8446 // §4.4.2) -- parses the presented chain, extracts the leaf's public key // (needed by processServerCertificateVerify()), and runs the chain-of-trust // check (hostname/CA-chain/pinning). verifyPeerChain() is a no-op unless the // caller opted in via setTrustPolicy(); the CertificateVerify signature check // that follows is independent of it either way and always runs. // ============================================================================ void quic::processServerCertificate(const std::vector<uint8_t>& msg) { _peer_presented_certs.clear(); size_t off = 4; // Skip type(1) + length(3) if (off >= msg.size()) { NetException e; e[NetException::Error] << "quic: TLS1.3 Certificate message truncated (context)"; throw e; } uint8_t ctxLen = msg[off++]; if (off + ctxLen > msg.size()) { NetException e; e[NetException::Error] << "quic: TLS1.3 Certificate message truncated (context)"; throw e; } off += ctxLen; // certificate_request_context, empty for server certs if (off + 3 > msg.size()) { NetException e; e[NetException::Error] << "quic: TLS1.3 Certificate message truncated (list length)"; throw e; } uint32_t listLen = (uint32_t(msg[off]) << 16) | (uint32_t(msg[off + 1]) << 8) | uint32_t(msg[off + 2]); off += 3; size_t listEnd = off + listLen; if (listEnd > msg.size()) { NetException e; e[NetException::Error] << "quic: TLS1.3 Certificate list truncated"; throw e; } while (off + 3 <= listEnd) { uint32_t certLen = (uint32_t(msg[off]) << 16) | (uint32_t(msg[off + 1]) << 8) | uint32_t(msg[off + 2]); off += 3; if (off + certLen > listEnd) { NetException e; e[NetException::Error] << "quic: TLS1.3 Certificate entry truncated"; throw e; } _peer_presented_certs.emplace_back(msg.begin() + off, msg.begin() + off + certLen); off += certLen; if (off + 2 > listEnd) { NetException e; e[NetException::Error] << "quic: TLS1.3 Certificate entry extensions truncated"; throw e; } uint16_t extLen = (uint16_t(msg[off]) << 8) | uint16_t(msg[off + 1]); off += 2; if (off + extLen > listEnd) { NetException e; e[NetException::Error] << "quic: TLS1.3 Certificate entry extensions truncated"; throw e; } off += extLen; } if (_peer_presented_certs.empty()) { NetException e; e[NetException::Error] << "quic: TLS1.3 Certificate message contained no certificates"; throw e; } // Extract the leaf's public key -- needed to verify the CertificateVerify // message that follows. { x509cert leafCert; if (!leafCert.loadFromBuffer(_peer_presented_certs[0])) { NetException e; e[NetException::Error] << "quic: TLS1.3 could not parse leaf certificate"; throw e; } if (leafCert.isECKey()) { _peer_cert_is_ecdsa = true; std::vector<uint8_t> pubRaw; if (!leafCert.extractECPublicKey(pubRaw) || !P256Point::decodeTls(_peer_ec_pub, pubRaw.data(), pubRaw.size()) || !_peer_ec_pub.isOnCurve()) { NetException e; e[NetException::Error] << "quic: TLS1.3 failed to extract EC public key from leaf certificate"; throw e; } } else { _peer_cert_is_ecdsa = false; if (!leafCert.extractPublicKey(_peer_rsa)) { NetException e; e[NetException::Error] << "quic: TLS1.3 failed to extract RSA public key from leaf certificate"; throw e; } } } // Full chain-of-trust check (hostname/CA-chain/pinning) -- no-op unless // the caller opted in via _trust_policy.verifyPeer (see cert_verify.h). // Independent of the CertificateVerify signature check below: this proves // the leaf is who it claims to be; CertificateVerify proves the peer holds // that leaf's private key. verifyPeerChain(_peer_presented_certs, _trust_policy); } // ============================================================================ // TLS 1.3: Process server CertificateVerify (Client side, RFC 8446 §4.4.3) -- // checks the server's signature over the handshake transcript so far using // the leaf public key processServerCertificate() extracted. Only the two // signature algorithms this client's ClientHello advertises (buildClientHello()) // are accepted; anything else is a protocol violation from a compliant peer. // ============================================================================ void quic::processServerCertificateVerify(const std::vector<uint8_t>& msg) { if (msg.size() < 8) { NetException e; e[NetException::Error] << "quic: TLS1.3 CertificateVerify too short"; throw e; } uint16_t sigAlg = (uint16_t(msg[4]) << 8) | uint16_t(msg[5]); uint16_t sigLen = (uint16_t(msg[6]) << 8) | uint16_t(msg[7]); if (size_t(8) + sigLen > msg.size()) { NetException e; e[NetException::Error] << "quic: TLS1.3 CertificateVerify signature truncated"; throw e; } std::vector<uint8_t> sig(msg.begin() + 8, msg.begin() + 8 + sigLen); // Content signed: 64 spaces + context string + 0x00 + transcript hash, // per RFC 8446 §4.4.3. The transcript hash (of everything received/sent // so far, NOT including this message) must use the negotiated cipher // suite's hash function. std::vector<uint8_t> cv_th = (_selected_cipher == 0x1302) ? sha384_hash(_tls_transcript) : sha256_hash(_tls_transcript); std::vector<uint8_t> toSign; toSign.reserve(64 + 34 + 1 + cv_th.size()); toSign.resize(64, 0x20); { static const char* ctx = "TLS 1.3, server CertificateVerify"; toSign.insert(toSign.end(), ctx, ctx + strlen(ctx)); } toSign.push_back(0x00); toSign.insert(toSign.end(), cv_th.begin(), cv_th.end()); bool sigOk; if (sigAlg == 0x0804) { // rsa_pss_rsae_sha256 sigOk = rsa_pss_sha256::verify(_peer_rsa, sig, toSign); } else if (sigAlg == 0x0403) { // ecdsa_secp256r1_sha256 sigOk = tls::verify_ecdsa_sha256(toSign, sig, _peer_ec_pub); } else { NetException e; e[NetException::Error] << "quic: TLS1.3 CertificateVerify unsupported signature algorithm"; throw e; } if (!sigOk) { NetException e; e[NetException::Error] << "quic: TLS1.3 CertificateVerify signature invalid"; throw e; } // Add to transcript only after a successful check -- the signature // covers everything received *before* this message. _tls_transcript.insert(_tls_transcript.end(), msg.begin(), msg.end()); } // ============================================================================ // TLS: Process EncryptedExtensions (Client side - contains transport params) // ============================================================================ Loading
src/socket.h +31 −0 Changes for src/socket.h: 31 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -740,6 +740,16 @@ namespace netplus { // Cipher suite information uint16_t getSelectedCipher() const { return _selected_cipher; } // Trust policy applied to the server's certificate chain during the // TLS 1.3 handshake (client role only; a no-op on the server role, // mirroring netplus::ssl::getTls().trust_policy for the TCP path). // Must be set before connect() to take effect. Default-constructed // policy verifies against the platform CA store (see // netplus::TlsTrustPolicy, crypto/cert_verify.h) but skips the // hostname check until expectedHostname is set explicitly. void setTrustPolicy(const netplus::TlsTrustPolicy& policy) { _trust_policy = policy; } const netplus::TlsTrustPolicy& getTrustPolicy() const { return _trust_policy; } // RFC 9000 §8.1.3: a client-side address-validation token issued by // the server after a successful handshake (via NEW_TOKEN). Save it // with getStoredToken() and hand it to setToken() on a later, separate Loading Loading @@ -1175,6 +1185,13 @@ namespace netplus { std::vector<uint8_t> buildClientHello(); std::vector<uint8_t> buildTransportParameters(); void processTLSMessages(); // Client-only, TLS 1.3 peer authentication (RFC 9001 §4.4): parses // the server's Certificate message into _peer_presented_certs, // extracts the leaf's public key, and runs verifyPeerChain() against // _trust_policy; processServerCertificateVerify() then checks the // server's CertificateVerify signature against that extracted key. void processServerCertificate(const std::vector<uint8_t>& msg); void processServerCertificateVerify(const std::vector<uint8_t>& msg); void processFinished(const std::vector<uint8_t>& msg); void sendClientFinished(); void sendHandshakeDone(); Loading Loading @@ -1225,6 +1242,20 @@ namespace netplus { bool _sigalg_rsa_pss_sha256 = true; // Safe default if extension missing bool _sigalg_rsa_pss_sha384 = false; // Client-only, TLS 1.3 peer authentication (RFC 9001 §4.4 / RFC 8446 // §4.4.2-4.4.3): every certificate the server presented in its // Certificate message, DER-encoded, leaf first, plus the leaf's // public key extracted from it -- used to verify the CertificateVerify // signature that follows (processTLSMessages(), cases 0x0b/0x0f). std::vector<std::vector<uint8_t>> _peer_presented_certs; netplus::rsa _peer_rsa; // leaf's RSA public key, if not ECDSA netplus::P256Point _peer_ec_pub; // leaf's EC public key, if ECDSA bool _peer_cert_is_ecdsa = false; // Trust policy consulted by the Certificate-message handling above // (see setTrustPolicy()/getTrustPolicy()); verifyPeerChain() is a // no-op unless the caller opted in via _trust_policy.verifyPeer. netplus::TlsTrustPolicy _trust_policy; // Connection IDs std::vector<uint8_t> _local_cid; std::vector<uint8_t> _remote_cid; Loading