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

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