選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

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