blob: ffd4a92eb1f38c0de6d6cb7b04f7cd8b78599faa [file] [log] [blame]
Steven Valdez4d71a9a2017-08-14 15:08:34 -04001/* 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-2002 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 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
110 * ECC cipher suite support in OpenSSL originally developed by
111 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. */
112
113#include <openssl/ssl.h>
114
115#include <assert.h>
116
David Benjamin879efc32017-09-21 11:20:53 -0400117#include <utility>
118
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400119#include "../crypto/internal.h"
120#include "internal.h"
121
122
123namespace bssl {
124
125SSL_HANDSHAKE::SSL_HANDSHAKE(SSL *ssl_arg)
126 : ssl(ssl_arg),
David Benjaminfd45ee72017-08-31 14:49:09 -0400127 scts_requested(false),
128 needs_psk_binder(false),
129 received_hello_retry_request(false),
130 received_custom_extension(false),
131 handshake_finalized(false),
132 accept_psk_mode(false),
133 cert_request(false),
134 certificate_status_expected(false),
135 ocsp_stapling_requested(false),
136 should_ack_sni(false),
137 in_false_start(false),
138 in_early_data(false),
139 early_data_offered(false),
140 can_early_read(false),
141 can_early_write(false),
142 next_proto_neg_seen(false),
143 ticket_expected(false),
144 extended_master_secret(false),
145 pending_private_key_op(false) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400146}
147
148SSL_HANDSHAKE::~SSL_HANDSHAKE() {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400149 OPENSSL_free(peer_sigalgs);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400150 ssl->ctx->x509_method->hs_flush_cached_ca_names(this);
Martin Kreichgauer6dc892f2017-08-30 10:49:05 -0700151 OPENSSL_free(key_block);
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400152}
153
154SSL_HANDSHAKE *ssl_handshake_new(SSL *ssl) {
155 UniquePtr<SSL_HANDSHAKE> hs = MakeUnique<SSL_HANDSHAKE>(ssl);
156 if (!hs ||
157 !hs->transcript.Init()) {
158 return nullptr;
159 }
160 return hs.release();
161}
162
163void ssl_handshake_free(SSL_HANDSHAKE *hs) { Delete(hs); }
164
165int ssl_check_message_type(SSL *ssl, const SSLMessage &msg, int type) {
166 if (msg.type != type) {
167 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
168 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
169 ERR_add_error_dataf("got type %d, wanted type %d", msg.type, type);
170 return 0;
171 }
172
173 return 1;
174}
175
176int ssl_add_message_cbb(SSL *ssl, CBB *cbb) {
David Benjamin879efc32017-09-21 11:20:53 -0400177 Array<uint8_t> msg;
178 if (!ssl->method->finish_message(ssl, cbb, &msg) ||
179 !ssl->method->add_message(ssl, std::move(msg))) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400180 return 0;
181 }
182
183 return 1;
184}
185
186size_t ssl_max_handshake_message_len(const SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400187 // kMaxMessageLen is the default maximum message size for handshakes which do
188 // not accept peer certificate chains.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400189 static const size_t kMaxMessageLen = 16384;
190
191 if (SSL_in_init(ssl)) {
192 if ((!ssl->server || (ssl->verify_mode & SSL_VERIFY_PEER)) &&
193 kMaxMessageLen < ssl->max_cert_list) {
194 return ssl->max_cert_list;
195 }
196 return kMaxMessageLen;
197 }
198
199 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400200 // In TLS 1.2 and below, the largest acceptable post-handshake message is
201 // a HelloRequest.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400202 return 0;
203 }
204
205 if (ssl->server) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400206 // The largest acceptable post-handshake message for a server is a
207 // KeyUpdate. We will never initiate post-handshake auth.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400208 return 1;
209 }
210
David Benjaminc11ea9422017-08-29 16:33:21 -0400211 // Clients must accept NewSessionTicket and CertificateRequest, so allow the
212 // default size.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400213 return kMaxMessageLen;
214}
215
216bool ssl_hash_message(SSL_HANDSHAKE *hs, const SSLMessage &msg) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400217 // V2ClientHello messages are pre-hashed.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400218 if (msg.is_v2_hello) {
219 return true;
220 }
221
222 return hs->transcript.Update(CBS_data(&msg.raw), CBS_len(&msg.raw));
223}
224
225int ssl_parse_extensions(const CBS *cbs, uint8_t *out_alert,
226 const SSL_EXTENSION_TYPE *ext_types,
227 size_t num_ext_types, int ignore_unknown) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400228 // Reset everything.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400229 for (size_t i = 0; i < num_ext_types; i++) {
230 *ext_types[i].out_present = 0;
231 CBS_init(ext_types[i].out_data, NULL, 0);
232 }
233
234 CBS copy = *cbs;
235 while (CBS_len(&copy) != 0) {
236 uint16_t type;
237 CBS data;
238 if (!CBS_get_u16(&copy, &type) ||
239 !CBS_get_u16_length_prefixed(&copy, &data)) {
240 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
241 *out_alert = SSL_AD_DECODE_ERROR;
242 return 0;
243 }
244
245 const SSL_EXTENSION_TYPE *ext_type = NULL;
246 for (size_t i = 0; i < num_ext_types; i++) {
247 if (type == ext_types[i].type) {
248 ext_type = &ext_types[i];
249 break;
250 }
251 }
252
253 if (ext_type == NULL) {
254 if (ignore_unknown) {
255 continue;
256 }
257 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
258 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
259 return 0;
260 }
261
David Benjaminc11ea9422017-08-29 16:33:21 -0400262 // Duplicate ext_types are forbidden.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400263 if (*ext_type->out_present) {
264 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_EXTENSION);
265 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
266 return 0;
267 }
268
269 *ext_type->out_present = 1;
270 *ext_type->out_data = data;
271 }
272
273 return 1;
274}
275
276static void set_crypto_buffer(CRYPTO_BUFFER **dest, CRYPTO_BUFFER *src) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400277 // TODO(davidben): Remove this helper once |SSL_SESSION| can use |UniquePtr|
278 // and |UniquePtr| has up_ref helpers.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400279 CRYPTO_BUFFER_free(*dest);
280 *dest = src;
281 if (src != nullptr) {
282 CRYPTO_BUFFER_up_ref(src);
283 }
284}
285
286enum ssl_verify_result_t ssl_verify_peer_cert(SSL_HANDSHAKE *hs) {
287 SSL *const ssl = hs->ssl;
288 const SSL_SESSION *prev_session = ssl->s3->established_session;
289 if (prev_session != NULL) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400290 // If renegotiating, the server must not change the server certificate. See
291 // https://mitls.org/pages/attacks/3SHAKE. We never resume on renegotiation,
292 // so this check is sufficient to ensure the reported peer certificate never
293 // changes on renegotiation.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400294 assert(!ssl->server);
295 if (sk_CRYPTO_BUFFER_num(prev_session->certs) !=
296 sk_CRYPTO_BUFFER_num(hs->new_session->certs)) {
297 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_CERT_CHANGED);
298 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
299 return ssl_verify_invalid;
300 }
301
302 for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(hs->new_session->certs); i++) {
303 const CRYPTO_BUFFER *old_cert =
304 sk_CRYPTO_BUFFER_value(prev_session->certs, i);
305 const CRYPTO_BUFFER *new_cert =
306 sk_CRYPTO_BUFFER_value(hs->new_session->certs, i);
307 if (CRYPTO_BUFFER_len(old_cert) != CRYPTO_BUFFER_len(new_cert) ||
308 OPENSSL_memcmp(CRYPTO_BUFFER_data(old_cert),
309 CRYPTO_BUFFER_data(new_cert),
310 CRYPTO_BUFFER_len(old_cert)) != 0) {
311 OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_CERT_CHANGED);
312 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
313 return ssl_verify_invalid;
314 }
315 }
316
David Benjaminc11ea9422017-08-29 16:33:21 -0400317 // The certificate is identical, so we may skip re-verifying the
318 // certificate. Since we only authenticated the previous one, copy other
319 // authentication from the established session and ignore what was newly
320 // received.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400321 set_crypto_buffer(&hs->new_session->ocsp_response,
322 prev_session->ocsp_response);
323 set_crypto_buffer(&hs->new_session->signed_cert_timestamp_list,
324 prev_session->signed_cert_timestamp_list);
325 hs->new_session->verify_result = prev_session->verify_result;
326 return ssl_verify_ok;
327 }
328
329 uint8_t alert = SSL_AD_CERTIFICATE_UNKNOWN;
330 enum ssl_verify_result_t ret;
331 if (ssl->custom_verify_callback != nullptr) {
332 ret = ssl->custom_verify_callback(ssl, &alert);
333 switch (ret) {
334 case ssl_verify_ok:
335 hs->new_session->verify_result = X509_V_OK;
336 break;
337 case ssl_verify_invalid:
338 hs->new_session->verify_result = X509_V_ERR_APPLICATION_VERIFICATION;
339 break;
340 case ssl_verify_retry:
341 break;
342 }
343 } else {
344 ret = ssl->ctx->x509_method->session_verify_cert_chain(
345 hs->new_session.get(), ssl, &alert)
346 ? ssl_verify_ok
347 : ssl_verify_invalid;
348 }
349
350 if (ret == ssl_verify_invalid) {
351 OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
352 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
353 }
354
355 return ret;
356}
357
358uint16_t ssl_get_grease_value(const SSL *ssl, enum ssl_grease_index_t index) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400359 // Use the client_random or server_random for entropy. This both avoids
360 // calling |RAND_bytes| on a single byte repeatedly and ensures the values are
361 // deterministic. This allows the same ClientHello be sent twice for a
362 // HelloRetryRequest or the same group be advertised in both supported_groups
363 // and key_shares.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400364 uint16_t ret = ssl->server ? ssl->s3->server_random[index]
365 : ssl->s3->client_random[index];
David Benjaminc11ea9422017-08-29 16:33:21 -0400366 // The first four bytes of server_random are a timestamp prior to TLS 1.3, but
367 // servers have no fields to GREASE until TLS 1.3.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400368 assert(!ssl->server || ssl3_protocol_version(ssl) >= TLS1_3_VERSION);
David Benjaminc11ea9422017-08-29 16:33:21 -0400369 // This generates a random value of the form 0xωaωa, for all 0 ≤ ω < 16.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400370 ret = (ret & 0xf0) | 0x0a;
371 ret |= ret << 8;
372 return ret;
373}
374
375enum ssl_hs_wait_t ssl_get_finished(SSL_HANDSHAKE *hs) {
376 SSL *const ssl = hs->ssl;
377 SSLMessage msg;
378 if (!ssl->method->get_message(ssl, &msg)) {
379 return ssl_hs_read_message;
380 }
381
382 if (!ssl_check_message_type(ssl, msg, SSL3_MT_FINISHED)) {
383 return ssl_hs_error;
384 }
385
David Benjaminc11ea9422017-08-29 16:33:21 -0400386 // Snapshot the finished hash before incorporating the new message.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400387 uint8_t finished[EVP_MAX_MD_SIZE];
388 size_t finished_len;
389 if (!hs->transcript.GetFinishedMAC(finished, &finished_len,
David Benjamin6abaa312017-08-31 01:12:28 -0400390 SSL_get_session(ssl), !ssl->server) ||
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400391 !ssl_hash_message(hs, msg)) {
392 return ssl_hs_error;
393 }
394
395 int finished_ok = CBS_mem_equal(&msg.body, finished, finished_len);
396#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
397 finished_ok = 1;
398#endif
399 if (!finished_ok) {
400 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
401 OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED);
402 return ssl_hs_error;
403 }
404
David Benjaminc11ea9422017-08-29 16:33:21 -0400405 // Copy the Finished so we can use it for renegotiation checks.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400406 if (ssl->version != SSL3_VERSION) {
407 if (finished_len > sizeof(ssl->s3->previous_client_finished) ||
408 finished_len > sizeof(ssl->s3->previous_server_finished)) {
409 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
410 return ssl_hs_error;
411 }
412
413 if (ssl->server) {
414 OPENSSL_memcpy(ssl->s3->previous_client_finished, finished, finished_len);
415 ssl->s3->previous_client_finished_len = finished_len;
416 } else {
417 OPENSSL_memcpy(ssl->s3->previous_server_finished, finished, finished_len);
418 ssl->s3->previous_server_finished_len = finished_len;
419 }
420 }
421
422 ssl->method->next_message(ssl);
423 return ssl_hs_ok;
424}
425
David Benjamin74795b32017-08-31 15:13:12 -0400426int ssl_run_handshake(SSL_HANDSHAKE *hs, bool *out_early_return) {
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400427 SSL *const ssl = hs->ssl;
428 for (;;) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400429 // Resolve the operation the handshake was waiting on.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400430 switch (hs->wait) {
431 case ssl_hs_error:
432 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
433 return -1;
434
435 case ssl_hs_flush: {
436 int ret = ssl->method->flush_flight(ssl);
437 if (ret <= 0) {
438 return ret;
439 }
440 break;
441 }
442
443 case ssl_hs_read_server_hello:
444 case ssl_hs_read_message: {
445 int ret = ssl->method->read_message(ssl);
446 if (ret <= 0) {
447 uint32_t err = ERR_peek_error();
448 if (hs->wait == ssl_hs_read_server_hello &&
449 ERR_GET_LIB(err) == ERR_LIB_SSL &&
450 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400451 // Add a dedicated error code to the queue for a handshake_failure
452 // alert in response to ClientHello. This matches NSS's client
453 // behavior and gives a better error on a (probable) failure to
454 // negotiate initial parameters. Note: this error code comes after
455 // the original one.
456 //
457 // See https://crbug.com/446505.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400458 OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
459 }
460 return ret;
461 }
462 break;
463 }
464
465 case ssl_hs_read_change_cipher_spec: {
466 int ret = ssl->method->read_change_cipher_spec(ssl);
467 if (ret <= 0) {
468 return ret;
469 }
470 break;
471 }
472
473 case ssl_hs_read_end_of_early_data: {
474 if (ssl->s3->hs->can_early_read) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400475 // While we are processing early data, the handshake returns early.
David Benjamin74795b32017-08-31 15:13:12 -0400476 *out_early_return = true;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400477 return 1;
478 }
479 hs->wait = ssl_hs_ok;
480 break;
481 }
482
483 case ssl_hs_certificate_selection_pending:
484 ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
485 hs->wait = ssl_hs_ok;
486 return -1;
487
488 case ssl_hs_x509_lookup:
489 ssl->rwstate = SSL_X509_LOOKUP;
490 hs->wait = ssl_hs_ok;
491 return -1;
492
493 case ssl_hs_channel_id_lookup:
494 ssl->rwstate = SSL_CHANNEL_ID_LOOKUP;
495 hs->wait = ssl_hs_ok;
496 return -1;
497
498 case ssl_hs_private_key_operation:
499 ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
500 hs->wait = ssl_hs_ok;
501 return -1;
502
503 case ssl_hs_pending_session:
504 ssl->rwstate = SSL_PENDING_SESSION;
505 hs->wait = ssl_hs_ok;
506 return -1;
507
508 case ssl_hs_pending_ticket:
509 ssl->rwstate = SSL_PENDING_TICKET;
510 hs->wait = ssl_hs_ok;
511 return -1;
512
513 case ssl_hs_certificate_verify:
514 ssl->rwstate = SSL_CERTIFICATE_VERIFY;
515 hs->wait = ssl_hs_ok;
516 return -1;
517
518 case ssl_hs_early_data_rejected:
519 ssl->rwstate = SSL_EARLY_DATA_REJECTED;
David Benjaminc11ea9422017-08-29 16:33:21 -0400520 // Cause |SSL_write| to start failing immediately.
David Benjaminfd45ee72017-08-31 14:49:09 -0400521 hs->can_early_write = false;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400522 return -1;
523
524 case ssl_hs_early_return:
David Benjamin74795b32017-08-31 15:13:12 -0400525 *out_early_return = true;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400526 hs->wait = ssl_hs_ok;
527 return 1;
528
529 case ssl_hs_ok:
530 break;
531 }
532
David Benjaminc11ea9422017-08-29 16:33:21 -0400533 // Run the state machine again.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400534 hs->wait = ssl->do_handshake(hs);
535 if (hs->wait == ssl_hs_error) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400536 // Don't loop around to avoid a stray |SSL_R_SSL_HANDSHAKE_FAILURE| the
537 // first time around.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400538 return -1;
539 }
540 if (hs->wait == ssl_hs_ok) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400541 // The handshake has completed.
David Benjamin74795b32017-08-31 15:13:12 -0400542 *out_early_return = false;
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400543 return 1;
544 }
545
David Benjaminc11ea9422017-08-29 16:33:21 -0400546 // Otherwise, loop to the beginning and resolve what was blocking the
547 // handshake.
Steven Valdez4d71a9a2017-08-14 15:08:34 -0400548 }
549}
550
551} // namespace bssl