Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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