QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "net/third_party/quiche/src/quic/core/crypto/quic_crypto_client_config.h" |
| 6 | |
| 7 | #include <algorithm> |
| 8 | #include <memory> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 9 | #include <string> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 10 | |
| 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 14 | #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" |
nharper | 6ebe83b | 2019-06-13 17:43:52 -0700 | [diff] [blame] | 24 | #include "net/third_party/quiche/src/quic/core/crypto/tls_client_connection.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 25 | #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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 28 | #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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 30 | #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" |
bnc | 4e9283d | 2019-12-17 07:08:57 -0800 | [diff] [blame] | 34 | #include "net/third_party/quiche/src/common/platform/api/quiche_arraysize.h" |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 35 | #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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 37 | |
| 38 | namespace quic { |
| 39 | |
| 40 | namespace { |
| 41 | |
| 42 | // Tracks the reason (the state of the server config) for sending inchoate |
| 43 | // ClientHello to the server. |
| 44 | void 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. |
| 52 | void 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 | |
| 61 | QuicCryptoClientConfig::QuicCryptoClientConfig( |
nharper | 6ebe83b | 2019-06-13 17:43:52 -0700 | [diff] [blame] | 62 | std::unique_ptr<ProofVerifier> proof_verifier) |
nharper | df7a77b | 2019-11-11 13:12:45 -0800 | [diff] [blame] | 63 | : QuicCryptoClientConfig(std::move(proof_verifier), nullptr) {} |
| 64 | |
| 65 | QuicCryptoClientConfig::QuicCryptoClientConfig( |
| 66 | std::unique_ptr<ProofVerifier> proof_verifier, |
| 67 | std::unique_ptr<SessionCache> session_cache) |
nharper | 1c06fd8 | 2019-06-24 14:33:34 -0700 | [diff] [blame] | 68 | : proof_verifier_(std::move(proof_verifier)), |
nharper | df7a77b | 2019-11-11 13:12:45 -0800 | [diff] [blame] | 69 | session_cache_(std::move(session_cache)), |
nharper | 1c06fd8 | 2019-06-24 14:33:34 -0700 | [diff] [blame] | 70 | ssl_ctx_(TlsClientConnection::CreateSslCtx()) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 71 | DCHECK(proof_verifier_.get()); |
| 72 | SetDefaults(); |
| 73 | } |
| 74 | |
| 75 | QuicCryptoClientConfig::~QuicCryptoClientConfig() {} |
| 76 | |
| 77 | QuicCryptoClientConfig::CachedState::CachedState() |
| 78 | : server_config_valid_(false), |
| 79 | expiration_time_(QuicWallTime::Zero()), |
| 80 | generation_counter_(0) {} |
| 81 | |
| 82 | QuicCryptoClientConfig::CachedState::~CachedState() {} |
| 83 | |
| 84 | bool 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 | |
| 118 | bool QuicCryptoClientConfig::CachedState::IsEmpty() const { |
| 119 | return server_config_.empty(); |
| 120 | } |
| 121 | |
| 122 | const CryptoHandshakeMessage* |
| 123 | QuicCryptoClientConfig::CachedState::GetServerConfig() const { |
| 124 | if (server_config_.empty()) { |
| 125 | return nullptr; |
| 126 | } |
| 127 | |
wub | 07a2b07 | 2019-10-24 11:23:20 -0700 | [diff] [blame] | 128 | if (!scfg_) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 129 | scfg_ = CryptoFramer::ParseMessage(server_config_); |
| 130 | DCHECK(scfg_.get()); |
| 131 | } |
| 132 | return scfg_.get(); |
| 133 | } |
| 134 | |
| 135 | void QuicCryptoClientConfig::CachedState::add_server_designated_connection_id( |
| 136 | QuicConnectionId connection_id) { |
| 137 | server_designated_connection_ids_.push(connection_id); |
| 138 | } |
| 139 | |
| 140 | bool QuicCryptoClientConfig::CachedState::has_server_designated_connection_id() |
| 141 | const { |
| 142 | return !server_designated_connection_ids_.empty(); |
| 143 | } |
| 144 | |
| 145 | void QuicCryptoClientConfig::CachedState::add_server_nonce( |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 146 | const std::string& server_nonce) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 147 | server_nonces_.push(server_nonce); |
| 148 | } |
| 149 | |
| 150 | bool QuicCryptoClientConfig::CachedState::has_server_nonce() const { |
| 151 | return !server_nonces_.empty(); |
| 152 | } |
| 153 | |
| 154 | QuicCryptoClientConfig::CachedState::ServerConfigState |
| 155 | QuicCryptoClientConfig::CachedState::SetServerConfig( |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 156 | quiche::QuicheStringPiece server_config, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 157 | QuicWallTime now, |
| 158 | QuicWallTime expiry_time, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 159 | std::string* error_details) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 160 | 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) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 196 | server_config_ = std::string(server_config); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 197 | SetProofInvalid(); |
| 198 | scfg_ = std::move(new_scfg_storage); |
| 199 | } |
| 200 | return SERVER_CONFIG_VALID; |
| 201 | } |
| 202 | |
| 203 | void 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 | |
| 212 | void QuicCryptoClientConfig::CachedState::SetProof( |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 213 | const std::vector<std::string>& certs, |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 214 | quiche::QuicheStringPiece cert_sct, |
| 215 | quiche::QuicheStringPiece chlo_hash, |
| 216 | quiche::QuicheStringPiece signature) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 217 | 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; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 236 | cert_sct_ = std::string(cert_sct); |
| 237 | chlo_hash_ = std::string(chlo_hash); |
| 238 | server_config_sig_ = std::string(signature); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | void 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 | |
| 257 | void QuicCryptoClientConfig::CachedState::ClearProof() { |
| 258 | SetProofInvalid(); |
| 259 | certs_.clear(); |
| 260 | cert_sct_.clear(); |
| 261 | chlo_hash_.clear(); |
| 262 | server_config_sig_.clear(); |
| 263 | } |
| 264 | |
| 265 | void QuicCryptoClientConfig::CachedState::SetProofValid() { |
| 266 | server_config_valid_ = true; |
| 267 | } |
| 268 | |
| 269 | void QuicCryptoClientConfig::CachedState::SetProofInvalid() { |
| 270 | server_config_valid_ = false; |
| 271 | ++generation_counter_; |
| 272 | } |
| 273 | |
| 274 | bool QuicCryptoClientConfig::CachedState::Initialize( |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 275 | quiche::QuicheStringPiece server_config, |
| 276 | quiche::QuicheStringPiece source_address_token, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 277 | const std::vector<std::string>& certs, |
| 278 | const std::string& cert_sct, |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 279 | quiche::QuicheStringPiece chlo_hash, |
| 280 | quiche::QuicheStringPiece signature, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 281 | 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 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 290 | std::string error_details; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 291 | 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 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 308 | const std::string& QuicCryptoClientConfig::CachedState::server_config() const { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 309 | return server_config_; |
| 310 | } |
| 311 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 312 | const std::string& QuicCryptoClientConfig::CachedState::source_address_token() |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 313 | const { |
| 314 | return source_address_token_; |
| 315 | } |
| 316 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 317 | const std::vector<std::string>& QuicCryptoClientConfig::CachedState::certs() |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 318 | const { |
| 319 | return certs_; |
| 320 | } |
| 321 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 322 | const std::string& QuicCryptoClientConfig::CachedState::cert_sct() const { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 323 | return cert_sct_; |
| 324 | } |
| 325 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 326 | const std::string& QuicCryptoClientConfig::CachedState::chlo_hash() const { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 327 | return chlo_hash_; |
| 328 | } |
| 329 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 330 | const std::string& QuicCryptoClientConfig::CachedState::signature() const { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 331 | return server_config_sig_; |
| 332 | } |
| 333 | |
| 334 | bool QuicCryptoClientConfig::CachedState::proof_valid() const { |
| 335 | return server_config_valid_; |
| 336 | } |
| 337 | |
| 338 | uint64_t QuicCryptoClientConfig::CachedState::generation_counter() const { |
| 339 | return generation_counter_; |
| 340 | } |
| 341 | |
| 342 | const ProofVerifyDetails* |
| 343 | QuicCryptoClientConfig::CachedState::proof_verify_details() const { |
| 344 | return proof_verify_details_.get(); |
| 345 | } |
| 346 | |
| 347 | void QuicCryptoClientConfig::CachedState::set_source_address_token( |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 348 | quiche::QuicheStringPiece token) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 349 | source_address_token_ = std::string(token); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | void QuicCryptoClientConfig::CachedState::set_cert_sct( |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 353 | quiche::QuicheStringPiece cert_sct) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 354 | cert_sct_ = std::string(cert_sct); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | void QuicCryptoClientConfig::CachedState::SetProofVerifyDetails( |
| 358 | ProofVerifyDetails* details) { |
| 359 | proof_verify_details_.reset(details); |
| 360 | } |
| 361 | |
| 362 | void 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 | |
| 381 | QuicConnectionId |
| 382 | QuicCryptoClientConfig::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 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 393 | std::string QuicCryptoClientConfig::CachedState::GetNextServerNonce() { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 394 | if (server_nonces_.empty()) { |
| 395 | QUIC_BUG |
| 396 | << "Attempting to consume a server nonce that was never designated."; |
| 397 | return ""; |
| 398 | } |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 399 | const std::string server_nonce = server_nonces_.front(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 400 | server_nonces_.pop(); |
| 401 | return server_nonce; |
| 402 | } |
| 403 | |
| 404 | void 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 | |
| 417 | QuicCryptoClientConfig::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 | |
| 433 | void 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 | |
| 440 | void 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) { |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 475 | quiche::QuicheStringPiece scid; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 476 | 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]; |
bnc | 4e9283d | 2019-12-17 07:08:57 -0800 | [diff] [blame] | 490 | rand->RandBytes(proof_nonce, QUICHE_ARRAYSIZE(proof_nonce)); |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 491 | out->SetStringPiece(kNONP, quiche::QuicheStringPiece( |
bnc | 4e9283d | 2019-12-17 07:08:57 -0800 | [diff] [blame] | 492 | proof_nonce, QUICHE_ARRAYSIZE(proof_nonce))); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 493 | |
| 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 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 502 | const std::vector<std::string>& certs = cached->certs(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 503 | // 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 | |
| 518 | QuicErrorCode QuicCryptoClientConfig::FillClientHello( |
| 519 | const QuicServerId& server_id, |
| 520 | QuicConnectionId connection_id, |
| 521 | const ParsedQuicVersion preferred_version, |
nharper | c1bbfe6 | 2019-09-27 16:48:40 -0700 | [diff] [blame] | 522 | const ParsedQuicVersion actual_version, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 523 | const CachedState* cached, |
| 524 | QuicWallTime now, |
| 525 | QuicRandom* rand, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 526 | QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters> out_params, |
| 527 | CryptoHandshakeMessage* out, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 528 | std::string* error_details) const { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 529 | 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 | |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 553 | quiche::QuicheStringPiece scid; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 554 | 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 | |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 585 | quiche::QuicheStringPiece public_value; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 586 | 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 | |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 592 | quiche::QuicheStringPiece orbit; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 593 | 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 team | fe1aca6 | 2019-03-14 13:39:01 -0700 | [diff] [blame] | 619 | if (!out_params->client_key_exchange->CalculateSharedKeySync( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 620 | 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 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 626 | const std::vector<std::string>& certs = cached->certs(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 627 | 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 633 | // 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(); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 638 | out_params->hkdf_input_suffix.append(connection_id.data(), |
| 639 | connection_id.length()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 640 | 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 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 650 | std::string hkdf_input; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 651 | 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 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 656 | std::string* subkey_secret = &out_params->initial_subkey_secret; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 657 | |
nharper | c1bbfe6 | 2019-09-27 16:48:40 -0700 | [diff] [blame] | 658 | 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 team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 664 | *error_details = "Symmetric key setup failed"; |
| 665 | return QUIC_CRYPTO_SYMMETRIC_KEY_SETUP_FAILED; |
| 666 | } |
| 667 | |
| 668 | return QUIC_NO_ERROR; |
| 669 | } |
| 670 | |
| 671 | QuicErrorCode QuicCryptoClientConfig::CacheNewServerConfig( |
| 672 | const CryptoHandshakeMessage& message, |
| 673 | QuicWallTime now, |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 674 | QuicTransportVersion /*version*/, |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 675 | quiche::QuicheStringPiece chlo_hash, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 676 | const std::vector<std::string>& cached_certs, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 677 | CachedState* cached, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 678 | std::string* error_details) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 679 | DCHECK(error_details != nullptr); |
| 680 | |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 681 | quiche::QuicheStringPiece scfg; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 682 | 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 | |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 706 | quiche::QuicheStringPiece token; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 707 | if (message.GetStringPiece(kSourceAddressTokenTag, &token)) { |
| 708 | cached->set_source_address_token(token); |
| 709 | } |
| 710 | |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 711 | quiche::QuicheStringPiece proof, cert_bytes, cert_sct; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 712 | bool has_proof = message.GetStringPiece(kPROF, &proof); |
| 713 | bool has_cert = message.GetStringPiece(kCertificateTag, &cert_bytes); |
| 714 | if (has_proof && has_cert) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 715 | std::vector<std::string> certs; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 716 | 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 | |
| 743 | QuicErrorCode QuicCryptoClientConfig::ProcessRejection( |
| 744 | const CryptoHandshakeMessage& rej, |
| 745 | QuicWallTime now, |
| 746 | const QuicTransportVersion version, |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 747 | quiche::QuicheStringPiece chlo_hash, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 748 | CachedState* cached, |
| 749 | QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters> out_params, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 750 | std::string* error_details) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 751 | DCHECK(error_details != nullptr); |
| 752 | |
wub | 0a4b9c5 | 2019-05-28 13:18:58 -0700 | [diff] [blame] | 753 | if (rej.tag() != kREJ) { |
| 754 | *error_details = "Message is not REJ"; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 755 | 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 | |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 765 | quiche::QuicheStringPiece nonce; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 766 | if (rej.GetStringPiece(kServerNonceTag, &nonce)) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 767 | out_params->server_nonce = std::string(nonce); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 768 | } |
| 769 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 770 | return QUIC_NO_ERROR; |
| 771 | } |
| 772 | |
| 773 | QuicErrorCode QuicCryptoClientConfig::ProcessServerHello( |
| 774 | const CryptoHandshakeMessage& server_hello, |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 775 | QuicConnectionId /*connection_id*/, |
nharper | c1bbfe6 | 2019-09-27 16:48:40 -0700 | [diff] [blame] | 776 | ParsedQuicVersion version, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 777 | const ParsedQuicVersionVector& negotiated_versions, |
| 778 | CachedState* cached, |
| 779 | QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters> out_params, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 780 | std::string* error_details) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 781 | 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. |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 790 | quiche::QuicheStringPiece token; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 791 | if (server_hello.GetStringPiece(kSourceAddressTokenTag, &token)) { |
| 792 | cached->set_source_address_token(token); |
| 793 | } |
| 794 | |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 795 | quiche::QuicheStringPiece shlo_nonce; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 796 | 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 | |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 804 | quiche::QuicheStringPiece public_value; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 805 | 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 team | fe1aca6 | 2019-03-14 13:39:01 -0700 | [diff] [blame] | 810 | if (!out_params->client_key_exchange->CalculateSharedKeySync( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 811 | public_value, &out_params->forward_secure_premaster_secret)) { |
| 812 | *error_details = "Key exchange failure"; |
| 813 | return QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER; |
| 814 | } |
| 815 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 816 | std::string hkdf_input; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 817 | 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( |
nharper | c1bbfe6 | 2019-09-27 16:48:40 -0700 | [diff] [blame] | 823 | version, out_params->forward_secure_premaster_secret, |
| 824 | out_params->aead, out_params->client_nonce, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 825 | 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 | |
| 836 | QuicErrorCode QuicCryptoClientConfig::ProcessServerConfigUpdate( |
| 837 | const CryptoHandshakeMessage& server_config_update, |
| 838 | QuicWallTime now, |
| 839 | const QuicTransportVersion version, |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 840 | quiche::QuicheStringPiece chlo_hash, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 841 | CachedState* cached, |
| 842 | QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters> out_params, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 843 | std::string* error_details) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 844 | 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 | |
| 854 | ProofVerifier* QuicCryptoClientConfig::proof_verifier() const { |
| 855 | return proof_verifier_.get(); |
| 856 | } |
| 857 | |
nharper | df7a77b | 2019-11-11 13:12:45 -0800 | [diff] [blame] | 858 | SessionCache* QuicCryptoClientConfig::session_cache() const { |
| 859 | return session_cache_.get(); |
| 860 | } |
| 861 | |
QUICHE team | 2d483ad | 2020-01-24 15:14:17 -0800 | [diff] [blame^] | 862 | ProofSource* QuicCryptoClientConfig::proof_source() const { |
| 863 | return proof_source_.get(); |
| 864 | } |
| 865 | |
| 866 | void QuicCryptoClientConfig::set_proof_source( |
| 867 | std::unique_ptr<ProofSource> proof_source) { |
| 868 | proof_source_ = std::move(proof_source); |
| 869 | } |
| 870 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 871 | SSL_CTX* QuicCryptoClientConfig::ssl_ctx() const { |
| 872 | return ssl_ctx_.get(); |
| 873 | } |
| 874 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 875 | void 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 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 888 | void QuicCryptoClientConfig::AddCanonicalSuffix(const std::string& suffix) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 889 | canonical_suffixes_.push_back(suffix); |
| 890 | } |
| 891 | |
| 892 | bool 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) { |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 898 | if (quiche::QuicheTextUtils::EndsWithIgnoreCase(server_id.host(), |
| 899 | canonical_suffixes_[i])) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 900 | 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 |