handshake.cc 21 KB

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