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.
 
 
 
 
 
 

1714 lines
57 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 = 1;
  263. // Handle FALLBACK_SCSV.
  264. if (ssl_client_cipher_list_contains_cipher(client_hello,
  265. SSL3_CK_FALLBACK_SCSV & 0xffff) &&
  266. ssl3_protocol_version(ssl) < hs->max_version) {
  267. OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
  268. *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK;
  269. return 0;
  270. }
  271. return 1;
  272. }
  273. static UniquePtr<STACK_OF(SSL_CIPHER)> ssl_parse_client_cipher_list(
  274. const SSL_CLIENT_HELLO *client_hello) {
  275. CBS cipher_suites;
  276. CBS_init(&cipher_suites, client_hello->cipher_suites,
  277. client_hello->cipher_suites_len);
  278. UniquePtr<STACK_OF(SSL_CIPHER)> sk(sk_SSL_CIPHER_new_null());
  279. if (!sk) {
  280. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  281. return nullptr;
  282. }
  283. while (CBS_len(&cipher_suites) > 0) {
  284. uint16_t cipher_suite;
  285. if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
  286. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
  287. return nullptr;
  288. }
  289. const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite);
  290. if (c != NULL && !sk_SSL_CIPHER_push(sk.get(), c)) {
  291. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  292. return nullptr;
  293. }
  294. }
  295. return sk;
  296. }
  297. // ssl_get_compatible_server_ciphers determines the key exchange and
  298. // authentication cipher suite masks compatible with the server configuration
  299. // and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key
  300. // exchange mask and |*out_mask_a| to the authentication mask.
  301. static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs,
  302. uint32_t *out_mask_k,
  303. uint32_t *out_mask_a) {
  304. SSL *const ssl = hs->ssl;
  305. uint32_t mask_k = 0;
  306. uint32_t mask_a = 0;
  307. if (ssl_has_certificate(ssl)) {
  308. mask_a |= ssl_cipher_auth_mask_for_key(hs->local_pubkey.get());
  309. if (EVP_PKEY_id(hs->local_pubkey.get()) == EVP_PKEY_RSA) {
  310. mask_k |= SSL_kRSA;
  311. }
  312. }
  313. // Check for a shared group to consider ECDHE ciphers.
  314. uint16_t unused;
  315. if (tls1_get_shared_group(hs, &unused)) {
  316. mask_k |= SSL_kECDHE;
  317. }
  318. // PSK requires a server callback.
  319. if (ssl->psk_server_callback != NULL) {
  320. mask_k |= SSL_kPSK;
  321. mask_a |= SSL_aPSK;
  322. }
  323. *out_mask_k = mask_k;
  324. *out_mask_a = mask_a;
  325. }
  326. static const SSL_CIPHER *ssl3_choose_cipher(
  327. SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello,
  328. const struct ssl_cipher_preference_list_st *server_pref) {
  329. SSL *const ssl = hs->ssl;
  330. STACK_OF(SSL_CIPHER) *prio, *allow;
  331. // in_group_flags will either be NULL, or will point to an array of bytes
  332. // which indicate equal-preference groups in the |prio| stack. See the
  333. // comment about |in_group_flags| in the |ssl_cipher_preference_list_st|
  334. // struct.
  335. const uint8_t *in_group_flags;
  336. // group_min contains the minimal index so far found in a group, or -1 if no
  337. // such value exists yet.
  338. int group_min = -1;
  339. UniquePtr<STACK_OF(SSL_CIPHER)> client_pref =
  340. ssl_parse_client_cipher_list(client_hello);
  341. if (!client_pref) {
  342. return nullptr;
  343. }
  344. if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
  345. prio = server_pref->ciphers;
  346. in_group_flags = server_pref->in_group_flags;
  347. allow = client_pref.get();
  348. } else {
  349. prio = client_pref.get();
  350. in_group_flags = NULL;
  351. allow = server_pref->ciphers;
  352. }
  353. uint32_t mask_k, mask_a;
  354. ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a);
  355. for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) {
  356. const SSL_CIPHER *c = sk_SSL_CIPHER_value(prio, i);
  357. size_t cipher_index;
  358. if (// Check if the cipher is supported for the current version.
  359. SSL_CIPHER_get_min_version(c) <= ssl3_protocol_version(ssl) &&
  360. ssl3_protocol_version(ssl) <= SSL_CIPHER_get_max_version(c) &&
  361. // Check the cipher is supported for the server configuration.
  362. (c->algorithm_mkey & mask_k) &&
  363. (c->algorithm_auth & mask_a) &&
  364. // Check the cipher is in the |allow| list.
  365. sk_SSL_CIPHER_find(allow, &cipher_index, c)) {
  366. if (in_group_flags != NULL && in_group_flags[i] == 1) {
  367. // This element of |prio| is in a group. Update the minimum index found
  368. // so far and continue looking.
  369. if (group_min == -1 || (size_t)group_min > cipher_index) {
  370. group_min = cipher_index;
  371. }
  372. } else {
  373. if (group_min != -1 && (size_t)group_min < cipher_index) {
  374. cipher_index = group_min;
  375. }
  376. return sk_SSL_CIPHER_value(allow, cipher_index);
  377. }
  378. }
  379. if (in_group_flags != NULL && in_group_flags[i] == 0 && group_min != -1) {
  380. // We are about to leave a group, but we found a match in it, so that's
  381. // our answer.
  382. return sk_SSL_CIPHER_value(allow, group_min);
  383. }
  384. }
  385. return nullptr;
  386. }
  387. static enum ssl_hs_wait_t do_start_accept(SSL_HANDSHAKE *hs) {
  388. ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_START, 1);
  389. hs->state = state_read_client_hello;
  390. return ssl_hs_ok;
  391. }
  392. static enum ssl_hs_wait_t do_read_client_hello(SSL_HANDSHAKE *hs) {
  393. SSL *const ssl = hs->ssl;
  394. SSLMessage msg;
  395. if (!ssl->method->get_message(ssl, &msg)) {
  396. return ssl_hs_read_message;
  397. }
  398. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_HELLO)) {
  399. return ssl_hs_error;
  400. }
  401. SSL_CLIENT_HELLO client_hello;
  402. if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
  403. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  404. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  405. return ssl_hs_error;
  406. }
  407. // Run the early callback.
  408. if (ssl->ctx->select_certificate_cb != NULL) {
  409. switch (ssl->ctx->select_certificate_cb(&client_hello)) {
  410. case ssl_select_cert_retry:
  411. return ssl_hs_certificate_selection_pending;
  412. case ssl_select_cert_error:
  413. // Connection rejected.
  414. OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
  415. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  416. return ssl_hs_error;
  417. default:
  418. /* fallthrough */;
  419. }
  420. }
  421. // Freeze the version range after the early callback.
  422. if (!ssl_get_version_range(ssl, &hs->min_version, &hs->max_version)) {
  423. return ssl_hs_error;
  424. }
  425. uint8_t alert = SSL_AD_DECODE_ERROR;
  426. if (!negotiate_version(hs, &alert, &client_hello)) {
  427. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  428. return ssl_hs_error;
  429. }
  430. hs->client_version = client_hello.version;
  431. if (client_hello.random_len != SSL3_RANDOM_SIZE) {
  432. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  433. return ssl_hs_error;
  434. }
  435. OPENSSL_memcpy(ssl->s3->client_random, client_hello.random,
  436. client_hello.random_len);
  437. // Only null compression is supported. TLS 1.3 further requires the peer
  438. // advertise no other compression.
  439. if (OPENSSL_memchr(client_hello.compression_methods, 0,
  440. client_hello.compression_methods_len) == NULL ||
  441. (ssl3_protocol_version(ssl) >= TLS1_3_VERSION &&
  442. client_hello.compression_methods_len != 1)) {
  443. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMPRESSION_LIST);
  444. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  445. return ssl_hs_error;
  446. }
  447. // TLS extensions.
  448. if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) {
  449. OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
  450. return ssl_hs_error;
  451. }
  452. hs->state = state_select_certificate;
  453. return ssl_hs_ok;
  454. }
  455. static enum ssl_hs_wait_t do_select_certificate(SSL_HANDSHAKE *hs) {
  456. SSL *const ssl = hs->ssl;
  457. SSLMessage msg;
  458. if (!ssl->method->get_message(ssl, &msg)) {
  459. return ssl_hs_read_message;
  460. }
  461. // Call |cert_cb| to update server certificates if required.
  462. if (ssl->cert->cert_cb != NULL) {
  463. int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
  464. if (rv == 0) {
  465. OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
  466. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  467. return ssl_hs_error;
  468. }
  469. if (rv < 0) {
  470. return ssl_hs_x509_lookup;
  471. }
  472. }
  473. if (!ssl_on_certificate_selected(hs)) {
  474. return ssl_hs_error;
  475. }
  476. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  477. // Jump to the TLS 1.3 state machine.
  478. hs->state = state_tls13;
  479. return ssl_hs_ok;
  480. }
  481. SSL_CLIENT_HELLO client_hello;
  482. if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
  483. return ssl_hs_error;
  484. }
  485. // Negotiate the cipher suite. This must be done after |cert_cb| so the
  486. // certificate is finalized.
  487. hs->new_cipher =
  488. ssl3_choose_cipher(hs, &client_hello, ssl_get_cipher_preferences(ssl));
  489. if (hs->new_cipher == NULL) {
  490. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
  491. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  492. return ssl_hs_error;
  493. }
  494. hs->state = state_select_parameters;
  495. return ssl_hs_ok;
  496. }
  497. static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) {
  498. enum ssl_hs_wait_t wait = tls13_server_handshake(hs);
  499. if (wait == ssl_hs_ok) {
  500. hs->state = state_finish_server_handshake;
  501. return ssl_hs_ok;
  502. }
  503. return wait;
  504. }
  505. static enum ssl_hs_wait_t do_select_parameters(SSL_HANDSHAKE *hs) {
  506. SSL *const ssl = hs->ssl;
  507. SSLMessage msg;
  508. if (!ssl->method->get_message(ssl, &msg)) {
  509. return ssl_hs_read_message;
  510. }
  511. SSL_CLIENT_HELLO client_hello;
  512. if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
  513. return ssl_hs_error;
  514. }
  515. // Determine whether we are doing session resumption.
  516. UniquePtr<SSL_SESSION> session;
  517. int tickets_supported = 0, renew_ticket = 0;
  518. enum ssl_hs_wait_t wait = ssl_get_prev_session(
  519. ssl, &session, &tickets_supported, &renew_ticket, &client_hello);
  520. if (wait != ssl_hs_ok) {
  521. return wait;
  522. }
  523. if (session) {
  524. if (session->extended_master_secret && !hs->extended_master_secret) {
  525. // A ClientHello without EMS that attempts to resume a session with EMS
  526. // is fatal to the connection.
  527. OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
  528. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  529. return ssl_hs_error;
  530. }
  531. if (!ssl_session_is_resumable(hs, session.get()) ||
  532. // If the client offers the EMS extension, but the previous session
  533. // didn't use it, then negotiate a new session.
  534. hs->extended_master_secret != session->extended_master_secret) {
  535. session.reset();
  536. }
  537. }
  538. if (session) {
  539. // Use the old session.
  540. hs->ticket_expected = renew_ticket;
  541. ssl->session = session.release();
  542. ssl->s3->session_reused = 1;
  543. } else {
  544. hs->ticket_expected = tickets_supported;
  545. ssl_set_session(ssl, NULL);
  546. if (!ssl_get_new_session(hs, 1 /* server */)) {
  547. return ssl_hs_error;
  548. }
  549. // Clear the session ID if we want the session to be single-use.
  550. if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
  551. hs->new_session->session_id_length = 0;
  552. }
  553. }
  554. if (ssl->ctx->dos_protection_cb != NULL &&
  555. ssl->ctx->dos_protection_cb(&client_hello) == 0) {
  556. // Connection rejected for DOS reasons.
  557. OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
  558. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  559. return ssl_hs_error;
  560. }
  561. if (ssl->session == NULL) {
  562. hs->new_session->cipher = hs->new_cipher;
  563. // On new sessions, stash the SNI value in the session.
  564. if (hs->hostname != NULL) {
  565. OPENSSL_free(hs->new_session->tlsext_hostname);
  566. hs->new_session->tlsext_hostname = BUF_strdup(hs->hostname.get());
  567. if (hs->new_session->tlsext_hostname == NULL) {
  568. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  569. return ssl_hs_error;
  570. }
  571. }
  572. // Determine whether to request a client certificate.
  573. hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
  574. // Only request a certificate if Channel ID isn't negotiated.
  575. if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
  576. ssl->s3->tlsext_channel_id_valid) {
  577. hs->cert_request = 0;
  578. }
  579. // CertificateRequest may only be sent in certificate-based ciphers.
  580. if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  581. hs->cert_request = 0;
  582. }
  583. if (!hs->cert_request) {
  584. // OpenSSL returns X509_V_OK when no certificates are requested. This is
  585. // classed by them as a bug, but it's assumed by at least NGINX.
  586. hs->new_session->verify_result = X509_V_OK;
  587. }
  588. }
  589. // HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
  590. // deferred. Complete it now.
  591. uint8_t alert = SSL_AD_DECODE_ERROR;
  592. if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) {
  593. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  594. return ssl_hs_error;
  595. }
  596. // Now that all parameters are known, initialize the handshake hash and hash
  597. // the ClientHello.
  598. if (!hs->transcript.InitHash(ssl3_protocol_version(ssl), hs->new_cipher) ||
  599. !ssl_hash_message(hs, msg)) {
  600. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  601. return ssl_hs_error;
  602. }
  603. // Release the handshake buffer if client authentication isn't required.
  604. if (!hs->cert_request) {
  605. hs->transcript.FreeBuffer();
  606. }
  607. ssl->method->next_message(ssl);
  608. hs->state = state_send_server_hello;
  609. return ssl_hs_ok;
  610. }
  611. static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) {
  612. SSL *const ssl = hs->ssl;
  613. // We only accept ChannelIDs on connections with ECDHE in order to avoid a
  614. // known attack while we fix ChannelID itself.
  615. if (ssl->s3->tlsext_channel_id_valid &&
  616. (hs->new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
  617. ssl->s3->tlsext_channel_id_valid = 0;
  618. }
  619. // If this is a resumption and the original handshake didn't support
  620. // ChannelID then we didn't record the original handshake hashes in the
  621. // session and so cannot resume with ChannelIDs.
  622. if (ssl->session != NULL &&
  623. ssl->session->original_handshake_hash_len == 0) {
  624. ssl->s3->tlsext_channel_id_valid = 0;
  625. }
  626. struct OPENSSL_timeval now;
  627. ssl_get_current_time(ssl, &now);
  628. ssl->s3->server_random[0] = now.tv_sec >> 24;
  629. ssl->s3->server_random[1] = now.tv_sec >> 16;
  630. ssl->s3->server_random[2] = now.tv_sec >> 8;
  631. ssl->s3->server_random[3] = now.tv_sec;
  632. if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
  633. return ssl_hs_error;
  634. }
  635. // TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
  636. // 1.3 is finalized and we are not implementing a draft version.
  637. const SSL_SESSION *session = hs->new_session.get();
  638. if (ssl->session != NULL) {
  639. session = ssl->session;
  640. }
  641. ScopedCBB cbb;
  642. CBB body, session_id;
  643. if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) ||
  644. !CBB_add_u16(&body, ssl->version) ||
  645. !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
  646. !CBB_add_u8_length_prefixed(&body, &session_id) ||
  647. !CBB_add_bytes(&session_id, session->session_id,
  648. session->session_id_length) ||
  649. !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) ||
  650. !CBB_add_u8(&body, 0 /* no compression */) ||
  651. !ssl_add_serverhello_tlsext(hs, &body) ||
  652. !ssl_add_message_cbb(ssl, cbb.get())) {
  653. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  654. return ssl_hs_error;
  655. }
  656. if (ssl->session != NULL) {
  657. hs->state = state_send_server_finished;
  658. } else {
  659. hs->state = state_send_server_certificate;
  660. }
  661. return ssl_hs_ok;
  662. }
  663. static enum ssl_hs_wait_t do_send_server_certificate(SSL_HANDSHAKE *hs) {
  664. SSL *const ssl = hs->ssl;
  665. ScopedCBB cbb;
  666. if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  667. if (!ssl_has_certificate(ssl)) {
  668. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
  669. return ssl_hs_error;
  670. }
  671. if (!ssl3_output_cert_chain(ssl)) {
  672. return ssl_hs_error;
  673. }
  674. if (hs->certificate_status_expected) {
  675. CBB body, ocsp_response;
  676. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  677. SSL3_MT_CERTIFICATE_STATUS) ||
  678. !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
  679. !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
  680. !CBB_add_bytes(&ocsp_response,
  681. CRYPTO_BUFFER_data(ssl->cert->ocsp_response),
  682. CRYPTO_BUFFER_len(ssl->cert->ocsp_response)) ||
  683. !ssl_add_message_cbb(ssl, cbb.get())) {
  684. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  685. return ssl_hs_error;
  686. }
  687. }
  688. }
  689. // Assemble ServerKeyExchange parameters if needed.
  690. uint32_t alg_k = hs->new_cipher->algorithm_mkey;
  691. uint32_t alg_a = hs->new_cipher->algorithm_auth;
  692. if (ssl_cipher_requires_server_key_exchange(hs->new_cipher) ||
  693. ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
  694. // Pre-allocate enough room to comfortably fit an ECDHE public key. Prepend
  695. // the client and server randoms for the signing transcript.
  696. CBB child;
  697. if (!CBB_init(cbb.get(), SSL3_RANDOM_SIZE * 2 + 128) ||
  698. !CBB_add_bytes(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
  699. !CBB_add_bytes(cbb.get(), ssl->s3->server_random, SSL3_RANDOM_SIZE)) {
  700. return ssl_hs_error;
  701. }
  702. // PSK ciphers begin with an identity hint.
  703. if (alg_a & SSL_aPSK) {
  704. size_t len =
  705. (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
  706. if (!CBB_add_u16_length_prefixed(cbb.get(), &child) ||
  707. !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
  708. len)) {
  709. return ssl_hs_error;
  710. }
  711. }
  712. if (alg_k & SSL_kECDHE) {
  713. // Determine the group to use.
  714. uint16_t group_id;
  715. if (!tls1_get_shared_group(hs, &group_id)) {
  716. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  717. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  718. return ssl_hs_error;
  719. }
  720. hs->new_session->group_id = group_id;
  721. // Set up ECDH, generate a key, and emit the public half.
  722. hs->key_share = SSLKeyShare::Create(group_id);
  723. if (!hs->key_share ||
  724. !CBB_add_u8(cbb.get(), NAMED_CURVE_TYPE) ||
  725. !CBB_add_u16(cbb.get(), group_id) ||
  726. !CBB_add_u8_length_prefixed(cbb.get(), &child) ||
  727. !hs->key_share->Offer(&child)) {
  728. return ssl_hs_error;
  729. }
  730. } else {
  731. assert(alg_k & SSL_kPSK);
  732. }
  733. if (!CBB_finish(cbb.get(), &hs->server_params, &hs->server_params_len)) {
  734. return ssl_hs_error;
  735. }
  736. }
  737. hs->state = state_send_server_key_exchange;
  738. return ssl_hs_ok;
  739. }
  740. static enum ssl_hs_wait_t do_send_server_key_exchange(SSL_HANDSHAKE *hs) {
  741. SSL *const ssl = hs->ssl;
  742. if (hs->server_params_len == 0) {
  743. hs->state = state_send_server_hello_done;
  744. return ssl_hs_ok;
  745. }
  746. ScopedCBB cbb;
  747. CBB body, child;
  748. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  749. SSL3_MT_SERVER_KEY_EXCHANGE) ||
  750. // |hs->server_params| contains a prefix for signing.
  751. hs->server_params_len < 2 * SSL3_RANDOM_SIZE ||
  752. !CBB_add_bytes(&body, hs->server_params + 2 * SSL3_RANDOM_SIZE,
  753. hs->server_params_len - 2 * SSL3_RANDOM_SIZE)) {
  754. return ssl_hs_error;
  755. }
  756. // Add a signature.
  757. if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  758. if (!ssl_has_private_key(ssl)) {
  759. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  760. return ssl_hs_error;
  761. }
  762. // Determine the signature algorithm.
  763. uint16_t signature_algorithm;
  764. if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
  765. return ssl_hs_error;
  766. }
  767. if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
  768. if (!CBB_add_u16(&body, signature_algorithm)) {
  769. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  770. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  771. return ssl_hs_error;
  772. }
  773. }
  774. // Add space for the signature.
  775. const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
  776. uint8_t *ptr;
  777. if (!CBB_add_u16_length_prefixed(&body, &child) ||
  778. !CBB_reserve(&child, &ptr, max_sig_len)) {
  779. return ssl_hs_error;
  780. }
  781. size_t sig_len;
  782. switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
  783. signature_algorithm, hs->server_params,
  784. hs->server_params_len)) {
  785. case ssl_private_key_success:
  786. if (!CBB_did_write(&child, sig_len)) {
  787. return ssl_hs_error;
  788. }
  789. break;
  790. case ssl_private_key_failure:
  791. return ssl_hs_error;
  792. case ssl_private_key_retry:
  793. return ssl_hs_private_key_operation;
  794. }
  795. }
  796. if (!ssl_add_message_cbb(ssl, cbb.get())) {
  797. return ssl_hs_error;
  798. }
  799. OPENSSL_free(hs->server_params);
  800. hs->server_params = NULL;
  801. hs->server_params_len = 0;
  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. (ssl3_protocol_version(ssl) >= TLS1_VERSION &&
  816. !CBB_add_u8(&cert_types, TLS_CT_ECDSA_SIGN)) ||
  817. (ssl3_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. ssl3_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. ssl3_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. ssl3_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. ssl3_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. ssl3_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. ssl3_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. ssl_hs_wait_t ret = ssl_hs_error;
  931. uint8_t *premaster_secret = NULL;
  932. size_t premaster_secret_len = 0;
  933. uint8_t *decrypt_buf = NULL;
  934. SSLMessage msg;
  935. if (!ssl->method->get_message(ssl, &msg)) {
  936. return ssl_hs_read_message;
  937. }
  938. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_KEY_EXCHANGE)) {
  939. return ssl_hs_error;
  940. }
  941. CBS client_key_exchange = msg.body;
  942. uint32_t alg_k = hs->new_cipher->algorithm_mkey;
  943. uint32_t alg_a = hs->new_cipher->algorithm_auth;
  944. // If using a PSK key exchange, parse the PSK identity.
  945. if (alg_a & SSL_aPSK) {
  946. CBS psk_identity;
  947. // If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
  948. // then this is the only field in the message.
  949. if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
  950. ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
  951. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  952. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  953. goto err;
  954. }
  955. if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
  956. CBS_contains_zero_byte(&psk_identity)) {
  957. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  958. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  959. goto err;
  960. }
  961. if (!CBS_strdup(&psk_identity, &hs->new_session->psk_identity)) {
  962. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  963. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  964. goto err;
  965. }
  966. }
  967. // Depending on the key exchange method, compute |premaster_secret| and
  968. // |premaster_secret_len|.
  969. if (alg_k & SSL_kRSA) {
  970. CBS encrypted_premaster_secret;
  971. if (ssl->version > SSL3_VERSION) {
  972. if (!CBS_get_u16_length_prefixed(&client_key_exchange,
  973. &encrypted_premaster_secret) ||
  974. CBS_len(&client_key_exchange) != 0) {
  975. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  976. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  977. goto err;
  978. }
  979. } else {
  980. encrypted_premaster_secret = client_key_exchange;
  981. }
  982. // Allocate a buffer large enough for an RSA decryption.
  983. const size_t rsa_size = EVP_PKEY_size(hs->local_pubkey.get());
  984. decrypt_buf = (uint8_t *)OPENSSL_malloc(rsa_size);
  985. if (decrypt_buf == NULL) {
  986. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  987. goto err;
  988. }
  989. // Decrypt with no padding. PKCS#1 padding will be removed as part of the
  990. // timing-sensitive code below.
  991. size_t decrypt_len;
  992. switch (ssl_private_key_decrypt(hs, decrypt_buf, &decrypt_len, rsa_size,
  993. CBS_data(&encrypted_premaster_secret),
  994. CBS_len(&encrypted_premaster_secret))) {
  995. case ssl_private_key_success:
  996. break;
  997. case ssl_private_key_failure:
  998. goto err;
  999. case ssl_private_key_retry:
  1000. ret = ssl_hs_private_key_operation;
  1001. goto err;
  1002. }
  1003. if (decrypt_len != rsa_size) {
  1004. OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
  1005. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
  1006. goto err;
  1007. }
  1008. // Prepare a random premaster, to be used on invalid padding. See RFC 5246,
  1009. // section 7.4.7.1.
  1010. premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
  1011. premaster_secret = (uint8_t *)OPENSSL_malloc(premaster_secret_len);
  1012. if (premaster_secret == NULL) {
  1013. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1014. goto err;
  1015. }
  1016. if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
  1017. goto err;
  1018. }
  1019. // The smallest padded premaster is 11 bytes of overhead. Small keys are
  1020. // publicly invalid.
  1021. if (decrypt_len < 11 + premaster_secret_len) {
  1022. OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
  1023. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
  1024. goto err;
  1025. }
  1026. // Check the padding. See RFC 3447, section 7.2.2.
  1027. size_t padding_len = decrypt_len - premaster_secret_len;
  1028. uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
  1029. constant_time_eq_int_8(decrypt_buf[1], 2);
  1030. for (size_t i = 2; i < padding_len - 1; i++) {
  1031. good &= ~constant_time_is_zero_8(decrypt_buf[i]);
  1032. }
  1033. good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
  1034. // The premaster secret must begin with |client_version|. This too must be
  1035. // checked in constant time (http://eprint.iacr.org/2003/052/).
  1036. good &= constant_time_eq_8(decrypt_buf[padding_len],
  1037. (unsigned)(hs->client_version >> 8));
  1038. good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
  1039. (unsigned)(hs->client_version & 0xff));
  1040. // Select, in constant time, either the decrypted premaster or the random
  1041. // premaster based on |good|.
  1042. for (size_t i = 0; i < premaster_secret_len; i++) {
  1043. premaster_secret[i] = constant_time_select_8(
  1044. good, decrypt_buf[padding_len + i], premaster_secret[i]);
  1045. }
  1046. OPENSSL_free(decrypt_buf);
  1047. decrypt_buf = NULL;
  1048. } else if (alg_k & SSL_kECDHE) {
  1049. // Parse the ClientKeyExchange.
  1050. CBS peer_key;
  1051. if (!CBS_get_u8_length_prefixed(&client_key_exchange, &peer_key) ||
  1052. CBS_len(&client_key_exchange) != 0) {
  1053. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1054. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1055. goto err;
  1056. }
  1057. // Compute the premaster.
  1058. uint8_t alert = SSL_AD_DECODE_ERROR;
  1059. if (!hs->key_share->Finish(&premaster_secret, &premaster_secret_len, &alert,
  1060. CBS_data(&peer_key), CBS_len(&peer_key))) {
  1061. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  1062. goto err;
  1063. }
  1064. // The key exchange state may now be discarded.
  1065. hs->key_share.reset();
  1066. } else if (!(alg_k & SSL_kPSK)) {
  1067. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1068. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1069. goto err;
  1070. }
  1071. // For a PSK cipher suite, the actual pre-master secret is combined with the
  1072. // pre-shared key.
  1073. if (alg_a & SSL_aPSK) {
  1074. if (ssl->psk_server_callback == NULL) {
  1075. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1076. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1077. goto err;
  1078. }
  1079. // Look up the key for the identity.
  1080. uint8_t psk[PSK_MAX_PSK_LEN];
  1081. unsigned psk_len = ssl->psk_server_callback(
  1082. ssl, hs->new_session->psk_identity, psk, sizeof(psk));
  1083. if (psk_len > PSK_MAX_PSK_LEN) {
  1084. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1085. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1086. goto err;
  1087. } else if (psk_len == 0) {
  1088. // PSK related to the given identity not found.
  1089. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
  1090. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNKNOWN_PSK_IDENTITY);
  1091. goto err;
  1092. }
  1093. if (alg_k & SSL_kPSK) {
  1094. // In plain PSK, other_secret is a block of 0s with the same length as the
  1095. // pre-shared key.
  1096. premaster_secret_len = psk_len;
  1097. premaster_secret = (uint8_t *)OPENSSL_malloc(premaster_secret_len);
  1098. if (premaster_secret == NULL) {
  1099. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1100. goto err;
  1101. }
  1102. OPENSSL_memset(premaster_secret, 0, premaster_secret_len);
  1103. }
  1104. ScopedCBB new_premaster;
  1105. CBB child;
  1106. uint8_t *new_data;
  1107. size_t new_len;
  1108. if (!CBB_init(new_premaster.get(),
  1109. 2 + psk_len + 2 + premaster_secret_len) ||
  1110. !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
  1111. !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
  1112. !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
  1113. !CBB_add_bytes(&child, psk, psk_len) ||
  1114. !CBB_finish(new_premaster.get(), &new_data, &new_len)) {
  1115. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1116. goto err;
  1117. }
  1118. OPENSSL_cleanse(premaster_secret, premaster_secret_len);
  1119. OPENSSL_free(premaster_secret);
  1120. premaster_secret = new_data;
  1121. premaster_secret_len = new_len;
  1122. }
  1123. if (!ssl_hash_message(hs, msg)) {
  1124. goto err;
  1125. }
  1126. // Compute the master secret.
  1127. hs->new_session->master_key_length = tls1_generate_master_secret(
  1128. hs, hs->new_session->master_key, premaster_secret, premaster_secret_len);
  1129. if (hs->new_session->master_key_length == 0) {
  1130. goto err;
  1131. }
  1132. hs->new_session->extended_master_secret = hs->extended_master_secret;
  1133. ssl->method->next_message(ssl);
  1134. hs->state = state_read_client_certificate_verify;
  1135. ret = ssl_hs_ok;
  1136. err:
  1137. if (premaster_secret != NULL) {
  1138. OPENSSL_cleanse(premaster_secret, premaster_secret_len);
  1139. OPENSSL_free(premaster_secret);
  1140. }
  1141. OPENSSL_free(decrypt_buf);
  1142. return ret;
  1143. }
  1144. static enum ssl_hs_wait_t do_read_client_certificate_verify(SSL_HANDSHAKE *hs) {
  1145. SSL *const ssl = hs->ssl;
  1146. // Only RSA and ECDSA client certificates are supported, so a
  1147. // CertificateVerify is required if and only if there's a client certificate.
  1148. if (!hs->peer_pubkey) {
  1149. hs->transcript.FreeBuffer();
  1150. hs->state = state_read_change_cipher_spec;
  1151. return ssl_hs_ok;
  1152. }
  1153. SSLMessage msg;
  1154. if (!ssl->method->get_message(ssl, &msg)) {
  1155. return ssl_hs_read_message;
  1156. }
  1157. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY)) {
  1158. return ssl_hs_error;
  1159. }
  1160. CBS certificate_verify = msg.body, signature;
  1161. // Determine the signature algorithm.
  1162. uint16_t signature_algorithm = 0;
  1163. if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
  1164. if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
  1165. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1166. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1167. return ssl_hs_error;
  1168. }
  1169. uint8_t alert = SSL_AD_DECODE_ERROR;
  1170. if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
  1171. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  1172. return ssl_hs_error;
  1173. }
  1174. hs->new_session->peer_signature_algorithm = signature_algorithm;
  1175. } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
  1176. hs->peer_pubkey.get())) {
  1177. OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
  1178. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
  1179. return ssl_hs_error;
  1180. }
  1181. // Parse and verify the signature.
  1182. if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
  1183. CBS_len(&certificate_verify) != 0) {
  1184. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1185. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1186. return ssl_hs_error;
  1187. }
  1188. int sig_ok;
  1189. // The SSL3 construction for CertificateVerify does not decompose into a
  1190. // single final digest and signature, and must be special-cased.
  1191. if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
  1192. uint8_t digest[EVP_MAX_MD_SIZE];
  1193. size_t digest_len;
  1194. if (!hs->transcript.GetSSL3CertVerifyHash(
  1195. digest, &digest_len, hs->new_session.get(), signature_algorithm)) {
  1196. return ssl_hs_error;
  1197. }
  1198. UniquePtr<EVP_PKEY_CTX> pctx(
  1199. EVP_PKEY_CTX_new(hs->peer_pubkey.get(), nullptr));
  1200. sig_ok = pctx &&
  1201. EVP_PKEY_verify_init(pctx.get()) &&
  1202. EVP_PKEY_verify(pctx.get(), CBS_data(&signature),
  1203. CBS_len(&signature), digest, digest_len);
  1204. } else {
  1205. sig_ok = ssl_public_key_verify(
  1206. ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
  1207. hs->peer_pubkey.get(), hs->transcript.buffer_data(),
  1208. hs->transcript.buffer_len());
  1209. }
  1210. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  1211. sig_ok = 1;
  1212. ERR_clear_error();
  1213. #endif
  1214. if (!sig_ok) {
  1215. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
  1216. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
  1217. return ssl_hs_error;
  1218. }
  1219. // The handshake buffer is no longer necessary, and we may hash the current
  1220. // message.
  1221. hs->transcript.FreeBuffer();
  1222. if (!ssl_hash_message(hs, msg)) {
  1223. return ssl_hs_error;
  1224. }
  1225. ssl->method->next_message(ssl);
  1226. hs->state = state_read_change_cipher_spec;
  1227. return ssl_hs_ok;
  1228. }
  1229. static enum ssl_hs_wait_t do_read_change_cipher_spec(SSL_HANDSHAKE *hs) {
  1230. hs->state = state_process_change_cipher_spec;
  1231. return ssl_hs_read_change_cipher_spec;
  1232. }
  1233. static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
  1234. if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_READ)) {
  1235. return ssl_hs_error;
  1236. }
  1237. hs->state = state_read_next_proto;
  1238. return ssl_hs_ok;
  1239. }
  1240. static enum ssl_hs_wait_t do_read_next_proto(SSL_HANDSHAKE *hs) {
  1241. SSL *const ssl = hs->ssl;
  1242. if (!hs->next_proto_neg_seen) {
  1243. hs->state = state_read_channel_id;
  1244. return ssl_hs_ok;
  1245. }
  1246. SSLMessage msg;
  1247. if (!ssl->method->get_message(ssl, &msg)) {
  1248. return ssl_hs_read_message;
  1249. }
  1250. if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEXT_PROTO) ||
  1251. !ssl_hash_message(hs, msg)) {
  1252. return ssl_hs_error;
  1253. }
  1254. CBS next_protocol = msg.body, selected_protocol, padding;
  1255. if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
  1256. !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
  1257. CBS_len(&next_protocol) != 0) {
  1258. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1259. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1260. return ssl_hs_error;
  1261. }
  1262. if (!CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
  1263. &ssl->s3->next_proto_negotiated_len)) {
  1264. return ssl_hs_error;
  1265. }
  1266. ssl->method->next_message(ssl);
  1267. hs->state = state_read_channel_id;
  1268. return ssl_hs_ok;
  1269. }
  1270. static enum ssl_hs_wait_t do_read_channel_id(SSL_HANDSHAKE *hs) {
  1271. SSL *const ssl = hs->ssl;
  1272. if (!ssl->s3->tlsext_channel_id_valid) {
  1273. hs->state = state_read_client_finished;
  1274. return ssl_hs_ok;
  1275. }
  1276. SSLMessage msg;
  1277. if (!ssl->method->get_message(ssl, &msg)) {
  1278. return ssl_hs_read_message;
  1279. }
  1280. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) ||
  1281. !tls1_verify_channel_id(hs, msg) ||
  1282. !ssl_hash_message(hs, msg)) {
  1283. return ssl_hs_error;
  1284. }
  1285. ssl->method->next_message(ssl);
  1286. hs->state = state_read_client_finished;
  1287. return ssl_hs_ok;
  1288. }
  1289. static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) {
  1290. SSL *const ssl = hs->ssl;
  1291. enum ssl_hs_wait_t wait = ssl_get_finished(hs);
  1292. if (wait != ssl_hs_ok) {
  1293. return wait;
  1294. }
  1295. if (ssl->session != NULL) {
  1296. hs->state = state_finish_server_handshake;
  1297. } else {
  1298. hs->state = state_send_server_finished;
  1299. }
  1300. // If this is a full handshake with ChannelID then record the handshake
  1301. // hashes in |hs->new_session| in case we need them to verify a
  1302. // ChannelID signature on a resumption of this session in the future.
  1303. if (ssl->session == NULL && ssl->s3->tlsext_channel_id_valid &&
  1304. !tls1_record_handshake_hashes_for_channel_id(hs)) {
  1305. return ssl_hs_error;
  1306. }
  1307. return ssl_hs_ok;
  1308. }
  1309. static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) {
  1310. SSL *const ssl = hs->ssl;
  1311. if (hs->ticket_expected) {
  1312. const SSL_SESSION *session;
  1313. UniquePtr<SSL_SESSION> session_copy;
  1314. if (ssl->session == NULL) {
  1315. // Fix the timeout to measure from the ticket issuance time.
  1316. ssl_session_rebase_time(ssl, hs->new_session.get());
  1317. session = hs->new_session.get();
  1318. } else {
  1319. // We are renewing an existing session. Duplicate the session to adjust
  1320. // the timeout.
  1321. session_copy = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
  1322. if (!session_copy) {
  1323. return ssl_hs_error;
  1324. }
  1325. ssl_session_rebase_time(ssl, session_copy.get());
  1326. session = session_copy.get();
  1327. }
  1328. ScopedCBB cbb;
  1329. CBB body, ticket;
  1330. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  1331. SSL3_MT_NEW_SESSION_TICKET) ||
  1332. !CBB_add_u32(&body, session->timeout) ||
  1333. !CBB_add_u16_length_prefixed(&body, &ticket) ||
  1334. !ssl_encrypt_ticket(ssl, &ticket, session) ||
  1335. !ssl_add_message_cbb(ssl, cbb.get())) {
  1336. return ssl_hs_error;
  1337. }
  1338. }
  1339. if (!ssl->method->add_change_cipher_spec(ssl) ||
  1340. !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_WRITE) ||
  1341. !ssl3_send_finished(hs)) {
  1342. return ssl_hs_error;
  1343. }
  1344. if (ssl->session != NULL) {
  1345. hs->state = state_read_change_cipher_spec;
  1346. } else {
  1347. hs->state = state_finish_server_handshake;
  1348. }
  1349. return ssl_hs_flush;
  1350. }
  1351. static enum ssl_hs_wait_t do_finish_server_handshake(SSL_HANDSHAKE *hs) {
  1352. SSL *const ssl = hs->ssl;
  1353. ssl->method->on_handshake_complete(ssl);
  1354. // If we aren't retaining peer certificates then we can discard it now.
  1355. if (hs->new_session != NULL && ssl->retain_only_sha256_of_client_certs) {
  1356. sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
  1357. hs->new_session->certs = NULL;
  1358. ssl->ctx->x509_method->session_clear(hs->new_session.get());
  1359. }
  1360. SSL_SESSION_free(ssl->s3->established_session);
  1361. if (ssl->session != NULL) {
  1362. SSL_SESSION_up_ref(ssl->session);
  1363. ssl->s3->established_session = ssl->session;
  1364. } else {
  1365. ssl->s3->established_session = hs->new_session.release();
  1366. ssl->s3->established_session->not_resumable = 0;
  1367. }
  1368. ssl->s3->initial_handshake_complete = 1;
  1369. ssl_update_cache(hs, SSL_SESS_CACHE_SERVER);
  1370. hs->state = state_done;
  1371. return ssl_hs_ok;
  1372. }
  1373. enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs) {
  1374. while (hs->state != state_done) {
  1375. enum ssl_hs_wait_t ret = ssl_hs_error;
  1376. enum ssl_server_hs_state_t state =
  1377. static_cast<enum ssl_server_hs_state_t>(hs->state);
  1378. switch (state) {
  1379. case state_start_accept:
  1380. ret = do_start_accept(hs);
  1381. break;
  1382. case state_read_client_hello:
  1383. ret = do_read_client_hello(hs);
  1384. break;
  1385. case state_select_certificate:
  1386. ret = do_select_certificate(hs);
  1387. break;
  1388. case state_tls13:
  1389. ret = do_tls13(hs);
  1390. break;
  1391. case state_select_parameters:
  1392. ret = do_select_parameters(hs);
  1393. break;
  1394. case state_send_server_hello:
  1395. ret = do_send_server_hello(hs);
  1396. break;
  1397. case state_send_server_certificate:
  1398. ret = do_send_server_certificate(hs);
  1399. break;
  1400. case state_send_server_key_exchange:
  1401. ret = do_send_server_key_exchange(hs);
  1402. break;
  1403. case state_send_server_hello_done:
  1404. ret = do_send_server_hello_done(hs);
  1405. break;
  1406. case state_read_client_certificate:
  1407. ret = do_read_client_certificate(hs);
  1408. break;
  1409. case state_verify_client_certificate:
  1410. ret = do_verify_client_certificate(hs);
  1411. break;
  1412. case state_read_client_key_exchange:
  1413. ret = do_read_client_key_exchange(hs);
  1414. break;
  1415. case state_read_client_certificate_verify:
  1416. ret = do_read_client_certificate_verify(hs);
  1417. break;
  1418. case state_read_change_cipher_spec:
  1419. ret = do_read_change_cipher_spec(hs);
  1420. break;
  1421. case state_process_change_cipher_spec:
  1422. ret = do_process_change_cipher_spec(hs);
  1423. break;
  1424. case state_read_next_proto:
  1425. ret = do_read_next_proto(hs);
  1426. break;
  1427. case state_read_channel_id:
  1428. ret = do_read_channel_id(hs);
  1429. break;
  1430. case state_read_client_finished:
  1431. ret = do_read_client_finished(hs);
  1432. break;
  1433. case state_send_server_finished:
  1434. ret = do_send_server_finished(hs);
  1435. break;
  1436. case state_finish_server_handshake:
  1437. ret = do_finish_server_handshake(hs);
  1438. break;
  1439. case state_done:
  1440. ret = ssl_hs_ok;
  1441. break;
  1442. }
  1443. if (hs->state != state) {
  1444. ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1);
  1445. }
  1446. if (ret != ssl_hs_ok) {
  1447. return ret;
  1448. }
  1449. }
  1450. ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
  1451. return ssl_hs_ok;
  1452. }
  1453. const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs) {
  1454. enum ssl_server_hs_state_t state =
  1455. static_cast<enum ssl_server_hs_state_t>(hs->state);
  1456. switch (state) {
  1457. case state_start_accept:
  1458. return "TLS server start_accept";
  1459. case state_read_client_hello:
  1460. return "TLS server read_client_hello";
  1461. case state_select_certificate:
  1462. return "TLS server select_certificate";
  1463. case state_tls13:
  1464. return tls13_server_handshake_state(hs);
  1465. case state_select_parameters:
  1466. return "TLS server select_parameters";
  1467. case state_send_server_hello:
  1468. return "TLS server send_server_hello";
  1469. case state_send_server_certificate:
  1470. return "TLS server send_server_certificate";
  1471. case state_send_server_key_exchange:
  1472. return "TLS server send_server_key_exchange";
  1473. case state_send_server_hello_done:
  1474. return "TLS server send_server_hello_done";
  1475. case state_read_client_certificate:
  1476. return "TLS server read_client_certificate";
  1477. case state_verify_client_certificate:
  1478. return "TLS server verify_client_certificate";
  1479. case state_read_client_key_exchange:
  1480. return "TLS server read_client_key_exchange";
  1481. case state_read_client_certificate_verify:
  1482. return "TLS server read_client_certificate_verify";
  1483. case state_read_change_cipher_spec:
  1484. return "TLS server read_change_cipher_spec";
  1485. case state_process_change_cipher_spec:
  1486. return "TLS server process_change_cipher_spec";
  1487. case state_read_next_proto:
  1488. return "TLS server read_next_proto";
  1489. case state_read_channel_id:
  1490. return "TLS server read_channel_id";
  1491. case state_read_client_finished:
  1492. return "TLS server read_client_finished";
  1493. case state_send_server_finished:
  1494. return "TLS server send_server_finished";
  1495. case state_finish_server_handshake:
  1496. return "TLS server finish_server_handshake";
  1497. case state_done:
  1498. return "TLS server done";
  1499. }
  1500. return "TLS server unknown";
  1501. }
  1502. }