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

Dependencies:   WncControllerK64F

Committer:
JMF
Date:
Thu Nov 17 16:13:29 2016 +0000
Revision:
18:198e9b0acf11
Parent:
12:0071cb144c7a
Updates to mbed os resulted in mutex.h going away and rtos.h needed to be used; This fixes the Mutex typedef failure.  Also cast data buffers from 'char *' to (const std::uint8_t*) to conform with Fred's changes in WncController

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JMF 12:0071cb144c7a 1 /*
JMF 12:0071cb144c7a 2 * DTLS cookie callbacks 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_COOKIE_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 #define mbedtls_calloc calloc
JMF 12:0071cb144c7a 38 #define mbedtls_free free
JMF 12:0071cb144c7a 39 #endif
JMF 12:0071cb144c7a 40
JMF 12:0071cb144c7a 41 #include "mbedtls/ssl_cookie.h"
JMF 12:0071cb144c7a 42 #include "mbedtls/ssl_internal.h"
JMF 12:0071cb144c7a 43
JMF 12:0071cb144c7a 44 #include <string.h>
JMF 12:0071cb144c7a 45
JMF 12:0071cb144c7a 46 /* Implementation that should never be optimized out by the compiler */
JMF 12:0071cb144c7a 47 static void mbedtls_zeroize( void *v, size_t n ) {
JMF 12:0071cb144c7a 48 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
JMF 12:0071cb144c7a 49 }
JMF 12:0071cb144c7a 50
JMF 12:0071cb144c7a 51 /*
JMF 12:0071cb144c7a 52 * If DTLS is in use, then at least one of SHA-1, SHA-256, SHA-512 is
JMF 12:0071cb144c7a 53 * available. Try SHA-256 first, 512 wastes resources since we need to stay
JMF 12:0071cb144c7a 54 * with max 32 bytes of cookie for DTLS 1.0
JMF 12:0071cb144c7a 55 */
JMF 12:0071cb144c7a 56 #if defined(MBEDTLS_SHA256_C)
JMF 12:0071cb144c7a 57 #define COOKIE_MD MBEDTLS_MD_SHA224
JMF 12:0071cb144c7a 58 #define COOKIE_MD_OUTLEN 32
JMF 12:0071cb144c7a 59 #define COOKIE_HMAC_LEN 28
JMF 12:0071cb144c7a 60 #elif defined(MBEDTLS_SHA512_C)
JMF 12:0071cb144c7a 61 #define COOKIE_MD MBEDTLS_MD_SHA384
JMF 12:0071cb144c7a 62 #define COOKIE_MD_OUTLEN 48
JMF 12:0071cb144c7a 63 #define COOKIE_HMAC_LEN 28
JMF 12:0071cb144c7a 64 #elif defined(MBEDTLS_SHA1_C)
JMF 12:0071cb144c7a 65 #define COOKIE_MD MBEDTLS_MD_SHA1
JMF 12:0071cb144c7a 66 #define COOKIE_MD_OUTLEN 20
JMF 12:0071cb144c7a 67 #define COOKIE_HMAC_LEN 20
JMF 12:0071cb144c7a 68 #else
JMF 12:0071cb144c7a 69 #error "DTLS hello verify needs SHA-1 or SHA-2"
JMF 12:0071cb144c7a 70 #endif
JMF 12:0071cb144c7a 71
JMF 12:0071cb144c7a 72 /*
JMF 12:0071cb144c7a 73 * Cookies are formed of a 4-bytes timestamp (or serial number) and
JMF 12:0071cb144c7a 74 * an HMAC of timestemp and client ID.
JMF 12:0071cb144c7a 75 */
JMF 12:0071cb144c7a 76 #define COOKIE_LEN ( 4 + COOKIE_HMAC_LEN )
JMF 12:0071cb144c7a 77
JMF 12:0071cb144c7a 78 void mbedtls_ssl_cookie_init( mbedtls_ssl_cookie_ctx *ctx )
JMF 12:0071cb144c7a 79 {
JMF 12:0071cb144c7a 80 mbedtls_md_init( &ctx->hmac_ctx );
JMF 12:0071cb144c7a 81 #if !defined(MBEDTLS_HAVE_TIME)
JMF 12:0071cb144c7a 82 ctx->serial = 0;
JMF 12:0071cb144c7a 83 #endif
JMF 12:0071cb144c7a 84 ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT;
JMF 12:0071cb144c7a 85
JMF 12:0071cb144c7a 86 #if defined(MBEDTLS_THREADING_C)
JMF 12:0071cb144c7a 87 mbedtls_mutex_init( &ctx->mutex );
JMF 12:0071cb144c7a 88 #endif
JMF 12:0071cb144c7a 89 }
JMF 12:0071cb144c7a 90
JMF 12:0071cb144c7a 91 void mbedtls_ssl_cookie_set_timeout( mbedtls_ssl_cookie_ctx *ctx, unsigned long delay )
JMF 12:0071cb144c7a 92 {
JMF 12:0071cb144c7a 93 ctx->timeout = delay;
JMF 12:0071cb144c7a 94 }
JMF 12:0071cb144c7a 95
JMF 12:0071cb144c7a 96 void mbedtls_ssl_cookie_free( mbedtls_ssl_cookie_ctx *ctx )
JMF 12:0071cb144c7a 97 {
JMF 12:0071cb144c7a 98 mbedtls_md_free( &ctx->hmac_ctx );
JMF 12:0071cb144c7a 99
JMF 12:0071cb144c7a 100 #if defined(MBEDTLS_THREADING_C)
JMF 12:0071cb144c7a 101 mbedtls_mutex_init( &ctx->mutex );
JMF 12:0071cb144c7a 102 #endif
JMF 12:0071cb144c7a 103
JMF 12:0071cb144c7a 104 mbedtls_zeroize( ctx, sizeof( mbedtls_ssl_cookie_ctx ) );
JMF 12:0071cb144c7a 105 }
JMF 12:0071cb144c7a 106
JMF 12:0071cb144c7a 107 int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx,
JMF 12:0071cb144c7a 108 int (*f_rng)(void *, unsigned char *, size_t),
JMF 12:0071cb144c7a 109 void *p_rng )
JMF 12:0071cb144c7a 110 {
JMF 12:0071cb144c7a 111 int ret;
JMF 12:0071cb144c7a 112 unsigned char key[COOKIE_MD_OUTLEN];
JMF 12:0071cb144c7a 113
JMF 12:0071cb144c7a 114 if( ( ret = f_rng( p_rng, key, sizeof( key ) ) ) != 0 )
JMF 12:0071cb144c7a 115 return( ret );
JMF 12:0071cb144c7a 116
JMF 12:0071cb144c7a 117 ret = mbedtls_md_setup( &ctx->hmac_ctx, mbedtls_md_info_from_type( COOKIE_MD ), 1 );
JMF 12:0071cb144c7a 118 if( ret != 0 )
JMF 12:0071cb144c7a 119 return( ret );
JMF 12:0071cb144c7a 120
JMF 12:0071cb144c7a 121 ret = mbedtls_md_hmac_starts( &ctx->hmac_ctx, key, sizeof( key ) );
JMF 12:0071cb144c7a 122 if( ret != 0 )
JMF 12:0071cb144c7a 123 return( ret );
JMF 12:0071cb144c7a 124
JMF 12:0071cb144c7a 125 mbedtls_zeroize( key, sizeof( key ) );
JMF 12:0071cb144c7a 126
JMF 12:0071cb144c7a 127 return( 0 );
JMF 12:0071cb144c7a 128 }
JMF 12:0071cb144c7a 129
JMF 12:0071cb144c7a 130 /*
JMF 12:0071cb144c7a 131 * Generate the HMAC part of a cookie
JMF 12:0071cb144c7a 132 */
JMF 12:0071cb144c7a 133 static int ssl_cookie_hmac( mbedtls_md_context_t *hmac_ctx,
JMF 12:0071cb144c7a 134 const unsigned char time[4],
JMF 12:0071cb144c7a 135 unsigned char **p, unsigned char *end,
JMF 12:0071cb144c7a 136 const unsigned char *cli_id, size_t cli_id_len )
JMF 12:0071cb144c7a 137 {
JMF 12:0071cb144c7a 138 unsigned char hmac_out[COOKIE_MD_OUTLEN];
JMF 12:0071cb144c7a 139
JMF 12:0071cb144c7a 140 if( (size_t)( end - *p ) < COOKIE_HMAC_LEN )
JMF 12:0071cb144c7a 141 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
JMF 12:0071cb144c7a 142
JMF 12:0071cb144c7a 143 if( mbedtls_md_hmac_reset( hmac_ctx ) != 0 ||
JMF 12:0071cb144c7a 144 mbedtls_md_hmac_update( hmac_ctx, time, 4 ) != 0 ||
JMF 12:0071cb144c7a 145 mbedtls_md_hmac_update( hmac_ctx, cli_id, cli_id_len ) != 0 ||
JMF 12:0071cb144c7a 146 mbedtls_md_hmac_finish( hmac_ctx, hmac_out ) != 0 )
JMF 12:0071cb144c7a 147 {
JMF 12:0071cb144c7a 148 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
JMF 12:0071cb144c7a 149 }
JMF 12:0071cb144c7a 150
JMF 12:0071cb144c7a 151 memcpy( *p, hmac_out, COOKIE_HMAC_LEN );
JMF 12:0071cb144c7a 152 *p += COOKIE_HMAC_LEN;
JMF 12:0071cb144c7a 153
JMF 12:0071cb144c7a 154 return( 0 );
JMF 12:0071cb144c7a 155 }
JMF 12:0071cb144c7a 156
JMF 12:0071cb144c7a 157 /*
JMF 12:0071cb144c7a 158 * Generate cookie for DTLS ClientHello verification
JMF 12:0071cb144c7a 159 */
JMF 12:0071cb144c7a 160 int mbedtls_ssl_cookie_write( void *p_ctx,
JMF 12:0071cb144c7a 161 unsigned char **p, unsigned char *end,
JMF 12:0071cb144c7a 162 const unsigned char *cli_id, size_t cli_id_len )
JMF 12:0071cb144c7a 163 {
JMF 12:0071cb144c7a 164 int ret;
JMF 12:0071cb144c7a 165 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
JMF 12:0071cb144c7a 166 unsigned long t;
JMF 12:0071cb144c7a 167
JMF 12:0071cb144c7a 168 if( ctx == NULL || cli_id == NULL )
JMF 12:0071cb144c7a 169 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
JMF 12:0071cb144c7a 170
JMF 12:0071cb144c7a 171 if( (size_t)( end - *p ) < COOKIE_LEN )
JMF 12:0071cb144c7a 172 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
JMF 12:0071cb144c7a 173
JMF 12:0071cb144c7a 174 #if defined(MBEDTLS_HAVE_TIME)
JMF 12:0071cb144c7a 175 t = (unsigned long) mbedtls_time( NULL );
JMF 12:0071cb144c7a 176 #else
JMF 12:0071cb144c7a 177 t = ctx->serial++;
JMF 12:0071cb144c7a 178 #endif
JMF 12:0071cb144c7a 179
JMF 12:0071cb144c7a 180 (*p)[0] = (unsigned char)( t >> 24 );
JMF 12:0071cb144c7a 181 (*p)[1] = (unsigned char)( t >> 16 );
JMF 12:0071cb144c7a 182 (*p)[2] = (unsigned char)( t >> 8 );
JMF 12:0071cb144c7a 183 (*p)[3] = (unsigned char)( t );
JMF 12:0071cb144c7a 184 *p += 4;
JMF 12:0071cb144c7a 185
JMF 12:0071cb144c7a 186 #if defined(MBEDTLS_THREADING_C)
JMF 12:0071cb144c7a 187 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
JMF 12:0071cb144c7a 188 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR + ret );
JMF 12:0071cb144c7a 189 #endif
JMF 12:0071cb144c7a 190
JMF 12:0071cb144c7a 191 ret = ssl_cookie_hmac( &ctx->hmac_ctx, *p - 4,
JMF 12:0071cb144c7a 192 p, end, cli_id, cli_id_len );
JMF 12:0071cb144c7a 193
JMF 12:0071cb144c7a 194 #if defined(MBEDTLS_THREADING_C)
JMF 12:0071cb144c7a 195 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
JMF 12:0071cb144c7a 196 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR +
JMF 12:0071cb144c7a 197 MBEDTLS_ERR_THREADING_MUTEX_ERROR );
JMF 12:0071cb144c7a 198 #endif
JMF 12:0071cb144c7a 199
JMF 12:0071cb144c7a 200 return( ret );
JMF 12:0071cb144c7a 201 }
JMF 12:0071cb144c7a 202
JMF 12:0071cb144c7a 203 /*
JMF 12:0071cb144c7a 204 * Check a cookie
JMF 12:0071cb144c7a 205 */
JMF 12:0071cb144c7a 206 int mbedtls_ssl_cookie_check( void *p_ctx,
JMF 12:0071cb144c7a 207 const unsigned char *cookie, size_t cookie_len,
JMF 12:0071cb144c7a 208 const unsigned char *cli_id, size_t cli_id_len )
JMF 12:0071cb144c7a 209 {
JMF 12:0071cb144c7a 210 unsigned char ref_hmac[COOKIE_HMAC_LEN];
JMF 12:0071cb144c7a 211 int ret = 0;
JMF 12:0071cb144c7a 212 unsigned char *p = ref_hmac;
JMF 12:0071cb144c7a 213 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
JMF 12:0071cb144c7a 214 unsigned long cur_time, cookie_time;
JMF 12:0071cb144c7a 215
JMF 12:0071cb144c7a 216 if( ctx == NULL || cli_id == NULL )
JMF 12:0071cb144c7a 217 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
JMF 12:0071cb144c7a 218
JMF 12:0071cb144c7a 219 if( cookie_len != COOKIE_LEN )
JMF 12:0071cb144c7a 220 return( -1 );
JMF 12:0071cb144c7a 221
JMF 12:0071cb144c7a 222 #if defined(MBEDTLS_THREADING_C)
JMF 12:0071cb144c7a 223 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
JMF 12:0071cb144c7a 224 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR + ret );
JMF 12:0071cb144c7a 225 #endif
JMF 12:0071cb144c7a 226
JMF 12:0071cb144c7a 227 if( ssl_cookie_hmac( &ctx->hmac_ctx, cookie,
JMF 12:0071cb144c7a 228 &p, p + sizeof( ref_hmac ),
JMF 12:0071cb144c7a 229 cli_id, cli_id_len ) != 0 )
JMF 12:0071cb144c7a 230 ret = -1;
JMF 12:0071cb144c7a 231
JMF 12:0071cb144c7a 232 #if defined(MBEDTLS_THREADING_C)
JMF 12:0071cb144c7a 233 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
JMF 12:0071cb144c7a 234 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR +
JMF 12:0071cb144c7a 235 MBEDTLS_ERR_THREADING_MUTEX_ERROR );
JMF 12:0071cb144c7a 236 #endif
JMF 12:0071cb144c7a 237
JMF 12:0071cb144c7a 238 if( ret != 0 )
JMF 12:0071cb144c7a 239 return( ret );
JMF 12:0071cb144c7a 240
JMF 12:0071cb144c7a 241 if( mbedtls_ssl_safer_memcmp( cookie + 4, ref_hmac, sizeof( ref_hmac ) ) != 0 )
JMF 12:0071cb144c7a 242 return( -1 );
JMF 12:0071cb144c7a 243
JMF 12:0071cb144c7a 244 #if defined(MBEDTLS_HAVE_TIME)
JMF 12:0071cb144c7a 245 cur_time = (unsigned long) mbedtls_time( NULL );
JMF 12:0071cb144c7a 246 #else
JMF 12:0071cb144c7a 247 cur_time = ctx->serial;
JMF 12:0071cb144c7a 248 #endif
JMF 12:0071cb144c7a 249
JMF 12:0071cb144c7a 250 cookie_time = ( (unsigned long) cookie[0] << 24 ) |
JMF 12:0071cb144c7a 251 ( (unsigned long) cookie[1] << 16 ) |
JMF 12:0071cb144c7a 252 ( (unsigned long) cookie[2] << 8 ) |
JMF 12:0071cb144c7a 253 ( (unsigned long) cookie[3] );
JMF 12:0071cb144c7a 254
JMF 12:0071cb144c7a 255 if( ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout )
JMF 12:0071cb144c7a 256 return( -1 );
JMF 12:0071cb144c7a 257
JMF 12:0071cb144c7a 258 return( 0 );
JMF 12:0071cb144c7a 259 }
JMF 12:0071cb144c7a 260 #endif /* MBEDTLS_SSL_COOKIE_C */