blob: 2dc04a47d92435b3985476e8115f71f1e56c89a7 [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;
37 uint8_t NumHandshakeMessages() const override;
38 uint8_t NumHandshakeMessagesWithServerNonces() const override;
39 int NumServerConfigUpdateMessagesSent() const override;
40 const CachedNetworkParameters* PreviousCachedNetworkParams() const override;
41 bool ZeroRttAttempted() const override;
42 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 {}
46 void OnHandshakeDoneReceived() override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050047 bool ShouldSendExpectCTHeader() const override;
48
49 // From QuicCryptoStream
50 bool encryption_established() const override;
fayang685367a2020-01-14 10:40:15 -080051 bool one_rtt_keys_available() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050052 const QuicCryptoNegotiatedParameters& crypto_negotiated_params()
53 const override;
54 CryptoMessageParser* crypto_message_parser() override;
fayang9a863cf2020-01-16 14:12:11 -080055 HandshakeState GetHandshakeState() const override;
nharper486a8a92019-08-28 16:25:10 -070056 size_t BufferSizeLimitForLevel(EncryptionLevel level) const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050057
58 // From QuicCryptoHandshaker
59 void OnHandshakeMessage(const CryptoHandshakeMessage& message) override;
60
61 protected:
nharperbb7d1f32020-02-21 17:20:33 -080062 QUIC_EXPORT_PRIVATE friend std::unique_ptr<QuicCryptoServerStreamBase>
63 CreateCryptoServerStream(const QuicCryptoServerConfig* crypto_config,
64 QuicCompressedCertsCache* compressed_certs_cache,
65 QuicSession* session,
66 QuicCryptoServerStreamBase::Helper* helper);
67
68 // |crypto_config| must outlive the stream.
69 // |session| must outlive the stream.
70 // |helper| must outlive the stream.
nharper21a32492020-02-28 12:53:17 -080071 QuicCryptoServerStream(const QuicCryptoServerConfig* crypto_config,
72 QuicCompressedCertsCache* compressed_certs_cache,
73 QuicSession* session,
74 QuicCryptoServerStreamBase::Helper* helper);
nharperbb7d1f32020-02-21 17:20:33 -080075
QUICHE teama6ef0a62019-03-07 20:34:33 -050076 virtual void ProcessClientHello(
77 QuicReferenceCountedPointer<ValidateClientHelloResultCallback::Result>
78 result,
79 std::unique_ptr<ProofSource::Details> proof_source_details,
80 std::unique_ptr<ProcessClientHelloResultCallback> done_cb);
81
82 // Hook that allows the server to set QuicConfig defaults just
83 // before going through the parameter negotiation step.
84 virtual void OverrideQuicConfigDefaults(QuicConfig* config);
85
86 // Returns client address used to generate and validate source address token.
87 virtual const QuicSocketAddress GetClientAddress();
88
89 // Returns the QuicSession that this stream belongs to.
90 QuicSession* session() const { return session_; }
91
92 void set_encryption_established(bool encryption_established) {
93 encryption_established_ = encryption_established;
94 }
95
fayang685367a2020-01-14 10:40:15 -080096 void set_one_rtt_keys_available(bool one_rtt_keys_available) {
97 one_rtt_keys_available_ = one_rtt_keys_available;
QUICHE teama6ef0a62019-03-07 20:34:33 -050098 }
99
100 private:
101 friend class test::QuicCryptoServerStreamPeer;
102
dschinazif25169a2019-10-23 08:12:18 -0700103 class QUIC_EXPORT_PRIVATE ValidateCallback
104 : public ValidateClientHelloResultCallback {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500105 public:
nharper21a32492020-02-28 12:53:17 -0800106 explicit ValidateCallback(QuicCryptoServerStream* parent);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500107 ValidateCallback(const ValidateCallback&) = delete;
108 ValidateCallback& operator=(const ValidateCallback&) = delete;
109 // To allow the parent to detach itself from the callback before deletion.
110 void Cancel();
111
112 // From ValidateClientHelloResultCallback
113 void Run(QuicReferenceCountedPointer<Result> result,
114 std::unique_ptr<ProofSource::Details> details) override;
115
116 private:
nharper21a32492020-02-28 12:53:17 -0800117 QuicCryptoServerStream* parent_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500118 };
119
120 class SendServerConfigUpdateCallback
121 : public BuildServerConfigUpdateMessageResultCallback {
122 public:
nharper21a32492020-02-28 12:53:17 -0800123 explicit SendServerConfigUpdateCallback(QuicCryptoServerStream* parent);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500124 SendServerConfigUpdateCallback(const SendServerConfigUpdateCallback&) =
125 delete;
126 void operator=(const SendServerConfigUpdateCallback&) = delete;
127
128 // To allow the parent to detach itself from the callback before deletion.
129 void Cancel();
130
131 // From BuildServerConfigUpdateMessageResultCallback
132 void Run(bool ok, const CryptoHandshakeMessage& message) override;
133
134 private:
nharper21a32492020-02-28 12:53:17 -0800135 QuicCryptoServerStream* parent_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500136 };
137
138 // Invoked by ValidateCallback::RunImpl once initial validation of
139 // the client hello is complete. Finishes processing of the client
140 // hello message and handles handshake success/failure.
141 void FinishProcessingHandshakeMessage(
142 QuicReferenceCountedPointer<ValidateClientHelloResultCallback::Result>
143 result,
144 std::unique_ptr<ProofSource::Details> details);
145
146 class ProcessClientHelloCallback;
147 friend class ProcessClientHelloCallback;
148
149 // Portion of FinishProcessingHandshakeMessage which executes after
150 // ProcessClientHello has been called.
151 void FinishProcessingHandshakeMessageAfterProcessClientHello(
152 const ValidateClientHelloResultCallback::Result& result,
153 QuicErrorCode error,
vasilvvc48c8712019-03-11 13:38:16 -0700154 const std::string& error_details,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500155 std::unique_ptr<CryptoHandshakeMessage> reply,
156 std::unique_ptr<DiversificationNonce> diversification_nonce,
157 std::unique_ptr<ProofSource::Details> proof_source_details);
158
159 // Invoked by SendServerConfigUpdateCallback::RunImpl once the proof has been
160 // received. |ok| indicates whether or not the proof was successfully
161 // acquired, and |message| holds the partially-constructed message from
162 // SendServerConfigUpdate.
163 void FinishSendServerConfigUpdate(bool ok,
164 const CryptoHandshakeMessage& message);
165
QUICHE teama6ef0a62019-03-07 20:34:33 -0500166 // Returns the QuicTransportVersion of the connection.
167 QuicTransportVersion transport_version() const {
renjietangd1d00852019-09-06 10:43:12 -0700168 return session_->transport_version();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500169 }
170
QUICHE teama6ef0a62019-03-07 20:34:33 -0500171 QuicSession* session_;
fayangd58736d2019-11-27 13:35:31 -0800172 HandshakerDelegateInterface* delegate_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500173
174 // crypto_config_ contains crypto parameters for the handshake.
175 const QuicCryptoServerConfig* crypto_config_;
176
177 // compressed_certs_cache_ contains a set of most recently compressed certs.
178 // Owned by QuicDispatcher.
179 QuicCompressedCertsCache* compressed_certs_cache_;
180
181 // Server's certificate chain and signature of the server config, as provided
182 // by ProofSource::GetProof.
183 QuicReferenceCountedPointer<QuicSignedServerConfig> signed_config_;
184
185 // Hash of the last received CHLO message which can be used for generating
186 // server config update messages.
vasilvvc48c8712019-03-11 13:38:16 -0700187 std::string chlo_hash_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500188
189 // Pointer to the helper for this crypto stream. Must outlive this stream.
nharper5f23a2d2020-02-20 10:44:09 -0800190 QuicCryptoServerStreamBase::Helper* helper_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500191
192 // Number of handshake messages received by this stream.
193 uint8_t num_handshake_messages_;
194
195 // Number of handshake messages received by this stream that contain
196 // server nonces (indicating that this is a non-zero-RTT handshake
197 // attempt).
198 uint8_t num_handshake_messages_with_server_nonces_;
199
200 // Pointer to the active callback that will receive the result of
201 // BuildServerConfigUpdateMessage and forward it to
202 // FinishSendServerConfigUpdate. nullptr if no update message is currently
203 // being built.
204 SendServerConfigUpdateCallback* send_server_config_update_cb_;
205
206 // Number of server config update (SCUP) messages sent by this stream.
207 int num_server_config_update_messages_sent_;
208
209 // If the client provides CachedNetworkParameters in the STK in the CHLO, then
210 // store here, and send back in future STKs if we have no better bandwidth
211 // estimate to send.
212 std::unique_ptr<CachedNetworkParameters> previous_cached_network_params_;
213
214 // Contains any source address tokens which were present in the CHLO.
215 SourceAddressTokens previous_source_address_tokens_;
216
wub4ca3d672019-06-12 08:27:17 -0700217 // True if client attempts 0-rtt handshake (which can succeed or fail).
QUICHE teama6ef0a62019-03-07 20:34:33 -0500218 bool zero_rtt_attempted_;
219
220 // Size of the packet containing the most recently received CHLO.
221 QuicByteCount chlo_packet_size_;
222
223 // Pointer to the active callback that will receive the result of the client
224 // hello validation request and forward it to FinishProcessingHandshakeMessage
225 // for processing. nullptr if no handshake message is being validated. Note
226 // that this field is mutually exclusive with process_client_hello_cb_.
227 ValidateCallback* validate_client_hello_cb_;
228
229 // Pointer to the active callback which will receive the results of
230 // ProcessClientHello and forward it to
231 // FinishProcessingHandshakeMessageAfterProcessClientHello. Note that this
232 // field is mutually exclusive with validate_client_hello_cb_.
233 ProcessClientHelloCallback* process_client_hello_cb_;
234
235 bool encryption_established_;
fayang685367a2020-01-14 10:40:15 -0800236 bool one_rtt_keys_available_;
fayang9a863cf2020-01-16 14:12:11 -0800237 bool one_rtt_packet_decrypted_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500238 QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters>
239 crypto_negotiated_params_;
240};
241
242} // namespace quic
243
nharper21a32492020-02-28 12:53:17 -0800244#endif // QUICHE_QUIC_CORE_QUIC_CRYPTO_SERVER_STREAM_H_