blob: 6082074b251d7588f635f863046517fc6773ebb2 [file] [log] [blame]
Paul Bakker0a597072012-09-25 21:55:46 +00001/*
2 * SSL session cache implementation
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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.
Paul Bakker0a597072012-09-25 21:55:46 +000018 */
19/*
20 * These session callbacks use a simple chained list
21 * to store and retrieve the session information.
22 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker0a597072012-09-25 21:55:46 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_SSL_CACHE_C)
Paul Bakker0a597072012-09-25 21:55:46 +000027
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/platform.h"
Gilles Peskinefe4d93a2023-09-29 13:40:33 +020029#include "mbedtls/error.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020030
SimonBd5800b72016-04-26 07:43:27 +010031#include "mbedtls/ssl_cache.h"
Hanno Beckeraee87172019-02-06 14:53:19 +000032#include "mbedtls/ssl_internal.h"
SimonBd5800b72016-04-26 07:43:27 +010033
34#include <string.h>
35
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010036void mbedtls_ssl_cache_init(mbedtls_ssl_cache_context *cache)
Paul Bakker0a597072012-09-25 21:55:46 +000037{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010038 memset(cache, 0, sizeof(mbedtls_ssl_cache_context));
Paul Bakker0a597072012-09-25 21:55:46 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040 cache->timeout = MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT;
41 cache->max_entries = MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES;
Paul Bakkerc5598842013-09-28 15:01:27 +020042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010044 mbedtls_mutex_init(&cache->mutex);
Paul Bakkerc5598842013-09-28 15:01:27 +020045#endif
Paul Bakker0a597072012-09-25 21:55:46 +000046}
47
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010048int mbedtls_ssl_cache_get(void *data, mbedtls_ssl_session *session)
Paul Bakker0a597072012-09-25 21:55:46 +000049{
Gilles Peskinefe4d93a2023-09-29 13:40:33 +020050 int ret = MBEDTLS_ERR_SSL_CACHE_ENTRY_NOT_FOUND;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010052 mbedtls_time_t t = mbedtls_time(NULL);
Paul Bakkerfa9b1002013-07-03 15:31:03 +020053#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
55 mbedtls_ssl_cache_entry *cur, *entry;
Paul Bakker0a597072012-09-25 21:55:46 +000056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_THREADING_C)
Gilles Peskinefe4d93a2023-09-29 13:40:33 +020058 if ((ret = mbedtls_mutex_lock(&cache->mutex)) != 0) {
59 return ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010060 }
Paul Bakkerc5598842013-09-28 15:01:27 +020061#endif
62
Paul Bakker0a597072012-09-25 21:55:46 +000063 cur = cache->chain;
64 entry = NULL;
65
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010066 while (cur != NULL) {
Paul Bakker0a597072012-09-25 21:55:46 +000067 entry = cur;
68 cur = cur->next;
69
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010071 if (cache->timeout != 0 &&
72 (int) (t - entry->timestamp) > cache->timeout) {
Paul Bakker0a597072012-09-25 21:55:46 +000073 continue;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +020075#endif
Paul Bakker0a597072012-09-25 21:55:46 +000076
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010077 if (session->id_len != entry->session.id_len ||
78 memcmp(session->id, entry->session.id,
79 entry->session.id_len) != 0) {
Paul Bakker0a597072012-09-25 21:55:46 +000080 continue;
Hanno Becker83e36712021-04-15 08:19:40 +010081 }
Paul Bakker0a597072012-09-25 21:55:46 +000082
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010083 ret = mbedtls_ssl_session_copy(session, &entry->session);
84 if (ret != 0) {
Hanno Beckeraee87172019-02-06 14:53:19 +000085 goto exit;
86 }
Manuel Pégourié-Gonnard38d1eba2013-08-23 10:44:29 +020087
Hanno Beckera887d1a2019-02-06 15:57:49 +000088#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010089 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkere81beda2013-03-06 17:40:46 +010090 /*
91 * Restore peer certificate (without rest of the original chain)
92 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010093 if (entry->peer_cert.p != NULL) {
Hanno Beckeraee87172019-02-06 14:53:19 +000094 /* `session->peer_cert` is NULL after the call to
95 * mbedtls_ssl_session_copy(), because cache entries
96 * have the `peer_cert` field set to NULL. */
97
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010098 if ((session->peer_cert = mbedtls_calloc(1,
99 sizeof(mbedtls_x509_crt))) == NULL) {
Gilles Peskinefe4d93a2023-09-29 13:40:33 +0200100 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakkerc5598842013-09-28 15:01:27 +0200101 goto exit;
102 }
Paul Bakkere81beda2013-03-06 17:40:46 +0100103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104 mbedtls_x509_crt_init(session->peer_cert);
Gilles Peskinefe4d93a2023-09-29 13:40:33 +0200105 if ((ret = mbedtls_x509_crt_parse(session->peer_cert, entry->peer_cert.p,
106 entry->peer_cert.len)) != 0) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100107 mbedtls_free(session->peer_cert);
Paul Bakkere81beda2013-03-06 17:40:46 +0100108 session->peer_cert = NULL;
Paul Bakkerc5598842013-09-28 15:01:27 +0200109 goto exit;
Paul Bakkere81beda2013-03-06 17:40:46 +0100110 }
111 }
Hanno Beckera887d1a2019-02-06 15:57:49 +0000112#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkere81beda2013-03-06 17:40:46 +0100113
Paul Bakkerc5598842013-09-28 15:01:27 +0200114 ret = 0;
115 goto exit;
Paul Bakker0a597072012-09-25 21:55:46 +0000116 }
117
Paul Bakkerc5598842013-09-28 15:01:27 +0200118exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100120 if (mbedtls_mutex_unlock(&cache->mutex) != 0) {
Gilles Peskinefe4d93a2023-09-29 13:40:33 +0200121 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122 }
Paul Bakkerc5598842013-09-28 15:01:27 +0200123#endif
124
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100125 return ret;
Paul Bakker0a597072012-09-25 21:55:46 +0000126}
127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100128int mbedtls_ssl_cache_set(void *data, const mbedtls_ssl_session *session)
Paul Bakker0a597072012-09-25 21:55:46 +0000129{
Gilles Peskinefe4d93a2023-09-29 13:40:33 +0200130 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132 mbedtls_time_t t = mbedtls_time(NULL), oldest = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_ssl_cache_entry *old = NULL;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200134#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data;
136 mbedtls_ssl_cache_entry *cur, *prv;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000137 int count = 0;
Paul Bakker0a597072012-09-25 21:55:46 +0000138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100140 if ((ret = mbedtls_mutex_lock(&cache->mutex)) != 0) {
141 return ret;
142 }
Paul Bakkerc5598842013-09-28 15:01:27 +0200143#endif
144
Paul Bakker0a597072012-09-25 21:55:46 +0000145 cur = cache->chain;
146 prv = NULL;
147
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100148 while (cur != NULL) {
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000149 count++;
150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100152 if (cache->timeout != 0 &&
153 (int) (t - cur->timestamp) > cache->timeout) {
Paul Bakker0a597072012-09-25 21:55:46 +0000154 cur->timestamp = t;
155 break; /* expired, reuse this slot, update timestamp */
156 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200157#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000158
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100159 if (memcmp(session->id, cur->session.id, cur->session.id_len) == 0) {
Paul Bakker0a597072012-09-25 21:55:46 +0000160 break; /* client reconnected, keep timestamp for session id */
161
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100162 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100164 if (oldest == 0 || cur->timestamp < oldest) {
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000165 oldest = cur->timestamp;
166 old = cur;
167 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200168#endif
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000169
Paul Bakker0a597072012-09-25 21:55:46 +0000170 prv = cur;
171 cur = cur->next;
172 }
173
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100174 if (cur == NULL) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000176 /*
177 * Reuse oldest entry if max_entries reached
178 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179 if (count >= cache->max_entries) {
180 if (old == NULL) {
Gilles Peskinefe4d93a2023-09-29 13:40:33 +0200181 /* This should only happen on an ill-configured cache
182 * with max_entries == 0. */
183 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100184 goto exit;
185 }
186
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000187 cur = old;
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000188 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#else /* MBEDTLS_HAVE_TIME */
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200190 /*
191 * Reuse first entry in chain if max_entries reached,
192 * but move to last place
193 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194 if (count >= cache->max_entries) {
195 if (cache->chain == NULL) {
Gilles Peskinefe4d93a2023-09-29 13:40:33 +0200196 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Paul Bakkerc5598842013-09-28 15:01:27 +0200197 goto exit;
198 }
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200199
200 cur = cache->chain;
201 cache->chain = cur->next;
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100202 cur->next = NULL;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200203 prv->next = cur;
204 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205#endif /* MBEDTLS_HAVE_TIME */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206 else {
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100207 /*
208 * max_entries not reached, create new entry
209 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210 cur = mbedtls_calloc(1, sizeof(mbedtls_ssl_cache_entry));
211 if (cur == NULL) {
Gilles Peskinefe4d93a2023-09-29 13:40:33 +0200212 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakkerc5598842013-09-28 15:01:27 +0200213 goto exit;
214 }
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216 if (prv == NULL) {
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000217 cache->chain = cur;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100218 } else {
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000219 prv->next = cur;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220 }
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000221 }
Paul Bakker0a597072012-09-25 21:55:46 +0000222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000224 cur->timestamp = t;
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200225#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000226 }
227
Hanno Beckera887d1a2019-02-06 15:57:49 +0000228#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
229 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkere81beda2013-03-06 17:40:46 +0100230 /*
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100231 * If we're reusing an entry, free its certificate first
232 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 if (cur->peer_cert.p != NULL) {
234 mbedtls_free(cur->peer_cert.p);
235 memset(&cur->peer_cert, 0, sizeof(mbedtls_x509_buf));
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100236 }
Hanno Beckera887d1a2019-02-06 15:57:49 +0000237#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnard84c30c72014-02-26 17:38:55 +0100238
Hanno Beckeraee87172019-02-06 14:53:19 +0000239 /* Copy the entire session; this temporarily makes a copy of the
240 * X.509 CRT structure even though we only want to store the raw CRT.
241 * This inefficiency will go away as soon as we implement on-demand
242 * parsing of CRTs, in which case there's no need for the `peer_cert`
243 * field anymore in the first place, and we're done after this call. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244 ret = mbedtls_ssl_session_copy(&cur->session, session);
245 if (ret != 0) {
Hanno Beckeraee87172019-02-06 14:53:19 +0000246 goto exit;
247 }
248
Hanno Beckera887d1a2019-02-06 15:57:49 +0000249#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
250 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckeraee87172019-02-06 14:53:19 +0000251 /* If present, free the X.509 structure and only store the raw CRT data. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100252 if (cur->session.peer_cert != NULL) {
Hanno Beckeraee87172019-02-06 14:53:19 +0000253 cur->peer_cert.p =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 mbedtls_calloc(1, cur->session.peer_cert->raw.len);
255 if (cur->peer_cert.p == NULL) {
Gilles Peskinefe4d93a2023-09-29 13:40:33 +0200256 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakkerc5598842013-09-28 15:01:27 +0200257 goto exit;
258 }
Paul Bakkere81beda2013-03-06 17:40:46 +0100259
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100260 memcpy(cur->peer_cert.p,
261 cur->session.peer_cert->raw.p,
262 cur->session.peer_cert->raw.len);
Paul Bakkere81beda2013-03-06 17:40:46 +0100263 cur->peer_cert.len = session->peer_cert->raw.len;
264
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100265 mbedtls_x509_crt_free(cur->session.peer_cert);
266 mbedtls_free(cur->session.peer_cert);
Paul Bakkere81beda2013-03-06 17:40:46 +0100267 cur->session.peer_cert = NULL;
268 }
Hanno Beckera887d1a2019-02-06 15:57:49 +0000269#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakker0a597072012-09-25 21:55:46 +0000270
Paul Bakkerc5598842013-09-28 15:01:27 +0200271 ret = 0;
272
273exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100275 if (mbedtls_mutex_unlock(&cache->mutex) != 0) {
Gilles Peskinefe4d93a2023-09-29 13:40:33 +0200276 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 }
Paul Bakkerc5598842013-09-28 15:01:27 +0200278#endif
279
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280 return ret;
Paul Bakker0a597072012-09-25 21:55:46 +0000281}
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100284void mbedtls_ssl_cache_set_timeout(mbedtls_ssl_cache_context *cache, int timeout)
Paul Bakker0a597072012-09-25 21:55:46 +0000285{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100286 if (timeout < 0) {
287 timeout = 0;
288 }
Paul Bakker0a597072012-09-25 21:55:46 +0000289
290 cache->timeout = timeout;
291}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000293
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100294void mbedtls_ssl_cache_set_max_entries(mbedtls_ssl_cache_context *cache, int max)
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000295{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100296 if (max < 0) {
297 max = 0;
298 }
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000299
300 cache->max_entries = max;
301}
302
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100303void mbedtls_ssl_cache_free(mbedtls_ssl_cache_context *cache)
Paul Bakker0a597072012-09-25 21:55:46 +0000304{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 mbedtls_ssl_cache_entry *cur, *prv;
Paul Bakker0a597072012-09-25 21:55:46 +0000306
307 cur = cache->chain;
308
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100309 while (cur != NULL) {
Paul Bakker0a597072012-09-25 21:55:46 +0000310 prv = cur;
311 cur = cur->next;
312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100313 mbedtls_ssl_session_free(&prv->session);
Paul Bakkere81beda2013-03-06 17:40:46 +0100314
Hanno Beckera887d1a2019-02-06 15:57:49 +0000315#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100316 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
317 mbedtls_free(prv->peer_cert.p);
Hanno Beckera887d1a2019-02-06 15:57:49 +0000318#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkere81beda2013-03-06 17:40:46 +0100319
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100320 mbedtls_free(prv);
Paul Bakker0a597072012-09-25 21:55:46 +0000321 }
Paul Bakkerc5598842013-09-28 15:01:27 +0200322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323#if defined(MBEDTLS_THREADING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100324 mbedtls_mutex_free(&cache->mutex);
Paul Bakkerc5598842013-09-28 15:01:27 +0200325#endif
Ron Eldor22360822017-10-29 17:53:52 +0200326 cache->chain = NULL;
Paul Bakker0a597072012-09-25 21:55:46 +0000327}
328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329#endif /* MBEDTLS_SSL_CACHE_C */