blob: fd14d27a2709211f7e2fd7ef229b6c4d4e033d64 [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
nharper21a32492020-02-28 12:53:17 -08005#ifndef QUICHE_QUIC_CORE_QUIC_CRYPTO_SERVER_STREAM_H_
6#define QUICHE_QUIC_CORE_QUIC_CRYPTO_SERVER_STREAM_H_
QUICHE teama6ef0a62019-03-07 20:34:33 -05007
vasilvv872e7a32019-03-12 16:42:44 -07008#include <string>
9
dschinazi56fb53e2019-06-21 15:30:04 -070010#include "net/third_party/quiche/src/quic/core/proto/cached_network_parameters_proto.h"
11#include "net/third_party/quiche/src/quic/core/proto/source_address_token_proto.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050012#include "net/third_party/quiche/src/quic/core/quic_crypto_handshaker.h"
nharperf6cb54d2020-02-27 14:14:21 -080013#include "net/third_party/quiche/src/quic/core/quic_crypto_server_stream_base.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050014#include "net/third_party/quiche/src/quic/core/quic_session.h"
15#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050016
17namespace quic {
18
19namespace test {
20class QuicCryptoServerStreamPeer;
21} // namespace test
22
nharper21a32492020-02-28 12:53:17 -080023class QUIC_EXPORT_PRIVATE QuicCryptoServerStream
nharperbb7d1f32020-02-21 17:20:33 -080024 : public QuicCryptoServerStreamBase,
QUICHE teama6ef0a62019-03-07 20:34:33 -050025 public QuicCryptoHandshaker {
26 public:
nharper21a32492020-02-28 12:53:17 -080027 QuicCryptoServerStream(const QuicCryptoServerStream&) = delete;
28 QuicCryptoServerStream& operator=(const QuicCryptoServerStream&) = delete;
QUICHE teama6ef0a62019-03-07 20:34:33 -050029
nharper21a32492020-02-28 12:53:17 -080030 ~QuicCryptoServerStream() override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050031
rch85240a12019-12-23 11:51:59 -080032 // From HandshakerInterface
QUICHE teama6ef0a62019-03-07 20:34:33 -050033 void CancelOutstandingCallbacks() override;
vasilvvc48c8712019-03-11 13:38:16 -070034 bool GetBase64SHA256ClientChannelID(std::string* output) const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050035 void SendServerConfigUpdate(
36 const CachedNetworkParameters* cached_network_params) override;
fayang6098a0a2020-03-13 15:32:10 -070037 bool IsZeroRtt() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050038 int NumServerConfigUpdateMessagesSent() const override;
39 const CachedNetworkParameters* PreviousCachedNetworkParams() const override;
40 bool ZeroRttAttempted() const override;
41 void SetPreviousCachedNetworkParams(
42 CachedNetworkParameters cached_network_params) override;
fayangd58736d2019-11-27 13:35:31 -080043 void OnPacketDecrypted(EncryptionLevel level) override;
nharperbb7d1f32020-02-21 17:20:33 -080044 void OnOneRttPacketAcknowledged() override {}
45 void OnHandshakeDoneReceived() override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050046 bool ShouldSendExpectCTHeader() const override;
47
48 // From QuicCryptoStream
49 bool encryption_established() const override;
fayang685367a2020-01-14 10:40:15 -080050 bool one_rtt_keys_available() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050051 const QuicCryptoNegotiatedParameters& crypto_negotiated_params()
52 const override;
53 CryptoMessageParser* crypto_message_parser() override;
fayang9a863cf2020-01-16 14:12:11 -080054 HandshakeState GetHandshakeState() const override;
nharper486a8a92019-08-28 16:25:10 -070055 size_t BufferSizeLimitForLevel(EncryptionLevel level) const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050056
57 // From QuicCryptoHandshaker
58 void OnHandshakeMessage(const CryptoHandshakeMessage& message) override;
59
60 protected:
nharperbb7d1f32020-02-21 17:20:33 -080061 QUIC_EXPORT_PRIVATE friend std::unique_ptr<QuicCryptoServerStreamBase>
62 CreateCryptoServerStream(const QuicCryptoServerConfig* crypto_config,
63 QuicCompressedCertsCache* compressed_certs_cache,
64 QuicSession* session,
65 QuicCryptoServerStreamBase::Helper* helper);
66
67 // |crypto_config| must outlive the stream.
68 // |session| must outlive the stream.
69 // |helper| must outlive the stream.
nharper21a32492020-02-28 12:53:17 -080070 QuicCryptoServerStream(const QuicCryptoServerConfig* crypto_config,
71 QuicCompressedCertsCache* compressed_certs_cache,
72 QuicSession* session,
73 QuicCryptoServerStreamBase::Helper* helper);
nharperbb7d1f32020-02-21 17:20:33 -080074
QUICHE teama6ef0a62019-03-07 20:34:33 -050075 virtual void ProcessClientHello(
76 QuicReferenceCountedPointer<ValidateClientHelloResultCallback::Result>
77 result,
78 std::unique_ptr<ProofSource::Details> proof_source_details,
79 std::unique_ptr<ProcessClientHelloResultCallback> done_cb);
80
81 // Hook that allows the server to set QuicConfig defaults just
82 // before going through the parameter negotiation step.
83 virtual void OverrideQuicConfigDefaults(QuicConfig* config);
84
85 // Returns client address used to generate and validate source address token.
86 virtual const QuicSocketAddress GetClientAddress();
87
88 // Returns the QuicSession that this stream belongs to.
89 QuicSession* session() const { return session_; }
90
91 void set_encryption_established(bool encryption_established) {
92 encryption_established_ = encryption_established;
93 }
94
fayang685367a2020-01-14 10:40:15 -080095 void set_one_rtt_keys_available(bool one_rtt_keys_available) {
96 one_rtt_keys_available_ = one_rtt_keys_available;
QUICHE teama6ef0a62019-03-07 20:34:33 -050097 }
98
99 private:
100 friend class test::QuicCryptoServerStreamPeer;
101
dschinazif25169a2019-10-23 08:12:18 -0700102 class QUIC_EXPORT_PRIVATE ValidateCallback
103 : public ValidateClientHelloResultCallback {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500104 public:
nharper21a32492020-02-28 12:53:17 -0800105 explicit ValidateCallback(QuicCryptoServerStream* parent);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500106 ValidateCallback(const ValidateCallback&) = delete;
107 ValidateCallback& operator=(const ValidateCallback&) = delete;
108 // To allow the parent to detach itself from the callback before deletion.
109 void Cancel();
110
111 // From ValidateClientHelloResultCallback
112 void Run(QuicReferenceCountedPointer<Result> result,
113 std::unique_ptr<ProofSource::Details> details) override;
114
115 private:
nharper21a32492020-02-28 12:53:17 -0800116 QuicCryptoServerStream* parent_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500117 };
118
119 class SendServerConfigUpdateCallback
120 : public BuildServerConfigUpdateMessageResultCallback {
121 public:
nharper21a32492020-02-28 12:53:17 -0800122 explicit SendServerConfigUpdateCallback(QuicCryptoServerStream* parent);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500123 SendServerConfigUpdateCallback(const SendServerConfigUpdateCallback&) =
124 delete;
125 void operator=(const SendServerConfigUpdateCallback&) = delete;
126
127 // To allow the parent to detach itself from the callback before deletion.
128 void Cancel();
129
130 // From BuildServerConfigUpdateMessageResultCallback
131 void Run(bool ok, const CryptoHandshakeMessage& message) override;
132
133 private:
nharper21a32492020-02-28 12:53:17 -0800134 QuicCryptoServerStream* parent_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500135 };
136
137 // Invoked by ValidateCallback::RunImpl once initial validation of
138 // the client hello is complete. Finishes processing of the client
139 // hello message and handles handshake success/failure.
140 void FinishProcessingHandshakeMessage(
141 QuicReferenceCountedPointer<ValidateClientHelloResultCallback::Result>
142 result,
143 std::unique_ptr<ProofSource::Details> details);
144
145 class ProcessClientHelloCallback;
146 friend class ProcessClientHelloCallback;
147
148 // Portion of FinishProcessingHandshakeMessage which executes after
149 // ProcessClientHello has been called.
150 void FinishProcessingHandshakeMessageAfterProcessClientHello(
151 const ValidateClientHelloResultCallback::Result& result,
152 QuicErrorCode error,
vasilvvc48c8712019-03-11 13:38:16 -0700153 const std::string& error_details,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500154 std::unique_ptr<CryptoHandshakeMessage> reply,
155 std::unique_ptr<DiversificationNonce> diversification_nonce,
156 std::unique_ptr<ProofSource::Details> proof_source_details);
157
158 // Invoked by SendServerConfigUpdateCallback::RunImpl once the proof has been
159 // received. |ok| indicates whether or not the proof was successfully
160 // acquired, and |message| holds the partially-constructed message from
161 // SendServerConfigUpdate.
162 void FinishSendServerConfigUpdate(bool ok,
163 const CryptoHandshakeMessage& message);
164
QUICHE teama6ef0a62019-03-07 20:34:33 -0500165 // Returns the QuicTransportVersion of the connection.
166 QuicTransportVersion transport_version() const {
renjietangd1d00852019-09-06 10:43:12 -0700167 return session_->transport_version();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500168 }
169
QUICHE teama6ef0a62019-03-07 20:34:33 -0500170 QuicSession* session_;
fayangd58736d2019-11-27 13:35:31 -0800171 HandshakerDelegateInterface* delegate_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500172
173 // crypto_config_ contains crypto parameters for the handshake.
174 const QuicCryptoServerConfig* crypto_config_;
175
176 // compressed_certs_cache_ contains a set of most recently compressed certs.
177 // Owned by QuicDispatcher.
178 QuicCompressedCertsCache* compressed_certs_cache_;
179
180 // Server's certificate chain and signature of the server config, as provided
181 // by ProofSource::GetProof.
182 QuicReferenceCountedPointer<QuicSignedServerConfig> signed_config_;
183
184 // Hash of the last received CHLO message which can be used for generating
185 // server config update messages.
vasilvvc48c8712019-03-11 13:38:16 -0700186 std::string chlo_hash_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500187
188 // Pointer to the helper for this crypto stream. Must outlive this stream.
nharper5f23a2d2020-02-20 10:44:09 -0800189 QuicCryptoServerStreamBase::Helper* helper_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500190
191 // Number of handshake messages received by this stream.
192 uint8_t num_handshake_messages_;
193
194 // Number of handshake messages received by this stream that contain
195 // server nonces (indicating that this is a non-zero-RTT handshake
196 // attempt).
197 uint8_t num_handshake_messages_with_server_nonces_;
198
199 // Pointer to the active callback that will receive the result of
200 // BuildServerConfigUpdateMessage and forward it to
201 // FinishSendServerConfigUpdate. nullptr if no update message is currently
202 // being built.
203 SendServerConfigUpdateCallback* send_server_config_update_cb_;
204
205 // Number of server config update (SCUP) messages sent by this stream.
206 int num_server_config_update_messages_sent_;
207
208 // If the client provides CachedNetworkParameters in the STK in the CHLO, then
209 // store here, and send back in future STKs if we have no better bandwidth
210 // estimate to send.
211 std::unique_ptr<CachedNetworkParameters> previous_cached_network_params_;
212
213 // Contains any source address tokens which were present in the CHLO.
214 SourceAddressTokens previous_source_address_tokens_;
215
wub4ca3d672019-06-12 08:27:17 -0700216 // True if client attempts 0-rtt handshake (which can succeed or fail).
QUICHE teama6ef0a62019-03-07 20:34:33 -0500217 bool zero_rtt_attempted_;
218
219 // Size of the packet containing the most recently received CHLO.
220 QuicByteCount chlo_packet_size_;
221
222 // Pointer to the active callback that will receive the result of the client
223 // hello validation request and forward it to FinishProcessingHandshakeMessage
224 // for processing. nullptr if no handshake message is being validated. Note
225 // that this field is mutually exclusive with process_client_hello_cb_.
226 ValidateCallback* validate_client_hello_cb_;
227
228 // Pointer to the active callback which will receive the results of
229 // ProcessClientHello and forward it to
230 // FinishProcessingHandshakeMessageAfterProcessClientHello. Note that this
231 // field is mutually exclusive with validate_client_hello_cb_.
232 ProcessClientHelloCallback* process_client_hello_cb_;
233
234 bool encryption_established_;
fayang685367a2020-01-14 10:40:15 -0800235 bool one_rtt_keys_available_;
fayang9a863cf2020-01-16 14:12:11 -0800236 bool one_rtt_packet_decrypted_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500237 QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters>
238 crypto_negotiated_params_;
239};
240
241} // namespace quic
242
nharper21a32492020-02-28 12:53:17 -0800243#endif // QUICHE_QUIC_CORE_QUIC_CRYPTO_SERVER_STREAM_H_