QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 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/crypto_utils.h" |
| 6 | |
| 7 | #include <memory> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 8 | #include <string> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9 | |
| 10 | #include "third_party/boringssl/src/include/openssl/bytestring.h" |
| 11 | #include "third_party/boringssl/src/include/openssl/hkdf.h" |
| 12 | #include "third_party/boringssl/src/include/openssl/sha.h" |
| 13 | #include "net/third_party/quiche/src/quic/core/crypto/aes_128_gcm_decrypter.h" |
| 14 | #include "net/third_party/quiche/src/quic/core/crypto/aes_128_gcm_encrypter.h" |
| 15 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_handshake.h" |
| 16 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_protocol.h" |
| 17 | #include "net/third_party/quiche/src/quic/core/crypto/quic_decrypter.h" |
| 18 | #include "net/third_party/quiche/src/quic/core/crypto/quic_encrypter.h" |
| 19 | #include "net/third_party/quiche/src/quic/core/crypto/quic_hkdf.h" |
| 20 | #include "net/third_party/quiche/src/quic/core/crypto/quic_random.h" |
| 21 | #include "net/third_party/quiche/src/quic/core/quic_data_writer.h" |
| 22 | #include "net/third_party/quiche/src/quic/core/quic_time.h" |
| 23 | #include "net/third_party/quiche/src/quic/core/quic_utils.h" |
| 24 | #include "net/third_party/quiche/src/quic/platform/api/quic_arraysize.h" |
| 25 | #include "net/third_party/quiche/src/quic/platform/api/quic_bug_tracker.h" |
| 26 | #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h" |
| 27 | #include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h" |
| 28 | #include "net/third_party/quiche/src/quic/platform/api/quic_str_cat.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 29 | |
| 30 | namespace quic { |
| 31 | |
| 32 | // TODO(nharper): HkdfExpandLabel and SetKeyAndIV (below) implement what is |
| 33 | // specified in draft-ietf-quic-tls-16. The latest editors' draft has changed |
| 34 | // derivation again, and this will need to be updated to reflect those (and any |
| 35 | // other future) changes. |
| 36 | // static |
| 37 | std::vector<uint8_t> CryptoUtils::HkdfExpandLabel( |
| 38 | const EVP_MD* prf, |
| 39 | const std::vector<uint8_t>& secret, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 40 | const std::string& label, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 41 | size_t out_len) { |
| 42 | bssl::ScopedCBB quic_hkdf_label; |
| 43 | CBB inner_label; |
| 44 | const char label_prefix[] = "quic "; |
| 45 | // The minimum possible length for the QuicHkdfLabel is 9 bytes - 2 bytes for |
| 46 | // Length, plus 1 byte for the length of the inner label, plus the length of |
| 47 | // that label (which is at least 5), plus 1 byte at the end. |
| 48 | if (!CBB_init(quic_hkdf_label.get(), 9) || |
| 49 | !CBB_add_u16(quic_hkdf_label.get(), out_len) || |
| 50 | !CBB_add_u8_length_prefixed(quic_hkdf_label.get(), &inner_label) || |
| 51 | !CBB_add_bytes(&inner_label, |
| 52 | reinterpret_cast<const uint8_t*>(label_prefix), |
| 53 | QUIC_ARRAYSIZE(label_prefix) - 1) || |
| 54 | !CBB_add_bytes(&inner_label, |
| 55 | reinterpret_cast<const uint8_t*>(label.data()), |
| 56 | label.size()) || |
| 57 | !CBB_add_u8(quic_hkdf_label.get(), 0) || |
| 58 | !CBB_flush(quic_hkdf_label.get())) { |
| 59 | QUIC_LOG(ERROR) << "Building HKDF label failed"; |
| 60 | return std::vector<uint8_t>(); |
| 61 | } |
| 62 | std::vector<uint8_t> out; |
| 63 | out.resize(out_len); |
| 64 | if (!HKDF_expand(out.data(), out_len, prf, secret.data(), secret.size(), |
| 65 | CBB_data(quic_hkdf_label.get()), |
| 66 | CBB_len(quic_hkdf_label.get()))) { |
| 67 | QUIC_LOG(ERROR) << "Running HKDF-Expand-Label failed"; |
| 68 | return std::vector<uint8_t>(); |
| 69 | } |
| 70 | return out; |
| 71 | } |
| 72 | |
| 73 | void CryptoUtils::SetKeyAndIV(const EVP_MD* prf, |
| 74 | const std::vector<uint8_t>& pp_secret, |
| 75 | QuicCrypter* crypter) { |
| 76 | std::vector<uint8_t> key = CryptoUtils::HkdfExpandLabel( |
| 77 | prf, pp_secret, "key", crypter->GetKeySize()); |
| 78 | std::vector<uint8_t> iv = |
| 79 | CryptoUtils::HkdfExpandLabel(prf, pp_secret, "iv", crypter->GetIVSize()); |
| 80 | crypter->SetKey( |
| 81 | QuicStringPiece(reinterpret_cast<char*>(key.data()), key.size())); |
| 82 | crypter->SetIV( |
| 83 | QuicStringPiece(reinterpret_cast<char*>(iv.data()), iv.size())); |
| 84 | } |
| 85 | |
| 86 | namespace { |
| 87 | |
| 88 | const uint8_t kInitialSalt[] = {0x9c, 0x10, 0x8f, 0x98, 0x52, 0x0a, 0x5c, |
| 89 | 0x5c, 0x32, 0x96, 0x8e, 0x95, 0x0e, 0x8a, |
| 90 | 0x2c, 0x5f, 0xe0, 0x6d, 0x6c, 0x38}; |
| 91 | |
| 92 | const char kPreSharedKeyLabel[] = "QUIC PSK"; |
| 93 | |
| 94 | } // namespace |
| 95 | |
| 96 | // static |
| 97 | void CryptoUtils::CreateTlsInitialCrypters(Perspective perspective, |
| 98 | QuicTransportVersion version, |
| 99 | QuicConnectionId connection_id, |
| 100 | CrypterPair* crypters) { |
| 101 | QUIC_BUG_IF(!QuicUtils::IsConnectionIdValidForVersion(connection_id, version)) |
| 102 | << "CreateTlsInitialCrypters: attempted to use connection ID " |
| 103 | << connection_id << " which is invalid with version " |
| 104 | << QuicVersionToString(version); |
| 105 | const EVP_MD* hash = EVP_sha256(); |
| 106 | |
| 107 | std::vector<uint8_t> handshake_secret; |
| 108 | handshake_secret.resize(EVP_MAX_MD_SIZE); |
| 109 | size_t handshake_secret_len; |
| 110 | const bool hkdf_extract_success = HKDF_extract( |
| 111 | handshake_secret.data(), &handshake_secret_len, hash, |
| 112 | reinterpret_cast<const uint8_t*>(connection_id.data()), |
| 113 | connection_id.length(), kInitialSalt, QUIC_ARRAYSIZE(kInitialSalt)); |
| 114 | QUIC_BUG_IF(!hkdf_extract_success) |
| 115 | << "HKDF_extract failed when creating initial crypters"; |
| 116 | handshake_secret.resize(handshake_secret_len); |
| 117 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 118 | const std::string client_label = "client in"; |
| 119 | const std::string server_label = "server in"; |
| 120 | std::string encryption_label, decryption_label; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 121 | if (perspective == Perspective::IS_CLIENT) { |
| 122 | encryption_label = client_label; |
| 123 | decryption_label = server_label; |
| 124 | } else { |
| 125 | encryption_label = server_label; |
| 126 | decryption_label = client_label; |
| 127 | } |
| 128 | crypters->encrypter = QuicMakeUnique<Aes128GcmEncrypter>(); |
| 129 | std::vector<uint8_t> encryption_secret = HkdfExpandLabel( |
| 130 | hash, handshake_secret, encryption_label, EVP_MD_size(hash)); |
| 131 | SetKeyAndIV(hash, encryption_secret, crypters->encrypter.get()); |
| 132 | |
| 133 | crypters->decrypter = QuicMakeUnique<Aes128GcmDecrypter>(); |
| 134 | std::vector<uint8_t> decryption_secret = HkdfExpandLabel( |
| 135 | hash, handshake_secret, decryption_label, EVP_MD_size(hash)); |
| 136 | SetKeyAndIV(hash, decryption_secret, crypters->decrypter.get()); |
| 137 | } |
| 138 | |
| 139 | // static |
| 140 | void CryptoUtils::GenerateNonce(QuicWallTime now, |
| 141 | QuicRandom* random_generator, |
| 142 | QuicStringPiece orbit, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 143 | std::string* nonce) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 144 | // a 4-byte timestamp + 28 random bytes. |
| 145 | nonce->reserve(kNonceSize); |
| 146 | nonce->resize(kNonceSize); |
| 147 | |
| 148 | uint32_t gmt_unix_time = static_cast<uint32_t>(now.ToUNIXSeconds()); |
| 149 | // The time in the nonce must be encoded in big-endian because the |
| 150 | // strike-register depends on the nonces being ordered by time. |
| 151 | (*nonce)[0] = static_cast<char>(gmt_unix_time >> 24); |
| 152 | (*nonce)[1] = static_cast<char>(gmt_unix_time >> 16); |
| 153 | (*nonce)[2] = static_cast<char>(gmt_unix_time >> 8); |
| 154 | (*nonce)[3] = static_cast<char>(gmt_unix_time); |
| 155 | size_t bytes_written = 4; |
| 156 | |
| 157 | if (orbit.size() == 8) { |
| 158 | memcpy(&(*nonce)[bytes_written], orbit.data(), orbit.size()); |
| 159 | bytes_written += orbit.size(); |
| 160 | } |
| 161 | |
| 162 | random_generator->RandBytes(&(*nonce)[bytes_written], |
| 163 | kNonceSize - bytes_written); |
| 164 | } |
| 165 | |
| 166 | // static |
| 167 | bool CryptoUtils::DeriveKeys(QuicStringPiece premaster_secret, |
| 168 | QuicTag aead, |
| 169 | QuicStringPiece client_nonce, |
| 170 | QuicStringPiece server_nonce, |
| 171 | QuicStringPiece pre_shared_key, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 172 | const std::string& hkdf_input, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 173 | Perspective perspective, |
| 174 | Diversification diversification, |
| 175 | CrypterPair* crypters, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 176 | std::string* subkey_secret) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 177 | // If the connection is using PSK, concatenate it with the pre-master secret. |
| 178 | std::unique_ptr<char[]> psk_premaster_secret; |
| 179 | if (!pre_shared_key.empty()) { |
| 180 | const QuicStringPiece label(kPreSharedKeyLabel); |
| 181 | const size_t psk_premaster_secret_size = label.size() + 1 + |
| 182 | pre_shared_key.size() + 8 + |
| 183 | premaster_secret.size() + 8; |
| 184 | |
| 185 | psk_premaster_secret = QuicMakeUnique<char[]>(psk_premaster_secret_size); |
| 186 | QuicDataWriter writer(psk_premaster_secret_size, psk_premaster_secret.get(), |
| 187 | HOST_BYTE_ORDER); |
| 188 | |
| 189 | if (!writer.WriteStringPiece(label) || !writer.WriteUInt8(0) || |
| 190 | !writer.WriteStringPiece(pre_shared_key) || |
| 191 | !writer.WriteUInt64(pre_shared_key.size()) || |
| 192 | !writer.WriteStringPiece(premaster_secret) || |
| 193 | !writer.WriteUInt64(premaster_secret.size()) || |
| 194 | writer.remaining() != 0) { |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | premaster_secret = |
| 199 | QuicStringPiece(psk_premaster_secret.get(), psk_premaster_secret_size); |
| 200 | } |
| 201 | |
| 202 | crypters->encrypter = QuicEncrypter::Create(aead); |
| 203 | crypters->decrypter = QuicDecrypter::Create(aead); |
| 204 | size_t key_bytes = crypters->encrypter->GetKeySize(); |
| 205 | size_t nonce_prefix_bytes = crypters->encrypter->GetNoncePrefixSize(); |
| 206 | size_t subkey_secret_bytes = |
| 207 | subkey_secret == nullptr ? 0 : premaster_secret.length(); |
| 208 | |
| 209 | QuicStringPiece nonce = client_nonce; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 210 | std::string nonce_storage; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 211 | if (!server_nonce.empty()) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 212 | nonce_storage = std::string(client_nonce) + std::string(server_nonce); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 213 | nonce = nonce_storage; |
| 214 | } |
| 215 | |
| 216 | QuicHKDF hkdf(premaster_secret, nonce, hkdf_input, key_bytes, |
| 217 | nonce_prefix_bytes, subkey_secret_bytes); |
| 218 | |
| 219 | // Key derivation depends on the key diversification method being employed. |
| 220 | // both the client and the server support never doing key diversification. |
| 221 | // The server also supports immediate diversification, and the client |
| 222 | // supports pending diversification. |
| 223 | switch (diversification.mode()) { |
| 224 | case Diversification::NEVER: { |
| 225 | if (perspective == Perspective::IS_SERVER) { |
| 226 | if (!crypters->encrypter->SetKey(hkdf.server_write_key()) || |
| 227 | !crypters->encrypter->SetNoncePrefix(hkdf.server_write_iv()) || |
| 228 | !crypters->decrypter->SetKey(hkdf.client_write_key()) || |
| 229 | !crypters->decrypter->SetNoncePrefix(hkdf.client_write_iv())) { |
| 230 | return false; |
| 231 | } |
| 232 | } else { |
| 233 | if (!crypters->encrypter->SetKey(hkdf.client_write_key()) || |
| 234 | !crypters->encrypter->SetNoncePrefix(hkdf.client_write_iv()) || |
| 235 | !crypters->decrypter->SetKey(hkdf.server_write_key()) || |
| 236 | !crypters->decrypter->SetNoncePrefix(hkdf.server_write_iv())) { |
| 237 | return false; |
| 238 | } |
| 239 | } |
| 240 | break; |
| 241 | } |
| 242 | case Diversification::PENDING: { |
| 243 | if (perspective == Perspective::IS_SERVER) { |
| 244 | QUIC_BUG << "Pending diversification is only for clients."; |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | if (!crypters->encrypter->SetKey(hkdf.client_write_key()) || |
| 249 | !crypters->encrypter->SetNoncePrefix(hkdf.client_write_iv()) || |
| 250 | !crypters->decrypter->SetPreliminaryKey(hkdf.server_write_key()) || |
| 251 | !crypters->decrypter->SetNoncePrefix(hkdf.server_write_iv())) { |
| 252 | return false; |
| 253 | } |
| 254 | break; |
| 255 | } |
| 256 | case Diversification::NOW: { |
| 257 | if (perspective == Perspective::IS_CLIENT) { |
| 258 | QUIC_BUG << "Immediate diversification is only for servers."; |
| 259 | return false; |
| 260 | } |
| 261 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 262 | std::string key, nonce_prefix; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 263 | QuicDecrypter::DiversifyPreliminaryKey( |
| 264 | hkdf.server_write_key(), hkdf.server_write_iv(), |
| 265 | *diversification.nonce(), key_bytes, nonce_prefix_bytes, &key, |
| 266 | &nonce_prefix); |
| 267 | if (!crypters->decrypter->SetKey(hkdf.client_write_key()) || |
| 268 | !crypters->decrypter->SetNoncePrefix(hkdf.client_write_iv()) || |
| 269 | !crypters->encrypter->SetKey(key) || |
| 270 | !crypters->encrypter->SetNoncePrefix(nonce_prefix)) { |
| 271 | return false; |
| 272 | } |
| 273 | break; |
| 274 | } |
| 275 | default: |
| 276 | DCHECK(false); |
| 277 | } |
| 278 | |
| 279 | if (subkey_secret != nullptr) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 280 | *subkey_secret = std::string(hkdf.subkey_secret()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | return true; |
| 284 | } |
| 285 | |
| 286 | // static |
| 287 | bool CryptoUtils::ExportKeyingMaterial(QuicStringPiece subkey_secret, |
| 288 | QuicStringPiece label, |
| 289 | QuicStringPiece context, |
| 290 | size_t result_len, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 291 | std::string* result) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 292 | for (size_t i = 0; i < label.length(); i++) { |
| 293 | if (label[i] == '\0') { |
| 294 | QUIC_LOG(ERROR) << "ExportKeyingMaterial label may not contain NULs"; |
| 295 | return false; |
| 296 | } |
| 297 | } |
| 298 | // Create HKDF info input: null-terminated label + length-prefixed context |
| 299 | if (context.length() >= std::numeric_limits<uint32_t>::max()) { |
| 300 | QUIC_LOG(ERROR) << "Context value longer than 2^32"; |
| 301 | return false; |
| 302 | } |
| 303 | uint32_t context_length = static_cast<uint32_t>(context.length()); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 304 | std::string info = std::string(label); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 305 | info.push_back('\0'); |
| 306 | info.append(reinterpret_cast<char*>(&context_length), sizeof(context_length)); |
| 307 | info.append(context.data(), context.length()); |
| 308 | |
| 309 | QuicHKDF hkdf(subkey_secret, QuicStringPiece() /* no salt */, info, |
| 310 | result_len, 0 /* no fixed IV */, 0 /* no subkey secret */); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 311 | *result = std::string(hkdf.client_write_key()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 312 | return true; |
| 313 | } |
| 314 | |
| 315 | // static |
| 316 | uint64_t CryptoUtils::ComputeLeafCertHash(QuicStringPiece cert) { |
| 317 | return QuicUtils::FNV1a_64_Hash(cert); |
| 318 | } |
| 319 | |
| 320 | QuicErrorCode CryptoUtils::ValidateServerHello( |
| 321 | const CryptoHandshakeMessage& server_hello, |
| 322 | const ParsedQuicVersionVector& negotiated_versions, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 323 | std::string* error_details) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 324 | DCHECK(error_details != nullptr); |
| 325 | |
| 326 | if (server_hello.tag() != kSHLO) { |
| 327 | *error_details = "Bad tag"; |
| 328 | return QUIC_INVALID_CRYPTO_MESSAGE_TYPE; |
| 329 | } |
| 330 | |
| 331 | QuicVersionLabelVector supported_version_labels; |
| 332 | if (server_hello.GetVersionLabelList(kVER, &supported_version_labels) != |
| 333 | QUIC_NO_ERROR) { |
| 334 | *error_details = "server hello missing version list"; |
| 335 | return QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER; |
| 336 | } |
| 337 | |
| 338 | return ValidateServerHelloVersions(supported_version_labels, |
| 339 | negotiated_versions, error_details); |
| 340 | } |
| 341 | |
| 342 | QuicErrorCode CryptoUtils::ValidateServerHelloVersions( |
| 343 | const QuicVersionLabelVector& server_versions, |
| 344 | const ParsedQuicVersionVector& negotiated_versions, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 345 | std::string* error_details) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 346 | if (!negotiated_versions.empty()) { |
| 347 | bool mismatch = server_versions.size() != negotiated_versions.size(); |
| 348 | for (size_t i = 0; i < server_versions.size() && !mismatch; ++i) { |
| 349 | mismatch = |
| 350 | server_versions[i] != CreateQuicVersionLabel(negotiated_versions[i]); |
| 351 | } |
| 352 | // The server sent a list of supported versions, and the connection |
| 353 | // reports that there was a version negotiation during the handshake. |
| 354 | // Ensure that these two lists are identical. |
| 355 | if (mismatch) { |
| 356 | *error_details = QuicStrCat( |
| 357 | "Downgrade attack detected: ServerVersions(", server_versions.size(), |
| 358 | ")[", QuicVersionLabelVectorToString(server_versions, ",", 30), |
| 359 | "] NegotiatedVersions(", negotiated_versions.size(), ")[", |
| 360 | ParsedQuicVersionVectorToString(negotiated_versions, ",", 30), "]"); |
| 361 | return QUIC_VERSION_NEGOTIATION_MISMATCH; |
| 362 | } |
| 363 | } |
| 364 | return QUIC_NO_ERROR; |
| 365 | } |
| 366 | |
| 367 | QuicErrorCode CryptoUtils::ValidateClientHello( |
| 368 | const CryptoHandshakeMessage& client_hello, |
| 369 | ParsedQuicVersion version, |
| 370 | const ParsedQuicVersionVector& supported_versions, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 371 | std::string* error_details) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 372 | if (client_hello.tag() != kCHLO) { |
| 373 | *error_details = "Bad tag"; |
| 374 | return QUIC_INVALID_CRYPTO_MESSAGE_TYPE; |
| 375 | } |
| 376 | |
| 377 | // If the client's preferred version is not the version we are currently |
| 378 | // speaking, then the client went through a version negotiation. In this |
| 379 | // case, we need to make sure that we actually do not support this version |
| 380 | // and that it wasn't a downgrade attack. |
| 381 | QuicVersionLabel client_version_label; |
| 382 | if (client_hello.GetVersionLabel(kVER, &client_version_label) != |
| 383 | QUIC_NO_ERROR) { |
| 384 | *error_details = "client hello missing version list"; |
| 385 | return QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER; |
| 386 | } |
| 387 | return ValidateClientHelloVersion(client_version_label, version, |
| 388 | supported_versions, error_details); |
| 389 | } |
| 390 | |
| 391 | QuicErrorCode CryptoUtils::ValidateClientHelloVersion( |
| 392 | QuicVersionLabel client_version, |
| 393 | ParsedQuicVersion connection_version, |
| 394 | const ParsedQuicVersionVector& supported_versions, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 395 | std::string* error_details) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 396 | if (client_version != CreateQuicVersionLabel(connection_version)) { |
| 397 | // Check to see if |client_version| is actually on the supported versions |
| 398 | // list. If not, the server doesn't support that version and it's not a |
| 399 | // downgrade attack. |
| 400 | for (size_t i = 0; i < supported_versions.size(); ++i) { |
| 401 | if (client_version == CreateQuicVersionLabel(supported_versions[i])) { |
| 402 | *error_details = QuicStrCat( |
| 403 | "Downgrade attack detected: ClientVersion[", |
| 404 | QuicVersionLabelToString(client_version), "] SupportedVersions(", |
| 405 | supported_versions.size(), ")[", |
| 406 | ParsedQuicVersionVectorToString(supported_versions, ",", 30), "]"); |
| 407 | return QUIC_VERSION_NEGOTIATION_MISMATCH; |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | return QUIC_NO_ERROR; |
| 412 | } |
| 413 | |
| 414 | #define RETURN_STRING_LITERAL(x) \ |
| 415 | case x: \ |
| 416 | return #x |
| 417 | |
| 418 | // Returns the name of the HandshakeFailureReason as a char* |
| 419 | // static |
| 420 | const char* CryptoUtils::HandshakeFailureReasonToString( |
| 421 | HandshakeFailureReason reason) { |
| 422 | switch (reason) { |
| 423 | RETURN_STRING_LITERAL(HANDSHAKE_OK); |
| 424 | RETURN_STRING_LITERAL(CLIENT_NONCE_UNKNOWN_FAILURE); |
| 425 | RETURN_STRING_LITERAL(CLIENT_NONCE_INVALID_FAILURE); |
| 426 | RETURN_STRING_LITERAL(CLIENT_NONCE_NOT_UNIQUE_FAILURE); |
| 427 | RETURN_STRING_LITERAL(CLIENT_NONCE_INVALID_ORBIT_FAILURE); |
| 428 | RETURN_STRING_LITERAL(CLIENT_NONCE_INVALID_TIME_FAILURE); |
| 429 | RETURN_STRING_LITERAL(CLIENT_NONCE_STRIKE_REGISTER_TIMEOUT); |
| 430 | RETURN_STRING_LITERAL(CLIENT_NONCE_STRIKE_REGISTER_FAILURE); |
| 431 | |
| 432 | RETURN_STRING_LITERAL(SERVER_NONCE_DECRYPTION_FAILURE); |
| 433 | RETURN_STRING_LITERAL(SERVER_NONCE_INVALID_FAILURE); |
| 434 | RETURN_STRING_LITERAL(SERVER_NONCE_NOT_UNIQUE_FAILURE); |
| 435 | RETURN_STRING_LITERAL(SERVER_NONCE_INVALID_TIME_FAILURE); |
| 436 | RETURN_STRING_LITERAL(SERVER_NONCE_REQUIRED_FAILURE); |
| 437 | |
| 438 | RETURN_STRING_LITERAL(SERVER_CONFIG_INCHOATE_HELLO_FAILURE); |
| 439 | RETURN_STRING_LITERAL(SERVER_CONFIG_UNKNOWN_CONFIG_FAILURE); |
| 440 | |
| 441 | RETURN_STRING_LITERAL(SOURCE_ADDRESS_TOKEN_INVALID_FAILURE); |
| 442 | RETURN_STRING_LITERAL(SOURCE_ADDRESS_TOKEN_DECRYPTION_FAILURE); |
| 443 | RETURN_STRING_LITERAL(SOURCE_ADDRESS_TOKEN_PARSE_FAILURE); |
| 444 | RETURN_STRING_LITERAL(SOURCE_ADDRESS_TOKEN_DIFFERENT_IP_ADDRESS_FAILURE); |
| 445 | RETURN_STRING_LITERAL(SOURCE_ADDRESS_TOKEN_CLOCK_SKEW_FAILURE); |
| 446 | RETURN_STRING_LITERAL(SOURCE_ADDRESS_TOKEN_EXPIRED_FAILURE); |
| 447 | |
| 448 | RETURN_STRING_LITERAL(INVALID_EXPECTED_LEAF_CERTIFICATE); |
| 449 | RETURN_STRING_LITERAL(MAX_FAILURE_REASON); |
| 450 | } |
| 451 | // Return a default value so that we return this when |reason| doesn't match |
| 452 | // any HandshakeFailureReason.. This can happen when the message by the peer |
| 453 | // (attacker) has invalid reason. |
| 454 | return "INVALID_HANDSHAKE_FAILURE_REASON"; |
| 455 | } |
| 456 | |
| 457 | // static |
QUICHE team | 84910bd | 2019-03-15 07:03:40 -0700 | [diff] [blame] | 458 | std::string CryptoUtils::HashHandshakeMessage( |
| 459 | const CryptoHandshakeMessage& message, |
| 460 | Perspective perspective) { |
rch | d5d13c2 | 2019-03-18 14:31:09 -0700 | [diff] [blame] | 461 | std::string output; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 462 | const QuicData& serialized = message.GetSerialized(); |
| 463 | uint8_t digest[SHA256_DIGEST_LENGTH]; |
| 464 | SHA256(reinterpret_cast<const uint8_t*>(serialized.data()), |
| 465 | serialized.length(), digest); |
QUICHE team | 84910bd | 2019-03-15 07:03:40 -0700 | [diff] [blame] | 466 | output.assign(reinterpret_cast<const char*>(digest), sizeof(digest)); |
| 467 | return output; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | #undef RETURN_STRING_LITERAL // undef for jumbo builds |
| 471 | } // namespace quic |