1 /* 2 * SSL session cache implementation 3 * 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 6 * 7 * This file is provided under the Apache License 2.0, or the 8 * GNU General Public License v2.0 or later. 9 * 10 * ********** 11 * Apache License 2.0: 12 * 13 * Licensed under the Apache License, Version 2.0 (the "License"); you may 14 * not use this file except in compliance with the License. 15 * You may obtain a copy of the License at 16 * 17 * http://www.apache.org/licenses/LICENSE-2.0 18 * 19 * Unless required by applicable law or agreed to in writing, software 20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22 * See the License for the specific language governing permissions and 23 * limitations under the License. 24 * 25 * ********** 26 * 27 * ********** 28 * GNU General Public License v2.0 or later: 29 * 30 * This program is free software; you can redistribute it and/or modify 31 * it under the terms of the GNU General Public License as published by 32 * the Free Software Foundation; either version 2 of the License, or 33 * (at your option) any later version. 34 * 35 * This program is distributed in the hope that it will be useful, 36 * but WITHOUT ANY WARRANTY; without even the implied warranty of 37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 38 * GNU General Public License for more details. 39 * 40 * You should have received a copy of the GNU General Public License along 41 * with this program; if not, write to the Free Software Foundation, Inc., 42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 43 * 44 * ********** 45 * 46 * This file is part of mbed TLS (https://tls.mbed.org) 47 */ 48 /* 49 * These session callbacks use a simple chained list 50 * to store and retrieve the session information. 51 */ 52 53 #if !defined(MBEDTLS_CONFIG_FILE) 54 #include "mbedtls/config.h" 55 #else 56 #include MBEDTLS_CONFIG_FILE 57 #endif 58 59 #if defined(MBEDTLS_SSL_CACHE_C) 60 61 #if defined(MBEDTLS_PLATFORM_C) 62 #include "mbedtls/platform.h" 63 #else 64 #include <stdlib.h> 65 #define mbedtls_calloc calloc 66 #define mbedtls_free free 67 #endif 68 69 #include "mbedtls/ssl_cache.h" 70 71 #include <string.h> 72 73 void mbedtls_ssl_cache_init( mbedtls_ssl_cache_context *cache ) 74 { 75 memset( cache, 0, sizeof( mbedtls_ssl_cache_context ) ); 76 77 cache->timeout = MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT; 78 cache->max_entries = MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES; 79 80 #if defined(MBEDTLS_THREADING_C) 81 mbedtls_mutex_init( &cache->mutex ); 82 #endif 83 } 84 85 int mbedtls_ssl_cache_get( void *data, mbedtls_ssl_session *session ) 86 { 87 int ret = 1; 88 #if defined(MBEDTLS_HAVE_TIME) 89 mbedtls_time_t t = mbedtls_time( NULL ); 90 #endif 91 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data; 92 mbedtls_ssl_cache_entry *cur, *entry; 93 94 #if defined(MBEDTLS_THREADING_C) 95 if( mbedtls_mutex_lock( &cache->mutex ) != 0 ) 96 return( 1 ); 97 #endif 98 99 cur = cache->chain; 100 entry = NULL; 101 102 while( cur != NULL ) 103 { 104 entry = cur; 105 cur = cur->next; 106 107 #if defined(MBEDTLS_HAVE_TIME) 108 if( cache->timeout != 0 && 109 (int) ( t - entry->timestamp ) > cache->timeout ) 110 continue; 111 #endif 112 113 if( session->ciphersuite != entry->session.ciphersuite || 114 session->compression != entry->session.compression || 115 session->id_len != entry->session.id_len ) 116 continue; 117 118 if( memcmp( session->id, entry->session.id, 119 entry->session.id_len ) != 0 ) 120 continue; 121 122 memcpy( session->master, entry->session.master, 48 ); 123 124 session->verify_result = entry->session.verify_result; 125 126 #if defined(MBEDTLS_X509_CRT_PARSE_C) 127 /* 128 * Restore peer certificate (without rest of the original chain) 129 */ 130 if( entry->peer_cert.p != NULL ) 131 { 132 if( ( session->peer_cert = mbedtls_calloc( 1, 133 sizeof(mbedtls_x509_crt) ) ) == NULL ) 134 { 135 ret = 1; 136 goto exit; 137 } 138 139 mbedtls_x509_crt_init( session->peer_cert ); 140 if( mbedtls_x509_crt_parse( session->peer_cert, entry->peer_cert.p, 141 entry->peer_cert.len ) != 0 ) 142 { 143 mbedtls_free( session->peer_cert ); 144 session->peer_cert = NULL; 145 ret = 1; 146 goto exit; 147 } 148 } 149 #endif /* MBEDTLS_X509_CRT_PARSE_C */ 150 151 ret = 0; 152 goto exit; 153 } 154 155 exit: 156 #if defined(MBEDTLS_THREADING_C) 157 if( mbedtls_mutex_unlock( &cache->mutex ) != 0 ) 158 ret = 1; 159 #endif 160 161 return( ret ); 162 } 163 164 int mbedtls_ssl_cache_set( void *data, const mbedtls_ssl_session *session ) 165 { 166 int ret = 1; 167 #if defined(MBEDTLS_HAVE_TIME) 168 mbedtls_time_t t = mbedtls_time( NULL ), oldest = 0; 169 mbedtls_ssl_cache_entry *old = NULL; 170 #endif 171 mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data; 172 mbedtls_ssl_cache_entry *cur, *prv; 173 int count = 0; 174 175 #if defined(MBEDTLS_THREADING_C) 176 if( ( ret = mbedtls_mutex_lock( &cache->mutex ) ) != 0 ) 177 return( ret ); 178 #endif 179 180 cur = cache->chain; 181 prv = NULL; 182 183 while( cur != NULL ) 184 { 185 count++; 186 187 #if defined(MBEDTLS_HAVE_TIME) 188 if( cache->timeout != 0 && 189 (int) ( t - cur->timestamp ) > cache->timeout ) 190 { 191 cur->timestamp = t; 192 break; /* expired, reuse this slot, update timestamp */ 193 } 194 #endif 195 196 if( memcmp( session->id, cur->session.id, cur->session.id_len ) == 0 ) 197 break; /* client reconnected, keep timestamp for session id */ 198 199 #if defined(MBEDTLS_HAVE_TIME) 200 if( oldest == 0 || cur->timestamp < oldest ) 201 { 202 oldest = cur->timestamp; 203 old = cur; 204 } 205 #endif 206 207 prv = cur; 208 cur = cur->next; 209 } 210 211 if( cur == NULL ) 212 { 213 #if defined(MBEDTLS_HAVE_TIME) 214 /* 215 * Reuse oldest entry if max_entries reached 216 */ 217 if( count >= cache->max_entries ) 218 { 219 if( old == NULL ) 220 { 221 ret = 1; 222 goto exit; 223 } 224 225 cur = old; 226 } 227 #else /* MBEDTLS_HAVE_TIME */ 228 /* 229 * Reuse first entry in chain if max_entries reached, 230 * but move to last place 231 */ 232 if( count >= cache->max_entries ) 233 { 234 if( cache->chain == NULL ) 235 { 236 ret = 1; 237 goto exit; 238 } 239 240 cur = cache->chain; 241 cache->chain = cur->next; 242 cur->next = NULL; 243 prv->next = cur; 244 } 245 #endif /* MBEDTLS_HAVE_TIME */ 246 else 247 { 248 /* 249 * max_entries not reached, create new entry 250 */ 251 cur = mbedtls_calloc( 1, sizeof(mbedtls_ssl_cache_entry) ); 252 if( cur == NULL ) 253 { 254 ret = 1; 255 goto exit; 256 } 257 258 if( prv == NULL ) 259 cache->chain = cur; 260 else 261 prv->next = cur; 262 } 263 264 #if defined(MBEDTLS_HAVE_TIME) 265 cur->timestamp = t; 266 #endif 267 } 268 269 memcpy( &cur->session, session, sizeof( mbedtls_ssl_session ) ); 270 271 #if defined(MBEDTLS_X509_CRT_PARSE_C) 272 /* 273 * If we're reusing an entry, free its certificate first 274 */ 275 if( cur->peer_cert.p != NULL ) 276 { 277 mbedtls_free( cur->peer_cert.p ); 278 memset( &cur->peer_cert, 0, sizeof(mbedtls_x509_buf) ); 279 } 280 281 /* 282 * Store peer certificate 283 */ 284 if( session->peer_cert != NULL ) 285 { 286 cur->peer_cert.p = mbedtls_calloc( 1, session->peer_cert->raw.len ); 287 if( cur->peer_cert.p == NULL ) 288 { 289 ret = 1; 290 goto exit; 291 } 292 293 memcpy( cur->peer_cert.p, session->peer_cert->raw.p, 294 session->peer_cert->raw.len ); 295 cur->peer_cert.len = session->peer_cert->raw.len; 296 297 cur->session.peer_cert = NULL; 298 } 299 #endif /* MBEDTLS_X509_CRT_PARSE_C */ 300 301 ret = 0; 302 303 exit: 304 #if defined(MBEDTLS_THREADING_C) 305 if( mbedtls_mutex_unlock( &cache->mutex ) != 0 ) 306 ret = 1; 307 #endif 308 309 return( ret ); 310 } 311 312 #if defined(MBEDTLS_HAVE_TIME) 313 void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout ) 314 { 315 if( timeout < 0 ) timeout = 0; 316 317 cache->timeout = timeout; 318 } 319 #endif /* MBEDTLS_HAVE_TIME */ 320 321 void mbedtls_ssl_cache_set_max_entries( mbedtls_ssl_cache_context *cache, int max ) 322 { 323 if( max < 0 ) max = 0; 324 325 cache->max_entries = max; 326 } 327 328 void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache ) 329 { 330 mbedtls_ssl_cache_entry *cur, *prv; 331 332 cur = cache->chain; 333 334 while( cur != NULL ) 335 { 336 prv = cur; 337 cur = cur->next; 338 339 mbedtls_ssl_session_free( &prv->session ); 340 341 #if defined(MBEDTLS_X509_CRT_PARSE_C) 342 mbedtls_free( prv->peer_cert.p ); 343 #endif /* MBEDTLS_X509_CRT_PARSE_C */ 344 345 mbedtls_free( prv ); 346 } 347 348 #if defined(MBEDTLS_THREADING_C) 349 mbedtls_mutex_free( &cache->mutex ); 350 #endif 351 cache->chain = NULL; 352 } 353 354 #endif /* MBEDTLS_SSL_CACHE_C */ 355