| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/ssl.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <string.h> |
| 19 |
| 20 | #include <algorithm> |
| 21 | #include <tuple> |
| 22 |
| 23 | #include <openssl/aead.h> |
| 24 | #include <openssl/bytestring.h> |
| 25 | #include <openssl/digest.h> |
| 26 | #include <openssl/err.h> |
| 27 | #include <openssl/hpke.h> |
| 28 | #include <openssl/mem.h> |
| 29 | #include <openssl/rand.h> |
| 30 | #include <openssl/stack.h> |
| 31 |
| 32 | #include "../crypto/internal.h" |
| 33 | #include "internal.h" |
| 34 |
| 35 |
| 36 | BSSL_NAMESPACE_BEGIN |
| 37 |
| 38 | static const uint8_t kZeroes[EVP_MAX_MD_SIZE] = {0}; |
| 39 |
| 40 | // Allow a minute of ticket age skew in either direction. This covers |
| 41 | // transmission delays in ClientHello and NewSessionTicket, as well as |
| 42 | // drift between client and server clock rate since the ticket was issued. |
| 43 | // See RFC 8446, section 8.3. |
| 44 | static const int32_t kMaxTicketAgeSkewSeconds = 60; |
| 45 |
| 46 | static bool resolve_pake_secret(SSL_HANDSHAKE *hs) { |
| 47 | uint8_t verifier_share[spake2plus::kShareSize]; |
| 48 | uint8_t verifier_confirm[spake2plus::kConfirmSize]; |
| 49 | uint8_t shared_secret[spake2plus::kSecretSize]; |
| 50 | if (!hs->pake_verifier->ProcessProverShare(verifier_share, verifier_confirm, |
| 51 | shared_secret, |
| 52 | hs->pake_share->pake_message)) { |
| 53 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 54 | ssl_send_alert(hs->ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 55 | return false; |
| 56 | } |
| 57 |
| 58 | bssl::ScopedCBB cbb; |
| 59 | if (!CBB_init(cbb.get(), sizeof(verifier_share) + sizeof(verifier_confirm)) || |
| 60 | !CBB_add_bytes(cbb.get(), verifier_share, sizeof(verifier_share)) || |
| 61 | !CBB_add_bytes(cbb.get(), verifier_confirm, sizeof(verifier_confirm)) || |
| 62 | !CBBFinishArray(cbb.get(), &hs->pake_share_bytes)) { |
| 63 | return false; |
| 64 | } |
| 65 |
| 66 | return tls13_advance_key_schedule( |
| 67 | hs, MakeConstSpan(shared_secret, sizeof(shared_secret))); |
| 68 | } |
| 69 |
| 70 | static bool resolve_ecdhe_secret(SSL_HANDSHAKE *hs, |
| 71 | const SSL_CLIENT_HELLO *client_hello) { |
| 72 | SSL *const ssl = hs->ssl; |
| 73 | const uint16_t group_id = hs->new_session->group_id; |
| 74 |
| 75 | bool found_key_share; |
| 76 | Span<const uint8_t> peer_key; |
| 77 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 78 | if (!ssl_ext_key_share_parse_clienthello(hs, &found_key_share, &peer_key, |
| 79 | &alert, client_hello)) { |
| 80 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 81 | return false; |
| 82 | } |
| 83 |
| 84 | if (!found_key_share) { |
| 85 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 86 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
| 87 | return false; |
| 88 | } |
| 89 |
| 90 | Array<uint8_t> secret; |
| 91 | SSL_HANDSHAKE_HINTS *const hints = hs->hints.get(); |
| 92 | if (hints && !hs->hints_requested && hints->key_share_group_id == group_id && |
| 93 | !hints->key_share_secret.empty()) { |
| 94 | // Copy the key_share secret from hints. |
| 95 | if (!hs->key_share_ciphertext.CopyFrom(hints->key_share_ciphertext) || |
| 96 | !secret.CopyFrom(hints->key_share_secret)) { |
| 97 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 98 | return false; |
| 99 | } |
| 100 | } else { |
| 101 | ScopedCBB ciphertext; |
| 102 | UniquePtr<SSLKeyShare> key_share = SSLKeyShare::Create(group_id); |
| 103 | if (!key_share || // |
| 104 | !CBB_init(ciphertext.get(), 32) || |
| 105 | !key_share->Encap(ciphertext.get(), &secret, &alert, peer_key) || |
| 106 | !CBBFinishArray(ciphertext.get(), &hs->key_share_ciphertext)) { |
| 107 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 108 | return false; |
| 109 | } |
| 110 | if (hints && hs->hints_requested) { |
| 111 | hints->key_share_group_id = group_id; |
| 112 | if (!hints->key_share_ciphertext.CopyFrom(hs->key_share_ciphertext) || |
| 113 | !hints->key_share_secret.CopyFrom(secret)) { |
| 114 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 115 | return false; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 |
| 120 | return tls13_advance_key_schedule(hs, secret); |
| 121 | } |
| 122 |
| 123 | static int ssl_ext_supported_versions_add_serverhello(SSL_HANDSHAKE *hs, |
| 124 | CBB *out) { |
| 125 | CBB contents; |
| 126 | if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) || // |
| 127 | !CBB_add_u16_length_prefixed(out, &contents) || // |
| 128 | !CBB_add_u16(&contents, hs->ssl->s3->version) || // |
| 129 | !CBB_flush(out)) { |
| 130 | return 0; |
| 131 | } |
| 132 |
| 133 | return 1; |
| 134 | } |
| 135 |
| 136 | static const SSL_CIPHER *choose_tls13_cipher( |
| 137 | const SSL *ssl, const SSL_CLIENT_HELLO *client_hello) { |
| 138 | CBS cipher_suites; |
| 139 | CBS_init(&cipher_suites, client_hello->cipher_suites, |
| 140 | client_hello->cipher_suites_len); |
| 141 |
| 142 | const uint16_t version = ssl_protocol_version(ssl); |
| 143 |
| 144 | return ssl_choose_tls13_cipher(cipher_suites, |
| 145 | ssl->config->aes_hw_override |
| 146 | ? ssl->config->aes_hw_override_value |
| 147 | : EVP_has_aes_hardware(), |
| 148 | version, ssl->config->compliance_policy); |
| 149 | } |
| 150 |
| 151 | static bool add_new_session_tickets(SSL_HANDSHAKE *hs, bool *out_sent_tickets) { |
| 152 | SSL *const ssl = hs->ssl; |
| 153 | if ( // If the client doesn't accept resumption with PSK_DHE_KE, don't send a |
| 154 | // session ticket. |
| 155 | !hs->accept_psk_mode || |
| 156 | // We only implement stateless resumption in TLS 1.3, so skip sending |
| 157 | // tickets if disabled. |
| 158 | (SSL_get_options(ssl) & SSL_OP_NO_TICKET) || |
| 159 | // Don't send tickets for PAKE connections. We don't support resumption |
| 160 | // with PAKEs. |
| 161 | hs->pake_verifier != nullptr) { |
| 162 | *out_sent_tickets = false; |
| 163 | return true; |
| 164 | } |
| 165 |
| 166 | // Rebase the session timestamp so that it is measured from ticket |
| 167 | // issuance. |
| 168 | ssl_session_rebase_time(ssl, hs->new_session.get()); |
| 169 |
| 170 | assert(ssl->session_ctx->num_tickets <= kMaxTickets); |
| 171 | bool sent_tickets = false; |
| 172 | for (size_t i = 0; i < ssl->session_ctx->num_tickets; i++) { |
| 173 | UniquePtr<SSL_SESSION> session( |
| 174 | SSL_SESSION_dup(hs->new_session.get(), SSL_SESSION_INCLUDE_NONAUTH)); |
| 175 | if (!session) { |
| 176 | return false; |
| 177 | } |
| 178 |
| 179 | if (!RAND_bytes((uint8_t *)&session->ticket_age_add, 4)) { |
| 180 | return false; |
| 181 | } |
| 182 | session->ticket_age_add_valid = true; |
| 183 | // TODO(crbug.com/381113363): Remove the SSL_is_dtls check once we support |
| 184 | // 0-RTT for DTLS 1.3. |
| 185 | bool enable_early_data = |
| 186 | ssl->enable_early_data && |
| 187 | (!SSL_is_quic(ssl) || !ssl->config->quic_early_data_context.empty()) && |
| 188 | !SSL_is_dtls(ssl); |
| 189 | if (enable_early_data) { |
| 190 | // QUIC does not use the max_early_data_size parameter and always sets it |
| 191 | // to a fixed value. See RFC 9001, section 4.6.1. |
| 192 | session->ticket_max_early_data = |
| 193 | SSL_is_quic(ssl) ? 0xffffffff : kMaxEarlyDataAccepted; |
| 194 | } |
| 195 | session->is_resumable_across_names = ssl->resumption_across_names_enabled; |
| 196 |
| 197 | static_assert(kMaxTickets < 256, "Too many tickets"); |
| 198 | assert(i < 256); |
| 199 | uint8_t nonce[] = {static_cast<uint8_t>(i)}; |
| 200 |
| 201 | ScopedCBB cbb; |
| 202 | CBB body, nonce_cbb, ticket, extensions; |
| 203 | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
| 204 | SSL3_MT_NEW_SESSION_TICKET) || |
| 205 | !CBB_add_u32(&body, session->timeout) || |
| 206 | !CBB_add_u32(&body, session->ticket_age_add) || |
| 207 | !CBB_add_u8_length_prefixed(&body, &nonce_cbb) || |
| 208 | !CBB_add_bytes(&nonce_cbb, nonce, sizeof(nonce)) || |
| 209 | !tls13_derive_session_psk(session.get(), nonce, SSL_is_dtls(ssl)) || |
| 210 | !CBB_add_u16_length_prefixed(&body, &ticket) || |
| 211 | !ssl_encrypt_ticket(hs, &ticket, session.get())) { |
| 212 | return false; |
| 213 | } |
| 214 |
| 215 | if (CBB_len(&ticket) == 0) { |
| 216 | // The caller decided not to encrypt a ticket. Skip the message. |
| 217 | continue; |
| 218 | } |
| 219 |
| 220 | if (!CBB_add_u16_length_prefixed(&body, &extensions)) { |
| 221 | return false; |
| 222 | } |
| 223 |
| 224 | if (enable_early_data) { |
| 225 | CBB early_data; |
| 226 | if (!CBB_add_u16(&extensions, TLSEXT_TYPE_early_data) || |
| 227 | !CBB_add_u16_length_prefixed(&extensions, &early_data) || |
| 228 | !CBB_add_u32(&early_data, session->ticket_max_early_data) || |
| 229 | !CBB_flush(&extensions)) { |
| 230 | return false; |
| 231 | } |
| 232 | } |
| 233 |
| 234 | SSLFlags flags = 0; |
| 235 | if (session->is_resumable_across_names) { |
| 236 | flags |= kSSLFlagResumptionAcrossNames; |
| 237 | } |
| 238 | if (!ssl_add_flags_extension(&extensions, flags)) { |
| 239 | return false; |
| 240 | } |
| 241 |
| 242 | // Add a fake extension. See RFC 8701. |
| 243 | if (!CBB_add_u16(&extensions, |
| 244 | ssl_get_grease_value(hs, ssl_grease_ticket_extension)) || |
| 245 | !CBB_add_u16(&extensions, 0 /* empty */)) { |
| 246 | return false; |
| 247 | } |
| 248 |
| 249 | if (!ssl_add_message_cbb(ssl, cbb.get())) { |
| 250 | return false; |
| 251 | } |
| 252 | sent_tickets = true; |
| 253 | } |
| 254 |
| 255 | *out_sent_tickets = sent_tickets; |
| 256 | return true; |
| 257 | } |
| 258 |
| 259 | static bool check_signature_credential(SSL_HANDSHAKE *hs, |
| 260 | const SSL_CREDENTIAL *cred, |
| 261 | uint16_t *out_sigalg) { |
| 262 | switch (cred->type) { |
| 263 | case SSLCredentialType::kX509: |
| 264 | break; |
| 265 | case SSLCredentialType::kDelegated: |
| 266 | // Check that the peer supports the signature over the delegated |
| 267 | // credential. |
| 268 | if (std::find(hs->peer_sigalgs.begin(), hs->peer_sigalgs.end(), |
| 269 | cred->dc_algorithm) == hs->peer_sigalgs.end()) { |
| 270 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS); |
| 271 | return false; |
| 272 | } |
| 273 | break; |
| 274 | default: |
| 275 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
| 276 | return false; |
| 277 | } |
| 278 |
| 279 | // If we reach here then the credential requires a signature. If |cred| is a |
| 280 | // delegated credential, this also checks that the peer supports delegated |
| 281 | // credentials and matched |dc_cert_verify_algorithm|. |
| 282 | if (!tls1_choose_signature_algorithm(hs, cred, out_sigalg)) { |
| 283 | return false; |
| 284 | } |
| 285 | // Use this credential if it either matches a requested issuer, |
| 286 | // or does not require issuer matching. |
| 287 | return ssl_credential_matches_requested_issuers(hs, cred); |
| 288 | } |
| 289 |
| 290 | static bool check_pake_credential(SSL_HANDSHAKE *hs, |
| 291 | const SSL_CREDENTIAL *cred) { |
| 292 | assert(cred->type == SSLCredentialType::kSPAKE2PlusV1Server); |
| 293 | // Look for a client PAKE share that matches |cred|. |
| 294 | if (hs->pake_share == nullptr || |
| 295 | hs->pake_share->named_pake != SSL_PAKE_SPAKE2PLUSV1 || |
| 296 | hs->pake_share->client_identity != Span(cred->client_identity) || |
| 297 | hs->pake_share->server_identity != Span(cred->server_identity)) { |
| 298 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_PAKE_MISMATCH); |
| 299 | return false; |
| 300 | } |
| 301 |
| 302 | return true; |
| 303 | } |
| 304 |
| 305 | static enum ssl_hs_wait_t do_select_parameters(SSL_HANDSHAKE *hs) { |
| 306 | // At this point, most ClientHello extensions have already been processed by |
| 307 | // the common handshake logic. Resolve the remaining non-PSK parameters. |
| 308 | SSL *const ssl = hs->ssl; |
| 309 | SSLMessage msg; |
| 310 | SSL_CLIENT_HELLO client_hello; |
| 311 | if (!hs->GetClientHello(&msg, &client_hello)) { |
| 312 | return ssl_hs_error; |
| 313 | } |
| 314 |
| 315 | if (SSL_is_quic(ssl) && client_hello.session_id_len > 0) { |
| 316 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_COMPATIBILITY_MODE); |
| 317 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 318 | return ssl_hs_error; |
| 319 | } |
| 320 | // DTLS 1.3 disables compatibility mode, and even if the client advertised a |
| 321 | // session ID (for resumption in DTLS 1.2), the server "MUST NOT echo the |
| 322 | // 'legacy_session_id' value from the client" (RFC 9147, section 5) as it |
| 323 | // would in a TLS 1.3 handshake. |
| 324 | if (!SSL_is_dtls(ssl)) { |
| 325 | hs->session_id.CopyFrom( |
| 326 | Span(client_hello.session_id, client_hello.session_id_len)); |
| 327 | } |
| 328 |
| 329 | Array<SSL_CREDENTIAL *> creds; |
| 330 | if (!ssl_get_full_credential_list(hs, &creds)) { |
| 331 | return ssl_hs_error; |
| 332 | } |
| 333 | if (creds.empty()) { |
| 334 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET); |
| 335 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 336 | return ssl_hs_error; |
| 337 | } |
| 338 |
| 339 | // Select the credential to use. |
| 340 | for (SSL_CREDENTIAL *cred : creds) { |
| 341 | ERR_clear_error(); |
| 342 | if (cred->type == SSLCredentialType::kSPAKE2PlusV1Server) { |
| 343 | if (check_pake_credential(hs, cred)) { |
| 344 | hs->credential = UpRef(cred); |
| 345 | hs->pake_verifier = MakeUnique<spake2plus::Verifier>(); |
| 346 | if (hs->pake_verifier == nullptr || |
| 347 | !hs->pake_verifier->Init(cred->pake_context, cred->client_identity, |
| 348 | cred->server_identity, |
| 349 | cred->password_verifier_w0, |
| 350 | cred->registration_record)) { |
| 351 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 352 | return ssl_hs_error; |
| 353 | } |
| 354 | break; |
| 355 | } |
| 356 | } else { |
| 357 | uint16_t sigalg; |
| 358 | if (check_signature_credential(hs, cred, &sigalg)) { |
| 359 | hs->credential = UpRef(cred); |
| 360 | hs->signature_algorithm = sigalg; |
| 361 | break; |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | if (hs->credential == nullptr) { |
| 366 | // The error from the last attempt is in the error queue. |
| 367 | assert(ERR_peek_error() != 0); |
| 368 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 369 | return ssl_hs_error; |
| 370 | } |
| 371 |
| 372 | // Negotiate the cipher suite. |
| 373 | hs->new_cipher = choose_tls13_cipher(ssl, &client_hello); |
| 374 | if (hs->new_cipher == NULL) { |
| 375 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER); |
| 376 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 377 | return ssl_hs_error; |
| 378 | } |
| 379 |
| 380 | // HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was |
| 381 | // deferred. Complete it now. |
| 382 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 383 | if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) { |
| 384 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 385 | return ssl_hs_error; |
| 386 | } |
| 387 |
| 388 | // The PRF hash is now known. |
| 389 | if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher)) { |
| 390 | return ssl_hs_error; |
| 391 | } |
| 392 |
| 393 | hs->tls13_state = state13_select_session; |
| 394 | return ssl_hs_ok; |
| 395 | } |
| 396 |
| 397 | static enum ssl_ticket_aead_result_t select_session( |
| 398 | SSL_HANDSHAKE *hs, uint8_t *out_alert, UniquePtr<SSL_SESSION> *out_session, |
| 399 | int32_t *out_ticket_age_skew, bool *out_offered_ticket, |
| 400 | const SSLMessage &msg, const SSL_CLIENT_HELLO *client_hello) { |
| 401 | SSL *const ssl = hs->ssl; |
| 402 | *out_session = nullptr; |
| 403 |
| 404 | CBS pre_shared_key; |
| 405 | *out_offered_ticket = ssl_client_hello_get_extension( |
| 406 | client_hello, &pre_shared_key, TLSEXT_TYPE_pre_shared_key); |
| 407 | if (!*out_offered_ticket) { |
| 408 | return ssl_ticket_aead_ignore_ticket; |
| 409 | } |
| 410 |
| 411 | // Per RFC 8446, section 4.2.9, servers MUST abort the handshake if the client |
| 412 | // sends pre_shared_key without psk_key_exchange_modes. |
| 413 | CBS unused; |
| 414 | if (!ssl_client_hello_get_extension(client_hello, &unused, |
| 415 | TLSEXT_TYPE_psk_key_exchange_modes)) { |
| 416 | *out_alert = SSL_AD_MISSING_EXTENSION; |
| 417 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION); |
| 418 | return ssl_ticket_aead_error; |
| 419 | } |
| 420 |
| 421 | CBS ticket, binders; |
| 422 | uint32_t client_ticket_age; |
| 423 | if (!ssl_ext_pre_shared_key_parse_clienthello( |
| 424 | hs, &ticket, &binders, &client_ticket_age, out_alert, client_hello, |
| 425 | &pre_shared_key)) { |
| 426 | return ssl_ticket_aead_error; |
| 427 | } |
| 428 |
| 429 | // If the peer did not offer psk_dhe, ignore the resumption. |
| 430 | if (!hs->accept_psk_mode) { |
| 431 | return ssl_ticket_aead_ignore_ticket; |
| 432 | } |
| 433 |
| 434 | // We do not currently support resumption with PAKEs. |
| 435 | if (hs->credential != nullptr && |
| 436 | hs->credential->type == SSLCredentialType::kSPAKE2PlusV1Server) { |
| 437 | return ssl_ticket_aead_ignore_ticket; |
| 438 | } |
| 439 |
| 440 | // TLS 1.3 session tickets are renewed separately as part of the |
| 441 | // NewSessionTicket. |
| 442 | bool unused_renew; |
| 443 | UniquePtr<SSL_SESSION> session; |
| 444 | enum ssl_ticket_aead_result_t ret = |
| 445 | ssl_process_ticket(hs, &session, &unused_renew, ticket, {}); |
| 446 | switch (ret) { |
| 447 | case ssl_ticket_aead_success: |
| 448 | break; |
| 449 | case ssl_ticket_aead_error: |
| 450 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 451 | return ret; |
| 452 | default: |
| 453 | return ret; |
| 454 | } |
| 455 |
| 456 | if (!ssl_session_is_resumable(hs, session.get()) || |
| 457 | // Historically, some TLS 1.3 tickets were missing ticket_age_add. |
| 458 | !session->ticket_age_add_valid) { |
| 459 | return ssl_ticket_aead_ignore_ticket; |
| 460 | } |
| 461 |
| 462 | // Recover the client ticket age and convert to seconds. |
| 463 | client_ticket_age -= session->ticket_age_add; |
| 464 | client_ticket_age /= 1000; |
| 465 |
| 466 | OPENSSL_timeval now = ssl_ctx_get_current_time(ssl->ctx.get()); |
| 467 |
| 468 | // Compute the server ticket age in seconds. |
| 469 | assert(now.tv_sec >= session->time); |
| 470 | uint64_t server_ticket_age = now.tv_sec - session->time; |
| 471 |
| 472 | // To avoid overflowing |hs->ticket_age_skew|, we will not resume |
| 473 | // 68-year-old sessions. |
| 474 | if (server_ticket_age > INT32_MAX) { |
| 475 | return ssl_ticket_aead_ignore_ticket; |
| 476 | } |
| 477 |
| 478 | *out_ticket_age_skew = static_cast<int32_t>(client_ticket_age) - |
| 479 | static_cast<int32_t>(server_ticket_age); |
| 480 |
| 481 | // Check the PSK binder. |
| 482 | if (!tls13_verify_psk_binder(hs, session.get(), msg, &binders)) { |
| 483 | *out_alert = SSL_AD_DECRYPT_ERROR; |
| 484 | return ssl_ticket_aead_error; |
| 485 | } |
| 486 |
| 487 | *out_session = std::move(session); |
| 488 | return ssl_ticket_aead_success; |
| 489 | } |
| 490 |
| 491 | static bool quic_ticket_compatible(const SSL_SESSION *session, |
| 492 | const SSL_CONFIG *config) { |
| 493 | if (!session->is_quic) { |
| 494 | return true; |
| 495 | } |
| 496 |
| 497 | if (session->quic_early_data_context.empty() || |
| 498 | config->quic_early_data_context.size() != |
| 499 | session->quic_early_data_context.size() || |
| 500 | CRYPTO_memcmp(config->quic_early_data_context.data(), |
| 501 | session->quic_early_data_context.data(), |
| 502 | session->quic_early_data_context.size()) != 0) { |
| 503 | return false; |
| 504 | } |
| 505 | return true; |
| 506 | } |
| 507 |
| 508 | static enum ssl_hs_wait_t do_select_session(SSL_HANDSHAKE *hs) { |
| 509 | SSL *const ssl = hs->ssl; |
| 510 | SSLMessage msg; |
| 511 | SSL_CLIENT_HELLO client_hello; |
| 512 | if (!hs->GetClientHello(&msg, &client_hello)) { |
| 513 | return ssl_hs_error; |
| 514 | } |
| 515 |
| 516 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 517 | UniquePtr<SSL_SESSION> session; |
| 518 | bool offered_ticket = false; |
| 519 | switch (select_session(hs, &alert, &session, &ssl->s3->ticket_age_skew, |
| 520 | &offered_ticket, msg, &client_hello)) { |
| 521 | case ssl_ticket_aead_ignore_ticket: |
| 522 | assert(!session); |
| 523 | if (!ssl_get_new_session(hs)) { |
| 524 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 525 | return ssl_hs_error; |
| 526 | } |
| 527 | break; |
| 528 |
| 529 | case ssl_ticket_aead_success: |
| 530 | // Carry over authentication information from the previous handshake into |
| 531 | // a fresh session. |
| 532 | hs->new_session = |
| 533 | SSL_SESSION_dup(session.get(), SSL_SESSION_DUP_AUTH_ONLY); |
| 534 | if (hs->new_session == nullptr) { |
| 535 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 536 | return ssl_hs_error; |
| 537 | } |
| 538 |
| 539 | ssl->s3->session_reused = true; |
| 540 | hs->can_release_private_key = true; |
| 541 |
| 542 | // Resumption incorporates fresh key material, so refresh the timeout. |
| 543 | ssl_session_renew_timeout(ssl, hs->new_session.get(), |
| 544 | ssl->session_ctx->session_psk_dhe_timeout); |
| 545 | break; |
| 546 |
| 547 | case ssl_ticket_aead_error: |
| 548 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 549 | return ssl_hs_error; |
| 550 |
| 551 | case ssl_ticket_aead_retry: |
| 552 | hs->tls13_state = state13_select_session; |
| 553 | return ssl_hs_pending_ticket; |
| 554 | } |
| 555 |
| 556 | // Negotiate ALPS now, after ALPN is negotiated and |hs->new_session| is |
| 557 | // initialized. |
| 558 | if (!ssl_negotiate_alps(hs, &alert, &client_hello)) { |
| 559 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 560 | return ssl_hs_error; |
| 561 | } |
| 562 |
| 563 | // Record connection properties in the new session. |
| 564 | hs->new_session->cipher = hs->new_cipher; |
| 565 |
| 566 | // If using key shares, resolve the supported group and determine if we need |
| 567 | // HelloRetryRequest. |
| 568 | bool need_hrr = false; |
| 569 | if (hs->pake_verifier == nullptr) { |
| 570 | if (!tls1_get_shared_group(hs, &hs->new_session->group_id)) { |
| 571 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP); |
| 572 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 573 | return ssl_hs_error; |
| 574 | } |
| 575 | bool found_key_share; |
| 576 | if (!ssl_ext_key_share_parse_clienthello(hs, &found_key_share, |
| 577 | /*out_key_share=*/nullptr, &alert, |
| 578 | &client_hello)) { |
| 579 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 580 | return ssl_hs_error; |
| 581 | } |
| 582 | need_hrr = !found_key_share; |
| 583 | } |
| 584 |
| 585 | // Determine if we're negotiating 0-RTT. |
| 586 | if (!ssl->enable_early_data) { |
| 587 | ssl->s3->early_data_reason = ssl_early_data_disabled; |
| 588 | } else if (!offered_ticket) { |
| 589 | ssl->s3->early_data_reason = ssl_early_data_no_session_offered; |
| 590 | } else if (!session) { |
| 591 | ssl->s3->early_data_reason = ssl_early_data_session_not_resumed; |
| 592 | } else if (session->ticket_max_early_data == 0) { |
| 593 | ssl->s3->early_data_reason = ssl_early_data_unsupported_for_session; |
| 594 | } else if (!hs->early_data_offered) { |
| 595 | ssl->s3->early_data_reason = ssl_early_data_peer_declined; |
| 596 | } else if (hs->channel_id_negotiated) { |
| 597 | // Channel ID is incompatible with 0-RTT. |
| 598 | ssl->s3->early_data_reason = ssl_early_data_channel_id; |
| 599 | } else if (Span(ssl->s3->alpn_selected) != session->early_alpn) { |
| 600 | // The negotiated ALPN must match the one in the ticket. |
| 601 | ssl->s3->early_data_reason = ssl_early_data_alpn_mismatch; |
| 602 | } else if (hs->new_session->has_application_settings != |
| 603 | session->has_application_settings || |
| 604 | Span(hs->new_session->local_application_settings) != |
| 605 | session->local_application_settings) { |
| 606 | ssl->s3->early_data_reason = ssl_early_data_alps_mismatch; |
| 607 | } else if (ssl->s3->ticket_age_skew < -kMaxTicketAgeSkewSeconds || |
| 608 | kMaxTicketAgeSkewSeconds < ssl->s3->ticket_age_skew) { |
| 609 | ssl->s3->early_data_reason = ssl_early_data_ticket_age_skew; |
| 610 | } else if (!quic_ticket_compatible(session.get(), hs->config)) { |
| 611 | ssl->s3->early_data_reason = ssl_early_data_quic_parameter_mismatch; |
| 612 | } else if (need_hrr) { |
| 613 | ssl->s3->early_data_reason = ssl_early_data_hello_retry_request; |
| 614 | } else { |
| 615 | // |ssl_session_is_resumable| forbids cross-cipher resumptions even if the |
| 616 | // PRF hashes match. |
| 617 | assert(hs->new_cipher == session->cipher); |
| 618 |
| 619 | ssl->s3->early_data_reason = ssl_early_data_accepted; |
| 620 | ssl->s3->early_data_accepted = true; |
| 621 | } |
| 622 |
| 623 | // Store the ALPN and ALPS values in the session for 0-RTT. Note the peer |
| 624 | // applications settings are not generally known until client |
| 625 | // EncryptedExtensions. |
| 626 | if (!hs->new_session->early_alpn.CopyFrom(ssl->s3->alpn_selected)) { |
| 627 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 628 | return ssl_hs_error; |
| 629 | } |
| 630 |
| 631 | // The peer applications settings are usually received later, in |
| 632 | // EncryptedExtensions. But, in 0-RTT handshakes, we carry over the |
| 633 | // values from |session|. Do this now, before |session| is discarded. |
| 634 | if (ssl->s3->early_data_accepted && |
| 635 | hs->new_session->has_application_settings && |
| 636 | !hs->new_session->peer_application_settings.CopyFrom( |
| 637 | session->peer_application_settings)) { |
| 638 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 639 | return ssl_hs_error; |
| 640 | } |
| 641 |
| 642 | // Copy the QUIC early data context to the session. |
| 643 | if (ssl->enable_early_data && SSL_is_quic(ssl)) { |
| 644 | if (!hs->new_session->quic_early_data_context.CopyFrom( |
| 645 | hs->config->quic_early_data_context)) { |
| 646 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 647 | return ssl_hs_error; |
| 648 | } |
| 649 | } |
| 650 |
| 651 | if (ssl->ctx->dos_protection_cb != NULL && |
| 652 | ssl->ctx->dos_protection_cb(&client_hello) == 0) { |
| 653 | // Connection rejected for DOS reasons. |
| 654 | OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED); |
| 655 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 656 | return ssl_hs_error; |
| 657 | } |
| 658 |
| 659 | size_t hash_len = EVP_MD_size( |
| 660 | ssl_get_handshake_digest(ssl_protocol_version(ssl), hs->new_cipher)); |
| 661 |
| 662 | // Set up the key schedule and incorporate the PSK into the running secret. |
| 663 | if (!tls13_init_key_schedule(hs, ssl->s3->session_reused |
| 664 | ? Span(hs->new_session->secret) |
| 665 | : Span(kZeroes, hash_len)) || |
| 666 | !ssl_hash_message(hs, msg)) { |
| 667 | return ssl_hs_error; |
| 668 | } |
| 669 |
| 670 | if (ssl->s3->early_data_accepted) { |
| 671 | if (!tls13_derive_early_secret(hs)) { |
| 672 | return ssl_hs_error; |
| 673 | } |
| 674 | } else if (hs->early_data_offered) { |
| 675 | ssl->s3->skip_early_data = true; |
| 676 | } |
| 677 |
| 678 | if (need_hrr) { |
| 679 | ssl->method->next_message(ssl); |
| 680 | if (!hs->transcript.UpdateForHelloRetryRequest()) { |
| 681 | return ssl_hs_error; |
| 682 | } |
| 683 | hs->tls13_state = state13_send_hello_retry_request; |
| 684 | return ssl_hs_ok; |
| 685 | } |
| 686 |
| 687 | if (hs->pake_verifier) { |
| 688 | assert(!ssl->s3->session_reused); |
| 689 | // Revealing the PAKE share (notably confirmV) allows the client to confirm |
| 690 | // one PAKE guess, so we must deduct from the brute force limit. |
| 691 | if (!hs->credential->ClaimPAKEAttempt()) { |
| 692 | OPENSSL_PUT_ERROR(SSL, SSL_R_PAKE_EXHAUSTED); |
| 693 | ssl_send_alert(hs->ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 694 | return ssl_hs_error; |
| 695 | } |
| 696 | if (!resolve_pake_secret(hs)) { |
| 697 | return ssl_hs_error; |
| 698 | } |
| 699 | } else { |
| 700 | if (!resolve_ecdhe_secret(hs, &client_hello)) { |
| 701 | return ssl_hs_error; |
| 702 | } |
| 703 | } |
| 704 |
| 705 | ssl->method->next_message(ssl); |
| 706 | hs->ech_client_hello_buf.Reset(); |
| 707 | hs->tls13_state = state13_send_server_hello; |
| 708 | return ssl_hs_ok; |
| 709 | } |
| 710 |
| 711 | static enum ssl_hs_wait_t do_send_hello_retry_request(SSL_HANDSHAKE *hs) { |
| 712 | SSL *const ssl = hs->ssl; |
| 713 | if (hs->hints_requested) { |
| 714 | return ssl_hs_hints_ready; |
| 715 | } |
| 716 |
| 717 | // Although a server could HelloRetryRequest with PAKEs to request a cookie, |
| 718 | // we never do so. |
| 719 | assert(hs->pake_verifier == nullptr); |
| 720 | ScopedCBB cbb; |
| 721 | CBB body, session_id, extensions; |
| 722 | if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) || |
| 723 | !CBB_add_u16(&body, |
| 724 | SSL_is_dtls(ssl) ? DTLS1_2_VERSION : TLS1_2_VERSION) || |
| 725 | !CBB_add_bytes(&body, kHelloRetryRequest, SSL3_RANDOM_SIZE) || |
| 726 | !CBB_add_u8_length_prefixed(&body, &session_id) || |
| 727 | !CBB_add_bytes(&session_id, hs->session_id.data(), |
| 728 | hs->session_id.size()) || |
| 729 | !CBB_add_u16(&body, SSL_CIPHER_get_protocol_id(hs->new_cipher)) || |
| 730 | !CBB_add_u8(&body, 0 /* no compression */) || |
| 731 | !CBB_add_u16_length_prefixed(&body, &extensions) || |
| 732 | !CBB_add_u16(&extensions, TLSEXT_TYPE_supported_versions) || |
| 733 | !CBB_add_u16(&extensions, 2 /* length */) || |
| 734 | !CBB_add_u16(&extensions, ssl->s3->version) || |
| 735 | !CBB_add_u16(&extensions, TLSEXT_TYPE_key_share) || |
| 736 | !CBB_add_u16(&extensions, 2 /* length */) || |
| 737 | !CBB_add_u16(&extensions, hs->new_session->group_id)) { |
| 738 | return ssl_hs_error; |
| 739 | } |
| 740 | if (hs->ech_is_inner) { |
| 741 | // Fill a placeholder for the ECH confirmation value. |
| 742 | if (!CBB_add_u16(&extensions, TLSEXT_TYPE_encrypted_client_hello) || |
| 743 | !CBB_add_u16(&extensions, ECH_CONFIRMATION_SIGNAL_LEN) || |
| 744 | !CBB_add_zeros(&extensions, ECH_CONFIRMATION_SIGNAL_LEN)) { |
| 745 | return ssl_hs_error; |
| 746 | } |
| 747 | } |
| 748 | Array<uint8_t> hrr; |
| 749 | if (!ssl->method->finish_message(ssl, cbb.get(), &hrr)) { |
| 750 | return ssl_hs_error; |
| 751 | } |
| 752 | if (hs->ech_is_inner) { |
| 753 | // Now that the message is encoded, fill in the whole value. |
| 754 | size_t offset = hrr.size() - ECH_CONFIRMATION_SIGNAL_LEN; |
| 755 | if (!ssl_ech_accept_confirmation( |
| 756 | hs, Span(hrr).last(ECH_CONFIRMATION_SIGNAL_LEN), |
| 757 | ssl->s3->client_random, hs->transcript, /*is_hrr=*/true, hrr, |
| 758 | offset)) { |
| 759 | return ssl_hs_error; |
| 760 | } |
| 761 | } |
| 762 |
| 763 | if (!ssl->method->add_message(ssl, std::move(hrr)) || |
| 764 | !ssl->method->add_change_cipher_spec(ssl)) { |
| 765 | return ssl_hs_error; |
| 766 | } |
| 767 |
| 768 | ssl->s3->used_hello_retry_request = true; |
| 769 | hs->tls13_state = state13_read_second_client_hello; |
| 770 | return ssl_hs_flush; |
| 771 | } |
| 772 |
| 773 | static enum ssl_hs_wait_t do_read_second_client_hello(SSL_HANDSHAKE *hs) { |
| 774 | SSL *const ssl = hs->ssl; |
| 775 | SSLMessage msg; |
| 776 | if (!ssl->method->get_message(ssl, &msg)) { |
| 777 | return ssl_hs_read_message; |
| 778 | } |
| 779 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_HELLO)) { |
| 780 | return ssl_hs_error; |
| 781 | } |
| 782 | SSL_CLIENT_HELLO client_hello; |
| 783 | if (!SSL_parse_client_hello(ssl, &client_hello, CBS_data(&msg.body), |
| 784 | CBS_len(&msg.body))) { |
| 785 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 786 | return ssl_hs_error; |
| 787 | } |
| 788 |
| 789 | if (ssl->s3->ech_status == ssl_ech_accepted) { |
| 790 | // If we previously accepted the ClientHelloInner, the second ClientHello |
| 791 | // must contain an outer encrypted_client_hello extension. |
| 792 | CBS ech_body; |
| 793 | if (!ssl_client_hello_get_extension(&client_hello, &ech_body, |
| 794 | TLSEXT_TYPE_encrypted_client_hello)) { |
| 795 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION); |
| 796 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION); |
| 797 | return ssl_hs_error; |
| 798 | } |
| 799 | uint16_t kdf_id, aead_id; |
| 800 | uint8_t type, config_id; |
| 801 | CBS enc, payload; |
| 802 | if (!CBS_get_u8(&ech_body, &type) || // |
| 803 | type != ECH_CLIENT_OUTER || // |
| 804 | !CBS_get_u16(&ech_body, &kdf_id) || // |
| 805 | !CBS_get_u16(&ech_body, &aead_id) || |
| 806 | !CBS_get_u8(&ech_body, &config_id) || |
| 807 | !CBS_get_u16_length_prefixed(&ech_body, &enc) || |
| 808 | !CBS_get_u16_length_prefixed(&ech_body, &payload) || |
| 809 | CBS_len(&ech_body) != 0) { |
| 810 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 811 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 812 | return ssl_hs_error; |
| 813 | } |
| 814 |
| 815 | if (kdf_id != EVP_HPKE_KDF_id(EVP_HPKE_CTX_kdf(hs->ech_hpke_ctx.get())) || |
| 816 | aead_id != |
| 817 | EVP_HPKE_AEAD_id(EVP_HPKE_CTX_aead(hs->ech_hpke_ctx.get())) || |
| 818 | config_id != hs->ech_config_id || CBS_len(&enc) > 0) { |
| 819 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 820 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 821 | return ssl_hs_error; |
| 822 | } |
| 823 |
| 824 | // Decrypt the payload with the HPKE context from the first ClientHello. |
| 825 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 826 | bool unused; |
| 827 | if (!ssl_client_hello_decrypt(hs, &alert, &unused, |
| 828 | &hs->ech_client_hello_buf, &client_hello, |
| 829 | payload)) { |
| 830 | // Decryption failure is fatal in the second ClientHello. |
| 831 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED); |
| 832 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 833 | return ssl_hs_error; |
| 834 | } |
| 835 |
| 836 | // Reparse |client_hello| from the buffer owned by |hs|. |
| 837 | if (!hs->GetClientHello(&msg, &client_hello)) { |
| 838 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 839 | return ssl_hs_error; |
| 840 | } |
| 841 | } |
| 842 |
| 843 | // We perform all our negotiation based on the first ClientHello (for |
| 844 | // consistency with what |select_certificate_cb| observed), which is in the |
| 845 | // transcript, so we can ignore most of this second one. |
| 846 | // |
| 847 | // We do, however, check the second PSK binder. This covers the client key |
| 848 | // share, in case we ever send half-RTT data (we currently do not). It is also |
| 849 | // a tricky computation, so we enforce the peer handled it correctly. |
| 850 | if (ssl->s3->session_reused) { |
| 851 | CBS pre_shared_key; |
| 852 | if (!ssl_client_hello_get_extension(&client_hello, &pre_shared_key, |
| 853 | TLSEXT_TYPE_pre_shared_key)) { |
| 854 | OPENSSL_PUT_ERROR(SSL, SSL_R_INCONSISTENT_CLIENT_HELLO); |
| 855 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 856 | return ssl_hs_error; |
| 857 | } |
| 858 |
| 859 | CBS ticket, binders; |
| 860 | uint32_t client_ticket_age; |
| 861 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 862 | if (!ssl_ext_pre_shared_key_parse_clienthello( |
| 863 | hs, &ticket, &binders, &client_ticket_age, &alert, &client_hello, |
| 864 | &pre_shared_key)) { |
| 865 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 866 | return ssl_hs_error; |
| 867 | } |
| 868 |
| 869 | // Note it is important that we do not obtain a new |SSL_SESSION| from |
| 870 | // |ticket|. We have already selected parameters based on the first |
| 871 | // ClientHello (in the transcript) and must not switch partway through. |
| 872 | if (!tls13_verify_psk_binder(hs, hs->new_session.get(), msg, &binders)) { |
| 873 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
| 874 | return ssl_hs_error; |
| 875 | } |
| 876 | } |
| 877 |
| 878 | // Although a server could HelloRetryRequest with PAKEs to request a cookie, |
| 879 | // we never do so. |
| 880 | assert(hs->pake_verifier == nullptr); |
| 881 | if (!resolve_ecdhe_secret(hs, &client_hello)) { |
| 882 | return ssl_hs_error; |
| 883 | } |
| 884 |
| 885 | if (!ssl_hash_message(hs, msg)) { |
| 886 | return ssl_hs_error; |
| 887 | } |
| 888 |
| 889 | // ClientHello should be the end of the flight. |
| 890 | if (ssl->method->has_unprocessed_handshake_data(ssl)) { |
| 891 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 892 | OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESS_HANDSHAKE_DATA); |
| 893 | return ssl_hs_error; |
| 894 | } |
| 895 |
| 896 | ssl->method->next_message(ssl); |
| 897 | hs->ech_client_hello_buf.Reset(); |
| 898 | hs->tls13_state = state13_send_server_hello; |
| 899 | return ssl_hs_ok; |
| 900 | } |
| 901 |
| 902 | static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) { |
| 903 | SSL *const ssl = hs->ssl; |
| 904 |
| 905 | Span<uint8_t> random(ssl->s3->server_random); |
| 906 |
| 907 | SSL_HANDSHAKE_HINTS *const hints = hs->hints.get(); |
| 908 | if (hints && !hs->hints_requested && |
| 909 | hints->server_random_tls13.size() == random.size()) { |
| 910 | OPENSSL_memcpy(random.data(), hints->server_random_tls13.data(), |
| 911 | random.size()); |
| 912 | } else { |
| 913 | RAND_bytes(random.data(), random.size()); |
| 914 | if (hints && hs->hints_requested && |
| 915 | !hints->server_random_tls13.CopyFrom(random)) { |
| 916 | return ssl_hs_error; |
| 917 | } |
| 918 | } |
| 919 |
| 920 | Array<uint8_t> server_hello; |
| 921 | ScopedCBB cbb; |
| 922 | CBB body, extensions, session_id; |
| 923 | if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) || |
| 924 | !CBB_add_u16(&body, |
| 925 | SSL_is_dtls(ssl) ? DTLS1_2_VERSION : TLS1_2_VERSION) || |
| 926 | !CBB_add_bytes(&body, ssl->s3->server_random, |
| 927 | sizeof(ssl->s3->server_random)) || |
| 928 | !CBB_add_u8_length_prefixed(&body, &session_id) || |
| 929 | !CBB_add_bytes(&session_id, hs->session_id.data(), |
| 930 | hs->session_id.size()) || |
| 931 | !CBB_add_u16(&body, SSL_CIPHER_get_protocol_id(hs->new_cipher)) || |
| 932 | !CBB_add_u8(&body, 0) || |
| 933 | !CBB_add_u16_length_prefixed(&body, &extensions) || |
| 934 | !ssl_ext_pre_shared_key_add_serverhello(hs, &extensions) || |
| 935 | !ssl_ext_pake_add_serverhello(hs, &extensions) || |
| 936 | !ssl_ext_key_share_add_serverhello(hs, &extensions) || |
| 937 | !ssl_ext_supported_versions_add_serverhello(hs, &extensions) || |
| 938 | !ssl->method->finish_message(ssl, cbb.get(), &server_hello)) { |
| 939 | return ssl_hs_error; |
| 940 | } |
| 941 |
| 942 | assert(ssl->s3->ech_status != ssl_ech_accepted || hs->ech_is_inner); |
| 943 | if (hs->ech_is_inner) { |
| 944 | // Fill in the ECH confirmation signal. |
| 945 | const size_t offset = ssl_ech_confirmation_signal_hello_offset(ssl); |
| 946 | Span<uint8_t> random_suffix = random.last(ECH_CONFIRMATION_SIGNAL_LEN); |
| 947 | if (!ssl_ech_accept_confirmation(hs, random_suffix, ssl->s3->client_random, |
| 948 | hs->transcript, |
| 949 | /*is_hrr=*/false, server_hello, offset)) { |
| 950 | return ssl_hs_error; |
| 951 | } |
| 952 |
| 953 | // Update |server_hello|. |
| 954 | Span<uint8_t> server_hello_out = |
| 955 | Span(server_hello).subspan(offset, ECH_CONFIRMATION_SIGNAL_LEN); |
| 956 | OPENSSL_memcpy(server_hello_out.data(), random_suffix.data(), |
| 957 | ECH_CONFIRMATION_SIGNAL_LEN); |
| 958 | } |
| 959 |
| 960 | if (!ssl->method->add_message(ssl, std::move(server_hello))) { |
| 961 | return ssl_hs_error; |
| 962 | } |
| 963 |
| 964 | hs->key_share_ciphertext.Reset(); // No longer needed. |
| 965 | if (!ssl->s3->used_hello_retry_request && |
| 966 | !ssl->method->add_change_cipher_spec(ssl)) { |
| 967 | return ssl_hs_error; |
| 968 | } |
| 969 |
| 970 | // Derive and enable the handshake traffic secrets. |
| 971 | if (!tls13_derive_handshake_secrets(hs) || |
| 972 | !tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_seal, |
| 973 | hs->new_session.get(), |
| 974 | hs->server_handshake_secret)) { |
| 975 | return ssl_hs_error; |
| 976 | } |
| 977 |
| 978 | // Send EncryptedExtensions. |
| 979 | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
| 980 | SSL3_MT_ENCRYPTED_EXTENSIONS) || |
| 981 | !ssl_add_serverhello_tlsext(hs, &body) || |
| 982 | !ssl_add_message_cbb(ssl, cbb.get())) { |
| 983 | return ssl_hs_error; |
| 984 | } |
| 985 |
| 986 | if (!ssl->s3->session_reused && !hs->pake_verifier) { |
| 987 | // Determine whether to request a client certificate. |
| 988 | hs->cert_request = !!(hs->config->verify_mode & SSL_VERIFY_PEER); |
| 989 | } |
| 990 |
| 991 | // Send a CertificateRequest, if necessary. |
| 992 | if (hs->cert_request) { |
| 993 | CBB cert_request_extensions, sigalg_contents, sigalgs_cbb; |
| 994 | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
| 995 | SSL3_MT_CERTIFICATE_REQUEST) || |
| 996 | !CBB_add_u8(&body, 0 /* no certificate_request_context. */) || |
| 997 | !CBB_add_u16_length_prefixed(&body, &cert_request_extensions) || |
| 998 | !CBB_add_u16(&cert_request_extensions, |
| 999 | TLSEXT_TYPE_signature_algorithms) || |
| 1000 | !CBB_add_u16_length_prefixed(&cert_request_extensions, |
| 1001 | &sigalg_contents) || |
| 1002 | !CBB_add_u16_length_prefixed(&sigalg_contents, &sigalgs_cbb) || |
| 1003 | !tls12_add_verify_sigalgs(hs, &sigalgs_cbb)) { |
| 1004 | return ssl_hs_error; |
| 1005 | } |
| 1006 |
| 1007 | if (ssl_has_client_CAs(hs->config)) { |
| 1008 | CBB ca_contents; |
| 1009 | if (!CBB_add_u16(&cert_request_extensions, |
| 1010 | TLSEXT_TYPE_certificate_authorities) || |
| 1011 | !CBB_add_u16_length_prefixed(&cert_request_extensions, |
| 1012 | &ca_contents) || |
| 1013 | !ssl_add_client_CA_list(hs, &ca_contents) || |
| 1014 | !CBB_flush(&cert_request_extensions)) { |
| 1015 | return ssl_hs_error; |
| 1016 | } |
| 1017 | } |
| 1018 |
| 1019 | if (!ssl_add_message_cbb(ssl, cbb.get())) { |
| 1020 | return ssl_hs_error; |
| 1021 | } |
| 1022 | } |
| 1023 |
| 1024 | // Send the server Certificate message, if necessary. |
| 1025 | if (!ssl->s3->session_reused && !hs->pake_verifier) { |
| 1026 | if (!tls13_add_certificate(hs)) { |
| 1027 | return ssl_hs_error; |
| 1028 | } |
| 1029 |
| 1030 | hs->tls13_state = state13_send_server_certificate_verify; |
| 1031 | return ssl_hs_ok; |
| 1032 | } |
| 1033 |
| 1034 | hs->tls13_state = state13_send_server_finished; |
| 1035 | return ssl_hs_ok; |
| 1036 | } |
| 1037 |
| 1038 | static enum ssl_hs_wait_t do_send_server_certificate_verify(SSL_HANDSHAKE *hs) { |
| 1039 | switch (tls13_add_certificate_verify(hs)) { |
| 1040 | case ssl_private_key_success: |
| 1041 | hs->tls13_state = state13_send_server_finished; |
| 1042 | return ssl_hs_ok; |
| 1043 |
| 1044 | case ssl_private_key_retry: |
| 1045 | hs->tls13_state = state13_send_server_certificate_verify; |
| 1046 | return ssl_hs_private_key_operation; |
| 1047 |
| 1048 | case ssl_private_key_failure: |
| 1049 | return ssl_hs_error; |
| 1050 | } |
| 1051 |
| 1052 | assert(0); |
| 1053 | return ssl_hs_error; |
| 1054 | } |
| 1055 |
| 1056 | static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) { |
| 1057 | SSL *const ssl = hs->ssl; |
| 1058 | if (hs->hints_requested) { |
| 1059 | return ssl_hs_hints_ready; |
| 1060 | } |
| 1061 |
| 1062 | hs->can_release_private_key = true; |
| 1063 | if (!tls13_add_finished(hs) || |
| 1064 | // Update the secret to the master secret and derive traffic keys. |
| 1065 | !tls13_advance_key_schedule(hs, |
| 1066 | Span(kZeroes, hs->transcript.DigestLen())) || |
| 1067 | !tls13_derive_application_secrets(hs) || |
| 1068 | !tls13_set_traffic_key(ssl, ssl_encryption_application, evp_aead_seal, |
| 1069 | hs->new_session.get(), |
| 1070 | hs->server_traffic_secret_0)) { |
| 1071 | return ssl_hs_error; |
| 1072 | } |
| 1073 |
| 1074 | hs->tls13_state = state13_send_half_rtt_ticket; |
| 1075 | return hs->handback ? ssl_hs_handback : ssl_hs_ok; |
| 1076 | } |
| 1077 |
| 1078 | static enum ssl_hs_wait_t do_send_half_rtt_ticket(SSL_HANDSHAKE *hs) { |
| 1079 | SSL *const ssl = hs->ssl; |
| 1080 |
| 1081 | if (ssl->s3->early_data_accepted) { |
| 1082 | // If accepting 0-RTT, we send tickets half-RTT. This gets the tickets on |
| 1083 | // the wire sooner and also avoids triggering a write on |SSL_read| when |
| 1084 | // processing the client Finished. This requires computing the client |
| 1085 | // Finished early. See RFC 8446, section 4.6.1. |
| 1086 | static const uint8_t kEndOfEarlyData[4] = {SSL3_MT_END_OF_EARLY_DATA, 0, 0, |
| 1087 | 0}; |
| 1088 | if (!SSL_is_quic(ssl) && !hs->transcript.Update(kEndOfEarlyData)) { |
| 1089 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1090 | return ssl_hs_error; |
| 1091 | } |
| 1092 |
| 1093 | size_t finished_len; |
| 1094 | hs->expected_client_finished.Resize(hs->transcript.DigestLen()); |
| 1095 | if (!tls13_finished_mac(hs, hs->expected_client_finished.data(), |
| 1096 | &finished_len, false /* client */)) { |
| 1097 | return ssl_hs_error; |
| 1098 | } |
| 1099 |
| 1100 | if (finished_len != hs->expected_client_finished.size()) { |
| 1101 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1102 | return ssl_hs_error; |
| 1103 | } |
| 1104 |
| 1105 | // Feed the predicted Finished into the transcript. This allows us to derive |
| 1106 | // the resumption secret early and send half-RTT tickets. |
| 1107 | // |
| 1108 | // TODO(crbug.com/381113363): Don't use half-RTT tickets with DTLS 1.3. |
| 1109 | // TODO(crbug.com/376939532): Perhaps don't use half-RTT tickets at all. |
| 1110 | assert(!SSL_is_dtls(hs->ssl)); |
| 1111 | assert(hs->expected_client_finished.size() <= 0xff); |
| 1112 | uint8_t header[4] = { |
| 1113 | SSL3_MT_FINISHED, 0, 0, |
| 1114 | static_cast<uint8_t>(hs->expected_client_finished.size())}; |
| 1115 | bool unused_sent_tickets; |
| 1116 | if (!hs->transcript.Update(header) || |
| 1117 | !hs->transcript.Update(hs->expected_client_finished) || |
| 1118 | !tls13_derive_resumption_secret(hs) || |
| 1119 | !add_new_session_tickets(hs, &unused_sent_tickets)) { |
| 1120 | return ssl_hs_error; |
| 1121 | } |
| 1122 | } |
| 1123 |
| 1124 | hs->tls13_state = state13_read_second_client_flight; |
| 1125 | return ssl_hs_flush; |
| 1126 | } |
| 1127 |
| 1128 | static bool uses_end_of_early_data(const SSL *ssl) { |
| 1129 | // DTLS and QUIC omit the EndOfEarlyData message. See RFC 9001, section 8.3, |
| 1130 | // and RFC 9147, section 5.6. |
| 1131 | return !SSL_is_quic(ssl) && !SSL_is_dtls(ssl); |
| 1132 | } |
| 1133 |
| 1134 | static enum ssl_hs_wait_t do_read_second_client_flight(SSL_HANDSHAKE *hs) { |
| 1135 | SSL *const ssl = hs->ssl; |
| 1136 | if (ssl->s3->early_data_accepted) { |
| 1137 | if (!tls13_set_traffic_key(ssl, ssl_encryption_early_data, evp_aead_open, |
| 1138 | hs->new_session.get(), |
| 1139 | hs->early_traffic_secret)) { |
| 1140 | return ssl_hs_error; |
| 1141 | } |
| 1142 | hs->can_early_write = true; |
| 1143 | hs->can_early_read = true; |
| 1144 | hs->in_early_data = true; |
| 1145 | } |
| 1146 |
| 1147 | // If the EndOfEarlyData message is not used, switch to |
| 1148 | // client_handshake_secret before the early return. |
| 1149 | if (!uses_end_of_early_data(ssl)) { |
| 1150 | if (!tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_open, |
| 1151 | hs->new_session.get(), |
| 1152 | hs->client_handshake_secret)) { |
| 1153 | return ssl_hs_error; |
| 1154 | } |
| 1155 | hs->tls13_state = state13_process_end_of_early_data; |
| 1156 | return ssl->s3->early_data_accepted ? ssl_hs_early_return : ssl_hs_ok; |
| 1157 | } |
| 1158 |
| 1159 | hs->tls13_state = state13_process_end_of_early_data; |
| 1160 | return ssl->s3->early_data_accepted ? ssl_hs_read_end_of_early_data |
| 1161 | : ssl_hs_ok; |
| 1162 | } |
| 1163 |
| 1164 | static enum ssl_hs_wait_t do_process_end_of_early_data(SSL_HANDSHAKE *hs) { |
| 1165 | SSL *const ssl = hs->ssl; |
| 1166 | // In protocols that use EndOfEarlyData, we must consume the extra message and |
| 1167 | // switch to client_handshake_secret after the early return. |
| 1168 | if (uses_end_of_early_data(ssl)) { |
| 1169 | // If early data was not accepted, the EndOfEarlyData will be in the |
| 1170 | // discarded early data. |
| 1171 | if (hs->ssl->s3->early_data_accepted) { |
| 1172 | SSLMessage msg; |
| 1173 | if (!ssl->method->get_message(ssl, &msg)) { |
| 1174 | return ssl_hs_read_message; |
| 1175 | } |
| 1176 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_END_OF_EARLY_DATA)) { |
| 1177 | return ssl_hs_error; |
| 1178 | } |
| 1179 | if (CBS_len(&msg.body) != 0) { |
| 1180 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 1181 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1182 | return ssl_hs_error; |
| 1183 | } |
| 1184 | ssl->method->next_message(ssl); |
| 1185 | } |
| 1186 | if (!tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_open, |
| 1187 | hs->new_session.get(), |
| 1188 | hs->client_handshake_secret)) { |
| 1189 | return ssl_hs_error; |
| 1190 | } |
| 1191 | } |
| 1192 | hs->tls13_state = state13_read_client_encrypted_extensions; |
| 1193 | return ssl_hs_ok; |
| 1194 | } |
| 1195 |
| 1196 | static enum ssl_hs_wait_t do_read_client_encrypted_extensions( |
| 1197 | SSL_HANDSHAKE *hs) { |
| 1198 | SSL *const ssl = hs->ssl; |
| 1199 | // For now, only one extension uses client EncryptedExtensions. This function |
| 1200 | // may be generalized if others use it in the future. |
| 1201 | if (hs->new_session->has_application_settings && |
| 1202 | !ssl->s3->early_data_accepted) { |
| 1203 | SSLMessage msg; |
| 1204 | if (!ssl->method->get_message(ssl, &msg)) { |
| 1205 | return ssl_hs_read_message; |
| 1206 | } |
| 1207 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_ENCRYPTED_EXTENSIONS)) { |
| 1208 | return ssl_hs_error; |
| 1209 | } |
| 1210 |
| 1211 | CBS body = msg.body, extensions; |
| 1212 | if (!CBS_get_u16_length_prefixed(&body, &extensions) || |
| 1213 | CBS_len(&body) != 0) { |
| 1214 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1215 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 1216 | return ssl_hs_error; |
| 1217 | } |
| 1218 |
| 1219 | uint16_t extension_type = TLSEXT_TYPE_application_settings_old; |
| 1220 | if (hs->config->alps_use_new_codepoint) { |
| 1221 | extension_type = TLSEXT_TYPE_application_settings; |
| 1222 | } |
| 1223 | SSLExtension application_settings(extension_type); |
| 1224 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 1225 | if (!ssl_parse_extensions(&extensions, &alert, {&application_settings}, |
| 1226 | /*ignore_unknown=*/false)) { |
| 1227 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 1228 | return ssl_hs_error; |
| 1229 | } |
| 1230 |
| 1231 | if (!application_settings.present) { |
| 1232 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION); |
| 1233 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION); |
| 1234 | return ssl_hs_error; |
| 1235 | } |
| 1236 |
| 1237 | // Note that, if 0-RTT was accepted, these values will already have been |
| 1238 | // initialized earlier. |
| 1239 | if (!hs->new_session->peer_application_settings.CopyFrom( |
| 1240 | application_settings.data) || |
| 1241 | !ssl_hash_message(hs, msg)) { |
| 1242 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 1243 | return ssl_hs_error; |
| 1244 | } |
| 1245 |
| 1246 | ssl->method->next_message(ssl); |
| 1247 | } |
| 1248 |
| 1249 | hs->tls13_state = state13_read_client_certificate; |
| 1250 | return ssl_hs_ok; |
| 1251 | } |
| 1252 |
| 1253 | static enum ssl_hs_wait_t do_read_client_certificate(SSL_HANDSHAKE *hs) { |
| 1254 | SSL *const ssl = hs->ssl; |
| 1255 | if (!hs->cert_request) { |
| 1256 | if (!ssl->s3->session_reused) { |
| 1257 | // OpenSSL returns X509_V_OK when no certificates are requested. This is |
| 1258 | // classed by them as a bug, but it's assumed by at least NGINX. (Only do |
| 1259 | // this in full handshakes as resumptions should carry over the previous |
| 1260 | // |verify_result|, though this is a no-op because servers do not |
| 1261 | // implement the client's odd soft-fail mode.) |
| 1262 | hs->new_session->verify_result = X509_V_OK; |
| 1263 | } |
| 1264 |
| 1265 | // Skip this state. |
| 1266 | hs->tls13_state = state13_read_channel_id; |
| 1267 | return ssl_hs_ok; |
| 1268 | } |
| 1269 |
| 1270 | const bool allow_anonymous = |
| 1271 | (hs->config->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) == 0; |
| 1272 | SSLMessage msg; |
| 1273 | if (!ssl->method->get_message(ssl, &msg)) { |
| 1274 | return ssl_hs_read_message; |
| 1275 | } |
| 1276 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE) || |
| 1277 | !tls13_process_certificate(hs, msg, allow_anonymous) || |
| 1278 | !ssl_hash_message(hs, msg)) { |
| 1279 | return ssl_hs_error; |
| 1280 | } |
| 1281 |
| 1282 | ssl->method->next_message(ssl); |
| 1283 | hs->tls13_state = state13_read_client_certificate_verify; |
| 1284 | return ssl_hs_ok; |
| 1285 | } |
| 1286 |
| 1287 | static enum ssl_hs_wait_t do_read_client_certificate_verify(SSL_HANDSHAKE *hs) { |
| 1288 | SSL *const ssl = hs->ssl; |
| 1289 | if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) == 0) { |
| 1290 | // Skip this state. |
| 1291 | hs->tls13_state = state13_read_channel_id; |
| 1292 | return ssl_hs_ok; |
| 1293 | } |
| 1294 |
| 1295 | SSLMessage msg; |
| 1296 | if (!ssl->method->get_message(ssl, &msg)) { |
| 1297 | return ssl_hs_read_message; |
| 1298 | } |
| 1299 |
| 1300 | switch (ssl_verify_peer_cert(hs)) { |
| 1301 | case ssl_verify_ok: |
| 1302 | break; |
| 1303 | case ssl_verify_invalid: |
| 1304 | return ssl_hs_error; |
| 1305 | case ssl_verify_retry: |
| 1306 | hs->tls13_state = state13_read_client_certificate_verify; |
| 1307 | return ssl_hs_certificate_verify; |
| 1308 | } |
| 1309 |
| 1310 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY) || |
| 1311 | !tls13_process_certificate_verify(hs, msg) || |
| 1312 | !ssl_hash_message(hs, msg)) { |
| 1313 | return ssl_hs_error; |
| 1314 | } |
| 1315 |
| 1316 | ssl->method->next_message(ssl); |
| 1317 | hs->tls13_state = state13_read_channel_id; |
| 1318 | return ssl_hs_ok; |
| 1319 | } |
| 1320 |
| 1321 | static enum ssl_hs_wait_t do_read_channel_id(SSL_HANDSHAKE *hs) { |
| 1322 | SSL *const ssl = hs->ssl; |
| 1323 | if (!hs->channel_id_negotiated) { |
| 1324 | hs->tls13_state = state13_read_client_finished; |
| 1325 | return ssl_hs_ok; |
| 1326 | } |
| 1327 |
| 1328 | SSLMessage msg; |
| 1329 | if (!ssl->method->get_message(ssl, &msg)) { |
| 1330 | return ssl_hs_read_message; |
| 1331 | } |
| 1332 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) || // |
| 1333 | !tls1_verify_channel_id(hs, msg) || // |
| 1334 | !ssl_hash_message(hs, msg)) { |
| 1335 | return ssl_hs_error; |
| 1336 | } |
| 1337 |
| 1338 | ssl->method->next_message(ssl); |
| 1339 | hs->tls13_state = state13_read_client_finished; |
| 1340 | return ssl_hs_ok; |
| 1341 | } |
| 1342 |
| 1343 | static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) { |
| 1344 | SSL *const ssl = hs->ssl; |
| 1345 | SSLMessage msg; |
| 1346 | if (!ssl->method->get_message(ssl, &msg)) { |
| 1347 | return ssl_hs_read_message; |
| 1348 | } |
| 1349 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_FINISHED) || |
| 1350 | // If early data was accepted, we've already computed the client Finished |
| 1351 | // and derived the resumption secret. |
| 1352 | !tls13_process_finished(hs, msg, ssl->s3->early_data_accepted) || |
| 1353 | // evp_aead_seal keys have already been switched. |
| 1354 | !tls13_set_traffic_key(ssl, ssl_encryption_application, evp_aead_open, |
| 1355 | hs->new_session.get(), |
| 1356 | hs->client_traffic_secret_0)) { |
| 1357 | return ssl_hs_error; |
| 1358 | } |
| 1359 |
| 1360 | if (!ssl->s3->early_data_accepted) { |
| 1361 | if (!ssl_hash_message(hs, msg) || // |
| 1362 | !tls13_derive_resumption_secret(hs)) { |
| 1363 | return ssl_hs_error; |
| 1364 | } |
| 1365 |
| 1366 | // We send post-handshake tickets as part of the handshake in 1-RTT. |
| 1367 | hs->tls13_state = state13_send_new_session_ticket; |
| 1368 | } else { |
| 1369 | // We already sent half-RTT tickets. |
| 1370 | hs->tls13_state = state13_done; |
| 1371 | } |
| 1372 |
| 1373 | if (hs->credential != nullptr && |
| 1374 | hs->credential->type == SSLCredentialType::kSPAKE2PlusV1Server) { |
| 1375 | // The client has now confirmed that it does know the correct password, so |
| 1376 | // this connection no longer counts towards the brute force limit. |
| 1377 | hs->credential->RestorePAKEAttempt(); |
| 1378 | } |
| 1379 |
| 1380 | ssl->method->next_message(ssl); |
| 1381 | if (SSL_is_dtls(ssl)) { |
| 1382 | ssl->method->schedule_ack(ssl); |
| 1383 | return ssl_hs_flush; |
| 1384 | } |
| 1385 | return ssl_hs_ok; |
| 1386 | } |
| 1387 |
| 1388 | static enum ssl_hs_wait_t do_send_new_session_ticket(SSL_HANDSHAKE *hs) { |
| 1389 | SSL *const ssl = hs->ssl; |
| 1390 | bool sent_tickets; |
| 1391 | if (!add_new_session_tickets(hs, &sent_tickets)) { |
| 1392 | return ssl_hs_error; |
| 1393 | } |
| 1394 |
| 1395 | hs->tls13_state = state13_done; |
| 1396 | // In QUIC and DTLS, we can flush the ticket to the transport immediately. In |
| 1397 | // TLS over TCP-like transports, we defer until the server performs a write. |
| 1398 | // This prevents a non-reading client from causing the server to hang in the |
| 1399 | // case of a small server write buffer. Consumers which don't write data to |
| 1400 | // the client will need to do a zero-byte write if they wish to flush the |
| 1401 | // tickets. |
| 1402 | bool should_flush = sent_tickets && (SSL_is_dtls(ssl) || SSL_is_quic(ssl)); |
| 1403 | return should_flush ? ssl_hs_flush : ssl_hs_ok; |
| 1404 | } |
| 1405 |
| 1406 | enum ssl_hs_wait_t tls13_server_handshake(SSL_HANDSHAKE *hs) { |
| 1407 | while (hs->tls13_state != state13_done) { |
| 1408 | enum ssl_hs_wait_t ret = ssl_hs_error; |
| 1409 | enum tls13_server_hs_state_t state = |
| 1410 | static_cast<enum tls13_server_hs_state_t>(hs->tls13_state); |
| 1411 | switch (state) { |
| 1412 | case state13_select_parameters: |
| 1413 | ret = do_select_parameters(hs); |
| 1414 | break; |
| 1415 | case state13_select_session: |
| 1416 | ret = do_select_session(hs); |
| 1417 | break; |
| 1418 | case state13_send_hello_retry_request: |
| 1419 | ret = do_send_hello_retry_request(hs); |
| 1420 | break; |
| 1421 | case state13_read_second_client_hello: |
| 1422 | ret = do_read_second_client_hello(hs); |
| 1423 | break; |
| 1424 | case state13_send_server_hello: |
| 1425 | ret = do_send_server_hello(hs); |
| 1426 | break; |
| 1427 | case state13_send_server_certificate_verify: |
| 1428 | ret = do_send_server_certificate_verify(hs); |
| 1429 | break; |
| 1430 | case state13_send_server_finished: |
| 1431 | ret = do_send_server_finished(hs); |
| 1432 | break; |
| 1433 | case state13_send_half_rtt_ticket: |
| 1434 | ret = do_send_half_rtt_ticket(hs); |
| 1435 | break; |
| 1436 | case state13_read_second_client_flight: |
| 1437 | ret = do_read_second_client_flight(hs); |
| 1438 | break; |
| 1439 | case state13_process_end_of_early_data: |
| 1440 | ret = do_process_end_of_early_data(hs); |
| 1441 | break; |
| 1442 | case state13_read_client_encrypted_extensions: |
| 1443 | ret = do_read_client_encrypted_extensions(hs); |
| 1444 | break; |
| 1445 | case state13_read_client_certificate: |
| 1446 | ret = do_read_client_certificate(hs); |
| 1447 | break; |
| 1448 | case state13_read_client_certificate_verify: |
| 1449 | ret = do_read_client_certificate_verify(hs); |
| 1450 | break; |
| 1451 | case state13_read_channel_id: |
| 1452 | ret = do_read_channel_id(hs); |
| 1453 | break; |
| 1454 | case state13_read_client_finished: |
| 1455 | ret = do_read_client_finished(hs); |
| 1456 | break; |
| 1457 | case state13_send_new_session_ticket: |
| 1458 | ret = do_send_new_session_ticket(hs); |
| 1459 | break; |
| 1460 | case state13_done: |
| 1461 | ret = ssl_hs_ok; |
| 1462 | break; |
| 1463 | } |
| 1464 |
| 1465 | if (hs->tls13_state != state) { |
| 1466 | ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1); |
| 1467 | } |
| 1468 |
| 1469 | if (ret != ssl_hs_ok) { |
| 1470 | return ret; |
| 1471 | } |
| 1472 | } |
| 1473 |
| 1474 | return ssl_hs_ok; |
| 1475 | } |
| 1476 |
| 1477 | const char *tls13_server_handshake_state(SSL_HANDSHAKE *hs) { |
| 1478 | enum tls13_server_hs_state_t state = |
| 1479 | static_cast<enum tls13_server_hs_state_t>(hs->tls13_state); |
| 1480 | switch (state) { |
| 1481 | case state13_select_parameters: |
| 1482 | return "TLS 1.3 server select_parameters"; |
| 1483 | case state13_select_session: |
| 1484 | return "TLS 1.3 server select_session"; |
| 1485 | case state13_send_hello_retry_request: |
| 1486 | return "TLS 1.3 server send_hello_retry_request"; |
| 1487 | case state13_read_second_client_hello: |
| 1488 | return "TLS 1.3 server read_second_client_hello"; |
| 1489 | case state13_send_server_hello: |
| 1490 | return "TLS 1.3 server send_server_hello"; |
| 1491 | case state13_send_server_certificate_verify: |
| 1492 | return "TLS 1.3 server send_server_certificate_verify"; |
| 1493 | case state13_send_half_rtt_ticket: |
| 1494 | return "TLS 1.3 server send_half_rtt_ticket"; |
| 1495 | case state13_send_server_finished: |
| 1496 | return "TLS 1.3 server send_server_finished"; |
| 1497 | case state13_read_second_client_flight: |
| 1498 | return "TLS 1.3 server read_second_client_flight"; |
| 1499 | case state13_process_end_of_early_data: |
| 1500 | return "TLS 1.3 server process_end_of_early_data"; |
| 1501 | case state13_read_client_encrypted_extensions: |
| 1502 | return "TLS 1.3 server read_client_encrypted_extensions"; |
| 1503 | case state13_read_client_certificate: |
| 1504 | return "TLS 1.3 server read_client_certificate"; |
| 1505 | case state13_read_client_certificate_verify: |
| 1506 | return "TLS 1.3 server read_client_certificate_verify"; |
| 1507 | case state13_read_channel_id: |
| 1508 | return "TLS 1.3 server read_channel_id"; |
| 1509 | case state13_read_client_finished: |
| 1510 | return "TLS 1.3 server read_client_finished"; |
| 1511 | case state13_send_new_session_ticket: |
| 1512 | return "TLS 1.3 server send_new_session_ticket"; |
| 1513 | case state13_done: |
| 1514 | return "TLS 1.3 server done"; |
| 1515 | } |
| 1516 |
| 1517 | return "TLS 1.3 server unknown"; |
| 1518 | } |
| 1519 |
| 1520 | BSSL_NAMESPACE_END |
| 1521 |