blob: c090b32b2965d6006cb9e427d8ec408907f50765 [file] [log] [blame]
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +02001/*
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é-Gonnarda64acd42014-07-23 18:30:45 +020036#if defined(POLARSSL_SSL_COOKIE_C)
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020037
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 */
48static 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é-Gonnarde9030812014-07-23 21:29:11 +020058#define COOKIE_MD POLARSSL_MD_SHA224
59#define COOKIE_MD_OUTLEN 32
60#define COOKIE_HMAC_LEN 28
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020061#elif defined(POLARSSL_SHA512_C)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020062#define COOKIE_MD POLARSSL_MD_SHA384
63#define COOKIE_MD_OUTLEN 48
64#define COOKIE_HMAC_LEN 28
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020065#elif defined(POLARSSL_SHA1_C)
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020066#define COOKIE_MD POLARSSL_MD_SHA1
67#define COOKIE_MD_OUTLEN 20
68#define COOKIE_HMAC_LEN 20
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020069#else
70#error "DTLS hello verify needs SHA-1 or SHA-2"
71#endif
72
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020073/*
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
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020079void ssl_cookie_init( ssl_cookie_ctx *ctx )
80{
81 md_init( &ctx->hmac_ctx );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020082#if !defined(POLARSSL_HAVE_TIME)
83 ctx->serial = 0;
84#endif
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +020085 ctx->timeout = POLARSSL_SSL_COOKIE_TIMEOUT;
86}
87
88void ssl_cookie_set_timeout( ssl_cookie_ctx *ctx, unsigned long delay )
89{
90 ctx->timeout = delay;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020091}
92
93void ssl_cookie_free( ssl_cookie_ctx *ctx )
94{
95 md_free( &ctx->hmac_ctx );
96}
97
98int ssl_cookie_setup( ssl_cookie_ctx *ctx,
99 int (*f_rng)(void *, unsigned char *, size_t),
100 void *p_rng )
101{
102 int ret;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200103 unsigned char key[COOKIE_MD_OUTLEN];
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200104
105 if( ( ret = f_rng( p_rng, key, sizeof( key ) ) ) != 0 )
106 return( ret );
107
Manuel Pégourié-Gonnard445a1ec2014-07-23 20:48:05 +0200108 ret = md_init_ctx( &ctx->hmac_ctx, md_info_from_type( COOKIE_MD ) );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200109 if( ret != 0 )
110 return( ret );
111
112 ret = md_hmac_starts( &ctx->hmac_ctx, key, sizeof( key ) );
113 if( ret != 0 )
114 return( ret );
115
116 polarssl_zeroize( key, sizeof( key ) );
117
118 return( 0 );
119}
120
121/*
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200122 * Generate the HMAC part of a cookie
123 */
124static int ssl_cookie_hmac( md_context_t *hmac_ctx,
125 const unsigned char time[4],
126 unsigned char **p, unsigned char *end,
127 const unsigned char *cli_id, size_t cli_id_len )
128{
129 int ret;
130 unsigned char hmac_out[COOKIE_MD_OUTLEN];
131
132 if( (size_t)( end - *p ) < COOKIE_HMAC_LEN )
Manuel Pégourié-Gonnard562eb782014-07-23 23:41:53 +0200133 return( POLARSSL_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200134
135 if( ( ret = md_hmac_reset( hmac_ctx ) ) != 0 ||
136 ( ret = md_hmac_update( hmac_ctx, time, 4 ) ) != 0 ||
137 ( ret = md_hmac_update( hmac_ctx, cli_id, cli_id_len ) ) != 0 ||
138 ( ret = md_hmac_finish( hmac_ctx, hmac_out ) ) != 0 )
139 {
140 return( POLARSSL_ERR_SSL_INTERNAL_ERROR );
141 }
142
143 memcpy( *p, hmac_out, COOKIE_HMAC_LEN );
144 *p += COOKIE_HMAC_LEN;
145
146 return( 0 );
147}
148
149/*
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200150 * Generate cookie for DTLS ClientHello verification
151 */
Manuel Pégourié-Gonnarde4de0612014-07-23 17:26:48 +0200152int ssl_cookie_write( void *p_ctx,
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200153 unsigned char **p, unsigned char *end,
154 const unsigned char *cli_id, size_t cli_id_len )
155{
Manuel Pégourié-Gonnarde4de0612014-07-23 17:26:48 +0200156 ssl_cookie_ctx *ctx = (ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200157 unsigned long t;
Manuel Pégourié-Gonnarde4de0612014-07-23 17:26:48 +0200158
Manuel Pégourié-Gonnard29ad7e82014-07-23 19:12:15 +0200159 if( ctx == NULL || cli_id == NULL )
Manuel Pégourié-Gonnarde4de0612014-07-23 17:26:48 +0200160 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200161
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200162 if( (size_t)( end - *p ) < COOKIE_LEN )
Manuel Pégourié-Gonnard562eb782014-07-23 23:41:53 +0200163 return( POLARSSL_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200164
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200165#if defined(POLARSSL_HAVE_TIME)
166 t = (unsigned long) time( NULL );
167#else
168 t = ctx->serial++;
169#endif
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200170
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200171 (*p)[0] = (unsigned char)( t >> 24 );
172 (*p)[1] = (unsigned char)( t >> 16 );
173 (*p)[2] = (unsigned char)( t >> 8 );
174 (*p)[3] = (unsigned char)( t );
175 *p += 4;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200176
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200177 return( ssl_cookie_hmac( &ctx->hmac_ctx, *p - 4,
178 p, end, cli_id, cli_id_len ) );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200179}
180
181/*
182 * Check a cookie
183 */
Manuel Pégourié-Gonnarde4de0612014-07-23 17:26:48 +0200184int ssl_cookie_check( void *p_ctx,
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200185 const unsigned char *cookie, size_t cookie_len,
186 const unsigned char *cli_id, size_t cli_id_len )
187{
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200188 unsigned char ref_hmac[COOKIE_HMAC_LEN];
189 unsigned char *p = ref_hmac;
190 ssl_cookie_ctx *ctx = (ssl_cookie_ctx *) p_ctx;
191 unsigned long cur_time, cookie_time;
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200192
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200193 if( ctx == NULL || cli_id == NULL )
194 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
195
196 if( cookie_len != COOKIE_LEN )
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200197 return( -1 );
198
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200199 if( ssl_cookie_hmac( &ctx->hmac_ctx, cookie,
200 &p, p + sizeof( ref_hmac ),
201 cli_id, cli_id_len ) != 0 )
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200202 return( -1 );
203
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200204 if( safer_memcmp( cookie + 4, ref_hmac, sizeof( ref_hmac ) ) != 0 )
205 return( -1 );
206
207#if defined(POLARSSL_HAVE_TIME)
208 cur_time = (unsigned long) time( NULL );
209#else
210 cur_time = ctx->serial;
211#endif
212
213 cookie_time = ( (unsigned long) cookie[0] << 24 ) |
214 ( (unsigned long) cookie[1] << 16 ) |
215 ( (unsigned long) cookie[2] << 8 ) |
216 ( (unsigned long) cookie[3] );
217
Manuel Pégourié-Gonnardbef8f092014-07-23 23:40:29 +0200218 if( ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout )
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200219 return( -1 );
220
221 return( 0 );
222}
223#endif /* POLARSSL_SSL_COOKIE_C */