ssl_sess.c 41 KB

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