You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ssl_sess.c 28 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com).
  108. *
  109. */
  110. /* ====================================================================
  111. * Copyright 2005 Nokia. All rights reserved.
  112. *
  113. * The portions of the attached software ("Contribution") is developed by
  114. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  115. * license.
  116. *
  117. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  118. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  119. * support (see RFC 4279) to OpenSSL.
  120. *
  121. * No patent licenses or other rights except those expressly stated in
  122. * the OpenSSL open source license shall be deemed granted or received
  123. * expressly, by implication, estoppel, or otherwise.
  124. *
  125. * No assurances are provided by Nokia that the Contribution does not
  126. * infringe the patent or other intellectual property rights of any third
  127. * party or that the license provides you with all the necessary rights
  128. * to make use of the Contribution.
  129. *
  130. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  131. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  132. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  133. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  134. * OTHERWISE. */
  135. #include <stdio.h>
  136. #include <string.h>
  137. #include <openssl/err.h>
  138. #include <openssl/lhash.h>
  139. #include <openssl/mem.h>
  140. #include <openssl/rand.h>
  141. #include "internal.h"
  142. #include "../crypto/internal.h"
  143. /* The address of this is a magic value, a pointer to which is returned by
  144. * SSL_magic_pending_session_ptr(). It allows a session callback to indicate
  145. * that it needs to asynchronously fetch session information. */
  146. static const char g_pending_session_magic = 0;
  147. static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT;
  148. static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s);
  149. static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s);
  150. static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck);
  151. SSL_SESSION *SSL_magic_pending_session_ptr(void) {
  152. return (SSL_SESSION *)&g_pending_session_magic;
  153. }
  154. SSL_SESSION *SSL_get_session(const SSL *ssl)
  155. {
  156. /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */
  157. return ssl->session;
  158. }
  159. SSL_SESSION *SSL_get1_session(SSL *ssl) {
  160. /* variant of SSL_get_session: caller really gets something */
  161. return SSL_SESSION_up_ref(ssl->session);
  162. }
  163. int SSL_SESSION_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
  164. CRYPTO_EX_dup *dup_func,
  165. CRYPTO_EX_free *free_func) {
  166. int index;
  167. if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, new_func,
  168. dup_func, free_func)) {
  169. return -1;
  170. }
  171. return index;
  172. }
  173. int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg) {
  174. return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
  175. }
  176. void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx) {
  177. return CRYPTO_get_ex_data(&s->ex_data, idx);
  178. }
  179. SSL_SESSION *SSL_SESSION_new(void) {
  180. SSL_SESSION *ss;
  181. ss = (SSL_SESSION *)OPENSSL_malloc(sizeof(SSL_SESSION));
  182. if (ss == NULL) {
  183. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_new, ERR_R_MALLOC_FAILURE);
  184. return 0;
  185. }
  186. memset(ss, 0, sizeof(SSL_SESSION));
  187. ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */
  188. ss->references = 1;
  189. ss->timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  190. ss->time = (unsigned long)time(NULL);
  191. CRYPTO_new_ex_data(&g_ex_data_class, ss, &ss->ex_data);
  192. return ss;
  193. }
  194. const uint8_t *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len) {
  195. if (len) {
  196. *len = s->session_id_length;
  197. }
  198. return s->session_id;
  199. }
  200. /* Even with SSLv2, we have 16 bytes (128 bits) of session ID space.
  201. * SSLv3/TLSv1 has 32 bytes (256 bits). As such, filling the ID with random
  202. * gunk repeatedly until we have no conflict is going to complete in one
  203. * iteration pretty much "most" of the time (btw: understatement). So, if it
  204. * takes us 10 iterations and we still can't avoid a conflict - well that's a
  205. * reasonable point to call it quits. Either the RAND code is broken or someone
  206. * is trying to open roughly very close to 2^128 (or 2^256) SSL sessions to our
  207. * server. How you might store that many sessions is perhaps a more interesting
  208. * question ... */
  209. static int def_generate_session_id(const SSL *ssl, uint8_t *id,
  210. unsigned int *id_len) {
  211. static const unsigned kMaxAttempts = 10;
  212. unsigned int retry = 0;
  213. do {
  214. if (!RAND_bytes(id, *id_len)) {
  215. return 0;
  216. }
  217. } while (SSL_has_matching_session_id(ssl, id, *id_len) &&
  218. (++retry < kMaxAttempts));
  219. if (retry < kMaxAttempts) {
  220. return 1;
  221. }
  222. /* else - woops a session_id match */
  223. /* XXX We should also check the external cache -- but the probability of a
  224. * collision is negligible, and we could not prevent the concurrent creation
  225. * of sessions with identical IDs since we currently don't have means to
  226. * atomically check whether a session ID already exists and make a
  227. * reservation for it if it does not (this problem applies to the internal
  228. * cache as well). */
  229. return 0;
  230. }
  231. int ssl_get_new_session(SSL *s, int session) {
  232. /* This gets used by clients and servers. */
  233. unsigned int tmp;
  234. SSL_SESSION *ss = NULL;
  235. GEN_SESSION_CB cb = def_generate_session_id;
  236. if (s->mode & SSL_MODE_NO_SESSION_CREATION) {
  237. OPENSSL_PUT_ERROR(SSL, ssl_get_new_session,
  238. SSL_R_SESSION_MAY_NOT_BE_CREATED);
  239. return 0;
  240. }
  241. ss = SSL_SESSION_new();
  242. if (ss == NULL) {
  243. return 0;
  244. }
  245. /* If the context has a default timeout, use it over the default. */
  246. if (s->initial_ctx->session_timeout != 0) {
  247. ss->timeout = s->initial_ctx->session_timeout;
  248. }
  249. SSL_SESSION_free(s->session);
  250. s->session = NULL;
  251. if (session) {
  252. if (s->version == SSL3_VERSION || s->version == TLS1_VERSION ||
  253. s->version == TLS1_1_VERSION || s->version == TLS1_2_VERSION ||
  254. s->version == DTLS1_VERSION || s->version == DTLS1_2_VERSION) {
  255. ss->ssl_version = s->version;
  256. ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
  257. } else {
  258. OPENSSL_PUT_ERROR(SSL, ssl_get_new_session,
  259. SSL_R_UNSUPPORTED_SSL_VERSION);
  260. SSL_SESSION_free(ss);
  261. return 0;
  262. }
  263. /* If RFC4507 ticket use empty session ID */
  264. if (s->tlsext_ticket_expected) {
  265. ss->session_id_length = 0;
  266. goto sess_id_done;
  267. }
  268. /* Choose which callback will set the session ID */
  269. if (s->generate_session_id) {
  270. cb = s->generate_session_id;
  271. } else if (s->initial_ctx->generate_session_id) {
  272. cb = s->initial_ctx->generate_session_id;
  273. }
  274. /* Choose a session ID */
  275. tmp = ss->session_id_length;
  276. if (!cb(s, ss->session_id, &tmp)) {
  277. /* The callback failed */
  278. OPENSSL_PUT_ERROR(SSL, ssl_get_new_session,
  279. SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
  280. SSL_SESSION_free(ss);
  281. return 0;
  282. }
  283. /* Don't allow the callback to set the session length to zero. nor set it
  284. * higher than it was. */
  285. if (!tmp || tmp > ss->session_id_length) {
  286. /* The callback set an illegal length */
  287. OPENSSL_PUT_ERROR(SSL, ssl_get_new_session,
  288. SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
  289. SSL_SESSION_free(ss);
  290. return 0;
  291. }
  292. ss->session_id_length = tmp;
  293. /* Finally, check for a conflict */
  294. if (SSL_has_matching_session_id(s, ss->session_id, ss->session_id_length)) {
  295. OPENSSL_PUT_ERROR(SSL, ssl_get_new_session,
  296. SSL_R_SSL_SESSION_ID_CONFLICT);
  297. SSL_SESSION_free(ss);
  298. return 0;
  299. }
  300. sess_id_done:
  301. if (s->tlsext_hostname) {
  302. ss->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
  303. if (ss->tlsext_hostname == NULL) {
  304. OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, ERR_R_INTERNAL_ERROR);
  305. SSL_SESSION_free(ss);
  306. return 0;
  307. }
  308. }
  309. } else {
  310. ss->session_id_length = 0;
  311. }
  312. if (s->sid_ctx_length > sizeof(ss->sid_ctx)) {
  313. OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, ERR_R_INTERNAL_ERROR);
  314. SSL_SESSION_free(ss);
  315. return 0;
  316. }
  317. memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length);
  318. ss->sid_ctx_length = s->sid_ctx_length;
  319. s->session = ss;
  320. ss->ssl_version = s->version;
  321. ss->verify_result = X509_V_OK;
  322. return 1;
  323. }
  324. /* ssl_get_prev attempts to find an SSL_SESSION to be used to resume this
  325. * connection. It is only called by servers.
  326. *
  327. * ctx: contains the early callback context, which is the result of a
  328. * shallow parse of the ClientHello.
  329. *
  330. * Returns:
  331. * -1: error
  332. * 0: a session may have been found.
  333. *
  334. * Side effects:
  335. * - If a session is found then s->session is pointed at it (after freeing an
  336. * existing session if need be) and s->verify_result is set from the session.
  337. * - Both for new and resumed sessions, s->tlsext_ticket_expected is set to 1
  338. * if the server should issue a new session ticket (to 0 otherwise). */
  339. int ssl_get_prev_session(SSL *s, const struct ssl_early_callback_ctx *ctx) {
  340. /* This is used only by servers. */
  341. SSL_SESSION *ret = NULL;
  342. int fatal = 0;
  343. int try_session_cache = 1;
  344. int r;
  345. if (ctx->session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
  346. goto err;
  347. }
  348. if (ctx->session_id_len == 0) {
  349. try_session_cache = 0;
  350. }
  351. r = tls1_process_ticket(s, ctx, &ret); /* sets s->tlsext_ticket_expected */
  352. switch (r) {
  353. case -1: /* Error during processing */
  354. fatal = 1;
  355. goto err;
  356. case 0: /* No ticket found */
  357. case 1: /* Zero length ticket found */
  358. break; /* Ok to carry on processing session id. */
  359. case 2: /* Ticket found but not decrypted. */
  360. case 3: /* Ticket decrypted, *ret has been set. */
  361. try_session_cache = 0;
  362. break;
  363. default:
  364. abort();
  365. }
  366. if (try_session_cache && ret == NULL &&
  367. !(s->initial_ctx->session_cache_mode &
  368. SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) {
  369. SSL_SESSION data;
  370. data.ssl_version = s->version;
  371. data.session_id_length = ctx->session_id_len;
  372. if (ctx->session_id_len == 0) {
  373. return 0;
  374. }
  375. memcpy(data.session_id, ctx->session_id, ctx->session_id_len);
  376. CRYPTO_MUTEX_lock_read(&s->initial_ctx->lock);
  377. ret = lh_SSL_SESSION_retrieve(s->initial_ctx->sessions, &data);
  378. CRYPTO_MUTEX_unlock(&s->initial_ctx->lock);
  379. if (ret != NULL) {
  380. SSL_SESSION_up_ref(ret);
  381. }
  382. }
  383. if (try_session_cache && ret == NULL &&
  384. s->initial_ctx->get_session_cb != NULL) {
  385. int copy = 1;
  386. ret = s->initial_ctx->get_session_cb(s, (uint8_t *)ctx->session_id,
  387. ctx->session_id_len, &copy);
  388. if (ret != NULL) {
  389. if (ret == SSL_magic_pending_session_ptr()) {
  390. /* This is a magic value which indicates that the callback needs to
  391. * unwind the stack and figure out the session asynchronously. */
  392. return PENDING_SESSION;
  393. }
  394. /* Increment reference count now if the session callback asks us to do so
  395. * (note that if the session structures returned by the callback are
  396. * shared between threads, it must handle the reference count itself
  397. * [i.e. copy == 0], or things won't be thread-safe). */
  398. if (copy) {
  399. SSL_SESSION_up_ref(ret);
  400. }
  401. /* Add the externally cached session to the internal cache as well if and
  402. * only if we are supposed to. */
  403. if (!(s->initial_ctx->session_cache_mode &
  404. SSL_SESS_CACHE_NO_INTERNAL_STORE)) {
  405. /* The following should not return 1, otherwise, things are very
  406. * strange */
  407. SSL_CTX_add_session(s->initial_ctx, ret);
  408. }
  409. }
  410. }
  411. if (ret == NULL) {
  412. goto err;
  413. }
  414. /* Now ret is non-NULL and we own one of its reference counts. */
  415. if (ret->sid_ctx_length != s->sid_ctx_length ||
  416. memcmp(ret->sid_ctx, s->sid_ctx, ret->sid_ctx_length)) {
  417. /* We have the session requested by the client, but we don't want to use it
  418. * in this context. */
  419. goto err; /* treat like cache miss */
  420. }
  421. if ((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0) {
  422. /* We can't be sure if this session is being used out of context, which is
  423. * especially important for SSL_VERIFY_PEER. The application should have
  424. * used SSL[_CTX]_set_session_id_context.
  425. *
  426. * For this error case, we generate an error instead of treating the event
  427. * like a cache miss (otherwise it would be easy for applications to
  428. * effectively disable the session cache by accident without anyone
  429. * noticing). */
  430. OPENSSL_PUT_ERROR(SSL, ssl_get_prev_session,
  431. SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
  432. fatal = 1;
  433. goto err;
  434. }
  435. if (ret->timeout < (long)(time(NULL) - ret->time)) {
  436. /* timeout */
  437. if (try_session_cache) {
  438. /* session was from the cache, so remove it */
  439. SSL_CTX_remove_session(s->initial_ctx, ret);
  440. }
  441. goto err;
  442. }
  443. SSL_SESSION_free(s->session);
  444. s->session = ret;
  445. s->verify_result = s->session->verify_result;
  446. return 1;
  447. err:
  448. if (ret != NULL) {
  449. SSL_SESSION_free(ret);
  450. if (!try_session_cache) {
  451. /* The session was from a ticket, so we should
  452. * issue a ticket for the new session */
  453. s->tlsext_ticket_expected = 1;
  454. }
  455. }
  456. if (fatal) {
  457. return -1;
  458. }
  459. return 0;
  460. }
  461. int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c) {
  462. int ret = 0;
  463. SSL_SESSION *s;
  464. /* add just 1 reference count for the SSL_CTX's session cache even though it
  465. * has two ways of access: each session is in a doubly linked list and an
  466. * lhash */
  467. SSL_SESSION_up_ref(c);
  468. /* if session c is in already in cache, we take back the increment later */
  469. CRYPTO_MUTEX_lock_write(&ctx->lock);
  470. if (!lh_SSL_SESSION_insert(ctx->sessions, &s, c)) {
  471. CRYPTO_MUTEX_unlock(&ctx->lock);
  472. return 0;
  473. }
  474. /* s != NULL iff we already had a session with the given PID. In this case, s
  475. * == c should hold (then we did not really modify ctx->sessions), or we're
  476. * in trouble. */
  477. if (s != NULL && s != c) {
  478. /* We *are* in trouble ... */
  479. SSL_SESSION_list_remove(ctx, s);
  480. SSL_SESSION_free(s);
  481. /* ... so pretend the other session did not exist in cache (we cannot
  482. * handle two SSL_SESSION structures with identical session ID in the same
  483. * cache, which could happen e.g. when two threads concurrently obtain the
  484. * same session from an external cache) */
  485. s = NULL;
  486. }
  487. /* Put at the head of the queue unless it is already in the cache */
  488. if (s == NULL) {
  489. SSL_SESSION_list_add(ctx, c);
  490. }
  491. if (s != NULL) {
  492. /* existing cache entry -- decrement previously incremented reference count
  493. * because it already takes into account the cache */
  494. SSL_SESSION_free(s); /* s == c */
  495. ret = 0;
  496. } else {
  497. /* new cache entry -- remove old ones if cache has become too large */
  498. ret = 1;
  499. if (SSL_CTX_sess_get_cache_size(ctx) > 0) {
  500. while (SSL_CTX_sess_number(ctx) > SSL_CTX_sess_get_cache_size(ctx)) {
  501. if (!remove_session_lock(ctx, ctx->session_cache_tail, 0)) {
  502. break;
  503. }
  504. }
  505. }
  506. }
  507. CRYPTO_MUTEX_unlock(&ctx->lock);
  508. return ret;
  509. }
  510. int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c) {
  511. return remove_session_lock(ctx, c, 1);
  512. }
  513. static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lock) {
  514. SSL_SESSION *r;
  515. int ret = 0;
  516. if (c != NULL && c->session_id_length != 0) {
  517. if (lock) {
  518. CRYPTO_MUTEX_lock_write(&ctx->lock);
  519. }
  520. r = lh_SSL_SESSION_retrieve(ctx->sessions, c);
  521. if (r == c) {
  522. ret = 1;
  523. r = lh_SSL_SESSION_delete(ctx->sessions, c);
  524. SSL_SESSION_list_remove(ctx, c);
  525. }
  526. if (lock) {
  527. CRYPTO_MUTEX_unlock(&ctx->lock);
  528. }
  529. if (ret) {
  530. r->not_resumable = 1;
  531. if (ctx->remove_session_cb != NULL) {
  532. ctx->remove_session_cb(ctx, r);
  533. }
  534. SSL_SESSION_free(r);
  535. }
  536. }
  537. return ret;
  538. }
  539. SSL_SESSION *SSL_SESSION_up_ref(SSL_SESSION *session) {
  540. if (session) {
  541. CRYPTO_refcount_inc(&session->references);
  542. }
  543. return session;
  544. }
  545. void SSL_SESSION_free(SSL_SESSION *session) {
  546. if (session == NULL ||
  547. !CRYPTO_refcount_dec_and_test_zero(&session->references)) {
  548. return;
  549. }
  550. CRYPTO_free_ex_data(&g_ex_data_class, session, &session->ex_data);
  551. OPENSSL_cleanse(session->master_key, sizeof(session->master_key));
  552. OPENSSL_cleanse(session->session_id, sizeof(session->session_id));
  553. ssl_sess_cert_free(session->sess_cert);
  554. X509_free(session->peer);
  555. OPENSSL_free(session->tlsext_hostname);
  556. OPENSSL_free(session->tlsext_tick);
  557. OPENSSL_free(session->tlsext_signed_cert_timestamp_list);
  558. OPENSSL_free(session->ocsp_response);
  559. OPENSSL_free(session->psk_identity);
  560. OPENSSL_cleanse(session, sizeof(*session));
  561. OPENSSL_free(session);
  562. }
  563. int SSL_set_session(SSL *s, SSL_SESSION *session) {
  564. if (s->session == session) {
  565. return 1;
  566. }
  567. SSL_SESSION_free(s->session);
  568. s->session = session;
  569. if (session != NULL) {
  570. SSL_SESSION_up_ref(session);
  571. s->verify_result = session->verify_result;
  572. }
  573. return 1;
  574. }
  575. long SSL_SESSION_set_timeout(SSL_SESSION *s, long t) {
  576. if (s == NULL) {
  577. return 0;
  578. }
  579. s->timeout = t;
  580. return 1;
  581. }
  582. long SSL_SESSION_get_timeout(const SSL_SESSION *s) {
  583. if (s == NULL) {
  584. return 0;
  585. }
  586. return s->timeout;
  587. }
  588. long SSL_SESSION_get_time(const SSL_SESSION *s) {
  589. if (s == NULL) {
  590. return 0;
  591. }
  592. return s->time;
  593. }
  594. long SSL_SESSION_set_time(SSL_SESSION *s, long t) {
  595. if (s == NULL) {
  596. return 0;
  597. }
  598. s->time = t;
  599. return t;
  600. }
  601. X509 *SSL_SESSION_get0_peer(SSL_SESSION *s) { return s->peer; }
  602. int SSL_SESSION_set1_id_context(SSL_SESSION *s, const uint8_t *sid_ctx,
  603. unsigned int sid_ctx_len) {
  604. if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
  605. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_set1_id_context,
  606. SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  607. return 0;
  608. }
  609. s->sid_ctx_length = sid_ctx_len;
  610. memcpy(s->sid_ctx, sid_ctx, sid_ctx_len);
  611. return 1;
  612. }
  613. long SSL_CTX_set_timeout(SSL_CTX *s, long t) {
  614. long l;
  615. if (s == NULL) {
  616. return 0;
  617. }
  618. l = s->session_timeout;
  619. s->session_timeout = t;
  620. return l;
  621. }
  622. long SSL_CTX_get_timeout(const SSL_CTX *s) {
  623. if (s == NULL) {
  624. return 0;
  625. }
  626. return s->session_timeout;
  627. }
  628. typedef struct timeout_param_st {
  629. SSL_CTX *ctx;
  630. long time;
  631. LHASH_OF(SSL_SESSION) *cache;
  632. } TIMEOUT_PARAM;
  633. static void timeout_doall_arg(SSL_SESSION *sess, void *void_param) {
  634. TIMEOUT_PARAM *param = void_param;
  635. if (param->time == 0 ||
  636. param->time > (sess->time + sess->timeout)) {
  637. /* timeout */
  638. /* The reason we don't call SSL_CTX_remove_session() is to
  639. * save on locking overhead */
  640. (void) lh_SSL_SESSION_delete(param->cache, sess);
  641. SSL_SESSION_list_remove(param->ctx, sess);
  642. sess->not_resumable = 1;
  643. if (param->ctx->remove_session_cb != NULL) {
  644. param->ctx->remove_session_cb(param->ctx, sess);
  645. }
  646. SSL_SESSION_free(sess);
  647. }
  648. }
  649. void SSL_CTX_flush_sessions(SSL_CTX *ctx, long t) {
  650. TIMEOUT_PARAM tp;
  651. tp.ctx = ctx;
  652. tp.cache = ctx->sessions;
  653. if (tp.cache == NULL) {
  654. return;
  655. }
  656. tp.time = t;
  657. CRYPTO_MUTEX_lock_write(&ctx->lock);
  658. lh_SSL_SESSION_doall_arg(tp.cache, timeout_doall_arg, &tp);
  659. CRYPTO_MUTEX_unlock(&ctx->lock);
  660. }
  661. int ssl_clear_bad_session(SSL *s) {
  662. if (s->session != NULL && !(s->shutdown & SSL_SENT_SHUTDOWN) &&
  663. !SSL_in_init(s)) {
  664. SSL_CTX_remove_session(s->ctx, s->session);
  665. return 1;
  666. }
  667. return 0;
  668. }
  669. /* locked by SSL_CTX in the calling function */
  670. static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s) {
  671. if (s->next == NULL || s->prev == NULL) {
  672. return;
  673. }
  674. if (s->next == (SSL_SESSION *)&ctx->session_cache_tail) {
  675. /* last element in list */
  676. if (s->prev == (SSL_SESSION *)&ctx->session_cache_head) {
  677. /* only one element in list */
  678. ctx->session_cache_head = NULL;
  679. ctx->session_cache_tail = NULL;
  680. } else {
  681. ctx->session_cache_tail = s->prev;
  682. s->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail);
  683. }
  684. } else {
  685. if (s->prev == (SSL_SESSION *)&ctx->session_cache_head) {
  686. /* first element in list */
  687. ctx->session_cache_head = s->next;
  688. s->next->prev = (SSL_SESSION *)&(ctx->session_cache_head);
  689. } else { /* middle of list */
  690. s->next->prev = s->prev;
  691. s->prev->next = s->next;
  692. }
  693. }
  694. s->prev = s->next = NULL;
  695. }
  696. static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s) {
  697. if (s->next != NULL && s->prev != NULL) {
  698. SSL_SESSION_list_remove(ctx, s);
  699. }
  700. if (ctx->session_cache_head == NULL) {
  701. ctx->session_cache_head = s;
  702. ctx->session_cache_tail = s;
  703. s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
  704. s->next = (SSL_SESSION *)&(ctx->session_cache_tail);
  705. } else {
  706. s->next = ctx->session_cache_head;
  707. s->next->prev = s;
  708. s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
  709. ctx->session_cache_head = s;
  710. }
  711. }
  712. void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
  713. int (*cb)(struct ssl_st *ssl, SSL_SESSION *sess)) {
  714. ctx->new_session_cb = cb;
  715. }
  716. int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *sess) {
  717. return ctx->new_session_cb;
  718. }
  719. void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
  720. void (*cb)(SSL_CTX *ctx, SSL_SESSION *sess)) {
  721. ctx->remove_session_cb = cb;
  722. }
  723. void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX *ctx,
  724. SSL_SESSION *sess) {
  725. return ctx->remove_session_cb;
  726. }
  727. void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
  728. SSL_SESSION *(*cb)(struct ssl_st *ssl,
  729. uint8_t *data, int len,
  730. int *copy)) {
  731. ctx->get_session_cb = cb;
  732. }
  733. SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl, uint8_t *data,
  734. int len, int *copy) {
  735. return ctx->get_session_cb;
  736. }
  737. void SSL_CTX_set_info_callback(SSL_CTX *ctx,
  738. void (*cb)(const SSL *ssl, int type, int val)) {
  739. ctx->info_callback = cb;
  740. }
  741. void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl, int type,
  742. int val) {
  743. return ctx->info_callback;
  744. }
  745. void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, X509 **x509,
  746. EVP_PKEY **pkey)) {
  747. ctx->client_cert_cb = cb;
  748. }
  749. int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL *ssl, X509 **x509,
  750. EVP_PKEY **pkey) {
  751. return ctx->client_cert_cb;
  752. }
  753. void SSL_CTX_set_channel_id_cb(SSL_CTX *ctx,
  754. void (*cb)(SSL *ssl, EVP_PKEY **pkey)) {
  755. ctx->channel_id_cb = cb;
  756. }
  757. void (*SSL_CTX_get_channel_id_cb(SSL_CTX *ctx))(SSL *ssl, EVP_PKEY **pkey) {
  758. return ctx->channel_id_cb;
  759. }
  760. IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)