ssl_cache.c 8.9 KB

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