ssl_sess.c 41 KB

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