25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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