ssl_cache.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 "mbedtls/ssl_internal.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. int mbedtls_ssl_cache_get( void *data, mbedtls_ssl_session *session )
  45. {
  46. int ret = 1;
  47. #if defined(MBEDTLS_HAVE_TIME)
  48. mbedtls_time_t t = mbedtls_time( NULL );
  49. #endif
  50. mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
  51. mbedtls_ssl_cache_entry *cur, *entry;
  52. #if defined(MBEDTLS_THREADING_C)
  53. if( mbedtls_mutex_lock( &cache->mutex ) != 0 )
  54. return( 1 );
  55. #endif
  56. cur = cache->chain;
  57. entry = NULL;
  58. while( cur != NULL )
  59. {
  60. entry = cur;
  61. cur = cur->next;
  62. #if defined(MBEDTLS_HAVE_TIME)
  63. if( cache->timeout != 0 &&
  64. (int) ( t - entry->timestamp ) > cache->timeout )
  65. continue;
  66. #endif
  67. if( session->id_len != entry->session.id_len ||
  68. memcmp( session->id, entry->session.id,
  69. entry->session.id_len ) != 0 )
  70. {
  71. continue;
  72. }
  73. ret = mbedtls_ssl_session_copy( session, &entry->session );
  74. if( ret != 0 )
  75. {
  76. ret = 1;
  77. goto exit;
  78. }
  79. #if defined(MBEDTLS_X509_CRT_PARSE_C) && \
  80. defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  81. /*
  82. * Restore peer certificate (without rest of the original chain)
  83. */
  84. if( entry->peer_cert.p != NULL )
  85. {
  86. /* `session->peer_cert` is NULL after the call to
  87. * mbedtls_ssl_session_copy(), because cache entries
  88. * have the `peer_cert` field set to NULL. */
  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 && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  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 = mbedtls_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. #if defined(MBEDTLS_X509_CRT_PARSE_C) && \
  209. defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  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. #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  219. /* Copy the entire session; this temporarily makes a copy of the
  220. * X.509 CRT structure even though we only want to store the raw CRT.
  221. * This inefficiency will go away as soon as we implement on-demand
  222. * parsing of CRTs, in which case there's no need for the `peer_cert`
  223. * field anymore in the first place, and we're done after this call. */
  224. ret = mbedtls_ssl_session_copy( &cur->session, session );
  225. if( ret != 0 )
  226. {
  227. ret = 1;
  228. goto exit;
  229. }
  230. #if defined(MBEDTLS_X509_CRT_PARSE_C) && \
  231. defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  232. /* If present, free the X.509 structure and only store the raw CRT data. */
  233. if( cur->session.peer_cert != NULL )
  234. {
  235. cur->peer_cert.p =
  236. mbedtls_calloc( 1, cur->session.peer_cert->raw.len );
  237. if( cur->peer_cert.p == NULL )
  238. {
  239. ret = 1;
  240. goto exit;
  241. }
  242. memcpy( cur->peer_cert.p,
  243. cur->session.peer_cert->raw.p,
  244. cur->session.peer_cert->raw.len );
  245. cur->peer_cert.len = session->peer_cert->raw.len;
  246. mbedtls_x509_crt_free( cur->session.peer_cert );
  247. mbedtls_free( cur->session.peer_cert );
  248. cur->session.peer_cert = NULL;
  249. }
  250. #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  251. ret = 0;
  252. exit:
  253. #if defined(MBEDTLS_THREADING_C)
  254. if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
  255. ret = 1;
  256. #endif
  257. return( ret );
  258. }
  259. #if defined(MBEDTLS_HAVE_TIME)
  260. void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout )
  261. {
  262. if( timeout < 0 ) timeout = 0;
  263. cache->timeout = timeout;
  264. }
  265. #endif /* MBEDTLS_HAVE_TIME */
  266. void mbedtls_ssl_cache_set_max_entries( mbedtls_ssl_cache_context *cache, int max )
  267. {
  268. if( max < 0 ) max = 0;
  269. cache->max_entries = max;
  270. }
  271. void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache )
  272. {
  273. mbedtls_ssl_cache_entry *cur, *prv;
  274. cur = cache->chain;
  275. while( cur != NULL )
  276. {
  277. prv = cur;
  278. cur = cur->next;
  279. mbedtls_ssl_session_free( &prv->session );
  280. #if defined(MBEDTLS_X509_CRT_PARSE_C) && \
  281. defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  282. mbedtls_free( prv->peer_cert.p );
  283. #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  284. mbedtls_free( prv );
  285. }
  286. #if defined(MBEDTLS_THREADING_C)
  287. mbedtls_mutex_free( &cache->mutex );
  288. #endif
  289. cache->chain = NULL;
  290. }
  291. #endif /* MBEDTLS_SSL_CACHE_C */