Coverage Report

Created: 2023-06-07 07:13

/src/boringssl/ssl/handshake_client.cc
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2
 * All rights reserved.
3
 *
4
 * This package is an SSL implementation written
5
 * by Eric Young (eay@cryptsoft.com).
6
 * The implementation was written so as to conform with Netscapes SSL.
7
 *
8
 * This library is free for commercial and non-commercial use as long as
9
 * the following conditions are aheared to.  The following conditions
10
 * apply to all code found in this distribution, be it the RC4, RSA,
11
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12
 * included with this distribution is covered by the same copyright terms
13
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14
 *
15
 * Copyright remains Eric Young's, and as such any Copyright notices in
16
 * the code are not to be removed.
17
 * If this package is used in a product, Eric Young should be given attribution
18
 * as the author of the parts of the library used.
19
 * This can be in the form of a textual message at program startup or
20
 * in documentation (online or textual) provided with the package.
21
 *
22
 * Redistribution and use in source and binary forms, with or without
23
 * modification, are permitted provided that the following conditions
24
 * are met:
25
 * 1. Redistributions of source code must retain the copyright
26
 *    notice, this list of conditions and the following disclaimer.
27
 * 2. Redistributions in binary form must reproduce the above copyright
28
 *    notice, this list of conditions and the following disclaimer in the
29
 *    documentation and/or other materials provided with the distribution.
30
 * 3. All advertising materials mentioning features or use of this software
31
 *    must display the following acknowledgement:
32
 *    "This product includes cryptographic software written by
33
 *     Eric Young (eay@cryptsoft.com)"
34
 *    The word 'cryptographic' can be left out if the rouines from the library
35
 *    being used are not cryptographic related :-).
36
 * 4. If you include any Windows specific code (or a derivative thereof) from
37
 *    the apps directory (application code) you must include an acknowledgement:
38
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39
 *
40
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50
 * SUCH DAMAGE.
51
 *
52
 * The licence and distribution terms for any publically available version or
53
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
54
 * copied and put under another distribution licence
55
 * [including the GNU Public Licence.]
56
 */
57
/* ====================================================================
58
 * Copyright (c) 1998-2007 The OpenSSL Project.  All rights reserved.
59
 *
60
 * Redistribution and use in source and binary forms, with or without
61
 * modification, are permitted provided that the following conditions
62
 * are met:
63
 *
64
 * 1. Redistributions of source code must retain the above copyright
65
 *    notice, this list of conditions and the following disclaimer.
66
 *
67
 * 2. Redistributions in binary form must reproduce the above copyright
68
 *    notice, this list of conditions and the following disclaimer in
69
 *    the documentation and/or other materials provided with the
70
 *    distribution.
71
 *
72
 * 3. All advertising materials mentioning features or use of this
73
 *    software must display the following acknowledgment:
74
 *    "This product includes software developed by the OpenSSL Project
75
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76
 *
77
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78
 *    endorse or promote products derived from this software without
79
 *    prior written permission. For written permission, please contact
80
 *    openssl-core@openssl.org.
81
 *
82
 * 5. Products derived from this software may not be called "OpenSSL"
83
 *    nor may "OpenSSL" appear in their names without prior written
84
 *    permission of the OpenSSL Project.
85
 *
86
 * 6. Redistributions of any form whatsoever must retain the following
87
 *    acknowledgment:
88
 *    "This product includes software developed by the OpenSSL Project
89
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90
 *
91
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
95
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102
 * OF THE POSSIBILITY OF SUCH DAMAGE.
103
 * ====================================================================
104
 *
105
 * This product includes cryptographic software written by Eric Young
106
 * (eay@cryptsoft.com).  This product includes software written by Tim
107
 * Hudson (tjh@cryptsoft.com).
108
 *
109
 */
110
/* ====================================================================
111
 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112
 *
113
 * Portions of the attached software ("Contribution") are developed by
114
 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
115
 *
116
 * The Contribution is licensed pursuant to the OpenSSL open source
117
 * license provided above.
118
 *
119
 * ECC cipher suite support in OpenSSL originally written by
120
 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
121
 *
122
 */
123
/* ====================================================================
124
 * Copyright 2005 Nokia. All rights reserved.
125
 *
126
 * The portions of the attached software ("Contribution") is developed by
127
 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
128
 * license.
129
 *
130
 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
131
 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
132
 * support (see RFC 4279) to OpenSSL.
133
 *
134
 * No patent licenses or other rights except those expressly stated in
135
 * the OpenSSL open source license shall be deemed granted or received
136
 * expressly, by implication, estoppel, or otherwise.
137
 *
138
 * No assurances are provided by Nokia that the Contribution does not
139
 * infringe the patent or other intellectual property rights of any third
140
 * party or that the license provides you with all the necessary rights
141
 * to make use of the Contribution.
142
 *
143
 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
144
 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
145
 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
146
 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
147
 * OTHERWISE.
148
 */
149
150
#include <openssl/ssl.h>
151
152
#include <assert.h>
153
#include <limits.h>
154
#include <string.h>
155
156
#include <utility>
157
158
#include <openssl/aead.h>
159
#include <openssl/bn.h>
160
#include <openssl/bytestring.h>
161
#include <openssl/ec_key.h>
162
#include <openssl/ecdsa.h>
163
#include <openssl/err.h>
164
#include <openssl/evp.h>
165
#include <openssl/hpke.h>
166
#include <openssl/md5.h>
167
#include <openssl/mem.h>
168
#include <openssl/rand.h>
169
#include <openssl/sha.h>
170
171
#include "../crypto/internal.h"
172
#include "internal.h"
173
174
175
BSSL_NAMESPACE_BEGIN
176
177
enum ssl_client_hs_state_t {
178
  state_start_connect = 0,
179
  state_enter_early_data,
180
  state_early_reverify_server_certificate,
181
  state_read_hello_verify_request,
182
  state_read_server_hello,
183
  state_tls13,
184
  state_read_server_certificate,
185
  state_read_certificate_status,
186
  state_verify_server_certificate,
187
  state_reverify_server_certificate,
188
  state_read_server_key_exchange,
189
  state_read_certificate_request,
190
  state_read_server_hello_done,
191
  state_send_client_certificate,
192
  state_send_client_key_exchange,
193
  state_send_client_certificate_verify,
194
  state_send_client_finished,
195
  state_finish_flight,
196
  state_read_session_ticket,
197
  state_process_change_cipher_spec,
198
  state_read_server_finished,
199
  state_finish_client_handshake,
200
  state_done,
201
};
202
203
// ssl_get_client_disabled sets |*out_mask_a| and |*out_mask_k| to masks of
204
// disabled algorithms.
205
static void ssl_get_client_disabled(const SSL_HANDSHAKE *hs,
206
                                    uint32_t *out_mask_a,
207
123k
                                    uint32_t *out_mask_k) {
208
123k
  *out_mask_a = 0;
209
123k
  *out_mask_k = 0;
210
211
  // PSK requires a client callback.
212
123k
  if (hs->config->psk_client_callback == NULL) {
213
123k
    *out_mask_a |= SSL_aPSK;
214
123k
    *out_mask_k |= SSL_kPSK;
215
123k
  }
216
123k
}
217
218
static bool ssl_add_tls13_cipher(CBB *cbb, uint16_t cipher_id,
219
187k
                                 ssl_compliance_policy_t policy) {
220
187k
  if (ssl_tls13_cipher_meets_policy(cipher_id, policy)) {
221
187k
    return CBB_add_u16(cbb, cipher_id);
222
187k
  }
223
0
  return true;
224
187k
}
225
226
static bool ssl_write_client_cipher_list(const SSL_HANDSHAKE *hs, CBB *out,
227
62.3k
                                         ssl_client_hello_type_t type) {
228
62.3k
  const SSL *const ssl = hs->ssl;
229
62.3k
  uint32_t mask_a, mask_k;
230
62.3k
  ssl_get_client_disabled(hs, &mask_a, &mask_k);
231
232
62.3k
  CBB child;
233
62.3k
  if (!CBB_add_u16_length_prefixed(out, &child)) {
234
0
    return false;
235
0
  }
236
237
  // Add a fake cipher suite. See RFC 8701.
238
62.3k
  if (ssl->ctx->grease_enabled &&
239
62.3k
      !CBB_add_u16(&child, ssl_get_grease_value(hs, ssl_grease_cipher))) {
240
0
    return false;
241
0
  }
242
243
  // Add TLS 1.3 ciphers. Order ChaCha20-Poly1305 relative to AES-GCM based on
244
  // hardware support.
245
62.3k
  if (hs->max_version >= TLS1_3_VERSION) {
246
62.3k
    const bool has_aes_hw = ssl->config->aes_hw_override
247
62.3k
                                ? ssl->config->aes_hw_override_value
248
62.3k
                                : EVP_has_aes_hardware();
249
250
62.3k
    if ((!has_aes_hw &&  //
251
62.3k
         !ssl_add_tls13_cipher(&child,
252
0
                               TLS1_3_CK_CHACHA20_POLY1305_SHA256 & 0xffff,
253
0
                               ssl->config->tls13_cipher_policy)) ||
254
62.3k
        !ssl_add_tls13_cipher(&child, TLS1_3_CK_AES_128_GCM_SHA256 & 0xffff,
255
62.3k
                              ssl->config->tls13_cipher_policy) ||
256
62.3k
        !ssl_add_tls13_cipher(&child, TLS1_3_CK_AES_256_GCM_SHA384 & 0xffff,
257
62.3k
                              ssl->config->tls13_cipher_policy) ||
258
62.3k
        (has_aes_hw &&  //
259
62.3k
         !ssl_add_tls13_cipher(&child,
260
62.3k
                               TLS1_3_CK_CHACHA20_POLY1305_SHA256 & 0xffff,
261
62.3k
                               ssl->config->tls13_cipher_policy))) {
262
0
      return false;
263
0
    }
264
62.3k
  }
265
266
62.3k
  if (hs->min_version < TLS1_3_VERSION && type != ssl_client_hello_inner) {
267
62.3k
    bool any_enabled = false;
268
1.24M
    for (const SSL_CIPHER *cipher : SSL_get_ciphers(ssl)) {
269
      // Skip disabled ciphers
270
1.24M
      if ((cipher->algorithm_mkey & mask_k) ||
271
1.24M
          (cipher->algorithm_auth & mask_a)) {
272
311k
        continue;
273
311k
      }
274
935k
      if (SSL_CIPHER_get_min_version(cipher) > hs->max_version ||
275
935k
          SSL_CIPHER_get_max_version(cipher) < hs->min_version) {
276
0
        continue;
277
0
      }
278
935k
      any_enabled = true;
279
935k
      if (!CBB_add_u16(&child, SSL_CIPHER_get_protocol_id(cipher))) {
280
0
        return false;
281
0
      }
282
935k
    }
283
284
    // If all ciphers were disabled, return the error to the caller.
285
62.3k
    if (!any_enabled && hs->max_version < TLS1_3_VERSION) {
286
0
      OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
287
0
      return false;
288
0
    }
289
62.3k
  }
290
291
62.3k
  if (ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
292
0
    if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
293
0
      return false;
294
0
    }
295
0
  }
296
297
62.3k
  return CBB_flush(out);
