Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

3093 рядки
82 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 <stdio.h>
  151. #include <openssl/buf.h>
  152. #include <openssl/bytestring.h>
  153. #include <openssl/rand.h>
  154. #include <openssl/obj.h>
  155. #include <openssl/evp.h>
  156. #include <openssl/mem.h>
  157. #include <openssl/md5.h>
  158. #include <openssl/dh.h>
  159. #include <openssl/bn.h>
  160. #include <openssl/engine.h>
  161. #include <openssl/x509.h>
  162. #include "ssl_locl.h"
  163. #include "../crypto/dh/internal.h"
  164. static const SSL_METHOD *ssl3_get_client_method(int ver);
  165. static const SSL_METHOD *ssl3_get_client_method(int ver)
  166. {
  167. if (ver == SSL3_VERSION)
  168. return(SSLv3_client_method());
  169. else
  170. return(NULL);
  171. }
  172. IMPLEMENT_ssl3_meth_func(SSLv3_client_method,
  173. ssl_undefined_function,
  174. ssl3_connect,
  175. ssl3_get_client_method)
  176. int ssl3_connect(SSL *s)
  177. {
  178. BUF_MEM *buf=NULL;
  179. void (*cb)(const SSL *ssl,int type,int val)=NULL;
  180. int ret= -1;
  181. int new_state,state,skip=0;
  182. ERR_clear_error();
  183. ERR_clear_system_error();
  184. if (s->info_callback != NULL)
  185. cb=s->info_callback;
  186. else if (s->ctx->info_callback != NULL)
  187. cb=s->ctx->info_callback;
  188. s->in_handshake++;
  189. if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
  190. for (;;)
  191. {
  192. state=s->state;
  193. switch(s->state)
  194. {
  195. case SSL_ST_RENEGOTIATE:
  196. s->renegotiate=1;
  197. s->state=SSL_ST_CONNECT;
  198. s->ctx->stats.sess_connect_renegotiate++;
  199. /* break */
  200. case SSL_ST_BEFORE:
  201. case SSL_ST_CONNECT:
  202. case SSL_ST_BEFORE|SSL_ST_CONNECT:
  203. case SSL_ST_OK|SSL_ST_CONNECT:
  204. s->server=0;
  205. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
  206. if ((s->version & 0xff00 ) != 0x0300)
  207. {
  208. OPENSSL_PUT_ERROR(SSL, ssl3_connect, ERR_R_INTERNAL_ERROR);
  209. ret = -1;
  210. goto end;
  211. }
  212. /* s->version=SSL3_VERSION; */
  213. s->type=SSL_ST_CONNECT;
  214. if (s->init_buf == NULL)
  215. {
  216. if ((buf=BUF_MEM_new()) == NULL)
  217. {
  218. ret= -1;
  219. goto end;
  220. }
  221. if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
  222. {
  223. ret= -1;
  224. goto end;
  225. }
  226. s->init_buf=buf;
  227. buf=NULL;
  228. }
  229. if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
  230. /* setup buffing BIO */
  231. if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
  232. /* don't push the buffering BIO quite yet */
  233. ssl3_init_finished_mac(s);
  234. s->state=SSL3_ST_CW_CLNT_HELLO_A;
  235. s->ctx->stats.sess_connect++;
  236. s->init_num=0;
  237. break;
  238. case SSL3_ST_CW_CLNT_HELLO_A:
  239. case SSL3_ST_CW_CLNT_HELLO_B:
  240. s->shutdown=0;
  241. ret=ssl3_client_hello(s);
  242. if (ret <= 0) goto end;
  243. s->state=SSL3_ST_CR_SRVR_HELLO_A;
  244. s->init_num=0;
  245. /* turn on buffering for the next lot of output */
  246. if (s->bbio != s->wbio)
  247. s->wbio=BIO_push(s->bbio,s->wbio);
  248. break;
  249. case SSL3_ST_CR_SRVR_HELLO_A:
  250. case SSL3_ST_CR_SRVR_HELLO_B:
  251. ret=ssl3_get_server_hello(s);
  252. if (ret <= 0) goto end;
  253. if (s->hit)
  254. {
  255. s->state=SSL3_ST_CR_FINISHED_A;
  256. if (s->tlsext_ticket_expected)
  257. {
  258. /* receive renewed session ticket */
  259. s->state=SSL3_ST_CR_SESSION_TICKET_A;
  260. }
  261. }
  262. else
  263. {
  264. s->state=SSL3_ST_CR_CERT_A;
  265. }
  266. s->init_num=0;
  267. break;
  268. case SSL3_ST_CR_CERT_A:
  269. case SSL3_ST_CR_CERT_B:
  270. if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher))
  271. {
  272. ret=ssl3_get_server_certificate(s);
  273. if (ret <= 0) goto end;
  274. if (s->tlsext_status_expected)
  275. s->state=SSL3_ST_CR_CERT_STATUS_A;
  276. else
  277. s->state=SSL3_ST_CR_KEY_EXCH_A;
  278. }
  279. else
  280. {
  281. skip = 1;
  282. s->state=SSL3_ST_CR_KEY_EXCH_A;
  283. }
  284. s->init_num=0;
  285. break;
  286. case SSL3_ST_CR_KEY_EXCH_A:
  287. case SSL3_ST_CR_KEY_EXCH_B:
  288. ret=ssl3_get_server_key_exchange(s);
  289. if (ret <= 0) goto end;
  290. s->state=SSL3_ST_CR_CERT_REQ_A;
  291. s->init_num=0;
  292. /* at this point we check that we have the
  293. * required stuff from the server */
  294. if (!ssl3_check_cert_and_algorithm(s))
  295. {
  296. ret= -1;
  297. goto end;
  298. }
  299. break;
  300. case SSL3_ST_CR_CERT_REQ_A:
  301. case SSL3_ST_CR_CERT_REQ_B:
  302. ret=ssl3_get_certificate_request(s);
  303. if (ret <= 0) goto end;
  304. s->state=SSL3_ST_CR_SRVR_DONE_A;
  305. s->init_num=0;
  306. break;
  307. case SSL3_ST_CR_SRVR_DONE_A:
  308. case SSL3_ST_CR_SRVR_DONE_B:
  309. ret=ssl3_get_server_done(s);
  310. if (ret <= 0) goto end;
  311. if (s->s3->tmp.cert_req)
  312. s->state=SSL3_ST_CW_CERT_A;
  313. else
  314. s->state=SSL3_ST_CW_KEY_EXCH_A;
  315. s->init_num=0;
  316. break;
  317. case SSL3_ST_CW_CERT_A:
  318. case SSL3_ST_CW_CERT_B:
  319. case SSL3_ST_CW_CERT_C:
  320. case SSL3_ST_CW_CERT_D:
  321. ret=ssl3_send_client_certificate(s);
  322. if (ret <= 0) goto end;
  323. s->state=SSL3_ST_CW_KEY_EXCH_A;
  324. s->init_num=0;
  325. break;
  326. case SSL3_ST_CW_KEY_EXCH_A:
  327. case SSL3_ST_CW_KEY_EXCH_B:
  328. ret=ssl3_send_client_key_exchange(s);
  329. if (ret <= 0) goto end;
  330. /* EAY EAY EAY need to check for DH fix cert
  331. * sent back */
  332. /* For TLS, cert_req is set to 2, so a cert chain
  333. * of nothing is sent, but no verify packet is sent */
  334. /* XXX: For now, we do not support client
  335. * authentication in ECDH cipher suites with
  336. * ECDH (rather than ECDSA) certificates.
  337. * We need to skip the certificate verify
  338. * message when client's ECDH public key is sent
  339. * inside the client certificate.
  340. */
  341. if (s->s3->tmp.cert_req == 1)
  342. {
  343. s->state=SSL3_ST_CW_CERT_VRFY_A;
  344. }
  345. else
  346. {
  347. s->state=SSL3_ST_CW_CHANGE_A;
  348. s->s3->change_cipher_spec=0;
  349. }
  350. if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
  351. {
  352. s->state=SSL3_ST_CW_CHANGE_A;
  353. s->s3->change_cipher_spec=0;
  354. }
  355. s->init_num=0;
  356. break;
  357. case SSL3_ST_CW_CERT_VRFY_A:
  358. case SSL3_ST_CW_CERT_VRFY_B:
  359. ret=ssl3_send_client_verify(s);
  360. if (ret <= 0) goto end;
  361. s->state=SSL3_ST_CW_CHANGE_A;
  362. s->init_num=0;
  363. s->s3->change_cipher_spec=0;
  364. break;
  365. case SSL3_ST_CW_CHANGE_A:
  366. case SSL3_ST_CW_CHANGE_B:
  367. ret=ssl3_send_change_cipher_spec(s,
  368. SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
  369. if (ret <= 0) goto end;
  370. s->state=SSL3_ST_CW_FINISHED_A;
  371. if (s->s3->tlsext_channel_id_valid)
  372. s->state=SSL3_ST_CW_CHANNEL_ID_A;
  373. # if !defined(OPENSSL_NO_NEXTPROTONEG)
  374. if (s->s3->next_proto_neg_seen)
  375. s->state=SSL3_ST_CW_NEXT_PROTO_A;
  376. # endif
  377. s->init_num=0;
  378. s->session->cipher=s->s3->tmp.new_cipher;
  379. if (!s->method->ssl3_enc->setup_key_block(s))
  380. {
  381. ret= -1;
  382. goto end;
  383. }
  384. if (!s->method->ssl3_enc->change_cipher_state(s,
  385. SSL3_CHANGE_CIPHER_CLIENT_WRITE))
  386. {
  387. ret= -1;
  388. goto end;
  389. }
  390. break;
  391. #if !defined(OPENSSL_NO_NEXTPROTONEG)
  392. case SSL3_ST_CW_NEXT_PROTO_A:
  393. case SSL3_ST_CW_NEXT_PROTO_B:
  394. ret=ssl3_send_next_proto(s);
  395. if (ret <= 0) goto end;
  396. if (s->s3->tlsext_channel_id_valid)
  397. s->state=SSL3_ST_CW_CHANNEL_ID_A;
  398. else
  399. s->state=SSL3_ST_CW_FINISHED_A;
  400. break;
  401. #endif
  402. case SSL3_ST_CW_CHANNEL_ID_A:
  403. case SSL3_ST_CW_CHANNEL_ID_B:
  404. ret=ssl3_send_channel_id(s);
  405. if (ret <= 0) goto end;
  406. s->state=SSL3_ST_CW_FINISHED_A;
  407. break;
  408. case SSL3_ST_CW_FINISHED_A:
  409. case SSL3_ST_CW_FINISHED_B:
  410. ret=ssl3_send_finished(s,
  411. SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
  412. s->method->ssl3_enc->client_finished_label,
  413. s->method->ssl3_enc->client_finished_label_len);
  414. if (ret <= 0) goto end;
  415. s->s3->flags |= SSL3_FLAGS_CCS_OK;
  416. s->state=SSL3_ST_CW_FLUSH;
  417. /* clear flags */
  418. s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
  419. if (s->hit)
  420. {
  421. s->s3->tmp.next_state=SSL_ST_OK;
  422. }
  423. else
  424. {
  425. /* This is a non-resumption handshake. If it
  426. * involves ChannelID, then record the
  427. * handshake hashes at this point in the
  428. * session so that any resumption of this
  429. * session with ChannelID can sign those
  430. * hashes. */
  431. if (s->s3->tlsext_channel_id_new)
  432. {
  433. ret = tls1_record_handshake_hashes_for_channel_id(s);
  434. if (ret <= 0)
  435. goto end;
  436. }
  437. if ((SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHROUGH)
  438. && ssl3_can_cutthrough(s)
  439. && s->s3->previous_server_finished_len == 0 /* no cutthrough on renegotiation (would complicate the state machine) */
  440. )
  441. {
  442. s->s3->tmp.next_state=SSL3_ST_CUTTHROUGH_COMPLETE;
  443. }
  444. else
  445. {
  446. /* Allow NewSessionTicket if ticket expected */
  447. if (s->tlsext_ticket_expected)
  448. s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A;
  449. else
  450. s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
  451. }
  452. }
  453. s->init_num=0;
  454. break;
  455. case SSL3_ST_CR_SESSION_TICKET_A:
  456. case SSL3_ST_CR_SESSION_TICKET_B:
  457. ret=ssl3_get_new_session_ticket(s);
  458. if (ret <= 0) goto end;
  459. s->state=SSL3_ST_CR_FINISHED_A;
  460. s->init_num=0;
  461. break;
  462. case SSL3_ST_CR_CERT_STATUS_A:
  463. case SSL3_ST_CR_CERT_STATUS_B:
  464. ret=ssl3_get_cert_status(s);
  465. if (ret <= 0) goto end;
  466. s->state=SSL3_ST_CR_KEY_EXCH_A;
  467. s->init_num=0;
  468. break;
  469. case SSL3_ST_CR_FINISHED_A:
  470. case SSL3_ST_CR_FINISHED_B:
  471. s->s3->flags |= SSL3_FLAGS_CCS_OK;
  472. ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
  473. SSL3_ST_CR_FINISHED_B);
  474. if (ret <= 0) goto end;
  475. if (s->hit)
  476. s->state=SSL3_ST_CW_CHANGE_A;
  477. else
  478. s->state=SSL_ST_OK;
  479. s->init_num=0;
  480. break;
  481. case SSL3_ST_CW_FLUSH:
  482. s->rwstate=SSL_WRITING;
  483. if (BIO_flush(s->wbio) <= 0)
  484. {
  485. ret= -1;
  486. goto end;
  487. }
  488. s->rwstate=SSL_NOTHING;
  489. s->state=s->s3->tmp.next_state;
  490. break;
  491. case SSL3_ST_CUTTHROUGH_COMPLETE:
  492. /* Allow NewSessionTicket if ticket expected */
  493. if (s->tlsext_ticket_expected)
  494. s->state=SSL3_ST_CR_SESSION_TICKET_A;
  495. else
  496. s->state=SSL3_ST_CR_FINISHED_A;
  497. ssl_free_wbio_buffer(s);
  498. ret = 1;
  499. goto end;
  500. /* break; */
  501. case SSL_ST_OK:
  502. /* clean a few things up */
  503. ssl3_cleanup_key_block(s);
  504. if (s->init_buf != NULL)
  505. {
  506. BUF_MEM_free(s->init_buf);
  507. s->init_buf=NULL;
  508. }
  509. /* If we are not 'joining' the last two packets,
  510. * remove the buffering now */
  511. if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
  512. ssl_free_wbio_buffer(s);
  513. /* else do it later in ssl3_write */
  514. s->init_num=0;
  515. s->renegotiate=0;
  516. s->new_session=0;
  517. ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
  518. if (s->hit) s->ctx->stats.sess_hit++;
  519. ret=1;
  520. /* s->server=0; */
  521. s->handshake_func=ssl3_connect;
  522. s->ctx->stats.sess_connect_good++;
  523. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
  524. goto end;
  525. /* break; */
  526. default:
  527. OPENSSL_PUT_ERROR(SSL, ssl3_connect, SSL_R_UNKNOWN_STATE);
  528. ret= -1;
  529. goto end;
  530. /* break; */
  531. }
  532. /* did we do anything */
  533. if (!s->s3->tmp.reuse_message && !skip)
  534. {
  535. if (s->debug)
  536. {
  537. if ((ret=BIO_flush(s->wbio)) <= 0)
  538. goto end;
  539. }
  540. if ((cb != NULL) && (s->state != state))
  541. {
  542. new_state=s->state;
  543. s->state=state;
  544. cb(s,SSL_CB_CONNECT_LOOP,1);
  545. s->state=new_state;
  546. }
  547. }
  548. skip=0;
  549. }
  550. end:
  551. s->in_handshake--;
  552. if (buf != NULL)
  553. BUF_MEM_free(buf);
  554. if (cb != NULL)
  555. cb(s,SSL_CB_CONNECT_EXIT,ret);
  556. return(ret);
  557. }
  558. int ssl3_client_hello(SSL *s)
  559. {
  560. unsigned char *buf;
  561. unsigned char *p,*d;
  562. int i;
  563. unsigned long l;
  564. buf=(unsigned char *)s->init_buf->data;
  565. if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
  566. {
  567. SSL_SESSION *sess = s->session;
  568. if (sess == NULL ||
  569. sess->ssl_version != s->version ||
  570. !sess->session_id_length ||
  571. sess->not_resumable)
  572. {
  573. if (!ssl_get_new_session(s,0))
  574. goto err;
  575. }
  576. if (s->method->version == DTLS_ANY_VERSION)
  577. {
  578. /* Determine which DTLS version to use */
  579. int options = s->options;
  580. /* If DTLS 1.2 disabled correct the version number */
  581. if (options & SSL_OP_NO_DTLSv1_2)
  582. {
  583. if (tls1_suiteb(s))
  584. {
  585. OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
  586. goto err;
  587. }
  588. /* Disabling all versions is silly: return an
  589. * error.
  590. */
  591. if (options & SSL_OP_NO_DTLSv1)
  592. {
  593. OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, SSL_R_WRONG_SSL_VERSION);
  594. goto err;
  595. }
  596. /* Update method so we don't use any DTLS 1.2
  597. * features.
  598. */
  599. s->method = DTLSv1_client_method();
  600. s->version = DTLS1_VERSION;
  601. }
  602. else
  603. {
  604. /* We only support one version: update method */
  605. if (options & SSL_OP_NO_DTLSv1)
  606. s->method = DTLSv1_2_client_method();
  607. s->version = DTLS1_2_VERSION;
  608. }
  609. s->client_version = s->version;
  610. }
  611. /* else use the pre-loaded session */
  612. p=s->s3->client_random;
  613. /* for DTLS if client_random is initialized, reuse it, we are
  614. * required to use same upon reply to HelloVerify */
  615. if (SSL_IS_DTLS(s))
  616. {
  617. size_t idx;
  618. i = 1;
  619. for (idx=0; idx < sizeof(s->s3->client_random); idx++)
  620. {
  621. if (p[idx])
  622. {
  623. i = 0;
  624. break;
  625. }
  626. }
  627. }
  628. else
  629. i = 1;
  630. if (i)
  631. ssl_fill_hello_random(s, 0, p,
  632. sizeof(s->s3->client_random));
  633. /* Do the message type and length last.
  634. * Note: the final argument to ssl_add_clienthello_tlsext below
  635. * depends on the size of this prefix. */
  636. d=p= ssl_handshake_start(s);
  637. /* version indicates the negotiated version: for example from
  638. * an SSLv2/v3 compatible client hello). The client_version
  639. * field is the maximum version we permit and it is also
  640. * used in RSA encrypted premaster secrets. Some servers can
  641. * choke if we initially report a higher version then
  642. * renegotiate to a lower one in the premaster secret. This
  643. * didn't happen with TLS 1.0 as most servers supported it
  644. * but it can with TLS 1.1 or later if the server only supports
  645. * 1.0.
  646. *
  647. * Possible scenario with previous logic:
  648. * 1. Client hello indicates TLS 1.2
  649. * 2. Server hello says TLS 1.0
  650. * 3. RSA encrypted premaster secret uses 1.2.
  651. * 4. Handhaked proceeds using TLS 1.0.
  652. * 5. Server sends hello request to renegotiate.
  653. * 6. Client hello indicates TLS v1.0 as we now
  654. * know that is maximum server supports.
  655. * 7. Server chokes on RSA encrypted premaster secret
  656. * containing version 1.0.
  657. *
  658. * For interoperability it should be OK to always use the
  659. * maximum version we support in client hello and then rely
  660. * on the checking of version to ensure the servers isn't
  661. * being inconsistent: for example initially negotiating with
  662. * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
  663. * client_version in client hello and not resetting it to
  664. * the negotiated version.
  665. */
  666. #if 0
  667. *(p++)=s->version>>8;
  668. *(p++)=s->version&0xff;
  669. s->client_version=s->version;
  670. #else
  671. *(p++)=s->client_version>>8;
  672. *(p++)=s->client_version&0xff;
  673. #endif
  674. /* Random stuff */
  675. memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
  676. p+=SSL3_RANDOM_SIZE;
  677. /* Session ID */
  678. if (s->new_session)
  679. i=0;
  680. else
  681. i=s->session->session_id_length;
  682. *(p++)=i;
  683. if (i != 0)
  684. {
  685. if (i > (int)sizeof(s->session->session_id))
  686. {
  687. OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, ERR_R_INTERNAL_ERROR);
  688. goto err;
  689. }
  690. memcpy(p,s->session->session_id,i);
  691. p+=i;
  692. }
  693. /* cookie stuff for DTLS */
  694. if (SSL_IS_DTLS(s))
  695. {
  696. if ( s->d1->cookie_len > sizeof(s->d1->cookie))
  697. {
  698. OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, ERR_R_INTERNAL_ERROR);
  699. goto err;
  700. }
  701. *(p++) = s->d1->cookie_len;
  702. memcpy(p, s->d1->cookie, s->d1->cookie_len);
  703. p += s->d1->cookie_len;
  704. }
  705. /* Ciphers supported */
  706. i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2]);
  707. if (i == 0)
  708. {
  709. OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, SSL_R_NO_CIPHERS_AVAILABLE);
  710. goto err;
  711. }
  712. #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
  713. /* Some servers hang if client hello > 256 bytes
  714. * as hack workaround chop number of supported ciphers
  715. * to keep it well below this if we use TLS v1.2
  716. */
  717. if (TLS1_get_version(s) >= TLS1_2_VERSION
  718. && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
  719. i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
  720. #endif
  721. s2n(i,p);
  722. p+=i;
  723. /* COMPRESSION */
  724. *(p++)=1;
  725. *(p++)=0; /* Add the NULL method */
  726. /* TLS extensions*/
  727. if (ssl_prepare_clienthello_tlsext(s) <= 0)
  728. {
  729. OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, SSL_R_CLIENTHELLO_TLSEXT);
  730. goto err;
  731. }
  732. if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH, p-buf)) == NULL)
  733. {
  734. OPENSSL_PUT_ERROR(SSL, ssl3_client_hello, ERR_R_INTERNAL_ERROR);
  735. goto err;
  736. }
  737. l= p-d;
  738. ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l);
  739. s->state=SSL3_ST_CW_CLNT_HELLO_B;
  740. }
  741. /* SSL3_ST_CW_CLNT_HELLO_B */
  742. return ssl_do_write(s);
  743. err:
  744. return(-1);
  745. }
  746. int ssl3_get_server_hello(SSL *s)
  747. {
  748. STACK_OF(SSL_CIPHER) *sk;
  749. const SSL_CIPHER *c;
  750. CERT *ct = s->cert;
  751. int al=SSL_AD_INTERNAL_ERROR,ok;
  752. long n;
  753. CBS server_hello, server_random, session_id;
  754. uint16_t server_version, cipher_suite;
  755. uint8_t compression_method;
  756. /* Hello verify request and/or server hello version may not
  757. * match so set first packet if we're negotiating version.
  758. */
  759. if (SSL_IS_DTLS(s))
  760. s->first_packet = 1;
  761. n=s->method->ssl_get_message(s,
  762. SSL3_ST_CR_SRVR_HELLO_A,
  763. SSL3_ST_CR_SRVR_HELLO_B,
  764. -1,
  765. 20000, /* ?? */
  766. &ok);
  767. if (!ok) return((int)n);
  768. if (SSL_IS_DTLS(s))
  769. {
  770. s->first_packet = 0;
  771. if ( s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST)
  772. {
  773. if ( s->d1->send_cookie == 0)
  774. {
  775. s->s3->tmp.reuse_message = 1;
  776. return 1;
  777. }
  778. else /* already sent a cookie */
  779. {
  780. al=SSL_AD_UNEXPECTED_MESSAGE;
  781. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_MESSAGE_TYPE);
  782. goto f_err;
  783. }
  784. }
  785. }
  786. if ( s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO)
  787. {
  788. al=SSL_AD_UNEXPECTED_MESSAGE;
  789. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_MESSAGE_TYPE);
  790. goto f_err;
  791. }
  792. CBS_init(&server_hello, s->init_msg, n);
  793. if (!CBS_get_u16(&server_hello, &server_version) ||
  794. !CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE) ||
  795. !CBS_get_u8_length_prefixed(&server_hello, &session_id) ||
  796. CBS_len(&session_id) > SSL3_SESSION_ID_SIZE ||
  797. !CBS_get_u16(&server_hello, &cipher_suite) ||
  798. !CBS_get_u8(&server_hello, &compression_method))
  799. {
  800. al = SSL_AD_DECODE_ERROR;
  801. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_DECODE_ERROR);
  802. goto f_err;
  803. }
  804. if (s->method->version == DTLS_ANY_VERSION)
  805. {
  806. /* Work out correct protocol version to use */
  807. int options = s->options;
  808. if (server_version == DTLS1_2_VERSION
  809. && !(options & SSL_OP_NO_DTLSv1_2))
  810. s->method = DTLSv1_2_client_method();
  811. else if (tls1_suiteb(s))
  812. {
  813. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
  814. s->version = server_version;
  815. al = SSL_AD_PROTOCOL_VERSION;
  816. goto f_err;
  817. }
  818. else if (server_version == DTLS1_VERSION
  819. && !(options & SSL_OP_NO_DTLSv1))
  820. s->method = DTLSv1_client_method();
  821. else
  822. {
  823. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
  824. s->version = server_version;
  825. al = SSL_AD_PROTOCOL_VERSION;
  826. goto f_err;
  827. }
  828. s->version = s->client_version = s->method->version;
  829. }
  830. if (server_version != s->version)
  831. {
  832. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_SSL_VERSION);
  833. s->version = (s->version & 0xff00) | (server_version & 0xff);
  834. al = SSL_AD_PROTOCOL_VERSION;
  835. goto f_err;
  836. }
  837. /* Copy over the server random. */
  838. memcpy(s->s3->server_random, CBS_data(&server_random), SSL3_RANDOM_SIZE);
  839. s->hit = 0;
  840. /* check if we want to resume the session based on external pre-shared secret */
  841. if (s->version >= TLS1_VERSION && s->tls_session_secret_cb)
  842. {
  843. SSL_CIPHER *pref_cipher=NULL;
  844. s->session->master_key_length=sizeof(s->session->master_key);
  845. if (s->tls_session_secret_cb(s, s->session->master_key,
  846. &s->session->master_key_length,
  847. NULL, &pref_cipher,
  848. s->tls_session_secret_cb_arg))
  849. {
  850. s->session->cipher = pref_cipher ?
  851. pref_cipher :
  852. ssl3_get_cipher_by_value(cipher_suite);
  853. s->s3->flags |= SSL3_FLAGS_CCS_OK;
  854. s->hit = 1;
  855. }
  856. }
  857. if (!s->hit && CBS_len(&session_id) != 0 &&
  858. CBS_mem_equal(&session_id,
  859. s->session->session_id, s->session->session_id_length))
  860. {
  861. if(s->sid_ctx_length != s->session->sid_ctx_length
  862. || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length))
  863. {
  864. /* actually a client application bug */
  865. al = SSL_AD_ILLEGAL_PARAMETER;
  866. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
  867. goto f_err;
  868. }
  869. s->s3->flags |= SSL3_FLAGS_CCS_OK;
  870. s->hit = 1;
  871. }
  872. /* a miss or crap from the other end */
  873. if (!s->hit)
  874. {
  875. /* If we were trying for session-id reuse, make a new
  876. * SSL_SESSION so we don't stuff up other people */
  877. if (s->session->session_id_length > 0)
  878. {
  879. if (!ssl_get_new_session(s,0))
  880. {
  881. goto f_err;
  882. }
  883. }
  884. /* Note: session_id could be empty. */
  885. s->session->session_id_length = CBS_len(&session_id);
  886. memcpy(s->session->session_id, CBS_data(&session_id), CBS_len(&session_id));
  887. }
  888. c = ssl3_get_cipher_by_value(cipher_suite);
  889. if (c == NULL)
  890. {
  891. /* unknown cipher */
  892. al = SSL_AD_ILLEGAL_PARAMETER;
  893. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNKNOWN_CIPHER_RETURNED);
  894. goto f_err;
  895. }
  896. /* If it is a disabled cipher we didn't send it in client hello,
  897. * so return an error.
  898. */
  899. if (c->algorithm_ssl & ct->mask_ssl ||
  900. c->algorithm_mkey & ct->mask_k ||
  901. c->algorithm_auth & ct->mask_a)
  902. {
  903. al=SSL_AD_ILLEGAL_PARAMETER;
  904. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
  905. goto f_err;
  906. }
  907. sk=ssl_get_ciphers_by_id(s);
  908. if (!sk_SSL_CIPHER_find(sk, NULL, c))
  909. {
  910. /* we did not say we would use this cipher */
  911. al=SSL_AD_ILLEGAL_PARAMETER;
  912. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_WRONG_CIPHER_RETURNED);
  913. goto f_err;
  914. }
  915. /* Depending on the session caching (internal/external), the cipher
  916. and/or cipher_id values may not be set. Make sure that
  917. cipher_id is set and use it for comparison. */
  918. if (s->session->cipher)
  919. s->session->cipher_id = s->session->cipher->id;
  920. if (s->hit && (s->session->cipher_id != c->id))
  921. {
  922. al = SSL_AD_ILLEGAL_PARAMETER;
  923. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
  924. goto f_err;
  925. }
  926. s->s3->tmp.new_cipher=c;
  927. /* Don't digest cached records if no sigalgs: we may need them for
  928. * client authentication.
  929. */
  930. if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s))
  931. goto f_err;
  932. /* Only the NULL compression algorithm is supported. */
  933. if (compression_method != 0)
  934. {
  935. al = SSL_AD_ILLEGAL_PARAMETER;
  936. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  937. goto f_err;
  938. }
  939. /* TLS extensions */
  940. if (!ssl_parse_serverhello_tlsext(s, &server_hello))
  941. {
  942. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_PARSE_TLSEXT);
  943. goto err;
  944. }
  945. /* There should be nothing left over in the record. */
  946. if (CBS_len(&server_hello) != 0)
  947. {
  948. /* wrong packet length */
  949. al=SSL_AD_DECODE_ERROR;
  950. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_hello, SSL_R_BAD_PACKET_LENGTH);
  951. goto f_err;
  952. }
  953. return(1);
  954. f_err:
  955. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  956. err:
  957. return(-1);
  958. }
  959. int ssl3_get_server_certificate(SSL *s)
  960. {
  961. int al,i,ok,ret= -1;
  962. unsigned long n;
  963. X509 *x=NULL;
  964. STACK_OF(X509) *sk=NULL;
  965. SESS_CERT *sc;
  966. EVP_PKEY *pkey=NULL;
  967. CBS cbs, certificate_list;
  968. const uint8_t* data;
  969. n=s->method->ssl_get_message(s,
  970. SSL3_ST_CR_CERT_A,
  971. SSL3_ST_CR_CERT_B,
  972. SSL3_MT_CERTIFICATE,
  973. s->max_cert_list,
  974. &ok);
  975. if (!ok) return((int)n);
  976. CBS_init(&cbs, s->init_msg, n);
  977. if ((sk=sk_X509_new_null()) == NULL)
  978. {
  979. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
  980. goto err;
  981. }
  982. if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list) ||
  983. CBS_len(&cbs) != 0)
  984. {
  985. al = SSL_AD_DECODE_ERROR;
  986. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_LENGTH_MISMATCH);
  987. goto f_err;
  988. }
  989. while (CBS_len(&certificate_list) > 0)
  990. {
  991. CBS certificate;
  992. if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate))
  993. {
  994. al = SSL_AD_DECODE_ERROR;
  995. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERT_LENGTH_MISMATCH);
  996. goto f_err;
  997. }
  998. data = CBS_data(&certificate);
  999. x = d2i_X509(NULL, &data, CBS_len(&certificate));
  1000. if (x == NULL)
  1001. {
  1002. al=SSL_AD_BAD_CERTIFICATE;
  1003. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_ASN1_LIB);
  1004. goto f_err;
  1005. }
  1006. if (!CBS_skip(&certificate, data - CBS_data(&certificate)))
  1007. {
  1008. al = SSL_AD_INTERNAL_ERROR;
  1009. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_INTERNAL_ERROR);
  1010. goto f_err;
  1011. }
  1012. if (CBS_len(&certificate) != 0)
  1013. {
  1014. al = SSL_AD_DECODE_ERROR;
  1015. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERT_LENGTH_MISMATCH);
  1016. goto f_err;
  1017. }
  1018. if (!sk_X509_push(sk,x))
  1019. {
  1020. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_MALLOC_FAILURE);
  1021. goto err;
  1022. }
  1023. x=NULL;
  1024. }
  1025. i=ssl_verify_cert_chain(s,sk);
  1026. if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)
  1027. )
  1028. {
  1029. al=ssl_verify_alarm_type(s->verify_result);
  1030. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_CERTIFICATE_VERIFY_FAILED);
  1031. goto f_err;
  1032. }
  1033. ERR_clear_error(); /* but we keep s->verify_result */
  1034. sc=ssl_sess_cert_new();
  1035. if (sc == NULL) goto err;
  1036. if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert);
  1037. s->session->sess_cert=sc;
  1038. sc->cert_chain=sk;
  1039. /* Inconsistency alert: cert_chain does include the peer's
  1040. * certificate, which we don't include in s3_srvr.c */
  1041. x=sk_X509_value(sk,0);
  1042. sk=NULL;
  1043. /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/
  1044. pkey=X509_get_pubkey(x);
  1045. if ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))
  1046. {
  1047. x=NULL;
  1048. al=SSL3_AL_FATAL;
  1049. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
  1050. goto f_err;
  1051. }
  1052. i=ssl_cert_type(x,pkey);
  1053. if (i < 0)
  1054. {
  1055. x=NULL;
  1056. al=SSL3_AL_FATAL;
  1057. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  1058. goto f_err;
  1059. }
  1060. int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
  1061. if (exp_idx >= 0 && i != exp_idx)
  1062. {
  1063. x=NULL;
  1064. al=SSL_AD_ILLEGAL_PARAMETER;
  1065. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, SSL_R_WRONG_CERTIFICATE_TYPE);
  1066. goto f_err;
  1067. }
  1068. sc->peer_cert_type=i;
  1069. CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
  1070. /* Why would the following ever happen?
  1071. * We just created sc a couple of lines ago. */
  1072. if (sc->peer_pkeys[i].x509 != NULL)
  1073. X509_free(sc->peer_pkeys[i].x509);
  1074. sc->peer_pkeys[i].x509=x;
  1075. sc->peer_key= &(sc->peer_pkeys[i]);
  1076. if (s->session->peer != NULL)
  1077. X509_free(s->session->peer);
  1078. CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
  1079. s->session->peer=x;
  1080. s->session->verify_result = s->verify_result;
  1081. x=NULL;
  1082. ret=1;
  1083. if (0)
  1084. {
  1085. f_err:
  1086. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1087. }
  1088. err:
  1089. EVP_PKEY_free(pkey);
  1090. X509_free(x);
  1091. sk_X509_pop_free(sk,X509_free);
  1092. return(ret);
  1093. }
  1094. int ssl3_get_server_key_exchange(SSL *s)
  1095. {
  1096. EVP_MD_CTX md_ctx;
  1097. int al,ok;
  1098. long n,alg_k,alg_a;
  1099. EVP_PKEY *pkey=NULL;
  1100. const EVP_MD *md = NULL;
  1101. RSA *rsa=NULL;
  1102. #ifndef OPENSSL_NO_DH
  1103. DH *dh=NULL;
  1104. #endif
  1105. #ifndef OPENSSL_NO_ECDH
  1106. EC_KEY *ecdh = NULL;
  1107. BN_CTX *bn_ctx = NULL;
  1108. EC_POINT *srvr_ecpoint = NULL;
  1109. #endif
  1110. CBS server_key_exchange, server_key_exchange_orig, parameter;
  1111. /* use same message size as in ssl3_get_certificate_request()
  1112. * as ServerKeyExchange message may be skipped */
  1113. n=s->method->ssl_get_message(s,
  1114. SSL3_ST_CR_KEY_EXCH_A,
  1115. SSL3_ST_CR_KEY_EXCH_B,
  1116. -1,
  1117. s->max_cert_list,
  1118. &ok);
  1119. if (!ok) return((int)n);
  1120. if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
  1121. {
  1122. if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher))
  1123. {
  1124. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
  1125. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
  1126. return -1;
  1127. }
  1128. #ifndef OPENSSL_NO_PSK
  1129. /* In plain PSK ciphersuite, ServerKeyExchange can be
  1130. omitted if no identity hint is sent. Set
  1131. session->sess_cert anyway to avoid problems
  1132. later.*/
  1133. if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aPSK)
  1134. {
  1135. /* PSK ciphersuites that also send a
  1136. * Certificate would have already initialized
  1137. * |sess_cert|. */
  1138. if (s->session->sess_cert == NULL)
  1139. s->session->sess_cert = ssl_sess_cert_new();
  1140. if (s->session->psk_identity_hint)
  1141. {
  1142. OPENSSL_free(s->session->psk_identity_hint);
  1143. s->session->psk_identity_hint = NULL;
  1144. }
  1145. }
  1146. #endif
  1147. s->s3->tmp.reuse_message=1;
  1148. return(1);
  1149. }
  1150. /* Retain a copy of the original CBS to compute the signature
  1151. * over. */
  1152. CBS_init(&server_key_exchange, s->init_msg, n);
  1153. server_key_exchange_orig = server_key_exchange;
  1154. if (s->session->sess_cert != NULL)
  1155. {
  1156. if (s->session->sess_cert->peer_rsa_tmp != NULL)
  1157. {
  1158. RSA_free(s->session->sess_cert->peer_rsa_tmp);
  1159. s->session->sess_cert->peer_rsa_tmp=NULL;
  1160. }
  1161. #ifndef OPENSSL_NO_DH
  1162. if (s->session->sess_cert->peer_dh_tmp)
  1163. {
  1164. DH_free(s->session->sess_cert->peer_dh_tmp);
  1165. s->session->sess_cert->peer_dh_tmp=NULL;
  1166. }
  1167. #endif
  1168. #ifndef OPENSSL_NO_ECDH
  1169. if (s->session->sess_cert->peer_ecdh_tmp)
  1170. {
  1171. EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
  1172. s->session->sess_cert->peer_ecdh_tmp=NULL;
  1173. }
  1174. #endif
  1175. }
  1176. else
  1177. {
  1178. s->session->sess_cert=ssl_sess_cert_new();
  1179. }
  1180. alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
  1181. alg_a=s->s3->tmp.new_cipher->algorithm_auth;
  1182. EVP_MD_CTX_init(&md_ctx);
  1183. #ifndef OPENSSL_NO_PSK
  1184. if (alg_a & SSL_aPSK)
  1185. {
  1186. CBS psk_identity_hint;
  1187. /* Each of the PSK key exchanges begins with a
  1188. * psk_identity_hint. */
  1189. if (!CBS_get_u16_length_prefixed(&server_key_exchange, &psk_identity_hint))
  1190. {
  1191. al = SSL_AD_DECODE_ERROR;
  1192. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
  1193. goto f_err;
  1194. }
  1195. /* Store PSK identity hint for later use, hint is used in
  1196. * ssl3_send_client_key_exchange. Assume that the maximum
  1197. * length of a PSK identity hint can be as long as the maximum
  1198. * length of a PSK identity. Also do not allow NULL
  1199. * characters; identities are saved as C strings.
  1200. *
  1201. * TODO(davidben): Should invalid hints be ignored? It's a hint
  1202. * rather than a specific identity. */
  1203. if (CBS_len(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN ||
  1204. CBS_contains_zero_byte(&psk_identity_hint))
  1205. {
  1206. al = SSL_AD_HANDSHAKE_FAILURE;
  1207. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DATA_LENGTH_TOO_LONG);
  1208. goto f_err;
  1209. }
  1210. /* Save the identity hint as a C string. */
  1211. if (!CBS_strdup(&psk_identity_hint, &s->session->psk_identity_hint))
  1212. {
  1213. al = SSL_AD_HANDSHAKE_FAILURE;
  1214. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_MALLOC_FAILURE);
  1215. goto f_err;
  1216. }
  1217. }
  1218. #endif /* !OPENSSL_NO_PSK */
  1219. if (0) {}
  1220. else if (alg_k & SSL_kRSA)
  1221. {
  1222. CBS rsa_modulus, rsa_exponent;
  1223. /* TODO(davidben): This was originally for export
  1224. * reasons. Do we still need to support it? */
  1225. if (!CBS_get_u16_length_prefixed(&server_key_exchange, &rsa_modulus) ||
  1226. CBS_len(&rsa_modulus) == 0 ||
  1227. !CBS_get_u16_length_prefixed(&server_key_exchange, &rsa_exponent) ||
  1228. CBS_len(&rsa_exponent) == 0)
  1229. {
  1230. al = SSL_AD_DECODE_ERROR;
  1231. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
  1232. goto f_err;
  1233. }
  1234. if ((rsa=RSA_new()) == NULL)
  1235. {
  1236. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_MALLOC_FAILURE);
  1237. goto err;
  1238. }
  1239. if (!(rsa->n = BN_bin2bn(CBS_data(&rsa_modulus),
  1240. CBS_len(&rsa_modulus), rsa->n)))
  1241. {
  1242. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
  1243. goto err;
  1244. }
  1245. if (!(rsa->e = BN_bin2bn(CBS_data(&rsa_exponent),
  1246. CBS_len(&rsa_exponent), rsa->e)))
  1247. {
  1248. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
  1249. goto err;
  1250. }
  1251. /* this should be because we are using an export cipher */
  1252. if (alg_a & SSL_aRSA)
  1253. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1254. else
  1255. {
  1256. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_INTERNAL_ERROR);
  1257. goto err;
  1258. }
  1259. s->session->sess_cert->peer_rsa_tmp=rsa;
  1260. rsa=NULL;
  1261. }
  1262. #ifndef OPENSSL_NO_DH
  1263. else if (alg_k & SSL_kEDH)
  1264. {
  1265. CBS dh_p, dh_g, dh_Ys;
  1266. if (!CBS_get_u16_length_prefixed(&server_key_exchange, &dh_p) ||
  1267. CBS_len(&dh_p) == 0 ||
  1268. !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_g) ||
  1269. CBS_len(&dh_g) == 0 ||
  1270. !CBS_get_u16_length_prefixed(&server_key_exchange, &dh_Ys) ||
  1271. CBS_len(&dh_Ys) == 0)
  1272. {
  1273. al = SSL_AD_DECODE_ERROR;
  1274. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
  1275. goto f_err;
  1276. }
  1277. if ((dh=DH_new()) == NULL)
  1278. {
  1279. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_DH_LIB);
  1280. goto err;
  1281. }
  1282. if (!(dh->p = BN_bin2bn(CBS_data(&dh_p), CBS_len(&dh_p), NULL)))
  1283. {
  1284. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
  1285. goto err;
  1286. }
  1287. if (!(dh->g=BN_bin2bn(CBS_data(&dh_g), CBS_len(&dh_g), NULL)))
  1288. {
  1289. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
  1290. goto err;
  1291. }
  1292. if (!(dh->pub_key = BN_bin2bn(CBS_data(&dh_Ys), CBS_len(&dh_Ys), NULL)))
  1293. {
  1294. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_BN_LIB);
  1295. goto err;
  1296. }
  1297. if (alg_a & SSL_aRSA)
  1298. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1299. #ifndef OPENSSL_NO_DSA
  1300. else if (alg_a & SSL_aDSS)
  1301. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509);
  1302. #endif
  1303. /* else anonymous DH, so no certificate or pkey. */
  1304. s->session->sess_cert->peer_dh_tmp=dh;
  1305. dh=NULL;
  1306. }
  1307. else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd))
  1308. {
  1309. al=SSL_AD_ILLEGAL_PARAMETER;
  1310. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
  1311. goto f_err;
  1312. }
  1313. #endif /* !OPENSSL_NO_DH */
  1314. #ifndef OPENSSL_NO_ECDH
  1315. else if (alg_k & SSL_kEECDH)
  1316. {
  1317. uint16_t curve_id;
  1318. int curve_nid = 0;
  1319. EC_GROUP *ngroup;
  1320. const EC_GROUP *group;
  1321. CBS point;
  1322. /* Extract elliptic curve parameters and the server's
  1323. * ephemeral ECDH public key. Check curve is one of
  1324. * our preferences, if not server has sent an invalid
  1325. * curve.
  1326. */
  1327. if (!tls1_check_curve(s, &server_key_exchange, &curve_id))
  1328. {
  1329. al = SSL_AD_DECODE_ERROR;
  1330. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_WRONG_CURVE);
  1331. goto f_err;
  1332. }
  1333. if ((curve_nid = tls1_ec_curve_id2nid(curve_id)) == 0)
  1334. {
  1335. al=SSL_AD_INTERNAL_ERROR;
  1336. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
  1337. goto f_err;
  1338. }
  1339. if ((ecdh=EC_KEY_new()) == NULL)
  1340. {
  1341. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_MALLOC_FAILURE);
  1342. goto err;
  1343. }
  1344. ngroup = EC_GROUP_new_by_curve_name(curve_nid);
  1345. if (ngroup == NULL)
  1346. {
  1347. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_EC_LIB);
  1348. goto err;
  1349. }
  1350. if (EC_KEY_set_group(ecdh, ngroup) == 0)
  1351. {
  1352. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_EC_LIB);
  1353. goto err;
  1354. }
  1355. EC_GROUP_free(ngroup);
  1356. group = EC_KEY_get0_group(ecdh);
  1357. if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
  1358. (EC_GROUP_get_degree(group) > 163))
  1359. {
  1360. al=SSL_AD_EXPORT_RESTRICTION;
  1361. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
  1362. goto f_err;
  1363. }
  1364. /* Next, get the encoded ECPoint */
  1365. if (!CBS_get_u8_length_prefixed(&server_key_exchange, &point))
  1366. {
  1367. al = SSL_AD_DECODE_ERROR;
  1368. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
  1369. goto f_err;
  1370. }
  1371. if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
  1372. ((bn_ctx = BN_CTX_new()) == NULL))
  1373. {
  1374. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_MALLOC_FAILURE);
  1375. goto err;
  1376. }
  1377. if (!EC_POINT_oct2point(group, srvr_ecpoint,
  1378. CBS_data(&point), CBS_len(&point), bn_ctx))
  1379. {
  1380. al = SSL_AD_DECODE_ERROR;
  1381. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_ECPOINT);
  1382. goto f_err;
  1383. }
  1384. /* The ECC/TLS specification does not mention
  1385. * the use of DSA to sign ECParameters in the server
  1386. * key exchange message. We do support RSA and ECDSA.
  1387. */
  1388. if (0) ;
  1389. else if (alg_a & SSL_aRSA)
  1390. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1391. #ifndef OPENSSL_NO_ECDSA
  1392. else if (alg_a & SSL_aECDSA)
  1393. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
  1394. #endif
  1395. /* else anonymous ECDH, so no certificate or pkey. */
  1396. EC_KEY_set_public_key(ecdh, srvr_ecpoint);
  1397. s->session->sess_cert->peer_ecdh_tmp=ecdh;
  1398. ecdh=NULL;
  1399. BN_CTX_free(bn_ctx);
  1400. bn_ctx = NULL;
  1401. EC_POINT_free(srvr_ecpoint);
  1402. srvr_ecpoint = NULL;
  1403. }
  1404. #endif /* !OPENSSL_NO_ECDH */
  1405. else if (!(alg_k & SSL_kPSK))
  1406. {
  1407. al=SSL_AD_UNEXPECTED_MESSAGE;
  1408. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
  1409. goto f_err;
  1410. }
  1411. /* At this point, |server_key_exchange| contains the
  1412. * signature, if any, while |server_key_exchange_orig|
  1413. * contains the entire message. From that, derive a CBS
  1414. * containing just the parameter. */
  1415. CBS_init(&parameter, CBS_data(&server_key_exchange_orig),
  1416. CBS_len(&server_key_exchange_orig) -
  1417. CBS_len(&server_key_exchange));
  1418. /* if it was signed, check the signature */
  1419. if (pkey != NULL)
  1420. {
  1421. CBS signature;
  1422. if (SSL_USE_SIGALGS(s))
  1423. {
  1424. if (!tls12_check_peer_sigalg(&md, &al, s, &server_key_exchange, pkey))
  1425. goto f_err;
  1426. }
  1427. else
  1428. md = EVP_sha1();
  1429. /* The last field in |server_key_exchange| is the
  1430. * signature. */
  1431. if (!CBS_get_u16_length_prefixed(&server_key_exchange, &signature) ||
  1432. CBS_len(&server_key_exchange) != 0)
  1433. {
  1434. al = SSL_AD_DECODE_ERROR;
  1435. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_DECODE_ERROR);
  1436. goto f_err;
  1437. }
  1438. if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s))
  1439. {
  1440. int num;
  1441. unsigned char *q, md_buf[EVP_MAX_MD_SIZE*2];
  1442. size_t md_len = 0;
  1443. q=md_buf;
  1444. for (num=2; num > 0; num--)
  1445. {
  1446. unsigned int digest_len;
  1447. EVP_DigestInit_ex(&md_ctx,(num == 2)
  1448. ?s->ctx->md5:s->ctx->sha1, NULL);
  1449. EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1450. EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1451. EVP_DigestUpdate(&md_ctx, CBS_data(&parameter), CBS_len(&parameter));
  1452. EVP_DigestFinal_ex(&md_ctx, q, &digest_len);
  1453. q += digest_len;
  1454. md_len += digest_len;
  1455. }
  1456. if (!RSA_verify(NID_md5_sha1, md_buf, md_len,
  1457. CBS_data(&signature), CBS_len(&signature),
  1458. pkey->pkey.rsa))
  1459. {
  1460. al = SSL_AD_DECRYPT_ERROR;
  1461. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
  1462. goto f_err;
  1463. }
  1464. }
  1465. else
  1466. {
  1467. EVP_VerifyInit_ex(&md_ctx, md, NULL);
  1468. EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1469. EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1470. EVP_VerifyUpdate(&md_ctx, CBS_data(&parameter), CBS_len(&parameter));
  1471. if (EVP_VerifyFinal(&md_ctx, CBS_data(&signature), CBS_len(&signature), pkey) <= 0)
  1472. {
  1473. /* bad signature */
  1474. al=SSL_AD_DECRYPT_ERROR;
  1475. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_BAD_SIGNATURE);
  1476. goto f_err;
  1477. }
  1478. }
  1479. }
  1480. else
  1481. {
  1482. if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher))
  1483. {
  1484. /* Might be wrong key type, check it */
  1485. if (ssl3_check_cert_and_algorithm(s))
  1486. /* Otherwise this shouldn't happen */
  1487. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, ERR_R_INTERNAL_ERROR);
  1488. goto err;
  1489. }
  1490. /* still data left over */
  1491. if (CBS_len(&server_key_exchange) > 0)
  1492. {
  1493. al=SSL_AD_DECODE_ERROR;
  1494. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_key_exchange, SSL_R_EXTRA_DATA_IN_MESSAGE);
  1495. goto f_err;
  1496. }
  1497. }
  1498. EVP_PKEY_free(pkey);
  1499. EVP_MD_CTX_cleanup(&md_ctx);
  1500. return(1);
  1501. f_err:
  1502. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1503. err:
  1504. EVP_PKEY_free(pkey);
  1505. if (rsa != NULL)
  1506. RSA_free(rsa);
  1507. #ifndef OPENSSL_NO_DH
  1508. if (dh != NULL)
  1509. DH_free(dh);
  1510. #endif
  1511. #ifndef OPENSSL_NO_ECDH
  1512. BN_CTX_free(bn_ctx);
  1513. EC_POINT_free(srvr_ecpoint);
  1514. if (ecdh != NULL)
  1515. EC_KEY_free(ecdh);
  1516. #endif
  1517. EVP_MD_CTX_cleanup(&md_ctx);
  1518. return(-1);
  1519. }
  1520. static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b)
  1521. {
  1522. return(X509_NAME_cmp(*a,*b));
  1523. }
  1524. int ssl3_get_certificate_request(SSL *s)
  1525. {
  1526. int ok,ret=0;
  1527. unsigned long n;
  1528. unsigned int i;
  1529. X509_NAME *xn=NULL;
  1530. STACK_OF(X509_NAME) *ca_sk=NULL;
  1531. CBS cbs;
  1532. CBS certificate_types;
  1533. CBS certificate_authorities;
  1534. const uint8_t *data;
  1535. n=s->method->ssl_get_message(s,
  1536. SSL3_ST_CR_CERT_REQ_A,
  1537. SSL3_ST_CR_CERT_REQ_B,
  1538. -1,
  1539. s->max_cert_list,
  1540. &ok);
  1541. if (!ok) return((int)n);
  1542. s->s3->tmp.cert_req=0;
  1543. if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
  1544. {
  1545. s->s3->tmp.reuse_message=1;
  1546. /* If we get here we don't need any cached handshake records
  1547. * as we wont be doing client auth.
  1548. */
  1549. if (s->s3->handshake_buffer)
  1550. {
  1551. if (!ssl3_digest_cached_records(s))
  1552. goto err;
  1553. }
  1554. return(1);
  1555. }
  1556. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
  1557. {
  1558. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
  1559. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_WRONG_MESSAGE_TYPE);
  1560. goto err;
  1561. }
  1562. /* TLS does not like anon-DH with client cert */
  1563. if (s->version > SSL3_VERSION)
  1564. {
  1565. if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL)
  1566. {
  1567. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
  1568. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
  1569. goto err;
  1570. }
  1571. }
  1572. CBS_init(&cbs, s->init_msg, n);
  1573. ca_sk = sk_X509_NAME_new(ca_dn_cmp);
  1574. if (ca_sk == NULL)
  1575. {
  1576. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
  1577. goto err;
  1578. }
  1579. /* get the certificate types */
  1580. if (!CBS_get_u8_length_prefixed(&cbs, &certificate_types))
  1581. {
  1582. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  1583. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
  1584. goto err;
  1585. }
  1586. if (!CBS_stow(&certificate_types,
  1587. &s->s3->tmp.certificate_types,
  1588. &s->s3->tmp.num_certificate_types))
  1589. {
  1590. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  1591. goto err;
  1592. }
  1593. if (SSL_USE_SIGALGS(s))
  1594. {
  1595. CBS supported_signature_algorithms;
  1596. if (!CBS_get_u16_length_prefixed(&cbs, &supported_signature_algorithms))
  1597. {
  1598. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1599. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_DECODE_ERROR);
  1600. goto err;
  1601. }
  1602. /* Clear certificate digests and validity flags */
  1603. for (i = 0; i < SSL_PKEY_NUM; i++)
  1604. {
  1605. s->cert->pkeys[i].digest = NULL;
  1606. s->cert->pkeys[i].valid_flags = 0;
  1607. }
  1608. if (!tls1_process_sigalgs(s,
  1609. CBS_data(&supported_signature_algorithms),
  1610. CBS_len(&supported_signature_algorithms)))
  1611. {
  1612. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1613. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_SIGNATURE_ALGORITHMS_ERROR);
  1614. goto err;
  1615. }
  1616. }
  1617. /* get the CA RDNs */
  1618. if (!CBS_get_u16_length_prefixed(&cbs, &certificate_authorities))
  1619. {
  1620. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1621. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_LENGTH_MISMATCH);
  1622. goto err;
  1623. }
  1624. while (CBS_len(&certificate_authorities) > 0)
  1625. {
  1626. CBS distinguished_name;
  1627. if (!CBS_get_u16_length_prefixed(&certificate_authorities, &distinguished_name))
  1628. {
  1629. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1630. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_CA_DN_TOO_LONG);
  1631. goto err;
  1632. }
  1633. data = CBS_data(&distinguished_name);
  1634. if ((xn=d2i_X509_NAME(NULL, &data, CBS_len(&distinguished_name))) == NULL)
  1635. {
  1636. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1637. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_ASN1_LIB);
  1638. goto err;
  1639. }
  1640. if (!CBS_skip(&distinguished_name, data - CBS_data(&distinguished_name)))
  1641. {
  1642. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1643. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_certificate, ERR_R_INTERNAL_ERROR);
  1644. goto err;
  1645. }
  1646. if (CBS_len(&distinguished_name) != 0)
  1647. {
  1648. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1649. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, SSL_R_CA_DN_LENGTH_MISMATCH);
  1650. goto err;
  1651. }
  1652. if (!sk_X509_NAME_push(ca_sk,xn))
  1653. {
  1654. OPENSSL_PUT_ERROR(SSL, ssl3_get_certificate_request, ERR_R_MALLOC_FAILURE);
  1655. goto err;
  1656. }
  1657. }
  1658. /* we should setup a certificate to return.... */
  1659. s->s3->tmp.cert_req=1;
  1660. if (s->s3->tmp.ca_names != NULL)
  1661. sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
  1662. s->s3->tmp.ca_names=ca_sk;
  1663. ca_sk=NULL;
  1664. ret=1;
  1665. err:
  1666. if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
  1667. return(ret);
  1668. }
  1669. int ssl3_get_new_session_ticket(SSL *s)
  1670. {
  1671. int ok,al,ret=0;
  1672. long n;
  1673. CBS new_session_ticket, ticket;
  1674. n=s->method->ssl_get_message(s,
  1675. SSL3_ST_CR_SESSION_TICKET_A,
  1676. SSL3_ST_CR_SESSION_TICKET_B,
  1677. SSL3_MT_NEWSESSION_TICKET,
  1678. 16384,
  1679. &ok);
  1680. if (!ok)
  1681. return((int)n);
  1682. CBS_init(&new_session_ticket, s->init_msg, n);
  1683. if (!CBS_get_u32(&new_session_ticket, &s->session->tlsext_tick_lifetime_hint) ||
  1684. !CBS_get_u16_length_prefixed(&new_session_ticket, &ticket) ||
  1685. CBS_len(&new_session_ticket) != 0)
  1686. {
  1687. al = SSL_AD_DECODE_ERROR;
  1688. OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, SSL_R_DECODE_ERROR);
  1689. goto f_err;
  1690. }
  1691. if (!CBS_stow(&ticket, &s->session->tlsext_tick, &s->session->tlsext_ticklen))
  1692. {
  1693. OPENSSL_PUT_ERROR(SSL, ssl3_get_new_session_ticket, ERR_R_MALLOC_FAILURE);
  1694. goto err;
  1695. }
  1696. /* There are two ways to detect a resumed ticket sesion.
  1697. * One is to set an appropriate session ID and then the server
  1698. * must return a match in ServerHello. This allows the normal
  1699. * client session ID matching to work and we know much
  1700. * earlier that the ticket has been accepted.
  1701. *
  1702. * The other way is to set zero length session ID when the
  1703. * ticket is presented and rely on the handshake to determine
  1704. * session resumption.
  1705. *
  1706. * We choose the former approach because this fits in with
  1707. * assumptions elsewhere in OpenSSL. The session ID is set
  1708. * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the
  1709. * ticket.
  1710. */
  1711. EVP_Digest(CBS_data(&ticket), CBS_len(&ticket),
  1712. s->session->session_id, &s->session->session_id_length,
  1713. #ifndef OPENSSL_NO_SHA256
  1714. EVP_sha256(), NULL);
  1715. #else
  1716. EVP_sha1(), NULL);
  1717. #endif
  1718. ret=1;
  1719. return(ret);
  1720. f_err:
  1721. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1722. err:
  1723. return(-1);
  1724. }
  1725. int ssl3_get_cert_status(SSL *s)
  1726. {
  1727. int ok, al;
  1728. long n;
  1729. CBS certificate_status, ocsp_response;
  1730. uint8_t status_type;
  1731. size_t resplen;
  1732. n=s->method->ssl_get_message(s,
  1733. SSL3_ST_CR_CERT_STATUS_A,
  1734. SSL3_ST_CR_CERT_STATUS_B,
  1735. SSL3_MT_CERTIFICATE_STATUS,
  1736. 16384,
  1737. &ok);
  1738. if (!ok) return((int)n);
  1739. CBS_init(&certificate_status, s->init_msg, n);
  1740. if (!CBS_get_u8(&certificate_status, &status_type) ||
  1741. status_type != TLSEXT_STATUSTYPE_ocsp ||
  1742. !CBS_get_u24_length_prefixed(&certificate_status, &ocsp_response) ||
  1743. CBS_len(&ocsp_response) == 0 ||
  1744. CBS_len(&certificate_status) != 0)
  1745. {
  1746. al = SSL_AD_DECODE_ERROR;
  1747. OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_DECODE_ERROR);
  1748. goto f_err;
  1749. }
  1750. /* TODO(davidben): Make tlsext_ocsp_resplen a
  1751. * size_t. Currently it uses -1 to signal no response. The
  1752. * spec does not allow ocsp_response to be zero-length, so
  1753. * using 0 should be fine. */
  1754. if (!CBS_stow(&ocsp_response, &s->tlsext_ocsp_resp, &resplen))
  1755. {
  1756. al = SSL_AD_INTERNAL_ERROR;
  1757. OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
  1758. goto f_err;
  1759. }
  1760. s->tlsext_ocsp_resplen = resplen;
  1761. if (s->ctx->tlsext_status_cb)
  1762. {
  1763. int ret;
  1764. ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
  1765. if (ret == 0)
  1766. {
  1767. al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
  1768. OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, SSL_R_INVALID_STATUS_RESPONSE);
  1769. goto f_err;
  1770. }
  1771. if (ret < 0)
  1772. {
  1773. al = SSL_AD_INTERNAL_ERROR;
  1774. OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_status, ERR_R_MALLOC_FAILURE);
  1775. goto f_err;
  1776. }
  1777. }
  1778. return 1;
  1779. f_err:
  1780. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1781. return(-1);
  1782. }
  1783. int ssl3_get_server_done(SSL *s)
  1784. {
  1785. int ok,ret=0;
  1786. long n;
  1787. n=s->method->ssl_get_message(s,
  1788. SSL3_ST_CR_SRVR_DONE_A,
  1789. SSL3_ST_CR_SRVR_DONE_B,
  1790. SSL3_MT_SERVER_DONE,
  1791. 30, /* should be very small, like 0 :-) */
  1792. &ok);
  1793. if (!ok) return((int)n);
  1794. if (n > 0)
  1795. {
  1796. /* should contain no data */
  1797. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
  1798. OPENSSL_PUT_ERROR(SSL, ssl3_get_server_done, SSL_R_LENGTH_MISMATCH);
  1799. return -1;
  1800. }
  1801. ret=1;
  1802. return(ret);
  1803. }
  1804. int ssl3_send_client_key_exchange(SSL *s)
  1805. {
  1806. unsigned char *p;
  1807. int n = 0;
  1808. unsigned long alg_k;
  1809. unsigned long alg_a;
  1810. unsigned char *q;
  1811. EVP_PKEY *pkey=NULL;
  1812. #ifndef OPENSSL_NO_ECDH
  1813. EC_KEY *clnt_ecdh = NULL;
  1814. const EC_POINT *srvr_ecpoint = NULL;
  1815. EVP_PKEY *srvr_pub_pkey = NULL;
  1816. unsigned char *encodedPoint = NULL;
  1817. int encoded_pt_len = 0;
  1818. BN_CTX * bn_ctx = NULL;
  1819. #ifndef OPENSSL_NO_PSK
  1820. unsigned int psk_len = 0;
  1821. unsigned char psk[PSK_MAX_PSK_LEN];
  1822. #endif /* OPENSSL_NO_PSK */
  1823. #endif /* OPENSSL_NO_ECDH */
  1824. if (s->state == SSL3_ST_CW_KEY_EXCH_A)
  1825. {
  1826. p = ssl_handshake_start(s);
  1827. alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
  1828. alg_a=s->s3->tmp.new_cipher->algorithm_auth;
  1829. #ifndef OPENSSL_NO_PSK
  1830. if (alg_a & SSL_aPSK)
  1831. {
  1832. char identity[PSK_MAX_IDENTITY_LEN + 1];
  1833. size_t identity_len;
  1834. unsigned char *t = NULL;
  1835. unsigned char pre_ms[PSK_MAX_PSK_LEN*2+4];
  1836. unsigned int pre_ms_len = 0;
  1837. int psk_err = 1;
  1838. n = 0;
  1839. if (s->psk_client_callback == NULL)
  1840. {
  1841. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_PSK_NO_CLIENT_CB);
  1842. goto err;
  1843. }
  1844. memset(identity, 0, sizeof(identity));
  1845. psk_len = s->psk_client_callback(s, s->session->psk_identity_hint,
  1846. identity, sizeof(identity), psk, sizeof(psk));
  1847. if (psk_len > PSK_MAX_PSK_LEN)
  1848. {
  1849. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
  1850. goto psk_err;
  1851. }
  1852. else if (psk_len == 0)
  1853. {
  1854. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_PSK_IDENTITY_NOT_FOUND);
  1855. goto psk_err;
  1856. }
  1857. identity_len = OPENSSL_strnlen(identity, sizeof(identity));
  1858. if (identity_len > PSK_MAX_IDENTITY_LEN)
  1859. {
  1860. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
  1861. goto psk_err;
  1862. }
  1863. if (!(alg_k & SSL_kEECDH))
  1864. {
  1865. /* Create the shared secret now if we're not using ECDHE-PSK.
  1866. * TODO(davidben): Refactor this logic similarly
  1867. * to ssl3_get_client_key_exchange. */
  1868. pre_ms_len = 2+psk_len+2+psk_len;
  1869. t = pre_ms;
  1870. s2n(psk_len, t);
  1871. memset(t, 0, psk_len);
  1872. t+=psk_len;
  1873. s2n(psk_len, t);
  1874. memcpy(t, psk, psk_len);
  1875. s->session->master_key_length =
  1876. s->method->ssl3_enc->generate_master_secret(s,
  1877. s->session->master_key,
  1878. pre_ms, pre_ms_len);
  1879. s2n(identity_len, p);
  1880. memcpy(p, identity, identity_len);
  1881. n = 2 + identity_len;
  1882. }
  1883. if (s->session->psk_identity != NULL)
  1884. OPENSSL_free(s->session->psk_identity);
  1885. s->session->psk_identity = BUF_strdup(identity);
  1886. if (s->session->psk_identity == NULL)
  1887. {
  1888. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
  1889. goto psk_err;
  1890. }
  1891. psk_err = 0;
  1892. psk_err:
  1893. OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
  1894. OPENSSL_cleanse(pre_ms, sizeof(pre_ms));
  1895. if (psk_err != 0)
  1896. {
  1897. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1898. goto err;
  1899. }
  1900. }
  1901. #endif
  1902. /* Fool emacs indentation */
  1903. if (0) {}
  1904. else if (alg_k & SSL_kRSA)
  1905. {
  1906. RSA *rsa;
  1907. unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
  1908. if (s->session->sess_cert->peer_rsa_tmp != NULL)
  1909. rsa=s->session->sess_cert->peer_rsa_tmp;
  1910. else
  1911. {
  1912. pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
  1913. if ((pkey == NULL) ||
  1914. (pkey->type != EVP_PKEY_RSA) ||
  1915. (pkey->pkey.rsa == NULL))
  1916. {
  1917. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
  1918. goto err;
  1919. }
  1920. rsa=pkey->pkey.rsa;
  1921. EVP_PKEY_free(pkey);
  1922. }
  1923. tmp_buf[0]=s->client_version>>8;
  1924. tmp_buf[1]=s->client_version&0xff;
  1925. if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0)
  1926. goto err;
  1927. s->session->master_key_length=sizeof tmp_buf;
  1928. q=p;
  1929. /* Fix buf for TLS and beyond */
  1930. if (s->version > SSL3_VERSION)
  1931. p+=2;
  1932. n=RSA_public_encrypt(sizeof tmp_buf,
  1933. tmp_buf,p,rsa,RSA_PKCS1_PADDING);
  1934. #ifdef PKCS1_CHECK
  1935. if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
  1936. if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
  1937. #endif
  1938. if (n <= 0)
  1939. {
  1940. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_BAD_RSA_ENCRYPT);
  1941. goto err;
  1942. }
  1943. /* Fix buf for TLS and beyond */
  1944. if (s->version > SSL3_VERSION)
  1945. {
  1946. s2n(n,q);
  1947. n+=2;
  1948. }
  1949. s->session->master_key_length=
  1950. s->method->ssl3_enc->generate_master_secret(s,
  1951. s->session->master_key,
  1952. tmp_buf,sizeof tmp_buf);
  1953. OPENSSL_cleanse(tmp_buf,sizeof tmp_buf);
  1954. }
  1955. #ifndef OPENSSL_NO_DH
  1956. else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  1957. {
  1958. DH *dh_srvr,*dh_clnt;
  1959. SESS_CERT *scert = s->session->sess_cert;
  1960. if (scert == NULL)
  1961. {
  1962. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
  1963. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
  1964. goto err;
  1965. }
  1966. if (scert->peer_dh_tmp != NULL)
  1967. dh_srvr=scert->peer_dh_tmp;
  1968. else
  1969. {
  1970. /* we get them from the cert */
  1971. int idx = scert->peer_cert_type;
  1972. EVP_PKEY *spkey = NULL;
  1973. dh_srvr = NULL;
  1974. if (idx >= 0)
  1975. spkey = X509_get_pubkey(
  1976. scert->peer_pkeys[idx].x509);
  1977. if (spkey)
  1978. {
  1979. dh_srvr = EVP_PKEY_get1_DH(spkey);
  1980. EVP_PKEY_free(spkey);
  1981. }
  1982. if (dh_srvr == NULL)
  1983. {
  1984. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
  1985. goto err;
  1986. }
  1987. }
  1988. if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
  1989. {
  1990. /* Use client certificate key */
  1991. EVP_PKEY *clkey = s->cert->key->privatekey;
  1992. dh_clnt = NULL;
  1993. if (clkey)
  1994. dh_clnt = EVP_PKEY_get1_DH(clkey);
  1995. if (dh_clnt == NULL)
  1996. {
  1997. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
  1998. goto err;
  1999. }
  2000. }
  2001. else
  2002. {
  2003. /* generate a new random key */
  2004. if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
  2005. {
  2006. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
  2007. goto err;
  2008. }
  2009. if (!DH_generate_key(dh_clnt))
  2010. {
  2011. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
  2012. DH_free(dh_clnt);
  2013. goto err;
  2014. }
  2015. }
  2016. /* use the 'p' output buffer for the DH key, but
  2017. * make sure to clear it out afterwards */
  2018. n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
  2019. if (scert->peer_dh_tmp == NULL)
  2020. DH_free(dh_srvr);
  2021. if (n <= 0)
  2022. {
  2023. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_DH_LIB);
  2024. DH_free(dh_clnt);
  2025. goto err;
  2026. }
  2027. /* generate master key from the result */
  2028. s->session->master_key_length=
  2029. s->method->ssl3_enc->generate_master_secret(s,
  2030. s->session->master_key,p,n);
  2031. /* clean up */
  2032. memset(p,0,n);
  2033. if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
  2034. n = 0;
  2035. else
  2036. {
  2037. /* send off the data */
  2038. n=BN_num_bytes(dh_clnt->pub_key);
  2039. s2n(n,p);
  2040. BN_bn2bin(dh_clnt->pub_key,p);
  2041. n+=2;
  2042. }
  2043. DH_free(dh_clnt);
  2044. /* perhaps clean things up a bit EAY EAY EAY EAY*/
  2045. }
  2046. #endif
  2047. #ifndef OPENSSL_NO_ECDH
  2048. else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe))
  2049. {
  2050. const EC_GROUP *srvr_group = NULL;
  2051. EC_KEY *tkey;
  2052. int ecdh_clnt_cert = 0;
  2053. int field_size = 0;
  2054. #ifndef OPENSSL_NO_PSK
  2055. unsigned char *pre_ms;
  2056. unsigned char *t;
  2057. unsigned int pre_ms_len;
  2058. unsigned int i;
  2059. #endif
  2060. if (s->session->sess_cert == NULL)
  2061. {
  2062. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
  2063. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNEXPECTED_MESSAGE);
  2064. goto err;
  2065. }
  2066. /* Did we send out the client's
  2067. * ECDH share for use in premaster
  2068. * computation as part of client certificate?
  2069. * If so, set ecdh_clnt_cert to 1.
  2070. */
  2071. if ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && (s->cert != NULL))
  2072. {
  2073. /* XXX: For now, we do not support client
  2074. * authentication using ECDH certificates.
  2075. * To add such support, one needs to add
  2076. * code that checks for appropriate
  2077. * conditions and sets ecdh_clnt_cert to 1.
  2078. * For example, the cert have an ECC
  2079. * key on the same curve as the server's
  2080. * and the key should be authorized for
  2081. * key agreement.
  2082. *
  2083. * One also needs to add code in ssl3_connect
  2084. * to skip sending the certificate verify
  2085. * message.
  2086. *
  2087. * if ((s->cert->key->privatekey != NULL) &&
  2088. * (s->cert->key->privatekey->type ==
  2089. * EVP_PKEY_EC) && ...)
  2090. * ecdh_clnt_cert = 1;
  2091. */
  2092. }
  2093. if (s->session->sess_cert->peer_ecdh_tmp != NULL)
  2094. {
  2095. tkey = s->session->sess_cert->peer_ecdh_tmp;
  2096. }
  2097. else
  2098. {
  2099. /* Get the Server Public Key from Cert */
  2100. srvr_pub_pkey = X509_get_pubkey(s->session-> \
  2101. sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
  2102. if ((srvr_pub_pkey == NULL) ||
  2103. (srvr_pub_pkey->type != EVP_PKEY_EC) ||
  2104. (srvr_pub_pkey->pkey.ec == NULL))
  2105. {
  2106. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
  2107. goto err;
  2108. }
  2109. tkey = srvr_pub_pkey->pkey.ec;
  2110. }
  2111. srvr_group = EC_KEY_get0_group(tkey);
  2112. srvr_ecpoint = EC_KEY_get0_public_key(tkey);
  2113. if ((srvr_group == NULL) || (srvr_ecpoint == NULL))
  2114. {
  2115. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
  2116. goto err;
  2117. }
  2118. if ((clnt_ecdh=EC_KEY_new()) == NULL)
  2119. {
  2120. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
  2121. goto err;
  2122. }
  2123. if (!EC_KEY_set_group(clnt_ecdh, srvr_group))
  2124. {
  2125. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
  2126. goto err;
  2127. }
  2128. if (ecdh_clnt_cert)
  2129. {
  2130. /* Reuse key info from our certificate
  2131. * We only need our private key to perform
  2132. * the ECDH computation.
  2133. */
  2134. const BIGNUM *priv_key;
  2135. tkey = s->cert->key->privatekey->pkey.ec;
  2136. priv_key = EC_KEY_get0_private_key(tkey);
  2137. if (priv_key == NULL)
  2138. {
  2139. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
  2140. goto err;
  2141. }
  2142. if (!EC_KEY_set_private_key(clnt_ecdh, priv_key))
  2143. {
  2144. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_EC_LIB);
  2145. goto err;
  2146. }
  2147. }
  2148. else
  2149. {
  2150. /* Generate a new ECDH key pair */
  2151. if (!(EC_KEY_generate_key(clnt_ecdh)))
  2152. {
  2153. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
  2154. goto err;
  2155. }
  2156. }
  2157. /* use the 'p' output buffer for the ECDH key, but
  2158. * make sure to clear it out afterwards
  2159. */
  2160. field_size = EC_GROUP_get_degree(srvr_group);
  2161. if (field_size <= 0)
  2162. {
  2163. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
  2164. goto err;
  2165. }
  2166. n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL);
  2167. if (n <= 0)
  2168. {
  2169. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_ECDH_LIB);
  2170. goto err;
  2171. }
  2172. #ifndef OPENSSL_NO_PSK
  2173. /* ECDHE PSK ciphersuites from RFC 5489 */
  2174. if ((alg_a & SSL_aPSK) && psk_len != 0)
  2175. {
  2176. pre_ms_len = 2+psk_len+2+n;
  2177. pre_ms = OPENSSL_malloc(pre_ms_len);
  2178. if (pre_ms == NULL)
  2179. {
  2180. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
  2181. goto err;
  2182. }
  2183. memset(pre_ms, 0, pre_ms_len);
  2184. t = pre_ms;
  2185. s2n(psk_len, t);
  2186. memcpy(t, psk, psk_len);
  2187. t += psk_len;
  2188. s2n(n, t);
  2189. memcpy(t, p, n);
  2190. s->session->master_key_length = s->method->ssl3_enc \
  2191. -> generate_master_secret(s,
  2192. s->session->master_key, pre_ms, pre_ms_len);
  2193. OPENSSL_cleanse(pre_ms, pre_ms_len);
  2194. OPENSSL_free(pre_ms);
  2195. }
  2196. #endif /* OPENSSL_NO_PSK */
  2197. if (!(alg_a & SSL_aPSK))
  2198. {
  2199. /* generate master key from the result */
  2200. s->session->master_key_length = s->method->ssl3_enc \
  2201. -> generate_master_secret(s,
  2202. s->session->master_key, p, n);
  2203. }
  2204. memset(p, 0, n); /* clean up */
  2205. if (ecdh_clnt_cert)
  2206. {
  2207. /* Send empty client key exch message */
  2208. n = 0;
  2209. }
  2210. else
  2211. {
  2212. /* First check the size of encoding and
  2213. * allocate memory accordingly.
  2214. */
  2215. encoded_pt_len =
  2216. EC_POINT_point2oct(srvr_group,
  2217. EC_KEY_get0_public_key(clnt_ecdh),
  2218. POINT_CONVERSION_UNCOMPRESSED,
  2219. NULL, 0, NULL);
  2220. encodedPoint = (unsigned char *)
  2221. OPENSSL_malloc(encoded_pt_len *
  2222. sizeof(unsigned char));
  2223. bn_ctx = BN_CTX_new();
  2224. if ((encodedPoint == NULL) ||
  2225. (bn_ctx == NULL))
  2226. {
  2227. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_MALLOC_FAILURE);
  2228. goto err;
  2229. }
  2230. /* Encode the public key */
  2231. encoded_pt_len = EC_POINT_point2oct(srvr_group,
  2232. EC_KEY_get0_public_key(clnt_ecdh),
  2233. POINT_CONVERSION_UNCOMPRESSED,
  2234. encodedPoint, encoded_pt_len, bn_ctx);
  2235. n = 0;
  2236. #ifndef OPENSSL_NO_PSK
  2237. if ((alg_a & SSL_aPSK) && psk_len != 0)
  2238. {
  2239. i = strlen(s->session->psk_identity);
  2240. s2n(i, p);
  2241. memcpy(p, s->session->psk_identity, i);
  2242. p += i;
  2243. n = i + 2;
  2244. }
  2245. #endif
  2246. *p = encoded_pt_len; /* length of encoded point */
  2247. /* Encoded point will be copied here */
  2248. p += 1;
  2249. n += 1;
  2250. /* copy the point */
  2251. memcpy((unsigned char *)p, encodedPoint, encoded_pt_len);
  2252. /* increment n to account for length field */
  2253. n += encoded_pt_len;
  2254. }
  2255. /* Free allocated memory */
  2256. BN_CTX_free(bn_ctx);
  2257. if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
  2258. if (clnt_ecdh != NULL)
  2259. EC_KEY_free(clnt_ecdh);
  2260. EVP_PKEY_free(srvr_pub_pkey);
  2261. }
  2262. #endif /* !OPENSSL_NO_ECDH */
  2263. else if (!(alg_k & SSL_kPSK) || ((alg_k & SSL_kPSK) && !(alg_a & SSL_aPSK)))
  2264. {
  2265. ssl3_send_alert(s, SSL3_AL_FATAL,
  2266. SSL_AD_HANDSHAKE_FAILURE);
  2267. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
  2268. goto err;
  2269. }
  2270. ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n);
  2271. s->state=SSL3_ST_CW_KEY_EXCH_B;
  2272. }
  2273. /* SSL3_ST_CW_KEY_EXCH_B */
  2274. return ssl_do_write(s);
  2275. err:
  2276. #ifndef OPENSSL_NO_ECDH
  2277. BN_CTX_free(bn_ctx);
  2278. if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
  2279. if (clnt_ecdh != NULL)
  2280. EC_KEY_free(clnt_ecdh);
  2281. EVP_PKEY_free(srvr_pub_pkey);
  2282. #endif
  2283. return(-1);
  2284. }
  2285. int ssl3_send_client_verify(SSL *s)
  2286. {
  2287. unsigned char *buf, *p;
  2288. const EVP_MD *md;
  2289. uint8_t digest[EVP_MAX_MD_SIZE];
  2290. unsigned digest_length;
  2291. EVP_PKEY *pkey;
  2292. EVP_PKEY_CTX *pctx = NULL;
  2293. EVP_MD_CTX mctx;
  2294. size_t signature_length = 0;
  2295. unsigned long n = 0;
  2296. EVP_MD_CTX_init(&mctx);
  2297. buf=(unsigned char *)s->init_buf->data;
  2298. if (s->state == SSL3_ST_CW_CERT_VRFY_A)
  2299. {
  2300. p= ssl_handshake_start(s);
  2301. pkey = s->cert->key->privatekey;
  2302. /* For TLS v1.2 send signature algorithm and signature using
  2303. * agreed digest and cached handshake records. Otherwise, use
  2304. * SHA1 or MD5 + SHA1 depending on key type.
  2305. */
  2306. if (SSL_USE_SIGALGS(s))
  2307. {
  2308. const uint8_t *hdata;
  2309. size_t hdatalen;
  2310. md = s->cert->key->digest;
  2311. if (!BIO_mem_contents(s->s3->handshake_buffer, &hdata, &hdatalen) ||
  2312. !tls12_get_sigandhash(p, pkey, md))
  2313. {
  2314. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_INTERNAL_ERROR);
  2315. goto err;
  2316. }
  2317. p += 2;
  2318. n += 2;
  2319. if (!EVP_DigestInit_ex(&mctx, md, NULL)
  2320. || !EVP_DigestUpdate(&mctx, hdata, hdatalen)
  2321. || !EVP_DigestFinal(&mctx, digest, &digest_length))
  2322. {
  2323. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_EVP_LIB);
  2324. goto err;
  2325. }
  2326. }
  2327. else if (pkey->type == EVP_PKEY_RSA)
  2328. {
  2329. s->method->ssl3_enc->cert_verify_mac(s, NID_md5, digest);
  2330. s->method->ssl3_enc->cert_verify_mac(s,
  2331. NID_sha1, &digest[MD5_DIGEST_LENGTH]);
  2332. digest_length = MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH;
  2333. /* Using a NULL signature MD makes EVP_PKEY_sign perform
  2334. * a raw RSA signature, rather than wrapping in a
  2335. * DigestInfo. */
  2336. md = NULL;
  2337. }
  2338. else if (pkey->type == EVP_PKEY_DSA || pkey->type == EVP_PKEY_EC)
  2339. {
  2340. s->method->ssl3_enc->cert_verify_mac(s, NID_sha1, digest);
  2341. digest_length = SHA_DIGEST_LENGTH;
  2342. md = EVP_sha1();
  2343. }
  2344. else
  2345. {
  2346. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_INTERNAL_ERROR);
  2347. goto err;
  2348. }
  2349. /* Sign the digest. */
  2350. pctx = EVP_PKEY_CTX_new(pkey, NULL);
  2351. if (pctx == NULL)
  2352. goto err;
  2353. /* Initialize the EVP_PKEY_CTX and determine the size of the signature. */
  2354. if (EVP_PKEY_sign_init(pctx) != 1 ||
  2355. EVP_PKEY_CTX_set_signature_md(pctx, md) != 1 ||
  2356. EVP_PKEY_sign(pctx, NULL, &signature_length,
  2357. digest, digest_length) != 1)
  2358. {
  2359. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_EVP_LIB);
  2360. goto err;
  2361. }
  2362. if (p + 2 + signature_length > buf + SSL3_RT_MAX_PLAIN_LENGTH)
  2363. {
  2364. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, SSL_R_DATA_LENGTH_TOO_LONG);
  2365. goto err;
  2366. }
  2367. if (EVP_PKEY_sign(pctx, &p[2], &signature_length,
  2368. digest, digest_length) != 1)
  2369. {
  2370. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_verify, ERR_R_EVP_LIB);
  2371. goto err;
  2372. }
  2373. s2n(signature_length, p);
  2374. n += signature_length + 2;
  2375. /* Now that client auth is completed, we no longer need cached
  2376. * handshake records and can digest them. */
  2377. if (SSL_USE_SIGALGS(s))
  2378. {
  2379. if (!ssl3_digest_cached_records(s))
  2380. goto err;
  2381. }
  2382. ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n);
  2383. s->state=SSL3_ST_CW_CERT_VRFY_B;
  2384. }
  2385. EVP_MD_CTX_cleanup(&mctx);
  2386. EVP_PKEY_CTX_free(pctx);
  2387. return ssl_do_write(s);
  2388. err:
  2389. EVP_MD_CTX_cleanup(&mctx);
  2390. EVP_PKEY_CTX_free(pctx);
  2391. return(-1);
  2392. }
  2393. /* Check a certificate can be used for client authentication. Currently
  2394. * check cert exists, if we have a suitable digest for TLS 1.2 if
  2395. * static DH client certificates can be used and optionally checks
  2396. * suitability for Suite B.
  2397. */
  2398. static int ssl3_check_client_certificate(SSL *s)
  2399. {
  2400. unsigned long alg_k;
  2401. if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
  2402. return 0;
  2403. /* If no suitable signature algorithm can't use certificate */
  2404. if (SSL_USE_SIGALGS(s) && !s->cert->key->digest)
  2405. return 0;
  2406. /* If strict mode check suitability of chain before using it.
  2407. * This also adjusts suite B digest if necessary.
  2408. */
  2409. if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
  2410. !tls1_check_chain(s, NULL, NULL, NULL, -2))
  2411. return 0;
  2412. alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
  2413. /* See if we can use client certificate for fixed DH */
  2414. if (alg_k & (SSL_kDHr|SSL_kDHd))
  2415. {
  2416. SESS_CERT *scert = s->session->sess_cert;
  2417. int i = scert->peer_cert_type;
  2418. EVP_PKEY *clkey = NULL, *spkey = NULL;
  2419. clkey = s->cert->key->privatekey;
  2420. /* If client key not DH assume it can be used */
  2421. if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
  2422. return 1;
  2423. if (i >= 0)
  2424. spkey = X509_get_pubkey(scert->peer_pkeys[i].x509);
  2425. if (spkey)
  2426. {
  2427. /* Compare server and client parameters */
  2428. i = EVP_PKEY_cmp_parameters(clkey, spkey);
  2429. EVP_PKEY_free(spkey);
  2430. if (i != 1)
  2431. return 0;
  2432. }
  2433. s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
  2434. }
  2435. return 1;
  2436. }
  2437. int ssl3_send_client_certificate(SSL *s)
  2438. {
  2439. X509 *x509=NULL;
  2440. EVP_PKEY *pkey=NULL;
  2441. int i;
  2442. if (s->state == SSL3_ST_CW_CERT_A)
  2443. {
  2444. /* Let cert callback update client certificates if required */
  2445. if (s->cert->cert_cb)
  2446. {
  2447. i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
  2448. if (i < 0)
  2449. {
  2450. s->rwstate=SSL_X509_LOOKUP;
  2451. return -1;
  2452. }
  2453. if (i == 0)
  2454. {
  2455. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_INTERNAL_ERROR);
  2456. return 0;
  2457. }
  2458. s->rwstate=SSL_NOTHING;
  2459. }
  2460. if (ssl3_check_client_certificate(s))
  2461. s->state=SSL3_ST_CW_CERT_C;
  2462. else
  2463. s->state=SSL3_ST_CW_CERT_B;
  2464. }
  2465. /* We need to get a client cert */
  2466. if (s->state == SSL3_ST_CW_CERT_B)
  2467. {
  2468. /* If we get an error, we need to
  2469. * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
  2470. * We then get retied later */
  2471. i=0;
  2472. i = ssl_do_client_cert_cb(s, &x509, &pkey);
  2473. if (i < 0)
  2474. {
  2475. s->rwstate=SSL_X509_LOOKUP;
  2476. return(-1);
  2477. }
  2478. s->rwstate=SSL_NOTHING;
  2479. if ((i == 1) && (pkey != NULL) && (x509 != NULL))
  2480. {
  2481. s->state=SSL3_ST_CW_CERT_B;
  2482. if ( !SSL_use_certificate(s,x509) ||
  2483. !SSL_use_PrivateKey(s,pkey))
  2484. i=0;
  2485. }
  2486. else if (i == 1)
  2487. {
  2488. i=0;
  2489. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
  2490. }
  2491. if (x509 != NULL) X509_free(x509);
  2492. if (pkey != NULL) EVP_PKEY_free(pkey);
  2493. if (i && !ssl3_check_client_certificate(s))
  2494. i = 0;
  2495. if (i == 0)
  2496. {
  2497. if (s->version == SSL3_VERSION)
  2498. {
  2499. s->s3->tmp.cert_req=0;
  2500. ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
  2501. return(1);
  2502. }
  2503. else
  2504. {
  2505. s->s3->tmp.cert_req=2;
  2506. }
  2507. }
  2508. /* Ok, we have a cert */
  2509. s->state=SSL3_ST_CW_CERT_C;
  2510. }
  2511. if (s->state == SSL3_ST_CW_CERT_C)
  2512. {
  2513. s->state=SSL3_ST_CW_CERT_D;
  2514. ssl3_output_cert_chain(s,
  2515. (s->s3->tmp.cert_req == 2)?NULL:s->cert->key);
  2516. }
  2517. /* SSL3_ST_CW_CERT_D */
  2518. return ssl_do_write(s);
  2519. }
  2520. #define has_bits(i,m) (((i)&(m)) == (m))
  2521. int ssl3_check_cert_and_algorithm(SSL *s)
  2522. {
  2523. int i,idx;
  2524. long alg_k,alg_a;
  2525. EVP_PKEY *pkey=NULL;
  2526. SESS_CERT *sc;
  2527. RSA *rsa;
  2528. #ifndef OPENSSL_NO_DH
  2529. DH *dh;
  2530. #endif
  2531. /* we don't have a certificate */
  2532. if (!ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher))
  2533. return 1;
  2534. alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
  2535. alg_a=s->s3->tmp.new_cipher->algorithm_auth;
  2536. sc=s->session->sess_cert;
  2537. if (sc == NULL)
  2538. {
  2539. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, ERR_R_INTERNAL_ERROR);
  2540. goto err;
  2541. }
  2542. rsa=s->session->sess_cert->peer_rsa_tmp;
  2543. #ifndef OPENSSL_NO_DH
  2544. dh=s->session->sess_cert->peer_dh_tmp;
  2545. #endif
  2546. /* This is the passed certificate */
  2547. idx=sc->peer_cert_type;
  2548. #ifndef OPENSSL_NO_ECDH
  2549. if (idx == SSL_PKEY_ECC)
  2550. {
  2551. if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509,
  2552. s) == 0)
  2553. { /* check failed */
  2554. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_BAD_ECC_CERT);
  2555. goto f_err;
  2556. }
  2557. else
  2558. {
  2559. return 1;
  2560. }
  2561. }
  2562. else if (alg_a & SSL_aECDSA)
  2563. {
  2564. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_ECDSA_SIGNING_CERT);
  2565. goto f_err;
  2566. }
  2567. else if (alg_k & (SSL_kECDHr|SSL_kECDHe))
  2568. {
  2569. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_ECDH_CERT);
  2570. goto f_err;
  2571. }
  2572. #endif
  2573. pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509);
  2574. i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey);
  2575. EVP_PKEY_free(pkey);
  2576. /* Check that we have a certificate if we require one */
  2577. if ((alg_a & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
  2578. {
  2579. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_RSA_SIGNING_CERT);
  2580. goto f_err;
  2581. }
  2582. #ifndef OPENSSL_NO_DSA
  2583. else if ((alg_a & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
  2584. {
  2585. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DSA_SIGNING_CERT);
  2586. goto f_err;
  2587. }
  2588. #endif
  2589. if ((alg_k & SSL_kRSA) &&
  2590. !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
  2591. {
  2592. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_RSA_ENCRYPTING_CERT);
  2593. goto f_err;
  2594. }
  2595. #ifndef OPENSSL_NO_DH
  2596. if ((alg_k & SSL_kEDH) &&
  2597. !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
  2598. {
  2599. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DH_KEY);
  2600. goto f_err;
  2601. }
  2602. else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
  2603. !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
  2604. {
  2605. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DH_RSA_CERT);
  2606. goto f_err;
  2607. }
  2608. #ifndef OPENSSL_NO_DSA
  2609. else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
  2610. !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
  2611. {
  2612. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_DH_DSA_CERT);
  2613. goto f_err;
  2614. }
  2615. #endif
  2616. #endif
  2617. if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP))
  2618. {
  2619. if (alg_k & SSL_kRSA)
  2620. {
  2621. if (rsa == NULL
  2622. || RSA_size(rsa)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
  2623. {
  2624. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
  2625. goto f_err;
  2626. }
  2627. }
  2628. else
  2629. #ifndef OPENSSL_NO_DH
  2630. if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
  2631. {
  2632. if (dh == NULL
  2633. || DH_size(dh)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
  2634. {
  2635. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_MISSING_EXPORT_TMP_DH_KEY);
  2636. goto f_err;
  2637. }
  2638. }
  2639. else
  2640. #endif
  2641. {
  2642. OPENSSL_PUT_ERROR(SSL, ssl3_send_client_key_exchange, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
  2643. goto f_err;
  2644. }
  2645. }
  2646. return(1);
  2647. f_err:
  2648. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  2649. err:
  2650. return(0);
  2651. }
  2652. # if !defined(OPENSSL_NO_NEXTPROTONEG)
  2653. int ssl3_send_next_proto(SSL *s)
  2654. {
  2655. unsigned int len, padding_len;
  2656. unsigned char *d;
  2657. if (s->state == SSL3_ST_CW_NEXT_PROTO_A)
  2658. {
  2659. len = s->next_proto_negotiated_len;
  2660. padding_len = 32 - ((len + 2) % 32);
  2661. d = (unsigned char *)s->init_buf->data;
  2662. d[4] = len;
  2663. memcpy(d + 5, s->next_proto_negotiated, len);
  2664. d[5 + len] = padding_len;
  2665. memset(d + 6 + len, 0, padding_len);
  2666. *(d++)=SSL3_MT_NEXT_PROTO;
  2667. l2n3(2 + len + padding_len, d);
  2668. s->state = SSL3_ST_CW_NEXT_PROTO_B;
  2669. s->init_num = 4 + 2 + len + padding_len;
  2670. s->init_off = 0;
  2671. }
  2672. return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
  2673. }
  2674. # endif /* !OPENSSL_NO_NEXTPROTONEG */
  2675. int ssl3_send_channel_id(SSL *s)
  2676. {
  2677. unsigned char *d;
  2678. int ret = -1, public_key_len;
  2679. EVP_MD_CTX md_ctx;
  2680. size_t sig_len;
  2681. ECDSA_SIG *sig = NULL;
  2682. unsigned char *public_key = NULL, *derp, *der_sig = NULL;
  2683. if (s->state != SSL3_ST_CW_CHANNEL_ID_A)
  2684. return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
  2685. if (!s->tlsext_channel_id_private && s->ctx->channel_id_cb)
  2686. {
  2687. EVP_PKEY *key = NULL;
  2688. s->ctx->channel_id_cb(s, &key);
  2689. if (key != NULL)
  2690. {
  2691. s->tlsext_channel_id_private = key;
  2692. }
  2693. }
  2694. if (!s->tlsext_channel_id_private)
  2695. {
  2696. s->rwstate=SSL_CHANNEL_ID_LOOKUP;
  2697. return (-1);
  2698. }
  2699. s->rwstate=SSL_NOTHING;
  2700. d = (unsigned char *)s->init_buf->data;
  2701. *(d++)=SSL3_MT_ENCRYPTED_EXTENSIONS;
  2702. l2n3(2 + 2 + TLSEXT_CHANNEL_ID_SIZE, d);
  2703. if (s->s3->tlsext_channel_id_new)
  2704. s2n(TLSEXT_TYPE_channel_id_new, d);
  2705. else
  2706. s2n(TLSEXT_TYPE_channel_id, d);
  2707. s2n(TLSEXT_CHANNEL_ID_SIZE, d);
  2708. EVP_MD_CTX_init(&md_ctx);
  2709. public_key_len = i2d_PublicKey(s->tlsext_channel_id_private, NULL);
  2710. if (public_key_len <= 0)
  2711. {
  2712. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CANNOT_SERIALIZE_PUBLIC_KEY);
  2713. goto err;
  2714. }
  2715. /* i2d_PublicKey will produce an ANSI X9.62 public key which, for a
  2716. * P-256 key, is 0x04 (meaning uncompressed) followed by the x and y
  2717. * field elements as 32-byte, big-endian numbers. */
  2718. if (public_key_len != 65)
  2719. {
  2720. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_CHANNEL_ID_NOT_P256);
  2721. goto err;
  2722. }
  2723. public_key = OPENSSL_malloc(public_key_len);
  2724. if (!public_key)
  2725. {
  2726. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
  2727. goto err;
  2728. }
  2729. derp = public_key;
  2730. i2d_PublicKey(s->tlsext_channel_id_private, &derp);
  2731. if (EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL,
  2732. s->tlsext_channel_id_private) != 1)
  2733. {
  2734. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNINIT_FAILED);
  2735. goto err;
  2736. }
  2737. if (!tls1_channel_id_hash(&md_ctx, s))
  2738. goto err;
  2739. if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len))
  2740. {
  2741. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
  2742. goto err;
  2743. }
  2744. der_sig = OPENSSL_malloc(sig_len);
  2745. if (!der_sig)
  2746. {
  2747. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, ERR_R_MALLOC_FAILURE);
  2748. goto err;
  2749. }
  2750. if (!EVP_DigestSignFinal(&md_ctx, der_sig, &sig_len))
  2751. {
  2752. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_EVP_DIGESTSIGNFINAL_FAILED);
  2753. goto err;
  2754. }
  2755. derp = der_sig;
  2756. sig = d2i_ECDSA_SIG(NULL, (const unsigned char**) &derp, sig_len);
  2757. if (sig == NULL)
  2758. {
  2759. OPENSSL_PUT_ERROR(SSL, ssl3_send_channel_id, SSL_R_D2I_ECDSA_SIG);
  2760. goto err;
  2761. }
  2762. /* The first byte of public_key will be 0x4, denoting an uncompressed key. */
  2763. memcpy(d, public_key + 1, 64);
  2764. d += 64;
  2765. memset(d, 0, 2 * 32);
  2766. BN_bn2bin(sig->r, d + 32 - BN_num_bytes(sig->r));
  2767. d += 32;
  2768. BN_bn2bin(sig->s, d + 32 - BN_num_bytes(sig->s));
  2769. d += 32;
  2770. s->state = SSL3_ST_CW_CHANNEL_ID_B;
  2771. s->init_num = 4 + 2 + 2 + TLSEXT_CHANNEL_ID_SIZE;
  2772. s->init_off = 0;
  2773. ret = ssl3_do_write(s, SSL3_RT_HANDSHAKE);
  2774. err:
  2775. EVP_MD_CTX_cleanup(&md_ctx);
  2776. if (public_key)
  2777. OPENSSL_free(public_key);
  2778. if (der_sig)
  2779. OPENSSL_free(der_sig);
  2780. if (sig)
  2781. ECDSA_SIG_free(sig);
  2782. return ret;
  2783. }
  2784. int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
  2785. {
  2786. int i = 0;
  2787. /* TODO(fork): remove */
  2788. #if 0
  2789. #ifndef OPENSSL_NO_ENGINE
  2790. if (s->ctx->client_cert_engine)
  2791. {
  2792. i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
  2793. SSL_get_client_CA_list(s),
  2794. px509, ppkey, NULL, NULL, NULL);
  2795. if (i != 0)
  2796. return i;
  2797. }
  2798. #endif
  2799. #endif
  2800. if (s->ctx->client_cert_cb)
  2801. i = s->ctx->client_cert_cb(s,px509,ppkey);
  2802. return i;
  2803. }