blob: 7f8edc22cfe73a4c34f2494b8626e184ec93b0ba [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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.
4
5#include "net/third_party/quiche/src/quic/core/crypto/quic_crypto_client_config.h"
6
7#include <algorithm>
8#include <memory>
vasilvv872e7a32019-03-12 16:42:44 -07009#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -050010
11#include "third_party/boringssl/src/include/openssl/ssl.h"
12#include "net/third_party/quiche/src/quic/core/crypto/cert_compressor.h"
13#include "net/third_party/quiche/src/quic/core/crypto/chacha20_poly1305_encrypter.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050014#include "net/third_party/quiche/src/quic/core/crypto/common_cert_set.h"
15#include "net/third_party/quiche/src/quic/core/crypto/crypto_framer.h"
16#include "net/third_party/quiche/src/quic/core/crypto/crypto_protocol.h"
17#include "net/third_party/quiche/src/quic/core/crypto/crypto_utils.h"
18#include "net/third_party/quiche/src/quic/core/crypto/curve25519_key_exchange.h"
19#include "net/third_party/quiche/src/quic/core/crypto/key_exchange.h"
20#include "net/third_party/quiche/src/quic/core/crypto/p256_key_exchange.h"
21#include "net/third_party/quiche/src/quic/core/crypto/proof_verifier.h"
22#include "net/third_party/quiche/src/quic/core/crypto/quic_encrypter.h"
23#include "net/third_party/quiche/src/quic/core/crypto/quic_random.h"
nharper6ebe83b2019-06-13 17:43:52 -070024#include "net/third_party/quiche/src/quic/core/crypto/tls_client_connection.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050025#include "net/third_party/quiche/src/quic/core/quic_connection_id.h"
26#include "net/third_party/quiche/src/quic/core/quic_types.h"
27#include "net/third_party/quiche/src/quic/core/quic_utils.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050028#include "net/third_party/quiche/src/quic/platform/api/quic_bug_tracker.h"
29#include "net/third_party/quiche/src/quic/platform/api/quic_client_stats.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050030#include "net/third_party/quiche/src/quic/platform/api/quic_hostname_utils.h"
31#include "net/third_party/quiche/src/quic/platform/api/quic_logging.h"
32#include "net/third_party/quiche/src/quic/platform/api/quic_map_util.h"
33#include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h"
bnc4e9283d2019-12-17 07:08:57 -080034#include "net/third_party/quiche/src/common/platform/api/quiche_arraysize.h"
dmcardle904ef182019-12-13 08:34:33 -080035#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h"
36#include "net/third_party/quiche/src/common/platform/api/quiche_text_utils.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050037
38namespace quic {
39
40namespace {
41
42// Tracks the reason (the state of the server config) for sending inchoate
43// ClientHello to the server.
44void RecordInchoateClientHelloReason(
45 QuicCryptoClientConfig::CachedState::ServerConfigState state) {
46 QUIC_CLIENT_HISTOGRAM_ENUM(
47 "QuicInchoateClientHelloReason", state,
48 QuicCryptoClientConfig::CachedState::SERVER_CONFIG_COUNT, "");
49}
50
51// Tracks the state of the QUIC server information loaded from the disk cache.
52void RecordDiskCacheServerConfigState(
53 QuicCryptoClientConfig::CachedState::ServerConfigState state) {
54 QUIC_CLIENT_HISTOGRAM_ENUM(
55 "QuicServerInfo.DiskCacheState", state,
56 QuicCryptoClientConfig::CachedState::SERVER_CONFIG_COUNT, "");
57}
58
59} // namespace
60
61QuicCryptoClientConfig::QuicCryptoClientConfig(
nharper6ebe83b2019-06-13 17:43:52 -070062 std::unique_ptr<ProofVerifier> proof_verifier)
nharperdf7a77b2019-11-11 13:12:45 -080063 : QuicCryptoClientConfig(std::move(proof_verifier), nullptr) {}
64
65QuicCryptoClientConfig::QuicCryptoClientConfig(
66 std::unique_ptr<ProofVerifier> proof_verifier,
67 std::unique_ptr<SessionCache> session_cache)
nharper1c06fd82019-06-24 14:33:34 -070068 : proof_verifier_(std::move(proof_verifier)),
nharperdf7a77b2019-11-11 13:12:45 -080069 session_cache_(std::move(session_cache)),
nharper1c06fd82019-06-24 14:33:34 -070070 ssl_ctx_(TlsClientConnection::CreateSslCtx()) {
QUICHE teama6ef0a62019-03-07 20:34:33 -050071 DCHECK(proof_verifier_.get());
72 SetDefaults();
73}
74
75QuicCryptoClientConfig::~QuicCryptoClientConfig() {}
76
77QuicCryptoClientConfig::CachedState::CachedState()
78 : server_config_valid_(false),
79 expiration_time_(QuicWallTime::Zero()),
80 generation_counter_(0) {}
81
82QuicCryptoClientConfig::CachedState::~CachedState() {}
83
84bool QuicCryptoClientConfig::CachedState::IsComplete(QuicWallTime now) const {
85 if (server_config_.empty()) {
86 RecordInchoateClientHelloReason(SERVER_CONFIG_EMPTY);
87 return false;
88 }
89
90 if (!server_config_valid_) {
91 RecordInchoateClientHelloReason(SERVER_CONFIG_INVALID);
92 return false;
93 }
94
95 const CryptoHandshakeMessage* scfg = GetServerConfig();
96 if (!scfg) {
97 // Should be impossible short of cache corruption.
98 RecordInchoateClientHelloReason(SERVER_CONFIG_CORRUPTED);
99 DCHECK(false);
100 return false;
101 }
102
103 if (now.IsBefore(expiration_time_)) {
104 return true;
105 }
106
107 QUIC_CLIENT_HISTOGRAM_TIMES(
108 "QuicClientHelloServerConfig.InvalidDuration",
109 QuicTime::Delta::FromSeconds(now.ToUNIXSeconds() -
110 expiration_time_.ToUNIXSeconds()),
111 QuicTime::Delta::FromSeconds(60), // 1 min.
112 QuicTime::Delta::FromSeconds(20 * 24 * 3600), // 20 days.
113 50, "");
114 RecordInchoateClientHelloReason(SERVER_CONFIG_EXPIRED);
115 return false;
116}
117
118bool QuicCryptoClientConfig::CachedState::IsEmpty() const {
119 return server_config_.empty();
120}
121
122const CryptoHandshakeMessage*
123QuicCryptoClientConfig::CachedState::GetServerConfig() const {
124 if (server_config_.empty()) {
125 return nullptr;
126 }
127
wub07a2b072019-10-24 11:23:20 -0700128 if (!scfg_) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500129 scfg_ = CryptoFramer::ParseMessage(server_config_);
130 DCHECK(scfg_.get());
131 }
132 return scfg_.get();
133}
134
135void QuicCryptoClientConfig::CachedState::add_server_designated_connection_id(
136 QuicConnectionId connection_id) {
137 server_designated_connection_ids_.push(connection_id);
138}
139
140bool QuicCryptoClientConfig::CachedState::has_server_designated_connection_id()
141 const {
142 return !server_designated_connection_ids_.empty();
143}
144
145void QuicCryptoClientConfig::CachedState::add_server_nonce(
vasilvvc48c8712019-03-11 13:38:16 -0700146 const std::string& server_nonce) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500147 server_nonces_.push(server_nonce);
148}
149
150bool QuicCryptoClientConfig::CachedState::has_server_nonce() const {
151 return !server_nonces_.empty();
152}
153
154QuicCryptoClientConfig::CachedState::ServerConfigState
155QuicCryptoClientConfig::CachedState::SetServerConfig(
dmcardle904ef182019-12-13 08:34:33 -0800156 quiche::QuicheStringPiece server_config,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500157 QuicWallTime now,
158 QuicWallTime expiry_time,
vasilvvc48c8712019-03-11 13:38:16 -0700159 std::string* error_details) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500160 const bool matches_existing = server_config == server_config_;
161
162 // Even if the new server config matches the existing one, we still wish to
163 // reject it if it has expired.
164 std::unique_ptr<CryptoHandshakeMessage> new_scfg_storage;
165 const CryptoHandshakeMessage* new_scfg;
166
167 if (!matches_existing) {
168 new_scfg_storage = CryptoFramer::ParseMessage(server_config);
169 new_scfg = new_scfg_storage.get();
170 } else {
171 new_scfg = GetServerConfig();
172 }
173
174 if (!new_scfg) {
175 *error_details = "SCFG invalid";
176 return SERVER_CONFIG_INVALID;
177 }
178
179 if (expiry_time.IsZero()) {
180 uint64_t expiry_seconds;
181 if (new_scfg->GetUint64(kEXPY, &expiry_seconds) != QUIC_NO_ERROR) {
182 *error_details = "SCFG missing EXPY";
183 return SERVER_CONFIG_INVALID_EXPIRY;
184 }
185 expiration_time_ = QuicWallTime::FromUNIXSeconds(expiry_seconds);
186 } else {
187 expiration_time_ = expiry_time;
188 }
189
190 if (now.IsAfter(expiration_time_)) {
191 *error_details = "SCFG has expired";
192 return SERVER_CONFIG_EXPIRED;
193 }
194
195 if (!matches_existing) {
vasilvvc48c8712019-03-11 13:38:16 -0700196 server_config_ = std::string(server_config);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500197 SetProofInvalid();
198 scfg_ = std::move(new_scfg_storage);
199 }
200 return SERVER_CONFIG_VALID;
201}
202
203void QuicCryptoClientConfig::CachedState::InvalidateServerConfig() {
204 server_config_.clear();
205 scfg_.reset();
206 SetProofInvalid();
207 QuicQueue<QuicConnectionId> empty_queue;
208 using std::swap;
209 swap(server_designated_connection_ids_, empty_queue);
210}
211
212void QuicCryptoClientConfig::CachedState::SetProof(
vasilvvc48c8712019-03-11 13:38:16 -0700213 const std::vector<std::string>& certs,
dmcardle904ef182019-12-13 08:34:33 -0800214 quiche::QuicheStringPiece cert_sct,
215 quiche::QuicheStringPiece chlo_hash,
216 quiche::QuicheStringPiece signature) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500217 bool has_changed = signature != server_config_sig_ ||
218 chlo_hash != chlo_hash_ || certs_.size() != certs.size();
219
220 if (!has_changed) {
221 for (size_t i = 0; i < certs_.size(); i++) {
222 if (certs_[i] != certs[i]) {
223 has_changed = true;
224 break;
225 }
226 }
227 }
228
229 if (!has_changed) {
230 return;
231 }
232
233 // If the proof has changed then it needs to be revalidated.
234 SetProofInvalid();
235 certs_ = certs;
vasilvvc48c8712019-03-11 13:38:16 -0700236 cert_sct_ = std::string(cert_sct);
237 chlo_hash_ = std::string(chlo_hash);
238 server_config_sig_ = std::string(signature);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500239}
240
241void QuicCryptoClientConfig::CachedState::Clear() {
242 server_config_.clear();
243 source_address_token_.clear();
244 certs_.clear();
245 cert_sct_.clear();
246 chlo_hash_.clear();
247 server_config_sig_.clear();
248 server_config_valid_ = false;
249 proof_verify_details_.reset();
250 scfg_.reset();
251 ++generation_counter_;
252 QuicQueue<QuicConnectionId> empty_queue;
253 using std::swap;
254 swap(server_designated_connection_ids_, empty_queue);
255}
256
257void QuicCryptoClientConfig::CachedState::ClearProof() {
258 SetProofInvalid();
259 certs_.clear();
260 cert_sct_.clear();
261 chlo_hash_.clear();
262 server_config_sig_.clear();
263}
264
265void QuicCryptoClientConfig::CachedState::SetProofValid() {
266 server_config_valid_ = true;
267}
268
269void QuicCryptoClientConfig::CachedState::SetProofInvalid() {
270 server_config_valid_ = false;
271 ++generation_counter_;
272}
273
274bool QuicCryptoClientConfig::CachedState::Initialize(
dmcardle904ef182019-12-13 08:34:33 -0800275 quiche::QuicheStringPiece server_config,
276 quiche::QuicheStringPiece source_address_token,
vasilvvc48c8712019-03-11 13:38:16 -0700277 const std::vector<std::string>& certs,
278 const std::string& cert_sct,
dmcardle904ef182019-12-13 08:34:33 -0800279 quiche::QuicheStringPiece chlo_hash,
280 quiche::QuicheStringPiece signature,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500281 QuicWallTime now,
282 QuicWallTime expiration_time) {
283 DCHECK(server_config_.empty());
284
285 if (server_config.empty()) {
286 RecordDiskCacheServerConfigState(SERVER_CONFIG_EMPTY);
287 return false;
288 }
289
vasilvvc48c8712019-03-11 13:38:16 -0700290 std::string error_details;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500291 ServerConfigState state =
292 SetServerConfig(server_config, now, expiration_time, &error_details);
293 RecordDiskCacheServerConfigState(state);
294 if (state != SERVER_CONFIG_VALID) {
295 QUIC_DVLOG(1) << "SetServerConfig failed with " << error_details;
296 return false;
297 }
298
299 chlo_hash_.assign(chlo_hash.data(), chlo_hash.size());
300 server_config_sig_.assign(signature.data(), signature.size());
301 source_address_token_.assign(source_address_token.data(),
302 source_address_token.size());
303 certs_ = certs;
304 cert_sct_ = cert_sct;
305 return true;
306}
307
vasilvvc48c8712019-03-11 13:38:16 -0700308const std::string& QuicCryptoClientConfig::CachedState::server_config() const {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500309 return server_config_;
310}
311
vasilvvc48c8712019-03-11 13:38:16 -0700312const std::string& QuicCryptoClientConfig::CachedState::source_address_token()
QUICHE teama6ef0a62019-03-07 20:34:33 -0500313 const {
314 return source_address_token_;
315}
316
vasilvvc48c8712019-03-11 13:38:16 -0700317const std::vector<std::string>& QuicCryptoClientConfig::CachedState::certs()
QUICHE teama6ef0a62019-03-07 20:34:33 -0500318 const {
319 return certs_;
320}
321
vasilvvc48c8712019-03-11 13:38:16 -0700322const std::string& QuicCryptoClientConfig::CachedState::cert_sct() const {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500323 return cert_sct_;
324}
325
vasilvvc48c8712019-03-11 13:38:16 -0700326const std::string& QuicCryptoClientConfig::CachedState::chlo_hash() const {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500327 return chlo_hash_;
328}
329
vasilvvc48c8712019-03-11 13:38:16 -0700330const std::string& QuicCryptoClientConfig::CachedState::signature() const {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500331 return server_config_sig_;
332}
333
334bool QuicCryptoClientConfig::CachedState::proof_valid() const {
335 return server_config_valid_;
336}
337
338uint64_t QuicCryptoClientConfig::CachedState::generation_counter() const {
339 return generation_counter_;
340}
341
342const ProofVerifyDetails*
343QuicCryptoClientConfig::CachedState::proof_verify_details() const {
344 return proof_verify_details_.get();
345}
346
347void QuicCryptoClientConfig::CachedState::set_source_address_token(
dmcardle904ef182019-12-13 08:34:33 -0800348 quiche::QuicheStringPiece token) {
vasilvvc48c8712019-03-11 13:38:16 -0700349 source_address_token_ = std::string(token);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500350}
351
352void QuicCryptoClientConfig::CachedState::set_cert_sct(
dmcardle904ef182019-12-13 08:34:33 -0800353 quiche::QuicheStringPiece cert_sct) {
vasilvvc48c8712019-03-11 13:38:16 -0700354 cert_sct_ = std::string(cert_sct);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500355}
356
357void QuicCryptoClientConfig::CachedState::SetProofVerifyDetails(
358 ProofVerifyDetails* details) {
359 proof_verify_details_.reset(details);
360}
361
362void QuicCryptoClientConfig::CachedState::InitializeFrom(
363 const QuicCryptoClientConfig::CachedState& other) {
364 DCHECK(server_config_.empty());
365 DCHECK(!server_config_valid_);
366 server_config_ = other.server_config_;
367 source_address_token_ = other.source_address_token_;
368 certs_ = other.certs_;
369 cert_sct_ = other.cert_sct_;
370 chlo_hash_ = other.chlo_hash_;
371 server_config_sig_ = other.server_config_sig_;
372 server_config_valid_ = other.server_config_valid_;
373 server_designated_connection_ids_ = other.server_designated_connection_ids_;
374 expiration_time_ = other.expiration_time_;
375 if (other.proof_verify_details_ != nullptr) {
376 proof_verify_details_.reset(other.proof_verify_details_->Clone());
377 }
378 ++generation_counter_;
379}
380
381QuicConnectionId
382QuicCryptoClientConfig::CachedState::GetNextServerDesignatedConnectionId() {
383 if (server_designated_connection_ids_.empty()) {
384 QUIC_BUG
385 << "Attempting to consume a connection id that was never designated.";
386 return EmptyQuicConnectionId();
387 }
388 const QuicConnectionId next_id = server_designated_connection_ids_.front();
389 server_designated_connection_ids_.pop();
390 return next_id;
391}
392
vasilvvc48c8712019-03-11 13:38:16 -0700393std::string QuicCryptoClientConfig::CachedState::GetNextServerNonce() {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500394 if (server_nonces_.empty()) {
395 QUIC_BUG
396 << "Attempting to consume a server nonce that was never designated.";
397 return "";
398 }
vasilvvc48c8712019-03-11 13:38:16 -0700399 const std::string server_nonce = server_nonces_.front();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500400 server_nonces_.pop();
401 return server_nonce;
402}
403
404void QuicCryptoClientConfig::SetDefaults() {
405 // Key exchange methods.
406 kexs = {kC255, kP256};
407
408 // Authenticated encryption algorithms. Prefer AES-GCM if hardware-supported
409 // fast implementation is available.
410 if (EVP_has_aes_hardware() == 1) {
411 aead = {kAESG, kCC20};
412 } else {
413 aead = {kCC20, kAESG};
414 }
415}
416
417QuicCryptoClientConfig::CachedState* QuicCryptoClientConfig::LookupOrCreate(
418 const QuicServerId& server_id) {
419 auto it = cached_states_.find(server_id);
420 if (it != cached_states_.end()) {
421 return it->second.get();
422 }
423
424 CachedState* cached = new CachedState;
425 cached_states_.insert(std::make_pair(server_id, QuicWrapUnique(cached)));
426 bool cache_populated = PopulateFromCanonicalConfig(server_id, cached);
427 QUIC_CLIENT_HISTOGRAM_BOOL(
428 "QuicCryptoClientConfig.PopulatedFromCanonicalConfig", cache_populated,
429 "");
430 return cached;
431}
432
433void QuicCryptoClientConfig::ClearCachedStates(const ServerIdFilter& filter) {
434 for (auto it = cached_states_.begin(); it != cached_states_.end(); ++it) {
435 if (filter.Matches(it->first))
436 it->second->Clear();
437 }
438}
439
440void QuicCryptoClientConfig::FillInchoateClientHello(
441 const QuicServerId& server_id,
442 const ParsedQuicVersion preferred_version,
443 const CachedState* cached,
444 QuicRandom* rand,
445 bool demand_x509_proof,
446 QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters> out_params,
447 CryptoHandshakeMessage* out) const {
448 out->set_tag(kCHLO);
449 // TODO(rch): Remove this when we remove quic_use_chlo_packet_size flag.
450 if (pad_inchoate_hello_) {
451 out->set_minimum_size(kClientHelloMinimumSize);
452 } else {
453 out->set_minimum_size(1);
454 }
455
456 // Server name indication. We only send SNI if it's a valid domain name, as
457 // per the spec.
458 if (QuicHostnameUtils::IsValidSNI(server_id.host())) {
459 out->SetStringPiece(kSNI, server_id.host());
460 }
461 out->SetVersion(kVER, preferred_version);
462
463 if (!user_agent_id_.empty()) {
464 out->SetStringPiece(kUAID, user_agent_id_);
465 }
466
467 if (!alpn_.empty()) {
468 out->SetStringPiece(kALPN, alpn_);
469 }
470
471 // Even though this is an inchoate CHLO, send the SCID so that
472 // the STK can be validated by the server.
473 const CryptoHandshakeMessage* scfg = cached->GetServerConfig();
474 if (scfg != nullptr) {
dmcardle904ef182019-12-13 08:34:33 -0800475 quiche::QuicheStringPiece scid;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500476 if (scfg->GetStringPiece(kSCID, &scid)) {
477 out->SetStringPiece(kSCID, scid);
478 }
479 }
480
481 if (!cached->source_address_token().empty()) {
482 out->SetStringPiece(kSourceAddressTokenTag, cached->source_address_token());
483 }
484
485 if (!demand_x509_proof) {
486 return;
487 }
488
489 char proof_nonce[32];
bnc4e9283d2019-12-17 07:08:57 -0800490 rand->RandBytes(proof_nonce, QUICHE_ARRAYSIZE(proof_nonce));
dmcardle904ef182019-12-13 08:34:33 -0800491 out->SetStringPiece(kNONP, quiche::QuicheStringPiece(
bnc4e9283d2019-12-17 07:08:57 -0800492 proof_nonce, QUICHE_ARRAYSIZE(proof_nonce)));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500493
494 out->SetVector(kPDMD, QuicTagVector{kX509});
495
496 if (common_cert_sets) {
497 out->SetStringPiece(kCCS, common_cert_sets->GetCommonHashes());
498 }
499
500 out->SetStringPiece(kCertificateSCTTag, "");
501
vasilvvc48c8712019-03-11 13:38:16 -0700502 const std::vector<std::string>& certs = cached->certs();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500503 // We save |certs| in the QuicCryptoNegotiatedParameters so that, if the
504 // client config is being used for multiple connections, another connection
505 // doesn't update the cached certificates and cause us to be unable to
506 // process the server's compressed certificate chain.
507 out_params->cached_certs = certs;
508 if (!certs.empty()) {
509 std::vector<uint64_t> hashes;
510 hashes.reserve(certs.size());
511 for (auto i = certs.begin(); i != certs.end(); ++i) {
512 hashes.push_back(QuicUtils::FNV1a_64_Hash(*i));
513 }
514 out->SetVector(kCCRT, hashes);
515 }
516}
517
518QuicErrorCode QuicCryptoClientConfig::FillClientHello(
519 const QuicServerId& server_id,
520 QuicConnectionId connection_id,
521 const ParsedQuicVersion preferred_version,
nharperc1bbfe62019-09-27 16:48:40 -0700522 const ParsedQuicVersion actual_version,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500523 const CachedState* cached,
524 QuicWallTime now,
525 QuicRandom* rand,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500526 QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters> out_params,
527 CryptoHandshakeMessage* out,
vasilvvc48c8712019-03-11 13:38:16 -0700528 std::string* error_details) const {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500529 DCHECK(error_details != nullptr);
530 QUIC_BUG_IF(!QuicUtils::IsConnectionIdValidForVersion(
531 connection_id, preferred_version.transport_version))
532 << "FillClientHello: attempted to use connection ID " << connection_id
533 << " which is invalid with version "
534 << QuicVersionToString(preferred_version.transport_version);
535
536 FillInchoateClientHello(server_id, preferred_version, cached, rand,
537 /* demand_x509_proof= */ true, out_params, out);
538
539 if (pad_full_hello_) {
540 out->set_minimum_size(kClientHelloMinimumSize);
541 } else {
542 out->set_minimum_size(1);
543 }
544
545 const CryptoHandshakeMessage* scfg = cached->GetServerConfig();
546 if (!scfg) {
547 // This should never happen as our caller should have checked
548 // cached->IsComplete() before calling this function.
549 *error_details = "Handshake not ready";
550 return QUIC_CRYPTO_INTERNAL_ERROR;
551 }
552
dmcardle904ef182019-12-13 08:34:33 -0800553 quiche::QuicheStringPiece scid;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500554 if (!scfg->GetStringPiece(kSCID, &scid)) {
555 *error_details = "SCFG missing SCID";
556 return QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER;
557 }
558 out->SetStringPiece(kSCID, scid);
559
560 out->SetStringPiece(kCertificateSCTTag, "");
561
562 QuicTagVector their_aeads;
563 QuicTagVector their_key_exchanges;
564 if (scfg->GetTaglist(kAEAD, &their_aeads) != QUIC_NO_ERROR ||
565 scfg->GetTaglist(kKEXS, &their_key_exchanges) != QUIC_NO_ERROR) {
566 *error_details = "Missing AEAD or KEXS";
567 return QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER;
568 }
569
570 // AEAD: the work loads on the client and server are symmetric. Since the
571 // client is more likely to be CPU-constrained, break the tie by favoring
572 // the client's preference.
573 // Key exchange: the client does more work than the server, so favor the
574 // client's preference.
575 size_t key_exchange_index;
576 if (!FindMutualQuicTag(aead, their_aeads, &out_params->aead, nullptr) ||
577 !FindMutualQuicTag(kexs, their_key_exchanges, &out_params->key_exchange,
578 &key_exchange_index)) {
579 *error_details = "Unsupported AEAD or KEXS";
580 return QUIC_CRYPTO_NO_SUPPORT;
581 }
582 out->SetVector(kAEAD, QuicTagVector{out_params->aead});
583 out->SetVector(kKEXS, QuicTagVector{out_params->key_exchange});
584
dmcardle904ef182019-12-13 08:34:33 -0800585 quiche::QuicheStringPiece public_value;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500586 if (scfg->GetNthValue24(kPUBS, key_exchange_index, &public_value) !=
587 QUIC_NO_ERROR) {
588 *error_details = "Missing public value";
589 return QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER;
590 }
591
dmcardle904ef182019-12-13 08:34:33 -0800592 quiche::QuicheStringPiece orbit;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500593 if (!scfg->GetStringPiece(kORBT, &orbit) || orbit.size() != kOrbitSize) {
594 *error_details = "SCFG missing OBIT";
595 return QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND;
596 }
597
598 CryptoUtils::GenerateNonce(now, rand, orbit, &out_params->client_nonce);
599 out->SetStringPiece(kNONC, out_params->client_nonce);
600 if (!out_params->server_nonce.empty()) {
601 out->SetStringPiece(kServerNonceTag, out_params->server_nonce);
602 }
603
604 switch (out_params->key_exchange) {
605 case kC255:
606 out_params->client_key_exchange = Curve25519KeyExchange::New(
607 Curve25519KeyExchange::NewPrivateKey(rand));
608 break;
609 case kP256:
610 out_params->client_key_exchange =
611 P256KeyExchange::New(P256KeyExchange::NewPrivateKey());
612 break;
613 default:
614 DCHECK(false);
615 *error_details = "Configured to support an unknown key exchange";
616 return QUIC_CRYPTO_INTERNAL_ERROR;
617 }
618
QUICHE teamfe1aca62019-03-14 13:39:01 -0700619 if (!out_params->client_key_exchange->CalculateSharedKeySync(
QUICHE teama6ef0a62019-03-07 20:34:33 -0500620 public_value, &out_params->initial_premaster_secret)) {
621 *error_details = "Key exchange failure";
622 return QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER;
623 }
624 out->SetStringPiece(kPUBS, out_params->client_key_exchange->public_value());
625
vasilvvc48c8712019-03-11 13:38:16 -0700626 const std::vector<std::string>& certs = cached->certs();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500627 if (certs.empty()) {
628 *error_details = "No certs to calculate XLCT";
629 return QUIC_CRYPTO_INTERNAL_ERROR;
630 }
631 out->SetValue(kXLCT, CryptoUtils::ComputeLeafCertHash(certs[0]));
632
QUICHE teama6ef0a62019-03-07 20:34:33 -0500633 // Derive the symmetric keys and set up the encrypters and decrypters.
634 // Set the following members of out_params:
635 // out_params->hkdf_input_suffix
636 // out_params->initial_crypters
637 out_params->hkdf_input_suffix.clear();
vasilvvc48c8712019-03-11 13:38:16 -0700638 out_params->hkdf_input_suffix.append(connection_id.data(),
639 connection_id.length());
QUICHE teama6ef0a62019-03-07 20:34:33 -0500640 const QuicData& client_hello_serialized = out->GetSerialized();
641 out_params->hkdf_input_suffix.append(client_hello_serialized.data(),
642 client_hello_serialized.length());
643 out_params->hkdf_input_suffix.append(cached->server_config());
644 if (certs.empty()) {
645 *error_details = "No certs found to include in KDF";
646 return QUIC_CRYPTO_INTERNAL_ERROR;
647 }
648 out_params->hkdf_input_suffix.append(certs[0]);
649
vasilvvc48c8712019-03-11 13:38:16 -0700650 std::string hkdf_input;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500651 const size_t label_len = strlen(QuicCryptoConfig::kInitialLabel) + 1;
652 hkdf_input.reserve(label_len + out_params->hkdf_input_suffix.size());
653 hkdf_input.append(QuicCryptoConfig::kInitialLabel, label_len);
654 hkdf_input.append(out_params->hkdf_input_suffix);
655
vasilvvc48c8712019-03-11 13:38:16 -0700656 std::string* subkey_secret = &out_params->initial_subkey_secret;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500657
nharperc1bbfe62019-09-27 16:48:40 -0700658 if (!CryptoUtils::DeriveKeys(
659 actual_version, out_params->initial_premaster_secret,
660 out_params->aead, out_params->client_nonce, out_params->server_nonce,
661 pre_shared_key_, hkdf_input, Perspective::IS_CLIENT,
662 CryptoUtils::Diversification::Pending(),
663 &out_params->initial_crypters, subkey_secret)) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500664 *error_details = "Symmetric key setup failed";
665 return QUIC_CRYPTO_SYMMETRIC_KEY_SETUP_FAILED;
666 }
667
668 return QUIC_NO_ERROR;
669}
670
671QuicErrorCode QuicCryptoClientConfig::CacheNewServerConfig(
672 const CryptoHandshakeMessage& message,
673 QuicWallTime now,
dschinazi17d42422019-06-18 16:35:07 -0700674 QuicTransportVersion /*version*/,
dmcardle904ef182019-12-13 08:34:33 -0800675 quiche::QuicheStringPiece chlo_hash,
vasilvvc48c8712019-03-11 13:38:16 -0700676 const std::vector<std::string>& cached_certs,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500677 CachedState* cached,
vasilvvc48c8712019-03-11 13:38:16 -0700678 std::string* error_details) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500679 DCHECK(error_details != nullptr);
680
dmcardle904ef182019-12-13 08:34:33 -0800681 quiche::QuicheStringPiece scfg;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500682 if (!message.GetStringPiece(kSCFG, &scfg)) {
683 *error_details = "Missing SCFG";
684 return QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND;
685 }
686
687 QuicWallTime expiration_time = QuicWallTime::Zero();
688 uint64_t expiry_seconds;
689 if (message.GetUint64(kSTTL, &expiry_seconds) == QUIC_NO_ERROR) {
690 // Only cache configs for a maximum of 1 week.
691 expiration_time = now.Add(QuicTime::Delta::FromSeconds(
692 std::min(expiry_seconds, kNumSecondsPerWeek)));
693 }
694
695 CachedState::ServerConfigState state =
696 cached->SetServerConfig(scfg, now, expiration_time, error_details);
697 if (state == CachedState::SERVER_CONFIG_EXPIRED) {
698 return QUIC_CRYPTO_SERVER_CONFIG_EXPIRED;
699 }
700 // TODO(rtenneti): Return more specific error code than returning
701 // QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER.
702 if (state != CachedState::SERVER_CONFIG_VALID) {
703 return QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER;
704 }
705
dmcardle904ef182019-12-13 08:34:33 -0800706 quiche::QuicheStringPiece token;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500707 if (message.GetStringPiece(kSourceAddressTokenTag, &token)) {
708 cached->set_source_address_token(token);
709 }
710
dmcardle904ef182019-12-13 08:34:33 -0800711 quiche::QuicheStringPiece proof, cert_bytes, cert_sct;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500712 bool has_proof = message.GetStringPiece(kPROF, &proof);
713 bool has_cert = message.GetStringPiece(kCertificateTag, &cert_bytes);
714 if (has_proof && has_cert) {
vasilvvc48c8712019-03-11 13:38:16 -0700715 std::vector<std::string> certs;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500716 if (!CertCompressor::DecompressChain(cert_bytes, cached_certs,
717 common_cert_sets, &certs)) {
718 *error_details = "Certificate data invalid";
719 return QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER;
720 }
721
722 message.GetStringPiece(kCertificateSCTTag, &cert_sct);
723 cached->SetProof(certs, cert_sct, chlo_hash, proof);
724 } else {
725 // Secure QUIC: clear existing proof as we have been sent a new SCFG
726 // without matching proof/certs.
727 cached->ClearProof();
728
729 if (has_proof && !has_cert) {
730 *error_details = "Certificate missing";
731 return QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER;
732 }
733
734 if (!has_proof && has_cert) {
735 *error_details = "Proof missing";
736 return QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER;
737 }
738 }
739
740 return QUIC_NO_ERROR;
741}
742
743QuicErrorCode QuicCryptoClientConfig::ProcessRejection(
744 const CryptoHandshakeMessage& rej,
745 QuicWallTime now,
746 const QuicTransportVersion version,
dmcardle904ef182019-12-13 08:34:33 -0800747 quiche::QuicheStringPiece chlo_hash,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500748 CachedState* cached,
749 QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters> out_params,
vasilvvc48c8712019-03-11 13:38:16 -0700750 std::string* error_details) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500751 DCHECK(error_details != nullptr);
752
wub0a4b9c52019-05-28 13:18:58 -0700753 if (rej.tag() != kREJ) {
754 *error_details = "Message is not REJ";
QUICHE teama6ef0a62019-03-07 20:34:33 -0500755 return QUIC_CRYPTO_INTERNAL_ERROR;
756 }
757
758 QuicErrorCode error =
759 CacheNewServerConfig(rej, now, version, chlo_hash,
760 out_params->cached_certs, cached, error_details);
761 if (error != QUIC_NO_ERROR) {
762 return error;
763 }
764
dmcardle904ef182019-12-13 08:34:33 -0800765 quiche::QuicheStringPiece nonce;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500766 if (rej.GetStringPiece(kServerNonceTag, &nonce)) {
vasilvvc48c8712019-03-11 13:38:16 -0700767 out_params->server_nonce = std::string(nonce);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500768 }
769
QUICHE teama6ef0a62019-03-07 20:34:33 -0500770 return QUIC_NO_ERROR;
771}
772
773QuicErrorCode QuicCryptoClientConfig::ProcessServerHello(
774 const CryptoHandshakeMessage& server_hello,
dschinazi17d42422019-06-18 16:35:07 -0700775 QuicConnectionId /*connection_id*/,
nharperc1bbfe62019-09-27 16:48:40 -0700776 ParsedQuicVersion version,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500777 const ParsedQuicVersionVector& negotiated_versions,
778 CachedState* cached,
779 QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters> out_params,
vasilvvc48c8712019-03-11 13:38:16 -0700780 std::string* error_details) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500781 DCHECK(error_details != nullptr);
782
783 QuicErrorCode valid = CryptoUtils::ValidateServerHello(
784 server_hello, negotiated_versions, error_details);
785 if (valid != QUIC_NO_ERROR) {
786 return valid;
787 }
788
789 // Learn about updated source address tokens.
dmcardle904ef182019-12-13 08:34:33 -0800790 quiche::QuicheStringPiece token;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500791 if (server_hello.GetStringPiece(kSourceAddressTokenTag, &token)) {
792 cached->set_source_address_token(token);
793 }
794
dmcardle904ef182019-12-13 08:34:33 -0800795 quiche::QuicheStringPiece shlo_nonce;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500796 if (!server_hello.GetStringPiece(kServerNonceTag, &shlo_nonce)) {
797 *error_details = "server hello missing server nonce";
798 return QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER;
799 }
800
801 // TODO(agl):
802 // learn about updated SCFGs.
803
dmcardle904ef182019-12-13 08:34:33 -0800804 quiche::QuicheStringPiece public_value;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500805 if (!server_hello.GetStringPiece(kPUBS, &public_value)) {
806 *error_details = "server hello missing forward secure public value";
807 return QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER;
808 }
809
QUICHE teamfe1aca62019-03-14 13:39:01 -0700810 if (!out_params->client_key_exchange->CalculateSharedKeySync(
QUICHE teama6ef0a62019-03-07 20:34:33 -0500811 public_value, &out_params->forward_secure_premaster_secret)) {
812 *error_details = "Key exchange failure";
813 return QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER;
814 }
815
vasilvvc48c8712019-03-11 13:38:16 -0700816 std::string hkdf_input;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500817 const size_t label_len = strlen(QuicCryptoConfig::kForwardSecureLabel) + 1;
818 hkdf_input.reserve(label_len + out_params->hkdf_input_suffix.size());
819 hkdf_input.append(QuicCryptoConfig::kForwardSecureLabel, label_len);
820 hkdf_input.append(out_params->hkdf_input_suffix);
821
822 if (!CryptoUtils::DeriveKeys(
nharperc1bbfe62019-09-27 16:48:40 -0700823 version, out_params->forward_secure_premaster_secret,
824 out_params->aead, out_params->client_nonce,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500825 shlo_nonce.empty() ? out_params->server_nonce : shlo_nonce,
826 pre_shared_key_, hkdf_input, Perspective::IS_CLIENT,
827 CryptoUtils::Diversification::Never(),
828 &out_params->forward_secure_crypters, &out_params->subkey_secret)) {
829 *error_details = "Symmetric key setup failed";
830 return QUIC_CRYPTO_SYMMETRIC_KEY_SETUP_FAILED;
831 }
832
833 return QUIC_NO_ERROR;
834}
835
836QuicErrorCode QuicCryptoClientConfig::ProcessServerConfigUpdate(
837 const CryptoHandshakeMessage& server_config_update,
838 QuicWallTime now,
839 const QuicTransportVersion version,
dmcardle904ef182019-12-13 08:34:33 -0800840 quiche::QuicheStringPiece chlo_hash,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500841 CachedState* cached,
842 QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters> out_params,
vasilvvc48c8712019-03-11 13:38:16 -0700843 std::string* error_details) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500844 DCHECK(error_details != nullptr);
845
846 if (server_config_update.tag() != kSCUP) {
847 *error_details = "ServerConfigUpdate must have kSCUP tag.";
848 return QUIC_INVALID_CRYPTO_MESSAGE_TYPE;
849 }
850 return CacheNewServerConfig(server_config_update, now, version, chlo_hash,
851 out_params->cached_certs, cached, error_details);
852}
853
854ProofVerifier* QuicCryptoClientConfig::proof_verifier() const {
855 return proof_verifier_.get();
856}
857
nharperdf7a77b2019-11-11 13:12:45 -0800858SessionCache* QuicCryptoClientConfig::session_cache() const {
859 return session_cache_.get();
860}
861
QUICHE team2d483ad2020-01-24 15:14:17 -0800862ProofSource* QuicCryptoClientConfig::proof_source() const {
863 return proof_source_.get();
864}
865
866void QuicCryptoClientConfig::set_proof_source(
867 std::unique_ptr<ProofSource> proof_source) {
868 proof_source_ = std::move(proof_source);
869}
870
QUICHE teama6ef0a62019-03-07 20:34:33 -0500871SSL_CTX* QuicCryptoClientConfig::ssl_ctx() const {
872 return ssl_ctx_.get();
873}
874
QUICHE teama6ef0a62019-03-07 20:34:33 -0500875void QuicCryptoClientConfig::InitializeFrom(
876 const QuicServerId& server_id,
877 const QuicServerId& canonical_server_id,
878 QuicCryptoClientConfig* canonical_crypto_config) {
879 CachedState* canonical_cached =
880 canonical_crypto_config->LookupOrCreate(canonical_server_id);
881 if (!canonical_cached->proof_valid()) {
882 return;
883 }
884 CachedState* cached = LookupOrCreate(server_id);
885 cached->InitializeFrom(*canonical_cached);
886}
887
vasilvvc48c8712019-03-11 13:38:16 -0700888void QuicCryptoClientConfig::AddCanonicalSuffix(const std::string& suffix) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500889 canonical_suffixes_.push_back(suffix);
890}
891
892bool QuicCryptoClientConfig::PopulateFromCanonicalConfig(
893 const QuicServerId& server_id,
894 CachedState* server_state) {
895 DCHECK(server_state->IsEmpty());
896 size_t i = 0;
897 for (; i < canonical_suffixes_.size(); ++i) {
dmcardle904ef182019-12-13 08:34:33 -0800898 if (quiche::QuicheTextUtils::EndsWithIgnoreCase(server_id.host(),
899 canonical_suffixes_[i])) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500900 break;
901 }
902 }
903 if (i == canonical_suffixes_.size()) {
904 return false;
905 }
906
907 QuicServerId suffix_server_id(canonical_suffixes_[i], server_id.port(),
908 server_id.privacy_mode_enabled());
909 if (!QuicContainsKey(canonical_server_map_, suffix_server_id)) {
910 // This is the first host we've seen which matches the suffix, so make it
911 // canonical.
912 canonical_server_map_[suffix_server_id] = server_id;
913 return false;
914 }
915
916 const QuicServerId& canonical_server_id =
917 canonical_server_map_[suffix_server_id];
918 CachedState* canonical_state = cached_states_[canonical_server_id].get();
919 if (!canonical_state->proof_valid()) {
920 return false;
921 }
922
923 // Update canonical version to point at the "most recent" entry.
924 canonical_server_map_[suffix_server_id] = server_id;
925
926 server_state->InitializeFrom(*canonical_state);
927 return true;
928}
929
930} // namespace quic