選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

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