Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

857 righe
28 KiB

  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 <assert.h>
  136. #include <stdio.h>
  137. #include <string.h>
  138. #include <openssl/err.h>
  139. #include <openssl/lhash.h>
  140. #include <openssl/mem.h>
  141. #include <openssl/rand.h>
  142. #include "internal.h"
  143. #include "../crypto/internal.h"
  144. /* The address of this is a magic value, a pointer to which is returned by
  145. * SSL_magic_pending_session_ptr(). It allows a session callback to indicate
  146. * that it needs to asynchronously fetch session information. */
  147. static const char g_pending_session_magic = 0;
  148. static CRYPTO_EX_DATA_CLASS g_ex_data_class =
  149. CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
  150. static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s);
  151. static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s);
  152. static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck);
  153. SSL_SESSION *SSL_magic_pending_session_ptr(void) {
  154. return (SSL_SESSION *)&g_pending_session_magic;
  155. }
  156. SSL_SESSION *SSL_get_session(const SSL *ssl)
  157. {
  158. /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */
  159. return ssl->session;
  160. }
  161. SSL_SESSION *SSL_get1_session(SSL *ssl) {
  162. /* variant of SSL_get_session: caller really gets something */
  163. return SSL_SESSION_up_ref(ssl->session);
  164. }
  165. int SSL_SESSION_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
  166. CRYPTO_EX_dup *dup_func,
  167. CRYPTO_EX_free *free_func) {
  168. int index;
  169. if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, new_func,
  170. dup_func, free_func)) {
  171. return -1;
  172. }
  173. return index;
  174. }
  175. int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg) {
  176. return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
  177. }
  178. void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx) {
  179. return CRYPTO_get_ex_data(&s->ex_data, idx);
  180. }
  181. SSL_SESSION *SSL_SESSION_new(void) {
  182. SSL_SESSION *ss;
  183. ss = (SSL_SESSION *)OPENSSL_malloc(sizeof(SSL_SESSION));
  184. if (ss == NULL) {
  185. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  186. return 0;
  187. }
  188. memset(ss, 0, sizeof(SSL_SESSION));
  189. ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */
  190. ss->references = 1;
  191. ss->timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  192. ss->time = (unsigned long)time(NULL);
  193. CRYPTO_new_ex_data(&g_ex_data_class, ss, &ss->ex_data);
  194. return ss;
  195. }
  196. const uint8_t *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len) {
  197. if (len) {
  198. *len = s->session_id_length;
  199. }
  200. return s->session_id;
  201. }
  202. /* Even with SSLv2, we have 16 bytes (128 bits) of session ID space.
  203. * SSLv3/TLSv1 has 32 bytes (256 bits). As such, filling the ID with random
  204. * gunk repeatedly until we have no conflict is going to complete in one
  205. * iteration pretty much "most" of the time (btw: understatement). So, if it
  206. * takes us 10 iterations and we still can't avoid a conflict - well that's a
  207. * reasonable point to call it quits. Either the RAND code is broken or someone
  208. * is trying to open roughly very close to 2^128 (or 2^256) SSL sessions to our
  209. * server. How you might store that many sessions is perhaps a more interesting
  210. * question ... */
  211. static int def_generate_session_id(const SSL *ssl, uint8_t *id,
  212. unsigned int *id_len) {
  213. static const unsigned kMaxAttempts = 10;
  214. unsigned int retry = 0;
  215. do {
  216. if (!RAND_bytes(id, *id_len)) {
  217. return 0;
  218. }
  219. } while (SSL_has_matching_session_id(ssl, id, *id_len) &&
  220. (++retry < kMaxAttempts));
  221. if (retry < kMaxAttempts) {
  222. return 1;
  223. }
  224. /* else - woops a session_id match */
  225. /* XXX We should also check the external cache -- but the probability of a
  226. * collision is negligible, and we could not prevent the concurrent creation
  227. * of sessions with identical IDs since we currently don't have means to
  228. * atomically check whether a session ID already exists and make a
  229. * reservation for it if it does not (this problem applies to the internal
  230. * cache as well). */
  231. return 0;
  232. }
  233. int ssl_get_new_session(SSL *s, int session) {
  234. /* This gets used by clients and servers. */
  235. unsigned int tmp;
  236. SSL_SESSION *ss = NULL;
  237. GEN_SESSION_CB cb = def_generate_session_id;
  238. if (s->mode & SSL_MODE_NO_SESSION_CREATION) {
  239. OPENSSL_PUT_ERROR(SSL, SSL_R_SESSION_MAY_NOT_BE_CREATED);
  240. return 0;
  241. }
  242. ss = SSL_SESSION_new();
  243. if (ss == NULL) {
  244. return 0;
  245. }
  246. /* If the context has a default timeout, use it over the default. */
  247. if (s->initial_ctx->session_timeout != 0) {
  248. ss->timeout = s->initial_ctx->session_timeout;
  249. }
  250. SSL_SESSION_free(s->session);
  251. s->session = NULL;
  252. if (session) {
  253. if (s->version == SSL3_VERSION || s->version == TLS1_VERSION ||
  254. s->version == TLS1_1_VERSION || s->version == TLS1_2_VERSION ||
  255. s->version == DTLS1_VERSION || s->version == DTLS1_2_VERSION) {
  256. ss->ssl_version = s->version;
  257. ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
  258. } else {
  259. OPENSSL_PUT_ERROR(SSL, 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_R_SSL_SESSION_ID_CALLBACK_FAILED);
  279. SSL_SESSION_free(ss);
  280. return 0;
  281. }
  282. /* Don't allow the callback to set the session length to zero. nor set it
  283. * higher than it was. */
  284. if (!tmp || tmp > ss->session_id_length) {
  285. /* The callback set an illegal length */
  286. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
  287. SSL_SESSION_free(ss);
  288. return 0;
  289. }
  290. ss->session_id_length = tmp;
  291. /* Finally, check for a conflict */
  292. if (SSL_has_matching_session_id(s, ss->session_id, ss->session_id_length)) {
  293. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONFLICT);
  294. SSL_SESSION_free(ss);
  295. return 0;
  296. }
  297. sess_id_done:
  298. if (s->tlsext_hostname) {
  299. ss->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
  300. if (ss->tlsext_hostname == NULL) {
  301. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  302. SSL_SESSION_free(ss);
  303. return 0;
  304. }
  305. }
  306. } else {
  307. ss->session_id_length = 0;
  308. }
  309. if (s->sid_ctx_length > sizeof(ss->sid_ctx)) {
  310. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  311. SSL_SESSION_free(ss);
  312. return 0;
  313. }
  314. memcpy(ss->sid_ctx, s->sid_ctx, s->sid_ctx_length);
  315. ss->sid_ctx_length = s->sid_ctx_length;
  316. s->session = ss;
  317. ss->ssl_version = s->version;
  318. ss->verify_result = X509_V_OK;
  319. return 1;
  320. }
  321. /* ssl_lookup_session looks up |session_id| in the session cache and sets
  322. * |*out_session| to an |SSL_SESSION| object if found. The caller takes
  323. * ownership of the result. */
  324. static enum ssl_session_result_t ssl_lookup_session(
  325. SSL *ssl, SSL_SESSION **out_session, const uint8_t *session_id,
  326. size_t session_id_len) {
  327. *out_session = NULL;
  328. if (session_id_len == 0 || session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
  329. return ssl_session_success;
  330. }
  331. SSL_SESSION *session;
  332. /* Try the internal cache, if it exists. */
  333. if (!(ssl->initial_ctx->session_cache_mode &
  334. SSL_SESS_CACHE_NO_INTERNAL_LOOKUP)) {
  335. SSL_SESSION data;
  336. data.ssl_version = ssl->version;
  337. data.session_id_length = session_id_len;
  338. memcpy(data.session_id, session_id, session_id_len);
  339. CRYPTO_MUTEX_lock_read(&ssl->initial_ctx->lock);
  340. session = lh_SSL_SESSION_retrieve(ssl->initial_ctx->sessions, &data);
  341. if (session != NULL) {
  342. SSL_SESSION_up_ref(session);
  343. }
  344. CRYPTO_MUTEX_unlock(&ssl->initial_ctx->lock);
  345. if (session != NULL) {
  346. *out_session = session;
  347. return ssl_session_success;
  348. }
  349. }
  350. /* Fall back to the external cache, if it exists. */
  351. if (ssl->initial_ctx->get_session_cb == NULL) {
  352. return ssl_session_success;
  353. }
  354. int copy = 1;
  355. session = ssl->initial_ctx->get_session_cb(ssl, (uint8_t *)session_id,
  356. session_id_len, &copy);
  357. if (session == NULL) {
  358. return ssl_session_success;
  359. }
  360. if (session == SSL_magic_pending_session_ptr()) {
  361. return ssl_session_retry;
  362. }
  363. /* Increment reference count now if the session callback asks us to do so
  364. * (note that if the session structures returned by the callback are shared
  365. * between threads, it must handle the reference count itself [i.e. copy ==
  366. * 0], or things won't be thread-safe). */
  367. if (copy) {
  368. SSL_SESSION_up_ref(session);
  369. }
  370. /* Add the externally cached session to the internal cache if necessary. */
  371. if (!(ssl->initial_ctx->session_cache_mode &
  372. SSL_SESS_CACHE_NO_INTERNAL_STORE)) {
  373. SSL_CTX_add_session(ssl->initial_ctx, session);
  374. }
  375. *out_session = session;
  376. return ssl_session_success;
  377. }
  378. enum ssl_session_result_t ssl_get_prev_session(
  379. SSL *ssl, SSL_SESSION **out_session, int *out_send_ticket,
  380. const struct ssl_early_callback_ctx *ctx) {
  381. /* This is used only by servers. */
  382. assert(ssl->server);
  383. SSL_SESSION *session = NULL;
  384. int send_ticket = 0;
  385. /* If tickets are disabled, always behave as if no tickets are present. */
  386. const uint8_t *ticket = NULL;
  387. size_t ticket_len = 0;
  388. const int tickets_supported =
  389. !(SSL_get_options(ssl) & SSL_OP_NO_TICKET) &&
  390. (ssl->version > SSL3_VERSION || ctx->extensions != NULL) &&
  391. SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_session_ticket,
  392. &ticket, &ticket_len);
  393. if (tickets_supported) {
  394. if (!tls_process_ticket(ssl, &session, &send_ticket, ticket, ticket_len,
  395. ctx->session_id, ctx->session_id_len)) {
  396. return ssl_session_error;
  397. }
  398. } else {
  399. /* The client does not support session tickets, so the session ID should be
  400. * used instead. */
  401. enum ssl_session_result_t lookup_ret = ssl_lookup_session(
  402. ssl, &session, ctx->session_id, ctx->session_id_len);
  403. if (lookup_ret != ssl_session_success) {
  404. return lookup_ret;
  405. }
  406. }
  407. if (session == NULL ||
  408. session->sid_ctx_length != ssl->sid_ctx_length ||
  409. memcmp(session->sid_ctx, ssl->sid_ctx, ssl->sid_ctx_length) != 0) {
  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. goto fatal_error;
  423. }
  424. if (session->timeout < (long)(time(NULL) - session->time)) {
  425. if (!tickets_supported) {
  426. /* The session was from the cache, so remove it. */
  427. SSL_CTX_remove_session(ssl->initial_ctx, session);
  428. }
  429. goto no_session;
  430. }
  431. *out_session = session;
  432. *out_send_ticket = send_ticket;
  433. return ssl_session_success;
  434. fatal_error:
  435. SSL_SESSION_free(session);
  436. return ssl_session_error;
  437. no_session:
  438. *out_session = NULL;
  439. *out_send_ticket = tickets_supported;
  440. SSL_SESSION_free(session);
  441. return ssl_session_success;
  442. }
  443. int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c) {
  444. int ret = 0;
  445. SSL_SESSION *s;
  446. /* add just 1 reference count for the SSL_CTX's session cache even though it
  447. * has two ways of access: each session is in a doubly linked list and an
  448. * lhash */
  449. SSL_SESSION_up_ref(c);
  450. /* if session c is in already in cache, we take back the increment later */
  451. CRYPTO_MUTEX_lock_write(&ctx->lock);
  452. if (!lh_SSL_SESSION_insert(ctx->sessions, &s, c)) {
  453. CRYPTO_MUTEX_unlock(&ctx->lock);
  454. SSL_SESSION_free(c);
  455. return 0;
  456. }
  457. /* s != NULL iff we already had a session with the given session ID. In this
  458. * case, s == c should hold (then we did not really modify ctx->sessions), or
  459. * we're in trouble. */
  460. if (s != NULL && s != c) {
  461. /* We *are* in trouble ... */
  462. SSL_SESSION_list_remove(ctx, s);
  463. SSL_SESSION_free(s);
  464. /* ... so pretend the other session did not exist in cache (we cannot
  465. * handle two SSL_SESSION structures with identical session ID in the same
  466. * cache, which could happen e.g. when two threads concurrently obtain the
  467. * same session from an external cache) */
  468. s = NULL;
  469. }
  470. /* Put at the head of the queue unless it is already in the cache */
  471. if (s == NULL) {
  472. SSL_SESSION_list_add(ctx, c);
  473. }
  474. if (s != NULL) {
  475. /* existing cache entry -- decrement previously incremented reference count
  476. * because it already takes into account the cache */
  477. SSL_SESSION_free(s); /* s == c */
  478. ret = 0;
  479. } else {
  480. /* new cache entry -- remove old ones if cache has become too large */
  481. ret = 1;
  482. if (SSL_CTX_sess_get_cache_size(ctx) > 0) {
  483. while (SSL_CTX_sess_number(ctx) > SSL_CTX_sess_get_cache_size(ctx)) {
  484. if (!remove_session_lock(ctx, ctx->session_cache_tail, 0)) {
  485. break;
  486. }
  487. }
  488. }
  489. }
  490. CRYPTO_MUTEX_unlock(&ctx->lock);
  491. return ret;
  492. }
  493. int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c) {
  494. return remove_session_lock(ctx, c, 1);
  495. }
  496. static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lock) {
  497. SSL_SESSION *r;
  498. int ret = 0;
  499. if (c != NULL && c->session_id_length != 0) {
  500. if (lock) {
  501. CRYPTO_MUTEX_lock_write(&ctx->lock);
  502. }
  503. r = lh_SSL_SESSION_retrieve(ctx->sessions, c);
  504. if (r == c) {
  505. ret = 1;
  506. r = lh_SSL_SESSION_delete(ctx->sessions, c);
  507. SSL_SESSION_list_remove(ctx, c);
  508. }
  509. if (lock) {
  510. CRYPTO_MUTEX_unlock(&ctx->lock);
  511. }
  512. if (ret) {
  513. r->not_resumable = 1;
  514. if (ctx->remove_session_cb != NULL) {
  515. ctx->remove_session_cb(ctx, r);
  516. }
  517. SSL_SESSION_free(r);
  518. }
  519. }
  520. return ret;
  521. }
  522. SSL_SESSION *SSL_SESSION_up_ref(SSL_SESSION *session) {
  523. if (session) {
  524. CRYPTO_refcount_inc(&session->references);
  525. }
  526. return session;
  527. }
  528. void SSL_SESSION_free(SSL_SESSION *session) {
  529. if (session == NULL ||
  530. !CRYPTO_refcount_dec_and_test_zero(&session->references)) {
  531. return;
  532. }
  533. CRYPTO_free_ex_data(&g_ex_data_class, session, &session->ex_data);
  534. OPENSSL_cleanse(session->master_key, sizeof(session->master_key));
  535. OPENSSL_cleanse(session->session_id, sizeof(session->session_id));
  536. ssl_sess_cert_free(session->sess_cert);
  537. X509_free(session->peer);
  538. OPENSSL_free(session->tlsext_hostname);
  539. OPENSSL_free(session->tlsext_tick);
  540. OPENSSL_free(session->tlsext_signed_cert_timestamp_list);
  541. OPENSSL_free(session->ocsp_response);
  542. OPENSSL_free(session->psk_identity);
  543. OPENSSL_cleanse(session, sizeof(*session));
  544. OPENSSL_free(session);
  545. }
  546. int SSL_set_session(SSL *s, SSL_SESSION *session) {
  547. if (s->session == session) {
  548. return 1;
  549. }
  550. SSL_SESSION_free(s->session);
  551. s->session = session;
  552. if (session != NULL) {
  553. SSL_SESSION_up_ref(session);
  554. s->verify_result = session->verify_result;
  555. }
  556. return 1;
  557. }
  558. long SSL_SESSION_set_timeout(SSL_SESSION *s, long t) {
  559. if (s == NULL) {
  560. return 0;
  561. }
  562. s->timeout = t;
  563. return 1;
  564. }
  565. long SSL_SESSION_get_timeout(const SSL_SESSION *s) {
  566. if (s == NULL) {
  567. return 0;
  568. }
  569. return s->timeout;
  570. }
  571. long SSL_SESSION_get_time(const SSL_SESSION *s) {
  572. if (s == NULL) {
  573. return 0;
  574. }
  575. return s->time;
  576. }
  577. long SSL_SESSION_set_time(SSL_SESSION *s, long t) {
  578. if (s == NULL) {
  579. return 0;
  580. }
  581. s->time = t;
  582. return t;
  583. }
  584. X509 *SSL_SESSION_get0_peer(SSL_SESSION *s) { return s->peer; }
  585. int SSL_SESSION_set1_id_context(SSL_SESSION *s, const uint8_t *sid_ctx,
  586. unsigned int sid_ctx_len) {
  587. if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
  588. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  589. return 0;
  590. }
  591. s->sid_ctx_length = sid_ctx_len;
  592. memcpy(s->sid_ctx, sid_ctx, sid_ctx_len);
  593. return 1;
  594. }
  595. long SSL_CTX_set_timeout(SSL_CTX *s, long t) {
  596. long l;
  597. if (s == NULL) {
  598. return 0;
  599. }
  600. l = s->session_timeout;
  601. s->session_timeout = t;
  602. return l;
  603. }
  604. long SSL_CTX_get_timeout(const SSL_CTX *s) {
  605. if (s == NULL) {
  606. return 0;
  607. }
  608. return s->session_timeout;
  609. }
  610. typedef struct timeout_param_st {
  611. SSL_CTX *ctx;
  612. long time;
  613. LHASH_OF(SSL_SESSION) *cache;
  614. } TIMEOUT_PARAM;
  615. static void timeout_doall_arg(SSL_SESSION *sess, void *void_param) {
  616. TIMEOUT_PARAM *param = void_param;
  617. if (param->time == 0 ||
  618. param->time > (sess->time + sess->timeout)) {
  619. /* timeout */
  620. /* The reason we don't call SSL_CTX_remove_session() is to
  621. * save on locking overhead */
  622. (void) lh_SSL_SESSION_delete(param->cache, sess);
  623. SSL_SESSION_list_remove(param->ctx, sess);
  624. sess->not_resumable = 1;
  625. if (param->ctx->remove_session_cb != NULL) {
  626. param->ctx->remove_session_cb(param->ctx, sess);
  627. }
  628. SSL_SESSION_free(sess);
  629. }
  630. }
  631. void SSL_CTX_flush_sessions(SSL_CTX *ctx, long t) {
  632. TIMEOUT_PARAM tp;
  633. tp.ctx = ctx;
  634. tp.cache = ctx->sessions;
  635. if (tp.cache == NULL) {
  636. return;
  637. }
  638. tp.time = t;
  639. CRYPTO_MUTEX_lock_write(&ctx->lock);
  640. lh_SSL_SESSION_doall_arg(tp.cache, timeout_doall_arg, &tp);
  641. CRYPTO_MUTEX_unlock(&ctx->lock);
  642. }
  643. int ssl_clear_bad_session(SSL *s) {
  644. if (s->session != NULL && !(s->shutdown & SSL_SENT_SHUTDOWN) &&
  645. !SSL_in_init(s)) {
  646. SSL_CTX_remove_session(s->ctx, s->session);
  647. return 1;
  648. }
  649. return 0;
  650. }
  651. /* locked by SSL_CTX in the calling function */
  652. static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s) {
  653. if (s->next == NULL || s->prev == NULL) {
  654. return;
  655. }
  656. if (s->next == (SSL_SESSION *)&ctx->session_cache_tail) {
  657. /* last element in list */
  658. if (s->prev == (SSL_SESSION *)&ctx->session_cache_head) {
  659. /* only one element in list */
  660. ctx->session_cache_head = NULL;
  661. ctx->session_cache_tail = NULL;
  662. } else {
  663. ctx->session_cache_tail = s->prev;
  664. s->prev->next = (SSL_SESSION *)&(ctx->session_cache_tail);
  665. }
  666. } else {
  667. if (s->prev == (SSL_SESSION *)&ctx->session_cache_head) {
  668. /* first element in list */
  669. ctx->session_cache_head = s->next;
  670. s->next->prev = (SSL_SESSION *)&(ctx->session_cache_head);
  671. } else { /* middle of list */
  672. s->next->prev = s->prev;
  673. s->prev->next = s->next;
  674. }
  675. }
  676. s->prev = s->next = NULL;
  677. }
  678. static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s) {
  679. if (s->next != NULL && s->prev != NULL) {
  680. SSL_SESSION_list_remove(ctx, s);
  681. }
  682. if (ctx->session_cache_head == NULL) {
  683. ctx->session_cache_head = s;
  684. ctx->session_cache_tail = s;
  685. s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
  686. s->next = (SSL_SESSION *)&(ctx->session_cache_tail);
  687. } else {
  688. s->next = ctx->session_cache_head;
  689. s->next->prev = s;
  690. s->prev = (SSL_SESSION *)&(ctx->session_cache_head);
  691. ctx->session_cache_head = s;
  692. }
  693. }
  694. void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
  695. int (*cb)(struct ssl_st *ssl, SSL_SESSION *sess)) {
  696. ctx->new_session_cb = cb;
  697. }
  698. int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *sess) {
  699. return ctx->new_session_cb;
  700. }
  701. void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
  702. void (*cb)(SSL_CTX *ctx, SSL_SESSION *sess)) {
  703. ctx->remove_session_cb = cb;
  704. }
  705. void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX *ctx,
  706. SSL_SESSION *sess) {
  707. return ctx->remove_session_cb;
  708. }
  709. void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
  710. SSL_SESSION *(*cb)(struct ssl_st *ssl,
  711. uint8_t *data, int len,
  712. int *copy)) {
  713. ctx->get_session_cb = cb;
  714. }
  715. SSL_SESSION *(*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl, uint8_t *data,
  716. int len, int *copy) {
  717. return ctx->get_session_cb;
  718. }
  719. void SSL_CTX_set_info_callback(SSL_CTX *ctx,
  720. void (*cb)(const SSL *ssl, int type, int val)) {
  721. ctx->info_callback = cb;
  722. }
  723. void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl, int type,
  724. int val) {
  725. return ctx->info_callback;
  726. }
  727. void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, X509 **x509,
  728. EVP_PKEY **pkey)) {
  729. ctx->client_cert_cb = cb;
  730. }
  731. int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL *ssl, X509 **x509,
  732. EVP_PKEY **pkey) {
  733. return ctx->client_cert_cb;
  734. }
  735. void SSL_CTX_set_channel_id_cb(SSL_CTX *ctx,
  736. void (*cb)(SSL *ssl, EVP_PKEY **pkey)) {
  737. ctx->channel_id_cb = cb;
  738. }
  739. void (*SSL_CTX_get_channel_id_cb(SSL_CTX *ctx))(SSL *ssl, EVP_PKEY **pkey) {
  740. return ctx->channel_id_cb;
  741. }
  742. IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)