Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Dec 16 16:27:57 2016 +0000
Revision:
3:1198227e6421
Parent:
0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms

Who changed what in which revision?

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