No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

1628 líneas
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. bool ssl_client_cipher_list_contains_cipher(
  169. const SSL_CLIENT_HELLO *client_hello, 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 false;
  177. }
  178. if (got_id == id) {
  179. return true;
  180. }
  181. }
  182. return false;
  183. }
  184. static bool 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 false;
  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 false;
  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 false;
  245. }
  246. return true;
  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 = hs->config->cipher_list
  480. ? hs->config->cipher_list.get()
  481. : ssl->ctx->cipher_list.get();
  482. hs->new_cipher = ssl3_choose_cipher(hs, &client_hello, prefs);
  483. if (hs->new_cipher == NULL) {
  484. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
  485. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  486. return ssl_hs_error;
  487. }
  488. hs->state = state12_select_parameters;
  489. return ssl_hs_ok;
  490. }
  491. static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) {
  492. enum ssl_hs_wait_t wait = tls13_server_handshake(hs);
  493. if (wait == ssl_hs_ok) {
  494. hs->state = state12_finish_server_handshake;
  495. return ssl_hs_ok;
  496. }
  497. return wait;
  498. }
  499. static enum ssl_hs_wait_t do_select_parameters(SSL_HANDSHAKE *hs) {
  500. SSL *const ssl = hs->ssl;
  501. SSLMessage msg;
  502. if (!ssl->method->get_message(ssl, &msg)) {
  503. return ssl_hs_read_message;
  504. }
  505. SSL_CLIENT_HELLO client_hello;
  506. if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
  507. return ssl_hs_error;
  508. }
  509. // Determine whether we are doing session resumption.
  510. UniquePtr<SSL_SESSION> session;
  511. bool tickets_supported = false, renew_ticket = false;
  512. enum ssl_hs_wait_t wait = ssl_get_prev_session(
  513. hs, &session, &tickets_supported, &renew_ticket, &client_hello);
  514. if (wait != ssl_hs_ok) {
  515. return wait;
  516. }
  517. if (session) {
  518. if (session->extended_master_secret && !hs->extended_master_secret) {
  519. // A ClientHello without EMS that attempts to resume a session with EMS
  520. // is fatal to the connection.
  521. OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
  522. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  523. return ssl_hs_error;
  524. }
  525. if (!ssl_session_is_resumable(hs, session.get()) ||
  526. // If the client offers the EMS extension, but the previous session
  527. // didn't use it, then negotiate a new session.
  528. hs->extended_master_secret != session->extended_master_secret) {
  529. session.reset();
  530. }
  531. }
  532. if (session) {
  533. // Use the old session.
  534. hs->ticket_expected = renew_ticket;
  535. ssl->session = std::move(session);
  536. ssl->s3->session_reused = true;
  537. } else {
  538. hs->ticket_expected = tickets_supported;
  539. ssl_set_session(ssl, NULL);
  540. if (!ssl_get_new_session(hs, 1 /* server */)) {
  541. return ssl_hs_error;
  542. }
  543. // Clear the session ID if we want the session to be single-use.
  544. if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
  545. hs->new_session->session_id_length = 0;
  546. }
  547. }
  548. if (ssl->ctx->dos_protection_cb != NULL &&
  549. ssl->ctx->dos_protection_cb(&client_hello) == 0) {
  550. // Connection rejected for DOS reasons.
  551. OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
  552. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  553. return ssl_hs_error;
  554. }
  555. if (ssl->session == NULL) {
  556. hs->new_session->cipher = hs->new_cipher;
  557. // Determine whether to request a client certificate.
  558. hs->cert_request = !!(hs->config->verify_mode & SSL_VERIFY_PEER);
  559. // Only request a certificate if Channel ID isn't negotiated.
  560. if ((hs->config->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
  561. ssl->s3->channel_id_valid) {
  562. hs->cert_request = false;
  563. }
  564. // CertificateRequest may only be sent in certificate-based ciphers.
  565. if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  566. hs->cert_request = false;
  567. }
  568. if (!hs->cert_request) {
  569. // OpenSSL returns X509_V_OK when no certificates are requested. This is
  570. // classed by them as a bug, but it's assumed by at least NGINX.
  571. hs->new_session->verify_result = X509_V_OK;
  572. }
  573. }
  574. // HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
  575. // deferred. Complete it now.
  576. uint8_t alert = SSL_AD_DECODE_ERROR;
  577. if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) {
  578. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  579. return ssl_hs_error;
  580. }
  581. // Now that all parameters are known, initialize the handshake hash and hash
  582. // the ClientHello.
  583. if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) ||
  584. !ssl_hash_message(hs, msg)) {
  585. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  586. return ssl_hs_error;
  587. }
  588. // Handback includes the whole handshake transcript, so we cannot free the
  589. // transcript buffer in the handback case.
  590. if (!hs->cert_request && !hs->handback) {
  591. hs->transcript.FreeBuffer();
  592. }
  593. ssl->method->next_message(ssl);
  594. hs->state = state12_send_server_hello;
  595. return ssl_hs_ok;
  596. }
  597. static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) {
  598. SSL *const ssl = hs->ssl;
  599. // We only accept ChannelIDs on connections with ECDHE in order to avoid a
  600. // known attack while we fix ChannelID itself.
  601. if (ssl->s3->channel_id_valid &&
  602. (hs->new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
  603. ssl->s3->channel_id_valid = false;
  604. }
  605. // If this is a resumption and the original handshake didn't support
  606. // ChannelID then we didn't record the original handshake hashes in the
  607. // session and so cannot resume with ChannelIDs.
  608. if (ssl->session != NULL &&
  609. ssl->session->original_handshake_hash_len == 0) {
  610. ssl->s3->channel_id_valid = false;
  611. }
  612. struct OPENSSL_timeval now;
  613. ssl_get_current_time(ssl, &now);
  614. ssl->s3->server_random[0] = now.tv_sec >> 24;
  615. ssl->s3->server_random[1] = now.tv_sec >> 16;
  616. ssl->s3->server_random[2] = now.tv_sec >> 8;
  617. ssl->s3->server_random[3] = now.tv_sec;
  618. if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
  619. return ssl_hs_error;
  620. }
  621. // Implement the TLS 1.3 anti-downgrade feature, but with a different value.
  622. //
  623. // For draft TLS 1.3 versions, it is not safe to deploy this feature. However,
  624. // some TLS terminators are non-compliant and copy the origin server's value,
  625. // so we wish to measure eventual compatibility impact.
  626. if (hs->max_version >= TLS1_3_VERSION) {
  627. OPENSSL_memcpy(ssl->s3->server_random + SSL3_RANDOM_SIZE -
  628. sizeof(kDraftDowngradeRandom),
  629. kDraftDowngradeRandom, sizeof(kDraftDowngradeRandom));
  630. }
  631. const SSL_SESSION *session = hs->new_session.get();
  632. if (ssl->session != nullptr) {
  633. session = ssl->session.get();
  634. }
  635. ScopedCBB cbb;
  636. CBB body, session_id;
  637. if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) ||
  638. !CBB_add_u16(&body, ssl->version) ||
  639. !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
  640. !CBB_add_u8_length_prefixed(&body, &session_id) ||
  641. !CBB_add_bytes(&session_id, session->session_id,
  642. session->session_id_length) ||
  643. !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) ||
  644. !CBB_add_u8(&body, 0 /* no compression */) ||
  645. !ssl_add_serverhello_tlsext(hs, &body) ||
  646. !ssl_add_message_cbb(ssl, cbb.get())) {
  647. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  648. return ssl_hs_error;
  649. }
  650. if (ssl->session != NULL) {
  651. hs->state = state12_send_server_finished;
  652. } else {
  653. hs->state = state12_send_server_certificate;
  654. }
  655. return ssl_hs_ok;
  656. }
  657. static enum ssl_hs_wait_t do_send_server_certificate(SSL_HANDSHAKE *hs) {
  658. SSL *const ssl = hs->ssl;
  659. ScopedCBB cbb;
  660. if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  661. if (!ssl_has_certificate(hs->config)) {
  662. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
  663. return ssl_hs_error;
  664. }
  665. if (!ssl_output_cert_chain(hs)) {
  666. return ssl_hs_error;
  667. }
  668. if (hs->certificate_status_expected) {
  669. CBB body, ocsp_response;
  670. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  671. SSL3_MT_CERTIFICATE_STATUS) ||
  672. !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
  673. !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
  674. !CBB_add_bytes(
  675. &ocsp_response,
  676. CRYPTO_BUFFER_data(hs->config->cert->ocsp_response.get()),
  677. CRYPTO_BUFFER_len(hs->config->cert->ocsp_response.get())) ||
  678. !ssl_add_message_cbb(ssl, cbb.get())) {
  679. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  680. return ssl_hs_error;
  681. }
  682. }
  683. }
  684. // Assemble ServerKeyExchange parameters if needed.
  685. uint32_t alg_k = hs->new_cipher->algorithm_mkey;
  686. uint32_t alg_a = hs->new_cipher->algorithm_auth;
  687. if (ssl_cipher_requires_server_key_exchange(hs->new_cipher) ||
  688. ((alg_a & SSL_aPSK) && hs->config->psk_identity_hint)) {
  689. // Pre-allocate enough room to comfortably fit an ECDHE public key. Prepend
  690. // the client and server randoms for the signing transcript.
  691. CBB child;
  692. if (!CBB_init(cbb.get(), SSL3_RANDOM_SIZE * 2 + 128) ||
  693. !CBB_add_bytes(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
  694. !CBB_add_bytes(cbb.get(), ssl->s3->server_random, SSL3_RANDOM_SIZE)) {
  695. return ssl_hs_error;
  696. }
  697. // PSK ciphers begin with an identity hint.
  698. if (alg_a & SSL_aPSK) {
  699. size_t len = hs->config->psk_identity_hint == nullptr
  700. ? 0
  701. : strlen(hs->config->psk_identity_hint.get());
  702. if (!CBB_add_u16_length_prefixed(cbb.get(), &child) ||
  703. !CBB_add_bytes(&child,
  704. (const uint8_t *)hs->config->psk_identity_hint.get(),
  705. len)) {
  706. return ssl_hs_error;
  707. }
  708. }
  709. if (alg_k & SSL_kECDHE) {
  710. // Determine the group to use.
  711. uint16_t group_id;
  712. if (!tls1_get_shared_group(hs, &group_id)) {
  713. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  714. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  715. return ssl_hs_error;
  716. }
  717. hs->new_session->group_id = group_id;
  718. // Set up ECDH, generate a key, and emit the public half.
  719. hs->key_share = SSLKeyShare::Create(group_id);
  720. if (!hs->key_share ||
  721. !CBB_add_u8(cbb.get(), NAMED_CURVE_TYPE) ||
  722. !CBB_add_u16(cbb.get(), group_id) ||
  723. !CBB_add_u8_length_prefixed(cbb.get(), &child) ||
  724. !hs->key_share->Offer(&child)) {
  725. return ssl_hs_error;
  726. }
  727. } else {
  728. assert(alg_k & SSL_kPSK);
  729. }
  730. if (!CBBFinishArray(cbb.get(), &hs->server_params)) {
  731. return ssl_hs_error;
  732. }
  733. }
  734. hs->state = state12_send_server_key_exchange;
  735. return ssl_hs_ok;
  736. }
  737. static enum ssl_hs_wait_t do_send_server_key_exchange(SSL_HANDSHAKE *hs) {
  738. SSL *const ssl = hs->ssl;
  739. if (hs->server_params.size() == 0) {
  740. hs->state = state12_send_server_hello_done;
  741. return ssl_hs_ok;
  742. }
  743. ScopedCBB cbb;
  744. CBB body, child;
  745. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  746. SSL3_MT_SERVER_KEY_EXCHANGE) ||
  747. // |hs->server_params| contains a prefix for signing.
  748. hs->server_params.size() < 2 * SSL3_RANDOM_SIZE ||
  749. !CBB_add_bytes(&body, hs->server_params.data() + 2 * SSL3_RANDOM_SIZE,
  750. hs->server_params.size() - 2 * SSL3_RANDOM_SIZE)) {
  751. return ssl_hs_error;
  752. }
  753. // Add a signature.
  754. if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  755. if (!ssl_has_private_key(hs->config)) {
  756. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  757. return ssl_hs_error;
  758. }
  759. // Determine the signature algorithm.
  760. uint16_t signature_algorithm;
  761. if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
  762. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  763. return ssl_hs_error;
  764. }
  765. if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
  766. if (!CBB_add_u16(&body, signature_algorithm)) {
  767. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  768. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  769. return ssl_hs_error;
  770. }
  771. }
  772. // Add space for the signature.
  773. const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
  774. uint8_t *ptr;
  775. if (!CBB_add_u16_length_prefixed(&body, &child) ||
  776. !CBB_reserve(&child, &ptr, max_sig_len)) {
  777. return ssl_hs_error;
  778. }
  779. size_t sig_len;
  780. switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
  781. signature_algorithm, hs->server_params)) {
  782. case ssl_private_key_success:
  783. if (!CBB_did_write(&child, sig_len)) {
  784. return ssl_hs_error;
  785. }
  786. break;
  787. case ssl_private_key_failure:
  788. return ssl_hs_error;
  789. case ssl_private_key_retry:
  790. return ssl_hs_private_key_operation;
  791. }
  792. }
  793. if (!ssl_add_message_cbb(ssl, cbb.get())) {
  794. return ssl_hs_error;
  795. }
  796. hs->server_params.Reset();
  797. hs->state = state12_send_server_hello_done;
  798. return ssl_hs_ok;
  799. }
  800. static enum ssl_hs_wait_t do_send_server_hello_done(SSL_HANDSHAKE *hs) {
  801. SSL *const ssl = hs->ssl;
  802. ScopedCBB cbb;
  803. CBB body;
  804. if (hs->cert_request) {
  805. CBB cert_types, sigalgs_cbb;
  806. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  807. SSL3_MT_CERTIFICATE_REQUEST) ||
  808. !CBB_add_u8_length_prefixed(&body, &cert_types) ||
  809. !CBB_add_u8(&cert_types, SSL3_CT_RSA_SIGN) ||
  810. !CBB_add_u8(&cert_types, TLS_CT_ECDSA_SIGN) ||
  811. // TLS 1.2 has no way to specify different signature algorithms for
  812. // certificates and the online signature, so emit the more restrictive
  813. // certificate list.
  814. (ssl_protocol_version(ssl) >= TLS1_2_VERSION &&
  815. (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb) ||
  816. !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb, true /* certs */))) ||
  817. !ssl_add_client_CA_list(hs, &body) ||
  818. !ssl_add_message_cbb(ssl, cbb.get())) {
  819. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  820. return ssl_hs_error;
  821. }
  822. }
  823. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  824. SSL3_MT_SERVER_HELLO_DONE) ||
  825. !ssl_add_message_cbb(ssl, cbb.get())) {
  826. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  827. return ssl_hs_error;
  828. }
  829. hs->state = state12_read_client_certificate;
  830. return ssl_hs_flush;
  831. }
  832. static enum ssl_hs_wait_t do_read_client_certificate(SSL_HANDSHAKE *hs) {
  833. SSL *const ssl = hs->ssl;
  834. if (hs->handback && hs->new_cipher->algorithm_mkey == SSL_kECDHE) {
  835. return ssl_hs_handback;
  836. }
  837. if (!hs->cert_request) {
  838. hs->state = state12_verify_client_certificate;
  839. return ssl_hs_ok;
  840. }
  841. SSLMessage msg;
  842. if (!ssl->method->get_message(ssl, &msg)) {
  843. return ssl_hs_read_message;
  844. }
  845. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE)) {
  846. return ssl_hs_error;
  847. }
  848. if (!ssl_hash_message(hs, msg)) {
  849. return ssl_hs_error;
  850. }
  851. CBS certificate_msg = msg.body;
  852. uint8_t alert = SSL_AD_DECODE_ERROR;
  853. if (!ssl_parse_cert_chain(&alert, &hs->new_session->certs, &hs->peer_pubkey,
  854. hs->config->retain_only_sha256_of_client_certs
  855. ? hs->new_session->peer_sha256
  856. : nullptr,
  857. &certificate_msg, ssl->ctx->pool)) {
  858. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  859. return ssl_hs_error;
  860. }
  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.get()) == 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.get()) > 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. char *raw = nullptr;
  931. if (!CBS_strdup(&psk_identity, &raw)) {
  932. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  933. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  934. return ssl_hs_error;
  935. }
  936. hs->new_session->psk_identity.reset(raw);
  937. }
  938. // Depending on the key exchange method, compute |premaster_secret|.
  939. Array<uint8_t> premaster_secret;
  940. if (alg_k & SSL_kRSA) {
  941. CBS encrypted_premaster_secret;
  942. if (!CBS_get_u16_length_prefixed(&client_key_exchange,
  943. &encrypted_premaster_secret) ||
  944. CBS_len(&client_key_exchange) != 0) {
  945. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  946. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  947. return ssl_hs_error;
  948. }
  949. // Allocate a buffer large enough for an RSA decryption.
  950. Array<uint8_t> decrypt_buf;
  951. if (!decrypt_buf.Init(EVP_PKEY_size(hs->local_pubkey.get()))) {
  952. return ssl_hs_error;
  953. }
  954. // Decrypt with no padding. PKCS#1 padding will be removed as part of the
  955. // timing-sensitive code below.
  956. size_t decrypt_len;
  957. switch (ssl_private_key_decrypt(hs, decrypt_buf.data(), &decrypt_len,
  958. decrypt_buf.size(),
  959. encrypted_premaster_secret)) {
  960. case ssl_private_key_success:
  961. break;
  962. case ssl_private_key_failure:
  963. return ssl_hs_error;
  964. case ssl_private_key_retry:
  965. return ssl_hs_private_key_operation;
  966. }
  967. if (decrypt_len != decrypt_buf.size()) {
  968. OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
  969. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
  970. return ssl_hs_error;
  971. }
  972. // Prepare a random premaster, to be used on invalid padding. See RFC 5246,
  973. // section 7.4.7.1.
  974. if (!premaster_secret.Init(SSL_MAX_MASTER_KEY_LENGTH) ||
  975. !RAND_bytes(premaster_secret.data(), premaster_secret.size())) {
  976. return ssl_hs_error;
  977. }
  978. // The smallest padded premaster is 11 bytes of overhead. Small keys are
  979. // publicly invalid.
  980. if (decrypt_len < 11 + premaster_secret.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. // Check the padding. See RFC 3447, section 7.2.2.
  986. size_t padding_len = decrypt_len - premaster_secret.size();
  987. uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
  988. constant_time_eq_int_8(decrypt_buf[1], 2);
  989. for (size_t i = 2; i < padding_len - 1; i++) {
  990. good &= ~constant_time_is_zero_8(decrypt_buf[i]);
  991. }
  992. good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
  993. // The premaster secret must begin with |client_version|. This too must be
  994. // checked in constant time (http://eprint.iacr.org/2003/052/).
  995. good &= constant_time_eq_8(decrypt_buf[padding_len],
  996. (unsigned)(hs->client_version >> 8));
  997. good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
  998. (unsigned)(hs->client_version & 0xff));
  999. // Select, in constant time, either the decrypted premaster or the random
  1000. // premaster based on |good|.
  1001. for (size_t i = 0; i < premaster_secret.size(); i++) {
  1002. premaster_secret[i] = constant_time_select_8(
  1003. good, decrypt_buf[padding_len + i], premaster_secret[i]);
  1004. }
  1005. } else if (alg_k & SSL_kECDHE) {
  1006. // Parse the ClientKeyExchange.
  1007. CBS peer_key;
  1008. if (!CBS_get_u8_length_prefixed(&client_key_exchange, &peer_key) ||
  1009. CBS_len(&client_key_exchange) != 0) {
  1010. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1011. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1012. return ssl_hs_error;
  1013. }
  1014. // Compute the premaster.
  1015. uint8_t alert = SSL_AD_DECODE_ERROR;
  1016. if (!hs->key_share->Finish(&premaster_secret, &alert, peer_key)) {
  1017. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  1018. return ssl_hs_error;
  1019. }
  1020. // The key exchange state may now be discarded.
  1021. hs->key_share.reset();
  1022. } else if (!(alg_k & SSL_kPSK)) {
  1023. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1024. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1025. return ssl_hs_error;
  1026. }
  1027. // For a PSK cipher suite, the actual pre-master secret is combined with the
  1028. // pre-shared key.
  1029. if (alg_a & SSL_aPSK) {
  1030. if (hs->config->psk_server_callback == NULL) {
  1031. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1032. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1033. return ssl_hs_error;
  1034. }
  1035. // Look up the key for the identity.
  1036. uint8_t psk[PSK_MAX_PSK_LEN];
  1037. unsigned psk_len = hs->config->psk_server_callback(
  1038. ssl, hs->new_session->psk_identity.get(), psk, sizeof(psk));
  1039. if (psk_len > PSK_MAX_PSK_LEN) {
  1040. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1041. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1042. return ssl_hs_error;
  1043. } else if (psk_len == 0) {
  1044. // PSK related to the given identity not found.
  1045. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
  1046. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNKNOWN_PSK_IDENTITY);
  1047. return ssl_hs_error;
  1048. }
  1049. if (alg_k & SSL_kPSK) {
  1050. // In plain PSK, other_secret is a block of 0s with the same length as the
  1051. // pre-shared key.
  1052. if (!premaster_secret.Init(psk_len)) {
  1053. return ssl_hs_error;
  1054. }
  1055. OPENSSL_memset(premaster_secret.data(), 0, premaster_secret.size());
  1056. }
  1057. ScopedCBB new_premaster;
  1058. CBB child;
  1059. if (!CBB_init(new_premaster.get(),
  1060. 2 + psk_len + 2 + premaster_secret.size()) ||
  1061. !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
  1062. !CBB_add_bytes(&child, premaster_secret.data(),
  1063. premaster_secret.size()) ||
  1064. !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
  1065. !CBB_add_bytes(&child, psk, psk_len) ||
  1066. !CBBFinishArray(new_premaster.get(), &premaster_secret)) {
  1067. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1068. return ssl_hs_error;
  1069. }
  1070. }
  1071. if (!ssl_hash_message(hs, msg)) {
  1072. return ssl_hs_error;
  1073. }
  1074. // Compute the master secret.
  1075. hs->new_session->master_key_length = tls1_generate_master_secret(
  1076. hs, hs->new_session->master_key, premaster_secret);
  1077. if (hs->new_session->master_key_length == 0) {
  1078. return ssl_hs_error;
  1079. }
  1080. hs->new_session->extended_master_secret = hs->extended_master_secret;
  1081. ssl->method->next_message(ssl);
  1082. hs->state = state12_read_client_certificate_verify;
  1083. return ssl_hs_ok;
  1084. }
  1085. static enum ssl_hs_wait_t do_read_client_certificate_verify(SSL_HANDSHAKE *hs) {
  1086. SSL *const ssl = hs->ssl;
  1087. // Only RSA and ECDSA client certificates are supported, so a
  1088. // CertificateVerify is required if and only if there's a client certificate.
  1089. if (!hs->peer_pubkey) {
  1090. hs->transcript.FreeBuffer();
  1091. hs->state = state12_read_change_cipher_spec;
  1092. return ssl_hs_ok;
  1093. }
  1094. SSLMessage msg;
  1095. if (!ssl->method->get_message(ssl, &msg)) {
  1096. return ssl_hs_read_message;
  1097. }
  1098. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY)) {
  1099. return ssl_hs_error;
  1100. }
  1101. CBS certificate_verify = msg.body, signature;
  1102. // Determine the signature algorithm.
  1103. uint16_t signature_algorithm = 0;
  1104. if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
  1105. if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
  1106. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1107. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1108. return ssl_hs_error;
  1109. }
  1110. uint8_t alert = SSL_AD_DECODE_ERROR;
  1111. if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
  1112. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  1113. return ssl_hs_error;
  1114. }
  1115. hs->new_session->peer_signature_algorithm = signature_algorithm;
  1116. } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
  1117. hs->peer_pubkey.get())) {
  1118. OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
  1119. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
  1120. return ssl_hs_error;
  1121. }
  1122. // Parse and verify the signature.
  1123. if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
  1124. CBS_len(&certificate_verify) != 0) {
  1125. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1126. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1127. return ssl_hs_error;
  1128. }
  1129. bool sig_ok =
  1130. ssl_public_key_verify(ssl, signature, signature_algorithm,
  1131. hs->peer_pubkey.get(), hs->transcript.buffer());
  1132. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  1133. sig_ok = true;
  1134. ERR_clear_error();
  1135. #endif
  1136. if (!sig_ok) {
  1137. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
  1138. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
  1139. return ssl_hs_error;
  1140. }
  1141. // The handshake buffer is no longer necessary, and we may hash the current
  1142. // message.
  1143. hs->transcript.FreeBuffer();
  1144. if (!ssl_hash_message(hs, msg)) {
  1145. return ssl_hs_error;
  1146. }
  1147. ssl->method->next_message(ssl);
  1148. hs->state = state12_read_change_cipher_spec;
  1149. return ssl_hs_ok;
  1150. }
  1151. static enum ssl_hs_wait_t do_read_change_cipher_spec(SSL_HANDSHAKE *hs) {
  1152. if (hs->handback && hs->ssl->session != NULL) {
  1153. return ssl_hs_handback;
  1154. }
  1155. hs->state = state12_process_change_cipher_spec;
  1156. return ssl_hs_read_change_cipher_spec;
  1157. }
  1158. static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
  1159. if (!tls1_change_cipher_state(hs, evp_aead_open)) {
  1160. return ssl_hs_error;
  1161. }
  1162. hs->state = state12_read_next_proto;
  1163. return ssl_hs_ok;
  1164. }
  1165. static enum ssl_hs_wait_t do_read_next_proto(SSL_HANDSHAKE *hs) {
  1166. SSL *const ssl = hs->ssl;
  1167. if (!hs->next_proto_neg_seen) {
  1168. hs->state = state12_read_channel_id;
  1169. return ssl_hs_ok;
  1170. }
  1171. SSLMessage msg;
  1172. if (!ssl->method->get_message(ssl, &msg)) {
  1173. return ssl_hs_read_message;
  1174. }
  1175. if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEXT_PROTO) ||
  1176. !ssl_hash_message(hs, msg)) {
  1177. return ssl_hs_error;
  1178. }
  1179. CBS next_protocol = msg.body, selected_protocol, padding;
  1180. if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
  1181. !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
  1182. CBS_len(&next_protocol) != 0) {
  1183. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1184. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1185. return ssl_hs_error;
  1186. }
  1187. if (!ssl->s3->next_proto_negotiated.CopyFrom(selected_protocol)) {
  1188. return ssl_hs_error;
  1189. }
  1190. ssl->method->next_message(ssl);
  1191. hs->state = state12_read_channel_id;
  1192. return ssl_hs_ok;
  1193. }
  1194. static enum ssl_hs_wait_t do_read_channel_id(SSL_HANDSHAKE *hs) {
  1195. SSL *const ssl = hs->ssl;
  1196. if (!ssl->s3->channel_id_valid) {
  1197. hs->state = state12_read_client_finished;
  1198. return ssl_hs_ok;
  1199. }
  1200. SSLMessage msg;
  1201. if (!ssl->method->get_message(ssl, &msg)) {
  1202. return ssl_hs_read_message;
  1203. }
  1204. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) ||
  1205. !tls1_verify_channel_id(hs, msg) ||
  1206. !ssl_hash_message(hs, msg)) {
  1207. return ssl_hs_error;
  1208. }
  1209. ssl->method->next_message(ssl);
  1210. hs->state = state12_read_client_finished;
  1211. return ssl_hs_ok;
  1212. }
  1213. static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) {
  1214. SSL *const ssl = hs->ssl;
  1215. enum ssl_hs_wait_t wait = ssl_get_finished(hs);
  1216. if (wait != ssl_hs_ok) {
  1217. return wait;
  1218. }
  1219. if (ssl->session != NULL) {
  1220. hs->state = state12_finish_server_handshake;
  1221. } else {
  1222. hs->state = state12_send_server_finished;
  1223. }
  1224. // If this is a full handshake with ChannelID then record the handshake
  1225. // hashes in |hs->new_session| in case we need them to verify a
  1226. // ChannelID signature on a resumption of this session in the future.
  1227. if (ssl->session == NULL && ssl->s3->channel_id_valid &&
  1228. !tls1_record_handshake_hashes_for_channel_id(hs)) {
  1229. return ssl_hs_error;
  1230. }
  1231. return ssl_hs_ok;
  1232. }
  1233. static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) {
  1234. SSL *const ssl = hs->ssl;
  1235. if (hs->ticket_expected) {
  1236. const SSL_SESSION *session;
  1237. UniquePtr<SSL_SESSION> session_copy;
  1238. if (ssl->session == NULL) {
  1239. // Fix the timeout to measure from the ticket issuance time.
  1240. ssl_session_rebase_time(ssl, hs->new_session.get());
  1241. session = hs->new_session.get();
  1242. } else {
  1243. // We are renewing an existing session. Duplicate the session to adjust
  1244. // the timeout.
  1245. session_copy =
  1246. SSL_SESSION_dup(ssl->session.get(), SSL_SESSION_INCLUDE_NONAUTH);
  1247. if (!session_copy) {
  1248. return ssl_hs_error;
  1249. }
  1250. ssl_session_rebase_time(ssl, session_copy.get());
  1251. session = session_copy.get();
  1252. }
  1253. ScopedCBB cbb;
  1254. CBB body, ticket;
  1255. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  1256. SSL3_MT_NEW_SESSION_TICKET) ||
  1257. !CBB_add_u32(&body, session->timeout) ||
  1258. !CBB_add_u16_length_prefixed(&body, &ticket) ||
  1259. !ssl_encrypt_ticket(hs, &ticket, session) ||
  1260. !ssl_add_message_cbb(ssl, cbb.get())) {
  1261. return ssl_hs_error;
  1262. }
  1263. }
  1264. if (!ssl->method->add_change_cipher_spec(ssl) ||
  1265. !tls1_change_cipher_state(hs, evp_aead_seal) ||
  1266. !ssl_send_finished(hs)) {
  1267. return ssl_hs_error;
  1268. }
  1269. if (ssl->session != NULL) {
  1270. hs->state = state12_read_change_cipher_spec;
  1271. } else {
  1272. hs->state = state12_finish_server_handshake;
  1273. }
  1274. return ssl_hs_flush;
  1275. }
  1276. static enum ssl_hs_wait_t do_finish_server_handshake(SSL_HANDSHAKE *hs) {
  1277. SSL *const ssl = hs->ssl;
  1278. if (hs->handback) {
  1279. return ssl_hs_handback;
  1280. }
  1281. ssl->method->on_handshake_complete(ssl);
  1282. // If we aren't retaining peer certificates then we can discard it now.
  1283. if (hs->new_session != NULL &&
  1284. hs->config->retain_only_sha256_of_client_certs) {
  1285. hs->new_session->certs.reset();
  1286. ssl->ctx->x509_method->session_clear(hs->new_session.get());
  1287. }
  1288. if (ssl->session != NULL) {
  1289. ssl->s3->established_session = UpRef(ssl->session);
  1290. } else {
  1291. ssl->s3->established_session = std::move(hs->new_session);
  1292. ssl->s3->established_session->not_resumable = false;
  1293. }
  1294. hs->handshake_finalized = true;
  1295. ssl->s3->initial_handshake_complete = true;
  1296. ssl_update_cache(hs, SSL_SESS_CACHE_SERVER);
  1297. hs->state = state12_done;
  1298. return ssl_hs_ok;
  1299. }
  1300. enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs) {
  1301. while (hs->state != state12_done) {
  1302. enum ssl_hs_wait_t ret = ssl_hs_error;
  1303. enum tls12_server_hs_state_t state =
  1304. static_cast<enum tls12_server_hs_state_t>(hs->state);
  1305. switch (state) {
  1306. case state12_start_accept:
  1307. ret = do_start_accept(hs);
  1308. break;
  1309. case state12_read_client_hello:
  1310. ret = do_read_client_hello(hs);
  1311. break;
  1312. case state12_select_certificate:
  1313. ret = do_select_certificate(hs);
  1314. break;
  1315. case state12_tls13:
  1316. ret = do_tls13(hs);
  1317. break;
  1318. case state12_select_parameters:
  1319. ret = do_select_parameters(hs);
  1320. break;
  1321. case state12_send_server_hello:
  1322. ret = do_send_server_hello(hs);
  1323. break;
  1324. case state12_send_server_certificate:
  1325. ret = do_send_server_certificate(hs);
  1326. break;
  1327. case state12_send_server_key_exchange:
  1328. ret = do_send_server_key_exchange(hs);
  1329. break;
  1330. case state12_send_server_hello_done:
  1331. ret = do_send_server_hello_done(hs);
  1332. break;
  1333. case state12_read_client_certificate:
  1334. ret = do_read_client_certificate(hs);
  1335. break;
  1336. case state12_verify_client_certificate:
  1337. ret = do_verify_client_certificate(hs);
  1338. break;
  1339. case state12_read_client_key_exchange:
  1340. ret = do_read_client_key_exchange(hs);
  1341. break;
  1342. case state12_read_client_certificate_verify:
  1343. ret = do_read_client_certificate_verify(hs);
  1344. break;
  1345. case state12_read_change_cipher_spec:
  1346. ret = do_read_change_cipher_spec(hs);
  1347. break;
  1348. case state12_process_change_cipher_spec:
  1349. ret = do_process_change_cipher_spec(hs);
  1350. break;
  1351. case state12_read_next_proto:
  1352. ret = do_read_next_proto(hs);
  1353. break;
  1354. case state12_read_channel_id:
  1355. ret = do_read_channel_id(hs);
  1356. break;
  1357. case state12_read_client_finished:
  1358. ret = do_read_client_finished(hs);
  1359. break;
  1360. case state12_send_server_finished:
  1361. ret = do_send_server_finished(hs);
  1362. break;
  1363. case state12_finish_server_handshake:
  1364. ret = do_finish_server_handshake(hs);
  1365. break;
  1366. case state12_done:
  1367. ret = ssl_hs_ok;
  1368. break;
  1369. }
  1370. if (hs->state != state) {
  1371. ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1);
  1372. }
  1373. if (ret != ssl_hs_ok) {
  1374. return ret;
  1375. }
  1376. }
  1377. ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
  1378. return ssl_hs_ok;
  1379. }
  1380. const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs) {
  1381. enum tls12_server_hs_state_t state =
  1382. static_cast<enum tls12_server_hs_state_t>(hs->state);
  1383. switch (state) {
  1384. case state12_start_accept:
  1385. return "TLS server start_accept";
  1386. case state12_read_client_hello:
  1387. return "TLS server read_client_hello";
  1388. case state12_select_certificate:
  1389. return "TLS server select_certificate";
  1390. case state12_tls13:
  1391. return tls13_server_handshake_state(hs);
  1392. case state12_select_parameters:
  1393. return "TLS server select_parameters";
  1394. case state12_send_server_hello:
  1395. return "TLS server send_server_hello";
  1396. case state12_send_server_certificate:
  1397. return "TLS server send_server_certificate";
  1398. case state12_send_server_key_exchange:
  1399. return "TLS server send_server_key_exchange";
  1400. case state12_send_server_hello_done:
  1401. return "TLS server send_server_hello_done";
  1402. case state12_read_client_certificate:
  1403. return "TLS server read_client_certificate";
  1404. case state12_verify_client_certificate:
  1405. return "TLS server verify_client_certificate";
  1406. case state12_read_client_key_exchange:
  1407. return "TLS server read_client_key_exchange";
  1408. case state12_read_client_certificate_verify:
  1409. return "TLS server read_client_certificate_verify";
  1410. case state12_read_change_cipher_spec:
  1411. return "TLS server read_change_cipher_spec";
  1412. case state12_process_change_cipher_spec:
  1413. return "TLS server process_change_cipher_spec";
  1414. case state12_read_next_proto:
  1415. return "TLS server read_next_proto";
  1416. case state12_read_channel_id:
  1417. return "TLS server read_channel_id";
  1418. case state12_read_client_finished:
  1419. return "TLS server read_client_finished";
  1420. case state12_send_server_finished:
  1421. return "TLS server send_server_finished";
  1422. case state12_finish_server_handshake:
  1423. return "TLS server finish_server_handshake";
  1424. case state12_done:
  1425. return "TLS server done";
  1426. }
  1427. return "TLS server unknown";
  1428. }
  1429. }