選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

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