Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ssl_session.cc 38 KiB

Revise version negotiation logic on the C side. This is in preparation for upcoming experiments which will require supporting multiple experimental versions of TLS 1.3 with, on the server, the ability to enable multiple variants at once. This means the version <-> wire bijection no longer exists, even when limiting to a single SSL*. Thus version_to_wire is removed and instead we treat the wire version as the canonical version value. There is a mapping from valid wire versions to protocol versions which describe the high-level handshake protocol in use. This mapping is not injective, so uses of version_from_wire are rewritten differently. All the version-munging logic is moved to ssl_versions.c with a master preference list of all TLS and DTLS versions. The legacy version negotiation is converted to the new scheme. The version lists and negotiation are driven by the preference lists and a ssl_supports_version API. To simplify the mess around SSL_SESSION and versions, version_from_wire is now DTLS/TLS-agnostic, with any filtering being done by ssl_supports_version. This is screwy but allows parsing SSL_SESSIONs to sanity-check it and reject all bogus versions in SSL_SESSION. This reduces a mess of error cases. As part of this, the weird logic where ssl->version is set early when sending the ClientHello is removed. The one place where we were relying on this behavior is tweaked to query hs->max_version instead. Change-Id: Ic91b348481ceba94d9ae06d6781187c11adc15b0 Reviewed-on: https://boringssl-review.googlesource.com/17524 Reviewed-by: David Benjamin <davidben@google.com> Commit-Queue: David Benjamin <davidben@google.com>
7 anos atrás
Revise version negotiation logic on the C side. This is in preparation for upcoming experiments which will require supporting multiple experimental versions of TLS 1.3 with, on the server, the ability to enable multiple variants at once. This means the version <-> wire bijection no longer exists, even when limiting to a single SSL*. Thus version_to_wire is removed and instead we treat the wire version as the canonical version value. There is a mapping from valid wire versions to protocol versions which describe the high-level handshake protocol in use. This mapping is not injective, so uses of version_from_wire are rewritten differently. All the version-munging logic is moved to ssl_versions.c with a master preference list of all TLS and DTLS versions. The legacy version negotiation is converted to the new scheme. The version lists and negotiation are driven by the preference lists and a ssl_supports_version API. To simplify the mess around SSL_SESSION and versions, version_from_wire is now DTLS/TLS-agnostic, with any filtering being done by ssl_supports_version. This is screwy but allows parsing SSL_SESSIONs to sanity-check it and reject all bogus versions in SSL_SESSION. This reduces a mess of error cases. As part of this, the weird logic where ssl->version is set early when sending the ClientHello is removed. The one place where we were relying on this behavior is tweaked to query hs->max_version instead. Change-Id: Ic91b348481ceba94d9ae06d6781187c11adc15b0 Reviewed-on: https://boringssl-review.googlesource.com/17524 Reviewed-by: David Benjamin <davidben@google.com> Commit-Queue: David Benjamin <davidben@google.com>
7 anos atrás
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  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 <openssl/err.h>
  140. #include <openssl/lhash.h>
  141. #include <openssl/mem.h>
  142. #include <openssl/rand.h>
  143. #include "internal.h"
  144. #include "../crypto/internal.h"
  145. /* The address of this is a magic value, a pointer to which is returned by
  146. * SSL_magic_pending_session_ptr(). It allows a session callback to indicate
  147. * that it needs to asynchronously fetch session information. */
  148. static const char g_pending_session_magic = 0;
  149. static CRYPTO_EX_DATA_CLASS g_ex_data_class =
  150. CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
  151. static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *session);
  152. static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *session);
  153. static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *session, int lock);
  154. SSL_SESSION *ssl_session_new(const SSL_X509_METHOD *x509_method) {
  155. SSL_SESSION *session = (SSL_SESSION *)OPENSSL_malloc(sizeof(SSL_SESSION));
  156. if (session == NULL) {
  157. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  158. return 0;
  159. }
  160. OPENSSL_memset(session, 0, sizeof(SSL_SESSION));
  161. session->x509_method = x509_method;
  162. session->verify_result = X509_V_ERR_INVALID_CALL;
  163. session->references = 1;
  164. session->timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  165. session->auth_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  166. session->time = time(NULL);
  167. CRYPTO_new_ex_data(&session->ex_data);
  168. return session;
  169. }
  170. SSL_SESSION *SSL_SESSION_new(const SSL_CTX *ctx) {
  171. return ssl_session_new(ctx->x509_method);
  172. }
  173. SSL_SESSION *SSL_SESSION_dup(SSL_SESSION *session, int dup_flags) {
  174. SSL_SESSION *new_session = ssl_session_new(session->x509_method);
  175. if (new_session == NULL) {
  176. goto err;
  177. }
  178. new_session->is_server = session->is_server;
  179. new_session->ssl_version = session->ssl_version;
  180. new_session->sid_ctx_length = session->sid_ctx_length;
  181. OPENSSL_memcpy(new_session->sid_ctx, session->sid_ctx, session->sid_ctx_length);
  182. /* Copy the key material. */
  183. new_session->master_key_length = session->master_key_length;
  184. OPENSSL_memcpy(new_session->master_key, session->master_key,
  185. session->master_key_length);
  186. new_session->cipher = session->cipher;
  187. /* Copy authentication state. */
  188. if (session->psk_identity != NULL) {
  189. new_session->psk_identity = BUF_strdup(session->psk_identity);
  190. if (new_session->psk_identity == NULL) {
  191. goto err;
  192. }
  193. }
  194. if (session->certs != NULL) {
  195. new_session->certs = sk_CRYPTO_BUFFER_new_null();
  196. if (new_session->certs == NULL) {
  197. goto err;
  198. }
  199. for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(session->certs); i++) {
  200. CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(session->certs, i);
  201. if (!sk_CRYPTO_BUFFER_push(new_session->certs, buffer)) {
  202. goto err;
  203. }
  204. CRYPTO_BUFFER_up_ref(buffer);
  205. }
  206. }
  207. if (!session->x509_method->session_dup(new_session, session)) {
  208. goto err;
  209. }
  210. new_session->verify_result = session->verify_result;
  211. new_session->ocsp_response_length = session->ocsp_response_length;
  212. if (session->ocsp_response != NULL) {
  213. new_session->ocsp_response = (uint8_t *)BUF_memdup(
  214. session->ocsp_response, session->ocsp_response_length);
  215. if (new_session->ocsp_response == NULL) {
  216. goto err;
  217. }
  218. }
  219. new_session->tlsext_signed_cert_timestamp_list_length =
  220. session->tlsext_signed_cert_timestamp_list_length;
  221. if (session->tlsext_signed_cert_timestamp_list != NULL) {
  222. new_session->tlsext_signed_cert_timestamp_list = (uint8_t *)BUF_memdup(
  223. session->tlsext_signed_cert_timestamp_list,
  224. session->tlsext_signed_cert_timestamp_list_length);
  225. if (new_session->tlsext_signed_cert_timestamp_list == NULL) {
  226. goto err;
  227. }
  228. }
  229. OPENSSL_memcpy(new_session->peer_sha256, session->peer_sha256,
  230. SHA256_DIGEST_LENGTH);
  231. new_session->peer_sha256_valid = session->peer_sha256_valid;
  232. if (session->tlsext_hostname != NULL) {
  233. new_session->tlsext_hostname = BUF_strdup(session->tlsext_hostname);
  234. if (new_session->tlsext_hostname == NULL) {
  235. goto err;
  236. }
  237. }
  238. new_session->peer_signature_algorithm = session->peer_signature_algorithm;
  239. new_session->timeout = session->timeout;
  240. new_session->auth_timeout = session->auth_timeout;
  241. new_session->time = session->time;
  242. /* Copy non-authentication connection properties. */
  243. if (dup_flags & SSL_SESSION_INCLUDE_NONAUTH) {
  244. new_session->session_id_length = session->session_id_length;
  245. OPENSSL_memcpy(new_session->session_id, session->session_id,
  246. session->session_id_length);
  247. new_session->group_id = session->group_id;
  248. OPENSSL_memcpy(new_session->original_handshake_hash,
  249. session->original_handshake_hash,
  250. session->original_handshake_hash_len);
  251. new_session->original_handshake_hash_len =
  252. session->original_handshake_hash_len;
  253. new_session->tlsext_tick_lifetime_hint = session->tlsext_tick_lifetime_hint;
  254. new_session->ticket_age_add = session->ticket_age_add;
  255. new_session->ticket_max_early_data = session->ticket_max_early_data;
  256. new_session->extended_master_secret = session->extended_master_secret;
  257. if (session->early_alpn != NULL) {
  258. new_session->early_alpn =
  259. (uint8_t *)BUF_memdup(session->early_alpn, session->early_alpn_len);
  260. if (new_session->early_alpn == NULL) {
  261. goto err;
  262. }
  263. }
  264. new_session->early_alpn_len = session->early_alpn_len;
  265. }
  266. /* Copy the ticket. */
  267. if (dup_flags & SSL_SESSION_INCLUDE_TICKET) {
  268. if (session->tlsext_tick != NULL) {
  269. new_session->tlsext_tick =
  270. (uint8_t *)BUF_memdup(session->tlsext_tick, session->tlsext_ticklen);
  271. if (new_session->tlsext_tick == NULL) {
  272. goto err;
  273. }
  274. }
  275. new_session->tlsext_ticklen = session->tlsext_ticklen;
  276. }
  277. /* The new_session does not get a copy of the ex_data. */
  278. new_session->not_resumable = 1;
  279. return new_session;
  280. err:
  281. SSL_SESSION_free(new_session);
  282. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  283. return 0;
  284. }
  285. void ssl_session_rebase_time(SSL *ssl, SSL_SESSION *session) {
  286. struct OPENSSL_timeval now;
  287. ssl_get_current_time(ssl, &now);
  288. /* To avoid overflows and underflows, if we've gone back in time, update the
  289. * time, but mark the session expired. */
  290. if (session->time > now.tv_sec) {
  291. session->time = now.tv_sec;
  292. session->timeout = 0;
  293. session->auth_timeout = 0;
  294. return;
  295. }
  296. /* Adjust the session time and timeouts. If the session has already expired,
  297. * clamp the timeouts at zero. */
  298. uint64_t delta = now.tv_sec - session->time;
  299. session->time = now.tv_sec;
  300. if (session->timeout < delta) {
  301. session->timeout = 0;
  302. } else {
  303. session->timeout -= delta;
  304. }
  305. if (session->auth_timeout < delta) {
  306. session->auth_timeout = 0;
  307. } else {
  308. session->auth_timeout -= delta;
  309. }
  310. }
  311. void ssl_session_renew_timeout(SSL *ssl, SSL_SESSION *session,
  312. uint32_t timeout) {
  313. /* Rebase the timestamp relative to the current time so |timeout| is measured
  314. * correctly. */
  315. ssl_session_rebase_time(ssl, session);
  316. if (session->timeout > timeout) {
  317. return;
  318. }
  319. session->timeout = timeout;
  320. if (session->timeout > session->auth_timeout) {
  321. session->timeout = session->auth_timeout;
  322. }
  323. }
  324. int SSL_SESSION_up_ref(SSL_SESSION *session) {
  325. CRYPTO_refcount_inc(&session->references);
  326. return 1;
  327. }
  328. void SSL_SESSION_free(SSL_SESSION *session) {
  329. if (session == NULL ||
  330. !CRYPTO_refcount_dec_and_test_zero(&session->references)) {
  331. return;
  332. }
  333. CRYPTO_free_ex_data(&g_ex_data_class, session, &session->ex_data);
  334. OPENSSL_cleanse(session->master_key, sizeof(session->master_key));
  335. OPENSSL_cleanse(session->session_id, sizeof(session->session_id));
  336. sk_CRYPTO_BUFFER_pop_free(session->certs, CRYPTO_BUFFER_free);
  337. session->x509_method->session_clear(session);
  338. OPENSSL_free(session->tlsext_hostname);
  339. OPENSSL_free(session->tlsext_tick);
  340. OPENSSL_free(session->tlsext_signed_cert_timestamp_list);
  341. OPENSSL_free(session->ocsp_response);
  342. OPENSSL_free(session->psk_identity);
  343. OPENSSL_free(session->early_alpn);
  344. OPENSSL_cleanse(session, sizeof(*session));
  345. OPENSSL_free(session);
  346. }
  347. const uint8_t *SSL_SESSION_get_id(const SSL_SESSION *session,
  348. unsigned *out_len) {
  349. if (out_len != NULL) {
  350. *out_len = session->session_id_length;
  351. }
  352. return session->session_id;
  353. }
  354. uint32_t SSL_SESSION_get_timeout(const SSL_SESSION *session) {
  355. return session->timeout;
  356. }
  357. uint64_t SSL_SESSION_get_time(const SSL_SESSION *session) {
  358. if (session == NULL) {
  359. /* NULL should crash, but silently accept it here for compatibility. */
  360. return 0;
  361. }
  362. return session->time;
  363. }
  364. X509 *SSL_SESSION_get0_peer(const SSL_SESSION *session) {
  365. return session->x509_peer;
  366. }
  367. size_t SSL_SESSION_get_master_key(const SSL_SESSION *session, uint8_t *out,
  368. size_t max_out) {
  369. /* TODO(davidben): Fix master_key_length's type and remove these casts. */
  370. if (max_out == 0) {
  371. return (size_t)session->master_key_length;
  372. }
  373. if (max_out > (size_t)session->master_key_length) {
  374. max_out = (size_t)session->master_key_length;
  375. }
  376. OPENSSL_memcpy(out, session->master_key, max_out);
  377. return max_out;
  378. }
  379. uint64_t SSL_SESSION_set_time(SSL_SESSION *session, uint64_t time) {
  380. if (session == NULL) {
  381. return 0;
  382. }
  383. session->time = time;
  384. return time;
  385. }
  386. uint32_t SSL_SESSION_set_timeout(SSL_SESSION *session, uint32_t timeout) {
  387. if (session == NULL) {
  388. return 0;
  389. }
  390. session->timeout = timeout;
  391. session->auth_timeout = timeout;
  392. return 1;
  393. }
  394. int SSL_SESSION_set1_id_context(SSL_SESSION *session, const uint8_t *sid_ctx,
  395. size_t sid_ctx_len) {
  396. if (sid_ctx_len > sizeof(session->sid_ctx)) {
  397. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  398. return 0;
  399. }
  400. assert(sizeof(session->sid_ctx) < 256);
  401. session->sid_ctx_length = (uint8_t)sid_ctx_len;
  402. OPENSSL_memcpy(session->sid_ctx, sid_ctx, sid_ctx_len);
  403. return 1;
  404. }
  405. SSL_SESSION *SSL_magic_pending_session_ptr(void) {
  406. return (SSL_SESSION *)&g_pending_session_magic;
  407. }
  408. SSL_SESSION *SSL_get_session(const SSL *ssl) {
  409. /* Once the handshake completes we return the established session. Otherwise
  410. * we return the intermediate session, either |session| (for resumption) or
  411. * |new_session| if doing a full handshake. */
  412. if (!SSL_in_init(ssl)) {
  413. return ssl->s3->established_session;
  414. }
  415. SSL_HANDSHAKE *hs = ssl->s3->hs;
  416. if (hs->early_session != NULL) {
  417. return hs->early_session;
  418. }
  419. if (hs->new_session != NULL) {
  420. return hs->new_session;
  421. }
  422. return ssl->session;
  423. }
  424. SSL_SESSION *SSL_get1_session(SSL *ssl) {
  425. SSL_SESSION *ret = SSL_get_session(ssl);
  426. if (ret != NULL) {
  427. SSL_SESSION_up_ref(ret);
  428. }
  429. return ret;
  430. }
  431. int SSL_SESSION_get_ex_new_index(long argl, void *argp,
  432. CRYPTO_EX_unused *unused,
  433. CRYPTO_EX_dup *dup_unused,
  434. CRYPTO_EX_free *free_func) {
  435. int index;
  436. if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp,
  437. free_func)) {
  438. return -1;
  439. }
  440. return index;
  441. }
  442. int SSL_SESSION_set_ex_data(SSL_SESSION *session, int idx, void *arg) {
  443. return CRYPTO_set_ex_data(&session->ex_data, idx, arg);
  444. }
  445. void *SSL_SESSION_get_ex_data(const SSL_SESSION *session, int idx) {
  446. return CRYPTO_get_ex_data(&session->ex_data, idx);
  447. }
  448. uint16_t SSL_SESSION_protocol_version(const SSL_SESSION *session) {
  449. uint16_t ret;
  450. if (!ssl_protocol_version_from_wire(&ret, session->ssl_version)) {
  451. /* An |SSL_SESSION| will never have an invalid version. This is enforced by
  452. * the parser. */
  453. assert(0);
  454. return 0;
  455. }
  456. return ret;
  457. }
  458. const EVP_MD *SSL_SESSION_get_digest(const SSL_SESSION *session) {
  459. return ssl_get_handshake_digest(session->cipher->algorithm_prf,
  460. SSL_SESSION_protocol_version(session));
  461. }
  462. int ssl_get_new_session(SSL_HANDSHAKE *hs, int is_server) {
  463. SSL *const ssl = hs->ssl;
  464. if (ssl->mode & SSL_MODE_NO_SESSION_CREATION) {
  465. OPENSSL_PUT_ERROR(SSL, SSL_R_SESSION_MAY_NOT_BE_CREATED);
  466. return 0;
  467. }
  468. SSL_SESSION *session = ssl_session_new(ssl->ctx->x509_method);
  469. if (session == NULL) {
  470. return 0;
  471. }
  472. session->is_server = is_server;
  473. session->ssl_version = ssl->version;
  474. /* Fill in the time from the |SSL_CTX|'s clock. */
  475. struct OPENSSL_timeval now;
  476. ssl_get_current_time(ssl, &now);
  477. session->time = now.tv_sec;
  478. uint16_t version = ssl3_protocol_version(ssl);
  479. if (version >= TLS1_3_VERSION) {
  480. /* TLS 1.3 uses tickets as authenticators, so we are willing to use them for
  481. * longer. */
  482. session->timeout = ssl->session_ctx->session_psk_dhe_timeout;
  483. session->auth_timeout = SSL_DEFAULT_SESSION_AUTH_TIMEOUT;
  484. } else {
  485. /* TLS 1.2 resumption does not incorporate new key material, so we use a
  486. * much shorter timeout. */
  487. session->timeout = ssl->session_ctx->session_timeout;
  488. session->auth_timeout = ssl->session_ctx->session_timeout;
  489. }
  490. if (is_server) {
  491. if (hs->ticket_expected || version >= TLS1_3_VERSION) {
  492. /* Don't set session IDs for sessions resumed with tickets. This will keep
  493. * them out of the session cache. */
  494. session->session_id_length = 0;
  495. } else {
  496. session->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
  497. if (!RAND_bytes(session->session_id, session->session_id_length)) {
  498. goto err;
  499. }
  500. }
  501. } else {
  502. session->session_id_length = 0;
  503. }
  504. if (ssl->cert->sid_ctx_length > sizeof(session->sid_ctx)) {
  505. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  506. goto err;
  507. }
  508. OPENSSL_memcpy(session->sid_ctx, ssl->cert->sid_ctx,
  509. ssl->cert->sid_ctx_length);
  510. session->sid_ctx_length = ssl->cert->sid_ctx_length;
  511. /* The session is marked not resumable until it is completely filled in. */
  512. session->not_resumable = 1;
  513. session->verify_result = X509_V_ERR_INVALID_CALL;
  514. SSL_SESSION_free(hs->new_session);
  515. hs->new_session = session;
  516. ssl_set_session(ssl, NULL);
  517. return 1;
  518. err:
  519. SSL_SESSION_free(session);
  520. return 0;
  521. }
  522. static int ssl_encrypt_ticket_with_cipher_ctx(SSL *ssl, CBB *out,
  523. const uint8_t *session_buf,
  524. size_t session_len) {
  525. bssl::ScopedEVP_CIPHER_CTX ctx;
  526. bssl::ScopedHMAC_CTX hctx;
  527. /* If the session is too long, emit a dummy value rather than abort the
  528. * connection. */
  529. static const size_t kMaxTicketOverhead =
  530. 16 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE;
  531. if (session_len > 0xffff - kMaxTicketOverhead) {
  532. static const char kTicketPlaceholder[] = "TICKET TOO LARGE";
  533. return CBB_add_bytes(out, (const uint8_t *)kTicketPlaceholder,
  534. strlen(kTicketPlaceholder));
  535. }
  536. /* Initialize HMAC and cipher contexts. If callback present it does all the
  537. * work otherwise use generated values from parent ctx. */
  538. SSL_CTX *tctx = ssl->session_ctx;
  539. uint8_t iv[EVP_MAX_IV_LENGTH];
  540. uint8_t key_name[16];
  541. if (tctx->tlsext_ticket_key_cb != NULL) {
  542. if (tctx->tlsext_ticket_key_cb(ssl, key_name, iv, ctx.get(), hctx.get(),
  543. 1 /* encrypt */) < 0) {
  544. return 0;
  545. }
  546. } else {
  547. if (!RAND_bytes(iv, 16) ||
  548. !EVP_EncryptInit_ex(ctx.get(), EVP_aes_128_cbc(), NULL,
  549. tctx->tlsext_tick_aes_key, iv) ||
  550. !HMAC_Init_ex(hctx.get(), tctx->tlsext_tick_hmac_key, 16,
  551. tlsext_tick_md(), NULL)) {
  552. return 0;
  553. }
  554. OPENSSL_memcpy(key_name, tctx->tlsext_tick_key_name, 16);
  555. }
  556. uint8_t *ptr;
  557. if (!CBB_add_bytes(out, key_name, 16) ||
  558. !CBB_add_bytes(out, iv, EVP_CIPHER_CTX_iv_length(ctx.get())) ||
  559. !CBB_reserve(out, &ptr, session_len + EVP_MAX_BLOCK_LENGTH)) {
  560. return 0;
  561. }
  562. size_t total = 0;
  563. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  564. OPENSSL_memcpy(ptr, session_buf, session_len);
  565. total = session_len;
  566. #else
  567. int len;
  568. if (!EVP_EncryptUpdate(ctx.get(), ptr + total, &len, session_buf, session_len)) {
  569. return 0;
  570. }
  571. total += len;
  572. if (!EVP_EncryptFinal_ex(ctx.get(), ptr + total, &len)) {
  573. return 0;
  574. }
  575. total += len;
  576. #endif
  577. if (!CBB_did_write(out, total)) {
  578. return 0;
  579. }
  580. unsigned hlen;
  581. if (!HMAC_Update(hctx.get(), CBB_data(out), CBB_len(out)) ||
  582. !CBB_reserve(out, &ptr, EVP_MAX_MD_SIZE) ||
  583. !HMAC_Final(hctx.get(), ptr, &hlen) ||
  584. !CBB_did_write(out, hlen)) {
  585. return 0;
  586. }
  587. return 1;
  588. }
  589. static int ssl_encrypt_ticket_with_method(SSL *ssl, CBB *out,
  590. const uint8_t *session_buf,
  591. size_t session_len) {
  592. const SSL_TICKET_AEAD_METHOD *method = ssl->session_ctx->ticket_aead_method;
  593. const size_t max_overhead = method->max_overhead(ssl);
  594. const size_t max_out = session_len + max_overhead;
  595. if (max_out < max_overhead) {
  596. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  597. return 0;
  598. }
  599. uint8_t *ptr;
  600. if (!CBB_reserve(out, &ptr, max_out)) {
  601. return 0;
  602. }
  603. size_t out_len;
  604. if (!method->seal(ssl, ptr, &out_len, max_out, session_buf, session_len)) {
  605. OPENSSL_PUT_ERROR(SSL, SSL_R_TICKET_ENCRYPTION_FAILED);
  606. return 0;
  607. }
  608. if (!CBB_did_write(out, out_len)) {
  609. return 0;
  610. }
  611. return 1;
  612. }
  613. int ssl_encrypt_ticket(SSL *ssl, CBB *out, const SSL_SESSION *session) {
  614. /* Serialize the SSL_SESSION to be encoded into the ticket. */
  615. uint8_t *session_buf = NULL;
  616. size_t session_len;
  617. if (!SSL_SESSION_to_bytes_for_ticket(session, &session_buf, &session_len)) {
  618. return -1;
  619. }
  620. int ret = 0;
  621. if (ssl->session_ctx->ticket_aead_method) {
  622. ret = ssl_encrypt_ticket_with_method(ssl, out, session_buf, session_len);
  623. } else {
  624. ret =
  625. ssl_encrypt_ticket_with_cipher_ctx(ssl, out, session_buf, session_len);
  626. }
  627. OPENSSL_free(session_buf);
  628. return ret;
  629. }
  630. int ssl_session_is_context_valid(const SSL *ssl, const SSL_SESSION *session) {
  631. if (session == NULL) {
  632. return 0;
  633. }
  634. return session->sid_ctx_length == ssl->cert->sid_ctx_length &&
  635. OPENSSL_memcmp(session->sid_ctx, ssl->cert->sid_ctx,
  636. ssl->cert->sid_ctx_length) == 0;
  637. }
  638. int ssl_session_is_time_valid(const SSL *ssl, const SSL_SESSION *session) {
  639. if (session == NULL) {
  640. return 0;
  641. }
  642. struct OPENSSL_timeval now;
  643. ssl_get_current_time(ssl, &now);
  644. /* Reject tickets from the future to avoid underflow. */
  645. if (now.tv_sec < session->time) {
  646. return 0;
  647. }
  648. return session->timeout > now.tv_sec - session->time;
  649. }
  650. int ssl_session_is_resumable(const SSL_HANDSHAKE *hs,
  651. const SSL_SESSION *session) {
  652. const SSL *const ssl = hs->ssl;
  653. return ssl_session_is_context_valid(ssl, session) &&
  654. /* The session must have been created by the same type of end point as
  655. * we're now using it with. */
  656. ssl->server == session->is_server &&
  657. /* The session must not be expired. */
  658. ssl_session_is_time_valid(ssl, session) &&
  659. /* Only resume if the session's version matches the negotiated
  660. * version. */
  661. ssl->version == session->ssl_version &&
  662. /* Only resume if the session's cipher matches the negotiated one. */
  663. hs->new_cipher == session->cipher &&
  664. /* If the session contains a client certificate (either the full
  665. * certificate or just the hash) then require that the form of the
  666. * certificate matches the current configuration. */
  667. ((sk_CRYPTO_BUFFER_num(session->certs) == 0 &&
  668. !session->peer_sha256_valid) ||
  669. session->peer_sha256_valid ==
  670. ssl->retain_only_sha256_of_client_certs);
  671. }
  672. /* ssl_lookup_session looks up |session_id| in the session cache and sets
  673. * |*out_session| to an |SSL_SESSION| object if found. The caller takes
  674. * ownership of the result. */
  675. static enum ssl_session_result_t ssl_lookup_session(
  676. SSL *ssl, SSL_SESSION **out_session, const uint8_t *session_id,
  677. size_t session_id_len) {
  678. *out_session = NULL;
  679. if (session_id_len == 0 || session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
  680. return ssl_session_success;
  681. }
  682. SSL_SESSION *session = NULL;
  683. /* Try the internal cache, if it exists. */
  684. if (!(ssl->session_ctx->session_cache_mode &
  685. SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) {
  686. SSL_SESSION data;
  687. data.ssl_version = ssl->version;
  688. data.session_id_length = session_id_len;
  689. OPENSSL_memcpy(data.session_id, session_id, session_id_len);
  690. CRYPTO_MUTEX_lock_read(&ssl->session_ctx->lock);
  691. session = lh_SSL_SESSION_retrieve(ssl->session_ctx->sessions, &data);
  692. if (session != NULL) {
  693. SSL_SESSION_up_ref(session);
  694. }
  695. /* TODO(davidben): This should probably move it to the front of the list. */
  696. CRYPTO_MUTEX_unlock_read(&ssl->session_ctx->lock);
  697. }
  698. /* Fall back to the external cache, if it exists. */
  699. if (session == NULL &&
  700. ssl->session_ctx->get_session_cb != NULL) {
  701. int copy = 1;
  702. session = ssl->session_ctx->get_session_cb(ssl, (uint8_t *)session_id,
  703. session_id_len, &copy);
  704. if (session == NULL) {
  705. return ssl_session_success;
  706. }
  707. if (session == SSL_magic_pending_session_ptr()) {
  708. return ssl_session_retry;
  709. }
  710. /* Increment reference count now if the session callback asks us to do so
  711. * (note that if the session structures returned by the callback are shared
  712. * between threads, it must handle the reference count itself [i.e. copy ==
  713. * 0], or things won't be thread-safe). */
  714. if (copy) {
  715. SSL_SESSION_up_ref(session);
  716. }
  717. /* Add the externally cached session to the internal cache if necessary. */
  718. if (!(ssl->session_ctx->session_cache_mode &
  719. SSL_SESS_CACHE_NO_INTERNAL_STORE)) {
  720. SSL_CTX_add_session(ssl->session_ctx, session);
  721. }
  722. }
  723. if (session != NULL &&
  724. !ssl_session_is_time_valid(ssl, session)) {
  725. /* The session was from the cache, so remove it. */
  726. SSL_CTX_remove_session(ssl->session_ctx, session);
  727. SSL_SESSION_free(session);
  728. session = NULL;
  729. }
  730. *out_session = session;
  731. return ssl_session_success;
  732. }
  733. enum ssl_session_result_t ssl_get_prev_session(
  734. SSL *ssl, SSL_SESSION **out_session, int *out_tickets_supported,
  735. int *out_renew_ticket, const SSL_CLIENT_HELLO *client_hello) {
  736. /* This is used only by servers. */
  737. assert(ssl->server);
  738. SSL_SESSION *session = NULL;
  739. int renew_ticket = 0;
  740. /* If tickets are disabled, always behave as if no tickets are present. */
  741. const uint8_t *ticket = NULL;
  742. size_t ticket_len = 0;
  743. const int tickets_supported =
  744. !(SSL_get_options(ssl) & SSL_OP_NO_TICKET) &&
  745. ssl->version > SSL3_VERSION &&
  746. SSL_early_callback_ctx_extension_get(
  747. client_hello, TLSEXT_TYPE_session_ticket, &ticket, &ticket_len);
  748. if (tickets_supported && ticket_len > 0) {
  749. switch (ssl_process_ticket(ssl, &session, &renew_ticket, ticket, ticket_len,
  750. client_hello->session_id,
  751. client_hello->session_id_len)) {
  752. case ssl_ticket_aead_success:
  753. break;
  754. case ssl_ticket_aead_ignore_ticket:
  755. assert(session == NULL);
  756. break;
  757. case ssl_ticket_aead_error:
  758. return ssl_session_error;
  759. case ssl_ticket_aead_retry:
  760. return ssl_session_ticket_retry;
  761. }
  762. } else {
  763. /* The client didn't send a ticket, so the session ID is a real ID. */
  764. enum ssl_session_result_t lookup_ret = ssl_lookup_session(
  765. ssl, &session, client_hello->session_id, client_hello->session_id_len);
  766. if (lookup_ret != ssl_session_success) {
  767. return lookup_ret;
  768. }
  769. }
  770. *out_session = session;
  771. *out_tickets_supported = tickets_supported;
  772. *out_renew_ticket = renew_ticket;
  773. return ssl_session_success;
  774. }
  775. int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *session) {
  776. /* Although |session| is inserted into two structures (a doubly-linked list
  777. * and the hash table), |ctx| only takes one reference. */
  778. SSL_SESSION_up_ref(session);
  779. SSL_SESSION *old_session;
  780. CRYPTO_MUTEX_lock_write(&ctx->lock);
  781. if (!lh_SSL_SESSION_insert(ctx->sessions, &old_session, session)) {
  782. CRYPTO_MUTEX_unlock_write(&ctx->lock);
  783. SSL_SESSION_free(session);
  784. return 0;
  785. }
  786. if (old_session != NULL) {
  787. if (old_session == session) {
  788. /* |session| was already in the cache. */
  789. CRYPTO_MUTEX_unlock_write(&ctx->lock);
  790. SSL_SESSION_free(old_session);
  791. return 0;
  792. }
  793. /* There was a session ID collision. |old_session| must be removed from
  794. * the linked list and released. */
  795. SSL_SESSION_list_remove(ctx, old_session);
  796. SSL_SESSION_free(old_session);
  797. }
  798. SSL_SESSION_list_add(ctx, session);
  799. /* Enforce any cache size limits. */
  800. if (SSL_CTX_sess_get_cache_size(ctx) > 0) {
  801. while (SSL_CTX_sess_number(ctx) > SSL_CTX_sess_get_cache_size(ctx)) {
  802. if (!remove_session_lock(ctx, ctx->session_cache_tail, 0)) {
  803. break;
  804. }
  805. }
  806. }
  807. CRYPTO_MUTEX_unlock_write(&ctx->lock);
  808. return 1;
  809. }
  810. int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *session) {
  811. return remove_session_lock(ctx, session, 1);
  812. }
  813. static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *session, int lock) {
  814. int ret = 0;
  815. if (session != NULL && session->session_id_length != 0) {
  816. if (lock) {
  817. CRYPTO_MUTEX_lock_write(&ctx->lock);
  818. }
  819. SSL_SESSION *found_session = lh_SSL_SESSION_retrieve(ctx->sessions,
  820. session);
  821. if (found_session == session) {
  822. ret = 1;
  823. found_session = lh_SSL_SESSION_delete(ctx->sessions, session);
  824. SSL_SESSION_list_remove(ctx, session);
  825. }
  826. if (lock) {
  827. CRYPTO_MUTEX_unlock_write(&ctx->lock);
  828. }
  829. if (ret) {
  830. if (ctx->remove_session_cb != NULL) {
  831. ctx->remove_session_cb(ctx, found_session);
  832. }
  833. SSL_SESSION_free(found_session);
  834. }
  835. }
  836. return ret;
  837. }
  838. int SSL_set_session(SSL *ssl, SSL_SESSION *session) {
  839. /* SSL_set_session may only be called before the handshake has started. */
  840. if (ssl->s3->initial_handshake_complete ||
  841. ssl->s3->hs == NULL ||
  842. ssl->s3->hs->state != SSL_ST_INIT) {
  843. abort();
  844. }
  845. ssl_set_session(ssl, session);
  846. return 1;
  847. }
  848. void ssl_set_session(SSL *ssl, SSL_SESSION *session) {
  849. if (ssl->session == session) {
  850. return;
  851. }
  852. SSL_SESSION_free(ssl->session);
  853. ssl->session = session;
  854. if (session != NULL) {
  855. SSL_SESSION_up_ref(session);
  856. }
  857. }
  858. uint32_t SSL_CTX_set_timeout(SSL_CTX *ctx, uint32_t timeout) {
  859. if (ctx == NULL) {
  860. return 0;
  861. }
  862. /* Historically, zero was treated as |SSL_DEFAULT_SESSION_TIMEOUT|. */
  863. if (timeout == 0) {
  864. timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  865. }
  866. uint32_t old_timeout = ctx->session_timeout;
  867. ctx->session_timeout = timeout;
  868. return old_timeout;
  869. }
  870. uint32_t SSL_CTX_get_timeout(const SSL_CTX *ctx) {
  871. if (ctx == NULL) {
  872. return 0;
  873. }
  874. return ctx->session_timeout;
  875. }
  876. void SSL_CTX_set_session_psk_dhe_timeout(SSL_CTX *ctx, uint32_t timeout) {
  877. ctx->session_psk_dhe_timeout = timeout;
  878. }
  879. typedef struct timeout_param_st {
  880. SSL_CTX *ctx;
  881. uint64_t time;
  882. LHASH_OF(SSL_SESSION) *cache;
  883. } TIMEOUT_PARAM;
  884. static void timeout_doall_arg(SSL_SESSION *session, void *void_param) {
  885. TIMEOUT_PARAM *param = reinterpret_cast<TIMEOUT_PARAM *>(void_param);
  886. if (param->time == 0 ||
  887. session->time + session->timeout < session->time ||
  888. param->time > (session->time + session->timeout)) {
  889. /* timeout */
  890. /* The reason we don't call SSL_CTX_remove_session() is to
  891. * save on locking overhead */
  892. (void) lh_SSL_SESSION_delete(param->cache, session);
  893. SSL_SESSION_list_remove(param->ctx, session);
  894. if (param->ctx->remove_session_cb != NULL) {
  895. param->ctx->remove_session_cb(param->ctx, session);
  896. }
  897. SSL_SESSION_free(session);
  898. }
  899. }
  900. void SSL_CTX_flush_sessions(SSL_CTX *ctx, uint64_t time) {
  901. TIMEOUT_PARAM tp;
  902. tp.ctx = ctx;
  903. tp.cache = ctx->sessions;
  904. if (tp.cache == NULL) {
  905. return;
  906. }
  907. tp.time = time;
  908. CRYPTO_MUTEX_lock_write(&ctx->lock);
  909. lh_SSL_SESSION_doall_arg(tp.cache, timeout_doall_arg, &tp);
  910. CRYPTO_MUTEX_unlock_write(&ctx->lock);
  911. }
  912. /* locked by SSL_CTX in the calling function */
  913. static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *session) {
  914. if (session->next == NULL || session->prev == NULL) {
  915. return;
  916. }
  917. if (session->next == (SSL_SESSION *)&ctx->session_cache_tail) {
  918. /* last element in list */
  919. if (session->prev == (SSL_SESSION *)&ctx->session_cache_head) {
  920. /* only one element in list */
  921. ctx->session_cache_head = NULL;
  922. ctx->session_cache_tail = NULL;
  923. } else {
  924. ctx->session_cache_tail = session->prev;
  925. session->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail);
  926. }
  927. } else {
  928. if (session->prev == (SSL_SESSION *)&ctx->session_cache_head) {
  929. /* first element in list */
  930. ctx->session_cache_head = session->next;
  931. session->next->prev = (SSL_SESSION *)&(ctx->session_cache_head);
  932. } else { /* middle of list */
  933. session->next->prev = session->prev;
  934. session->prev->next = session->next;
  935. }
  936. }
  937. session->prev = session->next = NULL;
  938. }
  939. static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *session) {
  940. if (session->next != NULL && session->prev != NULL) {
  941. SSL_SESSION_list_remove(ctx, session);
  942. }
  943. if (ctx->session_cache_head == NULL) {
  944. ctx->session_cache_head = session;
  945. ctx->session_cache_tail = session;
  946. session->prev = (SSL_SESSION *)&(ctx->session_cache_head);
  947. session->next = (SSL_SESSION *)&(ctx->session_cache_tail);
  948. } else {
  949. session->next = ctx->session_cache_head;
  950. session->next->prev = session;
  951. session->prev = (SSL_SESSION *)&(ctx->session_cache_head);
  952. ctx->session_cache_head = session;
  953. }
  954. }
  955. void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
  956. int (*cb)(SSL *ssl, SSL_SESSION *session)) {
  957. ctx->new_session_cb = cb;
  958. }
  959. int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *session) {
  960. return ctx->new_session_cb;
  961. }
  962. void SSL_CTX_sess_set_remove_cb(
  963. SSL_CTX *ctx, void (*cb)(SSL_CTX *ctx, SSL_SESSION *session)) {
  964. ctx->remove_session_cb = cb;
  965. }
  966. void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX *ctx,
  967. SSL_SESSION *session) {
  968. return ctx->remove_session_cb;
  969. }
  970. void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
  971. SSL_SESSION *(*cb)(SSL *ssl,
  972. uint8_t *id, int id_len,
  973. int *out_copy)) {
  974. ctx->get_session_cb = cb;
  975. }
  976. SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(
  977. SSL *ssl, uint8_t *id, int id_len, int *out_copy) {
  978. return ctx->get_session_cb;
  979. }
  980. void SSL_CTX_set_info_callback(
  981. SSL_CTX *ctx, void (*cb)(const SSL *ssl, int type, int value)) {
  982. ctx->info_callback = cb;
  983. }
  984. void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl, int type,
  985. int value) {
  986. return ctx->info_callback;
  987. }
  988. void SSL_CTX_set_channel_id_cb(SSL_CTX *ctx,
  989. void (*cb)(SSL *ssl, EVP_PKEY **pkey)) {
  990. ctx->channel_id_cb = cb;
  991. }
  992. void (*SSL_CTX_get_channel_id_cb(SSL_CTX *ctx))(SSL *ssl, EVP_PKEY **pkey) {
  993. return ctx->channel_id_cb;
  994. }