mbedtls ported to mbed-classic

Fork of mbedtls by Christopher Haster

Committer:
Christopher Haster
Date:
Fri Jan 22 16:44:49 2016 -0600
Revision:
1:24750b9ad5ef
Initial move of mbedtls to mercurial

Who changed what in which revision?

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