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, |
| 64 | uint32_t lifetime ) |
| 65 | { |
| 66 | int ret; |
| 67 | unsigned char buf[16]; |
| 68 | |
| 69 | ctx->f_rng = f_rng; |
| 70 | ctx->p_rng = p_rng; |
| 71 | |
| 72 | ctx->ticket_lifetime = lifetime; |
| 73 | |
| 74 | mbedtls_aes_init( &ctx->enc ); |
| 75 | mbedtls_aes_init( &ctx->dec ); |
| 76 | |
| 77 | if( ( ret = f_rng( p_rng, ctx->key_name, 16 ) != 0 ) || |
| 78 | ( ret = f_rng( p_rng, ctx->mac_key, 16 ) != 0 ) || |
| 79 | ( ret = f_rng( p_rng, buf, 16 ) != 0 ) ) |
| 80 | { |
| 81 | return( ret ); |
| 82 | } |
| 83 | |
| 84 | if( ( ret = mbedtls_aes_setkey_enc( &ctx->enc, buf, 128 ) ) != 0 || |
| 85 | ( ret = mbedtls_aes_setkey_dec( &ctx->dec, buf, 128 ) ) != 0 ) |
| 86 | { |
| 87 | mbedtls_ssl_ticket_free( ctx ); |
| 88 | return( ret ); |
| 89 | } |
| 90 | |
| 91 | mbedtls_zeroize( buf, sizeof( buf ) ); |
| 92 | |
| 93 | return( 0 ); |
| 94 | } |
| 95 | |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 96 | /* |
| 97 | * Serialize a session in the following format: |
| 98 | * 0 . n-1 session structure, n = sizeof(mbedtls_ssl_session) |
| 99 | * n . n+2 peer_cert length = m (0 if no certificate) |
| 100 | * n+3 . n+2+m peer cert ASN.1 |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 101 | */ |
| 102 | static int ssl_save_session( const mbedtls_ssl_session *session, |
| 103 | unsigned char *buf, size_t buf_len, |
| 104 | size_t *olen ) |
| 105 | { |
| 106 | unsigned char *p = buf; |
| 107 | size_t left = buf_len; |
| 108 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 109 | size_t cert_len; |
| 110 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 111 | |
| 112 | if( left < sizeof( mbedtls_ssl_session ) ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame^] | 113 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 114 | |
| 115 | memcpy( p, session, sizeof( mbedtls_ssl_session ) ); |
| 116 | p += sizeof( mbedtls_ssl_session ); |
| 117 | left -= sizeof( mbedtls_ssl_session ); |
| 118 | |
| 119 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 120 | if( session->peer_cert == NULL ) |
| 121 | cert_len = 0; |
| 122 | else |
| 123 | cert_len = session->peer_cert->raw.len; |
| 124 | |
| 125 | if( left < 3 + cert_len ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame^] | 126 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 127 | |
| 128 | *p++ = (unsigned char)( cert_len >> 16 & 0xFF ); |
| 129 | *p++ = (unsigned char)( cert_len >> 8 & 0xFF ); |
| 130 | *p++ = (unsigned char)( cert_len & 0xFF ); |
| 131 | |
| 132 | if( session->peer_cert != NULL ) |
| 133 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 134 | |
| 135 | p += cert_len; |
| 136 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 137 | |
| 138 | *olen = p - buf; |
| 139 | |
| 140 | return( 0 ); |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * Unserialise session, see ssl_save_session() |
| 145 | */ |
| 146 | static int ssl_load_session( mbedtls_ssl_session *session, |
| 147 | const unsigned char *buf, size_t len ) |
| 148 | { |
| 149 | const unsigned char *p = buf; |
| 150 | const unsigned char * const end = buf + len; |
| 151 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 152 | size_t cert_len; |
| 153 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 154 | |
| 155 | if( p + sizeof( mbedtls_ssl_session ) > end ) |
| 156 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 157 | |
| 158 | memcpy( session, p, sizeof( mbedtls_ssl_session ) ); |
| 159 | p += sizeof( mbedtls_ssl_session ); |
| 160 | |
| 161 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 162 | if( p + 3 > end ) |
| 163 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 164 | |
| 165 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 166 | p += 3; |
| 167 | |
| 168 | if( cert_len == 0 ) |
| 169 | { |
| 170 | session->peer_cert = NULL; |
| 171 | } |
| 172 | else |
| 173 | { |
| 174 | int ret; |
| 175 | |
| 176 | if( p + cert_len > end ) |
| 177 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 178 | |
| 179 | session->peer_cert = mbedtls_malloc( sizeof( mbedtls_x509_crt ) ); |
| 180 | |
| 181 | if( session->peer_cert == NULL ) |
| 182 | return( MBEDTLS_ERR_SSL_MALLOC_FAILED ); |
| 183 | |
| 184 | mbedtls_x509_crt_init( session->peer_cert ); |
| 185 | |
| 186 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
| 187 | p, cert_len ) ) != 0 ) |
| 188 | { |
| 189 | mbedtls_x509_crt_free( session->peer_cert ); |
| 190 | mbedtls_free( session->peer_cert ); |
| 191 | session->peer_cert = NULL; |
| 192 | return( ret ); |
| 193 | } |
| 194 | |
| 195 | p += cert_len; |
| 196 | } |
| 197 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 198 | |
| 199 | if( p != end ) |
| 200 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 201 | |
| 202 | return( 0 ); |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * Create session ticket, secured as recommended in RFC 5077 section 4: |
| 207 | * |
| 208 | * struct { |
| 209 | * opaque key_name[16]; |
| 210 | * opaque iv[16]; |
| 211 | * opaque encrypted_state<0..2^16-1>; |
| 212 | * opaque mac[32]; |
| 213 | * } ticket; |
| 214 | * |
| 215 | * (the internal state structure differs, however). |
| 216 | */ |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 217 | int mbedtls_ssl_ticket_write( void *p_ticket, |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 218 | const mbedtls_ssl_session *session, |
| 219 | unsigned char *start, |
| 220 | const unsigned char *end, |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 221 | size_t *tlen, |
| 222 | uint32_t *ticket_lifetime ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 223 | { |
| 224 | int ret; |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 225 | mbedtls_ssl_ticket_context *ctx = p_ticket; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 226 | unsigned char *p = start; |
| 227 | unsigned char *state; |
| 228 | unsigned char iv[16]; |
| 229 | size_t clear_len, enc_len, pad_len, i; |
| 230 | |
| 231 | *tlen = 0; |
| 232 | |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 233 | if( ctx == NULL ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 234 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 235 | |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 236 | /* We need at least 16 bytes for key_name, 16 for IV, 2 for len |
| 237 | * 16 for padding, 32 for MAC, in addition to session itself, |
| 238 | * that will be checked when writing it. */ |
| 239 | if( end - start < 16 + 16 + 2 + 16 + 32 ) |
| 240 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 241 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame^] | 242 | #if defined(MBEDTLS_THREADING_C) |
| 243 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 244 | return( ret ); |
| 245 | #endif |
| 246 | |
| 247 | *ticket_lifetime = ctx->ticket_lifetime; |
| 248 | |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 249 | /* Write key name */ |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 250 | memcpy( p, ctx->key_name, 16 ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 251 | p += 16; |
| 252 | |
| 253 | /* Generate and write IV (with a copy for aes_crypt) */ |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 254 | if( ( ret = ctx->f_rng( ctx->p_rng, p, 16 ) ) != 0 ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame^] | 255 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 256 | memcpy( iv, p, 16 ); |
| 257 | p += 16; |
| 258 | |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 259 | /* Dump session state */ |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 260 | state = p + 2; |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame^] | 261 | if( ( ret = ssl_save_session( session, |
| 262 | state, end - state, &clear_len ) ) != 0 ) |
| 263 | { |
| 264 | goto cleanup; |
| 265 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 266 | |
| 267 | /* Apply PKCS padding */ |
| 268 | pad_len = 16 - clear_len % 16; |
| 269 | enc_len = clear_len + pad_len; |
| 270 | for( i = clear_len; i < enc_len; i++ ) |
| 271 | state[i] = (unsigned char) pad_len; |
| 272 | |
| 273 | /* Encrypt */ |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 274 | if( ( ret = mbedtls_aes_crypt_cbc( &ctx->enc, MBEDTLS_AES_ENCRYPT, |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 275 | enc_len, iv, state, state ) ) != 0 ) |
| 276 | { |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame^] | 277 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /* Write length */ |
| 281 | *p++ = (unsigned char)( ( enc_len >> 8 ) & 0xFF ); |
| 282 | *p++ = (unsigned char)( ( enc_len ) & 0xFF ); |
| 283 | p = state + enc_len; |
| 284 | |
| 285 | /* Compute and write MAC( key_name + iv + enc_state_len + enc_state ) */ |
| 286 | if( ( ret = mbedtls_md_hmac( mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ), |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 287 | ctx->mac_key, 16, |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 288 | start, p - start, p ) ) != 0 ) |
| 289 | { |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame^] | 290 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 291 | } |
| 292 | p += 32; |
| 293 | |
| 294 | *tlen = p - start; |
| 295 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame^] | 296 | cleanup: |
| 297 | #if defined(MBEDTLS_THREADING_C) |
| 298 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
| 299 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
| 300 | #endif |
| 301 | |
| 302 | return( ret ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /* |
| 306 | * Load session ticket (see mbedtls_ssl_ticket_write for structure) |
| 307 | */ |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 308 | int mbedtls_ssl_ticket_parse( void *p_ticket, |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 309 | mbedtls_ssl_session *session, |
| 310 | unsigned char *buf, |
| 311 | size_t len ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 312 | { |
| 313 | int ret; |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 314 | mbedtls_ssl_ticket_context *ctx = p_ticket; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 315 | unsigned char *key_name = buf; |
| 316 | unsigned char *iv = buf + 16; |
| 317 | unsigned char *enc_len_p = iv + 16; |
| 318 | unsigned char *ticket = enc_len_p + 2; |
| 319 | unsigned char *mac; |
| 320 | unsigned char computed_mac[32]; |
| 321 | size_t enc_len, clear_len, i; |
| 322 | unsigned char pad_len, diff; |
| 323 | |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 324 | if( len < 34 || ctx == NULL ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 325 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 326 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame^] | 327 | #if defined(MBEDTLS_THREADING_C) |
| 328 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 329 | return( ret ); |
| 330 | #endif |
| 331 | |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 332 | enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1]; |
| 333 | mac = ticket + enc_len; |
| 334 | |
| 335 | if( len != enc_len + 66 ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame^] | 336 | { |
| 337 | ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 338 | goto cleanup; |
| 339 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 340 | |
| 341 | /* Check name, in constant time though it's not a big secret */ |
| 342 | diff = 0; |
| 343 | for( i = 0; i < 16; i++ ) |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 344 | diff |= key_name[i] ^ ctx->key_name[i]; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 345 | /* don't return yet, check the MAC anyway */ |
| 346 | |
| 347 | /* Check mac, with constant-time buffer comparison */ |
| 348 | if( ( ret = mbedtls_md_hmac( mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ), |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 349 | ctx->mac_key, 16, |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 350 | buf, len - 32, computed_mac ) ) != 0 ) |
| 351 | { |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame^] | 352 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | for( i = 0; i < 32; i++ ) |
| 356 | diff |= mac[i] ^ computed_mac[i]; |
| 357 | |
| 358 | /* Now return if ticket is not authentic, since we want to avoid |
| 359 | * decrypting arbitrary attacker-chosen data */ |
| 360 | if( diff != 0 ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame^] | 361 | { |
| 362 | ret = MBEDTLS_ERR_SSL_INVALID_MAC; |
| 363 | goto cleanup; |
| 364 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 365 | |
| 366 | /* Decrypt */ |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 367 | if( ( ret = mbedtls_aes_crypt_cbc( &ctx->dec, MBEDTLS_AES_DECRYPT, |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 368 | enc_len, iv, ticket, ticket ) ) != 0 ) |
| 369 | { |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame^] | 370 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | /* Check PKCS padding */ |
| 374 | pad_len = ticket[enc_len - 1]; |
| 375 | |
| 376 | ret = 0; |
| 377 | for( i = 2; i < pad_len; i++ ) |
| 378 | if( ticket[enc_len - i] != pad_len ) |
| 379 | ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 380 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame^] | 381 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 382 | |
| 383 | clear_len = enc_len - pad_len; |
| 384 | |
| 385 | /* Actually load session */ |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 386 | if( ( ret = ssl_load_session( session, ticket, clear_len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame^] | 387 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 388 | |
| 389 | #if defined(MBEDTLS_HAVE_TIME) |
| 390 | /* Check if still valid */ |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 391 | if( ( time( NULL) - session->start ) > ctx->ticket_lifetime ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame^] | 392 | { |
| 393 | ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED; |
| 394 | goto cleanup; |
| 395 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 396 | #endif |
| 397 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame^] | 398 | cleanup: |
| 399 | #if defined(MBEDTLS_THREADING_C) |
| 400 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
| 401 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
| 402 | #endif |
| 403 | |
| 404 | return( ret ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 405 | } |
| 406 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 407 | /* |
| 408 | * Free context |
| 409 | */ |
| 410 | void mbedtls_ssl_ticket_free( mbedtls_ssl_ticket_context *ctx ) |
| 411 | { |
| 412 | mbedtls_aes_free( &ctx->enc ); |
| 413 | mbedtls_aes_free( &ctx->dec ); |
| 414 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame^] | 415 | #if defined(MBEDTLS_THREADING_C) |
| 416 | mbedtls_mutex_free( &ctx->mutex ); |
| 417 | #endif |
| 418 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 419 | mbedtls_zeroize( ctx, sizeof( mbedtls_ssl_ticket_context ) ); |
| 420 | } |
| 421 | |
Manuel Pégourié-Gonnard | fd6d897 | 2015-05-15 12:09:00 +0200 | [diff] [blame] | 422 | #endif /* MBEDTLS_SSL_TICKET_C */ |