ssl_cache.c 8.1 KB

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