You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1031 lines
30 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 char g_pending_session_magic;
  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. {
  151. return (SSL_SESSION*) &g_pending_session_magic;
  152. }
  153. SSL_SESSION *SSL_get_session(const SSL *ssl)
  154. /* aka SSL_get0_session; gets 0 objects, just returns a copy of the pointer */
  155. {
  156. return(ssl->session);
  157. }
  158. SSL_SESSION *SSL_get1_session(SSL *ssl)
  159. /* variant of SSL_get_session: caller really gets something */
  160. {
  161. SSL_SESSION *sess;
  162. /* Need to lock this all up rather than just use CRYPTO_add so that
  163. * somebody doesn't free ssl->session between when we check it's
  164. * non-null and when we up the reference count. */
  165. CRYPTO_w_lock(CRYPTO_LOCK_SSL_SESSION);
  166. sess = ssl->session;
  167. if(sess)
  168. sess->references++;
  169. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_SESSION);
  170. return(sess);
  171. }
  172. int SSL_SESSION_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
  173. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
  174. {
  175. return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_SESSION, argl, argp,
  176. new_func, dup_func, free_func);
  177. }
  178. int SSL_SESSION_set_ex_data(SSL_SESSION *s, int idx, void *arg)
  179. {
  180. return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
  181. }
  182. void *SSL_SESSION_get_ex_data(const SSL_SESSION *s, int idx)
  183. {
  184. return(CRYPTO_get_ex_data(&s->ex_data,idx));
  185. }
  186. SSL_SESSION *SSL_SESSION_new(void)
  187. {
  188. SSL_SESSION *ss;
  189. ss=(SSL_SESSION *)OPENSSL_malloc(sizeof(SSL_SESSION));
  190. if (ss == NULL)
  191. {
  192. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_new, ERR_R_MALLOC_FAILURE);
  193. return(0);
  194. }
  195. memset(ss,0,sizeof(SSL_SESSION));
  196. ss->verify_result = 1; /* avoid 0 (= X509_V_OK) just in case */
  197. ss->references=1;
  198. ss->timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  199. ss->time=(unsigned long)time(NULL);
  200. CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
  201. return(ss);
  202. }
  203. const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len)
  204. {
  205. if(len)
  206. *len = s->session_id_length;
  207. return s->session_id;
  208. }
  209. /* Even with SSLv2, we have 16 bytes (128 bits) of session ID space. SSLv3/TLSv1
  210. * has 32 bytes (256 bits). As such, filling the ID with random gunk repeatedly
  211. * until we have no conflict is going to complete in one iteration pretty much
  212. * "most" of the time (btw: understatement). So, if it takes us 10 iterations
  213. * and we still can't avoid a conflict - well that's a reasonable point to call
  214. * it quits. Either the RAND code is broken or someone is trying to open roughly
  215. * very close to 2^128 (or 2^256) SSL sessions to our server. How you might
  216. * store that many sessions is perhaps a more interesting question ... */
  217. #define MAX_SESS_ID_ATTEMPTS 10
  218. static int def_generate_session_id(const SSL *ssl, unsigned char *id,
  219. unsigned int *id_len)
  220. {
  221. unsigned int retry = 0;
  222. do
  223. if (RAND_pseudo_bytes(id, *id_len) <= 0)
  224. return 0;
  225. while(SSL_has_matching_session_id(ssl, id, *id_len) &&
  226. (++retry < MAX_SESS_ID_ATTEMPTS));
  227. if(retry < MAX_SESS_ID_ATTEMPTS)
  228. return 1;
  229. /* else - woops a session_id match */
  230. /* XXX We should also check the external cache --
  231. * but the probability of a collision is negligible, and
  232. * we could not prevent the concurrent creation of sessions
  233. * with identical IDs since we currently don't have means
  234. * to atomically check whether a session ID already exists
  235. * and make a reservation for it if it does not
  236. * (this problem applies to the internal cache as well).
  237. */
  238. return 0;
  239. }
  240. int ssl_get_new_session(SSL *s, int session)
  241. {
  242. /* This gets used by clients and servers. */
  243. unsigned int tmp;
  244. SSL_SESSION *ss=NULL;
  245. GEN_SESSION_CB cb = def_generate_session_id;
  246. if (s->mode & SSL_MODE_NO_SESSION_CREATION)
  247. {
  248. OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, SSL_R_SESSION_MAY_NOT_BE_CREATED);
  249. return 0;
  250. }
  251. if ((ss=SSL_SESSION_new()) == NULL) return(0);
  252. /* If the context has a default timeout, use it over the default. */
  253. if (s->initial_ctx->session_timeout != 0)
  254. ss->timeout=s->initial_ctx->session_timeout;
  255. if (s->session != NULL)
  256. {
  257. SSL_SESSION_free(s->session);
  258. s->session=NULL;
  259. }
  260. if (session)
  261. {
  262. if (s->version == SSL3_VERSION ||
  263. s->version == TLS1_VERSION ||
  264. s->version == TLS1_1_VERSION ||
  265. s->version == TLS1_2_VERSION ||
  266. s->version == DTLS1_VERSION ||
  267. s->version == DTLS1_2_VERSION)
  268. {
  269. ss->ssl_version = s->version;
  270. ss->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
  271. }
  272. else
  273. {
  274. OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, SSL_R_UNSUPPORTED_SSL_VERSION);
  275. SSL_SESSION_free(ss);
  276. return(0);
  277. }
  278. /* If RFC4507 ticket use empty session ID */
  279. if (s->tlsext_ticket_expected)
  280. {
  281. ss->session_id_length = 0;
  282. goto sess_id_done;
  283. }
  284. /* Choose which callback will set the session ID */
  285. CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
  286. if(s->generate_session_id)
  287. cb = s->generate_session_id;
  288. else if(s->initial_ctx->generate_session_id)
  289. cb = s->initial_ctx->generate_session_id;
  290. CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
  291. /* Choose a session ID */
  292. tmp = ss->session_id_length;
  293. if(!cb(s, ss->session_id, &tmp))
  294. {
  295. /* The callback failed */
  296. OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, SSL_R_SSL_SESSION_ID_CALLBACK_FAILED);
  297. SSL_SESSION_free(ss);
  298. return(0);
  299. }
  300. /* Don't allow the callback to set the session length to zero.
  301. * nor set it higher than it was. */
  302. if(!tmp || (tmp > ss->session_id_length))
  303. {
  304. /* The callback set an illegal length */
  305. OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, SSL_R_SSL_SESSION_ID_HAS_BAD_LENGTH);
  306. SSL_SESSION_free(ss);
  307. return(0);
  308. }
  309. ss->session_id_length = tmp;
  310. /* Finally, check for a conflict */
  311. if(SSL_has_matching_session_id(s, ss->session_id,
  312. ss->session_id_length))
  313. {
  314. OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, SSL_R_SSL_SESSION_ID_CONFLICT);
  315. SSL_SESSION_free(ss);
  316. return(0);
  317. }
  318. sess_id_done:
  319. if (s->tlsext_hostname) {
  320. ss->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
  321. if (ss->tlsext_hostname == NULL) {
  322. OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, ERR_R_INTERNAL_ERROR);
  323. SSL_SESSION_free(ss);
  324. return 0;
  325. }
  326. }
  327. }
  328. else
  329. {
  330. ss->session_id_length=0;
  331. }
  332. if (s->sid_ctx_length > sizeof ss->sid_ctx)
  333. {
  334. OPENSSL_PUT_ERROR(SSL, ssl_get_new_session, ERR_R_INTERNAL_ERROR);
  335. SSL_SESSION_free(ss);
  336. return 0;
  337. }
  338. memcpy(ss->sid_ctx,s->sid_ctx,s->sid_ctx_length);
  339. ss->sid_ctx_length=s->sid_ctx_length;
  340. s->session=ss;
  341. ss->ssl_version=s->version;
  342. ss->verify_result = X509_V_OK;
  343. return(1);
  344. }
  345. /* ssl_get_prev attempts to find an SSL_SESSION to be used to resume this
  346. * connection. It is only called by servers.
  347. *
  348. * ctx: contains the early callback context, which is the result of a
  349. * shallow parse of the ClientHello.
  350. *
  351. * Returns:
  352. * -1: error
  353. * 0: a session may have been found.
  354. *
  355. * Side effects:
  356. * - If a session is found then s->session is pointed at it (after freeing an
  357. * existing session if need be) and s->verify_result is set from the session.
  358. * - Both for new and resumed sessions, s->tlsext_ticket_expected is set to 1
  359. * if the server should issue a new session ticket (to 0 otherwise).
  360. */
  361. int ssl_get_prev_session(SSL *s, const struct ssl_early_callback_ctx *ctx)
  362. {
  363. /* This is used only by servers. */
  364. SSL_SESSION *ret=NULL;
  365. int fatal = 0;
  366. int try_session_cache = 1;
  367. int r;
  368. if (ctx->session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH)
  369. goto err;
  370. if (ctx->session_id_len == 0)
  371. try_session_cache = 0;
  372. r = tls1_process_ticket(s, ctx, &ret); /* sets s->tlsext_ticket_expected */
  373. switch (r)
  374. {
  375. case -1: /* Error during processing */
  376. fatal = 1;
  377. goto err;
  378. case 0: /* No ticket found */
  379. case 1: /* Zero length ticket found */
  380. break; /* Ok to carry on processing session id. */
  381. case 2: /* Ticket found but not decrypted. */
  382. case 3: /* Ticket decrypted, *ret has been set. */
  383. try_session_cache = 0;
  384. break;
  385. default:
  386. abort();
  387. }
  388. if (try_session_cache &&
  389. ret == NULL &&
  390. !(s->initial_ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_LOOKUP))
  391. {
  392. SSL_SESSION data;
  393. data.ssl_version=s->version;
  394. data.session_id_length=ctx->session_id_len;
  395. if (ctx->session_id_len == 0)
  396. return 0;
  397. memcpy(data.session_id,ctx->session_id,ctx->session_id_len);
  398. CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
  399. ret=lh_SSL_SESSION_retrieve(s->initial_ctx->sessions,&data);
  400. if (ret != NULL)
  401. {
  402. /* don't allow other threads to steal it: */
  403. CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
  404. }
  405. CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
  406. if (ret == NULL)
  407. s->initial_ctx->stats.sess_miss++;
  408. }
  409. if (try_session_cache &&
  410. ret == NULL &&
  411. s->initial_ctx->get_session_cb != NULL)
  412. {
  413. int copy=1;
  414. if ((ret=s->initial_ctx->get_session_cb(s,(unsigned char *) ctx->session_id,ctx->session_id_len,&copy)))
  415. {
  416. if (ret == SSL_magic_pending_session_ptr())
  417. {
  418. /* This is a magic value which indicates that
  419. * the callback needs to unwind the stack and
  420. * figure out the session asynchronously. */
  421. return PENDING_SESSION;
  422. }
  423. s->initial_ctx->stats.sess_cb_hit++;
  424. /* Increment reference count now if the session callback
  425. * asks us to do so (note that if the session structures
  426. * returned by the callback are shared between threads,
  427. * it must handle the reference count itself [i.e. copy == 0],
  428. * or things won't be thread-safe). */
  429. if (copy)
  430. CRYPTO_add(&ret->references,1,CRYPTO_LOCK_SSL_SESSION);
  431. /* Add the externally cached session to the internal
  432. * cache as well if and only if we are supposed to. */
  433. if(!(s->initial_ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE))
  434. /* The following should not return 1, otherwise,
  435. * things are very strange */
  436. SSL_CTX_add_session(s->initial_ctx,ret);
  437. }
  438. }
  439. if (ret == NULL)
  440. goto err;
  441. /* Now ret is non-NULL and we own one of its reference counts. */
  442. if (ret->sid_ctx_length != s->sid_ctx_length
  443. || memcmp(ret->sid_ctx,s->sid_ctx,ret->sid_ctx_length))
  444. {
  445. /* We have the session requested by the client, but we don't
  446. * want to use it in this context. */
  447. goto err; /* treat like cache miss */
  448. }
  449. if((s->verify_mode & SSL_VERIFY_PEER) && s->sid_ctx_length == 0)
  450. {
  451. /* We can't be sure if this session is being used out of
  452. * context, which is especially important for SSL_VERIFY_PEER.
  453. * The application should have used SSL[_CTX]_set_session_id_context.
  454. *
  455. * For this error case, we generate an error instead of treating
  456. * the event like a cache miss (otherwise it would be easy for
  457. * applications to effectively disable the session cache by
  458. * accident without anyone noticing).
  459. */
  460. OPENSSL_PUT_ERROR(SSL, ssl_get_prev_session, SSL_R_SESSION_ID_CONTEXT_UNINITIALIZED);
  461. fatal = 1;
  462. goto err;
  463. }
  464. if (ret->timeout < (long)(time(NULL) - ret->time)) /* timeout */
  465. {
  466. s->initial_ctx->stats.sess_timeout++;
  467. if (try_session_cache)
  468. {
  469. /* session was from the cache, so remove it */
  470. SSL_CTX_remove_session(s->initial_ctx,ret);
  471. }
  472. goto err;
  473. }
  474. s->initial_ctx->stats.sess_hit++;
  475. if (s->session != NULL)
  476. SSL_SESSION_free(s->session);
  477. s->session=ret;
  478. s->verify_result = s->session->verify_result;
  479. return 1;
  480. err:
  481. if (ret != NULL)
  482. {
  483. SSL_SESSION_free(ret);
  484. if (!try_session_cache)
  485. {
  486. /* The session was from a ticket, so we should
  487. * issue a ticket for the new session */
  488. s->tlsext_ticket_expected = 1;
  489. }
  490. }
  491. if (fatal)
  492. return -1;
  493. else
  494. return 0;
  495. }
  496. int SSL_CTX_add_session(SSL_CTX *ctx, SSL_SESSION *c)
  497. {
  498. int ret=0;
  499. SSL_SESSION *s;
  500. /* add just 1 reference count for the SSL_CTX's session cache
  501. * even though it has two ways of access: each session is in a
  502. * doubly linked list and an lhash */
  503. CRYPTO_add(&c->references,1,CRYPTO_LOCK_SSL_SESSION);
  504. /* if session c is in already in cache, we take back the increment later */
  505. CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  506. if (!lh_SSL_SESSION_insert(ctx->sessions,&s,c)) {
  507. return 0;
  508. }
  509. /* s != NULL iff we already had a session with the given PID.
  510. * In this case, s == c should hold (then we did not really modify
  511. * ctx->sessions), or we're in trouble. */
  512. if (s != NULL && s != c)
  513. {
  514. /* We *are* in trouble ... */
  515. SSL_SESSION_list_remove(ctx,s);
  516. SSL_SESSION_free(s);
  517. /* ... so pretend the other session did not exist in cache
  518. * (we cannot handle two SSL_SESSION structures with identical
  519. * session ID in the same cache, which could happen e.g. when
  520. * two threads concurrently obtain the same session from an external
  521. * cache) */
  522. s = NULL;
  523. }
  524. /* Put at the head of the queue unless it is already in the cache */
  525. if (s == NULL)
  526. SSL_SESSION_list_add(ctx,c);
  527. if (s != NULL)
  528. {
  529. /* existing cache entry -- decrement previously incremented reference
  530. * count because it already takes into account the cache */
  531. SSL_SESSION_free(s); /* s == c */
  532. ret=0;
  533. }
  534. else
  535. {
  536. /* new cache entry -- remove old ones if cache has become too large */
  537. ret=1;
  538. if (SSL_CTX_sess_get_cache_size(ctx) > 0)
  539. {
  540. while (SSL_CTX_sess_number(ctx) >
  541. SSL_CTX_sess_get_cache_size(ctx))
  542. {
  543. if (!remove_session_lock(ctx,
  544. ctx->session_cache_tail, 0))
  545. break;
  546. else
  547. ctx->stats.sess_cache_full++;
  548. }
  549. }
  550. }
  551. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  552. return(ret);
  553. }
  554. int SSL_CTX_remove_session(SSL_CTX *ctx, SSL_SESSION *c)
  555. {
  556. return remove_session_lock(ctx, c, 1);
  557. }
  558. static int remove_session_lock(SSL_CTX *ctx, SSL_SESSION *c, int lck)
  559. {
  560. SSL_SESSION *r;
  561. int ret=0;
  562. if ((c != NULL) && (c->session_id_length != 0))
  563. {
  564. if(lck) CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  565. if ((r = lh_SSL_SESSION_retrieve(ctx->sessions,c)) == c)
  566. {
  567. ret=1;
  568. r=lh_SSL_SESSION_delete(ctx->sessions,c);
  569. SSL_SESSION_list_remove(ctx,c);
  570. }
  571. if(lck) CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  572. if (ret)
  573. {
  574. r->not_resumable=1;
  575. if (ctx->remove_session_cb != NULL)
  576. ctx->remove_session_cb(ctx,r);
  577. SSL_SESSION_free(r);
  578. }
  579. }
  580. else
  581. ret=0;
  582. return(ret);
  583. }
  584. void SSL_SESSION_free(SSL_SESSION *ss)
  585. {
  586. int i;
  587. if(ss == NULL)
  588. return;
  589. i=CRYPTO_add(&ss->references,-1,CRYPTO_LOCK_SSL_SESSION);
  590. #ifdef REF_PRINT
  591. REF_PRINT("SSL_SESSION",ss);
  592. #endif
  593. if (i > 0) return;
  594. #ifdef REF_CHECK
  595. if (i < 0)
  596. {
  597. fprintf(stderr,"SSL_SESSION_free, bad reference count\n");
  598. abort(); /* ok */
  599. }
  600. #endif
  601. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_SESSION, ss, &ss->ex_data);
  602. OPENSSL_cleanse(ss->master_key,sizeof ss->master_key);
  603. OPENSSL_cleanse(ss->session_id,sizeof ss->session_id);
  604. if (ss->sess_cert != NULL) ssl_sess_cert_free(ss->sess_cert);
  605. if (ss->peer != NULL) X509_free(ss->peer);
  606. if (ss->tlsext_hostname != NULL) OPENSSL_free(ss->tlsext_hostname);
  607. if (ss->tlsext_tick != NULL) OPENSSL_free(ss->tlsext_tick);
  608. if (ss->tlsext_signed_cert_timestamp_list != NULL)
  609. OPENSSL_free(ss->tlsext_signed_cert_timestamp_list);
  610. if (ss->ocsp_response != NULL)
  611. OPENSSL_free(ss->ocsp_response);
  612. if (ss->psk_identity != NULL)
  613. OPENSSL_free(ss->psk_identity);
  614. OPENSSL_cleanse(ss,sizeof(*ss));
  615. OPENSSL_free(ss);
  616. }
  617. int SSL_set_session(SSL *s, SSL_SESSION *session)
  618. {
  619. int ret=0;
  620. const SSL_METHOD *meth;
  621. if (session != NULL)
  622. {
  623. meth=s->ctx->method->get_ssl_method(session->ssl_version);
  624. if (meth == NULL)
  625. meth=s->method->get_ssl_method(session->ssl_version);
  626. if (meth == NULL)
  627. {
  628. OPENSSL_PUT_ERROR(SSL, SSL_set_session, SSL_R_UNABLE_TO_FIND_SSL_METHOD);
  629. return(0);
  630. }
  631. if (meth != s->method)
  632. {
  633. if (!SSL_set_ssl_method(s,meth))
  634. return(0);
  635. }
  636. /* CRYPTO_w_lock(CRYPTO_LOCK_SSL);*/
  637. CRYPTO_add(&session->references,1,CRYPTO_LOCK_SSL_SESSION);
  638. if (s->session != NULL)
  639. SSL_SESSION_free(s->session);
  640. s->session=session;
  641. s->verify_result = s->session->verify_result;
  642. /* CRYPTO_w_unlock(CRYPTO_LOCK_SSL);*/
  643. ret=1;
  644. }
  645. else
  646. {
  647. if (s->session != NULL)
  648. {
  649. SSL_SESSION_free(s->session);
  650. s->session=NULL;
  651. }
  652. meth=s->ctx->method;
  653. if (meth != s->method)
  654. {
  655. if (!SSL_set_ssl_method(s,meth))
  656. return(0);
  657. }
  658. ret=1;
  659. }
  660. return(ret);
  661. }
  662. long SSL_SESSION_set_timeout(SSL_SESSION *s, long t)
  663. {
  664. if (s == NULL) return(0);
  665. s->timeout=t;
  666. return(1);
  667. }
  668. long SSL_SESSION_get_timeout(const SSL_SESSION *s)
  669. {
  670. if (s == NULL) return(0);
  671. return(s->timeout);
  672. }
  673. long SSL_SESSION_get_time(const SSL_SESSION *s)
  674. {
  675. if (s == NULL) return(0);
  676. return(s->time);
  677. }
  678. long SSL_SESSION_set_time(SSL_SESSION *s, long t)
  679. {
  680. if (s == NULL) return(0);
  681. s->time=t;
  682. return(t);
  683. }
  684. X509 *SSL_SESSION_get0_peer(SSL_SESSION *s)
  685. {
  686. return s->peer;
  687. }
  688. int SSL_SESSION_set1_id_context(SSL_SESSION *s,const unsigned char *sid_ctx,
  689. unsigned int sid_ctx_len)
  690. {
  691. if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
  692. {
  693. OPENSSL_PUT_ERROR(SSL, SSL_SESSION_set1_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  694. return 0;
  695. }
  696. s->sid_ctx_length=sid_ctx_len;
  697. memcpy(s->sid_ctx,sid_ctx,sid_ctx_len);
  698. return 1;
  699. }
  700. long SSL_CTX_set_timeout(SSL_CTX *s, long t)
  701. {
  702. long l;
  703. if (s == NULL) return(0);
  704. l=s->session_timeout;
  705. s->session_timeout=t;
  706. return(l);
  707. }
  708. long SSL_CTX_get_timeout(const SSL_CTX *s)
  709. {
  710. if (s == NULL) return(0);
  711. return(s->session_timeout);
  712. }
  713. int SSL_set_session_secret_cb(SSL *s, int (*tls_session_secret_cb)(SSL *s, void *secret, int *secret_len,
  714. STACK_OF(SSL_CIPHER) *peer_ciphers, const SSL_CIPHER **cipher, void *arg), void *arg)
  715. {
  716. if (s == NULL) return(0);
  717. s->tls_session_secret_cb = tls_session_secret_cb;
  718. s->tls_session_secret_cb_arg = arg;
  719. return(1);
  720. }
  721. int SSL_set_session_ticket_ext_cb(SSL *s, tls_session_ticket_ext_cb_fn cb,
  722. void *arg)
  723. {
  724. if (s == NULL) return(0);
  725. s->tls_session_ticket_ext_cb = cb;
  726. s->tls_session_ticket_ext_cb_arg = arg;
  727. return(1);
  728. }
  729. int SSL_set_session_ticket_ext(SSL *s, void *ext_data, int ext_len)
  730. {
  731. if (s->version >= TLS1_VERSION)
  732. {
  733. if (s->tlsext_session_ticket)
  734. {
  735. OPENSSL_free(s->tlsext_session_ticket);
  736. s->tlsext_session_ticket = NULL;
  737. }
  738. s->tlsext_session_ticket = OPENSSL_malloc(sizeof(TLS_SESSION_TICKET_EXT) + ext_len);
  739. if (!s->tlsext_session_ticket)
  740. {
  741. OPENSSL_PUT_ERROR(SSL, SSL_set_session_ticket_ext, ERR_R_MALLOC_FAILURE);
  742. return 0;
  743. }
  744. if (ext_data)
  745. {
  746. s->tlsext_session_ticket->length = ext_len;
  747. s->tlsext_session_ticket->data = s->tlsext_session_ticket + 1;
  748. memcpy(s->tlsext_session_ticket->data, ext_data, ext_len);
  749. }
  750. else
  751. {
  752. s->tlsext_session_ticket->length = 0;
  753. s->tlsext_session_ticket->data = NULL;
  754. }
  755. return 1;
  756. }
  757. return 0;
  758. }
  759. typedef struct timeout_param_st
  760. {
  761. SSL_CTX *ctx;
  762. long time;
  763. LHASH_OF(SSL_SESSION) *cache;
  764. } TIMEOUT_PARAM;
  765. static void timeout_doall_arg(SSL_SESSION *sess, void *void_param)
  766. {
  767. TIMEOUT_PARAM *param = void_param;
  768. if ((param->time == 0) || (param->time > (sess->time+sess->timeout))) /* timeout */
  769. {
  770. /* The reason we don't call SSL_CTX_remove_session() is to
  771. * save on locking overhead */
  772. (void)lh_SSL_SESSION_delete(param->cache,sess);
  773. SSL_SESSION_list_remove(param->ctx,sess);
  774. sess->not_resumable=1;
  775. if (param->ctx->remove_session_cb != NULL)
  776. param->ctx->remove_session_cb(param->ctx,sess);
  777. SSL_SESSION_free(sess);
  778. }
  779. }
  780. void SSL_CTX_flush_sessions(SSL_CTX *s, long t)
  781. {
  782. TIMEOUT_PARAM tp;
  783. tp.ctx=s;
  784. tp.cache=s->sessions;
  785. if (tp.cache == NULL) return;
  786. tp.time=t;
  787. CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  788. lh_SSL_SESSION_doall_arg(tp.cache, timeout_doall_arg, &tp);
  789. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  790. }
  791. int ssl_clear_bad_session(SSL *s)
  792. {
  793. if ( (s->session != NULL) &&
  794. !(s->shutdown & SSL_SENT_SHUTDOWN) &&
  795. !(SSL_in_init(s) || SSL_in_before(s)))
  796. {
  797. SSL_CTX_remove_session(s->ctx,s->session);
  798. return(1);
  799. }
  800. else
  801. return(0);
  802. }
  803. /* locked by SSL_CTX in the calling function */
  804. static void SSL_SESSION_list_remove(SSL_CTX *ctx, SSL_SESSION *s)
  805. {
  806. if ((s->next == NULL) || (s->prev == NULL)) return;
  807. if (s->next == (SSL_SESSION *)&(ctx->session_cache_tail))
  808. { /* last element in list */
  809. if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head))
  810. { /* only one element in list */
  811. ctx->session_cache_head=NULL;
  812. ctx->session_cache_tail=NULL;
  813. }
  814. else
  815. {
  816. ctx->session_cache_tail=s->prev;
  817. s->prev->next=(SSL_SESSION *)&(ctx->session_cache_tail);
  818. }
  819. }
  820. else
  821. {
  822. if (s->prev == (SSL_SESSION *)&(ctx->session_cache_head))
  823. { /* first element in list */
  824. ctx->session_cache_head=s->next;
  825. s->next->prev=(SSL_SESSION *)&(ctx->session_cache_head);
  826. }
  827. else
  828. { /* middle of list */
  829. s->next->prev=s->prev;
  830. s->prev->next=s->next;
  831. }
  832. }
  833. s->prev=s->next=NULL;
  834. }
  835. static void SSL_SESSION_list_add(SSL_CTX *ctx, SSL_SESSION *s)
  836. {
  837. if ((s->next != NULL) && (s->prev != NULL))
  838. SSL_SESSION_list_remove(ctx,s);
  839. if (ctx->session_cache_head == NULL)
  840. {
  841. ctx->session_cache_head=s;
  842. ctx->session_cache_tail=s;
  843. s->prev=(SSL_SESSION *)&(ctx->session_cache_head);
  844. s->next=(SSL_SESSION *)&(ctx->session_cache_tail);
  845. }
  846. else
  847. {
  848. s->next=ctx->session_cache_head;
  849. s->next->prev=s;
  850. s->prev=(SSL_SESSION *)&(ctx->session_cache_head);
  851. ctx->session_cache_head=s;
  852. }
  853. }
  854. void SSL_CTX_sess_set_new_cb(SSL_CTX *ctx,
  855. int (*cb)(struct ssl_st *ssl,SSL_SESSION *sess))
  856. {
  857. ctx->new_session_cb=cb;
  858. }
  859. int (*SSL_CTX_sess_get_new_cb(SSL_CTX *ctx))(SSL *ssl, SSL_SESSION *sess)
  860. {
  861. return ctx->new_session_cb;
  862. }
  863. void SSL_CTX_sess_set_remove_cb(SSL_CTX *ctx,
  864. void (*cb)(SSL_CTX *ctx,SSL_SESSION *sess))
  865. {
  866. ctx->remove_session_cb=cb;
  867. }
  868. void (*SSL_CTX_sess_get_remove_cb(SSL_CTX *ctx))(SSL_CTX * ctx,SSL_SESSION *sess)
  869. {
  870. return ctx->remove_session_cb;
  871. }
  872. void SSL_CTX_sess_set_get_cb(SSL_CTX *ctx,
  873. SSL_SESSION *(*cb)(struct ssl_st *ssl,
  874. unsigned char *data,int len,int *copy))
  875. {
  876. ctx->get_session_cb=cb;
  877. }
  878. SSL_SESSION * (*SSL_CTX_sess_get_get_cb(SSL_CTX *ctx))(SSL *ssl,
  879. unsigned char *data,int len,int *copy)
  880. {
  881. return ctx->get_session_cb;
  882. }
  883. void SSL_CTX_set_info_callback(SSL_CTX *ctx,
  884. void (*cb)(const SSL *ssl,int type,int val))
  885. {
  886. ctx->info_callback=cb;
  887. }
  888. void (*SSL_CTX_get_info_callback(SSL_CTX *ctx))(const SSL *ssl,int type,int val)
  889. {
  890. return ctx->info_callback;
  891. }
  892. void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
  893. int (*cb)(SSL *ssl, X509 **x509, EVP_PKEY **pkey))
  894. {
  895. ctx->client_cert_cb=cb;
  896. }
  897. int (*SSL_CTX_get_client_cert_cb(SSL_CTX *ctx))(SSL * ssl, X509 ** x509 , EVP_PKEY **pkey)
  898. {
  899. return ctx->client_cert_cb;
  900. }
  901. void SSL_CTX_set_cookie_generate_cb(SSL_CTX *ctx,
  902. int (*cb)(SSL *ssl, uint8_t *cookie, size_t *cookie_len))
  903. {
  904. ctx->app_gen_cookie_cb=cb;
  905. }
  906. void SSL_CTX_set_cookie_verify_cb(SSL_CTX *ctx,
  907. int (*cb)(SSL *ssl, const uint8_t *cookie, size_t cookie_len))
  908. {
  909. ctx->app_verify_cookie_cb=cb;
  910. }
  911. void SSL_CTX_set_channel_id_cb(SSL_CTX *ctx,
  912. void (*cb)(SSL *ssl, EVP_PKEY **pkey))
  913. {
  914. ctx->channel_id_cb=cb;
  915. }
  916. void (*SSL_CTX_get_channel_id_cb(SSL_CTX *ctx))(SSL * ssl, EVP_PKEY **pkey)
  917. {
  918. return ctx->channel_id_cb;
  919. }
  920. IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)