Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

3054 строки
86 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/dh.h>
  147. #include <openssl/err.h>
  148. #include <openssl/lhash.h>
  149. #include <openssl/mem.h>
  150. #include <openssl/rand.h>
  151. #include "internal.h"
  152. #include "../crypto/internal.h"
  153. #if defined(OPENSSL_WINDOWS)
  154. #include <sys/timeb.h>
  155. #else
  156. #include <sys/socket.h>
  157. #include <sys/time.h>
  158. #endif
  159. /* |SSL_R_UNKNOWN_PROTOCOL| is no longer emitted, but continue to define it
  160. * to avoid downstream churn. */
  161. OPENSSL_DECLARE_ERROR_REASON(SSL, UNKNOWN_PROTOCOL)
  162. /* The following errors are no longer emitted, but are used in nginx without
  163. * #ifdefs. */
  164. OPENSSL_DECLARE_ERROR_REASON(SSL, BLOCK_CIPHER_PAD_IS_WRONG)
  165. OPENSSL_DECLARE_ERROR_REASON(SSL, NO_CIPHERS_SPECIFIED)
  166. /* Some error codes are special. Ensure the make_errors.go script never
  167. * regresses this. */
  168. OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
  169. SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
  170. ssl_alert_reason_code_mismatch);
  171. /* kMaxHandshakeSize is the maximum size, in bytes, of a handshake message. */
  172. static const size_t kMaxHandshakeSize = (1u << 24) - 1;
  173. static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl =
  174. CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
  175. static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx =
  176. CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
  177. int SSL_library_init(void) {
  178. CRYPTO_library_init();
  179. return 1;
  180. }
  181. static uint32_t ssl_session_hash(const SSL_SESSION *sess) {
  182. const uint8_t *session_id = sess->session_id;
  183. uint8_t tmp_storage[sizeof(uint32_t)];
  184. if (sess->session_id_length < sizeof(tmp_storage)) {
  185. memset(tmp_storage, 0, sizeof(tmp_storage));
  186. memcpy(tmp_storage, sess->session_id, sess->session_id_length);
  187. session_id = tmp_storage;
  188. }
  189. uint32_t hash =
  190. ((uint32_t)session_id[0]) |
  191. ((uint32_t)session_id[1] << 8) |
  192. ((uint32_t)session_id[2] << 16) |
  193. ((uint32_t)session_id[3] << 24);
  194. return hash;
  195. }
  196. /* NB: If this function (or indeed the hash function which uses a sort of
  197. * coarser function than this one) is changed, ensure
  198. * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
  199. * able to construct an SSL_SESSION that will collide with any existing session
  200. * with a matching session ID. */
  201. static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
  202. if (a->ssl_version != b->ssl_version) {
  203. return 1;
  204. }
  205. if (a->session_id_length != b->session_id_length) {
  206. return 1;
  207. }
  208. return memcmp(a->session_id, b->session_id, a->session_id_length);
  209. }
  210. SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
  211. SSL_CTX *ret = NULL;
  212. if (method == NULL) {
  213. OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
  214. return NULL;
  215. }
  216. if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
  217. OPENSSL_PUT_ERROR(SSL, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
  218. goto err;
  219. }
  220. ret = OPENSSL_malloc(sizeof(SSL_CTX));
  221. if (ret == NULL) {
  222. goto err;
  223. }
  224. memset(ret, 0, sizeof(SSL_CTX));
  225. ret->method = method->method;
  226. CRYPTO_MUTEX_init(&ret->lock);
  227. ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
  228. ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
  229. /* We take the system default */
  230. ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  231. ret->references = 1;
  232. ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
  233. ret->verify_mode = SSL_VERIFY_NONE;
  234. ret->cert = ssl_cert_new();
  235. if (ret->cert == NULL) {
  236. goto err;
  237. }
  238. ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
  239. if (ret->sessions == NULL) {
  240. goto err;
  241. }
  242. ret->cert_store = X509_STORE_new();
  243. if (ret->cert_store == NULL) {
  244. goto err;
  245. }
  246. ssl_create_cipher_list(ret->method, &ret->cipher_list,
  247. SSL_DEFAULT_CIPHER_LIST);
  248. if (ret->cipher_list == NULL ||
  249. sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
  250. OPENSSL_PUT_ERROR(SSL, SSL_R_LIBRARY_HAS_NO_CIPHERS);
  251. goto err2;
  252. }
  253. ret->param = X509_VERIFY_PARAM_new();
  254. if (!ret->param) {
  255. goto err;
  256. }
  257. ret->client_CA = sk_X509_NAME_new_null();
  258. if (ret->client_CA == NULL) {
  259. goto err;
  260. }
  261. CRYPTO_new_ex_data(&ret->ex_data);
  262. ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  263. /* Setup RFC4507 ticket keys */
  264. if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
  265. !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
  266. !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
  267. ret->options |= SSL_OP_NO_TICKET;
  268. }
  269. /* Disable the auto-chaining feature by default. Once this has stuck without
  270. * problems, the feature will be removed entirely. */
  271. ret->mode = SSL_MODE_NO_AUTO_CHAIN;
  272. /* Lock the SSL_CTX to the specified version, for compatibility with legacy
  273. * uses of SSL_METHOD. */
  274. if (!SSL_CTX_set_max_proto_version(ret, method->version) ||
  275. !SSL_CTX_set_min_proto_version(ret, method->version)) {
  276. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  277. goto err2;
  278. }
  279. return ret;
  280. err:
  281. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  282. err2:
  283. SSL_CTX_free(ret);
  284. return NULL;
  285. }
  286. int SSL_CTX_up_ref(SSL_CTX *ctx) {
  287. CRYPTO_refcount_inc(&ctx->references);
  288. return 1;
  289. }
  290. void SSL_CTX_free(SSL_CTX *ctx) {
  291. if (ctx == NULL ||
  292. !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
  293. return;
  294. }
  295. X509_VERIFY_PARAM_free(ctx->param);
  296. /* Free internal session cache. However: the remove_cb() may reference the
  297. * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
  298. * sessions were flushed. As the ex_data handling routines might also touch
  299. * the session cache, the most secure solution seems to be: empty (flush) the
  300. * cache, then free ex_data, then finally free the cache. (See ticket
  301. * [openssl.org #212].) */
  302. SSL_CTX_flush_sessions(ctx, 0);
  303. CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
  304. CRYPTO_MUTEX_cleanup(&ctx->lock);
  305. lh_SSL_SESSION_free(ctx->sessions);
  306. X509_STORE_free(ctx->cert_store);
  307. ssl_cipher_preference_list_free(ctx->cipher_list);
  308. ssl_cipher_preference_list_free(ctx->cipher_list_tls10);
  309. ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
  310. ssl_cert_free(ctx->cert);
  311. sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions,
  312. SSL_CUSTOM_EXTENSION_free);
  313. sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions,
  314. SSL_CUSTOM_EXTENSION_free);
  315. sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
  316. sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
  317. OPENSSL_free(ctx->psk_identity_hint);
  318. OPENSSL_free(ctx->supported_group_list);
  319. OPENSSL_free(ctx->alpn_client_proto_list);
  320. CRYPTO_BUFFER_free(ctx->ocsp_response);
  321. OPENSSL_free(ctx->signed_cert_timestamp_list);
  322. EVP_PKEY_free(ctx->tlsext_channel_id_private);
  323. OPENSSL_free(ctx);
  324. }
  325. SSL *SSL_new(SSL_CTX *ctx) {
  326. if (ctx == NULL) {
  327. OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
  328. return NULL;
  329. }
  330. if (ctx->method == NULL) {
  331. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
  332. return NULL;
  333. }
  334. SSL *ssl = OPENSSL_malloc(sizeof(SSL));
  335. if (ssl == NULL) {
  336. goto err;
  337. }
  338. memset(ssl, 0, sizeof(SSL));
  339. ssl->min_version = ctx->min_version;
  340. ssl->max_version = ctx->max_version;
  341. /* RFC 6347 states that implementations SHOULD use an initial timer value of
  342. * 1 second. */
  343. ssl->initial_timeout_duration_ms = 1000;
  344. ssl->options = ctx->options;
  345. ssl->mode = ctx->mode;
  346. ssl->max_cert_list = ctx->max_cert_list;
  347. ssl->cert = ssl_cert_dup(ctx->cert);
  348. if (ssl->cert == NULL) {
  349. goto err;
  350. }
  351. ssl->msg_callback = ctx->msg_callback;
  352. ssl->msg_callback_arg = ctx->msg_callback_arg;
  353. ssl->verify_mode = ctx->verify_mode;
  354. ssl->sid_ctx_length = ctx->sid_ctx_length;
  355. assert(ssl->sid_ctx_length <= sizeof ssl->sid_ctx);
  356. memcpy(&ssl->sid_ctx, &ctx->sid_ctx, sizeof(ssl->sid_ctx));
  357. ssl->verify_callback = ctx->default_verify_callback;
  358. ssl->retain_only_sha256_of_client_certs =
  359. ctx->retain_only_sha256_of_client_certs;
  360. ssl->param = X509_VERIFY_PARAM_new();
  361. if (!ssl->param) {
  362. goto err;
  363. }
  364. X509_VERIFY_PARAM_inherit(ssl->param, ctx->param);
  365. ssl->quiet_shutdown = ctx->quiet_shutdown;
  366. ssl->max_send_fragment = ctx->max_send_fragment;
  367. CRYPTO_refcount_inc(&ctx->references);
  368. ssl->ctx = ctx;
  369. CRYPTO_refcount_inc(&ctx->references);
  370. ssl->initial_ctx = ctx;
  371. if (ctx->supported_group_list) {
  372. ssl->supported_group_list =
  373. BUF_memdup(ctx->supported_group_list,
  374. ctx->supported_group_list_len * 2);
  375. if (!ssl->supported_group_list) {
  376. goto err;
  377. }
  378. ssl->supported_group_list_len = ctx->supported_group_list_len;
  379. }
  380. if (ssl->ctx->alpn_client_proto_list) {
  381. ssl->alpn_client_proto_list = BUF_memdup(
  382. ssl->ctx->alpn_client_proto_list, ssl->ctx->alpn_client_proto_list_len);
  383. if (ssl->alpn_client_proto_list == NULL) {
  384. goto err;
  385. }
  386. ssl->alpn_client_proto_list_len = ssl->ctx->alpn_client_proto_list_len;
  387. }
  388. ssl->method = ctx->method;
  389. if (!ssl->method->ssl_new(ssl)) {
  390. goto err;
  391. }
  392. ssl->rwstate = SSL_NOTHING;
  393. CRYPTO_new_ex_data(&ssl->ex_data);
  394. ssl->psk_identity_hint = NULL;
  395. if (ctx->psk_identity_hint) {
  396. ssl->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
  397. if (ssl->psk_identity_hint == NULL) {
  398. goto err;
  399. }
  400. }
  401. ssl->psk_client_callback = ctx->psk_client_callback;
  402. ssl->psk_server_callback = ctx->psk_server_callback;
  403. ssl->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
  404. if (ctx->tlsext_channel_id_private) {
  405. EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
  406. ssl->tlsext_channel_id_private = ctx->tlsext_channel_id_private;
  407. }
  408. ssl->signed_cert_timestamps_enabled =
  409. ssl->ctx->signed_cert_timestamps_enabled;
  410. ssl->ocsp_stapling_enabled = ssl->ctx->ocsp_stapling_enabled;
  411. ssl->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  412. /* If the context has a default timeout, use it over the default. */
  413. if (ctx->session_timeout != 0) {
  414. ssl->session_timeout = ctx->session_timeout;
  415. }
  416. /* If the context has an OCSP response, use it. */
  417. if (ctx->ocsp_response != NULL) {
  418. CRYPTO_BUFFER_up_ref(ctx->ocsp_response);
  419. ssl->ocsp_response = ctx->ocsp_response;
  420. }
  421. return ssl;
  422. err:
  423. SSL_free(ssl);
  424. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  425. return NULL;
  426. }
  427. void SSL_free(SSL *ssl) {
  428. if (ssl == NULL) {
  429. return;
  430. }
  431. X509_VERIFY_PARAM_free(ssl->param);
  432. CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
  433. ssl_free_wbio_buffer(ssl);
  434. assert(ssl->bbio == NULL);
  435. BIO_free_all(ssl->rbio);
  436. BIO_free_all(ssl->wbio);
  437. BUF_MEM_free(ssl->init_buf);
  438. /* add extra stuff */
  439. ssl_cipher_preference_list_free(ssl->cipher_list);
  440. SSL_SESSION_free(ssl->session);
  441. ssl_cert_free(ssl->cert);
  442. OPENSSL_free(ssl->tlsext_hostname);
  443. SSL_CTX_free(ssl->initial_ctx);
  444. OPENSSL_free(ssl->supported_group_list);
  445. OPENSSL_free(ssl->alpn_client_proto_list);
  446. EVP_PKEY_free(ssl->tlsext_channel_id_private);
  447. OPENSSL_free(ssl->psk_identity_hint);
  448. sk_X509_NAME_pop_free(ssl->client_CA, X509_NAME_free);
  449. sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
  450. CRYPTO_BUFFER_free(ssl->ocsp_response);
  451. if (ssl->method != NULL) {
  452. ssl->method->ssl_free(ssl);
  453. }
  454. SSL_CTX_free(ssl->ctx);
  455. OPENSSL_free(ssl);
  456. }
  457. void SSL_set_connect_state(SSL *ssl) {
  458. ssl->server = 0;
  459. ssl->handshake_func = ssl3_connect;
  460. }
  461. void SSL_set_accept_state(SSL *ssl) {
  462. ssl->server = 1;
  463. ssl->handshake_func = ssl3_accept;
  464. }
  465. void SSL_set0_rbio(SSL *ssl, BIO *rbio) {
  466. BIO_free_all(ssl->rbio);
  467. ssl->rbio = rbio;
  468. }
  469. void SSL_set0_wbio(SSL *ssl, BIO *wbio) {
  470. /* If the output buffering BIO is still in place, remove it. */
  471. if (ssl->bbio != NULL) {
  472. ssl->wbio = BIO_pop(ssl->wbio);
  473. }
  474. BIO_free_all(ssl->wbio);
  475. ssl->wbio = wbio;
  476. /* Re-attach |bbio| to the new |wbio|. */
  477. if (ssl->bbio != NULL) {
  478. ssl->wbio = BIO_push(ssl->bbio, ssl->wbio);
  479. }
  480. }
  481. void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
  482. /* For historical reasons, this function has many different cases in ownership
  483. * handling. */
  484. /* If nothing has changed, do nothing */
  485. if (rbio == SSL_get_rbio(ssl) && wbio == SSL_get_wbio(ssl)) {
  486. return;
  487. }
  488. /* If the two arguments are equal, one fewer reference is granted than
  489. * taken. */
  490. if (rbio != NULL && rbio == wbio) {
  491. BIO_up_ref(rbio);
  492. }
  493. /* If only the wbio is changed, adopt only one reference. */
  494. if (rbio == SSL_get_rbio(ssl)) {
  495. SSL_set0_wbio(ssl, wbio);
  496. return;
  497. }
  498. /* There is an asymmetry here for historical reasons. If only the rbio is
  499. * changed AND the rbio and wbio were originally different, then we only adopt
  500. * one reference. */
  501. if (wbio == SSL_get_wbio(ssl) && SSL_get_rbio(ssl) != SSL_get_wbio(ssl)) {
  502. SSL_set0_rbio(ssl, rbio);
  503. return;
  504. }
  505. /* Otherwise, adopt both references. */
  506. SSL_set0_rbio(ssl, rbio);
  507. SSL_set0_wbio(ssl, wbio);
  508. }
  509. BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio; }
  510. BIO *SSL_get_wbio(const SSL *ssl) {
  511. if (ssl->bbio != NULL) {
  512. /* If |bbio| is active, the true caller-configured BIO is its |next_bio|. */
  513. assert(ssl->bbio == ssl->wbio);
  514. return ssl->bbio->next_bio;
  515. }
  516. return ssl->wbio;
  517. }
  518. int SSL_do_handshake(SSL *ssl) {
  519. ssl->rwstate = SSL_NOTHING;
  520. /* Functions which use SSL_get_error must clear the error queue on entry. */
  521. ERR_clear_error();
  522. ERR_clear_system_error();
  523. if (ssl->handshake_func == NULL) {
  524. OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
  525. return -1;
  526. }
  527. if (!SSL_in_init(ssl)) {
  528. return 1;
  529. }
  530. if (ssl->s3->hs == NULL) {
  531. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  532. return -1;
  533. }
  534. /* Run the handshake. */
  535. assert(ssl->s3->hs != NULL);
  536. int ret = ssl->handshake_func(ssl->s3->hs);
  537. if (ret <= 0) {
  538. return ret;
  539. }
  540. /* Destroy the handshake object if the handshake has completely finished. */
  541. if (!SSL_in_init(ssl)) {
  542. ssl_handshake_free(ssl->s3->hs);
  543. ssl->s3->hs = NULL;
  544. }
  545. return 1;
  546. }
  547. int SSL_connect(SSL *ssl) {
  548. if (ssl->handshake_func == NULL) {
  549. /* Not properly initialized yet */
  550. SSL_set_connect_state(ssl);
  551. }
  552. return SSL_do_handshake(ssl);
  553. }
  554. int SSL_accept(SSL *ssl) {
  555. if (ssl->handshake_func == NULL) {
  556. /* Not properly initialized yet */
  557. SSL_set_accept_state(ssl);
  558. }
  559. return SSL_do_handshake(ssl);
  560. }
  561. static int ssl_do_renegotiate(SSL *ssl) {
  562. /* We do not accept renegotiations as a server or SSL 3.0. SSL 3.0 will be
  563. * removed entirely in the future and requires retaining more data for
  564. * renegotiation_info. */
  565. if (ssl->server || ssl->version == SSL3_VERSION) {
  566. goto no_renegotiation;
  567. }
  568. if (ssl->s3->tmp.message_type != SSL3_MT_HELLO_REQUEST ||
  569. ssl->init_num != 0) {
  570. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  571. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST);
  572. return 0;
  573. }
  574. switch (ssl->renegotiate_mode) {
  575. case ssl_renegotiate_ignore:
  576. /* Ignore the HelloRequest. */
  577. return 1;
  578. case ssl_renegotiate_once:
  579. if (ssl->s3->total_renegotiations != 0) {
  580. goto no_renegotiation;
  581. }
  582. break;
  583. case ssl_renegotiate_never:
  584. goto no_renegotiation;
  585. case ssl_renegotiate_freely:
  586. break;
  587. }
  588. /* Renegotiation is only supported at quiescent points in the application
  589. * protocol, namely in HTTPS, just before reading the HTTP response. Require
  590. * the record-layer be idle and avoid complexities of sending a handshake
  591. * record while an application_data record is being written. */
  592. if (ssl_write_buffer_is_pending(ssl)) {
  593. goto no_renegotiation;
  594. }
  595. /* Begin a new handshake. */
  596. if (ssl->s3->hs != NULL) {
  597. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  598. return 0;
  599. }
  600. ssl->s3->hs = ssl_handshake_new(ssl);
  601. if (ssl->s3->hs == NULL) {
  602. return 0;
  603. }
  604. ssl->s3->total_renegotiations++;
  605. return 1;
  606. no_renegotiation:
  607. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION);
  608. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
  609. return 0;
  610. }
  611. static int ssl_do_post_handshake(SSL *ssl) {
  612. if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
  613. return ssl_do_renegotiate(ssl);
  614. }
  615. return tls13_post_handshake(ssl);
  616. }
  617. static int ssl_read_impl(SSL *ssl, void *buf, int num, int peek) {
  618. ssl->rwstate = SSL_NOTHING;
  619. /* Functions which use SSL_get_error must clear the error queue on entry. */
  620. ERR_clear_error();
  621. ERR_clear_system_error();
  622. if (ssl->handshake_func == NULL) {
  623. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  624. return -1;
  625. }
  626. for (;;) {
  627. /* Complete the current handshake, if any. False Start will cause
  628. * |SSL_do_handshake| to return mid-handshake, so this may require multiple
  629. * iterations. */
  630. while (SSL_in_init(ssl)) {
  631. int ret = SSL_do_handshake(ssl);
  632. if (ret < 0) {
  633. return ret;
  634. }
  635. if (ret == 0) {
  636. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  637. return -1;
  638. }
  639. }
  640. int got_handshake;
  641. int ret = ssl->method->read_app_data(ssl, &got_handshake, buf, num, peek);
  642. if (ret > 0 || !got_handshake) {
  643. ssl->s3->key_update_count = 0;
  644. return ret;
  645. }
  646. /* Handle the post-handshake message and try again. */
  647. if (!ssl_do_post_handshake(ssl)) {
  648. return -1;
  649. }
  650. ssl->method->release_current_message(ssl, 1 /* free buffer */);
  651. }
  652. }
  653. int SSL_read(SSL *ssl, void *buf, int num) {
  654. return ssl_read_impl(ssl, buf, num, 0 /* consume bytes */);
  655. }
  656. int SSL_peek(SSL *ssl, void *buf, int num) {
  657. return ssl_read_impl(ssl, buf, num, 1 /* peek */);
  658. }
  659. int SSL_write(SSL *ssl, const void *buf, int num) {
  660. ssl->rwstate = SSL_NOTHING;
  661. /* Functions which use SSL_get_error must clear the error queue on entry. */
  662. ERR_clear_error();
  663. ERR_clear_system_error();
  664. if (ssl->handshake_func == NULL) {
  665. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  666. return -1;
  667. }
  668. if (ssl->s3->send_shutdown != ssl_shutdown_none) {
  669. OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  670. return -1;
  671. }
  672. /* If necessary, complete the handshake implicitly. */
  673. if (SSL_in_init(ssl) && !SSL_in_false_start(ssl)) {
  674. int ret = SSL_do_handshake(ssl);
  675. if (ret < 0) {
  676. return ret;
  677. }
  678. if (ret == 0) {
  679. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  680. return -1;
  681. }
  682. }
  683. return ssl->method->write_app_data(ssl, buf, num);
  684. }
  685. int SSL_shutdown(SSL *ssl) {
  686. ssl->rwstate = SSL_NOTHING;
  687. /* Functions which use SSL_get_error must clear the error queue on entry. */
  688. ERR_clear_error();
  689. ERR_clear_system_error();
  690. if (ssl->handshake_func == NULL) {
  691. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  692. return -1;
  693. }
  694. /* We can't shutdown properly if we are in the middle of a handshake. */
  695. if (SSL_in_init(ssl)) {
  696. OPENSSL_PUT_ERROR(SSL, SSL_R_SHUTDOWN_WHILE_IN_INIT);
  697. return -1;
  698. }
  699. if (ssl->quiet_shutdown) {
  700. /* Do nothing if configured not to send a close_notify. */
  701. ssl->s3->send_shutdown = ssl_shutdown_close_notify;
  702. ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
  703. return 1;
  704. }
  705. /* This function completes in two stages. It sends a close_notify and then it
  706. * waits for a close_notify to come in. Perform exactly one action and return
  707. * whether or not it succeeds. */
  708. if (ssl->s3->send_shutdown != ssl_shutdown_close_notify) {
  709. /* Send a close_notify. */
  710. if (ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY) <= 0) {
  711. return -1;
  712. }
  713. } else if (ssl->s3->alert_dispatch) {
  714. /* Finish sending the close_notify. */
  715. if (ssl->method->dispatch_alert(ssl) <= 0) {
  716. return -1;
  717. }
  718. } else if (ssl->s3->recv_shutdown != ssl_shutdown_close_notify) {
  719. /* Wait for the peer's close_notify. */
  720. ssl->method->read_close_notify(ssl);
  721. if (ssl->s3->recv_shutdown != ssl_shutdown_close_notify) {
  722. return -1;
  723. }
  724. }
  725. /* Return 0 for unidirectional shutdown and 1 for bidirectional shutdown. */
  726. return ssl->s3->recv_shutdown == ssl_shutdown_close_notify;
  727. }
  728. int SSL_send_fatal_alert(SSL *ssl, uint8_t alert) {
  729. if (ssl->s3->alert_dispatch) {
  730. if (ssl->s3->send_alert[0] != SSL3_AL_FATAL ||
  731. ssl->s3->send_alert[1] != alert) {
  732. /* We are already attempting to write a different alert. */
  733. OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  734. return -1;
  735. }
  736. return ssl->method->dispatch_alert(ssl);
  737. }
  738. return ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  739. }
  740. static int bio_retry_reason_to_error(int reason) {
  741. switch (reason) {
  742. case BIO_RR_CONNECT:
  743. return SSL_ERROR_WANT_CONNECT;
  744. case BIO_RR_ACCEPT:
  745. return SSL_ERROR_WANT_ACCEPT;
  746. default:
  747. return SSL_ERROR_SYSCALL;
  748. }
  749. }
  750. int SSL_get_error(const SSL *ssl, int ret_code) {
  751. if (ret_code > 0) {
  752. return SSL_ERROR_NONE;
  753. }
  754. /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
  755. * where we do encode the error */
  756. uint32_t err = ERR_peek_error();
  757. if (err != 0) {
  758. if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
  759. return SSL_ERROR_SYSCALL;
  760. }
  761. return SSL_ERROR_SSL;
  762. }
  763. if (ret_code == 0) {
  764. if (ssl->s3->recv_shutdown == ssl_shutdown_close_notify) {
  765. return SSL_ERROR_ZERO_RETURN;
  766. }
  767. /* An EOF was observed which violates the protocol, and the underlying
  768. * transport does not participate in the error queue. Bubble up to the
  769. * caller. */
  770. return SSL_ERROR_SYSCALL;
  771. }
  772. switch (ssl->rwstate) {
  773. case SSL_PENDING_SESSION:
  774. return SSL_ERROR_PENDING_SESSION;
  775. case SSL_CERTIFICATE_SELECTION_PENDING:
  776. return SSL_ERROR_PENDING_CERTIFICATE;
  777. case SSL_READING: {
  778. BIO *bio = SSL_get_rbio(ssl);
  779. if (BIO_should_read(bio)) {
  780. return SSL_ERROR_WANT_READ;
  781. }
  782. if (BIO_should_write(bio)) {
  783. /* TODO(davidben): OpenSSL historically checked for writes on the read
  784. * BIO. Can this be removed? */
  785. return SSL_ERROR_WANT_WRITE;
  786. }
  787. if (BIO_should_io_special(bio)) {
  788. return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
  789. }
  790. break;
  791. }
  792. case SSL_WRITING: {
  793. BIO *bio = SSL_get_wbio(ssl);
  794. if (BIO_should_write(bio)) {
  795. return SSL_ERROR_WANT_WRITE;
  796. }
  797. if (BIO_should_read(bio)) {
  798. /* TODO(davidben): OpenSSL historically checked for reads on the write
  799. * BIO. Can this be removed? */
  800. return SSL_ERROR_WANT_READ;
  801. }
  802. if (BIO_should_io_special(bio)) {
  803. return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
  804. }
  805. break;
  806. }
  807. case SSL_X509_LOOKUP:
  808. return SSL_ERROR_WANT_X509_LOOKUP;
  809. case SSL_CHANNEL_ID_LOOKUP:
  810. return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
  811. case SSL_PRIVATE_KEY_OPERATION:
  812. return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
  813. }
  814. return SSL_ERROR_SYSCALL;
  815. }
  816. static int set_min_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
  817. uint16_t version) {
  818. if (version == 0) {
  819. *out = method->min_version;
  820. return 1;
  821. }
  822. if (version == TLS1_3_VERSION) {
  823. version = TLS1_3_DRAFT_VERSION;
  824. }
  825. return method->version_from_wire(out, version);
  826. }
  827. static int set_max_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
  828. uint16_t version) {
  829. if (version == 0) {
  830. *out = method->max_version;
  831. /* TODO(svaldez): Enable TLS 1.3 by default once fully implemented. */
  832. if (*out > TLS1_2_VERSION) {
  833. *out = TLS1_2_VERSION;
  834. }
  835. return 1;
  836. }
  837. if (version == TLS1_3_VERSION) {
  838. version = TLS1_3_DRAFT_VERSION;
  839. }
  840. return method->version_from_wire(out, version);
  841. }
  842. int SSL_CTX_set_min_proto_version(SSL_CTX *ctx, uint16_t version) {
  843. return set_min_version(ctx->method, &ctx->min_version, version);
  844. }
  845. int SSL_CTX_set_max_proto_version(SSL_CTX *ctx, uint16_t version) {
  846. return set_max_version(ctx->method, &ctx->max_version, version);
  847. }
  848. int SSL_set_min_proto_version(SSL *ssl, uint16_t version) {
  849. return set_min_version(ssl->method, &ssl->min_version, version);
  850. }
  851. int SSL_set_max_proto_version(SSL *ssl, uint16_t version) {
  852. return set_max_version(ssl->method, &ssl->max_version, version);
  853. }
  854. uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
  855. ctx->options |= options;
  856. return ctx->options;
  857. }
  858. uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
  859. ctx->options &= ~options;
  860. return ctx->options;
  861. }
  862. uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
  863. uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
  864. ssl->options |= options;
  865. return ssl->options;
  866. }
  867. uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
  868. ssl->options &= ~options;
  869. return ssl->options;
  870. }
  871. uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
  872. uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
  873. ctx->mode |= mode;
  874. return ctx->mode;
  875. }
  876. uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
  877. ctx->mode &= ~mode;
  878. return ctx->mode;
  879. }
  880. uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
  881. uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
  882. ssl->mode |= mode;
  883. return ssl->mode;
  884. }
  885. uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
  886. ssl->mode &= ~mode;
  887. return ssl->mode;
  888. }
  889. uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
  890. void SSL_CTX_set0_buffer_pool(SSL_CTX *ctx, CRYPTO_BUFFER_POOL *pool) {
  891. ctx->pool = pool;
  892. }
  893. X509 *SSL_get_peer_certificate(const SSL *ssl) {
  894. if (ssl == NULL) {
  895. return NULL;
  896. }
  897. SSL_SESSION *session = SSL_get_session(ssl);
  898. if (session == NULL || session->x509_peer == NULL) {
  899. return NULL;
  900. }
  901. X509_up_ref(session->x509_peer);
  902. return session->x509_peer;
  903. }
  904. STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl) {
  905. if (ssl == NULL) {
  906. return NULL;
  907. }
  908. SSL_SESSION *session = SSL_get_session(ssl);
  909. if (session == NULL ||
  910. session->x509_chain == NULL) {
  911. return NULL;
  912. }
  913. if (!ssl->server) {
  914. return session->x509_chain;
  915. }
  916. /* OpenSSL historically didn't include the leaf certificate in the returned
  917. * certificate chain, but only for servers. */
  918. if (session->x509_chain_without_leaf == NULL) {
  919. session->x509_chain_without_leaf = sk_X509_new_null();
  920. if (session->x509_chain_without_leaf == NULL) {
  921. return NULL;
  922. }
  923. for (size_t i = 1; i < sk_X509_num(session->x509_chain); i++) {
  924. X509 *cert = sk_X509_value(session->x509_chain, i);
  925. if (!sk_X509_push(session->x509_chain_without_leaf, cert)) {
  926. sk_X509_pop_free(session->x509_chain_without_leaf, X509_free);
  927. session->x509_chain_without_leaf = NULL;
  928. return NULL;
  929. }
  930. X509_up_ref(cert);
  931. }
  932. }
  933. return session->x509_chain_without_leaf;
  934. }
  935. int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
  936. size_t max_out) {
  937. /* tls-unique is not defined for SSL 3.0 or TLS 1.3. */
  938. if (!ssl->s3->initial_handshake_complete ||
  939. ssl3_protocol_version(ssl) < TLS1_VERSION ||
  940. ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  941. goto err;
  942. }
  943. /* The tls-unique value is the first Finished message in the handshake, which
  944. * is the client's in a full handshake and the server's for a resumption. See
  945. * https://tools.ietf.org/html/rfc5929#section-3.1. */
  946. const uint8_t *finished = ssl->s3->previous_client_finished;
  947. size_t finished_len = ssl->s3->previous_client_finished_len;
  948. if (ssl->session != NULL) {
  949. /* tls-unique is broken for resumed sessions unless EMS is used. */
  950. if (!ssl->session->extended_master_secret) {
  951. goto err;
  952. }
  953. finished = ssl->s3->previous_server_finished;
  954. finished_len = ssl->s3->previous_server_finished_len;
  955. }
  956. *out_len = finished_len;
  957. if (finished_len > max_out) {
  958. *out_len = max_out;
  959. }
  960. memcpy(out, finished, *out_len);
  961. return 1;
  962. err:
  963. *out_len = 0;
  964. memset(out, 0, max_out);
  965. return 0;
  966. }
  967. int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
  968. size_t sid_ctx_len) {
  969. if (sid_ctx_len > sizeof(ctx->sid_ctx)) {
  970. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  971. return 0;
  972. }
  973. assert(sizeof(ctx->sid_ctx) < 256);
  974. ctx->sid_ctx_length = (uint8_t)sid_ctx_len;
  975. memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
  976. return 1;
  977. }
  978. int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
  979. size_t sid_ctx_len) {
  980. if (sid_ctx_len > sizeof(ssl->sid_ctx)) {
  981. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  982. return 0;
  983. }
  984. assert(sizeof(ssl->sid_ctx) < 256);
  985. ssl->sid_ctx_length = (uint8_t)sid_ctx_len;
  986. memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
  987. return 1;
  988. }
  989. int SSL_CTX_set_purpose(SSL_CTX *ctx, int purpose) {
  990. return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose);
  991. }
  992. int SSL_set_purpose(SSL *ssl, int purpose) {
  993. return X509_VERIFY_PARAM_set_purpose(ssl->param, purpose);
  994. }
  995. int SSL_CTX_set_trust(SSL_CTX *ctx, int trust) {
  996. return X509_VERIFY_PARAM_set_trust(ctx->param, trust);
  997. }
  998. int SSL_set_trust(SSL *ssl, int trust) {
  999. return X509_VERIFY_PARAM_set_trust(ssl->param, trust);
  1000. }
  1001. int SSL_CTX_set1_param(SSL_CTX *ctx, const X509_VERIFY_PARAM *param) {
  1002. return X509_VERIFY_PARAM_set1(ctx->param, param);
  1003. }
  1004. int SSL_set1_param(SSL *ssl, const X509_VERIFY_PARAM *param) {
  1005. return X509_VERIFY_PARAM_set1(ssl->param, param);
  1006. }
  1007. void ssl_cipher_preference_list_free(
  1008. struct ssl_cipher_preference_list_st *cipher_list) {
  1009. if (cipher_list == NULL) {
  1010. return;
  1011. }
  1012. sk_SSL_CIPHER_free(cipher_list->ciphers);
  1013. OPENSSL_free(cipher_list->in_group_flags);
  1014. OPENSSL_free(cipher_list);
  1015. }
  1016. X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
  1017. X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
  1018. void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); }
  1019. int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); }
  1020. int SSL_get_rfd(const SSL *ssl) {
  1021. int ret = -1;
  1022. BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR);
  1023. if (b != NULL) {
  1024. BIO_get_fd(b, &ret);
  1025. }
  1026. return ret;
  1027. }
  1028. int SSL_get_wfd(const SSL *ssl) {
  1029. int ret = -1;
  1030. BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR);
  1031. if (b != NULL) {
  1032. BIO_get_fd(b, &ret);
  1033. }
  1034. return ret;
  1035. }
  1036. int SSL_set_fd(SSL *ssl, int fd) {
  1037. BIO *bio = BIO_new(BIO_s_socket());
  1038. if (bio == NULL) {
  1039. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  1040. return 0;
  1041. }
  1042. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  1043. SSL_set_bio(ssl, bio, bio);
  1044. return 1;
  1045. }
  1046. int SSL_set_wfd(SSL *ssl, int fd) {
  1047. BIO *rbio = SSL_get_rbio(ssl);
  1048. if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET ||
  1049. BIO_get_fd(rbio, NULL) != fd) {
  1050. BIO *bio = BIO_new(BIO_s_socket());
  1051. if (bio == NULL) {
  1052. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  1053. return 0;
  1054. }
  1055. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  1056. SSL_set0_wbio(ssl, bio);
  1057. } else {
  1058. /* Copy the rbio over to the wbio. */
  1059. BIO_up_ref(rbio);
  1060. SSL_set0_wbio(ssl, rbio);
  1061. }
  1062. return 1;
  1063. }
  1064. int SSL_set_rfd(SSL *ssl, int fd) {
  1065. BIO *wbio = SSL_get_wbio(ssl);
  1066. if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET ||
  1067. BIO_get_fd(wbio, NULL) != fd) {
  1068. BIO *bio = BIO_new(BIO_s_socket());
  1069. if (bio == NULL) {
  1070. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  1071. return 0;
  1072. }
  1073. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  1074. SSL_set0_rbio(ssl, bio);
  1075. } else {
  1076. /* Copy the wbio over to the rbio. */
  1077. BIO_up_ref(wbio);
  1078. SSL_set0_rbio(ssl, wbio);
  1079. }
  1080. return 1;
  1081. }
  1082. static size_t copy_finished(void *out, size_t out_len, const uint8_t *in,
  1083. size_t in_len) {
  1084. if (out_len > in_len) {
  1085. out_len = in_len;
  1086. }
  1087. memcpy(out, in, out_len);
  1088. return in_len;
  1089. }
  1090. size_t SSL_get_finished(const SSL *ssl, void *buf, size_t count) {
  1091. if (!ssl->s3->initial_handshake_complete ||
  1092. ssl3_protocol_version(ssl) < TLS1_VERSION ||
  1093. ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  1094. return 0;
  1095. }
  1096. if (ssl->server) {
  1097. return copy_finished(buf, count, ssl->s3->previous_server_finished,
  1098. ssl->s3->previous_server_finished_len);
  1099. }
  1100. return copy_finished(buf, count, ssl->s3->previous_client_finished,
  1101. ssl->s3->previous_client_finished_len);
  1102. }
  1103. size_t SSL_get_peer_finished(const SSL *ssl, void *buf, size_t count) {
  1104. if (!ssl->s3->initial_handshake_complete ||
  1105. ssl3_protocol_version(ssl) < TLS1_VERSION ||
  1106. ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  1107. return 0;
  1108. }
  1109. if (ssl->server) {
  1110. return copy_finished(buf, count, ssl->s3->previous_client_finished,
  1111. ssl->s3->previous_client_finished_len);
  1112. }
  1113. return copy_finished(buf, count, ssl->s3->previous_server_finished,
  1114. ssl->s3->previous_server_finished_len);
  1115. }
  1116. int SSL_get_verify_mode(const SSL *ssl) { return ssl->verify_mode; }
  1117. int SSL_get_verify_depth(const SSL *ssl) {
  1118. return X509_VERIFY_PARAM_get_depth(ssl->param);
  1119. }
  1120. int SSL_get_extms_support(const SSL *ssl) {
  1121. if (!ssl->s3->have_version) {
  1122. return 0;
  1123. }
  1124. return ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
  1125. ssl->s3->tmp.extended_master_secret == 1;
  1126. }
  1127. int (*SSL_get_verify_callback(const SSL *ssl))(int, X509_STORE_CTX *) {
  1128. return ssl->verify_callback;
  1129. }
  1130. int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
  1131. int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
  1132. return X509_VERIFY_PARAM_get_depth(ctx->param);
  1133. }
  1134. int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(
  1135. int ok, X509_STORE_CTX *store_ctx) {
  1136. return ctx->default_verify_callback;
  1137. }
  1138. void SSL_set_verify(SSL *ssl, int mode,
  1139. int (*callback)(int ok, X509_STORE_CTX *store_ctx)) {
  1140. ssl->verify_mode = mode;
  1141. if (callback != NULL) {
  1142. ssl->verify_callback = callback;
  1143. }
  1144. }
  1145. void SSL_set_verify_depth(SSL *ssl, int depth) {
  1146. X509_VERIFY_PARAM_set_depth(ssl->param, depth);
  1147. }
  1148. int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
  1149. int SSL_get_read_ahead(const SSL *ssl) { return 0; }
  1150. void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
  1151. void SSL_set_read_ahead(SSL *ssl, int yes) { }
  1152. int SSL_pending(const SSL *ssl) {
  1153. if (ssl->s3->rrec.type != SSL3_RT_APPLICATION_DATA) {
  1154. return 0;
  1155. }
  1156. return ssl->s3->rrec.length;
  1157. }
  1158. /* Fix this so it checks all the valid key/cert options */
  1159. int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
  1160. if (ctx->cert->privatekey == NULL) {
  1161. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  1162. return 0;
  1163. }
  1164. X509 *x509 = ctx->cert->x509_leaf;
  1165. if (x509 == NULL) {
  1166. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
  1167. return 0;
  1168. }
  1169. return X509_check_private_key(x509, ctx->cert->privatekey);
  1170. }
  1171. /* Fix this function so that it takes an optional type parameter */
  1172. int SSL_check_private_key(const SSL *ssl) {
  1173. if (ssl->cert->privatekey == NULL) {
  1174. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  1175. return 0;
  1176. }
  1177. X509 *x509 = ssl->cert->x509_leaf;
  1178. if (x509 == NULL) {
  1179. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
  1180. return 0;
  1181. }
  1182. return X509_check_private_key(x509, ssl->cert->privatekey);
  1183. }
  1184. long SSL_get_default_timeout(const SSL *ssl) {
  1185. return SSL_DEFAULT_SESSION_TIMEOUT;
  1186. }
  1187. int SSL_renegotiate(SSL *ssl) {
  1188. /* Caller-initiated renegotiation is not supported. */
  1189. OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1190. return 0;
  1191. }
  1192. int SSL_renegotiate_pending(SSL *ssl) {
  1193. return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
  1194. }
  1195. int SSL_total_renegotiations(const SSL *ssl) {
  1196. return ssl->s3->total_renegotiations;
  1197. }
  1198. size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
  1199. return ctx->max_cert_list;
  1200. }
  1201. void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
  1202. if (max_cert_list > kMaxHandshakeSize) {
  1203. max_cert_list = kMaxHandshakeSize;
  1204. }
  1205. ctx->max_cert_list = (uint32_t)max_cert_list;
  1206. }
  1207. size_t SSL_get_max_cert_list(const SSL *ssl) {
  1208. return ssl->max_cert_list;
  1209. }
  1210. void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
  1211. if (max_cert_list > kMaxHandshakeSize) {
  1212. max_cert_list = kMaxHandshakeSize;
  1213. }
  1214. ssl->max_cert_list = (uint32_t)max_cert_list;
  1215. }
  1216. int SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
  1217. if (max_send_fragment < 512) {
  1218. max_send_fragment = 512;
  1219. }
  1220. if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
  1221. max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  1222. }
  1223. ctx->max_send_fragment = (uint16_t)max_send_fragment;
  1224. return 1;
  1225. }
  1226. int SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
  1227. if (max_send_fragment < 512) {
  1228. max_send_fragment = 512;
  1229. }
  1230. if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
  1231. max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  1232. }
  1233. ssl->max_send_fragment = (uint16_t)max_send_fragment;
  1234. return 1;
  1235. }
  1236. int SSL_set_mtu(SSL *ssl, unsigned mtu) {
  1237. if (!SSL_is_dtls(ssl) || mtu < dtls1_min_mtu()) {
  1238. return 0;
  1239. }
  1240. ssl->d1->mtu = mtu;
  1241. return 1;
  1242. }
  1243. int SSL_get_secure_renegotiation_support(const SSL *ssl) {
  1244. return ssl->s3->send_connection_binding;
  1245. }
  1246. LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
  1247. size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
  1248. return lh_SSL_SESSION_num_items(ctx->sessions);
  1249. }
  1250. unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
  1251. unsigned long ret = ctx->session_cache_size;
  1252. ctx->session_cache_size = size;
  1253. return ret;
  1254. }
  1255. unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
  1256. return ctx->session_cache_size;
  1257. }
  1258. int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
  1259. int ret = ctx->session_cache_mode;
  1260. ctx->session_cache_mode = mode;
  1261. return ret;
  1262. }
  1263. int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
  1264. return ctx->session_cache_mode;
  1265. }
  1266. int SSL_CTX_get_tlsext_ticket_keys(SSL_CTX *ctx, void *out, size_t len) {
  1267. if (out == NULL) {
  1268. return 48;
  1269. }
  1270. if (len != 48) {
  1271. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
  1272. return 0;
  1273. }
  1274. uint8_t *out_bytes = out;
  1275. memcpy(out_bytes, ctx->tlsext_tick_key_name, 16);
  1276. memcpy(out_bytes + 16, ctx->tlsext_tick_hmac_key, 16);
  1277. memcpy(out_bytes + 32, ctx->tlsext_tick_aes_key, 16);
  1278. return 1;
  1279. }
  1280. int SSL_CTX_set_tlsext_ticket_keys(SSL_CTX *ctx, const void *in, size_t len) {
  1281. if (in == NULL) {
  1282. return 48;
  1283. }
  1284. if (len != 48) {
  1285. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
  1286. return 0;
  1287. }
  1288. const uint8_t *in_bytes = in;
  1289. memcpy(ctx->tlsext_tick_key_name, in_bytes, 16);
  1290. memcpy(ctx->tlsext_tick_hmac_key, in_bytes + 16, 16);
  1291. memcpy(ctx->tlsext_tick_aes_key, in_bytes + 32, 16);
  1292. return 1;
  1293. }
  1294. int SSL_CTX_set_tlsext_ticket_key_cb(
  1295. SSL_CTX *ctx, int (*callback)(SSL *ssl, uint8_t *key_name, uint8_t *iv,
  1296. EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
  1297. int encrypt)) {
  1298. ctx->tlsext_ticket_key_cb = callback;
  1299. return 1;
  1300. }
  1301. int SSL_CTX_set1_curves(SSL_CTX *ctx, const int *curves, size_t curves_len) {
  1302. return tls1_set_curves(&ctx->supported_group_list,
  1303. &ctx->supported_group_list_len, curves,
  1304. curves_len);
  1305. }
  1306. int SSL_set1_curves(SSL *ssl, const int *curves, size_t curves_len) {
  1307. return tls1_set_curves(&ssl->supported_group_list,
  1308. &ssl->supported_group_list_len, curves,
  1309. curves_len);
  1310. }
  1311. int SSL_CTX_set1_curves_list(SSL_CTX *ctx, const char *curves) {
  1312. return tls1_set_curves_list(&ctx->supported_group_list,
  1313. &ctx->supported_group_list_len, curves);
  1314. }
  1315. int SSL_set1_curves_list(SSL *ssl, const char *curves) {
  1316. return tls1_set_curves_list(&ssl->supported_group_list,
  1317. &ssl->supported_group_list_len, curves);
  1318. }
  1319. uint16_t SSL_get_curve_id(const SSL *ssl) {
  1320. /* TODO(davidben): This checks the wrong session if there is a renegotiation in
  1321. * progress. */
  1322. SSL_SESSION *session = SSL_get_session(ssl);
  1323. if (session == NULL) {
  1324. return 0;
  1325. }
  1326. return session->group_id;
  1327. }
  1328. int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) {
  1329. DH_free(ctx->cert->dh_tmp);
  1330. ctx->cert->dh_tmp = DHparams_dup(dh);
  1331. if (ctx->cert->dh_tmp == NULL) {
  1332. OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
  1333. return 0;
  1334. }
  1335. return 1;
  1336. }
  1337. int SSL_set_tmp_dh(SSL *ssl, const DH *dh) {
  1338. DH_free(ssl->cert->dh_tmp);
  1339. ssl->cert->dh_tmp = DHparams_dup(dh);
  1340. if (ssl->cert->dh_tmp == NULL) {
  1341. OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
  1342. return 0;
  1343. }
  1344. return 1;
  1345. }
  1346. STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) {
  1347. if (ssl == NULL) {
  1348. return NULL;
  1349. }
  1350. const struct ssl_cipher_preference_list_st *prefs =
  1351. ssl_get_cipher_preferences(ssl);
  1352. if (prefs == NULL) {
  1353. return NULL;
  1354. }
  1355. return prefs->ciphers;
  1356. }
  1357. const char *SSL_get_cipher_list(const SSL *ssl, int n) {
  1358. const SSL_CIPHER *c;
  1359. STACK_OF(SSL_CIPHER) *sk;
  1360. if (ssl == NULL) {
  1361. return NULL;
  1362. }
  1363. sk = SSL_get_ciphers(ssl);
  1364. if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
  1365. return NULL;
  1366. }
  1367. c = sk_SSL_CIPHER_value(sk, n);
  1368. if (c == NULL) {
  1369. return NULL;
  1370. }
  1371. return c->name;
  1372. }
  1373. int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
  1374. STACK_OF(SSL_CIPHER) *cipher_list =
  1375. ssl_create_cipher_list(ctx->method, &ctx->cipher_list, str);
  1376. if (cipher_list == NULL) {
  1377. return 0;
  1378. }
  1379. /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
  1380. if (sk_SSL_CIPHER_num(cipher_list) == 0) {
  1381. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1382. return 0;
  1383. }
  1384. return 1;
  1385. }
  1386. int SSL_CTX_set_cipher_list_tls10(SSL_CTX *ctx, const char *str) {
  1387. STACK_OF(SSL_CIPHER) *cipher_list =
  1388. ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls10, str);
  1389. if (cipher_list == NULL) {
  1390. return 0;
  1391. }
  1392. /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
  1393. if (sk_SSL_CIPHER_num(cipher_list) == 0) {
  1394. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1395. return 0;
  1396. }
  1397. return 1;
  1398. }
  1399. int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
  1400. STACK_OF(SSL_CIPHER) *cipher_list =
  1401. ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, str);
  1402. if (cipher_list == NULL) {
  1403. return 0;
  1404. }
  1405. /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
  1406. if (sk_SSL_CIPHER_num(cipher_list) == 0) {
  1407. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1408. return 0;
  1409. }
  1410. return 1;
  1411. }
  1412. int SSL_set_cipher_list(SSL *ssl, const char *str) {
  1413. STACK_OF(SSL_CIPHER) *cipher_list =
  1414. ssl_create_cipher_list(ssl->ctx->method, &ssl->cipher_list, str);
  1415. if (cipher_list == NULL) {
  1416. return 0;
  1417. }
  1418. /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
  1419. if (sk_SSL_CIPHER_num(cipher_list) == 0) {
  1420. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1421. return 0;
  1422. }
  1423. return 1;
  1424. }
  1425. const char *SSL_get_servername(const SSL *ssl, const int type) {
  1426. if (type != TLSEXT_NAMETYPE_host_name) {
  1427. return NULL;
  1428. }
  1429. /* Historically, |SSL_get_servername| was also the configuration getter
  1430. * corresponding to |SSL_set_tlsext_host_name|. */
  1431. if (ssl->tlsext_hostname != NULL) {
  1432. return ssl->tlsext_hostname;
  1433. }
  1434. /* During the handshake, report the handshake value. */
  1435. if (ssl->s3->hs != NULL) {
  1436. return ssl->s3->hs->hostname;
  1437. }
  1438. /* SSL_get_servername may also be called after the handshake to look up the
  1439. * SNI value.
  1440. *
  1441. * TODO(davidben): This is almost unused. Can we remove it? */
  1442. SSL_SESSION *session = SSL_get_session(ssl);
  1443. if (session == NULL) {
  1444. return NULL;
  1445. }
  1446. return session->tlsext_hostname;
  1447. }
  1448. int SSL_get_servername_type(const SSL *ssl) {
  1449. SSL_SESSION *session = SSL_get_session(ssl);
  1450. if (session == NULL || session->tlsext_hostname == NULL) {
  1451. return -1;
  1452. }
  1453. return TLSEXT_NAMETYPE_host_name;
  1454. }
  1455. void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
  1456. ctx->signed_cert_timestamps_enabled = 1;
  1457. }
  1458. int SSL_enable_signed_cert_timestamps(SSL *ssl) {
  1459. ssl->signed_cert_timestamps_enabled = 1;
  1460. return 1;
  1461. }
  1462. void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
  1463. ctx->ocsp_stapling_enabled = 1;
  1464. }
  1465. int SSL_enable_ocsp_stapling(SSL *ssl) {
  1466. ssl->ocsp_stapling_enabled = 1;
  1467. return 1;
  1468. }
  1469. void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
  1470. size_t *out_len) {
  1471. SSL_SESSION *session = SSL_get_session(ssl);
  1472. *out_len = 0;
  1473. *out = NULL;
  1474. if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
  1475. return;
  1476. }
  1477. *out = session->tlsext_signed_cert_timestamp_list;
  1478. *out_len = session->tlsext_signed_cert_timestamp_list_length;
  1479. }
  1480. void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
  1481. size_t *out_len) {
  1482. SSL_SESSION *session = SSL_get_session(ssl);
  1483. *out_len = 0;
  1484. *out = NULL;
  1485. if (ssl->server || !session || !session->ocsp_response) {
  1486. return;
  1487. }
  1488. *out = session->ocsp_response;
  1489. *out_len = session->ocsp_response_length;
  1490. }
  1491. int SSL_CTX_set_signed_cert_timestamp_list(SSL_CTX *ctx, const uint8_t *list,
  1492. size_t list_len) {
  1493. CBS sct_list;
  1494. CBS_init(&sct_list, list, list_len);
  1495. if (!ssl_is_sct_list_valid(&sct_list)) {
  1496. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_SCT_LIST);
  1497. return 0;
  1498. }
  1499. return CBS_stow(&sct_list, &ctx->signed_cert_timestamp_list,
  1500. &ctx->signed_cert_timestamp_list_length);
  1501. }
  1502. int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response,
  1503. size_t response_len) {
  1504. CRYPTO_BUFFER_free(ctx->ocsp_response);
  1505. ctx->ocsp_response = CRYPTO_BUFFER_new(response, response_len, NULL);
  1506. return ctx->ocsp_response != NULL;
  1507. }
  1508. int SSL_set_ocsp_response(SSL *ssl, const uint8_t *response,
  1509. size_t response_len) {
  1510. CRYPTO_BUFFER_free(ssl->ocsp_response);
  1511. ssl->ocsp_response = CRYPTO_BUFFER_new(response, response_len, NULL);
  1512. return ssl->ocsp_response != NULL;
  1513. }
  1514. int SSL_set_tlsext_host_name(SSL *ssl, const char *name) {
  1515. OPENSSL_free(ssl->tlsext_hostname);
  1516. ssl->tlsext_hostname = NULL;
  1517. if (name == NULL) {
  1518. return 1;
  1519. }
  1520. size_t len = strlen(name);
  1521. if (len == 0 || len > TLSEXT_MAXLEN_host_name) {
  1522. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL3_EXT_INVALID_SERVERNAME);
  1523. return 0;
  1524. }
  1525. ssl->tlsext_hostname = BUF_strdup(name);
  1526. if (ssl->tlsext_hostname == NULL) {
  1527. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1528. return 0;
  1529. }
  1530. return 1;
  1531. }
  1532. int SSL_CTX_set_tlsext_servername_callback(
  1533. SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) {
  1534. ctx->tlsext_servername_callback = callback;
  1535. return 1;
  1536. }
  1537. int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) {
  1538. ctx->tlsext_servername_arg = arg;
  1539. return 1;
  1540. }
  1541. int SSL_select_next_proto(uint8_t **out, uint8_t *out_len,
  1542. const uint8_t *server, unsigned server_len,
  1543. const uint8_t *client, unsigned client_len) {
  1544. unsigned int i, j;
  1545. const uint8_t *result;
  1546. int status = OPENSSL_NPN_UNSUPPORTED;
  1547. /* For each protocol in server preference order, see if we support it. */
  1548. for (i = 0; i < server_len;) {
  1549. for (j = 0; j < client_len;) {
  1550. if (server[i] == client[j] &&
  1551. memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
  1552. /* We found a match */
  1553. result = &server[i];
  1554. status = OPENSSL_NPN_NEGOTIATED;
  1555. goto found;
  1556. }
  1557. j += client[j];
  1558. j++;
  1559. }
  1560. i += server[i];
  1561. i++;
  1562. }
  1563. /* There's no overlap between our protocols and the server's list. */
  1564. result = client;
  1565. status = OPENSSL_NPN_NO_OVERLAP;
  1566. found:
  1567. *out = (uint8_t *)result + 1;
  1568. *out_len = result[0];
  1569. return status;
  1570. }
  1571. void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data,
  1572. unsigned *out_len) {
  1573. *out_data = ssl->s3->next_proto_negotiated;
  1574. if (*out_data == NULL) {
  1575. *out_len = 0;
  1576. } else {
  1577. *out_len = ssl->s3->next_proto_negotiated_len;
  1578. }
  1579. }
  1580. void SSL_CTX_set_next_protos_advertised_cb(
  1581. SSL_CTX *ctx,
  1582. int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg),
  1583. void *arg) {
  1584. ctx->next_protos_advertised_cb = cb;
  1585. ctx->next_protos_advertised_cb_arg = arg;
  1586. }
  1587. void SSL_CTX_set_next_proto_select_cb(
  1588. SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
  1589. const uint8_t *in, unsigned in_len, void *arg),
  1590. void *arg) {
  1591. ctx->next_proto_select_cb = cb;
  1592. ctx->next_proto_select_cb_arg = arg;
  1593. }
  1594. int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
  1595. unsigned protos_len) {
  1596. OPENSSL_free(ctx->alpn_client_proto_list);
  1597. ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
  1598. if (!ctx->alpn_client_proto_list) {
  1599. return 1;
  1600. }
  1601. ctx->alpn_client_proto_list_len = protos_len;
  1602. return 0;
  1603. }
  1604. int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
  1605. OPENSSL_free(ssl->alpn_client_proto_list);
  1606. ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
  1607. if (!ssl->alpn_client_proto_list) {
  1608. return 1;
  1609. }
  1610. ssl->alpn_client_proto_list_len = protos_len;
  1611. return 0;
  1612. }
  1613. void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
  1614. int (*cb)(SSL *ssl, const uint8_t **out,
  1615. uint8_t *out_len, const uint8_t *in,
  1616. unsigned in_len, void *arg),
  1617. void *arg) {
  1618. ctx->alpn_select_cb = cb;
  1619. ctx->alpn_select_cb_arg = arg;
  1620. }
  1621. void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data,
  1622. unsigned *out_len) {
  1623. *out_data = NULL;
  1624. if (ssl->s3) {
  1625. *out_data = ssl->s3->alpn_selected;
  1626. }
  1627. if (*out_data == NULL) {
  1628. *out_len = 0;
  1629. } else {
  1630. *out_len = ssl->s3->alpn_selected_len;
  1631. }
  1632. }
  1633. void SSL_CTX_set_tls_channel_id_enabled(SSL_CTX *ctx, int enabled) {
  1634. ctx->tlsext_channel_id_enabled = !!enabled;
  1635. }
  1636. int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) {
  1637. SSL_CTX_set_tls_channel_id_enabled(ctx, 1);
  1638. return 1;
  1639. }
  1640. void SSL_set_tls_channel_id_enabled(SSL *ssl, int enabled) {
  1641. ssl->tlsext_channel_id_enabled = !!enabled;
  1642. }
  1643. int SSL_enable_tls_channel_id(SSL *ssl) {
  1644. SSL_set_tls_channel_id_enabled(ssl, 1);
  1645. return 1;
  1646. }
  1647. static int is_p256_key(EVP_PKEY *private_key) {
  1648. const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(private_key);
  1649. return ec_key != NULL &&
  1650. EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)) ==
  1651. NID_X9_62_prime256v1;
  1652. }
  1653. int SSL_CTX_set1_tls_channel_id(SSL_CTX *ctx, EVP_PKEY *private_key) {
  1654. if (!is_p256_key(private_key)) {
  1655. OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
  1656. return 0;
  1657. }
  1658. EVP_PKEY_free(ctx->tlsext_channel_id_private);
  1659. EVP_PKEY_up_ref(private_key);
  1660. ctx->tlsext_channel_id_private = private_key;
  1661. ctx->tlsext_channel_id_enabled = 1;
  1662. return 1;
  1663. }
  1664. int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) {
  1665. if (!is_p256_key(private_key)) {
  1666. OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
  1667. return 0;
  1668. }
  1669. EVP_PKEY_free(ssl->tlsext_channel_id_private);
  1670. EVP_PKEY_up_ref(private_key);
  1671. ssl->tlsext_channel_id_private = private_key;
  1672. ssl->tlsext_channel_id_enabled = 1;
  1673. return 1;
  1674. }
  1675. size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) {
  1676. if (!ssl->s3->tlsext_channel_id_valid) {
  1677. return 0;
  1678. }
  1679. memcpy(out, ssl->s3->tlsext_channel_id, (max_out < 64) ? max_out : 64);
  1680. return 64;
  1681. }
  1682. void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
  1683. int (*cb)(X509_STORE_CTX *store_ctx,
  1684. void *arg),
  1685. void *arg) {
  1686. ctx->app_verify_callback = cb;
  1687. ctx->app_verify_arg = arg;
  1688. }
  1689. void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
  1690. int (*cb)(int, X509_STORE_CTX *)) {
  1691. ctx->verify_mode = mode;
  1692. ctx->default_verify_callback = cb;
  1693. }
  1694. void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
  1695. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  1696. }
  1697. size_t SSL_get0_certificate_types(SSL *ssl, const uint8_t **out_types) {
  1698. if (ssl->server || ssl->s3->hs == NULL) {
  1699. *out_types = NULL;
  1700. return 0;
  1701. }
  1702. *out_types = ssl->s3->hs->certificate_types;
  1703. return ssl->s3->hs->num_certificate_types;
  1704. }
  1705. void ssl_update_cache(SSL_HANDSHAKE *hs, int mode) {
  1706. SSL *const ssl = hs->ssl;
  1707. SSL_CTX *ctx = ssl->initial_ctx;
  1708. /* Never cache sessions with empty session IDs. */
  1709. if (ssl->s3->established_session->session_id_length == 0 ||
  1710. (ctx->session_cache_mode & mode) != mode) {
  1711. return;
  1712. }
  1713. /* Clients never use the internal session cache. */
  1714. int use_internal_cache = ssl->server && !(ctx->session_cache_mode &
  1715. SSL_SESS_CACHE_NO_INTERNAL_STORE);
  1716. /* A client may see new sessions on abbreviated handshakes if the server
  1717. * decides to renew the ticket. Once the handshake is completed, it should be
  1718. * inserted into the cache. */
  1719. if (ssl->s3->established_session != ssl->session ||
  1720. (!ssl->server && hs->ticket_expected)) {
  1721. if (use_internal_cache) {
  1722. SSL_CTX_add_session(ctx, ssl->s3->established_session);
  1723. }
  1724. if (ctx->new_session_cb != NULL) {
  1725. SSL_SESSION_up_ref(ssl->s3->established_session);
  1726. if (!ctx->new_session_cb(ssl, ssl->s3->established_session)) {
  1727. /* |new_session_cb|'s return value signals whether it took ownership. */
  1728. SSL_SESSION_free(ssl->s3->established_session);
  1729. }
  1730. }
  1731. }
  1732. if (use_internal_cache &&
  1733. !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR)) {
  1734. /* Automatically flush the internal session cache every 255 connections. */
  1735. int flush_cache = 0;
  1736. CRYPTO_MUTEX_lock_write(&ctx->lock);
  1737. ctx->handshakes_since_cache_flush++;
  1738. if (ctx->handshakes_since_cache_flush >= 255) {
  1739. flush_cache = 1;
  1740. ctx->handshakes_since_cache_flush = 0;
  1741. }
  1742. CRYPTO_MUTEX_unlock_write(&ctx->lock);
  1743. if (flush_cache) {
  1744. struct timeval now;
  1745. ssl_get_current_time(ssl, &now);
  1746. SSL_CTX_flush_sessions(ctx, (long)now.tv_sec);
  1747. }
  1748. }
  1749. }
  1750. static const char *ssl_get_version(int version) {
  1751. switch (version) {
  1752. /* Report TLS 1.3 draft version as TLS 1.3 in the public API. */
  1753. case TLS1_3_DRAFT_VERSION:
  1754. return "TLSv1.3";
  1755. case TLS1_2_VERSION:
  1756. return "TLSv1.2";
  1757. case TLS1_1_VERSION:
  1758. return "TLSv1.1";
  1759. case TLS1_VERSION:
  1760. return "TLSv1";
  1761. case SSL3_VERSION:
  1762. return "SSLv3";
  1763. case DTLS1_VERSION:
  1764. return "DTLSv1";
  1765. case DTLS1_2_VERSION:
  1766. return "DTLSv1.2";
  1767. default:
  1768. return "unknown";
  1769. }
  1770. }
  1771. const char *SSL_get_version(const SSL *ssl) {
  1772. return ssl_get_version(ssl->version);
  1773. }
  1774. const char *SSL_SESSION_get_version(const SSL_SESSION *session) {
  1775. return ssl_get_version(session->ssl_version);
  1776. }
  1777. X509 *SSL_get_certificate(const SSL *ssl) {
  1778. if (ssl->cert != NULL) {
  1779. return ssl->cert->x509_leaf;
  1780. }
  1781. return NULL;
  1782. }
  1783. EVP_PKEY *SSL_get_privatekey(const SSL *ssl) {
  1784. if (ssl->cert != NULL) {
  1785. return ssl->cert->privatekey;
  1786. }
  1787. return NULL;
  1788. }
  1789. X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
  1790. if (ctx->cert != NULL) {
  1791. return ctx->cert->x509_leaf;
  1792. }
  1793. return NULL;
  1794. }
  1795. EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
  1796. if (ctx->cert != NULL) {
  1797. return ctx->cert->privatekey;
  1798. }
  1799. return NULL;
  1800. }
  1801. const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) {
  1802. if (ssl->s3->aead_write_ctx == NULL) {
  1803. return NULL;
  1804. }
  1805. return ssl->s3->aead_write_ctx->cipher;
  1806. }
  1807. int SSL_session_reused(const SSL *ssl) {
  1808. return ssl->s3->session_reused;
  1809. }
  1810. const COMP_METHOD *SSL_get_current_compression(SSL *ssl) { return NULL; }
  1811. const COMP_METHOD *SSL_get_current_expansion(SSL *ssl) { return NULL; }
  1812. int *SSL_get_server_tmp_key(SSL *ssl, EVP_PKEY **out_key) { return 0; }
  1813. int ssl_is_wbio_buffered(const SSL *ssl) {
  1814. return ssl->bbio != NULL;
  1815. }
  1816. int ssl_init_wbio_buffer(SSL *ssl) {
  1817. if (ssl->bbio != NULL) {
  1818. /* Already buffered. */
  1819. assert(ssl->bbio == ssl->wbio);
  1820. return 1;
  1821. }
  1822. BIO *bbio = BIO_new(BIO_f_buffer());
  1823. if (bbio == NULL ||
  1824. !BIO_set_read_buffer_size(bbio, 1)) {
  1825. BIO_free(bbio);
  1826. return 0;
  1827. }
  1828. ssl->bbio = bbio;
  1829. ssl->wbio = BIO_push(bbio, ssl->wbio);
  1830. return 1;
  1831. }
  1832. void ssl_free_wbio_buffer(SSL *ssl) {
  1833. if (ssl->bbio == NULL) {
  1834. return;
  1835. }
  1836. assert(ssl->bbio == ssl->wbio);
  1837. ssl->wbio = BIO_pop(ssl->wbio);
  1838. BIO_free(ssl->bbio);
  1839. ssl->bbio = NULL;
  1840. }
  1841. void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
  1842. ctx->quiet_shutdown = (mode != 0);
  1843. }
  1844. int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
  1845. return ctx->quiet_shutdown;
  1846. }
  1847. void SSL_set_quiet_shutdown(SSL *ssl, int mode) {
  1848. ssl->quiet_shutdown = (mode != 0);
  1849. }
  1850. int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; }
  1851. void SSL_set_shutdown(SSL *ssl, int mode) {
  1852. /* It is an error to clear any bits that have already been set. (We can't try
  1853. * to get a second close_notify or send two.) */
  1854. assert((SSL_get_shutdown(ssl) & mode) == SSL_get_shutdown(ssl));
  1855. if (mode & SSL_RECEIVED_SHUTDOWN &&
  1856. ssl->s3->recv_shutdown == ssl_shutdown_none) {
  1857. ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
  1858. }
  1859. if (mode & SSL_SENT_SHUTDOWN &&
  1860. ssl->s3->send_shutdown == ssl_shutdown_none) {
  1861. ssl->s3->send_shutdown = ssl_shutdown_close_notify;
  1862. }
  1863. }
  1864. int SSL_get_shutdown(const SSL *ssl) {
  1865. int ret = 0;
  1866. if (ssl->s3->recv_shutdown != ssl_shutdown_none) {
  1867. /* Historically, OpenSSL set |SSL_RECEIVED_SHUTDOWN| on both close_notify
  1868. * and fatal alert. */
  1869. ret |= SSL_RECEIVED_SHUTDOWN;
  1870. }
  1871. if (ssl->s3->send_shutdown == ssl_shutdown_close_notify) {
  1872. /* Historically, OpenSSL set |SSL_SENT_SHUTDOWN| on only close_notify. */
  1873. ret |= SSL_SENT_SHUTDOWN;
  1874. }
  1875. return ret;
  1876. }
  1877. int SSL_version(const SSL *ssl) {
  1878. /* Report TLS 1.3 draft version as TLS 1.3 in the public API. */
  1879. if (ssl->version == TLS1_3_DRAFT_VERSION) {
  1880. return TLS1_3_VERSION;
  1881. }
  1882. return ssl->version;
  1883. }
  1884. SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
  1885. SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
  1886. if (ssl->ctx == ctx) {
  1887. return ssl->ctx;
  1888. }
  1889. if (ctx == NULL) {
  1890. ctx = ssl->initial_ctx;
  1891. }
  1892. ssl_cert_free(ssl->cert);
  1893. ssl->cert = ssl_cert_dup(ctx->cert);
  1894. CRYPTO_refcount_inc(&ctx->references);
  1895. SSL_CTX_free(ssl->ctx); /* decrement reference count */
  1896. ssl->ctx = ctx;
  1897. ssl->sid_ctx_length = ctx->sid_ctx_length;
  1898. assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
  1899. memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
  1900. return ssl->ctx;
  1901. }
  1902. int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
  1903. return X509_STORE_set_default_paths(ctx->cert_store);
  1904. }
  1905. int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *ca_file,
  1906. const char *ca_dir) {
  1907. return X509_STORE_load_locations(ctx->cert_store, ca_file, ca_dir);
  1908. }
  1909. void SSL_set_info_callback(SSL *ssl,
  1910. void (*cb)(const SSL *ssl, int type, int value)) {
  1911. ssl->info_callback = cb;
  1912. }
  1913. void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type,
  1914. int value) {
  1915. return ssl->info_callback;
  1916. }
  1917. int SSL_state(const SSL *ssl) {
  1918. if (ssl->s3->hs == NULL) {
  1919. assert(ssl->s3->initial_handshake_complete);
  1920. return SSL_ST_OK;
  1921. }
  1922. return ssl->s3->hs->state;
  1923. }
  1924. void SSL_set_state(SSL *ssl, int state) { }
  1925. char *SSL_get_shared_ciphers(const SSL *ssl, char *buf, int len) {
  1926. if (len <= 0) {
  1927. return NULL;
  1928. }
  1929. buf[0] = '\0';
  1930. return buf;
  1931. }
  1932. void SSL_set_verify_result(SSL *ssl, long result) {
  1933. if (result != X509_V_OK) {
  1934. abort();
  1935. }
  1936. }
  1937. long SSL_get_verify_result(const SSL *ssl) {
  1938. SSL_SESSION *session = SSL_get_session(ssl);
  1939. if (session == NULL) {
  1940. return X509_V_ERR_INVALID_CALL;
  1941. }
  1942. return session->verify_result;
  1943. }
  1944. int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  1945. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
  1946. int index;
  1947. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
  1948. dup_func, free_func)) {
  1949. return -1;
  1950. }
  1951. return index;
  1952. }
  1953. int SSL_set_ex_data(SSL *ssl, int idx, void *arg) {
  1954. return CRYPTO_set_ex_data(&ssl->ex_data, idx, arg);
  1955. }
  1956. void *SSL_get_ex_data(const SSL *ssl, int idx) {
  1957. return CRYPTO_get_ex_data(&ssl->ex_data, idx);
  1958. }
  1959. int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  1960. CRYPTO_EX_dup *dup_func,
  1961. CRYPTO_EX_free *free_func) {
  1962. int index;
  1963. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
  1964. dup_func, free_func)) {
  1965. return -1;
  1966. }
  1967. return index;
  1968. }
  1969. int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *arg) {
  1970. return CRYPTO_set_ex_data(&ctx->ex_data, idx, arg);
  1971. }
  1972. void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) {
  1973. return CRYPTO_get_ex_data(&ctx->ex_data, idx);
  1974. }
  1975. X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
  1976. return ctx->cert_store;
  1977. }
  1978. void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
  1979. X509_STORE_free(ctx->cert_store);
  1980. ctx->cert_store = store;
  1981. }
  1982. int SSL_want(const SSL *ssl) { return ssl->rwstate; }
  1983. void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
  1984. RSA *(*cb)(SSL *ssl, int is_export,
  1985. int keylength)) {
  1986. }
  1987. void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
  1988. int keylength)) {
  1989. }
  1990. void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
  1991. DH *(*callback)(SSL *ssl, int is_export,
  1992. int keylength)) {
  1993. ctx->cert->dh_tmp_cb = callback;
  1994. }
  1995. void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
  1996. int keylength)) {
  1997. ssl->cert->dh_tmp_cb = callback;
  1998. }
  1999. int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
  2000. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  2001. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  2002. return 0;
  2003. }
  2004. OPENSSL_free(ctx->psk_identity_hint);
  2005. if (identity_hint != NULL) {
  2006. ctx->psk_identity_hint = BUF_strdup(identity_hint);
  2007. if (ctx->psk_identity_hint == NULL) {
  2008. return 0;
  2009. }
  2010. } else {
  2011. ctx->psk_identity_hint = NULL;
  2012. }
  2013. return 1;
  2014. }
  2015. int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) {
  2016. if (ssl == NULL) {
  2017. return 0;
  2018. }
  2019. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  2020. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  2021. return 0;
  2022. }
  2023. /* Clear currently configured hint, if any. */
  2024. OPENSSL_free(ssl->psk_identity_hint);
  2025. ssl->psk_identity_hint = NULL;
  2026. /* Treat the empty hint as not supplying one. Plain PSK makes it possible to
  2027. * send either no hint (omit ServerKeyExchange) or an empty hint, while
  2028. * ECDHE_PSK can only spell empty hint. Having different capabilities is odd,
  2029. * so we interpret empty and missing as identical. */
  2030. if (identity_hint != NULL && identity_hint[0] != '\0') {
  2031. ssl->psk_identity_hint = BUF_strdup(identity_hint);
  2032. if (ssl->psk_identity_hint == NULL) {
  2033. return 0;
  2034. }
  2035. }
  2036. return 1;
  2037. }
  2038. const char *SSL_get_psk_identity_hint(const SSL *ssl) {
  2039. if (ssl == NULL) {
  2040. return NULL;
  2041. }
  2042. return ssl->psk_identity_hint;
  2043. }
  2044. const char *SSL_get_psk_identity(const SSL *ssl) {
  2045. if (ssl == NULL) {
  2046. return NULL;
  2047. }
  2048. SSL_SESSION *session = SSL_get_session(ssl);
  2049. if (session == NULL) {
  2050. return NULL;
  2051. }
  2052. return session->psk_identity;
  2053. }
  2054. void SSL_set_psk_client_callback(
  2055. SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
  2056. unsigned max_identity_len, uint8_t *psk,
  2057. unsigned max_psk_len)) {
  2058. ssl->psk_client_callback = cb;
  2059. }
  2060. void SSL_CTX_set_psk_client_callback(
  2061. SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
  2062. unsigned max_identity_len, uint8_t *psk,
  2063. unsigned max_psk_len)) {
  2064. ctx->psk_client_callback = cb;
  2065. }
  2066. void SSL_set_psk_server_callback(
  2067. SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
  2068. unsigned max_psk_len)) {
  2069. ssl->psk_server_callback = cb;
  2070. }
  2071. void SSL_CTX_set_psk_server_callback(
  2072. SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity,
  2073. uint8_t *psk, unsigned max_psk_len)) {
  2074. ctx->psk_server_callback = cb;
  2075. }
  2076. void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
  2077. void (*cb)(int write_p, int version,
  2078. int content_type, const void *buf,
  2079. size_t len, SSL *ssl, void *arg)) {
  2080. ctx->msg_callback = cb;
  2081. }
  2082. void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
  2083. ctx->msg_callback_arg = arg;
  2084. }
  2085. void SSL_set_msg_callback(SSL *ssl,
  2086. void (*cb)(int write_p, int version, int content_type,
  2087. const void *buf, size_t len, SSL *ssl,
  2088. void *arg)) {
  2089. ssl->msg_callback = cb;
  2090. }
  2091. void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
  2092. ssl->msg_callback_arg = arg;
  2093. }
  2094. void SSL_CTX_set_keylog_callback(SSL_CTX *ctx,
  2095. void (*cb)(const SSL *ssl, const char *line)) {
  2096. ctx->keylog_callback = cb;
  2097. }
  2098. void (*SSL_CTX_get_keylog_callback(const SSL_CTX *ctx))(const SSL *ssl,
  2099. const char *line) {
  2100. return ctx->keylog_callback;
  2101. }
  2102. void SSL_CTX_set_current_time_cb(SSL_CTX *ctx,
  2103. void (*cb)(const SSL *ssl,
  2104. struct timeval *out_clock)) {
  2105. ctx->current_time_cb = cb;
  2106. }
  2107. static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
  2108. static const char hextable[] = "0123456789abcdef";
  2109. uint8_t *out;
  2110. if (!CBB_add_space(cbb, &out, in_len * 2)) {
  2111. return 0;
  2112. }
  2113. for (size_t i = 0; i < in_len; i++) {
  2114. *(out++) = (uint8_t)hextable[in[i] >> 4];
  2115. *(out++) = (uint8_t)hextable[in[i] & 0xf];
  2116. }
  2117. return 1;
  2118. }
  2119. int ssl_log_rsa_client_key_exchange(const SSL *ssl,
  2120. const uint8_t *encrypted_premaster,
  2121. size_t encrypted_premaster_len,
  2122. const uint8_t *premaster,
  2123. size_t premaster_len) {
  2124. if (ssl->ctx->keylog_callback == NULL) {
  2125. return 1;
  2126. }
  2127. if (encrypted_premaster_len < 8) {
  2128. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2129. return 0;
  2130. }
  2131. CBB cbb;
  2132. uint8_t *out;
  2133. size_t out_len;
  2134. if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1) ||
  2135. !CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
  2136. /* Only the first 8 bytes of the encrypted premaster secret are
  2137. * logged. */
  2138. !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
  2139. !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
  2140. !cbb_add_hex(&cbb, premaster, premaster_len) ||
  2141. !CBB_add_u8(&cbb, 0 /* NUL */) ||
  2142. !CBB_finish(&cbb, &out, &out_len)) {
  2143. CBB_cleanup(&cbb);
  2144. return 0;
  2145. }
  2146. ssl->ctx->keylog_callback(ssl, (const char *)out);
  2147. OPENSSL_free(out);
  2148. return 1;
  2149. }
  2150. int ssl_log_secret(const SSL *ssl, const char *label, const uint8_t *secret,
  2151. size_t secret_len) {
  2152. if (ssl->ctx->keylog_callback == NULL) {
  2153. return 1;
  2154. }
  2155. CBB cbb;
  2156. uint8_t *out;
  2157. size_t out_len;
  2158. if (!CBB_init(&cbb, strlen(label) + 1 + SSL3_RANDOM_SIZE * 2 + 1 +
  2159. secret_len * 2 + 1) ||
  2160. !CBB_add_bytes(&cbb, (const uint8_t *)label, strlen(label)) ||
  2161. !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
  2162. !cbb_add_hex(&cbb, ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
  2163. !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
  2164. !cbb_add_hex(&cbb, secret, secret_len) ||
  2165. !CBB_add_u8(&cbb, 0 /* NUL */) ||
  2166. !CBB_finish(&cbb, &out, &out_len)) {
  2167. CBB_cleanup(&cbb);
  2168. return 0;
  2169. }
  2170. ssl->ctx->keylog_callback(ssl, (const char *)out);
  2171. OPENSSL_free(out);
  2172. return 1;
  2173. }
  2174. int SSL_is_init_finished(const SSL *ssl) {
  2175. return SSL_state(ssl) == SSL_ST_OK;
  2176. }
  2177. int SSL_in_init(const SSL *ssl) {
  2178. return (SSL_state(ssl) & SSL_ST_INIT) != 0;
  2179. }
  2180. int SSL_in_false_start(const SSL *ssl) {
  2181. if (ssl->s3->hs == NULL) {
  2182. return 0;
  2183. }
  2184. return ssl->s3->hs->in_false_start;
  2185. }
  2186. int SSL_cutthrough_complete(const SSL *ssl) {
  2187. return SSL_in_false_start(ssl);
  2188. }
  2189. void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
  2190. size_t *ssl_session_size) {
  2191. *ssl_size = sizeof(SSL);
  2192. *ssl_ctx_size = sizeof(SSL_CTX);
  2193. *ssl_session_size = sizeof(SSL_SESSION);
  2194. }
  2195. int ssl3_can_false_start(const SSL *ssl) {
  2196. const SSL_CIPHER *const cipher = SSL_get_current_cipher(ssl);
  2197. /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
  2198. return !SSL_is_dtls(ssl) &&
  2199. SSL_version(ssl) == TLS1_2_VERSION &&
  2200. (ssl->s3->alpn_selected != NULL ||
  2201. ssl->s3->next_proto_negotiated != NULL) &&
  2202. cipher != NULL &&
  2203. cipher->algorithm_mkey == SSL_kECDHE &&
  2204. cipher->algorithm_mac == SSL_AEAD;
  2205. }
  2206. const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
  2207. switch (version) {
  2208. case SSL3_VERSION:
  2209. return &SSLv3_enc_data;
  2210. case TLS1_VERSION:
  2211. case TLS1_1_VERSION:
  2212. case TLS1_2_VERSION:
  2213. case TLS1_3_VERSION:
  2214. return &TLSv1_enc_data;
  2215. default:
  2216. return NULL;
  2217. }
  2218. }
  2219. const struct {
  2220. uint16_t version;
  2221. uint32_t flag;
  2222. } kVersions[] = {
  2223. {SSL3_VERSION, SSL_OP_NO_SSLv3},
  2224. {TLS1_VERSION, SSL_OP_NO_TLSv1},
  2225. {TLS1_1_VERSION, SSL_OP_NO_TLSv1_1},
  2226. {TLS1_2_VERSION, SSL_OP_NO_TLSv1_2},
  2227. {TLS1_3_VERSION, SSL_OP_NO_TLSv1_3},
  2228. };
  2229. static const size_t kVersionsLen = OPENSSL_ARRAY_SIZE(kVersions);
  2230. int ssl_get_version_range(const SSL *ssl, uint16_t *out_min_version,
  2231. uint16_t *out_max_version) {
  2232. /* For historical reasons, |SSL_OP_NO_DTLSv1| aliases |SSL_OP_NO_TLSv1|, but
  2233. * DTLS 1.0 should be mapped to TLS 1.1. */
  2234. uint32_t options = ssl->options;
  2235. if (SSL_is_dtls(ssl)) {
  2236. options &= ~SSL_OP_NO_TLSv1_1;
  2237. if (options & SSL_OP_NO_DTLSv1) {
  2238. options |= SSL_OP_NO_TLSv1_1;
  2239. }
  2240. }
  2241. uint16_t min_version = ssl->min_version;
  2242. uint16_t max_version = ssl->max_version;
  2243. /* Bound the range to only those implemented in this protocol. */
  2244. if (min_version < ssl->method->min_version) {
  2245. min_version = ssl->method->min_version;
  2246. }
  2247. if (max_version > ssl->method->max_version) {
  2248. max_version = ssl->method->max_version;
  2249. }
  2250. /* OpenSSL's API for controlling versions entails blacklisting individual
  2251. * protocols. This has two problems. First, on the client, the protocol can
  2252. * only express a contiguous range of versions. Second, a library consumer
  2253. * trying to set a maximum version cannot disable protocol versions that get
  2254. * added in a future version of the library.
  2255. *
  2256. * To account for both of these, OpenSSL interprets the client-side bitmask
  2257. * as a min/max range by picking the lowest contiguous non-empty range of
  2258. * enabled protocols. Note that this means it is impossible to set a maximum
  2259. * version of the higest supported TLS version in a future-proof way. */
  2260. int any_enabled = 0;
  2261. for (size_t i = 0; i < kVersionsLen; i++) {
  2262. /* Only look at the versions already enabled. */
  2263. if (min_version > kVersions[i].version) {
  2264. continue;
  2265. }
  2266. if (max_version < kVersions[i].version) {
  2267. break;
  2268. }
  2269. if (!(options & kVersions[i].flag)) {
  2270. /* The minimum version is the first enabled version. */
  2271. if (!any_enabled) {
  2272. any_enabled = 1;
  2273. min_version = kVersions[i].version;
  2274. }
  2275. continue;
  2276. }
  2277. /* If there is a disabled version after the first enabled one, all versions
  2278. * after it are implicitly disabled. */
  2279. if (any_enabled) {
  2280. max_version = kVersions[i-1].version;
  2281. break;
  2282. }
  2283. }
  2284. if (!any_enabled) {
  2285. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SSL_VERSION);
  2286. return 0;
  2287. }
  2288. *out_min_version = min_version;
  2289. *out_max_version = max_version;
  2290. return 1;
  2291. }
  2292. uint16_t ssl3_protocol_version(const SSL *ssl) {
  2293. assert(ssl->s3->have_version);
  2294. uint16_t version;
  2295. if (!ssl->method->version_from_wire(&version, ssl->version)) {
  2296. /* TODO(davidben): Use the internal version representation for ssl->version
  2297. * and map to the public API representation at API boundaries. */
  2298. assert(0);
  2299. return 0;
  2300. }
  2301. return version;
  2302. }
  2303. int SSL_is_server(const SSL *ssl) { return ssl->server; }
  2304. int SSL_is_dtls(const SSL *ssl) { return ssl->method->is_dtls; }
  2305. void SSL_CTX_set_select_certificate_cb(SSL_CTX *ctx,
  2306. int (*cb)(const SSL_CLIENT_HELLO *)) {
  2307. ctx->select_certificate_cb = cb;
  2308. }
  2309. void SSL_CTX_set_dos_protection_cb(SSL_CTX *ctx,
  2310. int (*cb)(const SSL_CLIENT_HELLO *)) {
  2311. ctx->dos_protection_cb = cb;
  2312. }
  2313. void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) {
  2314. ssl->renegotiate_mode = mode;
  2315. }
  2316. int SSL_get_ivs(const SSL *ssl, const uint8_t **out_read_iv,
  2317. const uint8_t **out_write_iv, size_t *out_iv_len) {
  2318. if (ssl->s3->aead_read_ctx == NULL || ssl->s3->aead_write_ctx == NULL) {
  2319. return 0;
  2320. }
  2321. size_t write_iv_len;
  2322. if (!EVP_AEAD_CTX_get_iv(&ssl->s3->aead_read_ctx->ctx, out_read_iv,
  2323. out_iv_len) ||
  2324. !EVP_AEAD_CTX_get_iv(&ssl->s3->aead_write_ctx->ctx, out_write_iv,
  2325. &write_iv_len) ||
  2326. *out_iv_len != write_iv_len) {
  2327. return 0;
  2328. }
  2329. return 1;
  2330. }
  2331. static uint64_t be_to_u64(const uint8_t in[8]) {
  2332. return (((uint64_t)in[0]) << 56) | (((uint64_t)in[1]) << 48) |
  2333. (((uint64_t)in[2]) << 40) | (((uint64_t)in[3]) << 32) |
  2334. (((uint64_t)in[4]) << 24) | (((uint64_t)in[5]) << 16) |
  2335. (((uint64_t)in[6]) << 8) | ((uint64_t)in[7]);
  2336. }
  2337. uint64_t SSL_get_read_sequence(const SSL *ssl) {
  2338. /* TODO(davidben): Internally represent sequence numbers as uint64_t. */
  2339. if (SSL_is_dtls(ssl)) {
  2340. /* max_seq_num already includes the epoch. */
  2341. assert(ssl->d1->r_epoch == (ssl->d1->bitmap.max_seq_num >> 48));
  2342. return ssl->d1->bitmap.max_seq_num;
  2343. }
  2344. return be_to_u64(ssl->s3->read_sequence);
  2345. }
  2346. uint64_t SSL_get_write_sequence(const SSL *ssl) {
  2347. uint64_t ret = be_to_u64(ssl->s3->write_sequence);
  2348. if (SSL_is_dtls(ssl)) {
  2349. assert((ret >> 48) == 0);
  2350. ret |= ((uint64_t)ssl->d1->w_epoch) << 48;
  2351. }
  2352. return ret;
  2353. }
  2354. uint16_t SSL_get_peer_signature_algorithm(const SSL *ssl) {
  2355. return ssl->s3->tmp.peer_signature_algorithm;
  2356. }
  2357. size_t SSL_get_client_random(const SSL *ssl, uint8_t *out, size_t max_out) {
  2358. if (max_out == 0) {
  2359. return sizeof(ssl->s3->client_random);
  2360. }
  2361. if (max_out > sizeof(ssl->s3->client_random)) {
  2362. max_out = sizeof(ssl->s3->client_random);
  2363. }
  2364. memcpy(out, ssl->s3->client_random, max_out);
  2365. return max_out;
  2366. }
  2367. size_t SSL_get_server_random(const SSL *ssl, uint8_t *out, size_t max_out) {
  2368. if (max_out == 0) {
  2369. return sizeof(ssl->s3->server_random);
  2370. }
  2371. if (max_out > sizeof(ssl->s3->server_random)) {
  2372. max_out = sizeof(ssl->s3->server_random);
  2373. }
  2374. memcpy(out, ssl->s3->server_random, max_out);
  2375. return max_out;
  2376. }
  2377. const SSL_CIPHER *SSL_get_pending_cipher(const SSL *ssl) {
  2378. if (!SSL_in_init(ssl)) {
  2379. return NULL;
  2380. }
  2381. return ssl->s3->tmp.new_cipher;
  2382. }
  2383. void SSL_set_retain_only_sha256_of_client_certs(SSL *ssl, int enabled) {
  2384. ssl->retain_only_sha256_of_client_certs = !!enabled;
  2385. }
  2386. void SSL_CTX_set_retain_only_sha256_of_client_certs(SSL_CTX *ctx, int enabled) {
  2387. ctx->retain_only_sha256_of_client_certs = !!enabled;
  2388. }
  2389. void SSL_CTX_set_grease_enabled(SSL_CTX *ctx, int enabled) {
  2390. ctx->grease_enabled = !!enabled;
  2391. }
  2392. int SSL_clear(SSL *ssl) {
  2393. /* In OpenSSL, reusing a client |SSL| with |SSL_clear| causes the previously
  2394. * established session to be offered the next time around. wpa_supplicant
  2395. * depends on this behavior, so emulate it. */
  2396. SSL_SESSION *session = NULL;
  2397. if (!ssl->server && ssl->s3->established_session != NULL) {
  2398. session = ssl->s3->established_session;
  2399. SSL_SESSION_up_ref(session);
  2400. }
  2401. /* TODO(davidben): Some state on |ssl| is reset both in |SSL_new| and
  2402. * |SSL_clear| because it is per-connection state rather than configuration
  2403. * state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is
  2404. * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
  2405. * |ssl3_new|. */
  2406. ssl->rwstate = SSL_NOTHING;
  2407. BUF_MEM_free(ssl->init_buf);
  2408. ssl->init_buf = NULL;
  2409. ssl->init_msg = NULL;
  2410. ssl->init_num = 0;
  2411. /* The ssl->d1->mtu is simultaneously configuration (preserved across
  2412. * clear) and connection-specific state (gets reset).
  2413. *
  2414. * TODO(davidben): Avoid this. */
  2415. unsigned mtu = 0;
  2416. if (ssl->d1 != NULL) {
  2417. mtu = ssl->d1->mtu;
  2418. }
  2419. ssl->method->ssl_free(ssl);
  2420. if (!ssl->method->ssl_new(ssl)) {
  2421. SSL_SESSION_free(session);
  2422. return 0;
  2423. }
  2424. if (SSL_is_dtls(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
  2425. ssl->d1->mtu = mtu;
  2426. }
  2427. if (session != NULL) {
  2428. SSL_set_session(ssl, session);
  2429. SSL_SESSION_free(session);
  2430. }
  2431. return 1;
  2432. }
  2433. void ssl_do_info_callback(const SSL *ssl, int type, int value) {
  2434. void (*cb)(const SSL *ssl, int type, int value) = NULL;
  2435. if (ssl->info_callback != NULL) {
  2436. cb = ssl->info_callback;
  2437. } else if (ssl->ctx->info_callback != NULL) {
  2438. cb = ssl->ctx->info_callback;
  2439. }
  2440. if (cb != NULL) {
  2441. cb(ssl, type, value);
  2442. }
  2443. }
  2444. void ssl_do_msg_callback(SSL *ssl, int is_write, int content_type,
  2445. const void *buf, size_t len) {
  2446. if (ssl->msg_callback == NULL) {
  2447. return;
  2448. }
  2449. /* |version| is zero when calling for |SSL3_RT_HEADER| and |SSL2_VERSION| for
  2450. * a V2ClientHello. */
  2451. int version;
  2452. switch (content_type) {
  2453. case 0:
  2454. /* V2ClientHello */
  2455. version = SSL2_VERSION;
  2456. break;
  2457. case SSL3_RT_HEADER:
  2458. version = 0;
  2459. break;
  2460. default:
  2461. version = SSL_version(ssl);
  2462. }
  2463. ssl->msg_callback(is_write, version, content_type, buf, len, ssl,
  2464. ssl->msg_callback_arg);
  2465. }
  2466. int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
  2467. int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
  2468. int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
  2469. int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
  2470. int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
  2471. int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
  2472. int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
  2473. int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
  2474. int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
  2475. int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
  2476. int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
  2477. int SSL_num_renegotiations(const SSL *ssl) {
  2478. return SSL_total_renegotiations(ssl);
  2479. }
  2480. int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) { return 0; }
  2481. int SSL_need_tmp_RSA(const SSL *ssl) { return 0; }
  2482. int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) { return 1; }
  2483. int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) { return 1; }
  2484. void ERR_load_SSL_strings(void) {}
  2485. void SSL_load_error_strings(void) {}
  2486. int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); }
  2487. int SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, const EC_KEY *ec_key) {
  2488. if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
  2489. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  2490. return 0;
  2491. }
  2492. int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
  2493. return SSL_CTX_set1_curves(ctx, &nid, 1);
  2494. }
  2495. int SSL_set_tmp_ecdh(SSL *ssl, const EC_KEY *ec_key) {
  2496. if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
  2497. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  2498. return 0;
  2499. }
  2500. int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
  2501. return SSL_set1_curves(ssl, &nid, 1);
  2502. }
  2503. void ssl_get_current_time(const SSL *ssl, struct timeval *out_clock) {
  2504. if (ssl->ctx->current_time_cb != NULL) {
  2505. ssl->ctx->current_time_cb(ssl, out_clock);
  2506. return;
  2507. }
  2508. #if defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
  2509. out_clock->tv_sec = 1234;
  2510. out_clock->tv_usec = 1234;
  2511. #elif defined(OPENSSL_WINDOWS)
  2512. struct _timeb time;
  2513. _ftime(&time);
  2514. out_clock->tv_sec = time.time;
  2515. out_clock->tv_usec = time.millitm * 1000;
  2516. #else
  2517. gettimeofday(out_clock, NULL);
  2518. #endif
  2519. }
  2520. int SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
  2521. return SSL_CTX_set_min_proto_version(ctx, version);
  2522. }
  2523. int SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
  2524. return SSL_CTX_set_max_proto_version(ctx, version);
  2525. }
  2526. int SSL_set_min_version(SSL *ssl, uint16_t version) {
  2527. return SSL_set_min_proto_version(ssl, version);
  2528. }
  2529. int SSL_set_max_version(SSL *ssl, uint16_t version) {
  2530. return SSL_set_max_proto_version(ssl, version);
  2531. }