ssl_sess.c 37 KB

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