You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1630 lines
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. BSSL_NAMESPACE_BEGIN
  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.
  622. if (ssl_supports_version(hs, TLS1_3_VERSION)) {
  623. if (ssl_protocol_version(ssl) == TLS1_2_VERSION) {
  624. OPENSSL_memcpy(ssl->s3->server_random + SSL3_RANDOM_SIZE -
  625. sizeof(kTLS13DowngradeRandom),
  626. kTLS13DowngradeRandom, sizeof(kTLS13DowngradeRandom));
  627. } else {
  628. OPENSSL_memcpy(ssl->s3->server_random + SSL3_RANDOM_SIZE -
  629. sizeof(kTLS12DowngradeRandom),
  630. kTLS12DowngradeRandom, sizeof(kTLS12DowngradeRandom));
  631. }
  632. }
  633. const SSL_SESSION *session = hs->new_session.get();
  634. if (ssl->session != nullptr) {
  635. session = ssl->session.get();
  636. }
  637. ScopedCBB cbb;
  638. CBB body, session_id;
  639. if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) ||
  640. !CBB_add_u16(&body, ssl->version) ||
  641. !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
  642. !CBB_add_u8_length_prefixed(&body, &session_id) ||
  643. !CBB_add_bytes(&session_id, session->session_id,
  644. session->session_id_length) ||
  645. !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) ||
  646. !CBB_add_u8(&body, 0 /* no compression */) ||
  647. !ssl_add_serverhello_tlsext(hs, &body) ||
  648. !ssl_add_message_cbb(ssl, cbb.get())) {
  649. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  650. return ssl_hs_error;
  651. }
  652. if (ssl->session != NULL) {
  653. hs->state = state12_send_server_finished;
  654. } else {
  655. hs->state = state12_send_server_certificate;
  656. }
  657. return ssl_hs_ok;
  658. }
  659. static enum ssl_hs_wait_t do_send_server_certificate(SSL_HANDSHAKE *hs) {
  660. SSL *const ssl = hs->ssl;
  661. ScopedCBB cbb;
  662. if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  663. if (!ssl_has_certificate(hs->config)) {
  664. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
  665. return ssl_hs_error;
  666. }
  667. if (!ssl_output_cert_chain(hs)) {
  668. return ssl_hs_error;
  669. }
  670. if (hs->certificate_status_expected) {
  671. CBB body, ocsp_response;
  672. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  673. SSL3_MT_CERTIFICATE_STATUS) ||
  674. !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
  675. !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
  676. !CBB_add_bytes(
  677. &ocsp_response,
  678. CRYPTO_BUFFER_data(hs->config->cert->ocsp_response.get()),
  679. CRYPTO_BUFFER_len(hs->config->cert->ocsp_response.get())) ||
  680. !ssl_add_message_cbb(ssl, cbb.get())) {
  681. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  682. return ssl_hs_error;
  683. }
  684. }
  685. }
  686. // Assemble ServerKeyExchange parameters if needed.
  687. uint32_t alg_k = hs->new_cipher->algorithm_mkey;
  688. uint32_t alg_a = hs->new_cipher->algorithm_auth;
  689. if (ssl_cipher_requires_server_key_exchange(hs->new_cipher) ||
  690. ((alg_a & SSL_aPSK) && hs->config->psk_identity_hint)) {
  691. // Pre-allocate enough room to comfortably fit an ECDHE public key. Prepend
  692. // the client and server randoms for the signing transcript.
  693. CBB child;
  694. if (!CBB_init(cbb.get(), SSL3_RANDOM_SIZE * 2 + 128) ||
  695. !CBB_add_bytes(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
  696. !CBB_add_bytes(cbb.get(), ssl->s3->server_random, SSL3_RANDOM_SIZE)) {
  697. return ssl_hs_error;
  698. }
  699. // PSK ciphers begin with an identity hint.
  700. if (alg_a & SSL_aPSK) {
  701. size_t len = hs->config->psk_identity_hint == nullptr
  702. ? 0
  703. : strlen(hs->config->psk_identity_hint.get());
  704. if (!CBB_add_u16_length_prefixed(cbb.get(), &child) ||
  705. !CBB_add_bytes(&child,
  706. (const uint8_t *)hs->config->psk_identity_hint.get(),
  707. len)) {
  708. return ssl_hs_error;
  709. }
  710. }
  711. if (alg_k & SSL_kECDHE) {
  712. // Determine the group to use.
  713. uint16_t group_id;
  714. if (!tls1_get_shared_group(hs, &group_id)) {
  715. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  716. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  717. return ssl_hs_error;
  718. }
  719. hs->new_session->group_id = group_id;
  720. // Set up ECDH, generate a key, and emit the public half.
  721. hs->key_share = SSLKeyShare::Create(group_id);
  722. if (!hs->key_share ||
  723. !CBB_add_u8(cbb.get(), NAMED_CURVE_TYPE) ||
  724. !CBB_add_u16(cbb.get(), group_id) ||
  725. !CBB_add_u8_length_prefixed(cbb.get(), &child) ||
  726. !hs->key_share->Offer(&child)) {
  727. return ssl_hs_error;
  728. }
  729. } else {
  730. assert(alg_k & SSL_kPSK);
  731. }
  732. if (!CBBFinishArray(cbb.get(), &hs->server_params)) {
  733. return ssl_hs_error;
  734. }
  735. }
  736. hs->state = state12_send_server_key_exchange;
  737. return ssl_hs_ok;
  738. }
  739. static enum ssl_hs_wait_t do_send_server_key_exchange(SSL_HANDSHAKE *hs) {
  740. SSL *const ssl = hs->ssl;
  741. if (hs->server_params.size() == 0) {
  742. hs->state = state12_send_server_hello_done;
  743. return ssl_hs_ok;
  744. }
  745. ScopedCBB cbb;
  746. CBB body, child;
  747. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  748. SSL3_MT_SERVER_KEY_EXCHANGE) ||
  749. // |hs->server_params| contains a prefix for signing.
  750. hs->server_params.size() < 2 * SSL3_RANDOM_SIZE ||
  751. !CBB_add_bytes(&body, hs->server_params.data() + 2 * SSL3_RANDOM_SIZE,
  752. hs->server_params.size() - 2 * SSL3_RANDOM_SIZE)) {
  753. return ssl_hs_error;
  754. }
  755. // Add a signature.
  756. if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  757. if (!ssl_has_private_key(hs->config)) {
  758. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  759. return ssl_hs_error;
  760. }
  761. // Determine the signature algorithm.
  762. uint16_t signature_algorithm;
  763. if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
  764. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  765. return ssl_hs_error;
  766. }
  767. if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
  768. if (!CBB_add_u16(&body, signature_algorithm)) {
  769. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  770. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  771. return ssl_hs_error;
  772. }
  773. }
  774. // Add space for the signature.
  775. const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
  776. uint8_t *ptr;
  777. if (!CBB_add_u16_length_prefixed(&body, &child) ||
  778. !CBB_reserve(&child, &ptr, max_sig_len)) {
  779. return ssl_hs_error;
  780. }
  781. size_t sig_len;
  782. switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
  783. signature_algorithm, hs->server_params)) {
  784. case ssl_private_key_success:
  785. if (!CBB_did_write(&child, sig_len)) {
  786. return ssl_hs_error;
  787. }
  788. break;
  789. case ssl_private_key_failure:
  790. return ssl_hs_error;
  791. case ssl_private_key_retry:
  792. return ssl_hs_private_key_operation;
  793. }
  794. }
  795. if (!ssl_add_message_cbb(ssl, cbb.get())) {
  796. return ssl_hs_error;
  797. }
  798. hs->server_params.Reset();
  799. hs->state = state12_send_server_hello_done;
  800. return ssl_hs_ok;
  801. }
  802. static enum ssl_hs_wait_t do_send_server_hello_done(SSL_HANDSHAKE *hs) {
  803. SSL *const ssl = hs->ssl;
  804. ScopedCBB cbb;
  805. CBB body;
  806. if (hs->cert_request) {
  807. CBB cert_types, sigalgs_cbb;
  808. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  809. SSL3_MT_CERTIFICATE_REQUEST) ||
  810. !CBB_add_u8_length_prefixed(&body, &cert_types) ||
  811. !CBB_add_u8(&cert_types, SSL3_CT_RSA_SIGN) ||
  812. !CBB_add_u8(&cert_types, TLS_CT_ECDSA_SIGN) ||
  813. // TLS 1.2 has no way to specify different signature algorithms for
  814. // certificates and the online signature, so emit the more restrictive
  815. // certificate list.
  816. (ssl_protocol_version(ssl) >= TLS1_2_VERSION &&
  817. (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb) ||
  818. !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb, true /* certs */))) ||
  819. !ssl_add_client_CA_list(hs, &body) ||
  820. !ssl_add_message_cbb(ssl, cbb.get())) {
  821. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  822. return ssl_hs_error;
  823. }
  824. }
  825. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  826. SSL3_MT_SERVER_HELLO_DONE) ||
  827. !ssl_add_message_cbb(ssl, cbb.get())) {
  828. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  829. return ssl_hs_error;
  830. }
  831. hs->state = state12_read_client_certificate;
  832. return ssl_hs_flush;
  833. }
  834. static enum ssl_hs_wait_t do_read_client_certificate(SSL_HANDSHAKE *hs) {
  835. SSL *const ssl = hs->ssl;
  836. if (hs->handback && hs->new_cipher->algorithm_mkey == SSL_kECDHE) {
  837. return ssl_hs_handback;
  838. }
  839. if (!hs->cert_request) {
  840. hs->state = state12_verify_client_certificate;
  841. return ssl_hs_ok;
  842. }
  843. SSLMessage msg;
  844. if (!ssl->method->get_message(ssl, &msg)) {
  845. return ssl_hs_read_message;
  846. }
  847. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE)) {
  848. return ssl_hs_error;
  849. }
  850. if (!ssl_hash_message(hs, msg)) {
  851. return ssl_hs_error;
  852. }
  853. CBS certificate_msg = msg.body;
  854. uint8_t alert = SSL_AD_DECODE_ERROR;
  855. if (!ssl_parse_cert_chain(&alert, &hs->new_session->certs, &hs->peer_pubkey,
  856. hs->config->retain_only_sha256_of_client_certs
  857. ? hs->new_session->peer_sha256
  858. : nullptr,
  859. &certificate_msg, ssl->ctx->pool)) {
  860. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  861. return ssl_hs_error;
  862. }
  863. if (CBS_len(&certificate_msg) != 0 ||
  864. !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
  865. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  866. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  867. return ssl_hs_error;
  868. }
  869. if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) == 0) {
  870. // No client certificate so the handshake buffer may be discarded.
  871. hs->transcript.FreeBuffer();
  872. if (hs->config->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  873. // Fail for TLS only if we required a certificate
  874. OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  875. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  876. return ssl_hs_error;
  877. }
  878. // OpenSSL returns X509_V_OK when no certificates are received. This is
  879. // classed by them as a bug, but it's assumed by at least NGINX.
  880. hs->new_session->verify_result = X509_V_OK;
  881. } else if (hs->config->retain_only_sha256_of_client_certs) {
  882. // The hash will have been filled in.
  883. hs->new_session->peer_sha256_valid = 1;
  884. }
  885. ssl->method->next_message(ssl);
  886. hs->state = state12_verify_client_certificate;
  887. return ssl_hs_ok;
  888. }
  889. static enum ssl_hs_wait_t do_verify_client_certificate(SSL_HANDSHAKE *hs) {
  890. if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) > 0) {
  891. switch (ssl_verify_peer_cert(hs)) {
  892. case ssl_verify_ok:
  893. break;
  894. case ssl_verify_invalid:
  895. return ssl_hs_error;
  896. case ssl_verify_retry:
  897. return ssl_hs_certificate_verify;
  898. }
  899. }
  900. hs->state = state12_read_client_key_exchange;
  901. return ssl_hs_ok;
  902. }
  903. static enum ssl_hs_wait_t do_read_client_key_exchange(SSL_HANDSHAKE *hs) {
  904. SSL *const ssl = hs->ssl;
  905. SSLMessage msg;
  906. if (!ssl->method->get_message(ssl, &msg)) {
  907. return ssl_hs_read_message;
  908. }
  909. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_KEY_EXCHANGE)) {
  910. return ssl_hs_error;
  911. }
  912. CBS client_key_exchange = msg.body;
  913. uint32_t alg_k = hs->new_cipher->algorithm_mkey;
  914. uint32_t alg_a = hs->new_cipher->algorithm_auth;
  915. // If using a PSK key exchange, parse the PSK identity.
  916. if (alg_a & SSL_aPSK) {
  917. CBS psk_identity;
  918. // If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
  919. // then this is the only field in the message.
  920. if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
  921. ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
  922. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  923. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  924. return ssl_hs_error;
  925. }
  926. if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
  927. CBS_contains_zero_byte(&psk_identity)) {
  928. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  929. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  930. return ssl_hs_error;
  931. }
  932. char *raw = nullptr;
  933. if (!CBS_strdup(&psk_identity, &raw)) {
  934. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  935. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  936. return ssl_hs_error;
  937. }
  938. hs->new_session->psk_identity.reset(raw);
  939. }
  940. // Depending on the key exchange method, compute |premaster_secret|.
  941. Array<uint8_t> premaster_secret;
  942. if (alg_k & SSL_kRSA) {
  943. CBS encrypted_premaster_secret;
  944. if (!CBS_get_u16_length_prefixed(&client_key_exchange,
  945. &encrypted_premaster_secret) ||
  946. CBS_len(&client_key_exchange) != 0) {
  947. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  948. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  949. return ssl_hs_error;
  950. }
  951. // Allocate a buffer large enough for an RSA decryption.
  952. Array<uint8_t> decrypt_buf;
  953. if (!decrypt_buf.Init(EVP_PKEY_size(hs->local_pubkey.get()))) {
  954. return ssl_hs_error;
  955. }
  956. // Decrypt with no padding. PKCS#1 padding will be removed as part of the
  957. // timing-sensitive code below.
  958. size_t decrypt_len;
  959. switch (ssl_private_key_decrypt(hs, decrypt_buf.data(), &decrypt_len,
  960. decrypt_buf.size(),
  961. encrypted_premaster_secret)) {
  962. case ssl_private_key_success:
  963. break;
  964. case ssl_private_key_failure:
  965. return ssl_hs_error;
  966. case ssl_private_key_retry:
  967. return ssl_hs_private_key_operation;
  968. }
  969. if (decrypt_len != decrypt_buf.size()) {
  970. OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
  971. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
  972. return ssl_hs_error;
  973. }
  974. // Prepare a random premaster, to be used on invalid padding. See RFC 5246,
  975. // section 7.4.7.1.
  976. if (!premaster_secret.Init(SSL_MAX_MASTER_KEY_LENGTH) ||
  977. !RAND_bytes(premaster_secret.data(), premaster_secret.size())) {
  978. return ssl_hs_error;
  979. }
  980. // The smallest padded premaster is 11 bytes of overhead. Small keys are
  981. // publicly invalid.
  982. if (decrypt_len < 11 + premaster_secret.size()) {
  983. OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
  984. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
  985. return ssl_hs_error;
  986. }
  987. // Check the padding. See RFC 3447, section 7.2.2.
  988. size_t padding_len = decrypt_len - premaster_secret.size();
  989. uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
  990. constant_time_eq_int_8(decrypt_buf[1], 2);
  991. for (size_t i = 2; i < padding_len - 1; i++) {
  992. good &= ~constant_time_is_zero_8(decrypt_buf[i]);
  993. }
  994. good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
  995. // The premaster secret must begin with |client_version|. This too must be
  996. // checked in constant time (http://eprint.iacr.org/2003/052/).
  997. good &= constant_time_eq_8(decrypt_buf[padding_len],
  998. (unsigned)(hs->client_version >> 8));
  999. good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
  1000. (unsigned)(hs->client_version & 0xff));
  1001. // Select, in constant time, either the decrypted premaster or the random
  1002. // premaster based on |good|.
  1003. for (size_t i = 0; i < premaster_secret.size(); i++) {
  1004. premaster_secret[i] = constant_time_select_8(
  1005. good, decrypt_buf[padding_len + i], premaster_secret[i]);
  1006. }
  1007. } else if (alg_k & SSL_kECDHE) {
  1008. // Parse the ClientKeyExchange.
  1009. CBS peer_key;
  1010. if (!CBS_get_u8_length_prefixed(&client_key_exchange, &peer_key) ||
  1011. CBS_len(&client_key_exchange) != 0) {
  1012. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1013. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1014. return ssl_hs_error;
  1015. }
  1016. // Compute the premaster.
  1017. uint8_t alert = SSL_AD_DECODE_ERROR;
  1018. if (!hs->key_share->Finish(&premaster_secret, &alert, peer_key)) {
  1019. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  1020. return ssl_hs_error;
  1021. }
  1022. // The key exchange state may now be discarded.
  1023. hs->key_share.reset();
  1024. } else if (!(alg_k & SSL_kPSK)) {
  1025. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1026. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1027. return ssl_hs_error;
  1028. }
  1029. // For a PSK cipher suite, the actual pre-master secret is combined with the
  1030. // pre-shared key.
  1031. if (alg_a & SSL_aPSK) {
  1032. if (hs->config->psk_server_callback == NULL) {
  1033. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1034. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1035. return ssl_hs_error;
  1036. }
  1037. // Look up the key for the identity.
  1038. uint8_t psk[PSK_MAX_PSK_LEN];
  1039. unsigned psk_len = hs->config->psk_server_callback(
  1040. ssl, hs->new_session->psk_identity.get(), psk, sizeof(psk));
  1041. if (psk_len > PSK_MAX_PSK_LEN) {
  1042. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1043. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1044. return ssl_hs_error;
  1045. } else if (psk_len == 0) {
  1046. // PSK related to the given identity not found.
  1047. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
  1048. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNKNOWN_PSK_IDENTITY);
  1049. return ssl_hs_error;
  1050. }
  1051. if (alg_k & SSL_kPSK) {
  1052. // In plain PSK, other_secret is a block of 0s with the same length as the
  1053. // pre-shared key.
  1054. if (!premaster_secret.Init(psk_len)) {
  1055. return ssl_hs_error;
  1056. }
  1057. OPENSSL_memset(premaster_secret.data(), 0, premaster_secret.size());
  1058. }
  1059. ScopedCBB new_premaster;
  1060. CBB child;
  1061. if (!CBB_init(new_premaster.get(),
  1062. 2 + psk_len + 2 + premaster_secret.size()) ||
  1063. !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
  1064. !CBB_add_bytes(&child, premaster_secret.data(),
  1065. premaster_secret.size()) ||
  1066. !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
  1067. !CBB_add_bytes(&child, psk, psk_len) ||
  1068. !CBBFinishArray(new_premaster.get(), &premaster_secret)) {
  1069. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1070. return ssl_hs_error;
  1071. }
  1072. }
  1073. if (!ssl_hash_message(hs, msg)) {
  1074. return ssl_hs_error;
  1075. }
  1076. // Compute the master secret.
  1077. hs->new_session->master_key_length = tls1_generate_master_secret(
  1078. hs, hs->new_session->master_key, premaster_secret);
  1079. if (hs->new_session->master_key_length == 0) {
  1080. return ssl_hs_error;
  1081. }
  1082. hs->new_session->extended_master_secret = hs->extended_master_secret;
  1083. ssl->method->next_message(ssl);
  1084. hs->state = state12_read_client_certificate_verify;
  1085. return ssl_hs_ok;
  1086. }
  1087. static enum ssl_hs_wait_t do_read_client_certificate_verify(SSL_HANDSHAKE *hs) {
  1088. SSL *const ssl = hs->ssl;
  1089. // Only RSA and ECDSA client certificates are supported, so a
  1090. // CertificateVerify is required if and only if there's a client certificate.
  1091. if (!hs->peer_pubkey) {
  1092. hs->transcript.FreeBuffer();
  1093. hs->state = state12_read_change_cipher_spec;
  1094. return ssl_hs_ok;
  1095. }
  1096. SSLMessage msg;
  1097. if (!ssl->method->get_message(ssl, &msg)) {
  1098. return ssl_hs_read_message;
  1099. }
  1100. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY)) {
  1101. return ssl_hs_error;
  1102. }
  1103. CBS certificate_verify = msg.body, signature;
  1104. // Determine the signature algorithm.
  1105. uint16_t signature_algorithm = 0;
  1106. if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
  1107. if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
  1108. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1109. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1110. return ssl_hs_error;
  1111. }
  1112. uint8_t alert = SSL_AD_DECODE_ERROR;
  1113. if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
  1114. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  1115. return ssl_hs_error;
  1116. }
  1117. hs->new_session->peer_signature_algorithm = signature_algorithm;
  1118. } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
  1119. hs->peer_pubkey.get())) {
  1120. OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
  1121. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
  1122. return ssl_hs_error;
  1123. }
  1124. // Parse and verify the signature.
  1125. if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
  1126. CBS_len(&certificate_verify) != 0) {
  1127. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1128. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1129. return ssl_hs_error;
  1130. }
  1131. bool sig_ok =
  1132. ssl_public_key_verify(ssl, signature, signature_algorithm,
  1133. hs->peer_pubkey.get(), hs->transcript.buffer());
  1134. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  1135. sig_ok = true;
  1136. ERR_clear_error();
  1137. #endif
  1138. if (!sig_ok) {
  1139. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
  1140. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
  1141. return ssl_hs_error;
  1142. }
  1143. // The handshake buffer is no longer necessary, and we may hash the current
  1144. // message.
  1145. hs->transcript.FreeBuffer();
  1146. if (!ssl_hash_message(hs, msg)) {
  1147. return ssl_hs_error;
  1148. }
  1149. ssl->method->next_message(ssl);
  1150. hs->state = state12_read_change_cipher_spec;
  1151. return ssl_hs_ok;
  1152. }
  1153. static enum ssl_hs_wait_t do_read_change_cipher_spec(SSL_HANDSHAKE *hs) {
  1154. if (hs->handback && hs->ssl->session != NULL) {
  1155. return ssl_hs_handback;
  1156. }
  1157. hs->state = state12_process_change_cipher_spec;
  1158. return ssl_hs_read_change_cipher_spec;
  1159. }
  1160. static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
  1161. if (!tls1_change_cipher_state(hs, evp_aead_open)) {
  1162. return ssl_hs_error;
  1163. }
  1164. hs->state = state12_read_next_proto;
  1165. return ssl_hs_ok;
  1166. }
  1167. static enum ssl_hs_wait_t do_read_next_proto(SSL_HANDSHAKE *hs) {
  1168. SSL *const ssl = hs->ssl;
  1169. if (!hs->next_proto_neg_seen) {
  1170. hs->state = state12_read_channel_id;
  1171. return ssl_hs_ok;
  1172. }
  1173. SSLMessage msg;
  1174. if (!ssl->method->get_message(ssl, &msg)) {
  1175. return ssl_hs_read_message;
  1176. }
  1177. if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEXT_PROTO) ||
  1178. !ssl_hash_message(hs, msg)) {
  1179. return ssl_hs_error;
  1180. }
  1181. CBS next_protocol = msg.body, selected_protocol, padding;
  1182. if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
  1183. !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
  1184. CBS_len(&next_protocol) != 0) {
  1185. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1186. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1187. return ssl_hs_error;
  1188. }
  1189. if (!ssl->s3->next_proto_negotiated.CopyFrom(selected_protocol)) {
  1190. return ssl_hs_error;
  1191. }
  1192. ssl->method->next_message(ssl);
  1193. hs->state = state12_read_channel_id;
  1194. return ssl_hs_ok;
  1195. }
  1196. static enum ssl_hs_wait_t do_read_channel_id(SSL_HANDSHAKE *hs) {
  1197. SSL *const ssl = hs->ssl;
  1198. if (!ssl->s3->channel_id_valid) {
  1199. hs->state = state12_read_client_finished;
  1200. return ssl_hs_ok;
  1201. }
  1202. SSLMessage msg;
  1203. if (!ssl->method->get_message(ssl, &msg)) {
  1204. return ssl_hs_read_message;
  1205. }
  1206. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) ||
  1207. !tls1_verify_channel_id(hs, msg) ||
  1208. !ssl_hash_message(hs, msg)) {
  1209. return ssl_hs_error;
  1210. }
  1211. ssl->method->next_message(ssl);
  1212. hs->state = state12_read_client_finished;
  1213. return ssl_hs_ok;
  1214. }
  1215. static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) {
  1216. SSL *const ssl = hs->ssl;
  1217. enum ssl_hs_wait_t wait = ssl_get_finished(hs);
  1218. if (wait != ssl_hs_ok) {
  1219. return wait;
  1220. }
  1221. if (ssl->session != NULL) {
  1222. hs->state = state12_finish_server_handshake;
  1223. } else {
  1224. hs->state = state12_send_server_finished;
  1225. }
  1226. // If this is a full handshake with ChannelID then record the handshake
  1227. // hashes in |hs->new_session| in case we need them to verify a
  1228. // ChannelID signature on a resumption of this session in the future.
  1229. if (ssl->session == NULL && ssl->s3->channel_id_valid &&
  1230. !tls1_record_handshake_hashes_for_channel_id(hs)) {
  1231. return ssl_hs_error;
  1232. }
  1233. return ssl_hs_ok;
  1234. }
  1235. static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) {
  1236. SSL *const ssl = hs->ssl;
  1237. if (hs->ticket_expected) {
  1238. const SSL_SESSION *session;
  1239. UniquePtr<SSL_SESSION> session_copy;
  1240. if (ssl->session == NULL) {
  1241. // Fix the timeout to measure from the ticket issuance time.
  1242. ssl_session_rebase_time(ssl, hs->new_session.get());
  1243. session = hs->new_session.get();
  1244. } else {
  1245. // We are renewing an existing session. Duplicate the session to adjust
  1246. // the timeout.
  1247. session_copy =
  1248. SSL_SESSION_dup(ssl->session.get(), SSL_SESSION_INCLUDE_NONAUTH);
  1249. if (!session_copy) {
  1250. return ssl_hs_error;
  1251. }
  1252. ssl_session_rebase_time(ssl, session_copy.get());
  1253. session = session_copy.get();
  1254. }
  1255. ScopedCBB cbb;
  1256. CBB body, ticket;
  1257. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  1258. SSL3_MT_NEW_SESSION_TICKET) ||
  1259. !CBB_add_u32(&body, session->timeout) ||
  1260. !CBB_add_u16_length_prefixed(&body, &ticket) ||
  1261. !ssl_encrypt_ticket(hs, &ticket, session) ||
  1262. !ssl_add_message_cbb(ssl, cbb.get())) {
  1263. return ssl_hs_error;
  1264. }
  1265. }
  1266. if (!ssl->method->add_change_cipher_spec(ssl) ||
  1267. !tls1_change_cipher_state(hs, evp_aead_seal) ||
  1268. !ssl_send_finished(hs)) {
  1269. return ssl_hs_error;
  1270. }
  1271. if (ssl->session != NULL) {
  1272. hs->state = state12_read_change_cipher_spec;
  1273. } else {
  1274. hs->state = state12_finish_server_handshake;
  1275. }
  1276. return ssl_hs_flush;
  1277. }
  1278. static enum ssl_hs_wait_t do_finish_server_handshake(SSL_HANDSHAKE *hs) {
  1279. SSL *const ssl = hs->ssl;
  1280. if (hs->handback) {
  1281. return ssl_hs_handback;
  1282. }
  1283. ssl->method->on_handshake_complete(ssl);
  1284. // If we aren't retaining peer certificates then we can discard it now.
  1285. if (hs->new_session != NULL &&
  1286. hs->config->retain_only_sha256_of_client_certs) {
  1287. hs->new_session->certs.reset();
  1288. ssl->ctx->x509_method->session_clear(hs->new_session.get());
  1289. }
  1290. if (ssl->session != NULL) {
  1291. ssl->s3->established_session = UpRef(ssl->session);
  1292. } else {
  1293. ssl->s3->established_session = std::move(hs->new_session);
  1294. ssl->s3->established_session->not_resumable = false;
  1295. }
  1296. hs->handshake_finalized = true;
  1297. ssl->s3->initial_handshake_complete = true;
  1298. ssl_update_cache(hs, SSL_SESS_CACHE_SERVER);
  1299. hs->state = state12_done;
  1300. return ssl_hs_ok;
  1301. }
  1302. enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs) {
  1303. while (hs->state != state12_done) {
  1304. enum ssl_hs_wait_t ret = ssl_hs_error;
  1305. enum tls12_server_hs_state_t state =
  1306. static_cast<enum tls12_server_hs_state_t>(hs->state);
  1307. switch (state) {
  1308. case state12_start_accept:
  1309. ret = do_start_accept(hs);
  1310. break;
  1311. case state12_read_client_hello:
  1312. ret = do_read_client_hello(hs);
  1313. break;
  1314. case state12_select_certificate:
  1315. ret = do_select_certificate(hs);
  1316. break;
  1317. case state12_tls13:
  1318. ret = do_tls13(hs);
  1319. break;
  1320. case state12_select_parameters:
  1321. ret = do_select_parameters(hs);
  1322. break;
  1323. case state12_send_server_hello:
  1324. ret = do_send_server_hello(hs);
  1325. break;
  1326. case state12_send_server_certificate:
  1327. ret = do_send_server_certificate(hs);
  1328. break;
  1329. case state12_send_server_key_exchange:
  1330. ret = do_send_server_key_exchange(hs);
  1331. break;
  1332. case state12_send_server_hello_done:
  1333. ret = do_send_server_hello_done(hs);
  1334. break;
  1335. case state12_read_client_certificate:
  1336. ret = do_read_client_certificate(hs);
  1337. break;
  1338. case state12_verify_client_certificate:
  1339. ret = do_verify_client_certificate(hs);
  1340. break;
  1341. case state12_read_client_key_exchange:
  1342. ret = do_read_client_key_exchange(hs);
  1343. break;
  1344. case state12_read_client_certificate_verify:
  1345. ret = do_read_client_certificate_verify(hs);
  1346. break;
  1347. case state12_read_change_cipher_spec:
  1348. ret = do_read_change_cipher_spec(hs);
  1349. break;
  1350. case state12_process_change_cipher_spec:
  1351. ret = do_process_change_cipher_spec(hs);
  1352. break;
  1353. case state12_read_next_proto:
  1354. ret = do_read_next_proto(hs);
  1355. break;
  1356. case state12_read_channel_id:
  1357. ret = do_read_channel_id(hs);
  1358. break;
  1359. case state12_read_client_finished:
  1360. ret = do_read_client_finished(hs);
  1361. break;
  1362. case state12_send_server_finished:
  1363. ret = do_send_server_finished(hs);
  1364. break;
  1365. case state12_finish_server_handshake:
  1366. ret = do_finish_server_handshake(hs);
  1367. break;
  1368. case state12_done:
  1369. ret = ssl_hs_ok;
  1370. break;
  1371. }
  1372. if (hs->state != state) {
  1373. ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1);
  1374. }
  1375. if (ret != ssl_hs_ok) {
  1376. return ret;
  1377. }
  1378. }
  1379. ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
  1380. return ssl_hs_ok;
  1381. }
  1382. const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs) {
  1383. enum tls12_server_hs_state_t state =
  1384. static_cast<enum tls12_server_hs_state_t>(hs->state);
  1385. switch (state) {
  1386. case state12_start_accept:
  1387. return "TLS server start_accept";
  1388. case state12_read_client_hello:
  1389. return "TLS server read_client_hello";
  1390. case state12_select_certificate:
  1391. return "TLS server select_certificate";
  1392. case state12_tls13:
  1393. return tls13_server_handshake_state(hs);
  1394. case state12_select_parameters:
  1395. return "TLS server select_parameters";
  1396. case state12_send_server_hello:
  1397. return "TLS server send_server_hello";
  1398. case state12_send_server_certificate:
  1399. return "TLS server send_server_certificate";
  1400. case state12_send_server_key_exchange:
  1401. return "TLS server send_server_key_exchange";
  1402. case state12_send_server_hello_done:
  1403. return "TLS server send_server_hello_done";
  1404. case state12_read_client_certificate:
  1405. return "TLS server read_client_certificate";
  1406. case state12_verify_client_certificate:
  1407. return "TLS server verify_client_certificate";
  1408. case state12_read_client_key_exchange:
  1409. return "TLS server read_client_key_exchange";
  1410. case state12_read_client_certificate_verify:
  1411. return "TLS server read_client_certificate_verify";
  1412. case state12_read_change_cipher_spec:
  1413. return "TLS server read_change_cipher_spec";
  1414. case state12_process_change_cipher_spec:
  1415. return "TLS server process_change_cipher_spec";
  1416. case state12_read_next_proto:
  1417. return "TLS server read_next_proto";
  1418. case state12_read_channel_id:
  1419. return "TLS server read_channel_id";
  1420. case state12_read_client_finished:
  1421. return "TLS server read_client_finished";
  1422. case state12_send_server_finished:
  1423. return "TLS server send_server_finished";
  1424. case state12_finish_server_handshake:
  1425. return "TLS server finish_server_handshake";
  1426. case state12_done:
  1427. return "TLS server done";
  1428. }
  1429. return "TLS server unknown";
  1430. }
  1431. BSSL_NAMESPACE_END