OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/quic/test_tools/crypto_test_utils.h" | 5 #include "net/quic/test_tools/crypto_test_utils.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "crypto/openssl_util.h" | 9 #include "crypto/openssl_util.h" |
10 #include "crypto/secure_hash.h" | 10 #include "crypto/secure_hash.h" |
11 #include "net/quic/core/crypto/channel_id.h" | 11 #include "net/quic/core/crypto/channel_id.h" |
12 #include "net/quic/core/crypto/common_cert_set.h" | 12 #include "net/quic/core/crypto/common_cert_set.h" |
13 #include "net/quic/core/crypto/crypto_handshake.h" | 13 #include "net/quic/core/crypto/crypto_handshake.h" |
14 #include "net/quic/core/crypto/crypto_server_config_protobuf.h" | 14 #include "net/quic/core/crypto/crypto_server_config_protobuf.h" |
15 #include "net/quic/core/crypto/quic_crypto_server_config.h" | 15 #include "net/quic/core/crypto/quic_crypto_server_config.h" |
16 #include "net/quic/core/crypto/quic_decrypter.h" | 16 #include "net/quic/core/crypto/quic_decrypter.h" |
17 #include "net/quic/core/crypto/quic_encrypter.h" | 17 #include "net/quic/core/crypto/quic_encrypter.h" |
18 #include "net/quic/core/crypto/quic_random.h" | 18 #include "net/quic/core/crypto/quic_random.h" |
19 #include "net/quic/core/quic_crypto_client_stream.h" | 19 #include "net/quic/core/quic_crypto_client_stream.h" |
20 #include "net/quic/core/quic_crypto_server_stream.h" | 20 #include "net/quic/core/quic_crypto_server_stream.h" |
21 #include "net/quic/core/quic_crypto_stream.h" | 21 #include "net/quic/core/quic_crypto_stream.h" |
22 #include "net/quic/core/quic_server_id.h" | 22 #include "net/quic/core/quic_server_id.h" |
23 #include "net/quic/core/quic_utils.h" | 23 #include "net/quic/core/quic_utils.h" |
24 #include "net/quic/platform/api/quic_clock.h" | 24 #include "net/quic/platform/api/quic_clock.h" |
25 #include "net/quic/platform/api/quic_logging.h" | 25 #include "net/quic/platform/api/quic_logging.h" |
26 #include "net/quic/platform/api/quic_socket_address.h" | 26 #include "net/quic/platform/api/quic_socket_address.h" |
27 #include "net/quic/platform/api/quic_text_utils.h" | 27 #include "net/quic/platform/api/quic_text_utils.h" |
28 #include "net/quic/test_tools/quic_connection_peer.h" | 28 #include "net/quic/test_tools/quic_connection_peer.h" |
29 #include "net/quic/test_tools/quic_framer_peer.h" | 29 #include "net/quic/test_tools/quic_framer_peer.h" |
| 30 #include "net/quic/test_tools/quic_stream_peer.h" |
30 #include "net/quic/test_tools/quic_test_utils.h" | 31 #include "net/quic/test_tools/quic_test_utils.h" |
31 #include "net/quic/test_tools/simple_quic_framer.h" | 32 #include "net/quic/test_tools/simple_quic_framer.h" |
32 #include "third_party/boringssl/src/include/openssl/bn.h" | 33 #include "third_party/boringssl/src/include/openssl/bn.h" |
33 #include "third_party/boringssl/src/include/openssl/ec.h" | 34 #include "third_party/boringssl/src/include/openssl/ec.h" |
34 #include "third_party/boringssl/src/include/openssl/ecdsa.h" | 35 #include "third_party/boringssl/src/include/openssl/ecdsa.h" |
35 #include "third_party/boringssl/src/include/openssl/evp.h" | 36 #include "third_party/boringssl/src/include/openssl/evp.h" |
36 #include "third_party/boringssl/src/include/openssl/obj_mac.h" | 37 #include "third_party/boringssl/src/include/openssl/obj_mac.h" |
37 #include "third_party/boringssl/src/include/openssl/sha.h" | 38 #include "third_party/boringssl/src/include/openssl/sha.h" |
38 | 39 |
39 using base::StringPiece; | 40 using base::StringPiece; |
40 using std::string; | 41 using std::string; |
41 | 42 |
42 namespace net { | 43 namespace net { |
43 namespace test { | 44 namespace test { |
| 45 namespace crypto_test_utils { |
44 | 46 |
45 namespace { | 47 namespace { |
46 | 48 |
47 // CryptoFramerVisitor is a framer visitor that records handshake messages. | 49 // CryptoFramerVisitor is a framer visitor that records handshake messages. |
48 class CryptoFramerVisitor : public CryptoFramerVisitorInterface { | 50 class CryptoFramerVisitor : public CryptoFramerVisitorInterface { |
49 public: | 51 public: |
50 CryptoFramerVisitor() : error_(false) {} | 52 CryptoFramerVisitor() : error_(false) {} |
51 | 53 |
52 void OnError(CryptoFramer* framer) override { error_ = true; } | 54 void OnError(CryptoFramer* framer) override { error_ = true; } |
53 | 55 |
(...skipping 25 matching lines...) Expand all Loading... |
79 } | 81 } |
80 if (c >= 'A' && c <= 'F') { | 82 if (c >= 'A' && c <= 'F') { |
81 *value = c - 'A' + 10; | 83 *value = c - 'A' + 10; |
82 return true; | 84 return true; |
83 } | 85 } |
84 return false; | 86 return false; |
85 } | 87 } |
86 | 88 |
87 // A ChannelIDSource that works in asynchronous mode unless the |callback| | 89 // A ChannelIDSource that works in asynchronous mode unless the |callback| |
88 // argument to GetChannelIDKey is nullptr. | 90 // argument to GetChannelIDKey is nullptr. |
89 class AsyncTestChannelIDSource : public ChannelIDSource, | 91 class AsyncTestChannelIDSource : public ChannelIDSource, public CallbackSource { |
90 public CryptoTestUtils::CallbackSource { | |
91 public: | 92 public: |
92 // Takes ownership of |sync_source|, a synchronous ChannelIDSource. | 93 // Takes ownership of |sync_source|, a synchronous ChannelIDSource. |
93 explicit AsyncTestChannelIDSource(ChannelIDSource* sync_source) | 94 explicit AsyncTestChannelIDSource(ChannelIDSource* sync_source) |
94 : sync_source_(sync_source) {} | 95 : sync_source_(sync_source) {} |
95 ~AsyncTestChannelIDSource() override {} | 96 ~AsyncTestChannelIDSource() override {} |
96 | 97 |
97 // ChannelIDSource implementation. | 98 // ChannelIDSource implementation. |
98 QuicAsyncStatus GetChannelIDKey(const string& hostname, | 99 QuicAsyncStatus GetChannelIDKey(const string& hostname, |
99 std::unique_ptr<ChannelIDKey>* channel_id_key, | 100 std::unique_ptr<ChannelIDKey>* channel_id_key, |
100 ChannelIDSourceCallback* callback) override { | 101 ChannelIDSourceCallback* callback) override { |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 bssl::UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new()); | 254 bssl::UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new()); |
254 // EVP_PKEY_set1_EC_KEY takes a reference so no |release| here. | 255 // EVP_PKEY_set1_EC_KEY takes a reference so no |release| here. |
255 EVP_PKEY_set1_EC_KEY(pkey.get(), ecdsa_key.get()); | 256 EVP_PKEY_set1_EC_KEY(pkey.get(), ecdsa_key.get()); |
256 | 257 |
257 return pkey.release(); | 258 return pkey.release(); |
258 } | 259 } |
259 }; | 260 }; |
260 | 261 |
261 } // anonymous namespace | 262 } // anonymous namespace |
262 | 263 |
263 CryptoTestUtils::FakeServerOptions::FakeServerOptions() {} | 264 FakeServerOptions::FakeServerOptions() {} |
264 | 265 |
265 CryptoTestUtils::FakeServerOptions::~FakeServerOptions() {} | 266 FakeServerOptions::~FakeServerOptions() {} |
266 | 267 |
267 CryptoTestUtils::FakeClientOptions::FakeClientOptions() | 268 FakeClientOptions::FakeClientOptions() |
268 : channel_id_enabled(false), channel_id_source_async(false) {} | 269 : channel_id_enabled(false), channel_id_source_async(false) {} |
269 | 270 |
270 CryptoTestUtils::FakeClientOptions::~FakeClientOptions() {} | 271 FakeClientOptions::~FakeClientOptions() {} |
271 | 272 |
272 namespace { | 273 namespace { |
273 // This class is used by GenerateFullCHLO() to extract SCID and STK from | 274 // This class is used by GenerateFullCHLO() to extract SCID and STK from |
274 // REJ/SREJ and to construct a full CHLO with these fields and given inchoate | 275 // REJ/SREJ and to construct a full CHLO with these fields and given inchoate |
275 // CHLO. | 276 // CHLO. |
276 class FullChloGenerator { | 277 class FullChloGenerator { |
277 public: | 278 public: |
278 FullChloGenerator( | 279 FullChloGenerator( |
279 QuicCryptoServerConfig* crypto_config, | 280 QuicCryptoServerConfig* crypto_config, |
280 QuicSocketAddress server_addr, | 281 QuicSocketAddress server_addr, |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 ASSERT_TRUE(rej->GetStringPiece(kSCFG, &scfg)); | 362 ASSERT_TRUE(rej->GetStringPiece(kSCFG, &scfg)); |
362 std::unique_ptr<CryptoHandshakeMessage> server_config( | 363 std::unique_ptr<CryptoHandshakeMessage> server_config( |
363 CryptoFramer::ParseMessage(scfg)); | 364 CryptoFramer::ParseMessage(scfg)); |
364 | 365 |
365 StringPiece scid; | 366 StringPiece scid; |
366 ASSERT_TRUE(server_config->GetStringPiece(kSCID, &scid)); | 367 ASSERT_TRUE(server_config->GetStringPiece(kSCID, &scid)); |
367 | 368 |
368 *out_ = result_->client_hello; | 369 *out_ = result_->client_hello; |
369 out_->SetStringPiece(kSCID, scid); | 370 out_->SetStringPiece(kSCID, scid); |
370 out_->SetStringPiece(kSourceAddressTokenTag, srct); | 371 out_->SetStringPiece(kSourceAddressTokenTag, srct); |
371 uint64_t xlct = CryptoTestUtils::LeafCertHashForTesting(); | 372 uint64_t xlct = LeafCertHashForTesting(); |
372 out_->SetValue(kXLCT, xlct); | 373 out_->SetValue(kXLCT, xlct); |
373 } | 374 } |
374 | 375 |
375 protected: | 376 protected: |
376 QuicCryptoServerConfig* crypto_config_; | 377 QuicCryptoServerConfig* crypto_config_; |
377 QuicSocketAddress server_addr_; | 378 QuicSocketAddress server_addr_; |
378 QuicSocketAddress client_addr_; | 379 QuicSocketAddress client_addr_; |
379 const QuicClock* clock_; | 380 const QuicClock* clock_; |
380 QuicReferenceCountedPointer<QuicSignedServerConfig> signed_config_; | 381 QuicReferenceCountedPointer<QuicSignedServerConfig> signed_config_; |
381 QuicCompressedCertsCache* compressed_certs_cache_; | 382 QuicCompressedCertsCache* compressed_certs_cache_; |
382 CryptoHandshakeMessage* out_; | 383 CryptoHandshakeMessage* out_; |
383 | 384 |
384 QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters> params_; | 385 QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters> params_; |
385 QuicReferenceCountedPointer<ValidateClientHelloResultCallback::Result> | 386 QuicReferenceCountedPointer<ValidateClientHelloResultCallback::Result> |
386 result_; | 387 result_; |
387 }; | 388 }; |
388 | 389 |
389 } // namespace | 390 } // namespace |
390 | 391 |
391 // static | 392 int HandshakeWithFakeServer(QuicConfig* server_quic_config, |
392 int CryptoTestUtils::HandshakeWithFakeServer( | 393 MockQuicConnectionHelper* helper, |
393 QuicConfig* server_quic_config, | 394 MockAlarmFactory* alarm_factory, |
394 MockQuicConnectionHelper* helper, | 395 PacketSavingConnection* client_conn, |
395 MockAlarmFactory* alarm_factory, | 396 QuicCryptoClientStream* client, |
396 PacketSavingConnection* client_conn, | 397 const FakeServerOptions& options) { |
397 QuicCryptoClientStream* client, | |
398 const FakeServerOptions& options) { | |
399 PacketSavingConnection* server_conn = | 398 PacketSavingConnection* server_conn = |
400 new PacketSavingConnection(helper, alarm_factory, Perspective::IS_SERVER, | 399 new PacketSavingConnection(helper, alarm_factory, Perspective::IS_SERVER, |
401 client_conn->supported_versions()); | 400 client_conn->supported_versions()); |
402 | 401 |
403 QuicCryptoServerConfig crypto_config(QuicCryptoServerConfig::TESTING, | 402 QuicCryptoServerConfig crypto_config(QuicCryptoServerConfig::TESTING, |
404 QuicRandom::GetInstance(), | 403 QuicRandom::GetInstance(), |
405 ProofSourceForTesting()); | 404 ProofSourceForTesting()); |
406 QuicCompressedCertsCache compressed_certs_cache( | 405 QuicCompressedCertsCache compressed_certs_cache( |
407 QuicCompressedCertsCache::kQuicCompressedCertsCacheSize); | 406 QuicCompressedCertsCache::kQuicCompressedCertsCacheSize); |
408 SetupCryptoServerConfigForTest(server_conn->clock(), | 407 SetupCryptoServerConfigForTest(server_conn->clock(), |
409 server_conn->random_generator(), | 408 server_conn->random_generator(), |
410 &crypto_config, options); | 409 &crypto_config, options); |
411 | 410 |
412 TestQuicSpdyServerSession server_session(server_conn, *server_quic_config, | 411 TestQuicSpdyServerSession server_session(server_conn, *server_quic_config, |
413 &crypto_config, | 412 &crypto_config, |
414 &compressed_certs_cache); | 413 &compressed_certs_cache); |
415 | 414 |
416 // The client's handshake must have been started already. | 415 // The client's handshake must have been started already. |
417 CHECK_NE(0u, client_conn->encrypted_packets_.size()); | 416 CHECK_NE(0u, client_conn->encrypted_packets_.size()); |
418 | 417 |
419 CommunicateHandshakeMessages(client_conn, client, server_conn, | 418 CommunicateHandshakeMessages(client_conn, client, server_conn, |
420 server_session.GetCryptoStream()); | 419 server_session.GetCryptoStream()); |
421 CompareClientAndServerKeys(client, server_session.GetCryptoStream()); | 420 CompareClientAndServerKeys(client, server_session.GetCryptoStream()); |
422 | 421 |
423 return client->num_sent_client_hellos(); | 422 return client->num_sent_client_hellos(); |
424 } | 423 } |
425 | 424 |
426 // static | 425 int HandshakeWithFakeClient(MockQuicConnectionHelper* helper, |
427 int CryptoTestUtils::HandshakeWithFakeClient( | 426 MockAlarmFactory* alarm_factory, |
428 MockQuicConnectionHelper* helper, | 427 PacketSavingConnection* server_conn, |
429 MockAlarmFactory* alarm_factory, | 428 QuicCryptoServerStream* server, |
430 PacketSavingConnection* server_conn, | 429 const QuicServerId& server_id, |
431 QuicCryptoServerStream* server, | 430 const FakeClientOptions& options) { |
432 const QuicServerId& server_id, | |
433 const FakeClientOptions& options) { | |
434 PacketSavingConnection* client_conn = | 431 PacketSavingConnection* client_conn = |
435 new PacketSavingConnection(helper, alarm_factory, Perspective::IS_CLIENT); | 432 new PacketSavingConnection(helper, alarm_factory, Perspective::IS_CLIENT); |
436 // Advance the time, because timers do not like uninitialized times. | 433 // Advance the time, because timers do not like uninitialized times. |
437 client_conn->AdvanceTime(QuicTime::Delta::FromSeconds(1)); | 434 client_conn->AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
438 | 435 |
439 QuicCryptoClientConfig crypto_config(ProofVerifierForTesting()); | 436 QuicCryptoClientConfig crypto_config(ProofVerifierForTesting()); |
440 AsyncTestChannelIDSource* async_channel_id_source = nullptr; | 437 AsyncTestChannelIDSource* async_channel_id_source = nullptr; |
441 if (options.channel_id_enabled) { | 438 if (options.channel_id_enabled) { |
442 ChannelIDSource* source = ChannelIDSourceForTesting(); | 439 ChannelIDSource* source = ChannelIDSourceForTesting(); |
443 if (options.channel_id_source_async) { | 440 if (options.channel_id_source_async) { |
(...skipping 30 matching lines...) Expand all Loading... |
474 server->crypto_negotiated_params().channel_id); | 471 server->crypto_negotiated_params().channel_id); |
475 EXPECT_EQ( | 472 EXPECT_EQ( |
476 options.channel_id_source_async, | 473 options.channel_id_source_async, |
477 client_session.GetCryptoStream()->WasChannelIDSourceCallbackRun()); | 474 client_session.GetCryptoStream()->WasChannelIDSourceCallbackRun()); |
478 } | 475 } |
479 } | 476 } |
480 | 477 |
481 return client_session.GetCryptoStream()->num_sent_client_hellos(); | 478 return client_session.GetCryptoStream()->num_sent_client_hellos(); |
482 } | 479 } |
483 | 480 |
484 // static | 481 void SetupCryptoServerConfigForTest(const QuicClock* clock, |
485 void CryptoTestUtils::SetupCryptoServerConfigForTest( | 482 QuicRandom* rand, |
486 const QuicClock* clock, | 483 QuicCryptoServerConfig* crypto_config, |
487 QuicRandom* rand, | 484 const FakeServerOptions& fake_options) { |
488 QuicCryptoServerConfig* crypto_config, | |
489 const FakeServerOptions& fake_options) { | |
490 QuicCryptoServerConfig::ConfigOptions options; | 485 QuicCryptoServerConfig::ConfigOptions options; |
491 options.channel_id_enabled = true; | 486 options.channel_id_enabled = true; |
492 options.token_binding_params = fake_options.token_binding_params; | 487 options.token_binding_params = fake_options.token_binding_params; |
493 std::unique_ptr<CryptoHandshakeMessage> scfg( | 488 std::unique_ptr<CryptoHandshakeMessage> scfg( |
494 crypto_config->AddDefaultConfig(rand, clock, options)); | 489 crypto_config->AddDefaultConfig(rand, clock, options)); |
495 } | 490 } |
496 | 491 |
497 // static | 492 void CommunicateHandshakeMessages(PacketSavingConnection* client_conn, |
498 void CryptoTestUtils::CommunicateHandshakeMessages( | 493 QuicCryptoStream* client, |
499 PacketSavingConnection* client_conn, | 494 PacketSavingConnection* server_conn, |
500 QuicCryptoStream* client, | 495 QuicCryptoStream* server) { |
501 PacketSavingConnection* server_conn, | |
502 QuicCryptoStream* server) { | |
503 CommunicateHandshakeMessagesAndRunCallbacks(client_conn, client, server_conn, | 496 CommunicateHandshakeMessagesAndRunCallbacks(client_conn, client, server_conn, |
504 server, nullptr); | 497 server, nullptr); |
505 } | 498 } |
506 | 499 |
507 // static | 500 void CommunicateHandshakeMessagesAndRunCallbacks( |
508 void CryptoTestUtils::CommunicateHandshakeMessagesAndRunCallbacks( | |
509 PacketSavingConnection* client_conn, | 501 PacketSavingConnection* client_conn, |
510 QuicCryptoStream* client, | 502 QuicCryptoStream* client, |
511 PacketSavingConnection* server_conn, | 503 PacketSavingConnection* server_conn, |
512 QuicCryptoStream* server, | 504 QuicCryptoStream* server, |
513 CallbackSource* callback_source) { | 505 CallbackSource* callback_source) { |
514 size_t client_i = 0, server_i = 0; | 506 size_t client_i = 0, server_i = 0; |
515 while (!client->handshake_confirmed()) { | 507 while (!client->handshake_confirmed()) { |
516 ASSERT_GT(client_conn->encrypted_packets_.size(), client_i); | 508 ASSERT_GT(client_conn->encrypted_packets_.size(), client_i); |
517 QUIC_LOG(INFO) << "Processing " | 509 QUIC_LOG(INFO) << "Processing " |
518 << client_conn->encrypted_packets_.size() - client_i | 510 << client_conn->encrypted_packets_.size() - client_i |
519 << " packets client->server"; | 511 << " packets client->server"; |
520 MovePackets(client_conn, &client_i, server, server_conn, | 512 MovePackets(client_conn, &client_i, server, server_conn, |
521 Perspective::IS_SERVER); | 513 Perspective::IS_SERVER); |
522 if (callback_source) { | 514 if (callback_source) { |
523 callback_source->RunPendingCallbacks(); | 515 callback_source->RunPendingCallbacks(); |
524 } | 516 } |
525 | 517 |
526 ASSERT_GT(server_conn->encrypted_packets_.size(), server_i); | 518 ASSERT_GT(server_conn->encrypted_packets_.size(), server_i); |
527 QUIC_LOG(INFO) << "Processing " | 519 QUIC_LOG(INFO) << "Processing " |
528 << server_conn->encrypted_packets_.size() - server_i | 520 << server_conn->encrypted_packets_.size() - server_i |
529 << " packets server->client"; | 521 << " packets server->client"; |
530 MovePackets(server_conn, &server_i, client, client_conn, | 522 MovePackets(server_conn, &server_i, client, client_conn, |
531 Perspective::IS_CLIENT); | 523 Perspective::IS_CLIENT); |
532 if (callback_source) { | 524 if (callback_source) { |
533 callback_source->RunPendingCallbacks(); | 525 callback_source->RunPendingCallbacks(); |
534 } | 526 } |
535 } | 527 } |
536 } | 528 } |
537 | 529 |
538 // static | 530 std::pair<size_t, size_t> AdvanceHandshake(PacketSavingConnection* client_conn, |
539 std::pair<size_t, size_t> CryptoTestUtils::AdvanceHandshake( | 531 QuicCryptoStream* client, |
540 PacketSavingConnection* client_conn, | 532 size_t client_i, |
541 QuicCryptoStream* client, | 533 PacketSavingConnection* server_conn, |
542 size_t client_i, | 534 QuicCryptoStream* server, |
543 PacketSavingConnection* server_conn, | 535 size_t server_i) { |
544 QuicCryptoStream* server, | |
545 size_t server_i) { | |
546 QUIC_LOG(INFO) << "Processing " | 536 QUIC_LOG(INFO) << "Processing " |
547 << client_conn->encrypted_packets_.size() - client_i | 537 << client_conn->encrypted_packets_.size() - client_i |
548 << " packets client->server"; | 538 << " packets client->server"; |
549 MovePackets(client_conn, &client_i, server, server_conn, | 539 MovePackets(client_conn, &client_i, server, server_conn, |
550 Perspective::IS_SERVER); | 540 Perspective::IS_SERVER); |
551 | 541 |
552 QUIC_LOG(INFO) << "Processing " | 542 QUIC_LOG(INFO) << "Processing " |
553 << server_conn->encrypted_packets_.size() - server_i | 543 << server_conn->encrypted_packets_.size() - server_i |
554 << " packets server->client"; | 544 << " packets server->client"; |
555 if (server_conn->encrypted_packets_.size() - server_i == 2) { | 545 if (server_conn->encrypted_packets_.size() - server_i == 2) { |
556 QUIC_LOG(INFO) << "here"; | 546 QUIC_LOG(INFO) << "here"; |
557 } | 547 } |
558 MovePackets(server_conn, &server_i, client, client_conn, | 548 MovePackets(server_conn, &server_i, client, client_conn, |
559 Perspective::IS_CLIENT); | 549 Perspective::IS_CLIENT); |
560 | 550 |
561 return std::make_pair(client_i, server_i); | 551 return std::make_pair(client_i, server_i); |
562 } | 552 } |
563 | 553 |
564 // static | 554 string GetValueForTag(const CryptoHandshakeMessage& message, QuicTag tag) { |
565 string CryptoTestUtils::GetValueForTag(const CryptoHandshakeMessage& message, | |
566 QuicTag tag) { | |
567 QuicTagValueMap::const_iterator it = message.tag_value_map().find(tag); | 555 QuicTagValueMap::const_iterator it = message.tag_value_map().find(tag); |
568 if (it == message.tag_value_map().end()) { | 556 if (it == message.tag_value_map().end()) { |
569 return string(); | 557 return string(); |
570 } | 558 } |
571 return it->second; | 559 return it->second; |
572 } | 560 } |
573 | 561 |
574 uint64_t CryptoTestUtils::LeafCertHashForTesting() { | 562 uint64_t LeafCertHashForTesting() { |
575 QuicReferenceCountedPointer<ProofSource::Chain> chain; | 563 QuicReferenceCountedPointer<ProofSource::Chain> chain; |
576 QuicSocketAddress server_address; | 564 QuicSocketAddress server_address; |
577 QuicCryptoProof proof; | 565 QuicCryptoProof proof; |
578 std::unique_ptr<ProofSource> proof_source( | 566 std::unique_ptr<ProofSource> proof_source(ProofSourceForTesting()); |
579 CryptoTestUtils::ProofSourceForTesting()); | |
580 if (!proof_source->GetProof(server_address, "", "", | 567 if (!proof_source->GetProof(server_address, "", "", |
581 AllSupportedVersions().front(), "", | 568 AllSupportedVersions().front(), "", |
582 QuicTagVector(), &chain, &proof) || | 569 QuicTagVector(), &chain, &proof) || |
583 chain->certs.empty()) { | 570 chain->certs.empty()) { |
584 DCHECK(false) << "Proof generation failed"; | 571 DCHECK(false) << "Proof generation failed"; |
585 return 0; | 572 return 0; |
586 } | 573 } |
587 | 574 |
588 return QuicUtils::FNV1a_64_Hash(chain->certs.at(0)); | 575 return QuicUtils::FNV1a_64_Hash(chain->certs.at(0)); |
589 } | 576 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
634 *out_index = index_; | 621 *out_index = index_; |
635 return true; | 622 return true; |
636 } | 623 } |
637 | 624 |
638 private: | 625 private: |
639 const string cert_; | 626 const string cert_; |
640 const uint64_t hash_; | 627 const uint64_t hash_; |
641 const uint32_t index_; | 628 const uint32_t index_; |
642 }; | 629 }; |
643 | 630 |
644 CommonCertSets* CryptoTestUtils::MockCommonCertSets(StringPiece cert, | 631 CommonCertSets* MockCommonCertSets(StringPiece cert, |
645 uint64_t hash, | 632 uint64_t hash, |
646 uint32_t index) { | 633 uint32_t index) { |
647 return new class MockCommonCertSets(cert, hash, index); | 634 return new class MockCommonCertSets(cert, hash, index); |
648 } | 635 } |
649 | 636 |
650 // static | 637 void FillInDummyReject(CryptoHandshakeMessage* rej, bool reject_is_stateless) { |
651 void CryptoTestUtils::FillInDummyReject(CryptoHandshakeMessage* rej, | |
652 bool reject_is_stateless) { | |
653 if (reject_is_stateless) { | 638 if (reject_is_stateless) { |
654 rej->set_tag(kSREJ); | 639 rej->set_tag(kSREJ); |
655 } else { | 640 } else { |
656 rej->set_tag(kREJ); | 641 rej->set_tag(kREJ); |
657 } | 642 } |
658 | 643 |
659 // Minimum SCFG that passes config validation checks. | 644 // Minimum SCFG that passes config validation checks. |
660 // clang-format off | 645 // clang-format off |
661 unsigned char scfg[] = { | 646 unsigned char scfg[] = { |
662 // SCFG | 647 // SCFG |
(...skipping 13 matching lines...) Expand all Loading... |
676 // clang-format on | 661 // clang-format on |
677 rej->SetValue(kSCFG, scfg); | 662 rej->SetValue(kSCFG, scfg); |
678 rej->SetStringPiece(kServerNonceTag, "SERVER_NONCE"); | 663 rej->SetStringPiece(kServerNonceTag, "SERVER_NONCE"); |
679 int64_t ttl = 2 * 24 * 60 * 60; | 664 int64_t ttl = 2 * 24 * 60 * 60; |
680 rej->SetValue(kSTTL, ttl); | 665 rej->SetValue(kSTTL, ttl); |
681 std::vector<QuicTag> reject_reasons; | 666 std::vector<QuicTag> reject_reasons; |
682 reject_reasons.push_back(CLIENT_NONCE_INVALID_FAILURE); | 667 reject_reasons.push_back(CLIENT_NONCE_INVALID_FAILURE); |
683 rej->SetVector(kRREJ, reject_reasons); | 668 rej->SetVector(kRREJ, reject_reasons); |
684 } | 669 } |
685 | 670 |
686 void CryptoTestUtils::CompareClientAndServerKeys( | 671 void CompareClientAndServerKeys(QuicCryptoClientStream* client, |
687 QuicCryptoClientStream* client, | 672 QuicCryptoServerStream* server) { |
688 QuicCryptoServerStream* server) { | 673 QuicFramer* client_framer = QuicConnectionPeer::GetFramer( |
689 QuicFramer* client_framer = | 674 QuicStreamPeer::session(client)->connection()); |
690 QuicConnectionPeer::GetFramer(client->session()->connection()); | 675 QuicFramer* server_framer = QuicConnectionPeer::GetFramer( |
691 QuicFramer* server_framer = | 676 QuicStreamPeer::session(server)->connection()); |
692 QuicConnectionPeer::GetFramer(server->session()->connection()); | |
693 const QuicEncrypter* client_encrypter( | 677 const QuicEncrypter* client_encrypter( |
694 QuicFramerPeer::GetEncrypter(client_framer, ENCRYPTION_INITIAL)); | 678 QuicFramerPeer::GetEncrypter(client_framer, ENCRYPTION_INITIAL)); |
695 const QuicDecrypter* client_decrypter( | 679 const QuicDecrypter* client_decrypter( |
696 client->session()->connection()->decrypter()); | 680 QuicStreamPeer::session(client)->connection()->decrypter()); |
697 const QuicEncrypter* client_forward_secure_encrypter( | 681 const QuicEncrypter* client_forward_secure_encrypter( |
698 QuicFramerPeer::GetEncrypter(client_framer, ENCRYPTION_FORWARD_SECURE)); | 682 QuicFramerPeer::GetEncrypter(client_framer, ENCRYPTION_FORWARD_SECURE)); |
699 const QuicDecrypter* client_forward_secure_decrypter( | 683 const QuicDecrypter* client_forward_secure_decrypter( |
700 client->session()->connection()->alternative_decrypter()); | 684 QuicStreamPeer::session(client)->connection()->alternative_decrypter()); |
701 const QuicEncrypter* server_encrypter( | 685 const QuicEncrypter* server_encrypter( |
702 QuicFramerPeer::GetEncrypter(server_framer, ENCRYPTION_INITIAL)); | 686 QuicFramerPeer::GetEncrypter(server_framer, ENCRYPTION_INITIAL)); |
703 const QuicDecrypter* server_decrypter( | 687 const QuicDecrypter* server_decrypter( |
704 server->session()->connection()->decrypter()); | 688 QuicStreamPeer::session(server)->connection()->decrypter()); |
705 const QuicEncrypter* server_forward_secure_encrypter( | 689 const QuicEncrypter* server_forward_secure_encrypter( |
706 QuicFramerPeer::GetEncrypter(server_framer, ENCRYPTION_FORWARD_SECURE)); | 690 QuicFramerPeer::GetEncrypter(server_framer, ENCRYPTION_FORWARD_SECURE)); |
707 const QuicDecrypter* server_forward_secure_decrypter( | 691 const QuicDecrypter* server_forward_secure_decrypter( |
708 server->session()->connection()->alternative_decrypter()); | 692 QuicStreamPeer::session(server)->connection()->alternative_decrypter()); |
709 | 693 |
710 StringPiece client_encrypter_key = client_encrypter->GetKey(); | 694 StringPiece client_encrypter_key = client_encrypter->GetKey(); |
711 StringPiece client_encrypter_iv = client_encrypter->GetNoncePrefix(); | 695 StringPiece client_encrypter_iv = client_encrypter->GetNoncePrefix(); |
712 StringPiece client_decrypter_key = client_decrypter->GetKey(); | 696 StringPiece client_decrypter_key = client_decrypter->GetKey(); |
713 StringPiece client_decrypter_iv = client_decrypter->GetNoncePrefix(); | 697 StringPiece client_decrypter_iv = client_decrypter->GetNoncePrefix(); |
714 StringPiece client_forward_secure_encrypter_key = | 698 StringPiece client_forward_secure_encrypter_key = |
715 client_forward_secure_encrypter->GetKey(); | 699 client_forward_secure_encrypter->GetKey(); |
716 StringPiece client_forward_secure_encrypter_iv = | 700 StringPiece client_forward_secure_encrypter_iv = |
717 client_forward_secure_encrypter->GetNoncePrefix(); | 701 client_forward_secure_encrypter->GetNoncePrefix(); |
718 StringPiece client_forward_secure_decrypter_key = | 702 StringPiece client_forward_secure_decrypter_key = |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
796 CompareCharArraysWithHexError( | 780 CompareCharArraysWithHexError( |
797 "sample key extraction", client_key_extraction.data(), | 781 "sample key extraction", client_key_extraction.data(), |
798 client_key_extraction.length(), server_key_extraction.data(), | 782 client_key_extraction.length(), server_key_extraction.data(), |
799 server_key_extraction.length()); | 783 server_key_extraction.length()); |
800 | 784 |
801 CompareCharArraysWithHexError("token binding key extraction", | 785 CompareCharArraysWithHexError("token binding key extraction", |
802 client_tb_ekm.data(), client_tb_ekm.length(), | 786 client_tb_ekm.data(), client_tb_ekm.length(), |
803 server_tb_ekm.data(), server_tb_ekm.length()); | 787 server_tb_ekm.data(), server_tb_ekm.length()); |
804 } | 788 } |
805 | 789 |
806 // static | 790 QuicTag ParseTag(const char* tagstr) { |
807 QuicTag CryptoTestUtils::ParseTag(const char* tagstr) { | |
808 const size_t len = strlen(tagstr); | 791 const size_t len = strlen(tagstr); |
809 CHECK_NE(0u, len); | 792 CHECK_NE(0u, len); |
810 | 793 |
811 QuicTag tag = 0; | 794 QuicTag tag = 0; |
812 | 795 |
813 if (tagstr[0] == '#') { | 796 if (tagstr[0] == '#') { |
814 CHECK_EQ(static_cast<size_t>(1 + 2 * 4), len); | 797 CHECK_EQ(static_cast<size_t>(1 + 2 * 4), len); |
815 tagstr++; | 798 tagstr++; |
816 | 799 |
817 for (size_t i = 0; i < 8; i++) { | 800 for (size_t i = 0; i < 8; i++) { |
(...skipping 11 matching lines...) Expand all Loading... |
829 for (size_t i = 0; i < 4; i++) { | 812 for (size_t i = 0; i < 4; i++) { |
830 tag >>= 8; | 813 tag >>= 8; |
831 if (i < len) { | 814 if (i < len) { |
832 tag |= static_cast<uint32_t>(tagstr[i]) << 24; | 815 tag |= static_cast<uint32_t>(tagstr[i]) << 24; |
833 } | 816 } |
834 } | 817 } |
835 | 818 |
836 return tag; | 819 return tag; |
837 } | 820 } |
838 | 821 |
839 // static | 822 CryptoHandshakeMessage Message(const char* message_tag, ...) { |
840 CryptoHandshakeMessage CryptoTestUtils::Message(const char* message_tag, ...) { | |
841 va_list ap; | 823 va_list ap; |
842 va_start(ap, message_tag); | 824 va_start(ap, message_tag); |
843 | 825 |
844 CryptoHandshakeMessage msg; | 826 CryptoHandshakeMessage msg; |
845 msg.set_tag(ParseTag(message_tag)); | 827 msg.set_tag(ParseTag(message_tag)); |
846 | 828 |
847 for (;;) { | 829 for (;;) { |
848 const char* tagstr = va_arg(ap, const char*); | 830 const char* tagstr = va_arg(ap, const char*); |
849 if (tagstr == nullptr) { | 831 if (tagstr == nullptr) { |
850 break; | 832 break; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
894 // that any padding is included. | 876 // that any padding is included. |
895 std::unique_ptr<QuicData> bytes(CryptoFramer::ConstructHandshakeMessage(msg)); | 877 std::unique_ptr<QuicData> bytes(CryptoFramer::ConstructHandshakeMessage(msg)); |
896 std::unique_ptr<CryptoHandshakeMessage> parsed( | 878 std::unique_ptr<CryptoHandshakeMessage> parsed( |
897 CryptoFramer::ParseMessage(bytes->AsStringPiece())); | 879 CryptoFramer::ParseMessage(bytes->AsStringPiece())); |
898 CHECK(parsed.get()); | 880 CHECK(parsed.get()); |
899 | 881 |
900 va_end(ap); | 882 va_end(ap); |
901 return *parsed; | 883 return *parsed; |
902 } | 884 } |
903 | 885 |
904 // static | 886 ChannelIDSource* ChannelIDSourceForTesting() { |
905 ChannelIDSource* CryptoTestUtils::ChannelIDSourceForTesting() { | |
906 return new TestChannelIDSource(); | 887 return new TestChannelIDSource(); |
907 } | 888 } |
908 | 889 |
909 // static | 890 void MovePackets(PacketSavingConnection* source_conn, |
910 void CryptoTestUtils::MovePackets(PacketSavingConnection* source_conn, | 891 size_t* inout_packet_index, |
911 size_t* inout_packet_index, | 892 QuicCryptoStream* dest_stream, |
912 QuicCryptoStream* dest_stream, | 893 PacketSavingConnection* dest_conn, |
913 PacketSavingConnection* dest_conn, | 894 Perspective dest_perspective) { |
914 Perspective dest_perspective) { | |
915 SimpleQuicFramer framer(source_conn->supported_versions(), dest_perspective); | 895 SimpleQuicFramer framer(source_conn->supported_versions(), dest_perspective); |
916 CryptoFramer crypto_framer; | 896 CryptoFramer crypto_framer; |
917 CryptoFramerVisitor crypto_visitor; | 897 CryptoFramerVisitor crypto_visitor; |
918 | 898 |
919 // In order to properly test the code we need to perform encryption and | 899 // In order to properly test the code we need to perform encryption and |
920 // decryption so that the crypters latch when expected. The crypters are in | 900 // decryption so that the crypters latch when expected. The crypters are in |
921 // |dest_conn|, but we don't want to try and use them there. Instead we swap | 901 // |dest_conn|, but we don't want to try and use them there. Instead we swap |
922 // them into |framer|, perform the decryption with them, and then swap ther | 902 // them into |framer|, perform the decryption with them, and then swap ther |
923 // back. | 903 // back. |
924 QuicConnectionPeer::SwapCrypters(dest_conn, framer.framer()); | 904 QuicConnectionPeer::SwapCrypters(dest_conn, framer.framer()); |
(...skipping 21 matching lines...) Expand all Loading... |
946 QuicConnectionPeer::SwapCrypters(dest_conn, framer.framer()); | 926 QuicConnectionPeer::SwapCrypters(dest_conn, framer.framer()); |
947 | 927 |
948 ASSERT_EQ(0u, crypto_framer.InputBytesRemaining()); | 928 ASSERT_EQ(0u, crypto_framer.InputBytesRemaining()); |
949 | 929 |
950 for (const CryptoHandshakeMessage& message : crypto_visitor.messages()) { | 930 for (const CryptoHandshakeMessage& message : crypto_visitor.messages()) { |
951 dest_stream->OnHandshakeMessage(message); | 931 dest_stream->OnHandshakeMessage(message); |
952 } | 932 } |
953 QuicConnectionPeer::SetCurrentPacket(dest_conn, StringPiece(nullptr, 0)); | 933 QuicConnectionPeer::SetCurrentPacket(dest_conn, StringPiece(nullptr, 0)); |
954 } | 934 } |
955 | 935 |
956 CryptoHandshakeMessage CryptoTestUtils::GenerateDefaultInchoateCHLO( | 936 CryptoHandshakeMessage GenerateDefaultInchoateCHLO( |
957 const QuicClock* clock, | 937 const QuicClock* clock, |
958 QuicVersion version, | 938 QuicVersion version, |
959 QuicCryptoServerConfig* crypto_config) { | 939 QuicCryptoServerConfig* crypto_config) { |
960 // clang-format off | 940 // clang-format off |
961 return CryptoTestUtils::Message( | 941 return Message( |
962 "CHLO", | 942 "CHLO", |
963 "PDMD", "X509", | 943 "PDMD", "X509", |
964 "AEAD", "AESG", | 944 "AEAD", "AESG", |
965 "KEXS", "C255", | 945 "KEXS", "C255", |
966 "PUBS", CryptoTestUtils::GenerateClientPublicValuesHex().c_str(), | 946 "PUBS", GenerateClientPublicValuesHex().c_str(), |
967 "NONC", CryptoTestUtils::GenerateClientNonceHex(clock, | 947 "NONC", GenerateClientNonceHex(clock, |
968 crypto_config).c_str(), | 948 crypto_config).c_str(), |
969 "VER\0", QuicTagToString( | 949 "VER\0", QuicTagToString( |
970 QuicVersionToQuicTag(version)).c_str(), | 950 QuicVersionToQuicTag(version)).c_str(), |
971 "$padding", static_cast<int>(kClientHelloMinimumSize), | 951 "$padding", static_cast<int>(kClientHelloMinimumSize), |
972 nullptr); | 952 nullptr); |
973 // clang-format on | 953 // clang-format on |
974 } | 954 } |
975 | 955 |
976 string CryptoTestUtils::GenerateClientNonceHex( | 956 string GenerateClientNonceHex(const QuicClock* clock, |
977 const QuicClock* clock, | 957 QuicCryptoServerConfig* crypto_config) { |
978 QuicCryptoServerConfig* crypto_config) { | |
979 net::QuicCryptoServerConfig::ConfigOptions old_config_options; | 958 net::QuicCryptoServerConfig::ConfigOptions old_config_options; |
980 net::QuicCryptoServerConfig::ConfigOptions new_config_options; | 959 net::QuicCryptoServerConfig::ConfigOptions new_config_options; |
981 old_config_options.id = "old-config-id"; | 960 old_config_options.id = "old-config-id"; |
982 delete crypto_config->AddDefaultConfig(net::QuicRandom::GetInstance(), clock, | 961 delete crypto_config->AddDefaultConfig(net::QuicRandom::GetInstance(), clock, |
983 old_config_options); | 962 old_config_options); |
984 std::unique_ptr<QuicServerConfigProtobuf> primary_config( | 963 std::unique_ptr<QuicServerConfigProtobuf> primary_config( |
985 crypto_config->GenerateConfig(net::QuicRandom::GetInstance(), clock, | 964 crypto_config->GenerateConfig(net::QuicRandom::GetInstance(), clock, |
986 new_config_options)); | 965 new_config_options)); |
987 primary_config->set_primary_time(clock->WallNow().ToUNIXSeconds()); | 966 primary_config->set_primary_time(clock->WallNow().ToUNIXSeconds()); |
988 std::unique_ptr<net::CryptoHandshakeMessage> msg( | 967 std::unique_ptr<net::CryptoHandshakeMessage> msg( |
989 crypto_config->AddConfig(std::move(primary_config), clock->WallNow())); | 968 crypto_config->AddConfig(std::move(primary_config), clock->WallNow())); |
990 StringPiece orbit; | 969 StringPiece orbit; |
991 CHECK(msg->GetStringPiece(net::kORBT, &orbit)); | 970 CHECK(msg->GetStringPiece(net::kORBT, &orbit)); |
992 string nonce; | 971 string nonce; |
993 net::CryptoUtils::GenerateNonce( | 972 net::CryptoUtils::GenerateNonce( |
994 clock->WallNow(), net::QuicRandom::GetInstance(), | 973 clock->WallNow(), net::QuicRandom::GetInstance(), |
995 StringPiece(reinterpret_cast<const char*>(orbit.data()), | 974 StringPiece(reinterpret_cast<const char*>(orbit.data()), |
996 sizeof(orbit.size())), | 975 sizeof(orbit.size())), |
997 &nonce); | 976 &nonce); |
998 return ("#" + QuicTextUtils::HexEncode(nonce)); | 977 return ("#" + QuicTextUtils::HexEncode(nonce)); |
999 } | 978 } |
1000 | 979 |
1001 string CryptoTestUtils::GenerateClientPublicValuesHex() { | 980 string GenerateClientPublicValuesHex() { |
1002 char public_value[32]; | 981 char public_value[32]; |
1003 memset(public_value, 42, sizeof(public_value)); | 982 memset(public_value, 42, sizeof(public_value)); |
1004 return ("#" + QuicTextUtils::HexEncode(public_value, sizeof(public_value))); | 983 return ("#" + QuicTextUtils::HexEncode(public_value, sizeof(public_value))); |
1005 } | 984 } |
1006 | 985 |
1007 // static | 986 void GenerateFullCHLO(const CryptoHandshakeMessage& inchoate_chlo, |
1008 void CryptoTestUtils::GenerateFullCHLO( | 987 QuicCryptoServerConfig* crypto_config, |
1009 const CryptoHandshakeMessage& inchoate_chlo, | 988 QuicSocketAddress server_addr, |
1010 QuicCryptoServerConfig* crypto_config, | 989 QuicSocketAddress client_addr, |
1011 QuicSocketAddress server_addr, | 990 QuicVersion version, |
1012 QuicSocketAddress client_addr, | 991 const QuicClock* clock, |
1013 QuicVersion version, | 992 QuicReferenceCountedPointer<QuicSignedServerConfig> proof, |
1014 const QuicClock* clock, | 993 QuicCompressedCertsCache* compressed_certs_cache, |
1015 QuicReferenceCountedPointer<QuicSignedServerConfig> proof, | 994 CryptoHandshakeMessage* out) { |
1016 QuicCompressedCertsCache* compressed_certs_cache, | |
1017 CryptoHandshakeMessage* out) { | |
1018 // Pass a inchoate CHLO. | 995 // Pass a inchoate CHLO. |
1019 FullChloGenerator generator(crypto_config, server_addr, client_addr, clock, | 996 FullChloGenerator generator(crypto_config, server_addr, client_addr, clock, |
1020 proof, compressed_certs_cache, out); | 997 proof, compressed_certs_cache, out); |
1021 crypto_config->ValidateClientHello( | 998 crypto_config->ValidateClientHello( |
1022 inchoate_chlo, client_addr.host(), server_addr, version, clock, proof, | 999 inchoate_chlo, client_addr.host(), server_addr, version, clock, proof, |
1023 generator.GetValidateClientHelloCallback()); | 1000 generator.GetValidateClientHelloCallback()); |
1024 } | 1001 } |
1025 | 1002 |
| 1003 } // namespace crypto_test_utils |
1026 } // namespace test | 1004 } // namespace test |
1027 } // namespace net | 1005 } // namespace net |
OLD | NEW |