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