• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
24 #include "common.h"
25 
26 #if defined(MBEDTLS_SSL_CACHE_C)
27 
28 #if defined(MBEDTLS_PLATFORM_C)
29 #include "mbedtls/platform.h"
30 #else
31 #include <stdlib.h>
32 #define mbedtls_calloc    calloc
33 #define mbedtls_free      free
34 #endif
35 
36 #include "mbedtls/ssl_cache.h"
37 #include "ssl_misc.h"
38 
39 #include <string.h>
40 
mbedtls_ssl_cache_init(mbedtls_ssl_cache_context * cache)41 void mbedtls_ssl_cache_init( mbedtls_ssl_cache_context *cache )
42 {
43     memset( cache, 0, sizeof( mbedtls_ssl_cache_context ) );
44 
45     cache->timeout = MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT;
46     cache->max_entries = MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES;
47 
48 #if defined(MBEDTLS_THREADING_C)
49     mbedtls_mutex_init( &cache->mutex );
50 #endif
51 }
52 
ssl_cache_find_entry(mbedtls_ssl_cache_context * cache,unsigned char const * session_id,size_t session_id_len,mbedtls_ssl_cache_entry ** dst)53 static int ssl_cache_find_entry( mbedtls_ssl_cache_context *cache,
54                                  unsigned char const *session_id,
55                                  size_t session_id_len,
56                                  mbedtls_ssl_cache_entry **dst )
57 {
58     int ret = 1;
59 #if defined(MBEDTLS_HAVE_TIME)
60     mbedtls_time_t t = mbedtls_time( NULL );
61 #endif
62     mbedtls_ssl_cache_entry *cur;
63 
64     for( cur = cache->chain; cur != NULL; cur = cur->next )
65     {
66 #if defined(MBEDTLS_HAVE_TIME)
67         if( cache->timeout != 0 &&
68             (int) ( t - cur->timestamp ) > cache->timeout )
69             continue;
70 #endif
71 
72         if( session_id_len != cur->session_id_len ||
73             memcmp( session_id, cur->session_id,
74                     cur->session_id_len ) != 0 )
75         {
76             continue;
77         }
78 
79         break;
80     }
81 
82     if( cur != NULL )
83     {
84         *dst = cur;
85         ret = 0;
86     }
87 
88     return( ret );
89 }
90 
91 
mbedtls_ssl_cache_get(void * data,unsigned char const * session_id,size_t session_id_len,mbedtls_ssl_session * session)92 int mbedtls_ssl_cache_get( void *data,
93                            unsigned char const *session_id,
94                            size_t session_id_len,
95                            mbedtls_ssl_session *session )
96 {
97     int ret = 1;
98     mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
99     mbedtls_ssl_cache_entry *entry;
100 
101 #if defined(MBEDTLS_THREADING_C)
102     if( mbedtls_mutex_lock( &cache->mutex ) != 0 )
103         return( 1 );
104 #endif
105 
106     ret = ssl_cache_find_entry( cache, session_id, session_id_len, &entry );
107     if( ret != 0 )
108         goto exit;
109 
110     ret = mbedtls_ssl_session_load( session,
111                                     entry->session,
112                                     entry->session_len );
113     if( ret != 0 )
114         goto exit;
115 
116     ret = 0;
117 
118 exit:
119 #if defined(MBEDTLS_THREADING_C)
120     if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
121         ret = 1;
122 #endif
123 
124     return( ret );
125 }
126 
ssl_cache_pick_writing_slot(mbedtls_ssl_cache_context * cache,unsigned char const * session_id,size_t session_id_len,mbedtls_ssl_cache_entry ** dst)127 static int ssl_cache_pick_writing_slot( mbedtls_ssl_cache_context *cache,
128                                         unsigned char const *session_id,
129                                         size_t session_id_len,
130                                         mbedtls_ssl_cache_entry **dst )
131 {
132 #if defined(MBEDTLS_HAVE_TIME)
133     mbedtls_time_t t = mbedtls_time( NULL ), oldest = 0;
134 #endif /* MBEDTLS_HAVE_TIME */
135 
136     mbedtls_ssl_cache_entry *old = NULL;
137     int count = 0;
138     mbedtls_ssl_cache_entry *cur, *last;
139 
140     /* Check 1: Is there already an entry with the given session ID?
141      *
142      * If yes, overwrite it.
143      *
144      * If not, `count` will hold the size of the session cache
145      * at the end of this loop, and `last` will point to the last
146      * entry, both of which will be used later. */
147 
148     last = NULL;
149     for( cur = cache->chain; cur != NULL; cur = cur->next )
150     {
151         count++;
152         if( session_id_len == cur->session_id_len &&
153             memcmp( session_id, cur->session_id, cur->session_id_len ) == 0 )
154         {
155             goto found;
156         }
157         last = cur;
158     }
159 
160     /* Check 2: Is there an outdated entry in the cache?
161      *
162      * If so, overwrite it.
163      *
164      * If not, remember the oldest entry in `old` for later.
165      */
166 
167 #if defined(MBEDTLS_HAVE_TIME)
168     for( cur = cache->chain; cur != NULL; cur = cur->next )
169     {
170         if( cache->timeout != 0 &&
171             (int) ( t - cur->timestamp ) > cache->timeout )
172         {
173             goto found;
174         }
175 
176         if( oldest == 0 || cur->timestamp < oldest )
177         {
178             oldest = cur->timestamp;
179             old = cur;
180         }
181     }
182 #endif /* MBEDTLS_HAVE_TIME */
183 
184     /* Check 3: Is there free space in the cache? */
185 
186     if( count < cache->max_entries )
187     {
188         /* Create new entry */
189         cur = mbedtls_calloc( 1, sizeof(mbedtls_ssl_cache_entry) );
190         if( cur == NULL )
191             return( 1 );
192 
193         /* Append to the end of the linked list. */
194         if( last == NULL )
195             cache->chain = cur;
196         else
197             last->next = cur;
198 
199         goto found;
200     }
201 
202     /* Last resort: The cache is full and doesn't contain any outdated
203      * elements. In this case, we evict the oldest one, judged by timestamp
204      * (if present) or cache-order. */
205 
206 #if defined(MBEDTLS_HAVE_TIME)
207     if( old == NULL )
208     {
209         /* This should only happen on an ill-configured cache
210          * with max_entries == 0. */
211         return( 1 );
212     }
213 #else /* MBEDTLS_HAVE_TIME */
214     /* Reuse first entry in chain, but move to last place. */
215     if( cache->chain == NULL )
216         return( 1 );
217 
218     old = cache->chain;
219     cache->chain = old->next;
220     old->next = NULL;
221     last->next = old;
222 #endif /* MBEDTLS_HAVE_TIME */
223 
224     /* Now `old` points to the oldest entry to be overwritten. */
225     cur = old;
226 
227 found:
228 
229 #if defined(MBEDTLS_HAVE_TIME)
230     cur->timestamp = t;
231 #endif
232 
233     /* If we're reusing an entry, free it first. */
234     if( cur->session != NULL )
235     {
236         mbedtls_free( cur->session );
237         cur->session = NULL;
238         cur->session_len = 0;
239         memset( cur->session_id, 0, sizeof( cur->session_id ) );
240         cur->session_id_len = 0;
241     }
242 
243     *dst = cur;
244     return( 0 );
245 }
246 
mbedtls_ssl_cache_set(void * data,unsigned char const * session_id,size_t session_id_len,const mbedtls_ssl_session * session)247 int mbedtls_ssl_cache_set( void *data,
248                            unsigned char const *session_id,
249                            size_t session_id_len,
250                            const mbedtls_ssl_session *session )
251 {
252     int ret = 1;
253     mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
254     mbedtls_ssl_cache_entry *cur;
255 
256     size_t session_serialized_len;
257     unsigned char *session_serialized = NULL;
258 
259 #if defined(MBEDTLS_THREADING_C)
260     if( ( ret = mbedtls_mutex_lock( &cache->mutex ) ) != 0 )
261         return( ret );
262 #endif
263 
264     ret = ssl_cache_pick_writing_slot( cache,
265                                        session_id, session_id_len,
266                                        &cur );
267     if( ret != 0 )
268         goto exit;
269 
270     /* Check how much space we need to serialize the session
271      * and allocate a sufficiently large buffer. */
272     ret = mbedtls_ssl_session_save( session, NULL, 0, &session_serialized_len );
273     if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
274     {
275         ret = 1;
276         goto exit;
277     }
278 
279     session_serialized = mbedtls_calloc( 1, session_serialized_len );
280     if( session_serialized == NULL )
281     {
282         ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
283         goto exit;
284     }
285 
286     /* Now serialize the session into the allocated buffer. */
287     ret = mbedtls_ssl_session_save( session,
288                                     session_serialized,
289                                     session_serialized_len,
290                                     &session_serialized_len );
291     if( ret != 0 )
292         goto exit;
293 
294     if( session_id_len > sizeof( cur->session_id ) )
295     {
296         ret = 1;
297         goto exit;
298     }
299     cur->session_id_len = session_id_len;
300     memcpy( cur->session_id, session_id, session_id_len );
301 
302     cur->session = session_serialized;
303     cur->session_len = session_serialized_len;
304     session_serialized = NULL;
305 
306     ret = 0;
307 
308 exit:
309 #if defined(MBEDTLS_THREADING_C)
310     if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
311         ret = 1;
312 #endif
313 
314     if( session_serialized != NULL )
315         mbedtls_platform_zeroize( session_serialized, session_serialized_len );
316 
317     return( ret );
318 }
319 
320 #if defined(MBEDTLS_HAVE_TIME)
mbedtls_ssl_cache_set_timeout(mbedtls_ssl_cache_context * cache,int timeout)321 void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout )
322 {
323     if( timeout < 0 ) timeout = 0;
324 
325     cache->timeout = timeout;
326 }
327 #endif /* MBEDTLS_HAVE_TIME */
328 
mbedtls_ssl_cache_set_max_entries(mbedtls_ssl_cache_context * cache,int max)329 void mbedtls_ssl_cache_set_max_entries( mbedtls_ssl_cache_context *cache, int max )
330 {
331     if( max < 0 ) max = 0;
332 
333     cache->max_entries = max;
334 }
335 
mbedtls_ssl_cache_free(mbedtls_ssl_cache_context * cache)336 void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache )
337 {
338     mbedtls_ssl_cache_entry *cur, *prv;
339 
340     cur = cache->chain;
341 
342     while( cur != NULL )
343     {
344         prv = cur;
345         cur = cur->next;
346 
347         mbedtls_free( prv->session );
348         mbedtls_free( prv );
349     }
350 
351 #if defined(MBEDTLS_THREADING_C)
352     mbedtls_mutex_free( &cache->mutex );
353 #endif
354     cache->chain = NULL;
355 }
356 
357 #endif /* MBEDTLS_SSL_CACHE_C */
358