QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/third_party/quiche/src/quic/core/quic_crypto_client_handshaker.h" |
dschinazi | 580d30b | 2019-04-26 15:05:20 -0700 | [diff] [blame] | 6 | |
dschinazi | 56fb53e | 2019-06-21 15:30:04 -0700 | [diff] [blame] | 7 | #include "net/third_party/quiche/src/quic/core/proto/crypto_server_config_proto.h" |
dschinazi | 580d30b | 2019-04-26 15:05:20 -0700 | [diff] [blame] | 8 | #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9 | #include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h" |
| 10 | |
| 11 | namespace quic { |
| 12 | namespace { |
| 13 | |
| 14 | using ::testing::Test; |
| 15 | |
| 16 | class TestProofHandler : public QuicCryptoClientStream::ProofHandler { |
| 17 | public: |
| 18 | ~TestProofHandler() override {} |
| 19 | void OnProofValid( |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 20 | const QuicCryptoClientConfig::CachedState& /*cached*/) override {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 21 | void OnProofVerifyDetailsAvailable( |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 22 | const ProofVerifyDetails& /*verify_details*/) override {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | class InsecureProofVerifier : public ProofVerifier { |
| 26 | public: |
| 27 | InsecureProofVerifier() {} |
| 28 | ~InsecureProofVerifier() override {} |
| 29 | |
| 30 | // ProofVerifier override. |
| 31 | QuicAsyncStatus VerifyProof( |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 32 | const std::string& /*hostname*/, |
| 33 | const uint16_t /*port*/, |
| 34 | const std::string& /*server_config*/, |
| 35 | QuicTransportVersion /*transport_version*/, |
| 36 | QuicStringPiece /*chlo_hash*/, |
| 37 | const std::vector<std::string>& /*certs*/, |
| 38 | const std::string& /*cert_sct*/, |
| 39 | const std::string& /*signature*/, |
| 40 | const ProofVerifyContext* /*context*/, |
| 41 | std::string* /*error_details*/, |
| 42 | std::unique_ptr<ProofVerifyDetails>* /*verify_details*/, |
| 43 | std::unique_ptr<ProofVerifierCallback> /*callback*/) override { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 44 | return QUIC_SUCCESS; |
| 45 | } |
| 46 | |
| 47 | QuicAsyncStatus VerifyCertChain( |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 48 | const std::string& /*hostname*/, |
| 49 | const std::vector<std::string>& /*certs*/, |
| 50 | const std::string& /*ocsp_response*/, |
| 51 | const std::string& /*cert_sct*/, |
| 52 | const ProofVerifyContext* /*context*/, |
| 53 | std::string* /*error_details*/, |
| 54 | std::unique_ptr<ProofVerifyDetails>* /*details*/, |
| 55 | std::unique_ptr<ProofVerifierCallback> /*callback*/) override { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 56 | return QUIC_SUCCESS; |
| 57 | } |
| 58 | |
| 59 | std::unique_ptr<ProofVerifyContext> CreateDefaultContext() override { |
| 60 | return nullptr; |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | class DummyProofSource : public ProofSource { |
| 65 | public: |
| 66 | DummyProofSource() {} |
| 67 | ~DummyProofSource() override {} |
| 68 | |
| 69 | // ProofSource override. |
| 70 | void GetProof(const QuicSocketAddress& server_address, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 71 | const std::string& hostname, |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 72 | const std::string& /*server_config*/, |
| 73 | QuicTransportVersion /*transport_version*/, |
| 74 | QuicStringPiece /*chlo_hash*/, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 75 | std::unique_ptr<Callback> callback) override { |
| 76 | QuicReferenceCountedPointer<ProofSource::Chain> chain = |
| 77 | GetCertChain(server_address, hostname); |
| 78 | QuicCryptoProof proof; |
| 79 | proof.signature = "Dummy signature"; |
| 80 | proof.leaf_cert_scts = "Dummy timestamp"; |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 81 | callback->Run(true, chain, proof, /*details=*/nullptr); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | QuicReferenceCountedPointer<Chain> GetCertChain( |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 85 | const QuicSocketAddress& /*server_address*/, |
| 86 | const std::string& /*hostname*/) override { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 87 | std::vector<std::string> certs; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 88 | certs.push_back("Dummy cert"); |
| 89 | return QuicReferenceCountedPointer<ProofSource::Chain>( |
| 90 | new ProofSource::Chain(certs)); |
| 91 | } |
| 92 | |
| 93 | void ComputeTlsSignature( |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 94 | const QuicSocketAddress& /*server_address*/, |
| 95 | const std::string& /*hostname*/, |
| 96 | uint16_t /*signature_algorit*/, |
| 97 | QuicStringPiece /*in*/, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 98 | std::unique_ptr<SignatureCallback> callback) override { |
| 99 | callback->Run(true, "Dummy signature"); |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | class Handshaker : public QuicCryptoClientHandshaker { |
| 104 | public: |
| 105 | Handshaker(const QuicServerId& server_id, |
| 106 | QuicCryptoClientStream* stream, |
| 107 | QuicSession* session, |
| 108 | std::unique_ptr<ProofVerifyContext> verify_context, |
| 109 | QuicCryptoClientConfig* crypto_config, |
| 110 | QuicCryptoClientStream::ProofHandler* proof_handler) |
| 111 | : QuicCryptoClientHandshaker(server_id, |
| 112 | stream, |
| 113 | session, |
| 114 | std::move(verify_context), |
| 115 | crypto_config, |
| 116 | proof_handler) {} |
| 117 | |
| 118 | void DoSendCHLOTest(QuicCryptoClientConfig::CachedState* cached) { |
| 119 | QuicCryptoClientHandshaker::DoSendCHLO(cached); |
| 120 | } |
| 121 | }; |
| 122 | |
| 123 | class QuicCryptoClientHandshakerTest : public Test { |
| 124 | protected: |
| 125 | QuicCryptoClientHandshakerTest() |
| 126 | : proof_handler_(), |
| 127 | helper_(), |
| 128 | alarm_factory_(), |
| 129 | server_id_("host", 123), |
| 130 | connection_(new test::MockQuicConnection(&helper_, |
| 131 | &alarm_factory_, |
| 132 | Perspective::IS_CLIENT)), |
| 133 | session_(connection_, false), |
nharper | 6ebe83b | 2019-06-13 17:43:52 -0700 | [diff] [blame] | 134 | crypto_client_config_(QuicMakeUnique<InsecureProofVerifier>()), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 135 | client_stream_(new QuicCryptoClientStream(server_id_, |
| 136 | &session_, |
| 137 | nullptr, |
| 138 | &crypto_client_config_, |
| 139 | &proof_handler_)), |
| 140 | handshaker_(server_id_, |
| 141 | client_stream_, |
| 142 | &session_, |
| 143 | nullptr, |
| 144 | &crypto_client_config_, |
| 145 | &proof_handler_), |
| 146 | state_() { |
| 147 | // Session takes the ownership of the client stream! (but handshaker also |
| 148 | // takes a reference to it, but doesn't take the ownership). |
| 149 | session_.SetCryptoStream(client_stream_); |
| 150 | session_.Initialize(); |
| 151 | } |
| 152 | |
| 153 | void InitializeServerParametersToEnableFullHello() { |
| 154 | QuicCryptoServerConfig::ConfigOptions options; |
QUICHE team | bbaa8be | 2019-03-21 12:54:17 -0700 | [diff] [blame] | 155 | QuicServerConfigProtobuf config = QuicCryptoServerConfig::GenerateConfig( |
| 156 | helper_.GetRandomGenerator(), helper_.GetClock(), options); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 157 | state_.Initialize( |
QUICHE team | bbaa8be | 2019-03-21 12:54:17 -0700 | [diff] [blame] | 158 | config.config(), "sourcetoken", std::vector<std::string>{"Dummy cert"}, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 159 | "", "chlo_hash", "signature", helper_.GetClock()->WallNow(), |
| 160 | helper_.GetClock()->WallNow().Add(QuicTime::Delta::FromSeconds(30))); |
| 161 | |
| 162 | state_.SetProofValid(); |
| 163 | } |
| 164 | |
| 165 | TestProofHandler proof_handler_; |
| 166 | test::MockQuicConnectionHelper helper_; |
| 167 | test::MockAlarmFactory alarm_factory_; |
| 168 | QuicServerId server_id_; |
| 169 | // Session takes the ownership of the connection. |
| 170 | test::MockQuicConnection* connection_; |
| 171 | test::MockQuicSession session_; |
| 172 | QuicCryptoClientConfig crypto_client_config_; |
| 173 | QuicCryptoClientStream* client_stream_; |
| 174 | Handshaker handshaker_; |
| 175 | QuicCryptoClientConfig::CachedState state_; |
| 176 | }; |
| 177 | |
| 178 | TEST_F(QuicCryptoClientHandshakerTest, TestSendFullPaddingInInchoateHello) { |
| 179 | handshaker_.DoSendCHLOTest(&state_); |
| 180 | |
| 181 | EXPECT_TRUE(connection_->fully_pad_during_crypto_handshake()); |
| 182 | } |
| 183 | |
| 184 | TEST_F(QuicCryptoClientHandshakerTest, TestDisabledPaddingInInchoateHello) { |
| 185 | crypto_client_config_.set_pad_inchoate_hello(false); |
| 186 | handshaker_.DoSendCHLOTest(&state_); |
| 187 | EXPECT_FALSE(connection_->fully_pad_during_crypto_handshake()); |
| 188 | } |
| 189 | |
| 190 | TEST_F(QuicCryptoClientHandshakerTest, |
| 191 | TestPaddingInFullHelloEvenIfInchoateDisabled) { |
| 192 | // Disable inchoate, but full hello should still be padded. |
| 193 | crypto_client_config_.set_pad_inchoate_hello(false); |
| 194 | |
| 195 | InitializeServerParametersToEnableFullHello(); |
| 196 | |
| 197 | handshaker_.DoSendCHLOTest(&state_); |
| 198 | EXPECT_TRUE(connection_->fully_pad_during_crypto_handshake()); |
| 199 | } |
| 200 | |
| 201 | TEST_F(QuicCryptoClientHandshakerTest, TestNoPaddingInFullHelloWhenDisabled) { |
| 202 | crypto_client_config_.set_pad_full_hello(false); |
| 203 | |
| 204 | InitializeServerParametersToEnableFullHello(); |
| 205 | |
| 206 | handshaker_.DoSendCHLOTest(&state_); |
| 207 | EXPECT_FALSE(connection_->fully_pad_during_crypto_handshake()); |
| 208 | } |
| 209 | |
| 210 | } // namespace |
| 211 | } // namespace quic |