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" |
| 43 | |
| 44 | #include <string.h> |
| 45 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 46 | void mbedtls_ssl_cache_init( mbedtls_ssl_cache_context *cache ) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 47 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 48 | memset( cache, 0, sizeof( mbedtls_ssl_cache_context ) ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 49 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 50 | cache->timeout = MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT; |
| 51 | cache->max_entries = MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES; |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 52 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 53 | #if defined(MBEDTLS_THREADING_C) |
| 54 | mbedtls_mutex_init( &cache->mutex ); |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 55 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 58 | int mbedtls_ssl_cache_get( void *data, mbedtls_ssl_session *session ) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 59 | { |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 60 | int ret = 1; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 61 | #if defined(MBEDTLS_HAVE_TIME) |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 62 | mbedtls_time_t t = mbedtls_time( NULL ); |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 63 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 64 | mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data; |
| 65 | mbedtls_ssl_cache_entry *cur, *entry; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 66 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 67 | #if defined(MBEDTLS_THREADING_C) |
| 68 | if( mbedtls_mutex_lock( &cache->mutex ) != 0 ) |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 69 | return( 1 ); |
| 70 | #endif |
| 71 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 72 | cur = cache->chain; |
| 73 | entry = NULL; |
| 74 | |
| 75 | while( cur != NULL ) |
| 76 | { |
| 77 | entry = cur; |
| 78 | cur = cur->next; |
| 79 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 80 | #if defined(MBEDTLS_HAVE_TIME) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 81 | if( cache->timeout != 0 && |
| 82 | (int) ( t - entry->timestamp ) > cache->timeout ) |
| 83 | continue; |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 84 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 85 | |
| 86 | if( session->ciphersuite != entry->session.ciphersuite || |
| 87 | session->compression != entry->session.compression || |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 88 | session->id_len != entry->session.id_len ) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 89 | continue; |
| 90 | |
| 91 | if( memcmp( session->id, entry->session.id, |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 92 | entry->session.id_len ) != 0 ) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 93 | continue; |
| 94 | |
| 95 | memcpy( session->master, entry->session.master, 48 ); |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 96 | |
Manuel Pégourié-Gonnard | 38d1eba | 2013-08-23 10:44:29 +0200 | [diff] [blame] | 97 | session->verify_result = entry->session.verify_result; |
| 98 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 99 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 100 | /* |
| 101 | * Restore peer certificate (without rest of the original chain) |
| 102 | */ |
| 103 | if( entry->peer_cert.p != NULL ) |
| 104 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 105 | if( ( session->peer_cert = mbedtls_calloc( 1, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 106 | sizeof(mbedtls_x509_crt) ) ) == NULL ) |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 107 | { |
| 108 | ret = 1; |
| 109 | goto exit; |
| 110 | } |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 111 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 112 | mbedtls_x509_crt_init( session->peer_cert ); |
| 113 | if( mbedtls_x509_crt_parse( session->peer_cert, entry->peer_cert.p, |
Paul Bakker | ddf26b4 | 2013-09-18 13:46:23 +0200 | [diff] [blame] | 114 | entry->peer_cert.len ) != 0 ) |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 115 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 116 | mbedtls_free( session->peer_cert ); |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 117 | session->peer_cert = NULL; |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 118 | ret = 1; |
| 119 | goto exit; |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 120 | } |
| 121 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 122 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 123 | |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 124 | ret = 0; |
| 125 | goto exit; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 128 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 129 | #if defined(MBEDTLS_THREADING_C) |
| 130 | if( mbedtls_mutex_unlock( &cache->mutex ) != 0 ) |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 131 | ret = 1; |
| 132 | #endif |
| 133 | |
| 134 | return( ret ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 137 | int mbedtls_ssl_cache_set( void *data, const mbedtls_ssl_session *session ) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 138 | { |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 139 | int ret = 1; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 140 | #if defined(MBEDTLS_HAVE_TIME) |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 141 | mbedtls_time_t t = time( NULL ), oldest = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 142 | mbedtls_ssl_cache_entry *old = NULL; |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 143 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 144 | mbedtls_ssl_cache_context *cache = (mbedtls_ssl_cache_context *) data; |
| 145 | mbedtls_ssl_cache_entry *cur, *prv; |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 146 | int count = 0; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 147 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 148 | #if defined(MBEDTLS_THREADING_C) |
| 149 | if( ( ret = mbedtls_mutex_lock( &cache->mutex ) ) != 0 ) |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 150 | return( ret ); |
| 151 | #endif |
| 152 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 153 | cur = cache->chain; |
| 154 | prv = NULL; |
| 155 | |
| 156 | while( cur != NULL ) |
| 157 | { |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 158 | count++; |
| 159 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 160 | #if defined(MBEDTLS_HAVE_TIME) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 161 | if( cache->timeout != 0 && |
| 162 | (int) ( t - cur->timestamp ) > cache->timeout ) |
| 163 | { |
| 164 | cur->timestamp = t; |
| 165 | break; /* expired, reuse this slot, update timestamp */ |
| 166 | } |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 167 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 168 | |
Manuel Pégourié-Gonnard | 12ad798 | 2015-06-18 15:50:37 +0200 | [diff] [blame] | 169 | if( memcmp( session->id, cur->session.id, cur->session.id_len ) == 0 ) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 170 | break; /* client reconnected, keep timestamp for session id */ |
| 171 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 172 | #if defined(MBEDTLS_HAVE_TIME) |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 173 | if( oldest == 0 || cur->timestamp < oldest ) |
| 174 | { |
| 175 | oldest = cur->timestamp; |
| 176 | old = cur; |
| 177 | } |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 178 | #endif |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 179 | |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 180 | prv = cur; |
| 181 | cur = cur->next; |
| 182 | } |
| 183 | |
| 184 | if( cur == NULL ) |
| 185 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 186 | #if defined(MBEDTLS_HAVE_TIME) |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 187 | /* |
| 188 | * Reuse oldest entry if max_entries reached |
| 189 | */ |
Manuel Pégourié-Gonnard | 274a12e | 2014-02-20 21:32:08 +0100 | [diff] [blame] | 190 | if( count >= cache->max_entries ) |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 191 | { |
Manuel Pégourié-Gonnard | 274a12e | 2014-02-20 21:32:08 +0100 | [diff] [blame] | 192 | if( old == NULL ) |
| 193 | { |
| 194 | ret = 1; |
| 195 | goto exit; |
| 196 | } |
| 197 | |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 198 | cur = old; |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 199 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 200 | #else /* MBEDTLS_HAVE_TIME */ |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 201 | /* |
| 202 | * Reuse first entry in chain if max_entries reached, |
| 203 | * but move to last place |
| 204 | */ |
| 205 | if( count >= cache->max_entries ) |
| 206 | { |
| 207 | if( cache->chain == NULL ) |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 208 | { |
| 209 | ret = 1; |
| 210 | goto exit; |
| 211 | } |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 212 | |
| 213 | cur = cache->chain; |
| 214 | cache->chain = cur->next; |
Manuel Pégourié-Gonnard | 84c30c7 | 2014-02-26 17:38:55 +0100 | [diff] [blame] | 215 | cur->next = NULL; |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 216 | prv->next = cur; |
| 217 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 218 | #endif /* MBEDTLS_HAVE_TIME */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 219 | else |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 220 | { |
Manuel Pégourié-Gonnard | 274a12e | 2014-02-20 21:32:08 +0100 | [diff] [blame] | 221 | /* |
| 222 | * max_entries not reached, create new entry |
| 223 | */ |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 224 | cur = mbedtls_calloc( 1, sizeof(mbedtls_ssl_cache_entry) ); |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 225 | if( cur == NULL ) |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 226 | { |
| 227 | ret = 1; |
| 228 | goto exit; |
| 229 | } |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 230 | |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 231 | if( prv == NULL ) |
| 232 | cache->chain = cur; |
| 233 | else |
| 234 | prv->next = cur; |
| 235 | } |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 236 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 237 | #if defined(MBEDTLS_HAVE_TIME) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 238 | cur->timestamp = t; |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 239 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 242 | memcpy( &cur->session, session, sizeof( mbedtls_ssl_session ) ); |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 243 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 244 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 245 | /* |
Manuel Pégourié-Gonnard | 84c30c7 | 2014-02-26 17:38:55 +0100 | [diff] [blame] | 246 | * If we're reusing an entry, free its certificate first |
| 247 | */ |
| 248 | if( cur->peer_cert.p != NULL ) |
| 249 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 250 | mbedtls_free( cur->peer_cert.p ); |
| 251 | memset( &cur->peer_cert, 0, sizeof(mbedtls_x509_buf) ); |
Manuel Pégourié-Gonnard | 84c30c7 | 2014-02-26 17:38:55 +0100 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /* |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 255 | * Store peer certificate |
| 256 | */ |
| 257 | if( session->peer_cert != NULL ) |
| 258 | { |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 259 | cur->peer_cert.p = mbedtls_calloc( 1, session->peer_cert->raw.len ); |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 260 | if( cur->peer_cert.p == NULL ) |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 261 | { |
| 262 | ret = 1; |
| 263 | goto exit; |
| 264 | } |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 265 | |
| 266 | memcpy( cur->peer_cert.p, session->peer_cert->raw.p, |
| 267 | session->peer_cert->raw.len ); |
| 268 | cur->peer_cert.len = session->peer_cert->raw.len; |
| 269 | |
| 270 | cur->session.peer_cert = NULL; |
| 271 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 272 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 273 | |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 274 | ret = 0; |
| 275 | |
| 276 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 277 | #if defined(MBEDTLS_THREADING_C) |
| 278 | if( mbedtls_mutex_unlock( &cache->mutex ) != 0 ) |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 279 | ret = 1; |
| 280 | #endif |
| 281 | |
| 282 | return( ret ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 285 | #if defined(MBEDTLS_HAVE_TIME) |
| 286 | 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] | 287 | { |
| 288 | if( timeout < 0 ) timeout = 0; |
| 289 | |
| 290 | cache->timeout = timeout; |
| 291 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 292 | #endif /* MBEDTLS_HAVE_TIME */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 293 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 294 | 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] | 295 | { |
| 296 | if( max < 0 ) max = 0; |
| 297 | |
| 298 | cache->max_entries = max; |
| 299 | } |
| 300 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 301 | void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache ) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 302 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 303 | mbedtls_ssl_cache_entry *cur, *prv; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 304 | |
| 305 | cur = cache->chain; |
| 306 | |
| 307 | while( cur != NULL ) |
| 308 | { |
| 309 | prv = cur; |
| 310 | cur = cur->next; |
| 311 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 312 | mbedtls_ssl_session_free( &prv->session ); |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 313 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 314 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 315 | mbedtls_free( prv->peer_cert.p ); |
| 316 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 317 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 318 | mbedtls_free( prv ); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 319 | } |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 320 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 321 | #if defined(MBEDTLS_THREADING_C) |
| 322 | mbedtls_mutex_free( &cache->mutex ); |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 323 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 326 | #endif /* MBEDTLS_SSL_CACHE_C */ |