| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) |
| 2 |
// Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 3 |
// Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 4 |
// Copyright 2005 Nokia. All rights reserved. |
| 5 |
// |
| 6 |
// ECC cipher suite support in OpenSSL originally written by |
| 7 |
// Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories. |
| 8 |
// |
| 9 |
// The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 10 |
// Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 11 |
// support (see RFC 4279) to OpenSSL. |
| 12 |
// |
| 13 |
// SPDX-License-Identifier: Apache-2.0 |
| 14 |
|
| 15 |
#include <openssl/ssl.h> |
| 16 |
|
| 17 |
#include <assert.h> |
| 18 |
#include <string.h> |
| 19 |
|
| 20 |
#include <openssl/bn.h> |
| 21 |
#include <openssl/bytestring.h> |
| 22 |
#include <openssl/cipher.h> |
| 23 |
#include <openssl/curve25519.h> |
| 24 |
#include <openssl/digest.h> |
| 25 |
#include <openssl/ec.h> |
| 26 |
#include <openssl/ecdsa.h> |
| 27 |
#include <openssl/err.h> |
| 28 |
#include <openssl/evp.h> |
| 29 |
#include <openssl/hmac.h> |
| 30 |
#include <openssl/md5.h> |
| 31 |
#include <openssl/mem.h> |
| 32 |
#include <openssl/nid.h> |
| 33 |
#include <openssl/rand.h> |
| 34 |
#include <openssl/x509.h> |
| 35 |
|
| 36 |
#include "internal.h" |
| 37 |
#include "../crypto/internal.h" |
| 38 |
|
| 39 |
|
| 40 |
BSSL_NAMESPACE_BEGIN |
| 41 |
|
| 42 |
bool ssl_client_cipher_list_contains_cipher( |
| 43 |
const SSL_CLIENT_HELLO *client_hello, uint16_t id) { |
| 44 |
CBS cipher_suites; |
| 45 |
CBS_init(&cipher_suites, client_hello->cipher_suites, |
| 46 |
client_hello->cipher_suites_len); |
| 47 |
|
| 48 |
while (CBS_len(&cipher_suites) > 0) { |
| 49 |
uint16_t got_id; |
| 50 |
if (!CBS_get_u16(&cipher_suites, &got_id)) { |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
if (got_id == id) { |
| 55 |
return true; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
return false; |
| 60 |
} |
| 61 |
|
| 62 |
static bool negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 63 |
const SSL_CLIENT_HELLO *client_hello) { |
| 64 |
SSL *const ssl = hs->ssl; |
| 65 |
assert(!ssl->s3->have_version); |
| 66 |
CBS supported_versions, versions; |
| 67 |
if (ssl_client_hello_get_extension(client_hello, &supported_versions, |
| 68 |
TLSEXT_TYPE_supported_versions)) { |
| 69 |
if (!CBS_get_u8_length_prefixed(&supported_versions, &versions) || |
| 70 |
CBS_len(&supported_versions) != 0 || |
| 71 |
CBS_len(&versions) == 0) { |
| 72 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 73 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 74 |
return false; |
| 75 |
} |
| 76 |
} else { |
| 77 |
// Convert the ClientHello version to an equivalent supported_versions |
| 78 |
// extension. |
| 79 |
static const uint8_t kTLSVersions[] = { |
| 80 |
0x03, 0x03, // TLS 1.2 |
| 81 |
0x03, 0x02, // TLS 1.1 |
| 82 |
0x03, 0x01, // TLS 1 |
| 83 |
}; |
| 84 |
|
| 85 |
static const uint8_t kDTLSVersions[] = { |
| 86 |
0xfe, 0xfd, // DTLS 1.2 |
| 87 |
0xfe, 0xff, // DTLS 1.0 |
| 88 |
}; |
| 89 |
|
| 90 |
size_t versions_len = 0; |
| 91 |
if (SSL_is_dtls(ssl)) { |
| 92 |
if (client_hello->version <= DTLS1_2_VERSION) { |
| 93 |
versions_len = 4; |
| 94 |
} else if (client_hello->version <= DTLS1_VERSION) { |
| 95 |
versions_len = 2; |
| 96 |
} |
| 97 |
CBS_init(&versions, kDTLSVersions + sizeof(kDTLSVersions) - versions_len, |
| 98 |
versions_len); |
| 99 |
} else { |
| 100 |
if (client_hello->version >= TLS1_2_VERSION) { |
| 101 |
versions_len = 6; |
| 102 |
} else if (client_hello->version >= TLS1_1_VERSION) { |
| 103 |
versions_len = 4; |
| 104 |
} else if (client_hello->version >= TLS1_VERSION) { |
| 105 |
versions_len = 2; |
| 106 |
} |
| 107 |
CBS_init(&versions, kTLSVersions + sizeof(kTLSVersions) - versions_len, |
| 108 |
versions_len); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
if (!ssl_negotiate_version(hs, out_alert, &ssl->version, &versions)) { |
| 113 |
return false; |
| 114 |
} |
| 115 |
|
| 116 |
// At this point, the connection's version is known and |ssl->version| is |
| 117 |
// fixed. Begin enforcing the record-layer version. |
| 118 |
ssl->s3->have_version = true; |
| 119 |
ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version); |
| 120 |
|
| 121 |
// Handle FALLBACK_SCSV. |
| 122 |
if (ssl_client_cipher_list_contains_cipher(client_hello, |
| 123 |
SSL3_CK_FALLBACK_SCSV & 0xffff) && |
| 124 |
ssl_protocol_version(ssl) < hs->max_version) { |
| 125 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK); |
| 126 |
*out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK; |
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
return true; |
| 131 |
} |
| 132 |
|
| 133 |
bool ssl_parse_client_cipher_list( |
| 134 |
SSL *ssl, const SSL_CLIENT_HELLO *client_hello, UniquePtr<STACK_OF(SSL_CIPHER)> *ciphers_out) { |
| 135 |
ciphers_out->reset(); |
| 136 |
|
| 137 |
CBS cipher_suites; |
| 138 |
CBS_init(&cipher_suites, client_hello->cipher_suites, |
| 139 |
client_hello->cipher_suites_len); |
| 140 |
|
| 141 |
// Store raw bytes for cipher suites offered |
| 142 |
ssl->all_client_cipher_suites.reset(static_cast<char *>(OPENSSL_memdup( |
| 143 |
client_hello->cipher_suites, client_hello->cipher_suites_len))); |
| 144 |
ssl->all_client_cipher_suites_len = client_hello->cipher_suites_len; |
| 145 |
|
| 146 |
// Cipher suites are encoded as 2-byte unsigned integers |
| 147 |
if (CBS_len(&cipher_suites) % 2 != 0) { |
| 148 |
OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST); |
| 149 |
return false; |
| 150 |
} |
| 151 |
|
| 152 |
UniquePtr<STACK_OF(SSL_CIPHER)> sk(sk_SSL_CIPHER_new_null()); |
| 153 |
if (!sk) { |
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
while (CBS_len(&cipher_suites) > 0) { |
| 158 |
uint16_t cipher_suite; |
| 159 |
|
| 160 |
if (!CBS_get_u16(&cipher_suites, &cipher_suite)) { |
| 161 |
OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST); |
| 162 |
return false; |
| 163 |
} |
| 164 |
|
| 165 |
// Storing only supported ciphers |
| 166 |
const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite); |
| 167 |
if (c != NULL && !sk_SSL_CIPHER_push(sk.get(), c)) { |
| 168 |
OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST); |
| 169 |
return false; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
*ciphers_out = std::move(sk); |
| 174 |
|
| 175 |
return true; |
| 176 |
} |
| 177 |
|
| 178 |
// ssl_get_compatible_server_ciphers determines the key exchange and |
| 179 |
// authentication cipher suite masks compatible with the server configuration |
| 180 |
// and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key |
| 181 |
// exchange mask and |*out_mask_a| to the authentication mask. |
| 182 |
static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs, |
| 183 |
uint32_t *out_mask_k, |
| 184 |
uint32_t *out_mask_a) { |
| 185 |
uint32_t mask_k = 0; |
| 186 |
uint32_t mask_a = 0; |
| 187 |
|
| 188 |
if (ssl_has_certificate(hs)) { |
| 189 |
CERT *cert = hs->config->cert.get(); |
| 190 |
mask_a |= ssl_cipher_auth_mask_for_key(hs->local_pubkey.get()); |
| 191 |
if (EVP_PKEY_id(hs->local_pubkey.get()) == EVP_PKEY_RSA) { |
| 192 |
mask_k |= SSL_kRSA; |
| 193 |
} |
| 194 |
// Also loop through all available private keys and set authentication masks |
| 195 |
// accordingly to indicate support. |
| 196 |
// |cert_private_keys| is already checked above in |ssl_has_certificate|. |
| 197 |
for (auto & cert_private_key : cert->cert_private_keys) { |
| 198 |
EVP_PKEY *private_key = cert_private_key.privatekey.get(); |
| 199 |
if(private_key != nullptr) { |
| 200 |
mask_a |= ssl_cipher_auth_mask_for_key(private_key); |
| 201 |
if (EVP_PKEY_id(private_key) == EVP_PKEY_RSA) { |
| 202 |
mask_k |= SSL_kRSA; |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
// Check for a shared group to consider ECDHE ciphers. |
| 209 |
uint16_t unused; |
| 210 |
if (tls1_get_shared_group(hs, &unused)) { |
| 211 |
mask_k |= SSL_kECDHE; |
| 212 |
} |
| 213 |
|
| 214 |
// PSK requires a server callback. |
| 215 |
if (hs->config->psk_server_callback != NULL) { |
| 216 |
mask_k |= SSL_kPSK; |
| 217 |
mask_a |= SSL_aPSK; |
| 218 |
} |
| 219 |
|
| 220 |
*out_mask_k = mask_k; |
| 221 |
*out_mask_a = mask_a; |
| 222 |
} |
| 223 |
|
| 224 |
static const SSL_CIPHER *choose_cipher(SSL_HANDSHAKE *hs, |
| 225 |
const SSLCipherPreferenceList *server_pref) { |
| 226 |
SSL *const ssl = hs->ssl; |
| 227 |
const STACK_OF(SSL_CIPHER) *prio, *allow; |
| 228 |
// in_group_flags will either be NULL, or will point to an array of bytes |
| 229 |
// which indicate equal-preference groups in the |prio| stack. See the |
| 230 |
// comment about |in_group_flags| in the |SSLCipherPreferenceList| |
| 231 |
// struct. |
| 232 |
const bool *in_group_flags; |
| 233 |
// group_min contains the minimal index so far found in a group, or -1 if no |
| 234 |
// such value exists yet. |
| 235 |
int group_min = -1; |
| 236 |
|
| 237 |
if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) { |
| 238 |
prio = server_pref->ciphers.get(); |
| 239 |
in_group_flags = server_pref->in_group_flags; |
| 240 |
allow = ssl->client_cipher_suites.get(); |
| 241 |
} else { |
| 242 |
prio = ssl->client_cipher_suites.get(); |
| 243 |
in_group_flags = NULL; |
| 244 |
allow = server_pref->ciphers.get(); |
| 245 |
} |
| 246 |
|
| 247 |
uint32_t mask_k, mask_a; |
| 248 |
ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a); |
| 249 |
|
| 250 |
for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) { |
| 251 |
const SSL_CIPHER *c = sk_SSL_CIPHER_value(prio, i); |
| 252 |
|
| 253 |
size_t cipher_index; |
| 254 |
if (// Check if the cipher is supported for the current version. |
| 255 |
SSL_CIPHER_get_min_version(c) <= ssl_protocol_version(ssl) && |
| 256 |
ssl_protocol_version(ssl) <= SSL_CIPHER_get_max_version(c) && |
| 257 |
// Check the cipher is supported for the server configuration. |
| 258 |
(c->algorithm_mkey & mask_k) && |
| 259 |
(c->algorithm_auth & mask_a) && |
| 260 |
// Check the cipher is in the |allow| list. |
| 261 |
sk_SSL_CIPHER_find_awslc(allow, &cipher_index, c)) { |
| 262 |
if (in_group_flags != NULL && in_group_flags[i]) { |
| 263 |
// This element of |prio| is in a group. Update the minimum index found |
| 264 |
// so far and continue looking. |
| 265 |
if (group_min == -1 || (size_t)group_min > cipher_index) { |
| 266 |
group_min = cipher_index; |
| 267 |
} |
| 268 |
} else { |
| 269 |
if (group_min != -1 && (size_t)group_min < cipher_index) { |
| 270 |
cipher_index = group_min; |
| 271 |
} |
| 272 |
return sk_SSL_CIPHER_value(allow, cipher_index); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
if (in_group_flags != NULL && !in_group_flags[i] && group_min != -1) { |
| 277 |
// We are about to leave a group, but we found a match in it, so that's |
| 278 |
// our answer. |
| 279 |
return sk_SSL_CIPHER_value(allow, group_min); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
return nullptr; |
| 284 |
} |
| 285 |
|
| 286 |
static enum ssl_hs_wait_t do_start_accept(SSL_HANDSHAKE *hs) { |
| 287 |
ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_START, 1); |
| 288 |
hs->state = state12_read_client_hello; |
| 289 |
ssl_update_counter(hs->ssl->session_ctx.get(), |
| 290 |
hs->ssl->session_ctx->stats.sess_accept, true); |
| 291 |
return ssl_hs_ok; |
| 292 |
} |
| 293 |
|
| 294 |
// is_probably_jdk11_with_tls13 returns whether |client_hello| was probably sent |
| 295 |
// from a JDK 11 client with both TLS 1.3 and a prior version enabled. |
| 296 |
static bool is_probably_jdk11_with_tls13(const SSL_CLIENT_HELLO *client_hello) { |
| 297 |
// JDK 11 ClientHellos contain a number of unusual properties which should |
| 298 |
// limit false positives. |
| 299 |
|
| 300 |
// JDK 11 does not support ChaCha20-Poly1305. This is unusual: many modern |
| 301 |
// clients implement ChaCha20-Poly1305. |
| 302 |
if (ssl_client_cipher_list_contains_cipher( |
| 303 |
client_hello, TLS1_3_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) { |
| 304 |
return false; |
| 305 |
} |
| 306 |
|
| 307 |
// JDK 11 always sends extensions in a particular order. |
| 308 |
constexpr uint16_t kMaxFragmentLength = 0x0001; |
| 309 |
constexpr uint16_t kStatusRequestV2 = 0x0011; |
| 310 |
static CONSTEXPR_ARRAY struct { |
| 311 |
uint16_t id; |
| 312 |
bool required; |
| 313 |
} kJavaExtensions[] = { |
| 314 |
{TLSEXT_TYPE_server_name, false}, |
| 315 |
{kMaxFragmentLength, false}, |
| 316 |
{TLSEXT_TYPE_status_request, false}, |
| 317 |
{TLSEXT_TYPE_supported_groups, true}, |
| 318 |
{TLSEXT_TYPE_ec_point_formats, false}, |
| 319 |
{TLSEXT_TYPE_signature_algorithms, true}, |
| 320 |
// Java always sends signature_algorithms_cert. |
| 321 |
{TLSEXT_TYPE_signature_algorithms_cert, true}, |
| 322 |
{TLSEXT_TYPE_application_layer_protocol_negotiation, false}, |
| 323 |
{kStatusRequestV2, false}, |
| 324 |
{TLSEXT_TYPE_extended_master_secret, false}, |
| 325 |
{TLSEXT_TYPE_supported_versions, true}, |
| 326 |
{TLSEXT_TYPE_cookie, false}, |
| 327 |
{TLSEXT_TYPE_psk_key_exchange_modes, true}, |
| 328 |
{TLSEXT_TYPE_key_share, true}, |
| 329 |
{TLSEXT_TYPE_renegotiate, false}, |
| 330 |
{TLSEXT_TYPE_pre_shared_key, false}, |
| 331 |
}; |
| 332 |
Span<const uint8_t> sigalgs, sigalgs_cert; |
| 333 |
bool has_status_request = false, has_status_request_v2 = false; |
| 334 |
CBS extensions, supported_groups; |
| 335 |
CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len); |
| 336 |
for (const auto &java_extension : kJavaExtensions) { |
| 337 |
CBS copy = extensions; |
| 338 |
uint16_t id; |
| 339 |
if (CBS_get_u16(©, &id) && id == java_extension.id) { |
| 340 |
// The next extension is the one we expected. |
| 341 |
extensions = copy; |
| 342 |
CBS body; |
| 343 |
if (!CBS_get_u16_length_prefixed(&extensions, &body)) { |
| 344 |
return false; |
| 345 |
} |
| 346 |
switch (id) { |
| 347 |
case TLSEXT_TYPE_status_request: |
| 348 |
has_status_request = true; |
| 349 |
break; |
| 350 |
case kStatusRequestV2: |
| 351 |
has_status_request_v2 = true; |
| 352 |
break; |
| 353 |
case TLSEXT_TYPE_signature_algorithms: |
| 354 |
sigalgs = body; |
| 355 |
break; |
| 356 |
case TLSEXT_TYPE_signature_algorithms_cert: |
| 357 |
sigalgs_cert = body; |
| 358 |
break; |
| 359 |
case TLSEXT_TYPE_supported_groups: |
| 360 |
supported_groups = body; |
| 361 |
break; |
| 362 |
} |
| 363 |
} else if (java_extension.required) { |
| 364 |
return false; |
| 365 |
} |
| 366 |
} |
| 367 |
if (CBS_len(&extensions) != 0) { |
| 368 |
return false; |
| 369 |
} |
| 370 |
|
| 371 |
// JDK 11 never advertises X25519. It is not offered by default, and |
| 372 |
// -Djdk.tls.namedGroups=x25519 does not work. This is unusual: many modern |
| 373 |
// clients implement X25519. |
| 374 |
while (CBS_len(&supported_groups) > 0) { |
| 375 |
uint16_t group; |
| 376 |
if (!CBS_get_u16(&supported_groups, &group) || |
| 377 |
group == SSL_GROUP_X25519) { |
| 378 |
return false; |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
if (// JDK 11 always sends the same contents in signature_algorithms and |
| 383 |
// signature_algorithms_cert. This is unusual: signature_algorithms_cert, |
| 384 |
// if omitted, is treated as if it were signature_algorithms. |
| 385 |
sigalgs != sigalgs_cert || |
| 386 |
// When TLS 1.2 or below is enabled, JDK 11 sends status_request_v2 iff it |
| 387 |
// sends status_request. This is unusual: status_request_v2 is not widely |
| 388 |
// implemented. |
| 389 |
has_status_request != has_status_request_v2) { |
| 390 |
return false; |
| 391 |
} |
| 392 |
|
| 393 |
return true; |
| 394 |
} |
| 395 |
|
| 396 |
static bool decrypt_ech(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 397 |
const SSL_CLIENT_HELLO *client_hello) { |
| 398 |
SSL *const ssl = hs->ssl; |
| 399 |
CBS body; |
| 400 |
if (!ssl_client_hello_get_extension(client_hello, &body, |
| 401 |
TLSEXT_TYPE_encrypted_client_hello)) { |
| 402 |
return true; |
| 403 |
} |
| 404 |
uint8_t type; |
| 405 |
if (!CBS_get_u8(&body, &type)) { |
| 406 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 407 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 408 |
return false; |
| 409 |
} |
| 410 |
if (type != ECH_CLIENT_OUTER) { |
| 411 |
return true; |
| 412 |
} |
| 413 |
// This is a ClientHelloOuter ECH extension. Attempt to decrypt it. |
| 414 |
uint8_t config_id; |
| 415 |
uint16_t kdf_id, aead_id; |
| 416 |
CBS enc, payload; |
| 417 |
if (!CBS_get_u16(&body, &kdf_id) || // |
| 418 |
!CBS_get_u16(&body, &aead_id) || // |
| 419 |
!CBS_get_u8(&body, &config_id) || |
| 420 |
!CBS_get_u16_length_prefixed(&body, &enc) || |
| 421 |
!CBS_get_u16_length_prefixed(&body, &payload) || // |
| 422 |
CBS_len(&body) != 0) { |
| 423 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 424 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 425 |
return false; |
| 426 |
} |
| 427 |
|
| 428 |
{ |
| 429 |
MutexReadLock lock(&ssl->ctx->lock); |
| 430 |
hs->ech_keys = UpRef(ssl->ctx->ech_keys); |
| 431 |
} |
| 432 |
|
| 433 |
if (!hs->ech_keys) { |
| 434 |
ssl->s3->ech_status = ssl_ech_rejected; |
| 435 |
return true; |
| 436 |
} |
| 437 |
|
| 438 |
for (const auto &config : hs->ech_keys->configs) { |
| 439 |
hs->ech_hpke_ctx.Reset(); |
| 440 |
if (config_id != config->ech_config().config_id || |
| 441 |
!config->SetupContext(hs->ech_hpke_ctx.get(), kdf_id, aead_id, enc)) { |
| 442 |
// Ignore the error and try another ECHConfig. |
| 443 |
ERR_clear_error(); |
| 444 |
continue; |
| 445 |
} |
| 446 |
bool is_decrypt_error; |
| 447 |
if (!ssl_client_hello_decrypt(hs, out_alert, &is_decrypt_error, |
| 448 |
&hs->ech_client_hello_buf, client_hello, |
| 449 |
payload)) { |
| 450 |
if (is_decrypt_error) { |
| 451 |
// Ignore the error and try another ECHConfig. |
| 452 |
ERR_clear_error(); |
| 453 |
// The |out_alert| calling convention currently relies on a default of |
| 454 |
// |SSL_AD_DECODE_ERROR|. https://crbug.com/boringssl/373 tracks |
| 455 |
// switching to sum types, which avoids this. |
| 456 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 457 |
continue; |
| 458 |
} |
| 459 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED); |
| 460 |
return false; |
| 461 |
} |
| 462 |
hs->ech_config_id = config_id; |
| 463 |
ssl->s3->ech_status = ssl_ech_accepted; |
| 464 |
return true; |
| 465 |
} |
| 466 |
|
| 467 |
// If we did not accept ECH, proceed with the ClientHelloOuter. Note this |
| 468 |
// could be key mismatch or ECH GREASE, so we must complete the handshake |
| 469 |
// as usual, except EncryptedExtensions will contain retry configs. |
| 470 |
ssl->s3->ech_status = ssl_ech_rejected; |
| 471 |
return true; |
| 472 |
} |
| 473 |
|
| 474 |
static bool extract_sni(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 475 |
const SSL_CLIENT_HELLO *client_hello) { |
| 476 |
SSL *const ssl = hs->ssl; |
| 477 |
CBS sni; |
| 478 |
if (!ssl_client_hello_get_extension(client_hello, &sni, |
| 479 |
TLSEXT_TYPE_server_name)) { |
| 480 |
// No SNI extension to parse. |
| 481 |
return true; |
| 482 |
} |
| 483 |
|
| 484 |
CBS server_name_list, host_name; |
| 485 |
uint8_t name_type; |
| 486 |
if (!CBS_get_u16_length_prefixed(&sni, &server_name_list) || |
| 487 |
!CBS_get_u8(&server_name_list, &name_type) || |
| 488 |
// Although the server_name extension was intended to be extensible to |
| 489 |
// new name types and multiple names, OpenSSL 1.0.x had a bug which meant |
| 490 |
// different name types will cause an error. Further, RFC 4366 originally |
| 491 |
// defined syntax inextensibly. RFC 6066 corrected this mistake, but |
| 492 |
// adding new name types is no longer feasible. |
| 493 |
// |
| 494 |
// Act as if the extensibility does not exist to simplify parsing. |
| 495 |
!CBS_get_u16_length_prefixed(&server_name_list, &host_name) || |
| 496 |
CBS_len(&server_name_list) != 0 || |
| 497 |
CBS_len(&sni) != 0) { |
| 498 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 499 |
return false; |
| 500 |
} |
| 501 |
|
| 502 |
if (name_type != TLSEXT_NAMETYPE_host_name || |
| 503 |
CBS_len(&host_name) == 0 || |
| 504 |
CBS_len(&host_name) > TLSEXT_MAXLEN_host_name || |
| 505 |
CBS_contains_zero_byte(&host_name)) { |
| 506 |
*out_alert = SSL_AD_UNRECOGNIZED_NAME; |
| 507 |
return false; |
| 508 |
} |
| 509 |
|
| 510 |
// Copy the hostname as a string. |
| 511 |
char *raw = nullptr; |
| 512 |
if (!CBS_strdup(&host_name, &raw)) { |
| 513 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 514 |
return false; |
| 515 |
} |
| 516 |
ssl->s3->hostname.reset(raw); |
| 517 |
|
| 518 |
hs->should_ack_sni = true; |
| 519 |
return true; |
| 520 |
} |
| 521 |
|
| 522 |
static enum ssl_hs_wait_t do_read_client_hello(SSL_HANDSHAKE *hs) { |
| 523 |
SSL *const ssl = hs->ssl; |
| 524 |
|
| 525 |
SSLMessage msg; |
| 526 |
if (!ssl->method->get_message(ssl, &msg)) { |
| 527 |
return ssl_hs_read_message; |
| 528 |
} |
| 529 |
|
| 530 |
if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_HELLO)) { |
| 531 |
return ssl_hs_error; |
| 532 |
} |
| 533 |
|
| 534 |
SSL_CLIENT_HELLO client_hello; |
| 535 |
if (!ssl_client_hello_init(ssl, &client_hello, msg.body)) { |
| 536 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 537 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 538 |
return ssl_hs_error; |
| 539 |
} |
| 540 |
|
| 541 |
// ClientHello should be the end of the flight. We check this early to cover |
| 542 |
// all protocol versions. |
| 543 |
if (ssl->method->has_unprocessed_handshake_data(ssl)) { |
| 544 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 545 |
OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESS_HANDSHAKE_DATA); |
| 546 |
return ssl_hs_error; |
| 547 |
} |
| 548 |
|
| 549 |
if (hs->config->handoff) { |
| 550 |
return ssl_hs_handoff; |
| 551 |
} |
| 552 |
|
| 553 |
uint8_t alert = SSL_AD_DECODE_ERROR; |
| 554 |
if (!decrypt_ech(hs, &alert, &client_hello)) { |
| 555 |
ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 556 |
return ssl_hs_error; |
| 557 |
} |
| 558 |
|
| 559 |
// ECH may have changed which ClientHello we process. Update |msg| and |
| 560 |
// |client_hello| in case. |
| 561 |
if (!hs->GetClientHello(&msg, &client_hello)) { |
| 562 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 563 |
return ssl_hs_error; |
| 564 |
} |
| 565 |
|
| 566 |
if (!extract_sni(hs, &alert, &client_hello)) { |
| 567 |
ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 568 |
return ssl_hs_error; |
| 569 |
} |
| 570 |
|
| 571 |
hs->state = state12_read_client_hello_after_ech; |
| 572 |
return ssl_hs_ok; |
| 573 |
} |
| 574 |
|
| 575 |
static enum ssl_hs_wait_t do_read_client_hello_after_ech(SSL_HANDSHAKE *hs) { |
| 576 |
SSL *const ssl = hs->ssl; |
| 577 |
|
| 578 |
SSLMessage msg_unused; |
| 579 |
SSL_CLIENT_HELLO client_hello; |
| 580 |
if (!hs->GetClientHello(&msg_unused, &client_hello)) { |
| 581 |
return ssl_hs_error; |
| 582 |
} |
| 583 |
|
| 584 |
// Finished parsing the ClientHello, now we can start processing it. |
| 585 |
// Give the ClientHello callback a crack at things |
| 586 |
const SSL_CTX* sctx = ssl->ctx.get(); |
| 587 |
if (sctx != NULL && sctx->client_hello_cb != NULL) { |
| 588 |
int al = SSL_AD_INTERNAL_ERROR; |
| 589 |
// A failure in the ClientHello callback terminates the connection. |
| 590 |
switch (sctx->client_hello_cb(ssl, &al, sctx->client_hello_cb_arg)) { |
| 591 |
case SSL_CLIENT_HELLO_SUCCESS: |
| 592 |
break; |
| 593 |
case SSL_CLIENT_HELLO_RETRY: |
| 594 |
// This case is treated as failure. |
| 595 |
OPENSSL_FALLTHROUGH; |
| 596 |
case SSL_CLIENT_HELLO_ERROR: |
| 597 |
default: |
| 598 |
OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED); |
| 599 |
ssl_send_alert(ssl, SSL3_AL_FATAL, al); |
| 600 |
return ssl_hs_error; |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
// Run the early callback. |
| 605 |
if (ssl->ctx->select_certificate_cb != NULL) { |
| 606 |
switch (ssl->ctx->select_certificate_cb(&client_hello)) { |
| 607 |
case ssl_select_cert_retry: |
| 608 |
return ssl_hs_certificate_selection_pending; |
| 609 |
|
| 610 |
case ssl_select_cert_error: |
| 611 |
// Connection rejected. |
| 612 |
OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED); |
| 613 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 614 |
return ssl_hs_error; |
| 615 |
|
| 616 |
default: |
| 617 |
/* fallthrough */; |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
// Freeze the version range after the early callback. |
| 622 |
if (!ssl_get_version_range(hs, &hs->min_version, &hs->max_version)) { |
| 623 |
return ssl_hs_error; |
| 624 |
} |
| 625 |
|
| 626 |
if (hs->config->jdk11_workaround && |
| 627 |
is_probably_jdk11_with_tls13(&client_hello)) { |
| 628 |
hs->apply_jdk11_workaround = true; |
| 629 |
} |
| 630 |
|
| 631 |
uint8_t alert = SSL_AD_DECODE_ERROR; |
| 632 |
if (!negotiate_version(hs, &alert, &client_hello)) { |
| 633 |
ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 634 |
return ssl_hs_error; |
| 635 |
} |
| 636 |
|
| 637 |
hs->client_version = client_hello.version; |
| 638 |
if (client_hello.random_len != SSL3_RANDOM_SIZE) { |
| 639 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 640 |
return ssl_hs_error; |
| 641 |
} |
| 642 |
OPENSSL_memcpy(ssl->s3->client_random, client_hello.random, |
| 643 |
client_hello.random_len); |
| 644 |
|
| 645 |
// Only null compression is supported. TLS 1.3 further requires the peer |
| 646 |
// advertise no other compression. |
| 647 |
if (OPENSSL_memchr(client_hello.compression_methods, 0, |
| 648 |
client_hello.compression_methods_len) == NULL || |
| 649 |
(ssl_protocol_version(ssl) >= TLS1_3_VERSION && |
| 650 |
client_hello.compression_methods_len != 1)) { |
| 651 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMPRESSION_LIST); |
| 652 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 653 |
return ssl_hs_error; |
| 654 |
} |
| 655 |
|
| 656 |
// TLS extensions. |
| 657 |
if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) { |
| 658 |
OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
| 659 |
return ssl_hs_error; |
| 660 |
} |
| 661 |
|
| 662 |
hs->state = state12_select_certificate; |
| 663 |
return ssl_hs_ok; |
| 664 |
} |
| 665 |
|
| 666 |
static enum ssl_hs_wait_t do_select_certificate(SSL_HANDSHAKE *hs) { |
| 667 |
SSL *const ssl = hs->ssl; |
| 668 |
|
| 669 |
// Call |cert_cb| to update server certificates if required. |
| 670 |
if (hs->config->cert->cert_cb != NULL) { |
| 671 |
int rv = hs->config->cert->cert_cb(ssl, hs->config->cert->cert_cb_arg); |
| 672 |
if (rv == 0) { |
| 673 |
OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR); |
| 674 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 675 |
return ssl_hs_error; |
| 676 |
} |
| 677 |
if (rv < 0) { |
| 678 |
return ssl_hs_x509_lookup; |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
// Load |hs->local_pubkey| from the cert (if present) prematurely. The |
| 683 |
// certificate could be subject to change once we negotiate signature |
| 684 |
// algorithms later. If it changes to another leaf certificate the server and |
| 685 |
// client has support for, we reload it. The public key may only be absent if |
| 686 |
// PSK is enabled on the server, as indicated by presense of a callback. |
| 687 |
if (!ssl_handshake_load_local_pubkey(hs) && |
| 688 |
!(hs->local_pubkey == nullptr && hs->config->psk_server_callback)) { |
| 689 |
return ssl_hs_error; |
| 690 |
} |
| 691 |
|
| 692 |
if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 693 |
// Jump to the TLS 1.3 state machine. |
| 694 |
hs->state = state12_tls13; |
| 695 |
return ssl_hs_ok; |
| 696 |
} |
| 697 |
|
| 698 |
// It should not be possible to negotiate TLS 1.2 with ECH. The |
| 699 |
// ClientHelloInner decoding function rejects ClientHellos which offer TLS 1.2 |
| 700 |
// or below. |
| 701 |
assert(ssl->s3->ech_status != ssl_ech_accepted); |
| 702 |
|
| 703 |
ssl->s3->early_data_reason = ssl_early_data_protocol_version; |
| 704 |
|
| 705 |
SSLMessage msg_unused; |
| 706 |
SSL_CLIENT_HELLO client_hello; |
| 707 |
if (!hs->GetClientHello(&msg_unused, &client_hello)) { |
| 708 |
return ssl_hs_error; |
| 709 |
} |
| 710 |
|
| 711 |
if (!ssl_parse_client_cipher_list(ssl, &client_hello, &ssl->client_cipher_suites)) { |
| 712 |
OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER); |
| 713 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 714 |
return ssl_hs_error; |
| 715 |
} |
| 716 |
|
| 717 |
// Negotiate the cipher suite. This must be done after |cert_cb| so the |
| 718 |
// certificate is finalized. |
| 719 |
SSLCipherPreferenceList *prefs = hs->config->cipher_list |
| 720 |
? hs->config->cipher_list.get() |
| 721 |
: ssl->ctx->cipher_list.get(); |
| 722 |
hs->new_cipher = choose_cipher(hs, prefs); |
| 723 |
if (hs->new_cipher == NULL) { |
| 724 |
OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER); |
| 725 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 726 |
return ssl_hs_error; |
| 727 |
} |
| 728 |
|
| 729 |
// Determine the signature algorithm. |
| 730 |
if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
| 731 |
if (!ssl_has_private_key(hs)) { |
| 732 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 733 |
return ssl_hs_error; |
| 734 |
} |
| 735 |
|
| 736 |
if (!tls1_choose_signature_algorithm(hs, &hs->signature_algorithm)) { |
| 737 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 738 |
return ssl_hs_error; |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
// Certificate is finalized here since it's subject to change based on the |
| 743 |
// negotiated signature algorithms. |
| 744 |
if (!ssl_on_certificate_selected(hs)) { |
| 745 |
return ssl_hs_error; |
| 746 |
} |
| 747 |
|
| 748 |
if (!tls1_call_ocsp_stapling_callback(hs)) { |
| 749 |
return ssl_hs_error; |
| 750 |
} |
| 751 |
|
| 752 |
hs->state = state12_select_parameters; |
| 753 |
return ssl_hs_ok; |
| 754 |
} |
| 755 |
|
| 756 |
static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) { |
| 757 |
enum ssl_hs_wait_t wait = tls13_server_handshake(hs); |
| 758 |
if (wait == ssl_hs_ok) { |
| 759 |
hs->state = state12_finish_server_handshake; |
| 760 |
return ssl_hs_ok; |
| 761 |
} |
| 762 |
|
| 763 |
return wait; |
| 764 |
} |
| 765 |
|
| 766 |
static enum ssl_hs_wait_t do_select_parameters(SSL_HANDSHAKE *hs) { |
| 767 |
SSL *const ssl = hs->ssl; |
| 768 |
|
| 769 |
SSLMessage msg; |
| 770 |
if (!ssl->method->get_message(ssl, &msg)) { |
| 771 |
return ssl_hs_read_message; |
| 772 |
} |
| 773 |
|
| 774 |
SSL_CLIENT_HELLO client_hello; |
| 775 |
if (!ssl_client_hello_init(ssl, &client_hello, msg.body)) { |
| 776 |
return ssl_hs_error; |
| 777 |
} |
| 778 |
|
| 779 |
hs->session_id_len = client_hello.session_id_len; |
| 780 |
// This is checked in |ssl_client_hello_init|. |
| 781 |
assert(hs->session_id_len <= sizeof(hs->session_id)); |
| 782 |
OPENSSL_memcpy(hs->session_id, client_hello.session_id, hs->session_id_len); |
| 783 |
|
| 784 |
// Determine whether we are doing session resumption. |
| 785 |
UniquePtr<SSL_SESSION> session; |
| 786 |
bool tickets_supported = false, renew_ticket = false; |
| 787 |
enum ssl_hs_wait_t wait = ssl_get_prev_session( |
| 788 |
hs, &session, &tickets_supported, &renew_ticket, &client_hello); |
| 789 |
if (wait != ssl_hs_ok) { |
| 790 |
return wait; |
| 791 |
} |
| 792 |
|
| 793 |
if (session) { |
| 794 |
if (session->extended_master_secret && !hs->extended_master_secret) { |
| 795 |
// A ClientHello without EMS that attempts to resume a session with EMS |
| 796 |
// is fatal to the connection. |
| 797 |
OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION); |
| 798 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 799 |
return ssl_hs_error; |
| 800 |
} |
| 801 |
|
| 802 |
if (!ssl_session_is_resumable(hs, session.get()) || |
| 803 |
// If the client offers the EMS extension, but the previous session |
| 804 |
// didn't use it, then negotiate a new session. |
| 805 |
hs->extended_master_secret != session->extended_master_secret) { |
| 806 |
session.reset(); |
| 807 |
} |
| 808 |
} |
| 809 |
|
| 810 |
if (session) { |
| 811 |
// Use the old session. |
| 812 |
hs->ticket_expected = renew_ticket; |
| 813 |
ssl->session = std::move(session); |
| 814 |
ssl->s3->session_reused = true; |
| 815 |
hs->can_release_private_key = true; |
| 816 |
ssl->verify_result = ssl->session->verify_result; |
| 817 |
} else { |
| 818 |
hs->ticket_expected = tickets_supported; |
| 819 |
ssl_set_session(ssl, nullptr); |
| 820 |
if (!ssl_get_new_session(hs)) { |
| 821 |
return ssl_hs_error; |
| 822 |
} |
| 823 |
|
| 824 |
// Assign a session ID if not using session tickets. |
| 825 |
if (!hs->ticket_expected && |
| 826 |
(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) { |
| 827 |
hs->new_session->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; |
| 828 |
RAND_bytes(hs->new_session->session_id, |
| 829 |
hs->new_session->session_id_length); |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
if (ssl->ctx->dos_protection_cb != NULL && |
| 834 |
ssl->ctx->dos_protection_cb(&client_hello) == 0) { |
| 835 |
// Connection rejected for DOS reasons. |
| 836 |
OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED); |
| 837 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 838 |
return ssl_hs_error; |
| 839 |
} |
| 840 |
|
| 841 |
if (ssl->session == NULL) { |
| 842 |
hs->new_session->cipher = hs->new_cipher; |
| 843 |
|
| 844 |
// Determine whether to request a client certificate. |
| 845 |
hs->cert_request = !!(hs->config->verify_mode & SSL_VERIFY_PEER); |
| 846 |
// Only request a certificate if Channel ID isn't negotiated. |
| 847 |
if ((hs->config->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) && |
| 848 |
hs->channel_id_negotiated) { |
| 849 |
hs->cert_request = false; |
| 850 |
} |
| 851 |
// CertificateRequest may only be sent in certificate-based ciphers. |
| 852 |
if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
| 853 |
hs->cert_request = false; |
| 854 |
} |
| 855 |
|
| 856 |
if (!hs->cert_request) { |
| 857 |
// OpenSSL returns X509_V_OK when no certificates are requested. This is |
| 858 |
// classed by them as a bug, but it's assumed by at least NGINX. |
| 859 |
hs->new_session->verify_result = X509_V_OK; |
| 860 |
ssl->verify_result = hs->new_session->verify_result; |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
// HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was |
| 865 |
// deferred. Complete it now. |
| 866 |
uint8_t alert = SSL_AD_DECODE_ERROR; |
| 867 |
if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) { |
| 868 |
ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 869 |
return ssl_hs_error; |
| 870 |
} |
| 871 |
|
| 872 |
// Now that all parameters are known, initialize the handshake hash and hash |
| 873 |
// the ClientHello. |
| 874 |
if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) || |
| 875 |
!ssl_hash_message(hs, msg)) { |
| 876 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 877 |
return ssl_hs_error; |
| 878 |
} |
| 879 |
|
| 880 |
// Handback includes the whole handshake transcript, so we cannot free the |
| 881 |
// transcript buffer in the handback case. |
| 882 |
if (!hs->cert_request && !hs->handback) { |
| 883 |
hs->transcript.FreeBuffer(); |
| 884 |
} |
| 885 |
|
| 886 |
ssl->method->next_message(ssl); |
| 887 |
|
| 888 |
hs->state = state12_send_server_hello; |
| 889 |
return ssl_hs_ok; |
| 890 |
} |
| 891 |
|
| 892 |
static void copy_suffix(Span<uint8_t> out, Span<const uint8_t> in) { |
| 893 |
out = out.last(in.size()); |
| 894 |
OPENSSL_memcpy(out.data(), in.data(), in.size()); |
| 895 |
} |
| 896 |
|
| 897 |
static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) { |
| 898 |
SSL *const ssl = hs->ssl; |
| 899 |
|
| 900 |
// We only accept ChannelIDs on connections with ECDHE in order to avoid a |
| 901 |
// known attack while we fix ChannelID itself. |
| 902 |
if (hs->channel_id_negotiated && |
| 903 |
(hs->new_cipher->algorithm_mkey & SSL_kECDHE) == 0) { |
| 904 |
hs->channel_id_negotiated = false; |
| 905 |
} |
| 906 |
|
| 907 |
// If this is a resumption and the original handshake didn't support |
| 908 |
// ChannelID then we didn't record the original handshake hashes in the |
| 909 |
// session and so cannot resume with ChannelIDs. |
| 910 |
if (ssl->session != NULL && |
| 911 |
ssl->session->original_handshake_hash_len == 0) { |
| 912 |
hs->channel_id_negotiated = false; |
| 913 |
} |
| 914 |
|
| 915 |
SSL_HANDSHAKE_HINTS *const hints = hs->hints.get(); |
| 916 |
if (hints && !hs->hints_requested && |
| 917 |
hints->server_random_tls12.size() == SSL3_RANDOM_SIZE) { |
| 918 |
OPENSSL_memcpy(ssl->s3->server_random, hints->server_random_tls12.data(), |
| 919 |
SSL3_RANDOM_SIZE); |
| 920 |
} else { |
| 921 |
struct OPENSSL_timeval now; |
| 922 |
ssl_get_current_time(ssl, &now); |
| 923 |
CRYPTO_store_u32_be(ssl->s3->server_random, |
| 924 |
static_cast<uint32_t>(now.tv_sec)); |
| 925 |
if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) { |
| 926 |
return ssl_hs_error; |
| 927 |
} |
| 928 |
if (hints && hs->hints_requested && |
| 929 |
!hints->server_random_tls12.CopyFrom(ssl->s3->server_random)) { |
| 930 |
return ssl_hs_error; |
| 931 |
} |
| 932 |
} |
| 933 |
|
| 934 |
// Implement the TLS 1.3 anti-downgrade feature. |
| 935 |
if (ssl_supports_version(hs, TLS1_3_VERSION)) { |
| 936 |
if (ssl_protocol_version(ssl) == TLS1_2_VERSION) { |
| 937 |
if (hs->apply_jdk11_workaround) { |
| 938 |
// JDK 11 implements the TLS 1.3 downgrade signal, so we cannot send it |
| 939 |
// here. However, the signal is only effective if all TLS 1.2 |
| 940 |
// ServerHellos produced by the server are marked. Thus we send a |
| 941 |
// different non-standard signal for the time being, until JDK 11.0.2 is |
| 942 |
// released and clients have updated. |
| 943 |
copy_suffix(ssl->s3->server_random, kJDK11DowngradeRandom); |
| 944 |
} else { |
| 945 |
copy_suffix(ssl->s3->server_random, kTLS13DowngradeRandom); |
| 946 |
} |
| 947 |
} else { |
| 948 |
copy_suffix(ssl->s3->server_random, kTLS12DowngradeRandom); |
| 949 |
} |
| 950 |
} |
| 951 |
|
| 952 |
Span<const uint8_t> session_id; |
| 953 |
if (ssl->session != nullptr) { |
| 954 |
// Echo the session ID from the ClientHello to indicate resumption. |
| 955 |
session_id = MakeConstSpan(hs->session_id, hs->session_id_len); |
| 956 |
} else { |
| 957 |
session_id = MakeConstSpan(hs->new_session->session_id, |
| 958 |
hs->new_session->session_id_length); |
| 959 |
} |
| 960 |
|
| 961 |
ScopedCBB cbb; |
| 962 |
CBB body, session_id_bytes; |
| 963 |
if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) || |
| 964 |
!CBB_add_u16(&body, ssl->version) || |
| 965 |
!CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) || |
| 966 |
!CBB_add_u8_length_prefixed(&body, &session_id_bytes) || |
| 967 |
!CBB_add_bytes(&session_id_bytes, session_id.data(), session_id.size()) || |
| 968 |
!CBB_add_u16(&body, SSL_CIPHER_get_protocol_id(hs->new_cipher)) || |
| 969 |
!CBB_add_u8(&body, 0 /* no compression */) || |
| 970 |
!ssl_add_serverhello_tlsext(hs, &body) || |
| 971 |
!ssl_add_message_cbb(ssl, cbb.get())) { |
| 972 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 973 |
return ssl_hs_error; |
| 974 |
} |
| 975 |
|
| 976 |
if (ssl->session != nullptr) { |
| 977 |
// No additional hints to generate in resumption. |
| 978 |
if (hs->hints_requested) { |
| 979 |
return ssl_hs_hints_ready; |
| 980 |
} |
| 981 |
hs->state = state12_send_server_finished; |
| 982 |
} else { |
| 983 |
hs->state = state12_send_server_certificate; |
| 984 |
} |
| 985 |
return ssl_hs_ok; |
| 986 |
} |
| 987 |
|
| 988 |
static enum ssl_hs_wait_t do_send_server_certificate(SSL_HANDSHAKE *hs) { |
| 989 |
SSL *const ssl = hs->ssl; |
| 990 |
ScopedCBB cbb; |
| 991 |
|
| 992 |
if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
| 993 |
if (!ssl_has_certificate(hs)) { |
| 994 |
OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET); |
| 995 |
return ssl_hs_error; |
| 996 |
} |
| 997 |
|
| 998 |
if (!ssl_output_cert_chain(hs)) { |
| 999 |
return ssl_hs_error; |
| 1000 |
} |
| 1001 |
|
| 1002 |
if (hs->certificate_status_expected) { |
| 1003 |
CBB body, ocsp_response; |
| 1004 |
if (!ssl->method->init_message(ssl, cbb.get(), &body, |
| 1005 |
SSL3_MT_CERTIFICATE_STATUS) || |
| 1006 |
!CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) || |
| 1007 |
!CBB_add_u24_length_prefixed(&body, &ocsp_response) || |
| 1008 |
!CBB_add_bytes( |
| 1009 |
&ocsp_response, |
| 1010 |
CRYPTO_BUFFER_data(hs->config->cert->ocsp_response.get()), |
| 1011 |
CRYPTO_BUFFER_len(hs->config->cert->ocsp_response.get())) || |
| 1012 |
!ssl_add_message_cbb(ssl, cbb.get())) { |
| 1013 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1014 |
return ssl_hs_error; |
| 1015 |
} |
| 1016 |
} |
| 1017 |
} |
| 1018 |
|
| 1019 |
// Assemble ServerKeyExchange parameters if needed. |
| 1020 |
uint32_t alg_k = hs->new_cipher->algorithm_mkey; |
| 1021 |
uint32_t alg_a = hs->new_cipher->algorithm_auth; |
| 1022 |
if (ssl_cipher_requires_server_key_exchange(hs->new_cipher) || |
| 1023 |
((alg_a & SSL_aPSK) && hs->config->psk_identity_hint)) { |
| 1024 |
// Pre-allocate enough room to comfortably fit an ECDHE public key. Prepend |
| 1025 |
// the client and server randoms for the signing transcript. |
| 1026 |
CBB child; |
| 1027 |
if (!CBB_init(cbb.get(), SSL3_RANDOM_SIZE * 2 + 128) || |
| 1028 |
!CBB_add_bytes(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) || |
| 1029 |
!CBB_add_bytes(cbb.get(), ssl->s3->server_random, SSL3_RANDOM_SIZE)) { |
| 1030 |
return ssl_hs_error; |
| 1031 |
} |
| 1032 |
|
| 1033 |
// PSK ciphers begin with an identity hint. |
| 1034 |
if (alg_a & SSL_aPSK) { |
| 1035 |
size_t len = hs->config->psk_identity_hint == nullptr |
| 1036 |
? 0 |
| 1037 |
: strlen(hs->config->psk_identity_hint.get()); |
| 1038 |
if (!CBB_add_u16_length_prefixed(cbb.get(), &child) || |
| 1039 |
!CBB_add_bytes(&child, |
| 1040 |
(const uint8_t *)hs->config->psk_identity_hint.get(), |
| 1041 |
len)) { |
| 1042 |
return ssl_hs_error; |
| 1043 |
} |
| 1044 |
} |
| 1045 |
|
| 1046 |
if (alg_k & SSL_kECDHE) { |
| 1047 |
// Determine the group to use. |
| 1048 |
uint16_t group_id; |
| 1049 |
if (!tls1_get_shared_group(hs, &group_id)) { |
| 1050 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1051 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1052 |
return ssl_hs_error; |
| 1053 |
} |
| 1054 |
hs->new_session->group_id = group_id; |
| 1055 |
|
| 1056 |
hs->key_shares[0] = SSLKeyShare::Create(group_id); |
| 1057 |
if (!hs->key_shares[0] || |
| 1058 |
!CBB_add_u8(cbb.get(), NAMED_CURVE_TYPE) || |
| 1059 |
!CBB_add_u16(cbb.get(), group_id) || |
| 1060 |
!CBB_add_u8_length_prefixed(cbb.get(), &child)) { |
| 1061 |
return ssl_hs_error; |
| 1062 |
} |
| 1063 |
|
| 1064 |
SSL_HANDSHAKE_HINTS *const hints = hs->hints.get(); |
| 1065 |
bool hint_ok = false; |
| 1066 |
if (hints && !hs->hints_requested && |
| 1067 |
hints->ecdhe_group_id == group_id && |
| 1068 |
!hints->ecdhe_public_key.empty() && |
| 1069 |
!hints->ecdhe_private_key.empty()) { |
| 1070 |
CBS cbs = MakeConstSpan(hints->ecdhe_private_key); |
| 1071 |
hint_ok = hs->key_shares[0]->DeserializePrivateKey(&cbs); |
| 1072 |
} |
| 1073 |
if (hint_ok) { |
| 1074 |
// Reuse the ECDH key from handshake hints. |
| 1075 |
if (!CBB_add_bytes(&child, hints->ecdhe_public_key.data(), |
| 1076 |
hints->ecdhe_public_key.size())) { |
| 1077 |
return ssl_hs_error; |
| 1078 |
} |
| 1079 |
} else { |
| 1080 |
// Generate a key, and emit the public half. |
| 1081 |
if (!hs->key_shares[0]->Offer(&child)) { |
| 1082 |
return ssl_hs_error; |
| 1083 |
} |
| 1084 |
// If generating hints, save the ECDHE key. |
| 1085 |
if (hints && hs->hints_requested) { |
| 1086 |
bssl::ScopedCBB private_key_cbb; |
| 1087 |
if (!hints->ecdhe_public_key.CopyFrom( |
| 1088 |
MakeConstSpan(CBB_data(&child), CBB_len(&child))) || |
| 1089 |
!CBB_init(private_key_cbb.get(), 32) || |
| 1090 |
!hs->key_shares[0]->SerializePrivateKey(private_key_cbb.get()) || |
| 1091 |
!CBBFinishArray(private_key_cbb.get(), |
| 1092 |
&hints->ecdhe_private_key)) { |
| 1093 |
return ssl_hs_error; |
| 1094 |
} |
| 1095 |
hints->ecdhe_group_id = group_id; |
| 1096 |
} |
| 1097 |
} |
| 1098 |
} else { |
| 1099 |
assert(alg_k & SSL_kPSK); |
| 1100 |
} |
| 1101 |
|
| 1102 |
if (!CBBFinishArray(cbb.get(), &hs->server_params)) { |
| 1103 |
return ssl_hs_error; |
| 1104 |
} |
| 1105 |
} |
| 1106 |
|
| 1107 |
hs->state = state12_send_server_key_exchange; |
| 1108 |
return ssl_hs_ok; |
| 1109 |
} |
| 1110 |
|
| 1111 |
static enum ssl_hs_wait_t do_send_server_key_exchange(SSL_HANDSHAKE *hs) { |
| 1112 |
SSL *const ssl = hs->ssl; |
| 1113 |
|
| 1114 |
if (hs->server_params.size() == 0) { |
| 1115 |
hs->state = state12_send_server_hello_done; |
| 1116 |
return ssl_hs_ok; |
| 1117 |
} |
| 1118 |
|
| 1119 |
ScopedCBB cbb; |
| 1120 |
CBB body, child; |
| 1121 |
if (!ssl->method->init_message(ssl, cbb.get(), &body, |
| 1122 |
SSL3_MT_SERVER_KEY_EXCHANGE) || |
| 1123 |
// |hs->server_params| contains a prefix for signing. |
| 1124 |
hs->server_params.size() < 2 * SSL3_RANDOM_SIZE || |
| 1125 |
!CBB_add_bytes(&body, hs->server_params.data() + 2 * SSL3_RANDOM_SIZE, |
| 1126 |
hs->server_params.size() - 2 * SSL3_RANDOM_SIZE)) { |
| 1127 |
return ssl_hs_error; |
| 1128 |
} |
| 1129 |
|
| 1130 |
// Add a signature. The signature algorithm is determined earlier during |
| 1131 |
// certificate selection. |
| 1132 |
if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
| 1133 |
if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) { |
| 1134 |
if (!CBB_add_u16(&body, hs->signature_algorithm)) { |
| 1135 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1136 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 1137 |
return ssl_hs_error; |
| 1138 |
} |
| 1139 |
} |
| 1140 |
|
| 1141 |
// Add space for the signature. |
| 1142 |
const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get()); |
| 1143 |
uint8_t *ptr; |
| 1144 |
if (!CBB_add_u16_length_prefixed(&body, &child) || |
| 1145 |
!CBB_reserve(&child, &ptr, max_sig_len)) { |
| 1146 |
return ssl_hs_error; |
| 1147 |
} |
| 1148 |
|
| 1149 |
size_t sig_len; |
| 1150 |
switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len, |
| 1151 |
hs->signature_algorithm, hs->server_params)) { |
| 1152 |
case ssl_private_key_success: |
| 1153 |
if (!CBB_did_write(&child, sig_len)) { |
| 1154 |
return ssl_hs_error; |
| 1155 |
} |
| 1156 |
break; |
| 1157 |
case ssl_private_key_failure: |
| 1158 |
return ssl_hs_error; |
| 1159 |
case ssl_private_key_retry: |
| 1160 |
return ssl_hs_private_key_operation; |
| 1161 |
} |
| 1162 |
} |
| 1163 |
|
| 1164 |
hs->can_release_private_key = true; |
| 1165 |
if (!ssl_add_message_cbb(ssl, cbb.get())) { |
| 1166 |
return ssl_hs_error; |
| 1167 |
} |
| 1168 |
|
| 1169 |
hs->server_params.Reset(); |
| 1170 |
|
| 1171 |
hs->state = state12_send_server_hello_done; |
| 1172 |
return ssl_hs_ok; |
| 1173 |
} |
| 1174 |
|
| 1175 |
static enum ssl_hs_wait_t do_send_server_hello_done(SSL_HANDSHAKE *hs) { |
| 1176 |
SSL *const ssl = hs->ssl; |
| 1177 |
if (hs->hints_requested) { |
| 1178 |
return ssl_hs_hints_ready; |
| 1179 |
} |
| 1180 |
|
| 1181 |
ScopedCBB cbb; |
| 1182 |
CBB body; |
| 1183 |
|
| 1184 |
if (hs->cert_request) { |
| 1185 |
CBB cert_types, sigalgs_cbb; |
| 1186 |
if (!ssl->method->init_message(ssl, cbb.get(), &body, |
| 1187 |
SSL3_MT_CERTIFICATE_REQUEST) || |
| 1188 |
!CBB_add_u8_length_prefixed(&body, &cert_types) || |
| 1189 |
!CBB_add_u8(&cert_types, SSL3_CT_RSA_SIGN) || |
| 1190 |
!CBB_add_u8(&cert_types, TLS_CT_ECDSA_SIGN) || |
| 1191 |
(ssl_protocol_version(ssl) >= TLS1_2_VERSION && |
| 1192 |
(!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb) || |
| 1193 |
!tls12_add_verify_sigalgs(hs, &sigalgs_cbb))) || |
| 1194 |
!ssl_add_client_CA_list(hs, &body) || |
| 1195 |
!ssl_add_message_cbb(ssl, cbb.get())) { |
| 1196 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1197 |
return ssl_hs_error; |
| 1198 |
} |
| 1199 |
} |
| 1200 |
|
| 1201 |
if (!ssl->method->init_message(ssl, cbb.get(), &body, |
| 1202 |
SSL3_MT_SERVER_HELLO_DONE) || |
| 1203 |
!ssl_add_message_cbb(ssl, cbb.get())) { |
| 1204 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1205 |
return ssl_hs_error; |
| 1206 |
} |
| 1207 |
|
| 1208 |
hs->state = state12_read_client_certificate; |
| 1209 |
return ssl_hs_flush; |
| 1210 |
} |
| 1211 |
|
| 1212 |
static enum ssl_hs_wait_t do_read_client_certificate(SSL_HANDSHAKE *hs) { |
| 1213 |
SSL *const ssl = hs->ssl; |
| 1214 |
|
| 1215 |
if (hs->handback && hs->new_cipher->algorithm_mkey == SSL_kECDHE) { |
| 1216 |
return ssl_hs_handback; |
| 1217 |
} |
| 1218 |
if (!hs->cert_request) { |
| 1219 |
hs->state = state12_verify_client_certificate; |
| 1220 |
return ssl_hs_ok; |
| 1221 |
} |
| 1222 |
|
| 1223 |
SSLMessage msg; |
| 1224 |
if (!ssl->method->get_message(ssl, &msg)) { |
| 1225 |
return ssl_hs_read_message; |
| 1226 |
} |
| 1227 |
|
| 1228 |
if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE)) { |
| 1229 |
return ssl_hs_error; |
| 1230 |
} |
| 1231 |
|
| 1232 |
if (!ssl_hash_message(hs, msg)) { |
| 1233 |
return ssl_hs_error; |
| 1234 |
} |
| 1235 |
|
| 1236 |
CBS certificate_msg = msg.body; |
| 1237 |
uint8_t alert = SSL_AD_DECODE_ERROR; |
| 1238 |
if (!ssl_parse_cert_chain(&alert, &hs->new_session->certs, &hs->peer_pubkey, |
| 1239 |
hs->config->retain_only_sha256_of_client_certs |
| 1240 |
? hs->new_session->peer_sha256 |
| 1241 |
: nullptr, |
| 1242 |
&certificate_msg, ssl->ctx->pool)) { |
| 1243 |
ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 1244 |
return ssl_hs_error; |
| 1245 |
} |
| 1246 |
|
| 1247 |
if (CBS_len(&certificate_msg) != 0 || |
| 1248 |
!ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) { |
| 1249 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1250 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 1251 |
return ssl_hs_error; |
| 1252 |
} |
| 1253 |
|
| 1254 |
if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) == 0) { |
| 1255 |
// No client certificate so the handshake buffer may be discarded. |
| 1256 |
hs->transcript.FreeBuffer(); |
| 1257 |
|
| 1258 |
if (hs->config->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) { |
| 1259 |
// Fail for TLS only if we required a certificate |
| 1260 |
OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
| 1261 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1262 |
return ssl_hs_error; |
| 1263 |
} |
| 1264 |
|
| 1265 |
// OpenSSL returns X509_V_OK when no certificates are received. This is |
| 1266 |
// classed by them as a bug, but it's assumed by at least NGINX. |
| 1267 |
hs->new_session->verify_result = X509_V_OK; |
| 1268 |
ssl->verify_result = hs->new_session->verify_result; |
| 1269 |
} else if (hs->config->retain_only_sha256_of_client_certs) { |
| 1270 |
// The hash will have been filled in. |
| 1271 |
hs->new_session->peer_sha256_valid = true; |
| 1272 |
} |
| 1273 |
|
| 1274 |
ssl->method->next_message(ssl); |
| 1275 |
hs->state = state12_verify_client_certificate; |
| 1276 |
return ssl_hs_ok; |
| 1277 |
} |
| 1278 |
|
| 1279 |
static enum ssl_hs_wait_t do_verify_client_certificate(SSL_HANDSHAKE *hs) { |
| 1280 |
if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) > 0) { |
| 1281 |
switch (ssl_verify_peer_cert(hs)) { |
| 1282 |
case ssl_verify_ok: |
| 1283 |
break; |
| 1284 |
case ssl_verify_invalid: |
| 1285 |
return ssl_hs_error; |
| 1286 |
case ssl_verify_retry: |
| 1287 |
return ssl_hs_certificate_verify; |
| 1288 |
} |
| 1289 |
} |
| 1290 |
|
| 1291 |
hs->state = state12_read_client_key_exchange; |
| 1292 |
return ssl_hs_ok; |
| 1293 |
} |
| 1294 |
|
| 1295 |
static enum ssl_hs_wait_t do_read_client_key_exchange(SSL_HANDSHAKE *hs) { |
| 1296 |
SSL *const ssl = hs->ssl; |
| 1297 |
SSLMessage msg; |
| 1298 |
if (!ssl->method->get_message(ssl, &msg)) { |
| 1299 |
return ssl_hs_read_message; |
| 1300 |
} |
| 1301 |
|
| 1302 |
if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_KEY_EXCHANGE)) { |
| 1303 |
return ssl_hs_error; |
| 1304 |
} |
| 1305 |
|
| 1306 |
CBS client_key_exchange = msg.body; |
| 1307 |
uint32_t alg_k = hs->new_cipher->algorithm_mkey; |
| 1308 |
uint32_t alg_a = hs->new_cipher->algorithm_auth; |
| 1309 |
|
| 1310 |
// If using a PSK key exchange, parse the PSK identity. |
| 1311 |
if (alg_a & SSL_aPSK) { |
| 1312 |
CBS psk_identity; |
| 1313 |
|
| 1314 |
// If using PSK, the ClientKeyExchange contains a psk_identity. If PSK, |
| 1315 |
// then this is the only field in the message. |
| 1316 |
if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) || |
| 1317 |
((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) { |
| 1318 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1319 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 1320 |
return ssl_hs_error; |
| 1321 |
} |
| 1322 |
|
| 1323 |
if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN || |
| 1324 |
CBS_contains_zero_byte(&psk_identity)) { |
| 1325 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
| 1326 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 1327 |
return ssl_hs_error; |
| 1328 |
} |
| 1329 |
char *raw = nullptr; |
| 1330 |
if (!CBS_strdup(&psk_identity, &raw)) { |
| 1331 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 1332 |
return ssl_hs_error; |
| 1333 |
} |
| 1334 |
hs->new_session->psk_identity.reset(raw); |
| 1335 |
} |
| 1336 |
|
| 1337 |
// Depending on the key exchange method, compute |premaster_secret|. |
| 1338 |
Array<uint8_t> premaster_secret; |
| 1339 |
if (alg_k & SSL_kRSA) { |
| 1340 |
CBS encrypted_premaster_secret; |
| 1341 |
if (!CBS_get_u16_length_prefixed(&client_key_exchange, |
| 1342 |
&encrypted_premaster_secret) || |
| 1343 |
CBS_len(&client_key_exchange) != 0) { |
| 1344 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1345 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 1346 |
return ssl_hs_error; |
| 1347 |
} |
| 1348 |
|
| 1349 |
// Allocate a buffer large enough for an RSA decryption. |
| 1350 |
Array<uint8_t> decrypt_buf; |
| 1351 |
if (!decrypt_buf.Init(EVP_PKEY_size(hs->local_pubkey.get()))) { |
| 1352 |
return ssl_hs_error; |
| 1353 |
} |
| 1354 |
|
| 1355 |
// Decrypt with no padding. PKCS#1 padding will be removed as part of the |
| 1356 |
// timing-sensitive code below. |
| 1357 |
size_t decrypt_len; |
| 1358 |
switch (ssl_private_key_decrypt(hs, decrypt_buf.data(), &decrypt_len, |
| 1359 |
decrypt_buf.size(), |
| 1360 |
encrypted_premaster_secret)) { |
| 1361 |
case ssl_private_key_success: |
| 1362 |
break; |
| 1363 |
case ssl_private_key_failure: |
| 1364 |
return ssl_hs_error; |
| 1365 |
case ssl_private_key_retry: |
| 1366 |
return ssl_hs_private_key_operation; |
| 1367 |
} |
| 1368 |
|
| 1369 |
if (decrypt_len != decrypt_buf.size()) { |
| 1370 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED); |
| 1371 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
| 1372 |
return ssl_hs_error; |
| 1373 |
} |
| 1374 |
|
| 1375 |
CONSTTIME_SECRET(decrypt_buf.data(), decrypt_len); |
| 1376 |
|
| 1377 |
// Prepare a random premaster, to be used on invalid padding. See RFC 5246, |
| 1378 |
// section 7.4.7.1. |
| 1379 |
if (!premaster_secret.Init(SSL_MAX_MASTER_KEY_LENGTH) || |
| 1380 |
!RAND_bytes(premaster_secret.data(), premaster_secret.size())) { |
| 1381 |
return ssl_hs_error; |
| 1382 |
} |
| 1383 |
|
| 1384 |
// The smallest padded premaster is 11 bytes of overhead. Small keys are |
| 1385 |
// publicly invalid. |
| 1386 |
if (decrypt_len < 11 + premaster_secret.size()) { |
| 1387 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED); |
| 1388 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
| 1389 |
return ssl_hs_error; |
| 1390 |
} |
| 1391 |
|
| 1392 |
// Check the padding. See RFC 8017, section 7.2.2. |
| 1393 |
size_t padding_len = decrypt_len - premaster_secret.size(); |
| 1394 |
uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) & |
| 1395 |
constant_time_eq_int_8(decrypt_buf[1], 2); |
| 1396 |
for (size_t i = 2; i < padding_len - 1; i++) { |
| 1397 |
good &= ~constant_time_is_zero_8(decrypt_buf[i]); |
| 1398 |
} |
| 1399 |
good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]); |
| 1400 |
|
| 1401 |
// The premaster secret must begin with |client_version|. This too must be |
| 1402 |
// checked in constant time (http://eprint.iacr.org/2003/052/). |
| 1403 |
good &= constant_time_eq_8(decrypt_buf[padding_len], |
| 1404 |
(unsigned)(hs->client_version >> 8)); |
| 1405 |
good &= constant_time_eq_8(decrypt_buf[padding_len + 1], |
| 1406 |
(unsigned)(hs->client_version & 0xff)); |
| 1407 |
|
| 1408 |
// Select, in constant time, either the decrypted premaster or the random |
| 1409 |
// premaster based on |good|. |
| 1410 |
for (size_t i = 0; i < premaster_secret.size(); i++) { |
| 1411 |
premaster_secret[i] = constant_time_select_8( |
| 1412 |
good, decrypt_buf[padding_len + i], premaster_secret[i]); |
| 1413 |
} |
| 1414 |
} else if (alg_k & SSL_kECDHE) { |
| 1415 |
// Parse the ClientKeyExchange. |
| 1416 |
CBS peer_key; |
| 1417 |
if (!CBS_get_u8_length_prefixed(&client_key_exchange, &peer_key) || |
| 1418 |
CBS_len(&client_key_exchange) != 0) { |
| 1419 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1420 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 1421 |
return ssl_hs_error; |
| 1422 |
} |
| 1423 |
|
| 1424 |
// Compute the premaster. |
| 1425 |
uint8_t alert = SSL_AD_DECODE_ERROR; |
| 1426 |
if (!hs->key_shares[0]->Finish(&premaster_secret, &alert, peer_key) || |
| 1427 |
// Save peer's public key for observation with |SSL_get_peer_tmp_key|. |
| 1428 |
!ssl->s3->peer_key.CopyFrom(peer_key)) { |
| 1429 |
ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 1430 |
return ssl_hs_error; |
| 1431 |
} |
| 1432 |
|
| 1433 |
// The key exchange state may now be discarded. |
| 1434 |
hs->key_shares[0].reset(); |
| 1435 |
hs->key_shares[1].reset(); |
| 1436 |
} else if (!(alg_k & SSL_kPSK)) { |
| 1437 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1438 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1439 |
return ssl_hs_error; |
| 1440 |
} |
| 1441 |
|
| 1442 |
// For a PSK cipher suite, the actual pre-master secret is combined with the |
| 1443 |
// pre-shared key. |
| 1444 |
if (alg_a & SSL_aPSK) { |
| 1445 |
if (hs->config->psk_server_callback == NULL) { |
| 1446 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1447 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 1448 |
return ssl_hs_error; |
| 1449 |
} |
| 1450 |
|
| 1451 |
// Look up the key for the identity. |
| 1452 |
uint8_t psk[PSK_MAX_PSK_LEN]; |
| 1453 |
unsigned psk_len = hs->config->psk_server_callback( |
| 1454 |
ssl, hs->new_session->psk_identity.get(), psk, sizeof(psk)); |
| 1455 |
if (psk_len > PSK_MAX_PSK_LEN) { |
| 1456 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1457 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 1458 |
return ssl_hs_error; |
| 1459 |
} else if (psk_len == 0) { |
| 1460 |
// PSK related to the given identity not found. |
| 1461 |
OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND); |
| 1462 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNKNOWN_PSK_IDENTITY); |
| 1463 |
return ssl_hs_error; |
| 1464 |
} |
| 1465 |
|
| 1466 |
if (alg_k & SSL_kPSK) { |
| 1467 |
// In plain PSK, other_secret is a block of 0s with the same length as the |
| 1468 |
// pre-shared key. |
| 1469 |
if (!premaster_secret.Init(psk_len)) { |
| 1470 |
return ssl_hs_error; |
| 1471 |
} |
| 1472 |
OPENSSL_memset(premaster_secret.data(), 0, premaster_secret.size()); |
| 1473 |
} |
| 1474 |
|
| 1475 |
ScopedCBB new_premaster; |
| 1476 |
CBB child; |
| 1477 |
if (!CBB_init(new_premaster.get(), |
| 1478 |
2 + psk_len + 2 + premaster_secret.size()) || |
| 1479 |
!CBB_add_u16_length_prefixed(new_premaster.get(), &child) || |
| 1480 |
!CBB_add_bytes(&child, premaster_secret.data(), |
| 1481 |
premaster_secret.size()) || |
| 1482 |
!CBB_add_u16_length_prefixed(new_premaster.get(), &child) || |
| 1483 |
!CBB_add_bytes(&child, psk, psk_len) || |
| 1484 |
!CBBFinishArray(new_premaster.get(), &premaster_secret)) { |
| 1485 |
return ssl_hs_error; |
| 1486 |
} |
| 1487 |
} |
| 1488 |
|
| 1489 |
if (!ssl_hash_message(hs, msg)) { |
| 1490 |
return ssl_hs_error; |
| 1491 |
} |
| 1492 |
|
| 1493 |
// Compute the master secret. |
| 1494 |
hs->new_session->secret_length = tls1_generate_master_secret( |
| 1495 |
hs, hs->new_session->secret, premaster_secret); |
| 1496 |
if (hs->new_session->secret_length == 0) { |
| 1497 |
return ssl_hs_error; |
| 1498 |
} |
| 1499 |
hs->new_session->extended_master_secret = hs->extended_master_secret; |
| 1500 |
CONSTTIME_DECLASSIFY(hs->new_session->secret, hs->new_session->secret_length); |
| 1501 |
hs->can_release_private_key = true; |
| 1502 |
|
| 1503 |
ssl->method->next_message(ssl); |
| 1504 |
hs->state = state12_read_client_certificate_verify; |
| 1505 |
return ssl_hs_ok; |
| 1506 |
} |
| 1507 |
|
| 1508 |
static enum ssl_hs_wait_t do_read_client_certificate_verify(SSL_HANDSHAKE *hs) { |
| 1509 |
SSL *const ssl = hs->ssl; |
| 1510 |
|
| 1511 |
// Only RSA and ECDSA client certificates are supported, so a |
| 1512 |
// CertificateVerify is required if and only if there's a client certificate. |
| 1513 |
if (!hs->peer_pubkey) { |
| 1514 |
hs->transcript.FreeBuffer(); |
| 1515 |
hs->state = state12_read_change_cipher_spec; |
| 1516 |
return ssl_hs_ok; |
| 1517 |
} |
| 1518 |
|
| 1519 |
SSLMessage msg; |
| 1520 |
if (!ssl->method->get_message(ssl, &msg)) { |
| 1521 |
return ssl_hs_read_message; |
| 1522 |
} |
| 1523 |
|
| 1524 |
if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY)) { |
| 1525 |
return ssl_hs_error; |
| 1526 |
} |
| 1527 |
|
| 1528 |
// The peer certificate must be valid for signing. |
| 1529 |
const CRYPTO_BUFFER *leaf = |
| 1530 |
sk_CRYPTO_BUFFER_value(hs->new_session->certs.get(), 0); |
| 1531 |
CBS leaf_cbs; |
| 1532 |
CRYPTO_BUFFER_init_CBS(leaf, &leaf_cbs); |
| 1533 |
if (!ssl_cert_check_key_usage(&leaf_cbs, key_usage_digital_signature)) { |
| 1534 |
return ssl_hs_error; |
| 1535 |
} |
| 1536 |
|
| 1537 |
CBS certificate_verify = msg.body, signature; |
| 1538 |
|
| 1539 |
// Determine the signature algorithm. |
| 1540 |
uint16_t signature_algorithm = 0; |
| 1541 |
if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) { |
| 1542 |
if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) { |
| 1543 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1544 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 1545 |
return ssl_hs_error; |
| 1546 |
} |
| 1547 |
uint8_t alert = SSL_AD_DECODE_ERROR; |
| 1548 |
if (!tls12_check_peer_sigalg(hs, &alert, signature_algorithm)) { |
| 1549 |
ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 1550 |
return ssl_hs_error; |
| 1551 |
} |
| 1552 |
hs->new_session->peer_signature_algorithm = signature_algorithm; |
| 1553 |
} else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm, |
| 1554 |
hs->peer_pubkey.get())) { |
| 1555 |
OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE); |
| 1556 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE); |
| 1557 |
return ssl_hs_error; |
| 1558 |
} |
| 1559 |
|
| 1560 |
// Parse and verify the signature. |
| 1561 |
if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) || |
| 1562 |
CBS_len(&certificate_verify) != 0) { |
| 1563 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1564 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 1565 |
return ssl_hs_error; |
| 1566 |
} |
| 1567 |
|
| 1568 |
if (!ssl_public_key_verify(ssl, signature, signature_algorithm, |
| 1569 |
hs->peer_pubkey.get(), hs->transcript.buffer())) { |
| 1570 |
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE); |
| 1571 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
| 1572 |
return ssl_hs_error; |
| 1573 |
} |
| 1574 |
|
| 1575 |
// The handshake buffer is no longer necessary, and we may hash the current |
| 1576 |
// message. |
| 1577 |
hs->transcript.FreeBuffer(); |
| 1578 |
if (!ssl_hash_message(hs, msg)) { |
| 1579 |
return ssl_hs_error; |
| 1580 |
} |
| 1581 |
|
| 1582 |
ssl->method->next_message(ssl); |
| 1583 |
hs->state = state12_read_change_cipher_spec; |
| 1584 |
return ssl_hs_ok; |
| 1585 |
} |
| 1586 |
|
| 1587 |
static enum ssl_hs_wait_t do_read_change_cipher_spec(SSL_HANDSHAKE *hs) { |
| 1588 |
if (hs->handback && hs->ssl->session != NULL) { |
| 1589 |
return ssl_hs_handback; |
| 1590 |
} |
| 1591 |
hs->state = state12_process_change_cipher_spec; |
| 1592 |
return ssl_hs_read_change_cipher_spec; |
| 1593 |
} |
| 1594 |
|
| 1595 |
static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) { |
| 1596 |
if (!tls1_change_cipher_state(hs, evp_aead_open)) { |
| 1597 |
return ssl_hs_error; |
| 1598 |
} |
| 1599 |
|
| 1600 |
hs->state = state12_read_next_proto; |
| 1601 |
return ssl_hs_ok; |
| 1602 |
} |
| 1603 |
|
| 1604 |
static enum ssl_hs_wait_t do_read_next_proto(SSL_HANDSHAKE *hs) { |
| 1605 |
SSL *const ssl = hs->ssl; |
| 1606 |
|
| 1607 |
if (!hs->next_proto_neg_seen) { |
| 1608 |
hs->state = state12_read_channel_id; |
| 1609 |
return ssl_hs_ok; |
| 1610 |
} |
| 1611 |
|
| 1612 |
SSLMessage msg; |
| 1613 |
if (!ssl->method->get_message(ssl, &msg)) { |
| 1614 |
return ssl_hs_read_message; |
| 1615 |
} |
| 1616 |
|
| 1617 |
if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEXT_PROTO) || |
| 1618 |
!ssl_hash_message(hs, msg)) { |
| 1619 |
return ssl_hs_error; |
| 1620 |
} |
| 1621 |
|
| 1622 |
CBS next_protocol = msg.body, selected_protocol, padding; |
| 1623 |
if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) || |
| 1624 |
!CBS_get_u8_length_prefixed(&next_protocol, &padding) || |
| 1625 |
CBS_len(&next_protocol) != 0) { |
| 1626 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1627 |
ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 1628 |
return ssl_hs_error; |
| 1629 |
} |
| 1630 |
|
| 1631 |
if (!ssl->s3->next_proto_negotiated.CopyFrom(selected_protocol)) { |
| 1632 |
return ssl_hs_error; |
| 1633 |
} |
| 1634 |
|
| 1635 |
ssl->method->next_message(ssl); |
| 1636 |
hs->state = state12_read_channel_id; |
| 1637 |
return ssl_hs_ok; |
| 1638 |
} |
| 1639 |
|
| 1640 |
static enum ssl_hs_wait_t do_read_channel_id(SSL_HANDSHAKE *hs) { |
| 1641 |
SSL *const ssl = hs->ssl; |
| 1642 |
|
| 1643 |
if (!hs->channel_id_negotiated) { |
| 1644 |
hs->state = state12_read_client_finished; |
| 1645 |
return ssl_hs_ok; |
| 1646 |
} |
| 1647 |
|
| 1648 |
SSLMessage msg; |
| 1649 |
if (!ssl->method->get_message(ssl, &msg)) { |
| 1650 |
return ssl_hs_read_message; |
| 1651 |
} |
| 1652 |
|
| 1653 |
if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) || |
| 1654 |
!tls1_verify_channel_id(hs, msg) || |
| 1655 |
!ssl_hash_message(hs, msg)) { |
| 1656 |
return ssl_hs_error; |
| 1657 |
} |
| 1658 |
|
| 1659 |
ssl->method->next_message(ssl); |
| 1660 |
hs->state = state12_read_client_finished; |
| 1661 |
return ssl_hs_ok; |
| 1662 |
} |
| 1663 |
|
| 1664 |
static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) { |
| 1665 |
SSL *const ssl = hs->ssl; |
| 1666 |
enum ssl_hs_wait_t wait = ssl_get_finished(hs); |
| 1667 |
if (wait != ssl_hs_ok) { |
| 1668 |
return wait; |
| 1669 |
} |
| 1670 |
|
| 1671 |
if (ssl->session != NULL) { |
| 1672 |
hs->state = state12_finish_server_handshake; |
| 1673 |
} else { |
| 1674 |
hs->state = state12_send_server_finished; |
| 1675 |
} |
| 1676 |
|
| 1677 |
// If this is a full handshake with ChannelID then record the handshake |
| 1678 |
// hashes in |hs->new_session| in case we need them to verify a |
| 1679 |
// ChannelID signature on a resumption of this session in the future. |
| 1680 |
if (ssl->session == NULL && ssl->s3->channel_id_valid && |
| 1681 |
!tls1_record_handshake_hashes_for_channel_id(hs)) { |
| 1682 |
return ssl_hs_error; |
| 1683 |
} |
| 1684 |
|
| 1685 |
return ssl_hs_ok; |
| 1686 |
} |
| 1687 |
|
| 1688 |
static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) { |
| 1689 |
SSL *const ssl = hs->ssl; |
| 1690 |
|
| 1691 |
if (hs->ticket_expected) { |
| 1692 |
const SSL_SESSION *session; |
| 1693 |
UniquePtr<SSL_SESSION> session_copy; |
| 1694 |
if (ssl->session == NULL) { |
| 1695 |
// Fix the timeout to measure from the ticket issuance time. |
| 1696 |
ssl_session_rebase_time(ssl, hs->new_session.get()); |
| 1697 |
session = hs->new_session.get(); |
| 1698 |
} else { |
| 1699 |
// We are renewing an existing session. Duplicate the session to adjust |
| 1700 |
// the timeout. |
| 1701 |
session_copy = |
| 1702 |
SSL_SESSION_dup(ssl->session.get(), SSL_SESSION_INCLUDE_NONAUTH); |
| 1703 |
if (!session_copy) { |
| 1704 |
return ssl_hs_error; |
| 1705 |
} |
| 1706 |
|
| 1707 |
ssl_session_rebase_time(ssl, session_copy.get()); |
| 1708 |
session = session_copy.get(); |
| 1709 |
} |
| 1710 |
|
| 1711 |
ScopedCBB cbb; |
| 1712 |
CBB body, ticket; |
| 1713 |
if (!ssl->method->init_message(ssl, cbb.get(), &body, |
| 1714 |
SSL3_MT_NEW_SESSION_TICKET) || |
| 1715 |
!CBB_add_u32(&body, session->timeout) || |
| 1716 |
!CBB_add_u16_length_prefixed(&body, &ticket) || |
| 1717 |
!ssl_encrypt_ticket(hs, &ticket, session) || |
| 1718 |
!ssl_add_message_cbb(ssl, cbb.get())) { |
| 1719 |
return ssl_hs_error; |
| 1720 |
} |
| 1721 |
} |
| 1722 |
|
| 1723 |
if (!ssl->method->add_change_cipher_spec(ssl) || |
| 1724 |
!tls1_change_cipher_state(hs, evp_aead_seal) || |
| 1725 |
!ssl_send_finished(hs)) { |
| 1726 |
return ssl_hs_error; |
| 1727 |
} |
| 1728 |
|
| 1729 |
if (ssl->session != NULL) { |
| 1730 |
hs->state = state12_read_change_cipher_spec; |
| 1731 |
} else { |
| 1732 |
hs->state = state12_finish_server_handshake; |
| 1733 |
} |
| 1734 |
return ssl_hs_flush; |
| 1735 |
} |
| 1736 |
|
| 1737 |
static enum ssl_hs_wait_t do_finish_server_handshake(SSL_HANDSHAKE *hs) { |
| 1738 |
SSL *const ssl = hs->ssl; |
| 1739 |
|
| 1740 |
if (hs->handback) { |
| 1741 |
return ssl_hs_handback; |
| 1742 |
} |
| 1743 |
|
| 1744 |
ssl->method->on_handshake_complete(ssl); |
| 1745 |
|
| 1746 |
// If we aren't retaining peer certificates then we can discard it now. |
| 1747 |
if (hs->new_session != NULL && |
| 1748 |
hs->config->retain_only_sha256_of_client_certs) { |
| 1749 |
hs->new_session->certs.reset(); |
| 1750 |
ssl->ctx->x509_method->session_clear(hs->new_session.get()); |
| 1751 |
} |
| 1752 |
|
| 1753 |
bool has_new_session = hs->new_session != nullptr; |
| 1754 |
if (has_new_session) { |
| 1755 |
assert(ssl->session == nullptr); |
| 1756 |
ssl->s3->established_session = std::move(hs->new_session); |
| 1757 |
ssl->s3->established_session->not_resumable = false; |
| 1758 |
} else { |
| 1759 |
assert(ssl->session != nullptr); |
| 1760 |
ssl->s3->established_session = UpRef(ssl->session); |
| 1761 |
} |
| 1762 |
|
| 1763 |
hs->handshake_finalized = true; |
| 1764 |
ssl->s3->initial_handshake_complete = true; |
| 1765 |
if (has_new_session) { |
| 1766 |
ssl_update_cache(ssl); |
| 1767 |
} |
| 1768 |
|
| 1769 |
hs->state = state12_done; |
| 1770 |
return ssl_hs_ok; |
| 1771 |
} |
| 1772 |
|
| 1773 |
enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs) { |
| 1774 |
while (hs->state != state12_done) { |
| 1775 |
enum ssl_hs_wait_t ret = ssl_hs_error; |
| 1776 |
enum tls12_server_hs_state_t state = |
| 1777 |
static_cast<enum tls12_server_hs_state_t>(hs->state); |
| 1778 |
switch (state) { |
| 1779 |
case state12_start_accept: |
| 1780 |
ret = do_start_accept(hs); |
| 1781 |
break; |
| 1782 |
case state12_read_client_hello: |
| 1783 |
ret = do_read_client_hello(hs); |
| 1784 |
break; |
| 1785 |
case state12_read_client_hello_after_ech: |
| 1786 |
ret = do_read_client_hello_after_ech(hs); |
| 1787 |
break; |
| 1788 |
case state12_select_certificate: |
| 1789 |
ret = do_select_certificate(hs); |
| 1790 |
break; |
| 1791 |
case state12_tls13: |
| 1792 |
ret = do_tls13(hs); |
| 1793 |
break; |
| 1794 |
case state12_select_parameters: |
| 1795 |
ret = do_select_parameters(hs); |
| 1796 |
break; |
| 1797 |
case state12_send_server_hello: |
| 1798 |
ret = do_send_server_hello(hs); |
| 1799 |
break; |
| 1800 |
case state12_send_server_certificate: |
| 1801 |
ret = do_send_server_certificate(hs); |
| 1802 |
break; |
| 1803 |
case state12_send_server_key_exchange: |
| 1804 |
ret = do_send_server_key_exchange(hs); |
| 1805 |
break; |
| 1806 |
case state12_send_server_hello_done: |
| 1807 |
ret = do_send_server_hello_done(hs); |
| 1808 |
break; |
| 1809 |
case state12_read_client_certificate: |
| 1810 |
ret = do_read_client_certificate(hs); |
| 1811 |
break; |
| 1812 |
case state12_verify_client_certificate: |
| 1813 |
ret = do_verify_client_certificate(hs); |
| 1814 |
break; |
| 1815 |
case state12_read_client_key_exchange: |
| 1816 |
ret = do_read_client_key_exchange(hs); |
| 1817 |
break; |
| 1818 |
case state12_read_client_certificate_verify: |
| 1819 |
ret = do_read_client_certificate_verify(hs); |
| 1820 |
break; |
| 1821 |
case state12_read_change_cipher_spec: |
| 1822 |
ret = do_read_change_cipher_spec(hs); |
| 1823 |
break; |
| 1824 |
case state12_process_change_cipher_spec: |
| 1825 |
ret = do_process_change_cipher_spec(hs); |
| 1826 |
break; |
| 1827 |
case state12_read_next_proto: |
| 1828 |
ret = do_read_next_proto(hs); |
| 1829 |
break; |
| 1830 |
case state12_read_channel_id: |
| 1831 |
ret = do_read_channel_id(hs); |
| 1832 |
break; |
| 1833 |
case state12_read_client_finished: |
| 1834 |
ret = do_read_client_finished(hs); |
| 1835 |
break; |
| 1836 |
case state12_send_server_finished: |
| 1837 |
ret = do_send_server_finished(hs); |
| 1838 |
break; |
| 1839 |
case state12_finish_server_handshake: |
| 1840 |
ret = do_finish_server_handshake(hs); |
| 1841 |
break; |
| 1842 |
case state12_done: |
| 1843 |
ret = ssl_hs_ok; |
| 1844 |
break; |
| 1845 |
} |
| 1846 |
|
| 1847 |
if (hs->state != state) { |
| 1848 |
ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1); |
| 1849 |
} |
| 1850 |
|
| 1851 |
if (ret != ssl_hs_ok) { |
| 1852 |
return ret; |
| 1853 |
} |
| 1854 |
} |
| 1855 |
|
| 1856 |
ssl_update_counter(hs->ssl->session_ctx.get(), |
| 1857 |
hs->ssl->session_ctx->stats.sess_accept_good, true); |
| 1858 |
ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1); |
| 1859 |
return ssl_hs_ok; |
| 1860 |
} |
| 1861 |
|
| 1862 |
const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs) { |
| 1863 |
enum tls12_server_hs_state_t state = |
| 1864 |
static_cast<enum tls12_server_hs_state_t>(hs->state); |
| 1865 |
switch (state) { |
| 1866 |
case state12_start_accept: |
| 1867 |
return "TLS server start_accept"; |
| 1868 |
case state12_read_client_hello: |
| 1869 |
return "TLS server read_client_hello"; |
| 1870 |
case state12_read_client_hello_after_ech: |
| 1871 |
return "TLS server read_client_hello_after_ech"; |
| 1872 |
case state12_select_certificate: |
| 1873 |
return "TLS server select_certificate"; |
| 1874 |
case state12_tls13: |
| 1875 |
return tls13_server_handshake_state(hs); |
| 1876 |
case state12_select_parameters: |
| 1877 |
return "TLS server select_parameters"; |
| 1878 |
case state12_send_server_hello: |
| 1879 |
return "TLS server send_server_hello"; |
| 1880 |
case state12_send_server_certificate: |
| 1881 |
return "TLS server send_server_certificate"; |
| 1882 |
case state12_send_server_key_exchange: |
| 1883 |
return "TLS server send_server_key_exchange"; |
| 1884 |
case state12_send_server_hello_done: |
| 1885 |
return "TLS server send_server_hello_done"; |
| 1886 |
case state12_read_client_certificate: |
| 1887 |
return "TLS server read_client_certificate"; |
| 1888 |
case state12_verify_client_certificate: |
| 1889 |
return "TLS server verify_client_certificate"; |
| 1890 |
case state12_read_client_key_exchange: |
| 1891 |
return "TLS server read_client_key_exchange"; |
| 1892 |
case state12_read_client_certificate_verify: |
| 1893 |
return "TLS server read_client_certificate_verify"; |
| 1894 |
case state12_read_change_cipher_spec: |
| 1895 |
return "TLS server read_change_cipher_spec"; |
| 1896 |
case state12_process_change_cipher_spec: |
| 1897 |
return "TLS server process_change_cipher_spec"; |
| 1898 |
case state12_read_next_proto: |
| 1899 |
return "TLS server read_next_proto"; |
| 1900 |
case state12_read_channel_id: |
| 1901 |
return "TLS server read_channel_id"; |
| 1902 |
case state12_read_client_finished: |
| 1903 |
return "TLS server read_client_finished"; |
| 1904 |
case state12_send_server_finished: |
| 1905 |
return "TLS server send_server_finished"; |
| 1906 |
case state12_finish_server_handshake: |
| 1907 |
return "TLS server finish_server_handshake"; |
| 1908 |
case state12_done: |
| 1909 |
return "TLS server done"; |
| 1910 |
} |
| 1911 |
|
| 1912 |
return "TLS server unknown"; |
| 1913 |
} |
| 1914 |
|
| 1915 |
BSSL_NAMESPACE_END |