blob: 15f5a3d112dd1c48d40ac89b9690f218e1ee1558 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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
3// found in the LICENSE file.
4
5#ifndef QUICHE_QUIC_CORE_QUIC_CRYPTO_STREAM_H_
6#define QUICHE_QUIC_CORE_QUIC_CRYPTO_STREAM_H_
7
bnc5e469412019-12-05 14:16:25 -08008#include <array>
QUICHE teama6ef0a62019-03-07 20:34:33 -05009#include <cstddef>
vasilvv872e7a32019-03-12 16:42:44 -070010#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -050011
QUICHE teama6ef0a62019-03-07 20:34:33 -050012#include "net/third_party/quiche/src/quic/core/crypto/crypto_framer.h"
13#include "net/third_party/quiche/src/quic/core/crypto/crypto_utils.h"
14#include "net/third_party/quiche/src/quic/core/quic_config.h"
15#include "net/third_party/quiche/src/quic/core/quic_packets.h"
16#include "net/third_party/quiche/src/quic/core/quic_stream.h"
17#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
dmcardlecf0bfcf2019-12-13 08:08:21 -080018#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050019
20namespace quic {
21
22class QuicSession;
23
24// Crypto handshake messages in QUIC take place over a reserved stream with the
25// id 1. Each endpoint (client and server) will allocate an instance of a
26// subclass of QuicCryptoStream to send and receive handshake messages. (In the
27// normal 1-RTT handshake, the client will send a client hello, CHLO, message.
28// The server will receive this message and respond with a server hello message,
29// SHLO. At this point both sides will have established a crypto context they
30// can use to send encrypted messages.
31//
32// For more details:
33// https://docs.google.com/document/d/1g5nIXAIkN_Y-7XJW5K45IblHd_L2f5LTaDUDwvZ5L6g/edit?usp=sharing
34class QUIC_EXPORT_PRIVATE QuicCryptoStream : public QuicStream {
35 public:
36 explicit QuicCryptoStream(QuicSession* session);
37 QuicCryptoStream(const QuicCryptoStream&) = delete;
38 QuicCryptoStream& operator=(const QuicCryptoStream&) = delete;
39
40 ~QuicCryptoStream() override;
41
42 // Returns the per-packet framing overhead associated with sending a
43 // handshake message for |version|.
44 static QuicByteCount CryptoMessageFramingOverhead(
45 QuicTransportVersion version,
46 QuicConnectionId connection_id);
47
48 // QuicStream implementation
49 void OnStreamFrame(const QuicStreamFrame& frame) override;
50 void OnDataAvailable() override;
51
52 // Called when a CRYPTO frame is received.
53 void OnCryptoFrame(const QuicCryptoFrame& frame);
54
55 // Called when a CRYPTO frame is ACKed.
56 bool OnCryptoFrameAcked(const QuicCryptoFrame& frame,
57 QuicTime::Delta ack_delay_time);
58
59 // Performs key extraction to derive a new secret of |result_len| bytes
60 // dependent on |label|, |context|, and the stream's negotiated subkey secret.
61 // Returns false if the handshake has not been confirmed or the parameters are
62 // invalid (e.g. |label| contains null bytes); returns true on success.
dmcardlecf0bfcf2019-12-13 08:08:21 -080063 bool ExportKeyingMaterial(quiche::QuicheStringPiece label,
64 quiche::QuicheStringPiece context,
QUICHE teama6ef0a62019-03-07 20:34:33 -050065 size_t result_len,
vasilvvc48c8712019-03-11 13:38:16 -070066 std::string* result) const;
QUICHE teama6ef0a62019-03-07 20:34:33 -050067
68 // Writes |data| to the QuicStream at level |level|.
dmcardlecf0bfcf2019-12-13 08:08:21 -080069 virtual void WriteCryptoData(EncryptionLevel level,
70 quiche::QuicheStringPiece data);
QUICHE teama6ef0a62019-03-07 20:34:33 -050071
72 // Returns true once an encrypter has been set for the connection.
73 virtual bool encryption_established() const = 0;
74
75 // Returns true once the crypto handshake has completed.
fayang685367a2020-01-14 10:40:15 -080076 virtual bool one_rtt_keys_available() const = 0;
QUICHE teama6ef0a62019-03-07 20:34:33 -050077
78 // Returns the parameters negotiated in the crypto handshake.
79 virtual const QuicCryptoNegotiatedParameters& crypto_negotiated_params()
80 const = 0;
81
82 // Provides the message parser to use when data is received on this stream.
83 virtual CryptoMessageParser* crypto_message_parser() = 0;
84
fayangd58736d2019-11-27 13:35:31 -080085 // Called when a packet of encryption |level| has been successfully decrypted.
86 virtual void OnPacketDecrypted(EncryptionLevel level) = 0;
87
nharper486a8a92019-08-28 16:25:10 -070088 // Returns the maximum number of bytes that can be buffered at a particular
89 // encryption level |level|.
90 virtual size_t BufferSizeLimitForLevel(EncryptionLevel level) const;
91
QUICHE teama6ef0a62019-03-07 20:34:33 -050092 // Called when the underlying QuicConnection has agreed upon a QUIC version to
93 // use.
94 virtual void OnSuccessfulVersionNegotiation(const ParsedQuicVersion& version);
95
96 // Called to cancel retransmission of unencrypted crypto stream data.
97 void NeuterUnencryptedStreamData();
98
99 // Override to record the encryption level of consumed data.
100 void OnStreamDataConsumed(size_t bytes_consumed) override;
101
102 // Returns whether there are any bytes pending retransmission in CRYPTO
103 // frames.
nharper46833c32019-05-15 21:33:05 -0700104 virtual bool HasPendingCryptoRetransmission() const;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500105
106 // Writes any pending CRYPTO frame retransmissions.
107 void WritePendingCryptoRetransmission();
108
109 // Override to retransmit lost crypto data with the appropriate encryption
110 // level.
111 void WritePendingRetransmission() override;
112
113 // Override to send unacked crypto data with the appropriate encryption level.
114 bool RetransmitStreamData(QuicStreamOffset offset,
115 QuicByteCount data_length,
116 bool fin) override;
117
118 // Returns the number of bytes of handshake data that have been received from
119 // the peer in either CRYPTO or STREAM frames.
120 uint64_t crypto_bytes_read() const;
121
122 // Returns the number of bytes of handshake data that have been received from
123 // the peer in CRYPTO frames at a particular encryption level.
124 QuicByteCount BytesReadOnLevel(EncryptionLevel level) const;
125
126 // Writes |data_length| of data of a crypto frame to |writer|. The data
127 // written is from the send buffer for encryption level |level| and starts at
128 // |offset|.
129 bool WriteCryptoFrame(EncryptionLevel level,
130 QuicStreamOffset offset,
131 QuicByteCount data_length,
132 QuicDataWriter* writer);
133
134 // Called when data from a CRYPTO frame is considered lost. The lost data is
135 // identified by the encryption level, offset, and length in |crypto_frame|.
136 void OnCryptoFrameLost(QuicCryptoFrame* crypto_frame);
137
138 // Called to retransmit any outstanding data in the range indicated by the
139 // encryption level, offset, and length in |crypto_frame|.
140 void RetransmitData(QuicCryptoFrame* crypto_frame);
141
fayangaee31ef2019-08-20 06:47:51 -0700142 // Called to write buffered crypto frames.
143 void WriteBufferedCryptoFrames();
144
145 // Returns true if there is buffered crypto frames.
146 bool HasBufferedCryptoFrames() const;
147
QUICHE teama6ef0a62019-03-07 20:34:33 -0500148 // Returns true if any portion of the data at encryption level |level|
149 // starting at |offset| for |length| bytes is outstanding.
150 bool IsFrameOutstanding(EncryptionLevel level,
151 size_t offset,
152 size_t length) const;
153
154 // Returns true if the crypto handshake is still waiting for acks of sent
155 // data, and false if all data has been acked.
156 bool IsWaitingForAcks() const;
157
158 private:
159 // Data sent and received in CRYPTO frames is sent at multiple encryption
160 // levels. Some of the state for the single logical crypto stream is split
161 // across encryption levels, and a CryptoSubstream is used to manage that
162 // state for a particular encryption level.
dschinazif25169a2019-10-23 08:12:18 -0700163 struct QUIC_EXPORT_PRIVATE CryptoSubstream {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500164 CryptoSubstream(QuicCryptoStream* crypto_stream, EncryptionLevel);
165
166 QuicStreamSequencer sequencer;
167 QuicStreamSendBuffer send_buffer;
168 };
169
170 // Helper method for OnDataAvailable. Calls CryptoMessageParser::ProcessInput
171 // with the data available in |sequencer| and |level|, and marks the data
172 // passed to ProcessInput as consumed.
173 void OnDataAvailableInSequencer(QuicStreamSequencer* sequencer,
174 EncryptionLevel level);
175
176 // Consumed data according to encryption levels.
177 // TODO(fayang): This is not needed once switching from QUIC crypto to
178 // TLS 1.3, which never encrypts crypto data.
179 QuicIntervalSet<QuicStreamOffset> bytes_consumed_[NUM_ENCRYPTION_LEVELS];
180
181 // Keeps state for data sent/received in CRYPTO frames at each encryption
182 // level.
bnc5e469412019-12-05 14:16:25 -0800183 std::array<CryptoSubstream, NUM_ENCRYPTION_LEVELS> substreams_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500184};
185
186} // namespace quic
187
188#endif // QUICHE_QUIC_CORE_QUIC_CRYPTO_STREAM_H_