298
62.3k
}
299
300
bool ssl_write_client_hello_without_extensions(const SSL_HANDSHAKE *hs,
301
                                               CBB *cbb,
302
                                               ssl_client_hello_type_t type,
303
62.3k
                                               bool empty_session_id) {
304
62.3k
  const SSL *const ssl = hs->ssl;
305
62.3k
  CBB child;
306
62.3k
  if (!CBB_add_u16(cbb, hs->client_version) ||
307
62.3k
      !CBB_add_bytes(cbb,
308
62.3k
                     type == ssl_client_hello_inner ? hs->inner_client_random
309
62.3k
                                                    : ssl->s3->client_random,
310
62.3k
                     SSL3_RANDOM_SIZE) ||
311
62.3k
      !CBB_add_u8_length_prefixed(cbb, &child)) {
312
0
    return false;
313
0
  }
314
315
  // Do not send a session ID on renegotiation.
316
62.3k
  if (!ssl->s3->initial_handshake_complete &&
317
62.3k
      !empty_session_id &&
318
62.3k
      !CBB_add_bytes(&child, hs->session_id, hs->session_id_len)) {
319
0
    return false;
320
0
  }
321
322
62.3k
  if (SSL_is_dtls(ssl)) {
323
0
    if (!CBB_add_u8_length_prefixed(cbb, &child) ||
324
0
        !CBB_add_bytes(&child, hs->dtls_cookie.data(),
325
0
                       hs->dtls_cookie.size())) {
326
0
      return false;
327
0
    }
328
0
  }
329
330
62.3k
  if (!ssl_write_client_cipher_list(hs, cbb, type) ||
331
62.3k
      !CBB_add_u8(cbb, 1 /* one compression method */) ||
332
62.3k
      !CBB_add_u8(cbb, 0 /* null compression */)) {
333
0
    return false;
334
0
  }
335
62.3k
  return true;
336
62.3k
}
337
338
62.3k
bool ssl_add_client_hello(SSL_HANDSHAKE *hs) {
339
62.3k
  SSL *const ssl = hs->ssl;
340
62.3k
  ScopedCBB cbb;
341
62.3k
  CBB body;
342
62.3k
  ssl_client_hello_type_t type = hs->selected_ech_config
343
62.3k
                                     ? ssl_client_hello_outer
344
62.3k
                                     : ssl_client_hello_unencrypted;
345
62.3k
  bool needs_psk_binder;
346
62.3k
  Array<uint8_t> msg;
347
62.3k
  if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CLIENT_HELLO) ||
348
62.3k
      !ssl_write_client_hello_without_extensions(hs, &body, type,
349
62.3k
                                                 /*empty_session_id=*/false) ||
350
62.3k
      !ssl_add_clienthello_tlsext(hs, &body, /*out_encoded=*/nullptr,
351
62.3k
                                  &needs_psk_binder, type, CBB_len(&body)) ||
352
62.3k
      !ssl->method->finish_message(ssl, cbb.get(), &msg)) {
353
0
    return false;
354
0
  }
355
356
  // Now that the length prefixes have been computed, fill in the placeholder
357
  // PSK binder.
358
62.3k
  if (needs_psk_binder) {
359
    // ClientHelloOuter cannot have a PSK binder. Otherwise the
360
    // ClientHellOuterAAD computation would break.
361
790
    assert(type != ssl_client_hello_outer);
362
790
    if (!tls13_write_psk_binder(hs, hs->transcript, MakeSpan(msg),
363
790
                                /*out_binder_len=*/0)) {
364
0
      return false;
365
0
    }
366
790
  }
367
368
62.3k
  return ssl->method->add_message(ssl, std::move(msg));
369
62.3k
}
370
371
static bool parse_server_version(const SSL_HANDSHAKE *hs, uint16_t *out_version,
372
                                 uint8_t *out_alert,
373
61.3k
                                 const ParsedServerHello &server_hello) {
374
  // If the outer version is not TLS 1.2, use it.
375
  // TODO(davidben): This function doesn't quite match the RFC8446 formulation.
376
61.3k
  if (server_hello.legacy_version != TLS1_2_VERSION) {
377
7.99k
    *out_version = server_hello.legacy_version;
378
7.99k
    return true;
379
7.99k
  }
380
381
53.3k
  SSLExtension supported_versions(TLSEXT_TYPE_supported_versions);
382
53.3k
  CBS extensions = server_hello.extensions;
383
53.3k
  if (!ssl_parse_extensions(&extensions, out_alert, {&supported_versions},
384
53.3k
                            /*ignore_unknown=*/true)) {
385
23
    return false;
386
23
  }
387
388
53.3k
  if (!supported_versions.present) {
389
52.9k
    *out_version = server_hello.legacy_version;
390
52.9k
    return true;
391
52.9k
  }
392
393
445
  if (!CBS_get_u16(&supported_versions.data, out_version) ||
394
445
       CBS_len(&supported_versions.data) != 0) {
395
2
    *out_alert = SSL_AD_DECODE_ERROR;
396
2
    return false;
397
2
  }
398
399
443
  return true;
400
445
}
401
402
// should_offer_early_data returns |ssl_early_data_accepted| if |hs| should
403
// offer early data, and some other reason code otherwise.
404
static ssl_early_data_reason_t should_offer_early_data(
405
62.0k
    const SSL_HANDSHAKE *hs) {
406
62.0k
  const SSL *const ssl = hs->ssl;
407
62.0k
  assert(!ssl->server);
408
62.0k
  if (!ssl->enable_early_data) {
409
0
    return ssl_early_data_disabled;
410
0
  }
411
412
62.0k
  if (hs->max_version < TLS1_3_VERSION) {
413
    // We discard inapplicable sessions, so this is redundant with the session
414
    // checks below, but reporting that TLS 1.3 was disabled is more useful.
415
0
    return ssl_early_data_protocol_version;
416
0
  }
417
418
62.0k
  if (ssl->session == nullptr) {
419
61.2k
    return ssl_early_data_no_session_offered;
420
61.2k
  }
421
422
807
  if (ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION ||
423
807
      ssl->session->ticket_max_early_data == 0) {
424
708
    return ssl_early_data_unsupported_for_session;
425
708
  }
426
427
99
  if (!ssl->session->early_alpn.empty()) {
428
1
    if (!ssl_is_alpn_protocol_allowed(hs, ssl->session->early_alpn)) {
429
      // Avoid reporting a confusing value in |SSL_get0_alpn_selected|.
430
1
      return ssl_early_data_alpn_mismatch;
431
1
    }
432
433
    // If the previous connection negotiated ALPS, only offer 0-RTT when the
434
    // local are settings are consistent with what we'd offer for this
435
    // connection.
436
0
    if (ssl->session->has_application_settings) {
437
0
      Span<const uint8_t> settings;
438
0
      if (!ssl_get_local_application_settings(hs, &settings,
439
0
                                              ssl->session->early_alpn) ||
440
0
          settings != ssl->session->local_application_settings) {
441
0
        return ssl_early_data_alps_mismatch;
442
0
      }
443
0
    }
444
0
  }
445
446
  // Early data has not yet been accepted, but we use it as a success code.
447
98
  return ssl_early_data_accepted;
448
99
}
449
450
61.1k
void ssl_done_writing_client_hello(SSL_HANDSHAKE *hs) {
451
61.1k
  hs->ech_client_outer.Reset();
452
61.1k
  hs->cookie.Reset();
453
61.1k
  hs->key_share_bytes.Reset();
454
61.1k
}
455
456
62.0k
static enum ssl_hs_wait_t do_start_connect(SSL_HANDSHAKE *hs) {
457
62.0k
  SSL *const ssl = hs->ssl;
458
459
62.0k
  ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
460
  // |session_reused| must be reset in case this is a renegotiation.
461
62.0k
  ssl->s3->session_reused = false;
462
463
  // Freeze the version range.
464
62.0k
  if (!ssl_get_version_range(hs, &hs->min_version, &hs->max_version)) {
465
0
    return ssl_hs_error;
466
0
  }
467
468
62.0k
  uint8_t ech_enc[EVP_HPKE_MAX_ENC_LENGTH];
469
62.0k
  size_t ech_enc_len;
470
62.0k
  if (!ssl_select_ech_config(hs, ech_enc, &ech_enc_len)) {
471
0
    return ssl_hs_error;
472
0
  }
473
474
  // Always advertise the ClientHello version from the original maximum version,
475
  // even on renegotiation. The static RSA key exchange uses this field, and
476
  // some servers fail when it changes across handshakes.
477
62.0k
  if (SSL_is_dtls(hs->ssl)) {
478
0
    hs->client_version =
479
0
        hs->max_version >= TLS1_2_VERSION ? DTLS1_2_VERSION : DTLS1_VERSION;
480
62.0k
  } else {
481
62.0k
    hs->client_version =
482
62.0k
        hs->max_version >= TLS1_2_VERSION ? TLS1_2_VERSION : hs->max_version;
483
62.0k
  }
484
485
  // If the configured session has expired or is not usable, drop it. We also do
486
  // not offer sessions on renegotiation.
487
62.0k
  if (ssl->session != nullptr) {
488
922
    if (ssl->session->is_server ||
489
922
        !ssl_supports_version(hs, ssl->session->ssl_version) ||
490
        // Do not offer TLS 1.2 sessions with ECH. ClientHelloInner does not
491
        // offer TLS 1.2, and the cleartext session ID may leak the server
492
        // identity.
493
922
        (hs->selected_ech_config &&
494
845
         ssl_session_protocol_version(ssl->session.get()) < TLS1_3_VERSION) ||
495
922
        !SSL_SESSION_is_resumable(ssl->session.get()) ||
496
922
        !ssl_session_is_time_valid(ssl, ssl->session.get()) ||
497
922
        (ssl->quic_method != nullptr) != ssl->session->is_quic ||
498
922
        ssl->s3->initial_handshake_complete) {
499
115
      ssl_set_session(ssl, nullptr);
500
115
    }
501
922
  }
502
503
62.0k
  if (!RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
504
0
    return ssl_hs_error;
505
0
  }
506
62.0k
  if (hs->selected_ech_config &&
507
62.0k
      !RAND_bytes(hs->inner_client_random, sizeof(hs->inner_client_random))) {
508
0
    return ssl_hs_error;
509
0
  }
510
511
  // Never send a session ID in QUIC. QUIC uses TLS 1.3 at a minimum and
512
  // disables TLS 1.3 middlebox compatibility mode.
513
62.0k
  if (ssl->quic_method == nullptr) {
514
62.0k
    const bool has_id_session = ssl->session != nullptr &&
515
62.0k
                                ssl->session->session_id_length > 0 &&
516
62.0k
                                ssl->session->ticket.empty();
517
62.0k
    const bool has_ticket_session =
518
62.0k
        ssl->session != nullptr && !ssl->session->ticket.empty();
519
62.0k
    if (has_id_session) {
520
800
      hs->session_id_len = ssl->session->session_id_length;
521
800
      OPENSSL_memcpy(hs->session_id, ssl->session->session_id,
522
800
                     hs->session_id_len);
523
61.2k
    } else if (has_ticket_session || hs->max_version >= TLS1_3_VERSION) {
524
      // Send a random session ID. TLS 1.3 always sends one, and TLS 1.2 session
525
      // tickets require a placeholder value to signal resumption.
526
61.2k
      hs->session_id_len = sizeof(hs->session_id);
527
61.2k
      if (!RAND_bytes(hs->session_id, hs->session_id_len)) {
528
0
        return ssl_hs_error;
529
0
      }
530
61.2k
    }
531
62.0k
  }
532
533
62.0k
  ssl_early_data_reason_t reason = should_offer_early_data(hs);
534
62.0k
  if (reason != ssl_early_data_accepted) {
535
61.9k
    ssl->s3->early_data_reason = reason;
536
61.9k
  } else {
537
98
    hs->early_data_offered = true;
538
98
  }
539
540
62.0k
  if (!ssl_setup_key_shares(hs, /*override_group_id=*/0) ||
541
62.0k
      !ssl_setup_extension_permutation(hs) ||
542
62.0k
      !ssl_encrypt_client_hello(hs, MakeConstSpan(ech_enc, ech_enc_len)) ||
543
62.0k
      !ssl_add_client_hello(hs)) {
544
0
    return ssl_hs_error;
545
0
  }
546
547
62.0k
  hs->state = state_enter_early_data;
548
62.0k
  return ssl_hs_flush;
549
62.0k
}
550
551
62.0k
static enum ssl_hs_wait_t do_enter_early_data(SSL_HANDSHAKE *hs) {
552
62.0k
  SSL *const ssl = hs->ssl;
553
554
62.0k
  if (SSL_is_dtls(ssl)) {
555
0
    hs->state = state_read_hello_verify_request;
556
0
    return ssl_hs_ok;
557
0
  }
558
559
62.0k
  if (!hs->early_data_offered) {
560
61.9k
    hs->state = state_read_server_hello;
561
61.9k
    return ssl_hs_ok;
562
61.9k
  }
563
564
98
  ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->session->ssl_version);
