Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 1 | /* |
| 2 | * DTLS cookie callbacks implementation |
| 3 | * |
| 4 | * Copyright (C) 2014, Brainspark B.V. |
| 5 | * |
| 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 8 | * |
| 9 | * All rights reserved. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | */ |
| 25 | /* |
| 26 | * These session callbacks use a simple chained list |
| 27 | * to store and retrieve the session information. |
| 28 | */ |
| 29 | |
| 30 | #if !defined(POLARSSL_CONFIG_FILE) |
| 31 | #include "polarssl/config.h" |
| 32 | #else |
| 33 | #include POLARSSL_CONFIG_FILE |
| 34 | #endif |
| 35 | |
Manuel Pégourié-Gonnard | a64acd4 | 2014-07-23 18:30:45 +0200 | [diff] [blame] | 36 | #if defined(POLARSSL_SSL_COOKIE_C) |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 37 | |
| 38 | #include "polarssl/ssl_cookie.h" |
| 39 | |
| 40 | #if defined(POLARSSL_PLATFORM_C) |
| 41 | #include "polarssl/platform.h" |
| 42 | #else |
| 43 | #define polarssl_malloc malloc |
| 44 | #define polarssl_free free |
| 45 | #endif |
| 46 | |
| 47 | /* Implementation that should never be optimized out by the compiler */ |
| 48 | static void polarssl_zeroize( void *v, size_t n ) { |
| 49 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | * If DTLS is in use, then at least one of SHA-1, SHA-256, SHA-512 is |
| 54 | * available. Try SHA-256 first, 512 wastes resources since we need to stay |
| 55 | * with max 32 bytes of cookie for DTLS 1.0 |
| 56 | */ |
| 57 | #if defined(POLARSSL_SHA256_C) |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame^] | 58 | #define COOKIE_MD POLARSSL_MD_SHA224 |
| 59 | #define COOKIE_MD_OUTLEN 32 |
| 60 | #define COOKIE_HMAC_LEN 28 |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 61 | #elif defined(POLARSSL_SHA512_C) |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame^] | 62 | #define COOKIE_MD POLARSSL_MD_SHA384 |
| 63 | #define COOKIE_MD_OUTLEN 48 |
| 64 | #define COOKIE_HMAC_LEN 28 |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 65 | #elif defined(POLARSSL_SHA1_C) |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame^] | 66 | #define COOKIE_MD POLARSSL_MD_SHA1 |
| 67 | #define COOKIE_MD_OUTLEN 20 |
| 68 | #define COOKIE_HMAC_LEN 20 |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 69 | #else |
| 70 | #error "DTLS hello verify needs SHA-1 or SHA-2" |
| 71 | #endif |
| 72 | |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame^] | 73 | /* |
| 74 | * Cookies are formed of a 4-bytes timestamp (or serial number) and |
| 75 | * an HMAC of timestemp and client ID. |
| 76 | */ |
| 77 | #define COOKIE_LEN ( 4 + COOKIE_HMAC_LEN ) |
| 78 | |
| 79 | #define COOKIE_TIMEOUT 60 |
| 80 | |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 81 | void ssl_cookie_init( ssl_cookie_ctx *ctx ) |
| 82 | { |
| 83 | md_init( &ctx->hmac_ctx ); |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame^] | 84 | #if !defined(POLARSSL_HAVE_TIME) |
| 85 | ctx->serial = 0; |
| 86 | #endif |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void ssl_cookie_free( ssl_cookie_ctx *ctx ) |
| 90 | { |
| 91 | md_free( &ctx->hmac_ctx ); |
| 92 | } |
| 93 | |
| 94 | int ssl_cookie_setup( ssl_cookie_ctx *ctx, |
| 95 | int (*f_rng)(void *, unsigned char *, size_t), |
| 96 | void *p_rng ) |
| 97 | { |
| 98 | int ret; |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame^] | 99 | unsigned char key[COOKIE_MD_OUTLEN]; |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 100 | |
| 101 | if( ( ret = f_rng( p_rng, key, sizeof( key ) ) ) != 0 ) |
| 102 | return( ret ); |
| 103 | |
Manuel Pégourié-Gonnard | 445a1ec | 2014-07-23 20:48:05 +0200 | [diff] [blame] | 104 | ret = md_init_ctx( &ctx->hmac_ctx, md_info_from_type( COOKIE_MD ) ); |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 105 | if( ret != 0 ) |
| 106 | return( ret ); |
| 107 | |
| 108 | ret = md_hmac_starts( &ctx->hmac_ctx, key, sizeof( key ) ); |
| 109 | if( ret != 0 ) |
| 110 | return( ret ); |
| 111 | |
| 112 | polarssl_zeroize( key, sizeof( key ) ); |
| 113 | |
| 114 | return( 0 ); |
| 115 | } |
| 116 | |
| 117 | /* |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame^] | 118 | * Generate the HMAC part of a cookie |
| 119 | */ |
| 120 | static int ssl_cookie_hmac( md_context_t *hmac_ctx, |
| 121 | const unsigned char time[4], |
| 122 | unsigned char **p, unsigned char *end, |
| 123 | const unsigned char *cli_id, size_t cli_id_len ) |
| 124 | { |
| 125 | int ret; |
| 126 | unsigned char hmac_out[COOKIE_MD_OUTLEN]; |
| 127 | |
| 128 | if( (size_t)( end - *p ) < COOKIE_HMAC_LEN ) |
| 129 | return( POLARSSL_ERR_SSL_BAD_INPUT_DATA ); |
| 130 | |
| 131 | if( ( ret = md_hmac_reset( hmac_ctx ) ) != 0 || |
| 132 | ( ret = md_hmac_update( hmac_ctx, time, 4 ) ) != 0 || |
| 133 | ( ret = md_hmac_update( hmac_ctx, cli_id, cli_id_len ) ) != 0 || |
| 134 | ( ret = md_hmac_finish( hmac_ctx, hmac_out ) ) != 0 ) |
| 135 | { |
| 136 | return( POLARSSL_ERR_SSL_INTERNAL_ERROR ); |
| 137 | } |
| 138 | |
| 139 | memcpy( *p, hmac_out, COOKIE_HMAC_LEN ); |
| 140 | *p += COOKIE_HMAC_LEN; |
| 141 | |
| 142 | return( 0 ); |
| 143 | } |
| 144 | |
| 145 | /* |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 146 | * Generate cookie for DTLS ClientHello verification |
| 147 | */ |
Manuel Pégourié-Gonnard | e4de061 | 2014-07-23 17:26:48 +0200 | [diff] [blame] | 148 | int ssl_cookie_write( void *p_ctx, |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 149 | unsigned char **p, unsigned char *end, |
| 150 | const unsigned char *cli_id, size_t cli_id_len ) |
| 151 | { |
Manuel Pégourié-Gonnard | e4de061 | 2014-07-23 17:26:48 +0200 | [diff] [blame] | 152 | ssl_cookie_ctx *ctx = (ssl_cookie_ctx *) p_ctx; |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame^] | 153 | unsigned long t; |
Manuel Pégourié-Gonnard | e4de061 | 2014-07-23 17:26:48 +0200 | [diff] [blame] | 154 | |
Manuel Pégourié-Gonnard | 29ad7e8 | 2014-07-23 19:12:15 +0200 | [diff] [blame] | 155 | if( ctx == NULL || cli_id == NULL ) |
Manuel Pégourié-Gonnard | e4de061 | 2014-07-23 17:26:48 +0200 | [diff] [blame] | 156 | return( POLARSSL_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 157 | |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame^] | 158 | if( (size_t)( end - *p ) < COOKIE_LEN ) |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 159 | return( POLARSSL_ERR_SSL_BAD_INPUT_DATA ); |
| 160 | |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame^] | 161 | #if defined(POLARSSL_HAVE_TIME) |
| 162 | t = (unsigned long) time( NULL ); |
| 163 | #else |
| 164 | t = ctx->serial++; |
| 165 | #endif |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 166 | |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame^] | 167 | (*p)[0] = (unsigned char)( t >> 24 ); |
| 168 | (*p)[1] = (unsigned char)( t >> 16 ); |
| 169 | (*p)[2] = (unsigned char)( t >> 8 ); |
| 170 | (*p)[3] = (unsigned char)( t ); |
| 171 | *p += 4; |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 172 | |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame^] | 173 | return( ssl_cookie_hmac( &ctx->hmac_ctx, *p - 4, |
| 174 | p, end, cli_id, cli_id_len ) ); |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /* |
| 178 | * Check a cookie |
| 179 | */ |
Manuel Pégourié-Gonnard | e4de061 | 2014-07-23 17:26:48 +0200 | [diff] [blame] | 180 | int ssl_cookie_check( void *p_ctx, |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 181 | const unsigned char *cookie, size_t cookie_len, |
| 182 | const unsigned char *cli_id, size_t cli_id_len ) |
| 183 | { |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame^] | 184 | unsigned char ref_hmac[COOKIE_HMAC_LEN]; |
| 185 | unsigned char *p = ref_hmac; |
| 186 | ssl_cookie_ctx *ctx = (ssl_cookie_ctx *) p_ctx; |
| 187 | unsigned long cur_time, cookie_time; |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 188 | |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame^] | 189 | if( ctx == NULL || cli_id == NULL ) |
| 190 | return( POLARSSL_ERR_SSL_BAD_INPUT_DATA ); |
| 191 | |
| 192 | if( cookie_len != COOKIE_LEN ) |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 193 | return( -1 ); |
| 194 | |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame^] | 195 | if( ssl_cookie_hmac( &ctx->hmac_ctx, cookie, |
| 196 | &p, p + sizeof( ref_hmac ), |
| 197 | cli_id, cli_id_len ) != 0 ) |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 198 | return( -1 ); |
| 199 | |
Manuel Pégourié-Gonnard | e903081 | 2014-07-23 21:29:11 +0200 | [diff] [blame^] | 200 | if( safer_memcmp( cookie + 4, ref_hmac, sizeof( ref_hmac ) ) != 0 ) |
| 201 | return( -1 ); |
| 202 | |
| 203 | #if defined(POLARSSL_HAVE_TIME) |
| 204 | cur_time = (unsigned long) time( NULL ); |
| 205 | #else |
| 206 | cur_time = ctx->serial; |
| 207 | #endif |
| 208 | |
| 209 | cookie_time = ( (unsigned long) cookie[0] << 24 ) | |
| 210 | ( (unsigned long) cookie[1] << 16 ) | |
| 211 | ( (unsigned long) cookie[2] << 8 ) | |
| 212 | ( (unsigned long) cookie[3] ); |
| 213 | |
| 214 | if( cur_time - cookie_time > COOKIE_TIMEOUT ) |
Manuel Pégourié-Gonnard | 232edd4 | 2014-07-23 16:56:27 +0200 | [diff] [blame] | 215 | return( -1 ); |
| 216 | |
| 217 | return( 0 ); |
| 218 | } |
| 219 | #endif /* POLARSSL_SSL_COOKIE_C */ |