This is a fork due to permission issues

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of 6_songs-from-the-cloud by MakingMusicWorkshop

Committer:
maclobdell
Date:
Wed May 18 19:06:32 2016 +0000
Revision:
0:f7c60d3e7b8a
clean version

Who changed what in which revision?

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