565
98
  if (!ssl->method->add_change_cipher_spec(ssl)) {
566
0
    return ssl_hs_error;
567
0
  }
568
569
98
  if (!tls13_init_early_key_schedule(hs, ssl->session.get()) ||
570
98
      !tls13_derive_early_secret(hs)) {
571
0
    return ssl_hs_error;
572
0
  }
573
574
  // Stash the early data session, so connection properties may be queried out
575
  // of it.
576
98
  hs->early_session = UpRef(ssl->session);
577
98
  hs->state = state_early_reverify_server_certificate;
578
98
  return ssl_hs_ok;
579
98
}
580
581
98
static enum ssl_hs_wait_t do_early_reverify_server_certificate(SSL_HANDSHAKE *hs) {
582
98
  if (hs->ssl->ctx->reverify_on_resume) {
583
    // Don't send an alert on error. The alert be in early data, which the
584
    // server may not accept anyway. It would also be a mismatch between QUIC
585
    // and TCP because the QUIC early keys are deferred below.
586
    //
587
    // TODO(davidben): The client behavior should be to verify the certificate
588
    // before deciding whether to offer the session and, if invalid, decline to
589
    // send the session.
590
0
    switch (ssl_reverify_peer_cert(hs, /*send_alert=*/false)) {
591
0
      case ssl_verify_ok:
592
0
        break;
593
0
      case ssl_verify_invalid:
594
0
        return ssl_hs_error;
595
0
      case ssl_verify_retry:
596
0
        hs->state = state_early_reverify_server_certificate;
597
0
        return ssl_hs_certificate_verify;
598
0
    }
599
0
  }
600
601
  // Defer releasing the 0-RTT key to after certificate reverification, so the
602
  // QUIC implementation does not accidentally write data too early.
603
98
  if (!tls13_set_traffic_key(hs->ssl, ssl_encryption_early_data, evp_aead_seal,
604
98
                             hs->early_session.get(),
605
98
                             hs->early_traffic_secret())) {
606
8
    return ssl_hs_error;
607
8
  }
608
609
90
  hs->in_early_data = true;
610
90
  hs->can_early_write = true;
611
90
  hs->state = state_read_server_hello;
612
90
  return ssl_hs_early_return;
613
98
}
614
615
0
static enum ssl_hs_wait_t do_read_hello_verify_request(SSL_HANDSHAKE *hs) {
616
0
  SSL *const ssl = hs->ssl;
617
618
0
  assert(SSL_is_dtls(ssl));
619
620
  // When implementing DTLS 1.3, we need to handle the interactions between
621
  // HelloVerifyRequest, DTLS 1.3's HelloVerifyRequest removal, and ECH.
622
0
  assert(hs->max_version < TLS1_3_VERSION);
623
624
0
  SSLMessage msg;
625
0
  if (!ssl->method->get_message(ssl, &msg)) {
626
0
    return ssl_hs_read_message;
627
0
  }
628
629
0
  if (msg.type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
630
0
    hs->state = state_read_server_hello;
631
0
    return ssl_hs_ok;
632
0
  }
633
634
0
  CBS hello_verify_request = msg.body, cookie;
635
0
  uint16_t server_version;
636
0
  if (!CBS_get_u16(&hello_verify_request, &server_version) ||
637
0
      !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
638
0
      CBS_len(&hello_verify_request) != 0) {
639
0
    OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
640
0
    ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
641
0
    return ssl_hs_error;
642
0
  }
643
644
0
  if (!hs->dtls_cookie.CopyFrom(cookie)) {
645
0
    ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
646
0
    return ssl_hs_error;
647
0
  }
648
649
0
  ssl->method->next_message(ssl);
650
651
  // DTLS resets the handshake buffer after HelloVerifyRequest.
652
0
  if (!hs->transcript.Init()) {
653
0
    return ssl_hs_error;
654
0
  }
655
656
0
  if (!ssl_add_client_hello(hs)) {
657
0
    return ssl_hs_error;
658
0
  }
659
660
0
  hs->state = state_read_server_hello;
661
0
  return ssl_hs_flush;
662
0
}
663
664
bool ssl_parse_server_hello(ParsedServerHello *out, uint8_t *out_alert,
665
62.3k
                            const SSLMessage &msg) {
666
62.3k
  if (msg.type != SSL3_MT_SERVER_HELLO) {
667
23
    OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
668
23
    *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
669
23
    return false;
670
23
  }
671
62.2k
  out->raw = msg.raw;
672
62.2k
  CBS body = msg.body;
673
62.2k
  if (!CBS_get_u16(&body, &out->legacy_version) ||
674
62.2k
      !CBS_get_bytes(&body, &out->random, SSL3_RANDOM_SIZE) ||
675
62.2k
      !CBS_get_u8_length_prefixed(&body, &out->session_id) ||
676
62.2k
      CBS_len(&out->session_id) > SSL3_SESSION_ID_SIZE ||
677
62.2k
      !CBS_get_u16(&body, &out->cipher_suite) ||
678
62.2k
      !CBS_get_u8(&body, &out->compression_method)) {
679
35
    OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
680
35
    *out_alert = SSL_AD_DECODE_ERROR;
681
35
    return false;
682
35
  }
683
  // In TLS 1.2 and below, empty extensions blocks may be omitted. In TLS 1.3,
684
  // ServerHellos always have extensions, so this can be applied generically.
685
62.2k
  CBS_init(&out->extensions, nullptr, 0);
686
62.2k
  if ((CBS_len(&body) != 0 &&
687
62.2k
       !CBS_get_u16_length_prefixed(&body, &out->extensions)) ||
688
62.2k
      CBS_len(&body) != 0) {
689
45
    OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
690
45
    *out_alert = SSL_AD_DECODE_ERROR;
691
45
    return false;
692
45
  }
693
62.1k
  return true;
694
62.2k
}
695
696
133k
static enum ssl_hs_wait_t do_read_server_hello(SSL_HANDSHAKE *hs) {
697
133k
  SSL *const ssl = hs->ssl;
698
133k
  SSLMessage msg;
699
133k
  if (!ssl->method->get_message(ssl, &msg)) {
700
72.3k
    return ssl_hs_read_server_hello;
701
72.3k
  }
702
703
61.4k
  ParsedServerHello server_hello;
704
61.4k
  uint16_t server_version;
705
61.4k
  uint8_t alert = SSL_AD_DECODE_ERROR;
706
61.4k
  if (!ssl_parse_server_hello(&server_hello, &alert, msg) ||
707
61.4k
      !parse_server_version(hs, &server_version, &alert, server_hello)) {
708
124
    ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
709
124
    return ssl_hs_error;
710
124
  }
711
712
61.3k
  if (!ssl_supports_version(hs, server_version)) {
713
21
    OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
714
21
    ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
715
21
    return ssl_hs_error;
716
21
  }
717
718
61.3k
  assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
719
61.3k
  if (!ssl->s3->have_version) {
720
4.85k
    ssl->version = server_version;
721
    // At this point, the connection's version is known and ssl->version is
722
    // fixed. Begin enforcing the record-layer version.
723
4.85k
    ssl->s3->have_version = true;
724
4.85k
    ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version);
725
56.4k
  } else if (server_version != ssl->version) {
726
3
    OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
727
3
    ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
728
3
    return ssl_hs_error;
729
3
  }
730
731
61.3k
  if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
732
446
    hs->state = state_tls13;
733
446
    return ssl_hs_ok;
734
446
  }
735
736
  // Clear some TLS 1.3 state that no longer needs to be retained.
737
60.8k
  hs->key_shares[0].reset();
738
60.8k
  hs->key_shares[1].reset();
739
60.8k
  ssl_done_writing_client_hello(hs);
740
741
  // A TLS 1.2 server would not know to skip the early data we offered. Report
742
  // an error code sooner. The caller may use this error code to implement the
743
  // fallback described in RFC 8446 appendix D.3.
744
60.8k
  if (hs->early_data_offered) {
745
    // Disconnect early writes. This ensures subsequent |SSL_write| calls query
746
    // the handshake which, in turn, will replay the error code rather than fail
747
    // at the |write_shutdown| check. See https://crbug.com/1078515.
748
    // TODO(davidben): Should all handshake errors do this? What about record
749
    // decryption failures?
750
1
    hs->can_early_write = false;
751
1
    OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_ON_EARLY_DATA);
752
1
    ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
753
1
    return ssl_hs_error;
754
1
  }
755
756
  // TLS 1.2 handshakes cannot accept ECH.
757
60.8k
  if (hs->selected_ech_config) {
758
0
    ssl->s3->ech_status = ssl_ech_rejected;
759
0
  }
760
761
  // Copy over the server random.
762
60.8k
  OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_hello.random),
763
60.8k
                 SSL3_RANDOM_SIZE);
764
765
  // Enforce the TLS 1.3 anti-downgrade feature.
766
60.8k
  if (!ssl->s3->initial_handshake_complete &&
767
60.8k
      ssl_supports_version(hs, TLS1_3_VERSION)) {
768
4.40k
    static_assert(
769
4.40k
        sizeof(kTLS12DowngradeRandom) == sizeof(kTLS13DowngradeRandom),
770
4.40k
        "downgrade signals have different size");
771
4.40k
    static_assert(
772
4.40k
        sizeof(kJDK11DowngradeRandom) == sizeof(kTLS13DowngradeRandom),
773
4.40k
        "downgrade signals have different size");
774
4.40k
    auto suffix =
775
4.40k
        MakeConstSpan(ssl->s3->server_random, sizeof(ssl->s3->server_random))
776
4.40k
            .subspan(SSL3_RANDOM_SIZE - sizeof(kTLS13DowngradeRandom));
777
4.40k
    if (suffix == kTLS12DowngradeRandom || suffix == kTLS13DowngradeRandom ||
778
4.40k
        suffix == kJDK11DowngradeRandom) {
779
3
      OPENSSL_PUT_ERROR(SSL, SSL_R_TLS13_DOWNGRADE);
780
3
      ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
781
3
      return ssl_hs_error;
782
3
    }
783
4.40k
  }
