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/core/quic_framer.h" | 5 #include "net/quic/core/quic_framer.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cstdint> | 8 #include <cstdint> |
9 #include <map> | 9 #include <map> |
10 #include <memory> | 10 #include <memory> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "net/quic/core/crypto/null_decrypter.h" | 14 #include "net/quic/core/crypto/null_decrypter.h" |
15 #include "net/quic/core/crypto/null_encrypter.h" | 15 #include "net/quic/core/crypto/null_encrypter.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/quic_flags.h" | 18 #include "net/quic/core/quic_flags.h" |
19 #include "net/quic/core/quic_packets.h" | 19 #include "net/quic/core/quic_packets.h" |
20 #include "net/quic/core/quic_utils.h" | 20 #include "net/quic/core/quic_utils.h" |
21 #include "net/quic/platform/api/quic_logging.h" | 21 #include "net/quic/platform/api/quic_logging.h" |
22 #include "net/quic/platform/api/quic_ptr_util.h" | 22 #include "net/quic/platform/api/quic_ptr_util.h" |
23 #include "net/quic/test_tools/quic_framer_peer.h" | 23 #include "net/quic/test_tools/quic_framer_peer.h" |
24 #include "net/quic/test_tools/quic_test_utils.h" | 24 #include "net/quic/test_tools/quic_test_utils.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
26 | 26 |
27 using base::StringPiece; | |
28 using std::string; | 27 using std::string; |
29 using testing::Return; | 28 using testing::Return; |
30 using testing::Truly; | 29 using testing::Truly; |
31 using testing::_; | 30 using testing::_; |
32 | 31 |
33 namespace net { | 32 namespace net { |
34 namespace test { | 33 namespace test { |
35 | 34 |
36 const QuicPacketNumber kEpoch = UINT64_C(1) << 48; | 35 const QuicPacketNumber kEpoch = UINT64_C(1) << 48; |
37 const QuicPacketNumber kMask = kEpoch - 1; | 36 const QuicPacketNumber kMask = kEpoch - 1; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 } | 93 } |
95 | 94 |
96 // Index into the message tag of the public reset packet. | 95 // Index into the message tag of the public reset packet. |
97 // Public resets always have full connection_ids. | 96 // Public resets always have full connection_ids. |
98 const size_t kPublicResetPacketMessageTagOffset = | 97 const size_t kPublicResetPacketMessageTagOffset = |
99 kConnectionIdOffset + PACKET_8BYTE_CONNECTION_ID; | 98 kConnectionIdOffset + PACKET_8BYTE_CONNECTION_ID; |
100 | 99 |
101 class TestEncrypter : public QuicEncrypter { | 100 class TestEncrypter : public QuicEncrypter { |
102 public: | 101 public: |
103 ~TestEncrypter() override {} | 102 ~TestEncrypter() override {} |
104 bool SetKey(StringPiece key) override { return true; } | 103 bool SetKey(QuicStringPiece key) override { return true; } |
105 bool SetNoncePrefix(StringPiece nonce_prefix) override { return true; } | 104 bool SetNoncePrefix(QuicStringPiece nonce_prefix) override { return true; } |
106 bool EncryptPacket(QuicVersion version, | 105 bool EncryptPacket(QuicVersion version, |
107 QuicPacketNumber packet_number, | 106 QuicPacketNumber packet_number, |
108 StringPiece associated_data, | 107 QuicStringPiece associated_data, |
109 StringPiece plaintext, | 108 QuicStringPiece plaintext, |
110 char* output, | 109 char* output, |
111 size_t* output_length, | 110 size_t* output_length, |
112 size_t max_output_length) override { | 111 size_t max_output_length) override { |
113 version_ = version; | 112 version_ = version; |
114 packet_number_ = packet_number; | 113 packet_number_ = packet_number; |
115 associated_data_ = associated_data.as_string(); | 114 associated_data_ = associated_data.as_string(); |
116 plaintext_ = plaintext.as_string(); | 115 plaintext_ = plaintext.as_string(); |
117 memcpy(output, plaintext.data(), plaintext.length()); | 116 memcpy(output, plaintext.data(), plaintext.length()); |
118 *output_length = plaintext.length(); | 117 *output_length = plaintext.length(); |
119 return true; | 118 return true; |
120 } | 119 } |
121 size_t GetKeySize() const override { return 0; } | 120 size_t GetKeySize() const override { return 0; } |
122 size_t GetNoncePrefixSize() const override { return 0; } | 121 size_t GetNoncePrefixSize() const override { return 0; } |
123 size_t GetMaxPlaintextSize(size_t ciphertext_size) const override { | 122 size_t GetMaxPlaintextSize(size_t ciphertext_size) const override { |
124 return ciphertext_size; | 123 return ciphertext_size; |
125 } | 124 } |
126 size_t GetCiphertextSize(size_t plaintext_size) const override { | 125 size_t GetCiphertextSize(size_t plaintext_size) const override { |
127 return plaintext_size; | 126 return plaintext_size; |
128 } | 127 } |
129 StringPiece GetKey() const override { return StringPiece(); } | 128 QuicStringPiece GetKey() const override { return QuicStringPiece(); } |
130 StringPiece GetNoncePrefix() const override { return StringPiece(); } | 129 QuicStringPiece GetNoncePrefix() const override { return QuicStringPiece(); } |
131 | 130 |
132 QuicVersion version_; | 131 QuicVersion version_; |
133 Perspective perspective_; | 132 Perspective perspective_; |
134 QuicPacketNumber packet_number_; | 133 QuicPacketNumber packet_number_; |
135 string associated_data_; | 134 string associated_data_; |
136 string plaintext_; | 135 string plaintext_; |
137 }; | 136 }; |
138 | 137 |
139 class TestDecrypter : public QuicDecrypter { | 138 class TestDecrypter : public QuicDecrypter { |
140 public: | 139 public: |
141 ~TestDecrypter() override {} | 140 ~TestDecrypter() override {} |
142 bool SetKey(StringPiece key) override { return true; } | 141 bool SetKey(QuicStringPiece key) override { return true; } |
143 bool SetNoncePrefix(StringPiece nonce_prefix) override { return true; } | 142 bool SetNoncePrefix(QuicStringPiece nonce_prefix) override { return true; } |
144 bool SetPreliminaryKey(StringPiece key) override { | 143 bool SetPreliminaryKey(QuicStringPiece key) override { |
145 QUIC_BUG << "should not be called"; | 144 QUIC_BUG << "should not be called"; |
146 return false; | 145 return false; |
147 } | 146 } |
148 bool SetDiversificationNonce(const DiversificationNonce& key) override { | 147 bool SetDiversificationNonce(const DiversificationNonce& key) override { |
149 return true; | 148 return true; |
150 } | 149 } |
151 bool DecryptPacket(QuicVersion version, | 150 bool DecryptPacket(QuicVersion version, |
152 QuicPacketNumber packet_number, | 151 QuicPacketNumber packet_number, |
153 StringPiece associated_data, | 152 QuicStringPiece associated_data, |
154 StringPiece ciphertext, | 153 QuicStringPiece ciphertext, |
155 char* output, | 154 char* output, |
156 size_t* output_length, | 155 size_t* output_length, |
157 size_t max_output_length) override { | 156 size_t max_output_length) override { |
158 version_ = version; | 157 version_ = version; |
159 packet_number_ = packet_number; | 158 packet_number_ = packet_number; |
160 associated_data_ = associated_data.as_string(); | 159 associated_data_ = associated_data.as_string(); |
161 ciphertext_ = ciphertext.as_string(); | 160 ciphertext_ = ciphertext.as_string(); |
162 memcpy(output, ciphertext.data(), ciphertext.length()); | 161 memcpy(output, ciphertext.data(), ciphertext.length()); |
163 *output_length = ciphertext.length(); | 162 *output_length = ciphertext.length(); |
164 return true; | 163 return true; |
165 } | 164 } |
166 StringPiece GetKey() const override { return StringPiece(); } | 165 QuicStringPiece GetKey() const override { return QuicStringPiece(); } |
167 StringPiece GetNoncePrefix() const override { return StringPiece(); } | 166 QuicStringPiece GetNoncePrefix() const override { return QuicStringPiece(); } |
168 const char* cipher_name() const override { return "Test"; } | 167 const char* cipher_name() const override { return "Test"; } |
169 // Use a distinct value starting with 0xFFFFFF, which is never used by TLS. | 168 // Use a distinct value starting with 0xFFFFFF, which is never used by TLS. |
170 uint32_t cipher_id() const override { return 0xFFFFFFF2; } | 169 uint32_t cipher_id() const override { return 0xFFFFFFF2; } |
171 QuicVersion version_; | 170 QuicVersion version_; |
172 Perspective perspective_; | 171 Perspective perspective_; |
173 QuicPacketNumber packet_number_; | 172 QuicPacketNumber packet_number_; |
174 string associated_data_; | 173 string associated_data_; |
175 string ciphertext_; | 174 string ciphertext_; |
176 }; | 175 }; |
177 | 176 |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 PACKET_6BYTE_PACKET_NUMBER) != decrypter_->associated_data_) { | 386 PACKET_6BYTE_PACKET_NUMBER) != decrypter_->associated_data_) { |
388 QUIC_LOG(ERROR) << "Decrypted incorrect associated data. expected " | 387 QUIC_LOG(ERROR) << "Decrypted incorrect associated data. expected " |
389 << QuicFramer::GetAssociatedDataFromEncryptedPacket( | 388 << QuicFramer::GetAssociatedDataFromEncryptedPacket( |
390 framer_.version(), encrypted, | 389 framer_.version(), encrypted, |
391 PACKET_8BYTE_CONNECTION_ID, includes_version, | 390 PACKET_8BYTE_CONNECTION_ID, includes_version, |
392 includes_diversification_nonce, | 391 includes_diversification_nonce, |
393 PACKET_6BYTE_PACKET_NUMBER) | 392 PACKET_6BYTE_PACKET_NUMBER) |
394 << " actual: " << decrypter_->associated_data_; | 393 << " actual: " << decrypter_->associated_data_; |
395 return false; | 394 return false; |
396 } | 395 } |
397 StringPiece ciphertext( | 396 QuicStringPiece ciphertext( |
398 encrypted.AsStringPiece().substr(GetStartOfEncryptedData( | 397 encrypted.AsStringPiece().substr(GetStartOfEncryptedData( |
399 framer_.version(), PACKET_8BYTE_CONNECTION_ID, includes_version, | 398 framer_.version(), PACKET_8BYTE_CONNECTION_ID, includes_version, |
400 includes_diversification_nonce, PACKET_6BYTE_PACKET_NUMBER))); | 399 includes_diversification_nonce, PACKET_6BYTE_PACKET_NUMBER))); |
401 if (ciphertext != decrypter_->ciphertext_) { | 400 if (ciphertext != decrypter_->ciphertext_) { |
402 QUIC_LOG(ERROR) << "Decrypted incorrect ciphertext data. expected " | 401 QUIC_LOG(ERROR) << "Decrypted incorrect ciphertext data. expected " |
403 << ciphertext << " actual: " << decrypter_->ciphertext_; | 402 << ciphertext << " actual: " << decrypter_->ciphertext_; |
404 return false; | 403 return false; |
405 } | 404 } |
406 return true; | 405 return true; |
407 } | 406 } |
(...skipping 2014 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2422 } | 2421 } |
2423 | 2422 |
2424 TEST_P(QuicFramerTest, BuildStreamFramePacket) { | 2423 TEST_P(QuicFramerTest, BuildStreamFramePacket) { |
2425 QuicPacketHeader header; | 2424 QuicPacketHeader header; |
2426 header.public_header.connection_id = kConnectionId; | 2425 header.public_header.connection_id = kConnectionId; |
2427 header.public_header.reset_flag = false; | 2426 header.public_header.reset_flag = false; |
2428 header.public_header.version_flag = false; | 2427 header.public_header.version_flag = false; |
2429 header.packet_number = kPacketNumber; | 2428 header.packet_number = kPacketNumber; |
2430 | 2429 |
2431 QuicStreamFrame stream_frame(kStreamId, true, kStreamOffset, | 2430 QuicStreamFrame stream_frame(kStreamId, true, kStreamOffset, |
2432 StringPiece("hello world!")); | 2431 QuicStringPiece("hello world!")); |
2433 | 2432 |
2434 QuicFrames frames = {QuicFrame(&stream_frame)}; | 2433 QuicFrames frames = {QuicFrame(&stream_frame)}; |
2435 | 2434 |
2436 // clang-format off | 2435 // clang-format off |
2437 unsigned char packet[] = { | 2436 unsigned char packet[] = { |
2438 // public flags (8 byte connection_id) | 2437 // public flags (8 byte connection_id) |
2439 0x38, | 2438 0x38, |
2440 // connection_id | 2439 // connection_id |
2441 0x10, 0x32, 0x54, 0x76, | 2440 0x10, 0x32, 0x54, 0x76, |
2442 0x98, 0xBA, 0xDC, 0xFE, | 2441 0x98, 0xBA, 0xDC, 0xFE, |
(...skipping 24 matching lines...) Expand all Loading... |
2467 } | 2466 } |
2468 | 2467 |
2469 TEST_P(QuicFramerTest, BuildStreamFramePacketWithVersionFlag) { | 2468 TEST_P(QuicFramerTest, BuildStreamFramePacketWithVersionFlag) { |
2470 QuicPacketHeader header; | 2469 QuicPacketHeader header; |
2471 header.public_header.connection_id = kConnectionId; | 2470 header.public_header.connection_id = kConnectionId; |
2472 header.public_header.reset_flag = false; | 2471 header.public_header.reset_flag = false; |
2473 header.public_header.version_flag = true; | 2472 header.public_header.version_flag = true; |
2474 header.packet_number = kPacketNumber; | 2473 header.packet_number = kPacketNumber; |
2475 | 2474 |
2476 QuicStreamFrame stream_frame(kStreamId, true, kStreamOffset, | 2475 QuicStreamFrame stream_frame(kStreamId, true, kStreamOffset, |
2477 StringPiece("hello world!")); | 2476 QuicStringPiece("hello world!")); |
2478 QuicFrames frames = {QuicFrame(&stream_frame)}; | 2477 QuicFrames frames = {QuicFrame(&stream_frame)}; |
2479 | 2478 |
2480 // clang-format off | 2479 // clang-format off |
2481 unsigned char packet[] = { | 2480 unsigned char packet[] = { |
2482 // public flags (version, 8 byte connection_id) | 2481 // public flags (version, 8 byte connection_id) |
2483 static_cast<unsigned char>( | 2482 static_cast<unsigned char>( |
2484 FLAGS_quic_reloadable_flag_quic_remove_v33_hacks2 ? 0x39 : 0x3D), | 2483 FLAGS_quic_reloadable_flag_quic_remove_v33_hacks2 ? 0x39 : 0x3D), |
2485 // connection_id | 2484 // connection_id |
2486 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE, | 2485 0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE, |
2487 // version tag | 2486 // version tag |
(...skipping 1147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3635 extern "C" { | 3634 extern "C" { |
3636 #endif | 3635 #endif |
3637 | 3636 |
3638 // target function to be fuzzed by Dr. Fuzz | 3637 // target function to be fuzzed by Dr. Fuzz |
3639 void QuicFramerFuzzFunc(unsigned char* data, size_t size) { | 3638 void QuicFramerFuzzFunc(unsigned char* data, size_t size) { |
3640 QuicFramer framer(AllSupportedVersions(), QuicTime::Zero(), | 3639 QuicFramer framer(AllSupportedVersions(), QuicTime::Zero(), |
3641 Perspective::IS_SERVER); | 3640 Perspective::IS_SERVER); |
3642 const char* const packet_bytes = reinterpret_cast<const char*>(data); | 3641 const char* const packet_bytes = reinterpret_cast<const char*>(data); |
3643 | 3642 |
3644 // Test the CryptoFramer. | 3643 // Test the CryptoFramer. |
3645 StringPiece crypto_input(packet_bytes, size); | 3644 QuicStringPiece crypto_input(packet_bytes, size); |
3646 std::unique_ptr<CryptoHandshakeMessage> handshake_message( | 3645 std::unique_ptr<CryptoHandshakeMessage> handshake_message( |
3647 CryptoFramer::ParseMessage(crypto_input)); | 3646 CryptoFramer::ParseMessage(crypto_input)); |
3648 | 3647 |
3649 // Test the regular QuicFramer with the same input. | 3648 // Test the regular QuicFramer with the same input. |
3650 NoOpFramerVisitor visitor; | 3649 NoOpFramerVisitor visitor; |
3651 framer.set_visitor(&visitor); | 3650 framer.set_visitor(&visitor); |
3652 QuicEncryptedPacket packet(packet_bytes, size); | 3651 QuicEncryptedPacket packet(packet_bytes, size); |
3653 framer.ProcessPacket(packet); | 3652 framer.ProcessPacket(packet); |
3654 } | 3653 } |
3655 | 3654 |
(...skipping 29 matching lines...) Expand all Loading... |
3685 'o', ' ', 'w', 'o', | 3684 'o', ' ', 'w', 'o', |
3686 'r', 'l', 'd', '!', | 3685 'r', 'l', 'd', '!', |
3687 }; | 3686 }; |
3688 // clang-format on | 3687 // clang-format on |
3689 | 3688 |
3690 QuicFramerFuzzFunc(packet, arraysize(packet)); | 3689 QuicFramerFuzzFunc(packet, arraysize(packet)); |
3691 } | 3690 } |
3692 | 3691 |
3693 } // namespace test | 3692 } // namespace test |
3694 } // namespace net | 3693 } // namespace net |
OLD | NEW |