Merge pull request #2735 from jareckib/master
[RRG-proxmark3.git] / common / mbedtls / ssl_cache.c
blob16818b391cf5083eed0e7f590c5d13be83a56119
1 /*
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.
24 #include "common.h"
26 #if defined(MBEDTLS_SSL_CACHE_C)
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
36 #include "mbedtls/ssl_cache.h"
37 #include "mbedtls/ssl_internal.h"
39 #include <string.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);
49 #endif
52 int mbedtls_ssl_cache_get(void *data, mbedtls_ssl_session *session) {
53 int ret = 1;
54 #if defined(MBEDTLS_HAVE_TIME)
55 mbedtls_time_t t = mbedtls_time(NULL);
56 #endif
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)
62 return (1);
63 #endif
65 cur = cache->chain;
66 entry = NULL;
68 while (cur != NULL) {
69 entry = cur;
70 cur = cur->next;
72 #if defined(MBEDTLS_HAVE_TIME)
73 if (cache->timeout != 0 &&
74 (int)(t - entry->timestamp) > cache->timeout)
75 continue;
76 #endif
78 if (session->ciphersuite != entry->session.ciphersuite ||
79 session->compression != entry->session.compression ||
80 session->id_len != entry->session.id_len)
81 continue;
83 if (memcmp(session->id, entry->session.id,
84 entry->session.id_len) != 0)
85 continue;
87 ret = mbedtls_ssl_session_copy(session, &entry->session);
88 if (ret != 0) {
89 ret = 1;
90 goto exit;
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) {
105 ret = 1;
106 goto exit;
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;
114 ret = 1;
115 goto exit;
118 #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
120 ret = 0;
121 goto exit;
124 exit:
125 #if defined(MBEDTLS_THREADING_C)
126 if (mbedtls_mutex_unlock(&cache->mutex) != 0)
127 ret = 1;
128 #endif
130 return (ret);
133 int mbedtls_ssl_cache_set(void *data, const mbedtls_ssl_session *session) {
134 int ret = 1;
135 #if defined(MBEDTLS_HAVE_TIME)
136 mbedtls_time_t t = mbedtls_time(NULL), oldest = 0;
137 mbedtls_ssl_cache_entry *old = NULL;
138 #endif
139 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
140 mbedtls_ssl_cache_entry *cur, *prv;
141 int count = 0;
143 #if defined(MBEDTLS_THREADING_C)
144 if ((ret = mbedtls_mutex_lock(&cache->mutex)) != 0)
145 return (ret);
146 #endif
148 cur = cache->chain;
149 prv = NULL;
151 while (cur != NULL) {
152 count++;
154 #if defined(MBEDTLS_HAVE_TIME)
155 if (cache->timeout != 0 &&
156 (int)(t - cur->timestamp) > cache->timeout) {
157 cur->timestamp = t;
158 break; /* expired, reuse this slot, update timestamp */
160 #endif
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;
168 old = cur;
170 #endif
172 prv = cur;
173 cur = cur->next;
176 if (cur == NULL) {
177 #if defined(MBEDTLS_HAVE_TIME)
179 * Reuse oldest entry if max_entries reached
181 if (count >= cache->max_entries) {
182 if (old == NULL) {
183 ret = 1;
184 goto exit;
187 cur = old;
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) {
196 ret = 1;
197 goto exit;
200 cur = cache->chain;
201 cache->chain = cur->next;
202 cur->next = NULL;
203 prv->next = cur;
205 #endif /* MBEDTLS_HAVE_TIME */
206 else {
208 * max_entries not reached, create new entry
210 cur = mbedtls_calloc(1, sizeof(mbedtls_ssl_cache_entry));
211 if (cur == NULL) {
212 ret = 1;
213 goto exit;
216 if (prv == NULL)
217 cache->chain = cur;
218 else
219 prv->next = cur;
222 #if defined(MBEDTLS_HAVE_TIME)
223 cur->timestamp = t;
224 #endif
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);
244 if (ret != 0) {
245 ret = 1;
246 goto exit;
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) {
253 cur->peer_cert.p =
254 mbedtls_calloc(1, cur->session.peer_cert->raw.len);
255 if (cur->peer_cert.p == NULL) {
256 ret = 1;
257 goto exit;
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 */
271 ret = 0;
273 exit:
274 #if defined(MBEDTLS_THREADING_C)
275 if (mbedtls_mutex_unlock(&cache->mutex) != 0)
276 ret = 1;
277 #endif
279 return (ret);
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;
299 cur = cache->chain;
301 while (cur != NULL) {
302 prv = cur;
303 cur = cur->next;
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 */
312 mbedtls_free(prv);
315 #if defined(MBEDTLS_THREADING_C)
316 mbedtls_mutex_free(&cache->mutex);
317 #endif
318 cache->chain = NULL;
321 #endif /* MBEDTLS_SSL_CACHE_C */