784
785
  // The cipher must be allowed in the selected version and enabled.
786
60.8k
  const SSL_CIPHER *cipher = SSL_get_cipher_by_value(server_hello.cipher_suite);
787
60.8k
  uint32_t mask_a, mask_k;
788
60.8k
  ssl_get_client_disabled(hs, &mask_a, &mask_k);
789
60.8k
  if (cipher == nullptr ||
790
60.8k
      (cipher->algorithm_mkey & mask_k) ||
791
60.8k
      (cipher->algorithm_auth & mask_a) ||
792
60.8k
      SSL_CIPHER_get_min_version(cipher) > ssl_protocol_version(ssl) ||
793
60.8k
      SSL_CIPHER_get_max_version(cipher) < ssl_protocol_version(ssl) ||
794
60.8k
      !sk_SSL_CIPHER_find(SSL_get_ciphers(ssl), nullptr, cipher)) {
795
21
    OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
796
21
    ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
797
21
    return ssl_hs_error;
798
21
  }
799
800
60.8k
  hs->new_cipher = cipher;
801
802
60.8k
  if (hs->session_id_len != 0 &&
803
60.8k
      CBS_mem_equal(&server_hello.session_id, hs->session_id,
804
60.8k
                    hs->session_id_len)) {
805
    // Echoing the ClientHello session ID in TLS 1.2, whether from the session
806
    // or a synthetic one, indicates resumption. If there was no session (or if
807
    // the session was only offered in ECH ClientHelloInner), this was the
808
    // TLS 1.3 compatibility mode session ID. As we know this is not a session
809
    // the server knows about, any server resuming it is in error. Reject the
810
    // first connection deterministicly, rather than installing an invalid
811
    // session into the session cache. https://crbug.com/796910
812
208
    if (ssl->session == nullptr || ssl->s3->ech_status == ssl_ech_rejected) {
813
0
      OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_ECHOED_INVALID_SESSION_ID);
814
0
      ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
815
0
      return ssl_hs_error;
816
0
    }
817
208
    if (ssl->session->ssl_version != ssl->version) {
818
4
      OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
819
4
      ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
820
4
      return ssl_hs_error;
821
4
    }
822
204
    if (ssl->session->cipher != hs->new_cipher) {
823
1
      OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
824
1
      ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
825
1
      return ssl_hs_error;
826
1
    }
827
203
    if (!ssl_session_is_context_valid(hs, ssl->session.get())) {
828
      // This is actually a client application bug.
829
0
      OPENSSL_PUT_ERROR(SSL,
830
0
                        SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
831
0
      ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
832
0
      return ssl_hs_error;
833
0
    }
834
    // We never offer sessions on renegotiation.
835
203
    assert(!ssl->s3->initial_handshake_complete);
836
0
    ssl->s3->session_reused = true;
837
60.6k
  } else {
838
    // The session wasn't resumed. Create a fresh SSL_SESSION to fill out.
839
60.6k
    ssl_set_session(ssl, NULL);
840
60.6k
    if (!ssl_get_new_session(hs)) {
841
0
      ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
842
0
      return ssl_hs_error;
843
0
    }
844
845
    // Save the session ID from the server. This may be empty if the session
846
    // isn't resumable, or if we'll receive a session ticket later.
847
60.6k
    assert(CBS_len(&server_hello.session_id) <= SSL3_SESSION_ID_SIZE);
848
0
    static_assert(SSL3_SESSION_ID_SIZE <= UINT8_MAX,
849
60.6k
                  "max session ID is too large");
850
60.6k
    hs->new_session->session_id_length =
851
60.6k
        static_cast<uint8_t>(CBS_len(&server_hello.session_id));
852
60.6k
    OPENSSL_memcpy(hs->new_session->session_id,
853
60.6k
                   CBS_data(&server_hello.session_id),
854
60.6k
                   CBS_len(&server_hello.session_id));
855
856
60.6k
    hs->new_session->cipher = hs->new_cipher;
857
60.6k
  }
858
859
  // Now that the cipher is known, initialize the handshake hash and hash the
860
  // ServerHello.
861
60.8k
  if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) ||
862
60.8k
      !ssl_hash_message(hs, msg)) {
863
0
    ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
864
0
    return ssl_hs_error;
865
0
  }
866
867
  // If doing a full handshake, the server may request a client certificate
868
  // which requires hashing the handshake transcript. Otherwise, the handshake
869
  // buffer may be released.
870
60.8k
  if (ssl->session != NULL ||
871
60.8k
      !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
872
203
    hs->transcript.FreeBuffer();
873
203
  }
874
875
  // Only the NULL compression algorithm is supported.
876
60.8k
  if (server_hello.compression_method != 0) {
877
8
    OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
878
8
    ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
879
8
    return ssl_hs_error;
880
8
  }
881
882
60.8k
  if (!ssl_parse_serverhello_tlsext(hs, &server_hello.extensions)) {
883
143
    OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
884
143
    return ssl_hs_error;
885
143
  }
886
887
60.6k
  if (ssl->session != NULL &&
888
60.6k
      hs->extended_master_secret != ssl->session->extended_master_secret) {
889
0
    if (ssl->session->extended_master_secret) {
890
0
      OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
891
0
    } else {
892
0
      OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
893
0
    }
894
0
    ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
895
0
    return ssl_hs_error;
896
0
  }
897
898
60.6k
  ssl->method->next_message(ssl);
899
900
60.6k
  if (ssl->session != NULL) {
901
203
    if (ssl->ctx->reverify_on_resume &&
902
203
        ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
903
0
      hs->state = state_reverify_server_certificate;
904
203
    } else {
905
203
      hs->state = state_read_session_ticket;
906
203
    }
907
203
    return ssl_hs_ok;
908
203
  }
909
910
60.4k
  hs->state = state_read_server_certificate;
911
60.4k
  return ssl_hs_ok;
912
60.6k
}
913
914
3.38k
static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) {
915
3.38k
  enum ssl_hs_wait_t wait = tls13_client_handshake(hs);
916
3.38k
  if (wait == ssl_hs_ok) {
917
185
    hs->state = state_finish_client_handshake;
918
185
    return ssl_hs_ok;
919
185
  }
920
921
3.19k
  return wait;
922
3.38k
}
923
924
133k
static enum ssl_hs_wait_t do_read_server_certificate(SSL_HANDSHAKE *hs) {
925
133k
  SSL *const ssl = hs->ssl;
926
927
133k
  if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
928
0
    hs->state = state_read_certificate_status;
929
0
    return ssl_hs_ok;
930
0
  }
931
932
133k
  SSLMessage msg;
933
133k
  if (!ssl->method->get_message(ssl, &msg)) {
934
73.6k
    return ssl_hs_read_message;
935
73.6k
  }
936
937
60.2k
  if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE) ||
938
60.2k
      !ssl_hash_message(hs, msg)) {
939
6
    return ssl_hs_error;
940
6
  }
941
942
60.2k
  CBS body = msg.body;
943
60.2k
  uint8_t alert = SSL_AD_DECODE_ERROR;
944
60.2k
  if (!ssl_parse_cert_chain(&alert, &hs->new_session->certs, &hs->peer_pubkey,
945
60.2k
                            NULL, &body, ssl->ctx->pool)) {
946
425
    ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
947
425
    return ssl_hs_error;
948
425
  }
949
950
59.8k
  if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) == 0 ||
951
59.8k
      CBS_len(&body) != 0 ||
952
59.8k
      !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
953
609
    OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
954
609
    ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
955
609
    return ssl_hs_error;
956
609
  }
957
958
59.2k
  if (!ssl_check_leaf_certificate(
959
59.2k
          hs, hs->peer_pubkey.get(),
960
59.2k
          sk_CRYPTO_BUFFER_value(hs->new_session->certs.get(), 0))) {
961
3
    ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
962
3
    return ssl_hs_error;
963
3
  }
964
965
59.2k
  ssl->method->next_message(ssl);
966
967
59.2k
  hs->state = state_read_certificate_status;
968
59.2k
  return ssl_hs_ok;
969
59.2k
}
970
971
73.0k
static enum ssl_hs_wait_t do_read_certificate_status(SSL_HANDSHAKE *hs) {
972
73.0k
  SSL *const ssl = hs->ssl;
973
974
73.0k
  if (!hs->certificate_status_expected) {
975
45.5k
    hs->state = state_verify_server_certificate;
976
45.5k
    return ssl_hs_ok;
977
45.5k
  }
978
979
27.5k
  SSLMessage msg;
980
27.5k
  if (!ssl->method->get_message(ssl, &msg)) {
981
13.8k
    return ssl_hs_read_message;
982
13.8k
  }
983
984
13.6k
  if (msg.type != SSL3_MT_CERTIFICATE_STATUS) {
985
    // A server may send status_request in ServerHello and then change its mind
986
    // about sending CertificateStatus.
987
12.5k
    hs->state = state_verify_server_certificate;
988
12.5k
    return ssl_hs_ok;
989
12.5k
  }
990
991
1.15k
  if (!ssl_hash_message(hs, msg)) {
992
0
    return ssl_hs_error;
993
0
  }
994
995
1.15k
  CBS certificate_status = msg.body, ocsp_response;
996
1.15k
  uint8_t status_type;
997
1.15k
  if (!CBS_get_u8(&certificate_status, &status_type) ||
998
1.15k
      status_type != TLSEXT_STATUSTYPE_ocsp ||
999
1.15k
      !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
1000
1.15k
      CBS_len(&ocsp_response) == 0 ||
1001
1.15k
      CBS_len(&certificate_status) != 0) {
1002
5
    OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1003
5
    ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1004
5
    return ssl_hs_error;
1005
5
  }
1006
1007
1.14k
  hs->new_session->ocsp_response.reset(
1008
1.14k
      CRYPTO_BUFFER_new_from_CBS(&ocsp_response, ssl->ctx->pool));
1009
1.14k
  if (hs->new_session->ocsp_response == nullptr) {
1010
0
    ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1011
0
    return ssl_hs_error;
1012
0
  }
1013
1014
1.14k
  ssl->method->next_message(ssl);
1015
1016
1.14k
  hs->state = state_verify_server_certificate;
1017
1.14k
  return ssl_hs_ok;
1018
1.14k
}
1019
1020
59.1k
static enum ssl_hs_wait_t do_verify_server_certificate(SSL_HANDSHAKE *hs) {
1021
59.1k
  if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
1022
0
    hs->state = state_read_server_key_exchange;
1023
0
    return ssl_hs_ok;
1024
0
  }
1025
1026
59.1k
  switch (ssl_verify_peer_cert(hs)) {
1027
59.0k
    case ssl_verify_ok:
1028
59.0k
      break;
1029
123
    case ssl_verify_invalid:
1030
123
      return ssl_hs_error;
1031
0
    case ssl_verify_retry:
1032
0
      hs->state = state_verify_server_certificate;
1033
0
      return ssl_hs_certificate_verify;
1034
59.1k
  }
