You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2892 lines
78 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com).
  108. *
  109. */
  110. /* ====================================================================
  111. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  112. *
  113. * Portions of the attached software ("Contribution") are developed by
  114. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  115. *
  116. * The Contribution is licensed pursuant to the OpenSSL open source
  117. * license provided above.
  118. *
  119. * ECC cipher suite support in OpenSSL originally written by
  120. * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
  121. *
  122. */
  123. /* ====================================================================
  124. * Copyright 2005 Nokia. All rights reserved.
  125. *
  126. * The portions of the attached software ("Contribution") is developed by
  127. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  128. * license.
  129. *
  130. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  131. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  132. * support (see RFC 4279) to OpenSSL.
  133. *
  134. * No patent licenses or other rights except those expressly stated in
  135. * the OpenSSL open source license shall be deemed granted or received
  136. * expressly, by implication, estoppel, or otherwise.
  137. *
  138. * No assurances are provided by Nokia that the Contribution does not
  139. * infringe the patent or other intellectual property rights of any third
  140. * party or that the license provides you with all the necessary rights
  141. * to make use of the Contribution.
  142. *
  143. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  144. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  145. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  146. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  147. * OTHERWISE. */
  148. #define NETSCAPE_HANG_BUG
  149. #include <assert.h>
  150. #include <stdio.h>
  151. #include <string.h>
  152. #include <openssl/bn.h>
  153. #include <openssl/buf.h>
  154. #include <openssl/bytestring.h>
  155. #include <openssl/cipher.h>
  156. #include <openssl/dh.h>
  157. #include <openssl/ec.h>
  158. #include <openssl/ecdsa.h>
  159. #include <openssl/evp.h>
  160. #include <openssl/hmac.h>
  161. #include <openssl/md5.h>
  162. #include <openssl/mem.h>
  163. #include <openssl/obj.h>
  164. #include <openssl/rand.h>
  165. #include <openssl/sha.h>
  166. #include <openssl/x509.h>
  167. #include "ssl_locl.h"
  168. #include "../crypto/dh/internal.h"
  169. static const SSL_METHOD *ssl3_get_server_method(int ver)
  170. {
  171. switch (ver)
  172. {
  173. case TLS1_2_VERSION:
  174. return TLSv1_2_server_method();
  175. case TLS1_1_VERSION:
  176. return TLSv1_1_server_method();
  177. case TLS1_VERSION:
  178. return TLSv1_server_method();
  179. case SSL3_VERSION:
  180. return SSLv3_server_method();
  181. default:
  182. return NULL;
  183. }
  184. }
  185. IMPLEMENT_tls_meth_func(TLS1_2_VERSION, TLSv1_2_server_method,
  186. ssl3_accept,
  187. ssl_undefined_function,
  188. ssl3_get_server_method,
  189. TLSv1_2_enc_data)
  190. IMPLEMENT_tls_meth_func(TLS1_1_VERSION, TLSv1_1_server_method,
  191. ssl3_accept,
  192. ssl_undefined_function,
  193. ssl3_get_server_method,
  194. TLSv1_1_enc_data)
  195. IMPLEMENT_tls_meth_func(TLS1_VERSION, TLSv1_server_method,
  196. ssl3_accept,
  197. ssl_undefined_function,
  198. ssl3_get_server_method,
  199. TLSv1_enc_data)
  200. IMPLEMENT_tls_meth_func(SSL3_VERSION, SSLv3_server_method,
  201. ssl3_accept,
  202. ssl_undefined_function,
  203. ssl3_get_server_method,
  204. SSLv3_enc_data)
  205. int ssl3_accept(SSL *s)
  206. {
  207. BUF_MEM *buf;
  208. unsigned long alg_a;
  209. void (*cb)(const SSL *ssl,int type,int val)=NULL;
  210. int ret= -1;
  211. int new_state,state,skip=0;
  212. ERR_clear_error();
  213. ERR_clear_system_error();
  214. if (s->info_callback != NULL)
  215. cb=s->info_callback;
  216. else if (s->ctx->info_callback != NULL)
  217. cb=s->ctx->info_callback;
  218. /* init things to blank */
  219. s->in_handshake++;
  220. if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
  221. if (s->cert == NULL)
  222. {
  223. OPENSSL_PUT_ERROR(SSL, ssl3_accept, SSL_R_NO_CERTIFICATE_SET);
  224. return(-1);
  225. }
  226. for (;;)
  227. {
  228. state=s->state;
  229. switch (s->state)
  230. {
  231. case SSL_ST_RENEGOTIATE:
  232. s->renegotiate=1;
  233. /* s->state=SSL_ST_ACCEPT; */
  234. case SSL_ST_BEFORE:
  235. case SSL_ST_ACCEPT:
  236. case SSL_ST_BEFORE|SSL_ST_ACCEPT:
  237. case SSL_ST_OK|SSL_ST_ACCEPT:
  238. s->server=1;
  239. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
  240. if ((s->version>>8) != 3)
  241. {
  242. OPENSSL_PUT_ERROR(SSL, ssl3_accept, ERR_R_INTERNAL_ERROR);
  243. return -1;
  244. }
  245. s->type=SSL_ST_ACCEPT;
  246. if (s->init_buf == NULL)
  247. {
  248. if ((buf=BUF_MEM_new()) == NULL)
  249. {
  250. ret= -1;
  251. goto end;
  252. }
  253. if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
  254. {
  255. ret= -1;
  256. goto end;
  257. }
  258. s->init_buf=buf;
  259. }
  260. if (!ssl3_setup_buffers(s))
  261. {
  262. ret= -1;
  263. goto end;
  264. }
  265. s->init_num=0;
  266. if (s->state != SSL_ST_RENEGOTIATE)
  267. {
  268. /* Ok, we now need to push on a buffering BIO so that
  269. * the output is sent in a way that TCP likes :-)
  270. */
  271. if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
  272. ssl3_init_finished_mac(s);
  273. s->state=SSL3_ST_SR_CLNT_HELLO_A;
  274. s->ctx->stats.sess_accept++;
  275. }
  276. else if (!s->s3->send_connection_binding &&
  277. !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION))
  278. {
  279. /* Server attempting to renegotiate with
  280. * client that doesn't support secure
  281. * renegotiation.
  282. */
  283. OPENSSL_PUT_ERROR(SSL, ssl3_accept, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
  284. ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
  285. ret = -1;
  286. goto end;
  287. }
  288. else
  289. {
  290. /* s->state == SSL_ST_RENEGOTIATE,
  291. * we will just send a HelloRequest */
  292. s->ctx->stats.sess_accept_renegotiate++;
  293. s->state=SSL3_ST_SW_HELLO_REQ_A;
  294. }
  295. break;
  296. case SSL3_ST_SW_HELLO_REQ_A:
  297. case SSL3_ST_SW_HELLO_REQ_B:
  298. s->shutdown=0;
  299. ret=ssl3_send_hello_request(s);
  300. if (ret <= 0) goto end;
  301. s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;
  302. s->state=SSL3_ST_SW_FLUSH;
  303. s->init_num=0;
  304. ssl3_init_finished_mac(s);
  305. break;
  306. case SSL3_ST_SW_HELLO_REQ_C:
  307. s->state=SSL_ST_OK;
  308. break;
  309. case SSL3_ST_SR_CLNT_HELLO_A:
  310. case SSL3_ST_SR_CLNT_HELLO_B:
  311. case SSL3_ST_SR_CLNT_HELLO_C:
  312. case SSL3_ST_SR_CLNT_HELLO_D:
  313. s->shutdown=0;
  314. ret=ssl3_get_client_hello(s);
  315. if (ret == PENDING_SESSION) {
  316. s->rwstate = SSL_PENDING_SESSION;
  317. goto end;
  318. }
  319. if (ret == CERTIFICATE_SELECTION_PENDING)
  320. {
  321. s->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
  322. goto end;
  323. }
  324. if (ret <= 0) goto end;
  325. s->renegotiate = 2;
  326. s->state=SSL3_ST_SW_SRVR_HELLO_A;
  327. s->init_num=0;
  328. break;
  329. case SSL3_ST_SW_SRVR_HELLO_A:
  330. case SSL3_ST_SW_SRVR_HELLO_B:
  331. ret=ssl3_send_server_hello(s);
  332. if (ret <= 0) goto end;
  333. if (s->hit)
  334. {
  335. if (s->tlsext_ticket_expected)
  336. s->state=SSL3_ST_SW_SESSION_TICKET_A;
  337. else
  338. s->state=SSL3_ST_SW_CHANGE_A;
  339. }
  340. else
  341. s->state = SSL3_ST_SW_CERT_A;
  342. s->init_num = 0;
  343. break;
  344. case SSL3_ST_SW_CERT_A:
  345. case SSL3_ST_SW_CERT_B:
  346. if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher))
  347. {
  348. ret=ssl3_send_server_certificate(s);
  349. if (ret <= 0) goto end;
  350. if (s->s3->tmp.certificate_status_expected)
  351. s->state=SSL3_ST_SW_CERT_STATUS_A;
  352. else
  353. s->state=SSL3_ST_SW_KEY_EXCH_A;
  354. }
  355. else
  356. {
  357. skip = 1;
  358. s->state=SSL3_ST_SW_KEY_EXCH_A;
  359. }
  360. s->init_num=0;
  361. break;
  362. case SSL3_ST_SW_KEY_EXCH_A:
  363. case SSL3_ST_SW_KEY_EXCH_B:
  364. alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  365. /* Send a ServerKeyExchange message if:
  366. * - The key exchange is ephemeral or anonymous
  367. * Diffie-Hellman.
  368. * - There is a PSK identity hint.
  369. *
  370. * TODO(davidben): This logic is currently duplicated
  371. * in d1_srvr.c. Fix this. In the meantime, keep them
  372. * in sync.
  373. */
  374. if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher) ||
  375. ((alg_a & SSL_aPSK) && s->session->psk_identity_hint))
  376. {
  377. ret=ssl3_send_server_key_exchange(s);
  378. if (ret <= 0) goto end;
  379. }
  380. else
  381. skip=1;
  382. s->state=SSL3_ST_SW_CERT_REQ_A;
  383. s->init_num=0;
  384. break;
  385. case SSL3_ST_SW_CERT_REQ_A:
  386. case SSL3_ST_SW_CERT_REQ_B:
  387. if (/* don't request cert unless asked for it: */
  388. !(s->verify_mode & SSL_VERIFY_PEER) ||
  389. /* Don't request a certificate if an obc was presented */
  390. ((s->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
  391. s->s3->tlsext_channel_id_valid) ||
  392. /* if SSL_VERIFY_CLIENT_ONCE is set,
  393. * don't request cert during re-negotiation: */
  394. ((s->session->peer != NULL) &&
  395. (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
  396. /* never request cert in anonymous ciphersuites
  397. * (see section "Certificate request" in SSL 3 drafts
  398. * and in RFC 2246): */
  399. ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
  400. /* ... except when the application insists on verification
  401. * (against the specs, but s3_clnt.c accepts this for SSL 3) */
  402. !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
  403. /* With normal PSK Certificates and
  404. * Certificate Requests are omitted */
  405. (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
  406. {
  407. /* no cert request */
  408. skip=1;
  409. s->s3->tmp.cert_request=0;
  410. s->state=SSL3_ST_SW_SRVR_DONE_A;
  411. if (s->s3->handshake_buffer)
  412. if (!ssl3_digest_cached_records(s))
  413. return -1;
  414. }
  415. else
  416. {
  417. s->s3->tmp.cert_request=1;
  418. ret=ssl3_send_certificate_request(s);
  419. if (ret <= 0) goto end;
  420. #ifndef NETSCAPE_HANG_BUG
  421. s->state=SSL3_ST_SW_SRVR_DONE_A;
  422. #else
  423. s->state=SSL3_ST_SW_FLUSH;
  424. s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
  425. #endif
  426. s->init_num=0;
  427. }
  428. break;
  429. case SSL3_ST_SW_SRVR_DONE_A:
  430. case SSL3_ST_SW_SRVR_DONE_B:
  431. ret=ssl3_send_server_done(s);
  432. if (ret <= 0) goto end;
  433. s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
  434. s->state=SSL3_ST_SW_FLUSH;
  435. s->init_num=0;
  436. break;
  437. case SSL3_ST_SW_FLUSH:
  438. /* This code originally checked to see if
  439. * any data was pending using BIO_CTRL_INFO
  440. * and then flushed. This caused problems
  441. * as documented in PR#1939. The proposed
  442. * fix doesn't completely resolve this issue
  443. * as buggy implementations of BIO_CTRL_PENDING
  444. * still exist. So instead we just flush
  445. * unconditionally.
  446. */
  447. s->rwstate=SSL_WRITING;
  448. if (BIO_flush(s->wbio) <= 0)
  449. {
  450. ret= -1;
  451. goto end;
  452. }
  453. s->rwstate=SSL_NOTHING;
  454. s->state=s->s3->tmp.next_state;
  455. break;
  456. case SSL3_ST_SR_CERT_A:
  457. case SSL3_ST_SR_CERT_B:
  458. if (s->s3->tmp.cert_request)
  459. {
  460. ret=ssl3_get_client_certificate(s);
  461. if (ret <= 0) goto end;
  462. }
  463. s->init_num=0;
  464. s->state=SSL3_ST_SR_KEY_EXCH_A;
  465. break;
  466. case SSL3_ST_SR_KEY_EXCH_A:
  467. case SSL3_ST_SR_KEY_EXCH_B:
  468. ret=ssl3_get_client_key_exchange(s);
  469. if (ret <= 0)
  470. goto end;
  471. s->state=SSL3_ST_SR_CERT_VRFY_A;
  472. s->init_num=0;
  473. break;
  474. case SSL3_ST_SR_CERT_VRFY_A:
  475. case SSL3_ST_SR_CERT_VRFY_B:
  476. ret=ssl3_get_cert_verify(s);
  477. if (ret <= 0) goto end;
  478. s->state = SSL3_ST_SR_CHANGE;
  479. s->init_num=0;
  480. break;
  481. case SSL3_ST_SR_CHANGE: {
  482. char next_proto_neg = 0;
  483. char channel_id = 0;
  484. next_proto_neg = s->s3->next_proto_neg_seen;
  485. channel_id = s->s3->tlsext_channel_id_valid;
  486. /* At this point, the next message must be entirely
  487. * behind a ChangeCipherSpec. */
  488. if (!ssl3_expect_change_cipher_spec(s))
  489. {
  490. ret = -1;
  491. goto end;
  492. }
  493. if (next_proto_neg)
  494. s->state = SSL3_ST_SR_NEXT_PROTO_A;
  495. else if (channel_id)
  496. s->state = SSL3_ST_SR_CHANNEL_ID_A;
  497. else
  498. s->state = SSL3_ST_SR_FINISHED_A;
  499. break;
  500. }
  501. case SSL3_ST_SR_NEXT_PROTO_A:
  502. case SSL3_ST_SR_NEXT_PROTO_B:
  503. ret=ssl3_get_next_proto(s);
  504. if (ret <= 0) goto end;
  505. s->init_num = 0;
  506. if (s->s3->tlsext_channel_id_valid)
  507. s->state=SSL3_ST_SR_CHANNEL_ID_A;
  508. else
  509. s->state=SSL3_ST_SR_FINISHED_A;
  510. break;
  511. case SSL3_ST_SR_CHANNEL_ID_A:
  512. case SSL3_ST_SR_CHANNEL_ID_B:
  513. ret=ssl3_get_channel_id(s);
  514. if (ret <= 0) goto end;
  515. s->init_num = 0;
  516. s->state=SSL3_ST_SR_FINISHED_A;
  517. break;
  518. case SSL3_ST_SR_FINISHED_A:
  519. case SSL3_ST_SR_FINISHED_B:
  520. ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
  521. SSL3_ST_SR_FINISHED_B);
  522. if (ret <= 0) goto end;
  523. if (s->hit)
  524. s->state=SSL_ST_OK;
  525. else if (s->tlsext_ticket_expected)
  526. s->state=SSL3_ST_SW_SESSION_TICKET_A;
  527. else
  528. s->state=SSL3_ST_SW_CHANGE_A;
  529. /* If this is a full handshake with ChannelID then
  530. * record the hashshake hashes in |s->session| in case
  531. * we need them to verify a ChannelID signature on a
  532. * resumption of this session in the future. */
  533. if (!s->hit && s->s3->tlsext_channel_id_new)
  534. {
  535. ret = tls1_record_handshake_hashes_for_channel_id(s);
  536. if (ret <= 0) goto end;
  537. }
  538. s->init_num=0;
  539. break;
  540. case SSL3_ST_SW_SESSION_TICKET_A:
  541. case SSL3_ST_SW_SESSION_TICKET_B:
  542. ret=ssl3_send_new_session_ticket(s);
  543. if (ret <= 0) goto end;
  544. s->state=SSL3_ST_SW_CHANGE_A;
  545. s->init_num=0;
  546. break;
  547. #if 0
  548. // TODO(davidben): Implement OCSP stapling on the server.
  549. case SSL3_ST_SW_CERT_STATUS_A:
  550. case SSL3_ST_SW_CERT_STATUS_B:
  551. ret=ssl3_send_cert_status(s);
  552. if (ret <= 0) goto end;
  553. s->state=SSL3_ST_SW_KEY_EXCH_A;
  554. s->init_num=0;
  555. break;
  556. #endif
  557. case SSL3_ST_SW_CHANGE_A:
  558. case SSL3_ST_SW_CHANGE_B:
  559. s->session->cipher=s->s3->tmp.new_cipher;
  560. if (!s->method->ssl3_enc->setup_key_block(s))
  561. { ret= -1; goto end; }
  562. ret=ssl3_send_change_cipher_spec(s,
  563. SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
  564. if (ret <= 0) goto end;
  565. s->state=SSL3_ST_SW_FINISHED_A;
  566. s->init_num=0;
  567. if (!s->method->ssl3_enc->change_cipher_state(s,
  568. SSL3_CHANGE_CIPHER_SERVER_WRITE))
  569. {
  570. ret= -1;
  571. goto end;
  572. }
  573. break;
  574. case SSL3_ST_SW_FINISHED_A:
  575. case SSL3_ST_SW_FINISHED_B:
  576. ret=ssl3_send_finished(s,
  577. SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
  578. s->method->ssl3_enc->server_finished_label,
  579. s->method->ssl3_enc->server_finished_label_len);
  580. if (ret <= 0) goto end;
  581. s->state = SSL3_ST_SW_FLUSH;
  582. if (s->hit)
  583. s->s3->tmp.next_state = SSL3_ST_SR_CHANGE;
  584. else
  585. s->s3->tmp.next_state = SSL_ST_OK;
  586. s->init_num=0;
  587. break;
  588. case SSL_ST_OK:
  589. /* clean a few things up */
  590. ssl3_cleanup_key_block(s);
  591. BUF_MEM_free(s->init_buf);
  592. s->init_buf=NULL;
  593. /* remove buffering on output */
  594. ssl_free_wbio_buffer(s);
  595. s->init_num=0;
  596. /* If we aren't retaining peer certificates then we can
  597. * discard it now. */
  598. if (s->session->peer && s->ctx->retain_only_sha256_of_client_certs)
  599. {
  600. X509_free(s->session->peer);
  601. s->session->peer = NULL;
  602. }
  603. if (s->renegotiate == 2) /* skipped if we just sent a HelloRequest */
  604. {
  605. s->renegotiate=0;
  606. s->new_session=0;
  607. ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
  608. s->ctx->stats.sess_accept_good++;
  609. /* s->server=1; */
  610. s->handshake_func=ssl3_accept;
  611. if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
  612. }
  613. ret = 1;
  614. goto end;
  615. /* break; */
  616. default:
  617. OPENSSL_PUT_ERROR(SSL, ssl3_accept, SSL_R_UNKNOWN_STATE);
  618. ret= -1;
  619. goto end;
  620. /* break; */
  621. }
  622. if (!s->s3->tmp.reuse_message && !skip)
  623. {
  624. if (s->debug)
  625. {
  626. if ((ret=BIO_flush(s->wbio)) <= 0)
  627. goto end;
  628. }
  629. if ((cb != NULL) && (s->state != state))
  630. {
  631. new_state=s->state;
  632. s->state=state;
  633. cb(s,SSL_CB_ACCEPT_LOOP,1);
  634. s->state=new_state;
  635. }
  636. }
  637. skip=0;
  638. }
  639. end:
  640. /* BIO_flush(s->wbio); */
  641. s->in_handshake--;
  642. if (cb != NULL)
  643. cb(s,SSL_CB_ACCEPT_EXIT,ret);
  644. return(ret);
  645. }
  646. int ssl3_send_hello_request(SSL *s)
  647. {
  648. if (s->state == SSL3_ST_SW_HELLO_REQ_A)
  649. {
  650. ssl_set_handshake_header(s, SSL3_MT_HELLO_REQUEST, 0);
  651. s->state=SSL3_ST_SW_HELLO_REQ_B;
  652. }
  653. /* SSL3_ST_SW_HELLO_REQ_B */
  654. return ssl_do_write(s);
  655. }
  656. int ssl3_get_client_hello(SSL *s)
  657. {
  658. int i,ok,al=SSL_AD_INTERNAL_ERROR,ret= -1;
  659. long n;
  660. const SSL_CIPHER *c;
  661. STACK_OF(SSL_CIPHER) *ciphers=NULL;
  662. struct ssl_early_callback_ctx early_ctx;
  663. CBS client_hello;
  664. uint16_t client_version;
  665. CBS client_random, session_id, cipher_suites, compression_methods;
  666. /* We do this so that we will respond with our native type.
  667. * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
  668. * This down switching should be handled by a different method.
  669. * If we are SSLv3, we will respond with SSLv3, even if prompted with
  670. * TLSv1.
  671. */
  672. switch (s->state) {
  673. case SSL3_ST_SR_CLNT_HELLO_A:
  674. case SSL3_ST_SR_CLNT_HELLO_B:
  675. s->first_packet=1;
  676. n=s->method->ssl_get_message(s,
  677. SSL3_ST_SR_CLNT_HELLO_A,
  678. SSL3_ST_SR_CLNT_HELLO_B,
  679. SSL3_MT_CLIENT_HELLO,
  680. SSL3_RT_MAX_PLAIN_LENGTH,
  681. SSL_GET_MESSAGE_HASH_MESSAGE,
  682. &ok);
  683. if (!ok) return((int)n);
  684. s->first_packet=0;
  685. /* If we require cookies and this ClientHello doesn't
  686. * contain one, just return since we do not want to
  687. * allocate any memory yet. So check cookie length...
  688. */
  689. if (SSL_IS_DTLS(s) && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE))
  690. {
  691. CBS session_id;
  692. uint8_t cookie_length;
  693. CBS_init(&client_hello, s->init_msg, n);
  694. if (!CBS_skip(&client_hello, 2 + SSL3_RANDOM_SIZE) ||
  695. !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
  696. !CBS_get_u8(&client_hello, &cookie_length))
  697. {
  698. al = SSL_AD_DECODE_ERROR;
  699. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR);
  700. goto f_err;
  701. }
  702. if (cookie_length == 0)
  703. return 1;
  704. }
  705. s->state = SSL3_ST_SR_CLNT_HELLO_C;
  706. /* fallthrough */
  707. case SSL3_ST_SR_CLNT_HELLO_C:
  708. case SSL3_ST_SR_CLNT_HELLO_D:
  709. /* We have previously parsed the ClientHello message,
  710. * and can't call ssl_get_message again without hashing
  711. * the message into the Finished digest again. */
  712. n = s->init_num;
  713. memset(&early_ctx, 0, sizeof(early_ctx));
  714. early_ctx.ssl = s;
  715. early_ctx.client_hello = s->init_msg;
  716. early_ctx.client_hello_len = n;
  717. if (!ssl_early_callback_init(&early_ctx))
  718. {
  719. al = SSL_AD_DECODE_ERROR;
  720. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CLIENTHELLO_PARSE_FAILED);
  721. goto f_err;
  722. }
  723. if (s->state == SSL3_ST_SR_CLNT_HELLO_C &&
  724. s->ctx->select_certificate_cb != NULL)
  725. {
  726. int ret;
  727. s->state = SSL3_ST_SR_CLNT_HELLO_D;
  728. ret = s->ctx->select_certificate_cb(&early_ctx);
  729. if (ret == 0)
  730. return CERTIFICATE_SELECTION_PENDING;
  731. else if (ret == -1)
  732. {
  733. /* Connection rejected. */
  734. al = SSL_AD_ACCESS_DENIED;
  735. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CONNECTION_REJECTED);
  736. goto f_err;
  737. }
  738. }
  739. s->state = SSL3_ST_SR_CLNT_HELLO_D;
  740. break;
  741. default:
  742. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_UNKNOWN_STATE);
  743. return -1;
  744. }
  745. CBS_init(&client_hello, s->init_msg, n);
  746. if (!CBS_get_u16(&client_hello, &client_version) ||
  747. !CBS_get_bytes(&client_hello, &client_random, SSL3_RANDOM_SIZE) ||
  748. !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
  749. CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH)
  750. {
  751. al = SSL_AD_DECODE_ERROR;
  752. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR);
  753. goto f_err;
  754. }
  755. /* use version from inside client hello, not from record header
  756. * (may differ: see RFC 2246, Appendix E, second paragraph) */
  757. s->client_version = client_version;
  758. if (SSL_IS_DTLS(s) ? (s->client_version > s->version &&
  759. s->method->version != DTLS_ANY_VERSION)
  760. : (s->client_version < s->version))
  761. {
  762. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_WRONG_VERSION_NUMBER);
  763. if ((s->client_version>>8) == SSL3_VERSION_MAJOR &&
  764. !s->enc_write_ctx && !s->write_hash)
  765. {
  766. /* similar to ssl3_get_record, send alert using remote version number */
  767. s->version = s->client_version;
  768. }
  769. al = SSL_AD_PROTOCOL_VERSION;
  770. goto f_err;
  771. }
  772. /* Load the client random. */
  773. memcpy(s->s3->client_random, CBS_data(&client_random), SSL3_RANDOM_SIZE);
  774. s->hit=0;
  775. /* Versions before 0.9.7 always allow clients to resume sessions in renegotiation.
  776. * 0.9.7 and later allow this by default, but optionally ignore resumption requests
  777. * with flag SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
  778. * than a change to default behavior so that applications relying on this for security
  779. * won't even compile against older library versions).
  780. *
  781. * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to request
  782. * renegotiation but not a new session (s->new_session remains unset): for servers,
  783. * this essentially just means that the SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
  784. * setting will be ignored.
  785. */
  786. if ((s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION)))
  787. {
  788. if (!ssl_get_new_session(s,1))
  789. goto err;
  790. }
  791. else
  792. {
  793. i=ssl_get_prev_session(s, &early_ctx);
  794. if (i == 1)
  795. { /* previous session */
  796. s->hit=1;
  797. }
  798. else if (i == -1)
  799. goto err;
  800. else if (i == PENDING_SESSION)
  801. {
  802. ret = PENDING_SESSION;
  803. goto err;
  804. }
  805. else /* i == 0 */
  806. {
  807. if (!ssl_get_new_session(s,1))
  808. goto err;
  809. }
  810. }
  811. if (SSL_IS_DTLS(s))
  812. {
  813. CBS cookie;
  814. /* TODO(davidben): The length check here is off. Per
  815. * spec, the maximum cookie length is 32. However, the
  816. * DTLS1_COOKIE_LENGTH check is checking against 256,
  817. * not 32 (so it's actually redundant).
  818. * 07a9d1a2c2b735cbc327065000b545deb5e136cf from
  819. * OpenSSL switched this from 32 to 256. */
  820. if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
  821. CBS_len(&cookie) > DTLS1_COOKIE_LENGTH)
  822. {
  823. al = SSL_AD_DECODE_ERROR;
  824. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR);
  825. goto f_err;
  826. }
  827. /* Verify the cookie if appropriate option is set. */
  828. if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) &&
  829. CBS_len(&cookie) > 0)
  830. {
  831. if (s->ctx->app_verify_cookie_cb != NULL)
  832. {
  833. if (s->ctx->app_verify_cookie_cb(s,
  834. CBS_data(&cookie), CBS_len(&cookie)) == 0)
  835. {
  836. al=SSL_AD_HANDSHAKE_FAILURE;
  837. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_COOKIE_MISMATCH);
  838. goto f_err;
  839. }
  840. /* else cookie verification succeeded */
  841. }
  842. else if (!CBS_mem_equal(&cookie, s->d1->cookie, s->d1->cookie_len))
  843. {
  844. /* default verification */
  845. al=SSL_AD_HANDSHAKE_FAILURE;
  846. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_COOKIE_MISMATCH);
  847. goto f_err;
  848. }
  849. /* Set to -2 so if successful we return 2 and
  850. * don't send HelloVerifyRequest. */
  851. ret = -2;
  852. }
  853. if (s->method->version == DTLS_ANY_VERSION)
  854. {
  855. /* Select version to use */
  856. if (s->client_version <= DTLS1_2_VERSION &&
  857. !(s->options & SSL_OP_NO_DTLSv1_2))
  858. {
  859. s->version = DTLS1_2_VERSION;
  860. s->method = DTLSv1_2_server_method();
  861. }
  862. else if (s->client_version <= DTLS1_VERSION &&
  863. !(s->options & SSL_OP_NO_DTLSv1))
  864. {
  865. s->version = DTLS1_VERSION;
  866. s->method = DTLSv1_server_method();
  867. }
  868. else
  869. {
  870. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_WRONG_VERSION_NUMBER);
  871. s->version = s->client_version;
  872. al = SSL_AD_PROTOCOL_VERSION;
  873. goto f_err;
  874. }
  875. s->session->ssl_version = s->version;
  876. }
  877. }
  878. if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
  879. !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
  880. CBS_len(&compression_methods) == 0)
  881. {
  882. al = SSL_AD_DECODE_ERROR;
  883. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR);
  884. goto f_err;
  885. }
  886. /* TODO(davidben): Per spec, cipher_suites can never be empty
  887. * (specified at the ClientHello structure level). This logic
  888. * allows it to be empty if resuming a session. Can we always
  889. * require non-empty? If a client sends empty cipher_suites
  890. * because it's resuming a session, it could always fail to
  891. * resume a session, so it's unlikely to actually work. */
  892. if (CBS_len(&cipher_suites) == 0 && CBS_len(&session_id) != 0)
  893. {
  894. /* We need a cipher if we are not resuming a session. */
  895. al = SSL_AD_ILLEGAL_PARAMETER;
  896. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_CIPHERS_SPECIFIED);
  897. goto f_err;
  898. }
  899. if (ssl_bytes_to_cipher_list(s, &cipher_suites, &ciphers) == NULL)
  900. {
  901. goto err;
  902. }
  903. /* If it is a hit, check that the cipher is in the list */
  904. if (s->hit && CBS_len(&cipher_suites) > 0)
  905. {
  906. size_t j;
  907. int found_cipher = 0;
  908. unsigned long id = s->session->cipher->id;
  909. for (j = 0; j < sk_SSL_CIPHER_num(ciphers); j++)
  910. {
  911. c = sk_SSL_CIPHER_value(ciphers, j);
  912. if (c->id == id)
  913. {
  914. found_cipher = 1;
  915. break;
  916. }
  917. }
  918. if (!found_cipher)
  919. {
  920. /* we need to have the cipher in the cipher
  921. * list if we are asked to reuse it */
  922. al=SSL_AD_ILLEGAL_PARAMETER;
  923. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_REQUIRED_CIPHER_MISSING);
  924. goto f_err;
  925. }
  926. }
  927. /* Only null compression is supported. */
  928. if (memchr(CBS_data(&compression_methods), 0,
  929. CBS_len(&compression_methods)) == NULL)
  930. {
  931. al = SSL_AD_ILLEGAL_PARAMETER;
  932. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_COMPRESSION_SPECIFIED);
  933. goto f_err;
  934. }
  935. /* TLS extensions*/
  936. if (s->version >= SSL3_VERSION)
  937. {
  938. if (!ssl_parse_clienthello_tlsext(s, &client_hello))
  939. {
  940. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_PARSE_TLSEXT);
  941. goto err;
  942. }
  943. }
  944. /* There should be nothing left over in the record. */
  945. if (CBS_len(&client_hello) != 0)
  946. {
  947. /* wrong packet length */
  948. al=SSL_AD_DECODE_ERROR;
  949. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_BAD_PACKET_LENGTH);
  950. goto f_err;
  951. }
  952. /* Check if we want to use external pre-shared secret for this
  953. * handshake for not reused session only. We need to generate
  954. * server_random before calling tls_session_secret_cb in order to allow
  955. * SessionTicket processing to use it in key derivation. */
  956. {
  957. unsigned char *pos;
  958. pos=s->s3->server_random;
  959. if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE) <= 0)
  960. {
  961. goto f_err;
  962. }
  963. }
  964. if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb)
  965. {
  966. const SSL_CIPHER *pref_cipher=NULL;
  967. s->session->master_key_length=sizeof(s->session->master_key);
  968. if(s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length,
  969. ciphers, &pref_cipher, s->tls_session_secret_cb_arg))
  970. {
  971. s->hit=1;
  972. s->session->verify_result=X509_V_OK;
  973. /* check if some cipher was preferred by call back */
  974. pref_cipher=pref_cipher ? pref_cipher : ssl3_choose_cipher(s, ciphers, ssl_get_cipher_preferences(s));
  975. if (pref_cipher == NULL)
  976. {
  977. al=SSL_AD_HANDSHAKE_FAILURE;
  978. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_SHARED_CIPHER);
  979. goto f_err;
  980. }
  981. s->session->cipher=pref_cipher;
  982. if (s->cipher_list)
  983. ssl_cipher_preference_list_free(s->cipher_list);
  984. if (s->cipher_list_by_id)
  985. sk_SSL_CIPHER_free(s->cipher_list_by_id);
  986. s->cipher_list = ssl_cipher_preference_list_from_ciphers(ciphers);
  987. s->cipher_list_by_id = sk_SSL_CIPHER_dup(ciphers);
  988. }
  989. }
  990. /* Given ciphers and SSL_get_ciphers, we must pick a cipher */
  991. if (!s->hit)
  992. {
  993. if (ciphers == NULL)
  994. {
  995. al=SSL_AD_ILLEGAL_PARAMETER;
  996. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_CIPHERS_PASSED);
  997. goto f_err;
  998. }
  999. /* Let cert callback update server certificates if required */
  1000. if (s->cert->cert_cb)
  1001. {
  1002. int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
  1003. if (rv == 0)
  1004. {
  1005. al=SSL_AD_INTERNAL_ERROR;
  1006. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CERT_CB_ERROR);
  1007. goto f_err;
  1008. }
  1009. if (rv < 0)
  1010. {
  1011. s->rwstate=SSL_X509_LOOKUP;
  1012. goto err;
  1013. }
  1014. s->rwstate = SSL_NOTHING;
  1015. }
  1016. c=ssl3_choose_cipher(s, ciphers, ssl_get_cipher_preferences(s));
  1017. if (c == NULL)
  1018. {
  1019. al=SSL_AD_HANDSHAKE_FAILURE;
  1020. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_SHARED_CIPHER);
  1021. goto f_err;
  1022. }
  1023. s->s3->tmp.new_cipher=c;
  1024. }
  1025. else
  1026. {
  1027. /* Session-id reuse */
  1028. s->s3->tmp.new_cipher=s->session->cipher;
  1029. }
  1030. if (!SSL_USE_SIGALGS(s) || !(s->verify_mode & SSL_VERIFY_PEER))
  1031. {
  1032. if (!ssl3_digest_cached_records(s))
  1033. goto f_err;
  1034. }
  1035. /* we now have the following setup.
  1036. * client_random
  1037. * cipher_list - our prefered list of ciphers
  1038. * ciphers - the clients prefered list of ciphers
  1039. * compression - basically ignored right now
  1040. * ssl version is set - sslv3
  1041. * s->session - The ssl session has been setup.
  1042. * s->hit - session reuse flag
  1043. * s->tmp.new_cipher - the new cipher to use.
  1044. */
  1045. if (ret < 0) ret=-ret;
  1046. if (0)
  1047. {
  1048. f_err:
  1049. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1050. }
  1051. err:
  1052. if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers);
  1053. return ret;
  1054. }
  1055. int ssl3_send_server_hello(SSL *s)
  1056. {
  1057. unsigned char *buf;
  1058. unsigned char *p,*d;
  1059. int sl;
  1060. unsigned long l;
  1061. if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
  1062. {
  1063. /* We only accept ChannelIDs on connections with ECDHE in order
  1064. * to avoid a known attack while we fix ChannelID itself. */
  1065. if (s->s3 &&
  1066. s->s3->tlsext_channel_id_valid &&
  1067. (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kEECDH) == 0)
  1068. s->s3->tlsext_channel_id_valid = 0;
  1069. /* If this is a resumption and the original handshake didn't
  1070. * support ChannelID then we didn't record the original
  1071. * handshake hashes in the session and so cannot resume with
  1072. * ChannelIDs. */
  1073. if (s->hit &&
  1074. s->s3->tlsext_channel_id_new &&
  1075. s->session->original_handshake_hash_len == 0)
  1076. s->s3->tlsext_channel_id_valid = 0;
  1077. buf=(unsigned char *)s->init_buf->data;
  1078. /* Do the message type and length last */
  1079. d=p= ssl_handshake_start(s);
  1080. *(p++)=s->version>>8;
  1081. *(p++)=s->version&0xff;
  1082. /* Random stuff */
  1083. memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
  1084. p+=SSL3_RANDOM_SIZE;
  1085. /* There are several cases for the session ID to send
  1086. * back in the server hello:
  1087. * - For session reuse from the session cache,
  1088. * we send back the old session ID.
  1089. * - If stateless session reuse (using a session ticket)
  1090. * is successful, we send back the client's "session ID"
  1091. * (which doesn't actually identify the session).
  1092. * - If it is a new session, we send back the new
  1093. * session ID.
  1094. * - However, if we want the new session to be single-use,
  1095. * we send back a 0-length session ID.
  1096. * s->hit is non-zero in either case of session reuse,
  1097. * so the following won't overwrite an ID that we're supposed
  1098. * to send back.
  1099. */
  1100. if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
  1101. && !s->hit)
  1102. s->session->session_id_length=0;
  1103. sl=s->session->session_id_length;
  1104. if (sl > (int)sizeof(s->session->session_id))
  1105. {
  1106. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR);
  1107. return -1;
  1108. }
  1109. *(p++)=sl;
  1110. memcpy(p,s->session->session_id,sl);
  1111. p+=sl;
  1112. /* put the cipher */
  1113. s2n(ssl3_get_cipher_value(s->s3->tmp.new_cipher), p);
  1114. /* put the compression method */
  1115. *(p++)=0;
  1116. if (ssl_prepare_serverhello_tlsext(s) <= 0)
  1117. {
  1118. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, SSL_R_SERVERHELLO_TLSEXT);
  1119. return -1;
  1120. }
  1121. if ((p = ssl_add_serverhello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL)
  1122. {
  1123. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR);
  1124. return -1;
  1125. }
  1126. /* do the header */
  1127. l=(p-d);
  1128. ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l);
  1129. s->state=SSL3_ST_SW_SRVR_HELLO_B;
  1130. }
  1131. /* SSL3_ST_SW_SRVR_HELLO_B */
  1132. return ssl_do_write(s);
  1133. }
  1134. int ssl3_send_server_done(SSL *s)
  1135. {
  1136. if (s->state == SSL3_ST_SW_SRVR_DONE_A)
  1137. {
  1138. ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0);
  1139. s->state = SSL3_ST_SW_SRVR_DONE_B;
  1140. }
  1141. /* SSL3_ST_SW_SRVR_DONE_B */
  1142. return ssl_do_write(s);
  1143. }
  1144. int ssl3_send_server_key_exchange(SSL *s)
  1145. {
  1146. unsigned char *q;
  1147. int j,num;
  1148. unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
  1149. unsigned int u;
  1150. DH *dh=NULL,*dhp;
  1151. EC_KEY *ecdh=NULL, *ecdhp;
  1152. unsigned char *encodedPoint = NULL;
  1153. int encodedlen = 0;
  1154. int curve_id = 0;
  1155. BN_CTX *bn_ctx = NULL;
  1156. const char* psk_identity_hint = NULL;
  1157. size_t psk_identity_hint_len = 0;
  1158. EVP_PKEY *pkey;
  1159. const EVP_MD *md = NULL;
  1160. unsigned char *p,*d;
  1161. int al,i;
  1162. unsigned long alg_k;
  1163. unsigned long alg_a;
  1164. int n;
  1165. CERT *cert;
  1166. BIGNUM *r[4];
  1167. int nr[4],kn;
  1168. BUF_MEM *buf;
  1169. EVP_MD_CTX md_ctx;
  1170. EVP_MD_CTX_init(&md_ctx);
  1171. if (s->state == SSL3_ST_SW_KEY_EXCH_A)
  1172. {
  1173. alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
  1174. alg_a=s->s3->tmp.new_cipher->algorithm_auth;
  1175. cert=s->cert;
  1176. buf=s->init_buf;
  1177. r[0]=r[1]=r[2]=r[3]=NULL;
  1178. n=0;
  1179. if (alg_a & SSL_aPSK)
  1180. {
  1181. /* size for PSK identity hint */
  1182. psk_identity_hint = s->session->psk_identity_hint;
  1183. if (psk_identity_hint)
  1184. psk_identity_hint_len = strlen(psk_identity_hint);
  1185. else
  1186. psk_identity_hint_len = 0;
  1187. n+=2+psk_identity_hint_len;
  1188. }
  1189. if (alg_k & SSL_kEDH)
  1190. {
  1191. dhp=cert->dh_tmp;
  1192. if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
  1193. dhp=s->cert->dh_tmp_cb(s, 0, 1024);
  1194. if (dhp == NULL)
  1195. {
  1196. al=SSL_AD_HANDSHAKE_FAILURE;
  1197. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_MISSING_TMP_DH_KEY);
  1198. goto f_err;
  1199. }
  1200. if (s->s3->tmp.dh != NULL)
  1201. {
  1202. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_INTERNAL_ERROR);
  1203. goto err;
  1204. }
  1205. if ((dh=DHparams_dup(dhp)) == NULL)
  1206. {
  1207. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB);
  1208. goto err;
  1209. }
  1210. s->s3->tmp.dh=dh;
  1211. if ((dhp->pub_key == NULL ||
  1212. dhp->priv_key == NULL ||
  1213. (s->options & SSL_OP_SINGLE_DH_USE)))
  1214. {
  1215. if(!DH_generate_key(dh))
  1216. {
  1217. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB);
  1218. goto err;
  1219. }
  1220. }
  1221. else
  1222. {
  1223. dh->pub_key=BN_dup(dhp->pub_key);
  1224. dh->priv_key=BN_dup(dhp->priv_key);
  1225. if ((dh->pub_key == NULL) ||
  1226. (dh->priv_key == NULL))
  1227. {
  1228. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB);
  1229. goto err;
  1230. }
  1231. }
  1232. r[0]=dh->p;
  1233. r[1]=dh->g;
  1234. r[2]=dh->pub_key;
  1235. }
  1236. else
  1237. if (alg_k & SSL_kEECDH)
  1238. {
  1239. const EC_GROUP *group;
  1240. ecdhp=cert->ecdh_tmp;
  1241. if (s->cert->ecdh_tmp_auto)
  1242. {
  1243. /* Get NID of appropriate shared curve */
  1244. int nid = tls1_get_shared_curve(s);
  1245. if (nid != NID_undef)
  1246. ecdhp = EC_KEY_new_by_curve_name(nid);
  1247. }
  1248. else if ((ecdhp == NULL) && s->cert->ecdh_tmp_cb)
  1249. {
  1250. ecdhp = s->cert->ecdh_tmp_cb(s, 0, 1024);
  1251. }
  1252. if (ecdhp == NULL)
  1253. {
  1254. al=SSL_AD_HANDSHAKE_FAILURE;
  1255. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_MISSING_TMP_ECDH_KEY);
  1256. goto f_err;
  1257. }
  1258. if (s->s3->tmp.ecdh != NULL)
  1259. {
  1260. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_INTERNAL_ERROR);
  1261. goto err;
  1262. }
  1263. /* Duplicate the ECDH structure. */
  1264. if (ecdhp == NULL)
  1265. {
  1266. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
  1267. goto err;
  1268. }
  1269. if (s->cert->ecdh_tmp_auto)
  1270. ecdh = ecdhp;
  1271. else if ((ecdh = EC_KEY_dup(ecdhp)) == NULL)
  1272. {
  1273. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
  1274. goto err;
  1275. }
  1276. s->s3->tmp.ecdh=ecdh;
  1277. if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
  1278. (EC_KEY_get0_private_key(ecdh) == NULL) ||
  1279. (s->options & SSL_OP_SINGLE_ECDH_USE))
  1280. {
  1281. if(!EC_KEY_generate_key(ecdh))
  1282. {
  1283. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
  1284. goto err;
  1285. }
  1286. }
  1287. if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
  1288. (EC_KEY_get0_public_key(ecdh) == NULL) ||
  1289. (EC_KEY_get0_private_key(ecdh) == NULL))
  1290. {
  1291. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
  1292. goto err;
  1293. }
  1294. /* XXX: For now, we only support ephemeral ECDH
  1295. * keys over named (not generic) curves. For
  1296. * supported named curves, curve_id is non-zero.
  1297. */
  1298. if ((curve_id =
  1299. tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group)))
  1300. == 0)
  1301. {
  1302. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
  1303. goto err;
  1304. }
  1305. /* Encode the public key.
  1306. * First check the size of encoding and
  1307. * allocate memory accordingly.
  1308. */
  1309. encodedlen = EC_POINT_point2oct(group,
  1310. EC_KEY_get0_public_key(ecdh),
  1311. POINT_CONVERSION_UNCOMPRESSED,
  1312. NULL, 0, NULL);
  1313. encodedPoint = (unsigned char *)
  1314. OPENSSL_malloc(encodedlen*sizeof(unsigned char));
  1315. bn_ctx = BN_CTX_new();
  1316. if ((encodedPoint == NULL) || (bn_ctx == NULL))
  1317. {
  1318. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_MALLOC_FAILURE);
  1319. goto err;
  1320. }
  1321. encodedlen = EC_POINT_point2oct(group,
  1322. EC_KEY_get0_public_key(ecdh),
  1323. POINT_CONVERSION_UNCOMPRESSED,
  1324. encodedPoint, encodedlen, bn_ctx);
  1325. if (encodedlen == 0)
  1326. {
  1327. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB);
  1328. goto err;
  1329. }
  1330. BN_CTX_free(bn_ctx); bn_ctx=NULL;
  1331. /* XXX: For now, we only support named (not
  1332. * generic) curves in ECDH ephemeral key exchanges.
  1333. * In this situation, we need four additional bytes
  1334. * to encode the entire ServerECDHParams
  1335. * structure.
  1336. */
  1337. n += 4 + encodedlen;
  1338. /* We'll generate the serverKeyExchange message
  1339. * explicitly so we can set these to NULLs
  1340. */
  1341. r[0]=NULL;
  1342. r[1]=NULL;
  1343. r[2]=NULL;
  1344. r[3]=NULL;
  1345. }
  1346. else
  1347. if (!(alg_k & SSL_kPSK))
  1348. {
  1349. al=SSL_AD_HANDSHAKE_FAILURE;
  1350. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
  1351. goto f_err;
  1352. }
  1353. for (i=0; i < 4 && r[i] != NULL; i++)
  1354. {
  1355. nr[i]=BN_num_bytes(r[i]);
  1356. n+=2+nr[i];
  1357. }
  1358. if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher))
  1359. {
  1360. if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher,&md))
  1361. == NULL)
  1362. {
  1363. al=SSL_AD_DECODE_ERROR;
  1364. goto f_err;
  1365. }
  1366. kn=EVP_PKEY_size(pkey);
  1367. }
  1368. else
  1369. {
  1370. pkey=NULL;
  1371. kn=0;
  1372. }
  1373. if (!BUF_MEM_grow_clean(buf,n+SSL_HM_HEADER_LENGTH(s)+kn))
  1374. {
  1375. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_BUF);
  1376. goto err;
  1377. }
  1378. d = p = ssl_handshake_start(s);
  1379. for (i=0; i < 4 && r[i] != NULL; i++)
  1380. {
  1381. s2n(nr[i],p);
  1382. BN_bn2bin(r[i],p);
  1383. p+=nr[i];
  1384. }
  1385. /* Note: ECDHE PSK ciphersuites use SSL_kEECDH and SSL_aPSK.
  1386. * When one of them is used, the server key exchange record needs to have both
  1387. * the psk_identity_hint and the ServerECDHParams. */
  1388. if (alg_a & SSL_aPSK)
  1389. {
  1390. /* copy PSK identity hint (if provided) */
  1391. s2n(psk_identity_hint_len, p);
  1392. if (psk_identity_hint_len > 0)
  1393. {
  1394. memcpy(p, psk_identity_hint, psk_identity_hint_len);
  1395. p+=psk_identity_hint_len;
  1396. }
  1397. }
  1398. if (alg_k & SSL_kEECDH)
  1399. {
  1400. /* XXX: For now, we only support named (not generic) curves.
  1401. * In this situation, the serverKeyExchange message has:
  1402. * [1 byte CurveType], [2 byte CurveName]
  1403. * [1 byte length of encoded point], followed by
  1404. * the actual encoded point itself
  1405. */
  1406. *p = NAMED_CURVE_TYPE;
  1407. p += 1;
  1408. *p = 0;
  1409. p += 1;
  1410. *p = curve_id;
  1411. p += 1;
  1412. *p = encodedlen;
  1413. p += 1;
  1414. memcpy((unsigned char*)p,
  1415. (unsigned char *)encodedPoint,
  1416. encodedlen);
  1417. OPENSSL_free(encodedPoint);
  1418. encodedPoint = NULL;
  1419. p += encodedlen;
  1420. }
  1421. /* not anonymous */
  1422. if (pkey != NULL)
  1423. {
  1424. /* n is the length of the params, they start at &(d[4])
  1425. * and p points to the space at the end. */
  1426. if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s))
  1427. {
  1428. q=md_buf;
  1429. j=0;
  1430. for (num=2; num > 0; num--)
  1431. {
  1432. EVP_DigestInit_ex(&md_ctx,
  1433. (num == 2) ? EVP_md5() : EVP_sha1(), NULL);
  1434. EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1435. EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1436. EVP_DigestUpdate(&md_ctx,d,n);
  1437. EVP_DigestFinal_ex(&md_ctx,q,
  1438. (unsigned int *)&i);
  1439. q+=i;
  1440. j+=i;
  1441. }
  1442. if (RSA_sign(NID_md5_sha1, md_buf, j,
  1443. &(p[2]), &u, pkey->pkey.rsa) <= 0)
  1444. {
  1445. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_RSA);
  1446. goto err;
  1447. }
  1448. s2n(u,p);
  1449. n+=u+2;
  1450. }
  1451. else
  1452. if (md)
  1453. {
  1454. /* send signature algorithm */
  1455. if (SSL_USE_SIGALGS(s))
  1456. {
  1457. if (!tls12_get_sigandhash(p, pkey, md))
  1458. {
  1459. /* Should never happen */
  1460. al=SSL_AD_INTERNAL_ERROR;
  1461. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_INTERNAL_ERROR);
  1462. goto f_err;
  1463. }
  1464. p+=2;
  1465. }
  1466. #ifdef SSL_DEBUG
  1467. fprintf(stderr, "Using hash %s\n",
  1468. EVP_MD_name(md));
  1469. #endif
  1470. EVP_SignInit_ex(&md_ctx, md, NULL);
  1471. EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
  1472. EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
  1473. EVP_SignUpdate(&md_ctx,d,n);
  1474. if (!EVP_SignFinal(&md_ctx,&(p[2]),
  1475. (unsigned int *)&i,pkey))
  1476. {
  1477. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_EVP);
  1478. goto err;
  1479. }
  1480. s2n(i,p);
  1481. n+=i+2;
  1482. if (SSL_USE_SIGALGS(s))
  1483. n+= 2;
  1484. }
  1485. else
  1486. {
  1487. /* Is this error check actually needed? */
  1488. al=SSL_AD_HANDSHAKE_FAILURE;
  1489. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, SSL_R_UNKNOWN_PKEY_TYPE);
  1490. goto f_err;
  1491. }
  1492. }
  1493. ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, n);
  1494. }
  1495. s->state = SSL3_ST_SW_KEY_EXCH_B;
  1496. EVP_MD_CTX_cleanup(&md_ctx);
  1497. return ssl_do_write(s);
  1498. f_err:
  1499. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1500. err:
  1501. if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
  1502. BN_CTX_free(bn_ctx);
  1503. EVP_MD_CTX_cleanup(&md_ctx);
  1504. return(-1);
  1505. }
  1506. int ssl3_send_certificate_request(SSL *s)
  1507. {
  1508. unsigned char *p,*d;
  1509. size_t i;
  1510. int j,nl,off,n;
  1511. STACK_OF(X509_NAME) *sk=NULL;
  1512. X509_NAME *name;
  1513. BUF_MEM *buf;
  1514. if (s->state == SSL3_ST_SW_CERT_REQ_A)
  1515. {
  1516. buf=s->init_buf;
  1517. d=p=ssl_handshake_start(s);
  1518. /* get the list of acceptable cert types */
  1519. p++;
  1520. n=ssl3_get_req_cert_type(s,p);
  1521. d[0]=n;
  1522. p+=n;
  1523. n++;
  1524. if (SSL_USE_SIGALGS(s))
  1525. {
  1526. const unsigned char *psigs;
  1527. nl = tls12_get_psigalgs(s, &psigs);
  1528. s2n(nl, p);
  1529. memcpy(p, psigs, nl);
  1530. p += nl;
  1531. n += nl + 2;
  1532. }
  1533. off=n;
  1534. p+=2;
  1535. n+=2;
  1536. sk=SSL_get_client_CA_list(s);
  1537. nl=0;
  1538. if (sk != NULL)
  1539. {
  1540. for (i=0; i<sk_X509_NAME_num(sk); i++)
  1541. {
  1542. name=sk_X509_NAME_value(sk,i);
  1543. j=i2d_X509_NAME(name,NULL);
  1544. if (!BUF_MEM_grow_clean(buf,SSL_HM_HEADER_LENGTH(s)+n+j+2))
  1545. {
  1546. OPENSSL_PUT_ERROR(SSL, ssl3_send_certificate_request, ERR_R_BUF_LIB);
  1547. goto err;
  1548. }
  1549. p = ssl_handshake_start(s) + n;
  1550. s2n(j,p);
  1551. i2d_X509_NAME(name,&p);
  1552. n+=2+j;
  1553. nl+=2+j;
  1554. }
  1555. }
  1556. /* else no CA names */
  1557. p = ssl_handshake_start(s) + off;
  1558. s2n(nl,p);
  1559. ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n);
  1560. #ifdef NETSCAPE_HANG_BUG
  1561. if (!SSL_IS_DTLS(s))
  1562. {
  1563. if (!BUF_MEM_grow_clean(buf, s->init_num + 4))
  1564. {
  1565. OPENSSL_PUT_ERROR(SSL, ssl3_send_certificate_request, ERR_R_BUF_LIB);
  1566. goto err;
  1567. }
  1568. p=(unsigned char *)s->init_buf->data + s->init_num;
  1569. /* do the header */
  1570. *(p++)=SSL3_MT_SERVER_DONE;
  1571. *(p++)=0;
  1572. *(p++)=0;
  1573. *(p++)=0;
  1574. s->init_num += 4;
  1575. }
  1576. #endif
  1577. s->state = SSL3_ST_SW_CERT_REQ_B;
  1578. }
  1579. /* SSL3_ST_SW_CERT_REQ_B */
  1580. return ssl_do_write(s);
  1581. err:
  1582. return(-1);
  1583. }
  1584. int ssl3_get_client_key_exchange(SSL *s)
  1585. {
  1586. int al,ok;
  1587. long n;
  1588. CBS client_key_exchange;
  1589. unsigned long alg_k;
  1590. unsigned long alg_a;
  1591. uint8_t *premaster_secret = NULL;
  1592. size_t premaster_secret_len = 0;
  1593. RSA *rsa=NULL;
  1594. uint8_t *decrypt_buf = NULL;
  1595. EVP_PKEY *pkey=NULL;
  1596. BIGNUM *pub=NULL;
  1597. DH *dh_srvr;
  1598. EC_KEY *srvr_ecdh = NULL;
  1599. EVP_PKEY *clnt_pub_pkey = NULL;
  1600. EC_POINT *clnt_ecpoint = NULL;
  1601. BN_CTX *bn_ctx = NULL;
  1602. unsigned int psk_len = 0;
  1603. unsigned char psk[PSK_MAX_PSK_LEN];
  1604. n=s->method->ssl_get_message(s,
  1605. SSL3_ST_SR_KEY_EXCH_A,
  1606. SSL3_ST_SR_KEY_EXCH_B,
  1607. SSL3_MT_CLIENT_KEY_EXCHANGE,
  1608. 2048, /* ??? */
  1609. SSL_GET_MESSAGE_HASH_MESSAGE,
  1610. &ok);
  1611. if (!ok) return((int)n);
  1612. CBS_init(&client_key_exchange, s->init_msg, n);
  1613. alg_k=s->s3->tmp.new_cipher->algorithm_mkey;
  1614. alg_a=s->s3->tmp.new_cipher->algorithm_auth;
  1615. /* If using a PSK key exchange, prepare the pre-shared key. */
  1616. if (alg_a & SSL_aPSK)
  1617. {
  1618. CBS psk_identity;
  1619. /* If using PSK, the ClientKeyExchange contains a
  1620. * psk_identity. If PSK, then this is the only field
  1621. * in the message. */
  1622. if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
  1623. ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0))
  1624. {
  1625. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECODE_ERROR);
  1626. al = SSL_AD_DECODE_ERROR;
  1627. goto f_err;
  1628. }
  1629. if (s->psk_server_callback == NULL)
  1630. {
  1631. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_PSK_NO_SERVER_CB);
  1632. al = SSL_AD_INTERNAL_ERROR;
  1633. goto f_err;
  1634. }
  1635. if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
  1636. CBS_contains_zero_byte(&psk_identity))
  1637. {
  1638. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DATA_LENGTH_TOO_LONG);
  1639. al = SSL_AD_ILLEGAL_PARAMETER;
  1640. goto f_err;
  1641. }
  1642. if (!CBS_strdup(&psk_identity, &s->session->psk_identity))
  1643. {
  1644. al = SSL_AD_INTERNAL_ERROR;
  1645. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE);
  1646. goto f_err;
  1647. }
  1648. /* Look up the key for the identity. */
  1649. psk_len = s->psk_server_callback(s, s->session->psk_identity, psk, sizeof(psk));
  1650. if (psk_len > PSK_MAX_PSK_LEN)
  1651. {
  1652. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_INTERNAL_ERROR);
  1653. al = SSL_AD_INTERNAL_ERROR;
  1654. goto f_err;
  1655. }
  1656. else if (psk_len == 0)
  1657. {
  1658. /* PSK related to the given identity not found */
  1659. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_PSK_IDENTITY_NOT_FOUND);
  1660. al = SSL_AD_UNKNOWN_PSK_IDENTITY;
  1661. goto f_err;
  1662. }
  1663. }
  1664. /* Depending on the key exchange method, compute |premaster_secret| and
  1665. * |premaster_secret_len|. */
  1666. if (alg_k & SSL_kRSA)
  1667. {
  1668. CBS encrypted_premaster_secret;
  1669. uint8_t rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
  1670. int decrypt_good_mask;
  1671. uint8_t version_good;
  1672. size_t rsa_size, decrypt_len, premaster_index, j;
  1673. pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
  1674. if ( (pkey == NULL) ||
  1675. (pkey->type != EVP_PKEY_RSA) ||
  1676. (pkey->pkey.rsa == NULL))
  1677. {
  1678. al=SSL_AD_HANDSHAKE_FAILURE;
  1679. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_MISSING_RSA_CERTIFICATE);
  1680. goto f_err;
  1681. }
  1682. rsa=pkey->pkey.rsa;
  1683. /* TLS and [incidentally] DTLS{0xFEFF} */
  1684. if (s->version > SSL3_VERSION)
  1685. {
  1686. CBS copy = client_key_exchange;
  1687. if (!CBS_get_u16_length_prefixed(&client_key_exchange,
  1688. &encrypted_premaster_secret) ||
  1689. CBS_len(&client_key_exchange) != 0)
  1690. {
  1691. if (!(s->options & SSL_OP_TLS_D5_BUG))
  1692. {
  1693. al = SSL_AD_DECODE_ERROR;
  1694. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
  1695. goto f_err;
  1696. }
  1697. else
  1698. encrypted_premaster_secret = copy;
  1699. }
  1700. }
  1701. else
  1702. encrypted_premaster_secret = client_key_exchange;
  1703. /* Reject overly short RSA keys because we want to be sure that
  1704. * the buffer size makes it safe to iterate over the entire size
  1705. * of a premaster secret (SSL_MAX_MASTER_KEY_LENGTH). The actual
  1706. * expected size is larger due to RSA padding, but the bound is
  1707. * sufficient to be safe. */
  1708. rsa_size = RSA_size(rsa);
  1709. if (rsa_size < SSL_MAX_MASTER_KEY_LENGTH)
  1710. {
  1711. al = SSL_AD_DECRYPT_ERROR;
  1712. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECRYPTION_FAILED);
  1713. goto f_err;
  1714. }
  1715. /* We must not leak whether a decryption failure occurs because
  1716. * of Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see
  1717. * RFC 2246, section 7.4.7.1). The code follows that advice of
  1718. * the TLS RFC and generates a random premaster secret for the
  1719. * case that the decrypt fails. See
  1720. * https://tools.ietf.org/html/rfc5246#section-7.4.7.1 */
  1721. if (RAND_pseudo_bytes(rand_premaster_secret,
  1722. sizeof(rand_premaster_secret)) <= 0)
  1723. goto err;
  1724. /* Allocate a buffer large enough for an RSA decryption. */
  1725. decrypt_buf = OPENSSL_malloc(rsa_size);
  1726. if (decrypt_buf == NULL)
  1727. {
  1728. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE);
  1729. goto err;
  1730. }
  1731. /* Decrypt with no padding. PKCS#1 padding will be removed as
  1732. * part of the timing-sensitive code below. */
  1733. if (!RSA_decrypt(rsa, &decrypt_len, decrypt_buf, rsa_size,
  1734. CBS_data(&encrypted_premaster_secret),
  1735. CBS_len(&encrypted_premaster_secret),
  1736. RSA_NO_PADDING))
  1737. {
  1738. goto err;
  1739. }
  1740. if (decrypt_len != rsa_size)
  1741. {
  1742. /* This should never happen, but do a check so we do not
  1743. * read uninitialized memory. */
  1744. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_INTERNAL_ERROR);
  1745. goto err;
  1746. }
  1747. /* Remove the PKCS#1 padding and adjust decrypt_len as
  1748. * appropriate. decrypt_good_mask will be zero if the premaster
  1749. * if good and non-zero otherwise. */
  1750. decrypt_good_mask = RSA_message_index_PKCS1_type_2(
  1751. decrypt_buf, decrypt_len, &premaster_index);
  1752. decrypt_good_mask--;
  1753. decrypt_len = decrypt_len - premaster_index;
  1754. /* decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. */
  1755. decrypt_good_mask |= decrypt_len ^ SSL_MAX_MASTER_KEY_LENGTH;
  1756. /* Copy over the unpadded premaster. Whatever the value of
  1757. * |decrypt_good_mask|, copy as if the premaster were the right
  1758. * length. It is important the memory access pattern be
  1759. * constant. */
  1760. premaster_secret = BUF_memdup(
  1761. decrypt_buf + (rsa_size - SSL_MAX_MASTER_KEY_LENGTH),
  1762. SSL_MAX_MASTER_KEY_LENGTH);
  1763. if (premaster_secret == NULL)
  1764. {
  1765. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE);
  1766. goto err;
  1767. }
  1768. OPENSSL_free(decrypt_buf);
  1769. decrypt_buf = NULL;
  1770. /* If the version in the decrypted pre-master secret is correct
  1771. * then version_good will be zero. The Klima-Pokorny-Rosa
  1772. * extension of Bleichenbacher's attack
  1773. * (http://eprint.iacr.org/2003/052/) exploits the version
  1774. * number check as a "bad version oracle". Thus version checks
  1775. * are done in constant time and are treated like any other
  1776. * decryption error. */
  1777. version_good = premaster_secret[0] ^ (s->client_version>>8);
  1778. version_good |= premaster_secret[1] ^ (s->client_version&0xff);
  1779. /* If any bits in version_good are set then they'll poision
  1780. * decrypt_good_mask and cause rand_premaster_secret to be
  1781. * used. */
  1782. decrypt_good_mask |= version_good;
  1783. /* decrypt_good_mask will be zero iff decrypt_len ==
  1784. * SSL_MAX_MASTER_KEY_LENGTH and the version check passed. We
  1785. * fold the bottom 32 bits of it with an OR so that the LSB
  1786. * will be zero iff everything is good. This assumes that we'll
  1787. * never decrypt a value > 2**31 bytes, which seems safe. */
  1788. decrypt_good_mask |= decrypt_good_mask >> 16;
  1789. decrypt_good_mask |= decrypt_good_mask >> 8;
  1790. decrypt_good_mask |= decrypt_good_mask >> 4;
  1791. decrypt_good_mask |= decrypt_good_mask >> 2;
  1792. decrypt_good_mask |= decrypt_good_mask >> 1;
  1793. /* Now select only the LSB and subtract one. If decrypt_len ==
  1794. * SSL_MAX_MASTER_KEY_LENGTH and the version check passed then
  1795. * decrypt_good_mask will be all ones. Otherwise it'll be all
  1796. * zeros. */
  1797. decrypt_good_mask &= 1;
  1798. decrypt_good_mask--;
  1799. /* Now copy rand_premaster_secret over premaster_secret using
  1800. * decrypt_good_mask. */
  1801. for (j = 0; j < sizeof(rand_premaster_secret); j++)
  1802. {
  1803. premaster_secret[j] = (premaster_secret[j] & decrypt_good_mask) |
  1804. (rand_premaster_secret[j] & ~decrypt_good_mask);
  1805. }
  1806. premaster_secret_len = sizeof(rand_premaster_secret);
  1807. }
  1808. else if (alg_k & SSL_kEDH)
  1809. {
  1810. CBS dh_Yc;
  1811. int dh_len;
  1812. if (!CBS_get_u16_length_prefixed(&client_key_exchange, &dh_Yc) ||
  1813. CBS_len(&dh_Yc) == 0 ||
  1814. CBS_len(&client_key_exchange) != 0)
  1815. {
  1816. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
  1817. al = SSL_R_DECODE_ERROR;
  1818. goto f_err;
  1819. }
  1820. if (s->s3->tmp.dh == NULL)
  1821. {
  1822. al=SSL_AD_HANDSHAKE_FAILURE;
  1823. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_MISSING_TMP_DH_KEY);
  1824. goto f_err;
  1825. }
  1826. dh_srvr=s->s3->tmp.dh;
  1827. pub = BN_bin2bn(CBS_data(&dh_Yc), CBS_len(&dh_Yc), NULL);
  1828. if (pub == NULL)
  1829. {
  1830. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_BN_LIB);
  1831. goto err;
  1832. }
  1833. /* Allocate a buffer for the premaster secret. */
  1834. premaster_secret = OPENSSL_malloc(DH_size(dh_srvr));
  1835. if (premaster_secret == NULL)
  1836. {
  1837. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE);
  1838. goto err;
  1839. }
  1840. dh_len = DH_compute_key(premaster_secret, pub, dh_srvr);
  1841. if (dh_len <= 0)
  1842. {
  1843. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_DH_LIB);
  1844. BN_clear_free(pub);
  1845. goto err;
  1846. }
  1847. DH_free(s->s3->tmp.dh);
  1848. s->s3->tmp.dh=NULL;
  1849. BN_clear_free(pub);
  1850. pub=NULL;
  1851. premaster_secret_len = dh_len;
  1852. }
  1853. else if (alg_k & SSL_kEECDH)
  1854. {
  1855. int field_size = 0, ecdh_len;
  1856. const EC_KEY *tkey;
  1857. const EC_GROUP *group;
  1858. const BIGNUM *priv_key;
  1859. CBS ecdh_Yc;
  1860. /* initialize structures for server's ECDH key pair */
  1861. if ((srvr_ecdh = EC_KEY_new()) == NULL)
  1862. {
  1863. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE);
  1864. goto err;
  1865. }
  1866. /* Use the ephermeral values we saved when generating the
  1867. * ServerKeyExchange msg. */
  1868. tkey = s->s3->tmp.ecdh;
  1869. group = EC_KEY_get0_group(tkey);
  1870. priv_key = EC_KEY_get0_private_key(tkey);
  1871. if (!EC_KEY_set_group(srvr_ecdh, group) ||
  1872. !EC_KEY_set_private_key(srvr_ecdh, priv_key))
  1873. {
  1874. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB);
  1875. goto err;
  1876. }
  1877. /* Let's get client's public key */
  1878. if ((clnt_ecpoint = EC_POINT_new(group)) == NULL)
  1879. {
  1880. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE);
  1881. goto err;
  1882. }
  1883. /* Get client's public key from encoded point
  1884. * in the ClientKeyExchange message.
  1885. */
  1886. if (!CBS_get_u8_length_prefixed(&client_key_exchange, &ecdh_Yc) ||
  1887. CBS_len(&client_key_exchange) != 0)
  1888. {
  1889. al = SSL_AD_DECODE_ERROR;
  1890. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECODE_ERROR);
  1891. goto f_err;
  1892. }
  1893. if ((bn_ctx = BN_CTX_new()) == NULL)
  1894. {
  1895. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE);
  1896. goto err;
  1897. }
  1898. if (!EC_POINT_oct2point(group, clnt_ecpoint,
  1899. CBS_data(&ecdh_Yc), CBS_len(&ecdh_Yc), bn_ctx))
  1900. {
  1901. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB);
  1902. goto err;
  1903. }
  1904. /* Allocate a buffer for both the secret and the PSK. */
  1905. field_size = EC_GROUP_get_degree(group);
  1906. if (field_size <= 0)
  1907. {
  1908. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_ECDH_LIB);
  1909. goto err;
  1910. }
  1911. ecdh_len = (field_size + 7) / 8;
  1912. premaster_secret = OPENSSL_malloc(ecdh_len);
  1913. if (premaster_secret == NULL)
  1914. {
  1915. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE);
  1916. goto err;
  1917. }
  1918. /* Compute the shared pre-master secret */
  1919. ecdh_len = ECDH_compute_key(premaster_secret,
  1920. ecdh_len, clnt_ecpoint, srvr_ecdh, NULL);
  1921. if (ecdh_len <= 0)
  1922. {
  1923. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_ECDH_LIB);
  1924. goto err;
  1925. }
  1926. EVP_PKEY_free(clnt_pub_pkey);
  1927. EC_POINT_free(clnt_ecpoint);
  1928. EC_KEY_free(srvr_ecdh);
  1929. BN_CTX_free(bn_ctx);
  1930. EC_KEY_free(s->s3->tmp.ecdh);
  1931. s->s3->tmp.ecdh = NULL;
  1932. premaster_secret_len = ecdh_len;
  1933. }
  1934. else if (alg_k & SSL_kPSK)
  1935. {
  1936. /* For plain PSK, other_secret is a block of 0s with the same
  1937. * length as the pre-shared key. */
  1938. premaster_secret_len = psk_len;
  1939. premaster_secret = OPENSSL_malloc(premaster_secret_len);
  1940. if (premaster_secret == NULL)
  1941. {
  1942. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE);
  1943. goto err;
  1944. }
  1945. memset(premaster_secret, 0, premaster_secret_len);
  1946. }
  1947. else
  1948. {
  1949. al=SSL_AD_HANDSHAKE_FAILURE;
  1950. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_UNKNOWN_CIPHER_TYPE);
  1951. goto f_err;
  1952. }
  1953. /* For a PSK cipher suite, the actual pre-master secret is combined with
  1954. * the pre-shared key. */
  1955. if (alg_a & SSL_aPSK)
  1956. {
  1957. CBB new_premaster, child;
  1958. uint8_t *new_data;
  1959. size_t new_len;
  1960. if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len))
  1961. {
  1962. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_MALLOC_FAILURE);
  1963. goto err;
  1964. }
  1965. if (!CBB_add_u16_length_prefixed(&new_premaster, &child) ||
  1966. !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
  1967. !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
  1968. !CBB_add_bytes(&child, psk, psk_len) ||
  1969. !CBB_finish(&new_premaster, &new_data, &new_len))
  1970. {
  1971. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_INTERNAL_ERROR);
  1972. CBB_cleanup(&new_premaster);
  1973. goto err;
  1974. }
  1975. OPENSSL_cleanse(premaster_secret, premaster_secret_len);
  1976. OPENSSL_free(premaster_secret);
  1977. premaster_secret = new_data;
  1978. premaster_secret_len = new_len;
  1979. }
  1980. /* Compute the master secret */
  1981. s->session->master_key_length = s->method->ssl3_enc
  1982. ->generate_master_secret(s,
  1983. s->session->master_key, premaster_secret, premaster_secret_len);
  1984. OPENSSL_cleanse(premaster_secret, premaster_secret_len);
  1985. OPENSSL_free(premaster_secret);
  1986. return 1;
  1987. f_err:
  1988. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  1989. err:
  1990. if (premaster_secret)
  1991. {
  1992. if (premaster_secret_len)
  1993. OPENSSL_cleanse(premaster_secret, premaster_secret_len);
  1994. OPENSSL_free(premaster_secret);
  1995. }
  1996. if (decrypt_buf)
  1997. OPENSSL_free(decrypt_buf);
  1998. EVP_PKEY_free(clnt_pub_pkey);
  1999. EC_POINT_free(clnt_ecpoint);
  2000. if (srvr_ecdh != NULL)
  2001. EC_KEY_free(srvr_ecdh);
  2002. BN_CTX_free(bn_ctx);
  2003. return(-1);
  2004. }
  2005. int ssl3_get_cert_verify(SSL *s)
  2006. {
  2007. int al,ok,ret=0;
  2008. long n;
  2009. CBS certificate_verify, signature;
  2010. X509 *peer = s->session->peer;
  2011. EVP_PKEY *pkey = NULL;
  2012. const EVP_MD *md = NULL;
  2013. uint8_t digest[EVP_MAX_MD_SIZE];
  2014. size_t digest_length;
  2015. EVP_PKEY_CTX *pctx = NULL;
  2016. /* Only RSA and ECDSA client certificates are supported, so a
  2017. * CertificateVerify is required if and only if there's a
  2018. * client certificate. */
  2019. if (peer == NULL)
  2020. {
  2021. if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s))
  2022. return -1;
  2023. return 1;
  2024. }
  2025. n=s->method->ssl_get_message(s,
  2026. SSL3_ST_SR_CERT_VRFY_A,
  2027. SSL3_ST_SR_CERT_VRFY_B,
  2028. SSL3_MT_CERTIFICATE_VERIFY,
  2029. SSL3_RT_MAX_PLAIN_LENGTH,
  2030. SSL_GET_MESSAGE_DONT_HASH_MESSAGE,
  2031. &ok);
  2032. if (!ok)
  2033. return (int)n;
  2034. /* Filter out unsupported certificate types. */
  2035. pkey = X509_get_pubkey(peer);
  2036. if (!(X509_certificate_type(peer, pkey) & EVP_PKT_SIGN) ||
  2037. (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_EC))
  2038. {
  2039. al = SSL_AD_UNSUPPORTED_CERTIFICATE;
  2040. OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
  2041. goto f_err;
  2042. }
  2043. CBS_init(&certificate_verify, s->init_msg, n);
  2044. /* Determine the digest type if needbe. */
  2045. if (SSL_USE_SIGALGS(s))
  2046. {
  2047. if (!tls12_check_peer_sigalg(&md, &al, s, &certificate_verify, pkey))
  2048. goto f_err;
  2049. }
  2050. /* Compute the digest. */
  2051. if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey))
  2052. goto err;
  2053. /* The handshake buffer is no longer necessary, and we may hash the
  2054. * current message.*/
  2055. if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s))
  2056. goto err;
  2057. ssl3_hash_current_message(s);
  2058. /* Parse and verify the signature. */
  2059. if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
  2060. CBS_len(&certificate_verify) != 0)
  2061. {
  2062. al = SSL_AD_DECODE_ERROR;
  2063. OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_DECODE_ERROR);
  2064. goto f_err;
  2065. }
  2066. pctx = EVP_PKEY_CTX_new(pkey, NULL);
  2067. if (pctx == NULL)
  2068. goto err;
  2069. if (!EVP_PKEY_verify_init(pctx) ||
  2070. !EVP_PKEY_CTX_set_signature_md(pctx, md) ||
  2071. !EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature),
  2072. digest, digest_length))
  2073. {
  2074. al = SSL_AD_DECRYPT_ERROR;
  2075. OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_BAD_SIGNATURE);
  2076. goto f_err;
  2077. }
  2078. ret = 1;
  2079. if (0)
  2080. {
  2081. f_err:
  2082. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  2083. }
  2084. err:
  2085. EVP_PKEY_CTX_free(pctx);
  2086. EVP_PKEY_free(pkey);
  2087. return(ret);
  2088. }
  2089. int ssl3_get_client_certificate(SSL *s)
  2090. {
  2091. int i,ok,al,ret= -1;
  2092. X509 *x=NULL;
  2093. unsigned long n;
  2094. STACK_OF(X509) *sk=NULL;
  2095. SHA256_CTX sha256;
  2096. CBS certificate_msg, certificate_list;
  2097. int is_first_certificate = 1;
  2098. n=s->method->ssl_get_message(s,
  2099. SSL3_ST_SR_CERT_A,
  2100. SSL3_ST_SR_CERT_B,
  2101. -1,
  2102. s->max_cert_list,
  2103. SSL_GET_MESSAGE_HASH_MESSAGE,
  2104. &ok);
  2105. if (!ok) return((int)n);
  2106. if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE)
  2107. {
  2108. if ( (s->verify_mode & SSL_VERIFY_PEER) &&
  2109. (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
  2110. {
  2111. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  2112. al=SSL_AD_HANDSHAKE_FAILURE;
  2113. goto f_err;
  2114. }
  2115. /* If tls asked for a client cert, the client must return a 0 list */
  2116. if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request)
  2117. {
  2118. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
  2119. al=SSL_AD_UNEXPECTED_MESSAGE;
  2120. goto f_err;
  2121. }
  2122. s->s3->tmp.reuse_message=1;
  2123. return(1);
  2124. }
  2125. if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
  2126. {
  2127. al=SSL_AD_UNEXPECTED_MESSAGE;
  2128. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_WRONG_MESSAGE_TYPE);
  2129. goto f_err;
  2130. }
  2131. CBS_init(&certificate_msg, s->init_msg, n);
  2132. if ((sk=sk_X509_new_null()) == NULL)
  2133. {
  2134. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE);
  2135. goto err;
  2136. }
  2137. if (!CBS_get_u24_length_prefixed(&certificate_msg, &certificate_list) ||
  2138. CBS_len(&certificate_msg) != 0)
  2139. {
  2140. al = SSL_AD_DECODE_ERROR;
  2141. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_DECODE_ERROR);
  2142. goto f_err;
  2143. }
  2144. while (CBS_len(&certificate_list) > 0)
  2145. {
  2146. CBS certificate;
  2147. const uint8_t *data;
  2148. if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate))
  2149. {
  2150. al = SSL_AD_DECODE_ERROR;
  2151. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_DECODE_ERROR);
  2152. goto f_err;
  2153. }
  2154. if (is_first_certificate && s->ctx->retain_only_sha256_of_client_certs)
  2155. {
  2156. /* If this is the first certificate, and we don't want
  2157. * to keep peer certificates in memory, then we hash it
  2158. * right away. */
  2159. SHA256_Init(&sha256);
  2160. SHA256_Update(&sha256, CBS_data(&certificate), CBS_len(&certificate));
  2161. SHA256_Final(s->session->peer_sha256, &sha256);
  2162. s->session->peer_sha256_valid = 1;
  2163. }
  2164. is_first_certificate = 0;
  2165. data = CBS_data(&certificate);
  2166. x = d2i_X509(NULL, &data, CBS_len(&certificate));
  2167. if (x == NULL)
  2168. {
  2169. al = SSL_AD_BAD_CERTIFICATE;
  2170. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_ASN1_LIB);
  2171. goto f_err;
  2172. }
  2173. if (!CBS_skip(&certificate, data - CBS_data(&certificate)))
  2174. {
  2175. al = SSL_AD_INTERNAL_ERROR;
  2176. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_INTERNAL_ERROR);
  2177. goto f_err;
  2178. }
  2179. if (CBS_len(&certificate) != 0)
  2180. {
  2181. al = SSL_AD_DECODE_ERROR;
  2182. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_CERT_LENGTH_MISMATCH);
  2183. goto f_err;
  2184. }
  2185. if (!sk_X509_push(sk,x))
  2186. {
  2187. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE);
  2188. goto err;
  2189. }
  2190. x = NULL;
  2191. }
  2192. if (sk_X509_num(sk) <= 0)
  2193. {
  2194. /* TLS does not mind 0 certs returned */
  2195. if (s->version == SSL3_VERSION)
  2196. {
  2197. al=SSL_AD_HANDSHAKE_FAILURE;
  2198. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_NO_CERTIFICATES_RETURNED);
  2199. goto f_err;
  2200. }
  2201. /* Fail for TLS only if we required a certificate */
  2202. else if ((s->verify_mode & SSL_VERIFY_PEER) &&
  2203. (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
  2204. {
  2205. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  2206. al=SSL_AD_HANDSHAKE_FAILURE;
  2207. goto f_err;
  2208. }
  2209. /* No client certificate so digest cached records */
  2210. if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s))
  2211. {
  2212. al=SSL_AD_INTERNAL_ERROR;
  2213. goto f_err;
  2214. }
  2215. }
  2216. else
  2217. {
  2218. i=ssl_verify_cert_chain(s,sk);
  2219. if (i <= 0)
  2220. {
  2221. al=ssl_verify_alarm_type(s->verify_result);
  2222. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_CERTIFICATE_VERIFY_FAILED);
  2223. goto f_err;
  2224. }
  2225. }
  2226. if (s->session->peer != NULL) /* This should not be needed */
  2227. X509_free(s->session->peer);
  2228. s->session->peer=sk_X509_shift(sk);
  2229. s->session->verify_result = s->verify_result;
  2230. /* With the current implementation, sess_cert will always be NULL
  2231. * when we arrive here. */
  2232. if (s->session->sess_cert == NULL)
  2233. {
  2234. s->session->sess_cert = ssl_sess_cert_new();
  2235. if (s->session->sess_cert == NULL)
  2236. {
  2237. OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE);
  2238. goto err;
  2239. }
  2240. }
  2241. if (s->session->sess_cert->cert_chain != NULL)
  2242. sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
  2243. s->session->sess_cert->cert_chain=sk;
  2244. /* Inconsistency alert: cert_chain does *not* include the
  2245. * peer's own certificate, while we do include it in s3_clnt.c */
  2246. sk=NULL;
  2247. ret=1;
  2248. if (0)
  2249. {
  2250. f_err:
  2251. ssl3_send_alert(s,SSL3_AL_FATAL,al);
  2252. }
  2253. err:
  2254. if (x != NULL) X509_free(x);
  2255. if (sk != NULL) sk_X509_pop_free(sk,X509_free);
  2256. return(ret);
  2257. }
  2258. int ssl3_send_server_certificate(SSL *s)
  2259. {
  2260. CERT_PKEY *cpk;
  2261. if (s->state == SSL3_ST_SW_CERT_A)
  2262. {
  2263. cpk=ssl_get_server_send_pkey(s);
  2264. if (cpk == NULL)
  2265. {
  2266. OPENSSL_PUT_ERROR(SSL, ssl3_send_server_certificate, ERR_R_INTERNAL_ERROR);
  2267. return(0);
  2268. }
  2269. ssl3_output_cert_chain(s,cpk);
  2270. s->state=SSL3_ST_SW_CERT_B;
  2271. }
  2272. /* SSL3_ST_SW_CERT_B */
  2273. return ssl_do_write(s);
  2274. }
  2275. /* send a new session ticket (not necessarily for a new session) */
  2276. int ssl3_send_new_session_ticket(SSL *s)
  2277. {
  2278. if (s->state == SSL3_ST_SW_SESSION_TICKET_A)
  2279. {
  2280. unsigned char *p, *senc, *macstart;
  2281. const unsigned char *const_p;
  2282. int len, slen_full, slen;
  2283. SSL_SESSION *sess;
  2284. unsigned int hlen;
  2285. EVP_CIPHER_CTX ctx;
  2286. HMAC_CTX hctx;
  2287. SSL_CTX *tctx = s->initial_ctx;
  2288. unsigned char iv[EVP_MAX_IV_LENGTH];
  2289. unsigned char key_name[16];
  2290. /* get session encoding length */
  2291. slen_full = i2d_SSL_SESSION(s->session, NULL);
  2292. /* Some length values are 16 bits, so forget it if session is
  2293. * too long
  2294. */
  2295. if (slen_full > 0xFF00)
  2296. return -1;
  2297. senc = OPENSSL_malloc(slen_full);
  2298. if (!senc)
  2299. return -1;
  2300. p = senc;
  2301. i2d_SSL_SESSION(s->session, &p);
  2302. /* create a fresh copy (not shared with other threads) to clean up */
  2303. const_p = senc;
  2304. sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
  2305. if (sess == NULL)
  2306. {
  2307. OPENSSL_free(senc);
  2308. return -1;
  2309. }
  2310. sess->session_id_length = 0; /* ID is irrelevant for the ticket */
  2311. slen = i2d_SSL_SESSION(sess, NULL);
  2312. if (slen > slen_full) /* shouldn't ever happen */
  2313. {
  2314. OPENSSL_free(senc);
  2315. return -1;
  2316. }
  2317. p = senc;
  2318. i2d_SSL_SESSION(sess, &p);
  2319. SSL_SESSION_free(sess);
  2320. /* Grow buffer if need be: the length calculation is as
  2321. * follows handshake_header_length +
  2322. * 4 (ticket lifetime hint) + 2 (ticket length) +
  2323. * 16 (key name) + max_iv_len (iv length) +
  2324. * session_length + max_enc_block_size (max encrypted session
  2325. * length) + max_md_size (HMAC).
  2326. */
  2327. if (!BUF_MEM_grow(s->init_buf,
  2328. SSL_HM_HEADER_LENGTH(s) + 22 + EVP_MAX_IV_LENGTH +
  2329. EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen))
  2330. return -1;
  2331. p = ssl_handshake_start(s);
  2332. EVP_CIPHER_CTX_init(&ctx);
  2333. HMAC_CTX_init(&hctx);
  2334. /* Initialize HMAC and cipher contexts. If callback present
  2335. * it does all the work otherwise use generated values
  2336. * from parent ctx.
  2337. */
  2338. if (tctx->tlsext_ticket_key_cb)
  2339. {
  2340. if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx,
  2341. &hctx, 1) < 0)
  2342. {
  2343. OPENSSL_free(senc);
  2344. return -1;
  2345. }
  2346. }
  2347. else
  2348. {
  2349. RAND_pseudo_bytes(iv, 16);
  2350. EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
  2351. tctx->tlsext_tick_aes_key, iv);
  2352. HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
  2353. tlsext_tick_md(), NULL);
  2354. memcpy(key_name, tctx->tlsext_tick_key_name, 16);
  2355. }
  2356. /* Ticket lifetime hint (advisory only):
  2357. * We leave this unspecified for resumed session (for simplicity),
  2358. * and guess that tickets for new sessions will live as long
  2359. * as their sessions. */
  2360. l2n(s->hit ? 0 : s->session->timeout, p);
  2361. /* Skip ticket length for now */
  2362. p += 2;
  2363. /* Output key name */
  2364. macstart = p;
  2365. memcpy(p, key_name, 16);
  2366. p += 16;
  2367. /* output IV */
  2368. memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
  2369. p += EVP_CIPHER_CTX_iv_length(&ctx);
  2370. /* Encrypt session data */
  2371. EVP_EncryptUpdate(&ctx, p, &len, senc, slen);
  2372. p += len;
  2373. EVP_EncryptFinal_ex(&ctx, p, &len);
  2374. p += len;
  2375. EVP_CIPHER_CTX_cleanup(&ctx);
  2376. HMAC_Update(&hctx, macstart, p - macstart);
  2377. HMAC_Final(&hctx, p, &hlen);
  2378. HMAC_CTX_cleanup(&hctx);
  2379. p += hlen;
  2380. /* Now write out lengths: p points to end of data written */
  2381. /* Total length */
  2382. len = p - ssl_handshake_start(s);
  2383. ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len);
  2384. /* Skip ticket lifetime hint */
  2385. p = ssl_handshake_start(s) + 4;
  2386. s2n(len - 6, p);
  2387. s->state=SSL3_ST_SW_SESSION_TICKET_B;
  2388. OPENSSL_free(senc);
  2389. }
  2390. /* SSL3_ST_SW_SESSION_TICKET_B */
  2391. return ssl_do_write(s);
  2392. }
  2393. #if 0
  2394. int ssl3_send_cert_status(SSL *s)
  2395. {
  2396. if (s->state == SSL3_ST_SW_CERT_STATUS_A)
  2397. {
  2398. unsigned char *p;
  2399. /* Grow buffer if need be: the length calculation is as
  2400. * follows 1 (message type) + 3 (message length) +
  2401. * 1 (ocsp response type) + 3 (ocsp response length)
  2402. * + (ocsp response)
  2403. */
  2404. if (!BUF_MEM_grow(s->init_buf, 8 + s->tlsext_ocsp_resplen))
  2405. return -1;
  2406. p=(unsigned char *)s->init_buf->data;
  2407. /* do the header */
  2408. *(p++)=SSL3_MT_CERTIFICATE_STATUS;
  2409. /* message length */
  2410. l2n3(s->tlsext_ocsp_resplen + 4, p);
  2411. /* status type */
  2412. *(p++)= s->tlsext_status_type;
  2413. /* length of OCSP response */
  2414. l2n3(s->tlsext_ocsp_resplen, p);
  2415. /* actual response */
  2416. memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen);
  2417. /* number of bytes to write */
  2418. s->init_num = 8 + s->tlsext_ocsp_resplen;
  2419. s->state=SSL3_ST_SW_CERT_STATUS_B;
  2420. s->init_off = 0;
  2421. }
  2422. /* SSL3_ST_SW_CERT_STATUS_B */
  2423. return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
  2424. }
  2425. #endif
  2426. /* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
  2427. * sets the next_proto member in s if found */
  2428. int ssl3_get_next_proto(SSL *s)
  2429. {
  2430. int ok;
  2431. long n;
  2432. CBS next_protocol, selected_protocol, padding;
  2433. /* Clients cannot send a NextProtocol message if we didn't see the
  2434. * extension in their ClientHello */
  2435. if (!s->s3->next_proto_neg_seen)
  2436. {
  2437. OPENSSL_PUT_ERROR(SSL, ssl3_get_next_proto, SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION);
  2438. return -1;
  2439. }
  2440. n=s->method->ssl_get_message(s,
  2441. SSL3_ST_SR_NEXT_PROTO_A,
  2442. SSL3_ST_SR_NEXT_PROTO_B,
  2443. SSL3_MT_NEXT_PROTO,
  2444. 514, /* See the payload format below */
  2445. SSL_GET_MESSAGE_HASH_MESSAGE,
  2446. &ok);
  2447. if (!ok)
  2448. return((int)n);
  2449. /* s->state doesn't reflect whether ChangeCipherSpec has been received
  2450. * in this handshake, but s->s3->change_cipher_spec does (will be reset
  2451. * by ssl3_get_finished).
  2452. * TODO(davidben): Is this check now redundant with
  2453. * SSL3_FLAGS_EXPECT_CCS? */
  2454. if (!s->s3->change_cipher_spec)
  2455. {
  2456. OPENSSL_PUT_ERROR(SSL, ssl3_get_next_proto, SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS);
  2457. return -1;
  2458. }
  2459. CBS_init(&next_protocol, s->init_msg, n);
  2460. /* The payload looks like:
  2461. * uint8 proto_len;
  2462. * uint8 proto[proto_len];
  2463. * uint8 padding_len;
  2464. * uint8 padding[padding_len];
  2465. */
  2466. if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
  2467. !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
  2468. CBS_len(&next_protocol) != 0)
  2469. return 0;
  2470. if (!CBS_stow(&selected_protocol,
  2471. &s->next_proto_negotiated,
  2472. &s->next_proto_negotiated_len))
  2473. return 0;
  2474. return 1;
  2475. }
  2476. /* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
  2477. int ssl3_get_channel_id(SSL *s)
  2478. {
  2479. int ret = -1, ok;
  2480. long n;
  2481. EVP_MD_CTX md_ctx;
  2482. uint8_t channel_id_hash[SHA256_DIGEST_LENGTH];
  2483. unsigned int channel_id_hash_len;
  2484. const uint8_t *p;
  2485. uint16_t extension_type, expected_extension_type;
  2486. EC_GROUP* p256 = NULL;
  2487. EC_KEY* key = NULL;
  2488. EC_POINT* point = NULL;
  2489. ECDSA_SIG sig;
  2490. BIGNUM x, y;
  2491. CBS encrypted_extensions, extension;
  2492. n = s->method->ssl_get_message(s,
  2493. SSL3_ST_SR_CHANNEL_ID_A,
  2494. SSL3_ST_SR_CHANNEL_ID_B,
  2495. SSL3_MT_ENCRYPTED_EXTENSIONS,
  2496. 2 + 2 + TLSEXT_CHANNEL_ID_SIZE,
  2497. SSL_GET_MESSAGE_DONT_HASH_MESSAGE,
  2498. &ok);
  2499. if (!ok)
  2500. return((int)n);
  2501. /* Before incorporating the EncryptedExtensions message to the
  2502. * handshake hash, compute the hash that should have been signed. */
  2503. channel_id_hash_len = sizeof(channel_id_hash);
  2504. EVP_MD_CTX_init(&md_ctx);
  2505. if (!EVP_DigestInit_ex(&md_ctx, EVP_sha256(), NULL) ||
  2506. !tls1_channel_id_hash(&md_ctx, s) ||
  2507. !EVP_DigestFinal(&md_ctx, channel_id_hash, &channel_id_hash_len))
  2508. {
  2509. EVP_MD_CTX_cleanup(&md_ctx);
  2510. return -1;
  2511. }
  2512. EVP_MD_CTX_cleanup(&md_ctx);
  2513. assert(channel_id_hash_len == SHA256_DIGEST_LENGTH);
  2514. ssl3_hash_current_message(s);
  2515. /* s->state doesn't reflect whether ChangeCipherSpec has been received
  2516. * in this handshake, but s->s3->change_cipher_spec does (will be reset
  2517. * by ssl3_get_finished).
  2518. * TODO(davidben): Is this check now redundant with
  2519. * SSL3_FLAGS_EXPECT_CCS? */
  2520. if (!s->s3->change_cipher_spec)
  2521. {
  2522. OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_GOT_CHANNEL_ID_BEFORE_A_CCS);
  2523. return -1;
  2524. }
  2525. CBS_init(&encrypted_extensions, s->init_msg, n);
  2526. /* EncryptedExtensions could include multiple extensions, but
  2527. * the only extension that could be negotiated is ChannelID,
  2528. * so there can only be one entry.
  2529. *
  2530. * The payload looks like:
  2531. * uint16 extension_type
  2532. * uint16 extension_len;
  2533. * uint8 x[32];
  2534. * uint8 y[32];
  2535. * uint8 r[32];
  2536. * uint8 s[32];
  2537. */
  2538. expected_extension_type = TLSEXT_TYPE_channel_id;
  2539. if (s->s3->tlsext_channel_id_new)
  2540. expected_extension_type = TLSEXT_TYPE_channel_id_new;
  2541. if (!CBS_get_u16(&encrypted_extensions, &extension_type) ||
  2542. !CBS_get_u16_length_prefixed(&encrypted_extensions, &extension) ||
  2543. CBS_len(&encrypted_extensions) != 0 ||
  2544. extension_type != expected_extension_type ||
  2545. CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE)
  2546. {
  2547. OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_INVALID_MESSAGE);
  2548. return -1;
  2549. }
  2550. p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
  2551. if (!p256)
  2552. {
  2553. OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_NO_P256_SUPPORT);
  2554. return -1;
  2555. }
  2556. BN_init(&x);
  2557. BN_init(&y);
  2558. sig.r = BN_new();
  2559. sig.s = BN_new();
  2560. p = CBS_data(&extension);
  2561. if (BN_bin2bn(p + 0, 32, &x) == NULL ||
  2562. BN_bin2bn(p + 32, 32, &y) == NULL ||
  2563. BN_bin2bn(p + 64, 32, sig.r) == NULL ||
  2564. BN_bin2bn(p + 96, 32, sig.s) == NULL)
  2565. goto err;
  2566. point = EC_POINT_new(p256);
  2567. if (!point ||
  2568. !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL))
  2569. goto err;
  2570. key = EC_KEY_new();
  2571. if (!key ||
  2572. !EC_KEY_set_group(key, p256) ||
  2573. !EC_KEY_set_public_key(key, point))
  2574. goto err;
  2575. /* We stored the handshake hash in |tlsext_channel_id| the first time
  2576. * that we were called. */
  2577. if (!ECDSA_do_verify(channel_id_hash, channel_id_hash_len, &sig, key))
  2578. {
  2579. OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
  2580. s->s3->tlsext_channel_id_valid = 0;
  2581. goto err;
  2582. }
  2583. memcpy(s->s3->tlsext_channel_id, p, 64);
  2584. ret = 1;
  2585. err:
  2586. BN_free(&x);
  2587. BN_free(&y);
  2588. BN_free(sig.r);
  2589. BN_free(sig.s);
  2590. if (key)
  2591. EC_KEY_free(key);
  2592. if (point)
  2593. EC_POINT_free(point);
  2594. if (p256)
  2595. EC_GROUP_free(p256);
  2596. return ret;
  2597. }