Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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