ssl_sess.c 30 KB

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