Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

2422 rader
74 KiB

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