mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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