1035
1036
59.0k
  hs->state = state_read_server_key_exchange;
1037
59.0k
  return ssl_hs_ok;
1038
59.1k
}
1039
1040
0
static enum ssl_hs_wait_t do_reverify_server_certificate(SSL_HANDSHAKE *hs) {
1041
0
  assert(hs->ssl->ctx->reverify_on_resume);
1042
1043
0
  switch (ssl_reverify_peer_cert(hs, /*send_alert=*/true)) {
1044
0
    case ssl_verify_ok:
1045
0
      break;
1046
0
    case ssl_verify_invalid:
1047
0
      return ssl_hs_error;
1048
0
    case ssl_verify_retry:
1049
0
      hs->state = state_reverify_server_certificate;
1050
0
      return ssl_hs_certificate_verify;
1051
0
  }
1052
1053
0
  hs->state = state_read_session_ticket;
1054
0
  return ssl_hs_ok;
1055
0
}
1056
1057
105k
static enum ssl_hs_wait_t do_read_server_key_exchange(SSL_HANDSHAKE *hs) {
1058
105k
  SSL *const ssl = hs->ssl;
1059
105k
  SSLMessage msg;
1060
105k
  if (!ssl->method->get_message(ssl, &msg)) {
1061
46.7k
    return ssl_hs_read_message;
1062
46.7k
  }
1063
1064
58.9k
  if (msg.type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1065
    // Some ciphers (pure PSK) have an optional ServerKeyExchange message.
1066
2.70k
    if (ssl_cipher_requires_server_key_exchange(hs->new_cipher)) {
1067
11
      OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
1068
11
      ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1069
11
      return ssl_hs_error;
1070
11
    }
1071
1072
2.69k
    hs->state = state_read_certificate_request;
1073
2.69k
    return ssl_hs_ok;
1074
2.70k
  }
1075
1076
56.2k
  if (!ssl_hash_message(hs, msg)) {
1077
0
    return ssl_hs_error;
1078
0
  }
1079
1080
56.2k
  uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1081
56.2k
  uint32_t alg_a = hs->new_cipher->algorithm_auth;
1082
56.2k
  CBS server_key_exchange = msg.body;
1083
56.2k
  if (alg_a & SSL_aPSK) {
1084
0
    CBS psk_identity_hint;
1085
1086
    // Each of the PSK key exchanges begins with a psk_identity_hint.
1087
0
    if (!CBS_get_u16_length_prefixed(&server_key_exchange,
1088
0
                                     &psk_identity_hint)) {
1089
0
      OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1090
0
      ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1091
0
      return ssl_hs_error;
1092
0
    }
1093
1094
    // Store the PSK identity hint for the ClientKeyExchange. Assume that the
1095
    // maximum length of a PSK identity hint can be as long as the maximum
1096
    // length of a PSK identity. Also do not allow NULL characters; identities
1097
    // are saved as C strings.
1098
    //
1099
    // TODO(davidben): Should invalid hints be ignored? It's a hint rather than
1100
    // a specific identity.
1101
0
    if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
1102
0
        CBS_contains_zero_byte(&psk_identity_hint)) {
1103
0
      OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
1104
0
      ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1105
0
      return ssl_hs_error;
1106
0
    }
1107
1108
    // Save non-empty identity hints as a C string. Empty identity hints we
1109
    // treat as missing. Plain PSK makes it possible to send either no hint
1110
    // (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell
1111
    // empty hint. Having different capabilities is odd, so we interpret empty
1112
    // and missing as identical.
1113
0
    char *raw = nullptr;
1114
0
    if (CBS_len(&psk_identity_hint) != 0 &&
1115
0
        !CBS_strdup(&psk_identity_hint, &raw)) {
1116
0
      ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1117
0
      return ssl_hs_error;
1118
0
    }
1119
0
    hs->peer_psk_identity_hint.reset(raw);
1120
0
  }
1121
1122
56.2k
  if (alg_k & SSL_kECDHE) {
1123
    // Parse the server parameters.
1124
56.2k
    uint8_t group_type;
1125
56.2k
    uint16_t group_id;
1126
56.2k
    CBS point;
1127
56.2k
    if (!CBS_get_u8(&server_key_exchange, &group_type) ||
1128
56.2k
        group_type != NAMED_CURVE_TYPE ||
1129
56.2k
        !CBS_get_u16(&server_key_exchange, &group_id) ||
1130
56.2k
        !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
1131
10
      OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1132
10
      ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1133
10
      return ssl_hs_error;
1134
10
    }
1135
1136
    // Ensure the group is consistent with preferences.
1137
56.2k
    if (!tls1_check_group_id(hs, group_id)) {
1138
33
      OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
1139
33
      ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1140
33
      return ssl_hs_error;
1141
33
    }
1142
1143
    // Save the group and peer public key for later.
1144
56.2k
    hs->new_session->group_id = group_id;
1145
56.2k
    if (!hs->peer_key.CopyFrom(point)) {
1146
0
      return ssl_hs_error;
1147
0
    }
1148
56.2k
  } else if (!(alg_k & SSL_kPSK)) {
1149
1
    OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
1150
1
    ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1151
1
    return ssl_hs_error;
1152
1
  }
1153
1154
  // At this point, |server_key_exchange| contains the signature, if any, while
1155
  // |msg.body| contains the entire message. From that, derive a CBS containing
1156
  // just the parameter.
1157
56.2k
  CBS parameter;
1158
56.2k
  CBS_init(&parameter, CBS_data(&msg.body),
1159
56.2k
           CBS_len(&msg.body) - CBS_len(&server_key_exchange));
1160
1161
  // ServerKeyExchange should be signed by the server's public key.
1162
56.2k
  if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
1163
56.2k
    uint16_t signature_algorithm = 0;
1164
56.2k
    if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
1165
48.6k
      if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) {
1166
1
        OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1167
1
        ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1168
1
        return ssl_hs_error;
1169
1
      }
1170
48.6k
      uint8_t alert = SSL_AD_DECODE_ERROR;
1171
48.6k
      if (!tls12_check_peer_sigalg(hs, &alert, signature_algorithm)) {
1172
38
        ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
1173
38
        return ssl_hs_error;
1174
38
      }
1175
48.5k
      hs->new_session->peer_signature_algorithm = signature_algorithm;
1176
48.5k
    } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
1177
7.62k
                                                    hs->peer_pubkey.get())) {
1178
1
      OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
1179
1
      ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
1180
1
      return ssl_hs_error;
1181
1
    }
1182
1183
    // The last field in |server_key_exchange| is the signature.
1184
56.2k
    CBS signature;
1185
56.2k
    if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1186
56.2k
        CBS_len(&server_key_exchange) != 0) {
1187
9
      OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1188
9
      ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1189
9
      return ssl_hs_error;
1190
9
    }
1191
1192
56.2k
    ScopedCBB transcript;
1193
56.2k
    Array<uint8_t> transcript_data;
1194
56.2k
    if (!CBB_init(transcript.get(),
1195
56.2k
                  2 * SSL3_RANDOM_SIZE + CBS_len(&parameter)) ||
1196
56.2k
        !CBB_add_bytes(transcript.get(), ssl->s3->client_random,
1197
56.2k
                       SSL3_RANDOM_SIZE) ||
1198
56.2k
        !CBB_add_bytes(transcript.get(), ssl->s3->server_random,
1199
56.2k
                       SSL3_RANDOM_SIZE) ||
1200
56.2k
        !CBB_add_bytes(transcript.get(), CBS_data(&parameter),
1201
56.2k
                       CBS_len(&parameter)) ||
1202
56.2k
        !CBBFinishArray(transcript.get(), &transcript_data)) {
1203
0
      OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1204
0
      ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1205
0
      return ssl_hs_error;
1206
0
    }
1207
1208
56.2k
    if (!ssl_public_key_verify(ssl, signature, signature_algorithm,
1209
56.2k
                               hs->peer_pubkey.get(), transcript_data)) {
1210
      // bad signature
1211
3
      OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
1212
3
      ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
1213
3
      return ssl_hs_error;
1214
3
    }
1215
56.2k
  } else {
1216
    // PSK ciphers are the only supported certificate-less ciphers.
1217
0
    assert(alg_a == SSL_aPSK);
1218
1219
0
    if (CBS_len(&server_key_exchange) > 0) {
1220
0
      OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
1221
0
      ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1222
0
      return ssl_hs_error;
1223
0
    }
1224
0
  }
1225
1226
56.2k
  ssl->method->next_message(ssl);
1227
56.2k
  hs->state = state_read_certificate_request;
1228
56.2k
  return ssl_hs_ok;
1229
56.2k
}
1230
1231
114k
static enum ssl_hs_wait_t do_read_certificate_request(SSL_HANDSHAKE *hs) {
1232
114k
  SSL *const ssl = hs->ssl;
1233
1234
114k
  if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
1235
0
    hs->state = state_read_server_hello_done;
1236
0
    return ssl_hs_ok;
1237
0
  }
1238
1239
114k
  SSLMessage msg;
1240
114k
  if (!ssl->method->get_message(ssl, &msg)) {
1241
56.3k
    return ssl_hs_read_message;
1242
56.3k
  }
1243
1244
57.9k
  if (msg.type == SSL3_MT_SERVER_HELLO_DONE) {
1245
    // If we get here we don't need the handshake buffer as we won't be doing
1246
    // client auth.
1247
29.4k
    hs->transcript.FreeBuffer();
1248
29.4k
    hs->state = state_read_server_hello_done;
1249
29.4k
    return ssl_hs_ok;
1250
29.4k
  }
1251
1252
28.4k
  if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_REQUEST) ||
1253
28.4k
      !ssl_hash_message(hs, msg)) {
1254
3
    return ssl_hs_error;
1255
3
  }
1256
1257
  // Get the certificate types.
1258
28.4k
  CBS body = msg.body, certificate_types;
1259
28.4k
  if (!CBS_get_u8_length_prefixed(&body, &certificate_types)) {
1260
1
    ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1261
1
    OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1262
1
    return ssl_hs_error;
1263
1
  }
1264
1265
28.4k
  if (!hs->certificate_types.CopyFrom(certificate_types)) {
1266
0
    ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1267
0
    return ssl_hs_error;
1268
0
  }
1269
1270
28.4k
  if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
1271
28.4k
    CBS supported_signature_algorithms;
1272
28.4k
    if (!CBS_get_u16_length_prefixed(&body, &supported_signature_algorithms) ||
1273
28.4k
        !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
1274
4
      ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1275
4
      OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1276
4
      return ssl_hs_error;
1277
4
    }
1278
28.4k
  }
1279
1280
28.4k
  uint8_t alert = SSL_AD_DECODE_ERROR;
1281
28.4k
  UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names =
1282
28.4k
      ssl_parse_client_CA_list(ssl, &alert, &body);
1283
28.4k
  if (!ca_names) {
1284
20
    ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
1285
20
    return ssl_hs_error;
1286
20
  }
1287
1288
28.4k
  if (CBS_len(&body) != 0) {
1289
5
    ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1290
5
    OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1291
5
    return ssl_hs_error;
1292
5
  }
1293
1294
28.4k
  hs->cert_request = true;
