ssl_sess.c 39 KB

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