ssl_cache.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * SSL session cache implementation
  3. *
  4. * Copyright (C) 2006-2014, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. /*
  26. * These session callbacks use a simple chained list
  27. * to store and retrieve the session information.
  28. */
  29. #if !defined(POLARSSL_CONFIG_FILE)
  30. #include "polarssl/config.h"
  31. #else
  32. #include POLARSSL_CONFIG_FILE
  33. #endif
  34. #if defined(POLARSSL_SSL_CACHE_C)
  35. #include "polarssl/ssl_cache.h"
  36. #if defined(POLARSSL_PLATFORM_C)
  37. #include "polarssl/platform.h"
  38. #else
  39. #define polarssl_malloc malloc
  40. #define polarssl_free free
  41. #endif
  42. #include <stdlib.h>
  43. void ssl_cache_init( ssl_cache_context *cache )
  44. {
  45. memset( cache, 0, sizeof( ssl_cache_context ) );
  46. cache->timeout = SSL_CACHE_DEFAULT_TIMEOUT;
  47. cache->max_entries = SSL_CACHE_DEFAULT_MAX_ENTRIES;
  48. #if defined(POLARSSL_THREADING_C)
  49. polarssl_mutex_init( &cache->mutex );
  50. #endif
  51. }
  52. int ssl_cache_get( void *data, ssl_session *session )
  53. {
  54. int ret = 1;
  55. #if defined(POLARSSL_HAVE_TIME)
  56. time_t t = time( NULL );
  57. #endif
  58. ssl_cache_context *cache = (ssl_cache_context *) data;
  59. ssl_cache_entry *cur, *entry;
  60. #if defined(POLARSSL_THREADING_C)
  61. if( polarssl_mutex_lock( &cache->mutex ) != 0 )
  62. return( 1 );
  63. #endif
  64. cur = cache->chain;
  65. entry = NULL;
  66. while( cur != NULL )
  67. {
  68. entry = cur;
  69. cur = cur->next;
  70. #if defined(POLARSSL_HAVE_TIME)
  71. if( cache->timeout != 0 &&
  72. (int) ( t - entry->timestamp ) > cache->timeout )
  73. continue;
  74. #endif
  75. if( session->ciphersuite != entry->session.ciphersuite ||
  76. session->compression != entry->session.compression ||
  77. session->length != entry->session.length )
  78. continue;
  79. if( memcmp( session->id, entry->session.id,
  80. entry->session.length ) != 0 )
  81. continue;
  82. memcpy( session->master, entry->session.master, 48 );
  83. session->verify_result = entry->session.verify_result;
  84. #if defined(POLARSSL_X509_CRT_PARSE_C)
  85. /*
  86. * Restore peer certificate (without rest of the original chain)
  87. */
  88. if( entry->peer_cert.p != NULL )
  89. {
  90. session->peer_cert =
  91. (x509_crt *) polarssl_malloc( sizeof(x509_crt) );
  92. if( session->peer_cert == NULL )
  93. {
  94. ret = 1;
  95. goto exit;
  96. }
  97. x509_crt_init( session->peer_cert );
  98. if( x509_crt_parse( session->peer_cert, entry->peer_cert.p,
  99. entry->peer_cert.len ) != 0 )
  100. {
  101. polarssl_free( session->peer_cert );
  102. session->peer_cert = NULL;
  103. ret = 1;
  104. goto exit;
  105. }
  106. }
  107. #endif /* POLARSSL_X509_CRT_PARSE_C */
  108. ret = 0;
  109. goto exit;
  110. }
  111. exit:
  112. #if defined(POLARSSL_THREADING_C)
  113. if( polarssl_mutex_unlock( &cache->mutex ) != 0 )
  114. ret = 1;
  115. #endif
  116. return( ret );
  117. }
  118. int ssl_cache_set( void *data, const ssl_session *session )
  119. {
  120. int ret = 1;
  121. #if defined(POLARSSL_HAVE_TIME)
  122. time_t t = time( NULL ), oldest = 0;
  123. ssl_cache_entry *old = NULL;
  124. #endif
  125. ssl_cache_context *cache = (ssl_cache_context *) data;
  126. ssl_cache_entry *cur, *prv;
  127. int count = 0;
  128. #if defined(POLARSSL_THREADING_C)
  129. if( ( ret = polarssl_mutex_lock( &cache->mutex ) ) != 0 )
  130. return( ret );
  131. #endif
  132. cur = cache->chain;
  133. prv = NULL;
  134. while( cur != NULL )
  135. {
  136. count++;
  137. #if defined(POLARSSL_HAVE_TIME)
  138. if( cache->timeout != 0 &&
  139. (int) ( t - cur->timestamp ) > cache->timeout )
  140. {
  141. cur->timestamp = t;
  142. break; /* expired, reuse this slot, update timestamp */
  143. }
  144. #endif
  145. if( memcmp( session->id, cur->session.id, cur->session.length ) == 0 )
  146. break; /* client reconnected, keep timestamp for session id */
  147. #if defined(POLARSSL_HAVE_TIME)
  148. if( oldest == 0 || cur->timestamp < oldest )
  149. {
  150. oldest = cur->timestamp;
  151. old = cur;
  152. }
  153. #endif
  154. prv = cur;
  155. cur = cur->next;
  156. }
  157. if( cur == NULL )
  158. {
  159. #if defined(POLARSSL_HAVE_TIME)
  160. /*
  161. * Reuse oldest entry if max_entries reached
  162. */
  163. if( count >= cache->max_entries )
  164. {
  165. if( old == NULL )
  166. {
  167. ret = 1;
  168. goto exit;
  169. }
  170. cur = old;
  171. }
  172. #else /* POLARSSL_HAVE_TIME */
  173. /*
  174. * Reuse first entry in chain if max_entries reached,
  175. * but move to last place
  176. */
  177. if( count >= cache->max_entries )
  178. {
  179. if( cache->chain == NULL )
  180. {
  181. ret = 1;
  182. goto exit;
  183. }
  184. cur = cache->chain;
  185. cache->chain = cur->next;
  186. cur->next = NULL;
  187. prv->next = cur;
  188. }
  189. #endif /* POLARSSL_HAVE_TIME */
  190. else
  191. {
  192. /*
  193. * max_entries not reached, create new entry
  194. */
  195. cur = (ssl_cache_entry *)
  196. polarssl_malloc( sizeof(ssl_cache_entry) );
  197. if( cur == NULL )
  198. {
  199. ret = 1;
  200. goto exit;
  201. }
  202. memset( cur, 0, sizeof(ssl_cache_entry) );
  203. if( prv == NULL )
  204. cache->chain = cur;
  205. else
  206. prv->next = cur;
  207. }
  208. #if defined(POLARSSL_HAVE_TIME)
  209. cur->timestamp = t;
  210. #endif
  211. }
  212. memcpy( &cur->session, session, sizeof( ssl_session ) );
  213. #if defined(POLARSSL_X509_CRT_PARSE_C)
  214. /*
  215. * If we're reusing an entry, free its certificate first
  216. */
  217. if( cur->peer_cert.p != NULL )
  218. {
  219. polarssl_free( cur->peer_cert.p );
  220. memset( &cur->peer_cert, 0, sizeof(x509_buf) );
  221. }
  222. /*
  223. * Store peer certificate
  224. */
  225. if( session->peer_cert != NULL )
  226. {
  227. cur->peer_cert.p = (unsigned char *)
  228. polarssl_malloc( session->peer_cert->raw.len );
  229. if( cur->peer_cert.p == NULL )
  230. {
  231. ret = 1;
  232. goto exit;
  233. }
  234. memcpy( cur->peer_cert.p, session->peer_cert->raw.p,
  235. session->peer_cert->raw.len );
  236. cur->peer_cert.len = session->peer_cert->raw.len;
  237. cur->session.peer_cert = NULL;
  238. }
  239. #endif /* POLARSSL_X509_CRT_PARSE_C */
  240. ret = 0;
  241. exit:
  242. #if defined(POLARSSL_THREADING_C)
  243. if( polarssl_mutex_unlock( &cache->mutex ) != 0 )
  244. ret = 1;
  245. #endif
  246. return( ret );
  247. }
  248. #if defined(POLARSSL_HAVE_TIME)
  249. void ssl_cache_set_timeout( ssl_cache_context *cache, int timeout )
  250. {
  251. if( timeout < 0 ) timeout = 0;
  252. cache->timeout = timeout;
  253. }
  254. #endif /* POLARSSL_HAVE_TIME */
  255. void ssl_cache_set_max_entries( ssl_cache_context *cache, int max )
  256. {
  257. if( max < 0 ) max = 0;
  258. cache->max_entries = max;
  259. }
  260. void ssl_cache_free( ssl_cache_context *cache )
  261. {
  262. ssl_cache_entry *cur, *prv;
  263. cur = cache->chain;
  264. while( cur != NULL )
  265. {
  266. prv = cur;
  267. cur = cur->next;
  268. ssl_session_free( &prv->session );
  269. #if defined(POLARSSL_X509_CRT_PARSE_C)
  270. polarssl_free( prv->peer_cert.p );
  271. #endif /* POLARSSL_X509_CRT_PARSE_C */
  272. polarssl_free( prv );
  273. }
  274. #if defined(POLARSSL_THREADING_C)
  275. polarssl_mutex_free( &cache->mutex );
  276. #endif
  277. }
  278. #endif /* POLARSSL_SSL_CACHE_C */