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

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