ssl_sess.c 41 KB

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