ssl_sess.c 41 KB

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