| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#include "common.h" |
| 25 |
|
| 26 |
#if defined(MBEDTLS_SSL_COOKIE_C) |
| 27 |
|
| 28 |
#if defined(MBEDTLS_PLATFORM_C) |
| 29 |
#include "mbedtls/platform.h" |
| 30 |
#else |
| 31 |
#define mbedtls_calloc calloc |
| 32 |
#define mbedtls_free free |
| 33 |
#endif |
| 34 |
|
| 35 |
#include "mbedtls/ssl_cookie.h" |
| 36 |
#include "mbedtls/ssl_internal.h" |
| 37 |
#include "mbedtls/error.h" |
| 38 |
#include "mbedtls/platform_util.h" |
| 39 |
#include "mbedtls/constant_time.h" |
| 40 |
|
| 41 |
#include <string.h> |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
#if defined(MBEDTLS_SHA256_C) |
| 49 |
#define COOKIE_MD MBEDTLS_MD_SHA224 |
| 50 |
#define COOKIE_MD_OUTLEN 32 |
| 51 |
#define COOKIE_HMAC_LEN 28 |
| 52 |
#elif defined(MBEDTLS_SHA512_C) |
| 53 |
#define COOKIE_MD MBEDTLS_MD_SHA384 |
| 54 |
#define COOKIE_MD_OUTLEN 48 |
| 55 |
#define COOKIE_HMAC_LEN 28 |
| 56 |
#elif defined(MBEDTLS_SHA1_C) |
| 57 |
#define COOKIE_MD MBEDTLS_MD_SHA1 |
| 58 |
#define COOKIE_MD_OUTLEN 20 |
| 59 |
#define COOKIE_HMAC_LEN 20 |
| 60 |
#else |
| 61 |
#error "DTLS hello verify needs SHA-1 or SHA-2" |
| 62 |
#endif |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
#define COOKIE_LEN ( 4 + COOKIE_HMAC_LEN ) |
| 69 |
|
| 70 |
void mbedtls_ssl_cookie_init( mbedtls_ssl_cookie_ctx *ctx ) |
| 71 |
{ |
| 72 |
mbedtls_md_init( &ctx->hmac_ctx ); |
| 73 |
#if !defined(MBEDTLS_HAVE_TIME) |
| 74 |
ctx->serial = 0; |
| 75 |
#endif |
| 76 |
ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT; |
| 77 |
|
| 78 |
#if defined(MBEDTLS_THREADING_C) |
| 79 |
mbedtls_mutex_init( &ctx->mutex ); |
| 80 |
#endif |
| 81 |
} |
| 82 |
|
| 83 |
void mbedtls_ssl_cookie_set_timeout( mbedtls_ssl_cookie_ctx *ctx, unsigned long delay ) |
| 84 |
{ |
| 85 |
ctx->timeout = delay; |
| 86 |
} |
| 87 |
|
| 88 |
void mbedtls_ssl_cookie_free( mbedtls_ssl_cookie_ctx *ctx ) |
| 89 |
{ |
| 90 |
mbedtls_md_free( &ctx->hmac_ctx ); |
| 91 |
|
| 92 |
#if defined(MBEDTLS_THREADING_C) |
| 93 |
mbedtls_mutex_free( &ctx->mutex ); |
| 94 |
#endif |
| 95 |
|
| 96 |
mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_cookie_ctx ) ); |
| 97 |
} |
| 98 |
|
| 99 |
int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx, |
| 100 |
int (*f_rng)(void *, unsigned char *, size_t), |
| 101 |
void *p_rng ) |
| 102 |
{ |
| 103 |
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 104 |
unsigned char key[COOKIE_MD_OUTLEN]; |
| 105 |
|
| 106 |
if( ( ret = f_rng( p_rng, key, sizeof( key ) ) ) != 0 ) |
| 107 |
return( ret ); |
| 108 |
|
| 109 |
ret = mbedtls_md_setup( &ctx->hmac_ctx, mbedtls_md_info_from_type( COOKIE_MD ), 1 ); |
| 110 |
if( ret != 0 ) |
| 111 |
return( ret ); |
| 112 |
|
| 113 |
ret = mbedtls_md_hmac_starts( &ctx->hmac_ctx, key, sizeof( key ) ); |
| 114 |
if( ret != 0 ) |
| 115 |
return( ret ); |
| 116 |
|
| 117 |
mbedtls_platform_zeroize( key, sizeof( key ) ); |
| 118 |
|
| 119 |
return( 0 ); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
static int ssl_cookie_hmac( mbedtls_md_context_t *hmac_ctx, |
| 126 |
const unsigned char time[4], |
| 127 |
unsigned char **p, unsigned char *end, |
| 128 |
const unsigned char *cli_id, size_t cli_id_len ) |
| 129 |
{ |
| 130 |
unsigned char hmac_out[COOKIE_MD_OUTLEN]; |
| 131 |
|
| 132 |
MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_HMAC_LEN ); |
| 133 |
|
| 134 |
if( mbedtls_md_hmac_reset( hmac_ctx ) != 0 || |
| 135 |
mbedtls_md_hmac_update( hmac_ctx, time, 4 ) != 0 || |
| 136 |
mbedtls_md_hmac_update( hmac_ctx, cli_id, cli_id_len ) != 0 || |
| 137 |
mbedtls_md_hmac_finish( hmac_ctx, hmac_out ) != 0 ) |
| 138 |
{ |
| 139 |
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 140 |
} |
| 141 |
|
| 142 |
memcpy( *p, hmac_out, COOKIE_HMAC_LEN ); |
| 143 |
*p += COOKIE_HMAC_LEN; |
| 144 |
|
| 145 |
return( 0 ); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
int mbedtls_ssl_cookie_write( void *p_ctx, |
| 152 |
unsigned char **p, unsigned char *end, |
| 153 |
const unsigned char *cli_id, size_t cli_id_len ) |
| 154 |
{ |
| 155 |
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 156 |
mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx; |
| 157 |
unsigned long t; |
| 158 |
|
| 159 |
if( ctx == NULL || cli_id == NULL ) |
| 160 |
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 161 |
|
| 162 |
MBEDTLS_SSL_CHK_BUF_PTR( *p, end, COOKIE_LEN ); |
| 163 |
|
| 164 |
#if defined(MBEDTLS_HAVE_TIME) |
| 165 |
t = (unsigned long) mbedtls_time( NULL ); |
| 166 |
#else |
| 167 |
t = ctx->serial++; |
| 168 |
#endif |
| 169 |
|
| 170 |
MBEDTLS_PUT_UINT32_BE(t, *p, 0); |
| 171 |
*p += 4; |
| 172 |
|
| 173 |
#if defined(MBEDTLS_THREADING_C) |
| 174 |
if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 175 |
return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret ) ); |
| 176 |
#endif |
| 177 |
|
| 178 |
ret = ssl_cookie_hmac( &ctx->hmac_ctx, *p - 4, |
| 179 |
p, end, cli_id, cli_id_len ); |
| 180 |
|
| 181 |
#if defined(MBEDTLS_THREADING_C) |
| 182 |
if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
| 183 |
return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR, |
| 184 |
MBEDTLS_ERR_THREADING_MUTEX_ERROR ) ); |
| 185 |
#endif |
| 186 |
|
| 187 |
return( ret ); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
int mbedtls_ssl_cookie_check( void *p_ctx, |
| 194 |
const unsigned char *cookie, size_t cookie_len, |
| 195 |
const unsigned char *cli_id, size_t cli_id_len ) |
| 196 |
{ |
| 197 |
unsigned char ref_hmac[COOKIE_HMAC_LEN]; |
| 198 |
int ret = 0; |
| 199 |
unsigned char *p = ref_hmac; |
| 200 |
mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx; |
| 201 |
unsigned long cur_time, cookie_time; |
| 202 |
|
| 203 |
if( ctx == NULL || cli_id == NULL ) |
| 204 |
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 205 |
|
| 206 |
if( cookie_len != COOKIE_LEN ) |
| 207 |
return( -1 ); |
| 208 |
|
| 209 |
#if defined(MBEDTLS_THREADING_C) |
| 210 |
if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 211 |
return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret ) ); |
| 212 |
#endif |
| 213 |
|
| 214 |
if( ssl_cookie_hmac( &ctx->hmac_ctx, cookie, |
| 215 |
&p, p + sizeof( ref_hmac ), |
| 216 |
cli_id, cli_id_len ) != 0 ) |
| 217 |
ret = -1; |
| 218 |
|
| 219 |
#if defined(MBEDTLS_THREADING_C) |
| 220 |
if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
| 221 |
{ |
| 222 |
ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_SSL_INTERNAL_ERROR, |
| 223 |
MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
| 224 |
} |
| 225 |
#endif |
| 226 |
|
| 227 |
if( ret != 0 ) |
| 228 |
goto exit; |
| 229 |
|
| 230 |
if( mbedtls_ct_memcmp( cookie + 4, ref_hmac, sizeof( ref_hmac ) ) != 0 ) |
| 231 |
{ |
| 232 |
ret = -1; |
| 233 |
goto exit; |
| 234 |
} |
| 235 |
|
| 236 |
#if defined(MBEDTLS_HAVE_TIME) |
| 237 |
cur_time = (unsigned long) mbedtls_time( NULL ); |
| 238 |
#else |
| 239 |
cur_time = ctx->serial; |
| 240 |
#endif |
| 241 |
|
| 242 |
cookie_time = ( (unsigned long) cookie[0] << 24 ) | |
| 243 |
( (unsigned long) cookie[1] << 16 ) | |
| 244 |
( (unsigned long) cookie[2] << 8 ) | |
| 245 |
( (unsigned long) cookie[3] ); |
| 246 |
|
| 247 |
if( ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout ) |
| 248 |
{ |
| 249 |
ret = -1; |
| 250 |
goto exit; |
| 251 |
} |
| 252 |
|
| 253 |
exit: |
| 254 |
mbedtls_platform_zeroize( ref_hmac, sizeof( ref_hmac ) ); |
| 255 |
return( ret ); |
| 256 |
} |
| 257 |
#endif |