Coverage Report

Created: 2025-11-17 06:18

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