| 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 <openssl/bn.h> | 7 #include <openssl/bn.h> |
| 8 #include <openssl/ec.h> | 8 #include <openssl/ec.h> |
| 9 #include <openssl/ecdsa.h> | 9 #include <openssl/ecdsa.h> |
| 10 #include <openssl/evp.h> | 10 #include <openssl/evp.h> |
| 11 #include <openssl/obj_mac.h> | 11 #include <openssl/obj_mac.h> |
| 12 #include <openssl/sha.h> | 12 #include <openssl/sha.h> |
| 13 | 13 |
| 14 #include <memory> | 14 #include <memory> |
| 15 | 15 |
| 16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 17 #include "crypto/openssl_util.h" | 17 #include "crypto/openssl_util.h" |
| 18 #include "crypto/scoped_openssl_types.h" | 18 #include "crypto/scoped_openssl_types.h" |
| 19 #include "crypto/secure_hash.h" | 19 #include "crypto/secure_hash.h" |
| 20 #include "net/quic/core/crypto/channel_id.h" | 20 #include "net/quic/core/crypto/channel_id.h" |
| 21 #include "net/quic/core/crypto/common_cert_set.h" | 21 #include "net/quic/core/crypto/common_cert_set.h" |
| 22 #include "net/quic/core/crypto/crypto_handshake.h" | 22 #include "net/quic/core/crypto/crypto_handshake.h" |
| 23 #include "net/quic/core/crypto/crypto_server_config_protobuf.h" |
| 23 #include "net/quic/core/crypto/quic_crypto_server_config.h" | 24 #include "net/quic/core/crypto/quic_crypto_server_config.h" |
| 24 #include "net/quic/core/crypto/quic_decrypter.h" | 25 #include "net/quic/core/crypto/quic_decrypter.h" |
| 25 #include "net/quic/core/crypto/quic_encrypter.h" | 26 #include "net/quic/core/crypto/quic_encrypter.h" |
| 26 #include "net/quic/core/crypto/quic_random.h" | 27 #include "net/quic/core/crypto/quic_random.h" |
| 27 #include "net/quic/core/quic_clock.h" | 28 #include "net/quic/core/quic_clock.h" |
| 28 #include "net/quic/core/quic_crypto_client_stream.h" | 29 #include "net/quic/core/quic_crypto_client_stream.h" |
| 29 #include "net/quic/core/quic_crypto_server_stream.h" | 30 #include "net/quic/core/quic_crypto_server_stream.h" |
| 30 #include "net/quic/core/quic_crypto_stream.h" | 31 #include "net/quic/core/quic_crypto_stream.h" |
| 31 #include "net/quic/core/quic_server_id.h" | 32 #include "net/quic/core/quic_server_id.h" |
| 32 #include "net/quic/core/quic_utils.h" | 33 #include "net/quic/core/quic_utils.h" |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 | 293 |
| 293 void RunImpl(const CryptoHandshakeMessage& client_hello, | 294 void RunImpl(const CryptoHandshakeMessage& client_hello, |
| 294 const ValidateClientHelloResultCallback::Result& result, | 295 const ValidateClientHelloResultCallback::Result& result, |
| 295 std::unique_ptr<ProofSource::Details> /* details */) override { | 296 std::unique_ptr<ProofSource::Details> /* details */) override { |
| 296 QuicCryptoNegotiatedParameters params; | 297 QuicCryptoNegotiatedParameters params; |
| 297 string error_details; | 298 string error_details; |
| 298 DiversificationNonce diversification_nonce; | 299 DiversificationNonce diversification_nonce; |
| 299 CryptoHandshakeMessage rej; | 300 CryptoHandshakeMessage rej; |
| 300 crypto_config_->ProcessClientHello( | 301 crypto_config_->ProcessClientHello( |
| 301 result, /*reject_only=*/false, /*connection_id=*/1, server_ip_, | 302 result, /*reject_only=*/false, /*connection_id=*/1, server_ip_, |
| 302 client_addr_, QuicSupportedVersions().front(), QuicSupportedVersions(), | 303 client_addr_, AllSupportedVersions().front(), AllSupportedVersions(), |
| 303 /*use_stateless_rejects=*/true, /*server_designated_connection_id=*/0, | 304 /*use_stateless_rejects=*/true, /*server_designated_connection_id=*/0, |
| 304 clock_, QuicRandom::GetInstance(), compressed_certs_cache_, ¶ms, | 305 clock_, QuicRandom::GetInstance(), compressed_certs_cache_, ¶ms, |
| 305 proof_, &rej, &diversification_nonce, &error_details); | 306 proof_, /*total_framing_overhead=*/50, kDefaultMaxPacketSize, &rej, |
| 307 &diversification_nonce, &error_details); |
| 306 // Verify output is a REJ or SREJ. | 308 // Verify output is a REJ or SREJ. |
| 307 EXPECT_THAT(rej.tag(), | 309 EXPECT_THAT(rej.tag(), |
| 308 testing::AnyOf(testing::Eq(kSREJ), testing::Eq(kREJ))); | 310 testing::AnyOf(testing::Eq(kSREJ), testing::Eq(kREJ))); |
| 309 | 311 |
| 310 VLOG(1) << "Extract valid STK and SCID from\n" << rej.DebugString(); | 312 VLOG(1) << "Extract valid STK and SCID from\n" << rej.DebugString(); |
| 311 StringPiece srct; | 313 StringPiece srct; |
| 312 ASSERT_TRUE(rej.GetStringPiece(kSourceAddressTokenTag, &srct)); | 314 ASSERT_TRUE(rej.GetStringPiece(kSourceAddressTokenTag, &srct)); |
| 313 | 315 |
| 314 StringPiece scfg; | 316 StringPiece scfg; |
| 315 ASSERT_TRUE(rej.GetStringPiece(kSCFG, &scfg)); | 317 ASSERT_TRUE(rej.GetStringPiece(kSCFG, &scfg)); |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 516 return it->second; | 518 return it->second; |
| 517 } | 519 } |
| 518 | 520 |
| 519 uint64_t CryptoTestUtils::LeafCertHashForTesting() { | 521 uint64_t CryptoTestUtils::LeafCertHashForTesting() { |
| 520 scoped_refptr<ProofSource::Chain> chain; | 522 scoped_refptr<ProofSource::Chain> chain; |
| 521 IPAddress server_ip; | 523 IPAddress server_ip; |
| 522 string sig; | 524 string sig; |
| 523 string cert_sct; | 525 string cert_sct; |
| 524 std::unique_ptr<ProofSource> proof_source( | 526 std::unique_ptr<ProofSource> proof_source( |
| 525 CryptoTestUtils::ProofSourceForTesting()); | 527 CryptoTestUtils::ProofSourceForTesting()); |
| 526 if (!proof_source->GetProof(server_ip, "", "", | 528 if (!proof_source->GetProof(server_ip, "", "", AllSupportedVersions().front(), |
| 527 QuicSupportedVersions().front(), "", &chain, &sig, | 529 "", &chain, &sig, &cert_sct) || |
| 528 &cert_sct) || | |
| 529 chain->certs.empty()) { | 530 chain->certs.empty()) { |
| 530 DCHECK(false) << "Proof generation failed"; | 531 DCHECK(false) << "Proof generation failed"; |
| 531 return 0; | 532 return 0; |
| 532 } | 533 } |
| 533 | 534 |
| 534 return QuicUtils::FNV1a_64_Hash(chain->certs.at(0).c_str(), | 535 return QuicUtils::FNV1a_64_Hash(chain->certs.at(0).c_str(), |
| 535 chain->certs.at(0).length()); | 536 chain->certs.at(0).length()); |
| 536 } | 537 } |
| 537 | 538 |
| 538 class MockCommonCertSets : public CommonCertSets { | 539 class MockCommonCertSets : public CommonCertSets { |
| (...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 876 // The framer will be unable to decrypt forward-secure packets sent after | 877 // The framer will be unable to decrypt forward-secure packets sent after |
| 877 // the handshake is complete. Don't treat them as handshake packets. | 878 // the handshake is complete. Don't treat them as handshake packets. |
| 878 break; | 879 break; |
| 879 } | 880 } |
| 880 | 881 |
| 881 for (const QuicStreamFrame* stream_frame : framer.stream_frames()) { | 882 for (const QuicStreamFrame* stream_frame : framer.stream_frames()) { |
| 882 ASSERT_TRUE(crypto_framer.ProcessInput( | 883 ASSERT_TRUE(crypto_framer.ProcessInput( |
| 883 StringPiece(stream_frame->data_buffer, stream_frame->data_length))); | 884 StringPiece(stream_frame->data_buffer, stream_frame->data_length))); |
| 884 ASSERT_FALSE(crypto_visitor.error()); | 885 ASSERT_FALSE(crypto_visitor.error()); |
| 885 } | 886 } |
| 887 QuicConnectionPeer::SetCurrentPacket( |
| 888 dest_conn, source_conn->encrypted_packets_[index]->AsStringPiece()); |
| 886 } | 889 } |
| 887 *inout_packet_index = index; | 890 *inout_packet_index = index; |
| 888 | 891 |
| 889 QuicConnectionPeer::SwapCrypters(dest_conn, framer.framer()); | 892 QuicConnectionPeer::SwapCrypters(dest_conn, framer.framer()); |
| 890 | 893 |
| 891 ASSERT_EQ(0u, crypto_framer.InputBytesRemaining()); | 894 ASSERT_EQ(0u, crypto_framer.InputBytesRemaining()); |
| 892 | 895 |
| 893 for (const CryptoHandshakeMessage& message : crypto_visitor.messages()) { | 896 for (const CryptoHandshakeMessage& message : crypto_visitor.messages()) { |
| 894 dest_stream->OnHandshakeMessage(message); | 897 dest_stream->OnHandshakeMessage(message); |
| 895 } | 898 } |
| 899 QuicConnectionPeer::SetCurrentPacket(dest_conn, StringPiece(nullptr, 0)); |
| 900 } |
| 901 |
| 902 CryptoHandshakeMessage CryptoTestUtils::GenerateDefaultInchoateCHLO( |
| 903 const QuicClock* clock, |
| 904 QuicVersion version, |
| 905 QuicCryptoServerConfig* crypto_config) { |
| 906 // clang-format off |
| 907 return CryptoTestUtils::Message( |
| 908 "CHLO", |
| 909 "PDMD", "X509", |
| 910 "AEAD", "AESG", |
| 911 "KEXS", "C255", |
| 912 "PUBS", CryptoTestUtils::GenerateClientPublicValuesHex().c_str(), |
| 913 "NONC", CryptoTestUtils::GenerateClientNonceHex(clock, |
| 914 crypto_config).c_str(), |
| 915 "VER\0", QuicUtils::TagToString( |
| 916 QuicVersionToQuicTag(version)).c_str(), |
| 917 "$padding", static_cast<int>(kClientHelloMinimumSize), |
| 918 nullptr); |
| 919 // clang-format on |
| 920 } |
| 921 |
| 922 string CryptoTestUtils::GenerateClientNonceHex( |
| 923 const QuicClock* clock, |
| 924 QuicCryptoServerConfig* crypto_config) { |
| 925 net::QuicCryptoServerConfig::ConfigOptions old_config_options; |
| 926 net::QuicCryptoServerConfig::ConfigOptions new_config_options; |
| 927 old_config_options.id = "old-config-id"; |
| 928 delete crypto_config->AddDefaultConfig(net::QuicRandom::GetInstance(), clock, |
| 929 old_config_options); |
| 930 std::unique_ptr<QuicServerConfigProtobuf> primary_config( |
| 931 crypto_config->GenerateConfig(net::QuicRandom::GetInstance(), clock, |
| 932 new_config_options)); |
| 933 primary_config->set_primary_time(clock->WallNow().ToUNIXSeconds()); |
| 934 std::unique_ptr<net::CryptoHandshakeMessage> msg( |
| 935 crypto_config->AddConfig(primary_config.get(), clock->WallNow())); |
| 936 StringPiece orbit; |
| 937 CHECK(msg->GetStringPiece(net::kORBT, &orbit)); |
| 938 string nonce; |
| 939 net::CryptoUtils::GenerateNonce( |
| 940 clock->WallNow(), net::QuicRandom::GetInstance(), |
| 941 StringPiece(reinterpret_cast<const char*>(orbit.data()), |
| 942 sizeof(orbit.size())), |
| 943 &nonce); |
| 944 return ("#" + net::QuicUtils::HexEncode(nonce)); |
| 945 } |
| 946 |
| 947 string CryptoTestUtils::GenerateClientPublicValuesHex() { |
| 948 char public_value[32]; |
| 949 memset(public_value, 42, sizeof(public_value)); |
| 950 return ("#" + net::QuicUtils::HexEncode(public_value, sizeof(public_value))); |
| 896 } | 951 } |
| 897 | 952 |
| 898 // static | 953 // static |
| 899 void CryptoTestUtils::GenerateFullCHLO( | 954 void CryptoTestUtils::GenerateFullCHLO( |
| 900 const CryptoHandshakeMessage& inchoate_chlo, | 955 const CryptoHandshakeMessage& inchoate_chlo, |
| 901 QuicCryptoServerConfig* crypto_config, | 956 QuicCryptoServerConfig* crypto_config, |
| 902 IPAddress server_ip, | 957 IPAddress server_ip, |
| 903 IPEndPoint client_addr, | 958 IPEndPoint client_addr, |
| 904 QuicVersion version, | 959 QuicVersion version, |
| 905 const QuicClock* clock, | 960 const QuicClock* clock, |
| 906 QuicCryptoProof* proof, | 961 QuicCryptoProof* proof, |
| 907 QuicCompressedCertsCache* compressed_certs_cache, | 962 QuicCompressedCertsCache* compressed_certs_cache, |
| 908 CryptoHandshakeMessage* out) { | 963 CryptoHandshakeMessage* out) { |
| 909 // Pass a inchoate CHLO. | 964 // Pass a inchoate CHLO. |
| 910 crypto_config->ValidateClientHello( | 965 crypto_config->ValidateClientHello( |
| 911 inchoate_chlo, client_addr.address(), server_ip, version, clock, proof, | 966 inchoate_chlo, client_addr.address(), server_ip, version, clock, proof, |
| 912 new FullChloGenerator(crypto_config, server_ip, client_addr, clock, proof, | 967 new FullChloGenerator(crypto_config, server_ip, client_addr, clock, proof, |
| 913 compressed_certs_cache, out)); | 968 compressed_certs_cache, out)); |
| 914 } | 969 } |
| 915 | 970 |
| 916 } // namespace test | 971 } // namespace test |
| 917 } // namespace net | 972 } // namespace net |
| OLD | NEW |