25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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