QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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 |
| 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 | |
bnc | 5e46941 | 2019-12-05 14:16:25 -0800 | [diff] [blame] | 8 | #include <array> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9 | #include <cstddef> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 10 | #include <string> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 11 | |
vasilvv | c872ee4 | 2020-10-07 19:50:22 -0700 | [diff] [blame] | 12 | #include "absl/strings/string_view.h" |
nharper | 6a6bd31 | 2020-09-17 13:20:53 -0700 | [diff] [blame] | 13 | #include "third_party/boringssl/src/include/openssl/ssl.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 14 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_framer.h" |
| 15 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_utils.h" |
| 16 | #include "net/third_party/quiche/src/quic/core/quic_config.h" |
| 17 | #include "net/third_party/quiche/src/quic/core/quic_packets.h" |
| 18 | #include "net/third_party/quiche/src/quic/core/quic_stream.h" |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 19 | #include "net/third_party/quiche/src/quic/core/quic_types.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 20 | #include "net/third_party/quiche/src/quic/platform/api/quic_export.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 21 | |
| 22 | namespace quic { |
| 23 | |
| 24 | class QuicSession; |
| 25 | |
| 26 | // Crypto handshake messages in QUIC take place over a reserved stream with the |
| 27 | // id 1. Each endpoint (client and server) will allocate an instance of a |
| 28 | // subclass of QuicCryptoStream to send and receive handshake messages. (In the |
| 29 | // normal 1-RTT handshake, the client will send a client hello, CHLO, message. |
| 30 | // The server will receive this message and respond with a server hello message, |
| 31 | // SHLO. At this point both sides will have established a crypto context they |
| 32 | // can use to send encrypted messages. |
| 33 | // |
| 34 | // For more details: |
| 35 | // https://docs.google.com/document/d/1g5nIXAIkN_Y-7XJW5K45IblHd_L2f5LTaDUDwvZ5L6g/edit?usp=sharing |
| 36 | class QUIC_EXPORT_PRIVATE QuicCryptoStream : public QuicStream { |
| 37 | public: |
| 38 | explicit QuicCryptoStream(QuicSession* session); |
| 39 | QuicCryptoStream(const QuicCryptoStream&) = delete; |
| 40 | QuicCryptoStream& operator=(const QuicCryptoStream&) = delete; |
| 41 | |
| 42 | ~QuicCryptoStream() override; |
| 43 | |
| 44 | // Returns the per-packet framing overhead associated with sending a |
| 45 | // handshake message for |version|. |
| 46 | static QuicByteCount CryptoMessageFramingOverhead( |
| 47 | QuicTransportVersion version, |
| 48 | QuicConnectionId connection_id); |
| 49 | |
| 50 | // QuicStream implementation |
| 51 | void OnStreamFrame(const QuicStreamFrame& frame) override; |
| 52 | void OnDataAvailable() override; |
| 53 | |
| 54 | // Called when a CRYPTO frame is received. |
| 55 | void OnCryptoFrame(const QuicCryptoFrame& frame); |
| 56 | |
| 57 | // Called when a CRYPTO frame is ACKed. |
| 58 | bool OnCryptoFrameAcked(const QuicCryptoFrame& frame, |
| 59 | QuicTime::Delta ack_delay_time); |
| 60 | |
renjietang | 546c714 | 2020-03-05 14:12:10 -0800 | [diff] [blame] | 61 | void OnStreamReset(const QuicRstStreamFrame& frame) override; |
| 62 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 63 | // Performs key extraction to derive a new secret of |result_len| bytes |
| 64 | // dependent on |label|, |context|, and the stream's negotiated subkey secret. |
| 65 | // Returns false if the handshake has not been confirmed or the parameters are |
| 66 | // invalid (e.g. |label| contains null bytes); returns true on success. |
vasilvv | c872ee4 | 2020-10-07 19:50:22 -0700 | [diff] [blame] | 67 | bool ExportKeyingMaterial(absl::string_view label, |
| 68 | absl::string_view context, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 69 | size_t result_len, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 70 | std::string* result) const; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 71 | |
| 72 | // Writes |data| to the QuicStream at level |level|. |
vasilvv | c872ee4 | 2020-10-07 19:50:22 -0700 | [diff] [blame] | 73 | virtual void WriteCryptoData(EncryptionLevel level, absl::string_view data); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 74 | |
nharper | 6a6bd31 | 2020-09-17 13:20:53 -0700 | [diff] [blame] | 75 | // Returns the ssl_early_data_reason_t describing why 0-RTT was accepted or |
| 76 | // rejected. Note that the value returned by this function may vary during the |
| 77 | // handshake. Once |one_rtt_keys_available| returns true, the value returned |
| 78 | // by this function will not change for the rest of the lifetime of the |
| 79 | // QuicCryptoStream. |
| 80 | virtual ssl_early_data_reason_t EarlyDataReason() const = 0; |
| 81 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 82 | // Returns true once an encrypter has been set for the connection. |
| 83 | virtual bool encryption_established() const = 0; |
| 84 | |
| 85 | // Returns true once the crypto handshake has completed. |
fayang | 685367a | 2020-01-14 10:40:15 -0800 | [diff] [blame] | 86 | virtual bool one_rtt_keys_available() const = 0; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 87 | |
| 88 | // Returns the parameters negotiated in the crypto handshake. |
| 89 | virtual const QuicCryptoNegotiatedParameters& crypto_negotiated_params() |
| 90 | const = 0; |
| 91 | |
| 92 | // Provides the message parser to use when data is received on this stream. |
| 93 | virtual CryptoMessageParser* crypto_message_parser() = 0; |
| 94 | |
fayang | d58736d | 2019-11-27 13:35:31 -0800 | [diff] [blame] | 95 | // Called when a packet of encryption |level| has been successfully decrypted. |
| 96 | virtual void OnPacketDecrypted(EncryptionLevel level) = 0; |
| 97 | |
fayang | 2f2915d | 2020-01-24 06:47:15 -0800 | [diff] [blame] | 98 | // Called when a 1RTT packet has been acknowledged. |
| 99 | virtual void OnOneRttPacketAcknowledged() = 0; |
| 100 | |
fayang | 44ae4e9 | 2020-04-28 13:09:42 -0700 | [diff] [blame] | 101 | // Called when a packet of ENCRYPTION_HANDSHAKE gets sent. |
| 102 | virtual void OnHandshakePacketSent() = 0; |
| 103 | |
fayang | 0106294 | 2020-01-22 07:23:23 -0800 | [diff] [blame] | 104 | // Called when a handshake done frame has been received. |
| 105 | virtual void OnHandshakeDoneReceived() = 0; |
| 106 | |
fayang | 9a863cf | 2020-01-16 14:12:11 -0800 | [diff] [blame] | 107 | // Returns current handshake state. |
| 108 | virtual HandshakeState GetHandshakeState() const = 0; |
| 109 | |
nharper | ac52a86 | 2020-06-08 12:41:06 -0700 | [diff] [blame] | 110 | // Called to provide the server-side application state that must be checked |
| 111 | // when performing a 0-RTT TLS resumption. |
| 112 | // |
| 113 | // On a client, this may be called at any time; 0-RTT tickets will not be |
| 114 | // cached until this function is called. When a 0-RTT resumption is attempted, |
| 115 | // QuicSession::SetApplicationState will be called with the state provided by |
| 116 | // a call to this function on a previous connection. |
| 117 | // |
| 118 | // On a server, this function must be called before commencing the handshake, |
| 119 | // otherwise 0-RTT tickets will not be issued. On subsequent connections, |
| 120 | // 0-RTT will be rejected if the data passed into this function does not match |
| 121 | // the data passed in on the connection where the 0-RTT ticket was issued. |
| 122 | virtual void SetServerApplicationStateForResumption( |
| 123 | std::unique_ptr<ApplicationState> state) = 0; |
| 124 | |
nharper | 486a8a9 | 2019-08-28 16:25:10 -0700 | [diff] [blame] | 125 | // Returns the maximum number of bytes that can be buffered at a particular |
| 126 | // encryption level |level|. |
| 127 | virtual size_t BufferSizeLimitForLevel(EncryptionLevel level) const; |
| 128 | |
mattm | 072a7e3 | 2020-10-09 16:16:56 -0700 | [diff] [blame] | 129 | // Returns whether the implementation supports key update. |
| 130 | virtual bool KeyUpdateSupportedLocally() const = 0; |
| 131 | |
| 132 | // Called to generate a decrypter for the next key phase. Each call should |
| 133 | // generate the key for phase n+1. |
| 134 | virtual std::unique_ptr<QuicDecrypter> |
| 135 | AdvanceKeysAndCreateCurrentOneRttDecrypter() = 0; |
| 136 | |
| 137 | // Called to generate an encrypter for the same key phase of the last |
| 138 | // decrypter returned by AdvanceKeysAndCreateCurrentOneRttDecrypter(). |
| 139 | virtual std::unique_ptr<QuicEncrypter> CreateCurrentOneRttEncrypter() = 0; |
| 140 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 141 | // Called to cancel retransmission of unencrypted crypto stream data. |
| 142 | void NeuterUnencryptedStreamData(); |
| 143 | |
fayang | be7b075 | 2020-04-07 11:27:21 -0700 | [diff] [blame] | 144 | // Called to cancel retransmission of data of encryption |level|. |
| 145 | void NeuterStreamDataOfEncryptionLevel(EncryptionLevel level); |
| 146 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 147 | // Override to record the encryption level of consumed data. |
dschinazi | f1e7b42 | 2020-04-30 12:21:28 -0700 | [diff] [blame] | 148 | void OnStreamDataConsumed(QuicByteCount bytes_consumed) override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 149 | |
| 150 | // Returns whether there are any bytes pending retransmission in CRYPTO |
| 151 | // frames. |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 152 | virtual bool HasPendingCryptoRetransmission() const; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 153 | |
| 154 | // Writes any pending CRYPTO frame retransmissions. |
| 155 | void WritePendingCryptoRetransmission(); |
| 156 | |
| 157 | // Override to retransmit lost crypto data with the appropriate encryption |
| 158 | // level. |
| 159 | void WritePendingRetransmission() override; |
| 160 | |
| 161 | // Override to send unacked crypto data with the appropriate encryption level. |
| 162 | bool RetransmitStreamData(QuicStreamOffset offset, |
| 163 | QuicByteCount data_length, |
renjietang | 4d992bf | 2020-03-03 13:01:55 -0800 | [diff] [blame] | 164 | bool fin, |
| 165 | TransmissionType type) override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 166 | |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 167 | // Sends stream retransmission data at |encryption_level|. |
| 168 | QuicConsumedData RetransmitStreamDataAtLevel( |
| 169 | QuicStreamOffset retransmission_offset, |
| 170 | QuicByteCount retransmission_length, |
renjietang | 4d992bf | 2020-03-03 13:01:55 -0800 | [diff] [blame] | 171 | EncryptionLevel encryption_level, |
| 172 | TransmissionType type); |
renjietang | 41a1b41 | 2020-02-27 15:05:14 -0800 | [diff] [blame] | 173 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 174 | // Returns the number of bytes of handshake data that have been received from |
| 175 | // the peer in either CRYPTO or STREAM frames. |
| 176 | uint64_t crypto_bytes_read() const; |
| 177 | |
| 178 | // Returns the number of bytes of handshake data that have been received from |
| 179 | // the peer in CRYPTO frames at a particular encryption level. |
| 180 | QuicByteCount BytesReadOnLevel(EncryptionLevel level) const; |
| 181 | |
| 182 | // Writes |data_length| of data of a crypto frame to |writer|. The data |
| 183 | // written is from the send buffer for encryption level |level| and starts at |
| 184 | // |offset|. |
| 185 | bool WriteCryptoFrame(EncryptionLevel level, |
| 186 | QuicStreamOffset offset, |
| 187 | QuicByteCount data_length, |
| 188 | QuicDataWriter* writer); |
| 189 | |
| 190 | // Called when data from a CRYPTO frame is considered lost. The lost data is |
| 191 | // identified by the encryption level, offset, and length in |crypto_frame|. |
| 192 | void OnCryptoFrameLost(QuicCryptoFrame* crypto_frame); |
| 193 | |
| 194 | // Called to retransmit any outstanding data in the range indicated by the |
| 195 | // encryption level, offset, and length in |crypto_frame|. |
renjietang | 4d992bf | 2020-03-03 13:01:55 -0800 | [diff] [blame] | 196 | void RetransmitData(QuicCryptoFrame* crypto_frame, TransmissionType type); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 197 | |
fayang | aee31ef | 2019-08-20 06:47:51 -0700 | [diff] [blame] | 198 | // Called to write buffered crypto frames. |
| 199 | void WriteBufferedCryptoFrames(); |
| 200 | |
| 201 | // Returns true if there is buffered crypto frames. |
| 202 | bool HasBufferedCryptoFrames() const; |
| 203 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 204 | // Returns true if any portion of the data at encryption level |level| |
| 205 | // starting at |offset| for |length| bytes is outstanding. |
| 206 | bool IsFrameOutstanding(EncryptionLevel level, |
| 207 | size_t offset, |
| 208 | size_t length) const; |
| 209 | |
| 210 | // Returns true if the crypto handshake is still waiting for acks of sent |
| 211 | // data, and false if all data has been acked. |
| 212 | bool IsWaitingForAcks() const; |
| 213 | |
| 214 | private: |
| 215 | // Data sent and received in CRYPTO frames is sent at multiple encryption |
| 216 | // levels. Some of the state for the single logical crypto stream is split |
| 217 | // across encryption levels, and a CryptoSubstream is used to manage that |
| 218 | // state for a particular encryption level. |
dschinazi | f25169a | 2019-10-23 08:12:18 -0700 | [diff] [blame] | 219 | struct QUIC_EXPORT_PRIVATE CryptoSubstream { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 220 | CryptoSubstream(QuicCryptoStream* crypto_stream, EncryptionLevel); |
| 221 | |
| 222 | QuicStreamSequencer sequencer; |
| 223 | QuicStreamSendBuffer send_buffer; |
| 224 | }; |
| 225 | |
| 226 | // Helper method for OnDataAvailable. Calls CryptoMessageParser::ProcessInput |
| 227 | // with the data available in |sequencer| and |level|, and marks the data |
| 228 | // passed to ProcessInput as consumed. |
| 229 | void OnDataAvailableInSequencer(QuicStreamSequencer* sequencer, |
| 230 | EncryptionLevel level); |
| 231 | |
| 232 | // Consumed data according to encryption levels. |
| 233 | // TODO(fayang): This is not needed once switching from QUIC crypto to |
| 234 | // TLS 1.3, which never encrypts crypto data. |
| 235 | QuicIntervalSet<QuicStreamOffset> bytes_consumed_[NUM_ENCRYPTION_LEVELS]; |
| 236 | |
| 237 | // Keeps state for data sent/received in CRYPTO frames at each encryption |
| 238 | // level. |
bnc | 5e46941 | 2019-12-05 14:16:25 -0800 | [diff] [blame] | 239 | std::array<CryptoSubstream, NUM_ENCRYPTION_LEVELS> substreams_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 240 | }; |
| 241 | |
| 242 | } // namespace quic |
| 243 | |
| 244 | #endif // QUICHE_QUIC_CORE_QUIC_CRYPTO_STREAM_H_ |