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 |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * 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 Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 18 | */ |
| 19 | /* |
| 20 | * These session callbacks use a simple chained list |
| 21 | * to store and retrieve the session information. |
| 22 | */ |
| 23 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 24 | #include "common.h" |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 25 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #if defined(MBEDTLS_SSL_CACHE_C) |
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 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 29 | #include "mbedtls/platform.h" |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 30 | #else |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 31 | #include <stdlib.h> |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 32 | #define mbedtls_calloc calloc |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 33 | #define mbedtls_free free |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 34 | #endif |
| 35 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 36 | #include "mbedtls/ssl_cache.h" |
Chris Jones | 84a773f | 2021-03-05 18:38:47 +0000 | [diff] [blame] | 37 | #include "ssl_misc.h" |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 38 | |
| 39 | #include <string.h> |
| 40 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 41 | void mbedtls_ssl_cache_init( mbedtls_ssl_cache_context *cache ) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 42 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 43 | memset( cache, 0, sizeof( mbedtls_ssl_cache_context ) ); |
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 | cache->timeout = MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT; |
| 46 | cache->max_entries = MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES; |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 47 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 48 | #if defined(MBEDTLS_THREADING_C) |
| 49 | mbedtls_mutex_init( &cache->mutex ); |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 50 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame^] | 53 | static int ssl_cache_find_entry( mbedtls_ssl_cache_context *cache, |
| 54 | unsigned char const *session_id, |
| 55 | size_t session_id_len, |
| 56 | mbedtls_ssl_cache_entry **dst ) |
| 57 | { |
| 58 | int ret = 1; |
| 59 | #if defined(MBEDTLS_HAVE_TIME) |
| 60 | mbedtls_time_t t = mbedtls_time( NULL ); |
| 61 | #endif |
| 62 | mbedtls_ssl_cache_entry *cur; |
| 63 | |
| 64 | for( cur = cache->chain; cur != NULL; cur = cur->next ) |
| 65 | { |
| 66 | #if defined(MBEDTLS_HAVE_TIME) |
| 67 | if( cache->timeout != 0 && |
| 68 | (int) ( t - cur->timestamp ) > cache->timeout ) |
| 69 | continue; |
| 70 | #endif |
| 71 | |
| 72 | if( session_id_len != cur->session.id_len || |
| 73 | memcmp( session_id, cur->session.id, |
| 74 | cur->session.id_len ) != 0 ) |
| 75 | { |
| 76 | continue; |
| 77 | } |
| 78 | |
| 79 | break; |
| 80 | } |
| 81 | |
| 82 | if( cur != NULL ) |
| 83 | { |
| 84 | *dst = cur; |
| 85 | ret = 0; |
| 86 | } |
| 87 | |
| 88 | return( ret ); |
| 89 | } |
| 90 | |
| 91 | |
Hanno Becker | ccdaf6e | 2021-04-15 09:26:17 +0100 | [diff] [blame] | 92 | int mbedtls_ssl_cache_get( void *data, |
| 93 | unsigned char const *session_id, |
| 94 | size_t session_id_len, |
| 95 | mbedtls_ssl_session *session ) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 96 | { |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 97 | int ret = 1; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 98 | mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data; |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame^] | 99 | mbedtls_ssl_cache_entry *entry; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 100 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 101 | #if defined(MBEDTLS_THREADING_C) |
| 102 | if( mbedtls_mutex_lock( &cache->mutex ) != 0 ) |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 103 | return( 1 ); |
| 104 | #endif |
| 105 | |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame^] | 106 | ret = ssl_cache_find_entry( cache, session_id, session_id_len, &entry ); |
| 107 | if( ret != 0 ) |
| 108 | goto exit; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 109 | |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame^] | 110 | ret = mbedtls_ssl_session_copy( session, &entry->session ); |
| 111 | if( ret != 0 ) |
| 112 | goto exit; |
| 113 | |
| 114 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 115 | defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 116 | /* |
| 117 | * Restore peer certificate (without rest of the original chain) |
| 118 | */ |
| 119 | if( entry->peer_cert.p != NULL ) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 120 | { |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame^] | 121 | /* `session->peer_cert` is NULL after the call to |
| 122 | * mbedtls_ssl_session_copy(), because cache entries |
| 123 | * have the `peer_cert` field set to NULL. */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 124 | |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame^] | 125 | if( ( session->peer_cert = mbedtls_calloc( 1, |
| 126 | sizeof(mbedtls_x509_crt) ) ) == NULL ) |
Hanno Becker | aee8717 | 2019-02-06 14:53:19 +0000 | [diff] [blame] | 127 | { |
| 128 | ret = 1; |
| 129 | goto exit; |
| 130 | } |
Manuel Pégourié-Gonnard | 38d1eba | 2013-08-23 10:44:29 +0200 | [diff] [blame] | 131 | |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame^] | 132 | mbedtls_x509_crt_init( session->peer_cert ); |
| 133 | if( mbedtls_x509_crt_parse( session->peer_cert, entry->peer_cert.p, |
| 134 | entry->peer_cert.len ) != 0 ) |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 135 | { |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame^] | 136 | mbedtls_free( session->peer_cert ); |
| 137 | session->peer_cert = NULL; |
| 138 | ret = 1; |
| 139 | goto exit; |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 140 | } |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame^] | 141 | } |
Hanno Becker | a887d1a | 2019-02-06 15:57:49 +0000 | [diff] [blame] | 142 | #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 143 | |
Hanno Becker | f938c43 | 2021-04-15 10:17:53 +0100 | [diff] [blame^] | 144 | ret = 0; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 145 | |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 146 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 147 | #if defined(MBEDTLS_THREADING_C) |
| 148 | if( mbedtls_mutex_unlock( &cache->mutex ) != 0 ) |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 149 | ret = 1; |
| 150 | #endif |
| 151 | |
| 152 | return( ret ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Hanno Becker | 02a68eb | 2021-04-15 09:57:17 +0100 | [diff] [blame] | 155 | static int ssl_cache_find_fresh_entry( mbedtls_ssl_cache_context *cache, |
| 156 | unsigned char const *session_id, |
| 157 | size_t session_id_len, |
| 158 | mbedtls_ssl_cache_entry **dst ) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 159 | { |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 160 | int ret = 1; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 161 | #if defined(MBEDTLS_HAVE_TIME) |
Simon Butcher | a55e084 | 2017-07-28 23:46:43 +0100 | [diff] [blame] | 162 | mbedtls_time_t t = mbedtls_time( NULL ), oldest = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 163 | mbedtls_ssl_cache_entry *old = NULL; |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 164 | #endif |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 165 | int count = 0; |
Hanno Becker | 02a68eb | 2021-04-15 09:57:17 +0100 | [diff] [blame] | 166 | mbedtls_ssl_cache_entry *cur, *prv; |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 167 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 168 | cur = cache->chain; |
| 169 | prv = NULL; |
| 170 | |
| 171 | while( cur != NULL ) |
| 172 | { |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 173 | count++; |
| 174 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 175 | #if defined(MBEDTLS_HAVE_TIME) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 176 | if( cache->timeout != 0 && |
| 177 | (int) ( t - cur->timestamp ) > cache->timeout ) |
| 178 | { |
| 179 | cur->timestamp = t; |
| 180 | break; /* expired, reuse this slot, update timestamp */ |
| 181 | } |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 182 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 183 | |
Hanno Becker | ccdaf6e | 2021-04-15 09:26:17 +0100 | [diff] [blame] | 184 | if( session_id_len == cur->session.id_len && |
| 185 | memcmp( session_id, cur->session.id, cur->session.id_len ) == 0 ) |
| 186 | { |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 187 | break; /* client reconnected, keep timestamp for session id */ |
Hanno Becker | ccdaf6e | 2021-04-15 09:26:17 +0100 | [diff] [blame] | 188 | } |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 189 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 190 | #if defined(MBEDTLS_HAVE_TIME) |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 191 | if( oldest == 0 || cur->timestamp < oldest ) |
| 192 | { |
| 193 | oldest = cur->timestamp; |
| 194 | old = cur; |
| 195 | } |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 196 | #endif |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 197 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 198 | prv = cur; |
| 199 | cur = cur->next; |
| 200 | } |
| 201 | |
| 202 | if( cur == NULL ) |
| 203 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 204 | #if defined(MBEDTLS_HAVE_TIME) |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 205 | /* |
| 206 | * Reuse oldest entry if max_entries reached |
| 207 | */ |
Manuel Pégourié-Gonnard | 274a12e | 2014-02-20 21:32:08 +0100 | [diff] [blame] | 208 | if( count >= cache->max_entries ) |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 209 | { |
Manuel Pégourié-Gonnard | 274a12e | 2014-02-20 21:32:08 +0100 | [diff] [blame] | 210 | if( old == NULL ) |
| 211 | { |
| 212 | ret = 1; |
| 213 | goto exit; |
| 214 | } |
| 215 | |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 216 | cur = old; |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 217 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 218 | #else /* MBEDTLS_HAVE_TIME */ |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 219 | /* |
| 220 | * Reuse first entry in chain if max_entries reached, |
| 221 | * but move to last place |
| 222 | */ |
| 223 | if( count >= cache->max_entries ) |
| 224 | { |
| 225 | if( cache->chain == NULL ) |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 226 | { |
| 227 | ret = 1; |
| 228 | goto exit; |
| 229 | } |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 230 | |
| 231 | cur = cache->chain; |
| 232 | cache->chain = cur->next; |
Manuel Pégourié-Gonnard | 84c30c7 | 2014-02-26 17:38:55 +0100 | [diff] [blame] | 233 | cur->next = NULL; |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 234 | prv->next = cur; |
| 235 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 236 | #endif /* MBEDTLS_HAVE_TIME */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 237 | else |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 238 | { |
Manuel Pégourié-Gonnard | 274a12e | 2014-02-20 21:32:08 +0100 | [diff] [blame] | 239 | /* |
| 240 | * max_entries not reached, create new entry |
| 241 | */ |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 242 | cur = mbedtls_calloc( 1, sizeof(mbedtls_ssl_cache_entry) ); |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 243 | if( cur == NULL ) |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 244 | { |
| 245 | ret = 1; |
| 246 | goto exit; |
| 247 | } |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 248 | |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 249 | if( prv == NULL ) |
| 250 | cache->chain = cur; |
| 251 | else |
| 252 | prv->next = cur; |
| 253 | } |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 254 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 255 | #if defined(MBEDTLS_HAVE_TIME) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 256 | cur->timestamp = t; |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 257 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Hanno Becker | 02a68eb | 2021-04-15 09:57:17 +0100 | [diff] [blame] | 260 | if( cur != NULL ) |
Manuel Pégourié-Gonnard | 84c30c7 | 2014-02-26 17:38:55 +0100 | [diff] [blame] | 261 | { |
Hanno Becker | 02a68eb | 2021-04-15 09:57:17 +0100 | [diff] [blame] | 262 | *dst = cur; |
| 263 | |
| 264 | /* |
| 265 | * If we're reusing an entry, free its certificate first |
| 266 | */ |
| 267 | if( cur->peer_cert.p != NULL ) |
| 268 | { |
| 269 | mbedtls_free( cur->peer_cert.p ); |
| 270 | memset( &cur->peer_cert, 0, sizeof(mbedtls_x509_buf) ); |
| 271 | } |
| 272 | |
| 273 | ret = 0; |
Manuel Pégourié-Gonnard | 84c30c7 | 2014-02-26 17:38:55 +0100 | [diff] [blame] | 274 | } |
Hanno Becker | 02a68eb | 2021-04-15 09:57:17 +0100 | [diff] [blame] | 275 | |
| 276 | exit: |
| 277 | |
| 278 | return( ret ); |
| 279 | } |
| 280 | |
| 281 | int mbedtls_ssl_cache_set( void *data, |
| 282 | unsigned char const *session_id, |
| 283 | size_t session_id_len, |
| 284 | const mbedtls_ssl_session *session ) |
| 285 | { |
| 286 | int ret = 1; |
| 287 | mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data; |
| 288 | mbedtls_ssl_cache_entry *cur; |
| 289 | |
| 290 | #if defined(MBEDTLS_THREADING_C) |
| 291 | if( ( ret = mbedtls_mutex_lock( &cache->mutex ) ) != 0 ) |
| 292 | return( ret ); |
| 293 | #endif |
| 294 | |
| 295 | ret = ssl_cache_find_fresh_entry( cache, |
| 296 | session_id, session_id_len, |
| 297 | &cur ); |
| 298 | if( ret != 0 ) |
| 299 | goto exit; |
Manuel Pégourié-Gonnard | 84c30c7 | 2014-02-26 17:38:55 +0100 | [diff] [blame] | 300 | |
Hanno Becker | aee8717 | 2019-02-06 14:53:19 +0000 | [diff] [blame] | 301 | /* Copy the entire session; this temporarily makes a copy of the |
| 302 | * X.509 CRT structure even though we only want to store the raw CRT. |
| 303 | * This inefficiency will go away as soon as we implement on-demand |
| 304 | * parsing of CRTs, in which case there's no need for the `peer_cert` |
| 305 | * field anymore in the first place, and we're done after this call. */ |
| 306 | ret = mbedtls_ssl_session_copy( &cur->session, session ); |
| 307 | if( ret != 0 ) |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 308 | { |
Hanno Becker | aee8717 | 2019-02-06 14:53:19 +0000 | [diff] [blame] | 309 | ret = 1; |
| 310 | goto exit; |
| 311 | } |
| 312 | |
Hanno Becker | a887d1a | 2019-02-06 15:57:49 +0000 | [diff] [blame] | 313 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 314 | defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Hanno Becker | aee8717 | 2019-02-06 14:53:19 +0000 | [diff] [blame] | 315 | /* If present, free the X.509 structure and only store the raw CRT data. */ |
| 316 | if( cur->session.peer_cert != NULL ) |
| 317 | { |
| 318 | cur->peer_cert.p = |
| 319 | mbedtls_calloc( 1, cur->session.peer_cert->raw.len ); |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 320 | if( cur->peer_cert.p == NULL ) |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 321 | { |
| 322 | ret = 1; |
| 323 | goto exit; |
| 324 | } |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 325 | |
Hanno Becker | aee8717 | 2019-02-06 14:53:19 +0000 | [diff] [blame] | 326 | memcpy( cur->peer_cert.p, |
| 327 | cur->session.peer_cert->raw.p, |
| 328 | cur->session.peer_cert->raw.len ); |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 329 | cur->peer_cert.len = session->peer_cert->raw.len; |
| 330 | |
Hanno Becker | aee8717 | 2019-02-06 14:53:19 +0000 | [diff] [blame] | 331 | mbedtls_x509_crt_free( cur->session.peer_cert ); |
| 332 | mbedtls_free( cur->session.peer_cert ); |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 333 | cur->session.peer_cert = NULL; |
| 334 | } |
Hanno Becker | a887d1a | 2019-02-06 15:57:49 +0000 | [diff] [blame] | 335 | #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 336 | |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 337 | ret = 0; |
| 338 | |
| 339 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 340 | #if defined(MBEDTLS_THREADING_C) |
| 341 | if( mbedtls_mutex_unlock( &cache->mutex ) != 0 ) |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 342 | ret = 1; |
| 343 | #endif |
| 344 | |
| 345 | return( ret ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 348 | #if defined(MBEDTLS_HAVE_TIME) |
| 349 | 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] | 350 | { |
| 351 | if( timeout < 0 ) timeout = 0; |
| 352 | |
| 353 | cache->timeout = timeout; |
| 354 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 355 | #endif /* MBEDTLS_HAVE_TIME */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 356 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 357 | 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] | 358 | { |
| 359 | if( max < 0 ) max = 0; |
| 360 | |
| 361 | cache->max_entries = max; |
| 362 | } |
| 363 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 364 | void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache ) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 365 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 366 | mbedtls_ssl_cache_entry *cur, *prv; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 367 | |
| 368 | cur = cache->chain; |
| 369 | |
| 370 | while( cur != NULL ) |
| 371 | { |
| 372 | prv = cur; |
| 373 | cur = cur->next; |
| 374 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 375 | mbedtls_ssl_session_free( &prv->session ); |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 376 | |
Hanno Becker | a887d1a | 2019-02-06 15:57:49 +0000 | [diff] [blame] | 377 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 378 | defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 379 | mbedtls_free( prv->peer_cert.p ); |
Hanno Becker | a887d1a | 2019-02-06 15:57:49 +0000 | [diff] [blame] | 380 | #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 381 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 382 | mbedtls_free( prv ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 383 | } |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 384 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 385 | #if defined(MBEDTLS_THREADING_C) |
| 386 | mbedtls_mutex_free( &cache->mutex ); |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 387 | #endif |
Ron Eldor | 2236082 | 2017-10-29 17:53:52 +0200 | [diff] [blame] | 388 | cache->chain = NULL; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 391 | #endif /* MBEDTLS_SSL_CACHE_C */ |