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.
 
 
 
 
 
 

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