blob: 911ca7e789fc211e703605deab60749101798ab6 [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
QUICHE team5be974e2020-12-29 18:35:24 -050010#include "quic/core/proto/cached_network_parameters_proto.h"
11#include "quic/core/proto/source_address_token_proto.h"
12#include "quic/core/quic_crypto_handshaker.h"
13#include "quic/core/quic_crypto_server_stream_base.h"
14#include "quic/core/quic_session.h"
15#include "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
nharper9b0a1af2020-08-07 17:11:29 -070032 // From QuicCryptoServerStreamBase
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;
nharperfd0e2632020-06-02 11:19:05 -070038 bool IsResumption() const override;
39 bool ResumptionAttempted() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050040 int NumServerConfigUpdateMessagesSent() const override;
41 const CachedNetworkParameters* PreviousCachedNetworkParams() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050042 void SetPreviousCachedNetworkParams(
43 CachedNetworkParameters cached_network_params) override;
fayangd58736d2019-11-27 13:35:31 -080044 void OnPacketDecrypted(EncryptionLevel level) override;
nharperbb7d1f32020-02-21 17:20:33 -080045 void OnOneRttPacketAcknowledged() override {}
fayang44ae4e92020-04-28 13:09:42 -070046 void OnHandshakePacketSent() override {}
fayanga6a85a82020-05-04 08:58:53 -070047 void OnConnectionClosed(QuicErrorCode /*error*/,
48 ConnectionCloseSource /*source*/) override {}
nharperbb7d1f32020-02-21 17:20:33 -080049 void OnHandshakeDoneReceived() override;
fayang133b8682020-12-08 05:50:33 -080050 void OnNewTokenReceived(absl::string_view token) override;
51 std::string GetAddressToken() const override;
52 bool ValidateAddressToken(absl::string_view token) const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050053 bool ShouldSendExpectCTHeader() const override;
nharper9b0a1af2020-08-07 17:11:29 -070054 const ProofSource::Details* ProofSourceDetails() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050055
56 // From QuicCryptoStream
nharper6a6bd312020-09-17 13:20:53 -070057 ssl_early_data_reason_t EarlyDataReason() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050058 bool encryption_established() const override;
fayang685367a2020-01-14 10:40:15 -080059 bool one_rtt_keys_available() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050060 const QuicCryptoNegotiatedParameters& crypto_negotiated_params()
61 const override;
62 CryptoMessageParser* crypto_message_parser() override;
fayang9a863cf2020-01-16 14:12:11 -080063 HandshakeState GetHandshakeState() const override;
nharperac52a862020-06-08 12:41:06 -070064 void SetServerApplicationStateForResumption(
65 std::unique_ptr<ApplicationState> state) override;
nharper486a8a92019-08-28 16:25:10 -070066 size_t BufferSizeLimitForLevel(EncryptionLevel level) const override;
mattm072a7e32020-10-09 16:16:56 -070067 bool KeyUpdateSupportedLocally() const override;
68 std::unique_ptr<QuicDecrypter> AdvanceKeysAndCreateCurrentOneRttDecrypter()
69 override;
70 std::unique_ptr<QuicEncrypter> CreateCurrentOneRttEncrypter() override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050071
72 // From QuicCryptoHandshaker
73 void OnHandshakeMessage(const CryptoHandshakeMessage& message) override;
74
75 protected:
nharperbb7d1f32020-02-21 17:20:33 -080076 QUIC_EXPORT_PRIVATE friend std::unique_ptr<QuicCryptoServerStreamBase>
77 CreateCryptoServerStream(const QuicCryptoServerConfig* crypto_config,
78 QuicCompressedCertsCache* compressed_certs_cache,
79 QuicSession* session,
80 QuicCryptoServerStreamBase::Helper* helper);
81
82 // |crypto_config| must outlive the stream.
83 // |session| must outlive the stream.
84 // |helper| must outlive the stream.
nharper21a32492020-02-28 12:53:17 -080085 QuicCryptoServerStream(const QuicCryptoServerConfig* crypto_config,
86 QuicCompressedCertsCache* compressed_certs_cache,
87 QuicSession* session,
88 QuicCryptoServerStreamBase::Helper* helper);
nharperbb7d1f32020-02-21 17:20:33 -080089
QUICHE teama6ef0a62019-03-07 20:34:33 -050090 virtual void ProcessClientHello(
91 QuicReferenceCountedPointer<ValidateClientHelloResultCallback::Result>
92 result,
93 std::unique_ptr<ProofSource::Details> proof_source_details,
94 std::unique_ptr<ProcessClientHelloResultCallback> done_cb);
95
96 // Hook that allows the server to set QuicConfig defaults just
97 // before going through the parameter negotiation step.
98 virtual void OverrideQuicConfigDefaults(QuicConfig* config);
99
100 // Returns client address used to generate and validate source address token.
101 virtual const QuicSocketAddress GetClientAddress();
102
103 // Returns the QuicSession that this stream belongs to.
104 QuicSession* session() const { return session_; }
105
106 void set_encryption_established(bool encryption_established) {
107 encryption_established_ = encryption_established;
108 }
109
fayang685367a2020-01-14 10:40:15 -0800110 void set_one_rtt_keys_available(bool one_rtt_keys_available) {
111 one_rtt_keys_available_ = one_rtt_keys_available;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500112 }
113
114 private:
115 friend class test::QuicCryptoServerStreamPeer;
116
dschinazif25169a2019-10-23 08:12:18 -0700117 class QUIC_EXPORT_PRIVATE ValidateCallback
118 : public ValidateClientHelloResultCallback {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500119 public:
nharper21a32492020-02-28 12:53:17 -0800120 explicit ValidateCallback(QuicCryptoServerStream* parent);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500121 ValidateCallback(const ValidateCallback&) = delete;
122 ValidateCallback& operator=(const ValidateCallback&) = delete;
123 // To allow the parent to detach itself from the callback before deletion.
124 void Cancel();
125
126 // From ValidateClientHelloResultCallback
127 void Run(QuicReferenceCountedPointer<Result> result,
128 std::unique_ptr<ProofSource::Details> details) override;
129
130 private:
nharper21a32492020-02-28 12:53:17 -0800131 QuicCryptoServerStream* parent_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500132 };
133
134 class SendServerConfigUpdateCallback
135 : public BuildServerConfigUpdateMessageResultCallback {
136 public:
nharper21a32492020-02-28 12:53:17 -0800137 explicit SendServerConfigUpdateCallback(QuicCryptoServerStream* parent);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500138 SendServerConfigUpdateCallback(const SendServerConfigUpdateCallback&) =
139 delete;
140 void operator=(const SendServerConfigUpdateCallback&) = delete;
141
142 // To allow the parent to detach itself from the callback before deletion.
143 void Cancel();
144
145 // From BuildServerConfigUpdateMessageResultCallback
146 void Run(bool ok, const CryptoHandshakeMessage& message) override;
147
148 private:
nharper21a32492020-02-28 12:53:17 -0800149 QuicCryptoServerStream* parent_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500150 };
151
152 // Invoked by ValidateCallback::RunImpl once initial validation of
153 // the client hello is complete. Finishes processing of the client
154 // hello message and handles handshake success/failure.
155 void FinishProcessingHandshakeMessage(
156 QuicReferenceCountedPointer<ValidateClientHelloResultCallback::Result>
157 result,
158 std::unique_ptr<ProofSource::Details> details);
159
160 class ProcessClientHelloCallback;
161 friend class ProcessClientHelloCallback;
162
163 // Portion of FinishProcessingHandshakeMessage which executes after
164 // ProcessClientHello has been called.
165 void FinishProcessingHandshakeMessageAfterProcessClientHello(
166 const ValidateClientHelloResultCallback::Result& result,
167 QuicErrorCode error,
vasilvvc48c8712019-03-11 13:38:16 -0700168 const std::string& error_details,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500169 std::unique_ptr<CryptoHandshakeMessage> reply,
170 std::unique_ptr<DiversificationNonce> diversification_nonce,
171 std::unique_ptr<ProofSource::Details> proof_source_details);
172
173 // Invoked by SendServerConfigUpdateCallback::RunImpl once the proof has been
174 // received. |ok| indicates whether or not the proof was successfully
175 // acquired, and |message| holds the partially-constructed message from
176 // SendServerConfigUpdate.
177 void FinishSendServerConfigUpdate(bool ok,
178 const CryptoHandshakeMessage& message);
179
QUICHE teama6ef0a62019-03-07 20:34:33 -0500180 // Returns the QuicTransportVersion of the connection.
181 QuicTransportVersion transport_version() const {
renjietangd1d00852019-09-06 10:43:12 -0700182 return session_->transport_version();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500183 }
184
QUICHE teama6ef0a62019-03-07 20:34:33 -0500185 QuicSession* session_;
fayangd58736d2019-11-27 13:35:31 -0800186 HandshakerDelegateInterface* delegate_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500187
188 // crypto_config_ contains crypto parameters for the handshake.
189 const QuicCryptoServerConfig* crypto_config_;
190
191 // compressed_certs_cache_ contains a set of most recently compressed certs.
192 // Owned by QuicDispatcher.
193 QuicCompressedCertsCache* compressed_certs_cache_;
194
195 // Server's certificate chain and signature of the server config, as provided
196 // by ProofSource::GetProof.
197 QuicReferenceCountedPointer<QuicSignedServerConfig> signed_config_;
198
199 // Hash of the last received CHLO message which can be used for generating
200 // server config update messages.
vasilvvc48c8712019-03-11 13:38:16 -0700201 std::string chlo_hash_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500202
203 // Pointer to the helper for this crypto stream. Must outlive this stream.
nharper5f23a2d2020-02-20 10:44:09 -0800204 QuicCryptoServerStreamBase::Helper* helper_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500205
206 // Number of handshake messages received by this stream.
207 uint8_t num_handshake_messages_;
208
209 // Number of handshake messages received by this stream that contain
210 // server nonces (indicating that this is a non-zero-RTT handshake
211 // attempt).
212 uint8_t num_handshake_messages_with_server_nonces_;
213
214 // Pointer to the active callback that will receive the result of
215 // BuildServerConfigUpdateMessage and forward it to
216 // FinishSendServerConfigUpdate. nullptr if no update message is currently
217 // being built.
218 SendServerConfigUpdateCallback* send_server_config_update_cb_;
219
220 // Number of server config update (SCUP) messages sent by this stream.
221 int num_server_config_update_messages_sent_;
222
223 // If the client provides CachedNetworkParameters in the STK in the CHLO, then
224 // store here, and send back in future STKs if we have no better bandwidth
225 // estimate to send.
226 std::unique_ptr<CachedNetworkParameters> previous_cached_network_params_;
227
228 // Contains any source address tokens which were present in the CHLO.
229 SourceAddressTokens previous_source_address_tokens_;
230
wub4ca3d672019-06-12 08:27:17 -0700231 // True if client attempts 0-rtt handshake (which can succeed or fail).
QUICHE teama6ef0a62019-03-07 20:34:33 -0500232 bool zero_rtt_attempted_;
233
234 // Size of the packet containing the most recently received CHLO.
235 QuicByteCount chlo_packet_size_;
236
237 // Pointer to the active callback that will receive the result of the client
238 // hello validation request and forward it to FinishProcessingHandshakeMessage
239 // for processing. nullptr if no handshake message is being validated. Note
240 // that this field is mutually exclusive with process_client_hello_cb_.
241 ValidateCallback* validate_client_hello_cb_;
242
243 // Pointer to the active callback which will receive the results of
244 // ProcessClientHello and forward it to
245 // FinishProcessingHandshakeMessageAfterProcessClientHello. Note that this
246 // field is mutually exclusive with validate_client_hello_cb_.
247 ProcessClientHelloCallback* process_client_hello_cb_;
248
nharper9b0a1af2020-08-07 17:11:29 -0700249 // The ProofSource::Details from this connection.
250 std::unique_ptr<ProofSource::Details> proof_source_details_;
251
QUICHE teama6ef0a62019-03-07 20:34:33 -0500252 bool encryption_established_;
fayang685367a2020-01-14 10:40:15 -0800253 bool one_rtt_keys_available_;
fayang9a863cf2020-01-16 14:12:11 -0800254 bool one_rtt_packet_decrypted_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500255 QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters>
256 crypto_negotiated_params_;
257};
258
259} // namespace quic
260
nharper21a32492020-02-28 12:53:17 -0800261#endif // QUICHE_QUIC_CORE_QUIC_CRYPTO_SERVER_STREAM_H_