QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2016 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/core/chlo_extractor.h" |
| 6 | |
| 7 | #include <memory> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 8 | #include <string> |
bnc | 463f235 | 2019-10-10 04:49:34 -0700 | [diff] [blame] | 9 | #include <utility> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 10 | |
| 11 | #include "net/third_party/quiche/src/quic/core/quic_framer.h" |
| 12 | #include "net/third_party/quiche/src/quic/core/quic_utils.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 13 | #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
| 14 | #include "net/third_party/quiche/src/quic/test_tools/crypto_test_utils.h" |
dschinazi | 9c869b9 | 2020-04-22 06:10:23 -0700 | [diff] [blame] | 15 | #include "net/third_party/quiche/src/quic/test_tools/first_flight.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 16 | #include "net/third_party/quiche/src/quic/test_tools/quic_test_utils.h" |
bnc | 4e9283d | 2019-12-17 07:08:57 -0800 | [diff] [blame] | 17 | #include "net/third_party/quiche/src/common/platform/api/quiche_arraysize.h" |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 18 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 19 | |
| 20 | namespace quic { |
| 21 | namespace test { |
| 22 | namespace { |
| 23 | |
| 24 | class TestDelegate : public ChloExtractor::Delegate { |
| 25 | public: |
| 26 | TestDelegate() = default; |
| 27 | ~TestDelegate() override = default; |
| 28 | |
| 29 | // ChloExtractor::Delegate implementation |
| 30 | void OnChlo(QuicTransportVersion version, |
| 31 | QuicConnectionId connection_id, |
| 32 | const CryptoHandshakeMessage& chlo) override { |
| 33 | version_ = version; |
| 34 | connection_id_ = connection_id; |
| 35 | chlo_ = chlo.DebugString(); |
dschinazi | 9c869b9 | 2020-04-22 06:10:23 -0700 | [diff] [blame] | 36 | quiche::QuicheStringPiece alpn_value; |
| 37 | if (chlo.GetStringPiece(kALPN, &alpn_value)) { |
| 38 | alpn_ = std::string(alpn_value); |
| 39 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | QuicConnectionId connection_id() const { return connection_id_; } |
| 43 | QuicTransportVersion transport_version() const { return version_; } |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 44 | const std::string& chlo() const { return chlo_; } |
dschinazi | 9c869b9 | 2020-04-22 06:10:23 -0700 | [diff] [blame] | 45 | const std::string& alpn() const { return alpn_; } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 46 | |
| 47 | private: |
| 48 | QuicConnectionId connection_id_; |
| 49 | QuicTransportVersion version_; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 50 | std::string chlo_; |
dschinazi | 9c869b9 | 2020-04-22 06:10:23 -0700 | [diff] [blame] | 51 | std::string alpn_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 52 | }; |
| 53 | |
dschinazi | 9c869b9 | 2020-04-22 06:10:23 -0700 | [diff] [blame] | 54 | class ChloExtractorTest : public QuicTestWithParam<ParsedQuicVersion> { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 55 | public: |
dschinazi | 9c869b9 | 2020-04-22 06:10:23 -0700 | [diff] [blame] | 56 | ChloExtractorTest() : version_(GetParam()) {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 57 | |
dschinazi | 9c869b9 | 2020-04-22 06:10:23 -0700 | [diff] [blame] | 58 | void MakePacket(quiche::QuicheStringPiece data, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 59 | bool munge_offset, |
| 60 | bool munge_stream_id) { |
dschinazi | 9c869b9 | 2020-04-22 06:10:23 -0700 | [diff] [blame] | 61 | QuicPacketHeader header; |
| 62 | header.destination_connection_id = TestConnectionId(); |
| 63 | header.destination_connection_id_included = CONNECTION_ID_PRESENT; |
| 64 | header.version_flag = true; |
| 65 | header.version = version_; |
| 66 | header.reset_flag = false; |
| 67 | header.packet_number_length = PACKET_4BYTE_PACKET_NUMBER; |
| 68 | header.packet_number = QuicPacketNumber(1); |
| 69 | if (version_.HasLongHeaderLengths()) { |
| 70 | header.retry_token_length_length = VARIABLE_LENGTH_INTEGER_LENGTH_1; |
| 71 | header.length_length = VARIABLE_LENGTH_INTEGER_LENGTH_2; |
| 72 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 73 | QuicFrames frames; |
| 74 | size_t offset = 0; |
| 75 | if (munge_offset) { |
| 76 | offset++; |
| 77 | } |
dschinazi | 9c869b9 | 2020-04-22 06:10:23 -0700 | [diff] [blame] | 78 | QuicFramer framer(SupportedVersions(version_), QuicTime::Zero(), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 79 | Perspective::IS_CLIENT, kQuicDefaultConnectionIdLength); |
nharper | 4a5a76c | 2019-09-13 13:44:37 -0700 | [diff] [blame] | 80 | framer.SetInitialObfuscators(TestConnectionId()); |
dschinazi | 9c869b9 | 2020-04-22 06:10:23 -0700 | [diff] [blame] | 81 | if (!version_.UsesCryptoFrames() || munge_stream_id) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 82 | QuicStreamId stream_id = |
dschinazi | 9c869b9 | 2020-04-22 06:10:23 -0700 | [diff] [blame] | 83 | QuicUtils::GetCryptoStreamId(version_.transport_version); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 84 | if (munge_stream_id) { |
| 85 | stream_id++; |
| 86 | } |
| 87 | frames.push_back( |
| 88 | QuicFrame(QuicStreamFrame(stream_id, false, offset, data))); |
| 89 | } else { |
| 90 | frames.push_back( |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 91 | QuicFrame(new QuicCryptoFrame(ENCRYPTION_INITIAL, offset, data))); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 92 | } |
| 93 | std::unique_ptr<QuicPacket> packet( |
dschinazi | 9c869b9 | 2020-04-22 06:10:23 -0700 | [diff] [blame] | 94 | BuildUnsizedDataPacket(&framer, header, frames)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 95 | EXPECT_TRUE(packet != nullptr); |
| 96 | size_t encrypted_length = |
dschinazi | 9c869b9 | 2020-04-22 06:10:23 -0700 | [diff] [blame] | 97 | framer.EncryptPayload(ENCRYPTION_INITIAL, header.packet_number, *packet, |
| 98 | buffer_, QUICHE_ARRAYSIZE(buffer_)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 99 | ASSERT_NE(0u, encrypted_length); |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 100 | packet_ = std::make_unique<QuicEncryptedPacket>(buffer_, encrypted_length); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 101 | EXPECT_TRUE(packet_ != nullptr); |
| 102 | DeleteFrames(&frames); |
| 103 | } |
| 104 | |
| 105 | protected: |
dschinazi | 9c869b9 | 2020-04-22 06:10:23 -0700 | [diff] [blame] | 106 | ParsedQuicVersion version_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 107 | TestDelegate delegate_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 108 | std::unique_ptr<QuicEncryptedPacket> packet_; |
dschinazi | 66dea07 | 2019-04-09 11:41:06 -0700 | [diff] [blame] | 109 | char buffer_[kMaxOutgoingPacketSize]; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 110 | }; |
| 111 | |
dschinazi | 9c869b9 | 2020-04-22 06:10:23 -0700 | [diff] [blame] | 112 | INSTANTIATE_TEST_SUITE_P( |
| 113 | ChloExtractorTests, |
| 114 | ChloExtractorTest, |
| 115 | ::testing::ValuesIn(AllSupportedVersionsWithQuicCrypto()), |
| 116 | ::testing::PrintToStringParamName()); |
| 117 | |
| 118 | TEST_P(ChloExtractorTest, FindsValidChlo) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 119 | CryptoHandshakeMessage client_hello; |
| 120 | client_hello.set_tag(kCHLO); |
| 121 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 122 | std::string client_hello_str(client_hello.GetSerialized().AsStringPiece()); |
dschinazi | 9c869b9 | 2020-04-22 06:10:23 -0700 | [diff] [blame] | 123 | |
| 124 | MakePacket(client_hello_str, /*munge_offset=*/false, |
| 125 | /*munge_stream_id=*/false); |
| 126 | EXPECT_TRUE(ChloExtractor::Extract(*packet_, version_, {}, &delegate_, |
| 127 | kQuicDefaultConnectionIdLength)); |
| 128 | EXPECT_EQ(version_.transport_version, delegate_.transport_version()); |
| 129 | EXPECT_EQ(TestConnectionId(), delegate_.connection_id()); |
| 130 | EXPECT_EQ(client_hello.DebugString(), delegate_.chlo()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 131 | } |
| 132 | |
dschinazi | 9c869b9 | 2020-04-22 06:10:23 -0700 | [diff] [blame] | 133 | TEST_P(ChloExtractorTest, DoesNotFindValidChloOnWrongStream) { |
| 134 | if (version_.UsesCryptoFrames()) { |
| 135 | // When crypto frames are in use we do not use stream frames. |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 136 | return; |
| 137 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 138 | CryptoHandshakeMessage client_hello; |
| 139 | client_hello.set_tag(kCHLO); |
| 140 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 141 | std::string client_hello_str(client_hello.GetSerialized().AsStringPiece()); |
dschinazi | 9c869b9 | 2020-04-22 06:10:23 -0700 | [diff] [blame] | 142 | MakePacket(client_hello_str, |
| 143 | /*munge_offset=*/false, /*munge_stream_id=*/true); |
| 144 | EXPECT_FALSE(ChloExtractor::Extract(*packet_, version_, {}, &delegate_, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 145 | kQuicDefaultConnectionIdLength)); |
| 146 | } |
| 147 | |
dschinazi | 9c869b9 | 2020-04-22 06:10:23 -0700 | [diff] [blame] | 148 | TEST_P(ChloExtractorTest, DoesNotFindValidChloOnWrongOffset) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 149 | CryptoHandshakeMessage client_hello; |
| 150 | client_hello.set_tag(kCHLO); |
| 151 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 152 | std::string client_hello_str(client_hello.GetSerialized().AsStringPiece()); |
dschinazi | 9c869b9 | 2020-04-22 06:10:23 -0700 | [diff] [blame] | 153 | MakePacket(client_hello_str, /*munge_offset=*/true, |
| 154 | /*munge_stream_id=*/false); |
| 155 | EXPECT_FALSE(ChloExtractor::Extract(*packet_, version_, {}, &delegate_, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 156 | kQuicDefaultConnectionIdLength)); |
| 157 | } |
| 158 | |
dschinazi | 9c869b9 | 2020-04-22 06:10:23 -0700 | [diff] [blame] | 159 | TEST_P(ChloExtractorTest, DoesNotFindInvalidChlo) { |
| 160 | MakePacket("foo", /*munge_offset=*/false, |
| 161 | /*munge_stream_id=*/false); |
| 162 | EXPECT_FALSE(ChloExtractor::Extract(*packet_, version_, {}, &delegate_, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 163 | kQuicDefaultConnectionIdLength)); |
| 164 | } |
| 165 | |
dschinazi | 9c869b9 | 2020-04-22 06:10:23 -0700 | [diff] [blame] | 166 | TEST_P(ChloExtractorTest, FirstFlight) { |
| 167 | std::vector<std::unique_ptr<QuicReceivedPacket>> packets = |
| 168 | GetFirstFlightOfPackets(version_); |
| 169 | ASSERT_EQ(packets.size(), 1u); |
| 170 | EXPECT_TRUE(ChloExtractor::Extract(*packets[0], version_, {}, &delegate_, |
| 171 | kQuicDefaultConnectionIdLength)); |
| 172 | EXPECT_EQ(version_.transport_version, delegate_.transport_version()); |
| 173 | EXPECT_EQ(TestConnectionId(), delegate_.connection_id()); |
| 174 | EXPECT_EQ(AlpnForVersion(version_), delegate_.alpn()); |
| 175 | } |
| 176 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 177 | } // namespace |
| 178 | } // namespace test |
| 179 | } // namespace quic |