1295
28.4k
  hs->ca_names = std::move(ca_names);
1296
28.4k
  ssl->ctx->x509_method->hs_flush_cached_ca_names(hs);
1297
1298
28.4k
  ssl->method->next_message(ssl);
1299
28.4k
  hs->state = state_read_server_hello_done;
1300
28.4k
  return ssl_hs_ok;
1301
28.4k
}
1302
1303
86.5k
static enum ssl_hs_wait_t do_read_server_hello_done(SSL_HANDSHAKE *hs) {
1304
86.5k
  SSL *const ssl = hs->ssl;
1305
86.5k
  SSLMessage msg;
1306
86.5k
  if (!ssl->method->get_message(ssl, &msg)) {
1307
28.6k
    return ssl_hs_read_message;
1308
28.6k
  }
1309
1310
57.9k
  if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO_DONE) ||
1311
57.9k
      !ssl_hash_message(hs, msg)) {
1312
1
    return ssl_hs_error;
1313
1
  }
1314
1315
  // ServerHelloDone is empty.
1316
57.9k
  if (CBS_len(&msg.body) != 0) {
1317
3
    ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1318
3
    OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1319
3
    return ssl_hs_error;
1320
3
  }
1321
1322
  // ServerHelloDone should be the end of the flight.
1323
57.9k
  if (ssl->method->has_unprocessed_handshake_data(ssl)) {
1324
4
    ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1325
4
    OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESS_HANDSHAKE_DATA);
1326
4
    return ssl_hs_error;
1327
4
  }
1328
1329
57.9k
  ssl->method->next_message(ssl);
1330
57.9k
  hs->state = state_send_client_certificate;
1331
57.9k
  return ssl_hs_ok;
1332
57.9k
}
1333
1334
57.9k
static enum ssl_hs_wait_t do_send_client_certificate(SSL_HANDSHAKE *hs) {
1335
57.9k
  SSL *const ssl = hs->ssl;
1336
1337
  // The peer didn't request a certificate.
1338
57.9k
  if (!hs->cert_request) {
1339
29.4k
    hs->state = state_send_client_key_exchange;
1340
29.4k
    return ssl_hs_ok;
1341
29.4k
  }
1342
1343
28.4k
  if (ssl->s3->ech_status == ssl_ech_rejected) {
1344
    // Do not send client certificates on ECH reject. We have not authenticated
1345
    // the server for the name that can learn the certificate.
1346
0
    SSL_certs_clear(ssl);
1347
28.4k
  } else if (hs->config->cert->cert_cb != nullptr) {
1348
    // Call cert_cb to update the certificate.
1349
0
    int rv = hs->config->cert->cert_cb(ssl, hs->config->cert->cert_cb_arg);
1350
0
    if (rv == 0) {
1351
0
      ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1352
0
      OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
1353
0
      return ssl_hs_error;
1354
0
    }
1355
0
    if (rv < 0) {
1356
0
      hs->state = state_send_client_certificate;
1357
0
      return ssl_hs_x509_lookup;
1358
0
    }
1359
0
  }
1360
1361
28.4k
  if (!ssl_has_certificate(hs)) {
1362
    // Without a client certificate, the handshake buffer may be released.
1363
0
    hs->transcript.FreeBuffer();
1364
0
  }
1365
1366
28.4k
  if (!ssl_on_certificate_selected(hs) ||
1367
28.4k
      !ssl_output_cert_chain(hs)) {
1368
0
    return ssl_hs_error;
1369
0
  }
1370
1371
1372
28.4k
  hs->state = state_send_client_key_exchange;
1373
28.4k
  return ssl_hs_ok;
1374
28.4k
}
1375
1376
static_assert(sizeof(size_t) >= sizeof(unsigned),
1377
              "size_t is smaller than unsigned");
1378
1379
57.9k
static enum ssl_hs_wait_t do_send_client_key_exchange(SSL_HANDSHAKE *hs) {
1380
57.9k
  SSL *const ssl = hs->ssl;
1381
57.9k
  ScopedCBB cbb;
1382
57.9k
  CBB body;
1383
57.9k
  if (!ssl->method->init_message(ssl, cbb.get(), &body,
1384
57.9k
                                 SSL3_MT_CLIENT_KEY_EXCHANGE)) {
1385
0
    return ssl_hs_error;
1386
0
  }
1387
1388
57.9k
  Array<uint8_t> pms;
1389
57.9k
  uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1390
57.9k
  uint32_t alg_a = hs->new_cipher->algorithm_auth;
1391
57.9k
  if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
1392
57.9k
    const CRYPTO_BUFFER *leaf =
1393
57.9k
        sk_CRYPTO_BUFFER_value(hs->new_session->certs.get(), 0);
1394
57.9k
    CBS leaf_cbs;
1395
57.9k
    CRYPTO_BUFFER_init_CBS(leaf, &leaf_cbs);
1396
1397
    // Check the key usage matches the cipher suite. We do this unconditionally
1398
    // for non-RSA certificates. In particular, it's needed to distinguish ECDH
1399
    // certificates, which we do not support, from ECDSA certificates.
1400
    // Historically, we have not checked RSA key usages, so it is controlled by
1401
    // a flag for now. See https://crbug.com/795089.
1402
57.9k
    ssl_key_usage_t intended_use = (alg_k & SSL_kRSA)
1403
57.9k
                                       ? key_usage_encipherment
1404
57.9k
                                       : key_usage_digital_signature;
1405
57.9k
    if (!ssl_cert_check_key_usage(&leaf_cbs, intended_use)) {
1406
14
      if (hs->config->enforce_rsa_key_usage ||
1407
14
          EVP_PKEY_id(hs->peer_pubkey.get()) != EVP_PKEY_RSA) {
1408
14
        return ssl_hs_error;
1409
14
      }
1410
0
      ERR_clear_error();
1411
0
      ssl->s3->was_key_usage_invalid = true;
1412
0
    }
1413
57.9k
  }
1414
1415
  // If using a PSK key exchange, prepare the pre-shared key.
1416
57.8k
  unsigned psk_len = 0;
1417
57.8k
  uint8_t psk[PSK_MAX_PSK_LEN];
1418
57.8k
  if (alg_a & SSL_aPSK) {
1419
0
    if (hs->config->psk_client_callback == NULL) {
1420
0
      OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
1421
0
      return ssl_hs_error;
1422
0
    }
1423
1424
0
    char identity[PSK_MAX_IDENTITY_LEN + 1];
1425
0
    OPENSSL_memset(identity, 0, sizeof(identity));
1426
0
    psk_len = hs->config->psk_client_callback(
1427
0
        ssl, hs->peer_psk_identity_hint.get(), identity, sizeof(identity), psk,
1428
0
        sizeof(psk));
1429
0
    if (psk_len == 0) {
1430
0
      OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1431
0
      ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1432
0
      return ssl_hs_error;
1433
0
    }
1434
0
    assert(psk_len <= PSK_MAX_PSK_LEN);
1435
1436
0
    hs->new_session->psk_identity.reset(OPENSSL_strdup(identity));
1437
0
    if (hs->new_session->psk_identity == nullptr) {
1438
0
      return ssl_hs_error;
1439
0
    }
1440
1441
    // Write out psk_identity.
1442
0
    CBB child;
1443
0
    if (!CBB_add_u16_length_prefixed(&body, &child) ||
1444
0
        !CBB_add_bytes(&child, (const uint8_t *)identity,
1445
0
                       OPENSSL_strnlen(identity, sizeof(identity))) ||
1446
0
        !CBB_flush(&body)) {
1447
0
      return ssl_hs_error;
1448
0
    }
1449
0
  }
1450
1451
  // Depending on the key exchange method, compute |pms|.
1452
57.8k
  if (alg_k & SSL_kRSA) {
1453
2.68k
    if (!pms.Init(SSL_MAX_MASTER_KEY_LENGTH)) {
1454
0
      return ssl_hs_error;
1455
0
    }
1456
1457
2.68k
    RSA *rsa = EVP_PKEY_get0_RSA(hs->peer_pubkey.get());
1458
2.68k
    if (rsa == NULL) {
1459
0
      OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1460
0
      return ssl_hs_error;
1461
0
    }
1462
1463
2.68k
    pms[0] = hs->client_version >> 8;
1464
2.68k
    pms[1] = hs->client_version & 0xff;
1465
2.68k
    if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1466
0
      return ssl_hs_error;
1467
0
    }
1468
1469
2.68k
    CBB enc_pms;
1470
2.68k
    uint8_t *ptr;
1471
2.68k
    size_t enc_pms_len;
1472
2.68k
    if (!CBB_add_u16_length_prefixed(&body, &enc_pms) ||
1473
2.68k
        !CBB_reserve(&enc_pms, &ptr, RSA_size(rsa)) ||
1474
2.68k
        !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms.data(),
1475
2.68k
                     pms.size(), RSA_PKCS1_PADDING) ||
1476
2.68k
        !CBB_did_write(&enc_pms, enc_pms_len) ||
1477
2.68k
        !CBB_flush(&body)) {
1478
0
      return ssl_hs_error;
1479
0
    }
1480
55.2k
  } else if (alg_k & SSL_kECDHE) {
1481
55.2k
    CBB child;
1482
55.2k
    if (!CBB_add_u8_length_prefixed(&body, &child)) {
1483
0
      return ssl_hs_error;
1484
0
    }
1485
1486
    // Generate a premaster secret and encapsulate it.
1487
55.2k
    bssl::UniquePtr<SSLKeyShare> kem =
1488
55.2k
        SSLKeyShare::Create(hs->new_session->group_id);
1489
55.2k
    uint8_t alert = SSL_AD_DECODE_ERROR;
1490
55.2k
    if (!kem || !kem->Encap(&child, &pms, &alert, hs->peer_key)) {
1491
245
      ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
1492
245
      return ssl_hs_error;
1493
245
    }
1494
54.9k
    if (!CBB_flush(&body)) {
1495
0
      return ssl_hs_error;
1496
0
    }
1497
1498
    // The peer key can now be discarded.
1499
54.9k
    hs->peer_key.Reset();
1500
54.9k
  } else if (alg_k & SSL_kPSK) {
1501
    // For plain PSK, other_secret is a block of 0s with the same length as
1502
    // the pre-shared key.
1503
0
    if (!pms.Init(psk_len)) {
1504
0
      return ssl_hs_error;
1505
0
    }
1506
0
    OPENSSL_memset(pms.data(), 0, pms.size());
1507
0
  } else {
1508
0
    ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1509
0
    OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1510
0
    return ssl_hs_error;
1511
0
  }
1512
1513
  // For a PSK cipher suite, other_secret is combined with the pre-shared
1514
  // key.
1515
57.6k
  if (alg_a & SSL_aPSK) {
1516
0
    ScopedCBB pms_cbb;
1517
0
    CBB child;
1518
0
    if (!CBB_init(pms_cbb.get(), 2 + psk_len + 2 + pms.size()) ||
1519
0
        !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
1520
0
        !CBB_add_bytes(&child, pms.data(), pms.size()) ||
1521
0
        !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
1522
0
        !CBB_add_bytes(&child, psk, psk_len) ||
1523
0
        !CBBFinishArray(pms_cbb.get(), &pms)) {
1524
0
      return ssl_hs_error;
1525
0
    }
1526
0
  }
