1 // Copyright 2013 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.
5 #include "net/quic/crypto/quic_crypto_client_config.h"
7 #include "base/metrics/histogram.h"
8 #include "base/metrics/sparse_histogram.h"
9 #include "base/stl_util.h"
10 #include "base/strings/string_util.h"
11 #include "net/quic/crypto/cert_compressor.h"
12 #include "net/quic/crypto/chacha20_poly1305_encrypter.h"
13 #include "net/quic/crypto/channel_id.h"
14 #include "net/quic/crypto/common_cert_set.h"
15 #include "net/quic/crypto/crypto_framer.h"
16 #include "net/quic/crypto/crypto_utils.h"
17 #include "net/quic/crypto/curve25519_key_exchange.h"
18 #include "net/quic/crypto/key_exchange.h"
19 #include "net/quic/crypto/p256_key_exchange.h"
20 #include "net/quic/crypto/proof_verifier.h"
21 #include "net/quic/crypto/quic_encrypter.h"
22 #include "net/quic/quic_utils.h"
24 using base::StringPiece;
35 // Tracks the reason (the state of the server config) for sending inchoate
36 // ClientHello to the server.
37 void RecordInchoateClientHelloReason(
38 QuicCryptoClientConfig::CachedState::ServerConfigState state) {
39 UMA_HISTOGRAM_ENUMERATION(
40 "Net.QuicInchoateClientHelloReason", state,
41 QuicCryptoClientConfig::CachedState::SERVER_CONFIG_COUNT);
44 // Tracks the state of the QUIC server information loaded from the disk cache.
45 void RecordDiskCacheServerConfigState(
46 QuicCryptoClientConfig::CachedState::ServerConfigState state) {
47 UMA_HISTOGRAM_ENUMERATION(
48 "Net.QuicServerInfo.DiskCacheState", state,
49 QuicCryptoClientConfig::CachedState::SERVER_CONFIG_COUNT);
54 QuicCryptoClientConfig::QuicCryptoClientConfig()
55 : disable_ecdsa_(false) {
59 QuicCryptoClientConfig::~QuicCryptoClientConfig() {
60 STLDeleteValues(&cached_states_);
63 QuicCryptoClientConfig::CachedState::CachedState()
64 : server_config_valid_(false),
65 generation_counter_(0) {}
67 QuicCryptoClientConfig::CachedState::~CachedState() {}
69 bool QuicCryptoClientConfig::CachedState::IsComplete(QuicWallTime now) const {
70 if (server_config_.empty()) {
71 RecordInchoateClientHelloReason(SERVER_CONFIG_EMPTY);
75 if (!server_config_valid_) {
76 RecordInchoateClientHelloReason(SERVER_CONFIG_INVALID);
80 const CryptoHandshakeMessage* scfg = GetServerConfig();
82 // Should be impossible short of cache corruption.
84 RecordInchoateClientHelloReason(SERVER_CONFIG_CORRUPTED);
88 uint64 expiry_seconds;
89 if (scfg->GetUint64(kEXPY, &expiry_seconds) != QUIC_NO_ERROR) {
90 RecordInchoateClientHelloReason(SERVER_CONFIG_INVALID_EXPIRY);
93 if (now.ToUNIXSeconds() >= expiry_seconds) {
94 UMA_HISTOGRAM_CUSTOM_TIMES(
95 "Net.QuicClientHelloServerConfig.InvalidDuration",
96 base::TimeDelta::FromSeconds(now.ToUNIXSeconds() - expiry_seconds),
97 base::TimeDelta::FromMinutes(1), base::TimeDelta::FromDays(20), 50);
98 RecordInchoateClientHelloReason(SERVER_CONFIG_EXPIRED);
105 bool QuicCryptoClientConfig::CachedState::IsEmpty() const {
106 return server_config_.empty();
109 const CryptoHandshakeMessage*
110 QuicCryptoClientConfig::CachedState::GetServerConfig() const {
111 if (server_config_.empty()) {
116 scfg_.reset(CryptoFramer::ParseMessage(server_config_));
122 QuicCryptoClientConfig::CachedState::ServerConfigState
123 QuicCryptoClientConfig::CachedState::SetServerConfig(
124 StringPiece server_config, QuicWallTime now, string* error_details) {
125 const bool matches_existing = server_config == server_config_;
127 // Even if the new server config matches the existing one, we still wish to
128 // reject it if it has expired.
129 scoped_ptr<CryptoHandshakeMessage> new_scfg_storage;
130 const CryptoHandshakeMessage* new_scfg;
132 if (!matches_existing) {
133 new_scfg_storage.reset(CryptoFramer::ParseMessage(server_config));
134 new_scfg = new_scfg_storage.get();
136 new_scfg = GetServerConfig();
140 *error_details = "SCFG invalid";
141 return SERVER_CONFIG_INVALID;
144 uint64 expiry_seconds;
145 if (new_scfg->GetUint64(kEXPY, &expiry_seconds) != QUIC_NO_ERROR) {
146 *error_details = "SCFG missing EXPY";
147 return SERVER_CONFIG_INVALID_EXPIRY;
150 if (now.ToUNIXSeconds() >= expiry_seconds) {
151 *error_details = "SCFG has expired";
152 return SERVER_CONFIG_EXPIRED;
155 if (!matches_existing) {
156 server_config_ = server_config.as_string();
158 scfg_.reset(new_scfg_storage.release());
160 return SERVER_CONFIG_VALID;
163 void QuicCryptoClientConfig::CachedState::InvalidateServerConfig() {
164 server_config_.clear();
169 void QuicCryptoClientConfig::CachedState::SetProof(const vector<string>& certs,
170 StringPiece signature) {
172 signature != server_config_sig_ || certs_.size() != certs.size();
175 for (size_t i = 0; i < certs_.size(); i++) {
176 if (certs_[i] != certs[i]) {
187 // If the proof has changed then it needs to be revalidated.
190 server_config_sig_ = signature.as_string();
193 void QuicCryptoClientConfig::CachedState::Clear() {
194 server_config_.clear();
195 source_address_token_.clear();
197 server_config_sig_.clear();
198 server_config_valid_ = false;
199 proof_verify_details_.reset();
201 ++generation_counter_;
204 void QuicCryptoClientConfig::CachedState::ClearProof() {
207 server_config_sig_.clear();
210 void QuicCryptoClientConfig::CachedState::SetProofValid() {
211 server_config_valid_ = true;
214 void QuicCryptoClientConfig::CachedState::SetProofInvalid() {
215 server_config_valid_ = false;
216 ++generation_counter_;
219 bool QuicCryptoClientConfig::CachedState::Initialize(
220 StringPiece server_config,
221 StringPiece source_address_token,
222 const vector<string>& certs,
223 StringPiece signature,
225 DCHECK(server_config_.empty());
227 if (server_config.empty()) {
228 RecordDiskCacheServerConfigState(SERVER_CONFIG_EMPTY);
232 string error_details;
233 ServerConfigState state = SetServerConfig(server_config, now,
235 RecordDiskCacheServerConfigState(state);
236 if (state != SERVER_CONFIG_VALID) {
237 DVLOG(1) << "SetServerConfig failed with " << error_details;
241 signature.CopyToString(&server_config_sig_);
242 source_address_token.CopyToString(&source_address_token_);
247 const string& QuicCryptoClientConfig::CachedState::server_config() const {
248 return server_config_;
252 QuicCryptoClientConfig::CachedState::source_address_token() const {
253 return source_address_token_;
256 const vector<string>& QuicCryptoClientConfig::CachedState::certs() const {
260 const string& QuicCryptoClientConfig::CachedState::signature() const {
261 return server_config_sig_;
264 bool QuicCryptoClientConfig::CachedState::proof_valid() const {
265 return server_config_valid_;
268 uint64 QuicCryptoClientConfig::CachedState::generation_counter() const {
269 return generation_counter_;
272 const ProofVerifyDetails*
273 QuicCryptoClientConfig::CachedState::proof_verify_details() const {
274 return proof_verify_details_.get();
277 void QuicCryptoClientConfig::CachedState::set_source_address_token(
279 source_address_token_ = token.as_string();
282 void QuicCryptoClientConfig::CachedState::SetProofVerifyDetails(
283 ProofVerifyDetails* details) {
284 proof_verify_details_.reset(details);
287 void QuicCryptoClientConfig::CachedState::InitializeFrom(
288 const QuicCryptoClientConfig::CachedState& other) {
289 DCHECK(server_config_.empty());
290 DCHECK(!server_config_valid_);
291 server_config_ = other.server_config_;
292 source_address_token_ = other.source_address_token_;
293 certs_ = other.certs_;
294 server_config_sig_ = other.server_config_sig_;
295 server_config_valid_ = other.server_config_valid_;
296 if (other.proof_verify_details_.get() != nullptr) {
297 proof_verify_details_.reset(other.proof_verify_details_->Clone());
299 ++generation_counter_;
302 void QuicCryptoClientConfig::SetDefaults() {
303 // Key exchange methods.
308 // Authenticated encryption algorithms. Prefer ChaCha20 by default.
310 if (ChaCha20Poly1305Encrypter::IsSupported()) {
311 aead.push_back(kCC12);
313 aead.push_back(kAESG);
315 disable_ecdsa_ = false;
318 QuicCryptoClientConfig::CachedState* QuicCryptoClientConfig::LookupOrCreate(
319 const QuicServerId& server_id) {
320 CachedStateMap::const_iterator it = cached_states_.find(server_id);
321 if (it != cached_states_.end()) {
325 CachedState* cached = new CachedState;
326 cached_states_.insert(make_pair(server_id, cached));
327 bool cache_populated = PopulateFromCanonicalConfig(server_id, cached);
328 UMA_HISTOGRAM_BOOLEAN(
329 "Net.QuicCryptoClientConfig.PopulatedFromCanonicalConfig",
334 void QuicCryptoClientConfig::ClearCachedStates() {
335 for (CachedStateMap::const_iterator it = cached_states_.begin();
336 it != cached_states_.end(); ++it) {
341 void QuicCryptoClientConfig::FillInchoateClientHello(
342 const QuicServerId& server_id,
343 const QuicVersion preferred_version,
344 const CachedState* cached,
345 QuicCryptoNegotiatedParameters* out_params,
346 CryptoHandshakeMessage* out) const {
348 out->set_minimum_size(kClientHelloMinimumSize);
350 // Server name indication. We only send SNI if it's a valid domain name, as
352 if (CryptoUtils::IsValidSNI(server_id.host())) {
353 out->SetStringPiece(kSNI, server_id.host());
355 out->SetValue(kVER, QuicVersionToQuicTag(preferred_version));
357 if (!user_agent_id_.empty()) {
358 out->SetStringPiece(kUAID, user_agent_id_);
361 if (!cached->source_address_token().empty()) {
362 out->SetStringPiece(kSourceAddressTokenTag, cached->source_address_token());
365 if (server_id.is_https()) {
366 if (disable_ecdsa_) {
367 out->SetTaglist(kPDMD, kX59R, 0);
369 out->SetTaglist(kPDMD, kX509, 0);
373 if (common_cert_sets) {
374 out->SetStringPiece(kCCS, common_cert_sets->GetCommonHashes());
377 const vector<string>& certs = cached->certs();
378 // We save |certs| in the QuicCryptoNegotiatedParameters so that, if the
379 // client config is being used for multiple connections, another connection
380 // doesn't update the cached certificates and cause us to be unable to
381 // process the server's compressed certificate chain.
382 out_params->cached_certs = certs;
383 if (!certs.empty()) {
384 vector<uint64> hashes;
385 hashes.reserve(certs.size());
386 for (vector<string>::const_iterator i = certs.begin();
387 i != certs.end(); ++i) {
388 hashes.push_back(QuicUtils::FNV1a_64_Hash(i->data(), i->size()));
390 out->SetVector(kCCRT, hashes);
394 QuicErrorCode QuicCryptoClientConfig::FillClientHello(
395 const QuicServerId& server_id,
396 QuicConnectionId connection_id,
397 const QuicVersion preferred_version,
398 const CachedState* cached,
401 const ChannelIDKey* channel_id_key,
402 QuicCryptoNegotiatedParameters* out_params,
403 CryptoHandshakeMessage* out,
404 string* error_details) const {
405 DCHECK(error_details != nullptr);
407 FillInchoateClientHello(server_id, preferred_version, cached,
410 const CryptoHandshakeMessage* scfg = cached->GetServerConfig();
412 // This should never happen as our caller should have checked
413 // cached->IsComplete() before calling this function.
414 *error_details = "Handshake not ready";
415 return QUIC_CRYPTO_INTERNAL_ERROR;
419 if (!scfg->GetStringPiece(kSCID, &scid)) {
420 *error_details = "SCFG missing SCID";
421 return QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER;
423 out->SetStringPiece(kSCID, scid);
425 const QuicTag* their_aeads;
426 const QuicTag* their_key_exchanges;
427 size_t num_their_aeads, num_their_key_exchanges;
428 if (scfg->GetTaglist(kAEAD, &their_aeads,
429 &num_their_aeads) != QUIC_NO_ERROR ||
430 scfg->GetTaglist(kKEXS, &their_key_exchanges,
431 &num_their_key_exchanges) != QUIC_NO_ERROR) {
432 *error_details = "Missing AEAD or KEXS";
433 return QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER;
436 // AEAD: the work loads on the client and server are symmetric. Since the
437 // client is more likely to be CPU-constrained, break the tie by favoring
438 // the client's preference.
439 // Key exchange: the client does more work than the server, so favor the
440 // client's preference.
441 size_t key_exchange_index;
442 if (!QuicUtils::FindMutualTag(
443 aead, their_aeads, num_their_aeads, QuicUtils::LOCAL_PRIORITY,
444 &out_params->aead, nullptr) ||
445 !QuicUtils::FindMutualTag(
446 kexs, their_key_exchanges, num_their_key_exchanges,
447 QuicUtils::LOCAL_PRIORITY, &out_params->key_exchange,
448 &key_exchange_index)) {
449 *error_details = "Unsupported AEAD or KEXS";
450 return QUIC_CRYPTO_NO_SUPPORT;
452 out->SetTaglist(kAEAD, out_params->aead, 0);
453 out->SetTaglist(kKEXS, out_params->key_exchange, 0);
455 StringPiece public_value;
456 if (scfg->GetNthValue24(kPUBS, key_exchange_index, &public_value) !=
458 *error_details = "Missing public value";
459 return QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER;
463 if (!scfg->GetStringPiece(kORBT, &orbit) || orbit.size() != kOrbitSize) {
464 *error_details = "SCFG missing OBIT";
465 return QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND;
468 CryptoUtils::GenerateNonce(now, rand, orbit, &out_params->client_nonce);
469 out->SetStringPiece(kNONC, out_params->client_nonce);
470 if (!out_params->server_nonce.empty()) {
471 out->SetStringPiece(kServerNonceTag, out_params->server_nonce);
474 switch (out_params->key_exchange) {
476 out_params->client_key_exchange.reset(Curve25519KeyExchange::New(
477 Curve25519KeyExchange::NewPrivateKey(rand)));
480 out_params->client_key_exchange.reset(P256KeyExchange::New(
481 P256KeyExchange::NewPrivateKey()));
485 *error_details = "Configured to support an unknown key exchange";
486 return QUIC_CRYPTO_INTERNAL_ERROR;
489 if (!out_params->client_key_exchange->CalculateSharedKey(
490 public_value, &out_params->initial_premaster_secret)) {
491 *error_details = "Key exchange failure";
492 return QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER;
494 out->SetStringPiece(kPUBS, out_params->client_key_exchange->public_value());
496 if (channel_id_key) {
497 // In order to calculate the encryption key for the CETV block we need to
498 // serialise the client hello as it currently is (i.e. without the CETV
499 // block). For this, the client hello is serialized without padding.
500 const size_t orig_min_size = out->minimum_size();
501 out->set_minimum_size(0);
503 CryptoHandshakeMessage cetv;
507 const QuicData& client_hello_serialized = out->GetSerialized();
508 hkdf_input.append(QuicCryptoConfig::kCETVLabel,
509 strlen(QuicCryptoConfig::kCETVLabel) + 1);
510 hkdf_input.append(reinterpret_cast<char*>(&connection_id),
511 sizeof(connection_id));
512 hkdf_input.append(client_hello_serialized.data(),
513 client_hello_serialized.length());
514 hkdf_input.append(cached->server_config());
516 string key = channel_id_key->SerializeKey();
518 if (!channel_id_key->Sign(hkdf_input, &signature)) {
519 *error_details = "Channel ID signature failed";
520 return QUIC_INVALID_CHANNEL_ID_SIGNATURE;
523 cetv.SetStringPiece(kCIDK, key);
524 cetv.SetStringPiece(kCIDS, signature);
526 CrypterPair crypters;
527 if (!CryptoUtils::DeriveKeys(out_params->initial_premaster_secret,
528 out_params->aead, out_params->client_nonce,
529 out_params->server_nonce, hkdf_input,
530 CryptoUtils::CLIENT, &crypters,
531 nullptr /* subkey secret */)) {
532 *error_details = "Symmetric key setup failed";
533 return QUIC_CRYPTO_SYMMETRIC_KEY_SETUP_FAILED;
536 const QuicData& cetv_plaintext = cetv.GetSerialized();
537 scoped_ptr<QuicData> cetv_ciphertext(crypters.encrypter->EncryptPacket(
538 0 /* sequence number */,
539 StringPiece() /* associated data */,
540 cetv_plaintext.AsStringPiece()));
541 if (!cetv_ciphertext.get()) {
542 *error_details = "Packet encryption failed";
543 return QUIC_ENCRYPTION_FAILURE;
546 out->SetStringPiece(kCETV, cetv_ciphertext->AsStringPiece());
549 out->set_minimum_size(orig_min_size);
552 // Derive the symmetric keys and set up the encrypters and decrypters.
553 // Set the following members of out_params:
554 // out_params->hkdf_input_suffix
555 // out_params->initial_crypters
556 out_params->hkdf_input_suffix.clear();
557 out_params->hkdf_input_suffix.append(reinterpret_cast<char*>(&connection_id),
558 sizeof(connection_id));
559 const QuicData& client_hello_serialized = out->GetSerialized();
560 out_params->hkdf_input_suffix.append(client_hello_serialized.data(),
561 client_hello_serialized.length());
562 out_params->hkdf_input_suffix.append(cached->server_config());
565 const size_t label_len = strlen(QuicCryptoConfig::kInitialLabel) + 1;
566 hkdf_input.reserve(label_len + out_params->hkdf_input_suffix.size());
567 hkdf_input.append(QuicCryptoConfig::kInitialLabel, label_len);
568 hkdf_input.append(out_params->hkdf_input_suffix);
570 if (!CryptoUtils::DeriveKeys(
571 out_params->initial_premaster_secret, out_params->aead,
572 out_params->client_nonce, out_params->server_nonce, hkdf_input,
573 CryptoUtils::CLIENT, &out_params->initial_crypters,
574 nullptr /* subkey secret */)) {
575 *error_details = "Symmetric key setup failed";
576 return QUIC_CRYPTO_SYMMETRIC_KEY_SETUP_FAILED;
579 return QUIC_NO_ERROR;
582 QuicErrorCode QuicCryptoClientConfig::CacheNewServerConfig(
583 const CryptoHandshakeMessage& message,
585 const vector<string>& cached_certs,
587 string* error_details) {
588 DCHECK(error_details != nullptr);
591 if (!message.GetStringPiece(kSCFG, &scfg)) {
592 *error_details = "Missing SCFG";
593 return QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND;
596 CachedState::ServerConfigState state = cached->SetServerConfig(
597 scfg, now, error_details);
598 if (state == CachedState::SERVER_CONFIG_EXPIRED) {
599 return QUIC_CRYPTO_SERVER_CONFIG_EXPIRED;
601 // TODO(rtenneti): Return more specific error code than returning
602 // QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER.
603 if (state != CachedState::SERVER_CONFIG_VALID) {
604 return QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER;
608 if (message.GetStringPiece(kSourceAddressTokenTag, &token)) {
609 cached->set_source_address_token(token);
612 StringPiece proof, cert_bytes;
613 bool has_proof = message.GetStringPiece(kPROF, &proof);
614 bool has_cert = message.GetStringPiece(kCertificateTag, &cert_bytes);
615 if (has_proof && has_cert) {
616 vector<string> certs;
617 if (!CertCompressor::DecompressChain(cert_bytes, cached_certs,
618 common_cert_sets, &certs)) {
619 *error_details = "Certificate data invalid";
620 return QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER;
623 cached->SetProof(certs, proof);
625 if (proof_verifier() != nullptr) {
626 // Secure QUIC: clear existing proof as we have been sent a new SCFG
627 // without matching proof/certs.
628 cached->ClearProof();
631 if (has_proof && !has_cert) {
632 *error_details = "Certificate missing";
633 return QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER;
636 if (!has_proof && has_cert) {
637 *error_details = "Proof missing";
638 return QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER;
642 return QUIC_NO_ERROR;
645 QuicErrorCode QuicCryptoClientConfig::ProcessRejection(
646 const CryptoHandshakeMessage& rej,
650 QuicCryptoNegotiatedParameters* out_params,
651 string* error_details) {
652 DCHECK(error_details != nullptr);
654 if (rej.tag() != kREJ) {
655 *error_details = "Message is not REJ";
656 return QUIC_CRYPTO_INTERNAL_ERROR;
659 QuicErrorCode error = CacheNewServerConfig(rej, now, out_params->cached_certs,
660 cached, error_details);
661 if (error != QUIC_NO_ERROR) {
666 if (rej.GetStringPiece(kServerNonceTag, &nonce)) {
667 out_params->server_nonce = nonce.as_string();
670 const uint32* reject_reasons;
671 size_t num_reject_reasons;
672 COMPILE_ASSERT(sizeof(QuicTag) == sizeof(uint32), header_out_of_sync);
673 if (rej.GetTaglist(kRREJ, &reject_reasons,
674 &num_reject_reasons) == QUIC_NO_ERROR) {
675 uint32 packed_error = 0;
676 for (size_t i = 0; i < num_reject_reasons; ++i) {
677 // HANDSHAKE_OK is 0 and don't report that as error.
678 if (reject_reasons[i] == HANDSHAKE_OK || reject_reasons[i] >= 32) {
681 HandshakeFailureReason reason =
682 static_cast<HandshakeFailureReason>(reject_reasons[i]);
683 packed_error |= 1 << (reason - 1);
685 DVLOG(1) << "Reasons for rejection: " << packed_error;
687 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.QuicClientHelloRejectReasons.Secure",
690 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.QuicClientHelloRejectReasons.Insecure",
695 return QUIC_NO_ERROR;
698 QuicErrorCode QuicCryptoClientConfig::ProcessServerHello(
699 const CryptoHandshakeMessage& server_hello,
700 QuicConnectionId connection_id,
701 const QuicVersionVector& negotiated_versions,
703 QuicCryptoNegotiatedParameters* out_params,
704 string* error_details) {
705 DCHECK(error_details != nullptr);
707 if (server_hello.tag() != kSHLO) {
708 *error_details = "Bad tag";
709 return QUIC_INVALID_CRYPTO_MESSAGE_TYPE;
712 const QuicTag* supported_version_tags;
713 size_t num_supported_versions;
715 if (server_hello.GetTaglist(kVER, &supported_version_tags,
716 &num_supported_versions) != QUIC_NO_ERROR) {
717 *error_details = "server hello missing version list";
718 return QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER;
720 if (!negotiated_versions.empty()) {
721 bool mismatch = num_supported_versions != negotiated_versions.size();
722 for (size_t i = 0; i < num_supported_versions && !mismatch; ++i) {
723 mismatch = QuicTagToQuicVersion(supported_version_tags[i]) !=
724 negotiated_versions[i];
726 // The server sent a list of supported versions, and the connection
727 // reports that there was a version negotiation during the handshake.
728 // Ensure that these two lists are identical.
730 *error_details = "Downgrade attack detected";
731 return QUIC_VERSION_NEGOTIATION_MISMATCH;
735 // Learn about updated source address tokens.
737 if (server_hello.GetStringPiece(kSourceAddressTokenTag, &token)) {
738 cached->set_source_address_token(token);
742 // learn about updated SCFGs.
744 StringPiece public_value;
745 if (!server_hello.GetStringPiece(kPUBS, &public_value)) {
746 *error_details = "server hello missing forward secure public value";
747 return QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER;
750 if (!out_params->client_key_exchange->CalculateSharedKey(
751 public_value, &out_params->forward_secure_premaster_secret)) {
752 *error_details = "Key exchange failure";
753 return QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER;
757 const size_t label_len = strlen(QuicCryptoConfig::kForwardSecureLabel) + 1;
758 hkdf_input.reserve(label_len + out_params->hkdf_input_suffix.size());
759 hkdf_input.append(QuicCryptoConfig::kForwardSecureLabel, label_len);
760 hkdf_input.append(out_params->hkdf_input_suffix);
762 if (!CryptoUtils::DeriveKeys(
763 out_params->forward_secure_premaster_secret, out_params->aead,
764 out_params->client_nonce, out_params->server_nonce, hkdf_input,
765 CryptoUtils::CLIENT, &out_params->forward_secure_crypters,
766 &out_params->subkey_secret)) {
767 *error_details = "Symmetric key setup failed";
768 return QUIC_CRYPTO_SYMMETRIC_KEY_SETUP_FAILED;
771 return QUIC_NO_ERROR;
774 QuicErrorCode QuicCryptoClientConfig::ProcessServerConfigUpdate(
775 const CryptoHandshakeMessage& server_config_update,
778 QuicCryptoNegotiatedParameters* out_params,
779 string* error_details) {
780 DCHECK(error_details != nullptr);
782 if (server_config_update.tag() != kSCUP) {
783 *error_details = "ServerConfigUpdate must have kSCUP tag.";
784 return QUIC_INVALID_CRYPTO_MESSAGE_TYPE;
787 return CacheNewServerConfig(server_config_update, now,
788 out_params->cached_certs, cached, error_details);
791 ProofVerifier* QuicCryptoClientConfig::proof_verifier() const {
792 return proof_verifier_.get();
795 void QuicCryptoClientConfig::SetProofVerifier(ProofVerifier* verifier) {
796 proof_verifier_.reset(verifier);
799 ChannelIDSource* QuicCryptoClientConfig::channel_id_source() const {
800 return channel_id_source_.get();
803 void QuicCryptoClientConfig::SetChannelIDSource(ChannelIDSource* source) {
804 channel_id_source_.reset(source);
807 void QuicCryptoClientConfig::InitializeFrom(
808 const QuicServerId& server_id,
809 const QuicServerId& canonical_server_id,
810 QuicCryptoClientConfig* canonical_crypto_config) {
811 CachedState* canonical_cached =
812 canonical_crypto_config->LookupOrCreate(canonical_server_id);
813 if (!canonical_cached->proof_valid()) {
816 CachedState* cached = LookupOrCreate(server_id);
817 cached->InitializeFrom(*canonical_cached);
820 void QuicCryptoClientConfig::AddCanonicalSuffix(const string& suffix) {
821 canonical_suffixes_.push_back(suffix);
824 void QuicCryptoClientConfig::PreferAesGcm() {
825 DCHECK(!aead.empty());
826 if (aead.size() <= 1) {
829 QuicTagVector::iterator pos = find(aead.begin(), aead.end(), kAESG);
830 if (pos != aead.end()) {
832 aead.insert(aead.begin(), kAESG);
836 void QuicCryptoClientConfig::DisableEcdsa() {
837 disable_ecdsa_ = true;
840 bool QuicCryptoClientConfig::PopulateFromCanonicalConfig(
841 const QuicServerId& server_id,
842 CachedState* server_state) {
843 DCHECK(server_state->IsEmpty());
845 for (; i < canonical_suffixes_.size(); ++i) {
846 if (EndsWith(server_id.host(), canonical_suffixes_[i], false)) {
850 if (i == canonical_suffixes_.size())
853 QuicServerId suffix_server_id(canonical_suffixes_[i], server_id.port(),
854 server_id.is_https(),
855 server_id.privacy_mode());
856 if (!ContainsKey(canonical_server_map_, suffix_server_id)) {
857 // This is the first host we've seen which matches the suffix, so make it
859 canonical_server_map_[suffix_server_id] = server_id;
863 const QuicServerId& canonical_server_id =
864 canonical_server_map_[suffix_server_id];
865 CachedState* canonical_state = cached_states_[canonical_server_id];
866 if (!canonical_state->proof_valid()) {
870 // Update canonical version to point at the "most recent" entry.
871 canonical_server_map_[suffix_server_id] = server_id;
873 server_state->InitializeFrom(*canonical_state);