Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

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