Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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