ssl_sess.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  1. /* ssl/ssl_sess.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * openssl-core@openssl.org.
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * (eay@cryptsoft.com). This product includes software written by Tim
  108. * Hudson (tjh@cryptsoft.com).
  109. *
  110. */
  111. /* ====================================================================
  112. * Copyright 2005 Nokia. All rights reserved.
  113. *
  114. * The portions of the attached software ("Contribution") is developed by
  115. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  116. * license.
  117. *
  118. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  119. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  120. * support (see RFC 4279) to OpenSSL.
  121. *
  122. * No patent licenses or other rights except those expressly stated in
  123. * the OpenSSL open source license shall be deemed granted or received
  124. * expressly, by implication, estoppel, or otherwise.
  125. *
  126. * No assurances are provided by Nokia that the Contribution does not
  127. * infringe the patent or other intellectual property rights of any third
  128. * party or that the license provides you with all the necessary rights
  129. * to make use of the Contribution.
  130. *
  131. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  132. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  133. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  134. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  135. * OTHERWISE.
  136. */
  137. #include <stdio.h>
  138. #include <openssl/lhash.h>
  139. #include <openssl/rand.h>
  140. #ifndef OPENSSL_NO_ENGINE
  141. # include <openssl/engine.h>
  142. #endif
  143. #include "ssl_locl.h"
  144. static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s);
  145. static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s);
  146. static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck);
  147. SSL_SESSION *SSL_get_session(const SSL *ssl)
  148. /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */
  149. {
  150. return (ssl->session);
  151. }
  152. SSL_SESSION *SSL_get1_session(SSL *ssl)
  153. /* variant of SSL_get_session: caller really gets something */
  154. {
  155. SSL_SESSION *sess;
  156. /*
  157. * Need to lock this all up rather than just use CRYPTO_add so that
  158. * somebody doesn't free ssl->session between when we check it's non-null
  159. * and when we up the reference count.
  160. */
  161. CRYPTO_w_lock(CRYPTO_LOCK_SSL_SESSION);
  162. sess = ssl->session;
  163. if (sess)
  164. sess->references++;
  165. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_SESSION);
  166. return (sess);
  167. }
  168. int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg)
  169. {
  170. return (CRYPTO_set_ex_data(&s->ex_data, idx, arg));
  171. }
  172. void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx)
  173. {
  174. return (CRYPTO_get_ex_data(&s->ex_data, idx));
  175. }
  176. SSL_SESSION *SSL_SESSION_new(void)
  177. {
  178. SSL_SESSION *ss;
  179. ss = OPENSSL_zalloc(sizeof(*ss));
  180. if (ss == NULL) {
  181. SSLerr(SSL_F_SSL_SESSION_NEW, ERR_R_MALLOC_FAILURE);
  182. return (NULL);
  183. }
  184. ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */
  185. ss->references = 1;
  186. ss->timeout = 60 * 5 + 4; /* 5 minute timeout by default */
  187. ss->time = (unsigned long)time(NULL);
  188. CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
  189. return (ss);
  190. }
  191. /*
  192. * Create a new SSL_SESSION and duplicate the contents of |src| into it. If
  193. * ticket == 0 then no ticket information is duplicated, otherwise it is.
  194. */
  195. SSL_SESSION *ssl_session_dup(SSL_SESSION *src, int ticket)
  196. {
  197. SSL_SESSION *dest;
  198. dest = OPENSSL_malloc(sizeof(*src));
  199. if (dest == NULL) {
  200. goto err;
  201. }
  202. memcpy(dest, src, sizeof(*dest));
  203. /*
  204. * Set the various pointers to NULL so that we can call SSL_SESSION_free in
  205. * the case of an error whilst halfway through constructing dest
  206. */
  207. #ifndef OPENSSL_NO_PSK
  208. dest->psk_identity_hint = NULL;
  209. dest->psk_identity = NULL;
  210. #endif
  211. dest->ciphers = NULL;
  212. dest->tlsext_hostname = NULL;
  213. #ifndef OPENSSL_NO_EC
  214. dest->tlsext_ecpointformatlist = NULL;
  215. dest->tlsext_ellipticcurvelist = NULL;
  216. #endif
  217. dest->tlsext_tick = NULL;
  218. #ifndef OPENSSL_NO_SRP
  219. dest->srp_username = NULL;
  220. #endif
  221. memset(&dest->ex_data, 0, sizeof(dest->ex_data));
  222. /* We deliberately don't copy the prev and next pointers */
  223. dest->prev = NULL;
  224. dest->next = NULL;
  225. dest->references = 1;
  226. if (src->peer != NULL)
  227. X509_up_ref(src->peer);
  228. if (src->peer_chain != NULL) {
  229. dest->peer_chain = X509_chain_up_ref(src->peer_chain);
  230. if (dest->peer_chain == NULL)
  231. goto err;
  232. }
  233. #ifndef OPENSSL_NO_PSK
  234. if (src->psk_identity_hint) {
  235. dest->psk_identity_hint = OPENSSL_strdup(src->psk_identity_hint);
  236. if (dest->psk_identity_hint == NULL) {
  237. goto err;
  238. }
  239. }
  240. if (src->psk_identity) {
  241. dest->psk_identity = OPENSSL_strdup(src->psk_identity);
  242. if (dest->psk_identity == NULL) {
  243. goto err;
  244. }
  245. }
  246. #endif
  247. if(src->ciphers != NULL) {
  248. dest->ciphers = sk_SSL_CIPHER_dup(src->ciphers);
  249. if (dest->ciphers == NULL)
  250. goto err;
  251. }
  252. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL_SESSION,
  253. &dest->ex_data, &src->ex_data)) {
  254. goto err;
  255. }
  256. if (src->tlsext_hostname) {
  257. dest->tlsext_hostname = OPENSSL_strdup(src->tlsext_hostname);
  258. if (dest->tlsext_hostname == NULL) {
  259. goto err;
  260. }
  261. }
  262. #ifndef OPENSSL_NO_EC
  263. if (src->tlsext_ecpointformatlist) {
  264. dest->tlsext_ecpointformatlist =
  265. OPENSSL_memdup(src->tlsext_ecpointformatlist,
  266. src->tlsext_ecpointformatlist_length);
  267. if (dest->tlsext_ecpointformatlist == NULL)
  268. goto err;
  269. }
  270. if (src->tlsext_ellipticcurvelist) {
  271. dest->tlsext_ellipticcurvelist =
  272. OPENSSL_memdup(src->tlsext_ellipticcurvelist,
  273. src->tlsext_ellipticcurvelist_length);
  274. if (dest->tlsext_ellipticcurvelist == NULL)
  275. goto err;
  276. }
  277. #endif
  278. if (ticket != 0) {
  279. dest->tlsext_tick = OPENSSL_memdup(src->tlsext_tick, src->tlsext_ticklen);
  280. if(dest->tlsext_tick == NULL)
  281. goto err;
  282. } else {
  283. dest->tlsext_tick_lifetime_hint = 0;
  284. dest->tlsext_ticklen = 0;
  285. }
  286. #ifndef OPENSSL_NO_SRP
  287. if (src->srp_username) {
  288. dest->srp_username = OPENSSL_strdup(src->srp_username);
  289. if (dest->srp_username == NULL) {
  290. goto err;
  291. }
  292. }
  293. #endif
  294. return dest;
  295. err:
  296. SSLerr(SSL_F_SSL_SESSION_DUP, ERR_R_MALLOC_FAILURE);
  297. SSL_SESSION_free(dest);
  298. return NULL;
  299. }
  300. const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s,
  301. unsigned int *len)
  302. {
  303. if (len)
  304. *len = s->session_id_length;
  305. return s->session_id;
  306. }
  307. unsigned int SSL_SESSION_get_compress_id(const SSL_SESSION *s)
  308. {
  309. return s->compress_meth;
  310. }
  311. /*
  312. * SSLv3/TLSv1 has 32 bytes (256 bits) of session ID space. As such, filling
  313. * the ID with random junk repeatedly until we have no conflict is going to
  314. * complete in one iteration pretty much "most" of the time (btw:
  315. * understatement). So, if it takes us 10 iterations and we still can't avoid
  316. * a conflict - well that's a reasonable point to call it quits. Either the
  317. * RAND code is broken or someone is trying to open roughly very close to
  318. * 2^256 SSL sessions to our server. How you might store that many sessions
  319. * is perhaps a more interesting question ...
  320. */
  321. #define MAX_SESS_ID_ATTEMPTS 10
  322. static int def_generate_session_id(const SSL *ssl, unsigned char *id,
  323. unsigned int *id_len)
  324. {
  325. unsigned int retry = 0;
  326. do
  327. if (RAND_bytes(id, *id_len) <= 0)
  328. return 0;
  329. while (SSL_has_matching_session_id(ssl, id, *id_len) &&
  330. (++retry < MAX_SESS_ID_ATTEMPTS)) ;
  331. if (retry < MAX_SESS_ID_ATTEMPTS)
  332. return 1;
  333. /* else - woops a session_id match */
  334. /*
  335. * XXX We should also check the external cache -- but the probability of
  336. * a collision is negligible, and we could not prevent the concurrent
  337. * creation of sessions with identical IDs since we currently don't have
  338. * means to atomically check whether a session ID already exists and make
  339. * a reservation for it if it does not (this problem applies to the
  340. * internal cache as well).
  341. */
  342. return 0;
  343. }
  344. int ssl_get_new_session(SSL *s, int session)
  345. {
  346. /* This gets used by clients and servers. */
  347. unsigned int tmp;
  348. SSL_SESSION *ss = NULL;
  349. GEN_SESSION_CB cb = def_generate_session_id;
  350. if ((ss = SSL_SESSION_new()) == NULL)
  351. return (0);
  352. /* If the context has a default timeout, use it */
  353. if (s->session_ctx->session_timeout == 0)
  354. ss->timeout = SSL_get_default_timeout(s);
  355. else
  356. ss->timeout = s->session_ctx->session_timeout;
  357. SSL_SESSION_free(s->session);
  358. s->session = NULL;
  359. if (session) {
  360. if (s->version == SSL3_VERSION) {
  361. ss->ssl_version = SSL3_VERSION;
  362. ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
  363. } else if (s->version == TLS1_VERSION) {
  364. ss->ssl_version = TLS1_VERSION;
  365. ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
  366. } else if (s->version == TLS1_1_VERSION) {
  367. ss->ssl_version = TLS1_1_VERSION;
  368. ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
  369. } else if (s->version == TLS1_2_VERSION) {
  370. ss->ssl_version = TLS1_2_VERSION;
  371. ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
  372. } else if (s->version == DTLS1_BAD_VER) {
  373. ss->ssl_version = DTLS1_BAD_VER;
  374. ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
  375. } else if (s->version == DTLS1_VERSION) {
  376. ss->ssl_version = DTLS1_VERSION;
  377. ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
  378. } else if (s->version == DTLS1_2_VERSION) {
  379. ss->ssl_version = DTLS1_2_VERSION;
  380. ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
  381. } else {
  382. SSLerr(SSL_F_SSL_GET_NEW_SESSION, SSL_R_UNSUPPORTED_SSL_VERSION);
  383. SSL_SESSION_free(ss);
  384. return (0);
  385. }
  386. /*-
  387. * If RFC5077 ticket, use empty session ID (as server).
  388. * Note that:
  389. * (a) ssl_get_prev_session() does lookahead into the
  390. * ClientHello extensions to find the session ticket.
  391. * When ssl_get_prev_session() fails, s3_srvr.c calls
  392. * ssl_get_new_session() in ssl3_get_client_hello().
  393. * At that point, it has not yet parsed the extensions,
  394. * however, because of the lookahead, it already knows
  395. * whether a ticket is expected or not.
  396. *
  397. * (b) s3_clnt.c calls ssl_get_new_session() before parsing
  398. * ServerHello extensions, and before recording the session
  399. * ID received from the server, so this block is a noop.
  400. */
  401. if (s->tlsext_ticket_expected) {
  402. ss->session_id_length = 0;
  403. goto sess_id_done;
  404. }
  405. /* Choose which callback will set the session ID */
  406. CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
  407. if (s->generate_session_id)
  408. cb = s->generate_session_id;
  409. else if (s->session_ctx->generate_session_id)
  410. cb = s->session_ctx->generate_session_id;
  411. CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
  412. /* Choose a session ID */
  413. tmp = ss->session_id_length;
  414. if (!cb(s, ss->session_id, &tmp)) {
  415. /* The callback failed */
  416. SSLerr(SSL_F_SSL_GET_NEW_SESSION,
  417. SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
  418. SSL_SESSION_free(ss);
  419. return (0);
  420. }
  421. /*
  422. * Don't allow the callback to set the session length to zero. nor
  423. * set it higher than it was.
  424. */
  425. if (tmp == 0 || tmp > ss->session_id_length) {
  426. /* The callback set an illegal length */
  427. SSLerr(SSL_F_SSL_GET_NEW_SESSION,
  428. SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
  429. SSL_SESSION_free(ss);
  430. return (0);
  431. }
  432. ss->session_id_length = tmp;
  433. /* Finally, check for a conflict */
  434. if (SSL_has_matching_session_id(s, ss->session_id,
  435. ss->session_id_length)) {
  436. SSLerr(SSL_F_SSL_GET_NEW_SESSION, SSL_R_SSL_SESSION_ID_CONFLICT);
  437. SSL_SESSION_free(ss);
  438. return (0);
  439. }
  440. sess_id_done:
  441. if (s->tlsext_hostname) {
  442. ss->tlsext_hostname = OPENSSL_strdup(s->tlsext_hostname);
  443. if (ss->tlsext_hostname == NULL) {
  444. SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR);
  445. SSL_SESSION_free(ss);
  446. return 0;
  447. }
  448. }
  449. } else {
  450. ss->session_id_length = 0;
  451. }
  452. if (s->sid_ctx_length > sizeof ss->sid_ctx) {
  453. SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR);
  454. SSL_SESSION_free(ss);
  455. return 0;
  456. }
  457. memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length);
  458. ss->sid_ctx_length = s->sid_ctx_length;
  459. s->session = ss;
  460. ss->ssl_version = s->version;
  461. ss->verify_result = X509_V_OK;
  462. /* If client supports extended master secret set it in session */
  463. if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS)
  464. ss->flags |= SSL_SESS_FLAG_EXTMS;
  465. return (1);
  466. }
  467. /*-
  468. * ssl_get_prev attempts to find an SSL_SESSION to be used to resume this
  469. * connection. It is only called by servers.
  470. *
  471. * ext: ClientHello extensions (including length prefix)
  472. * session_id: ClientHello session ID.
  473. *
  474. * Returns:
  475. * -1: error
  476. * 0: a session may have been found.
  477. *
  478. * Side effects:
  479. * - If a session is found then s->session is pointed at it (after freeing an
  480. * existing session if need be) and s->verify_result is set from the session.
  481. * - Both for new and resumed sessions, s->tlsext_ticket_expected is set to 1
  482. * if the server should issue a new session ticket (to 0 otherwise).
  483. */
  484. int ssl_get_prev_session(SSL *s, const PACKET *ext, const PACKET *session_id)
  485. {
  486. /* This is used only by servers. */
  487. SSL_SESSION *ret = NULL;
  488. int fatal = 0;
  489. int try_session_cache = 1;
  490. int r;
  491. if (PACKET_remaining(session_id) == 0)
  492. try_session_cache = 0;
  493. /* sets s->tlsext_ticket_expected and extended master secret flag */
  494. r = tls_check_serverhello_tlsext_early(s, ext, session_id, &ret);
  495. switch (r) {
  496. case -1: /* Error during processing */
  497. fatal = 1;
  498. goto err;
  499. case 0: /* No ticket found */
  500. case 1: /* Zero length ticket found */
  501. break; /* Ok to carry on processing session id. */
  502. case 2: /* Ticket found but not decrypted. */
  503. case 3: /* Ticket decrypted, *ret has been set. */
  504. try_session_cache = 0;
  505. break;
  506. default:
  507. abort();
  508. }
  509. if (try_session_cache &&
  510. ret == NULL &&
  511. !(s->session_ctx->session_cache_mode &
  512. SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) {
  513. SSL_SESSION data;
  514. size_t local_len;
  515. data.ssl_version = s->version;
  516. if (!PACKET_copy_all(session_id, data.session_id,
  517. sizeof(data.session_id),
  518. &local_len)) {
  519. goto err;
  520. }
  521. data.session_id_length = local_len;
  522. CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
  523. ret = lh_SSL_SESSION_retrieve(s->session_ctx->sessions, &data);
  524. if (ret != NULL) {
  525. /* don't allow other threads to steal it: */
  526. CRYPTO_add(&ret->references, 1, CRYPTO_LOCK_SSL_SESSION);
  527. }
  528. CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
  529. if (ret == NULL)
  530. s->session_ctx->stats.sess_miss++;
  531. }
  532. if (try_session_cache &&
  533. ret == NULL && s->session_ctx->get_session_cb != NULL) {
  534. int copy = 1;
  535. /* The user callback takes a non-const pointer, so grab a local copy. */
  536. unsigned char *sid = NULL;
  537. size_t sid_len;
  538. if (!PACKET_memdup(session_id, &sid, &sid_len))
  539. goto err;
  540. ret = s->session_ctx->get_session_cb(s, sid, sid_len, &copy);
  541. OPENSSL_free(sid);
  542. if (ret != NULL) {
  543. s->session_ctx->stats.sess_cb_hit++;
  544. /*
  545. * Increment reference count now if the session callback asks us
  546. * to do so (note that if the session structures returned by the
  547. * callback are shared between threads, it must handle the
  548. * reference count itself [i.e. copy == 0], or things won't be
  549. * thread-safe).
  550. */
  551. if (copy)
  552. CRYPTO_add(&ret->references, 1, CRYPTO_LOCK_SSL_SESSION);
  553. /*
  554. * Add the externally cached session to the internal cache as
  555. * well if and only if we are supposed to.
  556. */
  557. if (!
  558. (s->session_ctx->session_cache_mode &
  559. SSL_SESS_CACHE_NO_INTERNAL_STORE)) {
  560. /*
  561. * The following should not return 1, otherwise, things are
  562. * very strange
  563. */
  564. if (SSL_CTX_add_session(s->session_ctx, ret))
  565. goto err;
  566. }
  567. }
  568. }
  569. if (ret == NULL)
  570. goto err;
  571. /* Now ret is non-NULL and we own one of its reference counts. */
  572. if (ret->sid_ctx_length != s->sid_ctx_length
  573. || memcmp(ret->sid_ctx, s->sid_ctx, ret->sid_ctx_length)) {
  574. /*
  575. * We have the session requested by the client, but we don't want to
  576. * use it in this context.
  577. */
  578. goto err; /* treat like cache miss */
  579. }
  580. if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) {
  581. /*
  582. * We can't be sure if this session is being used out of context,
  583. * which is especially important for SSL_VERIFY_PEER. The application
  584. * should have used SSL[_CTX]_set_session_id_context. For this error
  585. * case, we generate an error instead of treating the event like a
  586. * cache miss (otherwise it would be easy for applications to
  587. * effectively disable the session cache by accident without anyone
  588. * noticing).
  589. */
  590. SSLerr(SSL_F_SSL_GET_PREV_SESSION,
  591. SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
  592. fatal = 1;
  593. goto err;
  594. }
  595. if (ret->cipher == NULL) {
  596. unsigned char buf[5], *p;
  597. unsigned long l;
  598. p = buf;
  599. l = ret->cipher_id;
  600. l2n(l, p);
  601. if ((ret->ssl_version >> 8) >= SSL3_VERSION_MAJOR)
  602. ret->cipher = ssl_get_cipher_by_char(s, &(buf[2]));
  603. else
  604. ret->cipher = ssl_get_cipher_by_char(s, &(buf[1]));
  605. if (ret->cipher == NULL)
  606. goto err;
  607. }
  608. if (ret->timeout < (long)(time(NULL) - ret->time)) { /* timeout */
  609. s->session_ctx->stats.sess_timeout++;
  610. if (try_session_cache) {
  611. /* session was from the cache, so remove it */
  612. SSL_CTX_remove_session(s->session_ctx, ret);
  613. }
  614. goto err;
  615. }
  616. /* Check extended master secret extension consistency */
  617. if (ret->flags & SSL_SESS_FLAG_EXTMS) {
  618. /* If old session includes extms, but new does not: abort handshake */
  619. if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS)) {
  620. SSLerr(SSL_F_SSL_GET_PREV_SESSION, SSL_R_INCONSISTENT_EXTMS);
  621. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  622. fatal = 1;
  623. goto err;
  624. }
  625. } else if (s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) {
  626. /* If new session includes extms, but old does not: do not resume */
  627. goto err;
  628. }
  629. s->session_ctx->stats.sess_hit++;
  630. SSL_SESSION_free(s->session);
  631. s->session = ret;
  632. s->verify_result = s->session->verify_result;
  633. return 1;
  634. err:
  635. if (ret != NULL) {
  636. SSL_SESSION_free(ret);
  637. if (!try_session_cache) {
  638. /*
  639. * The session was from a ticket, so we should issue a ticket for
  640. * the new session
  641. */
  642. s->tlsext_ticket_expected = 1;
  643. }
  644. }
  645. if (fatal)
  646. return -1;
  647. else
  648. return 0;
  649. }
  650. int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
  651. {
  652. int ret = 0;
  653. SSL_SESSION *s;
  654. /*
  655. * add just 1 reference count for the SSL_CTX's session cache even though
  656. * it has two ways of access: each session is in a doubly linked list and
  657. * an lhash
  658. */
  659. CRYPTO_add(&c->references, 1, CRYPTO_LOCK_SSL_SESSION);
  660. /*
  661. * if session c is in already in cache, we take back the increment later
  662. */
  663. CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  664. s = lh_SSL_SESSION_insert(ctx->sessions, c);
  665. /*
  666. * s != NULL iff we already had a session with the given PID. In this
  667. * case, s == c should hold (then we did not really modify
  668. * ctx->sessions), or we're in trouble.
  669. */
  670. if (s != NULL && s != c) {
  671. /* We *are* in trouble ... */
  672. SSL_SESSION_list_remove(ctx, s);
  673. SSL_SESSION_free(s);
  674. /*
  675. * ... so pretend the other session did not exist in cache (we cannot
  676. * handle two SSL_SESSION structures with identical session ID in the
  677. * same cache, which could happen e.g. when two threads concurrently
  678. * obtain the same session from an external cache)
  679. */
  680. s = NULL;
  681. }
  682. /* Put at the head of the queue unless it is already in the cache */
  683. if (s == NULL)
  684. SSL_SESSION_list_add(ctx, c);
  685. if (s != NULL) {
  686. /*
  687. * existing cache entry -- decrement previously incremented reference
  688. * count because it already takes into account the cache
  689. */
  690. SSL_SESSION_free(s); /* s == c */
  691. ret = 0;
  692. } else {
  693. /*
  694. * new cache entry -- remove old ones if cache has become too large
  695. */
  696. ret = 1;
  697. if (SSL_CTX_sess_get_cache_size(ctx) > 0) {
  698. while (SSL_CTX_sess_number(ctx) >
  699. SSL_CTX_sess_get_cache_size(ctx)) {
  700. if (!remove_session_lock(ctx, ctx->session_cache_tail, 0))
  701. break;
  702. else
  703. ctx->stats.sess_cache_full++;
  704. }
  705. }
  706. }
  707. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  708. return (ret);
  709. }
  710. int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
  711. {
  712. return remove_session_lock(ctx, c, 1);
  713. }
  714. static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
  715. {
  716. SSL_SESSION *r;
  717. int ret = 0;
  718. if ((c != NULL) && (c->session_id_length != 0)) {
  719. if (lck)
  720. CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  721. if ((r = lh_SSL_SESSION_retrieve(ctx->sessions, c)) == c) {
  722. ret = 1;
  723. r = lh_SSL_SESSION_delete(ctx->sessions, c);
  724. SSL_SESSION_list_remove(ctx, c);
  725. }
  726. if (lck)
  727. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  728. if (ret) {
  729. r->not_resumable = 1;
  730. if (ctx->remove_session_cb != NULL)
  731. ctx->remove_session_cb(ctx, r);
  732. SSL_SESSION_free(r);
  733. }
  734. } else
  735. ret = 0;
  736. return (ret);
  737. }
  738. void SSL_SESSION_free(SSL_SESSION *ss)
  739. {
  740. int i;
  741. if (ss == NULL)
  742. return;
  743. i = CRYPTO_add(&ss->references, -1, CRYPTO_LOCK_SSL_SESSION);
  744. #ifdef REF_PRINT
  745. REF_PRINT("SSL_SESSION", ss);
  746. #endif
  747. if (i > 0)
  748. return;
  749. #ifdef REF_CHECK
  750. if (i < 0) {
  751. fprintf(stderr, "SSL_SESSION_free, bad reference count\n");
  752. abort(); /* ok */
  753. }
  754. #endif
  755. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
  756. OPENSSL_cleanse(ss->master_key, sizeof ss->master_key);
  757. OPENSSL_cleanse(ss->session_id, sizeof ss->session_id);
  758. X509_free(ss->peer);
  759. sk_X509_pop_free(ss->peer_chain, X509_free);
  760. sk_SSL_CIPHER_free(ss->ciphers);
  761. OPENSSL_free(ss->tlsext_hostname);
  762. OPENSSL_free(ss->tlsext_tick);
  763. #ifndef OPENSSL_NO_EC
  764. ss->tlsext_ecpointformatlist_length = 0;
  765. OPENSSL_free(ss->tlsext_ecpointformatlist);
  766. ss->tlsext_ellipticcurvelist_length = 0;
  767. OPENSSL_free(ss->tlsext_ellipticcurvelist);
  768. #endif /* OPENSSL_NO_EC */
  769. #ifndef OPENSSL_NO_PSK
  770. OPENSSL_free(ss->psk_identity_hint);
  771. OPENSSL_free(ss->psk_identity);
  772. #endif
  773. #ifndef OPENSSL_NO_SRP
  774. OPENSSL_free(ss->srp_username);
  775. #endif
  776. OPENSSL_clear_free(ss, sizeof(*ss));
  777. }
  778. int SSL_set_session(SSL *s, SSL_SESSION *session)
  779. {
  780. int ret = 0;
  781. const SSL_METHOD *meth;
  782. if (session != NULL) {
  783. meth = s->ctx->method->get_ssl_method(session->ssl_version);
  784. if (meth == NULL)
  785. meth = s->method->get_ssl_method(session->ssl_version);
  786. if (meth == NULL) {
  787. SSLerr(SSL_F_SSL_SET_SESSION, SSL_R_UNABLE_TO_FIND_SSL_METHOD);
  788. return (0);
  789. }
  790. if (meth != s->method) {
  791. if (!SSL_set_ssl_method(s, meth))
  792. return (0);
  793. }
  794. /* CRYPTO_w_lock(CRYPTO_LOCK_SSL); */
  795. CRYPTO_add(&session->references, 1, CRYPTO_LOCK_SSL_SESSION);
  796. SSL_SESSION_free(s->session);
  797. s->session = session;
  798. s->verify_result = s->session->verify_result;
  799. /* CRYPTO_w_unlock(CRYPTO_LOCK_SSL); */
  800. ret = 1;
  801. } else {
  802. SSL_SESSION_free(s->session);
  803. s->session = NULL;
  804. meth = s->ctx->method;
  805. if (meth != s->method) {
  806. if (!SSL_set_ssl_method(s, meth))
  807. return (0);
  808. }
  809. ret = 1;
  810. }
  811. return (ret);
  812. }
  813. long SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
  814. {
  815. if (s == NULL)
  816. return (0);
  817. s->timeout = t;
  818. return (1);
  819. }
  820. long SSL_SESSION_get_timeout(const SSL_SESSION *s)
  821. {
  822. if (s == NULL)
  823. return (0);
  824. return (s->timeout);
  825. }
  826. long SSL_SESSION_get_time(const SSL_SESSION *s)
  827. {
  828. if (s == NULL)
  829. return (0);
  830. return (s->time);
  831. }
  832. long SSL_SESSION_set_time(SSL_SESSION *s, long t)
  833. {
  834. if (s == NULL)
  835. return (0);
  836. s->time = t;
  837. return (t);
  838. }
  839. int SSL_SESSION_has_ticket(const SSL_SESSION *s)
  840. {
  841. return (s->tlsext_ticklen > 0) ? 1 : 0;
  842. }
  843. unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION *s)
  844. {
  845. return s->tlsext_tick_lifetime_hint;
  846. }
  847. void SSL_SESSION_get0_ticket(const SSL_SESSION *s, unsigned char **tick,
  848. size_t *len)
  849. {
  850. *len = s->tlsext_ticklen;
  851. if (tick != NULL)
  852. *tick = s->tlsext_tick;
  853. }
  854. X509 *SSL_SESSION_get0_peer(SSL_SESSION *s)
  855. {
  856. return s->peer;
  857. }
  858. int SSL_SESSION_set1_id_context(SSL_SESSION *s, const unsigned char *sid_ctx,
  859. unsigned int sid_ctx_len)
  860. {
  861. if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
  862. SSLerr(SSL_F_SSL_SESSION_SET1_ID_CONTEXT,
  863. SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  864. return 0;
  865. }
  866. s->sid_ctx_length = sid_ctx_len;
  867. memcpy(s->sid_ctx, sid_ctx, sid_ctx_len);
  868. return 1;
  869. }
  870. long SSL_CTX_set_timeout(SSL_CTX *s, long t)
  871. {
  872. long l;
  873. if (s == NULL)
  874. return (0);
  875. l = s->session_timeout;
  876. s->session_timeout = t;
  877. return (l);
  878. }
  879. long SSL_CTX_get_timeout(const SSL_CTX *s)
  880. {
  881. if (s == NULL)
  882. return (0);
  883. return (s->session_timeout);
  884. }
  885. int SSL_set_session_secret_cb(SSL *s,
  886. int (*tls_session_secret_cb) (SSL *s,
  887. void *secret,
  888. int *secret_len,
  889. STACK_OF(SSL_CIPHER)
  890. *peer_ciphers,
  891. const SSL_CIPHER
  892. **cipher,
  893. void *arg),
  894. void *arg)
  895. {
  896. if (s == NULL)
  897. return (0);
  898. s->tls_session_secret_cb = tls_session_secret_cb;
  899. s->tls_session_secret_cb_arg = arg;
  900. return (1);
  901. }
  902. int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
  903. void *arg)
  904. {
  905. if (s == NULL)
  906. return (0);
  907. s->tls_session_ticket_ext_cb = cb;
  908. s->tls_session_ticket_ext_cb_arg = arg;
  909. return (1);
  910. }
  911. int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
  912. {
  913. if (s->version >= TLS1_VERSION) {
  914. OPENSSL_free(s->tlsext_session_ticket);
  915. s->tlsext_session_ticket = NULL;
  916. s->tlsext_session_ticket =
  917. OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
  918. if (s->tlsext_session_ticket == NULL) {
  919. SSLerr(SSL_F_SSL_SET_SESSION_TICKET_EXT, ERR_R_MALLOC_FAILURE);
  920. return 0;
  921. }
  922. if (ext_data) {
  923. s->tlsext_session_ticket->length = ext_len;
  924. s->tlsext_session_ticket->data = s->tlsext_session_ticket + 1;
  925. memcpy(s->tlsext_session_ticket->data, ext_data, ext_len);
  926. } else {
  927. s->tlsext_session_ticket->length = 0;
  928. s->tlsext_session_ticket->data = NULL;
  929. }
  930. return 1;
  931. }
  932. return 0;
  933. }
  934. typedef struct timeout_param_st {
  935. SSL_CTX *ctx;
  936. long time;
  937. LHASH_OF(SSL_SESSION) *cache;
  938. } TIMEOUT_PARAM;
  939. static void timeout_cb(SSL_SESSION *s, TIMEOUT_PARAM *p)
  940. {
  941. if ((p->time == 0) || (p->time > (s->time + s->timeout))) { /* timeout */
  942. /*
  943. * The reason we don't call SSL_CTX_remove_session() is to save on
  944. * locking overhead
  945. */
  946. (void)lh_SSL_SESSION_delete(p->cache, s);
  947. SSL_SESSION_list_remove(p->ctx, s);
  948. s->not_resumable = 1;
  949. if (p->ctx->remove_session_cb != NULL)
  950. p->ctx->remove_session_cb(p->ctx, s);
  951. SSL_SESSION_free(s);
  952. }
  953. }
  954. IMPLEMENT_LHASH_DOALL_ARG(SSL_SESSION, TIMEOUT_PARAM);
  955. void SSL_CTX_flush_sessions(SSL_CTX *s, long t)
  956. {
  957. unsigned long i;
  958. TIMEOUT_PARAM tp;
  959. tp.ctx = s;
  960. tp.cache = s->sessions;
  961. if (tp.cache == NULL)
  962. return;
  963. tp.time = t;
  964. CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  965. i = CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load;
  966. CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load = 0;
  967. lh_SSL_SESSION_doall_TIMEOUT_PARAM(tp.cache, timeout_cb, &tp);
  968. CHECKED_LHASH_OF(SSL_SESSION, tp.cache)->down_load = i;
  969. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  970. }
  971. int ssl_clear_bad_session(SSL *s)
  972. {
  973. if ((s->session != NULL) &&
  974. !(s->shutdown & SSL_SENT_SHUTDOWN) &&
  975. !(SSL_in_init(s) || SSL_in_before(s))) {
  976. SSL_CTX_remove_session(s->ctx, s->session);
  977. return (1);
  978. } else
  979. return (0);
  980. }
  981. /* locked by SSL_CTX in the calling function */
  982. static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
  983. {
  984. if ((s->next == NULL) || (s->prev == NULL))
  985. return;
  986. if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail)) {
  987. /* last element in list */
  988. if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
  989. /* only one element in list */
  990. ctx->session_cache_head = NULL;
  991. ctx->session_cache_tail = NULL;
  992. } else {
  993. ctx->session_cache_tail = s->prev;
  994. s->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail);
  995. }
  996. } else {
  997. if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head)) {
  998. /* first element in list */
  999. ctx->session_cache_head = s->next;
  1000. s->next->prev = (SSL_SESSION *)&(ctx->session_cache_head);
  1001. } else {
  1002. /* middle of list */
  1003. s->next->prev = s->prev;
  1004. s->prev->next = s->next;
  1005. }
  1006. }
  1007. s->prev = s->next = NULL;
  1008. }
  1009. static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
  1010. {
  1011. if ((s->next != NULL) && (s->prev != NULL))
  1012. SSL_SESSION_list_remove(ctx, s);
  1013. if (ctx->session_cache_head == NULL) {
  1014. ctx->session_cache_head = s;
  1015. ctx->session_cache_tail = s;
  1016. s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
  1017. s->next = (SSL_SESSION *)&(ctx->session_cache_tail);
  1018. } else {
  1019. s->next = ctx->session_cache_head;
  1020. s->next->prev = s;
  1021. s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
  1022. ctx->session_cache_head = s;
  1023. }
  1024. }
  1025. void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
  1026. int (*cb) (struct ssl_st *ssl,
  1027. SSL_SESSION *sess))
  1028. {
  1029. ctx->new_session_cb = cb;
  1030. }
  1031. int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx)) (SSL *ssl, SSL_SESSION *sess) {
  1032. return ctx->new_session_cb;
  1033. }
  1034. void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
  1035. void (*cb) (SSL_CTX *ctx, SSL_SESSION *sess))
  1036. {
  1037. ctx->remove_session_cb = cb;
  1038. }
  1039. void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx)) (SSL_CTX *ctx,
  1040. SSL_SESSION *sess) {
  1041. return ctx->remove_session_cb;
  1042. }
  1043. void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
  1044. SSL_SESSION *(*cb) (struct ssl_st *ssl,
  1045. unsigned char *data, int len,
  1046. int *copy))
  1047. {
  1048. ctx->get_session_cb = cb;
  1049. }
  1050. SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx)) (SSL *ssl,
  1051. unsigned char *data,
  1052. int len, int *copy) {
  1053. return ctx->get_session_cb;
  1054. }
  1055. void SSL_CTX_set_info_callback(SSL_CTX *ctx,
  1056. void (*cb) (const SSL *ssl, int type, int val))
  1057. {
  1058. ctx->info_callback = cb;
  1059. }
  1060. void (*SSL_CTX_get_info_callback(SSL_CTX *ctx)) (const SSL *ssl, int type,
  1061. int val) {
  1062. return ctx->info_callback;
  1063. }
  1064. void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
  1065. int (*cb) (SSL *ssl, X509 **x509,
  1066. EVP_PKEY **pkey))
  1067. {
  1068. ctx->client_cert_cb = cb;
  1069. }
  1070. int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx)) (SSL *ssl, X509 **x509,
  1071. EVP_PKEY **pkey) {
  1072. return ctx->client_cert_cb;
  1073. }
  1074. #ifndef OPENSSL_NO_ENGINE
  1075. int SSL_CTX_set_client_cert_engine(SSL_CTX *ctx, ENGINE *e)
  1076. {
  1077. if (!ENGINE_init(e)) {
  1078. SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE, ERR_R_ENGINE_LIB);
  1079. return 0;
  1080. }
  1081. if (!ENGINE_get_ssl_client_cert_function(e)) {
  1082. SSLerr(SSL_F_SSL_CTX_SET_CLIENT_CERT_ENGINE,
  1083. SSL_R_NO_CLIENT_CERT_METHOD);
  1084. ENGINE_finish(e);
  1085. return 0;
  1086. }
  1087. ctx->client_cert_engine = e;
  1088. return 1;
  1089. }
  1090. #endif
  1091. void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
  1092. int (*cb) (SSL *ssl,
  1093. unsigned char *cookie,
  1094. unsigned int *cookie_len))
  1095. {
  1096. ctx->app_gen_cookie_cb = cb;
  1097. }
  1098. void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
  1099. int (*cb) (SSL *ssl, const unsigned char *cookie,
  1100. unsigned int cookie_len))
  1101. {
  1102. ctx->app_verify_cookie_cb = cb;
  1103. }
  1104. IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION,
  1105. SSL_SESSION)