mbed TLS upgraded to 2.6.0

Fork of mbedtls by Mark Radbourne

Committer:
Jasper Wallace
Date:
Fri Sep 29 19:50:30 2017 +0100
Revision:
2:bbdeda018a3c
Parent:
0:cdf462088d13
Update to mbedtls 2.6.0, many changes.

Changes to mbedtls sources made:

in include/mbedtls/config.h comment out:

#define MBEDTLS_FS_IO
#define MBEDTLS_NET_C
#define MBEDTLS_TIMING_C

uncomment:

#define MBEDTLS_NO_PLATFORM_ENTROPY

remove the following directorys:

programs
yotta
visualc

Who changed what in which revision?

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