ssl_sess.c 39 KB

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