Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

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