1527
1528
  // The message must be added to the finished hash before calculating the
1529
  // master secret.
1530
57.6k
  if (!ssl_add_message_cbb(ssl, cbb.get())) {
1531
0
    return ssl_hs_error;
1532
0
  }
1533
1534
57.6k
  hs->new_session->secret_length =
1535
57.6k
      tls1_generate_master_secret(hs, hs->new_session->secret, pms);
1536
57.6k
  if (hs->new_session->secret_length == 0) {
1537
0
    return ssl_hs_error;
1538
0
  }
1539
57.6k
  hs->new_session->extended_master_secret = hs->extended_master_secret;
1540
1541
57.6k
  hs->state = state_send_client_certificate_verify;
1542
57.6k
  return ssl_hs_ok;
1543
57.6k
}
1544
1545
57.6k
static enum ssl_hs_wait_t do_send_client_certificate_verify(SSL_HANDSHAKE *hs) {
1546
57.6k
  SSL *const ssl = hs->ssl;
1547
1548
57.6k
  if (!hs->cert_request || !ssl_has_certificate(hs)) {
1549
29.2k
    hs->state = state_send_client_finished;
1550
29.2k
    return ssl_hs_ok;
1551
29.2k
  }
1552
1553
28.3k
  assert(ssl_has_private_key(hs));
1554
0
  ScopedCBB cbb;
1555
28.3k
  CBB body, child;
1556
28.3k
  if (!ssl->method->init_message(ssl, cbb.get(), &body,
1557
28.3k
                                 SSL3_MT_CERTIFICATE_VERIFY)) {
1558
0
    return ssl_hs_error;
1559
0
  }
1560
1561
28.3k
  uint16_t signature_algorithm;
1562
28.3k
  if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
1563
27
    ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1564
27
    return ssl_hs_error;
1565
27
  }
1566
28.3k
  if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
1567
    // Write out the digest type in TLS 1.2.
1568
28.3k
    if (!CBB_add_u16(&body, signature_algorithm)) {
1569
0
      OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1570
0
      return ssl_hs_error;
1571
0
    }
1572
28.3k
  }
1573
1574
  // Set aside space for the signature.
1575
28.3k
  const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
1576
28.3k
  uint8_t *ptr;
1577
28.3k
  if (!CBB_add_u16_length_prefixed(&body, &child) ||
1578
28.3k
      !CBB_reserve(&child, &ptr, max_sig_len)) {
1579
0
    return ssl_hs_error;
1580
0
  }
1581
1582
28.3k
  size_t sig_len = max_sig_len;
1583
28.3k
  switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
1584
28.3k
                               signature_algorithm,
1585
28.3k
                               hs->transcript.buffer())) {
1586
28.3k
    case ssl_private_key_success:
1587
28.3k
      break;
1588
0
    case ssl_private_key_failure:
1589
0
      return ssl_hs_error;
1590
0
    case ssl_private_key_retry:
1591
0
      hs->state = state_send_client_certificate_verify;
1592
0
      return ssl_hs_private_key_operation;
1593
28.3k
  }
1594
1595
28.3k
  if (!CBB_did_write(&child, sig_len) ||
1596
28.3k
      !ssl_add_message_cbb(ssl, cbb.get())) {
1597
0
    return ssl_hs_error;
1598
0
  }
1599
1600
  // The handshake buffer is no longer necessary.
1601
28.3k
  hs->transcript.FreeBuffer();
1602
1603
28.3k
  hs->state = state_send_client_finished;
1604
28.3k
  return ssl_hs_ok;
1605
28.3k
}
1606
1607
57.7k
static enum ssl_hs_wait_t do_send_client_finished(SSL_HANDSHAKE *hs) {
1608
57.7k
  SSL *const ssl = hs->ssl;
1609
57.7k
  hs->can_release_private_key = true;
1610
57.7k
  if (!ssl->method->add_change_cipher_spec(ssl) ||
1611
57.7k
      !tls1_change_cipher_state(hs, evp_aead_seal)) {
1612
0
    return ssl_hs_error;
1613
0
  }
1614
1615
57.7k
  if (hs->next_proto_neg_seen) {
1616
3
    static const uint8_t kZero[32] = {0};
1617
3
    size_t padding_len =
1618
3
        32 - ((ssl->s3->next_proto_negotiated.size() + 2) % 32);
1619
1620
3
    ScopedCBB cbb;
1621
3
    CBB body, child;
1622
3
    if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_NEXT_PROTO) ||
1623
3
        !CBB_add_u8_length_prefixed(&body, &child) ||
1624
3
        !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated.data(),
1625
3
                       ssl->s3->next_proto_negotiated.size()) ||
1626
3
        !CBB_add_u8_length_prefixed(&body, &child) ||
1627
3
        !CBB_add_bytes(&child, kZero, padding_len) ||
1628
3
        !ssl_add_message_cbb(ssl, cbb.get())) {
1629
0
      OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1630
0
      return ssl_hs_error;
1631
0
    }
1632
3
  }
1633
1634
57.7k
  if (hs->channel_id_negotiated) {
1635
87
    ScopedCBB cbb;
1636
87
    CBB body;
1637
87
    if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CHANNEL_ID) ||
1638
87
        !tls1_write_channel_id(hs, &body) ||
1639
87
        !ssl_add_message_cbb(ssl, cbb.get())) {
1640
0
      OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1641
0
      return ssl_hs_error;
1642
0
    }
1643
87
  }
1644
1645
57.7k
  if (!ssl_send_finished(hs)) {
1646
0
    return ssl_hs_error;
1647
0
  }
1648
1649
57.7k
  hs->state = state_finish_flight;
1650
57.7k
  return ssl_hs_flush;
1651
57.7k
}
1652
1653
0
static bool can_false_start(const SSL_HANDSHAKE *hs) {
1654
0
  const SSL *const ssl = hs->ssl;
1655
1656
  // False Start bypasses the Finished check's downgrade protection. This can
1657
  // enable attacks where we send data under weaker settings than supported
1658
  // (e.g. the Logjam attack). Thus we require TLS 1.2 with an ECDHE+AEAD
1659
  // cipher, our strongest settings before TLS 1.3.
1660
  //
1661
  // Now that TLS 1.3 exists, we would like to avoid similar attacks between
1662
  // TLS 1.2 and TLS 1.3, but there are too many TLS 1.2 deployments to
1663
  // sacrifice False Start on them. Instead, we rely on the ServerHello.random
1664
  // downgrade signal, which we unconditionally enforce.
1665
0
  if (SSL_is_dtls(ssl) ||
1666
0
      SSL_version(ssl) != TLS1_2_VERSION ||
1667
0
      hs->new_cipher->algorithm_mkey != SSL_kECDHE ||
1668
0
      hs->new_cipher->algorithm_mac != SSL_AEAD) {
1669
0
    return false;
1670
0
  }
1671
1672
  // If ECH was rejected, disable False Start. We run the handshake to
1673
  // completion, including the Finished downgrade check, to authenticate the
1674
  // recovery flow.
1675
0
  if (ssl->s3->ech_status == ssl_ech_rejected) {
1676
0
    return false;
1677
0
  }
1678
1679
  // Additionally require ALPN or NPN by default.
1680
  //
1681
  // TODO(davidben): Can this constraint be relaxed globally now that cipher
1682
  // suite requirements have been tightened?
1683
0
  if (!ssl->ctx->false_start_allowed_without_alpn &&
1684
0
      ssl->s3->alpn_selected.empty() &&
1685
0
      ssl->s3->next_proto_negotiated.empty()) {
1686
0
    return false;
1687
0
  }
1688
1689
0
  return true;
1690
0
}
1691
1692
57.7k
static enum ssl_hs_wait_t do_finish_flight(SSL_HANDSHAKE *hs) {
1693
57.7k
  SSL *const ssl = hs->ssl;
1694
57.7k
  if (ssl->session != NULL) {
1695
137
    hs->state = state_finish_client_handshake;
1696
137
    return ssl_hs_ok;
1697
137
  }
1698
1699
  // This is a full handshake. If it involves ChannelID, then record the
1700
  // handshake hashes at this point in the session so that any resumption of
1701
  // this session with ChannelID can sign those hashes.
1702
57.6k
  if (!tls1_record_handshake_hashes_for_channel_id(hs)) {
1703
0
    return ssl_hs_error;
1704
0
  }
1705
1706
57.6k
  hs->state = state_read_session_ticket;
1707
1708
57.6k
  if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
1709
57.6k
      can_false_start(hs) &&
1710
      // No False Start on renegotiation (would complicate the state machine).
1711
57.6k
      !ssl->s3->initial_handshake_complete) {
1712
0
    hs->in_false_start = true;
1713
0
    hs->can_early_write = true;
1714
0
    return ssl_hs_early_return;
1715
0
  }
1716
1717
57.6k
  return ssl_hs_ok;
1718
57.6k
}
1719
1720
114k
static enum ssl_hs_wait_t do_read_session_ticket(SSL_HANDSHAKE *hs) {
1721
114k
  SSL *const ssl = hs->ssl;
1722
1723
114k
  if (!hs->ticket_expected) {
1724
205
    hs->state = state_process_change_cipher_spec;
1725
205
    return ssl_hs_read_change_cipher_spec;
1726
205
  }
1727
1728
114k
  SSLMessage msg;
1729
114k
  if (!ssl->method->get_message(ssl, &msg)) {
1730
57.8k
    return ssl_hs_read_message;
1731
57.8k
  }
1732
1733
56.9k
  if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEW_SESSION_TICKET) ||
1734
56.9k
      !ssl_hash_message(hs, msg)) {
1735
11
    return ssl_hs_error;
1736
11
  }
1737
1738
56.8k
  CBS new_session_ticket = msg.body, ticket;
1739
56.8k
  uint32_t ticket_lifetime_hint;
1740
56.8k
  if (!CBS_get_u32(&new_session_ticket, &ticket_lifetime_hint) ||
1741
56.8k
      !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1742
56.8k
      CBS_len(&new_session_ticket) != 0) {
1743
31
    ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1744
31
    OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1745
31
    return ssl_hs_error;
1746
31
  }
1747
1748
56.8k
  if (CBS_len(&ticket) == 0) {
1749
    // RFC 5077 allows a server to change its mind and send no ticket after
1750
    // negotiating the extension. The value of |ticket_expected| is checked in
1751
    // |ssl_update_cache| so is cleared here to avoid an unnecessary update.
1752
1
    hs->ticket_expected = false;
1753
1
    ssl->method->next_message(ssl);
1754
1
    hs->state = state_process_change_cipher_spec;
1755
1
    return ssl_hs_read_change_cipher_spec;
1756
1
  }
1757
1758
56.8k
  if (ssl->session != nullptr) {
1759
    // The server is sending a new ticket for an existing session. Sessions are
1760
    // immutable once established, so duplicate all but the ticket of the
1761
    // existing session.
1762
0
    assert(!hs->new_session);
1763
0
    hs->new_session =
1764
0
        SSL_SESSION_dup(ssl->session.get(), SSL_SESSION_INCLUDE_NONAUTH);
1765
0
    if (!hs->new_session) {
1766
0
      return ssl_hs_error;
1767
0
    }
1768
0
  }
1769
1770
  // |ticket_lifetime_hint| is measured from when the ticket was issued.
