joey shelton / LED_Demo

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ssl_cookie.c Source File

ssl_cookie.c

00001 /*
00002  *  DTLS cookie callbacks implementation
00003  *
00004  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
00005  *  SPDX-License-Identifier: Apache-2.0
00006  *
00007  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00008  *  not use this file except in compliance with the License.
00009  *  You may obtain a copy of the License at
00010  *
00011  *  http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  *  Unless required by applicable law or agreed to in writing, software
00014  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00015  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  *  See the License for the specific language governing permissions and
00017  *  limitations under the License.
00018  *
00019  *  This file is part of mbed TLS (https://tls.mbed.org)
00020  */
00021 /*
00022  * These session callbacks use a simple chained list
00023  * to store and retrieve the session information.
00024  */
00025 
00026 #if !defined(MBEDTLS_CONFIG_FILE)
00027 #include "mbedtls/config.h"
00028 #else
00029 #include MBEDTLS_CONFIG_FILE
00030 #endif
00031 
00032 #if defined(MBEDTLS_SSL_COOKIE_C)
00033 
00034 #if defined(MBEDTLS_PLATFORM_C)
00035 #include "mbedtls/platform.h"
00036 #else
00037 #define mbedtls_calloc    calloc
00038 #define mbedtls_free      free
00039 #define mbedtls_time      time
00040 #define mbedtls_time_t    time_t
00041 #endif
00042 
00043 #include "mbedtls/ssl_cookie.h"
00044 #include "mbedtls/ssl_internal.h"
00045 
00046 #include <string.h>
00047 
00048 /* Implementation that should never be optimized out by the compiler */
00049 static void mbedtls_zeroize( void *v, size_t n ) {
00050     volatile unsigned char *p = v; while( n-- ) *p++ = 0;
00051 }
00052 
00053 /*
00054  * If DTLS is in use, then at least one of SHA-1, SHA-256, SHA-512 is
00055  * available. Try SHA-256 first, 512 wastes resources since we need to stay
00056  * with max 32 bytes of cookie for DTLS 1.0
00057  */
00058 #if defined(MBEDTLS_SHA256_C)
00059 #define COOKIE_MD           MBEDTLS_MD_SHA224
00060 #define COOKIE_MD_OUTLEN    32
00061 #define COOKIE_HMAC_LEN     28
00062 #elif defined(MBEDTLS_SHA512_C)
00063 #define COOKIE_MD           MBEDTLS_MD_SHA384
00064 #define COOKIE_MD_OUTLEN    48
00065 #define COOKIE_HMAC_LEN     28
00066 #elif defined(MBEDTLS_SHA1_C)
00067 #define COOKIE_MD           MBEDTLS_MD_SHA1
00068 #define COOKIE_MD_OUTLEN    20
00069 #define COOKIE_HMAC_LEN     20
00070 #else
00071 #error "DTLS hello verify needs SHA-1 or SHA-2"
00072 #endif
00073 
00074 /*
00075  * Cookies are formed of a 4-bytes timestamp (or serial number) and
00076  * an HMAC of timestemp and client ID.
00077  */
00078 #define COOKIE_LEN      ( 4 + COOKIE_HMAC_LEN )
00079 
00080 void mbedtls_ssl_cookie_init( mbedtls_ssl_cookie_ctx *ctx )
00081 {
00082     mbedtls_md_init( &ctx->hmac_ctx  );
00083 #if !defined(MBEDTLS_HAVE_TIME)
00084     ctx->serial  = 0;
00085 #endif
00086     ctx->timeout  = MBEDTLS_SSL_COOKIE_TIMEOUT;
00087 
00088 #if defined(MBEDTLS_THREADING_C)
00089     mbedtls_mutex_init( &ctx->mutex );
00090 #endif
00091 }
00092 
00093 void mbedtls_ssl_cookie_set_timeout( mbedtls_ssl_cookie_ctx *ctx, unsigned long delay )
00094 {
00095     ctx->timeout  = delay;
00096 }
00097 
00098 void mbedtls_ssl_cookie_free( mbedtls_ssl_cookie_ctx *ctx )
00099 {
00100     mbedtls_md_free( &ctx->hmac_ctx  );
00101 
00102 #if defined(MBEDTLS_THREADING_C)
00103     mbedtls_mutex_init( &ctx->mutex );
00104 #endif
00105 
00106     mbedtls_zeroize( ctx, sizeof( mbedtls_ssl_cookie_ctx ) );
00107 }
00108 
00109 int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx,
00110                       int (*f_rng)(void *, unsigned char *, size_t),
00111                       void *p_rng )
00112 {
00113     int ret;
00114     unsigned char key[COOKIE_MD_OUTLEN];
00115 
00116     if( ( ret = f_rng( p_rng, key, sizeof( key ) ) ) != 0 )
00117         return( ret );
00118 
00119     ret = mbedtls_md_setup( &ctx->hmac_ctx , mbedtls_md_info_from_type( COOKIE_MD ), 1 );
00120     if( ret != 0 )
00121         return( ret );
00122 
00123     ret = mbedtls_md_hmac_starts( &ctx->hmac_ctx , key, sizeof( key ) );
00124     if( ret != 0 )
00125         return( ret );
00126 
00127     mbedtls_zeroize( key, sizeof( key ) );
00128 
00129     return( 0 );
00130 }
00131 
00132 /*
00133  * Generate the HMAC part of a cookie
00134  */
00135 static int ssl_cookie_hmac( mbedtls_md_context_t *hmac_ctx,
00136                             const unsigned char time[4],
00137                             unsigned char **p, unsigned char *end,
00138                             const unsigned char *cli_id, size_t cli_id_len )
00139 {
00140     unsigned char hmac_out[COOKIE_MD_OUTLEN];
00141 
00142     if( (size_t)( end - *p ) < COOKIE_HMAC_LEN )
00143         return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
00144 
00145     if( mbedtls_md_hmac_reset(  hmac_ctx ) != 0 ||
00146         mbedtls_md_hmac_update( hmac_ctx, time, 4 ) != 0 ||
00147         mbedtls_md_hmac_update( hmac_ctx, cli_id, cli_id_len ) != 0 ||
00148         mbedtls_md_hmac_finish( hmac_ctx, hmac_out ) != 0 )
00149     {
00150         return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
00151     }
00152 
00153     memcpy( *p, hmac_out, COOKIE_HMAC_LEN );
00154     *p += COOKIE_HMAC_LEN;
00155 
00156     return( 0 );
00157 }
00158 
00159 /*
00160  * Generate cookie for DTLS ClientHello verification
00161  */
00162 int mbedtls_ssl_cookie_write( void *p_ctx,
00163                       unsigned char **p, unsigned char *end,
00164                       const unsigned char *cli_id, size_t cli_id_len )
00165 {
00166     int ret;
00167     mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
00168     unsigned long t;
00169 
00170     if( ctx == NULL || cli_id == NULL )
00171         return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
00172 
00173     if( (size_t)( end - *p ) < COOKIE_LEN )
00174         return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
00175 
00176 #if defined(MBEDTLS_HAVE_TIME)
00177     t = (unsigned long) mbedtls_time( NULL );
00178 #else
00179     t = ctx->serial ++;
00180 #endif
00181 
00182     (*p)[0] = (unsigned char)( t >> 24 );
00183     (*p)[1] = (unsigned char)( t >> 16 );
00184     (*p)[2] = (unsigned char)( t >>  8 );
00185     (*p)[3] = (unsigned char)( t       );
00186     *p += 4;
00187 
00188 #if defined(MBEDTLS_THREADING_C)
00189     if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
00190         return( MBEDTLS_ERR_SSL_INTERNAL_ERROR + ret );
00191 #endif
00192 
00193     ret = ssl_cookie_hmac( &ctx->hmac_ctx , *p - 4,
00194                            p, end, cli_id, cli_id_len );
00195 
00196 #if defined(MBEDTLS_THREADING_C)
00197     if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
00198         return( MBEDTLS_ERR_SSL_INTERNAL_ERROR +
00199                 MBEDTLS_ERR_THREADING_MUTEX_ERROR );
00200 #endif
00201 
00202     return( ret );
00203 }
00204 
00205 /*
00206  * Check a cookie
00207  */
00208 int mbedtls_ssl_cookie_check( void *p_ctx,
00209                       const unsigned char *cookie, size_t cookie_len,
00210                       const unsigned char *cli_id, size_t cli_id_len )
00211 {
00212     unsigned char ref_hmac[COOKIE_HMAC_LEN];
00213     int ret = 0;
00214     unsigned char *p = ref_hmac;
00215     mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
00216     unsigned long cur_time, cookie_time;
00217 
00218     if( ctx == NULL || cli_id == NULL )
00219         return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
00220 
00221     if( cookie_len != COOKIE_LEN )
00222         return( -1 );
00223 
00224 #if defined(MBEDTLS_THREADING_C)
00225     if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
00226         return( MBEDTLS_ERR_SSL_INTERNAL_ERROR + ret );
00227 #endif
00228 
00229     if( ssl_cookie_hmac( &ctx->hmac_ctx , cookie,
00230                          &p, p + sizeof( ref_hmac ),
00231                          cli_id, cli_id_len ) != 0 )
00232         ret = -1;
00233 
00234 #if defined(MBEDTLS_THREADING_C)
00235     if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
00236         return( MBEDTLS_ERR_SSL_INTERNAL_ERROR +
00237                 MBEDTLS_ERR_THREADING_MUTEX_ERROR );
00238 #endif
00239 
00240     if( ret != 0 )
00241         return( ret );
00242 
00243     if( mbedtls_ssl_safer_memcmp( cookie + 4, ref_hmac, sizeof( ref_hmac ) ) != 0 )
00244         return( -1 );
00245 
00246 #if defined(MBEDTLS_HAVE_TIME)
00247     cur_time = (unsigned long) mbedtls_time( NULL );
00248 #else
00249     cur_time = ctx->serial ;
00250 #endif
00251 
00252     cookie_time = ( (unsigned long) cookie[0] << 24 ) |
00253                   ( (unsigned long) cookie[1] << 16 ) |
00254                   ( (unsigned long) cookie[2] <<  8 ) |
00255                   ( (unsigned long) cookie[3]       );
00256 
00257     if( ctx->timeout  != 0 && cur_time - cookie_time > ctx->timeout  )
00258         return( -1 );
00259 
00260     return( 0 );
00261 }
00262 #endif /* MBEDTLS_SSL_COOKIE_C */