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.
 
 
 
 
 
 

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