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

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