Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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