Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

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