ssl_sess.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  1. /*
  2. * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2005 Nokia. All rights reserved.
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #if defined(__TANDEM) && defined(_SPT_MODEL_)
  11. # include <spthread.h>
  12. # include <spt_extensions.h> /* timeval */
  13. #endif
  14. #include <stdio.h>
  15. #include <openssl/rand.h>
  16. #include <openssl/engine.h>
  17. #include "internal/refcount.h"
  18. #include "internal/cryptlib.h"
  19. #include "ssl_local.h"
  20. #include "statem/statem_local.h"
  21. static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s);
  22. static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s);
  23. static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck);
  24. DEFINE_STACK_OF(SSL_SESSION)
  25. __owur static ossl_inline int sess_timedout(OSSL_TIME t, SSL_SESSION *ss)
  26. {
  27. return ossl_time_compare(t, ss->calc_timeout) > 0;
  28. }
  29. /*
  30. * Returns -1/0/+1 as other XXXcmp-type functions
  31. * Takes calculated timeout into consideration
  32. */
  33. __owur static ossl_inline int timeoutcmp(SSL_SESSION *a, SSL_SESSION *b)
  34. {
  35. return ossl_time_compare(a->calc_timeout, b->calc_timeout);
  36. }
  37. /*
  38. * Calculates effective timeout
  39. * Locking must be done by the caller of this function
  40. */
  41. void ssl_session_calculate_timeout(SSL_SESSION *ss)
  42. {
  43. ss->calc_timeout = ossl_time_add(ss->time, ss->timeout);
  44. }
  45. /*
  46. * SSL_get_session() and SSL_get1_session() are problematic in TLS1.3 because,
  47. * unlike in earlier protocol versions, the session ticket may not have been
  48. * sent yet even though a handshake has finished. The session ticket data could
  49. * come in sometime later...or even change if multiple session ticket messages
  50. * are sent from the server. The preferred way for applications to obtain
  51. * a resumable session is to use SSL_CTX_sess_set_new_cb().
  52. */
  53. SSL_SESSION *SSL_get_session(const SSL *ssl)
  54. /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */
  55. {
  56. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
  57. if (sc == NULL)
  58. return NULL;
  59. return sc->session;
  60. }
  61. SSL_SESSION *SSL_get1_session(SSL *ssl)
  62. /* variant of SSL_get_session: caller really gets something */
  63. {
  64. SSL_SESSION *sess;
  65. /*
  66. * Need to lock this all up rather than just use CRYPTO_add so that
  67. * somebody doesn't free ssl->session between when we check it's non-null
  68. * and when we up the reference count.
  69. */
  70. if (!CRYPTO_THREAD_read_lock(ssl->lock))
  71. return NULL;
  72. sess = SSL_get_session(ssl);
  73. if (sess != NULL)
  74. SSL_SESSION_up_ref(sess);
  75. CRYPTO_THREAD_unlock(ssl->lock);
  76. return sess;
  77. }
  78. int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg)
  79. {
  80. return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
  81. }
  82. void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx)
  83. {
  84. return CRYPTO_get_ex_data(&s->ex_data, idx);
  85. }
  86. SSL_SESSION *SSL_SESSION_new(void)
  87. {
  88. SSL_SESSION *ss;
  89. if (!OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL))
  90. return NULL;
  91. ss = OPENSSL_zalloc(sizeof(*ss));
  92. if (ss == NULL)
  93. return NULL;
  94. ss->ext.max_fragment_len_mode = TLSEXT_max_fragment_length_UNSPECIFIED;
  95. ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */
  96. /* 5 minute timeout by default */
  97. ss->timeout = ossl_seconds2time(60 * 5 + 4);
  98. ss->time = ossl_time_now();
  99. ssl_session_calculate_timeout(ss);
  100. if (!CRYPTO_NEW_REF(&ss->references, 1)) {
  101. OPENSSL_free(ss);
  102. return NULL;
  103. }
  104. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data)) {
  105. CRYPTO_FREE_REF(&ss->references);
  106. OPENSSL_free(ss);
  107. return NULL;
  108. }
  109. return ss;
  110. }
  111. /*
  112. * Create a new SSL_SESSION and duplicate the contents of |src| into it. If
  113. * ticket == 0 then no ticket information is duplicated, otherwise it is.
  114. */
  115. static SSL_SESSION *ssl_session_dup_intern(const SSL_SESSION *src, int ticket)
  116. {
  117. SSL_SESSION *dest;
  118. dest = OPENSSL_malloc(sizeof(*dest));
  119. if (dest == NULL)
  120. return NULL;
  121. /*
  122. * src is logically read-only but the prev/next pointers are not, they are
  123. * part of the session cache and can be modified concurrently.
  124. */
  125. memcpy(dest, src, offsetof(SSL_SESSION, prev));
  126. /*
  127. * Set the various pointers to NULL so that we can call SSL_SESSION_free in
  128. * the case of an error whilst halfway through constructing dest
  129. */
  130. #ifndef OPENSSL_NO_PSK
  131. dest->psk_identity_hint = NULL;
  132. dest->psk_identity = NULL;
  133. #endif
  134. dest->ext.hostname = NULL;
  135. dest->ext.tick = NULL;
  136. dest->ext.alpn_selected = NULL;
  137. #ifndef OPENSSL_NO_SRP
  138. dest->srp_username = NULL;
  139. #endif
  140. dest->peer_chain = NULL;
  141. dest->peer = NULL;
  142. dest->peer_rpk = NULL;
  143. dest->ticket_appdata = NULL;
  144. memset(&dest->ex_data, 0, sizeof(dest->ex_data));
  145. /* As the copy is not in the cache, we remove the associated pointers */
  146. dest->prev = NULL;
  147. dest->next = NULL;
  148. dest->owner = NULL;
  149. if (!CRYPTO_NEW_REF(&dest->references, 1)) {
  150. OPENSSL_free(dest);
  151. return NULL;
  152. }
  153. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, dest, &dest->ex_data)) {
  154. ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
  155. goto err;
  156. }
  157. if (src->peer != NULL) {
  158. if (!X509_up_ref(src->peer)) {
  159. ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
  160. goto err;
  161. }
  162. dest->peer = src->peer;
  163. }
  164. if (src->peer_chain != NULL) {
  165. dest->peer_chain = X509_chain_up_ref(src->peer_chain);
  166. if (dest->peer_chain == NULL) {
  167. ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
  168. goto err;
  169. }
  170. }
  171. if (src->peer_rpk != NULL) {
  172. if (!EVP_PKEY_up_ref(src->peer_rpk))
  173. goto err;
  174. dest->peer_rpk = src->peer_rpk;
  175. }
  176. #ifndef OPENSSL_NO_PSK
  177. if (src->psk_identity_hint) {
  178. dest->psk_identity_hint = OPENSSL_strdup(src->psk_identity_hint);
  179. if (dest->psk_identity_hint == NULL)
  180. goto err;
  181. }
  182. if (src->psk_identity) {
  183. dest->psk_identity = OPENSSL_strdup(src->psk_identity);
  184. if (dest->psk_identity == NULL)
  185. goto err;
  186. }
  187. #endif
  188. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL_SESSION,
  189. &dest->ex_data, &src->ex_data)) {
  190. ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
  191. goto err;
  192. }
  193. if (src->ext.hostname) {
  194. dest->ext.hostname = OPENSSL_strdup(src->ext.hostname);
  195. if (dest->ext.hostname == NULL)
  196. goto err;
  197. }
  198. if (ticket != 0 && src->ext.tick != NULL) {
  199. dest->ext.tick =
  200. OPENSSL_memdup(src->ext.tick, src->ext.ticklen);
  201. if (dest->ext.tick == NULL)
  202. goto err;
  203. } else {
  204. dest->ext.tick_lifetime_hint = 0;
  205. dest->ext.ticklen = 0;
  206. }
  207. if (src->ext.alpn_selected != NULL) {
  208. dest->ext.alpn_selected = OPENSSL_memdup(src->ext.alpn_selected,
  209. src->ext.alpn_selected_len);
  210. if (dest->ext.alpn_selected == NULL)
  211. goto err;
  212. }
  213. #ifndef OPENSSL_NO_SRP
  214. if (src->srp_username) {
  215. dest->srp_username = OPENSSL_strdup(src->srp_username);
  216. if (dest->srp_username == NULL)
  217. goto err;
  218. }
  219. #endif
  220. if (src->ticket_appdata != NULL) {
  221. dest->ticket_appdata =
  222. OPENSSL_memdup(src->ticket_appdata, src->ticket_appdata_len);
  223. if (dest->ticket_appdata == NULL)
  224. goto err;
  225. }
  226. return dest;
  227. err:
  228. SSL_SESSION_free(dest);
  229. return NULL;
  230. }
  231. SSL_SESSION *SSL_SESSION_dup(const SSL_SESSION *src)
  232. {
  233. return ssl_session_dup_intern(src, 1);
  234. }
  235. /*
  236. * Used internally when duplicating a session which might be already shared.
  237. * We will have resumed the original session. Subsequently we might have marked
  238. * it as non-resumable (e.g. in another thread) - but this copy should be ok to
  239. * resume from.
  240. */
  241. SSL_SESSION *ssl_session_dup(const SSL_SESSION *src, int ticket)
  242. {
  243. SSL_SESSION *sess = ssl_session_dup_intern(src, ticket);
  244. if (sess != NULL)
  245. sess->not_resumable = 0;
  246. return sess;
  247. }
  248. const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len)
  249. {
  250. if (len)
  251. *len = (unsigned int)s->session_id_length;
  252. return s->session_id;
  253. }
  254. const unsigned char *SSL_SESSION_get0_id_context(const SSL_SESSION *s,
  255. unsigned int *len)
  256. {
  257. if (len != NULL)
  258. *len = (unsigned int)s->sid_ctx_length;
  259. return s->sid_ctx;
  260. }
  261. unsigned int SSL_SESSION_get_compress_id(const SSL_SESSION *s)
  262. {
  263. return s->compress_meth;
  264. }
  265. /*
  266. * SSLv3/TLSv1 has 32 bytes (256 bits) of session ID space. As such, filling
  267. * the ID with random junk repeatedly until we have no conflict is going to
  268. * complete in one iteration pretty much "most" of the time (btw:
  269. * understatement). So, if it takes us 10 iterations and we still can't avoid
  270. * a conflict - well that's a reasonable point to call it quits. Either the
  271. * RAND code is broken or someone is trying to open roughly very close to
  272. * 2^256 SSL sessions to our server. How you might store that many sessions
  273. * is perhaps a more interesting question ...
  274. */
  275. #define MAX_SESS_ID_ATTEMPTS 10
  276. static int def_generate_session_id(SSL *ssl, unsigned char *id,
  277. unsigned int *id_len)
  278. {
  279. unsigned int retry = 0;
  280. do {
  281. if (RAND_bytes_ex(ssl->ctx->libctx, id, *id_len, 0) <= 0)
  282. return 0;
  283. #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  284. if (retry > 0) {
  285. id[0]++;
  286. }
  287. #endif
  288. } while (SSL_has_matching_session_id(ssl, id, *id_len) &&
  289. (++retry < MAX_SESS_ID_ATTEMPTS)) ;
  290. if (retry < MAX_SESS_ID_ATTEMPTS)
  291. return 1;
  292. /* else - woops a session_id match */
  293. /*
  294. * XXX We should also check the external cache -- but the probability of
  295. * a collision is negligible, and we could not prevent the concurrent
  296. * creation of sessions with identical IDs since we currently don't have
  297. * means to atomically check whether a session ID already exists and make
  298. * a reservation for it if it does not (this problem applies to the
  299. * internal cache as well).
  300. */
  301. return 0;
  302. }
  303. int ssl_generate_session_id(SSL_CONNECTION *s, SSL_SESSION *ss)
  304. {
  305. unsigned int tmp;
  306. GEN_SESSION_CB cb = def_generate_session_id;
  307. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  308. switch (s->version) {
  309. case SSL3_VERSION:
  310. case TLS1_VERSION:
  311. case TLS1_1_VERSION:
  312. case TLS1_2_VERSION:
  313. case TLS1_3_VERSION:
  314. case DTLS1_BAD_VER:
  315. case DTLS1_VERSION:
  316. case DTLS1_2_VERSION:
  317. ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
  318. break;
  319. default:
  320. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNSUPPORTED_SSL_VERSION);
  321. return 0;
  322. }
  323. /*-
  324. * If RFC5077 ticket, use empty session ID (as server).
  325. * Note that:
  326. * (a) ssl_get_prev_session() does lookahead into the
  327. * ClientHello extensions to find the session ticket.
  328. * When ssl_get_prev_session() fails, statem_srvr.c calls
  329. * ssl_get_new_session() in tls_process_client_hello().
  330. * At that point, it has not yet parsed the extensions,
  331. * however, because of the lookahead, it already knows
  332. * whether a ticket is expected or not.
  333. *
  334. * (b) statem_clnt.c calls ssl_get_new_session() before parsing
  335. * ServerHello extensions, and before recording the session
  336. * ID received from the server, so this block is a noop.
  337. */
  338. if (s->ext.ticket_expected) {
  339. ss->session_id_length = 0;
  340. return 1;
  341. }
  342. /* Choose which callback will set the session ID */
  343. if (!CRYPTO_THREAD_read_lock(SSL_CONNECTION_GET_SSL(s)->lock))
  344. return 0;
  345. if (!CRYPTO_THREAD_read_lock(s->session_ctx->lock)) {
  346. CRYPTO_THREAD_unlock(ssl->lock);
  347. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  348. SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
  349. return 0;
  350. }
  351. if (s->generate_session_id)
  352. cb = s->generate_session_id;
  353. else if (s->session_ctx->generate_session_id)
  354. cb = s->session_ctx->generate_session_id;
  355. CRYPTO_THREAD_unlock(s->session_ctx->lock);
  356. CRYPTO_THREAD_unlock(ssl->lock);
  357. /* Choose a session ID */
  358. memset(ss->session_id, 0, ss->session_id_length);
  359. tmp = (int)ss->session_id_length;
  360. if (!cb(ssl, ss->session_id, &tmp)) {
  361. /* The callback failed */
  362. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  363. SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
  364. return 0;
  365. }
  366. /*
  367. * Don't allow the callback to set the session length to zero. nor
  368. * set it higher than it was.
  369. */
  370. if (tmp == 0 || tmp > ss->session_id_length) {
  371. /* The callback set an illegal length */
  372. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  373. SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
  374. return 0;
  375. }
  376. ss->session_id_length = tmp;
  377. /* Finally, check for a conflict */
  378. if (SSL_has_matching_session_id(ssl, ss->session_id,
  379. (unsigned int)ss->session_id_length)) {
  380. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_SSL_SESSION_ID_CONFLICT);
  381. return 0;
  382. }
  383. return 1;
  384. }
  385. int ssl_get_new_session(SSL_CONNECTION *s, int session)
  386. {
  387. /* This gets used by clients and servers. */
  388. SSL_SESSION *ss = NULL;
  389. if ((ss = SSL_SESSION_new()) == NULL) {
  390. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB);
  391. return 0;
  392. }
  393. /* If the context has a default timeout, use it */
  394. if (ossl_time_is_zero(s->session_ctx->session_timeout))
  395. ss->timeout = SSL_CONNECTION_GET_SSL(s)->method->get_timeout();
  396. else
  397. ss->timeout = s->session_ctx->session_timeout;
  398. ssl_session_calculate_timeout(ss);
  399. SSL_SESSION_free(s->session);
  400. s->session = NULL;
  401. if (session) {
  402. if (SSL_CONNECTION_IS_TLS13(s)) {
  403. /*
  404. * We generate the session id while constructing the
  405. * NewSessionTicket in TLSv1.3.
  406. */
  407. ss->session_id_length = 0;
  408. } else if (!ssl_generate_session_id(s, ss)) {
  409. /* SSLfatal() already called */
  410. SSL_SESSION_free(ss);
  411. return 0;
  412. }
  413. } else {
  414. ss->session_id_length = 0;
  415. }
  416. if (s->sid_ctx_length > sizeof(ss->sid_ctx)) {
  417. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  418. SSL_SESSION_free(ss);
  419. return 0;
  420. }
  421. memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length);
  422. ss->sid_ctx_length = s->sid_ctx_length;
  423. s->session = ss;
  424. ss->ssl_version = s->version;
  425. ss->verify_result = X509_V_OK;
  426. /* If client supports extended master secret set it in session */
  427. if (s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS)
  428. ss->flags |= SSL_SESS_FLAG_EXTMS;
  429. return 1;
  430. }
  431. SSL_SESSION *lookup_sess_in_cache(SSL_CONNECTION *s,
  432. const unsigned char *sess_id,
  433. size_t sess_id_len)
  434. {
  435. SSL_SESSION *ret = NULL;
  436. if ((s->session_ctx->session_cache_mode
  437. & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP) == 0) {
  438. SSL_SESSION data;
  439. data.ssl_version = s->version;
  440. if (!ossl_assert(sess_id_len <= SSL_MAX_SSL_SESSION_ID_LENGTH))
  441. return NULL;
  442. memcpy(data.session_id, sess_id, sess_id_len);
  443. data.session_id_length = sess_id_len;
  444. if (!CRYPTO_THREAD_read_lock(s->session_ctx->lock))
  445. return NULL;
  446. ret = lh_SSL_SESSION_retrieve(s->session_ctx->sessions, &data);
  447. if (ret != NULL) {
  448. /* don't allow other threads to steal it: */
  449. SSL_SESSION_up_ref(ret);
  450. }
  451. CRYPTO_THREAD_unlock(s->session_ctx->lock);
  452. if (ret == NULL)
  453. ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_miss);
  454. }
  455. if (ret == NULL && s->session_ctx->get_session_cb != NULL) {
  456. int copy = 1;
  457. ret = s->session_ctx->get_session_cb(SSL_CONNECTION_GET_USER_SSL(s),
  458. sess_id, sess_id_len, &copy);
  459. if (ret != NULL) {
  460. if (ret->not_resumable) {
  461. /* If its not resumable then ignore this session */
  462. if (!copy)
  463. SSL_SESSION_free(ret);
  464. return NULL;
  465. }
  466. ssl_tsan_counter(s->session_ctx,
  467. &s->session_ctx->stats.sess_cb_hit);
  468. /*
  469. * Increment reference count now if the session callback asks us
  470. * to do so (note that if the session structures returned by the
  471. * callback are shared between threads, it must handle the
  472. * reference count itself [i.e. copy == 0], or things won't be
  473. * thread-safe).
  474. */
  475. if (copy)
  476. SSL_SESSION_up_ref(ret);
  477. /*
  478. * Add the externally cached session to the internal cache as
  479. * well if and only if we are supposed to.
  480. */
  481. if ((s->session_ctx->session_cache_mode &
  482. SSL_SESS_CACHE_NO_INTERNAL_STORE) == 0) {
  483. /*
  484. * Either return value of SSL_CTX_add_session should not
  485. * interrupt the session resumption process. The return
  486. * value is intentionally ignored.
  487. */
  488. (void)SSL_CTX_add_session(s->session_ctx, ret);
  489. }
  490. }
  491. }
  492. return ret;
  493. }
  494. /*-
  495. * ssl_get_prev attempts to find an SSL_SESSION to be used to resume this
  496. * connection. It is only called by servers.
  497. *
  498. * hello: The parsed ClientHello data
  499. *
  500. * Returns:
  501. * -1: fatal error
  502. * 0: no session found
  503. * 1: a session may have been found.
  504. *
  505. * Side effects:
  506. * - If a session is found then s->session is pointed at it (after freeing an
  507. * existing session if need be) and s->verify_result is set from the session.
  508. * - Both for new and resumed sessions, s->ext.ticket_expected is set to 1
  509. * if the server should issue a new session ticket (to 0 otherwise).
  510. */
  511. int ssl_get_prev_session(SSL_CONNECTION *s, CLIENTHELLO_MSG *hello)
  512. {
  513. /* This is used only by servers. */
  514. SSL_SESSION *ret = NULL;
  515. int fatal = 0;
  516. int try_session_cache = 0;
  517. SSL_TICKET_STATUS r;
  518. if (SSL_CONNECTION_IS_TLS13(s)) {
  519. /*
  520. * By default we will send a new ticket. This can be overridden in the
  521. * ticket processing.
  522. */
  523. s->ext.ticket_expected = 1;
  524. if (!tls_parse_extension(s, TLSEXT_IDX_psk_kex_modes,
  525. SSL_EXT_CLIENT_HELLO, hello->pre_proc_exts,
  526. NULL, 0)
  527. || !tls_parse_extension(s, TLSEXT_IDX_psk, SSL_EXT_CLIENT_HELLO,
  528. hello->pre_proc_exts, NULL, 0))
  529. return -1;
  530. ret = s->session;
  531. } else {
  532. /* sets s->ext.ticket_expected */
  533. r = tls_get_ticket_from_client(s, hello, &ret);
  534. switch (r) {
  535. case SSL_TICKET_FATAL_ERR_MALLOC:
  536. case SSL_TICKET_FATAL_ERR_OTHER:
  537. fatal = 1;
  538. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  539. goto err;
  540. case SSL_TICKET_NONE:
  541. case SSL_TICKET_EMPTY:
  542. if (hello->session_id_len > 0) {
  543. try_session_cache = 1;
  544. ret = lookup_sess_in_cache(s, hello->session_id,
  545. hello->session_id_len);
  546. }
  547. break;
  548. case SSL_TICKET_NO_DECRYPT:
  549. case SSL_TICKET_SUCCESS:
  550. case SSL_TICKET_SUCCESS_RENEW:
  551. break;
  552. }
  553. }
  554. if (ret == NULL)
  555. goto err;
  556. /* Now ret is non-NULL and we own one of its reference counts. */
  557. /* Check TLS version consistency */
  558. if (ret->ssl_version != s->version)
  559. goto err;
  560. if (ret->sid_ctx_length != s->sid_ctx_length
  561. || memcmp(ret->sid_ctx, s->sid_ctx, ret->sid_ctx_length)) {
  562. /*
  563. * We have the session requested by the client, but we don't want to
  564. * use it in this context.
  565. */
  566. goto err; /* treat like cache miss */
  567. }
  568. if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) {
  569. /*
  570. * We can't be sure if this session is being used out of context,
  571. * which is especially important for SSL_VERIFY_PEER. The application
  572. * should have used SSL[_CTX]_set_session_id_context. For this error
  573. * case, we generate an error instead of treating the event like a
  574. * cache miss (otherwise it would be easy for applications to
  575. * effectively disable the session cache by accident without anyone
  576. * noticing).
  577. */
  578. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  579. SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
  580. fatal = 1;
  581. goto err;
  582. }
  583. if (sess_timedout(ossl_time_now(), ret)) {
  584. ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_timeout);
  585. if (try_session_cache) {
  586. /* session was from the cache, so remove it */
  587. SSL_CTX_remove_session(s->session_ctx, ret);
  588. }
  589. goto err;
  590. }
  591. /* Check extended master secret extension consistency */
  592. if (ret->flags & SSL_SESS_FLAG_EXTMS) {
  593. /* If old session includes extms, but new does not: abort handshake */
  594. if (!(s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS)) {
  595. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_INCONSISTENT_EXTMS);
  596. fatal = 1;
  597. goto err;
  598. }
  599. } else if (s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) {
  600. /* If new session includes extms, but old does not: do not resume */
  601. goto err;
  602. }
  603. if (!SSL_CONNECTION_IS_TLS13(s)) {
  604. /* We already did this for TLS1.3 */
  605. SSL_SESSION_free(s->session);
  606. s->session = ret;
  607. }
  608. ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_hit);
  609. s->verify_result = s->session->verify_result;
  610. return 1;
  611. err:
  612. if (ret != NULL) {
  613. SSL_SESSION_free(ret);
  614. /* In TLSv1.3 s->session was already set to ret, so we NULL it out */
  615. if (SSL_CONNECTION_IS_TLS13(s))
  616. s->session = NULL;
  617. if (!try_session_cache) {
  618. /*
  619. * The session was from a ticket, so we should issue a ticket for
  620. * the new session
  621. */
  622. s->ext.ticket_expected = 1;
  623. }
  624. }
  625. if (fatal)
  626. return -1;
  627. return 0;
  628. }
  629. int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
  630. {
  631. int ret = 0;
  632. SSL_SESSION *s;
  633. /*
  634. * add just 1 reference count for the SSL_CTX's session cache even though
  635. * it has two ways of access: each session is in a doubly linked list and
  636. * an lhash
  637. */
  638. SSL_SESSION_up_ref(c);
  639. /*
  640. * if session c is in already in cache, we take back the increment later
  641. */
  642. if (!CRYPTO_THREAD_write_lock(ctx->lock)) {
  643. SSL_SESSION_free(c);
  644. return 0;
  645. }
  646. s = lh_SSL_SESSION_insert(ctx->sessions, c);
  647. /*
  648. * s != NULL iff we already had a session with the given PID. In this
  649. * case, s == c should hold (then we did not really modify
  650. * ctx->sessions), or we're in trouble.
  651. */
  652. if (s != NULL && s != c) {
  653. /* We *are* in trouble ... */
  654. SSL_SESSION_list_remove(ctx, s);
  655. SSL_SESSION_free(s);
  656. /*
  657. * ... so pretend the other session did not exist in cache (we cannot
  658. * handle two SSL_SESSION structures with identical session ID in the
  659. * same cache, which could happen e.g. when two threads concurrently
  660. * obtain the same session from an external cache)
  661. */
  662. s = NULL;
  663. } else if (s == NULL &&
  664. lh_SSL_SESSION_retrieve(ctx->sessions, c) == NULL) {
  665. /* s == NULL can also mean OOM error in lh_SSL_SESSION_insert ... */
  666. /*
  667. * ... so take back the extra reference and also don't add
  668. * the session to the SSL_SESSION_list at this time
  669. */
  670. s = c;
  671. }
  672. /* Adjust last used time, and add back into the cache at the appropriate spot */
  673. if (ctx->session_cache_mode & SSL_SESS_CACHE_UPDATE_TIME) {
  674. c->time = ossl_time_now();
  675. ssl_session_calculate_timeout(c);
  676. }
  677. if (s == NULL) {
  678. /*
  679. * new cache entry -- remove old ones if cache has become too large
  680. * delete cache entry *before* add, so we don't remove the one we're adding!
  681. */
  682. ret = 1;
  683. if (SSL_CTX_sess_get_cache_size(ctx) > 0) {
  684. while (SSL_CTX_sess_number(ctx) >= SSL_CTX_sess_get_cache_size(ctx)) {
  685. if (!remove_session_lock(ctx, ctx->session_cache_tail, 0))
  686. break;
  687. else
  688. ssl_tsan_counter(ctx, &ctx->stats.sess_cache_full);
  689. }
  690. }
  691. }
  692. SSL_SESSION_list_add(ctx, c);
  693. if (s != NULL) {
  694. /*
  695. * existing cache entry -- decrement previously incremented reference
  696. * count because it already takes into account the cache
  697. */
  698. SSL_SESSION_free(s); /* s == c */
  699. ret = 0;
  700. }
  701. CRYPTO_THREAD_unlock(ctx->lock);
  702. return ret;
  703. }
  704. int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
  705. {
  706. return remove_session_lock(ctx, c, 1);
  707. }
  708. static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
  709. {
  710. SSL_SESSION *r;
  711. int ret = 0;
  712. if ((c != NULL) && (c->session_id_length != 0)) {
  713. if (lck) {
  714. if (!CRYPTO_THREAD_write_lock(ctx->lock))
  715. return 0;
  716. }
  717. if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) != NULL) {
  718. ret = 1;
  719. r = lh_SSL_SESSION_delete(ctx->sessions, r);
  720. SSL_SESSION_list_remove(ctx, r);
  721. }
  722. c->not_resumable = 1;
  723. if (lck)
  724. CRYPTO_THREAD_unlock(ctx->lock);
  725. if (ctx->remove_session_cb != NULL)
  726. ctx->remove_session_cb(ctx, c);
  727. if (ret)
  728. SSL_SESSION_free(r);
  729. }
  730. return ret;
  731. }
  732. void SSL_SESSION_free(SSL_SESSION *ss)
  733. {
  734. int i;
  735. if (ss == NULL)
  736. return;
  737. CRYPTO_DOWN_REF(&ss->references, &i);
  738. REF_PRINT_COUNT("SSL_SESSION", i, ss);
  739. if (i > 0)
  740. return;
  741. REF_ASSERT_ISNT(i < 0);
  742. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
  743. OPENSSL_cleanse(ss->master_key, sizeof(ss->master_key));
  744. OPENSSL_cleanse(ss->session_id, sizeof(ss->session_id));
  745. X509_free(ss->peer);
  746. EVP_PKEY_free(ss->peer_rpk);
  747. OSSL_STACK_OF_X509_free(ss->peer_chain);
  748. OPENSSL_free(ss->ext.hostname);
  749. OPENSSL_free(ss->ext.tick);
  750. #ifndef OPENSSL_NO_PSK
  751. OPENSSL_free(ss->psk_identity_hint);
  752. OPENSSL_free(ss->psk_identity);
  753. #endif
  754. #ifndef OPENSSL_NO_SRP
  755. OPENSSL_free(ss->srp_username);
  756. #endif
  757. OPENSSL_free(ss->ext.alpn_selected);
  758. OPENSSL_free(ss->ticket_appdata);
  759. CRYPTO_FREE_REF(&ss->references);
  760. OPENSSL_clear_free(ss, sizeof(*ss));
  761. }
  762. int SSL_SESSION_up_ref(SSL_SESSION *ss)
  763. {
  764. int i;
  765. if (CRYPTO_UP_REF(&ss->references, &i) <= 0)
  766. return 0;
  767. REF_PRINT_COUNT("SSL_SESSION", i, ss);
  768. REF_ASSERT_ISNT(i < 2);
  769. return ((i > 1) ? 1 : 0);
  770. }
  771. int SSL_set_session(SSL *s, SSL_SESSION *session)
  772. {
  773. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  774. if (sc == NULL)
  775. return 0;
  776. ssl_clear_bad_session(sc);
  777. if (s->defltmeth != s->method) {
  778. if (!SSL_set_ssl_method(s, s->defltmeth))
  779. return 0;
  780. }
  781. if (session != NULL) {
  782. SSL_SESSION_up_ref(session);
  783. sc->verify_result = session->verify_result;
  784. }
  785. SSL_SESSION_free(sc->session);
  786. sc->session = session;
  787. return 1;
  788. }
  789. int SSL_SESSION_set1_id(SSL_SESSION *s, const unsigned char *sid,
  790. unsigned int sid_len)
  791. {
  792. if (sid_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
  793. ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_TOO_LONG);
  794. return 0;
  795. }
  796. s->session_id_length = sid_len;
  797. if (sid != s->session_id && sid_len > 0)
  798. memcpy(s->session_id, sid, sid_len);
  799. return 1;
  800. }
  801. long SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
  802. {
  803. OSSL_TIME new_timeout = ossl_seconds2time(t);
  804. if (s == NULL || t < 0)
  805. return 0;
  806. if (s->owner != NULL) {
  807. if (!CRYPTO_THREAD_write_lock(s->owner->lock))
  808. return 0;
  809. s->timeout = new_timeout;
  810. ssl_session_calculate_timeout(s);
  811. SSL_SESSION_list_add(s->owner, s);
  812. CRYPTO_THREAD_unlock(s->owner->lock);
  813. } else {
  814. s->timeout = new_timeout;
  815. ssl_session_calculate_timeout(s);
  816. }
  817. return 1;
  818. }
  819. long SSL_SESSION_get_timeout(const SSL_SESSION *s)
  820. {
  821. if (s == NULL)
  822. return 0;
  823. return (long)ossl_time_to_time_t(s->timeout);
  824. }
  825. long SSL_SESSION_get_time(const SSL_SESSION *s)
  826. {
  827. if (s == NULL)
  828. return 0;
  829. return (long)ossl_time_to_time_t(s->time);
  830. }
  831. long SSL_SESSION_set_time(SSL_SESSION *s, long t)
  832. {
  833. OSSL_TIME new_time = ossl_time_from_time_t((time_t)t);
  834. if (s == NULL)
  835. return 0;
  836. if (s->owner != NULL) {
  837. if (!CRYPTO_THREAD_write_lock(s->owner->lock))
  838. return 0;
  839. s->time = new_time;
  840. ssl_session_calculate_timeout(s);
  841. SSL_SESSION_list_add(s->owner, s);
  842. CRYPTO_THREAD_unlock(s->owner->lock);
  843. } else {
  844. s->time = new_time;
  845. ssl_session_calculate_timeout(s);
  846. }
  847. return t;
  848. }
  849. int SSL_SESSION_get_protocol_version(const SSL_SESSION *s)
  850. {
  851. return s->ssl_version;
  852. }
  853. int SSL_SESSION_set_protocol_version(SSL_SESSION *s, int version)
  854. {
  855. s->ssl_version = version;
  856. return 1;
  857. }
  858. const SSL_CIPHER *SSL_SESSION_get0_cipher(const SSL_SESSION *s)
  859. {
  860. return s->cipher;
  861. }
  862. int SSL_SESSION_set_cipher(SSL_SESSION *s, const SSL_CIPHER *cipher)
  863. {
  864. s->cipher = cipher;
  865. return 1;
  866. }
  867. const char *SSL_SESSION_get0_hostname(const SSL_SESSION *s)
  868. {
  869. return s->ext.hostname;
  870. }
  871. int SSL_SESSION_set1_hostname(SSL_SESSION *s, const char *hostname)
  872. {
  873. OPENSSL_free(s->ext.hostname);
  874. if (hostname == NULL) {
  875. s->ext.hostname = NULL;
  876. return 1;
  877. }
  878. s->ext.hostname = OPENSSL_strdup(hostname);
  879. return s->ext.hostname != NULL;
  880. }
  881. int SSL_SESSION_has_ticket(const SSL_SESSION *s)
  882. {
  883. return (s->ext.ticklen > 0) ? 1 : 0;
  884. }
  885. unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s)
  886. {
  887. return s->ext.tick_lifetime_hint;
  888. }
  889. void SSL_SESSION_get0_ticket(const SSL_SESSION *s, const unsigned char **tick,
  890. size_t *len)
  891. {
  892. *len = s->ext.ticklen;
  893. if (tick != NULL)
  894. *tick = s->ext.tick;
  895. }
  896. uint32_t SSL_SESSION_get_max_early_data(const SSL_SESSION *s)
  897. {
  898. return s->ext.max_early_data;
  899. }
  900. int SSL_SESSION_set_max_early_data(SSL_SESSION *s, uint32_t max_early_data)
  901. {
  902. s->ext.max_early_data = max_early_data;
  903. return 1;
  904. }
  905. void SSL_SESSION_get0_alpn_selected(const SSL_SESSION *s,
  906. const unsigned char **alpn,
  907. size_t *len)
  908. {
  909. *alpn = s->ext.alpn_selected;
  910. *len = s->ext.alpn_selected_len;
  911. }
  912. int SSL_SESSION_set1_alpn_selected(SSL_SESSION *s, const unsigned char *alpn,
  913. size_t len)
  914. {
  915. OPENSSL_free(s->ext.alpn_selected);
  916. if (alpn == NULL || len == 0) {
  917. s->ext.alpn_selected = NULL;
  918. s->ext.alpn_selected_len = 0;
  919. return 1;
  920. }
  921. s->ext.alpn_selected = OPENSSL_memdup(alpn, len);
  922. if (s->ext.alpn_selected == NULL) {
  923. s->ext.alpn_selected_len = 0;
  924. return 0;
  925. }
  926. s->ext.alpn_selected_len = len;
  927. return 1;
  928. }
  929. X509 *SSL_SESSION_get0_peer(SSL_SESSION *s)
  930. {
  931. return s->peer;
  932. }
  933. EVP_PKEY *SSL_SESSION_get0_peer_rpk(SSL_SESSION *s)
  934. {
  935. return s->peer_rpk;
  936. }
  937. int SSL_SESSION_set1_id_context(SSL_SESSION *s, const unsigned char *sid_ctx,
  938. unsigned int sid_ctx_len)
  939. {
  940. if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
  941. ERR_raise(ERR_LIB_SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  942. return 0;
  943. }
  944. s->sid_ctx_length = sid_ctx_len;
  945. if (sid_ctx != s->sid_ctx)
  946. memcpy(s->sid_ctx, sid_ctx, sid_ctx_len);
  947. return 1;
  948. }
  949. int SSL_SESSION_is_resumable(const SSL_SESSION *s)
  950. {
  951. /*
  952. * In the case of EAP-FAST, we can have a pre-shared "ticket" without a
  953. * session ID.
  954. */
  955. return !s->not_resumable
  956. && (s->session_id_length > 0 || s->ext.ticklen > 0);
  957. }
  958. long SSL_CTX_set_timeout(SSL_CTX *s, long t)
  959. {
  960. long l;
  961. if (s == NULL)
  962. return 0;
  963. l = (long)ossl_time2seconds(s->session_timeout);
  964. s->session_timeout = ossl_seconds2time(t);
  965. return l;
  966. }
  967. long SSL_CTX_get_timeout(const SSL_CTX *s)
  968. {
  969. if (s == NULL)
  970. return 0;
  971. return (long)ossl_time2seconds(s->session_timeout);
  972. }
  973. int SSL_set_session_secret_cb(SSL *s,
  974. tls_session_secret_cb_fn tls_session_secret_cb,
  975. void *arg)
  976. {
  977. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  978. if (sc == NULL)
  979. return 0;
  980. sc->ext.session_secret_cb = tls_session_secret_cb;
  981. sc->ext.session_secret_cb_arg = arg;
  982. return 1;
  983. }
  984. int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
  985. void *arg)
  986. {
  987. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  988. if (sc == NULL)
  989. return 0;
  990. sc->ext.session_ticket_cb = cb;
  991. sc->ext.session_ticket_cb_arg = arg;
  992. return 1;
  993. }
  994. int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
  995. {
  996. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  997. if (sc == NULL)
  998. return 0;
  999. if (sc->version >= TLS1_VERSION) {
  1000. OPENSSL_free(sc->ext.session_ticket);
  1001. sc->ext.session_ticket = NULL;
  1002. sc->ext.session_ticket =
  1003. OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
  1004. if (sc->ext.session_ticket == NULL)
  1005. return 0;
  1006. if (ext_data != NULL) {
  1007. sc->ext.session_ticket->length = ext_len;
  1008. sc->ext.session_ticket->data = sc->ext.session_ticket + 1;
  1009. memcpy(sc->ext.session_ticket->data, ext_data, ext_len);
  1010. } else {
  1011. sc->ext.session_ticket->length = 0;
  1012. sc->ext.session_ticket->data = NULL;
  1013. }
  1014. return 1;
  1015. }
  1016. return 0;
  1017. }
  1018. void SSL_CTX_flush_sessions(SSL_CTX *s, long t)
  1019. {
  1020. STACK_OF(SSL_SESSION) *sk;
  1021. SSL_SESSION *current;
  1022. unsigned long i;
  1023. const OSSL_TIME timeout = ossl_time_from_time_t(t);
  1024. if (!CRYPTO_THREAD_write_lock(s->lock))
  1025. return;
  1026. sk = sk_SSL_SESSION_new_null();
  1027. i = lh_SSL_SESSION_get_down_load(s->sessions);
  1028. lh_SSL_SESSION_set_down_load(s->sessions, 0);
  1029. /*
  1030. * Iterate over the list from the back (oldest), and stop
  1031. * when a session can no longer be removed.
  1032. * Add the session to a temporary list to be freed outside
  1033. * the SSL_CTX lock.
  1034. * But still do the remove_session_cb() within the lock.
  1035. */
  1036. while (s->session_cache_tail != NULL) {
  1037. current = s->session_cache_tail;
  1038. if (t == 0 || sess_timedout(timeout, current)) {
  1039. lh_SSL_SESSION_delete(s->sessions, current);
  1040. SSL_SESSION_list_remove(s, current);
  1041. current->not_resumable = 1;
  1042. if (s->remove_session_cb != NULL)
  1043. s->remove_session_cb(s, current);
  1044. /*
  1045. * Throw the session on a stack, it's entirely plausible
  1046. * that while freeing outside the critical section, the
  1047. * session could be re-added, so avoid using the next/prev
  1048. * pointers. If the stack failed to create, or the session
  1049. * couldn't be put on the stack, just free it here
  1050. */
  1051. if (sk == NULL || !sk_SSL_SESSION_push(sk, current))
  1052. SSL_SESSION_free(current);
  1053. } else {
  1054. break;
  1055. }
  1056. }
  1057. lh_SSL_SESSION_set_down_load(s->sessions, i);
  1058. CRYPTO_THREAD_unlock(s->lock);
  1059. sk_SSL_SESSION_pop_free(sk, SSL_SESSION_free);
  1060. }
  1061. int ssl_clear_bad_session(SSL_CONNECTION *s)
  1062. {
  1063. if ((s->session != NULL) &&
  1064. !(s->shutdown & SSL_SENT_SHUTDOWN) &&
  1065. !(SSL_in_init(SSL_CONNECTION_GET_SSL(s))
  1066. || SSL_in_before(SSL_CONNECTION_GET_SSL(s)))) {
  1067. SSL_CTX_remove_session(s->session_ctx, s->session);
  1068. return 1;
  1069. } else
  1070. return 0;
  1071. }
  1072. /* locked by SSL_CTX in the calling function */
  1073. static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
  1074. {
  1075. if ((s->next == NULL) || (s->prev == NULL))
  1076. return;
  1077. if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail)) {
  1078. /* last element in list */
  1079. if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
  1080. /* only one element in list */
  1081. ctx->session_cache_head = NULL;
  1082. ctx->session_cache_tail = NULL;
  1083. } else {
  1084. ctx->session_cache_tail = s->prev;
  1085. s->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail);
  1086. }
  1087. } else {
  1088. if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
  1089. /* first element in list */
  1090. ctx->session_cache_head = s->next;
  1091. s->next->prev = (SSL_SESSION *)&(ctx->session_cache_head);
  1092. } else {
  1093. /* middle of list */
  1094. s->next->prev = s->prev;
  1095. s->prev->next = s->next;
  1096. }
  1097. }
  1098. s->prev = s->next = NULL;
  1099. s->owner = NULL;
  1100. }
  1101. static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
  1102. {
  1103. SSL_SESSION *next;
  1104. if ((s->next != NULL) && (s->prev != NULL))
  1105. SSL_SESSION_list_remove(ctx, s);
  1106. if (ctx->session_cache_head == NULL) {
  1107. ctx->session_cache_head = s;
  1108. ctx->session_cache_tail = s;
  1109. s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
  1110. s->next = (SSL_SESSION *)&(ctx->session_cache_tail);
  1111. } else {
  1112. if (timeoutcmp(s, ctx->session_cache_head) >= 0) {
  1113. /*
  1114. * if we timeout after (or the same time as) the first
  1115. * session, put us first - usual case
  1116. */
  1117. s->next = ctx->session_cache_head;
  1118. s->next->prev = s;
  1119. s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
  1120. ctx->session_cache_head = s;
  1121. } else if (timeoutcmp(s, ctx->session_cache_tail) < 0) {
  1122. /* if we timeout before the last session, put us last */
  1123. s->prev = ctx->session_cache_tail;
  1124. s->prev->next = s;
  1125. s->next = (SSL_SESSION *)&(ctx->session_cache_tail);
  1126. ctx->session_cache_tail = s;
  1127. } else {
  1128. /*
  1129. * we timeout somewhere in-between - if there is only
  1130. * one session in the cache it will be caught above
  1131. */
  1132. next = ctx->session_cache_head->next;
  1133. while (next != (SSL_SESSION*)&(ctx->session_cache_tail)) {
  1134. if (timeoutcmp(s, next) >= 0) {
  1135. s->next = next;
  1136. s->prev = next->prev;
  1137. next->prev->next = s;
  1138. next->prev = s;
  1139. break;
  1140. }
  1141. next = next->next;
  1142. }
  1143. }
  1144. }
  1145. s->owner = ctx;
  1146. }
  1147. void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
  1148. int (*cb) (struct ssl_st *ssl, SSL_SESSION *sess))
  1149. {
  1150. ctx->new_session_cb = cb;
  1151. }
  1152. int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx)) (SSL *ssl, SSL_SESSION *sess) {
  1153. return ctx->new_session_cb;
  1154. }
  1155. void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
  1156. void (*cb) (SSL_CTX *ctx, SSL_SESSION *sess))
  1157. {
  1158. ctx->remove_session_cb = cb;
  1159. }
  1160. void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx)) (SSL_CTX *ctx,
  1161. SSL_SESSION *sess) {
  1162. return ctx->remove_session_cb;
  1163. }
  1164. void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
  1165. SSL_SESSION *(*cb) (SSL *ssl,
  1166. const unsigned char *data,
  1167. int len, int *copy))
  1168. {
  1169. ctx->get_session_cb = cb;
  1170. }
  1171. SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx)) (SSL *ssl,
  1172. const unsigned char
  1173. *data, int len,
  1174. int *copy) {
  1175. return ctx->get_session_cb;
  1176. }
  1177. void SSL_CTX_set_info_callback(SSL_CTX *ctx,
  1178. void (*cb) (const SSL *ssl, int type, int val))
  1179. {
  1180. ctx->info_callback = cb;
  1181. }
  1182. void (*SSL_CTX_get_info_callback(SSL_CTX *ctx)) (const SSL *ssl, int type,
  1183. int val) {
  1184. return ctx->info_callback;
  1185. }
  1186. void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
  1187. int (*cb) (SSL *ssl, X509 **x509,
  1188. EVP_PKEY **pkey))
  1189. {
  1190. ctx->client_cert_cb = cb;
  1191. }
  1192. int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx)) (SSL *ssl, X509 **x509,
  1193. EVP_PKEY **pkey) {
  1194. return ctx->client_cert_cb;
  1195. }
  1196. void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
  1197. int (*cb) (SSL *ssl,
  1198. unsigned char *cookie,
  1199. unsigned int *cookie_len))
  1200. {
  1201. ctx->app_gen_cookie_cb = cb;
  1202. }
  1203. void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
  1204. int (*cb) (SSL *ssl,
  1205. const unsigned char *cookie,
  1206. unsigned int cookie_len))
  1207. {
  1208. ctx->app_verify_cookie_cb = cb;
  1209. }
  1210. int SSL_SESSION_set1_ticket_appdata(SSL_SESSION *ss, const void *data, size_t len)
  1211. {
  1212. OPENSSL_free(ss->ticket_appdata);
  1213. ss->ticket_appdata_len = 0;
  1214. if (data == NULL || len == 0) {
  1215. ss->ticket_appdata = NULL;
  1216. return 1;
  1217. }
  1218. ss->ticket_appdata = OPENSSL_memdup(data, len);
  1219. if (ss->ticket_appdata != NULL) {
  1220. ss->ticket_appdata_len = len;
  1221. return 1;
  1222. }
  1223. return 0;
  1224. }
  1225. int SSL_SESSION_get0_ticket_appdata(SSL_SESSION *ss, void **data, size_t *len)
  1226. {
  1227. *data = ss->ticket_appdata;
  1228. *len = ss->ticket_appdata_len;
  1229. return 1;
  1230. }
  1231. void SSL_CTX_set_stateless_cookie_generate_cb(
  1232. SSL_CTX *ctx,
  1233. int (*cb) (SSL *ssl,
  1234. unsigned char *cookie,
  1235. size_t *cookie_len))
  1236. {
  1237. ctx->gen_stateless_cookie_cb = cb;
  1238. }
  1239. void SSL_CTX_set_stateless_cookie_verify_cb(
  1240. SSL_CTX *ctx,
  1241. int (*cb) (SSL *ssl,
  1242. const unsigned char *cookie,
  1243. size_t cookie_len))
  1244. {
  1245. ctx->verify_stateless_cookie_cb = cb;
  1246. }
  1247. IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)