ssl_sess.c 43 KB

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