Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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