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/md5.h>
166 #include <openssl/mem.h>
167 #include <openssl/rand.h>
168 #include <openssl/sha.h>
169
170 #include "../crypto/internal.h"
171 #include "internal.h"
172
173
174 BSSL_NAMESPACE_BEGIN
175
176 enum ssl_client_hs_state_t {
177 state_start_connect = 0,
178 state_enter_early_data,
179 state_early_reverify_server_certificate,
180 state_read_hello_verify_request,
181 state_read_server_hello,
182 state_tls13,
183 state_read_server_certificate,
184 state_read_certificate_status,
185 state_verify_server_certificate,
186 state_reverify_server_certificate,
187 state_read_server_key_exchange,
188 state_read_certificate_request,
189 state_read_server_hello_done,
190 state_send_client_certificate,
191 state_send_client_key_exchange,
192 state_send_client_certificate_verify,
193 state_send_client_finished,
194 state_finish_flight,
195 state_read_session_ticket,
196 state_process_change_cipher_spec,
197 state_read_server_finished,
198 state_finish_client_handshake,
199 state_done,
200 };
201
202 // ssl_get_client_disabled sets |*out_mask_a| and |*out_mask_k| to masks of
203 // disabled algorithms.
ssl_get_client_disabled(SSL_HANDSHAKE * hs,uint32_t * out_mask_a,uint32_t * out_mask_k)204 static void ssl_get_client_disabled(SSL_HANDSHAKE *hs, uint32_t *out_mask_a,
205 uint32_t *out_mask_k) {
206 *out_mask_a = 0;
207 *out_mask_k = 0;
208
209 // PSK requires a client callback.
210 if (hs->config->psk_client_callback == NULL) {
211 *out_mask_a |= SSL_aPSK;
212 *out_mask_k |= SSL_kPSK;
213 }
214 }
215
ssl_write_client_cipher_list(SSL_HANDSHAKE * hs,CBB * out)216 static bool ssl_write_client_cipher_list(SSL_HANDSHAKE *hs, CBB *out) {
217 SSL *const ssl = hs->ssl;
218 uint32_t mask_a, mask_k;
219 ssl_get_client_disabled(hs, &mask_a, &mask_k);
220
221 CBB child;
222 if (!CBB_add_u16_length_prefixed(out, &child)) {
223 return false;
224 }
225
226 // Add a fake cipher suite. See draft-davidben-tls-grease-01.
227 if (ssl->ctx->grease_enabled &&
228 !CBB_add_u16(&child, ssl_get_grease_value(hs, ssl_grease_cipher))) {
229 return false;
230 }
231
232 // Add TLS 1.3 ciphers. Order ChaCha20-Poly1305 relative to AES-GCM based on
233 // hardware support.
234 if (hs->max_version >= TLS1_3_VERSION) {
235 if (!EVP_has_aes_hardware() &&
236 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
237 return false;
238 }
239 if (!CBB_add_u16(&child, TLS1_CK_AES_128_GCM_SHA256 & 0xffff) ||
240 !CBB_add_u16(&child, TLS1_CK_AES_256_GCM_SHA384 & 0xffff)) {
241 return false;
242 }
243 if (EVP_has_aes_hardware() &&
244 !CBB_add_u16(&child, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
245 return false;
246 }
247 }
248
249 if (hs->min_version < TLS1_3_VERSION) {
250 bool any_enabled = false;
251 for (const SSL_CIPHER *cipher : SSL_get_ciphers(ssl)) {
252 // Skip disabled ciphers
253 if ((cipher->algorithm_mkey & mask_k) ||
254 (cipher->algorithm_auth & mask_a)) {
255 continue;
256 }
257 if (SSL_CIPHER_get_min_version(cipher) > hs->max_version ||
258 SSL_CIPHER_get_max_version(cipher) < hs->min_version) {
259 continue;
260 }
261 any_enabled = true;
262 if (!CBB_add_u16(&child, SSL_CIPHER_get_protocol_id(cipher))) {
263 return false;
264 }
265 }
266
267 // If all ciphers were disabled, return the error to the caller.
268 if (!any_enabled && hs->max_version < TLS1_3_VERSION) {
269 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
270 return false;
271 }
272 }
273
274 if (ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
275 if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
276 return false;
277 }
278 }
279
280 return CBB_flush(out);
281 }
282
ssl_write_client_hello(SSL_HANDSHAKE * hs)283 bool ssl_write_client_hello(SSL_HANDSHAKE *hs) {
284 SSL *const ssl = hs->ssl;
285 ScopedCBB cbb;
286 CBB body;
287 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CLIENT_HELLO)) {
288 return false;
289 }
290
291 CBB child;
292 if (!CBB_add_u16(&body, hs->client_version) ||
293 !CBB_add_bytes(&body, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
294 !CBB_add_u8_length_prefixed(&body, &child)) {
295 return false;
296 }
297
298 // Do not send a session ID on renegotiation.
299 if (!ssl->s3->initial_handshake_complete &&
300 !CBB_add_bytes(&child, hs->session_id, hs->session_id_len)) {
301 return false;
302 }
303
304 if (SSL_is_dtls(ssl)) {
305 if (!CBB_add_u8_length_prefixed(&body, &child) ||
306 !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
307 return false;
308 }
309 }
310
311 size_t header_len =
312 SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH;
313 if (!ssl_write_client_cipher_list(hs, &body) ||
314 !CBB_add_u8(&body, 1 /* one compression method */) ||
315 !CBB_add_u8(&body, 0 /* null compression */) ||
316 !ssl_add_clienthello_tlsext(hs, &body, header_len + CBB_len(&body))) {
317 return false;
318 }
319
320 Array<uint8_t> msg;
321 if (!ssl->method->finish_message(ssl, cbb.get(), &msg)) {
322 return false;
323 }
324
325 // Now that the length prefixes have been computed, fill in the placeholder
326 // PSK binder.
327 if (hs->needs_psk_binder &&
328 !tls13_write_psk_binder(hs, MakeSpan(msg))) {
329 return false;
330 }
331
332 return ssl->method->add_message(ssl, std::move(msg));
333 }
334
parse_supported_versions(SSL_HANDSHAKE * hs,uint16_t * version,const CBS * in)335 static bool parse_supported_versions(SSL_HANDSHAKE *hs, uint16_t *version,
336 const CBS *in) {
337 // If the outer version is not TLS 1.2, or there is no extensions block, use
338 // the outer version.
339 if (*version != TLS1_2_VERSION || CBS_len(in) == 0) {
340 return true;
341 }
342
343 SSL *const ssl = hs->ssl;
344 CBS copy = *in, extensions;
345 if (!CBS_get_u16_length_prefixed(©, &extensions) ||
346 CBS_len(©) != 0) {
347 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
348 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
349 return false;
350 }
351
352 bool have_supported_versions;
353 CBS supported_versions;
354 const SSL_EXTENSION_TYPE ext_types[] = {
355 {TLSEXT_TYPE_supported_versions, &have_supported_versions,
356 &supported_versions},
357 };
358
359 uint8_t alert = SSL_AD_DECODE_ERROR;
360 if (!ssl_parse_extensions(&extensions, &alert, ext_types,
361 /*ignore_unknown=*/true)) {
362 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
363 return false;
364 }
365
366 // Override the outer version with the extension, if present.
367 if (have_supported_versions &&
368 (!CBS_get_u16(&supported_versions, version) ||
369 CBS_len(&supported_versions) != 0)) {
370 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
371 return false;
372 }
373
374 return true;
375 }
376
do_start_connect(SSL_HANDSHAKE * hs)377 static enum ssl_hs_wait_t do_start_connect(SSL_HANDSHAKE *hs) {
378 SSL *const ssl = hs->ssl;
379
380 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
381 // |session_reused| must be reset in case this is a renegotiation.
382 ssl->s3->session_reused = false;
383
384 // Freeze the version range.
385 if (!ssl_get_version_range(hs, &hs->min_version, &hs->max_version)) {
386 return ssl_hs_error;
387 }
388
389 // Always advertise the ClientHello version from the original maximum version,
390 // even on renegotiation. The static RSA key exchange uses this field, and
391 // some servers fail when it changes across handshakes.
392 if (SSL_is_dtls(hs->ssl)) {
393 hs->client_version =
394 hs->max_version >= TLS1_2_VERSION ? DTLS1_2_VERSION : DTLS1_VERSION;
395 } else {
396 hs->client_version =
397 hs->max_version >= TLS1_2_VERSION ? TLS1_2_VERSION : hs->max_version;
398 }
399
400 // If the configured session has expired or was created at a disabled
401 // version, drop it.
402 if (ssl->session != NULL) {
403 if (ssl->session->is_server ||
404 !ssl_supports_version(hs, ssl->session->ssl_version) ||
405 (ssl->session->session_id_length == 0 &&
406 ssl->session->ticket.empty()) ||
407 ssl->session->not_resumable ||
408 !ssl_session_is_time_valid(ssl, ssl->session.get()) ||
409 (ssl->quic_method != nullptr) != ssl->session->is_quic) {
410 ssl_set_session(ssl, NULL);
411 }
412 }
413
414 if (!RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
415 return ssl_hs_error;
416 }
417
418 // Never send a session ID in QUIC. QUIC uses TLS 1.3 at a minimum and
419 // disables TLS 1.3 middlebox compatibility mode.
420 if (ssl->quic_method == nullptr) {
421 if (ssl->session != nullptr && !ssl->s3->initial_handshake_complete &&
422 ssl->session->session_id_length > 0) {
423 hs->session_id_len = ssl->session->session_id_length;
424 OPENSSL_memcpy(hs->session_id, ssl->session->session_id,
425 hs->session_id_len);
426 } else if (hs->max_version >= TLS1_3_VERSION) {
427 // Initialize a random session ID.
428 hs->session_id_len = sizeof(hs->session_id);
429 if (!RAND_bytes(hs->session_id, hs->session_id_len)) {
430 return ssl_hs_error;
431 }
432 }
433 }
434
435 if (!ssl_write_client_hello(hs)) {
436 return ssl_hs_error;
437 }
438
439 hs->state = state_enter_early_data;
440 return ssl_hs_flush;
441 }
442
do_enter_early_data(SSL_HANDSHAKE * hs)443 static enum ssl_hs_wait_t do_enter_early_data(SSL_HANDSHAKE *hs) {
444 SSL *const ssl = hs->ssl;
445
446 if (SSL_is_dtls(ssl)) {
447 hs->state = state_read_hello_verify_request;
448 return ssl_hs_ok;
449 }
450
451 if (!hs->early_data_offered) {
452 hs->state = state_read_server_hello;
453 return ssl_hs_ok;
454 }
455
456 ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->session->ssl_version);
457 if (!ssl->method->add_change_cipher_spec(ssl)) {
458 return ssl_hs_error;
459 }
460
461 if (!tls13_init_early_key_schedule(
462 hs,
463 MakeConstSpan(ssl->session->secret, ssl->session->secret_length)) ||
464 !tls13_derive_early_secret(hs)) {
465 return ssl_hs_error;
466 }
467
468 // Stash the early data session, so connection properties may be queried out
469 // of it.
470 hs->early_session = UpRef(ssl->session);
471 hs->state = state_early_reverify_server_certificate;
472 return ssl_hs_ok;
473 }
474
do_early_reverify_server_certificate(SSL_HANDSHAKE * hs)475 static enum ssl_hs_wait_t do_early_reverify_server_certificate(SSL_HANDSHAKE *hs) {
476 if (hs->ssl->ctx->reverify_on_resume) {
477 // Don't send an alert on error. The alert be in early data, which the
478 // server may not accept anyway. It would also be a mismatch between QUIC
479 // and TCP because the QUIC early keys are deferred below.
480 //
481 // TODO(davidben): The client behavior should be to verify the certificate
482 // before deciding whether to offer the session and, if invalid, decline to
483 // send the session.
484 switch (ssl_reverify_peer_cert(hs, /*send_alert=*/false)) {
485 case ssl_verify_ok:
486 break;
487 case ssl_verify_invalid:
488 return ssl_hs_error;
489 case ssl_verify_retry:
490 hs->state = state_early_reverify_server_certificate;
491 return ssl_hs_certificate_verify;
492 }
493 }
494
495 // Defer releasing the 0-RTT key to after certificate reverification, so the
496 // QUIC implementation does not accidentally write data too early.
497 if (!tls13_set_traffic_key(hs->ssl, ssl_encryption_early_data, evp_aead_seal,
498 hs->early_session.get(),
499 hs->early_traffic_secret())) {
500 return ssl_hs_error;
501 }
502
503 hs->in_early_data = true;
504 hs->can_early_write = true;
505 hs->state = state_read_server_hello;
506 return ssl_hs_early_return;
507 }
508
do_read_hello_verify_request(SSL_HANDSHAKE * hs)509 static enum ssl_hs_wait_t do_read_hello_verify_request(SSL_HANDSHAKE *hs) {
510 SSL *const ssl = hs->ssl;
511
512 assert(SSL_is_dtls(ssl));
513
514 SSLMessage msg;
515 if (!ssl->method->get_message(ssl, &msg)) {
516 return ssl_hs_read_message;
517 }
518
519 if (msg.type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
520 hs->state = state_read_server_hello;
521 return ssl_hs_ok;
522 }
523
524 CBS hello_verify_request = msg.body, cookie;
525 uint16_t server_version;
526 if (!CBS_get_u16(&hello_verify_request, &server_version) ||
527 !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
528 CBS_len(&cookie) > sizeof(ssl->d1->cookie) ||
529 CBS_len(&hello_verify_request) != 0) {
530 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
531 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
532 return ssl_hs_error;
533 }
534
535 OPENSSL_memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
536 ssl->d1->cookie_len = CBS_len(&cookie);
537
538 ssl->method->next_message(ssl);
539
540 // DTLS resets the handshake buffer after HelloVerifyRequest.
541 if (!hs->transcript.Init()) {
542 return ssl_hs_error;
543 }
544
545 if (!ssl_write_client_hello(hs)) {
546 return ssl_hs_error;
547 }
548
549 hs->state = state_read_server_hello;
550 return ssl_hs_flush;
551 }
552
do_read_server_hello(SSL_HANDSHAKE * hs)553 static enum ssl_hs_wait_t do_read_server_hello(SSL_HANDSHAKE *hs) {
554 SSL *const ssl = hs->ssl;
555 SSLMessage msg;
556 if (!ssl->method->get_message(ssl, &msg)) {
557 return ssl_hs_read_server_hello;
558 }
559
560 if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO)) {
561 return ssl_hs_error;
562 }
563
564 CBS server_hello = msg.body, server_random, session_id;
565 uint16_t server_version, cipher_suite;
566 uint8_t compression_method;
567 if (!CBS_get_u16(&server_hello, &server_version) ||
568 !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
569 !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
570 CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
571 !CBS_get_u16(&server_hello, &cipher_suite) ||
572 !CBS_get_u8(&server_hello, &compression_method)) {
573 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
574 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
575 return ssl_hs_error;
576 }
577
578 // Use the supported_versions extension if applicable.
579 if (!parse_supported_versions(hs, &server_version, &server_hello)) {
580 return ssl_hs_error;
581 }
582
583 if (!ssl_supports_version(hs, server_version)) {
584 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
585 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
586 return ssl_hs_error;
587 }
588
589 assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
590 if (!ssl->s3->have_version) {
591 ssl->version = server_version;
592 // At this point, the connection's version is known and ssl->version is
593 // fixed. Begin enforcing the record-layer version.
594 ssl->s3->have_version = true;
595 ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version);
596 } else if (server_version != ssl->version) {
597 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
598 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
599 return ssl_hs_error;
600 }
601
602 if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
603 hs->state = state_tls13;
604 return ssl_hs_ok;
605 }
606
607 // Clear some TLS 1.3 state that no longer needs to be retained.
608 hs->key_shares[0].reset();
609 hs->key_shares[1].reset();
610 hs->key_share_bytes.Reset();
611
612 // A TLS 1.2 server would not know to skip the early data we offered. Report
613 // an error code sooner. The caller may use this error code to implement the
614 // fallback described in RFC 8446 appendix D.3.
615 if (hs->early_data_offered) {
616 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_ON_EARLY_DATA);
617 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_PROTOCOL_VERSION);
618 return ssl_hs_error;
619 }
620
621 // Copy over the server random.
622 OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_random),
623 SSL3_RANDOM_SIZE);
624
625 // Enforce the TLS 1.3 anti-downgrade feature.
626 if (!ssl->s3->initial_handshake_complete &&
627 ssl_supports_version(hs, TLS1_3_VERSION)) {
628 static_assert(
629 sizeof(kTLS12DowngradeRandom) == sizeof(kTLS13DowngradeRandom),
630 "downgrade signals have different size");
631 static_assert(
632 sizeof(kJDK11DowngradeRandom) == sizeof(kTLS13DowngradeRandom),
633 "downgrade signals have different size");
634 auto suffix =
635 MakeConstSpan(ssl->s3->server_random, sizeof(ssl->s3->server_random))
636 .subspan(SSL3_RANDOM_SIZE - sizeof(kTLS13DowngradeRandom));
637 if (suffix == kTLS12DowngradeRandom || suffix == kTLS13DowngradeRandom ||
638 suffix == kJDK11DowngradeRandom) {
639 OPENSSL_PUT_ERROR(SSL, SSL_R_TLS13_DOWNGRADE);
640 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
641 return ssl_hs_error;
642 }
643 }
644
645 if (!ssl->s3->initial_handshake_complete && ssl->session != nullptr &&
646 ssl->session->session_id_length != 0 &&
647 CBS_mem_equal(&session_id, ssl->session->session_id,
648 ssl->session->session_id_length)) {
649 ssl->s3->session_reused = true;
650 } else {
651 // The server may also have echoed back the TLS 1.3 compatibility mode
652 // session ID. As we know this is not a session the server knows about, any
653 // server resuming it is in error. Reject the first connection
654 // deterministicly, rather than installing an invalid session into the
655 // session cache. https://crbug.com/796910
656 if (hs->session_id_len != 0 &&
657 CBS_mem_equal(&session_id, hs->session_id, hs->session_id_len)) {
658 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_ECHOED_INVALID_SESSION_ID);
659 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
660 return ssl_hs_error;
661 }
662
663 // The session wasn't resumed. Create a fresh SSL_SESSION to
664 // fill out.
665 ssl_set_session(ssl, NULL);
666 if (!ssl_get_new_session(hs, 0 /* client */)) {
667 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
668 return ssl_hs_error;
669 }
670 // Note: session_id could be empty.
671 hs->new_session->session_id_length = CBS_len(&session_id);
672 OPENSSL_memcpy(hs->new_session->session_id, CBS_data(&session_id),
673 CBS_len(&session_id));
674 }
675
676 const SSL_CIPHER *cipher = SSL_get_cipher_by_value(cipher_suite);
677 if (cipher == NULL) {
678 // unknown cipher
679 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
680 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
681 return ssl_hs_error;
682 }
683
684 // The cipher must be allowed in the selected version and enabled.
685 uint32_t mask_a, mask_k;
686 ssl_get_client_disabled(hs, &mask_a, &mask_k);
687 if ((cipher->algorithm_mkey & mask_k) || (cipher->algorithm_auth & mask_a) ||
688 SSL_CIPHER_get_min_version(cipher) > ssl_protocol_version(ssl) ||
689 SSL_CIPHER_get_max_version(cipher) < ssl_protocol_version(ssl) ||
690 !sk_SSL_CIPHER_find(SSL_get_ciphers(ssl), NULL, cipher)) {
691 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
692 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
693 return ssl_hs_error;
694 }
695
696 if (ssl->session != NULL) {
697 if (ssl->session->ssl_version != ssl->version) {
698 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
699 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
700 return ssl_hs_error;
701 }
702 if (ssl->session->cipher != cipher) {
703 OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
704 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
705 return ssl_hs_error;
706 }
707 if (!ssl_session_is_context_valid(hs, ssl->session.get())) {
708 // This is actually a client application bug.
709 OPENSSL_PUT_ERROR(SSL,
710 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
711 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
712 return ssl_hs_error;
713 }
714 } else {
715 hs->new_session->cipher = cipher;
716 }
717 hs->new_cipher = cipher;
718
719 // Now that the cipher is known, initialize the handshake hash and hash the
720 // ServerHello.
721 if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) ||
722 !ssl_hash_message(hs, msg)) {
723 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
724 return ssl_hs_error;
725 }
726
727 // If doing a full handshake, the server may request a client certificate
728 // which requires hashing the handshake transcript. Otherwise, the handshake
729 // buffer may be released.
730 if (ssl->session != NULL ||
731 !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
732 hs->transcript.FreeBuffer();
733 }
734
735 // Only the NULL compression algorithm is supported.
736 if (compression_method != 0) {
737 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
738 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
739 return ssl_hs_error;
740 }
741
742 // TLS extensions
743 if (!ssl_parse_serverhello_tlsext(hs, &server_hello)) {
744 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
745 return ssl_hs_error;
746 }
747
748 // There should be nothing left over in the record.
749 if (CBS_len(&server_hello) != 0) {
750 // wrong packet length
751 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
752 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
753 return ssl_hs_error;
754 }
755
756 if (ssl->session != NULL &&
757 hs->extended_master_secret != ssl->session->extended_master_secret) {
758 if (ssl->session->extended_master_secret) {
759 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
760 } else {
761 OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
762 }
763 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
764 return ssl_hs_error;
765 }
766
767 if (ssl->s3->token_binding_negotiated &&
768 (!hs->extended_master_secret || !ssl->s3->send_connection_binding)) {
769 OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_TB_WITHOUT_EMS_OR_RI);
770 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION);
771 return ssl_hs_error;
772 }
773
774 ssl->method->next_message(ssl);
775
776 if (ssl->session != NULL) {
777 if (ssl->ctx->reverify_on_resume &&
778 ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
779 hs->state = state_reverify_server_certificate;
780 } else {
781 hs->state = state_read_session_ticket;
782 }
783 return ssl_hs_ok;
784 }
785
786 hs->state = state_read_server_certificate;
787 return ssl_hs_ok;
788 }
789
do_tls13(SSL_HANDSHAKE * hs)790 static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) {
791 enum ssl_hs_wait_t wait = tls13_client_handshake(hs);
792 if (wait == ssl_hs_ok) {
793 hs->state = state_finish_client_handshake;
794 return ssl_hs_ok;
795 }
796
797 return wait;
798 }
799
do_read_server_certificate(SSL_HANDSHAKE * hs)800 static enum ssl_hs_wait_t do_read_server_certificate(SSL_HANDSHAKE *hs) {
801 SSL *const ssl = hs->ssl;
802
803 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
804 hs->state = state_read_certificate_status;
805 return ssl_hs_ok;
806 }
807
808 SSLMessage msg;
809 if (!ssl->method->get_message(ssl, &msg)) {
810 return ssl_hs_read_message;
811 }
812
813 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE) ||
814 !ssl_hash_message(hs, msg)) {
815 return ssl_hs_error;
816 }
817
818 CBS body = msg.body;
819 uint8_t alert = SSL_AD_DECODE_ERROR;
820 if (!ssl_parse_cert_chain(&alert, &hs->new_session->certs, &hs->peer_pubkey,
821 NULL, &body, ssl->ctx->pool)) {
822 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
823 return ssl_hs_error;
824 }
825
826 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) == 0 ||
827 CBS_len(&body) != 0 ||
828 !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
829 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
830 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
831 return ssl_hs_error;
832 }
833
834 if (!ssl_check_leaf_certificate(
835 hs, hs->peer_pubkey.get(),
836 sk_CRYPTO_BUFFER_value(hs->new_session->certs.get(), 0))) {
837 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
838 return ssl_hs_error;
839 }
840
841 ssl->method->next_message(ssl);
842
843 hs->state = state_read_certificate_status;
844 return ssl_hs_ok;
845 }
846
do_read_certificate_status(SSL_HANDSHAKE * hs)847 static enum ssl_hs_wait_t do_read_certificate_status(SSL_HANDSHAKE *hs) {
848 SSL *const ssl = hs->ssl;
849
850 if (!hs->certificate_status_expected) {
851 hs->state = state_verify_server_certificate;
852 return ssl_hs_ok;
853 }
854
855 SSLMessage msg;
856 if (!ssl->method->get_message(ssl, &msg)) {
857 return ssl_hs_read_message;
858 }
859
860 if (msg.type != SSL3_MT_CERTIFICATE_STATUS) {
861 // A server may send status_request in ServerHello and then change its mind
862 // about sending CertificateStatus.
863 hs->state = state_verify_server_certificate;
864 return ssl_hs_ok;
865 }
866
867 if (!ssl_hash_message(hs, msg)) {
868 return ssl_hs_error;
869 }
870
871 CBS certificate_status = msg.body, ocsp_response;
872 uint8_t status_type;
873 if (!CBS_get_u8(&certificate_status, &status_type) ||
874 status_type != TLSEXT_STATUSTYPE_ocsp ||
875 !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
876 CBS_len(&ocsp_response) == 0 ||
877 CBS_len(&certificate_status) != 0) {
878 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
879 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
880 return ssl_hs_error;
881 }
882
883 hs->new_session->ocsp_response.reset(
884 CRYPTO_BUFFER_new_from_CBS(&ocsp_response, ssl->ctx->pool));
885 if (hs->new_session->ocsp_response == nullptr) {
886 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
887 return ssl_hs_error;
888 }
889
890 ssl->method->next_message(ssl);
891
892 hs->state = state_verify_server_certificate;
893 return ssl_hs_ok;
894 }
895
do_verify_server_certificate(SSL_HANDSHAKE * hs)896 static enum ssl_hs_wait_t do_verify_server_certificate(SSL_HANDSHAKE *hs) {
897 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
898 hs->state = state_read_server_key_exchange;
899 return ssl_hs_ok;
900 }
901
902 switch (ssl_verify_peer_cert(hs)) {
903 case ssl_verify_ok:
904 break;
905 case ssl_verify_invalid:
906 return ssl_hs_error;
907 case ssl_verify_retry:
908 hs->state = state_verify_server_certificate;
909 return ssl_hs_certificate_verify;
910 }
911
912 hs->state = state_read_server_key_exchange;
913 return ssl_hs_ok;
914 }
915
do_reverify_server_certificate(SSL_HANDSHAKE * hs)916 static enum ssl_hs_wait_t do_reverify_server_certificate(SSL_HANDSHAKE *hs) {
917 assert(hs->ssl->ctx->reverify_on_resume);
918
919 switch (ssl_reverify_peer_cert(hs, /*send_alert=*/true)) {
920 case ssl_verify_ok:
921 break;
922 case ssl_verify_invalid:
923 return ssl_hs_error;
924 case ssl_verify_retry:
925 hs->state = state_reverify_server_certificate;
926 return ssl_hs_certificate_verify;
927 }
928
929 hs->state = state_read_session_ticket;
930 return ssl_hs_ok;
931 }
932
do_read_server_key_exchange(SSL_HANDSHAKE * hs)933 static enum ssl_hs_wait_t do_read_server_key_exchange(SSL_HANDSHAKE *hs) {
934 SSL *const ssl = hs->ssl;
935 SSLMessage msg;
936 if (!ssl->method->get_message(ssl, &msg)) {
937 return ssl_hs_read_message;
938 }
939
940 if (msg.type != SSL3_MT_SERVER_KEY_EXCHANGE) {
941 // Some ciphers (pure PSK) have an optional ServerKeyExchange message.
942 if (ssl_cipher_requires_server_key_exchange(hs->new_cipher)) {
943 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
944 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
945 return ssl_hs_error;
946 }
947
948 hs->state = state_read_certificate_request;
949 return ssl_hs_ok;
950 }
951
952 if (!ssl_hash_message(hs, msg)) {
953 return ssl_hs_error;
954 }
955
956 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
957 uint32_t alg_a = hs->new_cipher->algorithm_auth;
958 CBS server_key_exchange = msg.body;
959 if (alg_a & SSL_aPSK) {
960 CBS psk_identity_hint;
961
962 // Each of the PSK key exchanges begins with a psk_identity_hint.
963 if (!CBS_get_u16_length_prefixed(&server_key_exchange,
964 &psk_identity_hint)) {
965 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
966 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
967 return ssl_hs_error;
968 }
969
970 // Store the PSK identity hint for the ClientKeyExchange. Assume that the
971 // maximum length of a PSK identity hint can be as long as the maximum
972 // length of a PSK identity. Also do not allow NULL characters; identities
973 // are saved as C strings.
974 //
975 // TODO(davidben): Should invalid hints be ignored? It's a hint rather than
976 // a specific identity.
977 if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
978 CBS_contains_zero_byte(&psk_identity_hint)) {
979 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
980 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
981 return ssl_hs_error;
982 }
983
984 // Save non-empty identity hints as a C string. Empty identity hints we
985 // treat as missing. Plain PSK makes it possible to send either no hint
986 // (omit ServerKeyExchange) or an empty hint, while ECDHE_PSK can only spell
987 // empty hint. Having different capabilities is odd, so we interpret empty
988 // and missing as identical.
989 char *raw = nullptr;
990 if (CBS_len(&psk_identity_hint) != 0 &&
991 !CBS_strdup(&psk_identity_hint, &raw)) {
992 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
993 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
994 return ssl_hs_error;
995 }
996 hs->peer_psk_identity_hint.reset(raw);
997 }
998
999 if (alg_k & SSL_kECDHE) {
1000 // Parse the server parameters.
1001 uint8_t group_type;
1002 uint16_t group_id;
1003 CBS point;
1004 if (!CBS_get_u8(&server_key_exchange, &group_type) ||
1005 group_type != NAMED_CURVE_TYPE ||
1006 !CBS_get_u16(&server_key_exchange, &group_id) ||
1007 !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
1008 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1009 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1010 return ssl_hs_error;
1011 }
1012 hs->new_session->group_id = group_id;
1013
1014 // Ensure the group is consistent with preferences.
1015 if (!tls1_check_group_id(hs, group_id)) {
1016 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
1017 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
1018 return ssl_hs_error;
1019 }
1020
1021 // Initialize ECDH and save the peer public key for later.
1022 hs->key_shares[0] = SSLKeyShare::Create(group_id);
1023 if (!hs->key_shares[0] ||
1024 !hs->peer_key.CopyFrom(point)) {
1025 return ssl_hs_error;
1026 }
1027 } else if (!(alg_k & SSL_kPSK)) {
1028 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
1029 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1030 return ssl_hs_error;
1031 }
1032
1033 // At this point, |server_key_exchange| contains the signature, if any, while
1034 // |msg.body| contains the entire message. From that, derive a CBS containing
1035 // just the parameter.
1036 CBS parameter;
1037 CBS_init(¶meter, CBS_data(&msg.body),
1038 CBS_len(&msg.body) - CBS_len(&server_key_exchange));
1039
1040 // ServerKeyExchange should be signed by the server's public key.
1041 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
1042 uint16_t signature_algorithm = 0;
1043 if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
1044 if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) {
1045 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1046 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1047 return ssl_hs_error;
1048 }
1049 uint8_t alert = SSL_AD_DECODE_ERROR;
1050 if (!tls12_check_peer_sigalg(hs, &alert, signature_algorithm)) {
1051 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
1052 return ssl_hs_error;
1053 }
1054 hs->new_session->peer_signature_algorithm = signature_algorithm;
1055 } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
1056 hs->peer_pubkey.get())) {
1057 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
1058 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
1059 return ssl_hs_error;
1060 }
1061
1062 // The last field in |server_key_exchange| is the signature.
1063 CBS signature;
1064 if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
1065 CBS_len(&server_key_exchange) != 0) {
1066 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1067 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1068 return ssl_hs_error;
1069 }
1070
1071 ScopedCBB transcript;
1072 Array<uint8_t> transcript_data;
1073 if (!CBB_init(transcript.get(),
1074 2 * SSL3_RANDOM_SIZE + CBS_len(¶meter)) ||
1075 !CBB_add_bytes(transcript.get(), ssl->s3->client_random,
1076 SSL3_RANDOM_SIZE) ||
1077 !CBB_add_bytes(transcript.get(), ssl->s3->server_random,
1078 SSL3_RANDOM_SIZE) ||
1079 !CBB_add_bytes(transcript.get(), CBS_data(¶meter),
1080 CBS_len(¶meter)) ||
1081 !CBBFinishArray(transcript.get(), &transcript_data)) {
1082 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1083 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1084 return ssl_hs_error;
1085 }
1086
1087 if (!ssl_public_key_verify(ssl, signature, signature_algorithm,
1088 hs->peer_pubkey.get(), transcript_data)) {
1089 // bad signature
1090 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
1091 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
1092 return ssl_hs_error;
1093 }
1094 } else {
1095 // PSK ciphers are the only supported certificate-less ciphers.
1096 assert(alg_a == SSL_aPSK);
1097
1098 if (CBS_len(&server_key_exchange) > 0) {
1099 OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
1100 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1101 return ssl_hs_error;
1102 }
1103 }
1104
1105 ssl->method->next_message(ssl);
1106 hs->state = state_read_certificate_request;
1107 return ssl_hs_ok;
1108 }
1109
do_read_certificate_request(SSL_HANDSHAKE * hs)1110 static enum ssl_hs_wait_t do_read_certificate_request(SSL_HANDSHAKE *hs) {
1111 SSL *const ssl = hs->ssl;
1112
1113 if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
1114 hs->state = state_read_server_hello_done;
1115 return ssl_hs_ok;
1116 }
1117
1118 SSLMessage msg;
1119 if (!ssl->method->get_message(ssl, &msg)) {
1120 return ssl_hs_read_message;
1121 }
1122
1123 if (msg.type == SSL3_MT_SERVER_HELLO_DONE) {
1124 // If we get here we don't need the handshake buffer as we won't be doing
1125 // client auth.
1126 hs->transcript.FreeBuffer();
1127 hs->state = state_read_server_hello_done;
1128 return ssl_hs_ok;
1129 }
1130
1131 if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_REQUEST) ||
1132 !ssl_hash_message(hs, msg)) {
1133 return ssl_hs_error;
1134 }
1135
1136 // Get the certificate types.
1137 CBS body = msg.body, certificate_types;
1138 if (!CBS_get_u8_length_prefixed(&body, &certificate_types)) {
1139 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1140 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1141 return ssl_hs_error;
1142 }
1143
1144 if (!hs->certificate_types.CopyFrom(certificate_types)) {
1145 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1146 return ssl_hs_error;
1147 }
1148
1149 if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
1150 CBS supported_signature_algorithms;
1151 if (!CBS_get_u16_length_prefixed(&body, &supported_signature_algorithms) ||
1152 !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
1153 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1154 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1155 return ssl_hs_error;
1156 }
1157 }
1158
1159 uint8_t alert = SSL_AD_DECODE_ERROR;
1160 UniquePtr<STACK_OF(CRYPTO_BUFFER)> ca_names =
1161 ssl_parse_client_CA_list(ssl, &alert, &body);
1162 if (!ca_names) {
1163 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
1164 return ssl_hs_error;
1165 }
1166
1167 if (CBS_len(&body) != 0) {
1168 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1169 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1170 return ssl_hs_error;
1171 }
1172
1173 hs->cert_request = true;
1174 hs->ca_names = std::move(ca_names);
1175 ssl->ctx->x509_method->hs_flush_cached_ca_names(hs);
1176
1177 ssl->method->next_message(ssl);
1178 hs->state = state_read_server_hello_done;
1179 return ssl_hs_ok;
1180 }
1181
do_read_server_hello_done(SSL_HANDSHAKE * hs)1182 static enum ssl_hs_wait_t do_read_server_hello_done(SSL_HANDSHAKE *hs) {
1183 SSL *const ssl = hs->ssl;
1184 SSLMessage msg;
1185 if (!ssl->method->get_message(ssl, &msg)) {
1186 return ssl_hs_read_message;
1187 }
1188
1189 if (!ssl_check_message_type(ssl, msg, SSL3_MT_SERVER_HELLO_DONE) ||
1190 !ssl_hash_message(hs, msg)) {
1191 return ssl_hs_error;
1192 }
1193
1194 // ServerHelloDone is empty.
1195 if (CBS_len(&msg.body) != 0) {
1196 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1197 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1198 return ssl_hs_error;
1199 }
1200
1201 // ServerHelloDone should be the end of the flight.
1202 if (ssl->method->has_unprocessed_handshake_data(ssl)) {
1203 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1204 OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESS_HANDSHAKE_DATA);
1205 return ssl_hs_error;
1206 }
1207
1208 ssl->method->next_message(ssl);
1209 hs->state = state_send_client_certificate;
1210 return ssl_hs_ok;
1211 }
1212
do_send_client_certificate(SSL_HANDSHAKE * hs)1213 static enum ssl_hs_wait_t do_send_client_certificate(SSL_HANDSHAKE *hs) {
1214 SSL *const ssl = hs->ssl;
1215
1216 // The peer didn't request a certificate.
1217 if (!hs->cert_request) {
1218 hs->state = state_send_client_key_exchange;
1219 return ssl_hs_ok;
1220 }
1221
1222 // Call cert_cb to update the certificate.
1223 if (hs->config->cert->cert_cb != NULL) {
1224 int rv = hs->config->cert->cert_cb(ssl, hs->config->cert->cert_cb_arg);
1225 if (rv == 0) {
1226 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1227 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
1228 return ssl_hs_error;
1229 }
1230 if (rv < 0) {
1231 hs->state = state_send_client_certificate;
1232 return ssl_hs_x509_lookup;
1233 }
1234 }
1235
1236 if (!ssl_has_certificate(hs)) {
1237 // Without a client certificate, the handshake buffer may be released.
1238 hs->transcript.FreeBuffer();
1239 }
1240
1241 if (!ssl_on_certificate_selected(hs) ||
1242 !ssl_output_cert_chain(hs)) {
1243 return ssl_hs_error;
1244 }
1245
1246
1247 hs->state = state_send_client_key_exchange;
1248 return ssl_hs_ok;
1249 }
1250
1251 static_assert(sizeof(size_t) >= sizeof(unsigned),
1252 "size_t is smaller than unsigned");
1253
do_send_client_key_exchange(SSL_HANDSHAKE * hs)1254 static enum ssl_hs_wait_t do_send_client_key_exchange(SSL_HANDSHAKE *hs) {
1255 SSL *const ssl = hs->ssl;
1256 ScopedCBB cbb;
1257 CBB body;
1258 if (!ssl->method->init_message(ssl, cbb.get(), &body,
1259 SSL3_MT_CLIENT_KEY_EXCHANGE)) {
1260 return ssl_hs_error;
1261 }
1262
1263 Array<uint8_t> pms;
1264 uint32_t alg_k = hs->new_cipher->algorithm_mkey;
1265 uint32_t alg_a = hs->new_cipher->algorithm_auth;
1266 if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
1267 const CRYPTO_BUFFER *leaf =
1268 sk_CRYPTO_BUFFER_value(hs->new_session->certs.get(), 0);
1269 CBS leaf_cbs;
1270 CRYPTO_BUFFER_init_CBS(leaf, &leaf_cbs);
1271
1272 // Check the key usage matches the cipher suite. We do this unconditionally
1273 // for non-RSA certificates. In particular, it's needed to distinguish ECDH
1274 // certificates, which we do not support, from ECDSA certificates.
1275 // Historically, we have not checked RSA key usages, so it is controlled by
1276 // a flag for now. See https://crbug.com/795089.
1277 ssl_key_usage_t intended_use = (alg_k & SSL_kRSA)
1278 ? key_usage_encipherment
1279 : key_usage_digital_signature;
1280 if (hs->config->enforce_rsa_key_usage ||
1281 EVP_PKEY_id(hs->peer_pubkey.get()) != EVP_PKEY_RSA) {
1282 if (!ssl_cert_check_key_usage(&leaf_cbs, intended_use)) {
1283 return ssl_hs_error;
1284 }
1285 }
1286 }
1287
1288 // If using a PSK key exchange, prepare the pre-shared key.
1289 unsigned psk_len = 0;
1290 uint8_t psk[PSK_MAX_PSK_LEN];
1291 if (alg_a & SSL_aPSK) {
1292 if (hs->config->psk_client_callback == NULL) {
1293 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
1294 return ssl_hs_error;
1295 }
1296
1297 char identity[PSK_MAX_IDENTITY_LEN + 1];
1298 OPENSSL_memset(identity, 0, sizeof(identity));
1299 psk_len = hs->config->psk_client_callback(
1300 ssl, hs->peer_psk_identity_hint.get(), identity, sizeof(identity), psk,
1301 sizeof(psk));
1302 if (psk_len == 0) {
1303 OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
1304 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1305 return ssl_hs_error;
1306 }
1307 assert(psk_len <= PSK_MAX_PSK_LEN);
1308
1309 hs->new_session->psk_identity.reset(OPENSSL_strdup(identity));
1310 if (hs->new_session->psk_identity == nullptr) {
1311 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1312 return ssl_hs_error;
1313 }
1314
1315 // Write out psk_identity.
1316 CBB child;
1317 if (!CBB_add_u16_length_prefixed(&body, &child) ||
1318 !CBB_add_bytes(&child, (const uint8_t *)identity,
1319 OPENSSL_strnlen(identity, sizeof(identity))) ||
1320 !CBB_flush(&body)) {
1321 return ssl_hs_error;
1322 }
1323 }
1324
1325 // Depending on the key exchange method, compute |pms|.
1326 if (alg_k & SSL_kRSA) {
1327 if (!pms.Init(SSL_MAX_MASTER_KEY_LENGTH)) {
1328 return ssl_hs_error;
1329 }
1330
1331 RSA *rsa = EVP_PKEY_get0_RSA(hs->peer_pubkey.get());
1332 if (rsa == NULL) {
1333 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1334 return ssl_hs_error;
1335 }
1336
1337 pms[0] = hs->client_version >> 8;
1338 pms[1] = hs->client_version & 0xff;
1339 if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
1340 return ssl_hs_error;
1341 }
1342
1343 CBB enc_pms;
1344 uint8_t *ptr;
1345 size_t enc_pms_len;
1346 if (!CBB_add_u16_length_prefixed(&body, &enc_pms) ||
1347 !CBB_reserve(&enc_pms, &ptr, RSA_size(rsa)) ||
1348 !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms.data(),
1349 pms.size(), RSA_PKCS1_PADDING) ||
1350 !CBB_did_write(&enc_pms, enc_pms_len) ||
1351 !CBB_flush(&body)) {
1352 return ssl_hs_error;
1353 }
1354 } else if (alg_k & SSL_kECDHE) {
1355 // Generate a keypair and serialize the public half.
1356 CBB child;
1357 if (!CBB_add_u8_length_prefixed(&body, &child)) {
1358 return ssl_hs_error;
1359 }
1360
1361 // Compute the premaster.
1362 uint8_t alert = SSL_AD_DECODE_ERROR;
1363 if (!hs->key_shares[0]->Accept(&child, &pms, &alert, hs->peer_key)) {
1364 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
1365 return ssl_hs_error;
1366 }
1367 if (!CBB_flush(&body)) {
1368 return ssl_hs_error;
1369 }
1370
1371 // The key exchange state may now be discarded.
1372 hs->key_shares[0].reset();
1373 hs->key_shares[1].reset();
1374 hs->peer_key.Reset();
1375 } else if (alg_k & SSL_kPSK) {
1376 // For plain PSK, other_secret is a block of 0s with the same length as
1377 // the pre-shared key.
1378 if (!pms.Init(psk_len)) {
1379 return ssl_hs_error;
1380 }
1381 OPENSSL_memset(pms.data(), 0, pms.size());
1382 } else {
1383 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1384 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1385 return ssl_hs_error;
1386 }
1387
1388 // For a PSK cipher suite, other_secret is combined with the pre-shared
1389 // key.
1390 if (alg_a & SSL_aPSK) {
1391 ScopedCBB pms_cbb;
1392 CBB child;
1393 if (!CBB_init(pms_cbb.get(), 2 + psk_len + 2 + pms.size()) ||
1394 !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
1395 !CBB_add_bytes(&child, pms.data(), pms.size()) ||
1396 !CBB_add_u16_length_prefixed(pms_cbb.get(), &child) ||
1397 !CBB_add_bytes(&child, psk, psk_len) ||
1398 !CBBFinishArray(pms_cbb.get(), &pms)) {
1399 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
1400 return ssl_hs_error;
1401 }
1402 }
1403
1404 // The message must be added to the finished hash before calculating the
1405 // master secret.
1406 if (!ssl_add_message_cbb(ssl, cbb.get())) {
1407 return ssl_hs_error;
1408 }
1409
1410 hs->new_session->secret_length =
1411 tls1_generate_master_secret(hs, hs->new_session->secret, pms);
1412 if (hs->new_session->secret_length == 0) {
1413 return ssl_hs_error;
1414 }
1415 hs->new_session->extended_master_secret = hs->extended_master_secret;
1416
1417 hs->state = state_send_client_certificate_verify;
1418 return ssl_hs_ok;
1419 }
1420
do_send_client_certificate_verify(SSL_HANDSHAKE * hs)1421 static enum ssl_hs_wait_t do_send_client_certificate_verify(SSL_HANDSHAKE *hs) {
1422 SSL *const ssl = hs->ssl;
1423
1424 if (!hs->cert_request || !ssl_has_certificate(hs)) {
1425 hs->state = state_send_client_finished;
1426 return ssl_hs_ok;
1427 }
1428
1429 assert(ssl_has_private_key(hs));
1430 ScopedCBB cbb;
1431 CBB body, child;
1432 if (!ssl->method->init_message(ssl, cbb.get(), &body,
1433 SSL3_MT_CERTIFICATE_VERIFY)) {
1434 return ssl_hs_error;
1435 }
1436
1437 uint16_t signature_algorithm;
1438 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
1439 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1440 return ssl_hs_error;
1441 }
1442 if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
1443 // Write out the digest type in TLS 1.2.
1444 if (!CBB_add_u16(&body, signature_algorithm)) {
1445 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1446 return ssl_hs_error;
1447 }
1448 }
1449
1450 // Set aside space for the signature.
1451 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
1452 uint8_t *ptr;
1453 if (!CBB_add_u16_length_prefixed(&body, &child) ||
1454 !CBB_reserve(&child, &ptr, max_sig_len)) {
1455 return ssl_hs_error;
1456 }
1457
1458 size_t sig_len = max_sig_len;
1459 switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
1460 signature_algorithm,
1461 hs->transcript.buffer())) {
1462 case ssl_private_key_success:
1463 break;
1464 case ssl_private_key_failure:
1465 return ssl_hs_error;
1466 case ssl_private_key_retry:
1467 hs->state = state_send_client_certificate_verify;
1468 return ssl_hs_private_key_operation;
1469 }
1470
1471 if (!CBB_did_write(&child, sig_len) ||
1472 !ssl_add_message_cbb(ssl, cbb.get())) {
1473 return ssl_hs_error;
1474 }
1475
1476 // The handshake buffer is no longer necessary.
1477 hs->transcript.FreeBuffer();
1478
1479 hs->state = state_send_client_finished;
1480 return ssl_hs_ok;
1481 }
1482
do_send_client_finished(SSL_HANDSHAKE * hs)1483 static enum ssl_hs_wait_t do_send_client_finished(SSL_HANDSHAKE *hs) {
1484 SSL *const ssl = hs->ssl;
1485 // Resolve Channel ID first, before any non-idempotent operations.
1486 if (ssl->s3->channel_id_valid) {
1487 if (!ssl_do_channel_id_callback(hs)) {
1488 return ssl_hs_error;
1489 }
1490
1491 if (hs->config->channel_id_private == NULL) {
1492 hs->state = state_send_client_finished;
1493 return ssl_hs_channel_id_lookup;
1494 }
1495 }
1496
1497 if (!ssl->method->add_change_cipher_spec(ssl) ||
1498 !tls1_change_cipher_state(hs, evp_aead_seal)) {
1499 return ssl_hs_error;
1500 }
1501
1502 if (hs->next_proto_neg_seen) {
1503 static const uint8_t kZero[32] = {0};
1504 size_t padding_len =
1505 32 - ((ssl->s3->next_proto_negotiated.size() + 2) % 32);
1506
1507 ScopedCBB cbb;
1508 CBB body, child;
1509 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_NEXT_PROTO) ||
1510 !CBB_add_u8_length_prefixed(&body, &child) ||
1511 !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated.data(),
1512 ssl->s3->next_proto_negotiated.size()) ||
1513 !CBB_add_u8_length_prefixed(&body, &child) ||
1514 !CBB_add_bytes(&child, kZero, padding_len) ||
1515 !ssl_add_message_cbb(ssl, cbb.get())) {
1516 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1517 return ssl_hs_error;
1518 }
1519 }
1520
1521 if (ssl->s3->channel_id_valid) {
1522 ScopedCBB cbb;
1523 CBB body;
1524 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CHANNEL_ID) ||
1525 !tls1_write_channel_id(hs, &body) ||
1526 !ssl_add_message_cbb(ssl, cbb.get())) {
1527 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1528 return ssl_hs_error;
1529 }
1530 }
1531
1532 if (!ssl_send_finished(hs)) {
1533 return ssl_hs_error;
1534 }
1535
1536 hs->state = state_finish_flight;
1537 return ssl_hs_flush;
1538 }
1539
can_false_start(const SSL_HANDSHAKE * hs)1540 static bool can_false_start(const SSL_HANDSHAKE *hs) {
1541 SSL *const ssl = hs->ssl;
1542
1543 // False Start bypasses the Finished check's downgrade protection. This can
1544 // enable attacks where we send data under weaker settings than supported
1545 // (e.g. the Logjam attack). Thus we require TLS 1.2 with an ECDHE+AEAD
1546 // cipher, our strongest settings before TLS 1.3.
1547 //
1548 // Now that TLS 1.3 exists, we would like to avoid similar attacks between
1549 // TLS 1.2 and TLS 1.3, but there are too many TLS 1.2 deployments to
1550 // sacrifice False Start on them. Instead, we rely on the ServerHello.random
1551 // downgrade signal, which we unconditionally enforce.
1552 if (SSL_is_dtls(ssl) ||
1553 SSL_version(ssl) != TLS1_2_VERSION ||
1554 hs->new_cipher->algorithm_mkey != SSL_kECDHE ||
1555 hs->new_cipher->algorithm_mac != SSL_AEAD) {
1556 return false;
1557 }
1558
1559 // Additionally require ALPN or NPN by default.
1560 //
1561 // TODO(davidben): Can this constraint be relaxed globally now that cipher
1562 // suite requirements have been tightened?
1563 if (!ssl->ctx->false_start_allowed_without_alpn &&
1564 ssl->s3->alpn_selected.empty() &&
1565 ssl->s3->next_proto_negotiated.empty()) {
1566 return false;
1567 }
1568
1569 return true;
1570 }
1571
do_finish_flight(SSL_HANDSHAKE * hs)1572 static enum ssl_hs_wait_t do_finish_flight(SSL_HANDSHAKE *hs) {
1573 SSL *const ssl = hs->ssl;
1574 if (ssl->session != NULL) {
1575 hs->state = state_finish_client_handshake;
1576 return ssl_hs_ok;
1577 }
1578
1579 // This is a full handshake. If it involves ChannelID, then record the
1580 // handshake hashes at this point in the session so that any resumption of
1581 // this session with ChannelID can sign those hashes.
1582 if (!tls1_record_handshake_hashes_for_channel_id(hs)) {
1583 return ssl_hs_error;
1584 }
1585
1586 hs->state = state_read_session_ticket;
1587
1588 if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
1589 can_false_start(hs) &&
1590 // No False Start on renegotiation (would complicate the state machine).
1591 !ssl->s3->initial_handshake_complete) {
1592 hs->in_false_start = true;
1593 hs->can_early_write = true;
1594 return ssl_hs_early_return;
1595 }
1596
1597 return ssl_hs_ok;
1598 }
1599
do_read_session_ticket(SSL_HANDSHAKE * hs)1600 static enum ssl_hs_wait_t do_read_session_ticket(SSL_HANDSHAKE *hs) {
1601 SSL *const ssl = hs->ssl;
1602
1603 if (!hs->ticket_expected) {
1604 hs->state = state_process_change_cipher_spec;
1605 return ssl_hs_read_change_cipher_spec;
1606 }
1607
1608 SSLMessage msg;
1609 if (!ssl->method->get_message(ssl, &msg)) {
1610 return ssl_hs_read_message;
1611 }
1612
1613 if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEW_SESSION_TICKET) ||
1614 !ssl_hash_message(hs, msg)) {
1615 return ssl_hs_error;
1616 }
1617
1618 CBS new_session_ticket = msg.body, ticket;
1619 uint32_t ticket_lifetime_hint;
1620 if (!CBS_get_u32(&new_session_ticket, &ticket_lifetime_hint) ||
1621 !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
1622 CBS_len(&new_session_ticket) != 0) {
1623 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
1624 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
1625 return ssl_hs_error;
1626 }
1627
1628 if (CBS_len(&ticket) == 0) {
1629 // RFC 5077 allows a server to change its mind and send no ticket after
1630 // negotiating the extension. The value of |ticket_expected| is checked in
1631 // |ssl_update_cache| so is cleared here to avoid an unnecessary update.
1632 hs->ticket_expected = false;
1633 ssl->method->next_message(ssl);
1634 hs->state = state_process_change_cipher_spec;
1635 return ssl_hs_read_change_cipher_spec;
1636 }
1637
1638 SSL_SESSION *session = hs->new_session.get();
1639 UniquePtr<SSL_SESSION> renewed_session;
1640 if (ssl->session != NULL) {
1641 // The server is sending a new ticket for an existing session. Sessions are
1642 // immutable once established, so duplicate all but the ticket of the
1643 // existing session.
1644 renewed_session =
1645 SSL_SESSION_dup(ssl->session.get(), SSL_SESSION_INCLUDE_NONAUTH);
1646 if (!renewed_session) {
1647 // This should never happen.
1648 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
1649 return ssl_hs_error;
1650 }
1651 session = renewed_session.get();
1652 }
1653
1654 // |ticket_lifetime_hint| is measured from when the ticket was issued.
1655 ssl_session_rebase_time(ssl, session);
1656
1657 if (!session->ticket.CopyFrom(ticket)) {
1658 return ssl_hs_error;
1659 }
1660 session->ticket_lifetime_hint = ticket_lifetime_hint;
1661
1662 // Generate a session ID for this session. Some callers expect all sessions to
1663 // have a session ID. Additionally, it acts as the session ID to signal
1664 // resumption.
1665 SHA256(CBS_data(&ticket), CBS_len(&ticket), session->session_id);
1666 session->session_id_length = SHA256_DIGEST_LENGTH;
1667
1668 if (renewed_session) {
1669 session->not_resumable = false;
1670 ssl->session = std::move(renewed_session);
1671 }
1672
1673 ssl->method->next_message(ssl);
1674 hs->state = state_process_change_cipher_spec;
1675 return ssl_hs_read_change_cipher_spec;
1676 }
1677
do_process_change_cipher_spec(SSL_HANDSHAKE * hs)1678 static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
1679 if (!tls1_change_cipher_state(hs, evp_aead_open)) {
1680 return ssl_hs_error;
1681 }
1682
1683 hs->state = state_read_server_finished;
1684 return ssl_hs_ok;
1685 }
1686
do_read_server_finished(SSL_HANDSHAKE * hs)1687 static enum ssl_hs_wait_t do_read_server_finished(SSL_HANDSHAKE *hs) {
1688 SSL *const ssl = hs->ssl;
1689 enum ssl_hs_wait_t wait = ssl_get_finished(hs);
1690 if (wait != ssl_hs_ok) {
1691 return wait;
1692 }
1693
1694 if (ssl->session != NULL) {
1695 hs->state = state_send_client_finished;
1696 return ssl_hs_ok;
1697 }
1698
1699 hs->state = state_finish_client_handshake;
1700 return ssl_hs_ok;
1701 }
1702
do_finish_client_handshake(SSL_HANDSHAKE * hs)1703 static enum ssl_hs_wait_t do_finish_client_handshake(SSL_HANDSHAKE *hs) {
1704 SSL *const ssl = hs->ssl;
1705
1706 ssl->method->on_handshake_complete(ssl);
1707
1708 if (ssl->session != NULL) {
1709 ssl->s3->established_session = UpRef(ssl->session);
1710 } else {
1711 // We make a copy of the session in order to maintain the immutability
1712 // of the new established_session due to False Start. The caller may
1713 // have taken a reference to the temporary session.
1714 ssl->s3->established_session =
1715 SSL_SESSION_dup(hs->new_session.get(), SSL_SESSION_DUP_ALL);
1716 if (!ssl->s3->established_session) {
1717 return ssl_hs_error;
1718 }
1719 // Renegotiations do not participate in session resumption.
1720 if (!ssl->s3->initial_handshake_complete) {
1721 ssl->s3->established_session->not_resumable = false;
1722 }
1723
1724 hs->new_session.reset();
1725 }
1726
1727 hs->handshake_finalized = true;
1728 ssl->s3->initial_handshake_complete = true;
1729 ssl_update_cache(hs, SSL_SESS_CACHE_CLIENT);
1730
1731 hs->state = state_done;
1732 return ssl_hs_ok;
1733 }
1734
ssl_client_handshake(SSL_HANDSHAKE * hs)1735 enum ssl_hs_wait_t ssl_client_handshake(SSL_HANDSHAKE *hs) {
1736 while (hs->state != state_done) {
1737 enum ssl_hs_wait_t ret = ssl_hs_error;
1738 enum ssl_client_hs_state_t state =
1739 static_cast<enum ssl_client_hs_state_t>(hs->state);
1740 switch (state) {
1741 case state_start_connect:
1742 ret = do_start_connect(hs);
1743 break;
1744 case state_enter_early_data:
1745 ret = do_enter_early_data(hs);
1746 break;
1747 case state_early_reverify_server_certificate:
1748 ret = do_early_reverify_server_certificate(hs);
1749 break;
1750 case state_read_hello_verify_request:
1751 ret = do_read_hello_verify_request(hs);
1752 break;
1753 case state_read_server_hello:
1754 ret = do_read_server_hello(hs);
1755 break;
1756 case state_tls13:
1757 ret = do_tls13(hs);
1758 break;
1759 case state_read_server_certificate:
1760 ret = do_read_server_certificate(hs);
1761 break;
1762 case state_read_certificate_status:
1763 ret = do_read_certificate_status(hs);
1764 break;
1765 case state_verify_server_certificate:
1766 ret = do_verify_server_certificate(hs);
1767 break;
1768 case state_reverify_server_certificate:
1769 ret = do_reverify_server_certificate(hs);
1770 break;
1771 case state_read_server_key_exchange:
1772 ret = do_read_server_key_exchange(hs);
1773 break;
1774 case state_read_certificate_request:
1775 ret = do_read_certificate_request(hs);
1776 break;
1777 case state_read_server_hello_done:
1778 ret = do_read_server_hello_done(hs);
1779 break;
1780 case state_send_client_certificate:
1781 ret = do_send_client_certificate(hs);
1782 break;
1783 case state_send_client_key_exchange:
1784 ret = do_send_client_key_exchange(hs);
1785 break;
1786 case state_send_client_certificate_verify:
1787 ret = do_send_client_certificate_verify(hs);
1788 break;
1789 case state_send_client_finished:
1790 ret = do_send_client_finished(hs);
1791 break;
1792 case state_finish_flight:
1793 ret = do_finish_flight(hs);
1794 break;
1795 case state_read_session_ticket:
1796 ret = do_read_session_ticket(hs);
1797 break;
1798 case state_process_change_cipher_spec:
1799 ret = do_process_change_cipher_spec(hs);
1800 break;
1801 case state_read_server_finished:
1802 ret = do_read_server_finished(hs);
1803 break;
1804 case state_finish_client_handshake:
1805 ret = do_finish_client_handshake(hs);
1806 break;
1807 case state_done:
1808 ret = ssl_hs_ok;
1809 break;
1810 }
1811
1812 if (hs->state != state) {
1813 ssl_do_info_callback(hs->ssl, SSL_CB_CONNECT_LOOP, 1);
1814 }
1815
1816 if (ret != ssl_hs_ok) {
1817 return ret;
1818 }
1819 }
1820
1821 ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
1822 return ssl_hs_ok;
1823 }
1824
ssl_client_handshake_state(SSL_HANDSHAKE * hs)1825 const char *ssl_client_handshake_state(SSL_HANDSHAKE *hs) {
1826 enum ssl_client_hs_state_t state =
1827 static_cast<enum ssl_client_hs_state_t>(hs->state);
1828 switch (state) {
1829 case state_start_connect:
1830 return "TLS client start_connect";
1831 case state_enter_early_data:
1832 return "TLS client enter_early_data";
1833 case state_early_reverify_server_certificate:
1834 return "TLS client early_reverify_server_certificate";
1835 case state_read_hello_verify_request:
1836 return "TLS client read_hello_verify_request";
1837 case state_read_server_hello:
1838 return "TLS client read_server_hello";
1839 case state_tls13:
1840 return tls13_client_handshake_state(hs);
1841 case state_read_server_certificate:
1842 return "TLS client read_server_certificate";
1843 case state_read_certificate_status:
1844 return "TLS client read_certificate_status";
1845 case state_verify_server_certificate:
1846 return "TLS client verify_server_certificate";
1847 case state_reverify_server_certificate:
1848 return "TLS client reverify_server_certificate";
1849 case state_read_server_key_exchange:
1850 return "TLS client read_server_key_exchange";
1851 case state_read_certificate_request:
1852 return "TLS client read_certificate_request";
1853 case state_read_server_hello_done:
1854 return "TLS client read_server_hello_done";
1855 case state_send_client_certificate:
1856 return "TLS client send_client_certificate";
1857 case state_send_client_key_exchange:
1858 return "TLS client send_client_key_exchange";
1859 case state_send_client_certificate_verify:
1860 return "TLS client send_client_certificate_verify";
1861 case state_send_client_finished:
1862 return "TLS client send_client_finished";
1863 case state_finish_flight:
1864 return "TLS client finish_flight";
1865 case state_read_session_ticket:
1866 return "TLS client read_session_ticket";
1867 case state_process_change_cipher_spec:
1868 return "TLS client process_change_cipher_spec";
1869 case state_read_server_finished:
1870 return "TLS client read_server_finished";
1871 case state_finish_client_handshake:
1872 return "TLS client finish_client_handshake";
1873 case state_done:
1874 return "TLS client done";
1875 }
1876
1877 return "TLS client unknown";
1878 }
1879
1880 BSSL_NAMESPACE_END
1881