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

1213 lines
40 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-2006 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 2005 Nokia. All rights reserved.
  112. *
  113. * The portions of the attached software ("Contribution") is developed by
  114. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  115. * license.
  116. *
  117. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  118. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  119. * support (see RFC 4279) to OpenSSL.
  120. *
  121. * No patent licenses or other rights except those expressly stated in
  122. * the OpenSSL open source license shall be deemed granted or received
  123. * expressly, by implication, estoppel, or otherwise.
  124. *
  125. * No assurances are provided by Nokia that the Contribution does not
  126. * infringe the patent or other intellectual property rights of any third
  127. * party or that the license provides you with all the necessary rights
  128. * to make use of the Contribution.
  129. *
  130. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  131. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  132. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  133. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  134. * OTHERWISE. */
  135. #include <openssl/ssl.h>
  136. #include <assert.h>
  137. #include <stdlib.h>
  138. #include <string.h>
  139. #include <utility>
  140. #include <openssl/err.h>
  141. #include <openssl/hmac.h>
  142. #include <openssl/lhash.h>
  143. #include <openssl/mem.h>
  144. #include <openssl/rand.h>
  145. #include "internal.h"
  146. #include "../crypto/internal.h"
  147. namespace bssl {
  148. // The address of this is a magic value, a pointer to which is returned by
  149. // SSL_magic_pending_session_ptr(). It allows a session callback to indicate
  150. // that it needs to asynchronously fetch session information.
  151. static const char g_pending_session_magic = 0;
  152. static CRYPTO_EX_DATA_CLASS g_ex_data_class =
  153. CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
  154. static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *session);
  155. static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *session);
  156. static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *session, int lock);
  157. UniquePtr<SSL_SESSION> ssl_session_new(const SSL_X509_METHOD *x509_method) {
  158. UniquePtr<SSL_SESSION> session(
  159. (SSL_SESSION *)OPENSSL_malloc(sizeof(SSL_SESSION)));
  160. if (!session) {
  161. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  162. return 0;
  163. }
  164. OPENSSL_memset(session.get(), 0, sizeof(SSL_SESSION));
  165. session->x509_method = x509_method;
  166. session->verify_result = X509_V_ERR_INVALID_CALL;
  167. session->references = 1;
  168. session->timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  169. session->auth_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  170. session->time = time(NULL);
  171. CRYPTO_new_ex_data(&session->ex_data);
  172. return session;
  173. }
  174. UniquePtr<SSL_SESSION> SSL_SESSION_dup(SSL_SESSION *session, int dup_flags) {
  175. UniquePtr<SSL_SESSION> new_session = ssl_session_new(session->x509_method);
  176. if (!new_session) {
  177. return nullptr;
  178. }
  179. new_session->is_server = session->is_server;
  180. new_session->ssl_version = session->ssl_version;
  181. new_session->sid_ctx_length = session->sid_ctx_length;
  182. OPENSSL_memcpy(new_session->sid_ctx, session->sid_ctx, session->sid_ctx_length);
  183. // Copy the key material.
  184. new_session->master_key_length = session->master_key_length;
  185. OPENSSL_memcpy(new_session->master_key, session->master_key,
  186. session->master_key_length);
  187. new_session->cipher = session->cipher;
  188. // Copy authentication state.
  189. if (session->psk_identity != NULL) {
  190. new_session->psk_identity = BUF_strdup(session->psk_identity);
  191. if (new_session->psk_identity == NULL) {
  192. return nullptr;
  193. }
  194. }
  195. if (session->certs != NULL) {
  196. new_session->certs = sk_CRYPTO_BUFFER_new_null();
  197. if (new_session->certs == NULL) {
  198. return nullptr;
  199. }
  200. for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(session->certs); i++) {
  201. CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(session->certs, i);
  202. if (!sk_CRYPTO_BUFFER_push(new_session->certs, buffer)) {
  203. return nullptr;
  204. }
  205. CRYPTO_BUFFER_up_ref(buffer);
  206. }
  207. }
  208. if (!session->x509_method->session_dup(new_session.get(), session)) {
  209. return nullptr;
  210. }
  211. new_session->verify_result = session->verify_result;
  212. if (session->ocsp_response != NULL) {
  213. new_session->ocsp_response = session->ocsp_response;
  214. CRYPTO_BUFFER_up_ref(new_session->ocsp_response);
  215. }
  216. if (session->signed_cert_timestamp_list != NULL) {
  217. new_session->signed_cert_timestamp_list =
  218. session->signed_cert_timestamp_list;
  219. CRYPTO_BUFFER_up_ref(new_session->signed_cert_timestamp_list);
  220. }
  221. OPENSSL_memcpy(new_session->peer_sha256, session->peer_sha256,
  222. SHA256_DIGEST_LENGTH);
  223. new_session->peer_sha256_valid = session->peer_sha256_valid;
  224. new_session->peer_signature_algorithm = session->peer_signature_algorithm;
  225. new_session->timeout = session->timeout;
  226. new_session->auth_timeout = session->auth_timeout;
  227. new_session->time = session->time;
  228. // Copy non-authentication connection properties.
  229. if (dup_flags & SSL_SESSION_INCLUDE_NONAUTH) {
  230. new_session->session_id_length = session->session_id_length;
  231. OPENSSL_memcpy(new_session->session_id, session->session_id,
  232. session->session_id_length);
  233. new_session->group_id = session->group_id;
  234. OPENSSL_memcpy(new_session->original_handshake_hash,
  235. session->original_handshake_hash,
  236. session->original_handshake_hash_len);
  237. new_session->original_handshake_hash_len =
  238. session->original_handshake_hash_len;
  239. new_session->tlsext_tick_lifetime_hint = session->tlsext_tick_lifetime_hint;
  240. new_session->ticket_age_add = session->ticket_age_add;
  241. new_session->ticket_max_early_data = session->ticket_max_early_data;
  242. new_session->extended_master_secret = session->extended_master_secret;
  243. if (session->early_alpn != NULL) {
  244. new_session->early_alpn =
  245. (uint8_t *)BUF_memdup(session->early_alpn, session->early_alpn_len);
  246. if (new_session->early_alpn == NULL) {
  247. return nullptr;
  248. }
  249. }
  250. new_session->early_alpn_len = session->early_alpn_len;
  251. }
  252. // Copy the ticket.
  253. if (dup_flags & SSL_SESSION_INCLUDE_TICKET) {
  254. if (session->tlsext_tick != NULL) {
  255. new_session->tlsext_tick =
  256. (uint8_t *)BUF_memdup(session->tlsext_tick, session->tlsext_ticklen);
  257. if (new_session->tlsext_tick == NULL) {
  258. return nullptr;
  259. }
  260. }
  261. new_session->tlsext_ticklen = session->tlsext_ticklen;
  262. }
  263. // The new_session does not get a copy of the ex_data.
  264. new_session->not_resumable = 1;
  265. return new_session;
  266. }
  267. void ssl_session_rebase_time(SSL *ssl, SSL_SESSION *session) {
  268. struct OPENSSL_timeval now;
  269. ssl_get_current_time(ssl, &now);
  270. // To avoid overflows and underflows, if we've gone back in time, update the
  271. // time, but mark the session expired.
  272. if (session->time > now.tv_sec) {
  273. session->time = now.tv_sec;
  274. session->timeout = 0;
  275. session->auth_timeout = 0;
  276. return;
  277. }
  278. // Adjust the session time and timeouts. If the session has already expired,
  279. // clamp the timeouts at zero.
  280. uint64_t delta = now.tv_sec - session->time;
  281. session->time = now.tv_sec;
  282. if (session->timeout < delta) {
  283. session->timeout = 0;
  284. } else {
  285. session->timeout -= delta;
  286. }
  287. if (session->auth_timeout < delta) {
  288. session->auth_timeout = 0;
  289. } else {
  290. session->auth_timeout -= delta;
  291. }
  292. }
  293. void ssl_session_renew_timeout(SSL *ssl, SSL_SESSION *session,
  294. uint32_t timeout) {
  295. // Rebase the timestamp relative to the current time so |timeout| is measured
  296. // correctly.
  297. ssl_session_rebase_time(ssl, session);
  298. if (session->timeout > timeout) {
  299. return;
  300. }
  301. session->timeout = timeout;
  302. if (session->timeout > session->auth_timeout) {
  303. session->timeout = session->auth_timeout;
  304. }
  305. }
  306. uint16_t ssl_session_protocol_version(const SSL_SESSION *session) {
  307. uint16_t ret;
  308. if (!ssl_protocol_version_from_wire(&ret, session->ssl_version)) {
  309. // An |SSL_SESSION| will never have an invalid version. This is enforced by
  310. // the parser.
  311. assert(0);
  312. return 0;
  313. }
  314. return ret;
  315. }
  316. const EVP_MD *ssl_session_get_digest(const SSL_SESSION *session) {
  317. return ssl_get_handshake_digest(ssl_session_protocol_version(session),
  318. session->cipher);
  319. }
  320. int ssl_get_new_session(SSL_HANDSHAKE *hs, int is_server) {
  321. SSL *const ssl = hs->ssl;
  322. if (ssl->mode & SSL_MODE_NO_SESSION_CREATION) {
  323. OPENSSL_PUT_ERROR(SSL, SSL_R_SESSION_MAY_NOT_BE_CREATED);
  324. return 0;
  325. }
  326. UniquePtr<SSL_SESSION> session = ssl_session_new(ssl->ctx->x509_method);
  327. if (session == NULL) {
  328. return 0;
  329. }
  330. session->is_server = is_server;
  331. session->ssl_version = ssl->version;
  332. // Fill in the time from the |SSL_CTX|'s clock.
  333. struct OPENSSL_timeval now;
  334. ssl_get_current_time(ssl, &now);
  335. session->time = now.tv_sec;
  336. uint16_t version = ssl_protocol_version(ssl);
  337. if (version >= TLS1_3_VERSION) {
  338. // TLS 1.3 uses tickets as authenticators, so we are willing to use them for
  339. // longer.
  340. session->timeout = ssl->session_ctx->session_psk_dhe_timeout;
  341. session->auth_timeout = SSL_DEFAULT_SESSION_AUTH_TIMEOUT;
  342. } else {
  343. // TLS 1.2 resumption does not incorporate new key material, so we use a
  344. // much shorter timeout.
  345. session->timeout = ssl->session_ctx->session_timeout;
  346. session->auth_timeout = ssl->session_ctx->session_timeout;
  347. }
  348. if (is_server) {
  349. if (hs->ticket_expected || version >= TLS1_3_VERSION) {
  350. // Don't set session IDs for sessions resumed with tickets. This will keep
  351. // them out of the session cache.
  352. session->session_id_length = 0;
  353. } else {
  354. session->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
  355. if (!RAND_bytes(session->session_id, session->session_id_length)) {
  356. return 0;
  357. }
  358. }
  359. } else {
  360. session->session_id_length = 0;
  361. }
  362. if (ssl->cert->sid_ctx_length > sizeof(session->sid_ctx)) {
  363. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  364. return 0;
  365. }
  366. OPENSSL_memcpy(session->sid_ctx, ssl->cert->sid_ctx,
  367. ssl->cert->sid_ctx_length);
  368. session->sid_ctx_length = ssl->cert->sid_ctx_length;
  369. // The session is marked not resumable until it is completely filled in.
  370. session->not_resumable = 1;
  371. session->verify_result = X509_V_ERR_INVALID_CALL;
  372. hs->new_session = std::move(session);
  373. ssl_set_session(ssl, NULL);
  374. return 1;
  375. }
  376. int ssl_ctx_rotate_ticket_encryption_key(SSL_CTX *ctx) {
  377. OPENSSL_timeval now;
  378. ssl_ctx_get_current_time(ctx, &now);
  379. {
  380. // Avoid acquiring a write lock in the common case (i.e. a non-default key
  381. // is used or the default keys have not expired yet).
  382. MutexReadLock lock(&ctx->lock);
  383. if (ctx->tlsext_ticket_key_current &&
  384. (ctx->tlsext_ticket_key_current->next_rotation_tv_sec == 0 ||
  385. ctx->tlsext_ticket_key_current->next_rotation_tv_sec > now.tv_sec) &&
  386. (!ctx->tlsext_ticket_key_prev ||
  387. ctx->tlsext_ticket_key_prev->next_rotation_tv_sec > now.tv_sec)) {
  388. return 1;
  389. }
  390. }
  391. MutexWriteLock lock(&ctx->lock);
  392. if (!ctx->tlsext_ticket_key_current ||
  393. (ctx->tlsext_ticket_key_current->next_rotation_tv_sec != 0 &&
  394. ctx->tlsext_ticket_key_current->next_rotation_tv_sec <= now.tv_sec)) {
  395. // The current key has not been initialized or it is expired.
  396. auto new_key = bssl::MakeUnique<struct tlsext_ticket_key>();
  397. if (!new_key) {
  398. return 0;
  399. }
  400. OPENSSL_memset(new_key.get(), 0, sizeof(struct tlsext_ticket_key));
  401. if (ctx->tlsext_ticket_key_current) {
  402. // The current key expired. Rotate it to prev and bump up its rotation
  403. // timestamp. Note that even with the new rotation time it may still be
  404. // expired and get droppped below.
  405. ctx->tlsext_ticket_key_current->next_rotation_tv_sec +=
  406. SSL_DEFAULT_TICKET_KEY_ROTATION_INTERVAL;
  407. OPENSSL_free(ctx->tlsext_ticket_key_prev);
  408. ctx->tlsext_ticket_key_prev = ctx->tlsext_ticket_key_current;
  409. }
  410. ctx->tlsext_ticket_key_current = new_key.release();
  411. RAND_bytes(ctx->tlsext_ticket_key_current->name, 16);
  412. RAND_bytes(ctx->tlsext_ticket_key_current->hmac_key, 16);
  413. RAND_bytes(ctx->tlsext_ticket_key_current->aes_key, 16);
  414. ctx->tlsext_ticket_key_current->next_rotation_tv_sec =
  415. now.tv_sec + SSL_DEFAULT_TICKET_KEY_ROTATION_INTERVAL;
  416. }
  417. // Drop an expired prev key.
  418. if (ctx->tlsext_ticket_key_prev &&
  419. ctx->tlsext_ticket_key_prev->next_rotation_tv_sec <= now.tv_sec) {
  420. OPENSSL_free(ctx->tlsext_ticket_key_prev);
  421. ctx->tlsext_ticket_key_prev = nullptr;
  422. }
  423. return 1;
  424. }
  425. static int ssl_encrypt_ticket_with_cipher_ctx(SSL *ssl, CBB *out,
  426. const uint8_t *session_buf,
  427. size_t session_len) {
  428. ScopedEVP_CIPHER_CTX ctx;
  429. ScopedHMAC_CTX hctx;
  430. // If the session is too long, emit a dummy value rather than abort the
  431. // connection.
  432. static const size_t kMaxTicketOverhead =
  433. 16 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE;
  434. if (session_len > 0xffff - kMaxTicketOverhead) {
  435. static const char kTicketPlaceholder[] = "TICKET TOO LARGE";
  436. return CBB_add_bytes(out, (const uint8_t *)kTicketPlaceholder,
  437. strlen(kTicketPlaceholder));
  438. }
  439. // Initialize HMAC and cipher contexts. If callback present it does all the
  440. // work otherwise use generated values from parent ctx.
  441. SSL_CTX *tctx = ssl->session_ctx;
  442. uint8_t iv[EVP_MAX_IV_LENGTH];
  443. uint8_t key_name[16];
  444. if (tctx->tlsext_ticket_key_cb != NULL) {
  445. if (tctx->tlsext_ticket_key_cb(ssl, key_name, iv, ctx.get(), hctx.get(),
  446. 1 /* encrypt */) < 0) {
  447. return 0;
  448. }
  449. } else {
  450. // Rotate ticket key if necessary.
  451. if (!ssl_ctx_rotate_ticket_encryption_key(tctx)) {
  452. return 0;
  453. }
  454. MutexReadLock lock(&tctx->lock);
  455. if (!RAND_bytes(iv, 16) ||
  456. !EVP_EncryptInit_ex(ctx.get(), EVP_aes_128_cbc(), NULL,
  457. tctx->tlsext_ticket_key_current->aes_key, iv) ||
  458. !HMAC_Init_ex(hctx.get(), tctx->tlsext_ticket_key_current->hmac_key, 16,
  459. tlsext_tick_md(), NULL)) {
  460. return 0;
  461. }
  462. OPENSSL_memcpy(key_name, tctx->tlsext_ticket_key_current->name, 16);
  463. }
  464. uint8_t *ptr;
  465. if (!CBB_add_bytes(out, key_name, 16) ||
  466. !CBB_add_bytes(out, iv, EVP_CIPHER_CTX_iv_length(ctx.get())) ||
  467. !CBB_reserve(out, &ptr, session_len + EVP_MAX_BLOCK_LENGTH)) {
  468. return 0;
  469. }
  470. size_t total = 0;
  471. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  472. OPENSSL_memcpy(ptr, session_buf, session_len);
  473. total = session_len;
  474. #else
  475. int len;
  476. if (!EVP_EncryptUpdate(ctx.get(), ptr + total, &len, session_buf, session_len)) {
  477. return 0;
  478. }
  479. total += len;
  480. if (!EVP_EncryptFinal_ex(ctx.get(), ptr + total, &len)) {
  481. return 0;
  482. }
  483. total += len;
  484. #endif
  485. if (!CBB_did_write(out, total)) {
  486. return 0;
  487. }
  488. unsigned hlen;
  489. if (!HMAC_Update(hctx.get(), CBB_data(out), CBB_len(out)) ||
  490. !CBB_reserve(out, &ptr, EVP_MAX_MD_SIZE) ||
  491. !HMAC_Final(hctx.get(), ptr, &hlen) ||
  492. !CBB_did_write(out, hlen)) {
  493. return 0;
  494. }
  495. return 1;
  496. }
  497. static int ssl_encrypt_ticket_with_method(SSL *ssl, CBB *out,
  498. const uint8_t *session_buf,
  499. size_t session_len) {
  500. const SSL_TICKET_AEAD_METHOD *method = ssl->session_ctx->ticket_aead_method;
  501. const size_t max_overhead = method->max_overhead(ssl);
  502. const size_t max_out = session_len + max_overhead;
  503. if (max_out < max_overhead) {
  504. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  505. return 0;
  506. }
  507. uint8_t *ptr;
  508. if (!CBB_reserve(out, &ptr, max_out)) {
  509. return 0;
  510. }
  511. size_t out_len;
  512. if (!method->seal(ssl, ptr, &out_len, max_out, session_buf, session_len)) {
  513. OPENSSL_PUT_ERROR(SSL, SSL_R_TICKET_ENCRYPTION_FAILED);
  514. return 0;
  515. }
  516. if (!CBB_did_write(out, out_len)) {
  517. return 0;
  518. }
  519. return 1;
  520. }
  521. int ssl_encrypt_ticket(SSL *ssl, CBB *out, const SSL_SESSION *session) {
  522. // Serialize the SSL_SESSION to be encoded into the ticket.
  523. uint8_t *session_buf = NULL;
  524. size_t session_len;
  525. if (!SSL_SESSION_to_bytes_for_ticket(session, &session_buf, &session_len)) {
  526. return -1;
  527. }
  528. int ret = 0;
  529. if (ssl->session_ctx->ticket_aead_method) {
  530. ret = ssl_encrypt_ticket_with_method(ssl, out, session_buf, session_len);
  531. } else {
  532. ret =
  533. ssl_encrypt_ticket_with_cipher_ctx(ssl, out, session_buf, session_len);
  534. }
  535. OPENSSL_free(session_buf);
  536. return ret;
  537. }
  538. int ssl_session_is_context_valid(const SSL *ssl, const SSL_SESSION *session) {
  539. if (session == NULL) {
  540. return 0;
  541. }
  542. return session->sid_ctx_length == ssl->cert->sid_ctx_length &&
  543. OPENSSL_memcmp(session->sid_ctx, ssl->cert->sid_ctx,
  544. ssl->cert->sid_ctx_length) == 0;
  545. }
  546. int ssl_session_is_time_valid(const SSL *ssl, const SSL_SESSION *session) {
  547. if (session == NULL) {
  548. return 0;
  549. }
  550. struct OPENSSL_timeval now;
  551. ssl_get_current_time(ssl, &now);
  552. // Reject tickets from the future to avoid underflow.
  553. if (now.tv_sec < session->time) {
  554. return 0;
  555. }
  556. return session->timeout > now.tv_sec - session->time;
  557. }
  558. int ssl_session_is_resumable(const SSL_HANDSHAKE *hs,
  559. const SSL_SESSION *session) {
  560. const SSL *const ssl = hs->ssl;
  561. return ssl_session_is_context_valid(ssl, session) &&
  562. // The session must have been created by the same type of end point as
  563. // we're now using it with.
  564. ssl->server == session->is_server &&
  565. // The session must not be expired.
  566. ssl_session_is_time_valid(ssl, session) &&
  567. /* Only resume if the session's version matches the negotiated
  568. * version. */
  569. ssl->version == session->ssl_version &&
  570. // Only resume if the session's cipher matches the negotiated one.
  571. hs->new_cipher == session->cipher &&
  572. // If the session contains a client certificate (either the full
  573. // certificate or just the hash) then require that the form of the
  574. // certificate matches the current configuration.
  575. ((sk_CRYPTO_BUFFER_num(session->certs) == 0 &&
  576. !session->peer_sha256_valid) ||
  577. session->peer_sha256_valid ==
  578. ssl->retain_only_sha256_of_client_certs);
  579. }
  580. // ssl_lookup_session looks up |session_id| in the session cache and sets
  581. // |*out_session| to an |SSL_SESSION| object if found.
  582. static enum ssl_hs_wait_t ssl_lookup_session(
  583. SSL *ssl, UniquePtr<SSL_SESSION> *out_session, const uint8_t *session_id,
  584. size_t session_id_len) {
  585. out_session->reset();
  586. if (session_id_len == 0 || session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
  587. return ssl_hs_ok;
  588. }
  589. UniquePtr<SSL_SESSION> session;
  590. // Try the internal cache, if it exists.
  591. if (!(ssl->session_ctx->session_cache_mode &
  592. SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) {
  593. SSL_SESSION data;
  594. data.ssl_version = ssl->version;
  595. data.session_id_length = session_id_len;
  596. OPENSSL_memcpy(data.session_id, session_id, session_id_len);
  597. MutexReadLock lock(&ssl->session_ctx->lock);
  598. session.reset(lh_SSL_SESSION_retrieve(ssl->session_ctx->sessions, &data));
  599. if (session) {
  600. // |lh_SSL_SESSION_retrieve| returns a non-owning pointer.
  601. SSL_SESSION_up_ref(session.get());
  602. }
  603. // TODO(davidben): This should probably move it to the front of the list.
  604. }
  605. // Fall back to the external cache, if it exists.
  606. if (!session && ssl->session_ctx->get_session_cb != nullptr) {
  607. int copy = 1;
  608. session.reset(ssl->session_ctx->get_session_cb(ssl, session_id,
  609. session_id_len, &copy));
  610. if (!session) {
  611. return ssl_hs_ok;
  612. }
  613. if (session.get() == SSL_magic_pending_session_ptr()) {
  614. session.release(); // This pointer is not actually owned.
  615. return ssl_hs_pending_session;
  616. }
  617. // Increment reference count now if the session callback asks us to do so
  618. // (note that if the session structures returned by the callback are shared
  619. // between threads, it must handle the reference count itself [i.e. copy ==
  620. // 0], or things won't be thread-safe).
  621. if (copy) {
  622. SSL_SESSION_up_ref(session.get());
  623. }
  624. // Add the externally cached session to the internal cache if necessary.
  625. if (!(ssl->session_ctx->session_cache_mode &
  626. SSL_SESS_CACHE_NO_INTERNAL_STORE)) {
  627. SSL_CTX_add_session(ssl->session_ctx, session.get());
  628. }
  629. }
  630. if (session && !ssl_session_is_time_valid(ssl, session.get())) {
  631. // The session was from the cache, so remove it.
  632. SSL_CTX_remove_session(ssl->session_ctx, session.get());
  633. session.reset();
  634. }
  635. *out_session = std::move(session);
  636. return ssl_hs_ok;
  637. }
  638. enum ssl_hs_wait_t ssl_get_prev_session(SSL *ssl,
  639. UniquePtr<SSL_SESSION> *out_session,
  640. bool *out_tickets_supported,
  641. bool *out_renew_ticket,
  642. const SSL_CLIENT_HELLO *client_hello) {
  643. // This is used only by servers.
  644. assert(ssl->server);
  645. UniquePtr<SSL_SESSION> session;
  646. bool renew_ticket = false;
  647. // If tickets are disabled, always behave as if no tickets are present.
  648. const uint8_t *ticket = NULL;
  649. size_t ticket_len = 0;
  650. const bool tickets_supported =
  651. !(SSL_get_options(ssl) & SSL_OP_NO_TICKET) &&
  652. ssl->version > SSL3_VERSION &&
  653. SSL_early_callback_ctx_extension_get(
  654. client_hello, TLSEXT_TYPE_session_ticket, &ticket, &ticket_len);
  655. if (tickets_supported && ticket_len > 0) {
  656. switch (ssl_process_ticket(ssl, &session, &renew_ticket, ticket, ticket_len,
  657. client_hello->session_id,
  658. client_hello->session_id_len)) {
  659. case ssl_ticket_aead_success:
  660. break;
  661. case ssl_ticket_aead_ignore_ticket:
  662. assert(!session);
  663. break;
  664. case ssl_ticket_aead_error:
  665. return ssl_hs_error;
  666. case ssl_ticket_aead_retry:
  667. return ssl_hs_pending_ticket;
  668. }
  669. } else {
  670. // The client didn't send a ticket, so the session ID is a real ID.
  671. enum ssl_hs_wait_t lookup_ret = ssl_lookup_session(
  672. ssl, &session, client_hello->session_id, client_hello->session_id_len);
  673. if (lookup_ret != ssl_hs_ok) {
  674. return lookup_ret;
  675. }
  676. }
  677. *out_session = std::move(session);
  678. *out_tickets_supported = tickets_supported;
  679. *out_renew_ticket = renew_ticket;
  680. return ssl_hs_ok;
  681. }
  682. static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *session, int lock) {
  683. int ret = 0;
  684. if (session != NULL && session->session_id_length != 0) {
  685. if (lock) {
  686. CRYPTO_MUTEX_lock_write(&ctx->lock);
  687. }
  688. SSL_SESSION *found_session = lh_SSL_SESSION_retrieve(ctx->sessions,
  689. session);
  690. if (found_session == session) {
  691. ret = 1;
  692. found_session = lh_SSL_SESSION_delete(ctx->sessions, session);
  693. SSL_SESSION_list_remove(ctx, session);
  694. }
  695. if (lock) {
  696. CRYPTO_MUTEX_unlock_write(&ctx->lock);
  697. }
  698. if (ret) {
  699. if (ctx->remove_session_cb != NULL) {
  700. ctx->remove_session_cb(ctx, found_session);
  701. }
  702. SSL_SESSION_free(found_session);
  703. }
  704. }
  705. return ret;
  706. }
  707. void ssl_set_session(SSL *ssl, SSL_SESSION *session) {
  708. if (ssl->session == session) {
  709. return;
  710. }
  711. SSL_SESSION_free(ssl->session);
  712. ssl->session = session;
  713. if (session != NULL) {
  714. SSL_SESSION_up_ref(session);
  715. }
  716. }
  717. // locked by SSL_CTX in the calling function
  718. static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *session) {
  719. if (session->next == NULL || session->prev == NULL) {
  720. return;
  721. }
  722. if (session->next == (SSL_SESSION *)&ctx->session_cache_tail) {
  723. // last element in list
  724. if (session->prev == (SSL_SESSION *)&ctx->session_cache_head) {
  725. // only one element in list
  726. ctx->session_cache_head = NULL;
  727. ctx->session_cache_tail = NULL;
  728. } else {
  729. ctx->session_cache_tail = session->prev;
  730. session->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail);
  731. }
  732. } else {
  733. if (session->prev == (SSL_SESSION *)&ctx->session_cache_head) {
  734. // first element in list
  735. ctx->session_cache_head = session->next;
  736. session->next->prev = (SSL_SESSION *)&(ctx->session_cache_head);
  737. } else { // middle of list
  738. session->next->prev = session->prev;
  739. session->prev->next = session->next;
  740. }
  741. }
  742. session->prev = session->next = NULL;
  743. }
  744. static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *session) {
  745. if (session->next != NULL && session->prev != NULL) {
  746. SSL_SESSION_list_remove(ctx, session);
  747. }
  748. if (ctx->session_cache_head == NULL) {
  749. ctx->session_cache_head = session;
  750. ctx->session_cache_tail = session;
  751. session->prev = (SSL_SESSION *)&(ctx->session_cache_head);
  752. session->next = (SSL_SESSION *)&(ctx->session_cache_tail);
  753. } else {
  754. session->next = ctx->session_cache_head;
  755. session->next->prev = session;
  756. session->prev = (SSL_SESSION *)&(ctx->session_cache_head);
  757. ctx->session_cache_head = session;
  758. }
  759. }
  760. } // namespace bssl
  761. using namespace bssl;
  762. SSL_SESSION *SSL_SESSION_new(const SSL_CTX *ctx) {
  763. return ssl_session_new(ctx->x509_method).release();
  764. }
  765. int SSL_SESSION_up_ref(SSL_SESSION *session) {
  766. CRYPTO_refcount_inc(&session->references);
  767. return 1;
  768. }
  769. void SSL_SESSION_free(SSL_SESSION *session) {
  770. if (session == NULL ||
  771. !CRYPTO_refcount_dec_and_test_zero(&session->references)) {
  772. return;
  773. }
  774. CRYPTO_free_ex_data(&g_ex_data_class, session, &session->ex_data);
  775. OPENSSL_cleanse(session->master_key, sizeof(session->master_key));
  776. OPENSSL_cleanse(session->session_id, sizeof(session->session_id));
  777. sk_CRYPTO_BUFFER_pop_free(session->certs, CRYPTO_BUFFER_free);
  778. session->x509_method->session_clear(session);
  779. OPENSSL_free(session->tlsext_tick);
  780. CRYPTO_BUFFER_free(session->signed_cert_timestamp_list);
  781. CRYPTO_BUFFER_free(session->ocsp_response);
  782. OPENSSL_free(session->psk_identity);
  783. OPENSSL_free(session->early_alpn);
  784. OPENSSL_free(session);
  785. }
  786. const uint8_t *SSL_SESSION_get_id(const SSL_SESSION *session,
  787. unsigned *out_len) {
  788. if (out_len != NULL) {
  789. *out_len = session->session_id_length;
  790. }
  791. return session->session_id;
  792. }
  793. uint32_t SSL_SESSION_get_timeout(const SSL_SESSION *session) {
  794. return session->timeout;
  795. }
  796. uint64_t SSL_SESSION_get_time(const SSL_SESSION *session) {
  797. if (session == NULL) {
  798. // NULL should crash, but silently accept it here for compatibility.
  799. return 0;
  800. }
  801. return session->time;
  802. }
  803. X509 *SSL_SESSION_get0_peer(const SSL_SESSION *session) {
  804. return session->x509_peer;
  805. }
  806. size_t SSL_SESSION_get_master_key(const SSL_SESSION *session, uint8_t *out,
  807. size_t max_out) {
  808. // TODO(davidben): Fix master_key_length's type and remove these casts.
  809. if (max_out == 0) {
  810. return (size_t)session->master_key_length;
  811. }
  812. if (max_out > (size_t)session->master_key_length) {
  813. max_out = (size_t)session->master_key_length;
  814. }
  815. OPENSSL_memcpy(out, session->master_key, max_out);
  816. return max_out;
  817. }
  818. uint64_t SSL_SESSION_set_time(SSL_SESSION *session, uint64_t time) {
  819. if (session == NULL) {
  820. return 0;
  821. }
  822. session->time = time;
  823. return time;
  824. }
  825. uint32_t SSL_SESSION_set_timeout(SSL_SESSION *session, uint32_t timeout) {
  826. if (session == NULL) {
  827. return 0;
  828. }
  829. session->timeout = timeout;
  830. session->auth_timeout = timeout;
  831. return 1;
  832. }
  833. int SSL_SESSION_set1_id_context(SSL_SESSION *session, const uint8_t *sid_ctx,
  834. size_t sid_ctx_len) {
  835. if (sid_ctx_len > sizeof(session->sid_ctx)) {
  836. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  837. return 0;
  838. }
  839. static_assert(sizeof(session->sid_ctx) < 256, "sid_ctx_len does not fit");
  840. session->sid_ctx_length = (uint8_t)sid_ctx_len;
  841. OPENSSL_memcpy(session->sid_ctx, sid_ctx, sid_ctx_len);
  842. return 1;
  843. }
  844. int SSL_SESSION_should_be_single_use(const SSL_SESSION *session) {
  845. return ssl_session_protocol_version(session) >= TLS1_3_VERSION;
  846. }
  847. int SSL_SESSION_is_resumable(const SSL_SESSION *session) {
  848. return !session->not_resumable;
  849. }
  850. int SSL_SESSION_has_ticket(const SSL_SESSION *session) {
  851. return session->tlsext_ticklen > 0;
  852. }
  853. void SSL_SESSION_get0_ticket(const SSL_SESSION *session,
  854. const uint8_t **out_ticket, size_t *out_len) {
  855. if (out_ticket != nullptr) {
  856. *out_ticket = session->tlsext_tick;
  857. }
  858. *out_len = session->tlsext_ticklen;
  859. }
  860. uint32_t SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *session) {
  861. return session->tlsext_tick_lifetime_hint;
  862. }
  863. const SSL_CIPHER *SSL_SESSION_get0_cipher(const SSL_SESSION *session) {
  864. return session->cipher;
  865. }
  866. SSL_SESSION *SSL_magic_pending_session_ptr(void) {
  867. return (SSL_SESSION *)&g_pending_session_magic;
  868. }
  869. SSL_SESSION *SSL_get_session(const SSL *ssl) {
  870. // Once the handshake completes we return the established session. Otherwise
  871. // we return the intermediate session, either |session| (for resumption) or
  872. // |new_session| if doing a full handshake.
  873. if (!SSL_in_init(ssl)) {
  874. return ssl->s3->established_session.get();
  875. }
  876. SSL_HANDSHAKE *hs = ssl->s3->hs.get();
  877. if (hs->early_session) {
  878. return hs->early_session.get();
  879. }
  880. if (hs->new_session) {
  881. return hs->new_session.get();
  882. }
  883. return ssl->session;
  884. }
  885. SSL_SESSION *SSL_get1_session(SSL *ssl) {
  886. SSL_SESSION *ret = SSL_get_session(ssl);
  887. if (ret != NULL) {
  888. SSL_SESSION_up_ref(ret);
  889. }
  890. return ret;
  891. }
  892. int SSL_SESSION_get_ex_new_index(long argl, void *argp,
  893. CRYPTO_EX_unused *unused,
  894. CRYPTO_EX_dup *dup_unused,
  895. CRYPTO_EX_free *free_func) {
  896. int index;
  897. if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp,
  898. free_func)) {
  899. return -1;
  900. }
  901. return index;
  902. }
  903. int SSL_SESSION_set_ex_data(SSL_SESSION *session, int idx, void *arg) {
  904. return CRYPTO_set_ex_data(&session->ex_data, idx, arg);
  905. }
  906. void *SSL_SESSION_get_ex_data(const SSL_SESSION *session, int idx) {
  907. return CRYPTO_get_ex_data(&session->ex_data, idx);
  908. }
  909. int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *session) {
  910. // Although |session| is inserted into two structures (a doubly-linked list
  911. // and the hash table), |ctx| only takes one reference.
  912. SSL_SESSION_up_ref(session);
  913. UniquePtr<SSL_SESSION> owned_session(session);
  914. SSL_SESSION *old_session;
  915. MutexWriteLock lock(&ctx->lock);
  916. if (!lh_SSL_SESSION_insert(ctx->sessions, &old_session, session)) {
  917. return 0;
  918. }
  919. // |ctx->sessions| took ownership of |session| and gave us back a reference to
  920. // |old_session|. (|old_session| may be the same as |session|, in which case
  921. // we traded identical references with |ctx->sessions|.)
  922. owned_session.release();
  923. owned_session.reset(old_session);
  924. if (old_session != NULL) {
  925. if (old_session == session) {
  926. // |session| was already in the cache. There are no linked list pointers
  927. // to update.
  928. return 0;
  929. }
  930. // There was a session ID collision. |old_session| was replaced with
  931. // |session| in the hash table, so |old_session| must be removed from the
  932. // linked list to match.
  933. SSL_SESSION_list_remove(ctx, old_session);
  934. }
  935. SSL_SESSION_list_add(ctx, session);
  936. // Enforce any cache size limits.
  937. if (SSL_CTX_sess_get_cache_size(ctx) > 0) {
  938. while (lh_SSL_SESSION_num_items(ctx->sessions) >
  939. SSL_CTX_sess_get_cache_size(ctx)) {
  940. if (!remove_session_lock(ctx, ctx->session_cache_tail, 0)) {
  941. break;
  942. }
  943. }
  944. }
  945. return 1;
  946. }
  947. int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *session) {
  948. return remove_session_lock(ctx, session, 1);
  949. }
  950. int SSL_set_session(SSL *ssl, SSL_SESSION *session) {
  951. // SSL_set_session may only be called before the handshake has started.
  952. if (ssl->s3->initial_handshake_complete ||
  953. ssl->s3->hs == NULL ||
  954. ssl->s3->hs->state != 0) {
  955. abort();
  956. }
  957. ssl_set_session(ssl, session);
  958. return 1;
  959. }
  960. uint32_t SSL_CTX_set_timeout(SSL_CTX *ctx, uint32_t timeout) {
  961. if (ctx == NULL) {
  962. return 0;
  963. }
  964. // Historically, zero was treated as |SSL_DEFAULT_SESSION_TIMEOUT|.
  965. if (timeout == 0) {
  966. timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  967. }
  968. uint32_t old_timeout = ctx->session_timeout;
  969. ctx->session_timeout = timeout;
  970. return old_timeout;
  971. }
  972. uint32_t SSL_CTX_get_timeout(const SSL_CTX *ctx) {
  973. if (ctx == NULL) {
  974. return 0;
  975. }
  976. return ctx->session_timeout;
  977. }
  978. void SSL_CTX_set_session_psk_dhe_timeout(SSL_CTX *ctx, uint32_t timeout) {
  979. ctx->session_psk_dhe_timeout = timeout;
  980. }
  981. typedef struct timeout_param_st {
  982. SSL_CTX *ctx;
  983. uint64_t time;
  984. LHASH_OF(SSL_SESSION) *cache;
  985. } TIMEOUT_PARAM;
  986. static void timeout_doall_arg(SSL_SESSION *session, void *void_param) {
  987. TIMEOUT_PARAM *param = reinterpret_cast<TIMEOUT_PARAM *>(void_param);
  988. if (param->time == 0 ||
  989. session->time + session->timeout < session->time ||
  990. param->time > (session->time + session->timeout)) {
  991. // The reason we don't call SSL_CTX_remove_session() is to
  992. // save on locking overhead
  993. (void) lh_SSL_SESSION_delete(param->cache, session);
  994. SSL_SESSION_list_remove(param->ctx, session);
  995. if (param->ctx->remove_session_cb != NULL) {
  996. param->ctx->remove_session_cb(param->ctx, session);
  997. }
  998. SSL_SESSION_free(session);
  999. }
  1000. }
  1001. void SSL_CTX_flush_sessions(SSL_CTX *ctx, uint64_t time) {
  1002. TIMEOUT_PARAM tp;
  1003. tp.ctx = ctx;
  1004. tp.cache = ctx->sessions;
  1005. if (tp.cache == NULL) {
  1006. return;
  1007. }
  1008. tp.time = time;
  1009. MutexWriteLock lock(&ctx->lock);
  1010. lh_SSL_SESSION_doall_arg(tp.cache, timeout_doall_arg, &tp);
  1011. }
  1012. void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
  1013. int (*cb)(SSL *ssl, SSL_SESSION *session)) {
  1014. ctx->new_session_cb = cb;
  1015. }
  1016. int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *session) {
  1017. return ctx->new_session_cb;
  1018. }
  1019. void SSL_CTX_sess_set_remove_cb(
  1020. SSL_CTX *ctx, void (*cb)(SSL_CTX *ctx, SSL_SESSION *session)) {
  1021. ctx->remove_session_cb = cb;
  1022. }
  1023. void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX *ctx,
  1024. SSL_SESSION *session) {
  1025. return ctx->remove_session_cb;
  1026. }
  1027. void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
  1028. SSL_SESSION *(*cb)(SSL *ssl, const uint8_t *id,
  1029. int id_len, int *out_copy)) {
  1030. ctx->get_session_cb = cb;
  1031. }
  1032. SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl,
  1033. const uint8_t *id,
  1034. int id_len,
  1035. int *out_copy) {
  1036. return ctx->get_session_cb;
  1037. }
  1038. void SSL_CTX_set_info_callback(
  1039. SSL_CTX *ctx, void (*cb)(const SSL *ssl, int type, int value)) {
  1040. ctx->info_callback = cb;
  1041. }
  1042. void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl, int type,
  1043. int value) {
  1044. return ctx->info_callback;
  1045. }
  1046. void SSL_CTX_set_channel_id_cb(SSL_CTX *ctx,
  1047. void (*cb)(SSL *ssl, EVP_PKEY **pkey)) {
  1048. ctx->channel_id_cb = cb;
  1049. }
  1050. void (*SSL_CTX_get_channel_id_cb(SSL_CTX *ctx))(SSL *ssl, EVP_PKEY **pkey) {
  1051. return ctx->channel_id_cb;
  1052. }