Manuel Pégourié-Gonnard | fd6d897 | 2015-05-15 12:09:00 +0200 | [diff] [blame] | 1 | /* |
| 2 | * TLS server tickets callbacks implementation |
| 3 | * |
| 4 | * Copyright (C) 2015, ARM Limited, All Rights Reserved |
| 5 | * |
| 6 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
| 23 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 24 | #include "mbedtls/config.h" |
| 25 | #else |
| 26 | #include MBEDTLS_CONFIG_FILE |
| 27 | #endif |
| 28 | |
| 29 | #if defined(MBEDTLS_SSL_TICKET_C) |
| 30 | |
| 31 | #include "mbedtls/ssl_ticket.h" |
| 32 | |
| 33 | #if defined(MBEDTLS_PLATFORM_C) |
| 34 | #include "mbedtls/platform.h" |
| 35 | #else |
| 36 | #define mbedtls_malloc malloc |
| 37 | #define mbedtls_free free |
| 38 | #endif |
| 39 | |
| 40 | #include <string.h> |
| 41 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 42 | /* Implementation that should never be optimized out by the compiler */ |
| 43 | static void mbedtls_zeroize( void *v, size_t n ) { |
| 44 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
| 45 | } |
| 46 | |
| 47 | /* |
| 48 | * Initialze context |
| 49 | */ |
| 50 | void mbedtls_ssl_ticket_init( mbedtls_ssl_ticket_context *ctx ) |
| 51 | { |
| 52 | memset( ctx, 0, sizeof( mbedtls_ssl_ticket_context ) ); |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 53 | |
| 54 | #if defined(MBEDTLS_THREADING_C) |
| 55 | mbedtls_mutex_init( &ctx->mutex ); |
| 56 | #endif |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 57 | } |
| 58 | |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame^] | 59 | #define MAX_KEY_BYTES 32 /* 256 bits */ |
| 60 | |
| 61 | /* |
| 62 | * Generate/update a key |
| 63 | */ |
| 64 | static int ssl_ticket_gen_key( mbedtls_ssl_ticket_context *ctx, |
| 65 | unsigned char index ) |
| 66 | { |
| 67 | int ret; |
| 68 | unsigned char buf[MAX_KEY_BYTES]; |
| 69 | mbedtls_ssl_ticket_key *key = ctx->keys + index; |
| 70 | |
| 71 | #if defined(MBEDTLS_HAVE_TIME) |
| 72 | key->generation_time = (uint32_t) time( NULL ); |
| 73 | #endif |
| 74 | |
| 75 | if( ( ret = ctx->f_rng( ctx->p_rng, key->name, sizeof( key->name ) ) ) != 0 ) |
| 76 | return( ret ); |
| 77 | |
| 78 | if( ( ret = ctx->f_rng( ctx->p_rng, buf, sizeof( buf ) ) ) != 0 ) |
| 79 | return( ret ); |
| 80 | |
| 81 | /* With GCM and CCM, same context can encrypt & decrypt */ |
| 82 | ret = mbedtls_cipher_setkey( &key->ctx, buf, |
| 83 | mbedtls_cipher_get_key_size( &key->ctx ), |
| 84 | MBEDTLS_ENCRYPT ); |
| 85 | |
| 86 | mbedtls_zeroize( buf, sizeof( buf ) ); |
| 87 | |
| 88 | return( ret ); |
| 89 | } |
| 90 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 91 | /* |
| 92 | * Setup context for actual use |
| 93 | */ |
| 94 | int mbedtls_ssl_ticket_setup( mbedtls_ssl_ticket_context *ctx, |
| 95 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
Manuel Pégourié-Gonnard | a0adc1b | 2015-05-25 10:35:16 +0200 | [diff] [blame] | 96 | mbedtls_cipher_type_t cipher, |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 97 | uint32_t lifetime ) |
| 98 | { |
| 99 | int ret; |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame^] | 100 | const mbedtls_cipher_info_t *cipher_info; |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 101 | |
| 102 | ctx->f_rng = f_rng; |
| 103 | ctx->p_rng = p_rng; |
| 104 | |
| 105 | ctx->ticket_lifetime = lifetime; |
| 106 | |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame^] | 107 | cipher_info = mbedtls_cipher_info_from_type( cipher); |
| 108 | if( cipher_info == NULL ) |
| 109 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 110 | |
| 111 | if( cipher_info->mode != MBEDTLS_MODE_GCM && |
| 112 | cipher_info->mode != MBEDTLS_MODE_CCM ) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 113 | { |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame^] | 114 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame^] | 117 | if( cipher_info->key_length > 8 * MAX_KEY_BYTES ) |
| 118 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 119 | |
| 120 | if( ( ret = mbedtls_cipher_setup( &ctx->keys[0].ctx, cipher_info ) ) != 0 || |
| 121 | ( ret = mbedtls_cipher_setup( &ctx->keys[1].ctx, cipher_info ) ) != 0 ) |
Manuel Pégourié-Gonnard | a0adc1b | 2015-05-25 10:35:16 +0200 | [diff] [blame] | 122 | { |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame^] | 123 | return( ret ); |
Manuel Pégourié-Gonnard | a0adc1b | 2015-05-25 10:35:16 +0200 | [diff] [blame] | 124 | } |
| 125 | |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame^] | 126 | if( ( ret = ssl_ticket_gen_key( ctx, 0 ) ) != 0 || |
| 127 | ( ret = ssl_ticket_gen_key( ctx, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | a0adc1b | 2015-05-25 10:35:16 +0200 | [diff] [blame] | 128 | { |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame^] | 129 | return( ret ); |
Manuel Pégourié-Gonnard | a0adc1b | 2015-05-25 10:35:16 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame^] | 132 | return( 0 ); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 135 | /* |
| 136 | * Serialize a session in the following format: |
| 137 | * 0 . n-1 session structure, n = sizeof(mbedtls_ssl_session) |
| 138 | * n . n+2 peer_cert length = m (0 if no certificate) |
| 139 | * n+3 . n+2+m peer cert ASN.1 |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 140 | */ |
| 141 | static int ssl_save_session( const mbedtls_ssl_session *session, |
| 142 | unsigned char *buf, size_t buf_len, |
| 143 | size_t *olen ) |
| 144 | { |
| 145 | unsigned char *p = buf; |
| 146 | size_t left = buf_len; |
| 147 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 148 | size_t cert_len; |
| 149 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 150 | |
| 151 | if( left < sizeof( mbedtls_ssl_session ) ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 152 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 153 | |
| 154 | memcpy( p, session, sizeof( mbedtls_ssl_session ) ); |
| 155 | p += sizeof( mbedtls_ssl_session ); |
| 156 | left -= sizeof( mbedtls_ssl_session ); |
| 157 | |
| 158 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 159 | if( session->peer_cert == NULL ) |
| 160 | cert_len = 0; |
| 161 | else |
| 162 | cert_len = session->peer_cert->raw.len; |
| 163 | |
| 164 | if( left < 3 + cert_len ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 165 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 166 | |
| 167 | *p++ = (unsigned char)( cert_len >> 16 & 0xFF ); |
| 168 | *p++ = (unsigned char)( cert_len >> 8 & 0xFF ); |
| 169 | *p++ = (unsigned char)( cert_len & 0xFF ); |
| 170 | |
| 171 | if( session->peer_cert != NULL ) |
| 172 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 173 | |
| 174 | p += cert_len; |
| 175 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 176 | |
| 177 | *olen = p - buf; |
| 178 | |
| 179 | return( 0 ); |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Unserialise session, see ssl_save_session() |
| 184 | */ |
| 185 | static int ssl_load_session( mbedtls_ssl_session *session, |
| 186 | const unsigned char *buf, size_t len ) |
| 187 | { |
| 188 | const unsigned char *p = buf; |
| 189 | const unsigned char * const end = buf + len; |
| 190 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 191 | size_t cert_len; |
| 192 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 193 | |
| 194 | if( p + sizeof( mbedtls_ssl_session ) > end ) |
| 195 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 196 | |
| 197 | memcpy( session, p, sizeof( mbedtls_ssl_session ) ); |
| 198 | p += sizeof( mbedtls_ssl_session ); |
| 199 | |
| 200 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 201 | if( p + 3 > end ) |
| 202 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 203 | |
| 204 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 205 | p += 3; |
| 206 | |
| 207 | if( cert_len == 0 ) |
| 208 | { |
| 209 | session->peer_cert = NULL; |
| 210 | } |
| 211 | else |
| 212 | { |
| 213 | int ret; |
| 214 | |
| 215 | if( p + cert_len > end ) |
| 216 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 217 | |
| 218 | session->peer_cert = mbedtls_malloc( sizeof( mbedtls_x509_crt ) ); |
| 219 | |
| 220 | if( session->peer_cert == NULL ) |
| 221 | return( MBEDTLS_ERR_SSL_MALLOC_FAILED ); |
| 222 | |
| 223 | mbedtls_x509_crt_init( session->peer_cert ); |
| 224 | |
| 225 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
| 226 | p, cert_len ) ) != 0 ) |
| 227 | { |
| 228 | mbedtls_x509_crt_free( session->peer_cert ); |
| 229 | mbedtls_free( session->peer_cert ); |
| 230 | session->peer_cert = NULL; |
| 231 | return( ret ); |
| 232 | } |
| 233 | |
| 234 | p += cert_len; |
| 235 | } |
| 236 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 237 | |
| 238 | if( p != end ) |
| 239 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 240 | |
| 241 | return( 0 ); |
| 242 | } |
| 243 | |
| 244 | /* |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 245 | * Create session ticket, with the following structure: |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 246 | * |
| 247 | * struct { |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 248 | * opaque key_name[4]; |
| 249 | * opaque iv[12]; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 250 | * opaque encrypted_state<0..2^16-1>; |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 251 | * opaque tag[16]; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 252 | * } ticket; |
| 253 | * |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 254 | * The key_name, iv, and length of encrypted_state are the additional |
| 255 | * authenticated data. |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 256 | */ |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 257 | int mbedtls_ssl_ticket_write( void *p_ticket, |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 258 | const mbedtls_ssl_session *session, |
| 259 | unsigned char *start, |
| 260 | const unsigned char *end, |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 261 | size_t *tlen, |
| 262 | uint32_t *ticket_lifetime ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 263 | { |
| 264 | int ret; |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 265 | mbedtls_ssl_ticket_context *ctx = p_ticket; |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame^] | 266 | mbedtls_ssl_ticket_key *key = ctx->keys; |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 267 | unsigned char *key_name = start; |
| 268 | unsigned char *iv = start + 4; |
| 269 | unsigned char *state_len_bytes = iv + 12; |
| 270 | unsigned char *state = state_len_bytes + 2; |
| 271 | unsigned char *tag; |
| 272 | size_t clear_len, ciph_len; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 273 | |
| 274 | *tlen = 0; |
| 275 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 276 | if( ctx == NULL || ctx->f_rng == NULL ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 277 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 278 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 279 | /* We need at least 4 bytes for key_name, 12 for IV, 2 for len 16 for tag, |
| 280 | * in addition to session itself, that will be checked when writing it. */ |
| 281 | if( end - start < 4 + 12 + 2 + 16 ) |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 282 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 283 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 284 | #if defined(MBEDTLS_THREADING_C) |
| 285 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 286 | return( ret ); |
| 287 | #endif |
| 288 | |
| 289 | *ticket_lifetime = ctx->ticket_lifetime; |
| 290 | |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame^] | 291 | memcpy( key_name, key->name, 4 ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 292 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 293 | if( ( ret = ctx->f_rng( ctx->p_rng, iv, 12 ) ) != 0 ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 294 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 295 | |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 296 | /* Dump session state */ |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 297 | if( ( ret = ssl_save_session( session, |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 298 | state, end - state, &clear_len ) ) != 0 || |
| 299 | (unsigned long) clear_len > 65535 ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 300 | { |
| 301 | goto cleanup; |
| 302 | } |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 303 | state_len_bytes[0] = ( clear_len >> 8 ) & 0xff; |
| 304 | state_len_bytes[1] = ( clear_len ) & 0xff; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 305 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 306 | /* Encrypt and authenticate */ |
| 307 | tag = state + clear_len; |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame^] | 308 | if( ( ret = mbedtls_cipher_auth_encrypt( &key->ctx, |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 309 | iv, 12, key_name, 4 + 12 + 2, |
| 310 | state, clear_len, state, &ciph_len, tag, 16 ) ) != 0 ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 311 | { |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 312 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 313 | } |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 314 | if( ciph_len != clear_len ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 315 | { |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 316 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 317 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 318 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 319 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 320 | *tlen = 4 + 12 + 2 + 16 + ciph_len; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 321 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 322 | cleanup: |
| 323 | #if defined(MBEDTLS_THREADING_C) |
| 324 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
| 325 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
| 326 | #endif |
| 327 | |
| 328 | return( ret ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /* |
| 332 | * Load session ticket (see mbedtls_ssl_ticket_write for structure) |
| 333 | */ |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 334 | int mbedtls_ssl_ticket_parse( void *p_ticket, |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 335 | mbedtls_ssl_session *session, |
| 336 | unsigned char *buf, |
| 337 | size_t len ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 338 | { |
| 339 | int ret; |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 340 | mbedtls_ssl_ticket_context *ctx = p_ticket; |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame^] | 341 | mbedtls_ssl_ticket_key *key = ctx->keys; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 342 | unsigned char *key_name = buf; |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 343 | unsigned char *iv = buf + 4; |
| 344 | unsigned char *enc_len_p = iv + 12; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 345 | unsigned char *ticket = enc_len_p + 2; |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 346 | unsigned char *tag; |
| 347 | size_t enc_len, clear_len; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 348 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 349 | if( ctx == NULL || ctx->f_rng == NULL || |
| 350 | len < 4 + 12 + 2 + 16 ) |
| 351 | { |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 352 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 353 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 354 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 355 | #if defined(MBEDTLS_THREADING_C) |
| 356 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 357 | return( ret ); |
| 358 | #endif |
| 359 | |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 360 | enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1]; |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 361 | tag = ticket + enc_len; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 362 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 363 | if( len != 4 + 12 + 2 + enc_len + 16 ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 364 | { |
| 365 | ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 366 | goto cleanup; |
| 367 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 368 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 369 | /* Check name (public data) */ |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame^] | 370 | if( memcmp( key_name, key->name, 4 ) != 0 ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 371 | { |
| 372 | ret = MBEDTLS_ERR_SSL_INVALID_MAC; |
| 373 | goto cleanup; |
| 374 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 375 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 376 | /* Decrypt and authenticate */ |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame^] | 377 | if( ( ret = mbedtls_cipher_auth_decrypt( &key->ctx, iv, 12, |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 378 | key_name, 4 + 12 + 2, ticket, enc_len, |
| 379 | ticket, &clear_len, tag, 16 ) ) != 0 ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 380 | { |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 381 | /* TODO: convert AUTH_FAILED to INVALID_MAC */ |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 382 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 383 | } |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 384 | if( clear_len != enc_len ) |
| 385 | { |
| 386 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 387 | goto cleanup; |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 388 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 389 | |
| 390 | /* Actually load session */ |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 391 | if( ( ret = ssl_load_session( session, ticket, clear_len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 392 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 393 | |
| 394 | #if defined(MBEDTLS_HAVE_TIME) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 395 | { |
Manuel Pégourié-Gonnard | 8eff512 | 2015-05-20 11:41:36 +0200 | [diff] [blame] | 396 | /* Check for expiration */ |
| 397 | time_t current_time = time( NULL ); |
| 398 | |
| 399 | if( current_time < session->start || |
| 400 | (uint32_t)( current_time - session->start ) > ctx->ticket_lifetime ) |
| 401 | { |
| 402 | ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED; |
| 403 | goto cleanup; |
| 404 | } |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 405 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 406 | #endif |
| 407 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 408 | cleanup: |
| 409 | #if defined(MBEDTLS_THREADING_C) |
| 410 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
| 411 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
| 412 | #endif |
| 413 | |
| 414 | return( ret ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 415 | } |
| 416 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 417 | /* |
| 418 | * Free context |
| 419 | */ |
| 420 | void mbedtls_ssl_ticket_free( mbedtls_ssl_ticket_context *ctx ) |
| 421 | { |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame^] | 422 | mbedtls_cipher_free( &ctx->keys[0].ctx ); |
| 423 | mbedtls_cipher_free( &ctx->keys[1].ctx ); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 424 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 425 | #if defined(MBEDTLS_THREADING_C) |
| 426 | mbedtls_mutex_free( &ctx->mutex ); |
| 427 | #endif |
| 428 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 429 | mbedtls_zeroize( ctx, sizeof( mbedtls_ssl_ticket_context ) ); |
| 430 | } |
| 431 | |
Manuel Pégourié-Gonnard | fd6d897 | 2015-05-15 12:09:00 +0200 | [diff] [blame] | 432 | #endif /* MBEDTLS_SSL_TICKET_C */ |