Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

1675 linhas
56 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-2007 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. */
  110. /* ====================================================================
  111. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  112. *
  113. * Portions of the attached software ("Contribution") are developed by
  114. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  115. *
  116. * The Contribution is licensed pursuant to the OpenSSL open source
  117. * license provided above.
  118. *
  119. * ECC cipher suite support in OpenSSL originally written by
  120. * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
  121. *
  122. */
  123. /* ====================================================================
  124. * Copyright 2005 Nokia. All rights reserved.
  125. *
  126. * The portions of the attached software ("Contribution") is developed by
  127. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  128. * license.
  129. *
  130. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  131. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  132. * support (see RFC 4279) to OpenSSL.
  133. *
  134. * No patent licenses or other rights except those expressly stated in
  135. * the OpenSSL open source license shall be deemed granted or received
  136. * expressly, by implication, estoppel, or otherwise.
  137. *
  138. * No assurances are provided by Nokia that the Contribution does not
  139. * infringe the patent or other intellectual property rights of any third
  140. * party or that the license provides you with all the necessary rights
  141. * to make use of the Contribution.
  142. *
  143. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  144. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  145. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  146. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  147. * OTHERWISE. */
  148. #include <openssl/ssl.h>
  149. #include <assert.h>
  150. #include <string.h>
  151. #include <openssl/bn.h>
  152. #include <openssl/buf.h>
  153. #include <openssl/bytestring.h>
  154. #include <openssl/cipher.h>
  155. #include <openssl/ec.h>
  156. #include <openssl/ecdsa.h>
  157. #include <openssl/err.h>
  158. #include <openssl/evp.h>
  159. #include <openssl/hmac.h>
  160. #include <openssl/md5.h>
  161. #include <openssl/mem.h>
  162. #include <openssl/nid.h>
  163. #include <openssl/rand.h>
  164. #include <openssl/x509.h>
  165. #include "internal.h"
  166. #include "../crypto/internal.h"
  167. namespace bssl {
  168. enum ssl_server_hs_state_t {
  169. state_start_accept = 0,
  170. state_read_client_hello,
  171. state_select_certificate,
  172. state_tls13,
  173. state_select_parameters,
  174. state_send_server_hello,
  175. state_send_server_certificate,
  176. state_send_server_key_exchange,
  177. state_send_server_hello_done,
  178. state_read_client_certificate,
  179. state_verify_client_certificate,
  180. state_read_client_key_exchange,
  181. state_read_client_certificate_verify,
  182. state_read_change_cipher_spec,
  183. state_process_change_cipher_spec,
  184. state_read_next_proto,
  185. state_read_channel_id,
  186. state_read_client_finished,
  187. state_send_server_finished,
  188. state_finish_server_handshake,
  189. state_done,
  190. };
  191. int ssl_client_cipher_list_contains_cipher(const SSL_CLIENT_HELLO *client_hello,
  192. uint16_t id) {
  193. CBS cipher_suites;
  194. CBS_init(&cipher_suites, client_hello->cipher_suites,
  195. client_hello->cipher_suites_len);
  196. while (CBS_len(&cipher_suites) > 0) {
  197. uint16_t got_id;
  198. if (!CBS_get_u16(&cipher_suites, &got_id)) {
  199. return 0;
  200. }
  201. if (got_id == id) {
  202. return 1;
  203. }
  204. }
  205. return 0;
  206. }
  207. static int negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  208. const SSL_CLIENT_HELLO *client_hello) {
  209. SSL *const ssl = hs->ssl;
  210. assert(!ssl->s3->have_version);
  211. CBS supported_versions, versions;
  212. if (ssl_client_hello_get_extension(client_hello, &supported_versions,
  213. TLSEXT_TYPE_supported_versions)) {
  214. if (!CBS_get_u8_length_prefixed(&supported_versions, &versions) ||
  215. CBS_len(&supported_versions) != 0 ||
  216. CBS_len(&versions) == 0) {
  217. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  218. *out_alert = SSL_AD_DECODE_ERROR;
  219. return 0;
  220. }
  221. } else {
  222. // Convert the ClientHello version to an equivalent supported_versions
  223. // extension.
  224. static const uint8_t kTLSVersions[] = {
  225. 0x03, 0x03, // TLS 1.2
  226. 0x03, 0x02, // TLS 1.1
  227. 0x03, 0x01, // TLS 1
  228. 0x03, 0x00, // SSL 3
  229. };
  230. static const uint8_t kDTLSVersions[] = {
  231. 0xfe, 0xfd, // DTLS 1.2
  232. 0xfe, 0xff, // DTLS 1.0
  233. };
  234. size_t versions_len = 0;
  235. if (SSL_is_dtls(ssl)) {
  236. if (client_hello->version <= DTLS1_2_VERSION) {
  237. versions_len = 4;
  238. } else if (client_hello->version <= DTLS1_VERSION) {
  239. versions_len = 2;
  240. }
  241. CBS_init(&versions, kDTLSVersions + sizeof(kDTLSVersions) - versions_len,
  242. versions_len);
  243. } else {
  244. if (client_hello->version >= TLS1_2_VERSION) {
  245. versions_len = 8;
  246. } else if (client_hello->version >= TLS1_1_VERSION) {
  247. versions_len = 6;
  248. } else if (client_hello->version >= TLS1_VERSION) {
  249. versions_len = 4;
  250. } else if (client_hello->version >= SSL3_VERSION) {
  251. versions_len = 2;
  252. }
  253. CBS_init(&versions, kTLSVersions + sizeof(kTLSVersions) - versions_len,
  254. versions_len);
  255. }
  256. }
  257. if (!ssl_negotiate_version(hs, out_alert, &ssl->version, &versions)) {
  258. return 0;
  259. }
  260. // At this point, the connection's version is known and |ssl->version| is
  261. // fixed. Begin enforcing the record-layer version.
  262. ssl->s3->have_version = true;
  263. ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version);
  264. // Handle FALLBACK_SCSV.
  265. if (ssl_client_cipher_list_contains_cipher(client_hello,
  266. SSL3_CK_FALLBACK_SCSV & 0xffff) &&
  267. ssl_protocol_version(ssl) < hs->max_version) {
  268. OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
  269. *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK;
  270. return 0;
  271. }
  272. return 1;
  273. }
  274. static UniquePtr<STACK_OF(SSL_CIPHER)> ssl_parse_client_cipher_list(
  275. const SSL_CLIENT_HELLO *client_hello) {
  276. CBS cipher_suites;
  277. CBS_init(&cipher_suites, client_hello->cipher_suites,
  278. client_hello->cipher_suites_len);
  279. UniquePtr<STACK_OF(SSL_CIPHER)> sk(sk_SSL_CIPHER_new_null());
  280. if (!sk) {
  281. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  282. return nullptr;
  283. }
  284. while (CBS_len(&cipher_suites) > 0) {
  285. uint16_t cipher_suite;
  286. if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
  287. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
  288. return nullptr;
  289. }
  290. const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
  291. if (c != NULL && !sk_SSL_CIPHER_push(sk.get(), c)) {
  292. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  293. return nullptr;
  294. }
  295. }
  296. return sk;
  297. }
  298. // ssl_get_compatible_server_ciphers determines the key exchange and
  299. // authentication cipher suite masks compatible with the server configuration
  300. // and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key
  301. // exchange mask and |*out_mask_a| to the authentication mask.
  302. static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs,
  303. uint32_t *out_mask_k,
  304. uint32_t *out_mask_a) {
  305. SSL *const ssl = hs->ssl;
  306. uint32_t mask_k = 0;
  307. uint32_t mask_a = 0;
  308. if (ssl_has_certificate(ssl)) {
  309. mask_a |= ssl_cipher_auth_mask_for_key(hs->local_pubkey.get());
  310. if (EVP_PKEY_id(hs->local_pubkey.get()) == EVP_PKEY_RSA) {
  311. mask_k |= SSL_kRSA;
  312. }
  313. }
  314. // Check for a shared group to consider ECDHE ciphers.
  315. uint16_t unused;
  316. if (tls1_get_shared_group(hs, &unused)) {
  317. mask_k |= SSL_kECDHE;
  318. }
  319. // PSK requires a server callback.
  320. if (ssl->psk_server_callback != NULL) {
  321. mask_k |= SSL_kPSK;
  322. mask_a |= SSL_aPSK;
  323. }
  324. *out_mask_k = mask_k;
  325. *out_mask_a = mask_a;
  326. }
  327. static const SSL_CIPHER *ssl3_choose_cipher(
  328. SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello,
  329. const struct ssl_cipher_preference_list_st *server_pref) {
  330. SSL *const ssl = hs->ssl;
  331. STACK_OF(SSL_CIPHER) *prio, *allow;
  332. // in_group_flags will either be NULL, or will point to an array of bytes
  333. // which indicate equal-preference groups in the |prio| stack. See the
  334. // comment about |in_group_flags| in the |ssl_cipher_preference_list_st|
  335. // struct.
  336. const uint8_t *in_group_flags;
  337. // group_min contains the minimal index so far found in a group, or -1 if no
  338. // such value exists yet.
  339. int group_min = -1;
  340. UniquePtr<STACK_OF(SSL_CIPHER)> client_pref =
  341. ssl_parse_client_cipher_list(client_hello);
  342. if (!client_pref) {
  343. return nullptr;
  344. }
  345. if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
  346. prio = server_pref->ciphers;
  347. in_group_flags = server_pref->in_group_flags;
  348. allow = client_pref.get();
  349. } else {
  350. prio = client_pref.get();
  351. in_group_flags = NULL;
  352. allow = server_pref->ciphers;
  353. }
  354. uint32_t mask_k, mask_a;
  355. ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a);
  356. for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
  357. const SSL_CIPHER *c = sk_SSL_CIPHER_value(prio, i);
  358. size_t cipher_index;
  359. if (// Check if the cipher is supported for the current version.
  360. SSL_CIPHER_get_min_version(c) <= ssl_protocol_version(ssl) &&
  361. ssl_protocol_version(ssl) <= SSL_CIPHER_get_max_version(c) &&
  362. // Check the cipher is supported for the server configuration.
  363. (c->algorithm_mkey & mask_k) &&
  364. (c->algorithm_auth & mask_a) &&
  365. // Check the cipher is in the |allow| list.
  366. sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
  367. if (in_group_flags != NULL && in_group_flags[i] == 1) {
  368. // This element of |prio| is in a group. Update the minimum index found
  369. // so far and continue looking.
  370. if (group_min == -1 || (size_t)group_min > cipher_index) {
  371. group_min = cipher_index;
  372. }
  373. } else {
  374. if (group_min != -1 && (size_t)group_min < cipher_index) {
  375. cipher_index = group_min;
  376. }
  377. return sk_SSL_CIPHER_value(allow, cipher_index);
  378. }
  379. }
  380. if (in_group_flags != NULL && in_group_flags[i] == 0 && group_min != -1) {
  381. // We are about to leave a group, but we found a match in it, so that's
  382. // our answer.
  383. return sk_SSL_CIPHER_value(allow, group_min);
  384. }
  385. }
  386. return nullptr;
  387. }
  388. static enum ssl_hs_wait_t do_start_accept(SSL_HANDSHAKE *hs) {
  389. ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_START, 1);
  390. hs->state = state_read_client_hello;
  391. return ssl_hs_ok;
  392. }
  393. static enum ssl_hs_wait_t do_read_client_hello(SSL_HANDSHAKE *hs) {
  394. SSL *const ssl = hs->ssl;
  395. SSLMessage msg;
  396. if (!ssl->method->get_message(ssl, &msg)) {
  397. return ssl_hs_read_message;
  398. }
  399. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_HELLO)) {
  400. return ssl_hs_error;
  401. }
  402. if (ssl->handoff) {
  403. return ssl_hs_handoff;
  404. }
  405. SSL_CLIENT_HELLO client_hello;
  406. if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
  407. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  408. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  409. return ssl_hs_error;
  410. }
  411. // Run the early callback.
  412. if (ssl->ctx->select_certificate_cb != NULL) {
  413. switch (ssl->ctx->select_certificate_cb(&client_hello)) {
  414. case ssl_select_cert_retry:
  415. return ssl_hs_certificate_selection_pending;
  416. case ssl_select_cert_error:
  417. // Connection rejected.
  418. OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
  419. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  420. return ssl_hs_error;
  421. default:
  422. /* fallthrough */;
  423. }
  424. }
  425. // Freeze the version range after the early callback.
  426. if (!ssl_get_version_range(ssl, &hs->min_version, &hs->max_version)) {
  427. return ssl_hs_error;
  428. }
  429. uint8_t alert = SSL_AD_DECODE_ERROR;
  430. if (!negotiate_version(hs, &alert, &client_hello)) {
  431. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  432. return ssl_hs_error;
  433. }
  434. hs->client_version = client_hello.version;
  435. if (client_hello.random_len != SSL3_RANDOM_SIZE) {
  436. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  437. return ssl_hs_error;
  438. }
  439. OPENSSL_memcpy(ssl->s3->client_random, client_hello.random,
  440. client_hello.random_len);
  441. // Only null compression is supported. TLS 1.3 further requires the peer
  442. // advertise no other compression.
  443. if (OPENSSL_memchr(client_hello.compression_methods, 0,
  444. client_hello.compression_methods_len) == NULL ||
  445. (ssl_protocol_version(ssl) >= TLS1_3_VERSION &&
  446. client_hello.compression_methods_len != 1)) {
  447. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMPRESSION_LIST);
  448. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  449. return ssl_hs_error;
  450. }
  451. // TLS extensions.
  452. if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) {
  453. OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
  454. return ssl_hs_error;
  455. }
  456. hs->state = state_select_certificate;
  457. return ssl_hs_ok;
  458. }
  459. static enum ssl_hs_wait_t do_select_certificate(SSL_HANDSHAKE *hs) {
  460. SSL *const ssl = hs->ssl;
  461. SSLMessage msg;
  462. if (!ssl->method->get_message(ssl, &msg)) {
  463. return ssl_hs_read_message;
  464. }
  465. // Call |cert_cb| to update server certificates if required.
  466. if (ssl->cert->cert_cb != NULL) {
  467. int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
  468. if (rv == 0) {
  469. OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
  470. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  471. return ssl_hs_error;
  472. }
  473. if (rv < 0) {
  474. return ssl_hs_x509_lookup;
  475. }
  476. }
  477. if (!ssl_on_certificate_selected(hs)) {
  478. return ssl_hs_error;
  479. }
  480. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  481. // Jump to the TLS 1.3 state machine.
  482. hs->state = state_tls13;
  483. return ssl_hs_ok;
  484. }
  485. SSL_CLIENT_HELLO client_hello;
  486. if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
  487. return ssl_hs_error;
  488. }
  489. // Negotiate the cipher suite. This must be done after |cert_cb| so the
  490. // certificate is finalized.
  491. hs->new_cipher =
  492. ssl3_choose_cipher(hs, &client_hello, ssl_get_cipher_preferences(ssl));
  493. if (hs->new_cipher == NULL) {
  494. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
  495. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  496. return ssl_hs_error;
  497. }
  498. hs->state = state_select_parameters;
  499. return ssl_hs_ok;
  500. }
  501. static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) {
  502. enum ssl_hs_wait_t wait = tls13_server_handshake(hs);
  503. if (wait == ssl_hs_ok) {
  504. hs->state = state_finish_server_handshake;
  505. return ssl_hs_ok;
  506. }
  507. return wait;
  508. }
  509. static enum ssl_hs_wait_t do_select_parameters(SSL_HANDSHAKE *hs) {
  510. SSL *const ssl = hs->ssl;
  511. SSLMessage msg;
  512. if (!ssl->method->get_message(ssl, &msg)) {
  513. return ssl_hs_read_message;
  514. }
  515. SSL_CLIENT_HELLO client_hello;
  516. if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
  517. return ssl_hs_error;
  518. }
  519. // Determine whether we are doing session resumption.
  520. UniquePtr<SSL_SESSION> session;
  521. bool tickets_supported = false, renew_ticket = false;
  522. enum ssl_hs_wait_t wait = ssl_get_prev_session(
  523. ssl, &session, &tickets_supported, &renew_ticket, &client_hello);
  524. if (wait != ssl_hs_ok) {
  525. return wait;
  526. }
  527. if (session) {
  528. if (session->extended_master_secret && !hs->extended_master_secret) {
  529. // A ClientHello without EMS that attempts to resume a session with EMS
  530. // is fatal to the connection.
  531. OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
  532. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  533. return ssl_hs_error;
  534. }
  535. if (!ssl_session_is_resumable(hs, session.get()) ||
  536. // If the client offers the EMS extension, but the previous session
  537. // didn't use it, then negotiate a new session.
  538. hs->extended_master_secret != session->extended_master_secret) {
  539. session.reset();
  540. }
  541. }
  542. if (session) {
  543. // Use the old session.
  544. hs->ticket_expected = renew_ticket;
  545. ssl->session = session.release();
  546. ssl->s3->session_reused = true;
  547. } else {
  548. hs->ticket_expected = tickets_supported;
  549. ssl_set_session(ssl, NULL);
  550. if (!ssl_get_new_session(hs, 1 /* server */)) {
  551. return ssl_hs_error;
  552. }
  553. // Clear the session ID if we want the session to be single-use.
  554. if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
  555. hs->new_session->session_id_length = 0;
  556. }
  557. }
  558. if (ssl->ctx->dos_protection_cb != NULL &&
  559. ssl->ctx->dos_protection_cb(&client_hello) == 0) {
  560. // Connection rejected for DOS reasons.
  561. OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
  562. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  563. return ssl_hs_error;
  564. }
  565. if (ssl->session == NULL) {
  566. hs->new_session->cipher = hs->new_cipher;
  567. // Determine whether to request a client certificate.
  568. hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
  569. // Only request a certificate if Channel ID isn't negotiated.
  570. if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
  571. ssl->s3->tlsext_channel_id_valid) {
  572. hs->cert_request = false;
  573. }
  574. // CertificateRequest may only be sent in certificate-based ciphers.
  575. if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  576. hs->cert_request = false;
  577. }
  578. if (!hs->cert_request) {
  579. // OpenSSL returns X509_V_OK when no certificates are requested. This is
  580. // classed by them as a bug, but it's assumed by at least NGINX.
  581. hs->new_session->verify_result = X509_V_OK;
  582. }
  583. }
  584. // HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
  585. // deferred. Complete it now.
  586. uint8_t alert = SSL_AD_DECODE_ERROR;
  587. if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) {
  588. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  589. return ssl_hs_error;
  590. }
  591. // Now that all parameters are known, initialize the handshake hash and hash
  592. // the ClientHello.
  593. if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) ||
  594. !ssl_hash_message(hs, msg)) {
  595. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  596. return ssl_hs_error;
  597. }
  598. // Release the handshake buffer if client authentication isn't required.
  599. if (!hs->cert_request) {
  600. hs->transcript.FreeBuffer();
  601. }
  602. ssl->method->next_message(ssl);
  603. hs->state = state_send_server_hello;
  604. return ssl_hs_ok;
  605. }
  606. static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) {
  607. SSL *const ssl = hs->ssl;
  608. // We only accept ChannelIDs on connections with ECDHE in order to avoid a
  609. // known attack while we fix ChannelID itself.
  610. if (ssl->s3->tlsext_channel_id_valid &&
  611. (hs->new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
  612. ssl->s3->tlsext_channel_id_valid = false;
  613. }
  614. // If this is a resumption and the original handshake didn't support
  615. // ChannelID then we didn't record the original handshake hashes in the
  616. // session and so cannot resume with ChannelIDs.
  617. if (ssl->session != NULL &&
  618. ssl->session->original_handshake_hash_len == 0) {
  619. ssl->s3->tlsext_channel_id_valid = false;
  620. }
  621. struct OPENSSL_timeval now;
  622. ssl_get_current_time(ssl, &now);
  623. ssl->s3->server_random[0] = now.tv_sec >> 24;
  624. ssl->s3->server_random[1] = now.tv_sec >> 16;
  625. ssl->s3->server_random[2] = now.tv_sec >> 8;
  626. ssl->s3->server_random[3] = now.tv_sec;
  627. if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
  628. return ssl_hs_error;
  629. }
  630. // Implement the TLS 1.3 anti-downgrade feature, but with a different value.
  631. //
  632. // For draft TLS 1.3 versions, it is not safe to deploy this feature. However,
  633. // some TLS terminators are non-compliant and copy the origin server's value,
  634. // so we wish to measure eventual compatibility impact.
  635. if (hs->max_version >= TLS1_3_VERSION) {
  636. OPENSSL_memcpy(ssl->s3->server_random + SSL3_RANDOM_SIZE -
  637. sizeof(kDraftDowngradeRandom),
  638. kDraftDowngradeRandom, sizeof(kDraftDowngradeRandom));
  639. }
  640. const SSL_SESSION *session = hs->new_session.get();
  641. if (ssl->session != NULL) {
  642. session = ssl->session;
  643. }
  644. ScopedCBB cbb;
  645. CBB body, session_id;
  646. if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) ||
  647. !CBB_add_u16(&body, ssl->version) ||
  648. !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
  649. !CBB_add_u8_length_prefixed(&body, &session_id) ||
  650. !CBB_add_bytes(&session_id, session->session_id,
  651. session->session_id_length) ||
  652. !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) ||
  653. !CBB_add_u8(&body, 0 /* no compression */) ||
  654. !ssl_add_serverhello_tlsext(hs, &body) ||
  655. !ssl_add_message_cbb(ssl, cbb.get())) {
  656. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  657. return ssl_hs_error;
  658. }
  659. if (ssl->session != NULL) {
  660. hs->state = state_send_server_finished;
  661. } else {
  662. hs->state = state_send_server_certificate;
  663. }
  664. return ssl_hs_ok;
  665. }
  666. static enum ssl_hs_wait_t do_send_server_certificate(SSL_HANDSHAKE *hs) {
  667. SSL *const ssl = hs->ssl;
  668. ScopedCBB cbb;
  669. if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  670. if (!ssl_has_certificate(ssl)) {
  671. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
  672. return ssl_hs_error;
  673. }
  674. if (!ssl_output_cert_chain(ssl)) {
  675. return ssl_hs_error;
  676. }
  677. if (hs->certificate_status_expected) {
  678. CBB body, ocsp_response;
  679. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  680. SSL3_MT_CERTIFICATE_STATUS) ||
  681. !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
  682. !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
  683. !CBB_add_bytes(&ocsp_response,
  684. CRYPTO_BUFFER_data(ssl->cert->ocsp_response),
  685. CRYPTO_BUFFER_len(ssl->cert->ocsp_response)) ||
  686. !ssl_add_message_cbb(ssl, cbb.get())) {
  687. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  688. return ssl_hs_error;
  689. }
  690. }
  691. }
  692. // Assemble ServerKeyExchange parameters if needed.
  693. uint32_t alg_k = hs->new_cipher->algorithm_mkey;
  694. uint32_t alg_a = hs->new_cipher->algorithm_auth;
  695. if (ssl_cipher_requires_server_key_exchange(hs->new_cipher) ||
  696. ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
  697. // Pre-allocate enough room to comfortably fit an ECDHE public key. Prepend
  698. // the client and server randoms for the signing transcript.
  699. CBB child;
  700. if (!CBB_init(cbb.get(), SSL3_RANDOM_SIZE * 2 + 128) ||
  701. !CBB_add_bytes(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
  702. !CBB_add_bytes(cbb.get(), ssl->s3->server_random, SSL3_RANDOM_SIZE)) {
  703. return ssl_hs_error;
  704. }
  705. // PSK ciphers begin with an identity hint.
  706. if (alg_a & SSL_aPSK) {
  707. size_t len =
  708. (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
  709. if (!CBB_add_u16_length_prefixed(cbb.get(), &child) ||
  710. !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
  711. len)) {
  712. return ssl_hs_error;
  713. }
  714. }
  715. if (alg_k & SSL_kECDHE) {
  716. // Determine the group to use.
  717. uint16_t group_id;
  718. if (!tls1_get_shared_group(hs, &group_id)) {
  719. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  720. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  721. return ssl_hs_error;
  722. }
  723. hs->new_session->group_id = group_id;
  724. // Set up ECDH, generate a key, and emit the public half.
  725. hs->key_share = SSLKeyShare::Create(group_id);
  726. if (!hs->key_share ||
  727. !CBB_add_u8(cbb.get(), NAMED_CURVE_TYPE) ||
  728. !CBB_add_u16(cbb.get(), group_id) ||
  729. !CBB_add_u8_length_prefixed(cbb.get(), &child) ||
  730. !hs->key_share->Offer(&child)) {
  731. return ssl_hs_error;
  732. }
  733. } else {
  734. assert(alg_k & SSL_kPSK);
  735. }
  736. if (!CBBFinishArray(cbb.get(), &hs->server_params)) {
  737. return ssl_hs_error;
  738. }
  739. }
  740. hs->state = state_send_server_key_exchange;
  741. return ssl_hs_ok;
  742. }
  743. static enum ssl_hs_wait_t do_send_server_key_exchange(SSL_HANDSHAKE *hs) {
  744. SSL *const ssl = hs->ssl;
  745. if (hs->server_params.size() == 0) {
  746. hs->state = state_send_server_hello_done;
  747. return ssl_hs_ok;
  748. }
  749. ScopedCBB cbb;
  750. CBB body, child;
  751. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  752. SSL3_MT_SERVER_KEY_EXCHANGE) ||
  753. // |hs->server_params| contains a prefix for signing.
  754. hs->server_params.size() < 2 * SSL3_RANDOM_SIZE ||
  755. !CBB_add_bytes(&body, hs->server_params.data() + 2 * SSL3_RANDOM_SIZE,
  756. hs->server_params.size() - 2 * SSL3_RANDOM_SIZE)) {
  757. return ssl_hs_error;
  758. }
  759. // Add a signature.
  760. if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  761. if (!ssl_has_private_key(ssl)) {
  762. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  763. return ssl_hs_error;
  764. }
  765. // Determine the signature algorithm.
  766. uint16_t signature_algorithm;
  767. if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
  768. return ssl_hs_error;
  769. }
  770. if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
  771. if (!CBB_add_u16(&body, signature_algorithm)) {
  772. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  773. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  774. return ssl_hs_error;
  775. }
  776. }
  777. // Add space for the signature.
  778. const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
  779. uint8_t *ptr;
  780. if (!CBB_add_u16_length_prefixed(&body, &child) ||
  781. !CBB_reserve(&child, &ptr, max_sig_len)) {
  782. return ssl_hs_error;
  783. }
  784. size_t sig_len;
  785. switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
  786. signature_algorithm, hs->server_params)) {
  787. case ssl_private_key_success:
  788. if (!CBB_did_write(&child, sig_len)) {
  789. return ssl_hs_error;
  790. }
  791. break;
  792. case ssl_private_key_failure:
  793. return ssl_hs_error;
  794. case ssl_private_key_retry:
  795. return ssl_hs_private_key_operation;
  796. }
  797. }
  798. if (!ssl_add_message_cbb(ssl, cbb.get())) {
  799. return ssl_hs_error;
  800. }
  801. hs->server_params.Reset();
  802. hs->state = state_send_server_hello_done;
  803. return ssl_hs_ok;
  804. }
  805. static enum ssl_hs_wait_t do_send_server_hello_done(SSL_HANDSHAKE *hs) {
  806. SSL *const ssl = hs->ssl;
  807. ScopedCBB cbb;
  808. CBB body;
  809. if (hs->cert_request) {
  810. CBB cert_types, sigalgs_cbb;
  811. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  812. SSL3_MT_CERTIFICATE_REQUEST) ||
  813. !CBB_add_u8_length_prefixed(&body, &cert_types) ||
  814. !CBB_add_u8(&cert_types, SSL3_CT_RSA_SIGN) ||
  815. (ssl_protocol_version(ssl) >= TLS1_VERSION &&
  816. !CBB_add_u8(&cert_types, TLS_CT_ECDSA_SIGN)) ||
  817. (ssl_protocol_version(ssl) >= TLS1_2_VERSION &&
  818. (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb) ||
  819. !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb))) ||
  820. !ssl_add_client_CA_list(ssl, &body) ||
  821. !ssl_add_message_cbb(ssl, cbb.get())) {
  822. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  823. return ssl_hs_error;
  824. }
  825. }
  826. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  827. SSL3_MT_SERVER_HELLO_DONE) ||
  828. !ssl_add_message_cbb(ssl, cbb.get())) {
  829. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  830. return ssl_hs_error;
  831. }
  832. hs->state = state_read_client_certificate;
  833. return ssl_hs_flush;
  834. }
  835. static enum ssl_hs_wait_t do_read_client_certificate(SSL_HANDSHAKE *hs) {
  836. SSL *const ssl = hs->ssl;
  837. if (!hs->cert_request) {
  838. hs->state = state_verify_client_certificate;
  839. return ssl_hs_ok;
  840. }
  841. SSLMessage msg;
  842. if (!ssl->method->get_message(ssl, &msg)) {
  843. return ssl_hs_read_message;
  844. }
  845. if (msg.type != SSL3_MT_CERTIFICATE) {
  846. if (ssl->version == SSL3_VERSION &&
  847. msg.type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
  848. // In SSL 3.0, the Certificate message is omitted to signal no
  849. // certificate.
  850. if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  851. OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  852. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  853. return ssl_hs_error;
  854. }
  855. // OpenSSL returns X509_V_OK when no certificates are received. This is
  856. // classed by them as a bug, but it's assumed by at least NGINX.
  857. hs->new_session->verify_result = X509_V_OK;
  858. hs->state = state_verify_client_certificate;
  859. return ssl_hs_ok;
  860. }
  861. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
  862. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  863. return ssl_hs_error;
  864. }
  865. if (!ssl_hash_message(hs, msg)) {
  866. return ssl_hs_error;
  867. }
  868. CBS certificate_msg = msg.body;
  869. uint8_t alert = SSL_AD_DECODE_ERROR;
  870. UniquePtr<STACK_OF(CRYPTO_BUFFER)> chain;
  871. if (!ssl_parse_cert_chain(&alert, &chain, &hs->peer_pubkey,
  872. ssl->retain_only_sha256_of_client_certs
  873. ? hs->new_session->peer_sha256
  874. : NULL,
  875. &certificate_msg, ssl->ctx->pool)) {
  876. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  877. return ssl_hs_error;
  878. }
  879. sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
  880. hs->new_session->certs = chain.release();
  881. if (CBS_len(&certificate_msg) != 0 ||
  882. !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
  883. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  884. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  885. return ssl_hs_error;
  886. }
  887. if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0) {
  888. // No client certificate so the handshake buffer may be discarded.
  889. hs->transcript.FreeBuffer();
  890. // In SSL 3.0, sending no certificate is signaled by omitting the
  891. // Certificate message.
  892. if (ssl->version == SSL3_VERSION) {
  893. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
  894. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  895. return ssl_hs_error;
  896. }
  897. if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  898. // Fail for TLS only if we required a certificate
  899. OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  900. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  901. return ssl_hs_error;
  902. }
  903. // OpenSSL returns X509_V_OK when no certificates are received. This is
  904. // classed by them as a bug, but it's assumed by at least NGINX.
  905. hs->new_session->verify_result = X509_V_OK;
  906. } else if (ssl->retain_only_sha256_of_client_certs) {
  907. // The hash will have been filled in.
  908. hs->new_session->peer_sha256_valid = 1;
  909. }
  910. ssl->method->next_message(ssl);
  911. hs->state = state_verify_client_certificate;
  912. return ssl_hs_ok;
  913. }
  914. static enum ssl_hs_wait_t do_verify_client_certificate(SSL_HANDSHAKE *hs) {
  915. if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) > 0) {
  916. switch (ssl_verify_peer_cert(hs)) {
  917. case ssl_verify_ok:
  918. break;
  919. case ssl_verify_invalid:
  920. return ssl_hs_error;
  921. case ssl_verify_retry:
  922. return ssl_hs_certificate_verify;
  923. }
  924. }
  925. hs->state = state_read_client_key_exchange;
  926. return ssl_hs_ok;
  927. }
  928. static enum ssl_hs_wait_t do_read_client_key_exchange(SSL_HANDSHAKE *hs) {
  929. SSL *const ssl = hs->ssl;
  930. SSLMessage msg;
  931. if (!ssl->method->get_message(ssl, &msg)) {
  932. return ssl_hs_read_message;
  933. }
  934. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_KEY_EXCHANGE)) {
  935. return ssl_hs_error;
  936. }
  937. CBS client_key_exchange = msg.body;
  938. uint32_t alg_k = hs->new_cipher->algorithm_mkey;
  939. uint32_t alg_a = hs->new_cipher->algorithm_auth;
  940. // If using a PSK key exchange, parse the PSK identity.
  941. if (alg_a & SSL_aPSK) {
  942. CBS psk_identity;
  943. // If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
  944. // then this is the only field in the message.
  945. if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
  946. ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
  947. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  948. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  949. return ssl_hs_error;
  950. }
  951. if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
  952. CBS_contains_zero_byte(&psk_identity)) {
  953. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  954. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  955. return ssl_hs_error;
  956. }
  957. if (!CBS_strdup(&psk_identity, &hs->new_session->psk_identity)) {
  958. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  959. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  960. return ssl_hs_error;
  961. }
  962. }
  963. // Depending on the key exchange method, compute |premaster_secret|.
  964. Array<uint8_t> premaster_secret;
  965. if (alg_k & SSL_kRSA) {
  966. CBS encrypted_premaster_secret;
  967. if (ssl->version > SSL3_VERSION) {
  968. if (!CBS_get_u16_length_prefixed(&client_key_exchange,
  969. &encrypted_premaster_secret) ||
  970. CBS_len(&client_key_exchange) != 0) {
  971. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  972. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  973. return ssl_hs_error;
  974. }
  975. } else {
  976. encrypted_premaster_secret = client_key_exchange;
  977. }
  978. // Allocate a buffer large enough for an RSA decryption.
  979. Array<uint8_t> decrypt_buf;
  980. if (!decrypt_buf.Init(EVP_PKEY_size(hs->local_pubkey.get()))) {
  981. return ssl_hs_error;
  982. }
  983. // Decrypt with no padding. PKCS#1 padding will be removed as part of the
  984. // timing-sensitive code below.
  985. size_t decrypt_len;
  986. switch (ssl_private_key_decrypt(hs, decrypt_buf.data(), &decrypt_len,
  987. decrypt_buf.size(),
  988. encrypted_premaster_secret)) {
  989. case ssl_private_key_success:
  990. break;
  991. case ssl_private_key_failure:
  992. return ssl_hs_error;
  993. case ssl_private_key_retry:
  994. return ssl_hs_private_key_operation;
  995. }
  996. if (decrypt_len != decrypt_buf.size()) {
  997. OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
  998. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
  999. return ssl_hs_error;
  1000. }
  1001. // Prepare a random premaster, to be used on invalid padding. See RFC 5246,
  1002. // section 7.4.7.1.
  1003. if (!premaster_secret.Init(SSL_MAX_MASTER_KEY_LENGTH) ||
  1004. !RAND_bytes(premaster_secret.data(), premaster_secret.size())) {
  1005. return ssl_hs_error;
  1006. }
  1007. // The smallest padded premaster is 11 bytes of overhead. Small keys are
  1008. // publicly invalid.
  1009. if (decrypt_len < 11 + premaster_secret.size()) {
  1010. OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
  1011. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
  1012. return ssl_hs_error;
  1013. }
  1014. // Check the padding. See RFC 3447, section 7.2.2.
  1015. size_t padding_len = decrypt_len - premaster_secret.size();
  1016. uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
  1017. constant_time_eq_int_8(decrypt_buf[1], 2);
  1018. for (size_t i = 2; i < padding_len - 1; i++) {
  1019. good &= ~constant_time_is_zero_8(decrypt_buf[i]);
  1020. }
  1021. good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
  1022. // The premaster secret must begin with |client_version|. This too must be
  1023. // checked in constant time (http://eprint.iacr.org/2003/052/).
  1024. good &= constant_time_eq_8(decrypt_buf[padding_len],
  1025. (unsigned)(hs->client_version >> 8));
  1026. good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
  1027. (unsigned)(hs->client_version & 0xff));
  1028. // Select, in constant time, either the decrypted premaster or the random
  1029. // premaster based on |good|.
  1030. for (size_t i = 0; i < premaster_secret.size(); i++) {
  1031. premaster_secret[i] = constant_time_select_8(
  1032. good, decrypt_buf[padding_len + i], premaster_secret[i]);
  1033. }
  1034. } else if (alg_k & SSL_kECDHE) {
  1035. // Parse the ClientKeyExchange.
  1036. CBS peer_key;
  1037. if (!CBS_get_u8_length_prefixed(&client_key_exchange, &peer_key) ||
  1038. CBS_len(&client_key_exchange) != 0) {
  1039. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1040. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1041. return ssl_hs_error;
  1042. }
  1043. // Compute the premaster.
  1044. uint8_t alert = SSL_AD_DECODE_ERROR;
  1045. if (!hs->key_share->Finish(&premaster_secret, &alert, peer_key)) {
  1046. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  1047. return ssl_hs_error;
  1048. }
  1049. // The key exchange state may now be discarded.
  1050. hs->key_share.reset();
  1051. } else if (!(alg_k & SSL_kPSK)) {
  1052. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1053. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1054. return ssl_hs_error;
  1055. }
  1056. // For a PSK cipher suite, the actual pre-master secret is combined with the
  1057. // pre-shared key.
  1058. if (alg_a & SSL_aPSK) {
  1059. if (ssl->psk_server_callback == NULL) {
  1060. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1061. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1062. return ssl_hs_error;
  1063. }
  1064. // Look up the key for the identity.
  1065. uint8_t psk[PSK_MAX_PSK_LEN];
  1066. unsigned psk_len = ssl->psk_server_callback(
  1067. ssl, hs->new_session->psk_identity, psk, sizeof(psk));
  1068. if (psk_len > PSK_MAX_PSK_LEN) {
  1069. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1070. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1071. return ssl_hs_error;
  1072. } else if (psk_len == 0) {
  1073. // PSK related to the given identity not found.
  1074. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
  1075. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNKNOWN_PSK_IDENTITY);
  1076. return ssl_hs_error;
  1077. }
  1078. if (alg_k & SSL_kPSK) {
  1079. // In plain PSK, other_secret is a block of 0s with the same length as the
  1080. // pre-shared key.
  1081. if (!premaster_secret.Init(psk_len)) {
  1082. return ssl_hs_error;
  1083. }
  1084. OPENSSL_memset(premaster_secret.data(), 0, premaster_secret.size());
  1085. }
  1086. ScopedCBB new_premaster;
  1087. CBB child;
  1088. if (!CBB_init(new_premaster.get(),
  1089. 2 + psk_len + 2 + premaster_secret.size()) ||
  1090. !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
  1091. !CBB_add_bytes(&child, premaster_secret.data(),
  1092. premaster_secret.size()) ||
  1093. !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
  1094. !CBB_add_bytes(&child, psk, psk_len) ||
  1095. !CBBFinishArray(new_premaster.get(), &premaster_secret)) {
  1096. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1097. return ssl_hs_error;
  1098. }
  1099. }
  1100. if (!ssl_hash_message(hs, msg)) {
  1101. return ssl_hs_error;
  1102. }
  1103. // Compute the master secret.
  1104. hs->new_session->master_key_length = tls1_generate_master_secret(
  1105. hs, hs->new_session->master_key, premaster_secret);
  1106. if (hs->new_session->master_key_length == 0) {
  1107. return ssl_hs_error;
  1108. }
  1109. hs->new_session->extended_master_secret = hs->extended_master_secret;
  1110. ssl->method->next_message(ssl);
  1111. hs->state = state_read_client_certificate_verify;
  1112. return ssl_hs_ok;
  1113. }
  1114. static enum ssl_hs_wait_t do_read_client_certificate_verify(SSL_HANDSHAKE *hs) {
  1115. SSL *const ssl = hs->ssl;
  1116. // Only RSA and ECDSA client certificates are supported, so a
  1117. // CertificateVerify is required if and only if there's a client certificate.
  1118. if (!hs->peer_pubkey) {
  1119. hs->transcript.FreeBuffer();
  1120. hs->state = state_read_change_cipher_spec;
  1121. return ssl_hs_ok;
  1122. }
  1123. SSLMessage msg;
  1124. if (!ssl->method->get_message(ssl, &msg)) {
  1125. return ssl_hs_read_message;
  1126. }
  1127. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY)) {
  1128. return ssl_hs_error;
  1129. }
  1130. CBS certificate_verify = msg.body, signature;
  1131. // Determine the signature algorithm.
  1132. uint16_t signature_algorithm = 0;
  1133. if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
  1134. if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
  1135. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1136. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1137. return ssl_hs_error;
  1138. }
  1139. uint8_t alert = SSL_AD_DECODE_ERROR;
  1140. if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
  1141. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  1142. return ssl_hs_error;
  1143. }
  1144. hs->new_session->peer_signature_algorithm = signature_algorithm;
  1145. } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
  1146. hs->peer_pubkey.get())) {
  1147. OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
  1148. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
  1149. return ssl_hs_error;
  1150. }
  1151. // Parse and verify the signature.
  1152. if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
  1153. CBS_len(&certificate_verify) != 0) {
  1154. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1155. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1156. return ssl_hs_error;
  1157. }
  1158. bool sig_ok;
  1159. // The SSL3 construction for CertificateVerify does not decompose into a
  1160. // single final digest and signature, and must be special-cased.
  1161. if (ssl_protocol_version(ssl) == SSL3_VERSION) {
  1162. uint8_t digest[EVP_MAX_MD_SIZE];
  1163. size_t digest_len;
  1164. if (!hs->transcript.GetSSL3CertVerifyHash(
  1165. digest, &digest_len, hs->new_session.get(), signature_algorithm)) {
  1166. return ssl_hs_error;
  1167. }
  1168. UniquePtr<EVP_PKEY_CTX> pctx(
  1169. EVP_PKEY_CTX_new(hs->peer_pubkey.get(), nullptr));
  1170. sig_ok = pctx &&
  1171. EVP_PKEY_verify_init(pctx.get()) &&
  1172. EVP_PKEY_verify(pctx.get(), CBS_data(&signature),
  1173. CBS_len(&signature), digest, digest_len);
  1174. } else {
  1175. sig_ok =
  1176. ssl_public_key_verify(ssl, signature, signature_algorithm,
  1177. hs->peer_pubkey.get(), hs->transcript.buffer());
  1178. }
  1179. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  1180. sig_ok = true;
  1181. ERR_clear_error();
  1182. #endif
  1183. if (!sig_ok) {
  1184. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
  1185. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
  1186. return ssl_hs_error;
  1187. }
  1188. // The handshake buffer is no longer necessary, and we may hash the current
  1189. // message.
  1190. hs->transcript.FreeBuffer();
  1191. if (!ssl_hash_message(hs, msg)) {
  1192. return ssl_hs_error;
  1193. }
  1194. ssl->method->next_message(ssl);
  1195. hs->state = state_read_change_cipher_spec;
  1196. return ssl_hs_ok;
  1197. }
  1198. static enum ssl_hs_wait_t do_read_change_cipher_spec(SSL_HANDSHAKE *hs) {
  1199. hs->state = state_process_change_cipher_spec;
  1200. return ssl_hs_read_change_cipher_spec;
  1201. }
  1202. static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
  1203. if (!tls1_change_cipher_state(hs, evp_aead_open)) {
  1204. return ssl_hs_error;
  1205. }
  1206. hs->state = state_read_next_proto;
  1207. return ssl_hs_ok;
  1208. }
  1209. static enum ssl_hs_wait_t do_read_next_proto(SSL_HANDSHAKE *hs) {
  1210. SSL *const ssl = hs->ssl;
  1211. if (!hs->next_proto_neg_seen) {
  1212. hs->state = state_read_channel_id;
  1213. return ssl_hs_ok;
  1214. }
  1215. SSLMessage msg;
  1216. if (!ssl->method->get_message(ssl, &msg)) {
  1217. return ssl_hs_read_message;
  1218. }
  1219. if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEXT_PROTO) ||
  1220. !ssl_hash_message(hs, msg)) {
  1221. return ssl_hs_error;
  1222. }
  1223. CBS next_protocol = msg.body, selected_protocol, padding;
  1224. if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
  1225. !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
  1226. CBS_len(&next_protocol) != 0) {
  1227. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1228. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1229. return ssl_hs_error;
  1230. }
  1231. if (!ssl->s3->next_proto_negotiated.CopyFrom(selected_protocol)) {
  1232. return ssl_hs_error;
  1233. }
  1234. ssl->method->next_message(ssl);
  1235. hs->state = state_read_channel_id;
  1236. return ssl_hs_ok;
  1237. }
  1238. static enum ssl_hs_wait_t do_read_channel_id(SSL_HANDSHAKE *hs) {
  1239. SSL *const ssl = hs->ssl;
  1240. if (!ssl->s3->tlsext_channel_id_valid) {
  1241. hs->state = state_read_client_finished;
  1242. return ssl_hs_ok;
  1243. }
  1244. SSLMessage msg;
  1245. if (!ssl->method->get_message(ssl, &msg)) {
  1246. return ssl_hs_read_message;
  1247. }
  1248. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) ||
  1249. !tls1_verify_channel_id(hs, msg) ||
  1250. !ssl_hash_message(hs, msg)) {
  1251. return ssl_hs_error;
  1252. }
  1253. ssl->method->next_message(ssl);
  1254. hs->state = state_read_client_finished;
  1255. return ssl_hs_ok;
  1256. }
  1257. static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) {
  1258. SSL *const ssl = hs->ssl;
  1259. enum ssl_hs_wait_t wait = ssl_get_finished(hs);
  1260. if (wait != ssl_hs_ok) {
  1261. return wait;
  1262. }
  1263. if (ssl->session != NULL) {
  1264. hs->state = state_finish_server_handshake;
  1265. } else {
  1266. hs->state = state_send_server_finished;
  1267. }
  1268. // If this is a full handshake with ChannelID then record the handshake
  1269. // hashes in |hs->new_session| in case we need them to verify a
  1270. // ChannelID signature on a resumption of this session in the future.
  1271. if (ssl->session == NULL && ssl->s3->tlsext_channel_id_valid &&
  1272. !tls1_record_handshake_hashes_for_channel_id(hs)) {
  1273. return ssl_hs_error;
  1274. }
  1275. return ssl_hs_ok;
  1276. }
  1277. static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) {
  1278. SSL *const ssl = hs->ssl;
  1279. if (hs->ticket_expected) {
  1280. const SSL_SESSION *session;
  1281. UniquePtr<SSL_SESSION> session_copy;
  1282. if (ssl->session == NULL) {
  1283. // Fix the timeout to measure from the ticket issuance time.
  1284. ssl_session_rebase_time(ssl, hs->new_session.get());
  1285. session = hs->new_session.get();
  1286. } else {
  1287. // We are renewing an existing session. Duplicate the session to adjust
  1288. // the timeout.
  1289. session_copy = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
  1290. if (!session_copy) {
  1291. return ssl_hs_error;
  1292. }
  1293. ssl_session_rebase_time(ssl, session_copy.get());
  1294. session = session_copy.get();
  1295. }
  1296. ScopedCBB cbb;
  1297. CBB body, ticket;
  1298. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  1299. SSL3_MT_NEW_SESSION_TICKET) ||
  1300. !CBB_add_u32(&body, session->timeout) ||
  1301. !CBB_add_u16_length_prefixed(&body, &ticket) ||
  1302. !ssl_encrypt_ticket(ssl, &ticket, session) ||
  1303. !ssl_add_message_cbb(ssl, cbb.get())) {
  1304. return ssl_hs_error;
  1305. }
  1306. }
  1307. if (!ssl->method->add_change_cipher_spec(ssl) ||
  1308. !tls1_change_cipher_state(hs, evp_aead_seal) ||
  1309. !ssl_send_finished(hs)) {
  1310. return ssl_hs_error;
  1311. }
  1312. if (ssl->session != NULL) {
  1313. hs->state = state_read_change_cipher_spec;
  1314. } else {
  1315. hs->state = state_finish_server_handshake;
  1316. }
  1317. return ssl_hs_flush;
  1318. }
  1319. static enum ssl_hs_wait_t do_finish_server_handshake(SSL_HANDSHAKE *hs) {
  1320. SSL *const ssl = hs->ssl;
  1321. ssl->method->on_handshake_complete(ssl);
  1322. // If we aren't retaining peer certificates then we can discard it now.
  1323. if (hs->new_session != NULL && ssl->retain_only_sha256_of_client_certs) {
  1324. sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
  1325. hs->new_session->certs = NULL;
  1326. ssl->ctx->x509_method->session_clear(hs->new_session.get());
  1327. }
  1328. if (ssl->session != NULL) {
  1329. SSL_SESSION_up_ref(ssl->session);
  1330. ssl->s3->established_session.reset(ssl->session);
  1331. } else {
  1332. ssl->s3->established_session = std::move(hs->new_session);
  1333. ssl->s3->established_session->not_resumable = 0;
  1334. }
  1335. hs->handshake_finalized = true;
  1336. ssl->s3->initial_handshake_complete = true;
  1337. ssl_update_cache(hs, SSL_SESS_CACHE_SERVER);
  1338. hs->state = state_done;
  1339. return ssl_hs_ok;
  1340. }
  1341. enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs) {
  1342. while (hs->state != state_done) {
  1343. enum ssl_hs_wait_t ret = ssl_hs_error;
  1344. enum ssl_server_hs_state_t state =
  1345. static_cast<enum ssl_server_hs_state_t>(hs->state);
  1346. switch (state) {
  1347. case state_start_accept:
  1348. ret = do_start_accept(hs);
  1349. break;
  1350. case state_read_client_hello:
  1351. ret = do_read_client_hello(hs);
  1352. break;
  1353. case state_select_certificate:
  1354. ret = do_select_certificate(hs);
  1355. break;
  1356. case state_tls13:
  1357. ret = do_tls13(hs);
  1358. break;
  1359. case state_select_parameters:
  1360. ret = do_select_parameters(hs);
  1361. break;
  1362. case state_send_server_hello:
  1363. ret = do_send_server_hello(hs);
  1364. break;
  1365. case state_send_server_certificate:
  1366. ret = do_send_server_certificate(hs);
  1367. break;
  1368. case state_send_server_key_exchange:
  1369. ret = do_send_server_key_exchange(hs);
  1370. break;
  1371. case state_send_server_hello_done:
  1372. ret = do_send_server_hello_done(hs);
  1373. break;
  1374. case state_read_client_certificate:
  1375. ret = do_read_client_certificate(hs);
  1376. break;
  1377. case state_verify_client_certificate:
  1378. ret = do_verify_client_certificate(hs);
  1379. break;
  1380. case state_read_client_key_exchange:
  1381. ret = do_read_client_key_exchange(hs);
  1382. break;
  1383. case state_read_client_certificate_verify:
  1384. ret = do_read_client_certificate_verify(hs);
  1385. break;
  1386. case state_read_change_cipher_spec:
  1387. ret = do_read_change_cipher_spec(hs);
  1388. break;
  1389. case state_process_change_cipher_spec:
  1390. ret = do_process_change_cipher_spec(hs);
  1391. break;
  1392. case state_read_next_proto:
  1393. ret = do_read_next_proto(hs);
  1394. break;
  1395. case state_read_channel_id:
  1396. ret = do_read_channel_id(hs);
  1397. break;
  1398. case state_read_client_finished:
  1399. ret = do_read_client_finished(hs);
  1400. break;
  1401. case state_send_server_finished:
  1402. ret = do_send_server_finished(hs);
  1403. break;
  1404. case state_finish_server_handshake:
  1405. ret = do_finish_server_handshake(hs);
  1406. break;
  1407. case state_done:
  1408. ret = ssl_hs_ok;
  1409. break;
  1410. }
  1411. if (hs->state != state) {
  1412. ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1);
  1413. }
  1414. if (ret != ssl_hs_ok) {
  1415. return ret;
  1416. }
  1417. }
  1418. ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
  1419. return ssl_hs_ok;
  1420. }
  1421. const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs) {
  1422. enum ssl_server_hs_state_t state =
  1423. static_cast<enum ssl_server_hs_state_t>(hs->state);
  1424. switch (state) {
  1425. case state_start_accept:
  1426. return "TLS server start_accept";
  1427. case state_read_client_hello:
  1428. return "TLS server read_client_hello";
  1429. case state_select_certificate:
  1430. return "TLS server select_certificate";
  1431. case state_tls13:
  1432. return tls13_server_handshake_state(hs);
  1433. case state_select_parameters:
  1434. return "TLS server select_parameters";
  1435. case state_send_server_hello:
  1436. return "TLS server send_server_hello";
  1437. case state_send_server_certificate:
  1438. return "TLS server send_server_certificate";
  1439. case state_send_server_key_exchange:
  1440. return "TLS server send_server_key_exchange";
  1441. case state_send_server_hello_done:
  1442. return "TLS server send_server_hello_done";
  1443. case state_read_client_certificate:
  1444. return "TLS server read_client_certificate";
  1445. case state_verify_client_certificate:
  1446. return "TLS server verify_client_certificate";
  1447. case state_read_client_key_exchange:
  1448. return "TLS server read_client_key_exchange";
  1449. case state_read_client_certificate_verify:
  1450. return "TLS server read_client_certificate_verify";
  1451. case state_read_change_cipher_spec:
  1452. return "TLS server read_change_cipher_spec";
  1453. case state_process_change_cipher_spec:
  1454. return "TLS server process_change_cipher_spec";
  1455. case state_read_next_proto:
  1456. return "TLS server read_next_proto";
  1457. case state_read_channel_id:
  1458. return "TLS server read_channel_id";
  1459. case state_read_client_finished:
  1460. return "TLS server read_client_finished";
  1461. case state_send_server_finished:
  1462. return "TLS server send_server_finished";
  1463. case state_finish_server_handshake:
  1464. return "TLS server finish_server_handshake";
  1465. case state_done:
  1466. return "TLS server done";
  1467. }
  1468. return "TLS server unknown";
  1469. }
  1470. }