Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

2438 rindas
74 KiB

  1. /* ssl/s3_clnt.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * openssl-core@openssl.org.
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * (eay@cryptsoft.com). This product includes software written by Tim
  108. * Hudson (tjh@cryptsoft.com).
  109. *
  110. */
  111. /* ====================================================================
  112. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  113. *
  114. * Portions of the attached software ("Contribution") are developed by
  115. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  116. *
  117. * The Contribution is licensed pursuant to the OpenSSL open source
  118. * license provided above.
  119. *
  120. * ECC cipher suite support in OpenSSL originally written by
  121. * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
  122. *
  123. */
  124. /* ====================================================================
  125. * Copyright 2005 Nokia. All rights reserved.
  126. *
  127. * The portions of the attached software ("Contribution") is developed by
  128. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  129. * license.
  130. *
  131. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  132. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  133. * support (see RFC 4279) to OpenSSL.
  134. *
  135. * No patent licenses or other rights except those expressly stated in
  136. * the OpenSSL open source license shall be deemed granted or received
  137. * expressly, by implication, estoppel, or otherwise.
  138. *
  139. * No assurances are provided by Nokia that the Contribution does not
  140. * infringe the patent or other intellectual property rights of any third
  141. * party or that the license provides you with all the necessary rights
  142. * to make use of the Contribution.
  143. *
  144. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  145. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  146. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  147. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  148. * OTHERWISE.
  149. */
  150. #include <assert.h>
  151. #include <stdio.h>
  152. #include <openssl/buf.h>
  153. #include <openssl/bytestring.h>
  154. #include <openssl/rand.h>
  155. #include <openssl/obj.h>
  156. #include <openssl/evp.h>
  157. #include <openssl/mem.h>
  158. #include <openssl/md5.h>
  159. #include <openssl/dh.h>
  160. #include <openssl/bn.h>
  161. #include <openssl/engine.h>
  162. #include <openssl/x509.h>
  163. #include "ssl_locl.h"
  164. #include "../crypto/dh/internal.h"
  165. int ssl3_connect(SSL *s) {
  166. BUF_MEM *buf = NULL;
  167. void (*cb)(const SSL *ssl, int type, int val) = NULL;
  168. int ret = -1;
  169. int new_state, state, skip = 0;
  170. assert(s->handshake_func == ssl3_connect);
  171. assert(!s->server);
  172. assert(!SSL_IS_DTLS(s));
  173. ERR_clear_error();
  174. ERR_clear_system_error();
  175. if (s->info_callback != NULL) {
  176. cb = s->info_callback;
  177. } else if (s->ctx->info_callback != NULL) {
  178. cb = s->ctx->info_callback;
  179. }
  180. s->in_handshake++;
  181. for (;;) {
  182. state = s->state;
  183. switch (s->state) {
  184. case SSL_ST_RENEGOTIATE:
  185. s->renegotiate = 1;
  186. s->state = SSL_ST_CONNECT;
  187. s->ctx->stats.sess_connect_renegotiate++;
  188. /* fallthrough */
  189. case SSL_ST_CONNECT:
  190. case SSL_ST_BEFORE | SSL_ST_CONNECT:
  191. if (cb != NULL) {
  192. cb(s, SSL_CB_HANDSHAKE_START, 1);
  193. }
  194. if (s->init_buf == NULL) {
  195. buf = BUF_MEM_new();
  196. if (buf == NULL ||
  197. !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
  198. ret = -1;
  199. goto end;
  200. }
  201. s->init_buf = buf;
  202. buf = NULL;
  203. }
  204. if (!ssl3_setup_buffers(s) ||
  205. !ssl_init_wbio_buffer(s, 0)) {
  206. ret = -1;
  207. goto end;
  208. }
  209. /* don't push the buffering BIO quite yet */
  210. if (!ssl3_init_finished_mac(s)) {
  211. OPENSSL_PUT_ERROR(SSL, ssl3_connect, ERR_R_INTERNAL_ERROR);
  212. ret = -1;
  213. goto end;
  214. }
  215. s->state = SSL3_ST_CW_CLNT_HELLO_A;
  216. s->ctx->stats.sess_connect++;
  217. s->init_num = 0;
  218. break;
  219. case SSL3_ST_CW_CLNT_HELLO_A:
  220. case SSL3_ST_CW_CLNT_HELLO_B:
  221. s->shutdown = 0;
  222. ret = ssl3_send_client_hello(s);
  223. if (ret <= 0) {
  224. goto end;
  225. }
  226. s->state = SSL3_ST_CR_SRVR_HELLO_A;
  227. s->init_num = 0;
  228. /* turn on buffering for the next lot of output */
  229. if (s->bbio != s->wbio) {
  230. s->wbio = BIO_push(s->bbio, s->wbio);
  231. }
  232. break;
  233. case SSL3_ST_CR_SRVR_HELLO_A:
  234. case SSL3_ST_CR_SRVR_HELLO_B:
  235. ret = ssl3_get_server_hello(s);
  236. if (ret <= 0) {
  237. goto end;
  238. }
  239. if (s->hit) {
  240. s->state = SSL3_ST_CR_CHANGE;
  241. if (s->tlsext_ticket_expected) {
  242. /* receive renewed session ticket */
  243. s->state = SSL3_ST_CR_SESSION_TICKET_A;
  244. }
  245. } else {
  246. s->state = SSL3_ST_CR_CERT_A;
  247. }
  248. s->init_num = 0;
  249. break;
  250. case SSL3_ST_CR_CERT_A:
  251. case SSL3_ST_CR_CERT_B:
  252. if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
  253. ret = ssl3_get_server_certificate(s);
  254. if (ret <= 0) {
  255. goto end;
  256. }
  257. if (s->s3->tmp.certificate_status_expected) {
  258. s->state = SSL3_ST_CR_CERT_STATUS_A;
  259. } else {
  260. s->state = SSL3_ST_CR_KEY_EXCH_A;
  261. }
  262. } else {
  263. skip = 1;
  264. s->state = SSL3_ST_CR_KEY_EXCH_A;
  265. }
  266. s->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(s);
  271. if (ret <= 0) {
  272. goto end;
  273. }
  274. s->state = SSL3_ST_CR_CERT_REQ_A;
  275. s->init_num = 0;
  276. /* at this point we check that we have the
  277. * required stuff from the server */
  278. if (!ssl3_check_cert_and_algorithm(s)) {
  279. ret = -1;
  280. goto end;
  281. }
  282. break;
  283. case SSL3_ST_CR_CERT_REQ_A:
  284. case SSL3_ST_CR_CERT_REQ_B:
  285. ret = ssl3_get_certificate_request(s);
  286. if (ret <= 0) {
  287. goto end;
  288. }
  289. s->state = SSL3_ST_CR_SRVR_DONE_A;
  290. s->init_num = 0;
  291. break;
  292. case SSL3_ST_CR_SRVR_DONE_A:
  293. case SSL3_ST_CR_SRVR_DONE_B:
  294. ret = ssl3_get_server_done(s);
  295. if (ret <= 0) {
  296. goto end;
  297. }
  298. if (s->s3->tmp.cert_req) {
  299. s->state = SSL3_ST_CW_CERT_A;
  300. } else {
  301. s->state = SSL3_ST_CW_KEY_EXCH_A;
  302. }
  303. s->init_num = 0;
  304. break;
  305. case SSL3_ST_CW_CERT_A:
  306. case SSL3_ST_CW_CERT_B:
  307. case SSL3_ST_CW_CERT_C:
  308. case SSL3_ST_CW_CERT_D:
  309. ret = ssl3_send_client_certificate(s);
  310. if (ret <= 0) {
  311. goto end;
  312. }
  313. s->state = SSL3_ST_CW_KEY_EXCH_A;
  314. s->init_num = 0;
  315. break;
  316. case SSL3_ST_CW_KEY_EXCH_A:
  317. case SSL3_ST_CW_KEY_EXCH_B:
  318. ret = ssl3_send_client_key_exchange(s);
  319. if (ret <= 0) {
  320. goto end;
  321. }
  322. /* For TLS, cert_req is set to 2, so a cert chain
  323. * of nothing is sent, but no verify packet is sent */
  324. if (s->s3->tmp.cert_req == 1) {
  325. s->state = SSL3_ST_CW_CERT_VRFY_A;
  326. } else {
  327. s->state = SSL3_ST_CW_CHANGE_A;
  328. s->s3->change_cipher_spec = 0;
  329. }
  330. s->init_num = 0;
  331. break;
  332. case SSL3_ST_CW_CERT_VRFY_A:
  333. case SSL3_ST_CW_CERT_VRFY_B:
  334. ret = ssl3_send_cert_verify(s);
  335. if (ret <= 0) {
  336. goto end;
  337. }
  338. s->state = SSL3_ST_CW_CHANGE_A;
  339. s->init_num = 0;
  340. s->s3->change_cipher_spec = 0;
  341. break;
  342. case SSL3_ST_CW_CHANGE_A:
  343. case SSL3_ST_CW_CHANGE_B:
  344. ret = ssl3_send_change_cipher_spec(s, SSL3_ST_CW_CHANGE_A,
  345. SSL3_ST_CW_CHANGE_B);
  346. if (ret <= 0) {
  347. goto end;
  348. }
  349. s->state = SSL3_ST_CW_FINISHED_A;
  350. if (s->s3->tlsext_channel_id_valid) {
  351. s->state = SSL3_ST_CW_CHANNEL_ID_A;
  352. }
  353. if (s->s3->next_proto_neg_seen) {
  354. s->state = SSL3_ST_CW_NEXT_PROTO_A;
  355. }
  356. s->init_num = 0;
  357. s->session->cipher = s->s3->tmp.new_cipher;
  358. if (!s->enc_method->setup_key_block(s)) {
  359. ret = -1;
  360. goto end;
  361. }
  362. if (!s->enc_method->change_cipher_state(
  363. s, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
  364. ret = -1;
  365. goto end;
  366. }
  367. break;
  368. case SSL3_ST_CW_NEXT_PROTO_A:
  369. case SSL3_ST_CW_NEXT_PROTO_B:
  370. ret = ssl3_send_next_proto(s);
  371. if (ret <= 0) {
  372. goto end;
  373. }
  374. if (s->s3->tlsext_channel_id_valid) {
  375. s->state = SSL3_ST_CW_CHANNEL_ID_A;
  376. } else {
  377. s->state = SSL3_ST_CW_FINISHED_A;
  378. }
  379. break;
  380. case SSL3_ST_CW_CHANNEL_ID_A:
  381. case SSL3_ST_CW_CHANNEL_ID_B:
  382. ret = ssl3_send_channel_id(s);
  383. if (ret <= 0) {
  384. goto end;
  385. }
  386. s->state = SSL3_ST_CW_FINISHED_A;
  387. break;
  388. case SSL3_ST_CW_FINISHED_A:
  389. case SSL3_ST_CW_FINISHED_B:
  390. ret =
  391. ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A, SSL3_ST_CW_FINISHED_B,
  392. s->enc_method->client_finished_label,
  393. s->enc_method->client_finished_label_len);
  394. if (ret <= 0) {
  395. goto end;
  396. }
  397. s->state = SSL3_ST_CW_FLUSH;
  398. if (s->hit) {
  399. s->s3->tmp.next_state = SSL_ST_OK;
  400. } else {
  401. /* This is a non-resumption handshake. If it involves ChannelID, then
  402. * record the handshake hashes at this point in the session so that
  403. * any resumption of this session with ChannelID can sign those
  404. * hashes. */
  405. if (s->s3->tlsext_channel_id_new) {
  406. ret = tls1_record_handshake_hashes_for_channel_id(s);
  407. if (ret <= 0) {
  408. goto end;
  409. }
  410. }
  411. if ((SSL_get_mode(s) & SSL_MODE_ENABLE_FALSE_START) &&
  412. ssl3_can_false_start(s) &&
  413. /* No False Start on renegotiation (would complicate the state
  414. * machine). */
  415. s->s3->previous_server_finished_len == 0) {
  416. s->s3->tmp.next_state = SSL3_ST_FALSE_START;
  417. } else {
  418. /* Allow NewSessionTicket if ticket expected */
  419. if (s->tlsext_ticket_expected) {
  420. s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
  421. } else {
  422. s->s3->tmp.next_state = SSL3_ST_CR_CHANGE;
  423. }
  424. }
  425. }
  426. s->init_num = 0;
  427. break;
  428. case SSL3_ST_CR_SESSION_TICKET_A:
  429. case SSL3_ST_CR_SESSION_TICKET_B:
  430. ret = ssl3_get_new_session_ticket(s);
  431. if (ret <= 0) {
  432. goto end;
  433. }
  434. s->state = SSL3_ST_CR_CHANGE;
  435. s->init_num = 0;
  436. break;
  437. case SSL3_ST_CR_CERT_STATUS_A:
  438. case SSL3_ST_CR_CERT_STATUS_B:
  439. ret = ssl3_get_cert_status(s);
  440. if (ret <= 0) {
  441. goto end;
  442. }
  443. s->state = SSL3_ST_CR_KEY_EXCH_A;
  444. s->init_num = 0;
  445. break;
  446. case SSL3_ST_CR_CHANGE:
  447. /* At this point, the next message must be entirely behind a
  448. * ChangeCipherSpec. */
  449. if (!ssl3_expect_change_cipher_spec(s)) {
  450. ret = -1;
  451. goto end;
  452. }
  453. s->state = SSL3_ST_CR_FINISHED_A;
  454. break;
  455. case SSL3_ST_CR_FINISHED_A:
  456. case SSL3_ST_CR_FINISHED_B:
  457. ret =
  458. ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, SSL3_ST_CR_FINISHED_B);
  459. if (ret <= 0) {
  460. goto end;
  461. }
  462. if (s->hit) {
  463. s->state = SSL3_ST_CW_CHANGE_A;
  464. } else {
  465. s->state = SSL_ST_OK;
  466. }
  467. s->init_num = 0;
  468. break;
  469. case SSL3_ST_CW_FLUSH:
  470. s->rwstate = SSL_WRITING;
  471. if (BIO_flush(s->wbio) <= 0) {
  472. ret = -1;
  473. goto end;
  474. }
  475. s->rwstate = SSL_NOTHING;
  476. s->state = s->s3->tmp.next_state;
  477. break;
  478. case SSL3_ST_FALSE_START:
  479. /* Allow NewSessionTicket if ticket expected */
  480. if (s->tlsext_ticket_expected) {
  481. s->state = SSL3_ST_CR_SESSION_TICKET_A;
  482. } else {
  483. s->state = SSL3_ST_CR_CHANGE;
  484. }
  485. s->s3->tmp.in_false_start = 1;
  486. ssl_free_wbio_buffer(s);
  487. ret = 1;
  488. goto end;
  489. case SSL_ST_OK:
  490. /* clean a few things up */
  491. ssl3_cleanup_key_block(s);
  492. if (s->init_buf != NULL) {
  493. BUF_MEM_free(s->init_buf);
  494. s->init_buf = NULL;
  495. }
  496. /* Remove write buffering now. */
  497. ssl_free_wbio_buffer(s);
  498. s->init_num = 0;
  499. s->renegotiate = 0;
  500. s->new_session = 0;
  501. s->s3->tmp.in_false_start = 0;
  502. ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
  503. if (s->hit) {
  504. s->ctx->stats.sess_hit++;
  505. }
  506. ret = 1;
  507. /* s->server=0; */
  508. s->ctx->stats.sess_connect_good++;
  509. if (cb != NULL) {
  510. cb(s, SSL_CB_HANDSHAKE_DONE, 1);
  511. }
  512. goto end;
  513. default:
  514. OPENSSL_PUT_ERROR(SSL, ssl3_connect, SSL_R_UNKNOWN_STATE);
  515. ret = -1;
  516. goto end;
  517. }
  518. if (!s->s3->tmp.reuse_message && !skip) {
  519. if (cb != NULL && s->state != state) {
  520. new_state = s->state;
  521. s->state = state;
  522. cb(s, SSL_CB_CONNECT_LOOP, 1);
  523. s->state = new_state;
  524. }
  525. }
  526. skip = 0;
  527. }
  528. end:
  529. s->in_handshake--;
  530. if (buf != NULL) {
  531. BUF_MEM_free(buf);
  532. }
  533. if (cb != NULL) {
  534. cb(s, SSL_CB_CONNECT_EXIT, ret);
  535. }
  536. return ret;
  537. }
  538. int ssl3_send_client_hello(SSL *s) {
  539. uint8_t *buf, *p, *d;
  540. int i;
  541. unsigned long l;
  542. buf = (uint8_t *)s->init_buf->data;
  543. if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
  544. if (!s->s3->have_version) {
  545. uint16_t max_version = ssl3_get_max_client_version(s);
  546. /* Disabling all versions is silly: return an error. */
  547. if (max_version == 0) {
  548. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_WRONG_SSL_VERSION);
  549. goto err;
  550. }
  551. s->version = max_version;
  552. s->client_version = max_version;
  553. }
  554. /* If the configured session was created at a version higher than our
  555. * maximum version, drop it. */
  556. if (s->session &&
  557. (s->session->session_id_length == 0 || s->session->not_resumable ||
  558. (!SSL_IS_DTLS(s) && s->session->ssl_version > s->version) ||
  559. (SSL_IS_DTLS(s) && s->session->ssl_version < s->version))) {
  560. SSL_set_session(s, NULL);
  561. }
  562. /* else use the pre-loaded session */
  563. p = s->s3->client_random;
  564. /* If resending the ClientHello in DTLS after a HelloVerifyRequest, don't
  565. * renegerate the client_random. The random must be reused. */
  566. if (!SSL_IS_DTLS(s) || !s->d1->send_cookie) {
  567. ssl_fill_hello_random(s, 0, p, sizeof(s->s3->client_random));
  568. }
  569. /* Do the message type and length last. Note: the final argument to
  570. * ssl_add_clienthello_tlsext below depends on the size of this prefix. */
  571. d = p = ssl_handshake_start(s);
  572. /* version indicates the negotiated version: for example from an SSLv2/v3
  573. * compatible client hello). The client_version field is the maximum
  574. * version we permit and it is also used in RSA encrypted premaster
  575. * secrets. Some servers can choke if we initially report a higher version
  576. * then renegotiate to a lower one in the premaster secret. This didn't
  577. * happen with TLS 1.0 as most servers supported it but it can with TLS 1.1
  578. * or later if the server only supports 1.0.
  579. *
  580. * Possible scenario with previous logic:
  581. * 1. Client hello indicates TLS 1.2
  582. * 2. Server hello says TLS 1.0
  583. * 3. RSA encrypted premaster secret uses 1.2.
  584. * 4. Handhaked proceeds using TLS 1.0.
  585. * 5. Server sends hello request to renegotiate.
  586. * 6. Client hello indicates TLS v1.0 as we now
  587. * know that is maximum server supports.
  588. * 7. Server chokes on RSA encrypted premaster secret
  589. * containing version 1.0.
  590. *
  591. * For interoperability it should be OK to always use the maximum version
  592. * we support in client hello and then rely on the checking of version to
  593. * ensure the servers isn't being inconsistent: for example initially
  594. * negotiating with TLS 1.0 and renegotiating with TLS 1.2. We do this by
  595. * using client_version in client hello and not resetting it to the
  596. * negotiated version. */
  597. *(p++) = s->client_version >> 8;
  598. *(p++) = s->client_version & 0xff;
  599. /* Random stuff */
  600. memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
  601. p += SSL3_RANDOM_SIZE;
  602. /* Session ID */
  603. if (s->new_session || s->session == NULL) {
  604. i = 0;
  605. } else {
  606. i = s->session->session_id_length;
  607. }
  608. *(p++) = i;
  609. if (i != 0) {
  610. if (i > (int)sizeof(s->session->session_id)) {
  611. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
  612. goto err;
  613. }
  614. memcpy(p, s->session->session_id, i);
  615. p += i;
  616. }
  617. /* cookie stuff for DTLS */
  618. if (SSL_IS_DTLS(s)) {
  619. if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
  620. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
  621. goto err;
  622. }
  623. *(p++) = s->d1->cookie_len;
  624. memcpy(p, s->d1->cookie, s->d1->cookie_len);
  625. p += s->d1->cookie_len;
  626. }
  627. /* Ciphers supported */
  628. i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2]);
  629. if (i == 0) {
  630. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello,
  631. SSL_R_NO_CIPHERS_AVAILABLE);
  632. goto err;
  633. }
  634. s2n(i, p);
  635. p += i;
  636. /* COMPRESSION */
  637. *(p++) = 1;
  638. *(p++) = 0; /* Add the NULL method */
  639. /* TLS extensions*/
  640. if (ssl_prepare_clienthello_tlsext(s) <= 0) {
  641. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, SSL_R_CLIENTHELLO_TLSEXT);
  642. goto err;
  643. }
  644. p = ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
  645. p - buf);
  646. if (p == NULL) {
  647. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_hello, ERR_R_INTERNAL_ERROR);
  648. goto err;
  649. }
  650. l = p - d;
  651. if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
  652. goto err;
  653. }
  654. s->state = SSL3_ST_CW_CLNT_HELLO_B;
  655. }
  656. /* SSL3_ST_CW_CLNT_HELLO_B */
  657. return ssl_do_write(s);
  658. err:
  659. return -1;
  660. }
  661. int ssl3_get_server_hello(SSL *s) {
  662. STACK_OF(SSL_CIPHER) * sk;
  663. const SSL_CIPHER *c;
  664. CERT *ct = s->cert;
  665. int al = SSL_AD_INTERNAL_ERROR, ok;
  666. long n;
  667. CBS server_hello, server_random, session_id;
  668. uint16_t server_version, cipher_suite;
  669. uint8_t compression_method;
  670. unsigned long mask_ssl;
  671. n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_HELLO_A,
  672. SSL3_ST_CR_SRVR_HELLO_B, SSL3_MT_SERVER_HELLO,
  673. 20000, /* ?? */
  674. ssl_hash_message, &ok);
  675. if (!ok) {
  676. uint32_t err = ERR_peek_error();
  677. if (ERR_GET_LIB(err) == ERR_LIB_SSL &&
  678. ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) {
  679. /* Add a dedicated error code to the queue for a handshake_failure alert
  680. * in response to ClientHello. This matches NSS's client behavior and
  681. * gives a better error on a (probable) failure to negotiate initial
  682. * parameters. Note: this error code comes after the original one.
  683. *
  684. * See https://crbug.com/446505. */
  685. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
  686. SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO);
  687. }
  688. return n;
  689. }
  690. CBS_init(&server_hello, s->init_msg, n);
  691. if (!CBS_get_u16(&server_hello, &server_version) ||
  692. !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
  693. !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
  694. CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
  695. !CBS_get_u16(&server_hello, &cipher_suite) ||
  696. !CBS_get_u8(&server_hello, &compression_method)) {
  697. al = SSL_AD_DECODE_ERROR;
  698. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_DECODE_ERROR);
  699. goto f_err;
  700. }
  701. if (!s->s3->have_version) {
  702. if (!ssl3_is_version_enabled(s, server_version)) {
  703. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNSUPPORTED_PROTOCOL);
  704. s->version = server_version;
  705. /* Mark the version as fixed so the record-layer version is not clamped
  706. * to TLS 1.0. */
  707. s->s3->have_version = 1;
  708. al = SSL_AD_PROTOCOL_VERSION;
  709. goto f_err;
  710. }
  711. s->version = server_version;
  712. s->enc_method = ssl3_get_enc_method(server_version);
  713. assert(s->enc_method != NULL);
  714. /* At this point, the connection's version is known and s->version is
  715. * fixed. Begin enforcing the record-layer version. */
  716. s->s3->have_version = 1;
  717. } else if (server_version != s->version) {
  718. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
  719. al = SSL_AD_PROTOCOL_VERSION;
  720. goto f_err;
  721. }
  722. /* Copy over the server random. */
  723. memcpy(s->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
  724. assert(s->session == NULL || s->session->session_id_length > 0);
  725. if (s->session != NULL && CBS_mem_equal(&session_id, s->session->session_id,
  726. s->session->session_id_length)) {
  727. if (s->sid_ctx_length != s->session->sid_ctx_length ||
  728. memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
  729. /* actually a client application bug */
  730. al = SSL_AD_ILLEGAL_PARAMETER;
  731. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
  732. SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
  733. goto f_err;
  734. }
  735. s->hit = 1;
  736. } else {
  737. /* The session wasn't resumed. Create a fresh SSL_SESSION to
  738. * fill out. */
  739. s->hit = 0;
  740. if (!ssl_get_new_session(s, 0)) {
  741. goto f_err;
  742. }
  743. /* Note: session_id could be empty. */
  744. s->session->session_id_length = CBS_len(&session_id);
  745. memcpy(s->session->session_id, CBS_data(&session_id), CBS_len(&session_id));
  746. }
  747. c = ssl3_get_cipher_by_value(cipher_suite);
  748. if (c == NULL) {
  749. /* unknown cipher */
  750. al = SSL_AD_ILLEGAL_PARAMETER;
  751. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
  752. SSL_R_UNKNOWN_CIPHER_RETURNED);
  753. goto f_err;
  754. }
  755. /* ct->mask_ssl was computed from client capabilities. Now
  756. * that the final version is known, compute a new mask_ssl. */
  757. if (!SSL_USE_TLS1_2_CIPHERS(s)) {
  758. mask_ssl = SSL_TLSV1_2;
  759. } else {
  760. mask_ssl = 0;
  761. }
  762. /* If the cipher is disabled then we didn't sent it in the ClientHello, so if
  763. * the server selected it, it's an error. */
  764. if ((c->algorithm_ssl & mask_ssl) ||
  765. (c->algorithm_mkey & ct->mask_k) ||
  766. (c->algorithm_auth & ct->mask_a)) {
  767. al = SSL_AD_ILLEGAL_PARAMETER;
  768. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
  769. goto f_err;
  770. }
  771. sk = ssl_get_ciphers_by_id(s);
  772. if (!sk_SSL_CIPHER_find(sk, NULL, c)) {
  773. /* we did not say we would use this cipher */
  774. al = SSL_AD_ILLEGAL_PARAMETER;
  775. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
  776. goto f_err;
  777. }
  778. if (s->hit && s->session->cipher != c) {
  779. al = SSL_AD_ILLEGAL_PARAMETER;
  780. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
  781. SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
  782. goto f_err;
  783. }
  784. s->s3->tmp.new_cipher = c;
  785. /* Most clients also require that the negotiated version match the session's
  786. * version if resuming. However OpenSSL has historically not had the
  787. * corresponding logic on the server, so this may not be compatible,
  788. * depending on other factors. (Whether the ClientHello version is clamped to
  789. * the session's version and whether the session cache is keyed on IP
  790. * address.)
  791. *
  792. * TODO(davidben): See if we can still enforce this? Perhaps for the future
  793. * TLS 1.3 and forward if this is fixed upstream. */
  794. /* Don't digest cached records if no sigalgs: we may need them for client
  795. * authentication. */
  796. if (!SSL_USE_SIGALGS(s) &&
  797. !ssl3_digest_cached_records(s, free_handshake_buffer)) {
  798. goto f_err;
  799. }
  800. /* Only the NULL compression algorithm is supported. */
  801. if (compression_method != 0) {
  802. al = SSL_AD_ILLEGAL_PARAMETER;
  803. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello,
  804. SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  805. goto f_err;
  806. }
  807. /* TLS extensions */
  808. if (!ssl_parse_serverhello_tlsext(s, &server_hello)) {
  809. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_PARSE_TLSEXT);
  810. goto err;
  811. }
  812. /* There should be nothing left over in the record. */
  813. if (CBS_len(&server_hello) != 0) {
  814. /* wrong packet length */
  815. al = SSL_AD_DECODE_ERROR;
  816. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_PACKET_LENGTH);
  817. goto f_err;
  818. }
  819. return 1;
  820. f_err:
  821. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  822. err:
  823. return -1;
  824. }
  825. int ssl3_get_server_certificate(SSL *s) {
  826. int al, i, ok, ret = -1;
  827. unsigned long n;
  828. X509 *x = NULL;
  829. STACK_OF(X509) *sk = NULL;
  830. SESS_CERT *sc;
  831. EVP_PKEY *pkey = NULL;
  832. CBS cbs, certificate_list;
  833. const uint8_t *data;
  834. n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_A, SSL3_ST_CR_CERT_B,
  835. SSL3_MT_CERTIFICATE, s->max_cert_list,
  836. ssl_hash_message, &ok);
  837. if (!ok) {
  838. return n;
  839. }
  840. CBS_init(&cbs, s->init_msg, n);
  841. sk = sk_X509_new_null();
  842. if (sk == NULL) {
  843. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
  844. goto err;
  845. }
  846. if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
  847. CBS_len(&cbs) != 0) {
  848. al = SSL_AD_DECODE_ERROR;
  849. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_LENGTH_MISMATCH);
  850. goto f_err;
  851. }
  852. while (CBS_len(&certificate_list) > 0) {
  853. CBS certificate;
  854. if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
  855. al = SSL_AD_DECODE_ERROR;
  856. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
  857. SSL_R_CERT_LENGTH_MISMATCH);
  858. goto f_err;
  859. }
  860. data = CBS_data(&certificate);
  861. x = d2i_X509(NULL, &data, CBS_len(&certificate));
  862. if (x == NULL) {
  863. al = SSL_AD_BAD_CERTIFICATE;
  864. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_ASN1_LIB);
  865. goto f_err;
  866. }
  867. if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
  868. al = SSL_AD_DECODE_ERROR;
  869. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
  870. SSL_R_CERT_LENGTH_MISMATCH);
  871. goto f_err;
  872. }
  873. if (!sk_X509_push(sk, x)) {
  874. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
  875. goto err;
  876. }
  877. x = NULL;
  878. }
  879. i = ssl_verify_cert_chain(s, sk);
  880. if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
  881. al = ssl_verify_alarm_type(s->verify_result);
  882. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
  883. SSL_R_CERTIFICATE_VERIFY_FAILED);
  884. goto f_err;
  885. }
  886. ERR_clear_error(); /* but we keep s->verify_result */
  887. sc = ssl_sess_cert_new();
  888. if (sc == NULL) {
  889. goto err;
  890. }
  891. if (s->session->sess_cert) {
  892. ssl_sess_cert_free(s->session->sess_cert);
  893. }
  894. s->session->sess_cert = sc;
  895. sc->cert_chain = sk;
  896. /* Inconsistency alert: cert_chain does include the peer's certificate, which
  897. * we don't include in s3_srvr.c */
  898. x = sk_X509_value(sk, 0);
  899. sk = NULL;
  900. /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
  901. pkey = X509_get_pubkey(x);
  902. if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
  903. x = NULL;
  904. al = SSL3_AL_FATAL;
  905. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
  906. SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
  907. goto f_err;
  908. }
  909. i = ssl_cert_type(pkey);
  910. if (i < 0) {
  911. x = NULL;
  912. al = SSL3_AL_FATAL;
  913. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
  914. SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  915. goto f_err;
  916. }
  917. int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
  918. if (exp_idx >= 0 && i != exp_idx) {
  919. x = NULL;
  920. al = SSL_AD_ILLEGAL_PARAMETER;
  921. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate,
  922. SSL_R_WRONG_CERTIFICATE_TYPE);
  923. goto f_err;
  924. }
  925. sc->peer_cert_type = i;
  926. /* Why would the following ever happen? We just created sc a couple of lines
  927. * ago. */
  928. if (sc->peer_pkeys[i].x509 != NULL) {
  929. X509_free(sc->peer_pkeys[i].x509);
  930. }
  931. sc->peer_pkeys[i].x509 = X509_up_ref(x);
  932. sc->peer_key = &(sc->peer_pkeys[i]);
  933. if (s->session->peer != NULL) {
  934. X509_free(s->session->peer);
  935. }
  936. s->session->peer = X509_up_ref(x);
  937. s->session->verify_result = s->verify_result;
  938. x = NULL;
  939. ret = 1;
  940. if (0) {
  941. f_err:
  942. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  943. }
  944. err:
  945. EVP_PKEY_free(pkey);
  946. X509_free(x);
  947. sk_X509_pop_free(sk, X509_free);
  948. return ret;
  949. }
  950. int ssl3_get_server_key_exchange(SSL *s) {
  951. EVP_MD_CTX md_ctx;
  952. int al, ok;
  953. long n, alg_k, alg_a;
  954. EVP_PKEY *pkey = NULL;
  955. const EVP_MD *md = NULL;
  956. RSA *rsa = NULL;
  957. DH *dh = NULL;
  958. EC_KEY *ecdh = NULL;
  959. BN_CTX *bn_ctx = NULL;
  960. EC_POINT *srvr_ecpoint = NULL;
  961. CBS server_key_exchange, server_key_exchange_orig, parameter;
  962. /* use same message size as in ssl3_get_certificate_request() as
  963. * ServerKeyExchange message may be skipped */
  964. n = s->method->ssl_get_message(s, SSL3_ST_CR_KEY_EXCH_A,
  965. SSL3_ST_CR_KEY_EXCH_B, -1, s->max_cert_list,
  966. ssl_hash_message, &ok);
  967. if (!ok) {
  968. return n;
  969. }
  970. if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
  971. if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher)) {
  972. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
  973. SSL_R_UNEXPECTED_MESSAGE);
  974. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  975. return -1;
  976. }
  977. /* In plain PSK ciphersuite, ServerKeyExchange can be
  978. omitted if no identity hint is sent. Set session->sess_cert anyway to
  979. avoid problems later.*/
  980. if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK) {
  981. /* PSK ciphersuites that also send a Certificate would have already
  982. * initialized |sess_cert|. */
  983. if (s->session->sess_cert == NULL) {
  984. s->session->sess_cert = ssl_sess_cert_new();
  985. if (s->session->sess_cert == NULL) {
  986. return -1;
  987. }
  988. }
  989. /* TODO(davidben): This should be reset in one place with the rest of the
  990. * handshake state. */
  991. if (s->s3->tmp.peer_psk_identity_hint) {
  992. OPENSSL_free(s->s3->tmp.peer_psk_identity_hint);
  993. s->s3->tmp.peer_psk_identity_hint = NULL;
  994. }
  995. }
  996. s->s3->tmp.reuse_message = 1;
  997. return 1;
  998. }
  999. /* Retain a copy of the original CBS to compute the signature over. */
  1000. CBS_init(&server_key_exchange, s->init_msg, n);
  1001. server_key_exchange_orig = server_key_exchange;
  1002. if (s->session->sess_cert != NULL) {
  1003. if (s->session->sess_cert->peer_dh_tmp) {
  1004. DH_free(s->session->sess_cert->peer_dh_tmp);
  1005. s->session->sess_cert->peer_dh_tmp = NULL;
  1006. }
  1007. if (s->session->sess_cert->peer_ecdh_tmp) {
  1008. EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
  1009. s->session->sess_cert->peer_ecdh_tmp = NULL;
  1010. }
  1011. } else {
  1012. s->session->sess_cert = ssl_sess_cert_new();
  1013. if (s->session->sess_cert == NULL) {
  1014. return -1;
  1015. }
  1016. }
  1017. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  1018. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  1019. EVP_MD_CTX_init(&md_ctx);
  1020. if (alg_a & SSL_aPSK) {
  1021. CBS psk_identity_hint;
  1022. /* Each of the PSK key exchanges begins with a psk_identity_hint. */
  1023. if (!CBS_get_u16_length_prefixed(&server_key_exchange,
  1024. &psk_identity_hint)) {
  1025. al = SSL_AD_DECODE_ERROR;
  1026. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
  1027. goto f_err;
  1028. }
  1029. /* Store PSK identity hint for later use, hint is used in
  1030. * ssl3_send_client_key_exchange. Assume that the maximum length of a PSK
  1031. * identity hint can be as long as the maximum length of a PSK identity.
  1032. * Also do not allow NULL characters; identities are saved as C strings.
  1033. *
  1034. * TODO(davidben): Should invalid hints be ignored? It's a hint rather than
  1035. * a specific identity. */
  1036. if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
  1037. CBS_contains_zero_byte(&psk_identity_hint)) {
  1038. al = SSL_AD_HANDSHAKE_FAILURE;
  1039. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
  1040. SSL_R_DATA_LENGTH_TOO_LONG);
  1041. goto f_err;
  1042. }
  1043. /* Save the identity hint as a C string. */
  1044. if (!CBS_strdup(&psk_identity_hint, &s->s3->tmp.peer_psk_identity_hint)) {
  1045. al = SSL_AD_INTERNAL_ERROR;
  1046. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
  1047. ERR_R_MALLOC_FAILURE);
  1048. goto f_err;
  1049. }
  1050. }
  1051. if (alg_k & SSL_kEDH) {
  1052. CBS dh_p, dh_g, dh_Ys;
  1053. if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
  1054. CBS_len(&dh_p) == 0 ||
  1055. !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
  1056. CBS_len(&dh_g) == 0 ||
  1057. !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
  1058. CBS_len(&dh_Ys) == 0) {
  1059. al = SSL_AD_DECODE_ERROR;
  1060. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
  1061. goto f_err;
  1062. }
  1063. dh = DH_new();
  1064. if (dh == NULL) {
  1065. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_DH_LIB);
  1066. goto err;
  1067. }
  1068. if ((dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL)) == NULL ||
  1069. (dh->g = BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL)) == NULL ||
  1070. (dh->pub_key = BN_bin2bn(CBS_data(&dh_Ys), CBS_len(&dh_Ys), NULL)) ==
  1071. NULL) {
  1072. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
  1073. goto err;
  1074. }
  1075. if (DH_size(dh) < 512 / 8) {
  1076. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
  1077. SSL_R_BAD_DH_P_LENGTH);
  1078. goto err;
  1079. }
  1080. if (alg_a & SSL_aRSA) {
  1081. pkey = X509_get_pubkey(
  1082. s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1083. }
  1084. /* else anonymous DH, so no certificate or pkey. */
  1085. s->session->sess_cert->peer_dh_tmp = dh;
  1086. dh = NULL;
  1087. } else if (alg_k & SSL_kEECDH) {
  1088. uint16_t curve_id;
  1089. int curve_nid = 0;
  1090. EC_GROUP *ngroup;
  1091. const EC_GROUP *group;
  1092. CBS point;
  1093. /* Extract elliptic curve parameters and the server's ephemeral ECDH public
  1094. * key. Check curve is one of our preferences, if not server has sent an
  1095. * invalid curve. */
  1096. if (!tls1_check_curve(s, &server_key_exchange, &curve_id)) {
  1097. al = SSL_AD_DECODE_ERROR;
  1098. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_WRONG_CURVE);
  1099. goto f_err;
  1100. }
  1101. curve_nid = tls1_ec_curve_id2nid(curve_id);
  1102. if (curve_nid == 0) {
  1103. al = SSL_AD_INTERNAL_ERROR;
  1104. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
  1105. SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
  1106. goto f_err;
  1107. }
  1108. ecdh = EC_KEY_new();
  1109. if (ecdh == NULL) {
  1110. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
  1111. ERR_R_MALLOC_FAILURE);
  1112. goto err;
  1113. }
  1114. ngroup = EC_GROUP_new_by_curve_name(curve_nid);
  1115. if (ngroup == NULL) {
  1116. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_EC_LIB);
  1117. goto err;
  1118. }
  1119. if (!EC_KEY_set_group(ecdh, ngroup)) {
  1120. EC_GROUP_free(ngroup);
  1121. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_EC_LIB);
  1122. goto err;
  1123. }
  1124. EC_GROUP_free(ngroup);
  1125. group = EC_KEY_get0_group(ecdh);
  1126. /* Next, get the encoded ECPoint */
  1127. if (!CBS_get_u8_length_prefixed(&server_key_exchange, &point)) {
  1128. al = SSL_AD_DECODE_ERROR;
  1129. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
  1130. goto f_err;
  1131. }
  1132. if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
  1133. ((bn_ctx = BN_CTX_new()) == NULL)) {
  1134. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
  1135. ERR_R_MALLOC_FAILURE);
  1136. goto err;
  1137. }
  1138. if (!EC_POINT_oct2point(group, srvr_ecpoint, CBS_data(&point),
  1139. CBS_len(&point), bn_ctx)) {
  1140. al = SSL_AD_DECODE_ERROR;
  1141. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_ECPOINT);
  1142. goto f_err;
  1143. }
  1144. /* The ECC/TLS specification does not mention the use of DSA to sign
  1145. * ECParameters in the server key exchange message. We do support RSA and
  1146. * ECDSA. */
  1147. if (alg_a & SSL_aRSA) {
  1148. pkey = X509_get_pubkey(
  1149. s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1150. } else if (alg_a & SSL_aECDSA) {
  1151. pkey =
  1152. X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
  1153. }
  1154. /* else anonymous ECDH, so no certificate or pkey. */
  1155. EC_KEY_set_public_key(ecdh, srvr_ecpoint);
  1156. s->session->sess_cert->peer_ecdh_tmp = ecdh;
  1157. ecdh = NULL;
  1158. BN_CTX_free(bn_ctx);
  1159. bn_ctx = NULL;
  1160. EC_POINT_free(srvr_ecpoint);
  1161. srvr_ecpoint = NULL;
  1162. } else if (!(alg_k & SSL_kPSK)) {
  1163. al = SSL_AD_UNEXPECTED_MESSAGE;
  1164. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
  1165. SSL_R_UNEXPECTED_MESSAGE);
  1166. goto f_err;
  1167. }
  1168. /* At this point, |server_key_exchange| contains the signature, if any, while
  1169. * |server_key_exchange_orig| contains the entire message. From that, derive
  1170. * a CBS containing just the parameter. */
  1171. CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
  1172. CBS_len(&server_key_exchange_orig) - CBS_len(&server_key_exchange));
  1173. /* if it was signed, check the signature */
  1174. if (pkey != NULL) {
  1175. CBS signature;
  1176. if (SSL_USE_SIGALGS(s)) {
  1177. if (!tls12_check_peer_sigalg(&md, &al, s, &server_key_exchange, pkey)) {
  1178. goto f_err;
  1179. }
  1180. } else if (pkey->type == EVP_PKEY_RSA) {
  1181. md = EVP_md5_sha1();
  1182. } else {
  1183. md = EVP_sha1();
  1184. }
  1185. /* The last field in |server_key_exchange| is the signature. */
  1186. if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
  1187. CBS_len(&server_key_exchange) != 0) {
  1188. al = SSL_AD_DECODE_ERROR;
  1189. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
  1190. goto f_err;
  1191. }
  1192. if (!EVP_DigestVerifyInit(&md_ctx, NULL, md, NULL, pkey) ||
  1193. !EVP_DigestVerifyUpdate(&md_ctx, s->s3->client_random,
  1194. SSL3_RANDOM_SIZE) ||
  1195. !EVP_DigestVerifyUpdate(&md_ctx, s->s3->server_random,
  1196. SSL3_RANDOM_SIZE) ||
  1197. !EVP_DigestVerifyUpdate(&md_ctx, CBS_data(&parameter),
  1198. CBS_len(&parameter)) ||
  1199. !EVP_DigestVerifyFinal(&md_ctx, CBS_data(&signature),
  1200. CBS_len(&signature))) {
  1201. /* bad signature */
  1202. al = SSL_AD_DECRYPT_ERROR;
  1203. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
  1204. goto f_err;
  1205. }
  1206. } else {
  1207. if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
  1208. /* Might be wrong key type, check it */
  1209. if (ssl3_check_cert_and_algorithm(s)) {
  1210. /* Otherwise this shouldn't happen */
  1211. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
  1212. ERR_R_INTERNAL_ERROR);
  1213. }
  1214. goto err;
  1215. }
  1216. /* still data left over */
  1217. if (CBS_len(&server_key_exchange) > 0) {
  1218. al = SSL_AD_DECODE_ERROR;
  1219. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange,
  1220. SSL_R_EXTRA_DATA_IN_MESSAGE);
  1221. goto f_err;
  1222. }
  1223. }
  1224. EVP_PKEY_free(pkey);
  1225. EVP_MD_CTX_cleanup(&md_ctx);
  1226. return 1;
  1227. f_err:
  1228. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1229. err:
  1230. EVP_PKEY_free(pkey);
  1231. if (rsa != NULL) {
  1232. RSA_free(rsa);
  1233. }
  1234. if (dh != NULL) {
  1235. DH_free(dh);
  1236. }
  1237. BN_CTX_free(bn_ctx);
  1238. EC_POINT_free(srvr_ecpoint);
  1239. if (ecdh != NULL) {
  1240. EC_KEY_free(ecdh);
  1241. }
  1242. EVP_MD_CTX_cleanup(&md_ctx);
  1243. return -1;
  1244. }
  1245. static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
  1246. return X509_NAME_cmp(*a, *b);
  1247. }
  1248. int ssl3_get_certificate_request(SSL *s) {
  1249. int ok, ret = 0;
  1250. unsigned long n;
  1251. X509_NAME *xn = NULL;
  1252. STACK_OF(X509_NAME) *ca_sk = NULL;
  1253. CBS cbs;
  1254. CBS certificate_types;
  1255. CBS certificate_authorities;
  1256. const uint8_t *data;
  1257. n = s->method->ssl_get_message(s, SSL3_ST_CR_CERT_REQ_A,
  1258. SSL3_ST_CR_CERT_REQ_B, -1, s->max_cert_list,
  1259. ssl_hash_message, &ok);
  1260. if (!ok) {
  1261. return n;
  1262. }
  1263. s->s3->tmp.cert_req = 0;
  1264. if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
  1265. s->s3->tmp.reuse_message = 1;
  1266. /* If we get here we don't need any cached handshake records as we wont be
  1267. * doing client auth. */
  1268. if (s->s3->handshake_buffer &&
  1269. !ssl3_digest_cached_records(s, free_handshake_buffer)) {
  1270. goto err;
  1271. }
  1272. return 1;
  1273. }
  1274. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
  1275. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  1276. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
  1277. SSL_R_WRONG_MESSAGE_TYPE);
  1278. goto err;
  1279. }
  1280. /* TLS does not like anon-DH with client cert */
  1281. if (s->version > SSL3_VERSION &&
  1282. (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)) {
  1283. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  1284. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
  1285. SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
  1286. goto err;
  1287. }
  1288. CBS_init(&cbs, s->init_msg, n);
  1289. ca_sk = sk_X509_NAME_new(ca_dn_cmp);
  1290. if (ca_sk == NULL) {
  1291. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
  1292. goto err;
  1293. }
  1294. /* get the certificate types */
  1295. if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types)) {
  1296. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1297. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
  1298. goto err;
  1299. }
  1300. if (!CBS_stow(&certificate_types, &s->s3->tmp.certificate_types,
  1301. &s->s3->tmp.num_certificate_types)) {
  1302. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1303. goto err;
  1304. }
  1305. if (SSL_USE_SIGALGS(s)) {
  1306. CBS supported_signature_algorithms;
  1307. if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms)) {
  1308. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1309. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
  1310. goto err;
  1311. }
  1312. if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
  1313. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1314. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
  1315. SSL_R_SIGNATURE_ALGORITHMS_ERROR);
  1316. goto err;
  1317. }
  1318. }
  1319. /* get the CA RDNs */
  1320. if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities)) {
  1321. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1322. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
  1323. goto err;
  1324. }
  1325. while (CBS_len(&certificate_authorities) > 0) {
  1326. CBS distinguished_name;
  1327. if (!CBS_get_u16_length_prefixed(&certificate_authorities,
  1328. &distinguished_name)) {
  1329. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1330. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
  1331. SSL_R_CA_DN_TOO_LONG);
  1332. goto err;
  1333. }
  1334. data = CBS_data(&distinguished_name);
  1335. xn = d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name));
  1336. if (xn == NULL) {
  1337. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1338. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
  1339. goto err;
  1340. }
  1341. if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name))) {
  1342. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1343. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_INTERNAL_ERROR);
  1344. goto err;
  1345. }
  1346. if (CBS_len(&distinguished_name) != 0) {
  1347. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1348. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
  1349. SSL_R_CA_DN_LENGTH_MISMATCH);
  1350. goto err;
  1351. }
  1352. if (!sk_X509_NAME_push(ca_sk, xn)) {
  1353. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request,
  1354. ERR_R_MALLOC_FAILURE);
  1355. goto err;
  1356. }
  1357. }
  1358. /* we should setup a certificate to return.... */
  1359. s->s3->tmp.cert_req = 1;
  1360. if (s->s3->tmp.ca_names != NULL) {
  1361. sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
  1362. }
  1363. s->s3->tmp.ca_names = ca_sk;
  1364. ca_sk = NULL;
  1365. ret = 1;
  1366. err:
  1367. if (ca_sk != NULL) {
  1368. sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
  1369. }
  1370. return ret;
  1371. }
  1372. int ssl3_get_new_session_ticket(SSL *s) {
  1373. int ok, al;
  1374. long n;
  1375. CBS new_session_ticket, ticket;
  1376. n = s->method->ssl_get_message(
  1377. s, SSL3_ST_CR_SESSION_TICKET_A, SSL3_ST_CR_SESSION_TICKET_B,
  1378. SSL3_MT_NEWSESSION_TICKET, 16384, ssl_hash_message, &ok);
  1379. if (!ok) {
  1380. return n;
  1381. }
  1382. CBS_init(&new_session_ticket, s->init_msg, n);
  1383. if (!CBS_get_u32(&new_session_ticket,
  1384. &s->session->tlsext_tick_lifetime_hint) ||
  1385. !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
  1386. CBS_len(&new_session_ticket) != 0) {
  1387. al = SSL_AD_DECODE_ERROR;
  1388. OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_DECODE_ERROR);
  1389. goto f_err;
  1390. }
  1391. if (!CBS_stow(&ticket, &s->session->tlsext_tick,
  1392. &s->session->tlsext_ticklen)) {
  1393. OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
  1394. goto err;
  1395. }
  1396. /* Generate a session ID for this session based on the session ticket. We use
  1397. * the session ID mechanism for detecting ticket resumption. This also fits in
  1398. * with assumptions elsewhere in OpenSSL.*/
  1399. if (!EVP_Digest(CBS_data(&ticket), CBS_len(&ticket), s->session->session_id,
  1400. &s->session->session_id_length, EVP_sha256(), NULL)) {
  1401. goto err;
  1402. }
  1403. return 1;
  1404. f_err:
  1405. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1406. err:
  1407. return -1;
  1408. }
  1409. int ssl3_get_cert_status(SSL *s) {
  1410. int ok, al;
  1411. long n;
  1412. CBS certificate_status, ocsp_response;
  1413. uint8_t status_type;
  1414. n = s->method->ssl_get_message(
  1415. s, SSL3_ST_CR_CERT_STATUS_A, SSL3_ST_CR_CERT_STATUS_B,
  1416. SSL3_MT_CERTIFICATE_STATUS, 16384, ssl_hash_message, &ok);
  1417. if (!ok) {
  1418. return n;
  1419. }
  1420. CBS_init(&certificate_status, s->init_msg, n);
  1421. if (!CBS_get_u8(&certificate_status, &status_type) ||
  1422. status_type != TLSEXT_STATUSTYPE_ocsp ||
  1423. !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
  1424. CBS_len(&ocsp_response) == 0 ||
  1425. CBS_len(&certificate_status) != 0) {
  1426. al = SSL_AD_DECODE_ERROR;
  1427. OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR);
  1428. goto f_err;
  1429. }
  1430. if (!CBS_stow(&ocsp_response, &s->session->ocsp_response,
  1431. &s->session->ocsp_response_length)) {
  1432. al = SSL_AD_INTERNAL_ERROR;
  1433. OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
  1434. goto f_err;
  1435. }
  1436. return 1;
  1437. f_err:
  1438. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1439. return -1;
  1440. }
  1441. int ssl3_get_server_done(SSL *s) {
  1442. int ok;
  1443. long n;
  1444. n = s->method->ssl_get_message(s, SSL3_ST_CR_SRVR_DONE_A,
  1445. SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE,
  1446. 30, /* should be very small, like 0 :-) */
  1447. ssl_hash_message, &ok);
  1448. if (!ok) {
  1449. return n;
  1450. }
  1451. if (n > 0) {
  1452. /* should contain no data */
  1453. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1454. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
  1455. return -1;
  1456. }
  1457. return 1;
  1458. }
  1459. int ssl3_send_client_key_exchange(SSL *s) {
  1460. uint8_t *p;
  1461. int n = 0;
  1462. unsigned long alg_k;
  1463. unsigned long alg_a;
  1464. uint8_t *q;
  1465. EVP_PKEY *pkey = NULL;
  1466. EC_KEY *clnt_ecdh = NULL;
  1467. const EC_POINT *srvr_ecpoint = NULL;
  1468. EVP_PKEY *srvr_pub_pkey = NULL;
  1469. uint8_t *encodedPoint = NULL;
  1470. int encoded_pt_len = 0;
  1471. BN_CTX *bn_ctx = NULL;
  1472. unsigned int psk_len = 0;
  1473. uint8_t psk[PSK_MAX_PSK_LEN];
  1474. uint8_t *pms = NULL;
  1475. size_t pms_len = 0;
  1476. if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
  1477. p = ssl_handshake_start(s);
  1478. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  1479. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  1480. /* If using a PSK key exchange, prepare the pre-shared key. */
  1481. if (alg_a & SSL_aPSK) {
  1482. char identity[PSK_MAX_IDENTITY_LEN + 1];
  1483. size_t identity_len;
  1484. if (s->psk_client_callback == NULL) {
  1485. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1486. SSL_R_PSK_NO_CLIENT_CB);
  1487. goto err;
  1488. }
  1489. memset(identity, 0, sizeof(identity));
  1490. psk_len =
  1491. s->psk_client_callback(s, s->s3->tmp.peer_psk_identity_hint, identity,
  1492. sizeof(identity), psk, sizeof(psk));
  1493. if (psk_len > PSK_MAX_PSK_LEN) {
  1494. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1495. ERR_R_INTERNAL_ERROR);
  1496. goto err;
  1497. } else if (psk_len == 0) {
  1498. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1499. SSL_R_PSK_IDENTITY_NOT_FOUND);
  1500. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1501. goto err;
  1502. }
  1503. identity_len = OPENSSL_strnlen(identity, sizeof(identity));
  1504. if (identity_len > PSK_MAX_IDENTITY_LEN) {
  1505. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1506. ERR_R_INTERNAL_ERROR);
  1507. goto err;
  1508. }
  1509. if (s->session->psk_identity != NULL) {
  1510. OPENSSL_free(s->session->psk_identity);
  1511. }
  1512. s->session->psk_identity = BUF_strdup(identity);
  1513. if (s->session->psk_identity == NULL) {
  1514. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1515. ERR_R_MALLOC_FAILURE);
  1516. goto err;
  1517. }
  1518. /* Write out psk_identity. */
  1519. s2n(identity_len, p);
  1520. memcpy(p, identity, identity_len);
  1521. p += identity_len;
  1522. n = 2 + identity_len;
  1523. }
  1524. /* Depending on the key exchange method, compute |pms| and |pms_len|. */
  1525. if (alg_k & SSL_kRSA) {
  1526. RSA *rsa;
  1527. size_t enc_pms_len;
  1528. pms_len = SSL_MAX_MASTER_KEY_LENGTH;
  1529. pms = OPENSSL_malloc(pms_len);
  1530. if (pms == NULL) {
  1531. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1532. ERR_R_MALLOC_FAILURE);
  1533. goto err;
  1534. }
  1535. if (s->session->sess_cert == NULL) {
  1536. /* We should always have a server certificate with SSL_kRSA. */
  1537. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1538. ERR_R_INTERNAL_ERROR);
  1539. goto err;
  1540. }
  1541. pkey = X509_get_pubkey(
  1542. s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1543. if (pkey == NULL ||
  1544. pkey->type != EVP_PKEY_RSA ||
  1545. pkey->pkey.rsa == NULL) {
  1546. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1547. ERR_R_INTERNAL_ERROR);
  1548. if (pkey != NULL) {
  1549. EVP_PKEY_free(pkey);
  1550. }
  1551. goto err;
  1552. }
  1553. rsa = pkey->pkey.rsa;
  1554. EVP_PKEY_free(pkey);
  1555. pms[0] = s->client_version >> 8;
  1556. pms[1] = s->client_version & 0xff;
  1557. if (!RAND_bytes(&pms[2], SSL_MAX_MASTER_KEY_LENGTH - 2)) {
  1558. goto err;
  1559. }
  1560. s->session->master_key_length = SSL_MAX_MASTER_KEY_LENGTH;
  1561. q = p;
  1562. /* In TLS and beyond, reserve space for the length prefix. */
  1563. if (s->version > SSL3_VERSION) {
  1564. p += 2;
  1565. n += 2;
  1566. }
  1567. if (!RSA_encrypt(rsa, &enc_pms_len, p, RSA_size(rsa), pms, pms_len,
  1568. RSA_PKCS1_PADDING)) {
  1569. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1570. SSL_R_BAD_RSA_ENCRYPT);
  1571. goto err;
  1572. }
  1573. n += enc_pms_len;
  1574. /* Log the premaster secret, if logging is enabled. */
  1575. if (!ssl_ctx_log_rsa_client_key_exchange(s->ctx, p, enc_pms_len, pms,
  1576. pms_len)) {
  1577. goto err;
  1578. }
  1579. /* Fill in the length prefix. */
  1580. if (s->version > SSL3_VERSION) {
  1581. s2n(enc_pms_len, q);
  1582. }
  1583. } else if (alg_k & SSL_kEDH) {
  1584. DH *dh_srvr, *dh_clnt;
  1585. SESS_CERT *scert = s->session->sess_cert;
  1586. int dh_len;
  1587. size_t pub_len;
  1588. if (scert == NULL) {
  1589. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  1590. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1591. SSL_R_UNEXPECTED_MESSAGE);
  1592. goto err;
  1593. }
  1594. if (scert->peer_dh_tmp == NULL) {
  1595. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1596. ERR_R_INTERNAL_ERROR);
  1597. goto err;
  1598. }
  1599. dh_srvr = scert->peer_dh_tmp;
  1600. /* generate a new random key */
  1601. dh_clnt = DHparams_dup(dh_srvr);
  1602. if (dh_clnt == NULL) {
  1603. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
  1604. goto err;
  1605. }
  1606. if (!DH_generate_key(dh_clnt)) {
  1607. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
  1608. DH_free(dh_clnt);
  1609. goto err;
  1610. }
  1611. pms_len = DH_size(dh_clnt);
  1612. pms = OPENSSL_malloc(pms_len);
  1613. if (pms == NULL) {
  1614. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1615. ERR_R_MALLOC_FAILURE);
  1616. DH_free(dh_clnt);
  1617. goto err;
  1618. }
  1619. dh_len = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
  1620. if (dh_len <= 0) {
  1621. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
  1622. DH_free(dh_clnt);
  1623. goto err;
  1624. }
  1625. pms_len = dh_len;
  1626. /* send off the data */
  1627. pub_len = BN_num_bytes(dh_clnt->pub_key);
  1628. s2n(pub_len, p);
  1629. BN_bn2bin(dh_clnt->pub_key, p);
  1630. n += 2 + pub_len;
  1631. DH_free(dh_clnt);
  1632. } else if (alg_k & SSL_kEECDH) {
  1633. const EC_GROUP *srvr_group = NULL;
  1634. EC_KEY *tkey;
  1635. int field_size = 0, ecdh_len;
  1636. if (s->session->sess_cert == NULL) {
  1637. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  1638. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1639. SSL_R_UNEXPECTED_MESSAGE);
  1640. goto err;
  1641. }
  1642. if (s->session->sess_cert->peer_ecdh_tmp == NULL) {
  1643. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1644. ERR_R_INTERNAL_ERROR);
  1645. goto err;
  1646. }
  1647. tkey = s->session->sess_cert->peer_ecdh_tmp;
  1648. srvr_group = EC_KEY_get0_group(tkey);
  1649. srvr_ecpoint = EC_KEY_get0_public_key(tkey);
  1650. if (srvr_group == NULL || srvr_ecpoint == NULL) {
  1651. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1652. ERR_R_INTERNAL_ERROR);
  1653. goto err;
  1654. }
  1655. clnt_ecdh = EC_KEY_new();
  1656. if (clnt_ecdh == NULL) {
  1657. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1658. ERR_R_MALLOC_FAILURE);
  1659. goto err;
  1660. }
  1661. if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
  1662. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
  1663. goto err;
  1664. }
  1665. /* Generate a new ECDH key pair */
  1666. if (!EC_KEY_generate_key(clnt_ecdh)) {
  1667. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
  1668. goto err;
  1669. }
  1670. field_size = EC_GROUP_get_degree(srvr_group);
  1671. if (field_size <= 0) {
  1672. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
  1673. goto err;
  1674. }
  1675. pms_len = (field_size + 7) / 8;
  1676. pms = OPENSSL_malloc(pms_len);
  1677. if (pms == NULL) {
  1678. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1679. ERR_R_MALLOC_FAILURE);
  1680. goto err;
  1681. }
  1682. ecdh_len = ECDH_compute_key(pms, pms_len, srvr_ecpoint, clnt_ecdh, NULL);
  1683. if (ecdh_len <= 0) {
  1684. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
  1685. goto err;
  1686. }
  1687. pms_len = ecdh_len;
  1688. /* First check the size of encoding and allocate memory accordingly. */
  1689. encoded_pt_len =
  1690. EC_POINT_point2oct(srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
  1691. POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
  1692. encodedPoint =
  1693. (uint8_t *)OPENSSL_malloc(encoded_pt_len * sizeof(uint8_t));
  1694. bn_ctx = BN_CTX_new();
  1695. if (encodedPoint == NULL || bn_ctx == NULL) {
  1696. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1697. ERR_R_MALLOC_FAILURE);
  1698. goto err;
  1699. }
  1700. /* Encode the public key */
  1701. encoded_pt_len = EC_POINT_point2oct(
  1702. srvr_group, EC_KEY_get0_public_key(clnt_ecdh),
  1703. POINT_CONVERSION_UNCOMPRESSED, encodedPoint, encoded_pt_len, bn_ctx);
  1704. *p = encoded_pt_len; /* length of encoded point */
  1705. /* Encoded point will be copied here */
  1706. p += 1;
  1707. n += 1;
  1708. /* copy the point */
  1709. memcpy(p, encodedPoint, encoded_pt_len);
  1710. /* increment n to account for length field */
  1711. n += encoded_pt_len;
  1712. /* Free allocated memory */
  1713. BN_CTX_free(bn_ctx);
  1714. bn_ctx = NULL;
  1715. OPENSSL_free(encodedPoint);
  1716. encodedPoint = NULL;
  1717. EC_KEY_free(clnt_ecdh);
  1718. clnt_ecdh = NULL;
  1719. EVP_PKEY_free(srvr_pub_pkey);
  1720. srvr_pub_pkey = NULL;
  1721. } else if (alg_k & SSL_kPSK) {
  1722. /* For plain PSK, other_secret is a block of 0s with the same length as
  1723. * the pre-shared key. */
  1724. pms_len = psk_len;
  1725. pms = OPENSSL_malloc(pms_len);
  1726. if (pms == NULL) {
  1727. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1728. ERR_R_MALLOC_FAILURE);
  1729. goto err;
  1730. }
  1731. memset(pms, 0, pms_len);
  1732. } else {
  1733. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1734. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1735. ERR_R_INTERNAL_ERROR);
  1736. goto err;
  1737. }
  1738. /* For a PSK cipher suite, other_secret is combined with the pre-shared
  1739. * key. */
  1740. if (alg_a & SSL_aPSK) {
  1741. CBB cbb, child;
  1742. uint8_t *new_pms;
  1743. size_t new_pms_len;
  1744. if (!CBB_init(&cbb, 2 + psk_len + 2 + pms_len)) {
  1745. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1746. ERR_R_MALLOC_FAILURE);
  1747. goto err;
  1748. }
  1749. if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
  1750. !CBB_add_bytes(&child, pms, pms_len) ||
  1751. !CBB_add_u16_length_prefixed(&cbb, &child) ||
  1752. !CBB_add_bytes(&child, psk, psk_len) ||
  1753. !CBB_finish(&cbb, &new_pms, &new_pms_len)) {
  1754. CBB_cleanup(&cbb);
  1755. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange,
  1756. ERR_R_INTERNAL_ERROR);
  1757. goto err;
  1758. }
  1759. OPENSSL_cleanse(pms, pms_len);
  1760. OPENSSL_free(pms);
  1761. pms = new_pms;
  1762. pms_len = new_pms_len;
  1763. }
  1764. /* The message must be added to the finished hash before calculating the
  1765. * master secret. */
  1766. if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
  1767. goto err;
  1768. }
  1769. s->state = SSL3_ST_CW_KEY_EXCH_B;
  1770. s->session->master_key_length = s->enc_method->generate_master_secret(
  1771. s, s->session->master_key, pms, pms_len);
  1772. if (s->session->master_key_length == 0) {
  1773. goto err;
  1774. }
  1775. s->session->extended_master_secret = s->s3->tmp.extended_master_secret;
  1776. OPENSSL_cleanse(pms, pms_len);
  1777. OPENSSL_free(pms);
  1778. }
  1779. /* SSL3_ST_CW_KEY_EXCH_B */
  1780. return s->method->do_write(s);
  1781. err:
  1782. BN_CTX_free(bn_ctx);
  1783. if (encodedPoint != NULL) {
  1784. OPENSSL_free(encodedPoint);
  1785. }
  1786. if (clnt_ecdh != NULL) {
  1787. EC_KEY_free(clnt_ecdh);
  1788. }
  1789. EVP_PKEY_free(srvr_pub_pkey);
  1790. if (pms) {
  1791. OPENSSL_cleanse(pms, pms_len);
  1792. OPENSSL_free(pms);
  1793. }
  1794. return -1;
  1795. }
  1796. int ssl3_send_cert_verify(SSL *s) {
  1797. uint8_t *buf, *p;
  1798. const EVP_MD *md = NULL;
  1799. uint8_t digest[EVP_MAX_MD_SIZE];
  1800. size_t digest_length;
  1801. EVP_PKEY *pkey;
  1802. EVP_PKEY_CTX *pctx = NULL;
  1803. size_t signature_length = 0;
  1804. unsigned long n = 0;
  1805. buf = (uint8_t *)s->init_buf->data;
  1806. if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
  1807. p = ssl_handshake_start(s);
  1808. pkey = s->cert->key->privatekey;
  1809. /* Write out the digest type if needbe. */
  1810. if (SSL_USE_SIGALGS(s)) {
  1811. md = tls1_choose_signing_digest(s, pkey);
  1812. if (!tls12_get_sigandhash(p, pkey, md)) {
  1813. OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_INTERNAL_ERROR);
  1814. goto err;
  1815. }
  1816. p += 2;
  1817. n += 2;
  1818. }
  1819. /* Compute the digest. */
  1820. if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) {
  1821. goto err;
  1822. }
  1823. /* The handshake buffer is no longer necessary. */
  1824. if (s->s3->handshake_buffer &&
  1825. !ssl3_digest_cached_records(s, free_handshake_buffer)) {
  1826. goto err;
  1827. }
  1828. /* Sign the digest. */
  1829. pctx = EVP_PKEY_CTX_new(pkey, NULL);
  1830. if (pctx == NULL) {
  1831. goto err;
  1832. }
  1833. /* Initialize the EVP_PKEY_CTX and determine the size of the signature. */
  1834. if (!EVP_PKEY_sign_init(pctx) || !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
  1835. !EVP_PKEY_sign(pctx, NULL, &signature_length, digest, digest_length)) {
  1836. OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
  1837. goto err;
  1838. }
  1839. if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH) {
  1840. OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, SSL_R_DATA_LENGTH_TOO_LONG);
  1841. goto err;
  1842. }
  1843. if (!EVP_PKEY_sign(pctx, &p[2], &signature_length, digest, digest_length)) {
  1844. OPENSSL_PUT_ERROR(SSL, ssl3_send_cert_verify, ERR_R_EVP_LIB);
  1845. goto err;
  1846. }
  1847. s2n(signature_length, p);
  1848. n += signature_length + 2;
  1849. if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
  1850. goto err;
  1851. }
  1852. s->state = SSL3_ST_CW_CERT_VRFY_B;
  1853. }
  1854. EVP_PKEY_CTX_free(pctx);
  1855. return ssl_do_write(s);
  1856. err:
  1857. EVP_PKEY_CTX_free(pctx);
  1858. return -1;
  1859. }
  1860. /* ssl3_has_client_certificate returns true if a client certificate is
  1861. * configured. */
  1862. static int ssl3_has_client_certificate(SSL *s) {
  1863. return s->cert && s->cert->key->x509 && s->cert->key->privatekey;
  1864. }
  1865. int ssl3_send_client_certificate(SSL *s) {
  1866. X509 *x509 = NULL;
  1867. EVP_PKEY *pkey = NULL;
  1868. int i;
  1869. if (s->state == SSL3_ST_CW_CERT_A) {
  1870. /* Let cert callback update client certificates if required */
  1871. if (s->cert->cert_cb) {
  1872. i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
  1873. if (i < 0) {
  1874. s->rwstate = SSL_X509_LOOKUP;
  1875. return -1;
  1876. }
  1877. if (i == 0) {
  1878. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1879. return 0;
  1880. }
  1881. s->rwstate = SSL_NOTHING;
  1882. }
  1883. if (ssl3_has_client_certificate(s)) {
  1884. s->state = SSL3_ST_CW_CERT_C;
  1885. } else {
  1886. s->state = SSL3_ST_CW_CERT_B;
  1887. }
  1888. }
  1889. /* We need to get a client cert */
  1890. if (s->state == SSL3_ST_CW_CERT_B) {
  1891. /* If we get an error, we need to:
  1892. * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
  1893. * We then get retried later */
  1894. i = ssl_do_client_cert_cb(s, &x509, &pkey);
  1895. if (i < 0) {
  1896. s->rwstate = SSL_X509_LOOKUP;
  1897. return -1;
  1898. }
  1899. s->rwstate = SSL_NOTHING;
  1900. if (i == 1 && pkey != NULL && x509 != NULL) {
  1901. s->state = SSL3_ST_CW_CERT_B;
  1902. if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) {
  1903. i = 0;
  1904. }
  1905. } else if (i == 1) {
  1906. i = 0;
  1907. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_certificate,
  1908. SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
  1909. }
  1910. if (x509 != NULL) {
  1911. X509_free(x509);
  1912. }
  1913. if (pkey != NULL) {
  1914. EVP_PKEY_free(pkey);
  1915. }
  1916. if (i && !ssl3_has_client_certificate(s)) {
  1917. i = 0;
  1918. }
  1919. if (i == 0) {
  1920. if (s->version == SSL3_VERSION) {
  1921. s->s3->tmp.cert_req = 0;
  1922. ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
  1923. return 1;
  1924. } else {
  1925. s->s3->tmp.cert_req = 2;
  1926. }
  1927. }
  1928. /* Ok, we have a cert */
  1929. s->state = SSL3_ST_CW_CERT_C;
  1930. }
  1931. if (s->state == SSL3_ST_CW_CERT_C) {
  1932. CERT_PKEY *cert_pkey = (s->s3->tmp.cert_req == 2) ? NULL : s->cert->key;
  1933. if (!ssl3_output_cert_chain(s, cert_pkey)) {
  1934. return -1;
  1935. }
  1936. }
  1937. /* SSL3_ST_CW_CERT_D */
  1938. return ssl_do_write(s);
  1939. }
  1940. #define has_bits(i, m) (((i) & (m)) == (m))
  1941. int ssl3_check_cert_and_algorithm(SSL *s) {
  1942. int i, idx;
  1943. long alg_k, alg_a;
  1944. EVP_PKEY *pkey = NULL;
  1945. SESS_CERT *sc;
  1946. DH *dh;
  1947. /* we don't have a certificate */
  1948. if (!ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
  1949. return 1;
  1950. }
  1951. alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  1952. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  1953. sc = s->session->sess_cert;
  1954. if (sc == NULL) {
  1955. OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, ERR_R_INTERNAL_ERROR);
  1956. goto err;
  1957. }
  1958. dh = s->session->sess_cert->peer_dh_tmp;
  1959. /* This is the passed certificate */
  1960. idx = sc->peer_cert_type;
  1961. if (idx == SSL_PKEY_ECC) {
  1962. if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) {
  1963. /* check failed */
  1964. OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_BAD_ECC_CERT);
  1965. goto f_err;
  1966. } else {
  1967. return 1;
  1968. }
  1969. } else if (alg_a & SSL_aECDSA) {
  1970. OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
  1971. SSL_R_MISSING_ECDSA_SIGNING_CERT);
  1972. goto f_err;
  1973. }
  1974. pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
  1975. i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
  1976. EVP_PKEY_free(pkey);
  1977. /* Check that we have a certificate if we require one */
  1978. if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
  1979. OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
  1980. SSL_R_MISSING_RSA_SIGNING_CERT);
  1981. goto f_err;
  1982. }
  1983. if ((alg_k & SSL_kRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
  1984. OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm,
  1985. SSL_R_MISSING_RSA_ENCRYPTING_CERT);
  1986. goto f_err;
  1987. }
  1988. if ((alg_k & SSL_kEDH) &&
  1989. !(has_bits(i, EVP_PK_DH | EVP_PKT_EXCH) || dh != NULL)) {
  1990. OPENSSL_PUT_ERROR(SSL, ssl3_check_cert_and_algorithm, SSL_R_MISSING_DH_KEY);
  1991. goto f_err;
  1992. }
  1993. return 1;
  1994. f_err:
  1995. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1996. err:
  1997. return 0;
  1998. }
  1999. int ssl3_send_next_proto(SSL *s) {
  2000. unsigned int len, padding_len;
  2001. uint8_t *d, *p;
  2002. if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
  2003. len = s->next_proto_negotiated_len;
  2004. padding_len = 32 - ((len + 2) % 32);
  2005. d = p = ssl_handshake_start(s);
  2006. *(p++) = len;
  2007. memcpy(p, s->next_proto_negotiated, len);
  2008. p += len;
  2009. *(p++) = padding_len;
  2010. memset(p, 0, padding_len);
  2011. p += padding_len;
  2012. if (!ssl_set_handshake_header(s, SSL3_MT_NEXT_PROTO, p - d)) {
  2013. return -1;
  2014. }
  2015. s->state = SSL3_ST_CW_NEXT_PROTO_B;
  2016. }
  2017. return ssl_do_write(s);
  2018. }
  2019. int ssl3_send_channel_id(SSL *s) {
  2020. uint8_t *d;
  2021. int ret = -1, public_key_len;
  2022. EVP_MD_CTX md_ctx;
  2023. size_t sig_len;
  2024. ECDSA_SIG *sig = NULL;
  2025. uint8_t *public_key = NULL, *derp, *der_sig = NULL;
  2026. if (s->state != SSL3_ST_CW_CHANNEL_ID_A) {
  2027. return ssl_do_write(s);
  2028. }
  2029. if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb) {
  2030. EVP_PKEY *key = NULL;
  2031. s->ctx->channel_id_cb(s, &key);
  2032. if (key != NULL) {
  2033. s->tlsext_channel_id_private = key;
  2034. }
  2035. }
  2036. if (!s->tlsext_channel_id_private) {
  2037. s->rwstate = SSL_CHANNEL_ID_LOOKUP;
  2038. return -1;
  2039. }
  2040. s->rwstate = SSL_NOTHING;
  2041. d = ssl_handshake_start(s);
  2042. if (s->s3->tlsext_channel_id_new) {
  2043. s2n(TLSEXT_TYPE_channel_id_new, d);
  2044. } else {
  2045. s2n(TLSEXT_TYPE_channel_id, d);
  2046. }
  2047. s2n(TLSEXT_CHANNEL_ID_SIZE, d);
  2048. EVP_MD_CTX_init(&md_ctx);
  2049. public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
  2050. if (public_key_len <= 0) {
  2051. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
  2052. SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
  2053. goto err;
  2054. }
  2055. /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a
  2056. * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
  2057. * field elements as 32-byte, big-endian numbers. */
  2058. if (public_key_len != 65) {
  2059. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
  2060. goto err;
  2061. }
  2062. public_key = OPENSSL_malloc(public_key_len);
  2063. if (!public_key) {
  2064. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
  2065. goto err;
  2066. }
  2067. derp = public_key;
  2068. i2d_PublicKey(s->tlsext_channel_id_private, &derp);
  2069. if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
  2070. s->tlsext_channel_id_private) != 1) {
  2071. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
  2072. SSL_R_EVP_DIGESTSIGNINIT_FAILED);
  2073. goto err;
  2074. }
  2075. if (!tls1_channel_id_hash(&md_ctx, s)) {
  2076. goto err;
  2077. }
  2078. if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len)) {
  2079. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
  2080. SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
  2081. goto err;
  2082. }
  2083. der_sig = OPENSSL_malloc(sig_len);
  2084. if (!der_sig) {
  2085. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
  2086. goto err;
  2087. }
  2088. if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len)) {
  2089. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id,
  2090. SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
  2091. goto err;
  2092. }
  2093. derp = der_sig;
  2094. sig = d2i_ECDSA_SIG(NULL, (const uint8_t **)&derp, sig_len);
  2095. if (sig == NULL) {
  2096. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG);
  2097. goto err;
  2098. }
  2099. /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
  2100. memcpy(d, public_key + 1, 64);
  2101. d += 64;
  2102. if (!BN_bn2bin_padded(d, 32, sig->r) ||
  2103. !BN_bn2bin_padded(d + 32, 32, sig->s)) {
  2104. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_INTERNAL_ERROR);
  2105. goto err;
  2106. }
  2107. if (!ssl_set_handshake_header(s, SSL3_MT_ENCRYPTED_EXTENSIONS,
  2108. 2 + 2 + TLSEXT_CHANNEL_ID_SIZE)) {
  2109. goto err;
  2110. }
  2111. s->state = SSL3_ST_CW_CHANNEL_ID_B;
  2112. ret = ssl_do_write(s);
  2113. err:
  2114. EVP_MD_CTX_cleanup(&md_ctx);
  2115. if (public_key) {
  2116. OPENSSL_free(public_key);
  2117. }
  2118. if (der_sig) {
  2119. OPENSSL_free(der_sig);
  2120. }
  2121. if (sig) {
  2122. ECDSA_SIG_free(sig);
  2123. }
  2124. return ret;
  2125. }
  2126. int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) {
  2127. int i = 0;
  2128. if (s->ctx->client_cert_cb) {
  2129. i = s->ctx->client_cert_cb(s, px509, ppkey);
  2130. }
  2131. return i;
  2132. }