blob: 0430f8daf1d495357c852e20cf97f82c3f905aec [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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 "net/third_party/quiche/src/quic/core/crypto/crypto_framer.h"
nharper55fa6132019-05-07 19:37:21 -07008#include "net/third_party/quiche/src/quic/core/crypto/crypto_handshake.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -05009#include "net/third_party/quiche/src/quic/core/crypto/crypto_handshake_message.h"
10#include "net/third_party/quiche/src/quic/core/crypto/crypto_protocol.h"
nharper55fa6132019-05-07 19:37:21 -070011#include "net/third_party/quiche/src/quic/core/crypto/crypto_utils.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050012#include "net/third_party/quiche/src/quic/core/crypto/quic_decrypter.h"
13#include "net/third_party/quiche/src/quic/core/crypto/quic_encrypter.h"
14#include "net/third_party/quiche/src/quic/core/quic_framer.h"
15#include "net/third_party/quiche/src/quic/core/quic_utils.h"
16#include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h"
17#include "net/third_party/quiche/src/quic/platform/api/quic_text_utils.h"
18
19namespace quic {
20
21namespace {
22
23class ChloFramerVisitor : public QuicFramerVisitorInterface,
24 public CryptoFramerVisitorInterface {
25 public:
26 ChloFramerVisitor(QuicFramer* framer,
27 const QuicTagVector& create_session_tag_indicators,
28 ChloExtractor::Delegate* delegate);
29
30 ~ChloFramerVisitor() override = default;
31
32 // QuicFramerVisitorInterface implementation
dschinazi17d42422019-06-18 16:35:07 -070033 void OnError(QuicFramer* /*framer*/) override {}
fayang8aba1ff2019-06-21 12:00:54 -070034 bool OnProtocolVersionMismatch(ParsedQuicVersion version) override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050035 void OnPacket() override {}
dschinazi17d42422019-06-18 16:35:07 -070036 void OnPublicResetPacket(const QuicPublicResetPacket& /*packet*/) override {}
QUICHE teama6ef0a62019-03-07 20:34:33 -050037 void OnVersionNegotiationPacket(
dschinazi17d42422019-06-18 16:35:07 -070038 const QuicVersionNegotiationPacket& /*packet*/) override {}
39 void OnRetryPacket(QuicConnectionId /*original_connection_id*/,
40 QuicConnectionId /*new_connection_id*/,
41 QuicStringPiece /*retry_token*/) override {}
QUICHE teama6ef0a62019-03-07 20:34:33 -050042 bool OnUnauthenticatedPublicHeader(const QuicPacketHeader& header) override;
43 bool OnUnauthenticatedHeader(const QuicPacketHeader& header) override;
dschinazi17d42422019-06-18 16:35:07 -070044 void OnDecryptedPacket(EncryptionLevel /*level*/) override {}
QUICHE teama6ef0a62019-03-07 20:34:33 -050045 bool OnPacketHeader(const QuicPacketHeader& header) override;
46 void OnCoalescedPacket(const QuicEncryptedPacket& packet) override;
dschinazi4b5a68a2019-08-15 15:45:36 -070047 void OnUndecryptablePacket(const QuicEncryptedPacket& packet,
48 EncryptionLevel decryption_level,
49 bool has_decryption_key) override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050050 bool OnStreamFrame(const QuicStreamFrame& frame) override;
51 bool OnCryptoFrame(const QuicCryptoFrame& frame) override;
52 bool OnAckFrameStart(QuicPacketNumber largest_acked,
53 QuicTime::Delta ack_delay_time) override;
54 bool OnAckRange(QuicPacketNumber start, QuicPacketNumber end) override;
55 bool OnAckTimestamp(QuicPacketNumber packet_number,
56 QuicTime timestamp) override;
57 bool OnAckFrameEnd(QuicPacketNumber start) override;
58 bool OnStopWaitingFrame(const QuicStopWaitingFrame& frame) override;
59 bool OnPingFrame(const QuicPingFrame& frame) override;
60 bool OnRstStreamFrame(const QuicRstStreamFrame& frame) override;
61 bool OnConnectionCloseFrame(const QuicConnectionCloseFrame& frame) override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050062 bool OnNewConnectionIdFrame(const QuicNewConnectionIdFrame& frame) override;
63 bool OnRetireConnectionIdFrame(
64 const QuicRetireConnectionIdFrame& frame) override;
65 bool OnNewTokenFrame(const QuicNewTokenFrame& frame) override;
66 bool OnStopSendingFrame(const QuicStopSendingFrame& frame) override;
67 bool OnPathChallengeFrame(const QuicPathChallengeFrame& frame) override;
68 bool OnPathResponseFrame(const QuicPathResponseFrame& frame) override;
69 bool OnGoAwayFrame(const QuicGoAwayFrame& frame) override;
fkastenholz3c4eabf2019-04-22 07:49:59 -070070 bool OnMaxStreamsFrame(const QuicMaxStreamsFrame& frame) override;
71 bool OnStreamsBlockedFrame(const QuicStreamsBlockedFrame& frame) override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050072 bool OnWindowUpdateFrame(const QuicWindowUpdateFrame& frame) override;
73 bool OnBlockedFrame(const QuicBlockedFrame& frame) override;
74 bool OnPaddingFrame(const QuicPaddingFrame& frame) override;
75 bool OnMessageFrame(const QuicMessageFrame& frame) override;
76 void OnPacketComplete() override {}
77 bool IsValidStatelessResetToken(QuicUint128 token) const override;
78 void OnAuthenticatedIetfStatelessResetPacket(
dschinazi17d42422019-06-18 16:35:07 -070079 const QuicIetfStatelessResetPacket& /*packet*/) override {}
QUICHE teama6ef0a62019-03-07 20:34:33 -050080
81 // CryptoFramerVisitorInterface implementation.
82 void OnError(CryptoFramer* framer) override;
83 void OnHandshakeMessage(const CryptoHandshakeMessage& message) override;
84
85 // Shared implementation between OnStreamFrame and OnCryptoFrame.
86 bool OnHandshakeData(QuicStringPiece data);
87
88 bool found_chlo() { return found_chlo_; }
89 bool chlo_contains_tags() { return chlo_contains_tags_; }
90
91 private:
92 QuicFramer* framer_;
93 const QuicTagVector& create_session_tag_indicators_;
94 ChloExtractor::Delegate* delegate_;
95 bool found_chlo_;
96 bool chlo_contains_tags_;
97 QuicConnectionId connection_id_;
98};
99
100ChloFramerVisitor::ChloFramerVisitor(
101 QuicFramer* framer,
102 const QuicTagVector& create_session_tag_indicators,
103 ChloExtractor::Delegate* delegate)
104 : framer_(framer),
105 create_session_tag_indicators_(create_session_tag_indicators),
106 delegate_(delegate),
107 found_chlo_(false),
108 chlo_contains_tags_(false),
109 connection_id_(EmptyQuicConnectionId()) {}
110
fayang8aba1ff2019-06-21 12:00:54 -0700111bool ChloFramerVisitor::OnProtocolVersionMismatch(ParsedQuicVersion version) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500112 if (!framer_->IsSupportedVersion(version)) {
113 return false;
114 }
115 framer_->set_version(version);
116 return true;
117}
118
119bool ChloFramerVisitor::OnUnauthenticatedPublicHeader(
120 const QuicPacketHeader& header) {
121 connection_id_ = header.destination_connection_id;
nharper55fa6132019-05-07 19:37:21 -0700122 // QuicFramer creates a NullEncrypter and NullDecrypter at level
nharper4a5a76c2019-09-13 13:44:37 -0700123 // ENCRYPTION_INITIAL. While those are the correct ones to use with some
124 // versions of QUIC, others use the IETF-style initial crypters, so those need
125 // to be created and installed.
126 framer_->SetInitialObfuscators(header.destination_connection_id);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500127 return true;
128}
129bool ChloFramerVisitor::OnUnauthenticatedHeader(
dschinazi17d42422019-06-18 16:35:07 -0700130 const QuicPacketHeader& /*header*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500131 return true;
132}
dschinazi17d42422019-06-18 16:35:07 -0700133bool ChloFramerVisitor::OnPacketHeader(const QuicPacketHeader& /*header*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500134 return true;
135}
dschinazi4b5a68a2019-08-15 15:45:36 -0700136
dschinazi17d42422019-06-18 16:35:07 -0700137void ChloFramerVisitor::OnCoalescedPacket(
138 const QuicEncryptedPacket& /*packet*/) {}
dschinazi4b5a68a2019-08-15 15:45:36 -0700139
140void ChloFramerVisitor::OnUndecryptablePacket(
141 const QuicEncryptedPacket& /*packet*/,
142 EncryptionLevel /*decryption_level*/,
143 bool /*has_decryption_key*/) {}
144
QUICHE teama6ef0a62019-03-07 20:34:33 -0500145bool ChloFramerVisitor::OnStreamFrame(const QuicStreamFrame& frame) {
QUICHE teamea740082019-03-11 17:58:43 -0700146 if (QuicVersionUsesCryptoFrames(framer_->transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500147 // CHLO will be sent in CRYPTO frames in v47 and above.
148 return false;
149 }
150 QuicStringPiece data(frame.data_buffer, frame.data_length);
nharper46833c32019-05-15 21:33:05 -0700151 if (QuicUtils::IsCryptoStreamId(framer_->transport_version(),
152 frame.stream_id) &&
QUICHE teama6ef0a62019-03-07 20:34:33 -0500153 frame.offset == 0 && QuicTextUtils::StartsWith(data, "CHLO")) {
154 return OnHandshakeData(data);
155 }
156 return true;
157}
158
159bool ChloFramerVisitor::OnCryptoFrame(const QuicCryptoFrame& frame) {
QUICHE teamea740082019-03-11 17:58:43 -0700160 if (!QuicVersionUsesCryptoFrames(framer_->transport_version())) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500161 // CHLO will be in stream frames before v47.
162 return false;
163 }
164 QuicStringPiece data(frame.data_buffer, frame.data_length);
165 if (frame.offset == 0 && QuicTextUtils::StartsWith(data, "CHLO")) {
166 return OnHandshakeData(data);
167 }
168 return true;
169}
170
171bool ChloFramerVisitor::OnHandshakeData(QuicStringPiece data) {
172 CryptoFramer crypto_framer;
173 crypto_framer.set_visitor(this);
174 if (!crypto_framer.ProcessInput(data)) {
175 return false;
176 }
177 // Interrogate the crypto framer and see if there are any
178 // intersecting tags between what we saw in the maybe-CHLO and the
179 // indicator set.
180 for (const QuicTag tag : create_session_tag_indicators_) {
181 if (crypto_framer.HasTag(tag)) {
182 chlo_contains_tags_ = true;
183 }
184 }
185 if (chlo_contains_tags_ && delegate_) {
186 // Unfortunately, because this is a partial CHLO,
187 // OnHandshakeMessage was never called, so the ALPN was never
188 // extracted. Fake it up a bit and send it to the delegate so that
189 // the correct dispatch can happen.
190 crypto_framer.ForceHandshake();
191 }
192
193 return true;
194}
195
196bool ChloFramerVisitor::OnAckFrameStart(QuicPacketNumber /*largest_acked*/,
197 QuicTime::Delta /*ack_delay_time*/) {
198 return true;
199}
200
201bool ChloFramerVisitor::OnAckRange(QuicPacketNumber /*start*/,
202 QuicPacketNumber /*end*/) {
203 return true;
204}
205
206bool ChloFramerVisitor::OnAckTimestamp(QuicPacketNumber /*packet_number*/,
207 QuicTime /*timestamp*/) {
208 return true;
209}
210
211bool ChloFramerVisitor::OnAckFrameEnd(QuicPacketNumber /*start*/) {
212 return true;
213}
214
dschinazi17d42422019-06-18 16:35:07 -0700215bool ChloFramerVisitor::OnStopWaitingFrame(
216 const QuicStopWaitingFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500217 return true;
218}
219
dschinazi17d42422019-06-18 16:35:07 -0700220bool ChloFramerVisitor::OnPingFrame(const QuicPingFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500221 return true;
222}
223
dschinazi17d42422019-06-18 16:35:07 -0700224bool ChloFramerVisitor::OnRstStreamFrame(const QuicRstStreamFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500225 return true;
226}
227
228bool ChloFramerVisitor::OnConnectionCloseFrame(
dschinazi17d42422019-06-18 16:35:07 -0700229 const QuicConnectionCloseFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500230 return true;
231}
232
dschinazi17d42422019-06-18 16:35:07 -0700233bool ChloFramerVisitor::OnStopSendingFrame(
234 const QuicStopSendingFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500235 return true;
236}
237
238bool ChloFramerVisitor::OnPathChallengeFrame(
dschinazi17d42422019-06-18 16:35:07 -0700239 const QuicPathChallengeFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500240 return true;
241}
242
243bool ChloFramerVisitor::OnPathResponseFrame(
dschinazi17d42422019-06-18 16:35:07 -0700244 const QuicPathResponseFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500245 return true;
246}
247
dschinazi17d42422019-06-18 16:35:07 -0700248bool ChloFramerVisitor::OnGoAwayFrame(const QuicGoAwayFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500249 return true;
250}
251
252bool ChloFramerVisitor::OnWindowUpdateFrame(
dschinazi17d42422019-06-18 16:35:07 -0700253 const QuicWindowUpdateFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500254 return true;
255}
256
dschinazi17d42422019-06-18 16:35:07 -0700257bool ChloFramerVisitor::OnBlockedFrame(const QuicBlockedFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500258 return true;
259}
260
261bool ChloFramerVisitor::OnNewConnectionIdFrame(
dschinazi17d42422019-06-18 16:35:07 -0700262 const QuicNewConnectionIdFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500263 return true;
264}
265
266bool ChloFramerVisitor::OnRetireConnectionIdFrame(
dschinazi17d42422019-06-18 16:35:07 -0700267 const QuicRetireConnectionIdFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500268 return true;
269}
270
dschinazi17d42422019-06-18 16:35:07 -0700271bool ChloFramerVisitor::OnNewTokenFrame(const QuicNewTokenFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500272 return true;
273}
274
dschinazi17d42422019-06-18 16:35:07 -0700275bool ChloFramerVisitor::OnPaddingFrame(const QuicPaddingFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500276 return true;
277}
278
dschinazi17d42422019-06-18 16:35:07 -0700279bool ChloFramerVisitor::OnMessageFrame(const QuicMessageFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500280 return true;
281}
282
dschinazi17d42422019-06-18 16:35:07 -0700283bool ChloFramerVisitor::IsValidStatelessResetToken(
284 QuicUint128 /*token*/) const {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500285 return false;
286}
287
dschinazi17d42422019-06-18 16:35:07 -0700288bool ChloFramerVisitor::OnMaxStreamsFrame(
289 const QuicMaxStreamsFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500290 return true;
291}
292
fkastenholz3c4eabf2019-04-22 07:49:59 -0700293bool ChloFramerVisitor::OnStreamsBlockedFrame(
dschinazi17d42422019-06-18 16:35:07 -0700294 const QuicStreamsBlockedFrame& /*frame*/) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500295 return true;
296}
297
dschinazi17d42422019-06-18 16:35:07 -0700298void ChloFramerVisitor::OnError(CryptoFramer* /*framer*/) {}
QUICHE teama6ef0a62019-03-07 20:34:33 -0500299
300void ChloFramerVisitor::OnHandshakeMessage(
301 const CryptoHandshakeMessage& message) {
302 if (delegate_ != nullptr) {
303 delegate_->OnChlo(framer_->transport_version(), connection_id_, message);
304 }
305 found_chlo_ = true;
306}
307
308} // namespace
309
310// static
311bool ChloExtractor::Extract(const QuicEncryptedPacket& packet,
dschinazi4fd8cb12019-09-09 16:31:06 -0700312 ParsedQuicVersion version,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500313 const QuicTagVector& create_session_tag_indicators,
314 Delegate* delegate,
315 uint8_t connection_id_length) {
dschinazi4fd8cb12019-09-09 16:31:06 -0700316 QUIC_DVLOG(1) << "Extracting CHLO using version " << version;
317 QuicFramer framer({version}, QuicTime::Zero(), Perspective::IS_SERVER,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500318 connection_id_length);
319 ChloFramerVisitor visitor(&framer, create_session_tag_indicators, delegate);
320 framer.set_visitor(&visitor);
321 if (!framer.ProcessPacket(packet)) {
322 return false;
323 }
324 return visitor.found_chlo() || visitor.chlo_contains_tags();
325}
326
327} // namespace quic