ssl_sess.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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. #include <stdio.h>
  59. #include <openssl/lhash.h>
  60. #include <openssl/rand.h>
  61. #include "ssl_locl.h"
  62. static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s);
  63. static void SSL_SESSION_list_add(SSL_CTX *ctx,SSL_SESSION *s);
  64. static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck);
  65. SSL_SESSION *SSL_get_session(const SSL *ssl)
  66. /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */
  67. {
  68. return(ssl->session);
  69. }
  70. SSL_SESSION *SSL_get1_session(SSL *ssl)
  71. /* variant of SSL_get_session: caller really gets something */
  72. {
  73. SSL_SESSION *sess;
  74. /* Need to lock this all up rather than just use CRYPTO_add so that
  75. * somebody doesn't free ssl->session between when we check it's
  76. * non-null and when we up the reference count. */
  77. CRYPTO_w_lock(CRYPTO_LOCK_SSL_SESSION);
  78. sess = ssl->session;
  79. if(sess)
  80. sess->references++;
  81. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_SESSION);
  82. return(sess);
  83. }
  84. int SSL_SESSION_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
  85. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
  86. {
  87. return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_SESSION, argl, argp,
  88. new_func, dup_func, free_func);
  89. }
  90. int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg)
  91. {
  92. return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
  93. }
  94. void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx)
  95. {
  96. return(CRYPTO_get_ex_data(&s->ex_data,idx));
  97. }
  98. SSL_SESSION *SSL_SESSION_new(void)
  99. {
  100. SSL_SESSION *ss;
  101. ss=(SSL_SESSION *)OPENSSL_malloc(sizeof(SSL_SESSION));
  102. if (ss == NULL)
  103. {
  104. SSLerr(SSL_F_SSL_SESSION_NEW,ERR_R_MALLOC_FAILURE);
  105. return(0);
  106. }
  107. memset(ss,0,sizeof(SSL_SESSION));
  108. ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */
  109. ss->references=1;
  110. ss->timeout=60*5+4; /* 5 minute timeout by default */
  111. ss->time=(unsigned long)time(NULL);
  112. ss->prev=NULL;
  113. ss->next=NULL;
  114. ss->compress_meth=0;
  115. CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
  116. return(ss);
  117. }
  118. const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len)
  119. {
  120. if(len)
  121. *len = s->session_id_length;
  122. return s->session_id;
  123. }
  124. /* Even with SSLv2, we have 16 bytes (128 bits) of session ID space. SSLv3/TLSv1
  125. * has 32 bytes (256 bits). As such, filling the ID with random gunk repeatedly
  126. * until we have no conflict is going to complete in one iteration pretty much
  127. * "most" of the time (btw: understatement). So, if it takes us 10 iterations
  128. * and we still can't avoid a conflict - well that's a reasonable point to call
  129. * it quits. Either the RAND code is broken or someone is trying to open roughly
  130. * very close to 2^128 (or 2^256) SSL sessions to our server. How you might
  131. * store that many sessions is perhaps a more interesting question ... */
  132. #define MAX_SESS_ID_ATTEMPTS 10
  133. static int def_generate_session_id(const SSL *ssl, unsigned char *id,
  134. unsigned int *id_len)
  135. {
  136. unsigned int retry = 0;
  137. do
  138. if (RAND_pseudo_bytes(id, *id_len) <= 0)
  139. return 0;
  140. while(SSL_has_matching_session_id(ssl, id, *id_len) &&
  141. (++retry < MAX_SESS_ID_ATTEMPTS));
  142. if(retry < MAX_SESS_ID_ATTEMPTS)
  143. return 1;
  144. /* else - woops a session_id match */
  145. /* XXX We should also check the external cache --
  146. * but the probability of a collision is negligible, and
  147. * we could not prevent the concurrent creation of sessions
  148. * with identical IDs since we currently don't have means
  149. * to atomically check whether a session ID already exists
  150. * and make a reservation for it if it does not
  151. * (this problem applies to the internal cache as well).
  152. */
  153. return 0;
  154. }
  155. int ssl_get_new_session(SSL *s, int session)
  156. {
  157. /* This gets used by clients and servers. */
  158. unsigned int tmp;
  159. SSL_SESSION *ss=NULL;
  160. GEN_SESSION_CB cb = def_generate_session_id;
  161. if ((ss=SSL_SESSION_new()) == NULL) return(0);
  162. /* If the context has a default timeout, use it */
  163. if (s->ctx->session_timeout == 0)
  164. ss->timeout=SSL_get_default_timeout(s);
  165. else
  166. ss->timeout=s->ctx->session_timeout;
  167. if (s->session != NULL)
  168. {
  169. SSL_SESSION_free(s->session);
  170. s->session=NULL;
  171. }
  172. if (session)
  173. {
  174. if (s->version == SSL2_VERSION)
  175. {
  176. ss->ssl_version=SSL2_VERSION;
  177. ss->session_id_length=SSL2_SSL_SESSION_ID_LENGTH;
  178. }
  179. else if (s->version == SSL3_VERSION)
  180. {
  181. ss->ssl_version=SSL3_VERSION;
  182. ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
  183. }
  184. else if (s->version == TLS1_VERSION)
  185. {
  186. ss->ssl_version=TLS1_VERSION;
  187. ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
  188. }
  189. else if (s->version == DTLS1_VERSION)
  190. {
  191. ss->ssl_version=DTLS1_VERSION;
  192. ss->session_id_length=SSL3_SSL_SESSION_ID_LENGTH;
  193. }
  194. else
  195. {
  196. SSLerr(SSL_F_SSL_GET_NEW_SESSION,SSL_R_UNSUPPORTED_SSL_VERSION);
  197. SSL_SESSION_free(ss);
  198. return(0);
  199. }
  200. /* Choose which callback will set the session ID */
  201. CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
  202. if(s->generate_session_id)
  203. cb = s->generate_session_id;
  204. else if(s->ctx->generate_session_id)
  205. cb = s->ctx->generate_session_id;
  206. CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
  207. /* Choose a session ID */
  208. tmp = ss->session_id_length;
  209. if(!cb(s, ss->session_id, &tmp))
  210. {
  211. /* The callback failed */
  212. SSLerr(SSL_F_SSL_GET_NEW_SESSION,
  213. SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
  214. SSL_SESSION_free(ss);
  215. return(0);
  216. }
  217. /* Don't allow the callback to set the session length to zero.
  218. * nor set it higher than it was. */
  219. if(!tmp || (tmp > ss->session_id_length))
  220. {
  221. /* The callback set an illegal length */
  222. SSLerr(SSL_F_SSL_GET_NEW_SESSION,
  223. SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
  224. SSL_SESSION_free(ss);
  225. return(0);
  226. }
  227. /* If the session length was shrunk and we're SSLv2, pad it */
  228. if((tmp < ss->session_id_length) && (s->version == SSL2_VERSION))
  229. memset(ss->session_id + tmp, 0, ss->session_id_length - tmp);
  230. else
  231. ss->session_id_length = tmp;
  232. /* Finally, check for a conflict */
  233. if(SSL_has_matching_session_id(s, ss->session_id,
  234. ss->session_id_length))
  235. {
  236. SSLerr(SSL_F_SSL_GET_NEW_SESSION,
  237. SSL_R_SSL_SESSION_ID_CONFLICT);
  238. SSL_SESSION_free(ss);
  239. return(0);
  240. }
  241. }
  242. else
  243. {
  244. ss->session_id_length=0;
  245. }
  246. if (s->sid_ctx_length > sizeof ss->sid_ctx)
  247. {
  248. SSLerr(SSL_F_SSL_GET_NEW_SESSION, ERR_R_INTERNAL_ERROR);
  249. SSL_SESSION_free(ss);
  250. return 0;
  251. }
  252. memcpy(ss->sid_ctx,s->sid_ctx,s->sid_ctx_length);
  253. ss->sid_ctx_length=s->sid_ctx_length;
  254. s->session=ss;
  255. ss->ssl_version=s->version;
  256. ss->verify_result = X509_V_OK;
  257. return(1);
  258. }
  259. int ssl_get_prev_session(SSL *s, unsigned char *session_id, int len)
  260. {
  261. /* This is used only by servers. */
  262. SSL_SESSION *ret=NULL,data;
  263. int fatal = 0;
  264. data.ssl_version=s->version;
  265. data.session_id_length=len;
  266. if (len > SSL_MAX_SSL_SESSION_ID_LENGTH)
  267. goto err;
  268. memcpy(data.session_id,session_id,len);
  269. if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP))
  270. {
  271. CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
  272. ret=(SSL_SESSION *)lh_retrieve(s->ctx->sessions,&data);
  273. if (ret != NULL)
  274. /* don't allow other threads to steal it: */
  275. CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
  276. CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
  277. }
  278. if (ret == NULL)
  279. {
  280. int copy=1;
  281. s->ctx->stats.sess_miss++;
  282. ret=NULL;
  283. if (s->ctx->get_session_cb != NULL
  284. && (ret=s->ctx->get_session_cb(s,session_id,len,&copy))
  285. != NULL)
  286. {
  287. s->ctx->stats.sess_cb_hit++;
  288. /* Increment reference count now if the session callback
  289. * asks us to do so (note that if the session structures
  290. * returned by the callback are shared between threads,
  291. * it must handle the reference count itself [i.e. copy == 0],
  292. * or things won't be thread-safe). */
  293. if (copy)
  294. CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
  295. /* Add the externally cached session to the internal
  296. * cache as well if and only if we are supposed to. */
  297. if(!(s->ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE))
  298. /* The following should not return 1, otherwise,
  299. * things are very strange */
  300. SSL_CTX_add_session(s->ctx,ret);
  301. }
  302. if (ret == NULL)
  303. goto err;
  304. }
  305. /* Now ret is non-NULL, and we own one of its reference counts. */
  306. if (ret->sid_ctx_length != s->sid_ctx_length
  307. || memcmp(ret->sid_ctx,s->sid_ctx,ret->sid_ctx_length))
  308. {
  309. /* We've found the session named by the client, but we don't
  310. * want to use it in this context. */
  311. #if 0 /* The client cannot always know when a session is not appropriate,
  312. * so we shouldn't generate an error message. */
  313. SSLerr(SSL_F_SSL_GET_PREV_SESSION,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
  314. #endif
  315. goto err; /* treat like cache miss */
  316. }
  317. if((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0)
  318. {
  319. /* We can't be sure if this session is being used out of
  320. * context, which is especially important for SSL_VERIFY_PEER.
  321. * The application should have used SSL[_CTX]_set_session_id_context.
  322. *
  323. * For this error case, we generate an error instead of treating
  324. * the event like a cache miss (otherwise it would be easy for
  325. * applications to effectively disable the session cache by
  326. * accident without anyone noticing).
  327. */
  328. SSLerr(SSL_F_SSL_GET_PREV_SESSION,SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
  329. fatal = 1;
  330. goto err;
  331. }
  332. if (ret->cipher == NULL)
  333. {
  334. unsigned char buf[5],*p;
  335. unsigned long l;
  336. p=buf;
  337. l=ret->cipher_id;
  338. l2n(l,p);
  339. if ((ret->ssl_version>>8) == SSL3_VERSION_MAJOR)
  340. ret->cipher=ssl_get_cipher_by_char(s,&(buf[2]));
  341. else
  342. ret->cipher=ssl_get_cipher_by_char(s,&(buf[1]));
  343. if (ret->cipher == NULL)
  344. goto err;
  345. }
  346. #if 0 /* This is way too late. */
  347. /* If a thread got the session, then 'swaped', and another got
  348. * it and then due to a time-out decided to 'OPENSSL_free' it we could
  349. * be in trouble. So I'll increment it now, then double decrement
  350. * later - am I speaking rubbish?. */
  351. CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
  352. #endif
  353. if (ret->timeout < (long)(time(NULL) - ret->time)) /* timeout */
  354. {
  355. s->ctx->stats.sess_timeout++;
  356. /* remove it from the cache */
  357. SSL_CTX_remove_session(s->ctx,ret);
  358. goto err;
  359. }
  360. s->ctx->stats.sess_hit++;
  361. /* ret->time=time(NULL); */ /* rezero timeout? */
  362. /* again, just leave the session
  363. * if it is the same session, we have just incremented and
  364. * then decremented the reference count :-) */
  365. if (s->session != NULL)
  366. SSL_SESSION_free(s->session);
  367. s->session=ret;
  368. s->verify_result = s->session->verify_result;
  369. return(1);
  370. err:
  371. if (ret != NULL)
  372. SSL_SESSION_free(ret);
  373. if (fatal)
  374. return -1;
  375. else
  376. return 0;
  377. }
  378. int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
  379. {
  380. int ret=0;
  381. SSL_SESSION *s;
  382. /* add just 1 reference count for the SSL_CTX's session cache
  383. * even though it has two ways of access: each session is in a
  384. * doubly linked list and an lhash */
  385. CRYPTO_add(&c->references,1,CRYPTO_LOCK_SSL_SESSION);
  386. /* if session c is in already in cache, we take back the increment later */
  387. CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  388. s=(SSL_SESSION *)lh_insert(ctx->sessions,c);
  389. /* s != NULL iff we already had a session with the given PID.
  390. * In this case, s == c should hold (then we did not really modify
  391. * ctx->sessions), or we're in trouble. */
  392. if (s != NULL && s != c)
  393. {
  394. /* We *are* in trouble ... */
  395. SSL_SESSION_list_remove(ctx,s);
  396. SSL_SESSION_free(s);
  397. /* ... so pretend the other session did not exist in cache
  398. * (we cannot handle two SSL_SESSION structures with identical
  399. * session ID in the same cache, which could happen e.g. when
  400. * two threads concurrently obtain the same session from an external
  401. * cache) */
  402. s = NULL;
  403. }
  404. /* Put at the head of the queue unless it is already in the cache */
  405. if (s == NULL)
  406. SSL_SESSION_list_add(ctx,c);
  407. if (s != NULL)
  408. {
  409. /* existing cache entry -- decrement previously incremented reference
  410. * count because it already takes into account the cache */
  411. SSL_SESSION_free(s); /* s == c */
  412. ret=0;
  413. }
  414. else
  415. {
  416. /* new cache entry -- remove old ones if cache has become too large */
  417. ret=1;
  418. if (SSL_CTX_sess_get_cache_size(ctx) > 0)
  419. {
  420. while (SSL_CTX_sess_number(ctx) >
  421. SSL_CTX_sess_get_cache_size(ctx))
  422. {
  423. if (!remove_session_lock(ctx,
  424. ctx->session_cache_tail, 0))
  425. break;
  426. else
  427. ctx->stats.sess_cache_full++;
  428. }
  429. }
  430. }
  431. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  432. return(ret);
  433. }
  434. int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
  435. {
  436. return remove_session_lock(ctx, c, 1);
  437. }
  438. static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
  439. {
  440. SSL_SESSION *r;
  441. int ret=0;
  442. if ((c != NULL) && (c->session_id_length != 0))
  443. {
  444. if(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  445. if ((r = (SSL_SESSION *)lh_retrieve(ctx->sessions,c)) == c)
  446. {
  447. ret=1;
  448. r=(SSL_SESSION *)lh_delete(ctx->sessions,c);
  449. SSL_SESSION_list_remove(ctx,c);
  450. }
  451. if(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  452. if (ret)
  453. {
  454. r->not_resumable=1;
  455. if (ctx->remove_session_cb != NULL)
  456. ctx->remove_session_cb(ctx,r);
  457. SSL_SESSION_free(r);
  458. }
  459. }
  460. else
  461. ret=0;
  462. return(ret);
  463. }
  464. void SSL_SESSION_free(SSL_SESSION *ss)
  465. {
  466. int i;
  467. if(ss == NULL)
  468. return;
  469. i=CRYPTO_add(&ss->references,-1,CRYPTO_LOCK_SSL_SESSION);
  470. #ifdef REF_PRINT
  471. REF_PRINT("SSL_SESSION",ss);
  472. #endif
  473. if (i > 0) return;
  474. #ifdef REF_CHECK
  475. if (i < 0)
  476. {
  477. fprintf(stderr,"SSL_SESSION_free, bad reference count\n");
  478. abort(); /* ok */
  479. }
  480. #endif
  481. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
  482. OPENSSL_cleanse(ss->key_arg,sizeof ss->key_arg);
  483. OPENSSL_cleanse(ss->master_key,sizeof ss->master_key);
  484. OPENSSL_cleanse(ss->session_id,sizeof ss->session_id);
  485. if (ss->sess_cert != NULL) ssl_sess_cert_free(ss->sess_cert);
  486. if (ss->peer != NULL) X509_free(ss->peer);
  487. if (ss->ciphers != NULL) sk_SSL_CIPHER_free(ss->ciphers);
  488. OPENSSL_cleanse(ss,sizeof(*ss));
  489. OPENSSL_free(ss);
  490. }
  491. int SSL_set_session(SSL *s, SSL_SESSION *session)
  492. {
  493. int ret=0;
  494. SSL_METHOD *meth;
  495. if (session != NULL)
  496. {
  497. meth=s->ctx->method->get_ssl_method(session->ssl_version);
  498. if (meth == NULL)
  499. meth=s->method->get_ssl_method(session->ssl_version);
  500. if (meth == NULL)
  501. {
  502. SSLerr(SSL_F_SSL_SET_SESSION,SSL_R_UNABLE_TO_FIND_SSL_METHOD);
  503. return(0);
  504. }
  505. if (meth != s->method)
  506. {
  507. if (!SSL_set_ssl_method(s,meth))
  508. return(0);
  509. if (s->ctx->session_timeout == 0)
  510. session->timeout=SSL_get_default_timeout(s);
  511. else
  512. session->timeout=s->ctx->session_timeout;
  513. }
  514. #ifndef OPENSSL_NO_KRB5
  515. if (s->kssl_ctx && !s->kssl_ctx->client_princ &&
  516. session->krb5_client_princ_len > 0)
  517. {
  518. s->kssl_ctx->client_princ = (char *)OPENSSL_malloc(session->krb5_client_princ_len + 1);
  519. memcpy(s->kssl_ctx->client_princ,session->krb5_client_princ,
  520. session->krb5_client_princ_len);
  521. s->kssl_ctx->client_princ[session->krb5_client_princ_len] = '\0';
  522. }
  523. #endif /* OPENSSL_NO_KRB5 */
  524. /* CRYPTO_w_lock(CRYPTO_LOCK_SSL);*/
  525. CRYPTO_add(&session->references,1,CRYPTO_LOCK_SSL_SESSION);
  526. if (s->session != NULL)
  527. SSL_SESSION_free(s->session);
  528. s->session=session;
  529. s->verify_result = s->session->verify_result;
  530. /* CRYPTO_w_unlock(CRYPTO_LOCK_SSL);*/
  531. ret=1;
  532. }
  533. else
  534. {
  535. if (s->session != NULL)
  536. {
  537. SSL_SESSION_free(s->session);
  538. s->session=NULL;
  539. }
  540. meth=s->ctx->method;
  541. if (meth != s->method)
  542. {
  543. if (!SSL_set_ssl_method(s,meth))
  544. return(0);
  545. }
  546. ret=1;
  547. }
  548. return(ret);
  549. }
  550. long SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
  551. {
  552. if (s == NULL) return(0);
  553. s->timeout=t;
  554. return(1);
  555. }
  556. long SSL_SESSION_get_timeout(const SSL_SESSION *s)
  557. {
  558. if (s == NULL) return(0);
  559. return(s->timeout);
  560. }
  561. long SSL_SESSION_get_time(const SSL_SESSION *s)
  562. {
  563. if (s == NULL) return(0);
  564. return(s->time);
  565. }
  566. long SSL_SESSION_set_time(SSL_SESSION *s, long t)
  567. {
  568. if (s == NULL) return(0);
  569. s->time=t;
  570. return(t);
  571. }
  572. long SSL_CTX_set_timeout(SSL_CTX *s, long t)
  573. {
  574. long l;
  575. if (s == NULL) return(0);
  576. l=s->session_timeout;
  577. s->session_timeout=t;
  578. return(l);
  579. }
  580. long SSL_CTX_get_timeout(const SSL_CTX *s)
  581. {
  582. if (s == NULL) return(0);
  583. return(s->session_timeout);
  584. }
  585. typedef struct timeout_param_st
  586. {
  587. SSL_CTX *ctx;
  588. long time;
  589. LHASH *cache;
  590. } TIMEOUT_PARAM;
  591. static void timeout(SSL_SESSION *s, TIMEOUT_PARAM *p)
  592. {
  593. if ((p->time == 0) || (p->time > (s->time+s->timeout))) /* timeout */
  594. {
  595. /* The reason we don't call SSL_CTX_remove_session() is to
  596. * save on locking overhead */
  597. lh_delete(p->cache,s);
  598. SSL_SESSION_list_remove(p->ctx,s);
  599. s->not_resumable=1;
  600. if (p->ctx->remove_session_cb != NULL)
  601. p->ctx->remove_session_cb(p->ctx,s);
  602. SSL_SESSION_free(s);
  603. }
  604. }
  605. static IMPLEMENT_LHASH_DOALL_ARG_FN(timeout, SSL_SESSION *, TIMEOUT_PARAM *)
  606. void SSL_CTX_flush_sessions(SSL_CTX *s, long t)
  607. {
  608. unsigned long i;
  609. TIMEOUT_PARAM tp;
  610. tp.ctx=s;
  611. tp.cache=s->sessions;
  612. if (tp.cache == NULL) return;
  613. tp.time=t;
  614. CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  615. i=tp.cache->down_load;
  616. tp.cache->down_load=0;
  617. lh_doall_arg(tp.cache, LHASH_DOALL_ARG_FN(timeout), &tp);
  618. tp.cache->down_load=i;
  619. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  620. }
  621. int ssl_clear_bad_session(SSL *s)
  622. {
  623. if ( (s->session != NULL) &&
  624. !(s->shutdown & SSL_SENT_SHUTDOWN) &&
  625. !(SSL_in_init(s) || SSL_in_before(s)))
  626. {
  627. SSL_CTX_remove_session(s->ctx,s->session);
  628. return(1);
  629. }
  630. else
  631. return(0);
  632. }
  633. /* locked by SSL_CTX in the calling function */
  634. static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
  635. {
  636. if ((s->next == NULL) || (s->prev == NULL)) return;
  637. if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail))
  638. { /* last element in list */
  639. if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head))
  640. { /* only one element in list */
  641. ctx->session_cache_head=NULL;
  642. ctx->session_cache_tail=NULL;
  643. }
  644. else
  645. {
  646. ctx->session_cache_tail=s->prev;
  647. s->prev->next=(SSL_SESSION *)&(ctx->session_cache_tail);
  648. }
  649. }
  650. else
  651. {
  652. if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head))
  653. { /* first element in list */
  654. ctx->session_cache_head=s->next;
  655. s->next->prev=(SSL_SESSION *)&(ctx->session_cache_head);
  656. }
  657. else
  658. { /* middle of list */
  659. s->next->prev=s->prev;
  660. s->prev->next=s->next;
  661. }
  662. }
  663. s->prev=s->next=NULL;
  664. }
  665. static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
  666. {
  667. if ((s->next != NULL) && (s->prev != NULL))
  668. SSL_SESSION_list_remove(ctx,s);
  669. if (ctx->session_cache_head == NULL)
  670. {
  671. ctx->session_cache_head=s;
  672. ctx->session_cache_tail=s;
  673. s->prev=(SSL_SESSION *)&(ctx->session_cache_head);
  674. s->next=(SSL_SESSION *)&(ctx->session_cache_tail);
  675. }
  676. else
  677. {
  678. s->next=ctx->session_cache_head;
  679. s->next->prev=s;
  680. s->prev=(SSL_SESSION *)&(ctx->session_cache_head);
  681. ctx->session_cache_head=s;
  682. }
  683. }
  684. void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
  685. int (*cb)(struct ssl_st *ssl,SSL_SESSION *sess))
  686. {
  687. ctx->new_session_cb=cb;
  688. }
  689. int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *sess)
  690. {
  691. return ctx->new_session_cb;
  692. }
  693. void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
  694. void (*cb)(SSL_CTX *ctx,SSL_SESSION *sess))
  695. {
  696. ctx->remove_session_cb=cb;
  697. }
  698. void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX * ctx,SSL_SESSION *sess)
  699. {
  700. return ctx->remove_session_cb;
  701. }
  702. void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
  703. SSL_SESSION *(*cb)(struct ssl_st *ssl,
  704. unsigned char *data,int len,int *copy))
  705. {
  706. ctx->get_session_cb=cb;
  707. }
  708. SSL_SESSION * (*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl,
  709. unsigned char *data,int len,int *copy)
  710. {
  711. return ctx->get_session_cb;
  712. }
  713. void SSL_CTX_set_info_callback(SSL_CTX *ctx,
  714. void (*cb)(const SSL *ssl,int type,int val))
  715. {
  716. ctx->info_callback=cb;
  717. }
  718. void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl,int type,int val)
  719. {
  720. return ctx->info_callback;
  721. }
  722. void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
  723. int (*cb)(SSL *ssl, X509 **x509, EVP_PKEY **pkey))
  724. {
  725. ctx->client_cert_cb=cb;
  726. }
  727. int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL * ssl, X509 ** x509 , EVP_PKEY **pkey)
  728. {
  729. return ctx->client_cert_cb;
  730. }
  731. void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
  732. int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int *cookie_len))
  733. {
  734. ctx->app_gen_cookie_cb=cb;
  735. }
  736. void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
  737. int (*cb)(SSL *ssl, unsigned char *cookie, unsigned int cookie_len))
  738. {
  739. ctx->app_verify_cookie_cb=cb;
  740. }