You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

636 lines
22 KiB

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