mbed TLS library

Dependents:   HTTPClient-SSL WS_SERVER

Committer:
ansond
Date:
Thu Jun 11 03:27:03 2015 +0000
Revision:
0:137634ff4186
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:137634ff4186 1 /*
ansond 0:137634ff4186 2 * SSL session cache implementation
ansond 0:137634ff4186 3 *
ansond 0:137634ff4186 4 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
ansond 0:137634ff4186 5 *
ansond 0:137634ff4186 6 * This file is part of mbed TLS (https://tls.mbed.org)
ansond 0:137634ff4186 7 *
ansond 0:137634ff4186 8 * This program is free software; you can redistribute it and/or modify
ansond 0:137634ff4186 9 * it under the terms of the GNU General Public License as published by
ansond 0:137634ff4186 10 * the Free Software Foundation; either version 2 of the License, or
ansond 0:137634ff4186 11 * (at your option) any later version.
ansond 0:137634ff4186 12 *
ansond 0:137634ff4186 13 * This program is distributed in the hope that it will be useful,
ansond 0:137634ff4186 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ansond 0:137634ff4186 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ansond 0:137634ff4186 16 * GNU General Public License for more details.
ansond 0:137634ff4186 17 *
ansond 0:137634ff4186 18 * You should have received a copy of the GNU General Public License along
ansond 0:137634ff4186 19 * with this program; if not, write to the Free Software Foundation, Inc.,
ansond 0:137634ff4186 20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ansond 0:137634ff4186 21 */
ansond 0:137634ff4186 22 /*
ansond 0:137634ff4186 23 * These session callbacks use a simple chained list
ansond 0:137634ff4186 24 * to store and retrieve the session information.
ansond 0:137634ff4186 25 */
ansond 0:137634ff4186 26
ansond 0:137634ff4186 27 #if !defined(POLARSSL_CONFIG_FILE)
ansond 0:137634ff4186 28 #include "polarssl/config.h"
ansond 0:137634ff4186 29 #else
ansond 0:137634ff4186 30 #include POLARSSL_CONFIG_FILE
ansond 0:137634ff4186 31 #endif
ansond 0:137634ff4186 32
ansond 0:137634ff4186 33 #if defined(POLARSSL_SSL_CACHE_C)
ansond 0:137634ff4186 34
ansond 0:137634ff4186 35 #include "polarssl/ssl_cache.h"
ansond 0:137634ff4186 36
ansond 0:137634ff4186 37 #include <string.h>
ansond 0:137634ff4186 38
ansond 0:137634ff4186 39 #if defined(POLARSSL_PLATFORM_C)
ansond 0:137634ff4186 40 #include "polarssl/platform.h"
ansond 0:137634ff4186 41 #else
ansond 0:137634ff4186 42 #include <stdlib.h>
ansond 0:137634ff4186 43 #define polarssl_malloc malloc
ansond 0:137634ff4186 44 #define polarssl_free free
ansond 0:137634ff4186 45 #endif
ansond 0:137634ff4186 46
ansond 0:137634ff4186 47 void ssl_cache_init( ssl_cache_context *cache )
ansond 0:137634ff4186 48 {
ansond 0:137634ff4186 49 memset( cache, 0, sizeof( ssl_cache_context ) );
ansond 0:137634ff4186 50
ansond 0:137634ff4186 51 cache->timeout = SSL_CACHE_DEFAULT_TIMEOUT;
ansond 0:137634ff4186 52 cache->max_entries = SSL_CACHE_DEFAULT_MAX_ENTRIES;
ansond 0:137634ff4186 53
ansond 0:137634ff4186 54 #if defined(POLARSSL_THREADING_C)
ansond 0:137634ff4186 55 polarssl_mutex_init( &cache->mutex );
ansond 0:137634ff4186 56 #endif
ansond 0:137634ff4186 57 }
ansond 0:137634ff4186 58
ansond 0:137634ff4186 59 int ssl_cache_get( void *data, ssl_session *session )
ansond 0:137634ff4186 60 {
ansond 0:137634ff4186 61 int ret = 1;
ansond 0:137634ff4186 62 #if defined(POLARSSL_HAVE_TIME)
ansond 0:137634ff4186 63 time_t t = time( NULL );
ansond 0:137634ff4186 64 #endif
ansond 0:137634ff4186 65 ssl_cache_context *cache = (ssl_cache_context *) data;
ansond 0:137634ff4186 66 ssl_cache_entry *cur, *entry;
ansond 0:137634ff4186 67
ansond 0:137634ff4186 68 #if defined(POLARSSL_THREADING_C)
ansond 0:137634ff4186 69 if( polarssl_mutex_lock( &cache->mutex ) != 0 )
ansond 0:137634ff4186 70 return( 1 );
ansond 0:137634ff4186 71 #endif
ansond 0:137634ff4186 72
ansond 0:137634ff4186 73 cur = cache->chain;
ansond 0:137634ff4186 74 entry = NULL;
ansond 0:137634ff4186 75
ansond 0:137634ff4186 76 while( cur != NULL )
ansond 0:137634ff4186 77 {
ansond 0:137634ff4186 78 entry = cur;
ansond 0:137634ff4186 79 cur = cur->next;
ansond 0:137634ff4186 80
ansond 0:137634ff4186 81 #if defined(POLARSSL_HAVE_TIME)
ansond 0:137634ff4186 82 if( cache->timeout != 0 &&
ansond 0:137634ff4186 83 (int) ( t - entry->timestamp ) > cache->timeout )
ansond 0:137634ff4186 84 continue;
ansond 0:137634ff4186 85 #endif
ansond 0:137634ff4186 86
ansond 0:137634ff4186 87 if( session->ciphersuite != entry->session.ciphersuite ||
ansond 0:137634ff4186 88 session->compression != entry->session.compression ||
ansond 0:137634ff4186 89 session->length != entry->session.length )
ansond 0:137634ff4186 90 continue;
ansond 0:137634ff4186 91
ansond 0:137634ff4186 92 if( memcmp( session->id, entry->session.id,
ansond 0:137634ff4186 93 entry->session.length ) != 0 )
ansond 0:137634ff4186 94 continue;
ansond 0:137634ff4186 95
ansond 0:137634ff4186 96 memcpy( session->master, entry->session.master, 48 );
ansond 0:137634ff4186 97
ansond 0:137634ff4186 98 session->verify_result = entry->session.verify_result;
ansond 0:137634ff4186 99
ansond 0:137634ff4186 100 #if defined(POLARSSL_X509_CRT_PARSE_C)
ansond 0:137634ff4186 101 /*
ansond 0:137634ff4186 102 * Restore peer certificate (without rest of the original chain)
ansond 0:137634ff4186 103 */
ansond 0:137634ff4186 104 if( entry->peer_cert.p != NULL )
ansond 0:137634ff4186 105 {
ansond 0:137634ff4186 106 if( ( session->peer_cert = polarssl_malloc(
ansond 0:137634ff4186 107 sizeof(x509_crt) ) ) == NULL )
ansond 0:137634ff4186 108 {
ansond 0:137634ff4186 109 ret = 1;
ansond 0:137634ff4186 110 goto exit;
ansond 0:137634ff4186 111 }
ansond 0:137634ff4186 112
ansond 0:137634ff4186 113 x509_crt_init( session->peer_cert );
ansond 0:137634ff4186 114 if( x509_crt_parse( session->peer_cert, entry->peer_cert.p,
ansond 0:137634ff4186 115 entry->peer_cert.len ) != 0 )
ansond 0:137634ff4186 116 {
ansond 0:137634ff4186 117 polarssl_free( session->peer_cert );
ansond 0:137634ff4186 118 session->peer_cert = NULL;
ansond 0:137634ff4186 119 ret = 1;
ansond 0:137634ff4186 120 goto exit;
ansond 0:137634ff4186 121 }
ansond 0:137634ff4186 122 }
ansond 0:137634ff4186 123 #endif /* POLARSSL_X509_CRT_PARSE_C */
ansond 0:137634ff4186 124
ansond 0:137634ff4186 125 ret = 0;
ansond 0:137634ff4186 126 goto exit;
ansond 0:137634ff4186 127 }
ansond 0:137634ff4186 128
ansond 0:137634ff4186 129 exit:
ansond 0:137634ff4186 130 #if defined(POLARSSL_THREADING_C)
ansond 0:137634ff4186 131 if( polarssl_mutex_unlock( &cache->mutex ) != 0 )
ansond 0:137634ff4186 132 ret = 1;
ansond 0:137634ff4186 133 #endif
ansond 0:137634ff4186 134
ansond 0:137634ff4186 135 return( ret );
ansond 0:137634ff4186 136 }
ansond 0:137634ff4186 137
ansond 0:137634ff4186 138 int ssl_cache_set( void *data, const ssl_session *session )
ansond 0:137634ff4186 139 {
ansond 0:137634ff4186 140 int ret = 1;
ansond 0:137634ff4186 141 #if defined(POLARSSL_HAVE_TIME)
ansond 0:137634ff4186 142 time_t t = time( NULL ), oldest = 0;
ansond 0:137634ff4186 143 ssl_cache_entry *old = NULL;
ansond 0:137634ff4186 144 #endif
ansond 0:137634ff4186 145 ssl_cache_context *cache = (ssl_cache_context *) data;
ansond 0:137634ff4186 146 ssl_cache_entry *cur, *prv;
ansond 0:137634ff4186 147 int count = 0;
ansond 0:137634ff4186 148
ansond 0:137634ff4186 149 #if defined(POLARSSL_THREADING_C)
ansond 0:137634ff4186 150 if( ( ret = polarssl_mutex_lock( &cache->mutex ) ) != 0 )
ansond 0:137634ff4186 151 return( ret );
ansond 0:137634ff4186 152 #endif
ansond 0:137634ff4186 153
ansond 0:137634ff4186 154 cur = cache->chain;
ansond 0:137634ff4186 155 prv = NULL;
ansond 0:137634ff4186 156
ansond 0:137634ff4186 157 while( cur != NULL )
ansond 0:137634ff4186 158 {
ansond 0:137634ff4186 159 count++;
ansond 0:137634ff4186 160
ansond 0:137634ff4186 161 #if defined(POLARSSL_HAVE_TIME)
ansond 0:137634ff4186 162 if( cache->timeout != 0 &&
ansond 0:137634ff4186 163 (int) ( t - cur->timestamp ) > cache->timeout )
ansond 0:137634ff4186 164 {
ansond 0:137634ff4186 165 cur->timestamp = t;
ansond 0:137634ff4186 166 break; /* expired, reuse this slot, update timestamp */
ansond 0:137634ff4186 167 }
ansond 0:137634ff4186 168 #endif
ansond 0:137634ff4186 169
ansond 0:137634ff4186 170 if( memcmp( session->id, cur->session.id, cur->session.length ) == 0 )
ansond 0:137634ff4186 171 break; /* client reconnected, keep timestamp for session id */
ansond 0:137634ff4186 172
ansond 0:137634ff4186 173 #if defined(POLARSSL_HAVE_TIME)
ansond 0:137634ff4186 174 if( oldest == 0 || cur->timestamp < oldest )
ansond 0:137634ff4186 175 {
ansond 0:137634ff4186 176 oldest = cur->timestamp;
ansond 0:137634ff4186 177 old = cur;
ansond 0:137634ff4186 178 }
ansond 0:137634ff4186 179 #endif
ansond 0:137634ff4186 180
ansond 0:137634ff4186 181 prv = cur;
ansond 0:137634ff4186 182 cur = cur->next;
ansond 0:137634ff4186 183 }
ansond 0:137634ff4186 184
ansond 0:137634ff4186 185 if( cur == NULL )
ansond 0:137634ff4186 186 {
ansond 0:137634ff4186 187 #if defined(POLARSSL_HAVE_TIME)
ansond 0:137634ff4186 188 /*
ansond 0:137634ff4186 189 * Reuse oldest entry if max_entries reached
ansond 0:137634ff4186 190 */
ansond 0:137634ff4186 191 if( count >= cache->max_entries )
ansond 0:137634ff4186 192 {
ansond 0:137634ff4186 193 if( old == NULL )
ansond 0:137634ff4186 194 {
ansond 0:137634ff4186 195 ret = 1;
ansond 0:137634ff4186 196 goto exit;
ansond 0:137634ff4186 197 }
ansond 0:137634ff4186 198
ansond 0:137634ff4186 199 cur = old;
ansond 0:137634ff4186 200 }
ansond 0:137634ff4186 201 #else /* POLARSSL_HAVE_TIME */
ansond 0:137634ff4186 202 /*
ansond 0:137634ff4186 203 * Reuse first entry in chain if max_entries reached,
ansond 0:137634ff4186 204 * but move to last place
ansond 0:137634ff4186 205 */
ansond 0:137634ff4186 206 if( count >= cache->max_entries )
ansond 0:137634ff4186 207 {
ansond 0:137634ff4186 208 if( cache->chain == NULL )
ansond 0:137634ff4186 209 {
ansond 0:137634ff4186 210 ret = 1;
ansond 0:137634ff4186 211 goto exit;
ansond 0:137634ff4186 212 }
ansond 0:137634ff4186 213
ansond 0:137634ff4186 214 cur = cache->chain;
ansond 0:137634ff4186 215 cache->chain = cur->next;
ansond 0:137634ff4186 216 cur->next = NULL;
ansond 0:137634ff4186 217 prv->next = cur;
ansond 0:137634ff4186 218 }
ansond 0:137634ff4186 219 #endif /* POLARSSL_HAVE_TIME */
ansond 0:137634ff4186 220 else
ansond 0:137634ff4186 221 {
ansond 0:137634ff4186 222 /*
ansond 0:137634ff4186 223 * max_entries not reached, create new entry
ansond 0:137634ff4186 224 */
ansond 0:137634ff4186 225 cur = polarssl_malloc( sizeof(ssl_cache_entry) );
ansond 0:137634ff4186 226 if( cur == NULL )
ansond 0:137634ff4186 227 {
ansond 0:137634ff4186 228 ret = 1;
ansond 0:137634ff4186 229 goto exit;
ansond 0:137634ff4186 230 }
ansond 0:137634ff4186 231
ansond 0:137634ff4186 232 memset( cur, 0, sizeof(ssl_cache_entry) );
ansond 0:137634ff4186 233
ansond 0:137634ff4186 234 if( prv == NULL )
ansond 0:137634ff4186 235 cache->chain = cur;
ansond 0:137634ff4186 236 else
ansond 0:137634ff4186 237 prv->next = cur;
ansond 0:137634ff4186 238 }
ansond 0:137634ff4186 239
ansond 0:137634ff4186 240 #if defined(POLARSSL_HAVE_TIME)
ansond 0:137634ff4186 241 cur->timestamp = t;
ansond 0:137634ff4186 242 #endif
ansond 0:137634ff4186 243 }
ansond 0:137634ff4186 244
ansond 0:137634ff4186 245 memcpy( &cur->session, session, sizeof( ssl_session ) );
ansond 0:137634ff4186 246
ansond 0:137634ff4186 247 #if defined(POLARSSL_X509_CRT_PARSE_C)
ansond 0:137634ff4186 248 /*
ansond 0:137634ff4186 249 * If we're reusing an entry, free its certificate first
ansond 0:137634ff4186 250 */
ansond 0:137634ff4186 251 if( cur->peer_cert.p != NULL )
ansond 0:137634ff4186 252 {
ansond 0:137634ff4186 253 polarssl_free( cur->peer_cert.p );
ansond 0:137634ff4186 254 memset( &cur->peer_cert, 0, sizeof(x509_buf) );
ansond 0:137634ff4186 255 }
ansond 0:137634ff4186 256
ansond 0:137634ff4186 257 /*
ansond 0:137634ff4186 258 * Store peer certificate
ansond 0:137634ff4186 259 */
ansond 0:137634ff4186 260 if( session->peer_cert != NULL )
ansond 0:137634ff4186 261 {
ansond 0:137634ff4186 262 cur->peer_cert.p = polarssl_malloc( session->peer_cert->raw.len );
ansond 0:137634ff4186 263 if( cur->peer_cert.p == NULL )
ansond 0:137634ff4186 264 {
ansond 0:137634ff4186 265 ret = 1;
ansond 0:137634ff4186 266 goto exit;
ansond 0:137634ff4186 267 }
ansond 0:137634ff4186 268
ansond 0:137634ff4186 269 memcpy( cur->peer_cert.p, session->peer_cert->raw.p,
ansond 0:137634ff4186 270 session->peer_cert->raw.len );
ansond 0:137634ff4186 271 cur->peer_cert.len = session->peer_cert->raw.len;
ansond 0:137634ff4186 272
ansond 0:137634ff4186 273 cur->session.peer_cert = NULL;
ansond 0:137634ff4186 274 }
ansond 0:137634ff4186 275 #endif /* POLARSSL_X509_CRT_PARSE_C */
ansond 0:137634ff4186 276
ansond 0:137634ff4186 277 ret = 0;
ansond 0:137634ff4186 278
ansond 0:137634ff4186 279 exit:
ansond 0:137634ff4186 280 #if defined(POLARSSL_THREADING_C)
ansond 0:137634ff4186 281 if( polarssl_mutex_unlock( &cache->mutex ) != 0 )
ansond 0:137634ff4186 282 ret = 1;
ansond 0:137634ff4186 283 #endif
ansond 0:137634ff4186 284
ansond 0:137634ff4186 285 return( ret );
ansond 0:137634ff4186 286 }
ansond 0:137634ff4186 287
ansond 0:137634ff4186 288 #if defined(POLARSSL_HAVE_TIME)
ansond 0:137634ff4186 289 void ssl_cache_set_timeout( ssl_cache_context *cache, int timeout )
ansond 0:137634ff4186 290 {
ansond 0:137634ff4186 291 if( timeout < 0 ) timeout = 0;
ansond 0:137634ff4186 292
ansond 0:137634ff4186 293 cache->timeout = timeout;
ansond 0:137634ff4186 294 }
ansond 0:137634ff4186 295 #endif /* POLARSSL_HAVE_TIME */
ansond 0:137634ff4186 296
ansond 0:137634ff4186 297 void ssl_cache_set_max_entries( ssl_cache_context *cache, int max )
ansond 0:137634ff4186 298 {
ansond 0:137634ff4186 299 if( max < 0 ) max = 0;
ansond 0:137634ff4186 300
ansond 0:137634ff4186 301 cache->max_entries = max;
ansond 0:137634ff4186 302 }
ansond 0:137634ff4186 303
ansond 0:137634ff4186 304 void ssl_cache_free( ssl_cache_context *cache )
ansond 0:137634ff4186 305 {
ansond 0:137634ff4186 306 ssl_cache_entry *cur, *prv;
ansond 0:137634ff4186 307
ansond 0:137634ff4186 308 cur = cache->chain;
ansond 0:137634ff4186 309
ansond 0:137634ff4186 310 while( cur != NULL )
ansond 0:137634ff4186 311 {
ansond 0:137634ff4186 312 prv = cur;
ansond 0:137634ff4186 313 cur = cur->next;
ansond 0:137634ff4186 314
ansond 0:137634ff4186 315 ssl_session_free( &prv->session );
ansond 0:137634ff4186 316
ansond 0:137634ff4186 317 #if defined(POLARSSL_X509_CRT_PARSE_C)
ansond 0:137634ff4186 318 polarssl_free( prv->peer_cert.p );
ansond 0:137634ff4186 319 #endif /* POLARSSL_X509_CRT_PARSE_C */
ansond 0:137634ff4186 320
ansond 0:137634ff4186 321 polarssl_free( prv );
ansond 0:137634ff4186 322 }
ansond 0:137634ff4186 323
ansond 0:137634ff4186 324 #if defined(POLARSSL_THREADING_C)
ansond 0:137634ff4186 325 polarssl_mutex_free( &cache->mutex );
ansond 0:137634ff4186 326 #endif
ansond 0:137634ff4186 327 }
ansond 0:137634ff4186 328
ansond 0:137634ff4186 329 #endif /* POLARSSL_SSL_CACHE_C */
ansond 0:137634ff4186 330