ssl_cache.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * SSL session cache implementation
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. /*
  20. * These session callbacks use a simple chained list
  21. * to store and retrieve the session information.
  22. */
  23. #include "common.h"
  24. #if defined(MBEDTLS_SSL_CACHE_C)
  25. #if defined(MBEDTLS_PLATFORM_C)
  26. #include "mbedtls/platform.h"
  27. #else
  28. #include <stdlib.h>
  29. #define mbedtls_calloc calloc
  30. #define mbedtls_free free
  31. #endif
  32. #include "mbedtls/ssl_cache.h"
  33. #include "ssl_misc.h"
  34. #include <string.h>
  35. void mbedtls_ssl_cache_init( mbedtls_ssl_cache_context *cache )
  36. {
  37. memset( cache, 0, sizeof( mbedtls_ssl_cache_context ) );
  38. cache->timeout = MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT;
  39. cache->max_entries = MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES;
  40. #if defined(MBEDTLS_THREADING_C)
  41. mbedtls_mutex_init( &cache->mutex );
  42. #endif
  43. }
  44. static int ssl_cache_find_entry( mbedtls_ssl_cache_context *cache,
  45. unsigned char const *session_id,
  46. size_t session_id_len,
  47. mbedtls_ssl_cache_entry **dst )
  48. {
  49. int ret = 1;
  50. #if defined(MBEDTLS_HAVE_TIME)
  51. mbedtls_time_t t = mbedtls_time( NULL );
  52. #endif
  53. mbedtls_ssl_cache_entry *cur;
  54. for( cur = cache->chain; cur != NULL; cur = cur->next )
  55. {
  56. #if defined(MBEDTLS_HAVE_TIME)
  57. if( cache->timeout != 0 &&
  58. (int) ( t - cur->timestamp ) > cache->timeout )
  59. continue;
  60. #endif
  61. if( session_id_len != cur->session_id_len ||
  62. memcmp( session_id, cur->session_id,
  63. cur->session_id_len ) != 0 )
  64. {
  65. continue;
  66. }
  67. break;
  68. }
  69. if( cur != NULL )
  70. {
  71. *dst = cur;
  72. ret = 0;
  73. }
  74. return( ret );
  75. }
  76. int mbedtls_ssl_cache_get( void *data,
  77. unsigned char const *session_id,
  78. size_t session_id_len,
  79. mbedtls_ssl_session *session )
  80. {
  81. int ret = 1;
  82. mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
  83. mbedtls_ssl_cache_entry *entry;
  84. #if defined(MBEDTLS_THREADING_C)
  85. if( mbedtls_mutex_lock( &cache->mutex ) != 0 )
  86. return( 1 );
  87. #endif
  88. ret = ssl_cache_find_entry( cache, session_id, session_id_len, &entry );
  89. if( ret != 0 )
  90. goto exit;
  91. ret = mbedtls_ssl_session_load( session,
  92. entry->session,
  93. entry->session_len );
  94. if( ret != 0 )
  95. goto exit;
  96. ret = 0;
  97. exit:
  98. #if defined(MBEDTLS_THREADING_C)
  99. if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
  100. ret = 1;
  101. #endif
  102. return( ret );
  103. }
  104. static int ssl_cache_pick_writing_slot( mbedtls_ssl_cache_context *cache,
  105. unsigned char const *session_id,
  106. size_t session_id_len,
  107. mbedtls_ssl_cache_entry **dst )
  108. {
  109. #if defined(MBEDTLS_HAVE_TIME)
  110. mbedtls_time_t t = mbedtls_time( NULL ), oldest = 0;
  111. #endif /* MBEDTLS_HAVE_TIME */
  112. mbedtls_ssl_cache_entry *old = NULL;
  113. int count = 0;
  114. mbedtls_ssl_cache_entry *cur, *last;
  115. /* Check 1: Is there already an entry with the given session ID?
  116. *
  117. * If yes, overwrite it.
  118. *
  119. * If not, `count` will hold the size of the session cache
  120. * at the end of this loop, and `last` will point to the last
  121. * entry, both of which will be used later. */
  122. last = NULL;
  123. for( cur = cache->chain; cur != NULL; cur = cur->next )
  124. {
  125. count++;
  126. if( session_id_len == cur->session_id_len &&
  127. memcmp( session_id, cur->session_id, cur->session_id_len ) == 0 )
  128. {
  129. goto found;
  130. }
  131. last = cur;
  132. }
  133. /* Check 2: Is there an outdated entry in the cache?
  134. *
  135. * If so, overwrite it.
  136. *
  137. * If not, remember the oldest entry in `old` for later.
  138. */
  139. #if defined(MBEDTLS_HAVE_TIME)
  140. for( cur = cache->chain; cur != NULL; cur = cur->next )
  141. {
  142. if( cache->timeout != 0 &&
  143. (int) ( t - cur->timestamp ) > cache->timeout )
  144. {
  145. goto found;
  146. }
  147. if( oldest == 0 || cur->timestamp < oldest )
  148. {
  149. oldest = cur->timestamp;
  150. old = cur;
  151. }
  152. }
  153. #endif /* MBEDTLS_HAVE_TIME */
  154. /* Check 3: Is there free space in the cache? */
  155. if( count < cache->max_entries )
  156. {
  157. /* Create new entry */
  158. cur = mbedtls_calloc( 1, sizeof(mbedtls_ssl_cache_entry) );
  159. if( cur == NULL )
  160. return( 1 );
  161. /* Append to the end of the linked list. */
  162. if( last == NULL )
  163. cache->chain = cur;
  164. else
  165. last->next = cur;
  166. goto found;
  167. }
  168. /* Last resort: The cache is full and doesn't contain any outdated
  169. * elements. In this case, we evict the oldest one, judged by timestamp
  170. * (if present) or cache-order. */
  171. #if defined(MBEDTLS_HAVE_TIME)
  172. if( old == NULL )
  173. {
  174. /* This should only happen on an ill-configured cache
  175. * with max_entries == 0. */
  176. return( 1 );
  177. }
  178. #else /* MBEDTLS_HAVE_TIME */
  179. /* Reuse first entry in chain, but move to last place. */
  180. if( cache->chain == NULL )
  181. return( 1 );
  182. old = cache->chain;
  183. cache->chain = old->next;
  184. old->next = NULL;
  185. last->next = old;
  186. #endif /* MBEDTLS_HAVE_TIME */
  187. /* Now `old` points to the oldest entry to be overwritten. */
  188. cur = old;
  189. found:
  190. #if defined(MBEDTLS_HAVE_TIME)
  191. cur->timestamp = t;
  192. #endif
  193. /* If we're reusing an entry, free it first. */
  194. if( cur->session != NULL )
  195. {
  196. mbedtls_free( cur->session );
  197. cur->session = NULL;
  198. cur->session_len = 0;
  199. memset( cur->session_id, 0, sizeof( cur->session_id ) );
  200. cur->session_id_len = 0;
  201. }
  202. *dst = cur;
  203. return( 0 );
  204. }
  205. int mbedtls_ssl_cache_set( void *data,
  206. unsigned char const *session_id,
  207. size_t session_id_len,
  208. const mbedtls_ssl_session *session )
  209. {
  210. int ret = 1;
  211. mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
  212. mbedtls_ssl_cache_entry *cur;
  213. size_t session_serialized_len;
  214. unsigned char *session_serialized = NULL;
  215. #if defined(MBEDTLS_THREADING_C)
  216. if( ( ret = mbedtls_mutex_lock( &cache->mutex ) ) != 0 )
  217. return( ret );
  218. #endif
  219. ret = ssl_cache_pick_writing_slot( cache,
  220. session_id, session_id_len,
  221. &cur );
  222. if( ret != 0 )
  223. goto exit;
  224. /* Check how much space we need to serialize the session
  225. * and allocate a sufficiently large buffer. */
  226. ret = mbedtls_ssl_session_save( session, NULL, 0, &session_serialized_len );
  227. if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
  228. {
  229. ret = 1;
  230. goto exit;
  231. }
  232. session_serialized = mbedtls_calloc( 1, session_serialized_len );
  233. if( session_serialized == NULL )
  234. {
  235. ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
  236. goto exit;
  237. }
  238. /* Now serialize the session into the allocated buffer. */
  239. ret = mbedtls_ssl_session_save( session,
  240. session_serialized,
  241. session_serialized_len,
  242. &session_serialized_len );
  243. if( ret != 0 )
  244. goto exit;
  245. if( session_id_len > sizeof( cur->session_id ) )
  246. {
  247. ret = 1;
  248. goto exit;
  249. }
  250. cur->session_id_len = session_id_len;
  251. memcpy( cur->session_id, session_id, session_id_len );
  252. cur->session = session_serialized;
  253. cur->session_len = session_serialized_len;
  254. session_serialized = NULL;
  255. ret = 0;
  256. exit:
  257. #if defined(MBEDTLS_THREADING_C)
  258. if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
  259. ret = 1;
  260. #endif
  261. if( session_serialized != NULL )
  262. mbedtls_platform_zeroize( session_serialized, session_serialized_len );
  263. return( ret );
  264. }
  265. #if defined(MBEDTLS_HAVE_TIME)
  266. void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout )
  267. {
  268. if( timeout < 0 ) timeout = 0;
  269. cache->timeout = timeout;
  270. }
  271. #endif /* MBEDTLS_HAVE_TIME */
  272. void mbedtls_ssl_cache_set_max_entries( mbedtls_ssl_cache_context *cache, int max )
  273. {
  274. if( max < 0 ) max = 0;
  275. cache->max_entries = max;
  276. }
  277. void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache )
  278. {
  279. mbedtls_ssl_cache_entry *cur, *prv;
  280. cur = cache->chain;
  281. while( cur != NULL )
  282. {
  283. prv = cur;
  284. cur = cur->next;
  285. mbedtls_free( prv->session );
  286. mbedtls_free( prv );
  287. }
  288. #if defined(MBEDTLS_THREADING_C)
  289. mbedtls_mutex_free( &cache->mutex );
  290. #endif
  291. cache->chain = NULL;
  292. }
  293. #endif /* MBEDTLS_SSL_CACHE_C */