QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // 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 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 5 | #include "net/third_party/quiche/src/quic/core/quic_crypto_server_stream.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6 | |
| 7 | #include <memory> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 8 | #include <string> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9 | |
vasilvv | bed67c6 | 2020-10-20 06:38:43 -0700 | [diff] [blame] | 10 | #include "absl/base/macros.h" |
vasilvv | c872ee4 | 2020-10-07 19:50:22 -0700 | [diff] [blame] | 11 | #include "absl/strings/string_view.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 12 | #include "third_party/boringssl/src/include/openssl/sha.h" |
wub | be634b7 | 2020-06-16 08:41:26 -0700 | [diff] [blame] | 13 | #include "net/third_party/quiche/src/quic/platform/api/quic_flag_utils.h" |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 14 | #include "net/third_party/quiche/src/common/platform/api/quiche_text_utils.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 15 | |
| 16 | namespace quic { |
| 17 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 18 | class QuicCryptoServerStream::ProcessClientHelloCallback |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 19 | : public ProcessClientHelloResultCallback { |
| 20 | public: |
| 21 | ProcessClientHelloCallback( |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 22 | QuicCryptoServerStream* parent, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 23 | const QuicReferenceCountedPointer< |
| 24 | ValidateClientHelloResultCallback::Result>& result) |
| 25 | : parent_(parent), result_(result) {} |
| 26 | |
| 27 | void Run( |
| 28 | QuicErrorCode error, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 29 | const std::string& error_details, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 30 | std::unique_ptr<CryptoHandshakeMessage> message, |
| 31 | std::unique_ptr<DiversificationNonce> diversification_nonce, |
| 32 | std::unique_ptr<ProofSource::Details> proof_source_details) override { |
| 33 | if (parent_ == nullptr) { |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | parent_->FinishProcessingHandshakeMessageAfterProcessClientHello( |
| 38 | *result_, error, error_details, std::move(message), |
| 39 | std::move(diversification_nonce), std::move(proof_source_details)); |
| 40 | } |
| 41 | |
| 42 | void Cancel() { parent_ = nullptr; } |
| 43 | |
| 44 | private: |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 45 | QuicCryptoServerStream* parent_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 46 | QuicReferenceCountedPointer<ValidateClientHelloResultCallback::Result> |
| 47 | result_; |
| 48 | }; |
| 49 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 50 | QuicCryptoServerStream::QuicCryptoServerStream( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 51 | const QuicCryptoServerConfig* crypto_config, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 52 | QuicCompressedCertsCache* compressed_certs_cache, |
| 53 | QuicSession* session, |
nharper | 5f23a2d | 2020-02-20 10:44:09 -0800 | [diff] [blame] | 54 | QuicCryptoServerStreamBase::Helper* helper) |
nharper | bb7d1f3 | 2020-02-21 17:20:33 -0800 | [diff] [blame] | 55 | : QuicCryptoServerStreamBase(session), |
| 56 | QuicCryptoHandshaker(this, session), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 57 | session_(session), |
fayang | d58736d | 2019-11-27 13:35:31 -0800 | [diff] [blame] | 58 | delegate_(session), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 59 | crypto_config_(crypto_config), |
| 60 | compressed_certs_cache_(compressed_certs_cache), |
| 61 | signed_config_(new QuicSignedServerConfig), |
| 62 | helper_(helper), |
| 63 | num_handshake_messages_(0), |
| 64 | num_handshake_messages_with_server_nonces_(0), |
| 65 | send_server_config_update_cb_(nullptr), |
| 66 | num_server_config_update_messages_sent_(0), |
| 67 | zero_rtt_attempted_(false), |
| 68 | chlo_packet_size_(0), |
| 69 | validate_client_hello_cb_(nullptr), |
| 70 | process_client_hello_cb_(nullptr), |
| 71 | encryption_established_(false), |
fayang | 685367a | 2020-01-14 10:40:15 -0800 | [diff] [blame] | 72 | one_rtt_keys_available_(false), |
fayang | 9a863cf | 2020-01-16 14:12:11 -0800 | [diff] [blame] | 73 | one_rtt_packet_decrypted_(false), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 74 | crypto_negotiated_params_(new QuicCryptoNegotiatedParameters) {} |
| 75 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 76 | QuicCryptoServerStream::~QuicCryptoServerStream() { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 77 | CancelOutstandingCallbacks(); |
| 78 | } |
| 79 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 80 | void QuicCryptoServerStream::CancelOutstandingCallbacks() { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 81 | // Detach from the validation callback. Calling this multiple times is safe. |
| 82 | if (validate_client_hello_cb_ != nullptr) { |
| 83 | validate_client_hello_cb_->Cancel(); |
| 84 | validate_client_hello_cb_ = nullptr; |
| 85 | } |
| 86 | if (send_server_config_update_cb_ != nullptr) { |
| 87 | send_server_config_update_cb_->Cancel(); |
| 88 | send_server_config_update_cb_ = nullptr; |
| 89 | } |
| 90 | if (process_client_hello_cb_ != nullptr) { |
| 91 | process_client_hello_cb_->Cancel(); |
| 92 | process_client_hello_cb_ = nullptr; |
| 93 | } |
| 94 | } |
| 95 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 96 | void QuicCryptoServerStream::OnHandshakeMessage( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 97 | const CryptoHandshakeMessage& message) { |
| 98 | QuicCryptoHandshaker::OnHandshakeMessage(message); |
| 99 | ++num_handshake_messages_; |
| 100 | chlo_packet_size_ = session()->connection()->GetCurrentPacket().length(); |
| 101 | |
| 102 | // Do not process handshake messages after the handshake is confirmed. |
fayang | 685367a | 2020-01-14 10:40:15 -0800 | [diff] [blame] | 103 | if (one_rtt_keys_available_) { |
nharper | bb7d1f3 | 2020-02-21 17:20:33 -0800 | [diff] [blame] | 104 | OnUnrecoverableError(QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE, |
| 105 | "Unexpected handshake message from client"); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 106 | return; |
| 107 | } |
| 108 | |
| 109 | if (message.tag() != kCHLO) { |
nharper | bb7d1f3 | 2020-02-21 17:20:33 -0800 | [diff] [blame] | 110 | OnUnrecoverableError(QUIC_INVALID_CRYPTO_MESSAGE_TYPE, |
| 111 | "Handshake packet not CHLO"); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 112 | return; |
| 113 | } |
| 114 | |
| 115 | if (validate_client_hello_cb_ != nullptr || |
| 116 | process_client_hello_cb_ != nullptr) { |
| 117 | // Already processing some other handshake message. The protocol |
| 118 | // does not allow for clients to send multiple handshake messages |
| 119 | // before the server has a chance to respond. |
nharper | bb7d1f3 | 2020-02-21 17:20:33 -0800 | [diff] [blame] | 120 | OnUnrecoverableError(QUIC_CRYPTO_MESSAGE_WHILE_VALIDATING_CLIENT_HELLO, |
| 121 | "Unexpected handshake message while processing CHLO"); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 122 | return; |
| 123 | } |
| 124 | |
QUICHE team | 84910bd | 2019-03-15 07:03:40 -0700 | [diff] [blame] | 125 | chlo_hash_ = |
| 126 | CryptoUtils::HashHandshakeMessage(message, Perspective::IS_SERVER); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 127 | |
| 128 | std::unique_ptr<ValidateCallback> cb(new ValidateCallback(this)); |
| 129 | DCHECK(validate_client_hello_cb_ == nullptr); |
| 130 | DCHECK(process_client_hello_cb_ == nullptr); |
| 131 | validate_client_hello_cb_ = cb.get(); |
| 132 | crypto_config_->ValidateClientHello( |
danzh | d1fc591 | 2020-05-01 15:29:04 -0700 | [diff] [blame] | 133 | message, GetClientAddress(), session()->connection()->self_address(), |
| 134 | transport_version(), session()->connection()->clock(), signed_config_, |
| 135 | std::move(cb)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 136 | } |
| 137 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 138 | void QuicCryptoServerStream::FinishProcessingHandshakeMessage( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 139 | QuicReferenceCountedPointer<ValidateClientHelloResultCallback::Result> |
| 140 | result, |
| 141 | std::unique_ptr<ProofSource::Details> details) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 142 | // Clear the callback that got us here. |
| 143 | DCHECK(validate_client_hello_cb_ != nullptr); |
| 144 | DCHECK(process_client_hello_cb_ == nullptr); |
| 145 | validate_client_hello_cb_ = nullptr; |
| 146 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 147 | std::unique_ptr<ProcessClientHelloCallback> cb( |
| 148 | new ProcessClientHelloCallback(this, result)); |
| 149 | process_client_hello_cb_ = cb.get(); |
| 150 | ProcessClientHello(result, std::move(details), std::move(cb)); |
| 151 | } |
| 152 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 153 | void QuicCryptoServerStream:: |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 154 | FinishProcessingHandshakeMessageAfterProcessClientHello( |
| 155 | const ValidateClientHelloResultCallback::Result& result, |
| 156 | QuicErrorCode error, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 157 | const std::string& error_details, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 158 | std::unique_ptr<CryptoHandshakeMessage> reply, |
| 159 | std::unique_ptr<DiversificationNonce> diversification_nonce, |
nharper | 9b0a1af | 2020-08-07 17:11:29 -0700 | [diff] [blame] | 160 | std::unique_ptr<ProofSource::Details> proof_source_details) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 161 | // Clear the callback that got us here. |
| 162 | DCHECK(process_client_hello_cb_ != nullptr); |
| 163 | DCHECK(validate_client_hello_cb_ == nullptr); |
| 164 | process_client_hello_cb_ = nullptr; |
nharper | 9b0a1af | 2020-08-07 17:11:29 -0700 | [diff] [blame] | 165 | proof_source_details_ = std::move(proof_source_details); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 166 | |
| 167 | const CryptoHandshakeMessage& message = result.client_hello; |
| 168 | if (error != QUIC_NO_ERROR) { |
nharper | bb7d1f3 | 2020-02-21 17:20:33 -0800 | [diff] [blame] | 169 | OnUnrecoverableError(error, error_details); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 170 | return; |
| 171 | } |
| 172 | |
| 173 | if (reply->tag() != kSHLO) { |
nharper | 3907ac2 | 2019-09-25 15:32:28 -0700 | [diff] [blame] | 174 | session()->connection()->set_fully_pad_crypto_handshake_packets( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 175 | crypto_config_->pad_rej()); |
fayang | 9c41f8b | 2020-10-30 13:13:06 -0700 | [diff] [blame] | 176 | // Send REJ in plaintext. |
| 177 | SendHandshakeMessage(*reply, ENCRYPTION_INITIAL); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 178 | return; |
| 179 | } |
| 180 | |
| 181 | // If we are returning a SHLO then we accepted the handshake. Now |
| 182 | // process the negotiated configuration options as part of the |
| 183 | // session config. |
| 184 | QuicConfig* config = session()->config(); |
| 185 | OverrideQuicConfigDefaults(config); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 186 | std::string process_error_details; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 187 | const QuicErrorCode process_error = |
| 188 | config->ProcessPeerHello(message, CLIENT, &process_error_details); |
| 189 | if (process_error != QUIC_NO_ERROR) { |
nharper | bb7d1f3 | 2020-02-21 17:20:33 -0800 | [diff] [blame] | 190 | OnUnrecoverableError(process_error, process_error_details); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 191 | return; |
| 192 | } |
| 193 | |
| 194 | session()->OnConfigNegotiated(); |
| 195 | |
fkastenholz | d3a1de9 | 2019-05-15 07:00:07 -0700 | [diff] [blame] | 196 | config->ToHandshakeMessage(reply.get(), session()->transport_version()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 197 | |
| 198 | // Receiving a full CHLO implies the client is prepared to decrypt with |
| 199 | // the new server write key. We can start to encrypt with the new server |
| 200 | // write key. |
| 201 | // |
| 202 | // NOTE: the SHLO will be encrypted with the new server write key. |
fayang | 3f7bcbe | 2020-02-10 11:08:47 -0800 | [diff] [blame] | 203 | delegate_->OnNewEncryptionKeyAvailable( |
| 204 | ENCRYPTION_ZERO_RTT, |
| 205 | std::move(crypto_negotiated_params_->initial_crypters.encrypter)); |
| 206 | delegate_->OnNewDecryptionKeyAvailable( |
fayang | d96ecda | 2020-02-03 08:45:18 -0800 | [diff] [blame] | 207 | ENCRYPTION_ZERO_RTT, |
| 208 | std::move(crypto_negotiated_params_->initial_crypters.decrypter), |
| 209 | /*set_alternative_decrypter=*/false, |
fayang | 3f7bcbe | 2020-02-10 11:08:47 -0800 | [diff] [blame] | 210 | /*latch_once_used=*/false); |
fayang | d96ecda | 2020-02-03 08:45:18 -0800 | [diff] [blame] | 211 | delegate_->SetDefaultEncryptionLevel(ENCRYPTION_ZERO_RTT); |
| 212 | delegate_->DiscardOldDecryptionKey(ENCRYPTION_INITIAL); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 213 | session()->connection()->SetDiversificationNonce(*diversification_nonce); |
| 214 | |
nharper | 3907ac2 | 2019-09-25 15:32:28 -0700 | [diff] [blame] | 215 | session()->connection()->set_fully_pad_crypto_handshake_packets( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 216 | crypto_config_->pad_shlo()); |
fayang | 9c41f8b | 2020-10-30 13:13:06 -0700 | [diff] [blame] | 217 | // Send SHLO in ENCRYPTION_ZERO_RTT. |
| 218 | SendHandshakeMessage(*reply, ENCRYPTION_ZERO_RTT); |
fayang | 3f7bcbe | 2020-02-10 11:08:47 -0800 | [diff] [blame] | 219 | delegate_->OnNewEncryptionKeyAvailable( |
| 220 | ENCRYPTION_FORWARD_SECURE, |
| 221 | std::move(crypto_negotiated_params_->forward_secure_crypters.encrypter)); |
| 222 | delegate_->OnNewDecryptionKeyAvailable( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 223 | ENCRYPTION_FORWARD_SECURE, |
fayang | d96ecda | 2020-02-03 08:45:18 -0800 | [diff] [blame] | 224 | std::move(crypto_negotiated_params_->forward_secure_crypters.decrypter), |
| 225 | /*set_alternative_decrypter=*/true, |
fayang | 3f7bcbe | 2020-02-10 11:08:47 -0800 | [diff] [blame] | 226 | /*latch_once_used=*/false); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 227 | encryption_established_ = true; |
fayang | 685367a | 2020-01-14 10:40:15 -0800 | [diff] [blame] | 228 | one_rtt_keys_available_ = true; |
fayang | d96ecda | 2020-02-03 08:45:18 -0800 | [diff] [blame] | 229 | delegate_->SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 230 | delegate_->DiscardOldEncryptionKey(ENCRYPTION_INITIAL); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 231 | } |
| 232 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 233 | void QuicCryptoServerStream::SendServerConfigUpdate( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 234 | const CachedNetworkParameters* cached_network_params) { |
fayang | 685367a | 2020-01-14 10:40:15 -0800 | [diff] [blame] | 235 | if (!one_rtt_keys_available_) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 236 | return; |
| 237 | } |
| 238 | |
| 239 | if (send_server_config_update_cb_ != nullptr) { |
| 240 | QUIC_DVLOG(1) |
| 241 | << "Skipped server config update since one is already in progress"; |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | std::unique_ptr<SendServerConfigUpdateCallback> cb( |
| 246 | new SendServerConfigUpdateCallback(this)); |
| 247 | send_server_config_update_cb_ = cb.get(); |
| 248 | |
| 249 | crypto_config_->BuildServerConfigUpdateMessage( |
renjietang | d1d0085 | 2019-09-06 10:43:12 -0700 | [diff] [blame] | 250 | session()->transport_version(), chlo_hash_, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 251 | previous_source_address_tokens_, session()->connection()->self_address(), |
danzh | d1fc591 | 2020-05-01 15:29:04 -0700 | [diff] [blame] | 252 | GetClientAddress(), session()->connection()->clock(), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 253 | session()->connection()->random_generator(), compressed_certs_cache_, |
| 254 | *crypto_negotiated_params_, cached_network_params, std::move(cb)); |
| 255 | } |
| 256 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 257 | QuicCryptoServerStream::SendServerConfigUpdateCallback:: |
| 258 | SendServerConfigUpdateCallback(QuicCryptoServerStream* parent) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 259 | : parent_(parent) {} |
| 260 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 261 | void QuicCryptoServerStream::SendServerConfigUpdateCallback::Cancel() { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 262 | parent_ = nullptr; |
| 263 | } |
| 264 | |
| 265 | // From BuildServerConfigUpdateMessageResultCallback |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 266 | void QuicCryptoServerStream::SendServerConfigUpdateCallback::Run( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 267 | bool ok, |
| 268 | const CryptoHandshakeMessage& message) { |
| 269 | if (parent_ == nullptr) { |
| 270 | return; |
| 271 | } |
| 272 | parent_->FinishSendServerConfigUpdate(ok, message); |
| 273 | } |
| 274 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 275 | void QuicCryptoServerStream::FinishSendServerConfigUpdate( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 276 | bool ok, |
| 277 | const CryptoHandshakeMessage& message) { |
| 278 | // Clear the callback that got us here. |
| 279 | DCHECK(send_server_config_update_cb_ != nullptr); |
| 280 | send_server_config_update_cb_ = nullptr; |
| 281 | |
| 282 | if (!ok) { |
| 283 | QUIC_DVLOG(1) << "Server: Failed to build server config update (SCUP)!"; |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | QUIC_DVLOG(1) << "Server: Sending server config update: " |
| 288 | << message.DebugString(); |
fayang | 9c41f8b | 2020-10-30 13:13:06 -0700 | [diff] [blame] | 289 | |
| 290 | if (!session()->use_write_or_buffer_data_at_level() && |
| 291 | !QuicVersionUsesCryptoFrames(transport_version())) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 292 | const QuicData& data = message.GetSerialized(); |
vasilvv | c872ee4 | 2020-10-07 19:50:22 -0700 | [diff] [blame] | 293 | WriteOrBufferData(absl::string_view(data.data(), data.length()), false, |
| 294 | nullptr); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 295 | } else { |
fayang | 9c41f8b | 2020-10-30 13:13:06 -0700 | [diff] [blame] | 296 | // Send server config update in ENCRYPTION_FORWARD_SECURE. |
| 297 | SendHandshakeMessage(message, ENCRYPTION_FORWARD_SECURE); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | ++num_server_config_update_messages_sent_; |
| 301 | } |
| 302 | |
fayang | 6098a0a | 2020-03-13 15:32:10 -0700 | [diff] [blame] | 303 | bool QuicCryptoServerStream::IsZeroRtt() const { |
| 304 | return num_handshake_messages_ == 1 && |
| 305 | num_handshake_messages_with_server_nonces_ == 0; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 306 | } |
| 307 | |
nharper | fd0e263 | 2020-06-02 11:19:05 -0700 | [diff] [blame] | 308 | bool QuicCryptoServerStream::IsResumption() const { |
| 309 | // QUIC Crypto doesn't have a non-0-RTT resumption mode. |
| 310 | return IsZeroRtt(); |
| 311 | } |
| 312 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 313 | int QuicCryptoServerStream::NumServerConfigUpdateMessagesSent() const { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 314 | return num_server_config_update_messages_sent_; |
| 315 | } |
| 316 | |
| 317 | const CachedNetworkParameters* |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 318 | QuicCryptoServerStream::PreviousCachedNetworkParams() const { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 319 | return previous_cached_network_params_.get(); |
| 320 | } |
| 321 | |
nharper | fd0e263 | 2020-06-02 11:19:05 -0700 | [diff] [blame] | 322 | bool QuicCryptoServerStream::ResumptionAttempted() const { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 323 | return zero_rtt_attempted_; |
| 324 | } |
| 325 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 326 | void QuicCryptoServerStream::SetPreviousCachedNetworkParams( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 327 | CachedNetworkParameters cached_network_params) { |
| 328 | previous_cached_network_params_.reset( |
| 329 | new CachedNetworkParameters(cached_network_params)); |
| 330 | } |
| 331 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 332 | void QuicCryptoServerStream::OnPacketDecrypted(EncryptionLevel level) { |
fayang | d58736d | 2019-11-27 13:35:31 -0800 | [diff] [blame] | 333 | if (level == ENCRYPTION_FORWARD_SECURE) { |
fayang | 9a863cf | 2020-01-16 14:12:11 -0800 | [diff] [blame] | 334 | one_rtt_packet_decrypted_ = true; |
fayang | d58736d | 2019-11-27 13:35:31 -0800 | [diff] [blame] | 335 | delegate_->NeuterHandshakeData(); |
| 336 | } |
| 337 | } |
| 338 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 339 | void QuicCryptoServerStream::OnHandshakeDoneReceived() { |
nharper | bb7d1f3 | 2020-02-21 17:20:33 -0800 | [diff] [blame] | 340 | DCHECK(false); |
| 341 | } |
| 342 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 343 | bool QuicCryptoServerStream::ShouldSendExpectCTHeader() const { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 344 | return signed_config_->proof.send_expect_ct_header; |
| 345 | } |
| 346 | |
nharper | 9b0a1af | 2020-08-07 17:11:29 -0700 | [diff] [blame] | 347 | const ProofSource::Details* QuicCryptoServerStream::ProofSourceDetails() const { |
| 348 | return proof_source_details_.get(); |
| 349 | } |
| 350 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 351 | bool QuicCryptoServerStream::GetBase64SHA256ClientChannelID( |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 352 | std::string* output) const { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 353 | if (!encryption_established() || |
| 354 | crypto_negotiated_params_->channel_id.empty()) { |
| 355 | return false; |
| 356 | } |
| 357 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 358 | const std::string& channel_id(crypto_negotiated_params_->channel_id); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 359 | uint8_t digest[SHA256_DIGEST_LENGTH]; |
| 360 | SHA256(reinterpret_cast<const uint8_t*>(channel_id.data()), channel_id.size(), |
| 361 | digest); |
| 362 | |
vasilvv | bed67c6 | 2020-10-20 06:38:43 -0700 | [diff] [blame] | 363 | quiche::QuicheTextUtils::Base64Encode(digest, ABSL_ARRAYSIZE(digest), output); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 364 | return true; |
| 365 | } |
| 366 | |
nharper | 6a6bd31 | 2020-09-17 13:20:53 -0700 | [diff] [blame] | 367 | ssl_early_data_reason_t QuicCryptoServerStream::EarlyDataReason() const { |
| 368 | if (IsZeroRtt()) { |
| 369 | return ssl_early_data_accepted; |
| 370 | } |
| 371 | if (zero_rtt_attempted_) { |
| 372 | return ssl_early_data_session_not_resumed; |
| 373 | } |
| 374 | return ssl_early_data_no_session_offered; |
| 375 | } |
| 376 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 377 | bool QuicCryptoServerStream::encryption_established() const { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 378 | return encryption_established_; |
| 379 | } |
| 380 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 381 | bool QuicCryptoServerStream::one_rtt_keys_available() const { |
fayang | 685367a | 2020-01-14 10:40:15 -0800 | [diff] [blame] | 382 | return one_rtt_keys_available_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | const QuicCryptoNegotiatedParameters& |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 386 | QuicCryptoServerStream::crypto_negotiated_params() const { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 387 | return *crypto_negotiated_params_; |
| 388 | } |
| 389 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 390 | CryptoMessageParser* QuicCryptoServerStream::crypto_message_parser() { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 391 | return QuicCryptoHandshaker::crypto_message_parser(); |
| 392 | } |
| 393 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 394 | HandshakeState QuicCryptoServerStream::GetHandshakeState() const { |
fayang | 9a863cf | 2020-01-16 14:12:11 -0800 | [diff] [blame] | 395 | return one_rtt_packet_decrypted_ ? HANDSHAKE_COMPLETE : HANDSHAKE_START; |
| 396 | } |
| 397 | |
nharper | ac52a86 | 2020-06-08 12:41:06 -0700 | [diff] [blame] | 398 | void QuicCryptoServerStream::SetServerApplicationStateForResumption( |
| 399 | std::unique_ptr<ApplicationState> /*state*/) { |
| 400 | // QUIC Crypto doesn't need to remember any application state as part of doing |
| 401 | // 0-RTT resumption, so this function is a no-op. |
| 402 | } |
| 403 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 404 | size_t QuicCryptoServerStream::BufferSizeLimitForLevel( |
nharper | 486a8a9 | 2019-08-28 16:25:10 -0700 | [diff] [blame] | 405 | EncryptionLevel level) const { |
| 406 | return QuicCryptoHandshaker::BufferSizeLimitForLevel(level); |
| 407 | } |
| 408 | |
mattm | 072a7e3 | 2020-10-09 16:16:56 -0700 | [diff] [blame] | 409 | bool QuicCryptoServerStream::KeyUpdateSupportedLocally() const { |
| 410 | return false; |
| 411 | } |
| 412 | |
| 413 | std::unique_ptr<QuicDecrypter> |
| 414 | QuicCryptoServerStream::AdvanceKeysAndCreateCurrentOneRttDecrypter() { |
| 415 | // Key update is only defined in QUIC+TLS. |
| 416 | DCHECK(false); |
| 417 | return nullptr; |
| 418 | } |
| 419 | |
| 420 | std::unique_ptr<QuicEncrypter> |
| 421 | QuicCryptoServerStream::CreateCurrentOneRttEncrypter() { |
| 422 | // Key update is only defined in QUIC+TLS. |
| 423 | DCHECK(false); |
| 424 | return nullptr; |
| 425 | } |
| 426 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 427 | void QuicCryptoServerStream::ProcessClientHello( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 428 | QuicReferenceCountedPointer<ValidateClientHelloResultCallback::Result> |
| 429 | result, |
nharper | 9b0a1af | 2020-08-07 17:11:29 -0700 | [diff] [blame] | 430 | std::unique_ptr<ProofSource::Details> proof_source_details, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 431 | std::unique_ptr<ProcessClientHelloResultCallback> done_cb) { |
nharper | 9b0a1af | 2020-08-07 17:11:29 -0700 | [diff] [blame] | 432 | proof_source_details_ = std::move(proof_source_details); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 433 | const CryptoHandshakeMessage& message = result->client_hello; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 434 | std::string error_details; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 435 | if (!helper_->CanAcceptClientHello( |
| 436 | message, GetClientAddress(), session()->connection()->peer_address(), |
| 437 | session()->connection()->self_address(), &error_details)) { |
| 438 | done_cb->Run(QUIC_HANDSHAKE_FAILED, error_details, nullptr, nullptr, |
| 439 | nullptr); |
| 440 | return; |
| 441 | } |
wub | be634b7 | 2020-06-16 08:41:26 -0700 | [diff] [blame] | 442 | |
vasilvv | c872ee4 | 2020-10-07 19:50:22 -0700 | [diff] [blame] | 443 | absl::string_view user_agent_id; |
wub | 13ebfa6 | 2020-08-10 07:48:24 -0700 | [diff] [blame] | 444 | message.GetStringPiece(quic::kUAID, &user_agent_id); |
wub | 1abb2a1 | 2020-08-14 18:06:28 -0700 | [diff] [blame] | 445 | if (!session()->user_agent_id().has_value() && !user_agent_id.empty()) { |
| 446 | session()->SetUserAgentId(std::string(user_agent_id)); |
wub | be634b7 | 2020-06-16 08:41:26 -0700 | [diff] [blame] | 447 | } |
| 448 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 449 | if (!result->info.server_nonce.empty()) { |
| 450 | ++num_handshake_messages_with_server_nonces_; |
| 451 | } |
| 452 | |
| 453 | if (num_handshake_messages_ == 1) { |
| 454 | // Client attempts zero RTT handshake by sending a non-inchoate CHLO. |
vasilvv | c872ee4 | 2020-10-07 19:50:22 -0700 | [diff] [blame] | 455 | absl::string_view public_value; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 456 | zero_rtt_attempted_ = message.GetStringPiece(kPUBS, &public_value); |
| 457 | } |
| 458 | |
| 459 | // Store the bandwidth estimate from the client. |
| 460 | if (result->cached_network_params.bandwidth_estimate_bytes_per_second() > 0) { |
| 461 | previous_cached_network_params_.reset( |
| 462 | new CachedNetworkParameters(result->cached_network_params)); |
| 463 | } |
| 464 | previous_source_address_tokens_ = result->info.source_address_tokens; |
| 465 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 466 | QuicConnection* connection = session()->connection(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 467 | crypto_config_->ProcessClientHello( |
| 468 | result, /*reject_only=*/false, connection->connection_id(), |
| 469 | connection->self_address(), GetClientAddress(), connection->version(), |
wub | ecf9bd8 | 2019-06-05 04:57:02 -0700 | [diff] [blame] | 470 | session()->supported_versions(), connection->clock(), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 471 | connection->random_generator(), compressed_certs_cache_, |
| 472 | crypto_negotiated_params_, signed_config_, |
| 473 | QuicCryptoStream::CryptoMessageFramingOverhead( |
| 474 | transport_version(), connection->connection_id()), |
| 475 | chlo_packet_size_, std::move(done_cb)); |
| 476 | } |
| 477 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 478 | void QuicCryptoServerStream::OverrideQuicConfigDefaults( |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 479 | QuicConfig* /*config*/) {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 480 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 481 | QuicCryptoServerStream::ValidateCallback::ValidateCallback( |
| 482 | QuicCryptoServerStream* parent) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 483 | : parent_(parent) {} |
| 484 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 485 | void QuicCryptoServerStream::ValidateCallback::Cancel() { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 486 | parent_ = nullptr; |
| 487 | } |
| 488 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 489 | void QuicCryptoServerStream::ValidateCallback::Run( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 490 | QuicReferenceCountedPointer<Result> result, |
| 491 | std::unique_ptr<ProofSource::Details> details) { |
| 492 | if (parent_ != nullptr) { |
| 493 | parent_->FinishProcessingHandshakeMessage(std::move(result), |
| 494 | std::move(details)); |
| 495 | } |
| 496 | } |
| 497 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 498 | const QuicSocketAddress QuicCryptoServerStream::GetClientAddress() { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 499 | return session()->connection()->peer_address(); |
| 500 | } |
| 501 | |
| 502 | } // namespace quic |