mbed client on ethernet with LWIP

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by sandbox

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedAustin 11:cada08fc8a70 1 /*
mbedAustin 11:cada08fc8a70 2 * DTLS cookie callbacks implementation
mbedAustin 11:cada08fc8a70 3 *
mbedAustin 11:cada08fc8a70 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
mbedAustin 11:cada08fc8a70 5 * SPDX-License-Identifier: Apache-2.0
mbedAustin 11:cada08fc8a70 6 *
mbedAustin 11:cada08fc8a70 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
mbedAustin 11:cada08fc8a70 8 * not use this file except in compliance with the License.
mbedAustin 11:cada08fc8a70 9 * You may obtain a copy of the License at
mbedAustin 11:cada08fc8a70 10 *
mbedAustin 11:cada08fc8a70 11 * http://www.apache.org/licenses/LICENSE-2.0
mbedAustin 11:cada08fc8a70 12 *
mbedAustin 11:cada08fc8a70 13 * Unless required by applicable law or agreed to in writing, software
mbedAustin 11:cada08fc8a70 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
mbedAustin 11:cada08fc8a70 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbedAustin 11:cada08fc8a70 16 * See the License for the specific language governing permissions and
mbedAustin 11:cada08fc8a70 17 * limitations under the License.
mbedAustin 11:cada08fc8a70 18 *
mbedAustin 11:cada08fc8a70 19 * This file is part of mbed TLS (https://tls.mbed.org)
mbedAustin 11:cada08fc8a70 20 */
mbedAustin 11:cada08fc8a70 21 /*
mbedAustin 11:cada08fc8a70 22 * These session callbacks use a simple chained list
mbedAustin 11:cada08fc8a70 23 * to store and retrieve the session information.
mbedAustin 11:cada08fc8a70 24 */
mbedAustin 11:cada08fc8a70 25
mbedAustin 11:cada08fc8a70 26 #if !defined(MBEDTLS_CONFIG_FILE)
mbedAustin 11:cada08fc8a70 27 #include "mbedtls/config.h"
mbedAustin 11:cada08fc8a70 28 #else
mbedAustin 11:cada08fc8a70 29 #include MBEDTLS_CONFIG_FILE
mbedAustin 11:cada08fc8a70 30 #endif
mbedAustin 11:cada08fc8a70 31
mbedAustin 11:cada08fc8a70 32 #if defined(MBEDTLS_SSL_COOKIE_C)
mbedAustin 11:cada08fc8a70 33
mbedAustin 11:cada08fc8a70 34 #include "mbedtls/ssl_cookie.h"
mbedAustin 11:cada08fc8a70 35 #include "mbedtls/ssl_internal.h"
mbedAustin 11:cada08fc8a70 36
mbedAustin 11:cada08fc8a70 37 #if defined(MBEDTLS_PLATFORM_C)
mbedAustin 11:cada08fc8a70 38 #include "mbedtls/platform.h"
mbedAustin 11:cada08fc8a70 39 #else
mbedAustin 11:cada08fc8a70 40 #define mbedtls_calloc calloc
mbedAustin 11:cada08fc8a70 41 #define mbedtls_free free
mbedAustin 11:cada08fc8a70 42 #endif
mbedAustin 11:cada08fc8a70 43
mbedAustin 11:cada08fc8a70 44 #include <string.h>
mbedAustin 11:cada08fc8a70 45
mbedAustin 11:cada08fc8a70 46 /* Implementation that should never be optimized out by the compiler */
mbedAustin 11:cada08fc8a70 47 static void mbedtls_zeroize( void *v, size_t n ) {
mbedAustin 11:cada08fc8a70 48 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
mbedAustin 11:cada08fc8a70 49 }
mbedAustin 11:cada08fc8a70 50
mbedAustin 11:cada08fc8a70 51 /*
mbedAustin 11:cada08fc8a70 52 * If DTLS is in use, then at least one of SHA-1, SHA-256, SHA-512 is
mbedAustin 11:cada08fc8a70 53 * available. Try SHA-256 first, 512 wastes resources since we need to stay
mbedAustin 11:cada08fc8a70 54 * with max 32 bytes of cookie for DTLS 1.0
mbedAustin 11:cada08fc8a70 55 */
mbedAustin 11:cada08fc8a70 56 #if defined(MBEDTLS_SHA256_C)
mbedAustin 11:cada08fc8a70 57 #define COOKIE_MD MBEDTLS_MD_SHA224
mbedAustin 11:cada08fc8a70 58 #define COOKIE_MD_OUTLEN 32
mbedAustin 11:cada08fc8a70 59 #define COOKIE_HMAC_LEN 28
mbedAustin 11:cada08fc8a70 60 #elif defined(MBEDTLS_SHA512_C)
mbedAustin 11:cada08fc8a70 61 #define COOKIE_MD MBEDTLS_MD_SHA384
mbedAustin 11:cada08fc8a70 62 #define COOKIE_MD_OUTLEN 48
mbedAustin 11:cada08fc8a70 63 #define COOKIE_HMAC_LEN 28
mbedAustin 11:cada08fc8a70 64 #elif defined(MBEDTLS_SHA1_C)
mbedAustin 11:cada08fc8a70 65 #define COOKIE_MD MBEDTLS_MD_SHA1
mbedAustin 11:cada08fc8a70 66 #define COOKIE_MD_OUTLEN 20
mbedAustin 11:cada08fc8a70 67 #define COOKIE_HMAC_LEN 20
mbedAustin 11:cada08fc8a70 68 #else
mbedAustin 11:cada08fc8a70 69 #error "DTLS hello verify needs SHA-1 or SHA-2"
mbedAustin 11:cada08fc8a70 70 #endif
mbedAustin 11:cada08fc8a70 71
mbedAustin 11:cada08fc8a70 72 /*
mbedAustin 11:cada08fc8a70 73 * Cookies are formed of a 4-bytes timestamp (or serial number) and
mbedAustin 11:cada08fc8a70 74 * an HMAC of timestemp and client ID.
mbedAustin 11:cada08fc8a70 75 */
mbedAustin 11:cada08fc8a70 76 #define COOKIE_LEN ( 4 + COOKIE_HMAC_LEN )
mbedAustin 11:cada08fc8a70 77
mbedAustin 11:cada08fc8a70 78 void mbedtls_ssl_cookie_init( mbedtls_ssl_cookie_ctx *ctx )
mbedAustin 11:cada08fc8a70 79 {
mbedAustin 11:cada08fc8a70 80 mbedtls_md_init( &ctx->hmac_ctx );
mbedAustin 11:cada08fc8a70 81 #if !defined(MBEDTLS_HAVE_TIME)
mbedAustin 11:cada08fc8a70 82 ctx->serial = 0;
mbedAustin 11:cada08fc8a70 83 #endif
mbedAustin 11:cada08fc8a70 84 ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT;
mbedAustin 11:cada08fc8a70 85
mbedAustin 11:cada08fc8a70 86 #if defined(MBEDTLS_THREADING_C)
mbedAustin 11:cada08fc8a70 87 mbedtls_mutex_init( &ctx->mutex );
mbedAustin 11:cada08fc8a70 88 #endif
mbedAustin 11:cada08fc8a70 89 }
mbedAustin 11:cada08fc8a70 90
mbedAustin 11:cada08fc8a70 91 void mbedtls_ssl_cookie_set_timeout( mbedtls_ssl_cookie_ctx *ctx, unsigned long delay )
mbedAustin 11:cada08fc8a70 92 {
mbedAustin 11:cada08fc8a70 93 ctx->timeout = delay;
mbedAustin 11:cada08fc8a70 94 }
mbedAustin 11:cada08fc8a70 95
mbedAustin 11:cada08fc8a70 96 void mbedtls_ssl_cookie_free( mbedtls_ssl_cookie_ctx *ctx )
mbedAustin 11:cada08fc8a70 97 {
mbedAustin 11:cada08fc8a70 98 mbedtls_md_free( &ctx->hmac_ctx );
mbedAustin 11:cada08fc8a70 99
mbedAustin 11:cada08fc8a70 100 #if defined(MBEDTLS_THREADING_C)
mbedAustin 11:cada08fc8a70 101 mbedtls_mutex_init( &ctx->mutex );
mbedAustin 11:cada08fc8a70 102 #endif
mbedAustin 11:cada08fc8a70 103
mbedAustin 11:cada08fc8a70 104 mbedtls_zeroize( ctx, sizeof( mbedtls_ssl_cookie_ctx ) );
mbedAustin 11:cada08fc8a70 105 }
mbedAustin 11:cada08fc8a70 106
mbedAustin 11:cada08fc8a70 107 int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx,
mbedAustin 11:cada08fc8a70 108 int (*f_rng)(void *, unsigned char *, size_t),
mbedAustin 11:cada08fc8a70 109 void *p_rng )
mbedAustin 11:cada08fc8a70 110 {
mbedAustin 11:cada08fc8a70 111 int ret;
mbedAustin 11:cada08fc8a70 112 unsigned char key[COOKIE_MD_OUTLEN];
mbedAustin 11:cada08fc8a70 113
mbedAustin 11:cada08fc8a70 114 if( ( ret = f_rng( p_rng, key, sizeof( key ) ) ) != 0 )
mbedAustin 11:cada08fc8a70 115 return( ret );
mbedAustin 11:cada08fc8a70 116
mbedAustin 11:cada08fc8a70 117 ret = mbedtls_md_setup( &ctx->hmac_ctx, mbedtls_md_info_from_type( COOKIE_MD ), 1 );
mbedAustin 11:cada08fc8a70 118 if( ret != 0 )
mbedAustin 11:cada08fc8a70 119 return( ret );
mbedAustin 11:cada08fc8a70 120
mbedAustin 11:cada08fc8a70 121 ret = mbedtls_md_hmac_starts( &ctx->hmac_ctx, key, sizeof( key ) );
mbedAustin 11:cada08fc8a70 122 if( ret != 0 )
mbedAustin 11:cada08fc8a70 123 return( ret );
mbedAustin 11:cada08fc8a70 124
mbedAustin 11:cada08fc8a70 125 mbedtls_zeroize( key, sizeof( key ) );
mbedAustin 11:cada08fc8a70 126
mbedAustin 11:cada08fc8a70 127 return( 0 );
mbedAustin 11:cada08fc8a70 128 }
mbedAustin 11:cada08fc8a70 129
mbedAustin 11:cada08fc8a70 130 /*
mbedAustin 11:cada08fc8a70 131 * Generate the HMAC part of a cookie
mbedAustin 11:cada08fc8a70 132 */
mbedAustin 11:cada08fc8a70 133 static int ssl_cookie_hmac( mbedtls_md_context_t *hmac_ctx,
mbedAustin 11:cada08fc8a70 134 const unsigned char time[4],
mbedAustin 11:cada08fc8a70 135 unsigned char **p, unsigned char *end,
mbedAustin 11:cada08fc8a70 136 const unsigned char *cli_id, size_t cli_id_len )
mbedAustin 11:cada08fc8a70 137 {
mbedAustin 11:cada08fc8a70 138 unsigned char hmac_out[COOKIE_MD_OUTLEN];
mbedAustin 11:cada08fc8a70 139
mbedAustin 11:cada08fc8a70 140 if( (size_t)( end - *p ) < COOKIE_HMAC_LEN )
mbedAustin 11:cada08fc8a70 141 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
mbedAustin 11:cada08fc8a70 142
mbedAustin 11:cada08fc8a70 143 if( mbedtls_md_hmac_reset( hmac_ctx ) != 0 ||
mbedAustin 11:cada08fc8a70 144 mbedtls_md_hmac_update( hmac_ctx, time, 4 ) != 0 ||
mbedAustin 11:cada08fc8a70 145 mbedtls_md_hmac_update( hmac_ctx, cli_id, cli_id_len ) != 0 ||
mbedAustin 11:cada08fc8a70 146 mbedtls_md_hmac_finish( hmac_ctx, hmac_out ) != 0 )
mbedAustin 11:cada08fc8a70 147 {
mbedAustin 11:cada08fc8a70 148 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
mbedAustin 11:cada08fc8a70 149 }
mbedAustin 11:cada08fc8a70 150
mbedAustin 11:cada08fc8a70 151 memcpy( *p, hmac_out, COOKIE_HMAC_LEN );
mbedAustin 11:cada08fc8a70 152 *p += COOKIE_HMAC_LEN;
mbedAustin 11:cada08fc8a70 153
mbedAustin 11:cada08fc8a70 154 return( 0 );
mbedAustin 11:cada08fc8a70 155 }
mbedAustin 11:cada08fc8a70 156
mbedAustin 11:cada08fc8a70 157 /*
mbedAustin 11:cada08fc8a70 158 * Generate cookie for DTLS ClientHello verification
mbedAustin 11:cada08fc8a70 159 */
mbedAustin 11:cada08fc8a70 160 int mbedtls_ssl_cookie_write( void *p_ctx,
mbedAustin 11:cada08fc8a70 161 unsigned char **p, unsigned char *end,
mbedAustin 11:cada08fc8a70 162 const unsigned char *cli_id, size_t cli_id_len )
mbedAustin 11:cada08fc8a70 163 {
mbedAustin 11:cada08fc8a70 164 int ret;
mbedAustin 11:cada08fc8a70 165 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
mbedAustin 11:cada08fc8a70 166 unsigned long t;
mbedAustin 11:cada08fc8a70 167
mbedAustin 11:cada08fc8a70 168 if( ctx == NULL || cli_id == NULL )
mbedAustin 11:cada08fc8a70 169 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
mbedAustin 11:cada08fc8a70 170
mbedAustin 11:cada08fc8a70 171 if( (size_t)( end - *p ) < COOKIE_LEN )
mbedAustin 11:cada08fc8a70 172 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
mbedAustin 11:cada08fc8a70 173
mbedAustin 11:cada08fc8a70 174 #if defined(MBEDTLS_HAVE_TIME)
mbedAustin 11:cada08fc8a70 175 t = (unsigned long) time( NULL );
mbedAustin 11:cada08fc8a70 176 #else
mbedAustin 11:cada08fc8a70 177 t = ctx->serial++;
mbedAustin 11:cada08fc8a70 178 #endif
mbedAustin 11:cada08fc8a70 179
mbedAustin 11:cada08fc8a70 180 (*p)[0] = (unsigned char)( t >> 24 );
mbedAustin 11:cada08fc8a70 181 (*p)[1] = (unsigned char)( t >> 16 );
mbedAustin 11:cada08fc8a70 182 (*p)[2] = (unsigned char)( t >> 8 );
mbedAustin 11:cada08fc8a70 183 (*p)[3] = (unsigned char)( t );
mbedAustin 11:cada08fc8a70 184 *p += 4;
mbedAustin 11:cada08fc8a70 185
mbedAustin 11:cada08fc8a70 186 #if defined(MBEDTLS_THREADING_C)
mbedAustin 11:cada08fc8a70 187 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
mbedAustin 11:cada08fc8a70 188 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR + ret );
mbedAustin 11:cada08fc8a70 189 #endif
mbedAustin 11:cada08fc8a70 190
mbedAustin 11:cada08fc8a70 191 ret = ssl_cookie_hmac( &ctx->hmac_ctx, *p - 4,
mbedAustin 11:cada08fc8a70 192 p, end, cli_id, cli_id_len );
mbedAustin 11:cada08fc8a70 193
mbedAustin 11:cada08fc8a70 194 #if defined(MBEDTLS_THREADING_C)
mbedAustin 11:cada08fc8a70 195 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
mbedAustin 11:cada08fc8a70 196 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR +
mbedAustin 11:cada08fc8a70 197 MBEDTLS_ERR_THREADING_MUTEX_ERROR );
mbedAustin 11:cada08fc8a70 198 #endif
mbedAustin 11:cada08fc8a70 199
mbedAustin 11:cada08fc8a70 200 return( ret );
mbedAustin 11:cada08fc8a70 201 }
mbedAustin 11:cada08fc8a70 202
mbedAustin 11:cada08fc8a70 203 /*
mbedAustin 11:cada08fc8a70 204 * Check a cookie
mbedAustin 11:cada08fc8a70 205 */
mbedAustin 11:cada08fc8a70 206 int mbedtls_ssl_cookie_check( void *p_ctx,
mbedAustin 11:cada08fc8a70 207 const unsigned char *cookie, size_t cookie_len,
mbedAustin 11:cada08fc8a70 208 const unsigned char *cli_id, size_t cli_id_len )
mbedAustin 11:cada08fc8a70 209 {
mbedAustin 11:cada08fc8a70 210 unsigned char ref_hmac[COOKIE_HMAC_LEN];
mbedAustin 11:cada08fc8a70 211 int ret = 0;
mbedAustin 11:cada08fc8a70 212 unsigned char *p = ref_hmac;
mbedAustin 11:cada08fc8a70 213 mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
mbedAustin 11:cada08fc8a70 214 unsigned long cur_time, cookie_time;
mbedAustin 11:cada08fc8a70 215
mbedAustin 11:cada08fc8a70 216 if( ctx == NULL || cli_id == NULL )
mbedAustin 11:cada08fc8a70 217 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
mbedAustin 11:cada08fc8a70 218
mbedAustin 11:cada08fc8a70 219 if( cookie_len != COOKIE_LEN )
mbedAustin 11:cada08fc8a70 220 return( -1 );
mbedAustin 11:cada08fc8a70 221
mbedAustin 11:cada08fc8a70 222 #if defined(MBEDTLS_THREADING_C)
mbedAustin 11:cada08fc8a70 223 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
mbedAustin 11:cada08fc8a70 224 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR + ret );
mbedAustin 11:cada08fc8a70 225 #endif
mbedAustin 11:cada08fc8a70 226
mbedAustin 11:cada08fc8a70 227 if( ssl_cookie_hmac( &ctx->hmac_ctx, cookie,
mbedAustin 11:cada08fc8a70 228 &p, p + sizeof( ref_hmac ),
mbedAustin 11:cada08fc8a70 229 cli_id, cli_id_len ) != 0 )
mbedAustin 11:cada08fc8a70 230 ret = -1;
mbedAustin 11:cada08fc8a70 231
mbedAustin 11:cada08fc8a70 232 #if defined(MBEDTLS_THREADING_C)
mbedAustin 11:cada08fc8a70 233 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
mbedAustin 11:cada08fc8a70 234 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR +
mbedAustin 11:cada08fc8a70 235 MBEDTLS_ERR_THREADING_MUTEX_ERROR );
mbedAustin 11:cada08fc8a70 236 #endif
mbedAustin 11:cada08fc8a70 237
mbedAustin 11:cada08fc8a70 238 if( ret != 0 )
mbedAustin 11:cada08fc8a70 239 return( ret );
mbedAustin 11:cada08fc8a70 240
mbedAustin 11:cada08fc8a70 241 if( mbedtls_ssl_safer_memcmp( cookie + 4, ref_hmac, sizeof( ref_hmac ) ) != 0 )
mbedAustin 11:cada08fc8a70 242 return( -1 );
mbedAustin 11:cada08fc8a70 243
mbedAustin 11:cada08fc8a70 244 #if defined(MBEDTLS_HAVE_TIME)
mbedAustin 11:cada08fc8a70 245 cur_time = (unsigned long) time( NULL );
mbedAustin 11:cada08fc8a70 246 #else
mbedAustin 11:cada08fc8a70 247 cur_time = ctx->serial;
mbedAustin 11:cada08fc8a70 248 #endif
mbedAustin 11:cada08fc8a70 249
mbedAustin 11:cada08fc8a70 250 cookie_time = ( (unsigned long) cookie[0] << 24 ) |
mbedAustin 11:cada08fc8a70 251 ( (unsigned long) cookie[1] << 16 ) |
mbedAustin 11:cada08fc8a70 252 ( (unsigned long) cookie[2] << 8 ) |
mbedAustin 11:cada08fc8a70 253 ( (unsigned long) cookie[3] );
mbedAustin 11:cada08fc8a70 254
mbedAustin 11:cada08fc8a70 255 if( ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout )
mbedAustin 11:cada08fc8a70 256 return( -1 );
mbedAustin 11:cada08fc8a70 257
mbedAustin 11:cada08fc8a70 258 return( 0 );
mbedAustin 11:cada08fc8a70 259 }
mbedAustin 11:cada08fc8a70 260 #endif /* MBEDTLS_SSL_COOKIE_C */