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 | |
| 5 | #include "net/third_party/quiche/src/quic/core/quic_crypto_client_handshaker.h" |
| 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 | |
| 10 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_protocol.h" |
| 11 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_utils.h" |
| 12 | #include "net/third_party/quiche/src/quic/core/quic_session.h" |
| 13 | #include "net/third_party/quiche/src/quic/platform/api/quic_client_stats.h" |
| 14 | #include "net/third_party/quiche/src/quic/platform/api/quic_flags.h" |
| 15 | #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h" |
| 16 | #include "net/third_party/quiche/src/quic/platform/api/quic_str_cat.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 17 | |
| 18 | namespace quic { |
| 19 | |
| 20 | QuicCryptoClientHandshaker::ChannelIDSourceCallbackImpl:: |
| 21 | ChannelIDSourceCallbackImpl(QuicCryptoClientHandshaker* parent) |
| 22 | : parent_(parent) {} |
| 23 | |
| 24 | QuicCryptoClientHandshaker::ChannelIDSourceCallbackImpl:: |
| 25 | ~ChannelIDSourceCallbackImpl() {} |
| 26 | |
| 27 | void QuicCryptoClientHandshaker::ChannelIDSourceCallbackImpl::Run( |
| 28 | std::unique_ptr<ChannelIDKey>* channel_id_key) { |
| 29 | if (parent_ == nullptr) { |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | parent_->channel_id_key_ = std::move(*channel_id_key); |
| 34 | parent_->channel_id_source_callback_run_ = true; |
| 35 | parent_->channel_id_source_callback_ = nullptr; |
| 36 | parent_->DoHandshakeLoop(nullptr); |
| 37 | |
| 38 | // The ChannelIDSource owns this object and will delete it when this method |
| 39 | // returns. |
| 40 | } |
| 41 | |
| 42 | void QuicCryptoClientHandshaker::ChannelIDSourceCallbackImpl::Cancel() { |
| 43 | parent_ = nullptr; |
| 44 | } |
| 45 | |
| 46 | QuicCryptoClientHandshaker::ProofVerifierCallbackImpl:: |
| 47 | ProofVerifierCallbackImpl(QuicCryptoClientHandshaker* parent) |
| 48 | : parent_(parent) {} |
| 49 | |
| 50 | QuicCryptoClientHandshaker::ProofVerifierCallbackImpl:: |
| 51 | ~ProofVerifierCallbackImpl() {} |
| 52 | |
| 53 | void QuicCryptoClientHandshaker::ProofVerifierCallbackImpl::Run( |
| 54 | bool ok, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 55 | const std::string& error_details, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 56 | std::unique_ptr<ProofVerifyDetails>* details) { |
| 57 | if (parent_ == nullptr) { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | parent_->verify_ok_ = ok; |
| 62 | parent_->verify_error_details_ = error_details; |
| 63 | parent_->verify_details_ = std::move(*details); |
| 64 | parent_->proof_verify_callback_ = nullptr; |
| 65 | parent_->DoHandshakeLoop(nullptr); |
| 66 | |
| 67 | // The ProofVerifier owns this object and will delete it when this method |
| 68 | // returns. |
| 69 | } |
| 70 | |
| 71 | void QuicCryptoClientHandshaker::ProofVerifierCallbackImpl::Cancel() { |
| 72 | parent_ = nullptr; |
| 73 | } |
| 74 | |
| 75 | QuicCryptoClientHandshaker::QuicCryptoClientHandshaker( |
| 76 | const QuicServerId& server_id, |
| 77 | QuicCryptoClientStream* stream, |
| 78 | QuicSession* session, |
| 79 | std::unique_ptr<ProofVerifyContext> verify_context, |
| 80 | QuicCryptoClientConfig* crypto_config, |
| 81 | QuicCryptoClientStream::ProofHandler* proof_handler) |
| 82 | : QuicCryptoHandshaker(stream, session), |
| 83 | stream_(stream), |
| 84 | session_(session), |
| 85 | next_state_(STATE_IDLE), |
| 86 | num_client_hellos_(0), |
| 87 | crypto_config_(crypto_config), |
| 88 | server_id_(server_id), |
| 89 | generation_counter_(0), |
| 90 | channel_id_sent_(false), |
| 91 | channel_id_source_callback_run_(false), |
| 92 | channel_id_source_callback_(nullptr), |
| 93 | verify_context_(std::move(verify_context)), |
| 94 | proof_verify_callback_(nullptr), |
| 95 | proof_handler_(proof_handler), |
| 96 | verify_ok_(false), |
| 97 | stateless_reject_received_(false), |
| 98 | proof_verify_start_time_(QuicTime::Zero()), |
| 99 | num_scup_messages_received_(0), |
| 100 | encryption_established_(false), |
| 101 | handshake_confirmed_(false), |
| 102 | crypto_negotiated_params_(new QuicCryptoNegotiatedParameters) {} |
| 103 | |
| 104 | QuicCryptoClientHandshaker::~QuicCryptoClientHandshaker() { |
| 105 | if (channel_id_source_callback_) { |
| 106 | channel_id_source_callback_->Cancel(); |
| 107 | } |
| 108 | if (proof_verify_callback_) { |
| 109 | proof_verify_callback_->Cancel(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void QuicCryptoClientHandshaker::OnHandshakeMessage( |
| 114 | const CryptoHandshakeMessage& message) { |
| 115 | QuicCryptoHandshaker::OnHandshakeMessage(message); |
| 116 | if (message.tag() == kSCUP) { |
| 117 | if (!handshake_confirmed()) { |
| 118 | stream_->CloseConnectionWithDetails( |
| 119 | QUIC_CRYPTO_UPDATE_BEFORE_HANDSHAKE_COMPLETE, |
| 120 | "Early SCUP disallowed"); |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | // |message| is an update from the server, so we treat it differently from a |
| 125 | // handshake message. |
| 126 | HandleServerConfigUpdateMessage(message); |
| 127 | num_scup_messages_received_++; |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | // Do not process handshake messages after the handshake is confirmed. |
| 132 | if (handshake_confirmed()) { |
| 133 | stream_->CloseConnectionWithDetails( |
| 134 | QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE, |
| 135 | "Unexpected handshake message"); |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | DoHandshakeLoop(&message); |
| 140 | } |
| 141 | |
| 142 | bool QuicCryptoClientHandshaker::CryptoConnect() { |
| 143 | next_state_ = STATE_INITIALIZE; |
| 144 | DoHandshakeLoop(nullptr); |
| 145 | return session()->connection()->connected(); |
| 146 | } |
| 147 | |
| 148 | int QuicCryptoClientHandshaker::num_sent_client_hellos() const { |
| 149 | return num_client_hellos_; |
| 150 | } |
| 151 | |
| 152 | int QuicCryptoClientHandshaker::num_scup_messages_received() const { |
| 153 | return num_scup_messages_received_; |
| 154 | } |
| 155 | |
| 156 | bool QuicCryptoClientHandshaker::WasChannelIDSent() const { |
| 157 | return channel_id_sent_; |
| 158 | } |
| 159 | |
| 160 | bool QuicCryptoClientHandshaker::WasChannelIDSourceCallbackRun() const { |
| 161 | return channel_id_source_callback_run_; |
| 162 | } |
| 163 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 164 | std::string QuicCryptoClientHandshaker::chlo_hash() const { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 165 | return chlo_hash_; |
| 166 | } |
| 167 | |
| 168 | bool QuicCryptoClientHandshaker::encryption_established() const { |
| 169 | return encryption_established_; |
| 170 | } |
| 171 | |
| 172 | bool QuicCryptoClientHandshaker::handshake_confirmed() const { |
| 173 | return handshake_confirmed_; |
| 174 | } |
| 175 | |
| 176 | const QuicCryptoNegotiatedParameters& |
| 177 | QuicCryptoClientHandshaker::crypto_negotiated_params() const { |
| 178 | return *crypto_negotiated_params_; |
| 179 | } |
| 180 | |
| 181 | CryptoMessageParser* QuicCryptoClientHandshaker::crypto_message_parser() { |
| 182 | return QuicCryptoHandshaker::crypto_message_parser(); |
| 183 | } |
| 184 | |
| 185 | void QuicCryptoClientHandshaker::HandleServerConfigUpdateMessage( |
| 186 | const CryptoHandshakeMessage& server_config_update) { |
| 187 | DCHECK(server_config_update.tag() == kSCUP); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 188 | std::string error_details; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 189 | QuicCryptoClientConfig::CachedState* cached = |
| 190 | crypto_config_->LookupOrCreate(server_id_); |
| 191 | QuicErrorCode error = crypto_config_->ProcessServerConfigUpdate( |
| 192 | server_config_update, session()->connection()->clock()->WallNow(), |
| 193 | session()->connection()->transport_version(), chlo_hash_, cached, |
| 194 | crypto_negotiated_params_, &error_details); |
| 195 | |
| 196 | if (error != QUIC_NO_ERROR) { |
| 197 | stream_->CloseConnectionWithDetails( |
| 198 | error, "Server config update invalid: " + error_details); |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | DCHECK(handshake_confirmed()); |
| 203 | if (proof_verify_callback_) { |
| 204 | proof_verify_callback_->Cancel(); |
| 205 | } |
| 206 | next_state_ = STATE_INITIALIZE_SCUP; |
| 207 | DoHandshakeLoop(nullptr); |
| 208 | } |
| 209 | |
| 210 | void QuicCryptoClientHandshaker::DoHandshakeLoop( |
| 211 | const CryptoHandshakeMessage* in) { |
| 212 | QuicCryptoClientConfig::CachedState* cached = |
| 213 | crypto_config_->LookupOrCreate(server_id_); |
| 214 | |
| 215 | QuicAsyncStatus rv = QUIC_SUCCESS; |
| 216 | do { |
| 217 | CHECK_NE(STATE_NONE, next_state_); |
| 218 | const State state = next_state_; |
| 219 | next_state_ = STATE_IDLE; |
| 220 | rv = QUIC_SUCCESS; |
| 221 | switch (state) { |
| 222 | case STATE_INITIALIZE: |
| 223 | DoInitialize(cached); |
| 224 | break; |
| 225 | case STATE_SEND_CHLO: |
| 226 | DoSendCHLO(cached); |
| 227 | return; // return waiting to hear from server. |
| 228 | case STATE_RECV_REJ: |
| 229 | DoReceiveREJ(in, cached); |
| 230 | break; |
| 231 | case STATE_VERIFY_PROOF: |
| 232 | rv = DoVerifyProof(cached); |
| 233 | break; |
| 234 | case STATE_VERIFY_PROOF_COMPLETE: |
| 235 | DoVerifyProofComplete(cached); |
| 236 | break; |
| 237 | case STATE_GET_CHANNEL_ID: |
| 238 | rv = DoGetChannelID(cached); |
| 239 | break; |
| 240 | case STATE_GET_CHANNEL_ID_COMPLETE: |
| 241 | DoGetChannelIDComplete(); |
| 242 | break; |
| 243 | case STATE_RECV_SHLO: |
| 244 | DoReceiveSHLO(in, cached); |
| 245 | break; |
| 246 | case STATE_IDLE: |
| 247 | // This means that the peer sent us a message that we weren't expecting. |
| 248 | stream_->CloseConnectionWithDetails(QUIC_INVALID_CRYPTO_MESSAGE_TYPE, |
| 249 | "Handshake in idle state"); |
| 250 | return; |
| 251 | case STATE_INITIALIZE_SCUP: |
| 252 | DoInitializeServerConfigUpdate(cached); |
| 253 | break; |
| 254 | case STATE_NONE: |
| 255 | QUIC_NOTREACHED(); |
| 256 | return; // We are done. |
| 257 | } |
| 258 | } while (rv != QUIC_PENDING && next_state_ != STATE_NONE); |
| 259 | } |
| 260 | |
| 261 | void QuicCryptoClientHandshaker::DoInitialize( |
| 262 | QuicCryptoClientConfig::CachedState* cached) { |
| 263 | if (!cached->IsEmpty() && !cached->signature().empty()) { |
| 264 | // Note that we verify the proof even if the cached proof is valid. |
| 265 | // This allows us to respond to CA trust changes or certificate |
| 266 | // expiration because it may have been a while since we last verified |
| 267 | // the proof. |
| 268 | DCHECK(crypto_config_->proof_verifier()); |
| 269 | // Track proof verification time when cached server config is used. |
| 270 | proof_verify_start_time_ = session()->connection()->clock()->Now(); |
| 271 | chlo_hash_ = cached->chlo_hash(); |
| 272 | // If the cached state needs to be verified, do it now. |
| 273 | next_state_ = STATE_VERIFY_PROOF; |
| 274 | } else { |
| 275 | next_state_ = STATE_GET_CHANNEL_ID; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | void QuicCryptoClientHandshaker::DoSendCHLO( |
| 280 | QuicCryptoClientConfig::CachedState* cached) { |
| 281 | if (stateless_reject_received_) { |
| 282 | // If we've gotten to this point, we've sent at least one hello |
| 283 | // and received a stateless reject in response. We cannot |
| 284 | // continue to send hellos because the server has abandoned state |
| 285 | // for this connection. Abandon further handshakes. |
| 286 | next_state_ = STATE_NONE; |
| 287 | if (session()->connection()->connected()) { |
| 288 | session()->connection()->CloseConnection( |
| 289 | QUIC_CRYPTO_HANDSHAKE_STATELESS_REJECT, "stateless reject received", |
| 290 | ConnectionCloseBehavior::SILENT_CLOSE); |
| 291 | } |
| 292 | return; |
| 293 | } |
| 294 | |
| 295 | // Send the client hello in plaintext. |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 296 | session()->connection()->SetDefaultEncryptionLevel(ENCRYPTION_INITIAL); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 297 | encryption_established_ = false; |
| 298 | if (num_client_hellos_ > QuicCryptoClientStream::kMaxClientHellos) { |
| 299 | stream_->CloseConnectionWithDetails( |
| 300 | QUIC_CRYPTO_TOO_MANY_REJECTS, |
| 301 | QuicStrCat("More than ", QuicCryptoClientStream::kMaxClientHellos, |
| 302 | " rejects")); |
| 303 | return; |
| 304 | } |
| 305 | num_client_hellos_++; |
| 306 | |
| 307 | CryptoHandshakeMessage out; |
| 308 | DCHECK(session() != nullptr); |
| 309 | DCHECK(session()->config() != nullptr); |
| 310 | // Send all the options, regardless of whether we're sending an |
| 311 | // inchoate or subsequent hello. |
| 312 | session()->config()->ToHandshakeMessage(&out); |
| 313 | |
| 314 | if (!cached->IsComplete(session()->connection()->clock()->WallNow())) { |
| 315 | crypto_config_->FillInchoateClientHello( |
| 316 | server_id_, session()->supported_versions().front(), cached, |
| 317 | session()->connection()->random_generator(), |
| 318 | /* demand_x509_proof= */ true, crypto_negotiated_params_, &out); |
| 319 | // Pad the inchoate client hello to fill up a packet. |
| 320 | const QuicByteCount kFramingOverhead = 50; // A rough estimate. |
| 321 | const QuicByteCount max_packet_size = |
| 322 | session()->connection()->max_packet_length(); |
| 323 | if (max_packet_size <= kFramingOverhead) { |
| 324 | QUIC_DLOG(DFATAL) << "max_packet_length (" << max_packet_size |
| 325 | << ") has no room for framing overhead."; |
| 326 | stream_->CloseConnectionWithDetails(QUIC_INTERNAL_ERROR, |
| 327 | "max_packet_size too smalll"); |
| 328 | return; |
| 329 | } |
| 330 | if (kClientHelloMinimumSize > max_packet_size - kFramingOverhead) { |
| 331 | QUIC_DLOG(DFATAL) << "Client hello won't fit in a single packet."; |
| 332 | stream_->CloseConnectionWithDetails(QUIC_INTERNAL_ERROR, |
| 333 | "CHLO too large"); |
| 334 | return; |
| 335 | } |
| 336 | next_state_ = STATE_RECV_REJ; |
QUICHE team | 84910bd | 2019-03-15 07:03:40 -0700 | [diff] [blame] | 337 | chlo_hash_ = CryptoUtils::HashHandshakeMessage(out, Perspective::IS_CLIENT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 338 | session()->connection()->set_fully_pad_crypto_hadshake_packets( |
| 339 | crypto_config_->pad_inchoate_hello()); |
| 340 | SendHandshakeMessage(out); |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | // If the server nonce is empty, copy over the server nonce from a previous |
| 345 | // SREJ, if there is one. |
| 346 | if (GetQuicReloadableFlag(enable_quic_stateless_reject_support) && |
| 347 | crypto_negotiated_params_->server_nonce.empty() && |
| 348 | cached->has_server_nonce()) { |
| 349 | crypto_negotiated_params_->server_nonce = cached->GetNextServerNonce(); |
| 350 | DCHECK(!crypto_negotiated_params_->server_nonce.empty()); |
| 351 | } |
| 352 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 353 | std::string error_details; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 354 | QuicErrorCode error = crypto_config_->FillClientHello( |
| 355 | server_id_, session()->connection()->connection_id(), |
| 356 | session()->supported_versions().front(), cached, |
| 357 | session()->connection()->clock()->WallNow(), |
| 358 | session()->connection()->random_generator(), channel_id_key_.get(), |
| 359 | crypto_negotiated_params_, &out, &error_details); |
| 360 | if (error != QUIC_NO_ERROR) { |
| 361 | // Flush the cached config so that, if it's bad, the server has a |
| 362 | // chance to send us another in the future. |
| 363 | cached->InvalidateServerConfig(); |
| 364 | stream_->CloseConnectionWithDetails(error, error_details); |
| 365 | return; |
| 366 | } |
QUICHE team | 84910bd | 2019-03-15 07:03:40 -0700 | [diff] [blame] | 367 | chlo_hash_ = CryptoUtils::HashHandshakeMessage(out, Perspective::IS_CLIENT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 368 | channel_id_sent_ = (channel_id_key_ != nullptr); |
| 369 | if (cached->proof_verify_details()) { |
| 370 | proof_handler_->OnProofVerifyDetailsAvailable( |
| 371 | *cached->proof_verify_details()); |
| 372 | } |
| 373 | next_state_ = STATE_RECV_SHLO; |
| 374 | session()->connection()->set_fully_pad_crypto_hadshake_packets( |
| 375 | crypto_config_->pad_full_hello()); |
| 376 | SendHandshakeMessage(out); |
| 377 | // Be prepared to decrypt with the new server write key. |
| 378 | session()->connection()->SetAlternativeDecrypter( |
| 379 | ENCRYPTION_ZERO_RTT, |
| 380 | std::move(crypto_negotiated_params_->initial_crypters.decrypter), |
| 381 | true /* latch once used */); |
| 382 | // Send subsequent packets under encryption on the assumption that the |
| 383 | // server will accept the handshake. |
| 384 | session()->connection()->SetEncrypter( |
| 385 | ENCRYPTION_ZERO_RTT, |
| 386 | std::move(crypto_negotiated_params_->initial_crypters.encrypter)); |
| 387 | session()->connection()->SetDefaultEncryptionLevel(ENCRYPTION_ZERO_RTT); |
| 388 | |
| 389 | // TODO(ianswett): Merge ENCRYPTION_REESTABLISHED and |
| 390 | // ENCRYPTION_FIRST_ESTABLSIHED |
| 391 | encryption_established_ = true; |
| 392 | session()->OnCryptoHandshakeEvent(QuicSession::ENCRYPTION_REESTABLISHED); |
| 393 | } |
| 394 | |
| 395 | void QuicCryptoClientHandshaker::DoReceiveREJ( |
| 396 | const CryptoHandshakeMessage* in, |
| 397 | QuicCryptoClientConfig::CachedState* cached) { |
| 398 | // We sent a dummy CHLO because we didn't have enough information to |
| 399 | // perform a handshake, or we sent a full hello that the server |
| 400 | // rejected. Here we hope to have a REJ that contains the information |
| 401 | // that we need. |
| 402 | if ((in->tag() != kREJ) && (in->tag() != kSREJ)) { |
| 403 | next_state_ = STATE_NONE; |
| 404 | stream_->CloseConnectionWithDetails(QUIC_INVALID_CRYPTO_MESSAGE_TYPE, |
| 405 | "Expected REJ"); |
| 406 | return; |
| 407 | } |
| 408 | |
| 409 | QuicTagVector reject_reasons; |
| 410 | static_assert(sizeof(QuicTag) == sizeof(uint32_t), "header out of sync"); |
| 411 | if (in->GetTaglist(kRREJ, &reject_reasons) == QUIC_NO_ERROR) { |
| 412 | uint32_t packed_error = 0; |
| 413 | for (size_t i = 0; i < reject_reasons.size(); ++i) { |
| 414 | // HANDSHAKE_OK is 0 and don't report that as error. |
| 415 | if (reject_reasons[i] == HANDSHAKE_OK || reject_reasons[i] >= 32) { |
| 416 | continue; |
| 417 | } |
| 418 | HandshakeFailureReason reason = |
| 419 | static_cast<HandshakeFailureReason>(reject_reasons[i]); |
| 420 | packed_error |= 1 << (reason - 1); |
| 421 | } |
| 422 | DVLOG(1) << "Reasons for rejection: " << packed_error; |
| 423 | if (num_client_hellos_ == QuicCryptoClientStream::kMaxClientHellos) { |
| 424 | QuicClientSparseHistogram("QuicClientHelloRejectReasons.TooMany", |
| 425 | packed_error); |
| 426 | } |
| 427 | QuicClientSparseHistogram("QuicClientHelloRejectReasons.Secure", |
| 428 | packed_error); |
| 429 | } |
| 430 | |
| 431 | // Receipt of a REJ message means that the server received the CHLO |
| 432 | // so we can cancel and retransmissions. |
| 433 | session()->NeuterUnencryptedData(); |
| 434 | |
| 435 | stateless_reject_received_ = in->tag() == kSREJ; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 436 | std::string error_details; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 437 | QuicErrorCode error = crypto_config_->ProcessRejection( |
| 438 | *in, session()->connection()->clock()->WallNow(), |
| 439 | session()->connection()->transport_version(), chlo_hash_, cached, |
| 440 | crypto_negotiated_params_, &error_details); |
| 441 | |
| 442 | if (error != QUIC_NO_ERROR) { |
| 443 | next_state_ = STATE_NONE; |
| 444 | stream_->CloseConnectionWithDetails(error, error_details); |
| 445 | return; |
| 446 | } |
| 447 | if (!cached->proof_valid()) { |
| 448 | if (!cached->signature().empty()) { |
| 449 | // Note that we only verify the proof if the cached proof is not |
| 450 | // valid. If the cached proof is valid here, someone else must have |
| 451 | // just added the server config to the cache and verified the proof, |
| 452 | // so we can assume no CA trust changes or certificate expiration |
| 453 | // has happened since then. |
| 454 | next_state_ = STATE_VERIFY_PROOF; |
| 455 | return; |
| 456 | } |
| 457 | } |
| 458 | next_state_ = STATE_GET_CHANNEL_ID; |
| 459 | } |
| 460 | |
| 461 | QuicAsyncStatus QuicCryptoClientHandshaker::DoVerifyProof( |
| 462 | QuicCryptoClientConfig::CachedState* cached) { |
| 463 | ProofVerifier* verifier = crypto_config_->proof_verifier(); |
| 464 | DCHECK(verifier); |
| 465 | next_state_ = STATE_VERIFY_PROOF_COMPLETE; |
| 466 | generation_counter_ = cached->generation_counter(); |
| 467 | |
| 468 | ProofVerifierCallbackImpl* proof_verify_callback = |
| 469 | new ProofVerifierCallbackImpl(this); |
| 470 | |
| 471 | verify_ok_ = false; |
| 472 | |
| 473 | QuicAsyncStatus status = verifier->VerifyProof( |
| 474 | server_id_.host(), server_id_.port(), cached->server_config(), |
| 475 | session()->connection()->transport_version(), chlo_hash_, cached->certs(), |
| 476 | cached->cert_sct(), cached->signature(), verify_context_.get(), |
| 477 | &verify_error_details_, &verify_details_, |
| 478 | std::unique_ptr<ProofVerifierCallback>(proof_verify_callback)); |
| 479 | |
| 480 | switch (status) { |
| 481 | case QUIC_PENDING: |
| 482 | proof_verify_callback_ = proof_verify_callback; |
| 483 | QUIC_DVLOG(1) << "Doing VerifyProof"; |
| 484 | break; |
| 485 | case QUIC_FAILURE: |
| 486 | break; |
| 487 | case QUIC_SUCCESS: |
| 488 | verify_ok_ = true; |
| 489 | break; |
| 490 | } |
| 491 | return status; |
| 492 | } |
| 493 | |
| 494 | void QuicCryptoClientHandshaker::DoVerifyProofComplete( |
| 495 | QuicCryptoClientConfig::CachedState* cached) { |
| 496 | if (proof_verify_start_time_.IsInitialized()) { |
| 497 | QUIC_CLIENT_HISTOGRAM_TIMES( |
| 498 | "QuicSession.VerifyProofTime.CachedServerConfig", |
| 499 | (session()->connection()->clock()->Now() - proof_verify_start_time_), |
| 500 | QuicTime::Delta::FromMilliseconds(1), QuicTime::Delta::FromSeconds(10), |
| 501 | 50, ""); |
| 502 | } |
| 503 | if (!verify_ok_) { |
| 504 | if (verify_details_) { |
| 505 | proof_handler_->OnProofVerifyDetailsAvailable(*verify_details_); |
| 506 | } |
| 507 | if (num_client_hellos_ == 0) { |
| 508 | cached->Clear(); |
| 509 | next_state_ = STATE_INITIALIZE; |
| 510 | return; |
| 511 | } |
| 512 | next_state_ = STATE_NONE; |
| 513 | QUIC_CLIENT_HISTOGRAM_BOOL("QuicVerifyProofFailed.HandshakeConfirmed", |
| 514 | handshake_confirmed(), ""); |
| 515 | stream_->CloseConnectionWithDetails( |
| 516 | QUIC_PROOF_INVALID, "Proof invalid: " + verify_error_details_); |
| 517 | return; |
| 518 | } |
| 519 | |
| 520 | // Check if generation_counter has changed between STATE_VERIFY_PROOF and |
| 521 | // STATE_VERIFY_PROOF_COMPLETE state changes. |
| 522 | if (generation_counter_ != cached->generation_counter()) { |
| 523 | next_state_ = STATE_VERIFY_PROOF; |
| 524 | } else { |
| 525 | SetCachedProofValid(cached); |
| 526 | cached->SetProofVerifyDetails(verify_details_.release()); |
| 527 | if (!handshake_confirmed()) { |
| 528 | next_state_ = STATE_GET_CHANNEL_ID; |
| 529 | } else { |
| 530 | // TODO: Enable Expect-Staple. https://crbug.com/631101 |
| 531 | next_state_ = STATE_NONE; |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | QuicAsyncStatus QuicCryptoClientHandshaker::DoGetChannelID( |
| 537 | QuicCryptoClientConfig::CachedState* cached) { |
| 538 | next_state_ = STATE_GET_CHANNEL_ID_COMPLETE; |
| 539 | channel_id_key_.reset(); |
| 540 | if (!RequiresChannelID(cached)) { |
| 541 | next_state_ = STATE_SEND_CHLO; |
| 542 | return QUIC_SUCCESS; |
| 543 | } |
| 544 | |
| 545 | ChannelIDSourceCallbackImpl* channel_id_source_callback = |
| 546 | new ChannelIDSourceCallbackImpl(this); |
| 547 | QuicAsyncStatus status = crypto_config_->channel_id_source()->GetChannelIDKey( |
| 548 | server_id_.host(), &channel_id_key_, channel_id_source_callback); |
| 549 | |
| 550 | switch (status) { |
| 551 | case QUIC_PENDING: |
| 552 | channel_id_source_callback_ = channel_id_source_callback; |
| 553 | QUIC_DVLOG(1) << "Looking up channel ID"; |
| 554 | break; |
| 555 | case QUIC_FAILURE: |
| 556 | next_state_ = STATE_NONE; |
| 557 | delete channel_id_source_callback; |
| 558 | stream_->CloseConnectionWithDetails(QUIC_INVALID_CHANNEL_ID_SIGNATURE, |
| 559 | "Channel ID lookup failed"); |
| 560 | break; |
| 561 | case QUIC_SUCCESS: |
| 562 | delete channel_id_source_callback; |
| 563 | break; |
| 564 | } |
| 565 | return status; |
| 566 | } |
| 567 | |
| 568 | void QuicCryptoClientHandshaker::DoGetChannelIDComplete() { |
| 569 | if (!channel_id_key_.get()) { |
| 570 | next_state_ = STATE_NONE; |
| 571 | stream_->CloseConnectionWithDetails(QUIC_INVALID_CHANNEL_ID_SIGNATURE, |
| 572 | "Channel ID lookup failed"); |
| 573 | return; |
| 574 | } |
| 575 | next_state_ = STATE_SEND_CHLO; |
| 576 | } |
| 577 | |
| 578 | void QuicCryptoClientHandshaker::DoReceiveSHLO( |
| 579 | const CryptoHandshakeMessage* in, |
| 580 | QuicCryptoClientConfig::CachedState* cached) { |
| 581 | next_state_ = STATE_NONE; |
| 582 | // We sent a CHLO that we expected to be accepted and now we're |
| 583 | // hoping for a SHLO from the server to confirm that. First check |
| 584 | // to see whether the response was a reject, and if so, move on to |
| 585 | // the reject-processing state. |
| 586 | if ((in->tag() == kREJ) || (in->tag() == kSREJ)) { |
| 587 | // alternative_decrypter will be nullptr if the original alternative |
| 588 | // decrypter latched and became the primary decrypter. That happens |
| 589 | // if we received a message encrypted with the INITIAL key. |
| 590 | if (session()->connection()->alternative_decrypter() == nullptr) { |
| 591 | // The rejection was sent encrypted! |
| 592 | stream_->CloseConnectionWithDetails( |
| 593 | QUIC_CRYPTO_ENCRYPTION_LEVEL_INCORRECT, "encrypted REJ message"); |
| 594 | return; |
| 595 | } |
| 596 | next_state_ = STATE_RECV_REJ; |
| 597 | return; |
| 598 | } |
| 599 | |
| 600 | if (in->tag() != kSHLO) { |
| 601 | stream_->CloseConnectionWithDetails(QUIC_INVALID_CRYPTO_MESSAGE_TYPE, |
| 602 | "Expected SHLO or REJ"); |
| 603 | return; |
| 604 | } |
| 605 | |
| 606 | // alternative_decrypter will be nullptr if the original alternative |
| 607 | // decrypter latched and became the primary decrypter. That happens |
| 608 | // if we received a message encrypted with the INITIAL key. |
| 609 | if (session()->connection()->alternative_decrypter() != nullptr) { |
| 610 | // The server hello was sent without encryption. |
| 611 | stream_->CloseConnectionWithDetails(QUIC_CRYPTO_ENCRYPTION_LEVEL_INCORRECT, |
| 612 | "unencrypted SHLO message"); |
| 613 | return; |
| 614 | } |
| 615 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 616 | std::string error_details; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 617 | QuicErrorCode error = crypto_config_->ProcessServerHello( |
| 618 | *in, session()->connection()->connection_id(), |
| 619 | session()->connection()->version(), |
| 620 | session()->connection()->server_supported_versions(), cached, |
| 621 | crypto_negotiated_params_, &error_details); |
| 622 | |
| 623 | if (error != QUIC_NO_ERROR) { |
| 624 | stream_->CloseConnectionWithDetails( |
| 625 | error, "Server hello invalid: " + error_details); |
| 626 | return; |
| 627 | } |
| 628 | error = session()->config()->ProcessPeerHello(*in, SERVER, &error_details); |
| 629 | if (error != QUIC_NO_ERROR) { |
| 630 | stream_->CloseConnectionWithDetails( |
| 631 | error, "Server hello invalid: " + error_details); |
| 632 | return; |
| 633 | } |
| 634 | session()->OnConfigNegotiated(); |
| 635 | |
| 636 | CrypterPair* crypters = &crypto_negotiated_params_->forward_secure_crypters; |
| 637 | // TODO(agl): we don't currently latch this decrypter because the idea |
| 638 | // has been floated that the server shouldn't send packets encrypted |
| 639 | // with the FORWARD_SECURE key until it receives a FORWARD_SECURE |
| 640 | // packet from the client. |
| 641 | session()->connection()->SetAlternativeDecrypter( |
| 642 | ENCRYPTION_FORWARD_SECURE, std::move(crypters->decrypter), |
| 643 | false /* don't latch */); |
| 644 | session()->connection()->SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
| 645 | std::move(crypters->encrypter)); |
| 646 | session()->connection()->SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 647 | |
| 648 | handshake_confirmed_ = true; |
| 649 | session()->OnCryptoHandshakeEvent(QuicSession::HANDSHAKE_CONFIRMED); |
| 650 | session()->connection()->OnHandshakeComplete(); |
| 651 | } |
| 652 | |
| 653 | void QuicCryptoClientHandshaker::DoInitializeServerConfigUpdate( |
| 654 | QuicCryptoClientConfig::CachedState* cached) { |
| 655 | bool update_ignored = false; |
| 656 | if (!cached->IsEmpty() && !cached->signature().empty()) { |
| 657 | // Note that we verify the proof even if the cached proof is valid. |
| 658 | DCHECK(crypto_config_->proof_verifier()); |
| 659 | next_state_ = STATE_VERIFY_PROOF; |
| 660 | } else { |
| 661 | update_ignored = true; |
| 662 | next_state_ = STATE_NONE; |
| 663 | } |
| 664 | QUIC_CLIENT_HISTOGRAM_COUNTS("QuicNumServerConfig.UpdateMessagesIgnored", |
| 665 | update_ignored, 1, 1000000, 50, ""); |
| 666 | } |
| 667 | |
| 668 | void QuicCryptoClientHandshaker::SetCachedProofValid( |
| 669 | QuicCryptoClientConfig::CachedState* cached) { |
| 670 | cached->SetProofValid(); |
| 671 | proof_handler_->OnProofValid(*cached); |
| 672 | } |
| 673 | |
| 674 | bool QuicCryptoClientHandshaker::RequiresChannelID( |
| 675 | QuicCryptoClientConfig::CachedState* cached) { |
| 676 | if (server_id_.privacy_mode_enabled() || |
| 677 | !crypto_config_->channel_id_source()) { |
| 678 | return false; |
| 679 | } |
| 680 | const CryptoHandshakeMessage* scfg = cached->GetServerConfig(); |
| 681 | if (!scfg) { // scfg may be null then we send an inchoate CHLO. |
| 682 | return false; |
| 683 | } |
| 684 | QuicTagVector their_proof_demands; |
| 685 | if (scfg->GetTaglist(kPDMD, &their_proof_demands) != QUIC_NO_ERROR) { |
| 686 | return false; |
| 687 | } |
| 688 | for (const QuicTag tag : their_proof_demands) { |
| 689 | if (tag == kCHID) { |
| 690 | return true; |
| 691 | } |
| 692 | } |
| 693 | return false; |
| 694 | } |
| 695 | |
| 696 | } // namespace quic |