Coverage Report

Created: 2025-12-07 06:13

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