mbed TLS Build

Dependents:   Slave-prot-prod

Committer:
markrad
Date:
Thu Jan 05 00:18:44 2017 +0000
Revision:
0:cdf462088d13
Initial commit

Who changed what in which revision?

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