ssl_sess.c 40 KB

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