Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

2021 Zeilen
63 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. #include <openssl/ssl.h>
  149. #include <assert.h>
  150. #include <string.h>
  151. #include <openssl/bn.h>
  152. #include <openssl/buf.h>
  153. #include <openssl/bytestring.h>
  154. #include <openssl/cipher.h>
  155. #include <openssl/dh.h>
  156. #include <openssl/ec.h>
  157. #include <openssl/ecdsa.h>
  158. #include <openssl/err.h>
  159. #include <openssl/evp.h>
  160. #include <openssl/hmac.h>
  161. #include <openssl/md5.h>
  162. #include <openssl/mem.h>
  163. #include <openssl/nid.h>
  164. #include <openssl/rand.h>
  165. #include <openssl/sha.h>
  166. #include <openssl/x509.h>
  167. #include "internal.h"
  168. #include "../crypto/internal.h"
  169. #include "../crypto/dh/internal.h"
  170. static int ssl3_get_client_hello(SSL *ssl);
  171. static int ssl3_send_server_hello(SSL *ssl);
  172. static int ssl3_send_server_certificate(SSL *ssl);
  173. static int ssl3_send_certificate_status(SSL *ssl);
  174. static int ssl3_send_server_key_exchange(SSL *ssl);
  175. static int ssl3_send_certificate_request(SSL *ssl);
  176. static int ssl3_send_server_hello_done(SSL *ssl);
  177. static int ssl3_get_client_certificate(SSL *ssl);
  178. static int ssl3_get_client_key_exchange(SSL *ssl);
  179. static int ssl3_get_cert_verify(SSL *ssl);
  180. static int ssl3_get_next_proto(SSL *ssl);
  181. static int ssl3_get_channel_id(SSL *ssl);
  182. static int ssl3_send_new_session_ticket(SSL *ssl);
  183. int ssl3_accept(SSL *ssl) {
  184. uint32_t alg_a;
  185. int ret = -1;
  186. int state, skip = 0;
  187. assert(ssl->handshake_func == ssl3_accept);
  188. assert(ssl->server);
  189. for (;;) {
  190. state = ssl->state;
  191. switch (ssl->state) {
  192. case SSL_ST_ACCEPT:
  193. ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
  194. if (!ssl->method->begin_handshake(ssl)) {
  195. ret = -1;
  196. goto end;
  197. }
  198. /* Enable a write buffer. This groups handshake messages within a flight
  199. * into a single write. */
  200. if (!ssl_init_wbio_buffer(ssl)) {
  201. ret = -1;
  202. goto end;
  203. }
  204. if (!ssl3_init_handshake_buffer(ssl)) {
  205. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  206. ret = -1;
  207. goto end;
  208. }
  209. ssl->state = SSL3_ST_SR_CLNT_HELLO_A;
  210. break;
  211. case SSL3_ST_SR_CLNT_HELLO_A:
  212. case SSL3_ST_SR_CLNT_HELLO_B:
  213. case SSL3_ST_SR_CLNT_HELLO_C:
  214. ret = ssl3_get_client_hello(ssl);
  215. if (ret <= 0) {
  216. goto end;
  217. }
  218. ssl->method->received_flight(ssl);
  219. ssl->state = SSL3_ST_SW_SRVR_HELLO_A;
  220. break;
  221. case SSL3_ST_SW_SRVR_HELLO_A:
  222. case SSL3_ST_SW_SRVR_HELLO_B:
  223. ret = ssl3_send_server_hello(ssl);
  224. if (ret <= 0) {
  225. goto end;
  226. }
  227. if (ssl->hit) {
  228. ssl->state = SSL3_ST_SW_SESSION_TICKET_A;
  229. } else {
  230. ssl->state = SSL3_ST_SW_CERT_A;
  231. }
  232. break;
  233. case SSL3_ST_SW_CERT_A:
  234. case SSL3_ST_SW_CERT_B:
  235. if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
  236. ret = ssl3_send_server_certificate(ssl);
  237. if (ret <= 0) {
  238. goto end;
  239. }
  240. } else {
  241. skip = 1;
  242. }
  243. ssl->state = SSL3_ST_SW_CERT_STATUS_A;
  244. break;
  245. case SSL3_ST_SW_CERT_STATUS_A:
  246. case SSL3_ST_SW_CERT_STATUS_B:
  247. if (ssl->s3->tmp.certificate_status_expected) {
  248. ret = ssl3_send_certificate_status(ssl);
  249. if (ret <= 0) {
  250. goto end;
  251. }
  252. } else {
  253. skip = 1;
  254. }
  255. ssl->state = SSL3_ST_SW_KEY_EXCH_A;
  256. break;
  257. case SSL3_ST_SW_KEY_EXCH_A:
  258. case SSL3_ST_SW_KEY_EXCH_B:
  259. case SSL3_ST_SW_KEY_EXCH_C:
  260. alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
  261. /* PSK ciphers send ServerKeyExchange if there is an identity hint. */
  262. if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher) ||
  263. ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
  264. ret = ssl3_send_server_key_exchange(ssl);
  265. if (ret <= 0) {
  266. goto end;
  267. }
  268. } else {
  269. skip = 1;
  270. }
  271. ssl->state = SSL3_ST_SW_CERT_REQ_A;
  272. break;
  273. case SSL3_ST_SW_CERT_REQ_A:
  274. case SSL3_ST_SW_CERT_REQ_B:
  275. if (ssl->s3->tmp.cert_request) {
  276. ret = ssl3_send_certificate_request(ssl);
  277. if (ret <= 0) {
  278. goto end;
  279. }
  280. } else {
  281. skip = 1;
  282. }
  283. ssl->state = SSL3_ST_SW_SRVR_DONE_A;
  284. break;
  285. case SSL3_ST_SW_SRVR_DONE_A:
  286. case SSL3_ST_SW_SRVR_DONE_B:
  287. ret = ssl3_send_server_hello_done(ssl);
  288. if (ret <= 0) {
  289. goto end;
  290. }
  291. ssl->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
  292. ssl->state = SSL3_ST_SW_FLUSH;
  293. break;
  294. case SSL3_ST_SR_CERT_A:
  295. if (ssl->s3->tmp.cert_request) {
  296. ret = ssl3_get_client_certificate(ssl);
  297. if (ret <= 0) {
  298. goto end;
  299. }
  300. }
  301. ssl->state = SSL3_ST_SR_KEY_EXCH_A;
  302. break;
  303. case SSL3_ST_SR_KEY_EXCH_A:
  304. case SSL3_ST_SR_KEY_EXCH_B:
  305. ret = ssl3_get_client_key_exchange(ssl);
  306. if (ret <= 0) {
  307. goto end;
  308. }
  309. ssl->state = SSL3_ST_SR_CERT_VRFY_A;
  310. break;
  311. case SSL3_ST_SR_CERT_VRFY_A:
  312. ret = ssl3_get_cert_verify(ssl);
  313. if (ret <= 0) {
  314. goto end;
  315. }
  316. ssl->state = SSL3_ST_SR_CHANGE;
  317. break;
  318. case SSL3_ST_SR_CHANGE:
  319. ret = ssl->method->read_change_cipher_spec(ssl);
  320. if (ret <= 0) {
  321. goto end;
  322. }
  323. if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_READ)) {
  324. ret = -1;
  325. goto end;
  326. }
  327. ssl->state = SSL3_ST_SR_NEXT_PROTO_A;
  328. break;
  329. case SSL3_ST_SR_NEXT_PROTO_A:
  330. if (ssl->s3->next_proto_neg_seen) {
  331. ret = ssl3_get_next_proto(ssl);
  332. if (ret <= 0) {
  333. goto end;
  334. }
  335. } else {
  336. skip = 1;
  337. }
  338. ssl->state = SSL3_ST_SR_CHANNEL_ID_A;
  339. break;
  340. case SSL3_ST_SR_CHANNEL_ID_A:
  341. if (ssl->s3->tlsext_channel_id_valid) {
  342. ret = ssl3_get_channel_id(ssl);
  343. if (ret <= 0) {
  344. goto end;
  345. }
  346. } else {
  347. skip = 1;
  348. }
  349. ssl->state = SSL3_ST_SR_FINISHED_A;
  350. break;
  351. case SSL3_ST_SR_FINISHED_A:
  352. ret = ssl3_get_finished(ssl);
  353. if (ret <= 0) {
  354. goto end;
  355. }
  356. ssl->method->received_flight(ssl);
  357. if (ssl->hit) {
  358. ssl->state = SSL_ST_OK;
  359. } else {
  360. ssl->state = SSL3_ST_SW_SESSION_TICKET_A;
  361. }
  362. /* If this is a full handshake with ChannelID then record the hashshake
  363. * hashes in |ssl->session| in case we need them to verify a ChannelID
  364. * signature on a resumption of this session in the future. */
  365. if (!ssl->hit && ssl->s3->tlsext_channel_id_valid) {
  366. ret = tls1_record_handshake_hashes_for_channel_id(ssl);
  367. if (ret <= 0) {
  368. goto end;
  369. }
  370. }
  371. break;
  372. case SSL3_ST_SW_SESSION_TICKET_A:
  373. case SSL3_ST_SW_SESSION_TICKET_B:
  374. if (ssl->tlsext_ticket_expected) {
  375. ret = ssl3_send_new_session_ticket(ssl);
  376. if (ret <= 0) {
  377. goto end;
  378. }
  379. } else {
  380. skip = 1;
  381. }
  382. ssl->state = SSL3_ST_SW_CHANGE;
  383. break;
  384. case SSL3_ST_SW_CHANGE:
  385. ret = ssl->method->send_change_cipher_spec(ssl);
  386. if (ret <= 0) {
  387. goto end;
  388. }
  389. ssl->state = SSL3_ST_SW_FINISHED_A;
  390. if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
  391. ret = -1;
  392. goto end;
  393. }
  394. break;
  395. case SSL3_ST_SW_FINISHED_A:
  396. case SSL3_ST_SW_FINISHED_B:
  397. ret = ssl3_send_finished(ssl, SSL3_ST_SW_FINISHED_A,
  398. SSL3_ST_SW_FINISHED_B);
  399. if (ret <= 0) {
  400. goto end;
  401. }
  402. ssl->state = SSL3_ST_SW_FLUSH;
  403. if (ssl->hit) {
  404. ssl->s3->tmp.next_state = SSL3_ST_SR_CHANGE;
  405. } else {
  406. ssl->s3->tmp.next_state = SSL_ST_OK;
  407. }
  408. break;
  409. case SSL3_ST_SW_FLUSH:
  410. if (BIO_flush(ssl->wbio) <= 0) {
  411. ssl->rwstate = SSL_WRITING;
  412. ret = -1;
  413. goto end;
  414. }
  415. ssl->state = ssl->s3->tmp.next_state;
  416. if (ssl->state != SSL_ST_OK) {
  417. ssl->method->expect_flight(ssl);
  418. }
  419. break;
  420. case SSL_ST_OK:
  421. /* clean a few things up */
  422. ssl3_cleanup_key_block(ssl);
  423. ssl->method->finish_handshake(ssl);
  424. /* remove buffering on output */
  425. ssl_free_wbio_buffer(ssl);
  426. /* If we aren't retaining peer certificates then we can discard it
  427. * now. */
  428. if (ssl->ctx->retain_only_sha256_of_client_certs) {
  429. X509_free(ssl->session->peer);
  430. ssl->session->peer = NULL;
  431. sk_X509_pop_free(ssl->session->cert_chain, X509_free);
  432. ssl->session->cert_chain = NULL;
  433. }
  434. ssl->s3->initial_handshake_complete = 1;
  435. ssl_update_cache(ssl, SSL_SESS_CACHE_SERVER);
  436. ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
  437. ret = 1;
  438. goto end;
  439. default:
  440. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
  441. ret = -1;
  442. goto end;
  443. }
  444. if (!ssl->s3->tmp.reuse_message && !skip && ssl->state != state) {
  445. int new_state = ssl->state;
  446. ssl->state = state;
  447. ssl_do_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 1);
  448. ssl->state = new_state;
  449. }
  450. skip = 0;
  451. }
  452. end:
  453. ssl_do_info_callback(ssl, SSL_CB_ACCEPT_EXIT, ret);
  454. return ret;
  455. }
  456. static int ssl3_get_client_hello(SSL *ssl) {
  457. int ok, al = SSL_AD_INTERNAL_ERROR, ret = -1;
  458. long n;
  459. const SSL_CIPHER *c;
  460. STACK_OF(SSL_CIPHER) *ciphers = NULL;
  461. struct ssl_early_callback_ctx early_ctx;
  462. CBS client_hello;
  463. uint16_t client_wire_version;
  464. CBS client_random, session_id, cipher_suites, compression_methods;
  465. SSL_SESSION *session = NULL;
  466. /* We do this so that we will respond with our native type. If we are TLSv1
  467. * and we get SSLv3, we will respond with TLSv1, This down switching should
  468. * be handled by a different method. If we are SSLv3, we will respond with
  469. * SSLv3, even if prompted with TLSv1. */
  470. switch (ssl->state) {
  471. case SSL3_ST_SR_CLNT_HELLO_A:
  472. n = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_HELLO,
  473. ssl_hash_message, &ok);
  474. if (!ok) {
  475. return n;
  476. }
  477. ssl->state = SSL3_ST_SR_CLNT_HELLO_B;
  478. /* fallthrough */
  479. case SSL3_ST_SR_CLNT_HELLO_B:
  480. case SSL3_ST_SR_CLNT_HELLO_C:
  481. /* We have previously parsed the ClientHello message, and can't call
  482. * ssl_get_message again without hashing the message into the Finished
  483. * digest again. */
  484. n = ssl->init_num;
  485. memset(&early_ctx, 0, sizeof(early_ctx));
  486. early_ctx.ssl = ssl;
  487. early_ctx.client_hello = ssl->init_msg;
  488. early_ctx.client_hello_len = n;
  489. if (!ssl_early_callback_init(&early_ctx)) {
  490. al = SSL_AD_DECODE_ERROR;
  491. OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED);
  492. goto f_err;
  493. }
  494. if (ssl->state == SSL3_ST_SR_CLNT_HELLO_B &&
  495. ssl->ctx->select_certificate_cb != NULL) {
  496. ssl->state = SSL3_ST_SR_CLNT_HELLO_C;
  497. switch (ssl->ctx->select_certificate_cb(&early_ctx)) {
  498. case 0:
  499. ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING;
  500. goto err;
  501. case -1:
  502. /* Connection rejected. */
  503. al = SSL_AD_ACCESS_DENIED;
  504. OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
  505. goto f_err;
  506. default:
  507. /* fallthrough */;
  508. }
  509. }
  510. ssl->state = SSL3_ST_SR_CLNT_HELLO_C;
  511. break;
  512. default:
  513. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
  514. return -1;
  515. }
  516. CBS_init(&client_hello, ssl->init_msg, n);
  517. if (!CBS_get_u16(&client_hello, &client_wire_version) ||
  518. !CBS_get_bytes(&client_hello, &client_random, SSL3_RANDOM_SIZE) ||
  519. !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
  520. CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
  521. al = SSL_AD_DECODE_ERROR;
  522. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  523. goto f_err;
  524. }
  525. uint16_t client_version = ssl->method->version_from_wire(client_wire_version);
  526. /* use version from inside client hello, not from record header (may differ:
  527. * see RFC 2246, Appendix E, second paragraph) */
  528. ssl->client_version = client_wire_version;
  529. /* Load the client random. */
  530. memcpy(ssl->s3->client_random, CBS_data(&client_random), SSL3_RANDOM_SIZE);
  531. if (SSL_IS_DTLS(ssl)) {
  532. CBS cookie;
  533. if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
  534. CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
  535. al = SSL_AD_DECODE_ERROR;
  536. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  537. goto f_err;
  538. }
  539. }
  540. uint16_t min_version, max_version;
  541. if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
  542. al = SSL_AD_PROTOCOL_VERSION;
  543. goto f_err;
  544. }
  545. /* Note: This codepath may run twice if |ssl_get_prev_session| completes
  546. * asynchronously.
  547. *
  548. * TODO(davidben): Clean up the order of events around ClientHello
  549. * processing. */
  550. if (!ssl->s3->have_version) {
  551. /* Select the version to use. */
  552. uint16_t version = client_version;
  553. if (version > max_version) {
  554. version = max_version;
  555. }
  556. if (version < min_version) {
  557. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
  558. al = SSL_AD_PROTOCOL_VERSION;
  559. goto f_err;
  560. }
  561. ssl->version = ssl->method->version_to_wire(version);
  562. ssl->s3->enc_method = ssl3_get_enc_method(version);
  563. assert(ssl->s3->enc_method != NULL);
  564. /* At this point, the connection's version is known and |ssl->version| is
  565. * fixed. Begin enforcing the record-layer version. */
  566. ssl->s3->have_version = 1;
  567. } else if (client_version < ssl3_protocol_version(ssl)) {
  568. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_NUMBER);
  569. al = SSL_AD_PROTOCOL_VERSION;
  570. goto f_err;
  571. }
  572. ssl->hit = 0;
  573. int send_new_ticket = 0;
  574. switch (ssl_get_prev_session(ssl, &session, &send_new_ticket, &early_ctx)) {
  575. case ssl_session_success:
  576. break;
  577. case ssl_session_error:
  578. goto err;
  579. case ssl_session_retry:
  580. ssl->rwstate = SSL_PENDING_SESSION;
  581. goto err;
  582. }
  583. ssl->tlsext_ticket_expected = send_new_ticket;
  584. /* The EMS state is needed when making the resumption decision, but
  585. * extensions are not normally parsed until later. This detects the EMS
  586. * extension for the resumption decision and it's checked against the result
  587. * of the normal parse later in this function. */
  588. const uint8_t *ems_data;
  589. size_t ems_len;
  590. int have_extended_master_secret =
  591. ssl->version != SSL3_VERSION &&
  592. SSL_early_callback_ctx_extension_get(&early_ctx,
  593. TLSEXT_TYPE_extended_master_secret,
  594. &ems_data, &ems_len) &&
  595. ems_len == 0;
  596. if (session != NULL) {
  597. if (session->extended_master_secret &&
  598. !have_extended_master_secret) {
  599. /* A ClientHello without EMS that attempts to resume a session with EMS
  600. * is fatal to the connection. */
  601. al = SSL_AD_HANDSHAKE_FAILURE;
  602. OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION);
  603. goto f_err;
  604. }
  605. ssl->hit =
  606. /* Only resume if the session's version matches the negotiated version:
  607. * most clients do not accept a mismatch. */
  608. ssl->version == session->ssl_version &&
  609. /* If the client offers the EMS extension, but the previous session
  610. * didn't use it, then negotiate a new session. */
  611. have_extended_master_secret == session->extended_master_secret;
  612. }
  613. if (ssl->hit) {
  614. /* Use the new session. */
  615. SSL_SESSION_free(ssl->session);
  616. ssl->session = session;
  617. session = NULL;
  618. ssl->verify_result = ssl->session->verify_result;
  619. } else {
  620. if (!ssl_get_new_session(ssl, 1 /* server */)) {
  621. goto err;
  622. }
  623. /* Clear the session ID if we want the session to be single-use. */
  624. if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) {
  625. ssl->session->session_id_length = 0;
  626. }
  627. }
  628. if (ssl->ctx->dos_protection_cb != NULL &&
  629. ssl->ctx->dos_protection_cb(&early_ctx) == 0) {
  630. /* Connection rejected for DOS reasons. */
  631. al = SSL_AD_ACCESS_DENIED;
  632. OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED);
  633. goto f_err;
  634. }
  635. if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
  636. CBS_len(&cipher_suites) == 0 ||
  637. CBS_len(&cipher_suites) % 2 != 0 ||
  638. !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
  639. CBS_len(&compression_methods) == 0) {
  640. al = SSL_AD_DECODE_ERROR;
  641. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  642. goto f_err;
  643. }
  644. ciphers = ssl_bytes_to_cipher_list(ssl, &cipher_suites, max_version);
  645. if (ciphers == NULL) {
  646. goto err;
  647. }
  648. /* If it is a hit, check that the cipher is in the list. */
  649. if (ssl->hit) {
  650. size_t j;
  651. int found_cipher = 0;
  652. uint32_t id = ssl->session->cipher->id;
  653. for (j = 0; j < sk_SSL_CIPHER_num(ciphers); j++) {
  654. c = sk_SSL_CIPHER_value(ciphers, j);
  655. if (c->id == id) {
  656. found_cipher = 1;
  657. break;
  658. }
  659. }
  660. if (!found_cipher) {
  661. /* we need to have the cipher in the cipher list if we are asked to reuse
  662. * it */
  663. al = SSL_AD_ILLEGAL_PARAMETER;
  664. OPENSSL_PUT_ERROR(SSL, SSL_R_REQUIRED_CIPHER_MISSING);
  665. goto f_err;
  666. }
  667. }
  668. /* Only null compression is supported. */
  669. if (memchr(CBS_data(&compression_methods), 0,
  670. CBS_len(&compression_methods)) == NULL) {
  671. al = SSL_AD_ILLEGAL_PARAMETER;
  672. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMPRESSION_SPECIFIED);
  673. goto f_err;
  674. }
  675. /* TLS extensions. */
  676. if (ssl->version >= SSL3_VERSION &&
  677. !ssl_parse_clienthello_tlsext(ssl, &client_hello)) {
  678. OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
  679. goto err;
  680. }
  681. /* There should be nothing left over in the record. */
  682. if (CBS_len(&client_hello) != 0) {
  683. /* wrong packet length */
  684. al = SSL_AD_DECODE_ERROR;
  685. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
  686. goto f_err;
  687. }
  688. if (have_extended_master_secret != ssl->s3->tmp.extended_master_secret) {
  689. al = SSL_AD_INTERNAL_ERROR;
  690. OPENSSL_PUT_ERROR(SSL, SSL_R_EMS_STATE_INCONSISTENT);
  691. goto f_err;
  692. }
  693. /* Given ciphers and SSL_get_ciphers, we must pick a cipher */
  694. if (!ssl->hit) {
  695. if (ciphers == NULL) {
  696. al = SSL_AD_ILLEGAL_PARAMETER;
  697. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_PASSED);
  698. goto f_err;
  699. }
  700. /* Let cert callback update server certificates if required */
  701. if (ssl->cert->cert_cb) {
  702. int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg);
  703. if (rv == 0) {
  704. al = SSL_AD_INTERNAL_ERROR;
  705. OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR);
  706. goto f_err;
  707. }
  708. if (rv < 0) {
  709. ssl->rwstate = SSL_X509_LOOKUP;
  710. goto err;
  711. }
  712. }
  713. c = ssl3_choose_cipher(ssl, ciphers, ssl_get_cipher_preferences(ssl));
  714. if (c == NULL) {
  715. al = SSL_AD_HANDSHAKE_FAILURE;
  716. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER);
  717. goto f_err;
  718. }
  719. ssl->session->cipher = c;
  720. ssl->s3->tmp.new_cipher = c;
  721. /* Determine whether to request a client certificate. */
  722. ssl->s3->tmp.cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER);
  723. /* Only request a certificate if Channel ID isn't negotiated. */
  724. if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) &&
  725. ssl->s3->tlsext_channel_id_valid) {
  726. ssl->s3->tmp.cert_request = 0;
  727. }
  728. /* CertificateRequest may only be sent in certificate-based ciphers. */
  729. if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
  730. ssl->s3->tmp.cert_request = 0;
  731. }
  732. } else {
  733. /* Session-id reuse */
  734. ssl->s3->tmp.new_cipher = ssl->session->cipher;
  735. ssl->s3->tmp.cert_request = 0;
  736. }
  737. /* Now that the cipher is known, initialize the handshake hash. */
  738. if (!ssl3_init_handshake_hash(ssl)) {
  739. goto f_err;
  740. }
  741. /* Release the handshake buffer if client authentication isn't required. */
  742. if (!ssl->s3->tmp.cert_request) {
  743. ssl3_free_handshake_buffer(ssl);
  744. }
  745. /* we now have the following setup;
  746. * client_random
  747. * cipher_list - our prefered list of ciphers
  748. * ciphers - the clients prefered list of ciphers
  749. * compression - basically ignored right now
  750. * ssl version is set - sslv3
  751. * ssl->session - The ssl session has been setup.
  752. * ssl->hit - session reuse flag
  753. * ssl->tmp.new_cipher - the new cipher to use. */
  754. ret = 1;
  755. if (0) {
  756. f_err:
  757. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  758. }
  759. err:
  760. sk_SSL_CIPHER_free(ciphers);
  761. SSL_SESSION_free(session);
  762. return ret;
  763. }
  764. static int ssl3_send_server_hello(SSL *ssl) {
  765. if (ssl->state == SSL3_ST_SW_SRVR_HELLO_B) {
  766. return ssl->method->write_message(ssl);
  767. }
  768. assert(ssl->state == SSL3_ST_SW_SRVR_HELLO_A);
  769. /* We only accept ChannelIDs on connections with ECDHE in order to avoid a
  770. * known attack while we fix ChannelID itself. */
  771. if (ssl->s3->tlsext_channel_id_valid &&
  772. (ssl->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) {
  773. ssl->s3->tlsext_channel_id_valid = 0;
  774. }
  775. /* If this is a resumption and the original handshake didn't support
  776. * ChannelID then we didn't record the original handshake hashes in the
  777. * session and so cannot resume with ChannelIDs. */
  778. if (ssl->hit && ssl->session->original_handshake_hash_len == 0) {
  779. ssl->s3->tlsext_channel_id_valid = 0;
  780. }
  781. if (!ssl_fill_hello_random(ssl->s3->server_random, SSL3_RANDOM_SIZE,
  782. 1 /* server */)) {
  783. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  784. return -1;
  785. }
  786. CBB cbb, body, session_id;
  787. if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO) ||
  788. !CBB_add_u16(&body, ssl->version) ||
  789. !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
  790. !CBB_add_u8_length_prefixed(&body, &session_id) ||
  791. !CBB_add_bytes(&session_id, ssl->session->session_id,
  792. ssl->session->session_id_length) ||
  793. !CBB_add_u16(&body, ssl_cipher_get_value(ssl->s3->tmp.new_cipher)) ||
  794. !CBB_add_u8(&body, 0 /* no compression */) ||
  795. !ssl_add_serverhello_tlsext(ssl, &body) ||
  796. !ssl->method->finish_message(ssl, &cbb)) {
  797. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  798. CBB_cleanup(&cbb);
  799. return -1;
  800. }
  801. ssl->state = SSL3_ST_SW_SRVR_HELLO_B;
  802. return ssl->method->write_message(ssl);
  803. }
  804. static int ssl3_send_server_certificate(SSL *ssl) {
  805. if (ssl->state == SSL3_ST_SW_CERT_B) {
  806. return ssl->method->write_message(ssl);
  807. }
  808. if (!ssl3_output_cert_chain(ssl)) {
  809. return 0;
  810. }
  811. ssl->state = SSL3_ST_SW_CERT_B;
  812. return ssl->method->write_message(ssl);
  813. }
  814. static int ssl3_send_certificate_status(SSL *ssl) {
  815. if (ssl->state == SSL3_ST_SW_CERT_STATUS_B) {
  816. return ssl->method->write_message(ssl);
  817. }
  818. CBB cbb, body, ocsp_response;
  819. if (!ssl->method->init_message(ssl, &cbb, &body,
  820. SSL3_MT_CERTIFICATE_STATUS) ||
  821. !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) ||
  822. !CBB_add_u24_length_prefixed(&body, &ocsp_response) ||
  823. !CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
  824. ssl->ctx->ocsp_response_length) ||
  825. !ssl->method->finish_message(ssl, &cbb)) {
  826. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  827. CBB_cleanup(&cbb);
  828. return -1;
  829. }
  830. ssl->state = SSL3_ST_SW_CERT_STATUS_B;
  831. return ssl->method->write_message(ssl);
  832. }
  833. static int ssl3_send_server_key_exchange(SSL *ssl) {
  834. if (ssl->state == SSL3_ST_SW_KEY_EXCH_C) {
  835. return ssl->method->write_message(ssl);
  836. }
  837. CBB cbb, child;
  838. CBB_zero(&cbb);
  839. /* Put together the parameters. */
  840. if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) {
  841. uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
  842. uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
  843. /* Pre-allocate enough room to comfortably fit an ECDHE public key. */
  844. if (!CBB_init(&cbb, 128)) {
  845. goto err;
  846. }
  847. /* PSK ciphers begin with an identity hint. */
  848. if (alg_a & SSL_aPSK) {
  849. size_t len =
  850. (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint);
  851. if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
  852. !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint,
  853. len)) {
  854. goto err;
  855. }
  856. }
  857. if (alg_k & SSL_kDHE) {
  858. /* Determine the group to use. */
  859. DH *params = ssl->cert->dh_tmp;
  860. if (params == NULL && ssl->cert->dh_tmp_cb != NULL) {
  861. params = ssl->cert->dh_tmp_cb(ssl, 0, 1024);
  862. }
  863. if (params == NULL) {
  864. OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_DH_KEY);
  865. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  866. goto err;
  867. }
  868. ssl->session->key_exchange_info = DH_num_bits(params);
  869. /* Set up DH, generate a key, and emit the public half. */
  870. DH *dh = DHparams_dup(params);
  871. if (dh == NULL) {
  872. goto err;
  873. }
  874. SSL_ECDH_CTX_init_for_dhe(&ssl->s3->tmp.ecdh_ctx, dh);
  875. if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
  876. !BN_bn2cbb_padded(&child, BN_num_bytes(params->p), params->p) ||
  877. !CBB_add_u16_length_prefixed(&cbb, &child) ||
  878. !BN_bn2cbb_padded(&child, BN_num_bytes(params->g), params->g) ||
  879. !CBB_add_u16_length_prefixed(&cbb, &child) ||
  880. !SSL_ECDH_CTX_offer(&ssl->s3->tmp.ecdh_ctx, &child)) {
  881. goto err;
  882. }
  883. } else if (alg_k & SSL_kECDHE) {
  884. /* Determine the group to use. */
  885. uint16_t group_id;
  886. if (!tls1_get_shared_group(ssl, &group_id)) {
  887. OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY);
  888. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  889. goto err;
  890. }
  891. ssl->session->key_exchange_info = group_id;
  892. /* Set up ECDH, generate a key, and emit the public half. */
  893. if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, group_id) ||
  894. !CBB_add_u8(&cbb, NAMED_CURVE_TYPE) ||
  895. !CBB_add_u16(&cbb, group_id) ||
  896. !CBB_add_u8_length_prefixed(&cbb, &child) ||
  897. !SSL_ECDH_CTX_offer(&ssl->s3->tmp.ecdh_ctx, &child)) {
  898. goto err;
  899. }
  900. } else if (alg_k & SSL_kCECPQ1) {
  901. SSL_ECDH_CTX_init_for_cecpq1(&ssl->s3->tmp.ecdh_ctx);
  902. if (!CBB_add_u16_length_prefixed(&cbb, &child) ||
  903. !SSL_ECDH_CTX_offer(&ssl->s3->tmp.ecdh_ctx, &child)) {
  904. goto err;
  905. }
  906. } else {
  907. assert(alg_k & SSL_kPSK);
  908. }
  909. size_t len;
  910. if (!CBB_finish(&cbb, &ssl->s3->tmp.server_params, &len) ||
  911. len > 0xffffffffu) {
  912. OPENSSL_free(ssl->s3->tmp.server_params);
  913. ssl->s3->tmp.server_params = NULL;
  914. goto err;
  915. }
  916. ssl->s3->tmp.server_params_len = (uint32_t)len;
  917. }
  918. /* Assemble the message. */
  919. CBB body;
  920. if (!ssl->method->init_message(ssl, &cbb, &body,
  921. SSL3_MT_SERVER_KEY_EXCHANGE) ||
  922. !CBB_add_bytes(&body, ssl->s3->tmp.server_params,
  923. ssl->s3->tmp.server_params_len)) {
  924. goto err;
  925. }
  926. /* Add a signature. */
  927. if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
  928. if (!ssl_has_private_key(ssl)) {
  929. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  930. goto err;
  931. }
  932. /* Determine the signature algorithm. */
  933. uint16_t signature_algorithm = tls1_choose_signature_algorithm(ssl);
  934. if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
  935. if (!CBB_add_u16(&body, signature_algorithm)) {
  936. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  937. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  938. goto err;
  939. }
  940. }
  941. /* Add space for the signature. */
  942. const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
  943. uint8_t *ptr;
  944. if (!CBB_add_u16_length_prefixed(&body, &child) ||
  945. !CBB_reserve(&child, &ptr, max_sig_len)) {
  946. goto err;
  947. }
  948. size_t sig_len;
  949. enum ssl_private_key_result_t sign_result;
  950. if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) {
  951. CBB transcript;
  952. uint8_t *transcript_data;
  953. size_t transcript_len;
  954. if (!CBB_init(&transcript,
  955. 2*SSL3_RANDOM_SIZE + ssl->s3->tmp.server_params_len) ||
  956. !CBB_add_bytes(&transcript, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
  957. !CBB_add_bytes(&transcript, ssl->s3->server_random, SSL3_RANDOM_SIZE) ||
  958. !CBB_add_bytes(&transcript, ssl->s3->tmp.server_params,
  959. ssl->s3->tmp.server_params_len) ||
  960. !CBB_finish(&transcript, &transcript_data, &transcript_len)) {
  961. CBB_cleanup(&transcript);
  962. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  963. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  964. goto err;
  965. }
  966. sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len,
  967. signature_algorithm, transcript_data,
  968. transcript_len);
  969. OPENSSL_free(transcript_data);
  970. } else {
  971. assert(ssl->state == SSL3_ST_SW_KEY_EXCH_B);
  972. sign_result =
  973. ssl_private_key_sign_complete(ssl, ptr, &sig_len, max_sig_len);
  974. }
  975. switch (sign_result) {
  976. case ssl_private_key_success:
  977. if (!CBB_did_write(&child, sig_len)) {
  978. goto err;
  979. }
  980. break;
  981. case ssl_private_key_failure:
  982. goto err;
  983. case ssl_private_key_retry:
  984. ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
  985. ssl->state = SSL3_ST_SW_KEY_EXCH_B;
  986. goto err;
  987. }
  988. }
  989. if (!ssl->method->finish_message(ssl, &cbb)) {
  990. goto err;
  991. }
  992. OPENSSL_free(ssl->s3->tmp.server_params);
  993. ssl->s3->tmp.server_params = NULL;
  994. ssl->s3->tmp.server_params_len = 0;
  995. ssl->state = SSL3_ST_SW_KEY_EXCH_C;
  996. return ssl->method->write_message(ssl);
  997. err:
  998. CBB_cleanup(&cbb);
  999. return -1;
  1000. }
  1001. static int add_cert_types(SSL *ssl, CBB *cbb) {
  1002. /* Get configured signature algorithms. */
  1003. int have_rsa_sign = 0;
  1004. int have_ecdsa_sign = 0;
  1005. const uint16_t *sig_algs;
  1006. size_t sig_algs_len = tls12_get_psigalgs(ssl, &sig_algs);
  1007. size_t i;
  1008. for (i = 0; i < sig_algs_len; i++) {
  1009. switch (sig_algs[i]) {
  1010. case SSL_SIGN_RSA_PKCS1_SHA512:
  1011. case SSL_SIGN_RSA_PKCS1_SHA384:
  1012. case SSL_SIGN_RSA_PKCS1_SHA256:
  1013. case SSL_SIGN_RSA_PKCS1_SHA1:
  1014. have_rsa_sign = 1;
  1015. break;
  1016. case SSL_SIGN_ECDSA_SECP521R1_SHA512:
  1017. case SSL_SIGN_ECDSA_SECP384R1_SHA384:
  1018. case SSL_SIGN_ECDSA_SECP256R1_SHA256:
  1019. case SSL_SIGN_ECDSA_SHA1:
  1020. have_ecdsa_sign = 1;
  1021. break;
  1022. }
  1023. }
  1024. if (have_rsa_sign && !CBB_add_u8(cbb, SSL3_CT_RSA_SIGN)) {
  1025. return 0;
  1026. }
  1027. /* ECDSA certs can be used with RSA cipher suites as well so we don't need to
  1028. * check for SSL_kECDH or SSL_kECDHE. */
  1029. if (ssl->version >= TLS1_VERSION && have_ecdsa_sign &&
  1030. !CBB_add_u8(cbb, TLS_CT_ECDSA_SIGN)) {
  1031. return 0;
  1032. }
  1033. return 1;
  1034. }
  1035. static int ssl3_send_certificate_request(SSL *ssl) {
  1036. if (ssl->state == SSL3_ST_SW_CERT_REQ_B) {
  1037. return ssl->method->write_message(ssl);
  1038. }
  1039. CBB cbb, body, cert_types, sigalgs_cbb, names_cbb, name_cbb;
  1040. if (!ssl->method->init_message(ssl, &cbb, &body,
  1041. SSL3_MT_CERTIFICATE_REQUEST) ||
  1042. !CBB_add_u8_length_prefixed(&body, &cert_types) ||
  1043. !add_cert_types(ssl, &cert_types)) {
  1044. goto err;
  1045. }
  1046. if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
  1047. const uint16_t *sigalgs;
  1048. size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs);
  1049. if (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb)) {
  1050. goto err;
  1051. }
  1052. size_t i;
  1053. for (i = 0; i < sigalgs_len; i++) {
  1054. if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
  1055. goto err;
  1056. }
  1057. }
  1058. }
  1059. STACK_OF(X509_NAME) *sk = SSL_get_client_CA_list(ssl);
  1060. if (sk != NULL) {
  1061. if (!CBB_add_u16_length_prefixed(&body, &names_cbb)) {
  1062. goto err;
  1063. }
  1064. size_t i;
  1065. for (i = 0; i < sk_X509_NAME_num(sk); i++) {
  1066. X509_NAME *name = sk_X509_NAME_value(sk, i);
  1067. int len = i2d_X509_NAME(name, NULL);
  1068. if (len < 0) {
  1069. goto err;
  1070. }
  1071. uint8_t *ptr;
  1072. if (!CBB_add_u16_length_prefixed(&names_cbb, &name_cbb) ||
  1073. !CBB_add_space(&name_cbb, &ptr, (size_t)len) ||
  1074. (len > 0 && i2d_X509_NAME(name, &ptr) < 0)) {
  1075. goto err;
  1076. }
  1077. }
  1078. }
  1079. if (!ssl->method->finish_message(ssl, &cbb)) {
  1080. goto err;
  1081. }
  1082. ssl->state = SSL3_ST_SW_CERT_REQ_B;
  1083. return ssl->method->write_message(ssl);
  1084. err:
  1085. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1086. CBB_cleanup(&cbb);
  1087. return -1;
  1088. }
  1089. static int ssl3_send_server_hello_done(SSL *ssl) {
  1090. if (ssl->state == SSL3_ST_SW_SRVR_DONE_B) {
  1091. return ssl->method->write_message(ssl);
  1092. }
  1093. CBB cbb, body;
  1094. if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO_DONE) ||
  1095. !ssl->method->finish_message(ssl, &cbb)) {
  1096. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1097. CBB_cleanup(&cbb);
  1098. return -1;
  1099. }
  1100. ssl->state = SSL3_ST_SW_SRVR_DONE_B;
  1101. return ssl->method->write_message(ssl);
  1102. }
  1103. static int ssl3_get_client_certificate(SSL *ssl) {
  1104. int ok, al, ret = -1;
  1105. X509 *x = NULL;
  1106. unsigned long n;
  1107. STACK_OF(X509) *sk = NULL;
  1108. SHA256_CTX sha256;
  1109. CBS certificate_msg, certificate_list;
  1110. int is_first_certificate = 1;
  1111. assert(ssl->s3->tmp.cert_request);
  1112. n = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message, &ok);
  1113. if (!ok) {
  1114. return n;
  1115. }
  1116. if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
  1117. if (ssl->version == SSL3_VERSION &&
  1118. ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
  1119. /* In SSL 3.0, the Certificate message is omitted to signal no certificate. */
  1120. if ((ssl->verify_mode & SSL_VERIFY_PEER) &&
  1121. (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
  1122. OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  1123. al = SSL_AD_HANDSHAKE_FAILURE;
  1124. goto f_err;
  1125. }
  1126. ssl->s3->tmp.reuse_message = 1;
  1127. return 1;
  1128. }
  1129. al = SSL_AD_UNEXPECTED_MESSAGE;
  1130. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
  1131. goto f_err;
  1132. }
  1133. CBS_init(&certificate_msg, ssl->init_msg, n);
  1134. sk = sk_X509_new_null();
  1135. if (sk == NULL) {
  1136. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1137. goto err;
  1138. }
  1139. if (!CBS_get_u24_length_prefixed(&certificate_msg, &certificate_list) ||
  1140. CBS_len(&certificate_msg) != 0) {
  1141. al = SSL_AD_DECODE_ERROR;
  1142. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1143. goto f_err;
  1144. }
  1145. while (CBS_len(&certificate_list) > 0) {
  1146. CBS certificate;
  1147. const uint8_t *data;
  1148. if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
  1149. al = SSL_AD_DECODE_ERROR;
  1150. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1151. goto f_err;
  1152. }
  1153. if (is_first_certificate && ssl->ctx->retain_only_sha256_of_client_certs) {
  1154. /* If this is the first certificate, and we don't want to keep peer
  1155. * certificates in memory, then we hash it right away. */
  1156. SHA256_Init(&sha256);
  1157. SHA256_Update(&sha256, CBS_data(&certificate), CBS_len(&certificate));
  1158. SHA256_Final(ssl->session->peer_sha256, &sha256);
  1159. ssl->session->peer_sha256_valid = 1;
  1160. }
  1161. is_first_certificate = 0;
  1162. /* A u24 length cannot overflow a long. */
  1163. data = CBS_data(&certificate);
  1164. x = d2i_X509(NULL, &data, (long)CBS_len(&certificate));
  1165. if (x == NULL) {
  1166. al = SSL_AD_BAD_CERTIFICATE;
  1167. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  1168. goto f_err;
  1169. }
  1170. if (data != CBS_data(&certificate) + CBS_len(&certificate)) {
  1171. al = SSL_AD_DECODE_ERROR;
  1172. OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
  1173. goto f_err;
  1174. }
  1175. if (!sk_X509_push(sk, x)) {
  1176. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1177. goto err;
  1178. }
  1179. x = NULL;
  1180. }
  1181. if (sk_X509_num(sk) <= 0) {
  1182. /* No client certificate so the handshake buffer may be discarded. */
  1183. ssl3_free_handshake_buffer(ssl);
  1184. /* TLS does not mind 0 certs returned */
  1185. if (ssl->version == SSL3_VERSION) {
  1186. al = SSL_AD_HANDSHAKE_FAILURE;
  1187. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED);
  1188. goto f_err;
  1189. } else if ((ssl->verify_mode & SSL_VERIFY_PEER) &&
  1190. (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
  1191. /* Fail for TLS only if we required a certificate */
  1192. OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
  1193. al = SSL_AD_HANDSHAKE_FAILURE;
  1194. goto f_err;
  1195. }
  1196. } else {
  1197. if (ssl_verify_cert_chain(ssl, sk) <= 0) {
  1198. al = ssl_verify_alarm_type(ssl->verify_result);
  1199. OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
  1200. goto f_err;
  1201. }
  1202. }
  1203. X509_free(ssl->session->peer);
  1204. ssl->session->peer = sk_X509_shift(sk);
  1205. ssl->session->verify_result = ssl->verify_result;
  1206. sk_X509_pop_free(ssl->session->cert_chain, X509_free);
  1207. ssl->session->cert_chain = sk;
  1208. /* Inconsistency alert: cert_chain does *not* include the peer's own
  1209. * certificate, while we do include it in s3_clnt.c */
  1210. sk = NULL;
  1211. ret = 1;
  1212. if (0) {
  1213. f_err:
  1214. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  1215. }
  1216. err:
  1217. X509_free(x);
  1218. sk_X509_pop_free(sk, X509_free);
  1219. return ret;
  1220. }
  1221. static int ssl3_get_client_key_exchange(SSL *ssl) {
  1222. int al;
  1223. CBS client_key_exchange;
  1224. uint32_t alg_k;
  1225. uint32_t alg_a;
  1226. uint8_t *premaster_secret = NULL;
  1227. size_t premaster_secret_len = 0;
  1228. uint8_t *decrypt_buf = NULL;
  1229. unsigned psk_len = 0;
  1230. uint8_t psk[PSK_MAX_PSK_LEN];
  1231. if (ssl->state == SSL3_ST_SR_KEY_EXCH_A) {
  1232. int ok;
  1233. const long n = ssl->method->ssl_get_message(
  1234. ssl, SSL3_MT_CLIENT_KEY_EXCHANGE, ssl_hash_message, &ok);
  1235. if (!ok) {
  1236. return n;
  1237. }
  1238. }
  1239. CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num);
  1240. alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
  1241. alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
  1242. /* If using a PSK key exchange, prepare the pre-shared key. */
  1243. if (alg_a & SSL_aPSK) {
  1244. CBS psk_identity;
  1245. /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK,
  1246. * then this is the only field in the message. */
  1247. if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) ||
  1248. ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) {
  1249. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1250. al = SSL_AD_DECODE_ERROR;
  1251. goto f_err;
  1252. }
  1253. if (ssl->psk_server_callback == NULL) {
  1254. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB);
  1255. al = SSL_AD_INTERNAL_ERROR;
  1256. goto f_err;
  1257. }
  1258. if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN ||
  1259. CBS_contains_zero_byte(&psk_identity)) {
  1260. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  1261. al = SSL_AD_ILLEGAL_PARAMETER;
  1262. goto f_err;
  1263. }
  1264. if (!CBS_strdup(&psk_identity, &ssl->session->psk_identity)) {
  1265. al = SSL_AD_INTERNAL_ERROR;
  1266. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1267. goto f_err;
  1268. }
  1269. /* Look up the key for the identity. */
  1270. psk_len = ssl->psk_server_callback(ssl, ssl->session->psk_identity, psk,
  1271. sizeof(psk));
  1272. if (psk_len > PSK_MAX_PSK_LEN) {
  1273. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1274. al = SSL_AD_INTERNAL_ERROR;
  1275. goto f_err;
  1276. } else if (psk_len == 0) {
  1277. /* PSK related to the given identity not found */
  1278. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
  1279. al = SSL_AD_UNKNOWN_PSK_IDENTITY;
  1280. goto f_err;
  1281. }
  1282. }
  1283. /* Depending on the key exchange method, compute |premaster_secret| and
  1284. * |premaster_secret_len|. */
  1285. if (alg_k & SSL_kRSA) {
  1286. /* Allocate a buffer large enough for an RSA decryption. */
  1287. const size_t rsa_size = ssl_private_key_max_signature_len(ssl);
  1288. decrypt_buf = OPENSSL_malloc(rsa_size);
  1289. if (decrypt_buf == NULL) {
  1290. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1291. goto err;
  1292. }
  1293. enum ssl_private_key_result_t decrypt_result;
  1294. size_t decrypt_len;
  1295. if (ssl->state == SSL3_ST_SR_KEY_EXCH_A) {
  1296. if (!ssl_has_private_key(ssl) ||
  1297. ssl_private_key_type(ssl) != EVP_PKEY_RSA) {
  1298. al = SSL_AD_HANDSHAKE_FAILURE;
  1299. OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE);
  1300. goto f_err;
  1301. }
  1302. CBS encrypted_premaster_secret;
  1303. if (ssl->version > SSL3_VERSION) {
  1304. if (!CBS_get_u16_length_prefixed(&client_key_exchange,
  1305. &encrypted_premaster_secret) ||
  1306. CBS_len(&client_key_exchange) != 0) {
  1307. al = SSL_AD_DECODE_ERROR;
  1308. OPENSSL_PUT_ERROR(SSL,
  1309. SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
  1310. goto f_err;
  1311. }
  1312. } else {
  1313. encrypted_premaster_secret = client_key_exchange;
  1314. }
  1315. /* Decrypt with no padding. PKCS#1 padding will be removed as part of the
  1316. * timing-sensitive code below. */
  1317. decrypt_result = ssl_private_key_decrypt(
  1318. ssl, decrypt_buf, &decrypt_len, rsa_size,
  1319. CBS_data(&encrypted_premaster_secret),
  1320. CBS_len(&encrypted_premaster_secret));
  1321. } else {
  1322. assert(ssl->state == SSL3_ST_SR_KEY_EXCH_B);
  1323. /* Complete async decrypt. */
  1324. decrypt_result = ssl_private_key_decrypt_complete(
  1325. ssl, decrypt_buf, &decrypt_len, rsa_size);
  1326. }
  1327. switch (decrypt_result) {
  1328. case ssl_private_key_success:
  1329. break;
  1330. case ssl_private_key_failure:
  1331. goto err;
  1332. case ssl_private_key_retry:
  1333. ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
  1334. ssl->state = SSL3_ST_SR_KEY_EXCH_B;
  1335. goto err;
  1336. }
  1337. if (decrypt_len != rsa_size) {
  1338. al = SSL_AD_DECRYPT_ERROR;
  1339. OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
  1340. goto f_err;
  1341. }
  1342. /* Prepare a random premaster, to be used on invalid padding. See RFC 5246,
  1343. * section 7.4.7.1. */
  1344. premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH;
  1345. premaster_secret = OPENSSL_malloc(premaster_secret_len);
  1346. if (premaster_secret == NULL) {
  1347. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1348. goto err;
  1349. }
  1350. if (!RAND_bytes(premaster_secret, premaster_secret_len)) {
  1351. goto err;
  1352. }
  1353. /* The smallest padded premaster is 11 bytes of overhead. Small keys are
  1354. * publicly invalid. */
  1355. if (decrypt_len < 11 + premaster_secret_len) {
  1356. al = SSL_AD_DECRYPT_ERROR;
  1357. OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED);
  1358. goto f_err;
  1359. }
  1360. /* Check the padding. See RFC 3447, section 7.2.2. */
  1361. size_t padding_len = decrypt_len - premaster_secret_len;
  1362. uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) &
  1363. constant_time_eq_int_8(decrypt_buf[1], 2);
  1364. size_t i;
  1365. for (i = 2; i < padding_len - 1; i++) {
  1366. good &= ~constant_time_is_zero_8(decrypt_buf[i]);
  1367. }
  1368. good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]);
  1369. /* The premaster secret must begin with |client_version|. This too must be
  1370. * checked in constant time (http://eprint.iacr.org/2003/052/). */
  1371. good &= constant_time_eq_8(decrypt_buf[padding_len],
  1372. (unsigned)(ssl->client_version >> 8));
  1373. good &= constant_time_eq_8(decrypt_buf[padding_len + 1],
  1374. (unsigned)(ssl->client_version & 0xff));
  1375. /* Select, in constant time, either the decrypted premaster or the random
  1376. * premaster based on |good|. */
  1377. for (i = 0; i < premaster_secret_len; i++) {
  1378. premaster_secret[i] = constant_time_select_8(
  1379. good, decrypt_buf[padding_len + i], premaster_secret[i]);
  1380. }
  1381. OPENSSL_free(decrypt_buf);
  1382. decrypt_buf = NULL;
  1383. } else if (alg_k & (SSL_kECDHE|SSL_kDHE|SSL_kCECPQ1)) {
  1384. /* Parse the ClientKeyExchange. */
  1385. CBS peer_key;
  1386. if (!SSL_ECDH_CTX_get_key(&ssl->s3->tmp.ecdh_ctx, &client_key_exchange,
  1387. &peer_key) ||
  1388. CBS_len(&client_key_exchange) != 0) {
  1389. al = SSL_AD_DECODE_ERROR;
  1390. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1391. goto f_err;
  1392. }
  1393. /* Compute the premaster. */
  1394. uint8_t alert;
  1395. if (!SSL_ECDH_CTX_finish(&ssl->s3->tmp.ecdh_ctx, &premaster_secret,
  1396. &premaster_secret_len, &alert, CBS_data(&peer_key),
  1397. CBS_len(&peer_key))) {
  1398. al = alert;
  1399. goto f_err;
  1400. }
  1401. /* The key exchange state may now be discarded. */
  1402. SSL_ECDH_CTX_cleanup(&ssl->s3->tmp.ecdh_ctx);
  1403. } else if (alg_k & SSL_kPSK) {
  1404. /* For plain PSK, other_secret is a block of 0s with the same length as the
  1405. * pre-shared key. */
  1406. premaster_secret_len = psk_len;
  1407. premaster_secret = OPENSSL_malloc(premaster_secret_len);
  1408. if (premaster_secret == NULL) {
  1409. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1410. goto err;
  1411. }
  1412. memset(premaster_secret, 0, premaster_secret_len);
  1413. } else {
  1414. al = SSL_AD_HANDSHAKE_FAILURE;
  1415. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE);
  1416. goto f_err;
  1417. }
  1418. /* For a PSK cipher suite, the actual pre-master secret is combined with the
  1419. * pre-shared key. */
  1420. if (alg_a & SSL_aPSK) {
  1421. CBB new_premaster, child;
  1422. uint8_t *new_data;
  1423. size_t new_len;
  1424. CBB_zero(&new_premaster);
  1425. if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) ||
  1426. !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
  1427. !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) ||
  1428. !CBB_add_u16_length_prefixed(&new_premaster, &child) ||
  1429. !CBB_add_bytes(&child, psk, psk_len) ||
  1430. !CBB_finish(&new_premaster, &new_data, &new_len)) {
  1431. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1432. CBB_cleanup(&new_premaster);
  1433. goto err;
  1434. }
  1435. OPENSSL_cleanse(premaster_secret, premaster_secret_len);
  1436. OPENSSL_free(premaster_secret);
  1437. premaster_secret = new_data;
  1438. premaster_secret_len = new_len;
  1439. }
  1440. /* Compute the master secret */
  1441. ssl->session->master_key_length = tls1_generate_master_secret(
  1442. ssl, ssl->session->master_key, premaster_secret, premaster_secret_len);
  1443. if (ssl->session->master_key_length == 0) {
  1444. goto err;
  1445. }
  1446. ssl->session->extended_master_secret = ssl->s3->tmp.extended_master_secret;
  1447. OPENSSL_cleanse(premaster_secret, premaster_secret_len);
  1448. OPENSSL_free(premaster_secret);
  1449. return 1;
  1450. f_err:
  1451. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  1452. err:
  1453. if (premaster_secret != NULL) {
  1454. OPENSSL_cleanse(premaster_secret, premaster_secret_len);
  1455. OPENSSL_free(premaster_secret);
  1456. }
  1457. OPENSSL_free(decrypt_buf);
  1458. return -1;
  1459. }
  1460. static int ssl3_get_cert_verify(SSL *ssl) {
  1461. int al, ok, ret = 0;
  1462. long n;
  1463. CBS certificate_verify, signature;
  1464. X509 *peer = ssl->session->peer;
  1465. EVP_PKEY *pkey = NULL;
  1466. /* Only RSA and ECDSA client certificates are supported, so a
  1467. * CertificateVerify is required if and only if there's a client certificate.
  1468. * */
  1469. if (peer == NULL) {
  1470. ssl3_free_handshake_buffer(ssl);
  1471. return 1;
  1472. }
  1473. n = ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE_VERIFY,
  1474. ssl_dont_hash_message, &ok);
  1475. if (!ok) {
  1476. return n;
  1477. }
  1478. /* Filter out unsupported certificate types. */
  1479. pkey = X509_get_pubkey(peer);
  1480. if (pkey == NULL) {
  1481. goto err;
  1482. }
  1483. if (!(X509_certificate_type(peer, pkey) & EVP_PKT_SIGN) ||
  1484. (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_EC)) {
  1485. al = SSL_AD_UNSUPPORTED_CERTIFICATE;
  1486. OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE);
  1487. goto f_err;
  1488. }
  1489. CBS_init(&certificate_verify, ssl->init_msg, n);
  1490. /* Determine the digest type if needbe. */
  1491. uint16_t signature_algorithm = 0;
  1492. if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) {
  1493. if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) {
  1494. al = SSL_AD_DECODE_ERROR;
  1495. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1496. goto f_err;
  1497. }
  1498. if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm, pkey)) {
  1499. goto f_err;
  1500. }
  1501. ssl->s3->tmp.peer_signature_algorithm = signature_algorithm;
  1502. } else if (pkey->type == EVP_PKEY_RSA) {
  1503. signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
  1504. } else if (pkey->type == EVP_PKEY_EC) {
  1505. signature_algorithm = SSL_SIGN_ECDSA_SHA1;
  1506. }
  1507. /* Parse and verify the signature. */
  1508. if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) ||
  1509. CBS_len(&certificate_verify) != 0) {
  1510. al = SSL_AD_DECODE_ERROR;
  1511. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1512. goto f_err;
  1513. }
  1514. int sig_ok;
  1515. /* The SSL3 construction for CertificateVerify does not decompose into a
  1516. * single final digest and signature, and must be special-cased. */
  1517. if (ssl3_protocol_version(ssl) == SSL3_VERSION) {
  1518. if (ssl->cert->key_method != NULL) {
  1519. OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL_FOR_CUSTOM_KEY);
  1520. goto err;
  1521. }
  1522. uint8_t digest[EVP_MAX_MD_SIZE];
  1523. size_t digest_len;
  1524. if (!ssl3_cert_verify_hash(ssl, digest, &digest_len,
  1525. signature_algorithm)) {
  1526. goto err;
  1527. }
  1528. const EVP_MD *md = tls12_get_hash(signature_algorithm);
  1529. assert(md != NULL);
  1530. EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey, NULL);
  1531. sig_ok = pctx != NULL &&
  1532. EVP_PKEY_verify_init(pctx) &&
  1533. EVP_PKEY_CTX_set_signature_md(pctx, md) &&
  1534. EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature),
  1535. digest, digest_len);
  1536. EVP_PKEY_CTX_free(pctx);
  1537. } else {
  1538. sig_ok = ssl_public_key_verify(
  1539. ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm,
  1540. pkey, (const uint8_t *)ssl->s3->handshake_buffer->data,
  1541. ssl->s3->handshake_buffer->length);
  1542. }
  1543. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  1544. sig_ok = 1;
  1545. ERR_clear_error();
  1546. #endif
  1547. if (!sig_ok) {
  1548. al = SSL_AD_DECRYPT_ERROR;
  1549. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
  1550. goto f_err;
  1551. }
  1552. /* The handshake buffer is no longer necessary, and we may hash the current
  1553. * message.*/
  1554. ssl3_free_handshake_buffer(ssl);
  1555. if (!ssl3_hash_current_message(ssl)) {
  1556. goto err;
  1557. }
  1558. ret = 1;
  1559. if (0) {
  1560. f_err:
  1561. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  1562. }
  1563. err:
  1564. EVP_PKEY_free(pkey);
  1565. return ret;
  1566. }
  1567. /* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It
  1568. * sets the next_proto member in s if found */
  1569. static int ssl3_get_next_proto(SSL *ssl) {
  1570. int ok;
  1571. long n;
  1572. CBS next_protocol, selected_protocol, padding;
  1573. n = ssl->method->ssl_get_message(ssl, SSL3_MT_NEXT_PROTO, ssl_hash_message,
  1574. &ok);
  1575. if (!ok) {
  1576. return n;
  1577. }
  1578. CBS_init(&next_protocol, ssl->init_msg, n);
  1579. if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) ||
  1580. !CBS_get_u8_length_prefixed(&next_protocol, &padding) ||
  1581. CBS_len(&next_protocol) != 0 ||
  1582. !CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated,
  1583. &ssl->s3->next_proto_negotiated_len)) {
  1584. return 0;
  1585. }
  1586. return 1;
  1587. }
  1588. /* ssl3_get_channel_id reads and verifies a ClientID handshake message. */
  1589. static int ssl3_get_channel_id(SSL *ssl) {
  1590. int ret = -1, ok;
  1591. long n;
  1592. uint8_t channel_id_hash[EVP_MAX_MD_SIZE];
  1593. size_t channel_id_hash_len;
  1594. const uint8_t *p;
  1595. uint16_t extension_type;
  1596. EC_GROUP *p256 = NULL;
  1597. EC_KEY *key = NULL;
  1598. EC_POINT *point = NULL;
  1599. ECDSA_SIG sig;
  1600. BIGNUM x, y;
  1601. CBS encrypted_extensions, extension;
  1602. n = ssl->method->ssl_get_message(ssl, SSL3_MT_CHANNEL_ID,
  1603. ssl_dont_hash_message, &ok);
  1604. if (!ok) {
  1605. return n;
  1606. }
  1607. /* Before incorporating the EncryptedExtensions message to the handshake
  1608. * hash, compute the hash that should have been signed. */
  1609. if (!tls1_channel_id_hash(ssl, channel_id_hash, &channel_id_hash_len)) {
  1610. return -1;
  1611. }
  1612. assert(channel_id_hash_len == SHA256_DIGEST_LENGTH);
  1613. if (!ssl3_hash_current_message(ssl)) {
  1614. return -1;
  1615. }
  1616. CBS_init(&encrypted_extensions, ssl->init_msg, n);
  1617. /* EncryptedExtensions could include multiple extensions, but the only
  1618. * extension that could be negotiated is Channel ID, so there can only be one
  1619. * entry. */
  1620. if (!CBS_get_u16(&encrypted_extensions, &extension_type) ||
  1621. !CBS_get_u16_length_prefixed(&encrypted_extensions, &extension) ||
  1622. CBS_len(&encrypted_extensions) != 0 ||
  1623. extension_type != TLSEXT_TYPE_channel_id ||
  1624. CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
  1625. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_MESSAGE);
  1626. return -1;
  1627. }
  1628. p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
  1629. if (!p256) {
  1630. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
  1631. return -1;
  1632. }
  1633. BN_init(&x);
  1634. BN_init(&y);
  1635. sig.r = BN_new();
  1636. sig.s = BN_new();
  1637. if (sig.r == NULL || sig.s == NULL) {
  1638. goto err;
  1639. }
  1640. p = CBS_data(&extension);
  1641. if (BN_bin2bn(p + 0, 32, &x) == NULL ||
  1642. BN_bin2bn(p + 32, 32, &y) == NULL ||
  1643. BN_bin2bn(p + 64, 32, sig.r) == NULL ||
  1644. BN_bin2bn(p + 96, 32, sig.s) == NULL) {
  1645. goto err;
  1646. }
  1647. point = EC_POINT_new(p256);
  1648. if (!point ||
  1649. !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) {
  1650. goto err;
  1651. }
  1652. key = EC_KEY_new();
  1653. if (!key || !EC_KEY_set_group(key, p256) ||
  1654. !EC_KEY_set_public_key(key, point)) {
  1655. goto err;
  1656. }
  1657. /* We stored the handshake hash in |tlsext_channel_id| the first time that we
  1658. * were called. */
  1659. if (!ECDSA_do_verify(channel_id_hash, channel_id_hash_len, &sig, key)) {
  1660. OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
  1661. ssl->s3->tlsext_channel_id_valid = 0;
  1662. goto err;
  1663. }
  1664. memcpy(ssl->s3->tlsext_channel_id, p, 64);
  1665. ret = 1;
  1666. err:
  1667. BN_free(&x);
  1668. BN_free(&y);
  1669. BN_free(sig.r);
  1670. BN_free(sig.s);
  1671. EC_KEY_free(key);
  1672. EC_POINT_free(point);
  1673. EC_GROUP_free(p256);
  1674. return ret;
  1675. }
  1676. static int ssl3_send_new_session_ticket(SSL *ssl) {
  1677. if (ssl->state == SSL3_ST_SW_SESSION_TICKET_B) {
  1678. return ssl->method->write_message(ssl);
  1679. }
  1680. /* Serialize the SSL_SESSION to be encoded into the ticket. */
  1681. uint8_t *session = NULL;
  1682. size_t session_len;
  1683. if (!SSL_SESSION_to_bytes_for_ticket(ssl->session, &session,
  1684. &session_len)) {
  1685. return -1;
  1686. }
  1687. EVP_CIPHER_CTX ctx;
  1688. EVP_CIPHER_CTX_init(&ctx);
  1689. HMAC_CTX hctx;
  1690. HMAC_CTX_init(&hctx);
  1691. int ret = -1;
  1692. CBB cbb, body, ticket;
  1693. if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEW_SESSION_TICKET) ||
  1694. /* Ticket lifetime hint (advisory only): We leave this unspecified for
  1695. * resumed session (for simplicity), and guess that tickets for new
  1696. * sessions will live as long as their sessions. */
  1697. !CBB_add_u32(&body, ssl->hit ? 0 : ssl->session->timeout) ||
  1698. !CBB_add_u16_length_prefixed(&body, &ticket)) {
  1699. goto err;
  1700. }
  1701. /* If the session is too long, emit a dummy value rather than abort the
  1702. * connection. */
  1703. const size_t max_ticket_overhead =
  1704. 16 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE;
  1705. if (session_len > 0xffff - max_ticket_overhead) {
  1706. static const char kTicketPlaceholder[] = "TICKET TOO LARGE";
  1707. if (!CBB_add_bytes(&ticket, (const uint8_t *)kTicketPlaceholder,
  1708. strlen(kTicketPlaceholder)) ||
  1709. !ssl->method->finish_message(ssl, &cbb)) {
  1710. goto err;
  1711. }
  1712. ssl->state = SSL3_ST_SW_SESSION_TICKET_B;
  1713. ret = 1;
  1714. goto err;
  1715. }
  1716. /* Initialize HMAC and cipher contexts. If callback present it does all the
  1717. * work otherwise use generated values from parent ctx. */
  1718. SSL_CTX *tctx = ssl->initial_ctx;
  1719. uint8_t iv[EVP_MAX_IV_LENGTH];
  1720. uint8_t key_name[16];
  1721. if (tctx->tlsext_ticket_key_cb != NULL) {
  1722. if (tctx->tlsext_ticket_key_cb(ssl, key_name, iv, &ctx, &hctx,
  1723. 1 /* encrypt */) < 0) {
  1724. goto err;
  1725. }
  1726. } else {
  1727. if (!RAND_bytes(iv, 16) ||
  1728. !EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
  1729. tctx->tlsext_tick_aes_key, iv) ||
  1730. !HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(),
  1731. NULL)) {
  1732. goto err;
  1733. }
  1734. memcpy(key_name, tctx->tlsext_tick_key_name, 16);
  1735. }
  1736. uint8_t *ptr;
  1737. if (!CBB_add_bytes(&ticket, key_name, 16) ||
  1738. !CBB_add_bytes(&ticket, iv, EVP_CIPHER_CTX_iv_length(&ctx)) ||
  1739. !CBB_reserve(&ticket, &ptr, session_len + EVP_MAX_BLOCK_LENGTH)) {
  1740. goto err;
  1741. }
  1742. int len;
  1743. size_t total = 0;
  1744. if (!EVP_EncryptUpdate(&ctx, ptr + total, &len, session, session_len)) {
  1745. goto err;
  1746. }
  1747. total += len;
  1748. if (!EVP_EncryptFinal_ex(&ctx, ptr + total, &len)) {
  1749. goto err;
  1750. }
  1751. total += len;
  1752. if (!CBB_did_write(&ticket, total)) {
  1753. goto err;
  1754. }
  1755. unsigned hlen;
  1756. if (!HMAC_Update(&hctx, CBB_data(&ticket), CBB_len(&ticket)) ||
  1757. !CBB_reserve(&ticket, &ptr, EVP_MAX_MD_SIZE) ||
  1758. !HMAC_Final(&hctx, ptr, &hlen) ||
  1759. !CBB_did_write(&ticket, hlen) ||
  1760. !ssl->method->finish_message(ssl, &cbb)) {
  1761. goto err;
  1762. }
  1763. ssl->state = SSL3_ST_SW_SESSION_TICKET_B;
  1764. ret = ssl->method->write_message(ssl);
  1765. err:
  1766. OPENSSL_free(session);
  1767. EVP_CIPHER_CTX_cleanup(&ctx);
  1768. HMAC_CTX_cleanup(&hctx);
  1769. return ret;
  1770. }