25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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