ssl_cookie.c 6.34 KB
Newer Older
1 2 3 4 5
/*
 *  DTLS cookie callbacks implementation
 *
 *  Copyright (C) 2014, Brainspark B.V.
 *
6
 *  This file is part of mbed TLS (https://tls.mbed.org)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
/*
 * These session callbacks use a simple chained list
 * to store and retrieve the session information.
 */

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
27
#if !defined(MBEDTLS_CONFIG_FILE)
28
#include "mbedtls/config.h"
29
#else
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
30
#include MBEDTLS_CONFIG_FILE
31 32
#endif

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
33
#if defined(MBEDTLS_SSL_COOKIE_C)
34

35
#include "mbedtls/ssl_cookie.h"
36

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
37
#if defined(MBEDTLS_PLATFORM_C)
38
#include "mbedtls/platform.h"
39
#else
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
40 41
#define mbedtls_malloc     malloc
#define mbedtls_free       free
42 43
#endif

44 45
#include <string.h>

46
/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
47
static void mbedtls_zeroize( void *v, size_t n ) {
48 49 50 51 52 53 54 55
    volatile unsigned char *p = v; while( n-- ) *p++ = 0;
}

/*
 * If DTLS is in use, then at least one of SHA-1, SHA-256, SHA-512 is
 * available. Try SHA-256 first, 512 wastes resources since we need to stay
 * with max 32 bytes of cookie for DTLS 1.0
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
56 57
#if defined(MBEDTLS_SHA256_C)
#define COOKIE_MD           MBEDTLS_MD_SHA224
58 59
#define COOKIE_MD_OUTLEN    32
#define COOKIE_HMAC_LEN     28
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
60 61
#elif defined(MBEDTLS_SHA512_C)
#define COOKIE_MD           MBEDTLS_MD_SHA384
62 63
#define COOKIE_MD_OUTLEN    48
#define COOKIE_HMAC_LEN     28
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
64 65
#elif defined(MBEDTLS_SHA1_C)
#define COOKIE_MD           MBEDTLS_MD_SHA1
66 67
#define COOKIE_MD_OUTLEN    20
#define COOKIE_HMAC_LEN     20
68 69 70 71
#else
#error "DTLS hello verify needs SHA-1 or SHA-2"
#endif

72 73 74 75 76 77
/*
 * Cookies are formed of a 4-bytes timestamp (or serial number) and
 * an HMAC of timestemp and client ID.
 */
#define COOKIE_LEN      ( 4 + COOKIE_HMAC_LEN )

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
78
void mbedtls_ssl_cookie_init( mbedtls_ssl_cookie_ctx *ctx )
79
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
80 81
    mbedtls_md_init( &ctx->hmac_ctx );
#if !defined(MBEDTLS_HAVE_TIME)
82 83
    ctx->serial = 0;
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
84
    ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT;
