ssl_sess.c 42 KB

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