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