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 | |
| 59 | /* |
| 60 | * Setup context for actual use |
| 61 | */ |
| 62 | int mbedtls_ssl_ticket_setup( mbedtls_ssl_ticket_context *ctx, |
| 63 | 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^] | 64 | mbedtls_cipher_type_t cipher, |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 65 | uint32_t lifetime ) |
| 66 | { |
| 67 | int ret; |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 68 | unsigned char buf[32]; |
Manuel Pégourié-Gonnard | a0adc1b | 2015-05-25 10:35:16 +0200 | [diff] [blame^] | 69 | mbedtls_cipher_mode_t mode; |
| 70 | size_t key_bits; |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 71 | |
| 72 | ctx->f_rng = f_rng; |
| 73 | ctx->p_rng = p_rng; |
| 74 | |
| 75 | ctx->ticket_lifetime = lifetime; |
| 76 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 77 | if( ( ret = mbedtls_cipher_setup( &ctx->cipher, |
Manuel Pégourié-Gonnard | a0adc1b | 2015-05-25 10:35:16 +0200 | [diff] [blame^] | 78 | mbedtls_cipher_info_from_type( cipher) ) ) != 0 ) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 79 | { |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 80 | goto cleanup; |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 81 | } |
| 82 | |
Manuel Pégourié-Gonnard | a0adc1b | 2015-05-25 10:35:16 +0200 | [diff] [blame^] | 83 | mode = mbedtls_cipher_get_cipher_mode( &ctx->cipher ); |
| 84 | if( mode != MBEDTLS_MODE_GCM && mode != MBEDTLS_MODE_CCM ) |
| 85 | { |
| 86 | ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 87 | goto cleanup; |
| 88 | } |
| 89 | |
| 90 | key_bits = mbedtls_cipher_get_key_size( &ctx->cipher ); |
| 91 | if( key_bits > 8 * sizeof( buf ) ) |
| 92 | { |
| 93 | ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 94 | goto cleanup; |
| 95 | } |
| 96 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 97 | if( ( ret = f_rng( p_rng, buf, sizeof( buf ) ) != 0 ) ) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 98 | { |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 99 | goto cleanup; |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 100 | } |
| 101 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 102 | /* With GCM and CCM, same context can encrypt & decrypt */ |
Manuel Pégourié-Gonnard | a0adc1b | 2015-05-25 10:35:16 +0200 | [diff] [blame^] | 103 | if( ( ret = mbedtls_cipher_setkey( &ctx->cipher, buf, key_bits, |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 104 | MBEDTLS_ENCRYPT ) ) != 0 ) |
| 105 | { |
| 106 | goto cleanup; |
| 107 | } |
| 108 | |
| 109 | cleanup: |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 110 | mbedtls_zeroize( buf, sizeof( buf ) ); |
| 111 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 112 | if( ret != 0 ) |
| 113 | mbedtls_ssl_ticket_free( ctx ); |
| 114 | |
| 115 | return( ret ); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 116 | } |
| 117 | |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 118 | /* |
| 119 | * Serialize a session in the following format: |
| 120 | * 0 . n-1 session structure, n = sizeof(mbedtls_ssl_session) |
| 121 | * n . n+2 peer_cert length = m (0 if no certificate) |
| 122 | * n+3 . n+2+m peer cert ASN.1 |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 123 | */ |
| 124 | static int ssl_save_session( const mbedtls_ssl_session *session, |
| 125 | unsigned char *buf, size_t buf_len, |
| 126 | size_t *olen ) |
| 127 | { |
| 128 | unsigned char *p = buf; |
| 129 | size_t left = buf_len; |
| 130 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 131 | size_t cert_len; |
| 132 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 133 | |
| 134 | if( left < sizeof( mbedtls_ssl_session ) ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 135 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 136 | |
| 137 | memcpy( p, session, sizeof( mbedtls_ssl_session ) ); |
| 138 | p += sizeof( mbedtls_ssl_session ); |
| 139 | left -= sizeof( mbedtls_ssl_session ); |
| 140 | |
| 141 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 142 | if( session->peer_cert == NULL ) |
| 143 | cert_len = 0; |
| 144 | else |
| 145 | cert_len = session->peer_cert->raw.len; |
| 146 | |
| 147 | if( left < 3 + cert_len ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 148 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 149 | |
| 150 | *p++ = (unsigned char)( cert_len >> 16 & 0xFF ); |
| 151 | *p++ = (unsigned char)( cert_len >> 8 & 0xFF ); |
| 152 | *p++ = (unsigned char)( cert_len & 0xFF ); |
| 153 | |
| 154 | if( session->peer_cert != NULL ) |
| 155 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 156 | |
| 157 | p += cert_len; |
| 158 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 159 | |
| 160 | *olen = p - buf; |
| 161 | |
| 162 | return( 0 ); |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Unserialise session, see ssl_save_session() |
| 167 | */ |
| 168 | static int ssl_load_session( mbedtls_ssl_session *session, |
| 169 | const unsigned char *buf, size_t len ) |
| 170 | { |
| 171 | const unsigned char *p = buf; |
| 172 | const unsigned char * const end = buf + len; |
| 173 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 174 | size_t cert_len; |
| 175 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 176 | |
| 177 | if( p + sizeof( mbedtls_ssl_session ) > end ) |
| 178 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 179 | |
| 180 | memcpy( session, p, sizeof( mbedtls_ssl_session ) ); |
| 181 | p += sizeof( mbedtls_ssl_session ); |
| 182 | |
| 183 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 184 | if( p + 3 > end ) |
| 185 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 186 | |
| 187 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 188 | p += 3; |
| 189 | |
| 190 | if( cert_len == 0 ) |
| 191 | { |
| 192 | session->peer_cert = NULL; |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | int ret; |
| 197 | |
| 198 | if( p + cert_len > end ) |
| 199 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 200 | |
| 201 | session->peer_cert = mbedtls_malloc( sizeof( mbedtls_x509_crt ) ); |
| 202 | |
| 203 | if( session->peer_cert == NULL ) |
| 204 | return( MBEDTLS_ERR_SSL_MALLOC_FAILED ); |
| 205 | |
| 206 | mbedtls_x509_crt_init( session->peer_cert ); |
| 207 | |
| 208 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
| 209 | p, cert_len ) ) != 0 ) |
| 210 | { |
| 211 | mbedtls_x509_crt_free( session->peer_cert ); |
| 212 | mbedtls_free( session->peer_cert ); |
| 213 | session->peer_cert = NULL; |
| 214 | return( ret ); |
| 215 | } |
| 216 | |
| 217 | p += cert_len; |
| 218 | } |
| 219 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 220 | |
| 221 | if( p != end ) |
| 222 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 223 | |
| 224 | return( 0 ); |
| 225 | } |
| 226 | |
| 227 | /* |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 228 | * Create session ticket, with the following structure: |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 229 | * |
| 230 | * struct { |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 231 | * opaque key_name[4]; |
| 232 | * opaque iv[12]; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 233 | * opaque encrypted_state<0..2^16-1>; |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 234 | * opaque tag[16]; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 235 | * } ticket; |
| 236 | * |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 237 | * The key_name, iv, and length of encrypted_state are the additional |
| 238 | * authenticated data. |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 239 | */ |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 240 | int mbedtls_ssl_ticket_write( void *p_ticket, |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 241 | const mbedtls_ssl_session *session, |
| 242 | unsigned char *start, |
| 243 | const unsigned char *end, |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 244 | size_t *tlen, |
| 245 | uint32_t *ticket_lifetime ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 246 | { |
| 247 | int ret; |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 248 | mbedtls_ssl_ticket_context *ctx = p_ticket; |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 249 | unsigned char *key_name = start; |
| 250 | unsigned char *iv = start + 4; |
| 251 | unsigned char *state_len_bytes = iv + 12; |
| 252 | unsigned char *state = state_len_bytes + 2; |
| 253 | unsigned char *tag; |
| 254 | size_t clear_len, ciph_len; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 255 | |
| 256 | *tlen = 0; |
| 257 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 258 | if( ctx == NULL || ctx->f_rng == NULL ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 259 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 260 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 261 | /* We need at least 4 bytes for key_name, 12 for IV, 2 for len 16 for tag, |
| 262 | * in addition to session itself, that will be checked when writing it. */ |
| 263 | if( end - start < 4 + 12 + 2 + 16 ) |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 264 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 265 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 266 | #if defined(MBEDTLS_THREADING_C) |
| 267 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 268 | return( ret ); |
| 269 | #endif |
| 270 | |
| 271 | *ticket_lifetime = ctx->ticket_lifetime; |
| 272 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 273 | memcpy( key_name, ctx->key_name, 4 ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 274 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 275 | 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] | 276 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 277 | |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 278 | /* Dump session state */ |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 279 | if( ( ret = ssl_save_session( session, |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 280 | state, end - state, &clear_len ) ) != 0 || |
| 281 | (unsigned long) clear_len > 65535 ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 282 | { |
| 283 | goto cleanup; |
| 284 | } |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 285 | state_len_bytes[0] = ( clear_len >> 8 ) & 0xff; |
| 286 | state_len_bytes[1] = ( clear_len ) & 0xff; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 287 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 288 | /* Encrypt and authenticate */ |
| 289 | tag = state + clear_len; |
| 290 | if( ( ret = mbedtls_cipher_auth_encrypt( &ctx->cipher, |
| 291 | iv, 12, key_name, 4 + 12 + 2, |
| 292 | state, clear_len, state, &ciph_len, tag, 16 ) ) != 0 ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 293 | { |
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 | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 296 | if( ciph_len != clear_len ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 297 | { |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 298 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 299 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 300 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 301 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 302 | *tlen = 4 + 12 + 2 + 16 + ciph_len; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 303 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 304 | cleanup: |
| 305 | #if defined(MBEDTLS_THREADING_C) |
| 306 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
| 307 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
| 308 | #endif |
| 309 | |
| 310 | return( ret ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /* |
| 314 | * Load session ticket (see mbedtls_ssl_ticket_write for structure) |
| 315 | */ |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 316 | int mbedtls_ssl_ticket_parse( void *p_ticket, |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 317 | mbedtls_ssl_session *session, |
| 318 | unsigned char *buf, |
| 319 | size_t len ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 320 | { |
| 321 | int ret; |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 322 | mbedtls_ssl_ticket_context *ctx = p_ticket; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 323 | unsigned char *key_name = buf; |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 324 | unsigned char *iv = buf + 4; |
| 325 | unsigned char *enc_len_p = iv + 12; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 326 | unsigned char *ticket = enc_len_p + 2; |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 327 | unsigned char *tag; |
| 328 | size_t enc_len, clear_len; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 329 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 330 | if( ctx == NULL || ctx->f_rng == NULL || |
| 331 | len < 4 + 12 + 2 + 16 ) |
| 332 | { |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 333 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 334 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 335 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 336 | #if defined(MBEDTLS_THREADING_C) |
| 337 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 338 | return( ret ); |
| 339 | #endif |
| 340 | |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 341 | 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] | 342 | tag = ticket + enc_len; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 343 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 344 | if( len != 4 + 12 + 2 + enc_len + 16 ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 345 | { |
| 346 | ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 347 | goto cleanup; |
| 348 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 349 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 350 | /* Check name (public data) */ |
| 351 | if( memcmp( key_name, ctx->key_name, 4 ) != 0 ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 352 | { |
| 353 | ret = MBEDTLS_ERR_SSL_INVALID_MAC; |
| 354 | goto cleanup; |
| 355 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 356 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 357 | /* Decrypt and authenticate */ |
| 358 | if( ( ret = mbedtls_cipher_auth_decrypt( &ctx->cipher, iv, 12, |
| 359 | key_name, 4 + 12 + 2, ticket, enc_len, |
| 360 | ticket, &clear_len, tag, 16 ) ) != 0 ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 361 | { |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 362 | /* TODO: convert AUTH_FAILED to INVALID_MAC */ |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 363 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 364 | } |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 365 | if( clear_len != enc_len ) |
| 366 | { |
| 367 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 368 | goto cleanup; |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 369 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 370 | |
| 371 | /* Actually load session */ |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 372 | if( ( ret = ssl_load_session( session, ticket, clear_len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 373 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 374 | |
| 375 | #if defined(MBEDTLS_HAVE_TIME) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 376 | { |
Manuel Pégourié-Gonnard | 8eff512 | 2015-05-20 11:41:36 +0200 | [diff] [blame] | 377 | /* Check for expiration */ |
| 378 | time_t current_time = time( NULL ); |
| 379 | |
| 380 | if( current_time < session->start || |
| 381 | (uint32_t)( current_time - session->start ) > ctx->ticket_lifetime ) |
| 382 | { |
| 383 | ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED; |
| 384 | goto cleanup; |
| 385 | } |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 386 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 387 | #endif |
| 388 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 389 | cleanup: |
| 390 | #if defined(MBEDTLS_THREADING_C) |
| 391 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
| 392 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
| 393 | #endif |
| 394 | |
| 395 | return( ret ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 396 | } |
| 397 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 398 | /* |
| 399 | * Free context |
| 400 | */ |
| 401 | void mbedtls_ssl_ticket_free( mbedtls_ssl_ticket_context *ctx ) |
| 402 | { |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 403 | mbedtls_cipher_free( &ctx->cipher ); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 404 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 405 | #if defined(MBEDTLS_THREADING_C) |
| 406 | mbedtls_mutex_free( &ctx->mutex ); |
| 407 | #endif |
| 408 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 409 | mbedtls_zeroize( ctx, sizeof( mbedtls_ssl_ticket_context ) ); |
| 410 | } |
| 411 | |
Manuel Pégourié-Gonnard | fd6d897 | 2015-05-15 12:09:00 +0200 | [diff] [blame] | 412 | #endif /* MBEDTLS_SSL_TICKET_C */ |