Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(269)

Side by Side Diff: net/quic/crypto/quic_crypto_client_config_test.cc

Issue 2193073003: Move shared files in net/quic/ into net/quic/core/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: io_thread_unittest.cc Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2013 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/quic/crypto/quic_crypto_client_config.h"
6
7 #include "net/quic/crypto/proof_verifier.h"
8 #include "net/quic/quic_server_id.h"
9 #include "net/quic/test_tools/crypto_test_utils.h"
10 #include "net/quic/test_tools/mock_random.h"
11 #include "net/quic/test_tools/quic_test_utils.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 using std::string;
15 using std::vector;
16
17 namespace net {
18 namespace test {
19 namespace {
20
21 class TestProofVerifyDetails : public ProofVerifyDetails {
22 ~TestProofVerifyDetails() override {}
23
24 // ProofVerifyDetails implementation
25 ProofVerifyDetails* Clone() const override {
26 return new TestProofVerifyDetails;
27 }
28 };
29
30 } // namespace
31
32 TEST(QuicCryptoClientConfigTest, CachedState_IsEmpty) {
33 QuicCryptoClientConfig::CachedState state;
34 EXPECT_TRUE(state.IsEmpty());
35 }
36
37 TEST(QuicCryptoClientConfigTest, CachedState_IsComplete) {
38 QuicCryptoClientConfig::CachedState state;
39 EXPECT_FALSE(state.IsComplete(QuicWallTime::FromUNIXSeconds(0)));
40 }
41
42 TEST(QuicCryptoClientConfigTest, CachedState_GenerationCounter) {
43 QuicCryptoClientConfig::CachedState state;
44 EXPECT_EQ(0u, state.generation_counter());
45 state.SetProofInvalid();
46 EXPECT_EQ(1u, state.generation_counter());
47 }
48
49 TEST(QuicCryptoClientConfigTest, CachedState_SetProofVerifyDetails) {
50 QuicCryptoClientConfig::CachedState state;
51 EXPECT_TRUE(state.proof_verify_details() == nullptr);
52 ProofVerifyDetails* details = new TestProofVerifyDetails;
53 state.SetProofVerifyDetails(details);
54 EXPECT_EQ(details, state.proof_verify_details());
55 }
56
57 TEST(QuicCryptoClientConfigTest, CachedState_ServerDesignatedConnectionId) {
58 QuicCryptoClientConfig::CachedState state;
59 EXPECT_FALSE(state.has_server_designated_connection_id());
60
61 QuicConnectionId connection_id = 1234;
62 state.add_server_designated_connection_id(connection_id);
63 EXPECT_TRUE(state.has_server_designated_connection_id());
64 EXPECT_EQ(connection_id, state.GetNextServerDesignatedConnectionId());
65 EXPECT_FALSE(state.has_server_designated_connection_id());
66
67 // Allow the ID to be set multiple times. It's unusual that this would
68 // happen, but not impossible.
69 ++connection_id;
70 state.add_server_designated_connection_id(connection_id);
71 EXPECT_TRUE(state.has_server_designated_connection_id());
72 EXPECT_EQ(connection_id, state.GetNextServerDesignatedConnectionId());
73 ++connection_id;
74 state.add_server_designated_connection_id(connection_id);
75 EXPECT_EQ(connection_id, state.GetNextServerDesignatedConnectionId());
76 EXPECT_FALSE(state.has_server_designated_connection_id());
77
78 // Test FIFO behavior.
79 const QuicConnectionId first_cid = 0xdeadbeef;
80 const QuicConnectionId second_cid = 0xfeedbead;
81 state.add_server_designated_connection_id(first_cid);
82 state.add_server_designated_connection_id(second_cid);
83 EXPECT_TRUE(state.has_server_designated_connection_id());
84 EXPECT_EQ(first_cid, state.GetNextServerDesignatedConnectionId());
85 EXPECT_EQ(second_cid, state.GetNextServerDesignatedConnectionId());
86 }
87
88 TEST(QuicCryptoClientConfigTest, CachedState_ServerIdConsumedBeforeSet) {
89 QuicCryptoClientConfig::CachedState state;
90 EXPECT_FALSE(state.has_server_designated_connection_id());
91 #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
92 EXPECT_DEBUG_DEATH(state.GetNextServerDesignatedConnectionId(),
93 "Attempting to consume a connection id "
94 "that was never designated.");
95 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
96 }
97
98 TEST(QuicCryptoClientConfigTest, CachedState_ServerNonce) {
99 QuicCryptoClientConfig::CachedState state;
100 EXPECT_FALSE(state.has_server_nonce());
101
102 string server_nonce = "nonce_1";
103 state.add_server_nonce(server_nonce);
104 EXPECT_TRUE(state.has_server_nonce());
105 EXPECT_EQ(server_nonce, state.GetNextServerNonce());
106 EXPECT_FALSE(state.has_server_nonce());
107
108 // Allow the ID to be set multiple times. It's unusual that this would
109 // happen, but not impossible.
110 server_nonce = "nonce_2";
111 state.add_server_nonce(server_nonce);
112 EXPECT_TRUE(state.has_server_nonce());
113 EXPECT_EQ(server_nonce, state.GetNextServerNonce());
114 server_nonce = "nonce_3";
115 state.add_server_nonce(server_nonce);
116 EXPECT_EQ(server_nonce, state.GetNextServerNonce());
117 EXPECT_FALSE(state.has_server_nonce());
118
119 // Test FIFO behavior.
120 const string first_nonce = "first_nonce";
121 const string second_nonce = "second_nonce";
122 state.add_server_nonce(first_nonce);
123 state.add_server_nonce(second_nonce);
124 EXPECT_TRUE(state.has_server_nonce());
125 EXPECT_EQ(first_nonce, state.GetNextServerNonce());
126 EXPECT_EQ(second_nonce, state.GetNextServerNonce());
127 }
128
129 TEST(QuicCryptoClientConfigTest, CachedState_ServerNonceConsumedBeforeSet) {
130 QuicCryptoClientConfig::CachedState state;
131 EXPECT_FALSE(state.has_server_nonce());
132 #if GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
133 EXPECT_DEBUG_DEATH(state.GetNextServerNonce(),
134 "Attempting to consume a server nonce "
135 "that was never designated.");
136 #endif // GTEST_HAS_DEATH_TEST && !defined(NDEBUG)
137 }
138
139 TEST(QuicCryptoClientConfigTest, CachedState_InitializeFrom) {
140 QuicCryptoClientConfig::CachedState state;
141 QuicCryptoClientConfig::CachedState other;
142 state.set_source_address_token("TOKEN");
143 // TODO(rch): Populate other fields of |state|.
144 other.InitializeFrom(state);
145 EXPECT_EQ(state.server_config(), other.server_config());
146 EXPECT_EQ(state.source_address_token(), other.source_address_token());
147 EXPECT_EQ(state.certs(), other.certs());
148 EXPECT_EQ(1u, other.generation_counter());
149 EXPECT_FALSE(state.has_server_designated_connection_id());
150 EXPECT_FALSE(state.has_server_nonce());
151 }
152
153 TEST(QuicCryptoClientConfigTest, InchoateChlo) {
154 QuicCryptoClientConfig::CachedState state;
155 QuicCryptoClientConfig config(CryptoTestUtils::ProofVerifierForTesting());
156 QuicCryptoNegotiatedParameters params;
157 CryptoHandshakeMessage msg;
158 QuicServerId server_id("www.google.com", 443, PRIVACY_MODE_DISABLED);
159 MockRandom rand;
160 config.FillInchoateClientHello(server_id, QuicVersionMax(), &state, &rand,
161 /* demand_x509_proof= */ true, &params, &msg);
162
163 QuicTag cver;
164 EXPECT_EQ(QUIC_NO_ERROR, msg.GetUint32(kVER, &cver));
165 EXPECT_EQ(QuicVersionToQuicTag(QuicVersionMax()), cver);
166 StringPiece proof_nonce;
167 EXPECT_TRUE(msg.GetStringPiece(kNONP, &proof_nonce));
168 EXPECT_EQ(string(32, 'r'), proof_nonce);
169 }
170
171 TEST(QuicCryptoClientConfigTest, PreferAesGcm) {
172 QuicCryptoClientConfig config(CryptoTestUtils::ProofVerifierForTesting());
173 if (config.aead.size() > 1)
174 EXPECT_NE(kAESG, config.aead[0]);
175 config.PreferAesGcm();
176 EXPECT_EQ(kAESG, config.aead[0]);
177 }
178
179 TEST(QuicCryptoClientConfigTest, InchoateChloSecure) {
180 QuicCryptoClientConfig::CachedState state;
181 QuicCryptoClientConfig config(CryptoTestUtils::ProofVerifierForTesting());
182 QuicCryptoNegotiatedParameters params;
183 CryptoHandshakeMessage msg;
184 QuicServerId server_id("www.google.com", 443, PRIVACY_MODE_DISABLED);
185 MockRandom rand;
186 config.FillInchoateClientHello(server_id, QuicVersionMax(), &state, &rand,
187 /* demand_x509_proof= */ true, &params, &msg);
188
189 QuicTag pdmd;
190 EXPECT_EQ(QUIC_NO_ERROR, msg.GetUint32(kPDMD, &pdmd));
191 EXPECT_EQ(kX509, pdmd);
192 StringPiece scid;
193 EXPECT_FALSE(msg.GetStringPiece(kSCID, &scid));
194 }
195
196 TEST(QuicCryptoClientConfigTest, InchoateChloSecureWithSCID) {
197 QuicCryptoClientConfig::CachedState state;
198 CryptoHandshakeMessage scfg;
199 scfg.set_tag(kSCFG);
200 uint64_t future = 1;
201 scfg.SetValue(kEXPY, future);
202 scfg.SetStringPiece(kSCID, "12345678");
203 string details;
204 state.SetServerConfig(scfg.GetSerialized().AsStringPiece(),
205 QuicWallTime::FromUNIXSeconds(0), &details);
206
207 QuicCryptoClientConfig config(CryptoTestUtils::ProofVerifierForTesting());
208 QuicCryptoNegotiatedParameters params;
209 CryptoHandshakeMessage msg;
210 QuicServerId server_id("www.google.com", 443, PRIVACY_MODE_DISABLED);
211 MockRandom rand;
212 config.FillInchoateClientHello(server_id, QuicVersionMax(), &state, &rand,
213 /* demand_x509_proof= */ true, &params, &msg);
214
215 StringPiece scid;
216 EXPECT_TRUE(msg.GetStringPiece(kSCID, &scid));
217 EXPECT_EQ("12345678", scid);
218 }
219
220 TEST(QuicCryptoClientConfigTest, InchoateChloSecureNoEcdsa) {
221 QuicCryptoClientConfig::CachedState state;
222 QuicCryptoClientConfig config(CryptoTestUtils::ProofVerifierForTesting());
223 config.DisableEcdsa();
224 QuicCryptoNegotiatedParameters params;
225 CryptoHandshakeMessage msg;
226 QuicServerId server_id("www.google.com", 443, PRIVACY_MODE_DISABLED);
227 MockRandom rand;
228 config.FillInchoateClientHello(server_id, QuicVersionMax(), &state, &rand,
229 /* demand_x509_proof= */ true, &params, &msg);
230
231 QuicTag pdmd;
232 EXPECT_EQ(QUIC_NO_ERROR, msg.GetUint32(kPDMD, &pdmd));
233 EXPECT_EQ(kX59R, pdmd);
234 }
235
236 TEST(QuicCryptoClientConfigTest, FillClientHello) {
237 QuicCryptoClientConfig::CachedState state;
238 QuicCryptoClientConfig config(CryptoTestUtils::ProofVerifierForTesting());
239 QuicCryptoNegotiatedParameters params;
240 QuicConnectionId kConnectionId = 1234;
241 string error_details;
242 MockRandom rand;
243 CryptoHandshakeMessage chlo;
244 QuicServerId server_id("www.google.com", 443, PRIVACY_MODE_DISABLED);
245 config.FillClientHello(server_id, kConnectionId, QuicVersionMax(),
246 QuicVersionMax(), &state, QuicWallTime::Zero(), &rand,
247 nullptr, // channel_id_key
248 &params, &chlo, &error_details);
249
250 // Verify that certain QuicTags have been set correctly in the CHLO.
251 QuicTag cver;
252 EXPECT_EQ(QUIC_NO_ERROR, chlo.GetUint32(kVER, &cver));
253 EXPECT_EQ(QuicVersionToQuicTag(QuicVersionMax()), cver);
254 }
255
256 TEST(QuicCryptoClientConfigTest, ProcessServerDowngradeAttack) {
257 QuicVersionVector supported_versions = QuicSupportedVersions();
258 if (supported_versions.size() == 1) {
259 // No downgrade attack is possible if the client only supports one version.
260 return;
261 }
262 QuicTagVector supported_version_tags;
263 for (size_t i = supported_versions.size(); i > 0; --i) {
264 supported_version_tags.push_back(
265 QuicVersionToQuicTag(supported_versions[i - 1]));
266 }
267 CryptoHandshakeMessage msg;
268 msg.set_tag(kSHLO);
269 msg.SetVector(kVER, supported_version_tags);
270
271 QuicCryptoClientConfig::CachedState cached;
272 QuicCryptoNegotiatedParameters out_params;
273 string error;
274 QuicCryptoClientConfig config(CryptoTestUtils::ProofVerifierForTesting());
275 EXPECT_EQ(QUIC_VERSION_NEGOTIATION_MISMATCH,
276 config.ProcessServerHello(msg, 0, supported_versions.front(),
277 supported_versions, &cached, &out_params,
278 &error));
279 EXPECT_EQ("Downgrade attack detected", error);
280 }
281
282 TEST(QuicCryptoClientConfigTest, InitializeFrom) {
283 QuicCryptoClientConfig config(CryptoTestUtils::ProofVerifierForTesting());
284 QuicServerId canonical_server_id("www.google.com", 443,
285 PRIVACY_MODE_DISABLED);
286 QuicCryptoClientConfig::CachedState* state =
287 config.LookupOrCreate(canonical_server_id);
288 // TODO(rch): Populate other fields of |state|.
289 state->set_source_address_token("TOKEN");
290 state->SetProofValid();
291
292 QuicServerId other_server_id("mail.google.com", 443, PRIVACY_MODE_DISABLED);
293 config.InitializeFrom(other_server_id, canonical_server_id, &config);
294 QuicCryptoClientConfig::CachedState* other =
295 config.LookupOrCreate(other_server_id);
296
297 EXPECT_EQ(state->server_config(), other->server_config());
298 EXPECT_EQ(state->source_address_token(), other->source_address_token());
299 EXPECT_EQ(state->certs(), other->certs());
300 EXPECT_EQ(1u, other->generation_counter());
301 }
302
303 TEST(QuicCryptoClientConfigTest, Canonical) {
304 QuicCryptoClientConfig config(CryptoTestUtils::ProofVerifierForTesting());
305 config.AddCanonicalSuffix(".google.com");
306 QuicServerId canonical_id1("www.google.com", 443, PRIVACY_MODE_DISABLED);
307 QuicServerId canonical_id2("mail.google.com", 443, PRIVACY_MODE_DISABLED);
308 QuicCryptoClientConfig::CachedState* state =
309 config.LookupOrCreate(canonical_id1);
310 // TODO(rch): Populate other fields of |state|.
311 state->set_source_address_token("TOKEN");
312 state->SetProofValid();
313
314 QuicCryptoClientConfig::CachedState* other =
315 config.LookupOrCreate(canonical_id2);
316
317 EXPECT_TRUE(state->IsEmpty());
318 EXPECT_EQ(state->server_config(), other->server_config());
319 EXPECT_EQ(state->source_address_token(), other->source_address_token());
320 EXPECT_EQ(state->certs(), other->certs());
321 EXPECT_EQ(1u, other->generation_counter());
322
323 QuicServerId different_id("mail.google.org", 443, PRIVACY_MODE_DISABLED);
324 EXPECT_TRUE(config.LookupOrCreate(different_id)->IsEmpty());
325 }
326
327 TEST(QuicCryptoClientConfigTest, CanonicalNotUsedIfNotValid) {
328 QuicCryptoClientConfig config(CryptoTestUtils::ProofVerifierForTesting());
329 config.AddCanonicalSuffix(".google.com");
330 QuicServerId canonical_id1("www.google.com", 443, PRIVACY_MODE_DISABLED);
331 QuicServerId canonical_id2("mail.google.com", 443, PRIVACY_MODE_DISABLED);
332 QuicCryptoClientConfig::CachedState* state =
333 config.LookupOrCreate(canonical_id1);
334 // TODO(rch): Populate other fields of |state|.
335 state->set_source_address_token("TOKEN");
336
337 // Do not set the proof as valid, and check that it is not used
338 // as a canonical entry.
339 EXPECT_TRUE(config.LookupOrCreate(canonical_id2)->IsEmpty());
340 }
341
342 TEST(QuicCryptoClientConfigTest, ClearCachedStates) {
343 QuicCryptoClientConfig config(CryptoTestUtils::ProofVerifierForTesting());
344 QuicServerId server_id("www.google.com", 443, PRIVACY_MODE_DISABLED);
345 QuicCryptoClientConfig::CachedState* state = config.LookupOrCreate(server_id);
346 // TODO(rch): Populate other fields of |state|.
347 vector<string> certs(1);
348 certs[0] = "Hello Cert";
349 state->SetProof(certs, "cert_sct", "chlo_hash", "signature");
350 state->set_source_address_token("TOKEN");
351 state->SetProofValid();
352 EXPECT_EQ(1u, state->generation_counter());
353
354 // Verify LookupOrCreate returns the same data.
355 QuicCryptoClientConfig::CachedState* other = config.LookupOrCreate(server_id);
356
357 EXPECT_EQ(state, other);
358 EXPECT_EQ(1u, other->generation_counter());
359
360 // Clear the cached states.
361 config.ClearCachedStates();
362
363 // Verify LookupOrCreate doesn't have any data.
364 QuicCryptoClientConfig::CachedState* cleared_cache =
365 config.LookupOrCreate(server_id);
366
367 EXPECT_EQ(state, cleared_cache);
368 EXPECT_FALSE(cleared_cache->proof_valid());
369 EXPECT_TRUE(cleared_cache->server_config().empty());
370 EXPECT_TRUE(cleared_cache->certs().empty());
371 EXPECT_TRUE(cleared_cache->cert_sct().empty());
372 EXPECT_TRUE(cleared_cache->signature().empty());
373 EXPECT_EQ(2u, cleared_cache->generation_counter());
374 }
375
376 TEST(QuicCryptoClientConfigTest, ProcessReject) {
377 CryptoHandshakeMessage rej;
378 CryptoTestUtils::FillInDummyReject(&rej, /* stateless */ false);
379
380 // Now process the rejection.
381 QuicCryptoClientConfig::CachedState cached;
382 QuicCryptoNegotiatedParameters out_params;
383 string error;
384 QuicCryptoClientConfig config(CryptoTestUtils::ProofVerifierForTesting());
385 EXPECT_EQ(QUIC_NO_ERROR,
386 config.ProcessRejection(rej, QuicWallTime::FromUNIXSeconds(0),
387 QuicSupportedVersions().front(), "",
388 &cached, &out_params, &error));
389 EXPECT_FALSE(cached.has_server_designated_connection_id());
390 EXPECT_FALSE(cached.has_server_nonce());
391 }
392
393 TEST(QuicCryptoClientConfigTest, ProcessStatelessReject) {
394 // Create a dummy reject message and mark it as stateless.
395 CryptoHandshakeMessage rej;
396 CryptoTestUtils::FillInDummyReject(&rej, /* stateless */ true);
397 const QuicConnectionId kConnectionId = 0xdeadbeef;
398 const string server_nonce = "SERVER_NONCE";
399 rej.SetValue(kRCID, kConnectionId);
400 rej.SetStringPiece(kServerNonceTag, server_nonce);
401
402 // Now process the rejection.
403 QuicCryptoClientConfig::CachedState cached;
404 QuicCryptoNegotiatedParameters out_params;
405 string error;
406 QuicCryptoClientConfig config(CryptoTestUtils::ProofVerifierForTesting());
407 EXPECT_EQ(QUIC_NO_ERROR,
408 config.ProcessRejection(rej, QuicWallTime::FromUNIXSeconds(0),
409 QuicSupportedVersions().front(), "",
410 &cached, &out_params, &error));
411 EXPECT_TRUE(cached.has_server_designated_connection_id());
412 EXPECT_EQ(kConnectionId, cached.GetNextServerDesignatedConnectionId());
413 EXPECT_EQ(server_nonce, cached.GetNextServerNonce());
414 }
415
416 TEST(QuicCryptoClientConfigTest, BadlyFormattedStatelessReject) {
417 // Create a dummy reject message and mark it as stateless. Do not
418 // add an server-designated connection-id.
419 CryptoHandshakeMessage rej;
420 CryptoTestUtils::FillInDummyReject(&rej, /* stateless */ true);
421
422 // Now process the rejection.
423 QuicCryptoClientConfig::CachedState cached;
424 QuicCryptoNegotiatedParameters out_params;
425 string error;
426 QuicCryptoClientConfig config(CryptoTestUtils::ProofVerifierForTesting());
427 EXPECT_EQ(QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND,
428 config.ProcessRejection(rej, QuicWallTime::FromUNIXSeconds(0),
429 QuicSupportedVersions().front(), "",
430 &cached, &out_params, &error));
431 EXPECT_FALSE(cached.has_server_designated_connection_id());
432 EXPECT_EQ("Missing kRCID", error);
433 }
434
435 TEST(QuicCryptoClientConfigTest, ServerNonceinSHLO) {
436 // Test that the server must include a nonce in the SHLO.
437 CryptoHandshakeMessage msg;
438 msg.set_tag(kSHLO);
439 // Choose the latest version.
440 QuicVersionVector supported_versions;
441 QuicVersion version = QuicSupportedVersions().front();
442 supported_versions.push_back(version);
443 QuicTagVector versions;
444 versions.push_back(QuicVersionToQuicTag(version));
445 msg.SetVector(kVER, versions);
446
447 QuicCryptoClientConfig config(CryptoTestUtils::ProofVerifierForTesting());
448 QuicCryptoClientConfig::CachedState cached;
449 QuicCryptoNegotiatedParameters out_params;
450 string error_details;
451 EXPECT_EQ(QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER,
452 config.ProcessServerHello(msg, 0, version, supported_versions,
453 &cached, &out_params, &error_details));
454 EXPECT_EQ("server hello missing server nonce", error_details);
455 }
456
457 } // namespace test
458 } // namespace net
OLDNEW
« no previous file with comments | « net/quic/crypto/quic_crypto_client_config.cc ('k') | net/quic/crypto/quic_crypto_server_config.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698