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

2731 lines
78 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com).
  108. *
  109. */
  110. /* ====================================================================
  111. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  112. * ECC cipher suite support in OpenSSL originally developed by
  113. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  114. */
  115. /* ====================================================================
  116. * Copyright 2005 Nokia. All rights reserved.
  117. *
  118. * The portions of the attached software ("Contribution") is developed by
  119. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  120. * license.
  121. *
  122. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  123. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  124. * support (see RFC 4279) to OpenSSL.
  125. *
  126. * No patent licenses or other rights except those expressly stated in
  127. * the OpenSSL open source license shall be deemed granted or received
  128. * expressly, by implication, estoppel, or otherwise.
  129. *
  130. * No assurances are provided by Nokia that the Contribution does not
  131. * infringe the patent or other intellectual property rights of any third
  132. * party or that the license provides you with all the necessary rights
  133. * to make use of the Contribution.
  134. *
  135. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  136. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  137. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  138. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  139. * OTHERWISE. */
  140. #include <openssl/ssl.h>
  141. #include <assert.h>
  142. #include <stdlib.h>
  143. #include <string.h>
  144. #include <openssl/bytestring.h>
  145. #include <openssl/crypto.h>
  146. #include <openssl/err.h>
  147. #include <openssl/lhash.h>
  148. #include <openssl/mem.h>
  149. #include <openssl/rand.h>
  150. #include "internal.h"
  151. #include "../crypto/internal.h"
  152. #if defined(OPENSSL_WINDOWS)
  153. #include <sys/timeb.h>
  154. #else
  155. #include <sys/socket.h>
  156. #include <sys/time.h>
  157. #endif
  158. /* |SSL_R_UNKNOWN_PROTOCOL| is no longer emitted, but continue to define it
  159. * to avoid downstream churn. */
  160. OPENSSL_DECLARE_ERROR_REASON(SSL, UNKNOWN_PROTOCOL)
  161. /* The following errors are no longer emitted, but are used in nginx without
  162. * #ifdefs. */
  163. OPENSSL_DECLARE_ERROR_REASON(SSL, BLOCK_CIPHER_PAD_IS_WRONG)
  164. OPENSSL_DECLARE_ERROR_REASON(SSL, NO_CIPHERS_SPECIFIED)
  165. /* Some error codes are special. Ensure the make_errors.go script never
  166. * regresses this. */
  167. OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
  168. SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
  169. ssl_alert_reason_code_mismatch);
  170. /* kMaxHandshakeSize is the maximum size, in bytes, of a handshake message. */
  171. static const size_t kMaxHandshakeSize = (1u << 24) - 1;
  172. static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl =
  173. CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
  174. static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx =
  175. CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
  176. int SSL_library_init(void) {
  177. CRYPTO_library_init();
  178. return 1;
  179. }
  180. static uint32_t ssl_session_hash(const SSL_SESSION *sess) {
  181. const uint8_t *session_id = sess->session_id;
  182. uint8_t tmp_storage[sizeof(uint32_t)];
  183. if (sess->session_id_length < sizeof(tmp_storage)) {
  184. OPENSSL_memset(tmp_storage, 0, sizeof(tmp_storage));
  185. OPENSSL_memcpy(tmp_storage, sess->session_id, sess->session_id_length);
  186. session_id = tmp_storage;
  187. }
  188. uint32_t hash =
  189. ((uint32_t)session_id[0]) |
  190. ((uint32_t)session_id[1] << 8) |
  191. ((uint32_t)session_id[2] << 16) |
  192. ((uint32_t)session_id[3] << 24);
  193. return hash;
  194. }
  195. /* NB: If this function (or indeed the hash function which uses a sort of
  196. * coarser function than this one) is changed, ensure
  197. * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
  198. * able to construct an SSL_SESSION that will collide with any existing session
  199. * with a matching session ID. */
  200. static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
  201. if (a->ssl_version != b->ssl_version) {
  202. return 1;
  203. }
  204. if (a->session_id_length != b->session_id_length) {
  205. return 1;
  206. }
  207. return OPENSSL_memcmp(a->session_id, b->session_id, a->session_id_length);
  208. }
  209. SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
  210. SSL_CTX *ret = NULL;
  211. if (method == NULL) {
  212. OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
  213. return NULL;
  214. }
  215. ret = OPENSSL_malloc(sizeof(SSL_CTX));
  216. if (ret == NULL) {
  217. goto err;
  218. }
  219. OPENSSL_memset(ret, 0, sizeof(SSL_CTX));
  220. ret->method = method->method;
  221. ret->x509_method = method->x509_method;
  222. CRYPTO_MUTEX_init(&ret->lock);
  223. ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
  224. ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
  225. ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  226. ret->session_psk_dhe_timeout = SSL_DEFAULT_SESSION_PSK_DHE_TIMEOUT;
  227. ret->references = 1;
  228. ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
  229. ret->verify_mode = SSL_VERIFY_NONE;
  230. ret->cert = ssl_cert_new(method->x509_method);
  231. if (ret->cert == NULL) {
  232. goto err;
  233. }
  234. ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
  235. if (ret->sessions == NULL) {
  236. goto err;
  237. }
  238. if (!ret->x509_method->ssl_ctx_new(ret)) {
  239. goto err;
  240. }
  241. if (!SSL_CTX_set_strict_cipher_list(ret, SSL_DEFAULT_CIPHER_LIST)) {
  242. goto err2;
  243. }
  244. ret->client_CA = sk_CRYPTO_BUFFER_new_null();
  245. if (ret->client_CA == NULL) {
  246. goto err;
  247. }
  248. CRYPTO_new_ex_data(&ret->ex_data);
  249. ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  250. /* Setup RFC4507 ticket keys */
  251. if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
  252. !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
  253. !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
  254. ret->options |= SSL_OP_NO_TICKET;
  255. }
  256. /* Disable the auto-chaining feature by default. Once this has stuck without
  257. * problems, the feature will be removed entirely. */
  258. ret->mode = SSL_MODE_NO_AUTO_CHAIN;
  259. /* Lock the SSL_CTX to the specified version, for compatibility with legacy
  260. * uses of SSL_METHOD, but we do not set the minimum version for
  261. * |SSLv3_method|. */
  262. if (!SSL_CTX_set_max_proto_version(ret, method->version) ||
  263. !SSL_CTX_set_min_proto_version(ret, method->version == SSL3_VERSION
  264. ? 0 /* default */
  265. : method->version)) {
  266. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  267. goto err2;
  268. }
  269. return ret;
  270. err:
  271. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  272. err2:
  273. SSL_CTX_free(ret);
  274. return NULL;
  275. }
  276. int SSL_CTX_up_ref(SSL_CTX *ctx) {
  277. CRYPTO_refcount_inc(&ctx->references);
  278. return 1;
  279. }
  280. void SSL_CTX_free(SSL_CTX *ctx) {
  281. if (ctx == NULL ||
  282. !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
  283. return;
  284. }
  285. /* Free internal session cache. However: the remove_cb() may reference the
  286. * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
  287. * sessions were flushed. As the ex_data handling routines might also touch
  288. * the session cache, the most secure solution seems to be: empty (flush) the
  289. * cache, then free ex_data, then finally free the cache. (See ticket
  290. * [openssl.org #212].) */
  291. SSL_CTX_flush_sessions(ctx, 0);
  292. CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
  293. CRYPTO_MUTEX_cleanup(&ctx->lock);
  294. lh_SSL_SESSION_free(ctx->sessions);
  295. ssl_cipher_preference_list_free(ctx->cipher_list);
  296. ssl_cert_free(ctx->cert);
  297. sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions,
  298. SSL_CUSTOM_EXTENSION_free);
  299. sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions,
  300. SSL_CUSTOM_EXTENSION_free);
  301. sk_CRYPTO_BUFFER_pop_free(ctx->client_CA, CRYPTO_BUFFER_free);
  302. ctx->x509_method->ssl_ctx_free(ctx);
  303. sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
  304. OPENSSL_free(ctx->psk_identity_hint);
  305. OPENSSL_free(ctx->supported_group_list);
  306. OPENSSL_free(ctx->alpn_client_proto_list);
  307. EVP_PKEY_free(ctx->tlsext_channel_id_private);
  308. OPENSSL_free(ctx->verify_sigalgs);
  309. OPENSSL_free(ctx);
  310. }
  311. SSL *SSL_new(SSL_CTX *ctx) {
  312. if (ctx == NULL) {
  313. OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
  314. return NULL;
  315. }
  316. if (ctx->method == NULL) {
  317. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
  318. return NULL;
  319. }
  320. SSL *ssl = OPENSSL_malloc(sizeof(SSL));
  321. if (ssl == NULL) {
  322. goto err;
  323. }
  324. OPENSSL_memset(ssl, 0, sizeof(SSL));
  325. ssl->min_version = ctx->min_version;
  326. ssl->max_version = ctx->max_version;
  327. /* RFC 6347 states that implementations SHOULD use an initial timer value of
  328. * 1 second. */
  329. ssl->initial_timeout_duration_ms = 1000;
  330. ssl->options = ctx->options;
  331. ssl->mode = ctx->mode;
  332. ssl->max_cert_list = ctx->max_cert_list;
  333. ssl->cert = ssl_cert_dup(ctx->cert);
  334. if (ssl->cert == NULL) {
  335. goto err;
  336. }
  337. ssl->msg_callback = ctx->msg_callback;
  338. ssl->msg_callback_arg = ctx->msg_callback_arg;
  339. ssl->verify_mode = ctx->verify_mode;
  340. ssl->verify_callback = ctx->default_verify_callback;
  341. ssl->retain_only_sha256_of_client_certs =
  342. ctx->retain_only_sha256_of_client_certs;
  343. ssl->quiet_shutdown = ctx->quiet_shutdown;
  344. ssl->max_send_fragment = ctx->max_send_fragment;
  345. SSL_CTX_up_ref(ctx);
  346. ssl->ctx = ctx;
  347. SSL_CTX_up_ref(ctx);
  348. ssl->session_ctx = ctx;
  349. if (!ssl->ctx->x509_method->ssl_new(ssl)) {
  350. goto err;
  351. }
  352. if (ctx->supported_group_list) {
  353. ssl->supported_group_list = BUF_memdup(ctx->supported_group_list,
  354. ctx->supported_group_list_len * 2);
  355. if (!ssl->supported_group_list) {
  356. goto err;
  357. }
  358. ssl->supported_group_list_len = ctx->supported_group_list_len;
  359. }
  360. if (ctx->alpn_client_proto_list) {
  361. ssl->alpn_client_proto_list = BUF_memdup(ctx->alpn_client_proto_list,
  362. ctx->alpn_client_proto_list_len);
  363. if (ssl->alpn_client_proto_list == NULL) {
  364. goto err;
  365. }
  366. ssl->alpn_client_proto_list_len = ctx->alpn_client_proto_list_len;
  367. }
  368. ssl->method = ctx->method;
  369. if (!ssl->method->ssl_new(ssl)) {
  370. goto err;
  371. }
  372. ssl->rwstate = SSL_NOTHING;
  373. CRYPTO_new_ex_data(&ssl->ex_data);
  374. ssl->psk_identity_hint = NULL;
  375. if (ctx->psk_identity_hint) {
  376. ssl->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
  377. if (ssl->psk_identity_hint == NULL) {
  378. goto err;
  379. }
  380. }
  381. ssl->psk_client_callback = ctx->psk_client_callback;
  382. ssl->psk_server_callback = ctx->psk_server_callback;
  383. ssl->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
  384. if (ctx->tlsext_channel_id_private) {
  385. EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
  386. ssl->tlsext_channel_id_private = ctx->tlsext_channel_id_private;
  387. }
  388. ssl->signed_cert_timestamps_enabled = ctx->signed_cert_timestamps_enabled;
  389. ssl->ocsp_stapling_enabled = ctx->ocsp_stapling_enabled;
  390. return ssl;
  391. err:
  392. SSL_free(ssl);
  393. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  394. return NULL;
  395. }
  396. void SSL_free(SSL *ssl) {
  397. if (ssl == NULL) {
  398. return;
  399. }
  400. ssl->ctx->x509_method->ssl_free(ssl);
  401. CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
  402. BIO_free_all(ssl->rbio);
  403. BIO_free_all(ssl->wbio);
  404. BUF_MEM_free(ssl->init_buf);
  405. /* add extra stuff */
  406. ssl_cipher_preference_list_free(ssl->cipher_list);
  407. SSL_SESSION_free(ssl->session);
  408. ssl_cert_free(ssl->cert);
  409. OPENSSL_free(ssl->tlsext_hostname);
  410. SSL_CTX_free(ssl->session_ctx);
  411. OPENSSL_free(ssl->supported_group_list);
  412. OPENSSL_free(ssl->alpn_client_proto_list);
  413. EVP_PKEY_free(ssl->tlsext_channel_id_private);
  414. OPENSSL_free(ssl->psk_identity_hint);
  415. sk_CRYPTO_BUFFER_pop_free(ssl->client_CA, CRYPTO_BUFFER_free);
  416. sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
  417. if (ssl->method != NULL) {
  418. ssl->method->ssl_free(ssl);
  419. }
  420. SSL_CTX_free(ssl->ctx);
  421. OPENSSL_free(ssl);
  422. }
  423. void SSL_set_connect_state(SSL *ssl) {
  424. ssl->server = 0;
  425. ssl->handshake_func = ssl3_connect;
  426. }
  427. void SSL_set_accept_state(SSL *ssl) {
  428. ssl->server = 1;
  429. ssl->handshake_func = ssl3_accept;
  430. }
  431. void SSL_set0_rbio(SSL *ssl, BIO *rbio) {
  432. BIO_free_all(ssl->rbio);
  433. ssl->rbio = rbio;
  434. }
  435. void SSL_set0_wbio(SSL *ssl, BIO *wbio) {
  436. BIO_free_all(ssl->wbio);
  437. ssl->wbio = wbio;
  438. }
  439. void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
  440. /* For historical reasons, this function has many different cases in ownership
  441. * handling. */
  442. /* If nothing has changed, do nothing */
  443. if (rbio == SSL_get_rbio(ssl) && wbio == SSL_get_wbio(ssl)) {
  444. return;
  445. }
  446. /* If the two arguments are equal, one fewer reference is granted than
  447. * taken. */
  448. if (rbio != NULL && rbio == wbio) {
  449. BIO_up_ref(rbio);
  450. }
  451. /* If only the wbio is changed, adopt only one reference. */
  452. if (rbio == SSL_get_rbio(ssl)) {
  453. SSL_set0_wbio(ssl, wbio);
  454. return;
  455. }
  456. /* There is an asymmetry here for historical reasons. If only the rbio is
  457. * changed AND the rbio and wbio were originally different, then we only adopt
  458. * one reference. */
  459. if (wbio == SSL_get_wbio(ssl) && SSL_get_rbio(ssl) != SSL_get_wbio(ssl)) {
  460. SSL_set0_rbio(ssl, rbio);
  461. return;
  462. }
  463. /* Otherwise, adopt both references. */
  464. SSL_set0_rbio(ssl, rbio);
  465. SSL_set0_wbio(ssl, wbio);
  466. }
  467. BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio; }
  468. BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio; }
  469. void ssl_reset_error_state(SSL *ssl) {
  470. /* Functions which use |SSL_get_error| must reset I/O and error state on
  471. * entry. */
  472. ssl->rwstate = SSL_NOTHING;
  473. ERR_clear_error();
  474. ERR_clear_system_error();
  475. }
  476. int SSL_do_handshake(SSL *ssl) {
  477. ssl_reset_error_state(ssl);
  478. if (ssl->handshake_func == NULL) {
  479. OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
  480. return -1;
  481. }
  482. if (!SSL_in_init(ssl)) {
  483. return 1;
  484. }
  485. if (ssl->s3->hs == NULL) {
  486. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  487. return -1;
  488. }
  489. /* Run the handshake. */
  490. assert(ssl->s3->hs != NULL);
  491. int ret = ssl->handshake_func(ssl->s3->hs);
  492. if (ret <= 0) {
  493. return ret;
  494. }
  495. /* Destroy the handshake object if the handshake has completely finished. */
  496. if (!SSL_in_init(ssl)) {
  497. ssl_handshake_free(ssl->s3->hs);
  498. ssl->s3->hs = NULL;
  499. }
  500. return 1;
  501. }
  502. int SSL_connect(SSL *ssl) {
  503. if (ssl->handshake_func == NULL) {
  504. /* Not properly initialized yet */
  505. SSL_set_connect_state(ssl);
  506. }
  507. return SSL_do_handshake(ssl);
  508. }
  509. int SSL_accept(SSL *ssl) {
  510. if (ssl->handshake_func == NULL) {
  511. /* Not properly initialized yet */
  512. SSL_set_accept_state(ssl);
  513. }
  514. return SSL_do_handshake(ssl);
  515. }
  516. int ssl_can_write(const SSL *ssl) {
  517. return !SSL_in_init(ssl) || ssl->s3->hs->can_early_write;
  518. }
  519. int ssl_can_read(const SSL *ssl) {
  520. return !SSL_in_init(ssl) || ssl->s3->hs->can_early_read;
  521. }
  522. static int ssl_do_renegotiate(SSL *ssl) {
  523. /* We do not accept renegotiations as a server or SSL 3.0. SSL 3.0 will be
  524. * removed entirely in the future and requires retaining more data for
  525. * renegotiation_info. */
  526. if (ssl->server || ssl->version == SSL3_VERSION) {
  527. goto no_renegotiation;
  528. }
  529. if (ssl->s3->tmp.message_type != SSL3_MT_HELLO_REQUEST ||
  530. ssl->init_num != 0) {
  531. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  532. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST);
  533. return 0;
  534. }
  535. switch (ssl->renegotiate_mode) {
  536. case ssl_renegotiate_ignore:
  537. /* Ignore the HelloRequest. */
  538. return 1;
  539. case ssl_renegotiate_once:
  540. if (ssl->s3->total_renegotiations != 0) {
  541. goto no_renegotiation;
  542. }
  543. break;
  544. case ssl_renegotiate_never:
  545. goto no_renegotiation;
  546. case ssl_renegotiate_freely:
  547. break;
  548. }
  549. /* Renegotiation is only supported at quiescent points in the application
  550. * protocol, namely in HTTPS, just before reading the HTTP response. Require
  551. * the record-layer be idle and avoid complexities of sending a handshake
  552. * record while an application_data record is being written. */
  553. if (ssl_write_buffer_is_pending(ssl)) {
  554. goto no_renegotiation;
  555. }
  556. /* Begin a new handshake. */
  557. if (ssl->s3->hs != NULL) {
  558. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  559. return 0;
  560. }
  561. ssl->s3->hs = ssl_handshake_new(ssl);
  562. if (ssl->s3->hs == NULL) {
  563. return 0;
  564. }
  565. ssl->s3->total_renegotiations++;
  566. return 1;
  567. no_renegotiation:
  568. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION);
  569. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
  570. return 0;
  571. }
  572. static int ssl_do_post_handshake(SSL *ssl) {
  573. if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
  574. return ssl_do_renegotiate(ssl);
  575. }
  576. return tls13_post_handshake(ssl);
  577. }
  578. static int ssl_read_impl(SSL *ssl, void *buf, int num, int peek) {
  579. ssl_reset_error_state(ssl);
  580. if (ssl->handshake_func == NULL) {
  581. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  582. return -1;
  583. }
  584. for (;;) {
  585. /* Complete the current handshake, if any. False Start will cause
  586. * |SSL_do_handshake| to return mid-handshake, so this may require multiple
  587. * iterations. */
  588. while (!ssl_can_read(ssl)) {
  589. int ret = SSL_do_handshake(ssl);
  590. if (ret < 0) {
  591. return ret;
  592. }
  593. if (ret == 0) {
  594. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  595. return -1;
  596. }
  597. }
  598. int got_handshake;
  599. int ret = ssl->method->read_app_data(ssl, &got_handshake, buf, num, peek);
  600. if (ret > 0 || !got_handshake) {
  601. ssl->s3->key_update_count = 0;
  602. return ret;
  603. }
  604. /* If we received an interrupt in early read (the end_of_early_data alert),
  605. * loop again for the handshake to process it. */
  606. if (SSL_in_init(ssl)) {
  607. continue;
  608. }
  609. /* Handle the post-handshake message and try again. */
  610. if (!ssl_do_post_handshake(ssl)) {
  611. return -1;
  612. }
  613. ssl->method->release_current_message(ssl, 1 /* free buffer */);
  614. }
  615. }
  616. int SSL_read(SSL *ssl, void *buf, int num) {
  617. return ssl_read_impl(ssl, buf, num, 0 /* consume bytes */);
  618. }
  619. int SSL_peek(SSL *ssl, void *buf, int num) {
  620. return ssl_read_impl(ssl, buf, num, 1 /* peek */);
  621. }
  622. int SSL_write(SSL *ssl, const void *buf, int num) {
  623. ssl_reset_error_state(ssl);
  624. if (ssl->handshake_func == NULL) {
  625. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  626. return -1;
  627. }
  628. if (ssl->s3->send_shutdown != ssl_shutdown_none) {
  629. OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  630. return -1;
  631. }
  632. /* If necessary, complete the handshake implicitly. */
  633. if (!ssl_can_write(ssl)) {
  634. int ret = SSL_do_handshake(ssl);
  635. if (ret < 0) {
  636. return ret;
  637. }
  638. if (ret == 0) {
  639. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  640. return -1;
  641. }
  642. }
  643. return ssl->method->write_app_data(ssl, buf, num);
  644. }
  645. int SSL_shutdown(SSL *ssl) {
  646. ssl_reset_error_state(ssl);
  647. if (ssl->handshake_func == NULL) {
  648. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  649. return -1;
  650. }
  651. /* If we are in the middle of a handshake, silently succeed. Consumers often
  652. * call this function before |SSL_free|, whether the handshake succeeded or
  653. * not. We assume the caller has already handled failed handshakes. */
  654. if (SSL_in_init(ssl)) {
  655. return 1;
  656. }
  657. if (ssl->quiet_shutdown) {
  658. /* Do nothing if configured not to send a close_notify. */
  659. ssl->s3->send_shutdown = ssl_shutdown_close_notify;
  660. ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
  661. return 1;
  662. }
  663. /* This function completes in two stages. It sends a close_notify and then it
  664. * waits for a close_notify to come in. Perform exactly one action and return
  665. * whether or not it succeeds. */
  666. if (ssl->s3->send_shutdown != ssl_shutdown_close_notify) {
  667. /* Send a close_notify. */
  668. if (ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY) <= 0) {
  669. return -1;
  670. }
  671. } else if (ssl->s3->alert_dispatch) {
  672. /* Finish sending the close_notify. */
  673. if (ssl->method->dispatch_alert(ssl) <= 0) {
  674. return -1;
  675. }
  676. } else if (ssl->s3->recv_shutdown != ssl_shutdown_close_notify) {
  677. /* Wait for the peer's close_notify. */
  678. ssl->method->read_close_notify(ssl);
  679. if (ssl->s3->recv_shutdown != ssl_shutdown_close_notify) {
  680. return -1;
  681. }
  682. }
  683. /* Return 0 for unidirectional shutdown and 1 for bidirectional shutdown. */
  684. return ssl->s3->recv_shutdown == ssl_shutdown_close_notify;
  685. }
  686. int SSL_send_fatal_alert(SSL *ssl, uint8_t alert) {
  687. if (ssl->s3->alert_dispatch) {
  688. if (ssl->s3->send_alert[0] != SSL3_AL_FATAL ||
  689. ssl->s3->send_alert[1] != alert) {
  690. /* We are already attempting to write a different alert. */
  691. OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  692. return -1;
  693. }
  694. return ssl->method->dispatch_alert(ssl);
  695. }
  696. return ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  697. }
  698. void SSL_CTX_set_early_data_enabled(SSL_CTX *ctx, int enabled) {
  699. ctx->cert->enable_early_data = !!enabled;
  700. }
  701. void SSL_set_early_data_enabled(SSL *ssl, int enabled) {
  702. ssl->cert->enable_early_data = !!enabled;
  703. }
  704. int SSL_early_data_accepted(const SSL *ssl) {
  705. return ssl->early_data_accepted;
  706. }
  707. static int bio_retry_reason_to_error(int reason) {
  708. switch (reason) {
  709. case BIO_RR_CONNECT:
  710. return SSL_ERROR_WANT_CONNECT;
  711. case BIO_RR_ACCEPT:
  712. return SSL_ERROR_WANT_ACCEPT;
  713. default:
  714. return SSL_ERROR_SYSCALL;
  715. }
  716. }
  717. int SSL_get_error(const SSL *ssl, int ret_code) {
  718. if (ret_code > 0) {
  719. return SSL_ERROR_NONE;
  720. }
  721. /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
  722. * where we do encode the error */
  723. uint32_t err = ERR_peek_error();
  724. if (err != 0) {
  725. if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
  726. return SSL_ERROR_SYSCALL;
  727. }
  728. return SSL_ERROR_SSL;
  729. }
  730. if (ret_code == 0) {
  731. if (ssl->s3->recv_shutdown == ssl_shutdown_close_notify) {
  732. return SSL_ERROR_ZERO_RETURN;
  733. }
  734. /* An EOF was observed which violates the protocol, and the underlying
  735. * transport does not participate in the error queue. Bubble up to the
  736. * caller. */
  737. return SSL_ERROR_SYSCALL;
  738. }
  739. switch (ssl->rwstate) {
  740. case SSL_PENDING_SESSION:
  741. return SSL_ERROR_PENDING_SESSION;
  742. case SSL_CERTIFICATE_SELECTION_PENDING:
  743. return SSL_ERROR_PENDING_CERTIFICATE;
  744. case SSL_READING: {
  745. BIO *bio = SSL_get_rbio(ssl);
  746. if (BIO_should_read(bio)) {
  747. return SSL_ERROR_WANT_READ;
  748. }
  749. if (BIO_should_write(bio)) {
  750. /* TODO(davidben): OpenSSL historically checked for writes on the read
  751. * BIO. Can this be removed? */
  752. return SSL_ERROR_WANT_WRITE;
  753. }
  754. if (BIO_should_io_special(bio)) {
  755. return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
  756. }
  757. break;
  758. }
  759. case SSL_WRITING: {
  760. BIO *bio = SSL_get_wbio(ssl);
  761. if (BIO_should_write(bio)) {
  762. return SSL_ERROR_WANT_WRITE;
  763. }
  764. if (BIO_should_read(bio)) {
  765. /* TODO(davidben): OpenSSL historically checked for reads on the write
  766. * BIO. Can this be removed? */
  767. return SSL_ERROR_WANT_READ;
  768. }
  769. if (BIO_should_io_special(bio)) {
  770. return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
  771. }
  772. break;
  773. }
  774. case SSL_X509_LOOKUP:
  775. return SSL_ERROR_WANT_X509_LOOKUP;
  776. case SSL_CHANNEL_ID_LOOKUP:
  777. return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
  778. case SSL_PRIVATE_KEY_OPERATION:
  779. return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
  780. case SSL_PENDING_TICKET:
  781. return SSL_ERROR_PENDING_TICKET;
  782. }
  783. return SSL_ERROR_SYSCALL;
  784. }
  785. static int set_min_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
  786. uint16_t version) {
  787. /* Zero is interpreted as the default minimum version. */
  788. if (version == 0) {
  789. *out = method->min_version;
  790. /* SSL 3.0 is disabled unless explicitly enabled. */
  791. if (*out < TLS1_VERSION) {
  792. *out = TLS1_VERSION;
  793. }
  794. return 1;
  795. }
  796. if (version == TLS1_3_VERSION) {
  797. version = TLS1_3_DRAFT_VERSION;
  798. }
  799. return method->version_from_wire(out, version);
  800. }
  801. static int set_max_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
  802. uint16_t version) {
  803. /* Zero is interpreted as the default maximum version. */
  804. if (version == 0) {
  805. *out = method->max_version;
  806. /* TODO(svaldez): Enable TLS 1.3 by default once fully implemented. */
  807. if (*out > TLS1_2_VERSION) {
  808. *out = TLS1_2_VERSION;
  809. }
  810. return 1;
  811. }
  812. if (version == TLS1_3_VERSION) {
  813. version = TLS1_3_DRAFT_VERSION;
  814. }
  815. return method->version_from_wire(out, version);
  816. }
  817. int SSL_CTX_set_min_proto_version(SSL_CTX *ctx, uint16_t version) {
  818. return set_min_version(ctx->method, &ctx->min_version, version);
  819. }
  820. int SSL_CTX_set_max_proto_version(SSL_CTX *ctx, uint16_t version) {
  821. return set_max_version(ctx->method, &ctx->max_version, version);
  822. }
  823. int SSL_set_min_proto_version(SSL *ssl, uint16_t version) {
  824. return set_min_version(ssl->method, &ssl->min_version, version);
  825. }
  826. int SSL_set_max_proto_version(SSL *ssl, uint16_t version) {
  827. return set_max_version(ssl->method, &ssl->max_version, version);
  828. }
  829. uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
  830. ctx->options |= options;
  831. return ctx->options;
  832. }
  833. uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
  834. ctx->options &= ~options;
  835. return ctx->options;
  836. }
  837. uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
  838. uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
  839. ssl->options |= options;
  840. return ssl->options;
  841. }
  842. uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
  843. ssl->options &= ~options;
  844. return ssl->options;
  845. }
  846. uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
  847. uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
  848. ctx->mode |= mode;
  849. return ctx->mode;
  850. }
  851. uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
  852. ctx->mode &= ~mode;
  853. return ctx->mode;
  854. }
  855. uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
  856. uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
  857. ssl->mode |= mode;
  858. return ssl->mode;
  859. }
  860. uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
  861. ssl->mode &= ~mode;
  862. return ssl->mode;
  863. }
  864. uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
  865. void SSL_CTX_set0_buffer_pool(SSL_CTX *ctx, CRYPTO_BUFFER_POOL *pool) {
  866. ctx->pool = pool;
  867. }
  868. int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
  869. size_t max_out) {
  870. /* tls-unique is not defined for SSL 3.0 or TLS 1.3. */
  871. if (!ssl->s3->initial_handshake_complete ||
  872. ssl3_protocol_version(ssl) < TLS1_VERSION ||
  873. ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  874. goto err;
  875. }
  876. /* The tls-unique value is the first Finished message in the handshake, which
  877. * is the client's in a full handshake and the server's for a resumption. See
  878. * https://tools.ietf.org/html/rfc5929#section-3.1. */
  879. const uint8_t *finished = ssl->s3->previous_client_finished;
  880. size_t finished_len = ssl->s3->previous_client_finished_len;
  881. if (ssl->session != NULL) {
  882. /* tls-unique is broken for resumed sessions unless EMS is used. */
  883. if (!ssl->session->extended_master_secret) {
  884. goto err;
  885. }
  886. finished = ssl->s3->previous_server_finished;
  887. finished_len = ssl->s3->previous_server_finished_len;
  888. }
  889. *out_len = finished_len;
  890. if (finished_len > max_out) {
  891. *out_len = max_out;
  892. }
  893. OPENSSL_memcpy(out, finished, *out_len);
  894. return 1;
  895. err:
  896. *out_len = 0;
  897. OPENSSL_memset(out, 0, max_out);
  898. return 0;
  899. }
  900. static int set_session_id_context(CERT *cert, const uint8_t *sid_ctx,
  901. size_t sid_ctx_len) {
  902. if (sid_ctx_len > sizeof(cert->sid_ctx)) {
  903. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  904. return 0;
  905. }
  906. OPENSSL_COMPILE_ASSERT(sizeof(cert->sid_ctx) < 256, sid_ctx_too_large);
  907. cert->sid_ctx_length = (uint8_t)sid_ctx_len;
  908. OPENSSL_memcpy(cert->sid_ctx, sid_ctx, sid_ctx_len);
  909. return 1;
  910. }
  911. int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
  912. size_t sid_ctx_len) {
  913. return set_session_id_context(ctx->cert, sid_ctx, sid_ctx_len);
  914. }
  915. int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
  916. size_t sid_ctx_len) {
  917. return set_session_id_context(ssl->cert, sid_ctx, sid_ctx_len);
  918. }
  919. const uint8_t *SSL_get0_session_id_context(const SSL *ssl, size_t *out_len) {
  920. *out_len = ssl->cert->sid_ctx_length;
  921. return ssl->cert->sid_ctx;
  922. }
  923. void ssl_cipher_preference_list_free(
  924. struct ssl_cipher_preference_list_st *cipher_list) {
  925. if (cipher_list == NULL) {
  926. return;
  927. }
  928. sk_SSL_CIPHER_free(cipher_list->ciphers);
  929. OPENSSL_free(cipher_list->in_group_flags);
  930. OPENSSL_free(cipher_list);
  931. }
  932. void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); }
  933. int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); }
  934. int SSL_get_rfd(const SSL *ssl) {
  935. int ret = -1;
  936. BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR);
  937. if (b != NULL) {
  938. BIO_get_fd(b, &ret);
  939. }
  940. return ret;
  941. }
  942. int SSL_get_wfd(const SSL *ssl) {
  943. int ret = -1;
  944. BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR);
  945. if (b != NULL) {
  946. BIO_get_fd(b, &ret);
  947. }
  948. return ret;
  949. }
  950. int SSL_set_fd(SSL *ssl, int fd) {
  951. BIO *bio = BIO_new(BIO_s_socket());
  952. if (bio == NULL) {
  953. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  954. return 0;
  955. }
  956. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  957. SSL_set_bio(ssl, bio, bio);
  958. return 1;
  959. }
  960. int SSL_set_wfd(SSL *ssl, int fd) {
  961. BIO *rbio = SSL_get_rbio(ssl);
  962. if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET ||
  963. BIO_get_fd(rbio, NULL) != fd) {
  964. BIO *bio = BIO_new(BIO_s_socket());
  965. if (bio == NULL) {
  966. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  967. return 0;
  968. }
  969. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  970. SSL_set0_wbio(ssl, bio);
  971. } else {
  972. /* Copy the rbio over to the wbio. */
  973. BIO_up_ref(rbio);
  974. SSL_set0_wbio(ssl, rbio);
  975. }
  976. return 1;
  977. }
  978. int SSL_set_rfd(SSL *ssl, int fd) {
  979. BIO *wbio = SSL_get_wbio(ssl);
  980. if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET ||
  981. BIO_get_fd(wbio, NULL) != fd) {
  982. BIO *bio = BIO_new(BIO_s_socket());
  983. if (bio == NULL) {
  984. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  985. return 0;
  986. }
  987. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  988. SSL_set0_rbio(ssl, bio);
  989. } else {
  990. /* Copy the wbio over to the rbio. */
  991. BIO_up_ref(wbio);
  992. SSL_set0_rbio(ssl, wbio);
  993. }
  994. return 1;
  995. }
  996. static size_t copy_finished(void *out, size_t out_len, const uint8_t *in,
  997. size_t in_len) {
  998. if (out_len > in_len) {
  999. out_len = in_len;
  1000. }
  1001. OPENSSL_memcpy(out, in, out_len);
  1002. return in_len;
  1003. }
  1004. size_t SSL_get_finished(const SSL *ssl, void *buf, size_t count) {
  1005. if (!ssl->s3->initial_handshake_complete ||
  1006. ssl3_protocol_version(ssl) < TLS1_VERSION ||
  1007. ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  1008. return 0;
  1009. }
  1010. if (ssl->server) {
  1011. return copy_finished(buf, count, ssl->s3->previous_server_finished,
  1012. ssl->s3->previous_server_finished_len);
  1013. }
  1014. return copy_finished(buf, count, ssl->s3->previous_client_finished,
  1015. ssl->s3->previous_client_finished_len);
  1016. }
  1017. size_t SSL_get_peer_finished(const SSL *ssl, void *buf, size_t count) {
  1018. if (!ssl->s3->initial_handshake_complete ||
  1019. ssl3_protocol_version(ssl) < TLS1_VERSION ||
  1020. ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  1021. return 0;
  1022. }
  1023. if (ssl->server) {
  1024. return copy_finished(buf, count, ssl->s3->previous_client_finished,
  1025. ssl->s3->previous_client_finished_len);
  1026. }
  1027. return copy_finished(buf, count, ssl->s3->previous_server_finished,
  1028. ssl->s3->previous_server_finished_len);
  1029. }
  1030. int SSL_get_verify_mode(const SSL *ssl) { return ssl->verify_mode; }
  1031. int SSL_get_extms_support(const SSL *ssl) {
  1032. /* TLS 1.3 does not require extended master secret and always reports as
  1033. * supporting it. */
  1034. if (!ssl->s3->have_version) {
  1035. return 0;
  1036. }
  1037. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  1038. return 1;
  1039. }
  1040. /* If the initial handshake completed, query the established session. */
  1041. if (ssl->s3->established_session != NULL) {
  1042. return ssl->s3->established_session->extended_master_secret;
  1043. }
  1044. /* Otherwise, query the in-progress handshake. */
  1045. if (ssl->s3->hs != NULL) {
  1046. return ssl->s3->hs->extended_master_secret;
  1047. }
  1048. assert(0);
  1049. return 0;
  1050. }
  1051. int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
  1052. int SSL_get_read_ahead(const SSL *ssl) { return 0; }
  1053. void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
  1054. void SSL_set_read_ahead(SSL *ssl, int yes) { }
  1055. int SSL_pending(const SSL *ssl) {
  1056. if (ssl->s3->rrec.type != SSL3_RT_APPLICATION_DATA) {
  1057. return 0;
  1058. }
  1059. return ssl->s3->rrec.length;
  1060. }
  1061. /* Fix this so it checks all the valid key/cert options */
  1062. int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
  1063. return ssl_cert_check_private_key(ctx->cert, ctx->cert->privatekey);
  1064. }
  1065. /* Fix this function so that it takes an optional type parameter */
  1066. int SSL_check_private_key(const SSL *ssl) {
  1067. return ssl_cert_check_private_key(ssl->cert, ssl->cert->privatekey);
  1068. }
  1069. long SSL_get_default_timeout(const SSL *ssl) {
  1070. return SSL_DEFAULT_SESSION_TIMEOUT;
  1071. }
  1072. int SSL_renegotiate(SSL *ssl) {
  1073. /* Caller-initiated renegotiation is not supported. */
  1074. OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1075. return 0;
  1076. }
  1077. int SSL_renegotiate_pending(SSL *ssl) {
  1078. return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
  1079. }
  1080. int SSL_total_renegotiations(const SSL *ssl) {
  1081. return ssl->s3->total_renegotiations;
  1082. }
  1083. size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
  1084. return ctx->max_cert_list;
  1085. }
  1086. void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
  1087. if (max_cert_list > kMaxHandshakeSize) {
  1088. max_cert_list = kMaxHandshakeSize;
  1089. }
  1090. ctx->max_cert_list = (uint32_t)max_cert_list;
  1091. }
  1092. size_t SSL_get_max_cert_list(const SSL *ssl) {
  1093. return ssl->max_cert_list;
  1094. }
  1095. void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
  1096. if (max_cert_list > kMaxHandshakeSize) {
  1097. max_cert_list = kMaxHandshakeSize;
  1098. }
  1099. ssl->max_cert_list = (uint32_t)max_cert_list;
  1100. }
  1101. int SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
  1102. if (max_send_fragment < 512) {
  1103. max_send_fragment = 512;
  1104. }
  1105. if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
  1106. max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  1107. }
  1108. ctx->max_send_fragment = (uint16_t)max_send_fragment;
  1109. return 1;
  1110. }
  1111. int SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
  1112. if (max_send_fragment < 512) {
  1113. max_send_fragment = 512;
  1114. }
  1115. if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
  1116. max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  1117. }
  1118. ssl->max_send_fragment = (uint16_t)max_send_fragment;
  1119. return 1;
  1120. }
  1121. int SSL_set_mtu(SSL *ssl, unsigned mtu) {
  1122. if (!SSL_is_dtls(ssl) || mtu < dtls1_min_mtu()) {
  1123. return 0;
  1124. }
  1125. ssl->d1->mtu = mtu;
  1126. return 1;
  1127. }
  1128. int SSL_get_secure_renegotiation_support(const SSL *ssl) {
  1129. if (!ssl->s3->have_version) {
  1130. return 0;
  1131. }
  1132. return ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
  1133. ssl->s3->send_connection_binding;
  1134. }
  1135. LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
  1136. size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
  1137. return lh_SSL_SESSION_num_items(ctx->sessions);
  1138. }
  1139. unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
  1140. unsigned long ret = ctx->session_cache_size;
  1141. ctx->session_cache_size = size;
  1142. return ret;
  1143. }
  1144. unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
  1145. return ctx->session_cache_size;
  1146. }
  1147. int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
  1148. int ret = ctx->session_cache_mode;
  1149. ctx->session_cache_mode = mode;
  1150. return ret;
  1151. }
  1152. int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
  1153. return ctx->session_cache_mode;
  1154. }
  1155. int SSL_CTX_get_tlsext_ticket_keys(SSL_CTX *ctx, void *out, size_t len) {
  1156. if (out == NULL) {
  1157. return 48;
  1158. }
  1159. if (len != 48) {
  1160. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
  1161. return 0;
  1162. }
  1163. uint8_t *out_bytes = out;
  1164. OPENSSL_memcpy(out_bytes, ctx->tlsext_tick_key_name, 16);
  1165. OPENSSL_memcpy(out_bytes + 16, ctx->tlsext_tick_hmac_key, 16);
  1166. OPENSSL_memcpy(out_bytes + 32, ctx->tlsext_tick_aes_key, 16);
  1167. return 1;
  1168. }
  1169. int SSL_CTX_set_tlsext_ticket_keys(SSL_CTX *ctx, const void *in, size_t len) {
  1170. if (in == NULL) {
  1171. return 48;
  1172. }
  1173. if (len != 48) {
  1174. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
  1175. return 0;
  1176. }
  1177. const uint8_t *in_bytes = in;
  1178. OPENSSL_memcpy(ctx->tlsext_tick_key_name, in_bytes, 16);
  1179. OPENSSL_memcpy(ctx->tlsext_tick_hmac_key, in_bytes + 16, 16);
  1180. OPENSSL_memcpy(ctx->tlsext_tick_aes_key, in_bytes + 32, 16);
  1181. return 1;
  1182. }
  1183. int SSL_CTX_set_tlsext_ticket_key_cb(
  1184. SSL_CTX *ctx, int (*callback)(SSL *ssl, uint8_t *key_name, uint8_t *iv,
  1185. EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
  1186. int encrypt)) {
  1187. ctx->tlsext_ticket_key_cb = callback;
  1188. return 1;
  1189. }
  1190. int SSL_CTX_set1_curves(SSL_CTX *ctx, const int *curves, size_t curves_len) {
  1191. return tls1_set_curves(&ctx->supported_group_list,
  1192. &ctx->supported_group_list_len, curves,
  1193. curves_len);
  1194. }
  1195. int SSL_set1_curves(SSL *ssl, const int *curves, size_t curves_len) {
  1196. return tls1_set_curves(&ssl->supported_group_list,
  1197. &ssl->supported_group_list_len, curves,
  1198. curves_len);
  1199. }
  1200. int SSL_CTX_set1_curves_list(SSL_CTX *ctx, const char *curves) {
  1201. return tls1_set_curves_list(&ctx->supported_group_list,
  1202. &ctx->supported_group_list_len, curves);
  1203. }
  1204. int SSL_set1_curves_list(SSL *ssl, const char *curves) {
  1205. return tls1_set_curves_list(&ssl->supported_group_list,
  1206. &ssl->supported_group_list_len, curves);
  1207. }
  1208. uint16_t SSL_get_curve_id(const SSL *ssl) {
  1209. /* TODO(davidben): This checks the wrong session if there is a renegotiation in
  1210. * progress. */
  1211. SSL_SESSION *session = SSL_get_session(ssl);
  1212. if (session == NULL) {
  1213. return 0;
  1214. }
  1215. return session->group_id;
  1216. }
  1217. int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) {
  1218. return 1;
  1219. }
  1220. int SSL_set_tmp_dh(SSL *ssl, const DH *dh) {
  1221. return 1;
  1222. }
  1223. OPENSSL_EXPORT STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx) {
  1224. return ctx->cipher_list->ciphers;
  1225. }
  1226. STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) {
  1227. if (ssl == NULL) {
  1228. return NULL;
  1229. }
  1230. const struct ssl_cipher_preference_list_st *prefs =
  1231. ssl_get_cipher_preferences(ssl);
  1232. if (prefs == NULL) {
  1233. return NULL;
  1234. }
  1235. return prefs->ciphers;
  1236. }
  1237. const char *SSL_get_cipher_list(const SSL *ssl, int n) {
  1238. if (ssl == NULL) {
  1239. return NULL;
  1240. }
  1241. STACK_OF(SSL_CIPHER) *sk = SSL_get_ciphers(ssl);
  1242. if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
  1243. return NULL;
  1244. }
  1245. const SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, n);
  1246. if (c == NULL) {
  1247. return NULL;
  1248. }
  1249. return c->name;
  1250. }
  1251. int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
  1252. return ssl_create_cipher_list(ctx->method, &ctx->cipher_list, str,
  1253. 0 /* not strict */);
  1254. }
  1255. int SSL_CTX_set_strict_cipher_list(SSL_CTX *ctx, const char *str) {
  1256. return ssl_create_cipher_list(ctx->method, &ctx->cipher_list, str,
  1257. 1 /* strict */);
  1258. }
  1259. int SSL_set_cipher_list(SSL *ssl, const char *str) {
  1260. return ssl_create_cipher_list(ssl->ctx->method, &ssl->cipher_list, str,
  1261. 0 /* not strict */);
  1262. }
  1263. int SSL_set_strict_cipher_list(SSL *ssl, const char *str) {
  1264. return ssl_create_cipher_list(ssl->ctx->method, &ssl->cipher_list, str,
  1265. 1 /* strict */);
  1266. }
  1267. const char *SSL_get_servername(const SSL *ssl, const int type) {
  1268. if (type != TLSEXT_NAMETYPE_host_name) {
  1269. return NULL;
  1270. }
  1271. /* Historically, |SSL_get_servername| was also the configuration getter
  1272. * corresponding to |SSL_set_tlsext_host_name|. */
  1273. if (ssl->tlsext_hostname != NULL) {
  1274. return ssl->tlsext_hostname;
  1275. }
  1276. /* During the handshake, report the handshake value. */
  1277. if (ssl->s3->hs != NULL) {
  1278. return ssl->s3->hs->hostname;
  1279. }
  1280. /* SSL_get_servername may also be called after the handshake to look up the
  1281. * SNI value.
  1282. *
  1283. * TODO(davidben): This is almost unused. Can we remove it? */
  1284. SSL_SESSION *session = SSL_get_session(ssl);
  1285. if (session == NULL) {
  1286. return NULL;
  1287. }
  1288. return session->tlsext_hostname;
  1289. }
  1290. int SSL_get_servername_type(const SSL *ssl) {
  1291. SSL_SESSION *session = SSL_get_session(ssl);
  1292. if (session == NULL || session->tlsext_hostname == NULL) {
  1293. return -1;
  1294. }
  1295. return TLSEXT_NAMETYPE_host_name;
  1296. }
  1297. void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
  1298. ctx->signed_cert_timestamps_enabled = 1;
  1299. }
  1300. void SSL_CTX_i_promise_to_verify_certs_after_the_handshake(SSL_CTX *ctx) {
  1301. ctx->i_promise_to_verify_certs_after_the_handshake = 1;
  1302. }
  1303. void SSL_enable_signed_cert_timestamps(SSL *ssl) {
  1304. ssl->signed_cert_timestamps_enabled = 1;
  1305. }
  1306. void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
  1307. ctx->ocsp_stapling_enabled = 1;
  1308. }
  1309. void SSL_enable_ocsp_stapling(SSL *ssl) {
  1310. ssl->ocsp_stapling_enabled = 1;
  1311. }
  1312. void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
  1313. size_t *out_len) {
  1314. SSL_SESSION *session = SSL_get_session(ssl);
  1315. *out_len = 0;
  1316. *out = NULL;
  1317. if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
  1318. return;
  1319. }
  1320. *out = session->tlsext_signed_cert_timestamp_list;
  1321. *out_len = session->tlsext_signed_cert_timestamp_list_length;
  1322. }
  1323. void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
  1324. size_t *out_len) {
  1325. SSL_SESSION *session = SSL_get_session(ssl);
  1326. *out_len = 0;
  1327. *out = NULL;
  1328. if (ssl->server || !session || !session->ocsp_response) {
  1329. return;
  1330. }
  1331. *out = session->ocsp_response;
  1332. *out_len = session->ocsp_response_length;
  1333. }
  1334. int SSL_set_tlsext_host_name(SSL *ssl, const char *name) {
  1335. OPENSSL_free(ssl->tlsext_hostname);
  1336. ssl->tlsext_hostname = NULL;
  1337. if (name == NULL) {
  1338. return 1;
  1339. }
  1340. size_t len = strlen(name);
  1341. if (len == 0 || len > TLSEXT_MAXLEN_host_name) {
  1342. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL3_EXT_INVALID_SERVERNAME);
  1343. return 0;
  1344. }
  1345. ssl->tlsext_hostname = BUF_strdup(name);
  1346. if (ssl->tlsext_hostname == NULL) {
  1347. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1348. return 0;
  1349. }
  1350. return 1;
  1351. }
  1352. int SSL_CTX_set_tlsext_servername_callback(
  1353. SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) {
  1354. ctx->tlsext_servername_callback = callback;
  1355. return 1;
  1356. }
  1357. int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) {
  1358. ctx->tlsext_servername_arg = arg;
  1359. return 1;
  1360. }
  1361. int SSL_select_next_proto(uint8_t **out, uint8_t *out_len,
  1362. const uint8_t *server, unsigned server_len,
  1363. const uint8_t *client, unsigned client_len) {
  1364. unsigned int i, j;
  1365. const uint8_t *result;
  1366. int status = OPENSSL_NPN_UNSUPPORTED;
  1367. /* For each protocol in server preference order, see if we support it. */
  1368. for (i = 0; i < server_len;) {
  1369. for (j = 0; j < client_len;) {
  1370. if (server[i] == client[j] &&
  1371. OPENSSL_memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
  1372. /* We found a match */
  1373. result = &server[i];
  1374. status = OPENSSL_NPN_NEGOTIATED;
  1375. goto found;
  1376. }
  1377. j += client[j];
  1378. j++;
  1379. }
  1380. i += server[i];
  1381. i++;
  1382. }
  1383. /* There's no overlap between our protocols and the server's list. */
  1384. result = client;
  1385. status = OPENSSL_NPN_NO_OVERLAP;
  1386. found:
  1387. *out = (uint8_t *)result + 1;
  1388. *out_len = result[0];
  1389. return status;
  1390. }
  1391. void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data,
  1392. unsigned *out_len) {
  1393. *out_data = ssl->s3->next_proto_negotiated;
  1394. if (*out_data == NULL) {
  1395. *out_len = 0;
  1396. } else {
  1397. *out_len = ssl->s3->next_proto_negotiated_len;
  1398. }
  1399. }
  1400. void SSL_CTX_set_next_protos_advertised_cb(
  1401. SSL_CTX *ctx,
  1402. int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg),
  1403. void *arg) {
  1404. ctx->next_protos_advertised_cb = cb;
  1405. ctx->next_protos_advertised_cb_arg = arg;
  1406. }
  1407. void SSL_CTX_set_next_proto_select_cb(
  1408. SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
  1409. const uint8_t *in, unsigned in_len, void *arg),
  1410. void *arg) {
  1411. ctx->next_proto_select_cb = cb;
  1412. ctx->next_proto_select_cb_arg = arg;
  1413. }
  1414. int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
  1415. unsigned protos_len) {
  1416. OPENSSL_free(ctx->alpn_client_proto_list);
  1417. ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
  1418. if (!ctx->alpn_client_proto_list) {
  1419. return 1;
  1420. }
  1421. ctx->alpn_client_proto_list_len = protos_len;
  1422. return 0;
  1423. }
  1424. int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
  1425. OPENSSL_free(ssl->alpn_client_proto_list);
  1426. ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
  1427. if (!ssl->alpn_client_proto_list) {
  1428. return 1;
  1429. }
  1430. ssl->alpn_client_proto_list_len = protos_len;
  1431. return 0;
  1432. }
  1433. void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
  1434. int (*cb)(SSL *ssl, const uint8_t **out,
  1435. uint8_t *out_len, const uint8_t *in,
  1436. unsigned in_len, void *arg),
  1437. void *arg) {
  1438. ctx->alpn_select_cb = cb;
  1439. ctx->alpn_select_cb_arg = arg;
  1440. }
  1441. void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data,
  1442. unsigned *out_len) {
  1443. *out_data = NULL;
  1444. if (ssl->s3) {
  1445. *out_data = ssl->s3->alpn_selected;
  1446. }
  1447. if (*out_data == NULL) {
  1448. *out_len = 0;
  1449. } else {
  1450. *out_len = ssl->s3->alpn_selected_len;
  1451. }
  1452. }
  1453. void SSL_CTX_set_allow_unknown_alpn_protos(SSL_CTX *ctx, int enabled) {
  1454. ctx->allow_unknown_alpn_protos = !!enabled;
  1455. }
  1456. void SSL_CTX_set_tls_channel_id_enabled(SSL_CTX *ctx, int enabled) {
  1457. ctx->tlsext_channel_id_enabled = !!enabled;
  1458. }
  1459. int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) {
  1460. SSL_CTX_set_tls_channel_id_enabled(ctx, 1);
  1461. return 1;
  1462. }
  1463. void SSL_set_tls_channel_id_enabled(SSL *ssl, int enabled) {
  1464. ssl->tlsext_channel_id_enabled = !!enabled;
  1465. }
  1466. int SSL_enable_tls_channel_id(SSL *ssl) {
  1467. SSL_set_tls_channel_id_enabled(ssl, 1);
  1468. return 1;
  1469. }
  1470. static int is_p256_key(EVP_PKEY *private_key) {
  1471. const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(private_key);
  1472. return ec_key != NULL &&
  1473. EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)) ==
  1474. NID_X9_62_prime256v1;
  1475. }
  1476. int SSL_CTX_set1_tls_channel_id(SSL_CTX *ctx, EVP_PKEY *private_key) {
  1477. if (!is_p256_key(private_key)) {
  1478. OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
  1479. return 0;
  1480. }
  1481. EVP_PKEY_free(ctx->tlsext_channel_id_private);
  1482. EVP_PKEY_up_ref(private_key);
  1483. ctx->tlsext_channel_id_private = private_key;
  1484. ctx->tlsext_channel_id_enabled = 1;
  1485. return 1;
  1486. }
  1487. int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) {
  1488. if (!is_p256_key(private_key)) {
  1489. OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
  1490. return 0;
  1491. }
  1492. EVP_PKEY_free(ssl->tlsext_channel_id_private);
  1493. EVP_PKEY_up_ref(private_key);
  1494. ssl->tlsext_channel_id_private = private_key;
  1495. ssl->tlsext_channel_id_enabled = 1;
  1496. return 1;
  1497. }
  1498. size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) {
  1499. if (!ssl->s3->tlsext_channel_id_valid) {
  1500. return 0;
  1501. }
  1502. OPENSSL_memcpy(out, ssl->s3->tlsext_channel_id,
  1503. (max_out < 64) ? max_out : 64);
  1504. return 64;
  1505. }
  1506. size_t SSL_get0_certificate_types(SSL *ssl, const uint8_t **out_types) {
  1507. if (ssl->server || ssl->s3->hs == NULL) {
  1508. *out_types = NULL;
  1509. return 0;
  1510. }
  1511. *out_types = ssl->s3->hs->certificate_types;
  1512. return ssl->s3->hs->num_certificate_types;
  1513. }
  1514. void ssl_update_cache(SSL_HANDSHAKE *hs, int mode) {
  1515. SSL *const ssl = hs->ssl;
  1516. SSL_CTX *ctx = ssl->session_ctx;
  1517. /* Never cache sessions with empty session IDs. */
  1518. if (ssl->s3->established_session->session_id_length == 0 ||
  1519. (ctx->session_cache_mode & mode) != mode) {
  1520. return;
  1521. }
  1522. /* Clients never use the internal session cache. */
  1523. int use_internal_cache = ssl->server && !(ctx->session_cache_mode &
  1524. SSL_SESS_CACHE_NO_INTERNAL_STORE);
  1525. /* A client may see new sessions on abbreviated handshakes if the server
  1526. * decides to renew the ticket. Once the handshake is completed, it should be
  1527. * inserted into the cache. */
  1528. if (ssl->s3->established_session != ssl->session ||
  1529. (!ssl->server && hs->ticket_expected)) {
  1530. if (use_internal_cache) {
  1531. SSL_CTX_add_session(ctx, ssl->s3->established_session);
  1532. }
  1533. if (ctx->new_session_cb != NULL) {
  1534. SSL_SESSION_up_ref(ssl->s3->established_session);
  1535. if (!ctx->new_session_cb(ssl, ssl->s3->established_session)) {
  1536. /* |new_session_cb|'s return value signals whether it took ownership. */
  1537. SSL_SESSION_free(ssl->s3->established_session);
  1538. }
  1539. }
  1540. }
  1541. if (use_internal_cache &&
  1542. !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR)) {
  1543. /* Automatically flush the internal session cache every 255 connections. */
  1544. int flush_cache = 0;
  1545. CRYPTO_MUTEX_lock_write(&ctx->lock);
  1546. ctx->handshakes_since_cache_flush++;
  1547. if (ctx->handshakes_since_cache_flush >= 255) {
  1548. flush_cache = 1;
  1549. ctx->handshakes_since_cache_flush = 0;
  1550. }
  1551. CRYPTO_MUTEX_unlock_write(&ctx->lock);
  1552. if (flush_cache) {
  1553. struct OPENSSL_timeval now;
  1554. ssl_get_current_time(ssl, &now);
  1555. SSL_CTX_flush_sessions(ctx, now.tv_sec);
  1556. }
  1557. }
  1558. }
  1559. static const char *ssl_get_version(int version) {
  1560. switch (version) {
  1561. /* Report TLS 1.3 draft version as TLS 1.3 in the public API. */
  1562. case TLS1_3_DRAFT_VERSION:
  1563. return "TLSv1.3";
  1564. case TLS1_2_VERSION:
  1565. return "TLSv1.2";
  1566. case TLS1_1_VERSION:
  1567. return "TLSv1.1";
  1568. case TLS1_VERSION:
  1569. return "TLSv1";
  1570. case SSL3_VERSION:
  1571. return "SSLv3";
  1572. case DTLS1_VERSION:
  1573. return "DTLSv1";
  1574. case DTLS1_2_VERSION:
  1575. return "DTLSv1.2";
  1576. default:
  1577. return "unknown";
  1578. }
  1579. }
  1580. const char *SSL_get_version(const SSL *ssl) {
  1581. return ssl_get_version(ssl->version);
  1582. }
  1583. const char *SSL_SESSION_get_version(const SSL_SESSION *session) {
  1584. return ssl_get_version(session->ssl_version);
  1585. }
  1586. EVP_PKEY *SSL_get_privatekey(const SSL *ssl) {
  1587. if (ssl->cert != NULL) {
  1588. return ssl->cert->privatekey;
  1589. }
  1590. return NULL;
  1591. }
  1592. EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
  1593. if (ctx->cert != NULL) {
  1594. return ctx->cert->privatekey;
  1595. }
  1596. return NULL;
  1597. }
  1598. const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) {
  1599. if (ssl->s3->aead_write_ctx == NULL) {
  1600. return NULL;
  1601. }
  1602. return ssl->s3->aead_write_ctx->cipher;
  1603. }
  1604. int SSL_session_reused(const SSL *ssl) {
  1605. return ssl->s3->session_reused;
  1606. }
  1607. const COMP_METHOD *SSL_get_current_compression(SSL *ssl) { return NULL; }
  1608. const COMP_METHOD *SSL_get_current_expansion(SSL *ssl) { return NULL; }
  1609. int *SSL_get_server_tmp_key(SSL *ssl, EVP_PKEY **out_key) { return 0; }
  1610. void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
  1611. ctx->quiet_shutdown = (mode != 0);
  1612. }
  1613. int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
  1614. return ctx->quiet_shutdown;
  1615. }
  1616. void SSL_set_quiet_shutdown(SSL *ssl, int mode) {
  1617. ssl->quiet_shutdown = (mode != 0);
  1618. }
  1619. int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; }
  1620. void SSL_set_shutdown(SSL *ssl, int mode) {
  1621. /* It is an error to clear any bits that have already been set. (We can't try
  1622. * to get a second close_notify or send two.) */
  1623. assert((SSL_get_shutdown(ssl) & mode) == SSL_get_shutdown(ssl));
  1624. if (mode & SSL_RECEIVED_SHUTDOWN &&
  1625. ssl->s3->recv_shutdown == ssl_shutdown_none) {
  1626. ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
  1627. }
  1628. if (mode & SSL_SENT_SHUTDOWN &&
  1629. ssl->s3->send_shutdown == ssl_shutdown_none) {
  1630. ssl->s3->send_shutdown = ssl_shutdown_close_notify;
  1631. }
  1632. }
  1633. int SSL_get_shutdown(const SSL *ssl) {
  1634. int ret = 0;
  1635. if (ssl->s3->recv_shutdown != ssl_shutdown_none) {
  1636. /* Historically, OpenSSL set |SSL_RECEIVED_SHUTDOWN| on both close_notify
  1637. * and fatal alert. */
  1638. ret |= SSL_RECEIVED_SHUTDOWN;
  1639. }
  1640. if (ssl->s3->send_shutdown == ssl_shutdown_close_notify) {
  1641. /* Historically, OpenSSL set |SSL_SENT_SHUTDOWN| on only close_notify. */
  1642. ret |= SSL_SENT_SHUTDOWN;
  1643. }
  1644. return ret;
  1645. }
  1646. int SSL_version(const SSL *ssl) {
  1647. /* Report TLS 1.3 draft version as TLS 1.3 in the public API. */
  1648. if (ssl->version == TLS1_3_DRAFT_VERSION) {
  1649. return TLS1_3_VERSION;
  1650. }
  1651. return ssl->version;
  1652. }
  1653. SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
  1654. SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
  1655. if (ssl->ctx == ctx) {
  1656. return ssl->ctx;
  1657. }
  1658. /* One cannot change the X.509 callbacks during a connection. */
  1659. if (ssl->ctx->x509_method != ctx->x509_method) {
  1660. assert(0);
  1661. return NULL;
  1662. }
  1663. if (ctx == NULL) {
  1664. ctx = ssl->session_ctx;
  1665. }
  1666. ssl_cert_free(ssl->cert);
  1667. ssl->cert = ssl_cert_dup(ctx->cert);
  1668. SSL_CTX_up_ref(ctx);
  1669. SSL_CTX_free(ssl->ctx);
  1670. ssl->ctx = ctx;
  1671. return ssl->ctx;
  1672. }
  1673. void SSL_set_info_callback(SSL *ssl,
  1674. void (*cb)(const SSL *ssl, int type, int value)) {
  1675. ssl->info_callback = cb;
  1676. }
  1677. void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type,
  1678. int value) {
  1679. return ssl->info_callback;
  1680. }
  1681. int SSL_state(const SSL *ssl) {
  1682. return SSL_in_init(ssl) ? SSL_ST_INIT : SSL_ST_OK;
  1683. }
  1684. void SSL_set_state(SSL *ssl, int state) { }
  1685. char *SSL_get_shared_ciphers(const SSL *ssl, char *buf, int len) {
  1686. if (len <= 0) {
  1687. return NULL;
  1688. }
  1689. buf[0] = '\0';
  1690. return buf;
  1691. }
  1692. int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  1693. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
  1694. int index;
  1695. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
  1696. dup_func, free_func)) {
  1697. return -1;
  1698. }
  1699. return index;
  1700. }
  1701. int SSL_set_ex_data(SSL *ssl, int idx, void *arg) {
  1702. return CRYPTO_set_ex_data(&ssl->ex_data, idx, arg);
  1703. }
  1704. void *SSL_get_ex_data(const SSL *ssl, int idx) {
  1705. return CRYPTO_get_ex_data(&ssl->ex_data, idx);
  1706. }
  1707. int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  1708. CRYPTO_EX_dup *dup_func,
  1709. CRYPTO_EX_free *free_func) {
  1710. int index;
  1711. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
  1712. dup_func, free_func)) {
  1713. return -1;
  1714. }
  1715. return index;
  1716. }
  1717. int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *arg) {
  1718. return CRYPTO_set_ex_data(&ctx->ex_data, idx, arg);
  1719. }
  1720. void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) {
  1721. return CRYPTO_get_ex_data(&ctx->ex_data, idx);
  1722. }
  1723. int SSL_want(const SSL *ssl) { return ssl->rwstate; }
  1724. void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
  1725. RSA *(*cb)(SSL *ssl, int is_export,
  1726. int keylength)) {
  1727. }
  1728. void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
  1729. int keylength)) {
  1730. }
  1731. void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
  1732. DH *(*callback)(SSL *ssl, int is_export,
  1733. int keylength)) {
  1734. }
  1735. void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
  1736. int keylength)) {
  1737. }
  1738. int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
  1739. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  1740. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  1741. return 0;
  1742. }
  1743. OPENSSL_free(ctx->psk_identity_hint);
  1744. if (identity_hint != NULL) {
  1745. ctx->psk_identity_hint = BUF_strdup(identity_hint);
  1746. if (ctx->psk_identity_hint == NULL) {
  1747. return 0;
  1748. }
  1749. } else {
  1750. ctx->psk_identity_hint = NULL;
  1751. }
  1752. return 1;
  1753. }
  1754. int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) {
  1755. if (ssl == NULL) {
  1756. return 0;
  1757. }
  1758. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  1759. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  1760. return 0;
  1761. }
  1762. /* Clear currently configured hint, if any. */
  1763. OPENSSL_free(ssl->psk_identity_hint);
  1764. ssl->psk_identity_hint = NULL;
  1765. /* Treat the empty hint as not supplying one. Plain PSK makes it possible to
  1766. * send either no hint (omit ServerKeyExchange) or an empty hint, while
  1767. * ECDHE_PSK can only spell empty hint. Having different capabilities is odd,
  1768. * so we interpret empty and missing as identical. */
  1769. if (identity_hint != NULL && identity_hint[0] != '\0') {
  1770. ssl->psk_identity_hint = BUF_strdup(identity_hint);
  1771. if (ssl->psk_identity_hint == NULL) {
  1772. return 0;
  1773. }
  1774. }
  1775. return 1;
  1776. }
  1777. const char *SSL_get_psk_identity_hint(const SSL *ssl) {
  1778. if (ssl == NULL) {
  1779. return NULL;
  1780. }
  1781. return ssl->psk_identity_hint;
  1782. }
  1783. const char *SSL_get_psk_identity(const SSL *ssl) {
  1784. if (ssl == NULL) {
  1785. return NULL;
  1786. }
  1787. SSL_SESSION *session = SSL_get_session(ssl);
  1788. if (session == NULL) {
  1789. return NULL;
  1790. }
  1791. return session->psk_identity;
  1792. }
  1793. void SSL_set_psk_client_callback(
  1794. SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
  1795. unsigned max_identity_len, uint8_t *psk,
  1796. unsigned max_psk_len)) {
  1797. ssl->psk_client_callback = cb;
  1798. }
  1799. void SSL_CTX_set_psk_client_callback(
  1800. SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
  1801. unsigned max_identity_len, uint8_t *psk,
  1802. unsigned max_psk_len)) {
  1803. ctx->psk_client_callback = cb;
  1804. }
  1805. void SSL_set_psk_server_callback(
  1806. SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
  1807. unsigned max_psk_len)) {
  1808. ssl->psk_server_callback = cb;
  1809. }
  1810. void SSL_CTX_set_psk_server_callback(
  1811. SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity,
  1812. uint8_t *psk, unsigned max_psk_len)) {
  1813. ctx->psk_server_callback = cb;
  1814. }
  1815. void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
  1816. void (*cb)(int write_p, int version,
  1817. int content_type, const void *buf,
  1818. size_t len, SSL *ssl, void *arg)) {
  1819. ctx->msg_callback = cb;
  1820. }
  1821. void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
  1822. ctx->msg_callback_arg = arg;
  1823. }
  1824. void SSL_set_msg_callback(SSL *ssl,
  1825. void (*cb)(int write_p, int version, int content_type,
  1826. const void *buf, size_t len, SSL *ssl,
  1827. void *arg)) {
  1828. ssl->msg_callback = cb;
  1829. }
  1830. void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
  1831. ssl->msg_callback_arg = arg;
  1832. }
  1833. void SSL_CTX_set_keylog_callback(SSL_CTX *ctx,
  1834. void (*cb)(const SSL *ssl, const char *line)) {
  1835. ctx->keylog_callback = cb;
  1836. }
  1837. void (*SSL_CTX_get_keylog_callback(const SSL_CTX *ctx))(const SSL *ssl,
  1838. const char *line) {
  1839. return ctx->keylog_callback;
  1840. }
  1841. void SSL_CTX_set_current_time_cb(SSL_CTX *ctx,
  1842. void (*cb)(const SSL *ssl,
  1843. struct timeval *out_clock)) {
  1844. ctx->current_time_cb = cb;
  1845. }
  1846. static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
  1847. static const char hextable[] = "0123456789abcdef";
  1848. uint8_t *out;
  1849. if (!CBB_add_space(cbb, &out, in_len * 2)) {
  1850. return 0;
  1851. }
  1852. for (size_t i = 0; i < in_len; i++) {
  1853. *(out++) = (uint8_t)hextable[in[i] >> 4];
  1854. *(out++) = (uint8_t)hextable[in[i] & 0xf];
  1855. }
  1856. return 1;
  1857. }
  1858. int ssl_log_secret(const SSL *ssl, const char *label, const uint8_t *secret,
  1859. size_t secret_len) {
  1860. if (ssl->ctx->keylog_callback == NULL) {
  1861. return 1;
  1862. }
  1863. CBB cbb;
  1864. uint8_t *out;
  1865. size_t out_len;
  1866. if (!CBB_init(&cbb, strlen(label) + 1 + SSL3_RANDOM_SIZE * 2 + 1 +
  1867. secret_len * 2 + 1) ||
  1868. !CBB_add_bytes(&cbb, (const uint8_t *)label, strlen(label)) ||
  1869. !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
  1870. !cbb_add_hex(&cbb, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
  1871. !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
  1872. !cbb_add_hex(&cbb, secret, secret_len) ||
  1873. !CBB_add_u8(&cbb, 0 /* NUL */) ||
  1874. !CBB_finish(&cbb, &out, &out_len)) {
  1875. CBB_cleanup(&cbb);
  1876. return 0;
  1877. }
  1878. ssl->ctx->keylog_callback(ssl, (const char *)out);
  1879. OPENSSL_free(out);
  1880. return 1;
  1881. }
  1882. int SSL_is_init_finished(const SSL *ssl) {
  1883. return !SSL_in_init(ssl);
  1884. }
  1885. int SSL_in_init(const SSL *ssl) {
  1886. SSL_HANDSHAKE *hs = ssl->s3->hs;
  1887. return hs != NULL && hs->state != SSL_ST_OK;
  1888. }
  1889. int SSL_in_false_start(const SSL *ssl) {
  1890. if (ssl->s3->hs == NULL) {
  1891. return 0;
  1892. }
  1893. return ssl->s3->hs->in_false_start;
  1894. }
  1895. int SSL_cutthrough_complete(const SSL *ssl) {
  1896. return SSL_in_false_start(ssl);
  1897. }
  1898. void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
  1899. size_t *ssl_session_size) {
  1900. *ssl_size = sizeof(SSL);
  1901. *ssl_ctx_size = sizeof(SSL_CTX);
  1902. *ssl_session_size = sizeof(SSL_SESSION);
  1903. }
  1904. int ssl3_can_false_start(const SSL *ssl) {
  1905. const SSL_CIPHER *const cipher = SSL_get_current_cipher(ssl);
  1906. /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
  1907. return !SSL_is_dtls(ssl) &&
  1908. SSL_version(ssl) == TLS1_2_VERSION &&
  1909. (ssl->s3->alpn_selected != NULL ||
  1910. ssl->s3->next_proto_negotiated != NULL) &&
  1911. cipher != NULL &&
  1912. cipher->algorithm_mkey == SSL_kECDHE &&
  1913. cipher->algorithm_mac == SSL_AEAD;
  1914. }
  1915. const struct {
  1916. uint16_t version;
  1917. uint32_t flag;
  1918. } kVersions[] = {
  1919. {SSL3_VERSION, SSL_OP_NO_SSLv3},
  1920. {TLS1_VERSION, SSL_OP_NO_TLSv1},
  1921. {TLS1_1_VERSION, SSL_OP_NO_TLSv1_1},
  1922. {TLS1_2_VERSION, SSL_OP_NO_TLSv1_2},
  1923. {TLS1_3_VERSION, SSL_OP_NO_TLSv1_3},
  1924. };
  1925. static const size_t kVersionsLen = OPENSSL_ARRAY_SIZE(kVersions);
  1926. int ssl_get_version_range(const SSL *ssl, uint16_t *out_min_version,
  1927. uint16_t *out_max_version) {
  1928. /* For historical reasons, |SSL_OP_NO_DTLSv1| aliases |SSL_OP_NO_TLSv1|, but
  1929. * DTLS 1.0 should be mapped to TLS 1.1. */
  1930. uint32_t options = ssl->options;
  1931. if (SSL_is_dtls(ssl)) {
  1932. options &= ~SSL_OP_NO_TLSv1_1;
  1933. if (options & SSL_OP_NO_DTLSv1) {
  1934. options |= SSL_OP_NO_TLSv1_1;
  1935. }
  1936. }
  1937. uint16_t min_version = ssl->min_version;
  1938. uint16_t max_version = ssl->max_version;
  1939. /* Bound the range to only those implemented in this protocol. */
  1940. if (min_version < ssl->method->min_version) {
  1941. min_version = ssl->method->min_version;
  1942. }
  1943. if (max_version > ssl->method->max_version) {
  1944. max_version = ssl->method->max_version;
  1945. }
  1946. /* OpenSSL's API for controlling versions entails blacklisting individual
  1947. * protocols. This has two problems. First, on the client, the protocol can
  1948. * only express a contiguous range of versions. Second, a library consumer
  1949. * trying to set a maximum version cannot disable protocol versions that get
  1950. * added in a future version of the library.
  1951. *
  1952. * To account for both of these, OpenSSL interprets the client-side bitmask
  1953. * as a min/max range by picking the lowest contiguous non-empty range of
  1954. * enabled protocols. Note that this means it is impossible to set a maximum
  1955. * version of the higest supported TLS version in a future-proof way. */
  1956. int any_enabled = 0;
  1957. for (size_t i = 0; i < kVersionsLen; i++) {
  1958. /* Only look at the versions already enabled. */
  1959. if (min_version > kVersions[i].version) {
  1960. continue;
  1961. }
  1962. if (max_version < kVersions[i].version) {
  1963. break;
  1964. }
  1965. if (!(options & kVersions[i].flag)) {
  1966. /* The minimum version is the first enabled version. */
  1967. if (!any_enabled) {
  1968. any_enabled = 1;
  1969. min_version = kVersions[i].version;
  1970. }
  1971. continue;
  1972. }
  1973. /* If there is a disabled version after the first enabled one, all versions
  1974. * after it are implicitly disabled. */
  1975. if (any_enabled) {
  1976. max_version = kVersions[i-1].version;
  1977. break;
  1978. }
  1979. }
  1980. if (!any_enabled) {
  1981. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SUPPORTED_VERSIONS_ENABLED);
  1982. return 0;
  1983. }
  1984. *out_min_version = min_version;
  1985. *out_max_version = max_version;
  1986. return 1;
  1987. }
  1988. uint16_t ssl3_protocol_version(const SSL *ssl) {
  1989. assert(ssl->s3->have_version);
  1990. uint16_t version;
  1991. if (!ssl->method->version_from_wire(&version, ssl->version)) {
  1992. /* TODO(davidben): Use the internal version representation for ssl->version
  1993. * and map to the public API representation at API boundaries. */
  1994. assert(0);
  1995. return 0;
  1996. }
  1997. return version;
  1998. }
  1999. int SSL_is_server(const SSL *ssl) { return ssl->server; }
  2000. int SSL_is_dtls(const SSL *ssl) { return ssl->method->is_dtls; }
  2001. void SSL_CTX_set_select_certificate_cb(
  2002. SSL_CTX *ctx,
  2003. enum ssl_select_cert_result_t (*cb)(const SSL_CLIENT_HELLO *)) {
  2004. ctx->select_certificate_cb = cb;
  2005. }
  2006. void SSL_CTX_set_dos_protection_cb(SSL_CTX *ctx,
  2007. int (*cb)(const SSL_CLIENT_HELLO *)) {
  2008. ctx->dos_protection_cb = cb;
  2009. }
  2010. void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) {
  2011. ssl->renegotiate_mode = mode;
  2012. }
  2013. int SSL_get_ivs(const SSL *ssl, const uint8_t **out_read_iv,
  2014. const uint8_t **out_write_iv, size_t *out_iv_len) {
  2015. if (ssl->s3->aead_read_ctx == NULL || ssl->s3->aead_write_ctx == NULL) {
  2016. return 0;
  2017. }
  2018. size_t write_iv_len;
  2019. if (!EVP_AEAD_CTX_get_iv(&ssl->s3->aead_read_ctx->ctx, out_read_iv,
  2020. out_iv_len) ||
  2021. !EVP_AEAD_CTX_get_iv(&ssl->s3->aead_write_ctx->ctx, out_write_iv,
  2022. &write_iv_len) ||
  2023. *out_iv_len != write_iv_len) {
  2024. return 0;
  2025. }
  2026. return 1;
  2027. }
  2028. static uint64_t be_to_u64(const uint8_t in[8]) {
  2029. return (((uint64_t)in[0]) << 56) | (((uint64_t)in[1]) << 48) |
  2030. (((uint64_t)in[2]) << 40) | (((uint64_t)in[3]) << 32) |
  2031. (((uint64_t)in[4]) << 24) | (((uint64_t)in[5]) << 16) |
  2032. (((uint64_t)in[6]) << 8) | ((uint64_t)in[7]);
  2033. }
  2034. uint64_t SSL_get_read_sequence(const SSL *ssl) {
  2035. /* TODO(davidben): Internally represent sequence numbers as uint64_t. */
  2036. if (SSL_is_dtls(ssl)) {
  2037. /* max_seq_num already includes the epoch. */
  2038. assert(ssl->d1->r_epoch == (ssl->d1->bitmap.max_seq_num >> 48));
  2039. return ssl->d1->bitmap.max_seq_num;
  2040. }
  2041. return be_to_u64(ssl->s3->read_sequence);
  2042. }
  2043. uint64_t SSL_get_write_sequence(const SSL *ssl) {
  2044. uint64_t ret = be_to_u64(ssl->s3->write_sequence);
  2045. if (SSL_is_dtls(ssl)) {
  2046. assert((ret >> 48) == 0);
  2047. ret |= ((uint64_t)ssl->d1->w_epoch) << 48;
  2048. }
  2049. return ret;
  2050. }
  2051. uint16_t SSL_get_peer_signature_algorithm(const SSL *ssl) {
  2052. /* TODO(davidben): This checks the wrong session if there is a renegotiation
  2053. * in progress. */
  2054. SSL_SESSION *session = SSL_get_session(ssl);
  2055. if (session == NULL) {
  2056. return 0;
  2057. }
  2058. return session->peer_signature_algorithm;
  2059. }
  2060. size_t SSL_get_client_random(const SSL *ssl, uint8_t *out, size_t max_out) {
  2061. if (max_out == 0) {
  2062. return sizeof(ssl->s3->client_random);
  2063. }
  2064. if (max_out > sizeof(ssl->s3->client_random)) {
  2065. max_out = sizeof(ssl->s3->client_random);
  2066. }
  2067. OPENSSL_memcpy(out, ssl->s3->client_random, max_out);
  2068. return max_out;
  2069. }
  2070. size_t SSL_get_server_random(const SSL *ssl, uint8_t *out, size_t max_out) {
  2071. if (max_out == 0) {
  2072. return sizeof(ssl->s3->server_random);
  2073. }
  2074. if (max_out > sizeof(ssl->s3->server_random)) {
  2075. max_out = sizeof(ssl->s3->server_random);
  2076. }
  2077. OPENSSL_memcpy(out, ssl->s3->server_random, max_out);
  2078. return max_out;
  2079. }
  2080. const SSL_CIPHER *SSL_get_pending_cipher(const SSL *ssl) {
  2081. SSL_HANDSHAKE *hs = ssl->s3->hs;
  2082. if (hs == NULL) {
  2083. return NULL;
  2084. }
  2085. return hs->new_cipher;
  2086. }
  2087. void SSL_set_retain_only_sha256_of_client_certs(SSL *ssl, int enabled) {
  2088. ssl->retain_only_sha256_of_client_certs = !!enabled;
  2089. }
  2090. void SSL_CTX_set_retain_only_sha256_of_client_certs(SSL_CTX *ctx, int enabled) {
  2091. ctx->retain_only_sha256_of_client_certs = !!enabled;
  2092. }
  2093. void SSL_CTX_set_grease_enabled(SSL_CTX *ctx, int enabled) {
  2094. ctx->grease_enabled = !!enabled;
  2095. }
  2096. int32_t SSL_get_ticket_age_skew(const SSL *ssl) {
  2097. return ssl->s3->ticket_age_skew;
  2098. }
  2099. int SSL_clear(SSL *ssl) {
  2100. /* In OpenSSL, reusing a client |SSL| with |SSL_clear| causes the previously
  2101. * established session to be offered the next time around. wpa_supplicant
  2102. * depends on this behavior, so emulate it. */
  2103. SSL_SESSION *session = NULL;
  2104. if (!ssl->server && ssl->s3->established_session != NULL) {
  2105. session = ssl->s3->established_session;
  2106. SSL_SESSION_up_ref(session);
  2107. }
  2108. /* TODO(davidben): Some state on |ssl| is reset both in |SSL_new| and
  2109. * |SSL_clear| because it is per-connection state rather than configuration
  2110. * state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is
  2111. * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
  2112. * |ssl3_new|. */
  2113. ssl->rwstate = SSL_NOTHING;
  2114. BUF_MEM_free(ssl->init_buf);
  2115. ssl->init_buf = NULL;
  2116. ssl->init_msg = NULL;
  2117. ssl->init_num = 0;
  2118. /* The ssl->d1->mtu is simultaneously configuration (preserved across
  2119. * clear) and connection-specific state (gets reset).
  2120. *
  2121. * TODO(davidben): Avoid this. */
  2122. unsigned mtu = 0;
  2123. if (ssl->d1 != NULL) {
  2124. mtu = ssl->d1->mtu;
  2125. }
  2126. ssl->method->ssl_free(ssl);
  2127. if (!ssl->method->ssl_new(ssl)) {
  2128. SSL_SESSION_free(session);
  2129. return 0;
  2130. }
  2131. if (SSL_is_dtls(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
  2132. ssl->d1->mtu = mtu;
  2133. }
  2134. if (session != NULL) {
  2135. SSL_set_session(ssl, session);
  2136. SSL_SESSION_free(session);
  2137. }
  2138. return 1;
  2139. }
  2140. void ssl_do_info_callback(const SSL *ssl, int type, int value) {
  2141. void (*cb)(const SSL *ssl, int type, int value) = NULL;
  2142. if (ssl->info_callback != NULL) {
  2143. cb = ssl->info_callback;
  2144. } else if (ssl->ctx->info_callback != NULL) {
  2145. cb = ssl->ctx->info_callback;
  2146. }
  2147. if (cb != NULL) {
  2148. cb(ssl, type, value);
  2149. }
  2150. }
  2151. void ssl_do_msg_callback(SSL *ssl, int is_write, int content_type,
  2152. const void *buf, size_t len) {
  2153. if (ssl->msg_callback == NULL) {
  2154. return;
  2155. }
  2156. /* |version| is zero when calling for |SSL3_RT_HEADER| and |SSL2_VERSION| for
  2157. * a V2ClientHello. */
  2158. int version;
  2159. switch (content_type) {
  2160. case 0:
  2161. /* V2ClientHello */
  2162. version = SSL2_VERSION;
  2163. break;
  2164. case SSL3_RT_HEADER:
  2165. version = 0;
  2166. break;
  2167. default:
  2168. version = SSL_version(ssl);
  2169. }
  2170. ssl->msg_callback(is_write, version, content_type, buf, len, ssl,
  2171. ssl->msg_callback_arg);
  2172. }
  2173. int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
  2174. int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
  2175. int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
  2176. int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
  2177. int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
  2178. int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
  2179. int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
  2180. int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
  2181. int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
  2182. int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
  2183. int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
  2184. int SSL_num_renegotiations(const SSL *ssl) {
  2185. return SSL_total_renegotiations(ssl);
  2186. }
  2187. int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) { return 0; }
  2188. int SSL_need_tmp_RSA(const SSL *ssl) { return 0; }
  2189. int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) { return 1; }
  2190. int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) { return 1; }
  2191. void ERR_load_SSL_strings(void) {}
  2192. void SSL_load_error_strings(void) {}
  2193. int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); }
  2194. int SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, const EC_KEY *ec_key) {
  2195. if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
  2196. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  2197. return 0;
  2198. }
  2199. int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
  2200. return SSL_CTX_set1_curves(ctx, &nid, 1);
  2201. }
  2202. int SSL_set_tmp_ecdh(SSL *ssl, const EC_KEY *ec_key) {
  2203. if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
  2204. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  2205. return 0;
  2206. }
  2207. int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
  2208. return SSL_set1_curves(ssl, &nid, 1);
  2209. }
  2210. void ssl_get_current_time(const SSL *ssl, struct OPENSSL_timeval *out_clock) {
  2211. if (ssl->ctx->current_time_cb != NULL) {
  2212. /* TODO(davidben): Update current_time_cb to use OPENSSL_timeval. See
  2213. * https://crbug.com/boringssl/155. */
  2214. struct timeval clock;
  2215. ssl->ctx->current_time_cb(ssl, &clock);
  2216. if (clock.tv_sec < 0) {
  2217. assert(0);
  2218. out_clock->tv_sec = 0;
  2219. out_clock->tv_usec = 0;
  2220. } else {
  2221. out_clock->tv_sec = (uint64_t)clock.tv_sec;
  2222. out_clock->tv_usec = (uint32_t)clock.tv_usec;
  2223. }
  2224. return;
  2225. }
  2226. #if defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
  2227. out_clock->tv_sec = 1234;
  2228. out_clock->tv_usec = 1234;
  2229. #elif defined(OPENSSL_WINDOWS)
  2230. struct _timeb time;
  2231. _ftime(&time);
  2232. if (time.time < 0) {
  2233. assert(0);
  2234. out_clock->tv_sec = 0;
  2235. out_clock->tv_usec = 0;
  2236. } else {
  2237. out_clock->tv_sec = time.time;
  2238. out_clock->tv_usec = time.millitm * 1000;
  2239. }
  2240. #else
  2241. struct timeval clock;
  2242. gettimeofday(&clock, NULL);
  2243. if (clock.tv_sec < 0) {
  2244. assert(0);
  2245. out_clock->tv_sec = 0;
  2246. out_clock->tv_usec = 0;
  2247. } else {
  2248. out_clock->tv_sec = (uint64_t)clock.tv_sec;
  2249. out_clock->tv_usec = (uint32_t)clock.tv_usec;
  2250. }
  2251. #endif
  2252. }
  2253. int SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
  2254. return SSL_CTX_set_min_proto_version(ctx, version);
  2255. }
  2256. int SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
  2257. return SSL_CTX_set_max_proto_version(ctx, version);
  2258. }
  2259. int SSL_set_min_version(SSL *ssl, uint16_t version) {
  2260. return SSL_set_min_proto_version(ssl, version);
  2261. }
  2262. int SSL_set_max_version(SSL *ssl, uint16_t version) {
  2263. return SSL_set_max_proto_version(ssl, version);
  2264. }
  2265. void SSL_CTX_set_ticket_aead_method(SSL_CTX *ctx,
  2266. const SSL_TICKET_AEAD_METHOD *aead_method) {
  2267. ctx->ticket_aead_method = aead_method;
  2268. }