blob: e60277b0f99e30d75b37f76652e57aa53595fcc8 [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
8#include <cstddef>
9
10#include "base/macros.h"
11#include "net/third_party/quiche/src/quic/core/crypto/crypto_framer.h"
12#include "net/third_party/quiche/src/quic/core/crypto/crypto_utils.h"
13#include "net/third_party/quiche/src/quic/core/quic_config.h"
14#include "net/third_party/quiche/src/quic/core/quic_packets.h"
15#include "net/third_party/quiche/src/quic/core/quic_stream.h"
16#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
17#include "net/third_party/quiche/src/quic/platform/api/quic_string.h"
18#include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h"
19
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.
63 bool ExportKeyingMaterial(QuicStringPiece label,
64 QuicStringPiece context,
65 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|.
69 virtual void WriteCryptoData(EncryptionLevel level, QuicStringPiece data);
70
71 // Returns true once an encrypter has been set for the connection.
72 virtual bool encryption_established() const = 0;
73
74 // Returns true once the crypto handshake has completed.
75 virtual bool handshake_confirmed() const = 0;
76
77 // Returns the parameters negotiated in the crypto handshake.
78 virtual const QuicCryptoNegotiatedParameters& crypto_negotiated_params()
79 const = 0;
80
81 // Provides the message parser to use when data is received on this stream.
82 virtual CryptoMessageParser* crypto_message_parser() = 0;
83
84 // Called when the underlying QuicConnection has agreed upon a QUIC version to
85 // use.
86 virtual void OnSuccessfulVersionNegotiation(const ParsedQuicVersion& version);
87
88 // Called to cancel retransmission of unencrypted crypto stream data.
89 void NeuterUnencryptedStreamData();
90
91 // Override to record the encryption level of consumed data.
92 void OnStreamDataConsumed(size_t bytes_consumed) override;
93
94 // Returns whether there are any bytes pending retransmission in CRYPTO
95 // frames.
96 virtual bool HasPendingCryptoRetransmission();
97
98 // Writes any pending CRYPTO frame retransmissions.
99 void WritePendingCryptoRetransmission();
100
101 // Override to retransmit lost crypto data with the appropriate encryption
102 // level.
103 void WritePendingRetransmission() override;
104
105 // Override to send unacked crypto data with the appropriate encryption level.
106 bool RetransmitStreamData(QuicStreamOffset offset,
107 QuicByteCount data_length,
108 bool fin) override;
109
110 // Returns the number of bytes of handshake data that have been received from
111 // the peer in either CRYPTO or STREAM frames.
112 uint64_t crypto_bytes_read() const;
113
114 // Returns the number of bytes of handshake data that have been received from
115 // the peer in CRYPTO frames at a particular encryption level.
116 QuicByteCount BytesReadOnLevel(EncryptionLevel level) const;
117
118 // Writes |data_length| of data of a crypto frame to |writer|. The data
119 // written is from the send buffer for encryption level |level| and starts at
120 // |offset|.
121 bool WriteCryptoFrame(EncryptionLevel level,
122 QuicStreamOffset offset,
123 QuicByteCount data_length,
124 QuicDataWriter* writer);
125
126 // Called when data from a CRYPTO frame is considered lost. The lost data is
127 // identified by the encryption level, offset, and length in |crypto_frame|.
128 void OnCryptoFrameLost(QuicCryptoFrame* crypto_frame);
129
130 // Called to retransmit any outstanding data in the range indicated by the
131 // encryption level, offset, and length in |crypto_frame|.
132 void RetransmitData(QuicCryptoFrame* crypto_frame);
133
134 // Returns true if any portion of the data at encryption level |level|
135 // starting at |offset| for |length| bytes is outstanding.
136 bool IsFrameOutstanding(EncryptionLevel level,
137 size_t offset,
138 size_t length) const;
139
140 // Returns true if the crypto handshake is still waiting for acks of sent
141 // data, and false if all data has been acked.
142 bool IsWaitingForAcks() const;
143
144 private:
145 // Data sent and received in CRYPTO frames is sent at multiple encryption
146 // levels. Some of the state for the single logical crypto stream is split
147 // across encryption levels, and a CryptoSubstream is used to manage that
148 // state for a particular encryption level.
149 struct CryptoSubstream {
150 CryptoSubstream(QuicCryptoStream* crypto_stream, EncryptionLevel);
151
152 QuicStreamSequencer sequencer;
153 QuicStreamSendBuffer send_buffer;
154 };
155
156 // Helper method for OnDataAvailable. Calls CryptoMessageParser::ProcessInput
157 // with the data available in |sequencer| and |level|, and marks the data
158 // passed to ProcessInput as consumed.
159 void OnDataAvailableInSequencer(QuicStreamSequencer* sequencer,
160 EncryptionLevel level);
161
162 // Consumed data according to encryption levels.
163 // TODO(fayang): This is not needed once switching from QUIC crypto to
164 // TLS 1.3, which never encrypts crypto data.
165 QuicIntervalSet<QuicStreamOffset> bytes_consumed_[NUM_ENCRYPTION_LEVELS];
166
167 // Keeps state for data sent/received in CRYPTO frames at each encryption
168 // level.
169 CryptoSubstream substreams_[NUM_ENCRYPTION_LEVELS];
170};
171
172} // namespace quic
173
174#endif // QUICHE_QUIC_CORE_QUIC_CRYPTO_STREAM_H_