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.
 
 
 
 
 
 

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