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.
 
 
 
 
 
 

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