No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

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