85 86
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
87
void mbedtls_ssl_cookie_set_timeout( mbedtls_ssl_cookie_ctx *ctx, unsigned long delay )
88 89
{
    ctx->timeout = delay;
90 91
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
92
void mbedtls_ssl_cookie_free( mbedtls_ssl_cookie_ctx *ctx )
93
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
94
    mbedtls_md_free( &ctx->hmac_ctx );
95 96
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
97
int mbedtls_ssl_cookie_setup( mbedtls_ssl_cookie_ctx *ctx,
98 99 100 101
                      int (*f_rng)(void *, unsigned char *, size_t),
                      void *p_rng )
{
    int ret;
102
    unsigned char key[COOKIE_MD_OUTLEN];
103 104 105 106

    if( ( ret = f_rng( p_rng, key, sizeof( key ) ) ) != 0 )
        return( ret );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
107
    ret = mbedtls_md_setup( &ctx->hmac_ctx, mbedtls_md_info_from_type( COOKIE_MD ), 1 );
108 109 110
    if( ret != 0 )
        return( ret );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
111
    ret = mbedtls_md_hmac_starts( &ctx->hmac_ctx, key, sizeof( key ) );
112 113 114
    if( ret != 0 )
        return( ret );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
115
    mbedtls_zeroize( key, sizeof( key ) );
116 117 118 119

    return( 0 );
}

120 121 122
/*
 * Generate the HMAC part of a cookie
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
123
static int ssl_cookie_hmac( mbedtls_md_context_t *hmac_ctx,
124 125 126 127 128 129 130
                            const unsigned char time[4],
                            unsigned char **p, unsigned char *end,
                            const unsigned char *cli_id, size_t cli_id_len )
{
    unsigned char hmac_out[COOKIE_MD_OUTLEN];

    if( (size_t)( end - *p ) < COOKIE_HMAC_LEN )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
131
        return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
132

133 134 135 136
    if( mbedtls_md_hmac_reset(  hmac_ctx ) != 0 ||
        mbedtls_md_hmac_update( hmac_ctx, time, 4 ) != 0 ||
        mbedtls_md_hmac_update( hmac_ctx, cli_id, cli_id_len ) != 0 ||
        mbedtls_md_hmac_finish( hmac_ctx, hmac_out ) != 0 )
137
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
138
        return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
139 140 141 142 143 144 145 146
    }

    memcpy( *p, hmac_out, COOKIE_HMAC_LEN );
    *p += COOKIE_HMAC_LEN;

    return( 0 );
}

147 148 149
/*
 * Generate cookie for DTLS ClientHello verification
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
150
int mbedtls_ssl_cookie_write( void *p_ctx,
151 152 153
                      unsigned char **p, unsigned char *end,
                      const unsigned char *cli_id, size_t cli_id_len )
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
154
    mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
155
    unsigned long t;
156

157
    if( ctx == NULL || cli_id == NULL )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
158
        return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
159

160
    if( (size_t)( end - *p ) < COOKIE_LEN )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
161
        return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
162

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
163
#if defined(MBEDTLS_HAVE_TIME)
164 165 166 167
    t = (unsigned long) time( NULL );
#else
    t = ctx->serial++;
#endif
168

169 170 171 172 173
    (*p)[0] = (unsigned char)( t >> 24 );
    (*p)[1] = (unsigned char)( t >> 16 );
    (*p)[2] = (unsigned char)( t >>  8 );
    (*p)[3] = (unsigned char)( t       );
    *p += 4;
174

175 176
    return( ssl_cookie_hmac( &ctx->hmac_ctx, *p - 4,
                             p, end, cli_id, cli_id_len ) );
177 178 179 180 181
}

/*
 * Check a cookie
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
182
int mbedtls_ssl_cookie_check( void *p_ctx,
183 184 185
                      const unsigned char *cookie, size_t cookie_len,
                      const unsigned char *cli_id, size_t cli_id_len )
{
186 187
    unsigned char ref_hmac[COOKIE_HMAC_LEN];
    unsigned char *p = ref_hmac;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
188
    mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
189
    unsigned long cur_time, cookie_time;
190

191
    if( ctx == NULL || cli_id == NULL )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
192
        return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
193 194 195 196 197 198 199

    if( cookie_len != COOKIE_LEN )
        return( -1 );

    if( ssl_cookie_hmac( &ctx->hmac_ctx, cookie,
                         &p, p + sizeof( ref_hmac ),
                         cli_id, cli_id_len ) != 0 )
200 201
        return( -1 );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
202
    if( mbedtls_ssl_safer_memcmp( cookie + 4, ref_hmac, sizeof( ref_hmac ) ) != 0 )
203 204
        return( -1 );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
205
#if defined(MBEDTLS_HAVE_TIME)
206 207 208 209 210 211 212 213 214 215
    cur_time = (unsigned long) time( NULL );
#else
    cur_time = ctx->serial;
#endif

    cookie_time = ( (unsigned long) cookie[0] << 24 ) |
                  ( (unsigned long) cookie[1] << 16 ) |
                  ( (unsigned long) cookie[2] <<  8 ) |
                  ( (unsigned long) cookie[3]       );

216
    if( ctx->timeout != 0 && cur_time - cookie_time > ctx->timeout )
217 218 219 220
        return( -1 );

    return( 0 );
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
221
#endif /* MBEDTLS_SSL_COOKIE_C */