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