blob: fa0aabc27b1cc262f513833c1970a6ff80d2326e [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
79#define COOKIE_TIMEOUT 60
80
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020081void ssl_cookie_init( ssl_cookie_ctx *ctx )
82{
83 md_init( &ctx->hmac_ctx );
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +020084#if !defined(POLARSSL_HAVE_TIME)
85 ctx->serial = 0;
86#endif
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +020087}
88
89void ssl_cookie_free( ssl_cookie_ctx *ctx )
90{
91 md_free( &ctx->hmac_ctx );
92}
93
94int 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é-Gonnarde9030812014-07-23 21:29:11 +020099 unsigned char key[COOKIE_MD_OUTLEN];
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200100
101 if( ( ret = f_rng( p_rng, key, sizeof( key ) ) ) != 0 )
102 return( ret );
103
Manuel Pégourié-Gonnard445a1ec2014-07-23 20:48:05 +0200104 ret = md_init_ctx( &ctx->hmac_ctx, md_info_from_type( COOKIE_MD ) );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200105 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é-Gonnarde9030812014-07-23 21:29:11 +0200118 * Generate the HMAC part of a cookie
119 */
120static 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é-Gonnard232edd42014-07-23 16:56:27 +0200146 * Generate cookie for DTLS ClientHello verification
147 */
Manuel Pégourié-Gonnarde4de0612014-07-23 17:26:48 +0200148int ssl_cookie_write( void *p_ctx,
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200149 unsigned char **p, unsigned char *end,
150 const unsigned char *cli_id, size_t cli_id_len )
151{
Manuel Pégourié-Gonnarde4de0612014-07-23 17:26:48 +0200152 ssl_cookie_ctx *ctx = (ssl_cookie_ctx *) p_ctx;
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200153 unsigned long t;
Manuel Pégourié-Gonnarde4de0612014-07-23 17:26:48 +0200154
Manuel Pégourié-Gonnard29ad7e82014-07-23 19:12:15 +0200155 if( ctx == NULL || cli_id == NULL )
Manuel Pégourié-Gonnarde4de0612014-07-23 17:26:48 +0200156 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200157
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200158 if( (size_t)( end - *p ) < COOKIE_LEN )
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200159 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
160
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200161#if defined(POLARSSL_HAVE_TIME)
162 t = (unsigned long) time( NULL );
163#else
164 t = ctx->serial++;
165#endif
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200166
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200167 (*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é-Gonnard232edd42014-07-23 16:56:27 +0200172
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200173 return( ssl_cookie_hmac( &ctx->hmac_ctx, *p - 4,
174 p, end, cli_id, cli_id_len ) );
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200175}
176
177/*
178 * Check a cookie
179 */
Manuel Pégourié-Gonnarde4de0612014-07-23 17:26:48 +0200180int ssl_cookie_check( void *p_ctx,
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200181 const unsigned char *cookie, size_t cookie_len,
182 const unsigned char *cli_id, size_t cli_id_len )
183{
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200184 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é-Gonnard232edd42014-07-23 16:56:27 +0200188
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200189 if( ctx == NULL || cli_id == NULL )
190 return( POLARSSL_ERR_SSL_BAD_INPUT_DATA );
191
192 if( cookie_len != COOKIE_LEN )
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200193 return( -1 );
194
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200195 if( ssl_cookie_hmac( &ctx->hmac_ctx, cookie,
196 &p, p + sizeof( ref_hmac ),
197 cli_id, cli_id_len ) != 0 )
Manuel Pégourié-Gonnard232edd42014-07-23 16:56:27 +0200198 return( -1 );
199
Manuel Pégourié-Gonnarde9030812014-07-23 21:29:11 +0200200 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é-Gonnard232edd42014-07-23 16:56:27 +0200215 return( -1 );
216
217 return( 0 );
218}
219#endif /* POLARSSL_SSL_COOKIE_C */