1771
56.8k
  ssl_session_rebase_time(ssl, hs->new_session.get());
1772
1773
56.8k
  if (!hs->new_session->ticket.CopyFrom(ticket)) {
1774
0
    return ssl_hs_error;
1775
0
  }
1776
56.8k
  hs->new_session->ticket_lifetime_hint = ticket_lifetime_hint;
1777
1778
  // Historically, OpenSSL filled in fake session IDs for ticket-based sessions.
1779
  // TODO(davidben): Are external callers relying on this? Try removing this.
1780
56.8k
  SHA256(CBS_data(&ticket), CBS_len(&ticket), hs->new_session->session_id);
1781
56.8k
  hs->new_session->session_id_length = SHA256_DIGEST_LENGTH;
1782
1783
56.8k
  ssl->method->next_message(ssl);
1784
56.8k
  hs->state = state_process_change_cipher_spec;
1785
56.8k
  return ssl_hs_read_change_cipher_spec;
1786
56.8k
}
1787
1788
56.9k
static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
1789
56.9k
  if (!tls1_change_cipher_state(hs, evp_aead_open)) {
1790
0
    return ssl_hs_error;
1791
0
  }
1792
1793
56.9k
  hs->state = state_read_server_finished;
1794
56.9k
  return ssl_hs_ok;
1795
56.9k
}
1796
1797
114k
static enum ssl_hs_wait_t do_read_server_finished(SSL_HANDSHAKE *hs) {
1798
114k
  SSL *const ssl = hs->ssl;
1799
114k
  enum ssl_hs_wait_t wait = ssl_get_finished(hs);
1800
114k
  if (wait != ssl_hs_ok) {
1801
57.2k
    return wait;
1802
57.2k
  }
1803
1804
56.8k
  if (ssl->session != NULL) {
1805
137
    hs->state = state_send_client_finished;
1806
137
    return ssl_hs_ok;
1807
137
  }
1808
1809
56.6k
  hs->state = state_finish_client_handshake;
1810
56.6k
  return ssl_hs_ok;
1811
56.8k
}
1812
1813
57.0k
static enum ssl_hs_wait_t do_finish_client_handshake(SSL_HANDSHAKE *hs) {
1814
57.0k
  SSL *const ssl = hs->ssl;
1815
57.0k
  if (ssl->s3->ech_status == ssl_ech_rejected) {
1816
    // Release the retry configs.
1817
0
    hs->ech_authenticated_reject = true;
1818
0
    ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ECH_REQUIRED);
1819
0
    OPENSSL_PUT_ERROR(SSL, SSL_R_ECH_REJECTED);
1820
0
    return ssl_hs_error;
1821
0
  }
1822
1823
57.0k
  ssl->method->on_handshake_complete(ssl);
1824
1825
  // Note TLS 1.2 resumptions with ticket renewal have both |ssl->session| (the
1826
  // resumed session) and |hs->new_session| (the session with the new ticket).
1827
57.0k
  bool has_new_session = hs->new_session != nullptr;
1828
57.0k
  if (has_new_session) {
1829
    // When False Start is enabled, the handshake reports completion early. The
1830
    // caller may then have passed the (then unresuable) |hs->new_session| to
1831
    // another thread via |SSL_get0_session| for resumption. To avoid potential
1832
    // race conditions in such callers, we duplicate the session before
1833
    // clearing |not_resumable|.
1834
56.8k
    ssl->s3->established_session =
1835
56.8k
        SSL_SESSION_dup(hs->new_session.get(), SSL_SESSION_DUP_ALL);
1836
56.8k
    if (!ssl->s3->established_session) {
1837
0
      return ssl_hs_error;
1838
0
    }
1839
    // Renegotiations do not participate in session resumption.
1840
56.8k
    if (!ssl->s3->initial_handshake_complete) {
1841
1.67k
      ssl->s3->established_session->not_resumable = false;
1842
1.67k
    }
1843
1844
56.8k
    hs->new_session.reset();
1845
56.8k
  } else {
1846
137
    assert(ssl->session != nullptr);
1847
0
    ssl->s3->established_session = UpRef(ssl->session);
1848
137
  }
1849
1850
57.0k
  hs->handshake_finalized = true;
1851
57.0k
  ssl->s3->initial_handshake_complete = true;
1852
57.0k
  if (has_new_session) {
1853
56.8k
    ssl_update_cache(ssl);
1854
56.8k
  }
1855
1856
57.0k
  hs->state = state_done;
1857
57.0k
  return ssl_hs_ok;
1858
57.0k
}
1859
1860
645k
enum ssl_hs_wait_t ssl_client_handshake(SSL_HANDSHAKE *hs) {
1861
1.52M
  while (hs->state != state_done) {
1862
1.46M
    enum ssl_hs_wait_t ret = ssl_hs_error;
1863
1.46M
    enum ssl_client_hs_state_t state =
1864
1.46M
        static_cast<enum ssl_client_hs_state_t>(hs->state);
1865
1.46M
    switch (state) {
1866
62.0k
      case state_start_connect:
1867
62.0k
        ret = do_start_connect(hs);
1868
62.0k
        break;
1869
62.0k
      case state_enter_early_data:
1870
62.0k
        ret = do_enter_early_data(hs);
1871
62.0k
        break;
1872
98
      case state_early_reverify_server_certificate:
1873
98
        ret = do_early_reverify_server_certificate(hs);
1874
98
        break;
1875
0
      case state_read_hello_verify_request:
1876
0
        ret = do_read_hello_verify_request(hs);
1877
0
        break;
1878
133k
      case state_read_server_hello:
1879
133k
        ret = do_read_server_hello(hs);
1880
133k
        break;
1881
3.38k
      case state_tls13:
1882
3.38k
        ret = do_tls13(hs);
1883
3.38k
        break;
1884
133k
      case state_read_server_certificate:
1885
133k
        ret = do_read_server_certificate(hs);
1886
133k
        break;
1887
73.0k
      case state_read_certificate_status:
1888
73.0k
        ret = do_read_certificate_status(hs);
1889
73.0k
        break;
1890
59.1k
      case state_verify_server_certificate:
1891
59.1k
        ret = do_verify_server_certificate(hs);
1892
59.1k
        break;
1893
0
      case state_reverify_server_certificate:
1894
0
        ret = do_reverify_server_certificate(hs);
1895
0
        break;
1896
105k
      case state_read_server_key_exchange:
1897
105k
        ret = do_read_server_key_exchange(hs);
1898
105k
        break;
1899
114k
      case state_read_certificate_request:
1900
114k
        ret = do_read_certificate_request(hs);
1901
114k
        break;
1902
86.5k
      case state_read_server_hello_done:
1903
86.5k
        ret = do_read_server_hello_done(hs);
1904
86.5k
        break;
1905
57.9k
      case state_send_client_certificate:
1906
57.9k
        ret = do_send_client_certificate(hs);
1907
57.9k
        break;
1908
57.9k
      case state_send_client_key_exchange:
1909
57.9k
        ret = do_send_client_key_exchange(hs);
1910
57.9k
        break;
1911
57.6k
      case state_send_client_certificate_verify:
1912
57.6k
        ret = do_send_client_certificate_verify(hs);
1913
57.6k
        break;
1914
57.7k
      case state_send_client_finished:
1915
57.7k
        ret = do_send_client_finished(hs);
1916
57.7k
        break;
1917
57.7k
      case state_finish_flight:
1918
57.7k
        ret = do_finish_flight(hs);
1919
57.7k
        break;
1920
114k
      case state_read_session_ticket:
1921
114k
        ret = do_read_session_ticket(hs);
1922
114k
        break;
1923
56.9k
      case state_process_change_cipher_spec:
1924
56.9k
        ret = do_process_change_cipher_spec(hs);
1925
56.9k
        break;
1926
114k
      case state_read_server_finished:
1927
114k
        ret = do_read_server_finished(hs);
1928
114k
        break;
1929
57.0k
      case state_finish_client_handshake:
1930
57.0k
        ret = do_finish_client_handshake(hs);
1931
57.0k
        break;
1932
0
      case state_done:
1933
0
        ret = ssl_hs_ok;
1934
0
        break;
1935
1.46M
    }
1936
1937
1.46M
    if (hs->state != state) {
1938
1.05M
      ssl_do_info_callback(hs->ssl, SSL_CB_CONNECT_LOOP, 1);
1939
1.05M
    }
1940
1941
1.46M
    if (ret != ssl_hs_ok) {
1942
588k
      return ret;
1943
588k
    }
1944
1.46M
  }
1945
1946
57.0k
  ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
1947
57.0k
  return ssl_hs_ok;
1948
645k
}
1949
1950
0
const char *ssl_client_handshake_state(SSL_HANDSHAKE *hs) {
1951
0
  enum ssl_client_hs_state_t state =
1952
0
      static_cast<enum ssl_client_hs_state_t>(hs->state);
1953
0
  switch (state) {
1954
0
    case state_start_connect:
1955
0
      return "TLS client start_connect";
1956
0
    case state_enter_early_data:
1957
0
      return "TLS client enter_early_data";
1958
0
    case state_early_reverify_server_certificate:
1959
0
      return "TLS client early_reverify_server_certificate";
1960
0
    case state_read_hello_verify_request:
1961
0
      return "TLS client read_hello_verify_request";
1962
0
    case state_read_server_hello:
1963
0
      return "TLS client read_server_hello";
1964
0
    case state_tls13:
1965
0
      return tls13_client_handshake_state(hs);
1966
0
    case state_read_server_certificate:
1967
0
      return "TLS client read_server_certificate";
1968
0
    case state_read_certificate_status:
1969
0
      return "TLS client read_certificate_status";
1970
0
    case state_verify_server_certificate:
1971
0
      return "TLS client verify_server_certificate";
1972
0
    case state_reverify_server_certificate:
1973
0
      return "TLS client reverify_server_certificate";
1974
0
    case state_read_server_key_exchange:
1975
0
      return "TLS client read_server_key_exchange";
1976
0
    case state_read_certificate_request:
1977
0
      return "TLS client read_certificate_request";
1978
0
    case state_read_server_hello_done:
1979
0
      return "TLS client read_server_hello_done";
1980
0
    case state_send_client_certificate:
1981
0
      return "TLS client send_client_certificate";
1982
0
    case state_send_client_key_exchange:
1983
0
      return "TLS client send_client_key_exchange";
1984
0
    case state_send_client_certificate_verify:
1985
0
      return "TLS client send_client_certificate_verify";
1986
0
    case state_send_client_finished:
1987
0
      return "TLS client send_client_finished";
1988
0
    case state_finish_flight:
1989
0
      return "TLS client finish_flight";
1990
0
    case state_read_session_ticket:
1991
0
      return "TLS client read_session_ticket";
1992
0
    case state_process_change_cipher_spec:
1993
0
      return "TLS client process_change_cipher_spec";
1994
0
    case state_read_server_finished:
1995
0
      return "TLS client read_server_finished";
1996
0
    case state_finish_client_handshake:
1997
0
      return "TLS client finish_client_handshake";
1998
0
    case state_done:
1999
0
      return "TLS client done";
2000
0
  }
2001
2002
0
  return "TLS client unknown";
2003
0
}
2004
2005
BSSL_NAMESPACE_END