ssl_session.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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 <openssl/ssl.h>
  136. #include <assert.h>
  137. #include <stdio.h>
  138. #include <string.h>
  139. #include <openssl/err.h>
  140. #include <openssl/lhash.h>
  141. #include <openssl/mem.h>
  142. #include <openssl/rand.h>
  143. #include "internal.h"
  144. #include "../crypto/internal.h"
  145. /* The address of this is a magic value, a pointer to which is returned by
  146. * SSL_magic_pending_session_ptr(). It allows a session callback to indicate
  147. * that it needs to asynchronously fetch session information. */
  148. static const char g_pending_session_magic = 0;
  149. static CRYPTO_EX_DATA_CLASS g_ex_data_class =
  150. CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
  151. static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *session);
  152. static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *session);
  153. static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *session, int lock);
  154. SSL_SESSION *SSL_SESSION_new(void) {
  155. SSL_SESSION *session = OPENSSL_malloc(sizeof(SSL_SESSION));
  156. if (session == NULL) {
  157. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  158. return 0;
  159. }
  160. memset(session, 0, sizeof(SSL_SESSION));
  161. session->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */
  162. session->references = 1;
  163. session->timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  164. session->time = (unsigned long)time(NULL);
  165. CRYPTO_new_ex_data(&session->ex_data);
  166. return session;
  167. }
  168. SSL_SESSION *SSL_SESSION_up_ref(SSL_SESSION *session) {
  169. if (session != NULL) {
  170. CRYPTO_refcount_inc(&session->references);
  171. }
  172. return session;
  173. }
  174. void SSL_SESSION_free(SSL_SESSION *session) {
  175. if (session == NULL ||
  176. !CRYPTO_refcount_dec_and_test_zero(&session->references)) {
  177. return;
  178. }
  179. CRYPTO_free_ex_data(&g_ex_data_class, session, &session->ex_data);
  180. OPENSSL_cleanse(session->master_key, sizeof(session->master_key));
  181. OPENSSL_cleanse(session->session_id, sizeof(session->session_id));
  182. X509_free(session->peer);
  183. sk_X509_pop_free(session->cert_chain, X509_free);
  184. OPENSSL_free(session->tlsext_hostname);
  185. OPENSSL_free(session->tlsext_tick);
  186. OPENSSL_free(session->tlsext_signed_cert_timestamp_list);
  187. OPENSSL_free(session->ocsp_response);
  188. OPENSSL_free(session->psk_identity);
  189. OPENSSL_cleanse(session, sizeof(*session));
  190. OPENSSL_free(session);
  191. }
  192. const uint8_t *SSL_SESSION_get_id(const SSL_SESSION *session,
  193. unsigned *out_len) {
  194. if (out_len != NULL) {
  195. *out_len = session->session_id_length;
  196. }
  197. return session->session_id;
  198. }
  199. long SSL_SESSION_get_timeout(const SSL_SESSION *session) {
  200. return session->timeout;
  201. }
  202. long SSL_SESSION_get_time(const SSL_SESSION *session) {
  203. if (session == NULL) {
  204. /* NULL should crash, but silently accept it here for compatibility. */
  205. return 0;
  206. }
  207. return session->time;
  208. }
  209. uint32_t SSL_SESSION_get_key_exchange_info(const SSL_SESSION *session) {
  210. return session->key_exchange_info;
  211. }
  212. X509 *SSL_SESSION_get0_peer(const SSL_SESSION *session) {
  213. return session->peer;
  214. }
  215. long SSL_SESSION_set_time(SSL_SESSION *session, long time) {
  216. if (session == NULL) {
  217. return 0;
  218. }
  219. session->time = time;
  220. return time;
  221. }
  222. long SSL_SESSION_set_timeout(SSL_SESSION *session, long timeout) {
  223. if (session == NULL) {
  224. return 0;
  225. }
  226. session->timeout = timeout;
  227. return 1;
  228. }
  229. int SSL_SESSION_set1_id_context(SSL_SESSION *session, const uint8_t *sid_ctx,
  230. unsigned sid_ctx_len) {
  231. if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
  232. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  233. return 0;
  234. }
  235. session->sid_ctx_length = sid_ctx_len;
  236. memcpy(session->sid_ctx, sid_ctx, sid_ctx_len);
  237. return 1;
  238. }
  239. SSL_SESSION *SSL_magic_pending_session_ptr(void) {
  240. return (SSL_SESSION *)&g_pending_session_magic;
  241. }
  242. SSL_SESSION *SSL_get_session(const SSL *ssl)
  243. {
  244. /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */
  245. return ssl->session;
  246. }
  247. SSL_SESSION *SSL_get1_session(SSL *ssl) {
  248. /* variant of SSL_get_session: caller really gets something */
  249. return SSL_SESSION_up_ref(ssl->session);
  250. }
  251. int SSL_SESSION_get_ex_new_index(long argl, void *argp,
  252. CRYPTO_EX_unused *unused,
  253. CRYPTO_EX_dup *dup_func,
  254. CRYPTO_EX_free *free_func) {
  255. int index;
  256. if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, dup_func,
  257. free_func)) {
  258. return -1;
  259. }
  260. return index;
  261. }
  262. int SSL_SESSION_set_ex_data(SSL_SESSION *session, int idx, void *arg) {
  263. return CRYPTO_set_ex_data(&session->ex_data, idx, arg);
  264. }
  265. void *SSL_SESSION_get_ex_data(const SSL_SESSION *session, int idx) {
  266. return CRYPTO_get_ex_data(&session->ex_data, idx);
  267. }
  268. int ssl_get_new_session(SSL *ssl, int is_server) {
  269. if (ssl->mode & SSL_MODE_NO_SESSION_CREATION) {
  270. OPENSSL_PUT_ERROR(SSL, SSL_R_SESSION_MAY_NOT_BE_CREATED);
  271. return 0;
  272. }
  273. SSL_SESSION *session = SSL_SESSION_new();
  274. if (session == NULL) {
  275. return 0;
  276. }
  277. /* If the context has a default timeout, use it over the default. */
  278. if (ssl->initial_ctx->session_timeout != 0) {
  279. session->timeout = ssl->initial_ctx->session_timeout;
  280. }
  281. session->ssl_version = ssl->version;
  282. if (is_server) {
  283. if (ssl->tlsext_ticket_expected) {
  284. /* Don't set session IDs for sessions resumed with tickets. This will keep
  285. * them out of the session cache. */
  286. session->session_id_length = 0;
  287. } else {
  288. session->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
  289. if (!RAND_bytes(session->session_id, session->session_id_length)) {
  290. goto err;
  291. }
  292. }
  293. if (ssl->tlsext_hostname != NULL) {
  294. session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
  295. if (session->tlsext_hostname == NULL) {
  296. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  297. goto err;
  298. }
  299. }
  300. } else {
  301. session->session_id_length = 0;
  302. }
  303. if (ssl->sid_ctx_length > sizeof(session->sid_ctx)) {
  304. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  305. goto err;
  306. }
  307. memcpy(session->sid_ctx, ssl->sid_ctx, ssl->sid_ctx_length);
  308. session->sid_ctx_length = ssl->sid_ctx_length;
  309. session->verify_result = X509_V_OK;
  310. SSL_SESSION_free(ssl->session);
  311. ssl->session = session;
  312. return 1;
  313. err:
  314. SSL_SESSION_free(session);
  315. return 0;
  316. }
  317. /* ssl_lookup_session looks up |session_id| in the session cache and sets
  318. * |*out_session| to an |SSL_SESSION| object if found. The caller takes
  319. * ownership of the result. */
  320. static enum ssl_session_result_t ssl_lookup_session(
  321. SSL *ssl, SSL_SESSION **out_session, const uint8_t *session_id,
  322. size_t session_id_len) {
  323. *out_session = NULL;
  324. if (session_id_len == 0 || session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
  325. return ssl_session_success;
  326. }
  327. SSL_SESSION *session;
  328. /* Try the internal cache, if it exists. */
  329. if (!(ssl->initial_ctx->session_cache_mode &
  330. SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) {
  331. SSL_SESSION data;
  332. data.ssl_version = ssl->version;
  333. data.session_id_length = session_id_len;
  334. memcpy(data.session_id, session_id, session_id_len);
  335. CRYPTO_MUTEX_lock_read(&ssl->initial_ctx->lock);
  336. session = lh_SSL_SESSION_retrieve(ssl->initial_ctx->sessions, &data);
  337. if (session != NULL) {
  338. SSL_SESSION_up_ref(session);
  339. }
  340. /* TODO(davidben): This should probably move it to the front of the list. */
  341. CRYPTO_MUTEX_unlock(&ssl->initial_ctx->lock);
  342. if (session != NULL) {
  343. *out_session = session;
  344. return ssl_session_success;
  345. }
  346. }
  347. /* Fall back to the external cache, if it exists. */
  348. if (ssl->initial_ctx->get_session_cb == NULL) {
  349. return ssl_session_success;
  350. }
  351. int copy = 1;
  352. session = ssl->initial_ctx->get_session_cb(ssl, (uint8_t *)session_id,
  353. session_id_len, &copy);
  354. if (session == NULL) {
  355. return ssl_session_success;
  356. }
  357. if (session == SSL_magic_pending_session_ptr()) {
  358. return ssl_session_retry;
  359. }
  360. /* Increment reference count now if the session callback asks us to do so
  361. * (note that if the session structures returned by the callback are shared
  362. * between threads, it must handle the reference count itself [i.e. copy ==
  363. * 0], or things won't be thread-safe). */
  364. if (copy) {
  365. SSL_SESSION_up_ref(session);
  366. }
  367. /* Add the externally cached session to the internal cache if necessary. */
  368. if (!(ssl->initial_ctx->session_cache_mode &
  369. SSL_SESS_CACHE_NO_INTERNAL_STORE)) {
  370. SSL_CTX_add_session(ssl->initial_ctx, session);
  371. }
  372. *out_session = session;
  373. return ssl_session_success;
  374. }
  375. enum ssl_session_result_t ssl_get_prev_session(
  376. SSL *ssl, SSL_SESSION **out_session, int *out_send_ticket,
  377. const struct ssl_early_callback_ctx *ctx) {
  378. /* This is used only by servers. */
  379. assert(ssl->server);
  380. SSL_SESSION *session = NULL;
  381. int renew_ticket = 0;
  382. /* If tickets are disabled, always behave as if no tickets are present. */
  383. const uint8_t *ticket = NULL;
  384. size_t ticket_len = 0;
  385. const int tickets_supported =
  386. !(SSL_get_options(ssl) & SSL_OP_NO_TICKET) &&
  387. ssl->version > SSL3_VERSION &&
  388. SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_session_ticket,
  389. &ticket, &ticket_len);
  390. int from_cache = 0;
  391. if (tickets_supported && ticket_len > 0) {
  392. if (!tls_process_ticket(ssl, &session, &renew_ticket, ticket, ticket_len,
  393. ctx->session_id, ctx->session_id_len)) {
  394. return ssl_session_error;
  395. }
  396. } else {
  397. /* The client didn't send a ticket, so the session ID is a real ID. */
  398. enum ssl_session_result_t lookup_ret = ssl_lookup_session(
  399. ssl, &session, ctx->session_id, ctx->session_id_len);
  400. if (lookup_ret != ssl_session_success) {
  401. return lookup_ret;
  402. }
  403. from_cache = 1;
  404. }
  405. if (session == NULL ||
  406. session->sid_ctx_length != ssl->sid_ctx_length ||
  407. memcmp(session->sid_ctx, ssl->sid_ctx, ssl->sid_ctx_length) != 0) {
  408. /* The client did not offer a suitable ticket or session ID. If supported,
  409. * the new session should use a ticket. */
  410. goto no_session;
  411. }
  412. if ((ssl->verify_mode & SSL_VERIFY_PEER) && ssl->sid_ctx_length == 0) {
  413. /* We can't be sure if this session is being used out of context, which is
  414. * especially important for SSL_VERIFY_PEER. The application should have
  415. * used SSL[_CTX]_set_session_id_context.
  416. *
  417. * For this error case, we generate an error instead of treating the event
  418. * like a cache miss (otherwise it would be easy for applications to
  419. * effectively disable the session cache by accident without anyone
  420. * noticing). */
  421. OPENSSL_PUT_ERROR(SSL, SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
  422. SSL_SESSION_free(session);
  423. return ssl_session_error;
  424. }
  425. if (session->timeout < (long)(time(NULL) - session->time)) {
  426. if (from_cache) {
  427. /* The session was from the cache, so remove it. */
  428. SSL_CTX_remove_session(ssl->initial_ctx, session);
  429. }
  430. goto no_session;
  431. }
  432. *out_session = session;
  433. *out_send_ticket = renew_ticket;
  434. return ssl_session_success;
  435. no_session:
  436. *out_session = NULL;
  437. *out_send_ticket = tickets_supported;
  438. SSL_SESSION_free(session);
  439. return ssl_session_success;
  440. }
  441. int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *session) {
  442. /* Although |session| is inserted into two structures (a doubly-linked list
  443. * and the hash table), |ctx| only takes one reference. */
  444. SSL_SESSION_up_ref(session);
  445. SSL_SESSION *old_session;
  446. CRYPTO_MUTEX_lock_write(&ctx->lock);
  447. if (!lh_SSL_SESSION_insert(ctx->sessions, &old_session, session)) {
  448. CRYPTO_MUTEX_unlock(&ctx->lock);
  449. SSL_SESSION_free(session);
  450. return 0;
  451. }
  452. if (old_session != NULL) {
  453. if (old_session == session) {
  454. /* |session| was already in the cache. */
  455. CRYPTO_MUTEX_unlock(&ctx->lock);
  456. SSL_SESSION_free(old_session);
  457. return 0;
  458. }
  459. /* There was a session ID collision. |old_session| must be removed from
  460. * the linked list and released. */
  461. SSL_SESSION_list_remove(ctx, old_session);
  462. SSL_SESSION_free(old_session);
  463. }
  464. SSL_SESSION_list_add(ctx, session);
  465. /* Enforce any cache size limits. */
  466. if (SSL_CTX_sess_get_cache_size(ctx) > 0) {
  467. while (SSL_CTX_sess_number(ctx) > SSL_CTX_sess_get_cache_size(ctx)) {
  468. if (!remove_session_lock(ctx, ctx->session_cache_tail, 0)) {
  469. break;
  470. }
  471. }
  472. }
  473. CRYPTO_MUTEX_unlock(&ctx->lock);
  474. return 1;
  475. }
  476. int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *session) {
  477. return remove_session_lock(ctx, session, 1);
  478. }
  479. static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *session, int lock) {
  480. int ret = 0;
  481. if (session != NULL && session->session_id_length != 0) {
  482. if (lock) {
  483. CRYPTO_MUTEX_lock_write(&ctx->lock);
  484. }
  485. SSL_SESSION *found_session = lh_SSL_SESSION_retrieve(ctx->sessions,
  486. session);
  487. if (found_session == session) {
  488. ret = 1;
  489. found_session = lh_SSL_SESSION_delete(ctx->sessions, session);
  490. SSL_SESSION_list_remove(ctx, session);
  491. }
  492. if (lock) {
  493. CRYPTO_MUTEX_unlock(&ctx->lock);
  494. }
  495. if (ret) {
  496. found_session->not_resumable = 1;
  497. if (ctx->remove_session_cb != NULL) {
  498. ctx->remove_session_cb(ctx, found_session);
  499. }
  500. SSL_SESSION_free(found_session);
  501. }
  502. }
  503. return ret;
  504. }
  505. int SSL_set_session(SSL *ssl, SSL_SESSION *session) {
  506. if (ssl->session == session) {
  507. return 1;
  508. }
  509. SSL_SESSION_free(ssl->session);
  510. ssl->session = session;
  511. if (session != NULL) {
  512. SSL_SESSION_up_ref(session);
  513. ssl->verify_result = session->verify_result;
  514. }
  515. return 1;
  516. }
  517. long SSL_CTX_set_timeout(SSL_CTX *ctx, long timeout) {
  518. if (ctx == NULL) {
  519. return 0;
  520. }
  521. long old_timeout = ctx->session_timeout;
  522. ctx->session_timeout = timeout;
  523. return old_timeout;
  524. }
  525. long SSL_CTX_get_timeout(const SSL_CTX *ctx) {
  526. if (ctx == NULL) {
  527. return 0;
  528. }
  529. return ctx->session_timeout;
  530. }
  531. typedef struct timeout_param_st {
  532. SSL_CTX *ctx;
  533. long time;
  534. LHASH_OF(SSL_SESSION) *cache;
  535. } TIMEOUT_PARAM;
  536. static void timeout_doall_arg(SSL_SESSION *session, void *void_param) {
  537. TIMEOUT_PARAM *param = void_param;
  538. if (param->time == 0 ||
  539. param->time > (session->time + session->timeout)) {
  540. /* timeout */
  541. /* The reason we don't call SSL_CTX_remove_session() is to
  542. * save on locking overhead */
  543. (void) lh_SSL_SESSION_delete(param->cache, session);
  544. SSL_SESSION_list_remove(param->ctx, session);
  545. session->not_resumable = 1;
  546. if (param->ctx->remove_session_cb != NULL) {
  547. param->ctx->remove_session_cb(param->ctx, session);
  548. }
  549. SSL_SESSION_free(session);
  550. }
  551. }
  552. void SSL_CTX_flush_sessions(SSL_CTX *ctx, long time) {
  553. TIMEOUT_PARAM tp;
  554. tp.ctx = ctx;
  555. tp.cache = ctx->sessions;
  556. if (tp.cache == NULL) {
  557. return;
  558. }
  559. tp.time = time;
  560. CRYPTO_MUTEX_lock_write(&ctx->lock);
  561. lh_SSL_SESSION_doall_arg(tp.cache, timeout_doall_arg, &tp);
  562. CRYPTO_MUTEX_unlock(&ctx->lock);
  563. }
  564. int ssl_clear_bad_session(SSL *ssl) {
  565. if (ssl->session != NULL && !(ssl->shutdown & SSL_SENT_SHUTDOWN) &&
  566. !SSL_in_init(ssl)) {
  567. SSL_CTX_remove_session(ssl->ctx, ssl->session);
  568. return 1;
  569. }
  570. return 0;
  571. }
  572. /* locked by SSL_CTX in the calling function */
  573. static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *session) {
  574. if (session->next == NULL || session->prev == NULL) {
  575. return;
  576. }
  577. if (session->next == (SSL_SESSION *)&ctx->session_cache_tail) {
  578. /* last element in list */
  579. if (session->prev == (SSL_SESSION *)&ctx->session_cache_head) {
  580. /* only one element in list */
  581. ctx->session_cache_head = NULL;
  582. ctx->session_cache_tail = NULL;
  583. } else {
  584. ctx->session_cache_tail = session->prev;
  585. session->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail);
  586. }
  587. } else {
  588. if (session->prev == (SSL_SESSION *)&ctx->session_cache_head) {
  589. /* first element in list */
  590. ctx->session_cache_head = session->next;
  591. session->next->prev = (SSL_SESSION *)&(ctx->session_cache_head);
  592. } else { /* middle of list */
  593. session->next->prev = session->prev;
  594. session->prev->next = session->next;
  595. }
  596. }
  597. session->prev = session->next = NULL;
  598. }
  599. static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *session) {
  600. if (session->next != NULL && session->prev != NULL) {
  601. SSL_SESSION_list_remove(ctx, session);
  602. }
  603. if (ctx->session_cache_head == NULL) {
  604. ctx->session_cache_head = session;
  605. ctx->session_cache_tail = session;
  606. session->prev = (SSL_SESSION *)&(ctx->session_cache_head);
  607. session->next = (SSL_SESSION *)&(ctx->session_cache_tail);
  608. } else {
  609. session->next = ctx->session_cache_head;
  610. session->next->prev = session;
  611. session->prev = (SSL_SESSION *)&(ctx->session_cache_head);
  612. ctx->session_cache_head = session;
  613. }
  614. }
  615. void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
  616. int (*cb)(SSL *ssl, SSL_SESSION *session)) {
  617. ctx->new_session_cb = cb;
  618. }
  619. int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *session) {
  620. return ctx->new_session_cb;
  621. }
  622. void SSL_CTX_sess_set_remove_cb(
  623. SSL_CTX *ctx, void (*cb)(SSL_CTX *ctx, SSL_SESSION *session)) {
  624. ctx->remove_session_cb = cb;
  625. }
  626. void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX *ctx,
  627. SSL_SESSION *session) {
  628. return ctx->remove_session_cb;
  629. }
  630. void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
  631. SSL_SESSION *(*cb)(SSL *ssl,
  632. uint8_t *id, int id_len,
  633. int *out_copy)) {
  634. ctx->get_session_cb = cb;
  635. }
  636. SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(
  637. SSL *ssl, uint8_t *id, int id_len, int *out_copy) {
  638. return ctx->get_session_cb;
  639. }
  640. void SSL_CTX_set_info_callback(
  641. SSL_CTX *ctx, void (*cb)(const SSL *ssl, int type, int value)) {
  642. ctx->info_callback = cb;
  643. }
  644. void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl, int type,
  645. int value) {
  646. return ctx->info_callback;
  647. }
  648. void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl,
  649. X509 **out_x509,
  650. EVP_PKEY **out_pkey)) {
  651. ctx->client_cert_cb = cb;
  652. }
  653. int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL *ssl, X509 **out_x509,
  654. EVP_PKEY **out_pkey) {
  655. return ctx->client_cert_cb;
  656. }
  657. void SSL_CTX_set_channel_id_cb(SSL_CTX *ctx,
  658. void (*cb)(SSL *ssl, EVP_PKEY **pkey)) {
  659. ctx->channel_id_cb = cb;
  660. }
  661. void (*SSL_CTX_get_channel_id_cb(SSL_CTX *ctx))(SSL *ssl, EVP_PKEY **pkey) {
  662. return ctx->channel_id_cb;
  663. }