Enable client cert verification at QUICHE `TlsServerHandshaker`. This change updates the `QuicCryptoServerConfig` to support an optional `ProofVerifier`. When provided, the `TlsServerHandshaker` will use the `ProofVerifier` to validate client certificates during an mTLS handshake. The current client certificate verification behavior for mTLS always succeeds by default. To keep the current behavior across all consumers, the method will succeed by default if no `Proof Verifier` is provided. PiperOrigin-RevId: 795091489
diff --git a/quiche/quic/core/crypto/quic_crypto_server_config.cc b/quiche/quic/core/crypto/quic_crypto_server_config.cc index fa85ce1..032a38c 100644 --- a/quiche/quic/core/crypto/quic_crypto_server_config.cc +++ b/quiche/quic/core/crypto/quic_crypto_server_config.cc
@@ -31,6 +31,7 @@ #include "quiche/quic/core/crypto/key_exchange.h" #include "quiche/quic/core/crypto/p256_key_exchange.h" #include "quiche/quic/core/crypto/proof_source.h" +#include "quiche/quic/core/crypto/proof_verifier.h" #include "quiche/quic/core/crypto/quic_decrypter.h" #include "quiche/quic/core/crypto/quic_encrypter.h" #include "quiche/quic/core/crypto/quic_hkdf.h" @@ -273,13 +274,15 @@ QuicCryptoServerConfig::QuicCryptoServerConfig( absl::string_view source_address_token_secret, QuicRandom* server_nonce_entropy, std::unique_ptr<ProofSource> proof_source, - std::unique_ptr<KeyExchangeSource> key_exchange_source) + std::unique_ptr<KeyExchangeSource> key_exchange_source, + std::unique_ptr<ProofVerifier> proof_verifier) : replay_protection_(true), chlo_multiplier_(kMultiplier), configs_lock_(), primary_config_(nullptr), next_config_promotion_time_(QuicWallTime::Zero()), proof_source_(std::move(proof_source)), + proof_verifier_(std::move(proof_verifier)), key_exchange_source_(std::move(key_exchange_source)), ssl_ctx_(TlsServerConnection::CreateSslCtx(proof_source_.get())), source_address_token_future_secs_(3600), @@ -1766,6 +1769,10 @@ return proof_source_.get(); } +ProofVerifier* QuicCryptoServerConfig::proof_verifier() const { + return proof_verifier_.get(); +} + SSL_CTX* QuicCryptoServerConfig::ssl_ctx() const { return ssl_ctx_.get(); } HandshakeFailureReason QuicCryptoServerConfig::ParseSourceAddressToken(
diff --git a/quiche/quic/core/crypto/quic_crypto_server_config.h b/quiche/quic/core/crypto/quic_crypto_server_config.h index 1692cea..42f9ef3 100644 --- a/quiche/quic/core/crypto/quic_crypto_server_config.h +++ b/quiche/quic/core/crypto/quic_crypto_server_config.h
@@ -12,6 +12,7 @@ #include <string> #include <vector> +#include "absl/base/nullability.h" #include "absl/base/thread_annotations.h" #include "absl/strings/string_view.h" #include "absl/synchronization/mutex.h" @@ -22,6 +23,7 @@ #include "quiche/quic/core/crypto/crypto_secret_boxer.h" #include "quiche/quic/core/crypto/key_exchange.h" #include "quiche/quic/core/crypto/proof_source.h" +#include "quiche/quic/core/crypto/proof_verifier.h" #include "quiche/quic/core/crypto/quic_compressed_certs_cache.h" #include "quiche/quic/core/crypto/quic_crypto_proof.h" #include "quiche/quic/core/crypto/quic_random.h" @@ -36,6 +38,7 @@ class CryptoHandshakeMessage; class ProofSource; +class ProofVerifier; class QuicClock; class QuicServerConfigProtobuf; struct QuicSignedServerConfig; @@ -213,7 +216,8 @@ absl::string_view source_address_token_secret, QuicRandom* server_nonce_entropy, std::unique_ptr<ProofSource> proof_source, - std::unique_ptr<KeyExchangeSource> key_exchange_source); + std::unique_ptr<KeyExchangeSource> key_exchange_source, + std::unique_ptr<ProofVerifier> proof_verifier = nullptr); QuicCryptoServerConfig(const QuicCryptoServerConfig&) = delete; QuicCryptoServerConfig& operator=(const QuicCryptoServerConfig&) = delete; ~QuicCryptoServerConfig(); @@ -437,6 +441,7 @@ } ProofSource* proof_source() const; + ProofVerifier* absl_nullable proof_verifier() const; SSL_CTX* ssl_ctx() const; @@ -903,6 +908,12 @@ // signatures. std::unique_ptr<ProofSource> proof_source_; + // proof_verifier_ is an optional object that can verify a client's + // certificate chain. Crypto server stream implementations can use this when + // verifying a client's certificate chain, but must be able to handle the case + // where this is null. + std::unique_ptr<ProofVerifier> proof_verifier_; + // key_exchange_source_ contains an object that can provide key exchange // objects. std::unique_ptr<KeyExchangeSource> key_exchange_source_;
diff --git a/quiche/quic/core/crypto/quic_crypto_server_config_test.cc b/quiche/quic/core/crypto/quic_crypto_server_config_test.cc index 6b517f4..3c4c5b7 100644 --- a/quiche/quic/core/crypto/quic_crypto_server_config_test.cc +++ b/quiche/quic/core/crypto/quic_crypto_server_config_test.cc
@@ -492,5 +492,17 @@ test_peer_.CheckConfigs({{"a", false}, {"b", true}, {"c", false}}); } +TEST_F(QuicCryptoServerConfigTest, SetProofVerifier) { + QuicRandom* rand = QuicRandom::GetInstance(); + auto proof_verifier = crypto_test_utils::ProofVerifierForTesting(); + auto proof_verifier_ptr = proof_verifier.get(); + QuicCryptoServerConfig server(QuicCryptoServerConfig::TESTING, rand, + crypto_test_utils::ProofSourceForTesting(), + KeyExchangeSource::Default(), + std::move(proof_verifier)); + auto retrieved_proof_verifier = server.proof_verifier(); + EXPECT_EQ(retrieved_proof_verifier, proof_verifier_ptr); +} + } // namespace test } // namespace quic
diff --git a/quiche/quic/core/tls_server_handshaker.cc b/quiche/quic/core/tls_server_handshaker.cc index f59f7bf..f0ea720 100644 --- a/quiche/quic/core/tls_server_handshaker.cc +++ b/quiche/quic/core/tls_server_handshaker.cc
@@ -213,6 +213,7 @@ : TlsHandshaker(this, session), QuicCryptoServerStreamBase(session), proof_source_(crypto_config->proof_source()), + proof_verifier_(crypto_config->proof_verifier()), pre_shared_key_(crypto_config->pre_shared_key()), crypto_negotiated_params_(new QuicCryptoNegotiatedParameters), tls_connection_(crypto_config->ssl_ctx(), this, session->GetSSLConfig()), @@ -675,15 +676,22 @@ // appropriate time. } +// VerifyCertChain is called to verify the client's certificate chain. If the +// proof verifier is not set, the method will assume the certificate chain is +// valid and return QUIC_SUCCESS. QuicAsyncStatus TlsServerHandshaker::VerifyCertChain( - const std::vector<std::string>& /*certs*/, std::string* /*error_details*/, - std::unique_ptr<ProofVerifyDetails>* /*details*/, uint8_t* /*out_alert*/, - std::unique_ptr<ProofVerifierCallback> /*callback*/) { - QUIC_DVLOG(1) << "VerifyCertChain returning success"; + const std::vector<std::string>& certs, std::string* error_details, + std::unique_ptr<ProofVerifyDetails>* details, uint8_t* out_alert, + std::unique_ptr<ProofVerifierCallback> callback) { + if (proof_verifier_ == nullptr) { + QUIC_DVLOG(1) << "Proof verifier is not set, skipping cert verification"; + return QUIC_SUCCESS; + } - // No real verification here. A subclass can override this function to verify - // the client cert if needed. - return QUIC_SUCCESS; + return proof_verifier_->VerifyCertChain( + /*hostname=*/"", /*port=*/0, certs, /*ocsp_response=*/"", /*cert_sct=*/"", + /*context=*/nullptr, error_details, details, out_alert, + std::move(callback)); } void TlsServerHandshaker::OnProofVerifyDetailsAvailable(
diff --git a/quiche/quic/core/tls_server_handshaker.h b/quiche/quic/core/tls_server_handshaker.h index a2b2bd2..8541968 100644 --- a/quiche/quic/core/tls_server_handshaker.h +++ b/quiche/quic/core/tls_server_handshaker.h
@@ -13,6 +13,7 @@ #include <utility> #include <vector> +#include "absl/base/nullability.h" #include "absl/strings/string_view.h" #include "absl/types/span.h" #include "openssl/base.h" @@ -350,6 +351,11 @@ std::unique_ptr<ProofSourceHandle> proof_source_handle_; ProofSource* proof_source_; + // proof_verifier_ is an optional object that can verify a client's + // certificate chain. If this is null, then client certificates will always be + // considered valid. + ProofVerifier* absl_nullable proof_verifier_; + // State to handle potentially asynchronous session ticket decryption. // |ticket_decryption_callback_| points to the non-owned callback that was // passed to ProofSource::TicketCrypter::Decrypt but hasn't finished running
diff --git a/quiche/quic/core/tls_server_handshaker_test.cc b/quiche/quic/core/tls_server_handshaker_test.cc index 2284938..9e14c79 100644 --- a/quiche/quic/core/tls_server_handshaker_test.cc +++ b/quiche/quic/core/tls_server_handshaker_test.cc
@@ -5,6 +5,7 @@ #include "quiche/quic/core/tls_server_handshaker.h" #include <algorithm> +#include <cstdint> #include <memory> #include <string> #include <utility> @@ -15,6 +16,7 @@ #include "quiche/quic/core/crypto/certificate_util.h" #include "quiche/quic/core/crypto/client_proof_source.h" #include "quiche/quic/core/crypto/proof_source.h" +#include "quiche/quic/core/crypto/proof_verifier.h" #include "quiche/quic/core/crypto/quic_random.h" #include "quiche/quic/core/quic_connection_id.h" #include "quiche/quic/core/quic_crypto_client_stream.h" @@ -77,6 +79,33 @@ return params; } +class MockProofVerifier : public ProofVerifier { + public: + MOCK_METHOD(QuicAsyncStatus, VerifyProof, + (const std::string& hostname, uint16_t port, + const std::string& server_config, + QuicTransportVersion transport_version, + absl::string_view chlo_hash, + const std::vector<std::string>& certs, + const std::string& cert_sct, const std::string& signature, + const ProofVerifyContext* context, std::string* error_details, + std::unique_ptr<ProofVerifyDetails>* details, + std::unique_ptr<ProofVerifierCallback> callback), + (override)); + + MOCK_METHOD(QuicAsyncStatus, VerifyCertChain, + (const std::string& hostname, uint16_t port, + const std::vector<std::string>& certs, + const std::string& ocsp_response, const std::string& cert_sct, + const ProofVerifyContext* context, std::string* error_details, + std::unique_ptr<ProofVerifyDetails>* details, uint8_t* out_alert, + std::unique_ptr<ProofVerifierCallback> callback), + (override)); + + MOCK_METHOD(std::unique_ptr<ProofVerifyContext>, CreateDefaultContext, (), + (override)); +}; + class TestTlsServerHandshaker : public TlsServerHandshaker { public: static constexpr TransportParameters::TransportParameterId @@ -122,6 +151,9 @@ } bool received_client_cert() const { return received_client_cert_; } + QuicAsyncStatus verify_cert_chain_status() const { + return verify_cert_chain_status_; + } using TlsServerHandshaker::AdvanceHandshake; using TlsServerHandshaker::expected_ssl_error; @@ -132,8 +164,9 @@ std::unique_ptr<ProofVerifyDetails>* details, uint8_t* out_alert, std::unique_ptr<ProofVerifierCallback> callback) override { received_client_cert_ = true; - return TlsServerHandshaker::VerifyCertChain(certs, error_details, details, - out_alert, std::move(callback)); + verify_cert_chain_status_ = TlsServerHandshaker::VerifyCertChain( + certs, error_details, details, out_alert, std::move(callback)); + return verify_cert_chain_status_; } bool ProcessAdditionalTransportParameters( @@ -153,6 +186,7 @@ // Owned by TlsServerHandshaker. FakeProofSourceHandle* fake_proof_source_handle_ = nullptr; ProofSource* proof_source_ = nullptr; + QuicAsyncStatus verify_cert_chain_status_; bool received_client_cert_ = false; }; @@ -200,15 +234,19 @@ alarm_factories_.clear(); } - void InitializeServerConfig() { + std::unique_ptr<FakeProofSource> InitializeProofSource() { auto ticket_crypter = std::make_unique<TestTicketCrypter>(); ticket_crypter_ = ticket_crypter.get(); auto proof_source = std::make_unique<FakeProofSource>(); proof_source_ = proof_source.get(); proof_source_->SetTicketCrypter(std::move(ticket_crypter)); + return proof_source; + } + + void InitializeServerConfig() { server_crypto_config_ = std::make_unique<QuicCryptoServerConfig>( QuicCryptoServerConfig::TESTING, QuicRandom::GetInstance(), - std::move(proof_source), KeyExchangeSource::Default()); + InitializeProofSource(), KeyExchangeSource::Default()); } void InitializeServerConfigWithFailingProofSource() { @@ -217,6 +255,15 @@ std::make_unique<FailingProofSource>(), KeyExchangeSource::Default()); } + void InitializeServerConfigWithProofSourceAndVerifier( + std::unique_ptr<ProofSource> proof_source, + std::unique_ptr<ProofVerifier> proof_verifier) { + server_crypto_config_ = std::make_unique<QuicCryptoServerConfig>( + QuicCryptoServerConfig::TESTING, QuicRandom::GetInstance(), + std::move(proof_source), KeyExchangeSource::Default(), + std::move(proof_verifier)); + } + void CreateTlsServerHandshakerTestSession(MockQuicConnectionHelper* helper, MockAlarmFactory* alarm_factory) { server_connection_ = new PacketSavingConnection( @@ -980,6 +1027,65 @@ EXPECT_TRUE(server_handshaker_->received_client_cert()); } +TEST_P(TlsServerHandshakerTest, RequestClientCertAndVerify) { + ASSERT_TRUE(SetupClientCert()); + InitializeFakeClient(); + + initial_client_cert_mode_ = ClientCertMode::kRequest; + auto proof_source = InitializeProofSource(); + auto proof_verifier = std::make_unique<NiceMock<MockProofVerifier>>(); + EXPECT_CALL(*proof_verifier, VerifyCertChain).WillOnce(Return(QUIC_SUCCESS)); + InitializeServerConfigWithProofSourceAndVerifier(std::move(proof_source), + std::move(proof_verifier)); + InitializeServerWithFakeProofSourceHandle(); + server_handshaker_->SetupProofSourceHandle( + /*select_cert_action=*/FakeProofSourceHandle::Action::DELEGATE_SYNC, + /*compute_signature_action=*/FakeProofSourceHandle::Action:: + DELEGATE_SYNC); + + CompleteCryptoHandshake(); + ExpectHandshakeSuccessful(); + EXPECT_TRUE(server_handshaker_->received_client_cert()); + EXPECT_EQ(QuicAsyncStatus::QUIC_SUCCESS, + server_handshaker_->verify_cert_chain_status()); +} + +TEST_P(TlsServerHandshakerTest, RequestClientCertAndFailVerification) { + ASSERT_TRUE(SetupClientCert()); + InitializeFakeClient(); + + initial_client_cert_mode_ = ClientCertMode::kRequest; + auto proof_source = InitializeProofSource(); + auto proof_verifier = std::make_unique<NiceMock<MockProofVerifier>>(); + EXPECT_CALL(*proof_verifier, VerifyCertChain).WillOnce(Return(QUIC_FAILURE)); + InitializeServerConfigWithProofSourceAndVerifier(std::move(proof_source), + std::move(proof_verifier)); + InitializeServerWithFakeProofSourceHandle(); + server_handshaker_->SetupProofSourceHandle( + /*select_cert_action=*/FakeProofSourceHandle::Action::DELEGATE_SYNC, + /*compute_signature_action=*/FakeProofSourceHandle::Action:: + DELEGATE_SYNC); + + // Advance handshake until it can't make any more progress due to the + // failing proof verifier. + while (!client_stream()->one_rtt_keys_available() || + !server_stream()->one_rtt_keys_available()) { + auto previous_moved_messages_counts = moved_messages_counts_; + AdvanceHandshakeWithFakeClient(); + // Break if the handshake has stopped making progress. + if (previous_moved_messages_counts == moved_messages_counts_) { + break; + } + } + + // Check that handshake did not finish successfully from the server side. + EXPECT_FALSE(server_stream()->one_rtt_keys_available()); + // Handshake is not complete and not confirmed. + EXPECT_EQ(HANDSHAKE_PROCESSED, server_stream()->GetHandshakeState()); + EXPECT_EQ(QuicAsyncStatus::QUIC_FAILURE, + server_handshaker_->verify_cert_chain_status()); +} + TEST_P(TlsServerHandshakerTest, SetInvalidServerTransportParamsByDelayedSslConfig) { ASSERT_TRUE(SetupClientCert());