2
0

ssl_sess.c 37 KB

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