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 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 12 | #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" |
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 | |
| 22 | class 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 |
| 34 | class 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. |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 63 | bool ExportKeyingMaterial(quiche::QuicheStringPiece label, |
| 64 | quiche::QuicheStringPiece context, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 65 | size_t result_len, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 66 | std::string* result) const; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 67 | |
| 68 | // Writes |data| to the QuicStream at level |level|. |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 69 | virtual void WriteCryptoData(EncryptionLevel level, |
| 70 | quiche::QuicheStringPiece data); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 71 | |
| 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. |
fayang | 685367a | 2020-01-14 10:40:15 -0800 | [diff] [blame] | 76 | virtual bool one_rtt_keys_available() const = 0; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 77 | |
| 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 | |
fayang | d58736d | 2019-11-27 13:35:31 -0800 | [diff] [blame] | 85 | // Called when a packet of encryption |level| has been successfully decrypted. |
| 86 | virtual void OnPacketDecrypted(EncryptionLevel level) = 0; |
| 87 | |
nharper | 486a8a9 | 2019-08-28 16:25:10 -0700 | [diff] [blame] | 88 | // 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 92 | // 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. |
nharper | 46833c3 | 2019-05-15 21:33:05 -0700 | [diff] [blame] | 104 | virtual bool HasPendingCryptoRetransmission() const; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 105 | |
| 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 | |
fayang | aee31ef | 2019-08-20 06:47:51 -0700 | [diff] [blame] | 142 | // 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 148 | // 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. |
dschinazi | f25169a | 2019-10-23 08:12:18 -0700 | [diff] [blame] | 163 | struct QUIC_EXPORT_PRIVATE CryptoSubstream { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 164 | 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. |
bnc | 5e46941 | 2019-12-05 14:16:25 -0800 | [diff] [blame] | 183 | std::array<CryptoSubstream, NUM_ENCRYPTION_LEVELS> substreams_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | } // namespace quic |
| 187 | |
| 188 | #endif // QUICHE_QUIC_CORE_QUIC_CRYPTO_STREAM_H_ |