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/test_tools/crypto_test_utils.h" |
| 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" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 8 | #include "net/third_party/quiche/src/quic/core/quic_utils.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9 | #include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h" |
| 10 | #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
| 11 | #include "net/third_party/quiche/src/quic/platform/api/quic_text_utils.h" |
| 12 | #include "net/third_party/quiche/src/quic/test_tools/mock_clock.h" |
| 13 | |
| 14 | namespace quic { |
| 15 | namespace test { |
| 16 | |
| 17 | class ShloVerifier { |
| 18 | public: |
| 19 | ShloVerifier( |
| 20 | QuicCryptoServerConfig* crypto_config, |
| 21 | QuicSocketAddress server_addr, |
| 22 | QuicSocketAddress client_addr, |
| 23 | const QuicClock* clock, |
| 24 | QuicReferenceCountedPointer<QuicSignedServerConfig> signed_config, |
| 25 | QuicCompressedCertsCache* compressed_certs_cache) |
| 26 | : crypto_config_(crypto_config), |
| 27 | server_addr_(server_addr), |
| 28 | client_addr_(client_addr), |
| 29 | clock_(clock), |
| 30 | signed_config_(signed_config), |
| 31 | compressed_certs_cache_(compressed_certs_cache), |
| 32 | params_(new QuicCryptoNegotiatedParameters) {} |
| 33 | |
| 34 | class ValidateClientHelloCallback : public ValidateClientHelloResultCallback { |
| 35 | public: |
| 36 | explicit ValidateClientHelloCallback(ShloVerifier* shlo_verifier) |
| 37 | : shlo_verifier_(shlo_verifier) {} |
| 38 | void Run(QuicReferenceCountedPointer< |
| 39 | ValidateClientHelloResultCallback::Result> result, |
| 40 | std::unique_ptr<ProofSource::Details> /* details */) override { |
| 41 | shlo_verifier_->ValidateClientHelloDone(result); |
| 42 | } |
| 43 | |
| 44 | private: |
| 45 | ShloVerifier* shlo_verifier_; |
| 46 | }; |
| 47 | |
| 48 | std::unique_ptr<ValidateClientHelloCallback> |
| 49 | GetValidateClientHelloCallback() { |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 50 | return std::make_unique<ValidateClientHelloCallback>(this); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | private: |
| 54 | void ValidateClientHelloDone( |
| 55 | const QuicReferenceCountedPointer< |
| 56 | ValidateClientHelloResultCallback::Result>& result) { |
| 57 | result_ = result; |
| 58 | crypto_config_->ProcessClientHello( |
| 59 | result_, /*reject_only=*/false, |
| 60 | /*connection_id=*/TestConnectionId(1), server_addr_, client_addr_, |
wub | ecf9bd8 | 2019-06-05 04:57:02 -0700 | [diff] [blame] | 61 | AllSupportedVersions().front(), AllSupportedVersions(), clock_, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 62 | QuicRandom::GetInstance(), compressed_certs_cache_, params_, |
| 63 | signed_config_, /*total_framing_overhead=*/50, kDefaultMaxPacketSize, |
| 64 | GetProcessClientHelloCallback()); |
| 65 | } |
| 66 | |
| 67 | class ProcessClientHelloCallback : public ProcessClientHelloResultCallback { |
| 68 | public: |
| 69 | explicit ProcessClientHelloCallback(ShloVerifier* shlo_verifier) |
| 70 | : shlo_verifier_(shlo_verifier) {} |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 71 | void Run(QuicErrorCode /*error*/, |
| 72 | const std::string& /*error_details*/, |
| 73 | std::unique_ptr<CryptoHandshakeMessage> message, |
| 74 | std::unique_ptr<DiversificationNonce> /*diversification_nonce*/, |
| 75 | std::unique_ptr<ProofSource::Details> /*proof_source_details*/) |
| 76 | override { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 77 | shlo_verifier_->ProcessClientHelloDone(std::move(message)); |
| 78 | } |
| 79 | |
| 80 | private: |
| 81 | ShloVerifier* shlo_verifier_; |
| 82 | }; |
| 83 | |
| 84 | std::unique_ptr<ProcessClientHelloCallback> GetProcessClientHelloCallback() { |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 85 | return std::make_unique<ProcessClientHelloCallback>(this); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void ProcessClientHelloDone(std::unique_ptr<CryptoHandshakeMessage> message) { |
| 89 | // Verify output is a SHLO. |
| 90 | EXPECT_EQ(message->tag(), kSHLO) |
| 91 | << "Fail to pass validation. Get " << message->DebugString(); |
| 92 | } |
| 93 | |
| 94 | QuicCryptoServerConfig* crypto_config_; |
| 95 | QuicSocketAddress server_addr_; |
| 96 | QuicSocketAddress client_addr_; |
| 97 | const QuicClock* clock_; |
| 98 | QuicReferenceCountedPointer<QuicSignedServerConfig> signed_config_; |
| 99 | QuicCompressedCertsCache* compressed_certs_cache_; |
| 100 | |
| 101 | QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters> params_; |
| 102 | QuicReferenceCountedPointer<ValidateClientHelloResultCallback::Result> |
| 103 | result_; |
| 104 | }; |
| 105 | |
| 106 | class CryptoTestUtilsTest : public QuicTest {}; |
| 107 | |
| 108 | TEST_F(CryptoTestUtilsTest, TestGenerateFullCHLO) { |
| 109 | MockClock clock; |
| 110 | QuicCryptoServerConfig crypto_config( |
| 111 | QuicCryptoServerConfig::TESTING, QuicRandom::GetInstance(), |
nharper | 6ebe83b | 2019-06-13 17:43:52 -0700 | [diff] [blame] | 112 | crypto_test_utils::ProofSourceForTesting(), KeyExchangeSource::Default()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 113 | QuicSocketAddress server_addr(QuicIpAddress::Any4(), 5); |
| 114 | QuicSocketAddress client_addr(QuicIpAddress::Loopback4(), 1); |
| 115 | QuicReferenceCountedPointer<QuicSignedServerConfig> signed_config( |
| 116 | new QuicSignedServerConfig); |
| 117 | QuicCompressedCertsCache compressed_certs_cache( |
| 118 | QuicCompressedCertsCache::kQuicCompressedCertsCacheSize); |
| 119 | CryptoHandshakeMessage full_chlo; |
| 120 | |
| 121 | QuicCryptoServerConfig::ConfigOptions old_config_options; |
| 122 | old_config_options.id = "old-config-id"; |
QUICHE team | d5af58a | 2019-03-14 20:35:50 -0700 | [diff] [blame] | 123 | crypto_config.AddDefaultConfig(QuicRandom::GetInstance(), &clock, |
| 124 | old_config_options); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 125 | QuicCryptoServerConfig::ConfigOptions new_config_options; |
QUICHE team | bbaa8be | 2019-03-21 12:54:17 -0700 | [diff] [blame] | 126 | QuicServerConfigProtobuf primary_config = crypto_config.GenerateConfig( |
| 127 | QuicRandom::GetInstance(), &clock, new_config_options); |
| 128 | primary_config.set_primary_time(clock.WallNow().ToUNIXSeconds()); |
QUICHE team | d5af58a | 2019-03-14 20:35:50 -0700 | [diff] [blame] | 129 | std::unique_ptr<CryptoHandshakeMessage> msg = |
| 130 | crypto_config.AddConfig(std::move(primary_config), clock.WallNow()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 131 | QuicStringPiece orbit; |
| 132 | ASSERT_TRUE(msg->GetStringPiece(kORBT, &orbit)); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 133 | std::string nonce; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 134 | CryptoUtils::GenerateNonce(clock.WallNow(), QuicRandom::GetInstance(), orbit, |
| 135 | &nonce); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 136 | std::string nonce_hex = "#" + QuicTextUtils::HexEncode(nonce); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 137 | |
| 138 | char public_value[32]; |
| 139 | memset(public_value, 42, sizeof(public_value)); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 140 | std::string pub_hex = |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 141 | "#" + QuicTextUtils::HexEncode(public_value, sizeof(public_value)); |
| 142 | |
| 143 | QuicTransportVersion version(AllSupportedTransportVersions().front()); |
| 144 | CryptoHandshakeMessage inchoate_chlo = crypto_test_utils::CreateCHLO( |
| 145 | {{"PDMD", "X509"}, |
| 146 | {"AEAD", "AESG"}, |
| 147 | {"KEXS", "C255"}, |
| 148 | {"COPT", "SREJ"}, |
| 149 | {"PUBS", pub_hex}, |
| 150 | {"NONC", nonce_hex}, |
| 151 | {"VER\0", |
| 152 | QuicVersionLabelToString(QuicVersionToQuicVersionLabel(version))}}, |
| 153 | kClientHelloMinimumSize); |
| 154 | |
| 155 | crypto_test_utils::GenerateFullCHLO( |
| 156 | inchoate_chlo, &crypto_config, server_addr, client_addr, version, &clock, |
| 157 | signed_config, &compressed_certs_cache, &full_chlo); |
| 158 | // Verify that full_chlo can pass crypto_config's verification. |
| 159 | ShloVerifier shlo_verifier(&crypto_config, server_addr, client_addr, &clock, |
| 160 | signed_config, &compressed_certs_cache); |
| 161 | crypto_config.ValidateClientHello( |
| 162 | full_chlo, client_addr.host(), server_addr, version, &clock, |
| 163 | signed_config, shlo_verifier.GetValidateClientHelloCallback()); |
| 164 | } |
| 165 | |
| 166 | } // namespace test |
| 167 | } // namespace quic |