1
#pragma clang diagnostic ignored "-Wunknown-warning-option"
2
#pragma clang diagnostic ignored "-Wimplicit-int-conversion"
3
#pragma clang diagnostic ignored "-Wsign-conversion"
5
#pragma GCC diagnostic ignored "-Wconversion"
7
#pragma clang diagnostic ignored "-Wimplicit-int-conversion"
8
#pragma clang diagnostic ignored "-Wsign-conversion"
10
#pragma GCC diagnostic ignored "-Wconversion"
12
#pragma clang diagnostic ignored "-Wimplicit-int-conversion"
13
#pragma clang diagnostic ignored "-Wsign-conversion"
15
* SSL session cache implementation
17
* Copyright The Mbed TLS Contributors
18
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
20
* This file is provided under the Apache License 2.0, or the
21
* GNU General Public License v2.0 or later.
26
* Licensed under the Apache License, Version 2.0 (the "License"); you may
27
* not use this file except in compliance with the License.
28
* You may obtain a copy of the License at
30
* http://www.apache.org/licenses/LICENSE-2.0
32
* Unless required by applicable law or agreed to in writing, software
33
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
34
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35
* See the License for the specific language governing permissions and
36
* limitations under the License.
41
* GNU General Public License v2.0 or later:
43
* This program is free software; you can redistribute it and/or modify
44
* it under the terms of the GNU General Public License as published by
45
* the Free Software Foundation; either version 2 of the License, or
46
* (at your option) any later version.
48
* This program is distributed in the hope that it will be useful,
49
* but WITHOUT ANY WARRANTY; without even the implied warranty of
50
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
51
* GNU General Public License for more details.
53
* You should have received a copy of the GNU General Public License along
54
* with this program; if not, write to the Free Software Foundation, Inc.,
55
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
60
* These session callbacks use a simple chained list
61
* to store and retrieve the session information.
64
#if !defined(MBEDTLS_CONFIG_FILE)
65
#include "mbedtls/config.h"
67
#include MBEDTLS_CONFIG_FILE
70
#if defined(MBEDTLS_SSL_CACHE_C)
72
#if defined(MBEDTLS_PLATFORM_C)
73
#include "mbedtls/platform.h"
76
#define mbedtls_calloc calloc
77
#define mbedtls_free free
80
#include "mbedtls/ssl_cache.h"
84
void mbedtls_ssl_cache_init( mbedtls_ssl_cache_context *cache )
86
memset( cache, 0, sizeof( mbedtls_ssl_cache_context ) );
88
cache->timeout = MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT;
89
cache->max_entries = MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES;
91
#if defined(MBEDTLS_THREADING_C)
92
mbedtls_mutex_init( &cache->mutex );
96
int mbedtls_ssl_cache_get( void *data, mbedtls_ssl_session *session )
99
#if defined(MBEDTLS_HAVE_TIME)
100
mbedtls_time_t t = mbedtls_time( NULL );
102
mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
103
mbedtls_ssl_cache_entry *cur, *entry;
105
#if defined(MBEDTLS_THREADING_C)
106
if( mbedtls_mutex_lock( &cache->mutex ) != 0 )
118
#if defined(MBEDTLS_HAVE_TIME)
119
if( cache->timeout != 0 &&
120
(int) ( t - entry->timestamp ) > cache->timeout )
124
if( session->ciphersuite != entry->session.ciphersuite ||
125
session->compression != entry->session.compression ||
126
session->id_len != entry->session.id_len )
129
if( memcmp( session->id, entry->session.id,
130
entry->session.id_len ) != 0 )
133
memcpy( session->master, entry->session.master, 48 );
135
session->verify_result = entry->session.verify_result;
137
#if defined(MBEDTLS_X509_CRT_PARSE_C)
139
* Restore peer certificate (without rest of the original chain)
141
if( entry->peer_cert.p != NULL )
143
if( ( session->peer_cert = mbedtls_calloc( 1,
144
sizeof(mbedtls_x509_crt) ) ) == NULL )
150
mbedtls_x509_crt_init( session->peer_cert );
151
if( mbedtls_x509_crt_parse( session->peer_cert, entry->peer_cert.p,
152
entry->peer_cert.len ) != 0 )
154
mbedtls_free( session->peer_cert );
155
session->peer_cert = NULL;
160
#endif /* MBEDTLS_X509_CRT_PARSE_C */
167
#if defined(MBEDTLS_THREADING_C)
168
if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
175
int mbedtls_ssl_cache_set( void *data, const mbedtls_ssl_session *session )
178
#if defined(MBEDTLS_HAVE_TIME)
179
mbedtls_time_t t = mbedtls_time( NULL ), oldest = 0;
180
mbedtls_ssl_cache_entry *old = NULL;
182
mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
183
mbedtls_ssl_cache_entry *cur, *prv;
186
#if defined(MBEDTLS_THREADING_C)
187
if( ( ret = mbedtls_mutex_lock( &cache->mutex ) ) != 0 )
198
#if defined(MBEDTLS_HAVE_TIME)
199
if( cache->timeout != 0 &&
200
(int) ( t - cur->timestamp ) > cache->timeout )
203
break; /* expired, reuse this slot, update timestamp */
207
if( memcmp( session->id, cur->session.id, cur->session.id_len ) == 0 )
208
break; /* client reconnected, keep timestamp for session id */
210
#if defined(MBEDTLS_HAVE_TIME)
211
if( oldest == 0 || cur->timestamp < oldest )
213
oldest = cur->timestamp;
224
#if defined(MBEDTLS_HAVE_TIME)
226
* Reuse oldest entry if max_entries reached
228
if( count >= cache->max_entries )
238
#else /* MBEDTLS_HAVE_TIME */
240
* Reuse first entry in chain if max_entries reached,
241
* but move to last place
243
if( count >= cache->max_entries )
245
if( cache->chain == NULL )
252
cache->chain = cur->next;
256
#endif /* MBEDTLS_HAVE_TIME */
260
* max_entries not reached, create new entry
262
cur = mbedtls_calloc( 1, sizeof(mbedtls_ssl_cache_entry) );
275
#if defined(MBEDTLS_HAVE_TIME)
280
memcpy( &cur->session, session, sizeof( mbedtls_ssl_session ) );
282
#if defined(MBEDTLS_X509_CRT_PARSE_C)
284
* If we're reusing an entry, free its certificate first
286
if( cur->peer_cert.p != NULL )
288
mbedtls_free( cur->peer_cert.p );
289
memset( &cur->peer_cert, 0, sizeof(mbedtls_x509_buf) );
293
* Store peer certificate
295
if( session->peer_cert != NULL )
297
cur->peer_cert.p = mbedtls_calloc( 1, session->peer_cert->raw.len );
298
if( cur->peer_cert.p == NULL )
304
memcpy( cur->peer_cert.p, session->peer_cert->raw.p,
305
session->peer_cert->raw.len );
306
cur->peer_cert.len = session->peer_cert->raw.len;
308
cur->session.peer_cert = NULL;
310
#endif /* MBEDTLS_X509_CRT_PARSE_C */
315
#if defined(MBEDTLS_THREADING_C)
316
if( mbedtls_mutex_unlock( &cache->mutex ) != 0 )
323
#if defined(MBEDTLS_HAVE_TIME)
324
void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout )
326
if( timeout < 0 ) timeout = 0;
328
cache->timeout = timeout;
330
#endif /* MBEDTLS_HAVE_TIME */
332
void mbedtls_ssl_cache_set_max_entries( mbedtls_ssl_cache_context *cache, int max )
334
if( max < 0 ) max = 0;
336
cache->max_entries = max;
339
void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache )
341
mbedtls_ssl_cache_entry *cur, *prv;
350
mbedtls_ssl_session_free( &prv->session );
352
#if defined(MBEDTLS_X509_CRT_PARSE_C)
353
mbedtls_free( prv->peer_cert.p );
354
#endif /* MBEDTLS_X509_CRT_PARSE_C */
359
#if defined(MBEDTLS_THREADING_C)
360
mbedtls_mutex_free( &cache->mutex );
365
#endif /* MBEDTLS_SSL_CACHE_C */