Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SSL session cache implementation |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 6 | */ |
| 7 | /* |
| 8 | * These session callbacks use a simple chained list |
| 9 | * to store and retrieve the session information. |
| 10 | */ |
| 11 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 12 | #include "common.h" |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 13 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 14 | #if defined(MBEDTLS_SSL_CACHE_C) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 15 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 16 | #include "mbedtls/platform.h" |
Gilles Peskine | fe4d93a | 2023-09-29 13:40:33 +0200 | [diff] [blame] | 17 | #include "mbedtls/error.h" |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 18 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 19 | #include "mbedtls/ssl_cache.h" |
Hanno Becker | aee8717 | 2019-02-06 14:53:19 +0000 | [diff] [blame] | 20 | #include "mbedtls/ssl_internal.h" |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 21 | |
| 22 | #include <string.h> |
| 23 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 24 | void mbedtls_ssl_cache_init(mbedtls_ssl_cache_context *cache) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 25 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 26 | memset(cache, 0, sizeof(mbedtls_ssl_cache_context)); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 27 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | cache->timeout = MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT; |
| 29 | cache->max_entries = MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES; |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 30 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 31 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 32 | mbedtls_mutex_init(&cache->mutex); |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 33 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 36 | int mbedtls_ssl_cache_get(void *data, mbedtls_ssl_session *session) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 37 | { |
Gilles Peskine | fe4d93a | 2023-09-29 13:40:33 +0200 | [diff] [blame] | 38 | int ret = MBEDTLS_ERR_SSL_CACHE_ENTRY_NOT_FOUND; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 39 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 40 | mbedtls_time_t t = mbedtls_time(NULL); |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 41 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 42 | mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data; |
| 43 | mbedtls_ssl_cache_entry *cur, *entry; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 44 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 45 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | fe4d93a | 2023-09-29 13:40:33 +0200 | [diff] [blame] | 46 | if ((ret = mbedtls_mutex_lock(&cache->mutex)) != 0) { |
| 47 | return ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 48 | } |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 49 | #endif |
| 50 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 51 | cur = cache->chain; |
| 52 | entry = NULL; |
| 53 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 54 | while (cur != NULL) { |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 55 | entry = cur; |
| 56 | cur = cur->next; |
| 57 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 58 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 59 | if (cache->timeout != 0 && |
| 60 | (int) (t - entry->timestamp) > cache->timeout) { |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 61 | continue; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 62 | } |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 63 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 64 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 65 | if (session->id_len != entry->session.id_len || |
| 66 | memcmp(session->id, entry->session.id, |
| 67 | entry->session.id_len) != 0) { |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 68 | continue; |
Hanno Becker | 83e3671 | 2021-04-15 08:19:40 +0100 | [diff] [blame] | 69 | } |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 70 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 71 | ret = mbedtls_ssl_session_copy(session, &entry->session); |
| 72 | if (ret != 0) { |
Hanno Becker | aee8717 | 2019-02-06 14:53:19 +0000 | [diff] [blame] | 73 | goto exit; |
| 74 | } |
Manuel Pégourié-Gonnard | 38d1eba | 2013-08-23 10:44:29 +0200 | [diff] [blame] | 75 | |
Hanno Becker | a887d1a | 2019-02-06 15:57:49 +0000 | [diff] [blame] | 76 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 77 | defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 78 | /* |
| 79 | * Restore peer certificate (without rest of the original chain) |
| 80 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 81 | if (entry->peer_cert.p != NULL) { |
Hanno Becker | aee8717 | 2019-02-06 14:53:19 +0000 | [diff] [blame] | 82 | /* `session->peer_cert` is NULL after the call to |
| 83 | * mbedtls_ssl_session_copy(), because cache entries |
| 84 | * have the `peer_cert` field set to NULL. */ |
| 85 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 86 | if ((session->peer_cert = mbedtls_calloc(1, |
| 87 | sizeof(mbedtls_x509_crt))) == NULL) { |
Gilles Peskine | fe4d93a | 2023-09-29 13:40:33 +0200 | [diff] [blame] | 88 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 89 | goto exit; |
| 90 | } |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 91 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 92 | mbedtls_x509_crt_init(session->peer_cert); |
Gilles Peskine | fe4d93a | 2023-09-29 13:40:33 +0200 | [diff] [blame] | 93 | if ((ret = mbedtls_x509_crt_parse(session->peer_cert, entry->peer_cert.p, |
| 94 | entry->peer_cert.len)) != 0) { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 95 | mbedtls_free(session->peer_cert); |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 96 | session->peer_cert = NULL; |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 97 | goto exit; |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 98 | } |
| 99 | } |
Hanno Becker | a887d1a | 2019-02-06 15:57:49 +0000 | [diff] [blame] | 100 | #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 101 | |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 102 | ret = 0; |
| 103 | goto exit; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 106 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 107 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 108 | if (mbedtls_mutex_unlock(&cache->mutex) != 0) { |
Gilles Peskine | fe4d93a | 2023-09-29 13:40:33 +0200 | [diff] [blame] | 109 | ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 110 | } |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 111 | #endif |
| 112 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 113 | return ret; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 116 | int mbedtls_ssl_cache_set(void *data, const mbedtls_ssl_session *session) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 117 | { |
Gilles Peskine | fe4d93a | 2023-09-29 13:40:33 +0200 | [diff] [blame] | 118 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 119 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 120 | mbedtls_time_t t = mbedtls_time(NULL), oldest = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 121 | mbedtls_ssl_cache_entry *old = NULL; |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 122 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 123 | mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data; |
| 124 | mbedtls_ssl_cache_entry *cur, *prv; |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 125 | int count = 0; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 126 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 127 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 128 | if ((ret = mbedtls_mutex_lock(&cache->mutex)) != 0) { |
| 129 | return ret; |
| 130 | } |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 131 | #endif |
| 132 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 133 | cur = cache->chain; |
| 134 | prv = NULL; |
| 135 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 136 | while (cur != NULL) { |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 137 | count++; |
| 138 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 139 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 140 | if (cache->timeout != 0 && |
| 141 | (int) (t - cur->timestamp) > cache->timeout) { |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 142 | cur->timestamp = t; |
| 143 | break; /* expired, reuse this slot, update timestamp */ |
| 144 | } |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 145 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 146 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 147 | if (memcmp(session->id, cur->session.id, cur->session.id_len) == 0) { |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 148 | break; /* client reconnected, keep timestamp for session id */ |
| 149 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 150 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 151 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 152 | if (oldest == 0 || cur->timestamp < oldest) { |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 153 | oldest = cur->timestamp; |
| 154 | old = cur; |
| 155 | } |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 156 | #endif |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 157 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 158 | prv = cur; |
| 159 | cur = cur->next; |
| 160 | } |
| 161 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 162 | if (cur == NULL) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 163 | #if defined(MBEDTLS_HAVE_TIME) |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 164 | /* |
| 165 | * Reuse oldest entry if max_entries reached |
| 166 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 167 | if (count >= cache->max_entries) { |
| 168 | if (old == NULL) { |
Gilles Peskine | fe4d93a | 2023-09-29 13:40:33 +0200 | [diff] [blame] | 169 | /* This should only happen on an ill-configured cache |
| 170 | * with max_entries == 0. */ |
| 171 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 274a12e | 2014-02-20 21:32:08 +0100 | [diff] [blame] | 172 | goto exit; |
| 173 | } |
| 174 | |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 175 | cur = old; |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 176 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 177 | #else /* MBEDTLS_HAVE_TIME */ |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 178 | /* |
| 179 | * Reuse first entry in chain if max_entries reached, |
| 180 | * but move to last place |
| 181 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 182 | if (count >= cache->max_entries) { |
| 183 | if (cache->chain == NULL) { |
Gilles Peskine | fe4d93a | 2023-09-29 13:40:33 +0200 | [diff] [blame] | 184 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 185 | goto exit; |
| 186 | } |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 187 | |
| 188 | cur = cache->chain; |
| 189 | cache->chain = cur->next; |
Manuel Pégourié-Gonnard | 84c30c7 | 2014-02-26 17:38:55 +0100 | [diff] [blame] | 190 | cur->next = NULL; |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 191 | prv->next = cur; |
| 192 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 193 | #endif /* MBEDTLS_HAVE_TIME */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 194 | else { |
Manuel Pégourié-Gonnard | 274a12e | 2014-02-20 21:32:08 +0100 | [diff] [blame] | 195 | /* |
| 196 | * max_entries not reached, create new entry |
| 197 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 198 | cur = mbedtls_calloc(1, sizeof(mbedtls_ssl_cache_entry)); |
| 199 | if (cur == NULL) { |
Gilles Peskine | fe4d93a | 2023-09-29 13:40:33 +0200 | [diff] [blame] | 200 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 201 | goto exit; |
| 202 | } |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 203 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 204 | if (prv == NULL) { |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 205 | cache->chain = cur; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 206 | } else { |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 207 | prv->next = cur; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 208 | } |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 209 | } |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 210 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 211 | #if defined(MBEDTLS_HAVE_TIME) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 212 | cur->timestamp = t; |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 213 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Hanno Becker | a887d1a | 2019-02-06 15:57:49 +0000 | [diff] [blame] | 216 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 217 | defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 218 | /* |
Manuel Pégourié-Gonnard | 84c30c7 | 2014-02-26 17:38:55 +0100 | [diff] [blame] | 219 | * If we're reusing an entry, free its certificate first |
| 220 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 221 | if (cur->peer_cert.p != NULL) { |
| 222 | mbedtls_free(cur->peer_cert.p); |
| 223 | memset(&cur->peer_cert, 0, sizeof(mbedtls_x509_buf)); |
Manuel Pégourié-Gonnard | 84c30c7 | 2014-02-26 17:38:55 +0100 | [diff] [blame] | 224 | } |
Hanno Becker | a887d1a | 2019-02-06 15:57:49 +0000 | [diff] [blame] | 225 | #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | 84c30c7 | 2014-02-26 17:38:55 +0100 | [diff] [blame] | 226 | |
Hanno Becker | aee8717 | 2019-02-06 14:53:19 +0000 | [diff] [blame] | 227 | /* Copy the entire session; this temporarily makes a copy of the |
| 228 | * X.509 CRT structure even though we only want to store the raw CRT. |
| 229 | * This inefficiency will go away as soon as we implement on-demand |
| 230 | * parsing of CRTs, in which case there's no need for the `peer_cert` |
| 231 | * field anymore in the first place, and we're done after this call. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 232 | ret = mbedtls_ssl_session_copy(&cur->session, session); |
| 233 | if (ret != 0) { |
Hanno Becker | aee8717 | 2019-02-06 14:53:19 +0000 | [diff] [blame] | 234 | goto exit; |
| 235 | } |
| 236 | |
Hanno Becker | a887d1a | 2019-02-06 15:57:49 +0000 | [diff] [blame] | 237 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 238 | defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | aee8717 | 2019-02-06 14:53:19 +0000 | [diff] [blame] | 239 | /* If present, free the X.509 structure and only store the raw CRT data. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 240 | if (cur->session.peer_cert != NULL) { |
Hanno Becker | aee8717 | 2019-02-06 14:53:19 +0000 | [diff] [blame] | 241 | cur->peer_cert.p = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 242 | mbedtls_calloc(1, cur->session.peer_cert->raw.len); |
| 243 | if (cur->peer_cert.p == NULL) { |
Gilles Peskine | fe4d93a | 2023-09-29 13:40:33 +0200 | [diff] [blame] | 244 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 245 | goto exit; |
| 246 | } |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 247 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 248 | memcpy(cur->peer_cert.p, |
| 249 | cur->session.peer_cert->raw.p, |
| 250 | cur->session.peer_cert->raw.len); |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 251 | cur->peer_cert.len = session->peer_cert->raw.len; |
| 252 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 253 | mbedtls_x509_crt_free(cur->session.peer_cert); |
| 254 | mbedtls_free(cur->session.peer_cert); |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 255 | cur->session.peer_cert = NULL; |
| 256 | } |
Hanno Becker | a887d1a | 2019-02-06 15:57:49 +0000 | [diff] [blame] | 257 | #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 258 | |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 259 | ret = 0; |
| 260 | |
| 261 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 262 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 263 | if (mbedtls_mutex_unlock(&cache->mutex) != 0) { |
Gilles Peskine | fe4d93a | 2023-09-29 13:40:33 +0200 | [diff] [blame] | 264 | ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 265 | } |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 266 | #endif |
| 267 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 268 | return ret; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 271 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 272 | void mbedtls_ssl_cache_set_timeout(mbedtls_ssl_cache_context *cache, int timeout) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 273 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 274 | if (timeout < 0) { |
| 275 | timeout = 0; |
| 276 | } |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 277 | |
| 278 | cache->timeout = timeout; |
| 279 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 280 | #endif /* MBEDTLS_HAVE_TIME */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 281 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 282 | void mbedtls_ssl_cache_set_max_entries(mbedtls_ssl_cache_context *cache, int max) |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 283 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 284 | if (max < 0) { |
| 285 | max = 0; |
| 286 | } |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 287 | |
| 288 | cache->max_entries = max; |
| 289 | } |
| 290 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 291 | void mbedtls_ssl_cache_free(mbedtls_ssl_cache_context *cache) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 292 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 293 | mbedtls_ssl_cache_entry *cur, *prv; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 294 | |
| 295 | cur = cache->chain; |
| 296 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 297 | while (cur != NULL) { |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 298 | prv = cur; |
| 299 | cur = cur->next; |
| 300 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 301 | mbedtls_ssl_session_free(&prv->session); |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 302 | |
Hanno Becker | a887d1a | 2019-02-06 15:57:49 +0000 | [diff] [blame] | 303 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 304 | defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 305 | mbedtls_free(prv->peer_cert.p); |
Hanno Becker | a887d1a | 2019-02-06 15:57:49 +0000 | [diff] [blame] | 306 | #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 307 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 308 | mbedtls_free(prv); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 309 | } |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 310 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 311 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 312 | mbedtls_mutex_free(&cache->mutex); |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 313 | #endif |
Ron Eldor | 2236082 | 2017-10-29 17:53:52 +0200 | [diff] [blame] | 314 | cache->chain = NULL; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 317 | #endif /* MBEDTLS_SSL_CACHE_C */ |