Home | History | Annotate | Download | only in ssl
      1 /* Copyright (C) 1995-1998 Eric Young (eay (at) cryptsoft.com)
      2  * All rights reserved.
      3  *
      4  * This package is an SSL implementation written
      5  * by Eric Young (eay (at) 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 (at) 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 (at) 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 (at) 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 (at) 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 (at) cryptsoft.com).  This product includes software written by Tim
    107  * Hudson (tjh (at) 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 
    117 #include <utility>
    118 
    119 #include <openssl/rand.h>
    120 
    121 #include "../crypto/internal.h"
    122 #include "internal.h"
    123 
    124 
    125 namespace bssl {
    126 
    127 SSL_HANDSHAKE::SSL_HANDSHAKE(SSL *ssl_arg)
    128     : ssl(ssl_arg),
    129       scts_requested(false),
    130       needs_psk_binder(false),
    131       received_hello_retry_request(false),
    132       sent_hello_retry_request(false),
    133       received_custom_extension(false),
    134       handshake_finalized(false),
    135       accept_psk_mode(false),
    136       cert_request(false),
    137       certificate_status_expected(false),
    138       ocsp_stapling_requested(false),
    139       should_ack_sni(false),
    140       in_false_start(false),
    141       in_early_data(false),
    142       early_data_offered(false),
    143       can_early_read(false),
    144       can_early_write(false),
    145       next_proto_neg_seen(false),
    146       ticket_expected(false),
    147       extended_master_secret(false),
    148       pending_private_key_op(false),
    149       grease_seeded(false) {
    150 }
    151 
    152 SSL_HANDSHAKE::~SSL_HANDSHAKE() {
    153   ssl->ctx->x509_method->hs_flush_cached_ca_names(this);
    154 }
    155 
    156 UniquePtr<SSL_HANDSHAKE> ssl_handshake_new(SSL *ssl) {
    157   UniquePtr<SSL_HANDSHAKE> hs = MakeUnique<SSL_HANDSHAKE>(ssl);
    158   if (!hs ||
    159       !hs->transcript.Init()) {
    160     return nullptr;
    161   }
    162   return hs;
    163 }
    164 
    165 bool ssl_check_message_type(SSL *ssl, const SSLMessage &msg, int type) {
    166   if (msg.type != type) {
    167     ssl_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 false;
    171   }
    172 
    173   return true;
    174 }
    175 
    176 bool ssl_add_message_cbb(SSL *ssl, CBB *cbb) {
    177   Array<uint8_t> msg;
    178   if (!ssl->method->finish_message(ssl, cbb, &msg) ||
    179       !ssl->method->add_message(ssl, std::move(msg))) {
    180     return false;
    181   }
    182 
    183   return true;
    184 }
    185 
    186 size_t ssl_max_handshake_message_len(const SSL *ssl) {
    187   // kMaxMessageLen is the default maximum message size for handshakes which do
    188   // not accept peer certificate chains.
    189   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 (ssl_protocol_version(ssl) < TLS1_3_VERSION) {
    200     // In TLS 1.2 and below, the largest acceptable post-handshake message is
    201     // a HelloRequest.
    202     return 0;
    203   }
    204 
    205   if (ssl->server) {
    206     // The largest acceptable post-handshake message for a server is a
    207     // KeyUpdate. We will never initiate post-handshake auth.
    208     return 1;
    209   }
    210 
    211   // Clients must accept NewSessionTicket, so allow the default size.
    212   return kMaxMessageLen;
    213 }
    214 
    215 bool ssl_hash_message(SSL_HANDSHAKE *hs, const SSLMessage &msg) {
    216   // V2ClientHello messages are pre-hashed.
    217   if (msg.is_v2_hello) {
    218     return true;
    219   }
    220 
    221   return hs->transcript.Update(msg.raw);
    222 }
    223 
    224 int ssl_parse_extensions(const CBS *cbs, uint8_t *out_alert,
    225                          const SSL_EXTENSION_TYPE *ext_types,
    226                          size_t num_ext_types, int ignore_unknown) {
    227   // Reset everything.
    228   for (size_t i = 0; i < num_ext_types; i++) {
    229     *ext_types[i].out_present = 0;
    230     CBS_init(ext_types[i].out_data, NULL, 0);
    231   }
    232 
    233   CBS copy = *cbs;
    234   while (CBS_len(&copy) != 0) {
    235     uint16_t type;
    236     CBS data;
    237     if (!CBS_get_u16(&copy, &type) ||
    238         !CBS_get_u16_length_prefixed(&copy, &data)) {
    239       OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
    240       *out_alert = SSL_AD_DECODE_ERROR;
    241       return 0;
    242     }
    243 
    244     const SSL_EXTENSION_TYPE *ext_type = NULL;
    245     for (size_t i = 0; i < num_ext_types; i++) {
    246       if (type == ext_types[i].type) {
    247         ext_type = &ext_types[i];
    248         break;
    249       }
    250     }
    251 
    252     if (ext_type == NULL) {
    253       if (ignore_unknown) {
    254         continue;
    255       }
    256       OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
    257       *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
    258       return 0;
    259     }
    260 
    261     // Duplicate ext_types are forbidden.
    262     if (*ext_type->out_present) {
    263       OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_EXTENSION);
    264       *out_alert = SSL_AD_ILLEGAL_PARAMETER;
    265       return 0;
    266     }
    267 
    268     *ext_type->out_present = 1;
    269     *ext_type->out_data = data;
    270   }
    271 
    272   return 1;
    273 }
    274 
    275 static void set_crypto_buffer(CRYPTO_BUFFER **dest, CRYPTO_BUFFER *src) {
    276   // TODO(davidben): Remove this helper once |SSL_SESSION| can use |UniquePtr|
    277   // and |UniquePtr| has up_ref helpers.
    278   CRYPTO_BUFFER_free(*dest);
    279   *dest = src;
    280   if (src != nullptr) {
    281     CRYPTO_BUFFER_up_ref(src);
    282   }
    283 }
    284 
    285 enum ssl_verify_result_t ssl_verify_peer_cert(SSL_HANDSHAKE *hs) {
    286   SSL *const ssl = hs->ssl;
    287   const SSL_SESSION *prev_session = ssl->s3->established_session.get();
    288   if (prev_session != NULL) {
    289     // If renegotiating, the server must not change the server certificate. See
    290     // https://mitls.org/pages/attacks/3SHAKE. We never resume on renegotiation,
    291     // so this check is sufficient to ensure the reported peer certificate never
    292     // changes on renegotiation.
    293     assert(!ssl->server);
    294     if (sk_CRYPTO_BUFFER_num(prev_session->certs) !=
    295         sk_CRYPTO_BUFFER_num(hs->new_session->certs)) {
    296       OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_CERT_CHANGED);
    297       ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
    298       return ssl_verify_invalid;
    299     }
    300 
    301     for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(hs->new_session->certs); i++) {
    302       const CRYPTO_BUFFER *old_cert =
    303           sk_CRYPTO_BUFFER_value(prev_session->certs, i);
    304       const CRYPTO_BUFFER *new_cert =
    305           sk_CRYPTO_BUFFER_value(hs->new_session->certs, i);
    306       if (CRYPTO_BUFFER_len(old_cert) != CRYPTO_BUFFER_len(new_cert) ||
    307           OPENSSL_memcmp(CRYPTO_BUFFER_data(old_cert),
    308                          CRYPTO_BUFFER_data(new_cert),
    309                          CRYPTO_BUFFER_len(old_cert)) != 0) {
    310         OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_CERT_CHANGED);
    311         ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
    312         return ssl_verify_invalid;
    313       }
    314     }
    315 
    316     // The certificate is identical, so we may skip re-verifying the
    317     // certificate. Since we only authenticated the previous one, copy other
    318     // authentication from the established session and ignore what was newly
    319     // received.
    320     set_crypto_buffer(&hs->new_session->ocsp_response,
    321                       prev_session->ocsp_response);
    322     set_crypto_buffer(&hs->new_session->signed_cert_timestamp_list,
    323                       prev_session->signed_cert_timestamp_list);
    324     hs->new_session->verify_result = prev_session->verify_result;
    325     return ssl_verify_ok;
    326   }
    327 
    328   uint8_t alert = SSL_AD_CERTIFICATE_UNKNOWN;
    329   enum ssl_verify_result_t ret;
    330   if (ssl->custom_verify_callback != nullptr) {
    331     ret = ssl->custom_verify_callback(ssl, &alert);
    332     switch (ret) {
    333       case ssl_verify_ok:
    334         hs->new_session->verify_result = X509_V_OK;
    335         break;
    336       case ssl_verify_invalid:
    337         // If |SSL_VERIFY_NONE|, the error is non-fatal, but we keep the result.
    338         if (ssl->verify_mode == SSL_VERIFY_NONE) {
    339           ERR_clear_error();
    340           ret = ssl_verify_ok;
    341         }
    342         hs->new_session->verify_result = X509_V_ERR_APPLICATION_VERIFICATION;
    343         break;
    344       case ssl_verify_retry:
    345         break;
    346     }
    347   } else {
    348     ret = ssl->ctx->x509_method->session_verify_cert_chain(
    349               hs->new_session.get(), ssl, &alert)
    350               ? ssl_verify_ok
    351               : ssl_verify_invalid;
    352   }
    353 
    354   if (ret == ssl_verify_invalid) {
    355     OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
    356     ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
    357   }
    358 
    359   return ret;
    360 }
    361 
    362 uint16_t ssl_get_grease_value(SSL_HANDSHAKE *hs,
    363                               enum ssl_grease_index_t index) {
    364   // Draw entropy for all GREASE values at once. This avoids calling
    365   // |RAND_bytes| repeatedly and makes the values consistent within a
    366   // connection. The latter is so the second ClientHello matches after
    367   // HelloRetryRequest and so supported_groups and key_shares are consistent.
    368   if (!hs->grease_seeded) {
    369     RAND_bytes(hs->grease_seed, sizeof(hs->grease_seed));
    370     hs->grease_seeded = true;
    371   }
    372 
    373   // This generates a random value of the form 0xaa, for all 0   < 16.
    374   uint16_t ret = hs->grease_seed[index];
    375   ret = (ret & 0xf0) | 0x0a;
    376   ret |= ret << 8;
    377   return ret;
    378 }
    379 
    380 enum ssl_hs_wait_t ssl_get_finished(SSL_HANDSHAKE *hs) {
    381   SSL *const ssl = hs->ssl;
    382   SSLMessage msg;
    383   if (!ssl->method->get_message(ssl, &msg)) {
    384     return ssl_hs_read_message;
    385   }
    386 
    387   if (!ssl_check_message_type(ssl, msg, SSL3_MT_FINISHED)) {
    388     return ssl_hs_error;
    389   }
    390 
    391   // Snapshot the finished hash before incorporating the new message.
    392   uint8_t finished[EVP_MAX_MD_SIZE];
    393   size_t finished_len;
    394   if (!hs->transcript.GetFinishedMAC(finished, &finished_len,
    395                                      SSL_get_session(ssl), !ssl->server) ||
    396       !ssl_hash_message(hs, msg)) {
    397     return ssl_hs_error;
    398   }
    399 
    400   int finished_ok = CBS_mem_equal(&msg.body, finished, finished_len);
    401 #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
    402   finished_ok = 1;
    403 #endif
    404   if (!finished_ok) {
    405     ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
    406     OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED);
    407     return ssl_hs_error;
    408   }
    409 
    410   // Copy the Finished so we can use it for renegotiation checks.
    411   if (ssl->version != SSL3_VERSION) {
    412     if (finished_len > sizeof(ssl->s3->previous_client_finished) ||
    413         finished_len > sizeof(ssl->s3->previous_server_finished)) {
    414       OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
    415       return ssl_hs_error;
    416     }
    417 
    418     if (ssl->server) {
    419       OPENSSL_memcpy(ssl->s3->previous_client_finished, finished, finished_len);
    420       ssl->s3->previous_client_finished_len = finished_len;
    421     } else {
    422       OPENSSL_memcpy(ssl->s3->previous_server_finished, finished, finished_len);
    423       ssl->s3->previous_server_finished_len = finished_len;
    424     }
    425   }
    426 
    427   ssl->method->next_message(ssl);
    428   return ssl_hs_ok;
    429 }
    430 
    431 bool ssl_send_finished(SSL_HANDSHAKE *hs) {
    432   SSL *const ssl = hs->ssl;
    433   const SSL_SESSION *session = SSL_get_session(ssl);
    434 
    435   uint8_t finished[EVP_MAX_MD_SIZE];
    436   size_t finished_len;
    437   if (!hs->transcript.GetFinishedMAC(finished, &finished_len, session,
    438                                      ssl->server)) {
    439     return 0;
    440   }
    441 
    442   // Log the master secret, if logging is enabled.
    443   if (!ssl_log_secret(ssl, "CLIENT_RANDOM",
    444                       session->master_key,
    445                       session->master_key_length)) {
    446     return 0;
    447   }
    448 
    449   // Copy the Finished so we can use it for renegotiation checks.
    450   if (ssl->version != SSL3_VERSION) {
    451     if (finished_len > sizeof(ssl->s3->previous_client_finished) ||
    452         finished_len > sizeof(ssl->s3->previous_server_finished)) {
    453       OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
    454       return 0;
    455     }
    456 
    457     if (ssl->server) {
    458       OPENSSL_memcpy(ssl->s3->previous_server_finished, finished, finished_len);
    459       ssl->s3->previous_server_finished_len = finished_len;
    460     } else {
    461       OPENSSL_memcpy(ssl->s3->previous_client_finished, finished, finished_len);
    462       ssl->s3->previous_client_finished_len = finished_len;
    463     }
    464   }
    465 
    466   ScopedCBB cbb;
    467   CBB body;
    468   if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_FINISHED) ||
    469       !CBB_add_bytes(&body, finished, finished_len) ||
    470       !ssl_add_message_cbb(ssl, cbb.get())) {
    471     OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
    472     return 0;
    473   }
    474 
    475   return 1;
    476 }
    477 
    478 bool ssl_output_cert_chain(SSL *ssl) {
    479   ScopedCBB cbb;
    480   CBB body;
    481   if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CERTIFICATE) ||
    482       !ssl_add_cert_chain(ssl, &body) ||
    483       !ssl_add_message_cbb(ssl, cbb.get())) {
    484     OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
    485     return false;
    486   }
    487 
    488   return true;
    489 }
    490 
    491 int ssl_run_handshake(SSL_HANDSHAKE *hs, bool *out_early_return) {
    492   SSL *const ssl = hs->ssl;
    493   for (;;) {
    494     // Resolve the operation the handshake was waiting on.
    495     switch (hs->wait) {
    496       case ssl_hs_error:
    497         ERR_restore_state(hs->error.get());
    498         return -1;
    499 
    500       case ssl_hs_flush: {
    501         int ret = ssl->method->flush_flight(ssl);
    502         if (ret <= 0) {
    503           return ret;
    504         }
    505         break;
    506       }
    507 
    508       case ssl_hs_read_server_hello:
    509       case ssl_hs_read_message:
    510       case ssl_hs_read_change_cipher_spec: {
    511         uint8_t alert = SSL_AD_DECODE_ERROR;
    512         size_t consumed = 0;
    513         ssl_open_record_t ret;
    514         if (hs->wait == ssl_hs_read_change_cipher_spec) {
    515           ret = ssl_open_change_cipher_spec(ssl, &consumed, &alert,
    516                                             ssl->s3->read_buffer.span());
    517         } else {
    518           ret = ssl_open_handshake(ssl, &consumed, &alert,
    519                                    ssl->s3->read_buffer.span());
    520         }
    521         if (ret == ssl_open_record_error &&
    522             hs->wait == ssl_hs_read_server_hello) {
    523           uint32_t err = ERR_peek_error();
    524           if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
    525               ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
    526             // Add a dedicated error code to the queue for a handshake_failure
    527             // alert in response to ClientHello. This matches NSS's client
    528             // behavior and gives a better error on a (probable) failure to
    529             // negotiate initial parameters. Note: this error code comes after
    530             // the original one.
    531             //
    532             // See https://crbug.com/446505.
    533             OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
    534           }
    535         }
    536         bool retry;
    537         int bio_ret = ssl_handle_open_record(ssl, &retry, ret, consumed, alert);
    538         if (bio_ret <= 0) {
    539           return bio_ret;
    540         }
    541         if (retry) {
    542           continue;
    543         }
    544         ssl->s3->read_buffer.DiscardConsumed();
    545         break;
    546       }
    547 
    548       case ssl_hs_read_end_of_early_data: {
    549         if (ssl->s3->hs->can_early_read) {
    550           // While we are processing early data, the handshake returns early.
    551           *out_early_return = true;
    552           return 1;
    553         }
    554         hs->wait = ssl_hs_ok;
    555         break;
    556       }
    557 
    558       case ssl_hs_certificate_selection_pending:
    559         ssl->s3->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
    560         hs->wait = ssl_hs_ok;
    561         return -1;
    562 
    563       case ssl_hs_handoff:
    564         ssl->s3->rwstate = SSL_HANDOFF;
    565         hs->wait = ssl_hs_ok;
    566         return -1;
    567 
    568       case ssl_hs_x509_lookup:
    569         ssl->s3->rwstate = SSL_X509_LOOKUP;
    570         hs->wait = ssl_hs_ok;
    571         return -1;
    572 
    573       case ssl_hs_channel_id_lookup:
    574         ssl->s3->rwstate = SSL_CHANNEL_ID_LOOKUP;
    575         hs->wait = ssl_hs_ok;
    576         return -1;
    577 
    578       case ssl_hs_private_key_operation:
    579         ssl->s3->rwstate = SSL_PRIVATE_KEY_OPERATION;
    580         hs->wait = ssl_hs_ok;
    581         return -1;
    582 
    583       case ssl_hs_pending_session:
    584         ssl->s3->rwstate = SSL_PENDING_SESSION;
    585         hs->wait = ssl_hs_ok;
    586         return -1;
    587 
    588       case ssl_hs_pending_ticket:
    589         ssl->s3->rwstate = SSL_PENDING_TICKET;
    590         hs->wait = ssl_hs_ok;
    591         return -1;
    592 
    593       case ssl_hs_certificate_verify:
    594         ssl->s3->rwstate = SSL_CERTIFICATE_VERIFY;
    595         hs->wait = ssl_hs_ok;
    596         return -1;
    597 
    598       case ssl_hs_early_data_rejected:
    599         ssl->s3->rwstate = SSL_EARLY_DATA_REJECTED;
    600         // Cause |SSL_write| to start failing immediately.
    601         hs->can_early_write = false;
    602         return -1;
    603 
    604       case ssl_hs_early_return:
    605         *out_early_return = true;
    606         hs->wait = ssl_hs_ok;
    607         return 1;
    608 
    609       case ssl_hs_ok:
    610         break;
    611     }
    612 
    613     // Run the state machine again.
    614     hs->wait = ssl->do_handshake(hs);
    615     if (hs->wait == ssl_hs_error) {
    616       hs->error.reset(ERR_save_state());
    617       return -1;
    618     }
    619     if (hs->wait == ssl_hs_ok) {
    620       // The handshake has completed.
    621       *out_early_return = false;
    622       return 1;
    623     }
    624 
    625     // Otherwise, loop to the beginning and resolve what was blocking the
    626     // handshake.
    627   }
    628 }
    629 
    630 }  // namespace bssl
    631