2 * SSL session cache implementation
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * These session callbacks use a simple chained list
21 * to store and retrieve the session information.
26 #if defined(MBEDTLS_SSL_CACHE_C)
28 #if defined(MBEDTLS_PLATFORM_C)
29 #include "mbedtls/platform.h"
32 #define mbedtls_calloc calloc
33 #define mbedtls_free free
36 #include "mbedtls/ssl_cache.h"
37 #include "mbedtls/ssl_internal.h"
41 void mbedtls_ssl_cache_init(mbedtls_ssl_cache_context
*cache
) {
42 memset(cache
, 0, sizeof(mbedtls_ssl_cache_context
));
44 cache
->timeout
= MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT
;
45 cache
->max_entries
= MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES
;
47 #if defined(MBEDTLS_THREADING_C)
48 mbedtls_mutex_init(&cache
->mutex
);
52 int mbedtls_ssl_cache_get(void *data
, mbedtls_ssl_session
*session
) {
54 #if defined(MBEDTLS_HAVE_TIME)
55 mbedtls_time_t t
= mbedtls_time(NULL
);
57 mbedtls_ssl_cache_context
*cache
= (mbedtls_ssl_cache_context
*) data
;
58 mbedtls_ssl_cache_entry
*cur
, *entry
;
60 #if defined(MBEDTLS_THREADING_C)
61 if (mbedtls_mutex_lock(&cache
->mutex
) != 0)
72 #if defined(MBEDTLS_HAVE_TIME)
73 if (cache
->timeout
!= 0 &&
74 (int)(t
- entry
->timestamp
) > cache
->timeout
)
78 if (session
->ciphersuite
!= entry
->session
.ciphersuite
||
79 session
->compression
!= entry
->session
.compression
||
80 session
->id_len
!= entry
->session
.id_len
)
83 if (memcmp(session
->id
, entry
->session
.id
,
84 entry
->session
.id_len
) != 0)
87 ret
= mbedtls_ssl_session_copy(session
, &entry
->session
);
93 #if defined(MBEDTLS_X509_CRT_PARSE_C) && \
94 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
96 * Restore peer certificate (without rest of the original chain)
98 if (entry
->peer_cert
.p
!= NULL
) {
99 /* `session->peer_cert` is NULL after the call to
100 * mbedtls_ssl_session_copy(), because cache entries
101 * have the `peer_cert` field set to NULL. */
103 if ((session
->peer_cert
= mbedtls_calloc(1,
104 sizeof(mbedtls_x509_crt
))) == NULL
) {
109 mbedtls_x509_crt_init(session
->peer_cert
);
110 if (mbedtls_x509_crt_parse(session
->peer_cert
, entry
->peer_cert
.p
,
111 entry
->peer_cert
.len
) != 0) {
112 mbedtls_free(session
->peer_cert
);
113 session
->peer_cert
= NULL
;
118 #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
125 #if defined(MBEDTLS_THREADING_C)
126 if (mbedtls_mutex_unlock(&cache
->mutex
) != 0)
133 int mbedtls_ssl_cache_set(void *data
, const mbedtls_ssl_session
*session
) {
135 #if defined(MBEDTLS_HAVE_TIME)
136 mbedtls_time_t t
= mbedtls_time(NULL
), oldest
= 0;
137 mbedtls_ssl_cache_entry
*old
= NULL
;
139 mbedtls_ssl_cache_context
*cache
= (mbedtls_ssl_cache_context
*) data
;
140 mbedtls_ssl_cache_entry
*cur
, *prv
;
143 #if defined(MBEDTLS_THREADING_C)
144 if ((ret
= mbedtls_mutex_lock(&cache
->mutex
)) != 0)
151 while (cur
!= NULL
) {
154 #if defined(MBEDTLS_HAVE_TIME)
155 if (cache
->timeout
!= 0 &&
156 (int)(t
- cur
->timestamp
) > cache
->timeout
) {
158 break; /* expired, reuse this slot, update timestamp */
162 if (memcmp(session
->id
, cur
->session
.id
, cur
->session
.id_len
) == 0)
163 break; /* client reconnected, keep timestamp for session id */
165 #if defined(MBEDTLS_HAVE_TIME)
166 if (oldest
== 0 || cur
->timestamp
< oldest
) {
167 oldest
= cur
->timestamp
;
177 #if defined(MBEDTLS_HAVE_TIME)
179 * Reuse oldest entry if max_entries reached
181 if (count
>= cache
->max_entries
) {
189 #else /* MBEDTLS_HAVE_TIME */
191 * Reuse first entry in chain if max_entries reached,
192 * but move to last place
194 if (count
>= cache
->max_entries
) {
195 if (cache
->chain
== NULL
) {
201 cache
->chain
= cur
->next
;
205 #endif /* MBEDTLS_HAVE_TIME */
208 * max_entries not reached, create new entry
210 cur
= mbedtls_calloc(1, sizeof(mbedtls_ssl_cache_entry
));
222 #if defined(MBEDTLS_HAVE_TIME)
227 #if defined(MBEDTLS_X509_CRT_PARSE_C) && \
228 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
230 * If we're reusing an entry, free its certificate first
232 if (cur
->peer_cert
.p
!= NULL
) {
233 mbedtls_free(cur
->peer_cert
.p
);
234 memset(&cur
->peer_cert
, 0, sizeof(mbedtls_x509_buf
));
236 #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
238 /* Copy the entire session; this temporarily makes a copy of the
239 * X.509 CRT structure even though we only want to store the raw CRT.
240 * This inefficiency will go away as soon as we implement on-demand
241 * parsing of CRTs, in which case there's no need for the `peer_cert`
242 * field anymore in the first place, and we're done after this call. */
243 ret
= mbedtls_ssl_session_copy(&cur
->session
, session
);
249 #if defined(MBEDTLS_X509_CRT_PARSE_C) && \
250 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
251 /* If present, free the X.509 structure and only store the raw CRT data. */
252 if (cur
->session
.peer_cert
!= NULL
) {
254 mbedtls_calloc(1, cur
->session
.peer_cert
->raw
.len
);
255 if (cur
->peer_cert
.p
== NULL
) {
260 memcpy(cur
->peer_cert
.p
,
261 cur
->session
.peer_cert
->raw
.p
,
262 cur
->session
.peer_cert
->raw
.len
);
263 cur
->peer_cert
.len
= session
->peer_cert
->raw
.len
;
265 mbedtls_x509_crt_free(cur
->session
.peer_cert
);
266 mbedtls_free(cur
->session
.peer_cert
);
267 cur
->session
.peer_cert
= NULL
;
269 #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
274 #if defined(MBEDTLS_THREADING_C)
275 if (mbedtls_mutex_unlock(&cache
->mutex
) != 0)
282 #if defined(MBEDTLS_HAVE_TIME)
283 void mbedtls_ssl_cache_set_timeout(mbedtls_ssl_cache_context
*cache
, int timeout
) {
284 if (timeout
< 0) timeout
= 0;
286 cache
->timeout
= timeout
;
288 #endif /* MBEDTLS_HAVE_TIME */
290 void mbedtls_ssl_cache_set_max_entries(mbedtls_ssl_cache_context
*cache
, int max
) {
291 if (max
< 0) max
= 0;
293 cache
->max_entries
= max
;
296 void mbedtls_ssl_cache_free(mbedtls_ssl_cache_context
*cache
) {
297 mbedtls_ssl_cache_entry
*cur
, *prv
;
301 while (cur
!= NULL
) {
305 mbedtls_ssl_session_free(&prv
->session
);
307 #if defined(MBEDTLS_X509_CRT_PARSE_C) && \
308 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
309 mbedtls_free(prv
->peer_cert
.p
);
310 #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
315 #if defined(MBEDTLS_THREADING_C)
316 mbedtls_mutex_free(&cache
->mutex
);
321 #endif /* MBEDTLS_SSL_CACHE_C */