I added functionality to get the RSSI, BER, and Cell Neighbor for reporting connection issues to M2X

Dependencies:   WncControllerK64F

Committer:
JMF
Date:
Tue Nov 01 14:22:56 2016 +0000
Revision:
12:0071cb144c7a
Adding mbedtls files

Who changed what in which revision?

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