blob: 3c9f18f0096f7cedd5175187e56e85dc06cfdc08 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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
dschinazi56fb53e2019-06-21 15:30:04 -07007#include "net/third_party/quiche/src/quic/core/proto/crypto_server_config_proto.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -05008#include "net/third_party/quiche/src/quic/core/quic_utils.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -05009#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
14namespace quic {
15namespace test {
16
17class 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() {
vasilvv0fc587f2019-09-06 13:33:08 -070050 return std::make_unique<ValidateClientHelloCallback>(this);
QUICHE teama6ef0a62019-03-07 20:34:33 -050051 }
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_,
wubecf9bd82019-06-05 04:57:02 -070061 AllSupportedVersions().front(), AllSupportedVersions(), clock_,
QUICHE teama6ef0a62019-03-07 20:34:33 -050062 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) {}
dschinazi17d42422019-06-18 16:35:07 -070071 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 teama6ef0a62019-03-07 20:34:33 -050077 shlo_verifier_->ProcessClientHelloDone(std::move(message));
78 }
79
80 private:
81 ShloVerifier* shlo_verifier_;
82 };
83
84 std::unique_ptr<ProcessClientHelloCallback> GetProcessClientHelloCallback() {
vasilvv0fc587f2019-09-06 13:33:08 -070085 return std::make_unique<ProcessClientHelloCallback>(this);
QUICHE teama6ef0a62019-03-07 20:34:33 -050086 }
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
106class CryptoTestUtilsTest : public QuicTest {};
107
108TEST_F(CryptoTestUtilsTest, TestGenerateFullCHLO) {
109 MockClock clock;
110 QuicCryptoServerConfig crypto_config(
111 QuicCryptoServerConfig::TESTING, QuicRandom::GetInstance(),
nharper6ebe83b2019-06-13 17:43:52 -0700112 crypto_test_utils::ProofSourceForTesting(), KeyExchangeSource::Default());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500113 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 teamd5af58a2019-03-14 20:35:50 -0700123 crypto_config.AddDefaultConfig(QuicRandom::GetInstance(), &clock,
124 old_config_options);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500125 QuicCryptoServerConfig::ConfigOptions new_config_options;
QUICHE teambbaa8be2019-03-21 12:54:17 -0700126 QuicServerConfigProtobuf primary_config = crypto_config.GenerateConfig(
127 QuicRandom::GetInstance(), &clock, new_config_options);
128 primary_config.set_primary_time(clock.WallNow().ToUNIXSeconds());
QUICHE teamd5af58a2019-03-14 20:35:50 -0700129 std::unique_ptr<CryptoHandshakeMessage> msg =
130 crypto_config.AddConfig(std::move(primary_config), clock.WallNow());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500131 QuicStringPiece orbit;
132 ASSERT_TRUE(msg->GetStringPiece(kORBT, &orbit));
vasilvvc48c8712019-03-11 13:38:16 -0700133 std::string nonce;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500134 CryptoUtils::GenerateNonce(clock.WallNow(), QuicRandom::GetInstance(), orbit,
135 &nonce);
vasilvvc48c8712019-03-11 13:38:16 -0700136 std::string nonce_hex = "#" + QuicTextUtils::HexEncode(nonce);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500137
138 char public_value[32];
139 memset(public_value, 42, sizeof(public_value));
vasilvvc48c8712019-03-11 13:38:16 -0700140 std::string pub_hex =
QUICHE teama6ef0a62019-03-07 20:34:33 -0500141 "#" + 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