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.
 
 
 
 
 
 

1979 lines
62 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. */
  149. #include <openssl/ssl.h>
  150. #include <assert.h>
  151. #include <string.h>
  152. #include <openssl/bn.h>
  153. #include <openssl/buf.h>
  154. #include <openssl/bytestring.h>
  155. #include <openssl/dh.h>
  156. #include <openssl/ec_key.h>
  157. #include <openssl/ecdsa.h>
  158. #include <openssl/err.h>
  159. #include <openssl/evp.h>
  160. #include <openssl/md5.h>
  161. #include <openssl/mem.h>
  162. #include <openssl/rand.h>
  163. #include <openssl/x509.h>
  164. #include <openssl/x509v3.h>
  165. #include "internal.h"
  166. static int ssl3_send_client_hello(SSL *ssl);
  167. static int dtls1_get_hello_verify(SSL *ssl);
  168. static int ssl3_get_server_hello(SSL *ssl);
  169. static int ssl3_get_server_certificate(SSL *ssl);
  170. static int ssl3_get_cert_status(SSL *ssl);
  171. static int ssl3_verify_server_cert(SSL *ssl);
  172. static int ssl3_get_server_key_exchange(SSL *ssl);
  173. static int ssl3_get_certificate_request(SSL *ssl);
  174. static int ssl3_get_server_hello_done(SSL *ssl);
  175. static int ssl3_send_client_certificate(SSL *ssl);
  176. static int ssl3_send_client_key_exchange(SSL *ssl);
  177. static int ssl3_send_cert_verify(SSL *ssl);
  178. static int ssl3_send_next_proto(SSL *ssl);
  179. static int ssl3_send_channel_id(SSL *ssl);
  180. static int ssl3_get_new_session_ticket(SSL *ssl);
  181. int ssl3_connect(SSL *ssl) {
  182. int ret = -1;
  183. int state, skip = 0;
  184. assert(ssl->handshake_func == ssl3_connect);
  185. assert(!ssl->server);
  186. for (;;) {
  187. state = ssl->state;
  188. switch (ssl->state) {
  189. case SSL_ST_INIT:
  190. ssl->state = SSL_ST_CONNECT;
  191. skip = 1;
  192. break;
  193. case SSL_ST_CONNECT:
  194. ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
  195. ssl->s3->hs = ssl_handshake_new(tls13_client_handshake);
  196. if (ssl->s3->hs == NULL) {
  197. ret = -1;
  198. goto end;
  199. }
  200. if (!ssl_init_wbio_buffer(ssl)) {
  201. ret = -1;
  202. goto end;
  203. }
  204. ssl->state = SSL3_ST_CW_CLNT_HELLO_A;
  205. break;
  206. case SSL3_ST_CW_CLNT_HELLO_A:
  207. case SSL3_ST_CW_CLNT_HELLO_B:
  208. ret = ssl3_send_client_hello(ssl);
  209. if (ret <= 0) {
  210. goto end;
  211. }
  212. if (!SSL_is_dtls(ssl) || ssl->d1->send_cookie) {
  213. ssl->s3->tmp.next_state = SSL3_ST_CR_SRVR_HELLO_A;
  214. } else {
  215. ssl->s3->tmp.next_state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
  216. }
  217. ssl->state = SSL3_ST_CW_FLUSH;
  218. break;
  219. case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
  220. assert(SSL_is_dtls(ssl));
  221. ret = dtls1_get_hello_verify(ssl);
  222. if (ret <= 0) {
  223. goto end;
  224. }
  225. if (ssl->d1->send_cookie) {
  226. ssl->method->received_flight(ssl);
  227. ssl->state = SSL3_ST_CW_CLNT_HELLO_A;
  228. } else {
  229. ssl->state = SSL3_ST_CR_SRVR_HELLO_A;
  230. }
  231. break;
  232. case SSL3_ST_CR_SRVR_HELLO_A:
  233. ret = ssl3_get_server_hello(ssl);
  234. if (ssl->state == SSL_ST_TLS13) {
  235. break;
  236. }
  237. if (ret <= 0) {
  238. goto end;
  239. }
  240. if (ssl->session != NULL) {
  241. ssl->state = SSL3_ST_CR_SESSION_TICKET_A;
  242. } else {
  243. ssl->state = SSL3_ST_CR_CERT_A;
  244. }
  245. break;
  246. case SSL3_ST_CR_CERT_A:
  247. if (ssl->s3->hs->use_cert_auth) {
  248. ret = ssl3_get_server_certificate(ssl);
  249. if (ret <= 0) {
  250. goto end;
  251. }
  252. } else {
  253. skip = 1;
  254. }
  255. ssl->state = SSL3_ST_CR_CERT_STATUS_A;
  256. break;
  257. case SSL3_ST_CR_CERT_STATUS_A:
  258. if (ssl->s3->tmp.certificate_status_expected) {
  259. ret = ssl3_get_cert_status(ssl);
  260. if (ret <= 0) {
  261. goto end;
  262. }
  263. } else {
  264. skip = 1;
  265. }
  266. ssl->state = SSL3_ST_VERIFY_SERVER_CERT;
  267. break;
  268. case SSL3_ST_VERIFY_SERVER_CERT:
  269. if (ssl->s3->hs->use_cert_auth) {
  270. ret = ssl3_verify_server_cert(ssl);
  271. if (ret <= 0) {
  272. goto end;
  273. }
  274. } else {
  275. skip = 1;
  276. }
  277. ssl->state = SSL3_ST_CR_KEY_EXCH_A;
  278. break;
  279. case SSL3_ST_CR_KEY_EXCH_A:
  280. ret = ssl3_get_server_key_exchange(ssl);
  281. if (ret <= 0) {
  282. goto end;
  283. }
  284. ssl->state = SSL3_ST_CR_CERT_REQ_A;
  285. break;
  286. case SSL3_ST_CR_CERT_REQ_A:
  287. if (ssl->s3->hs->use_cert_auth) {
  288. ret = ssl3_get_certificate_request(ssl);
  289. if (ret <= 0) {
  290. goto end;
  291. }
  292. } else {
  293. skip = 1;
  294. }
  295. ssl->state = SSL3_ST_CR_SRVR_DONE_A;
  296. break;
  297. case SSL3_ST_CR_SRVR_DONE_A:
  298. ret = ssl3_get_server_hello_done(ssl);
  299. if (ret <= 0) {
  300. goto end;
  301. }
  302. ssl->method->received_flight(ssl);
  303. ssl->state = SSL3_ST_CW_CERT_A;
  304. break;
  305. case SSL3_ST_CW_CERT_A:
  306. case SSL3_ST_CW_CERT_B:
  307. case SSL3_ST_CW_CERT_C:
  308. if (ssl->s3->tmp.cert_request) {
  309. ret = ssl3_send_client_certificate(ssl);
  310. if (ret <= 0) {
  311. goto end;
  312. }
  313. } else {
  314. skip = 1;
  315. }
  316. ssl->state = SSL3_ST_CW_KEY_EXCH_A;
  317. break;
  318. case SSL3_ST_CW_KEY_EXCH_A:
  319. case SSL3_ST_CW_KEY_EXCH_B:
  320. ret = ssl3_send_client_key_exchange(ssl);
  321. if (ret <= 0) {
  322. goto end;
  323. }
  324. ssl->state = SSL3_ST_CW_CERT_VRFY_A;
  325. break;
  326. case SSL3_ST_CW_CERT_VRFY_A:
  327. case SSL3_ST_CW_CERT_VRFY_B:
  328. case SSL3_ST_CW_CERT_VRFY_C:
  329. if (ssl->s3->tmp.cert_request) {
  330. ret = ssl3_send_cert_verify(ssl);
  331. if (ret <= 0) {
  332. goto end;
  333. }
  334. } else {
  335. skip = 1;
  336. }
  337. ssl->state = SSL3_ST_CW_CHANGE;
  338. break;
  339. case SSL3_ST_CW_CHANGE:
  340. ret = ssl->method->send_change_cipher_spec(ssl);
  341. if (ret <= 0) {
  342. goto end;
  343. }
  344. ssl->state = SSL3_ST_CW_NEXT_PROTO_A;
  345. if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
  346. ret = -1;
  347. goto end;
  348. }
  349. break;
  350. case SSL3_ST_CW_NEXT_PROTO_A:
  351. case SSL3_ST_CW_NEXT_PROTO_B:
  352. if (ssl->s3->next_proto_neg_seen) {
  353. ret = ssl3_send_next_proto(ssl);
  354. if (ret <= 0) {
  355. goto end;
  356. }
  357. } else {
  358. skip = 1;
  359. }
  360. ssl->state = SSL3_ST_CW_CHANNEL_ID_A;
  361. break;
  362. case SSL3_ST_CW_CHANNEL_ID_A:
  363. case SSL3_ST_CW_CHANNEL_ID_B:
  364. if (ssl->s3->tlsext_channel_id_valid) {
  365. ret = ssl3_send_channel_id(ssl);
  366. if (ret <= 0) {
  367. goto end;
  368. }
  369. } else {
  370. skip = 1;
  371. }
  372. ssl->state = SSL3_ST_CW_FINISHED_A;
  373. break;
  374. case SSL3_ST_CW_FINISHED_A:
  375. case SSL3_ST_CW_FINISHED_B:
  376. ret = ssl3_send_finished(ssl, SSL3_ST_CW_FINISHED_A,
  377. SSL3_ST_CW_FINISHED_B);
  378. if (ret <= 0) {
  379. goto end;
  380. }
  381. ssl->state = SSL3_ST_CW_FLUSH;
  382. if (ssl->session != NULL) {
  383. ssl->s3->tmp.next_state = SSL_ST_OK;
  384. } else {
  385. /* This is a non-resumption handshake. If it involves ChannelID, then
  386. * record the handshake hashes at this point in the session so that
  387. * any resumption of this session with ChannelID can sign those
  388. * hashes. */
  389. ret = tls1_record_handshake_hashes_for_channel_id(ssl);
  390. if (ret <= 0) {
  391. goto end;
  392. }
  393. if ((SSL_get_mode(ssl) & SSL_MODE_ENABLE_FALSE_START) &&
  394. ssl3_can_false_start(ssl) &&
  395. /* No False Start on renegotiation (would complicate the state
  396. * machine). */
  397. !ssl->s3->initial_handshake_complete) {
  398. ssl->s3->tmp.next_state = SSL3_ST_FALSE_START;
  399. } else {
  400. ssl->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
  401. }
  402. }
  403. break;
  404. case SSL3_ST_FALSE_START:
  405. ssl->state = SSL3_ST_CR_SESSION_TICKET_A;
  406. ssl->s3->tmp.in_false_start = 1;
  407. ssl_free_wbio_buffer(ssl);
  408. ret = 1;
  409. goto end;
  410. case SSL3_ST_CR_SESSION_TICKET_A:
  411. if (ssl->tlsext_ticket_expected) {
  412. ret = ssl3_get_new_session_ticket(ssl);
  413. if (ret <= 0) {
  414. goto end;
  415. }
  416. } else {
  417. skip = 1;
  418. }
  419. ssl->state = SSL3_ST_CR_CHANGE;
  420. break;
  421. case SSL3_ST_CR_CHANGE:
  422. ret = ssl->method->read_change_cipher_spec(ssl);
  423. if (ret <= 0) {
  424. goto end;
  425. }
  426. if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_CLIENT_READ)) {
  427. ret = -1;
  428. goto end;
  429. }
  430. ssl->state = SSL3_ST_CR_FINISHED_A;
  431. break;
  432. case SSL3_ST_CR_FINISHED_A:
  433. ret = ssl3_get_finished(ssl);
  434. if (ret <= 0) {
  435. goto end;
  436. }
  437. ssl->method->received_flight(ssl);
  438. if (ssl->session != NULL) {
  439. ssl->state = SSL3_ST_CW_CHANGE;
  440. } else {
  441. ssl->state = SSL_ST_OK;
  442. }
  443. break;
  444. case SSL3_ST_CW_FLUSH:
  445. if (BIO_flush(ssl->wbio) <= 0) {
  446. ssl->rwstate = SSL_WRITING;
  447. ret = -1;
  448. goto end;
  449. }
  450. ssl->state = ssl->s3->tmp.next_state;
  451. if (ssl->state != SSL_ST_OK) {
  452. ssl->method->expect_flight(ssl);
  453. }
  454. break;
  455. case SSL_ST_TLS13:
  456. ret = tls13_handshake(ssl);
  457. if (ret <= 0) {
  458. goto end;
  459. }
  460. ssl->state = SSL_ST_OK;
  461. break;
  462. case SSL_ST_OK:
  463. /* Clean a few things up. */
  464. ssl3_cleanup_key_block(ssl);
  465. ssl->method->release_current_message(ssl, 1 /* free_buffer */);
  466. SSL_SESSION_free(ssl->s3->established_session);
  467. if (ssl->session != NULL) {
  468. SSL_SESSION_up_ref(ssl->session);
  469. ssl->s3->established_session = ssl->session;
  470. } else {
  471. /* We make a copy of the session in order to maintain the immutability
  472. * of the new established_session due to False Start. The caller may
  473. * have taken a reference to the temporary session. */
  474. ssl->s3->established_session =
  475. SSL_SESSION_dup(ssl->s3->new_session, SSL_SESSION_DUP_ALL);
  476. if (ssl->s3->established_session == NULL) {
  477. /* Do not stay in SSL_ST_OK, to avoid confusing |SSL_in_init|
  478. * callers. */
  479. ssl->state = SSL_ST_ERROR;
  480. skip = 1;
  481. ret = -1;
  482. goto end;
  483. }
  484. ssl->s3->established_session->not_resumable = 0;
  485. SSL_SESSION_free(ssl->s3->new_session);
  486. ssl->s3->new_session = NULL;
  487. }
  488. /* Remove write buffering now. */
  489. ssl_free_wbio_buffer(ssl);
  490. ssl_handshake_free(ssl->s3->hs);
  491. ssl->s3->hs = NULL;
  492. const int is_initial_handshake = !ssl->s3->initial_handshake_complete;
  493. ssl->s3->tmp.in_false_start = 0;
  494. ssl->s3->initial_handshake_complete = 1;
  495. if (is_initial_handshake) {
  496. /* Renegotiations do not participate in session resumption. */
  497. ssl_update_cache(ssl, SSL_SESS_CACHE_CLIENT);
  498. }
  499. ret = 1;
  500. ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
  501. goto end;
  502. case SSL_ST_ERROR:
  503. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  504. ret = -1;
  505. goto end;
  506. default:
  507. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
  508. ret = -1;
  509. goto end;
  510. }
  511. if (!ssl->s3->tmp.reuse_message && !skip && ssl->state != state) {
  512. int new_state = ssl->state;
  513. ssl->state = state;
  514. ssl_do_info_callback(ssl, SSL_CB_CONNECT_LOOP, 1);
  515. ssl->state = new_state;
  516. }
  517. skip = 0;
  518. }
  519. end:
  520. ssl_do_info_callback(ssl, SSL_CB_CONNECT_EXIT, ret);
  521. return ret;
  522. }
  523. static int ssl_write_client_cipher_list(SSL *ssl, CBB *out,
  524. uint16_t min_version,
  525. uint16_t max_version,
  526. uint16_t real_max_version) {
  527. /* Prepare disabled cipher masks. */
  528. ssl_set_client_disabled(ssl);
  529. CBB child;
  530. if (!CBB_add_u16_length_prefixed(out, &child)) {
  531. return 0;
  532. }
  533. STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(ssl);
  534. int any_enabled = 0;
  535. for (size_t i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
  536. const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i);
  537. /* Skip disabled ciphers */
  538. if ((cipher->algorithm_mkey & ssl->cert->mask_k) ||
  539. (cipher->algorithm_auth & ssl->cert->mask_a)) {
  540. continue;
  541. }
  542. if (SSL_CIPHER_get_min_version(cipher) > max_version ||
  543. SSL_CIPHER_get_max_version(cipher) < min_version) {
  544. continue;
  545. }
  546. any_enabled = 1;
  547. if (!CBB_add_u16(&child, ssl_cipher_get_value(cipher))) {
  548. return 0;
  549. }
  550. /* Add PSK ciphers for TLS 1.3 resumption. */
  551. if (ssl->session != NULL &&
  552. ssl->method->version_from_wire(ssl->session->ssl_version) >=
  553. TLS1_3_VERSION) {
  554. uint16_t resumption_cipher;
  555. if (ssl_cipher_get_ecdhe_psk_cipher(cipher, &resumption_cipher) &&
  556. !CBB_add_u16(&child, resumption_cipher)) {
  557. return 0;
  558. }
  559. }
  560. }
  561. /* If all ciphers were disabled, return the error to the caller. */
  562. if (!any_enabled) {
  563. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_AVAILABLE);
  564. return 0;
  565. }
  566. /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is
  567. * added. */
  568. if (ssl->client_version == SSL3_VERSION &&
  569. !ssl->s3->initial_handshake_complete) {
  570. if (!CBB_add_u16(&child, SSL3_CK_SCSV & 0xffff)) {
  571. return 0;
  572. }
  573. }
  574. if ((ssl->mode & SSL_MODE_SEND_FALLBACK_SCSV) ||
  575. real_max_version > max_version) {
  576. if (!CBB_add_u16(&child, SSL3_CK_FALLBACK_SCSV & 0xffff)) {
  577. return 0;
  578. }
  579. }
  580. return CBB_flush(out);
  581. }
  582. int ssl_add_client_hello_body(SSL *ssl, CBB *body) {
  583. uint16_t min_version, max_version, real_max_version;
  584. if (!ssl_get_full_version_range(ssl, &min_version, &max_version,
  585. &real_max_version)) {
  586. return 0;
  587. }
  588. /* Renegotiations do not participate in session resumption. */
  589. int has_session = ssl->session != NULL &&
  590. !ssl->s3->initial_handshake_complete;
  591. CBB child;
  592. if (!CBB_add_u16(body, ssl->client_version) ||
  593. !CBB_add_bytes(body, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
  594. !CBB_add_u8_length_prefixed(body, &child) ||
  595. (has_session &&
  596. !CBB_add_bytes(&child, ssl->session->session_id,
  597. ssl->session->session_id_length))) {
  598. return 0;
  599. }
  600. if (SSL_is_dtls(ssl)) {
  601. if (!CBB_add_u8_length_prefixed(body, &child) ||
  602. !CBB_add_bytes(&child, ssl->d1->cookie, ssl->d1->cookie_len)) {
  603. return 0;
  604. }
  605. }
  606. size_t header_len =
  607. SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH;
  608. if (!ssl_write_client_cipher_list(ssl, body, min_version, max_version,
  609. real_max_version) ||
  610. !CBB_add_u8(body, 1 /* one compression method */) ||
  611. !CBB_add_u8(body, 0 /* null compression */) ||
  612. !ssl_add_clienthello_tlsext(ssl, body, header_len + CBB_len(body))) {
  613. return 0;
  614. }
  615. return 1;
  616. }
  617. static int ssl3_send_client_hello(SSL *ssl) {
  618. if (ssl->state == SSL3_ST_CW_CLNT_HELLO_B) {
  619. return ssl->method->write_message(ssl);
  620. }
  621. /* The handshake buffer is reset on every ClientHello. Notably, in DTLS, we
  622. * may send multiple ClientHellos if we receive HelloVerifyRequest. */
  623. if (!ssl3_init_handshake_buffer(ssl)) {
  624. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  625. return -1;
  626. }
  627. CBB cbb;
  628. CBB_zero(&cbb);
  629. uint16_t min_version, max_version;
  630. if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
  631. goto err;
  632. }
  633. assert(ssl->state == SSL3_ST_CW_CLNT_HELLO_A);
  634. if (!ssl->s3->have_version) {
  635. ssl->version = ssl->method->version_to_wire(max_version);
  636. /* Only set |ssl->client_version| on the initial handshake. Renegotiations,
  637. * although locked to a version, reuse the value. When using the plain RSA
  638. * key exchange, the ClientHello version is checked in the premaster secret.
  639. * Some servers fail when this value changes. */
  640. ssl->client_version = ssl->version;
  641. }
  642. /* If the configured session has expired or was created at a disabled
  643. * version, drop it. */
  644. if (ssl->session != NULL) {
  645. uint16_t session_version =
  646. ssl->method->version_from_wire(ssl->session->ssl_version);
  647. if ((session_version < TLS1_3_VERSION &&
  648. ssl->session->session_id_length == 0) ||
  649. ssl->session->not_resumable ||
  650. !ssl_session_is_time_valid(ssl, ssl->session) ||
  651. session_version < min_version || session_version > max_version) {
  652. ssl_set_session(ssl, NULL);
  653. }
  654. }
  655. /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
  656. * renegerate the client_random. The random must be reused. */
  657. if ((!SSL_is_dtls(ssl) || !ssl->d1->send_cookie) &&
  658. !RAND_bytes(ssl->s3->client_random, sizeof(ssl->s3->client_random))) {
  659. goto err;
  660. }
  661. CBB body;
  662. if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CLIENT_HELLO) ||
  663. !ssl_add_client_hello_body(ssl, &body) ||
  664. !ssl->method->finish_message(ssl, &cbb)) {
  665. goto err;
  666. }
  667. ssl->state = SSL3_ST_CW_CLNT_HELLO_B;
  668. return ssl->method->write_message(ssl);
  669. err:
  670. CBB_cleanup(&cbb);
  671. return -1;
  672. }
  673. static int dtls1_get_hello_verify(SSL *ssl) {
  674. int al;
  675. CBS hello_verify_request, cookie;
  676. uint16_t server_version;
  677. int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
  678. if (ret <= 0) {
  679. return ret;
  680. }
  681. if (ssl->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
  682. ssl->d1->send_cookie = 0;
  683. ssl->s3->tmp.reuse_message = 1;
  684. return 1;
  685. }
  686. CBS_init(&hello_verify_request, ssl->init_msg, ssl->init_num);
  687. if (!CBS_get_u16(&hello_verify_request, &server_version) ||
  688. !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
  689. CBS_len(&hello_verify_request) != 0) {
  690. al = SSL_AD_DECODE_ERROR;
  691. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  692. goto f_err;
  693. }
  694. if (CBS_len(&cookie) > sizeof(ssl->d1->cookie)) {
  695. al = SSL_AD_ILLEGAL_PARAMETER;
  696. goto f_err;
  697. }
  698. memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
  699. ssl->d1->cookie_len = CBS_len(&cookie);
  700. ssl->d1->send_cookie = 1;
  701. return 1;
  702. f_err:
  703. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  704. return -1;
  705. }
  706. static int ssl3_get_server_hello(SSL *ssl) {
  707. STACK_OF(SSL_CIPHER) *sk;
  708. const SSL_CIPHER *c;
  709. CERT *ct = ssl->cert;
  710. int al = SSL_AD_INTERNAL_ERROR;
  711. CBS server_hello, server_random, session_id;
  712. uint16_t server_wire_version, server_version, cipher_suite;
  713. uint8_t compression_method;
  714. int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
  715. if (ret <= 0) {
  716. uint32_t err = ERR_peek_error();
  717. if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
  718. ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
  719. /* Add a dedicated error code to the queue for a handshake_failure alert
  720. * in response to ClientHello. This matches NSS's client behavior and
  721. * gives a better error on a (probable) failure to negotiate initial
  722. * parameters. Note: this error code comes after the original one.
  723. *
  724. * See https://crbug.com/446505. */
  725. OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
  726. }
  727. return ret;
  728. }
  729. if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_HELLO &&
  730. ssl->s3->tmp.message_type != SSL3_MT_HELLO_RETRY_REQUEST) {
  731. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  732. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
  733. return -1;
  734. }
  735. CBS_init(&server_hello, ssl->init_msg, ssl->init_num);
  736. if (!CBS_get_u16(&server_hello, &server_wire_version)) {
  737. al = SSL_AD_DECODE_ERROR;
  738. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  739. goto f_err;
  740. }
  741. server_version = ssl->method->version_from_wire(server_wire_version);
  742. uint16_t min_version, max_version, real_max_version;
  743. if (!ssl_get_full_version_range(ssl, &min_version, &max_version,
  744. &real_max_version) ||
  745. server_version < min_version || server_version > max_version) {
  746. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
  747. al = SSL_AD_PROTOCOL_VERSION;
  748. goto f_err;
  749. }
  750. assert(ssl->s3->have_version == ssl->s3->initial_handshake_complete);
  751. if (!ssl->s3->have_version) {
  752. ssl->version = server_wire_version;
  753. ssl->s3->enc_method = ssl3_get_enc_method(server_version);
  754. assert(ssl->s3->enc_method != NULL);
  755. /* At this point, the connection's version is known and ssl->version is
  756. * fixed. Begin enforcing the record-layer version. */
  757. ssl->s3->have_version = 1;
  758. } else if (server_wire_version != ssl->version) {
  759. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
  760. al = SSL_AD_PROTOCOL_VERSION;
  761. goto f_err;
  762. }
  763. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  764. ssl->state = SSL_ST_TLS13;
  765. return 1;
  766. }
  767. if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
  768. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  769. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
  770. return -1;
  771. }
  772. if (!CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
  773. !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
  774. CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
  775. !CBS_get_u16(&server_hello, &cipher_suite) ||
  776. !CBS_get_u8(&server_hello, &compression_method)) {
  777. al = SSL_AD_DECODE_ERROR;
  778. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  779. goto f_err;
  780. }
  781. /* Copy over the server random. */
  782. memcpy(ssl->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
  783. /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS
  784. * 1.3 is finalized and we are not implementing a draft version. */
  785. if (!ssl->s3->initial_handshake_complete && ssl->session != NULL &&
  786. ssl->session->session_id_length != 0 &&
  787. CBS_mem_equal(&session_id, ssl->session->session_id,
  788. ssl->session->session_id_length)) {
  789. ssl->s3->session_reused = 1;
  790. } else {
  791. /* The session wasn't resumed. Create a fresh SSL_SESSION to
  792. * fill out. */
  793. ssl_set_session(ssl, NULL);
  794. if (!ssl_get_new_session(ssl, 0 /* client */)) {
  795. goto f_err;
  796. }
  797. /* Note: session_id could be empty. */
  798. ssl->s3->new_session->session_id_length = CBS_len(&session_id);
  799. memcpy(ssl->s3->new_session->session_id, CBS_data(&session_id),
  800. CBS_len(&session_id));
  801. }
  802. c = SSL_get_cipher_by_value(cipher_suite);
  803. if (c == NULL) {
  804. /* unknown cipher */
  805. al = SSL_AD_ILLEGAL_PARAMETER;
  806. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_RETURNED);
  807. goto f_err;
  808. }
  809. /* If the cipher is disabled then we didn't sent it in the ClientHello, so if
  810. * the server selected it, it's an error. */
  811. if ((c->algorithm_mkey & ct->mask_k) || (c->algorithm_auth & ct->mask_a) ||
  812. SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) ||
  813. SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl)) {
  814. al = SSL_AD_ILLEGAL_PARAMETER;
  815. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
  816. goto f_err;
  817. }
  818. sk = ssl_get_ciphers_by_id(ssl);
  819. if (!sk_SSL_CIPHER_find(sk, NULL, c)) {
  820. /* we did not say we would use this cipher */
  821. al = SSL_AD_ILLEGAL_PARAMETER;
  822. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED);
  823. goto f_err;
  824. }
  825. if (ssl->session != NULL) {
  826. if (ssl->session->cipher != c) {
  827. al = SSL_AD_ILLEGAL_PARAMETER;
  828. OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
  829. goto f_err;
  830. }
  831. if (ssl->session->ssl_version != ssl->version) {
  832. al = SSL_AD_ILLEGAL_PARAMETER;
  833. OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED);
  834. goto f_err;
  835. }
  836. if (!ssl_session_is_context_valid(ssl, ssl->session)) {
  837. /* This is actually a client application bug. */
  838. al = SSL_AD_ILLEGAL_PARAMETER;
  839. OPENSSL_PUT_ERROR(SSL,
  840. SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
  841. goto f_err;
  842. }
  843. } else {
  844. ssl->s3->new_session->cipher = c;
  845. }
  846. ssl->s3->tmp.new_cipher = c;
  847. if (ssl_cipher_uses_certificate_auth(c)) {
  848. ssl->s3->hs->use_cert_auth = 1;
  849. }
  850. /* Now that the cipher is known, initialize the handshake hash. */
  851. if (!ssl3_init_handshake_hash(ssl)) {
  852. goto f_err;
  853. }
  854. /* If doing a full handshake, the server may request a client certificate
  855. * which requires hashing the handshake transcript. Otherwise, the handshake
  856. * buffer may be released. */
  857. if (ssl->session != NULL || !ssl->s3->hs->use_cert_auth) {
  858. ssl3_free_handshake_buffer(ssl);
  859. }
  860. /* Only the NULL compression algorithm is supported. */
  861. if (compression_method != 0) {
  862. al = SSL_AD_ILLEGAL_PARAMETER;
  863. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  864. goto f_err;
  865. }
  866. /* TLS extensions */
  867. if (!ssl_parse_serverhello_tlsext(ssl, &server_hello)) {
  868. OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
  869. goto err;
  870. }
  871. /* There should be nothing left over in the record. */
  872. if (CBS_len(&server_hello) != 0) {
  873. /* wrong packet length */
  874. al = SSL_AD_DECODE_ERROR;
  875. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
  876. goto f_err;
  877. }
  878. if (ssl->session != NULL &&
  879. ssl->s3->tmp.extended_master_secret !=
  880. ssl->session->extended_master_secret) {
  881. al = SSL_AD_HANDSHAKE_FAILURE;
  882. if (ssl->session->extended_master_secret) {
  883. OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
  884. } else {
  885. OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_NON_EMS_SESSION_WITH_EMS_EXTENSION);
  886. }
  887. goto f_err;
  888. }
  889. return 1;
  890. f_err:
  891. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  892. err:
  893. return -1;
  894. }
  895. static int ssl3_get_server_certificate(SSL *ssl) {
  896. int ret =
  897. ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE, ssl_hash_message);
  898. if (ret <= 0) {
  899. return ret;
  900. }
  901. CBS cbs;
  902. CBS_init(&cbs, ssl->init_msg, ssl->init_num);
  903. uint8_t alert;
  904. STACK_OF(X509) *chain = ssl_parse_cert_chain(ssl, &alert, NULL, &cbs);
  905. if (chain == NULL) {
  906. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  907. goto err;
  908. }
  909. if (sk_X509_num(chain) == 0 || CBS_len(&cbs) != 0) {
  910. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  911. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  912. goto err;
  913. }
  914. X509 *leaf = sk_X509_value(chain, 0);
  915. if (!ssl_check_leaf_certificate(ssl, leaf)) {
  916. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
  917. goto err;
  918. }
  919. /* NOTE: Unlike the server half, the client's copy of |cert_chain| includes
  920. * the leaf. */
  921. sk_X509_pop_free(ssl->s3->new_session->cert_chain, X509_free);
  922. ssl->s3->new_session->cert_chain = chain;
  923. X509_free(ssl->s3->new_session->peer);
  924. X509_up_ref(leaf);
  925. ssl->s3->new_session->peer = leaf;
  926. return 1;
  927. err:
  928. sk_X509_pop_free(chain, X509_free);
  929. return -1;
  930. }
  931. static int ssl3_get_cert_status(SSL *ssl) {
  932. int al;
  933. CBS certificate_status, ocsp_response;
  934. uint8_t status_type;
  935. int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
  936. if (ret <= 0) {
  937. return ret;
  938. }
  939. if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) {
  940. /* A server may send status_request in ServerHello and then change
  941. * its mind about sending CertificateStatus. */
  942. ssl->s3->tmp.reuse_message = 1;
  943. return 1;
  944. }
  945. CBS_init(&certificate_status, ssl->init_msg, ssl->init_num);
  946. if (!CBS_get_u8(&certificate_status, &status_type) ||
  947. status_type != TLSEXT_STATUSTYPE_ocsp ||
  948. !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
  949. CBS_len(&ocsp_response) == 0 ||
  950. CBS_len(&certificate_status) != 0) {
  951. al = SSL_AD_DECODE_ERROR;
  952. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  953. goto f_err;
  954. }
  955. if (!CBS_stow(&ocsp_response, &ssl->s3->new_session->ocsp_response,
  956. &ssl->s3->new_session->ocsp_response_length)) {
  957. al = SSL_AD_INTERNAL_ERROR;
  958. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  959. goto f_err;
  960. }
  961. return 1;
  962. f_err:
  963. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  964. return -1;
  965. }
  966. static int ssl3_verify_server_cert(SSL *ssl) {
  967. if (!ssl_verify_cert_chain(ssl, &ssl->s3->new_session->verify_result,
  968. ssl->s3->new_session->cert_chain)) {
  969. return -1;
  970. }
  971. return 1;
  972. }
  973. static int ssl3_get_server_key_exchange(SSL *ssl) {
  974. int al;
  975. EVP_PKEY *pkey = NULL;
  976. DH *dh = NULL;
  977. EC_KEY *ecdh = NULL;
  978. EC_POINT *srvr_ecpoint = NULL;
  979. int ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
  980. if (ret <= 0) {
  981. return ret;
  982. }
  983. if (ssl->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
  984. if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher)) {
  985. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
  986. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  987. return -1;
  988. }
  989. /* In plain PSK ciphersuite, ServerKeyExchange may be omitted to send no
  990. * identity hint. */
  991. if (ssl->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) {
  992. /* TODO(davidben): This should be reset in one place with the rest of the
  993. * handshake state. */
  994. OPENSSL_free(ssl->s3->tmp.peer_psk_identity_hint);
  995. ssl->s3->tmp.peer_psk_identity_hint = NULL;
  996. }
  997. ssl->s3->tmp.reuse_message = 1;
  998. return 1;
  999. }
  1000. /* Retain a copy of the original CBS to compute the signature over. */
  1001. CBS server_key_exchange;
  1002. CBS_init(&server_key_exchange, ssl->init_msg, ssl->init_num);
  1003. CBS server_key_exchange_orig = server_key_exchange;
  1004. uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
  1005. uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
  1006. if (alg_a & SSL_aPSK) {
  1007. CBS psk_identity_hint;
  1008. /* Each of the PSK key exchanges begins with a psk_identity_hint. */
  1009. if (!CBS_get_u16_length_prefixed(&server_key_exchange,
  1010. &psk_identity_hint)) {
  1011. al = SSL_AD_DECODE_ERROR;
  1012. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1013. goto f_err;
  1014. }
  1015. /* Store PSK identity hint for later use, hint is used in
  1016. * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
  1017. * identity hint can be as long as the maximum length of a PSK identity.
  1018. * Also do not allow NULL characters; identities are saved as C strings.
  1019. *
  1020. * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
  1021. * a specific identity. */
  1022. if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
  1023. CBS_contains_zero_byte(&psk_identity_hint)) {
  1024. al = SSL_AD_HANDSHAKE_FAILURE;
  1025. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  1026. goto f_err;
  1027. }
  1028. /* Save the identity hint as a C string. */
  1029. if (!CBS_strdup(&psk_identity_hint, &ssl->s3->tmp.peer_psk_identity_hint)) {
  1030. al = SSL_AD_INTERNAL_ERROR;
  1031. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1032. goto f_err;
  1033. }
  1034. }
  1035. if (alg_k & SSL_kDHE) {
  1036. CBS dh_p, dh_g, dh_Ys;
  1037. if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
  1038. CBS_len(&dh_p) == 0 ||
  1039. !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
  1040. CBS_len(&dh_g) == 0 ||
  1041. !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
  1042. CBS_len(&dh_Ys) == 0) {
  1043. al = SSL_AD_DECODE_ERROR;
  1044. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1045. goto f_err;
  1046. }
  1047. dh = DH_new();
  1048. if (dh == NULL) {
  1049. goto err;
  1050. }
  1051. dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL);
  1052. dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL);
  1053. if (dh->p == NULL || dh->g == NULL) {
  1054. goto err;
  1055. }
  1056. ssl->s3->new_session->key_exchange_info = DH_num_bits(dh);
  1057. if (ssl->s3->new_session->key_exchange_info < 1024) {
  1058. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_DH_P_LENGTH);
  1059. goto err;
  1060. } else if (ssl->s3->new_session->key_exchange_info > 4096) {
  1061. /* Overly large DHE groups are prohibitively expensive, so enforce a limit
  1062. * to prevent a server from causing us to perform too expensive of a
  1063. * computation. */
  1064. OPENSSL_PUT_ERROR(SSL, SSL_R_DH_P_TOO_LONG);
  1065. goto err;
  1066. }
  1067. SSL_ECDH_CTX_init_for_dhe(&ssl->s3->tmp.ecdh_ctx, dh);
  1068. dh = NULL;
  1069. /* Save the peer public key for later. */
  1070. size_t peer_key_len;
  1071. if (!CBS_stow(&dh_Ys, &ssl->s3->tmp.peer_key, &peer_key_len)) {
  1072. goto err;
  1073. }
  1074. /* |dh_Ys| was initialized with CBS_get_u16_length_prefixed, so peer_key_len
  1075. * fits in a uint16_t. */
  1076. assert(sizeof(ssl->s3->tmp.peer_key_len) == 2 && peer_key_len <= 0xffff);
  1077. ssl->s3->tmp.peer_key_len = (uint16_t)peer_key_len;
  1078. } else if (alg_k & SSL_kECDHE) {
  1079. /* Parse the server parameters. */
  1080. uint8_t group_type;
  1081. uint16_t group_id;
  1082. CBS point;
  1083. if (!CBS_get_u8(&server_key_exchange, &group_type) ||
  1084. group_type != NAMED_CURVE_TYPE ||
  1085. !CBS_get_u16(&server_key_exchange, &group_id) ||
  1086. !CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
  1087. al = SSL_AD_DECODE_ERROR;
  1088. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1089. goto f_err;
  1090. }
  1091. ssl->s3->new_session->key_exchange_info = group_id;
  1092. /* Ensure the group is consistent with preferences. */
  1093. if (!tls1_check_group_id(ssl, group_id)) {
  1094. al = SSL_AD_ILLEGAL_PARAMETER;
  1095. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
  1096. goto f_err;
  1097. }
  1098. /* Initialize ECDH and save the peer public key for later. */
  1099. size_t peer_key_len;
  1100. if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, group_id) ||
  1101. !CBS_stow(&point, &ssl->s3->tmp.peer_key, &peer_key_len)) {
  1102. goto err;
  1103. }
  1104. /* |point| was initialized with CBS_get_u8_length_prefixed, so peer_key_len
  1105. * fits in a uint16_t. */
  1106. assert(sizeof(ssl->s3->tmp.peer_key_len) == 2 && peer_key_len <= 0xffff);
  1107. ssl->s3->tmp.peer_key_len = (uint16_t)peer_key_len;
  1108. } else if (alg_k & SSL_kCECPQ1) {
  1109. SSL_ECDH_CTX_init_for_cecpq1(&ssl->s3->tmp.ecdh_ctx);
  1110. CBS key;
  1111. if (!CBS_get_u16_length_prefixed(&server_key_exchange, &key)) {
  1112. al = SSL_AD_DECODE_ERROR;
  1113. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1114. goto f_err;
  1115. }
  1116. size_t peer_key_len;
  1117. if (!CBS_stow(&key, &ssl->s3->tmp.peer_key, &peer_key_len)) {
  1118. goto err;
  1119. }
  1120. /* |key| was initialized with CBS_get_u16_length_prefixed, so peer_key_len
  1121. * fits in a uint16_t. */
  1122. assert(sizeof(ssl->s3->tmp.peer_key_len) == 2 && peer_key_len <= 0xffff);
  1123. ssl->s3->tmp.peer_key_len = (uint16_t)peer_key_len;
  1124. } else if (!(alg_k & SSL_kPSK)) {
  1125. al = SSL_AD_UNEXPECTED_MESSAGE;
  1126. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
  1127. goto f_err;
  1128. }
  1129. /* At this point, |server_key_exchange| contains the signature, if any, while
  1130. * |server_key_exchange_orig| contains the entire message. From that, derive
  1131. * a CBS containing just the parameter. */
  1132. CBS parameter;
  1133. CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
  1134. CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
  1135. /* ServerKeyExchange should be signed by the server's public key. */
  1136. if (ssl->s3->hs->use_cert_auth) {
  1137. pkey = X509_get_pubkey(ssl->s3->new_session->peer);
  1138. if (pkey == NULL) {
  1139. goto err;
  1140. }
  1141. uint16_t signature_algorithm = 0;
  1142. if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
  1143. if (!CBS_get_u16(&server_key_exchange, &signature_algorithm)) {
  1144. al = SSL_AD_DECODE_ERROR;
  1145. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1146. goto f_err;
  1147. }
  1148. if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) {
  1149. goto f_err;
  1150. }
  1151. ssl->s3->tmp.peer_signature_algorithm = signature_algorithm;
  1152. } else if (pkey->type == EVP_PKEY_RSA) {
  1153. signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
  1154. } else if (pkey->type == EVP_PKEY_EC) {
  1155. signature_algorithm = SSL_SIGN_ECDSA_SHA1;
  1156. } else {
  1157. al = SSL_AD_UNSUPPORTED_CERTIFICATE;
  1158. OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
  1159. goto f_err;
  1160. }
  1161. /* The last field in |server_key_exchange| is the signature. */
  1162. CBS signature;
  1163. if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
  1164. CBS_len(&server_key_exchange) != 0) {
  1165. al = SSL_AD_DECODE_ERROR;
  1166. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1167. goto f_err;
  1168. }
  1169. CBB transcript;
  1170. uint8_t *transcript_data;
  1171. size_t transcript_len;
  1172. if (!CBB_init(&transcript, 2*SSL3_RANDOM_SIZE + CBS_len(&parameter)) ||
  1173. !CBB_add_bytes(&transcript, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
  1174. !CBB_add_bytes(&transcript, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
  1175. !CBB_add_bytes(&transcript, CBS_data(&parameter), CBS_len(&parameter)) ||
  1176. !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
  1177. CBB_cleanup(&transcript);
  1178. al = SSL_AD_INTERNAL_ERROR;
  1179. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1180. goto f_err;
  1181. }
  1182. int sig_ok = ssl_public_key_verify(
  1183. ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
  1184. pkey, transcript_data, transcript_len);
  1185. OPENSSL_free(transcript_data);
  1186. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  1187. sig_ok = 1;
  1188. ERR_clear_error();
  1189. #endif
  1190. if (!sig_ok) {
  1191. /* bad signature */
  1192. al = SSL_AD_DECRYPT_ERROR;
  1193. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
  1194. goto f_err;
  1195. }
  1196. } else {
  1197. /* PSK ciphers are the only supported certificate-less ciphers. */
  1198. assert(alg_a == SSL_aPSK);
  1199. if (CBS_len(&server_key_exchange) > 0) {
  1200. al = SSL_AD_DECODE_ERROR;
  1201. OPENSSL_PUT_ERROR(SSL, SSL_R_EXTRA_DATA_IN_MESSAGE);
  1202. goto f_err;
  1203. }
  1204. }
  1205. EVP_PKEY_free(pkey);
  1206. return 1;
  1207. f_err:
  1208. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  1209. err:
  1210. EVP_PKEY_free(pkey);
  1211. DH_free(dh);
  1212. EC_POINT_free(srvr_ecpoint);
  1213. EC_KEY_free(ecdh);
  1214. return -1;
  1215. }
  1216. static int ssl3_get_certificate_request(SSL *ssl) {
  1217. int msg_ret = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message);
  1218. if (msg_ret <= 0) {
  1219. return msg_ret;
  1220. }
  1221. ssl->s3->tmp.cert_request = 0;
  1222. if (ssl->s3->tmp.message_type == SSL3_MT_SERVER_HELLO_DONE) {
  1223. ssl->s3->tmp.reuse_message = 1;
  1224. /* If we get here we don't need the handshake buffer as we won't be doing
  1225. * client auth. */
  1226. ssl3_free_handshake_buffer(ssl);
  1227. return 1;
  1228. }
  1229. if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
  1230. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  1231. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
  1232. return -1;
  1233. }
  1234. CBS cbs;
  1235. CBS_init(&cbs, ssl->init_msg, ssl->init_num);
  1236. /* Get the certificate types. */
  1237. CBS certificate_types;
  1238. if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
  1239. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1240. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1241. return -1;
  1242. }
  1243. if (!CBS_stow(&certificate_types, &ssl->s3->tmp.certificate_types,
  1244. &ssl->s3->tmp.num_certificate_types)) {
  1245. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1246. return -1;
  1247. }
  1248. if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
  1249. CBS supported_signature_algorithms;
  1250. if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms) ||
  1251. !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
  1252. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1253. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1254. return -1;
  1255. }
  1256. }
  1257. uint8_t alert;
  1258. STACK_OF(X509_NAME) *ca_sk = ssl_parse_client_CA_list(ssl, &alert, &cbs);
  1259. if (ca_sk == NULL) {
  1260. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  1261. return -1;
  1262. }
  1263. ssl->s3->tmp.cert_request = 1;
  1264. sk_X509_NAME_pop_free(ssl->s3->tmp.ca_names, X509_NAME_free);
  1265. ssl->s3->tmp.ca_names = ca_sk;
  1266. return 1;
  1267. }
  1268. static int ssl3_get_server_hello_done(SSL *ssl) {
  1269. int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_SERVER_HELLO_DONE,
  1270. ssl_hash_message);
  1271. if (ret <= 0) {
  1272. return ret;
  1273. }
  1274. /* ServerHelloDone is empty. */
  1275. if (ssl->init_num > 0) {
  1276. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1277. OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
  1278. return -1;
  1279. }
  1280. return 1;
  1281. }
  1282. static int ssl3_send_client_certificate(SSL *ssl) {
  1283. if (ssl->state == SSL3_ST_CW_CERT_A) {
  1284. /* Call cert_cb to update the certificate. */
  1285. if (ssl->cert->cert_cb) {
  1286. int ret = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
  1287. if (ret < 0) {
  1288. ssl->rwstate = SSL_X509_LOOKUP;
  1289. return -1;
  1290. }
  1291. if (ret == 0) {
  1292. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1293. return -1;
  1294. }
  1295. }
  1296. ssl->state = SSL3_ST_CW_CERT_B;
  1297. }
  1298. if (ssl->state == SSL3_ST_CW_CERT_B) {
  1299. /* Call client_cert_cb to update the certificate. */
  1300. int should_retry;
  1301. if (!ssl_do_client_cert_cb(ssl, &should_retry)) {
  1302. if (should_retry) {
  1303. ssl->rwstate = SSL_X509_LOOKUP;
  1304. }
  1305. return -1;
  1306. }
  1307. if (!ssl_has_certificate(ssl)) {
  1308. ssl->s3->tmp.cert_request = 0;
  1309. /* Without a client certificate, the handshake buffer may be released. */
  1310. ssl3_free_handshake_buffer(ssl);
  1311. if (ssl->version == SSL3_VERSION) {
  1312. /* In SSL 3.0, send no certificate by skipping both messages. */
  1313. ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
  1314. return 1;
  1315. }
  1316. }
  1317. if (!ssl3_output_cert_chain(ssl)) {
  1318. return -1;
  1319. }
  1320. ssl->state = SSL3_ST_CW_CERT_C;
  1321. }
  1322. assert(ssl->state == SSL3_ST_CW_CERT_C);
  1323. return ssl->method->write_message(ssl);
  1324. }
  1325. OPENSSL_COMPILE_ASSERT(sizeof(size_t) >= sizeof(unsigned),
  1326. SIZE_T_IS_SMALLER_THAN_UNSIGNED);
  1327. static int ssl3_send_client_key_exchange(SSL *ssl) {
  1328. if (ssl->state == SSL3_ST_CW_KEY_EXCH_B) {
  1329. return ssl->method->write_message(ssl);
  1330. }
  1331. assert(ssl->state == SSL3_ST_CW_KEY_EXCH_A);
  1332. uint8_t *pms = NULL;
  1333. size_t pms_len = 0;
  1334. CBB cbb, body;
  1335. if (!ssl->method->init_message(ssl, &cbb, &body,
  1336. SSL3_MT_CLIENT_KEY_EXCHANGE)) {
  1337. goto err;
  1338. }
  1339. uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
  1340. uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
  1341. /* If using a PSK key exchange, prepare the pre-shared key. */
  1342. unsigned psk_len = 0;
  1343. uint8_t psk[PSK_MAX_PSK_LEN];
  1344. if (alg_a & SSL_aPSK) {
  1345. if (ssl->psk_client_callback == NULL) {
  1346. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_CLIENT_CB);
  1347. goto err;
  1348. }
  1349. char identity[PSK_MAX_IDENTITY_LEN + 1];
  1350. memset(identity, 0, sizeof(identity));
  1351. psk_len = ssl->psk_client_callback(
  1352. ssl, ssl->s3->tmp.peer_psk_identity_hint, identity, sizeof(identity),
  1353. psk, sizeof(psk));
  1354. if (psk_len == 0) {
  1355. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
  1356. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1357. goto err;
  1358. }
  1359. assert(psk_len <= PSK_MAX_PSK_LEN);
  1360. OPENSSL_free(ssl->s3->new_session->psk_identity);
  1361. ssl->s3->new_session->psk_identity = BUF_strdup(identity);
  1362. if (ssl->s3->new_session->psk_identity == NULL) {
  1363. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1364. goto err;
  1365. }
  1366. /* Write out psk_identity. */
  1367. CBB child;
  1368. if (!CBB_add_u16_length_prefixed(&body, &child) ||
  1369. !CBB_add_bytes(&child, (const uint8_t *)identity,
  1370. OPENSSL_strnlen(identity, sizeof(identity))) ||
  1371. !CBB_flush(&body)) {
  1372. goto err;
  1373. }
  1374. }
  1375. /* Depending on the key exchange method, compute |pms| and |pms_len|. */
  1376. if (alg_k & SSL_kRSA) {
  1377. pms_len = SSL_MAX_MASTER_KEY_LENGTH;
  1378. pms = OPENSSL_malloc(pms_len);
  1379. if (pms == NULL) {
  1380. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1381. goto err;
  1382. }
  1383. EVP_PKEY *pkey = X509_get_pubkey(ssl->s3->new_session->peer);
  1384. if (pkey == NULL) {
  1385. goto err;
  1386. }
  1387. RSA *rsa = EVP_PKEY_get0_RSA(pkey);
  1388. if (rsa == NULL) {
  1389. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1390. EVP_PKEY_free(pkey);
  1391. goto err;
  1392. }
  1393. EVP_PKEY_free(pkey);
  1394. pms[0] = ssl->client_version >> 8;
  1395. pms[1] = ssl->client_version & 0xff;
  1396. if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
  1397. goto err;
  1398. }
  1399. CBB child, *enc_pms = &body;
  1400. size_t enc_pms_len;
  1401. /* In TLS, there is a length prefix. */
  1402. if (ssl->version > SSL3_VERSION) {
  1403. if (!CBB_add_u16_length_prefixed(&body, &child)) {
  1404. goto err;
  1405. }
  1406. enc_pms = &child;
  1407. }
  1408. uint8_t *ptr;
  1409. if (!CBB_reserve(enc_pms, &ptr, RSA_size(rsa)) ||
  1410. !RSA_encrypt(rsa, &enc_pms_len, ptr, RSA_size(rsa), pms, pms_len,
  1411. RSA_PKCS1_PADDING) ||
  1412. /* Log the premaster secret, if logging is enabled. */
  1413. !ssl_log_rsa_client_key_exchange(ssl, ptr, enc_pms_len, pms, pms_len) ||
  1414. !CBB_did_write(enc_pms, enc_pms_len) ||
  1415. !CBB_flush(&body)) {
  1416. goto err;
  1417. }
  1418. } else if (alg_k & (SSL_kECDHE|SSL_kDHE|SSL_kCECPQ1)) {
  1419. /* Generate a keypair and serialize the public half. */
  1420. CBB child;
  1421. if (!SSL_ECDH_CTX_add_key(&ssl->s3->tmp.ecdh_ctx, &body, &child)) {
  1422. goto err;
  1423. }
  1424. /* Compute the premaster. */
  1425. uint8_t alert;
  1426. if (!SSL_ECDH_CTX_accept(&ssl->s3->tmp.ecdh_ctx, &child, &pms, &pms_len,
  1427. &alert, ssl->s3->tmp.peer_key,
  1428. ssl->s3->tmp.peer_key_len)) {
  1429. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  1430. goto err;
  1431. }
  1432. if (!CBB_flush(&body)) {
  1433. goto err;
  1434. }
  1435. /* The key exchange state may now be discarded. */
  1436. SSL_ECDH_CTX_cleanup(&ssl->s3->tmp.ecdh_ctx);
  1437. OPENSSL_free(ssl->s3->tmp.peer_key);
  1438. ssl->s3->tmp.peer_key = NULL;
  1439. } else if (alg_k & SSL_kPSK) {
  1440. /* For plain PSK, other_secret is a block of 0s with the same length as
  1441. * the pre-shared key. */
  1442. pms_len = psk_len;
  1443. pms = OPENSSL_malloc(pms_len);
  1444. if (pms == NULL) {
  1445. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1446. goto err;
  1447. }
  1448. memset(pms, 0, pms_len);
  1449. } else {
  1450. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1451. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1452. goto err;
  1453. }
  1454. /* For a PSK cipher suite, other_secret is combined with the pre-shared
  1455. * key. */
  1456. if (alg_a & SSL_aPSK) {
  1457. CBB pms_cbb, child;
  1458. uint8_t *new_pms;
  1459. size_t new_pms_len;
  1460. CBB_zero(&pms_cbb);
  1461. if (!CBB_init(&pms_cbb, 2 + psk_len + 2 + pms_len) ||
  1462. !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
  1463. !CBB_add_bytes(&child, pms, pms_len) ||
  1464. !CBB_add_u16_length_prefixed(&pms_cbb, &child) ||
  1465. !CBB_add_bytes(&child, psk, psk_len) ||
  1466. !CBB_finish(&pms_cbb, &new_pms, &new_pms_len)) {
  1467. CBB_cleanup(&pms_cbb);
  1468. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1469. goto err;
  1470. }
  1471. OPENSSL_cleanse(pms, pms_len);
  1472. OPENSSL_free(pms);
  1473. pms = new_pms;
  1474. pms_len = new_pms_len;
  1475. }
  1476. /* The message must be added to the finished hash before calculating the
  1477. * master secret. */
  1478. if (!ssl->method->finish_message(ssl, &cbb)) {
  1479. goto err;
  1480. }
  1481. ssl->state = SSL3_ST_CW_KEY_EXCH_B;
  1482. ssl->s3->new_session->master_key_length =
  1483. tls1_generate_master_secret(ssl, ssl->s3->new_session->master_key, pms,
  1484. pms_len);
  1485. if (ssl->s3->new_session->master_key_length == 0) {
  1486. goto err;
  1487. }
  1488. ssl->s3->new_session->extended_master_secret =
  1489. ssl->s3->tmp.extended_master_secret;
  1490. OPENSSL_cleanse(pms, pms_len);
  1491. OPENSSL_free(pms);
  1492. return ssl->method->write_message(ssl);
  1493. err:
  1494. CBB_cleanup(&cbb);
  1495. if (pms != NULL) {
  1496. OPENSSL_cleanse(pms, pms_len);
  1497. OPENSSL_free(pms);
  1498. }
  1499. return -1;
  1500. }
  1501. static int ssl3_send_cert_verify(SSL *ssl) {
  1502. if (ssl->state == SSL3_ST_CW_CERT_VRFY_C) {
  1503. return ssl->method->write_message(ssl);
  1504. }
  1505. assert(ssl_has_private_key(ssl));
  1506. CBB cbb, body, child;
  1507. if (!ssl->method->init_message(ssl, &cbb, &body,
  1508. SSL3_MT_CERTIFICATE_VERIFY)) {
  1509. goto err;
  1510. }
  1511. uint16_t signature_algorithm;
  1512. if (!tls1_choose_signature_algorithm(ssl, &signature_algorithm)) {
  1513. goto err;
  1514. }
  1515. if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
  1516. /* Write out the digest type in TLS 1.2. */
  1517. if (!CBB_add_u16(&body, signature_algorithm)) {
  1518. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1519. goto err;
  1520. }
  1521. }
  1522. /* Set aside space for the signature. */
  1523. const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
  1524. uint8_t *ptr;
  1525. if (!CBB_add_u16_length_prefixed(&body, &child) ||
  1526. !CBB_reserve(&child, &ptr, max_sig_len)) {
  1527. goto err;
  1528. }
  1529. size_t sig_len = max_sig_len;
  1530. enum ssl_private_key_result_t sign_result;
  1531. if (ssl->state == SSL3_ST_CW_CERT_VRFY_A) {
  1532. /* The SSL3 construction for CertificateVerify does not decompose into a
  1533. * single final digest and signature, and must be special-cased. */
  1534. if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
  1535. if (ssl->cert->key_method != NULL) {
  1536. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
  1537. goto err;
  1538. }
  1539. const EVP_MD *md;
  1540. uint8_t digest[EVP_MAX_MD_SIZE];
  1541. size_t digest_len;
  1542. if (!ssl3_cert_verify_hash(ssl, &md, digest, &digest_len,
  1543. signature_algorithm)) {
  1544. goto err;
  1545. }
  1546. sign_result = ssl_private_key_success;
  1547. EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL);
  1548. if (pctx == NULL ||
  1549. !EVP_PKEY_sign_init(pctx) ||
  1550. !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
  1551. !EVP_PKEY_sign(pctx, ptr, &sig_len, digest, digest_len)) {
  1552. EVP_PKEY_CTX_free(pctx);
  1553. sign_result = ssl_private_key_failure;
  1554. goto err;
  1555. }
  1556. EVP_PKEY_CTX_free(pctx);
  1557. } else {
  1558. sign_result = ssl_private_key_sign(
  1559. ssl, ptr, &sig_len, max_sig_len, signature_algorithm,
  1560. (const uint8_t *)ssl->s3->handshake_buffer->data,
  1561. ssl->s3->handshake_buffer->length);
  1562. }
  1563. /* The handshake buffer is no longer necessary. */
  1564. ssl3_free_handshake_buffer(ssl);
  1565. } else {
  1566. assert(ssl->state == SSL3_ST_CW_CERT_VRFY_B);
  1567. sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len);
  1568. }
  1569. switch (sign_result) {
  1570. case ssl_private_key_success:
  1571. break;
  1572. case ssl_private_key_failure:
  1573. goto err;
  1574. case ssl_private_key_retry:
  1575. ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
  1576. ssl->state = SSL3_ST_CW_CERT_VRFY_B;
  1577. goto err;
  1578. }
  1579. if (!CBB_did_write(&child, sig_len) ||
  1580. !ssl->method->finish_message(ssl, &cbb)) {
  1581. goto err;
  1582. }
  1583. ssl->state = SSL3_ST_CW_CERT_VRFY_C;
  1584. return ssl->method->write_message(ssl);
  1585. err:
  1586. CBB_cleanup(&cbb);
  1587. return -1;
  1588. }
  1589. static int ssl3_send_next_proto(SSL *ssl) {
  1590. if (ssl->state == SSL3_ST_CW_NEXT_PROTO_B) {
  1591. return ssl->method->write_message(ssl);
  1592. }
  1593. assert(ssl->state == SSL3_ST_CW_NEXT_PROTO_A);
  1594. static const uint8_t kZero[32] = {0};
  1595. size_t padding_len = 32 - ((ssl->s3->next_proto_negotiated_len + 2) % 32);
  1596. CBB cbb, body, child;
  1597. if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEXT_PROTO) ||
  1598. !CBB_add_u8_length_prefixed(&body, &child) ||
  1599. !CBB_add_bytes(&child, ssl->s3->next_proto_negotiated,
  1600. ssl->s3->next_proto_negotiated_len) ||
  1601. !CBB_add_u8_length_prefixed(&body, &child) ||
  1602. !CBB_add_bytes(&child, kZero, padding_len) ||
  1603. !ssl->method->finish_message(ssl, &cbb)) {
  1604. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1605. CBB_cleanup(&cbb);
  1606. return -1;
  1607. }
  1608. ssl->state = SSL3_ST_CW_NEXT_PROTO_B;
  1609. return ssl->method->write_message(ssl);
  1610. }
  1611. static int ssl3_send_channel_id(SSL *ssl) {
  1612. if (ssl->state == SSL3_ST_CW_CHANNEL_ID_B) {
  1613. return ssl->method->write_message(ssl);
  1614. }
  1615. assert(ssl->state == SSL3_ST_CW_CHANNEL_ID_A);
  1616. if (ssl->tlsext_channel_id_private == NULL &&
  1617. ssl->ctx->channel_id_cb != NULL) {
  1618. EVP_PKEY *key = NULL;
  1619. ssl->ctx->channel_id_cb(ssl, &key);
  1620. if (key != NULL &&
  1621. !SSL_set1_tls_channel_id(ssl, key)) {
  1622. EVP_PKEY_free(key);
  1623. return -1;
  1624. }
  1625. EVP_PKEY_free(key);
  1626. }
  1627. if (ssl->tlsext_channel_id_private == NULL) {
  1628. ssl->rwstate = SSL_CHANNEL_ID_LOOKUP;
  1629. return -1;
  1630. }
  1631. EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
  1632. if (ec_key == NULL) {
  1633. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1634. return -1;
  1635. }
  1636. int ret = -1;
  1637. BIGNUM *x = BN_new();
  1638. BIGNUM *y = BN_new();
  1639. ECDSA_SIG *sig = NULL;
  1640. if (x == NULL || y == NULL ||
  1641. !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
  1642. EC_KEY_get0_public_key(ec_key),
  1643. x, y, NULL)) {
  1644. goto err;
  1645. }
  1646. uint8_t digest[EVP_MAX_MD_SIZE];
  1647. size_t digest_len;
  1648. if (!tls1_channel_id_hash(ssl, digest, &digest_len)) {
  1649. goto err;
  1650. }
  1651. sig = ECDSA_do_sign(digest, digest_len, ec_key);
  1652. if (sig == NULL) {
  1653. goto err;
  1654. }
  1655. CBB cbb, body, child;
  1656. if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CHANNEL_ID) ||
  1657. !CBB_add_u16(&body, TLSEXT_TYPE_channel_id) ||
  1658. !CBB_add_u16_length_prefixed(&body, &child) ||
  1659. !BN_bn2cbb_padded(&child, 32, x) || !BN_bn2cbb_padded(&child, 32, y) ||
  1660. !BN_bn2cbb_padded(&child, 32, sig->r) ||
  1661. !BN_bn2cbb_padded(&child, 32, sig->s) ||
  1662. !ssl->method->finish_message(ssl, &cbb)) {
  1663. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1664. CBB_cleanup(&cbb);
  1665. goto err;
  1666. }
  1667. ssl->state = SSL3_ST_CW_CHANNEL_ID_B;
  1668. ret = ssl->method->write_message(ssl);
  1669. err:
  1670. BN_free(x);
  1671. BN_free(y);
  1672. ECDSA_SIG_free(sig);
  1673. return ret;
  1674. }
  1675. static int ssl3_get_new_session_ticket(SSL *ssl) {
  1676. int ret = ssl->method->ssl_get_message(ssl, SSL3_MT_NEW_SESSION_TICKET,
  1677. ssl_hash_message);
  1678. if (ret <= 0) {
  1679. return ret;
  1680. }
  1681. CBS new_session_ticket, ticket;
  1682. uint32_t tlsext_tick_lifetime_hint;
  1683. CBS_init(&new_session_ticket, ssl->init_msg, ssl->init_num);
  1684. if (!CBS_get_u32(&new_session_ticket, &tlsext_tick_lifetime_hint) ||
  1685. !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
  1686. CBS_len(&new_session_ticket) != 0) {
  1687. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1688. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1689. return -1;
  1690. }
  1691. if (CBS_len(&ticket) == 0) {
  1692. /* RFC 5077 allows a server to change its mind and send no ticket after
  1693. * negotiating the extension. The value of |tlsext_ticket_expected| is
  1694. * checked in |ssl_update_cache| so is cleared here to avoid an unnecessary
  1695. * update. */
  1696. ssl->tlsext_ticket_expected = 0;
  1697. return 1;
  1698. }
  1699. int session_renewed = ssl->session != NULL;
  1700. SSL_SESSION *session = ssl->s3->new_session;
  1701. if (session_renewed) {
  1702. /* The server is sending a new ticket for an existing session. Sessions are
  1703. * immutable once established, so duplicate all but the ticket of the
  1704. * existing session. */
  1705. session = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH);
  1706. if (session == NULL) {
  1707. /* This should never happen. */
  1708. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1709. goto err;
  1710. }
  1711. }
  1712. if (!CBS_stow(&ticket, &session->tlsext_tick, &session->tlsext_ticklen)) {
  1713. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1714. goto err;
  1715. }
  1716. session->tlsext_tick_lifetime_hint = tlsext_tick_lifetime_hint;
  1717. /* Generate a session ID for this session based on the session ticket. We use
  1718. * the session ID mechanism for detecting ticket resumption. This also fits in
  1719. * with assumptions elsewhere in OpenSSL.*/
  1720. if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
  1721. session->session_id, &session->session_id_length,
  1722. EVP_sha256(), NULL)) {
  1723. goto err;
  1724. }
  1725. if (session_renewed) {
  1726. session->not_resumable = 0;
  1727. SSL_SESSION_free(ssl->session);
  1728. ssl->session = session;
  1729. }
  1730. return 1;
  1731. err:
  1732. if (session_renewed) {
  1733. SSL_SESSION_free(session);
  1734. }
  1735. return -1;
  1736. }