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.
 
 
 
 
 
 

1753 rivejä
59 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)) {
  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. // is_probably_jdk11_with_tls13 returns whether |client_hello| was probably sent
  367. // from a JDK 11 client with both TLS 1.3 and a prior version enabled.
  368. static bool is_probably_jdk11_with_tls13(const SSL_CLIENT_HELLO *client_hello) {
  369. // JDK 11 ClientHellos contain a number of unusual properties which should
  370. // limit false positives.
  371. // JDK 11 does not support ChaCha20-Poly1305. This is unusual: many modern
  372. // clients implement ChaCha20-Poly1305.
  373. if (ssl_client_cipher_list_contains_cipher(
  374. client_hello, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) {
  375. return false;
  376. }
  377. // JDK 11 always sends extensions in a particular order.
  378. constexpr uint16_t kMaxFragmentLength = 0x0001;
  379. constexpr uint16_t kStatusRequestV2 = 0x0011;
  380. static CONSTEXPR_ARRAY struct {
  381. uint16_t id;
  382. bool required;
  383. } kJavaExtensions[] = {
  384. {TLSEXT_TYPE_server_name, false},
  385. {kMaxFragmentLength, false},
  386. {TLSEXT_TYPE_status_request, false},
  387. {TLSEXT_TYPE_supported_groups, true},
  388. {TLSEXT_TYPE_ec_point_formats, false},
  389. {TLSEXT_TYPE_signature_algorithms, true},
  390. // Java always sends signature_algorithms_cert.
  391. {TLSEXT_TYPE_signature_algorithms_cert, true},
  392. {TLSEXT_TYPE_application_layer_protocol_negotiation, false},
  393. {kStatusRequestV2, false},
  394. {TLSEXT_TYPE_extended_master_secret, false},
  395. {TLSEXT_TYPE_supported_versions, true},
  396. {TLSEXT_TYPE_cookie, false},
  397. {TLSEXT_TYPE_psk_key_exchange_modes, true},
  398. {TLSEXT_TYPE_key_share, true},
  399. {TLSEXT_TYPE_renegotiate, false},
  400. {TLSEXT_TYPE_pre_shared_key, false},
  401. };
  402. Span<const uint8_t> sigalgs, sigalgs_cert;
  403. bool has_status_request = false, has_status_request_v2 = false;
  404. CBS extensions, supported_groups;
  405. CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
  406. for (const auto &java_extension : kJavaExtensions) {
  407. CBS copy = extensions;
  408. uint16_t id;
  409. if (CBS_get_u16(&copy, &id) && id == java_extension.id) {
  410. // The next extension is the one we expected.
  411. extensions = copy;
  412. CBS body;
  413. if (!CBS_get_u16_length_prefixed(&extensions, &body)) {
  414. return false;
  415. }
  416. switch (id) {
  417. case TLSEXT_TYPE_status_request:
  418. has_status_request = true;
  419. break;
  420. case kStatusRequestV2:
  421. has_status_request_v2 = true;
  422. break;
  423. case TLSEXT_TYPE_signature_algorithms:
  424. sigalgs = body;
  425. break;
  426. case TLSEXT_TYPE_signature_algorithms_cert:
  427. sigalgs_cert = body;
  428. break;
  429. case TLSEXT_TYPE_supported_groups:
  430. supported_groups = body;
  431. break;
  432. }
  433. } else if (java_extension.required) {
  434. return false;
  435. }
  436. }
  437. if (CBS_len(&extensions) != 0) {
  438. return false;
  439. }
  440. // JDK 11 never advertises X25519. It is not offered by default, and
  441. // -Djdk.tls.namedGroups=x25519 does not work. This is unusual: many modern
  442. // clients implement X25519.
  443. while (CBS_len(&supported_groups) > 0) {
  444. uint16_t group;
  445. if (!CBS_get_u16(&supported_groups, &group) ||
  446. group == SSL_CURVE_X25519) {
  447. return false;
  448. }
  449. }
  450. if (// JDK 11 always sends the same contents in signature_algorithms and
  451. // signature_algorithms_cert. This is unusual: signature_algorithms_cert,
  452. // if omitted, is treated as if it were signature_algorithms.
  453. sigalgs != sigalgs_cert ||
  454. // When TLS 1.2 or below is enabled, JDK 11 sends status_request_v2 iff it
  455. // sends status_request. This is unusual: status_request_v2 is not widely
  456. // implemented.
  457. has_status_request != has_status_request_v2) {
  458. return false;
  459. }
  460. return true;
  461. }
  462. static enum ssl_hs_wait_t do_read_client_hello(SSL_HANDSHAKE *hs) {
  463. SSL *const ssl = hs->ssl;
  464. SSLMessage msg;
  465. if (!ssl->method->get_message(ssl, &msg)) {
  466. return ssl_hs_read_message;
  467. }
  468. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_HELLO)) {
  469. return ssl_hs_error;
  470. }
  471. if (hs->config->handoff) {
  472. return ssl_hs_handoff;
  473. }
  474. SSL_CLIENT_HELLO client_hello;
  475. if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
  476. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  477. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  478. return ssl_hs_error;
  479. }
  480. // Run the early callback.
  481. if (ssl->ctx->select_certificate_cb != NULL) {
  482. switch (ssl->ctx->select_certificate_cb(&client_hello)) {
  483. case ssl_select_cert_retry:
  484. return ssl_hs_certificate_selection_pending;
  485. case ssl_select_cert_error:
  486. // Connection rejected.
  487. OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
  488. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  489. return ssl_hs_error;
  490. default:
  491. /* fallthrough */;
  492. }
  493. }
  494. // Freeze the version range after the early callback.
  495. if (!ssl_get_version_range(hs, &hs->min_version, &hs->max_version)) {
  496. return ssl_hs_error;
  497. }
  498. if (hs->config->jdk11_workaround &&
  499. is_probably_jdk11_with_tls13(&client_hello)) {
  500. hs->apply_jdk11_workaround = true;
  501. }
  502. uint8_t alert = SSL_AD_DECODE_ERROR;
  503. if (!negotiate_version(hs, &alert, &client_hello)) {
  504. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  505. return ssl_hs_error;
  506. }
  507. hs->client_version = client_hello.version;
  508. if (client_hello.random_len != SSL3_RANDOM_SIZE) {
  509. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  510. return ssl_hs_error;
  511. }
  512. OPENSSL_memcpy(ssl->s3->client_random, client_hello.random,
  513. client_hello.random_len);
  514. // Only null compression is supported. TLS 1.3 further requires the peer
  515. // advertise no other compression.
  516. if (OPENSSL_memchr(client_hello.compression_methods, 0,
  517. client_hello.compression_methods_len) == NULL ||
  518. (ssl_protocol_version(ssl) >= TLS1_3_VERSION &&
  519. client_hello.compression_methods_len != 1)) {
  520. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMPRESSION_LIST);
  521. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  522. return ssl_hs_error;
  523. }
  524. // TLS extensions.
  525. if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) {
  526. OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
  527. return ssl_hs_error;
  528. }
  529. hs->state = state12_select_certificate;
  530. return ssl_hs_ok;
  531. }
  532. static enum ssl_hs_wait_t do_select_certificate(SSL_HANDSHAKE *hs) {
  533. SSL *const ssl = hs->ssl;
  534. SSLMessage msg;
  535. if (!ssl->method->get_message(ssl, &msg)) {
  536. return ssl_hs_read_message;
  537. }
  538. // Call |cert_cb| to update server certificates if required.
  539. if (hs->config->cert->cert_cb != NULL) {
  540. int rv = hs->config->cert->cert_cb(ssl, hs->config->cert->cert_cb_arg);
  541. if (rv == 0) {
  542. OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
  543. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  544. return ssl_hs_error;
  545. }
  546. if (rv < 0) {
  547. return ssl_hs_x509_lookup;
  548. }
  549. }
  550. if (!ssl_on_certificate_selected(hs)) {
  551. return ssl_hs_error;
  552. }
  553. if (hs->ocsp_stapling_requested &&
  554. ssl->ctx->legacy_ocsp_callback != nullptr) {
  555. switch (ssl->ctx->legacy_ocsp_callback(
  556. ssl, ssl->ctx->legacy_ocsp_callback_arg)) {
  557. case SSL_TLSEXT_ERR_OK:
  558. break;
  559. case SSL_TLSEXT_ERR_NOACK:
  560. hs->ocsp_stapling_requested = false;
  561. break;
  562. default:
  563. OPENSSL_PUT_ERROR(SSL, SSL_R_OCSP_CB_ERROR);
  564. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  565. return ssl_hs_error;
  566. }
  567. }
  568. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  569. // Jump to the TLS 1.3 state machine.
  570. hs->state = state12_tls13;
  571. return ssl_hs_ok;
  572. }
  573. SSL_CLIENT_HELLO client_hello;
  574. if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
  575. return ssl_hs_error;
  576. }
  577. // Negotiate the cipher suite. This must be done after |cert_cb| so the
  578. // certificate is finalized.
  579. SSLCipherPreferenceList *prefs = hs->config->cipher_list
  580. ? hs->config->cipher_list.get()
  581. : ssl->ctx->cipher_list.get();
  582. hs->new_cipher = ssl3_choose_cipher(hs, &client_hello, prefs);
  583. if (hs->new_cipher == NULL) {
  584. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
  585. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  586. return ssl_hs_error;
  587. }
  588. hs->state = state12_select_parameters;
  589. return ssl_hs_ok;
  590. }
  591. static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) {
  592. enum ssl_hs_wait_t wait = tls13_server_handshake(hs);
  593. if (wait == ssl_hs_ok) {
  594. hs->state = state12_finish_server_handshake;
  595. return ssl_hs_ok;
  596. }
  597. return wait;
  598. }
  599. static enum ssl_hs_wait_t do_select_parameters(SSL_HANDSHAKE *hs) {
  600. SSL *const ssl = hs->ssl;
  601. SSLMessage msg;
  602. if (!ssl->method->get_message(ssl, &msg)) {
  603. return ssl_hs_read_message;
  604. }
  605. SSL_CLIENT_HELLO client_hello;
  606. if (!ssl_client_hello_init(ssl, &client_hello, msg)) {
  607. return ssl_hs_error;
  608. }
  609. // Determine whether we are doing session resumption.
  610. UniquePtr<SSL_SESSION> session;
  611. bool tickets_supported = false, renew_ticket = false;
  612. enum ssl_hs_wait_t wait = ssl_get_prev_session(
  613. hs, &session, &tickets_supported, &renew_ticket, &client_hello);
  614. if (wait != ssl_hs_ok) {
  615. return wait;
  616. }
  617. if (session) {
  618. if (session->extended_master_secret && !hs->extended_master_secret) {
  619. // A ClientHello without EMS that attempts to resume a session with EMS
  620. // is fatal to the connection.
  621. OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
  622. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  623. return ssl_hs_error;
  624. }
  625. if (!ssl_session_is_resumable(hs, session.get()) ||
  626. // If the client offers the EMS extension, but the previous session
  627. // didn't use it, then negotiate a new session.
  628. hs->extended_master_secret != session->extended_master_secret) {
  629. session.reset();
  630. }
  631. }
  632. if (session) {
  633. // Use the old session.
  634. hs->ticket_expected = renew_ticket;
  635. ssl->session = std::move(session);
  636. ssl->s3->session_reused = true;
  637. } else {
  638. hs->ticket_expected = tickets_supported;
  639. ssl_set_session(ssl, NULL);
  640. if (!ssl_get_new_session(hs, 1 /* server */)) {
  641. return ssl_hs_error;
  642. }
  643. // Clear the session ID if we want the session to be single-use.
  644. if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
  645. hs->new_session->session_id_length = 0;
  646. }
  647. }
  648. if (ssl->ctx->dos_protection_cb != NULL &&
  649. ssl->ctx->dos_protection_cb(&client_hello) == 0) {
  650. // Connection rejected for DOS reasons.
  651. OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
  652. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  653. return ssl_hs_error;
  654. }
  655. if (ssl->session == NULL) {
  656. hs->new_session->cipher = hs->new_cipher;
  657. // Determine whether to request a client certificate.
  658. hs->cert_request = !!(hs->config->verify_mode & SSL_VERIFY_PEER);
  659. // Only request a certificate if Channel ID isn't negotiated.
  660. if ((hs->config->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
  661. ssl->s3->channel_id_valid) {
  662. hs->cert_request = false;
  663. }
  664. // CertificateRequest may only be sent in certificate-based ciphers.
  665. if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  666. hs->cert_request = false;
  667. }
  668. if (!hs->cert_request) {
  669. // OpenSSL returns X509_V_OK when no certificates are requested. This is
  670. // classed by them as a bug, but it's assumed by at least NGINX.
  671. hs->new_session->verify_result = X509_V_OK;
  672. }
  673. }
  674. // HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was
  675. // deferred. Complete it now.
  676. uint8_t alert = SSL_AD_DECODE_ERROR;
  677. if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) {
  678. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  679. return ssl_hs_error;
  680. }
  681. // Now that all parameters are known, initialize the handshake hash and hash
  682. // the ClientHello.
  683. if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) ||
  684. !ssl_hash_message(hs, msg)) {
  685. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  686. return ssl_hs_error;
  687. }
  688. // Handback includes the whole handshake transcript, so we cannot free the
  689. // transcript buffer in the handback case.
  690. if (!hs->cert_request && !hs->handback) {
  691. hs->transcript.FreeBuffer();
  692. }
  693. ssl->method->next_message(ssl);
  694. hs->state = state12_send_server_hello;
  695. return ssl_hs_ok;
  696. }
  697. static void copy_suffix(Span<uint8_t> out, Span<const uint8_t> in) {
  698. out = out.subspan(out.size() - in.size());
  699. assert(out.size() == in.size());
  700. OPENSSL_memcpy(out.data(), in.data(), in.size());
  701. }
  702. static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) {
  703. SSL *const ssl = hs->ssl;
  704. // We only accept ChannelIDs on connections with ECDHE in order to avoid a
  705. // known attack while we fix ChannelID itself.
  706. if (ssl->s3->channel_id_valid &&
  707. (hs->new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
  708. ssl->s3->channel_id_valid = false;
  709. }
  710. // If this is a resumption and the original handshake didn't support
  711. // ChannelID then we didn't record the original handshake hashes in the
  712. // session and so cannot resume with ChannelIDs.
  713. if (ssl->session != NULL &&
  714. ssl->session->original_handshake_hash_len == 0) {
  715. ssl->s3->channel_id_valid = false;
  716. }
  717. struct OPENSSL_timeval now;
  718. ssl_get_current_time(ssl, &now);
  719. ssl->s3->server_random[0] = now.tv_sec >> 24;
  720. ssl->s3->server_random[1] = now.tv_sec >> 16;
  721. ssl->s3->server_random[2] = now.tv_sec >> 8;
  722. ssl->s3->server_random[3] = now.tv_sec;
  723. if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) {
  724. return ssl_hs_error;
  725. }
  726. // Implement the TLS 1.3 anti-downgrade feature.
  727. if (ssl_supports_version(hs, TLS1_3_VERSION)) {
  728. if (ssl_protocol_version(ssl) == TLS1_2_VERSION) {
  729. if (hs->apply_jdk11_workaround) {
  730. // JDK 11 implements the TLS 1.3 downgrade signal, so we cannot send it
  731. // here. However, the signal is only effective if all TLS 1.2
  732. // ServerHellos produced by the server are marked. Thus we send a
  733. // different non-standard signal for the time being, until JDK 11.0.2 is
  734. // released and clients have updated.
  735. copy_suffix(ssl->s3->server_random, kJDK11DowngradeRandom);
  736. } else {
  737. copy_suffix(ssl->s3->server_random, kTLS13DowngradeRandom);
  738. }
  739. } else {
  740. copy_suffix(ssl->s3->server_random, kTLS12DowngradeRandom);
  741. }
  742. }
  743. const SSL_SESSION *session = hs->new_session.get();
  744. if (ssl->session != nullptr) {
  745. session = ssl->session.get();
  746. }
  747. ScopedCBB cbb;
  748. CBB body, session_id;
  749. if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) ||
  750. !CBB_add_u16(&body, ssl->version) ||
  751. !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
  752. !CBB_add_u8_length_prefixed(&body, &session_id) ||
  753. !CBB_add_bytes(&session_id, session->session_id,
  754. session->session_id_length) ||
  755. !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) ||
  756. !CBB_add_u8(&body, 0 /* no compression */) ||
  757. !ssl_add_serverhello_tlsext(hs, &body) ||
  758. !ssl_add_message_cbb(ssl, cbb.get())) {
  759. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  760. return ssl_hs_error;
  761. }
  762. if (ssl->session != NULL) {
  763. hs->state = state12_send_server_finished;
  764. } else {
  765. hs->state = state12_send_server_certificate;
  766. }
  767. return ssl_hs_ok;
  768. }
  769. static enum ssl_hs_wait_t do_send_server_certificate(SSL_HANDSHAKE *hs) {
  770. SSL *const ssl = hs->ssl;
  771. ScopedCBB cbb;
  772. if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  773. if (!ssl_has_certificate(hs)) {
  774. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
  775. return ssl_hs_error;
  776. }
  777. if (!ssl_output_cert_chain(hs)) {
  778. return ssl_hs_error;
  779. }
  780. if (hs->certificate_status_expected) {
  781. CBB body, ocsp_response;
  782. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  783. SSL3_MT_CERTIFICATE_STATUS) ||
  784. !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
  785. !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
  786. !CBB_add_bytes(
  787. &ocsp_response,
  788. CRYPTO_BUFFER_data(hs->config->cert->ocsp_response.get()),
  789. CRYPTO_BUFFER_len(hs->config->cert->ocsp_response.get())) ||
  790. !ssl_add_message_cbb(ssl, cbb.get())) {
  791. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  792. return ssl_hs_error;
  793. }
  794. }
  795. }
  796. // Assemble ServerKeyExchange parameters if needed.
  797. uint32_t alg_k = hs->new_cipher->algorithm_mkey;
  798. uint32_t alg_a = hs->new_cipher->algorithm_auth;
  799. if (ssl_cipher_requires_server_key_exchange(hs->new_cipher) ||
  800. ((alg_a & SSL_aPSK) && hs->config->psk_identity_hint)) {
  801. // Pre-allocate enough room to comfortably fit an ECDHE public key. Prepend
  802. // the client and server randoms for the signing transcript.
  803. CBB child;
  804. if (!CBB_init(cbb.get(), SSL3_RANDOM_SIZE * 2 + 128) ||
  805. !CBB_add_bytes(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
  806. !CBB_add_bytes(cbb.get(), ssl->s3->server_random, SSL3_RANDOM_SIZE)) {
  807. return ssl_hs_error;
  808. }
  809. // PSK ciphers begin with an identity hint.
  810. if (alg_a & SSL_aPSK) {
  811. size_t len = hs->config->psk_identity_hint == nullptr
  812. ? 0
  813. : strlen(hs->config->psk_identity_hint.get());
  814. if (!CBB_add_u16_length_prefixed(cbb.get(), &child) ||
  815. !CBB_add_bytes(&child,
  816. (const uint8_t *)hs->config->psk_identity_hint.get(),
  817. len)) {
  818. return ssl_hs_error;
  819. }
  820. }
  821. if (alg_k & SSL_kECDHE) {
  822. // Determine the group to use.
  823. uint16_t group_id;
  824. if (!tls1_get_shared_group(hs, &group_id)) {
  825. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  826. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  827. return ssl_hs_error;
  828. }
  829. hs->new_session->group_id = group_id;
  830. // Set up ECDH, generate a key, and emit the public half.
  831. hs->key_shares[0] = SSLKeyShare::Create(group_id);
  832. if (!hs->key_shares[0] ||
  833. !CBB_add_u8(cbb.get(), NAMED_CURVE_TYPE) ||
  834. !CBB_add_u16(cbb.get(), group_id) ||
  835. !CBB_add_u8_length_prefixed(cbb.get(), &child) ||
  836. !hs->key_shares[0]->Offer(&child)) {
  837. return ssl_hs_error;
  838. }
  839. } else {
  840. assert(alg_k & SSL_kPSK);
  841. }
  842. if (!CBBFinishArray(cbb.get(), &hs->server_params)) {
  843. return ssl_hs_error;
  844. }
  845. }
  846. hs->state = state12_send_server_key_exchange;
  847. return ssl_hs_ok;
  848. }
  849. static enum ssl_hs_wait_t do_send_server_key_exchange(SSL_HANDSHAKE *hs) {
  850. SSL *const ssl = hs->ssl;
  851. if (hs->server_params.size() == 0) {
  852. hs->state = state12_send_server_hello_done;
  853. return ssl_hs_ok;
  854. }
  855. ScopedCBB cbb;
  856. CBB body, child;
  857. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  858. SSL3_MT_SERVER_KEY_EXCHANGE) ||
  859. // |hs->server_params| contains a prefix for signing.
  860. hs->server_params.size() < 2 * SSL3_RANDOM_SIZE ||
  861. !CBB_add_bytes(&body, hs->server_params.data() + 2 * SSL3_RANDOM_SIZE,
  862. hs->server_params.size() - 2 * SSL3_RANDOM_SIZE)) {
  863. return ssl_hs_error;
  864. }
  865. // Add a signature.
  866. if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  867. if (!ssl_has_private_key(hs)) {
  868. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  869. return ssl_hs_error;
  870. }
  871. // Determine the signature algorithm.
  872. uint16_t signature_algorithm;
  873. if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
  874. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  875. return ssl_hs_error;
  876. }
  877. if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
  878. if (!CBB_add_u16(&body, signature_algorithm)) {
  879. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  880. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  881. return ssl_hs_error;
  882. }
  883. }
  884. // Add space for the signature.
  885. const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
  886. uint8_t *ptr;
  887. if (!CBB_add_u16_length_prefixed(&body, &child) ||
  888. !CBB_reserve(&child, &ptr, max_sig_len)) {
  889. return ssl_hs_error;
  890. }
  891. size_t sig_len;
  892. switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len,
  893. signature_algorithm, hs->server_params)) {
  894. case ssl_private_key_success:
  895. if (!CBB_did_write(&child, sig_len)) {
  896. return ssl_hs_error;
  897. }
  898. break;
  899. case ssl_private_key_failure:
  900. return ssl_hs_error;
  901. case ssl_private_key_retry:
  902. return ssl_hs_private_key_operation;
  903. }
  904. }
  905. if (!ssl_add_message_cbb(ssl, cbb.get())) {
  906. return ssl_hs_error;
  907. }
  908. hs->server_params.Reset();
  909. hs->state = state12_send_server_hello_done;
  910. return ssl_hs_ok;
  911. }
  912. static enum ssl_hs_wait_t do_send_server_hello_done(SSL_HANDSHAKE *hs) {
  913. SSL *const ssl = hs->ssl;
  914. ScopedCBB cbb;
  915. CBB body;
  916. if (hs->cert_request) {
  917. CBB cert_types, sigalgs_cbb;
  918. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  919. SSL3_MT_CERTIFICATE_REQUEST) ||
  920. !CBB_add_u8_length_prefixed(&body, &cert_types) ||
  921. !CBB_add_u8(&cert_types, SSL3_CT_RSA_SIGN) ||
  922. !CBB_add_u8(&cert_types, TLS_CT_ECDSA_SIGN) ||
  923. // TLS 1.2 has no way to specify different signature algorithms for
  924. // certificates and the online signature, so emit the more restrictive
  925. // certificate list.
  926. (ssl_protocol_version(ssl) >= TLS1_2_VERSION &&
  927. (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb) ||
  928. !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb, true /* certs */))) ||
  929. !ssl_add_client_CA_list(hs, &body) ||
  930. !ssl_add_message_cbb(ssl, cbb.get())) {
  931. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  932. return ssl_hs_error;
  933. }
  934. }
  935. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  936. SSL3_MT_SERVER_HELLO_DONE) ||
  937. !ssl_add_message_cbb(ssl, cbb.get())) {
  938. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  939. return ssl_hs_error;
  940. }
  941. hs->state = state12_read_client_certificate;
  942. return ssl_hs_flush;
  943. }
  944. static enum ssl_hs_wait_t do_read_client_certificate(SSL_HANDSHAKE *hs) {
  945. SSL *const ssl = hs->ssl;
  946. if (hs->handback && hs->new_cipher->algorithm_mkey == SSL_kECDHE) {
  947. return ssl_hs_handback;
  948. }
  949. if (!hs->cert_request) {
  950. hs->state = state12_verify_client_certificate;
  951. return ssl_hs_ok;
  952. }
  953. SSLMessage msg;
  954. if (!ssl->method->get_message(ssl, &msg)) {
  955. return ssl_hs_read_message;
  956. }
  957. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE)) {
  958. return ssl_hs_error;
  959. }
  960. if (!ssl_hash_message(hs, msg)) {
  961. return ssl_hs_error;
  962. }
  963. CBS certificate_msg = msg.body;
  964. uint8_t alert = SSL_AD_DECODE_ERROR;
  965. if (!ssl_parse_cert_chain(&alert, &hs->new_session->certs, &hs->peer_pubkey,
  966. hs->config->retain_only_sha256_of_client_certs
  967. ? hs->new_session->peer_sha256
  968. : nullptr,
  969. &certificate_msg, ssl->ctx->pool)) {
  970. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  971. return ssl_hs_error;
  972. }
  973. if (CBS_len(&certificate_msg) != 0 ||
  974. !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
  975. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  976. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  977. return ssl_hs_error;
  978. }
  979. if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) == 0) {
  980. // No client certificate so the handshake buffer may be discarded.
  981. hs->transcript.FreeBuffer();
  982. if (hs->config->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
  983. // Fail for TLS only if we required a certificate
  984. OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  985. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  986. return ssl_hs_error;
  987. }
  988. // OpenSSL returns X509_V_OK when no certificates are received. This is
  989. // classed by them as a bug, but it's assumed by at least NGINX.
  990. hs->new_session->verify_result = X509_V_OK;
  991. } else if (hs->config->retain_only_sha256_of_client_certs) {
  992. // The hash will have been filled in.
  993. hs->new_session->peer_sha256_valid = 1;
  994. }
  995. ssl->method->next_message(ssl);
  996. hs->state = state12_verify_client_certificate;
  997. return ssl_hs_ok;
  998. }
  999. static enum ssl_hs_wait_t do_verify_client_certificate(SSL_HANDSHAKE *hs) {
  1000. if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) > 0) {
  1001. switch (ssl_verify_peer_cert(hs)) {
  1002. case ssl_verify_ok:
  1003. break;
  1004. case ssl_verify_invalid:
  1005. return ssl_hs_error;
  1006. case ssl_verify_retry:
  1007. return ssl_hs_certificate_verify;
  1008. }
  1009. }
  1010. hs->state = state12_read_client_key_exchange;
  1011. return ssl_hs_ok;
  1012. }
  1013. static enum ssl_hs_wait_t do_read_client_key_exchange(SSL_HANDSHAKE *hs) {
  1014. SSL *const ssl = hs->ssl;
  1015. SSLMessage msg;
  1016. if (!ssl->method->get_message(ssl, &msg)) {
  1017. return ssl_hs_read_message;
  1018. }
  1019. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_KEY_EXCHANGE)) {
  1020. return ssl_hs_error;
  1021. }
  1022. CBS client_key_exchange = msg.body;
  1023. uint32_t alg_k = hs->new_cipher->algorithm_mkey;
  1024. uint32_t alg_a = hs->new_cipher->algorithm_auth;
  1025. // If using a PSK key exchange, parse the PSK identity.
  1026. if (alg_a & SSL_aPSK) {
  1027. CBS psk_identity;
  1028. // If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
  1029. // then this is the only field in the message.
  1030. if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
  1031. ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
  1032. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1033. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1034. return ssl_hs_error;
  1035. }
  1036. if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
  1037. CBS_contains_zero_byte(&psk_identity)) {
  1038. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  1039. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  1040. return ssl_hs_error;
  1041. }
  1042. char *raw = nullptr;
  1043. if (!CBS_strdup(&psk_identity, &raw)) {
  1044. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1045. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1046. return ssl_hs_error;
  1047. }
  1048. hs->new_session->psk_identity.reset(raw);
  1049. }
  1050. // Depending on the key exchange method, compute |premaster_secret|.
  1051. Array<uint8_t> premaster_secret;
  1052. if (alg_k & SSL_kRSA) {
  1053. CBS encrypted_premaster_secret;
  1054. if (!CBS_get_u16_length_prefixed(&client_key_exchange,
  1055. &encrypted_premaster_secret) ||
  1056. CBS_len(&client_key_exchange) != 0) {
  1057. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1058. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1059. return ssl_hs_error;
  1060. }
  1061. // Allocate a buffer large enough for an RSA decryption.
  1062. Array<uint8_t> decrypt_buf;
  1063. if (!decrypt_buf.Init(EVP_PKEY_size(hs->local_pubkey.get()))) {
  1064. return ssl_hs_error;
  1065. }
  1066. // Decrypt with no padding. PKCS#1 padding will be removed as part of the
  1067. // timing-sensitive code below.
  1068. size_t decrypt_len;
  1069. switch (ssl_private_key_decrypt(hs, decrypt_buf.data(), &decrypt_len,
  1070. decrypt_buf.size(),
  1071. encrypted_premaster_secret)) {
  1072. case ssl_private_key_success:
  1073. break;
  1074. case ssl_private_key_failure:
  1075. return ssl_hs_error;
  1076. case ssl_private_key_retry:
  1077. return ssl_hs_private_key_operation;
  1078. }
  1079. if (decrypt_len != decrypt_buf.size()) {
  1080. OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
  1081. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
  1082. return ssl_hs_error;
  1083. }
  1084. CONSTTIME_SECRET(decrypt_buf.data(), decrypt_len);
  1085. // Prepare a random premaster, to be used on invalid padding. See RFC 5246,
  1086. // section 7.4.7.1.
  1087. if (!premaster_secret.Init(SSL_MAX_MASTER_KEY_LENGTH) ||
  1088. !RAND_bytes(premaster_secret.data(), premaster_secret.size())) {
  1089. return ssl_hs_error;
  1090. }
  1091. // The smallest padded premaster is 11 bytes of overhead. Small keys are
  1092. // publicly invalid.
  1093. if (decrypt_len < 11 + premaster_secret.size()) {
  1094. OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
  1095. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
  1096. return ssl_hs_error;
  1097. }
  1098. // Check the padding. See RFC 3447, section 7.2.2.
  1099. size_t padding_len = decrypt_len - premaster_secret.size();
  1100. uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
  1101. constant_time_eq_int_8(decrypt_buf[1], 2);
  1102. for (size_t i = 2; i < padding_len - 1; i++) {
  1103. good &= ~constant_time_is_zero_8(decrypt_buf[i]);
  1104. }
  1105. good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
  1106. // The premaster secret must begin with |client_version|. This too must be
  1107. // checked in constant time (http://eprint.iacr.org/2003/052/).
  1108. good &= constant_time_eq_8(decrypt_buf[padding_len],
  1109. (unsigned)(hs->client_version >> 8));
  1110. good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
  1111. (unsigned)(hs->client_version & 0xff));
  1112. // Select, in constant time, either the decrypted premaster or the random
  1113. // premaster based on |good|.
  1114. for (size_t i = 0; i < premaster_secret.size(); i++) {
  1115. premaster_secret[i] = constant_time_select_8(
  1116. good, decrypt_buf[padding_len + i], premaster_secret[i]);
  1117. }
  1118. } else if (alg_k & SSL_kECDHE) {
  1119. // Parse the ClientKeyExchange.
  1120. CBS peer_key;
  1121. if (!CBS_get_u8_length_prefixed(&client_key_exchange, &peer_key) ||
  1122. CBS_len(&client_key_exchange) != 0) {
  1123. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1124. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1125. return ssl_hs_error;
  1126. }
  1127. // Compute the premaster.
  1128. uint8_t alert = SSL_AD_DECODE_ERROR;
  1129. if (!hs->key_shares[0]->Finish(&premaster_secret, &alert, peer_key)) {
  1130. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  1131. return ssl_hs_error;
  1132. }
  1133. // The key exchange state may now be discarded.
  1134. hs->key_shares[0].reset();
  1135. hs->key_shares[1].reset();
  1136. } else if (!(alg_k & SSL_kPSK)) {
  1137. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1138. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1139. return ssl_hs_error;
  1140. }
  1141. // For a PSK cipher suite, the actual pre-master secret is combined with the
  1142. // pre-shared key.
  1143. if (alg_a & SSL_aPSK) {
  1144. if (hs->config->psk_server_callback == NULL) {
  1145. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1146. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1147. return ssl_hs_error;
  1148. }
  1149. // Look up the key for the identity.
  1150. uint8_t psk[PSK_MAX_PSK_LEN];
  1151. unsigned psk_len = hs->config->psk_server_callback(
  1152. ssl, hs->new_session->psk_identity.get(), psk, sizeof(psk));
  1153. if (psk_len > PSK_MAX_PSK_LEN) {
  1154. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1155. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1156. return ssl_hs_error;
  1157. } else if (psk_len == 0) {
  1158. // PSK related to the given identity not found.
  1159. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
  1160. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNKNOWN_PSK_IDENTITY);
  1161. return ssl_hs_error;
  1162. }
  1163. if (alg_k & SSL_kPSK) {
  1164. // In plain PSK, other_secret is a block of 0s with the same length as the
  1165. // pre-shared key.
  1166. if (!premaster_secret.Init(psk_len)) {
  1167. return ssl_hs_error;
  1168. }
  1169. OPENSSL_memset(premaster_secret.data(), 0, premaster_secret.size());
  1170. }
  1171. ScopedCBB new_premaster;
  1172. CBB child;
  1173. if (!CBB_init(new_premaster.get(),
  1174. 2 + psk_len + 2 + premaster_secret.size()) ||
  1175. !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
  1176. !CBB_add_bytes(&child, premaster_secret.data(),
  1177. premaster_secret.size()) ||
  1178. !CBB_add_u16_length_prefixed(new_premaster.get(), &child) ||
  1179. !CBB_add_bytes(&child, psk, psk_len) ||
  1180. !CBBFinishArray(new_premaster.get(), &premaster_secret)) {
  1181. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1182. return ssl_hs_error;
  1183. }
  1184. }
  1185. if (!ssl_hash_message(hs, msg)) {
  1186. return ssl_hs_error;
  1187. }
  1188. // Compute the master secret.
  1189. hs->new_session->master_key_length = tls1_generate_master_secret(
  1190. hs, hs->new_session->master_key, premaster_secret);
  1191. if (hs->new_session->master_key_length == 0) {
  1192. return ssl_hs_error;
  1193. }
  1194. hs->new_session->extended_master_secret = hs->extended_master_secret;
  1195. CONSTTIME_DECLASSIFY(hs->new_session->master_key,
  1196. hs->new_session->master_key_length);
  1197. ssl->method->next_message(ssl);
  1198. hs->state = state12_read_client_certificate_verify;
  1199. return ssl_hs_ok;
  1200. }
  1201. static enum ssl_hs_wait_t do_read_client_certificate_verify(SSL_HANDSHAKE *hs) {
  1202. SSL *const ssl = hs->ssl;
  1203. // Only RSA and ECDSA client certificates are supported, so a
  1204. // CertificateVerify is required if and only if there's a client certificate.
  1205. if (!hs->peer_pubkey) {
  1206. hs->transcript.FreeBuffer();
  1207. hs->state = state12_read_change_cipher_spec;
  1208. return ssl_hs_ok;
  1209. }
  1210. SSLMessage msg;
  1211. if (!ssl->method->get_message(ssl, &msg)) {
  1212. return ssl_hs_read_message;
  1213. }
  1214. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY)) {
  1215. return ssl_hs_error;
  1216. }
  1217. CBS certificate_verify = msg.body, signature;
  1218. // Determine the signature algorithm.
  1219. uint16_t signature_algorithm = 0;
  1220. if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) {
  1221. if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
  1222. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1223. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1224. return ssl_hs_error;
  1225. }
  1226. uint8_t alert = SSL_AD_DECODE_ERROR;
  1227. if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
  1228. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  1229. return ssl_hs_error;
  1230. }
  1231. hs->new_session->peer_signature_algorithm = signature_algorithm;
  1232. } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm,
  1233. hs->peer_pubkey.get())) {
  1234. OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
  1235. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE);
  1236. return ssl_hs_error;
  1237. }
  1238. // Parse and verify the signature.
  1239. if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
  1240. CBS_len(&certificate_verify) != 0) {
  1241. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1242. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1243. return ssl_hs_error;
  1244. }
  1245. bool sig_ok =
  1246. ssl_public_key_verify(ssl, signature, signature_algorithm,
  1247. hs->peer_pubkey.get(), hs->transcript.buffer());
  1248. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  1249. sig_ok = true;
  1250. ERR_clear_error();
  1251. #endif
  1252. if (!sig_ok) {
  1253. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
  1254. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
  1255. return ssl_hs_error;
  1256. }
  1257. // The handshake buffer is no longer necessary, and we may hash the current
  1258. // message.
  1259. hs->transcript.FreeBuffer();
  1260. if (!ssl_hash_message(hs, msg)) {
  1261. return ssl_hs_error;
  1262. }
  1263. ssl->method->next_message(ssl);
  1264. hs->state = state12_read_change_cipher_spec;
  1265. return ssl_hs_ok;
  1266. }
  1267. static enum ssl_hs_wait_t do_read_change_cipher_spec(SSL_HANDSHAKE *hs) {
  1268. if (hs->handback && hs->ssl->session != NULL) {
  1269. return ssl_hs_handback;
  1270. }
  1271. hs->state = state12_process_change_cipher_spec;
  1272. return ssl_hs_read_change_cipher_spec;
  1273. }
  1274. static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) {
  1275. if (!tls1_change_cipher_state(hs, evp_aead_open)) {
  1276. return ssl_hs_error;
  1277. }
  1278. hs->state = state12_read_next_proto;
  1279. return ssl_hs_ok;
  1280. }
  1281. static enum ssl_hs_wait_t do_read_next_proto(SSL_HANDSHAKE *hs) {
  1282. SSL *const ssl = hs->ssl;
  1283. if (!hs->next_proto_neg_seen) {
  1284. hs->state = state12_read_channel_id;
  1285. return ssl_hs_ok;
  1286. }
  1287. SSLMessage msg;
  1288. if (!ssl->method->get_message(ssl, &msg)) {
  1289. return ssl_hs_read_message;
  1290. }
  1291. if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEXT_PROTO) ||
  1292. !ssl_hash_message(hs, msg)) {
  1293. return ssl_hs_error;
  1294. }
  1295. CBS next_protocol = msg.body, selected_protocol, padding;
  1296. if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
  1297. !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
  1298. CBS_len(&next_protocol) != 0) {
  1299. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1300. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1301. return ssl_hs_error;
  1302. }
  1303. if (!ssl->s3->next_proto_negotiated.CopyFrom(selected_protocol)) {
  1304. return ssl_hs_error;
  1305. }
  1306. ssl->method->next_message(ssl);
  1307. hs->state = state12_read_channel_id;
  1308. return ssl_hs_ok;
  1309. }
  1310. static enum ssl_hs_wait_t do_read_channel_id(SSL_HANDSHAKE *hs) {
  1311. SSL *const ssl = hs->ssl;
  1312. if (!ssl->s3->channel_id_valid) {
  1313. hs->state = state12_read_client_finished;
  1314. return ssl_hs_ok;
  1315. }
  1316. SSLMessage msg;
  1317. if (!ssl->method->get_message(ssl, &msg)) {
  1318. return ssl_hs_read_message;
  1319. }
  1320. if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) ||
  1321. !tls1_verify_channel_id(hs, msg) ||
  1322. !ssl_hash_message(hs, msg)) {
  1323. return ssl_hs_error;
  1324. }
  1325. ssl->method->next_message(ssl);
  1326. hs->state = state12_read_client_finished;
  1327. return ssl_hs_ok;
  1328. }
  1329. static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) {
  1330. SSL *const ssl = hs->ssl;
  1331. enum ssl_hs_wait_t wait = ssl_get_finished(hs);
  1332. if (wait != ssl_hs_ok) {
  1333. return wait;
  1334. }
  1335. if (ssl->session != NULL) {
  1336. hs->state = state12_finish_server_handshake;
  1337. } else {
  1338. hs->state = state12_send_server_finished;
  1339. }
  1340. // If this is a full handshake with ChannelID then record the handshake
  1341. // hashes in |hs->new_session| in case we need them to verify a
  1342. // ChannelID signature on a resumption of this session in the future.
  1343. if (ssl->session == NULL && ssl->s3->channel_id_valid &&
  1344. !tls1_record_handshake_hashes_for_channel_id(hs)) {
  1345. return ssl_hs_error;
  1346. }
  1347. return ssl_hs_ok;
  1348. }
  1349. static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) {
  1350. SSL *const ssl = hs->ssl;
  1351. if (hs->ticket_expected) {
  1352. const SSL_SESSION *session;
  1353. UniquePtr<SSL_SESSION> session_copy;
  1354. if (ssl->session == NULL) {
  1355. // Fix the timeout to measure from the ticket issuance time.
  1356. ssl_session_rebase_time(ssl, hs->new_session.get());
  1357. session = hs->new_session.get();
  1358. } else {
  1359. // We are renewing an existing session. Duplicate the session to adjust
  1360. // the timeout.
  1361. session_copy =
  1362. SSL_SESSION_dup(ssl->session.get(), SSL_SESSION_INCLUDE_NONAUTH);
  1363. if (!session_copy) {
  1364. return ssl_hs_error;
  1365. }
  1366. ssl_session_rebase_time(ssl, session_copy.get());
  1367. session = session_copy.get();
  1368. }
  1369. ScopedCBB cbb;
  1370. CBB body, ticket;
  1371. if (!ssl->method->init_message(ssl, cbb.get(), &body,
  1372. SSL3_MT_NEW_SESSION_TICKET) ||
  1373. !CBB_add_u32(&body, session->timeout) ||
  1374. !CBB_add_u16_length_prefixed(&body, &ticket) ||
  1375. !ssl_encrypt_ticket(hs, &ticket, session) ||
  1376. !ssl_add_message_cbb(ssl, cbb.get())) {
  1377. return ssl_hs_error;
  1378. }
  1379. }
  1380. if (!ssl->method->add_change_cipher_spec(ssl) ||
  1381. !tls1_change_cipher_state(hs, evp_aead_seal) ||
  1382. !ssl_send_finished(hs)) {
  1383. return ssl_hs_error;
  1384. }
  1385. if (ssl->session != NULL) {
  1386. hs->state = state12_read_change_cipher_spec;
  1387. } else {
  1388. hs->state = state12_finish_server_handshake;
  1389. }
  1390. return ssl_hs_flush;
  1391. }
  1392. static enum ssl_hs_wait_t do_finish_server_handshake(SSL_HANDSHAKE *hs) {
  1393. SSL *const ssl = hs->ssl;
  1394. if (hs->handback) {
  1395. return ssl_hs_handback;
  1396. }
  1397. ssl->method->on_handshake_complete(ssl);
  1398. // If we aren't retaining peer certificates then we can discard it now.
  1399. if (hs->new_session != NULL &&
  1400. hs->config->retain_only_sha256_of_client_certs) {
  1401. hs->new_session->certs.reset();
  1402. ssl->ctx->x509_method->session_clear(hs->new_session.get());
  1403. }
  1404. if (ssl->session != NULL) {
  1405. ssl->s3->established_session = UpRef(ssl->session);
  1406. } else {
  1407. ssl->s3->established_session = std::move(hs->new_session);
  1408. ssl->s3->established_session->not_resumable = false;
  1409. }
  1410. hs->handshake_finalized = true;
  1411. ssl->s3->initial_handshake_complete = true;
  1412. ssl_update_cache(hs, SSL_SESS_CACHE_SERVER);
  1413. hs->state = state12_done;
  1414. return ssl_hs_ok;
  1415. }
  1416. enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs) {
  1417. while (hs->state != state12_done) {
  1418. enum ssl_hs_wait_t ret = ssl_hs_error;
  1419. enum tls12_server_hs_state_t state =
  1420. static_cast<enum tls12_server_hs_state_t>(hs->state);
  1421. switch (state) {
  1422. case state12_start_accept:
  1423. ret = do_start_accept(hs);
  1424. break;
  1425. case state12_read_client_hello:
  1426. ret = do_read_client_hello(hs);
  1427. break;
  1428. case state12_select_certificate:
  1429. ret = do_select_certificate(hs);
  1430. break;
  1431. case state12_tls13:
  1432. ret = do_tls13(hs);
  1433. break;
  1434. case state12_select_parameters:
  1435. ret = do_select_parameters(hs);
  1436. break;
  1437. case state12_send_server_hello:
  1438. ret = do_send_server_hello(hs);
  1439. break;
  1440. case state12_send_server_certificate:
  1441. ret = do_send_server_certificate(hs);
  1442. break;
  1443. case state12_send_server_key_exchange:
  1444. ret = do_send_server_key_exchange(hs);
  1445. break;
  1446. case state12_send_server_hello_done:
  1447. ret = do_send_server_hello_done(hs);
  1448. break;
  1449. case state12_read_client_certificate:
  1450. ret = do_read_client_certificate(hs);
  1451. break;
  1452. case state12_verify_client_certificate:
  1453. ret = do_verify_client_certificate(hs);
  1454. break;
  1455. case state12_read_client_key_exchange:
  1456. ret = do_read_client_key_exchange(hs);
  1457. break;
  1458. case state12_read_client_certificate_verify:
  1459. ret = do_read_client_certificate_verify(hs);
  1460. break;
  1461. case state12_read_change_cipher_spec:
  1462. ret = do_read_change_cipher_spec(hs);
  1463. break;
  1464. case state12_process_change_cipher_spec:
  1465. ret = do_process_change_cipher_spec(hs);
  1466. break;
  1467. case state12_read_next_proto:
  1468. ret = do_read_next_proto(hs);
  1469. break;
  1470. case state12_read_channel_id:
  1471. ret = do_read_channel_id(hs);
  1472. break;
  1473. case state12_read_client_finished:
  1474. ret = do_read_client_finished(hs);
  1475. break;
  1476. case state12_send_server_finished:
  1477. ret = do_send_server_finished(hs);
  1478. break;
  1479. case state12_finish_server_handshake:
  1480. ret = do_finish_server_handshake(hs);
  1481. break;
  1482. case state12_done:
  1483. ret = ssl_hs_ok;
  1484. break;
  1485. }
  1486. if (hs->state != state) {
  1487. ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1);
  1488. }
  1489. if (ret != ssl_hs_ok) {
  1490. return ret;
  1491. }
  1492. }
  1493. ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1);
  1494. return ssl_hs_ok;
  1495. }
  1496. const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs) {
  1497. enum tls12_server_hs_state_t state =
  1498. static_cast<enum tls12_server_hs_state_t>(hs->state);
  1499. switch (state) {
  1500. case state12_start_accept:
  1501. return "TLS server start_accept";
  1502. case state12_read_client_hello:
  1503. return "TLS server read_client_hello";
  1504. case state12_select_certificate:
  1505. return "TLS server select_certificate";
  1506. case state12_tls13:
  1507. return tls13_server_handshake_state(hs);
  1508. case state12_select_parameters:
  1509. return "TLS server select_parameters";
  1510. case state12_send_server_hello:
  1511. return "TLS server send_server_hello";
  1512. case state12_send_server_certificate:
  1513. return "TLS server send_server_certificate";
  1514. case state12_send_server_key_exchange:
  1515. return "TLS server send_server_key_exchange";
  1516. case state12_send_server_hello_done:
  1517. return "TLS server send_server_hello_done";
  1518. case state12_read_client_certificate:
  1519. return "TLS server read_client_certificate";
  1520. case state12_verify_client_certificate:
  1521. return "TLS server verify_client_certificate";
  1522. case state12_read_client_key_exchange:
  1523. return "TLS server read_client_key_exchange";
  1524. case state12_read_client_certificate_verify:
  1525. return "TLS server read_client_certificate_verify";
  1526. case state12_read_change_cipher_spec:
  1527. return "TLS server read_change_cipher_spec";
  1528. case state12_process_change_cipher_spec:
  1529. return "TLS server process_change_cipher_spec";
  1530. case state12_read_next_proto:
  1531. return "TLS server read_next_proto";
  1532. case state12_read_channel_id:
  1533. return "TLS server read_channel_id";
  1534. case state12_read_client_finished:
  1535. return "TLS server read_client_finished";
  1536. case state12_send_server_finished:
  1537. return "TLS server send_server_finished";
  1538. case state12_finish_server_handshake:
  1539. return "TLS server finish_server_handshake";
  1540. case state12_done:
  1541. return "TLS server done";
  1542. }
  1543. return "TLS server unknown";
  1544. }
  1545. BSSL_NAMESPACE_END