mbed TLS Build

Dependents:   Slave-prot-prod

Committer:
markrad
Date:
Thu Jan 05 00:18:44 2017 +0000
Revision:
0:cdf462088d13
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
markrad 0:cdf462088d13 1 /*
markrad 0:cdf462088d13 2 * TLS server tickets callbacks implementation
markrad 0:cdf462088d13 3 *
markrad 0:cdf462088d13 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
markrad 0:cdf462088d13 5 * SPDX-License-Identifier: Apache-2.0
markrad 0:cdf462088d13 6 *
markrad 0:cdf462088d13 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
markrad 0:cdf462088d13 8 * not use this file except in compliance with the License.
markrad 0:cdf462088d13 9 * You may obtain a copy of the License at
markrad 0:cdf462088d13 10 *
markrad 0:cdf462088d13 11 * http://www.apache.org/licenses/LICENSE-2.0
markrad 0:cdf462088d13 12 *
markrad 0:cdf462088d13 13 * Unless required by applicable law or agreed to in writing, software
markrad 0:cdf462088d13 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
markrad 0:cdf462088d13 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
markrad 0:cdf462088d13 16 * See the License for the specific language governing permissions and
markrad 0:cdf462088d13 17 * limitations under the License.
markrad 0:cdf462088d13 18 *
markrad 0:cdf462088d13 19 * This file is part of mbed TLS (https://tls.mbed.org)
markrad 0:cdf462088d13 20 */
markrad 0:cdf462088d13 21
markrad 0:cdf462088d13 22 #if !defined(MBEDTLS_CONFIG_FILE)
markrad 0:cdf462088d13 23 #include "mbedtls/config.h"
markrad 0:cdf462088d13 24 #else
markrad 0:cdf462088d13 25 #include MBEDTLS_CONFIG_FILE
markrad 0:cdf462088d13 26 #endif
markrad 0:cdf462088d13 27
markrad 0:cdf462088d13 28 #if defined(MBEDTLS_SSL_TICKET_C)
markrad 0:cdf462088d13 29
markrad 0:cdf462088d13 30 #if defined(MBEDTLS_PLATFORM_C)
markrad 0:cdf462088d13 31 #include "mbedtls/platform.h"
markrad 0:cdf462088d13 32 #else
markrad 0:cdf462088d13 33 #include <stdlib.h>
markrad 0:cdf462088d13 34 #define mbedtls_calloc calloc
markrad 0:cdf462088d13 35 #define mbedtls_free free
markrad 0:cdf462088d13 36 #endif
markrad 0:cdf462088d13 37
markrad 0:cdf462088d13 38 #include "mbedtls/ssl_ticket.h"
markrad 0:cdf462088d13 39
markrad 0:cdf462088d13 40 #include <string.h>
markrad 0:cdf462088d13 41
markrad 0:cdf462088d13 42 /* Implementation that should never be optimized out by the compiler */
markrad 0:cdf462088d13 43 static void mbedtls_zeroize( void *v, size_t n ) {
markrad 0:cdf462088d13 44 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
markrad 0:cdf462088d13 45 }
markrad 0:cdf462088d13 46
markrad 0:cdf462088d13 47 /*
markrad 0:cdf462088d13 48 * Initialze context
markrad 0:cdf462088d13 49 */
markrad 0:cdf462088d13 50 void mbedtls_ssl_ticket_init( mbedtls_ssl_ticket_context *ctx )
markrad 0:cdf462088d13 51 {
markrad 0:cdf462088d13 52 memset( ctx, 0, sizeof( mbedtls_ssl_ticket_context ) );
markrad 0:cdf462088d13 53
markrad 0:cdf462088d13 54 #if defined(MBEDTLS_THREADING_C)
markrad 0:cdf462088d13 55 mbedtls_mutex_init( &ctx->mutex );
markrad 0:cdf462088d13 56 #endif
markrad 0:cdf462088d13 57 }
markrad 0:cdf462088d13 58
markrad 0:cdf462088d13 59 #define MAX_KEY_BYTES 32 /* 256 bits */
markrad 0:cdf462088d13 60
markrad 0:cdf462088d13 61 /*
markrad 0:cdf462088d13 62 * Generate/update a key
markrad 0:cdf462088d13 63 */
markrad 0:cdf462088d13 64 static int ssl_ticket_gen_key( mbedtls_ssl_ticket_context *ctx,
markrad 0:cdf462088d13 65 unsigned char index )
markrad 0:cdf462088d13 66 {
markrad 0:cdf462088d13 67 int ret;
markrad 0:cdf462088d13 68 unsigned char buf[MAX_KEY_BYTES];
markrad 0:cdf462088d13 69 mbedtls_ssl_ticket_key *key = ctx->keys + index;
markrad 0:cdf462088d13 70
markrad 0:cdf462088d13 71 #if defined(MBEDTLS_HAVE_TIME)
markrad 0:cdf462088d13 72 key->generation_time = (uint32_t) mbedtls_time( NULL );
markrad 0:cdf462088d13 73 #endif
markrad 0:cdf462088d13 74
markrad 0:cdf462088d13 75 if( ( ret = ctx->f_rng( ctx->p_rng, key->name, sizeof( key->name ) ) ) != 0 )
markrad 0:cdf462088d13 76 return( ret );
markrad 0:cdf462088d13 77
markrad 0:cdf462088d13 78 if( ( ret = ctx->f_rng( ctx->p_rng, buf, sizeof( buf ) ) ) != 0 )
markrad 0:cdf462088d13 79 return( ret );
markrad 0:cdf462088d13 80
markrad 0:cdf462088d13 81 /* With GCM and CCM, same context can encrypt & decrypt */
markrad 0:cdf462088d13 82 ret = mbedtls_cipher_setkey( &key->ctx, buf,
markrad 0:cdf462088d13 83 mbedtls_cipher_get_key_bitlen( &key->ctx ),
markrad 0:cdf462088d13 84 MBEDTLS_ENCRYPT );
markrad 0:cdf462088d13 85
markrad 0:cdf462088d13 86 mbedtls_zeroize( buf, sizeof( buf ) );
markrad 0:cdf462088d13 87
markrad 0:cdf462088d13 88 return( ret );
markrad 0:cdf462088d13 89 }
markrad 0:cdf462088d13 90
markrad 0:cdf462088d13 91 /*
markrad 0:cdf462088d13 92 * Rotate/generate keys if necessary
markrad 0:cdf462088d13 93 */
markrad 0:cdf462088d13 94 static int ssl_ticket_update_keys( mbedtls_ssl_ticket_context *ctx )
markrad 0:cdf462088d13 95 {
markrad 0:cdf462088d13 96 #if !defined(MBEDTLS_HAVE_TIME)
markrad 0:cdf462088d13 97 ((void) ctx);
markrad 0:cdf462088d13 98 #else
markrad 0:cdf462088d13 99 if( ctx->ticket_lifetime != 0 )
markrad 0:cdf462088d13 100 {
markrad 0:cdf462088d13 101 uint32_t current_time = (uint32_t) mbedtls_time( NULL );
markrad 0:cdf462088d13 102 uint32_t key_time = ctx->keys[ctx->active].generation_time;
markrad 0:cdf462088d13 103
markrad 0:cdf462088d13 104 if( current_time > key_time &&
markrad 0:cdf462088d13 105 current_time - key_time < ctx->ticket_lifetime )
markrad 0:cdf462088d13 106 {
markrad 0:cdf462088d13 107 return( 0 );
markrad 0:cdf462088d13 108 }
markrad 0:cdf462088d13 109
markrad 0:cdf462088d13 110 ctx->active = 1 - ctx->active;
markrad 0:cdf462088d13 111
markrad 0:cdf462088d13 112 return( ssl_ticket_gen_key( ctx, ctx->active ) );
markrad 0:cdf462088d13 113 }
markrad 0:cdf462088d13 114 else
markrad 0:cdf462088d13 115 #endif /* MBEDTLS_HAVE_TIME */
markrad 0:cdf462088d13 116 return( 0 );
markrad 0:cdf462088d13 117 }
markrad 0:cdf462088d13 118
markrad 0:cdf462088d13 119 /*
markrad 0:cdf462088d13 120 * Setup context for actual use
markrad 0:cdf462088d13 121 */
markrad 0:cdf462088d13 122 int mbedtls_ssl_ticket_setup( mbedtls_ssl_ticket_context *ctx,
markrad 0:cdf462088d13 123 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
markrad 0:cdf462088d13 124 mbedtls_cipher_type_t cipher,
markrad 0:cdf462088d13 125 uint32_t lifetime )
markrad 0:cdf462088d13 126 {
markrad 0:cdf462088d13 127 int ret;
markrad 0:cdf462088d13 128 const mbedtls_cipher_info_t *cipher_info;
markrad 0:cdf462088d13 129
markrad 0:cdf462088d13 130 ctx->f_rng = f_rng;
markrad 0:cdf462088d13 131 ctx->p_rng = p_rng;
markrad 0:cdf462088d13 132
markrad 0:cdf462088d13 133 ctx->ticket_lifetime = lifetime;
markrad 0:cdf462088d13 134
markrad 0:cdf462088d13 135 cipher_info = mbedtls_cipher_info_from_type( cipher);
markrad 0:cdf462088d13 136 if( cipher_info == NULL )
markrad 0:cdf462088d13 137 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
markrad 0:cdf462088d13 138
markrad 0:cdf462088d13 139 if( cipher_info->mode != MBEDTLS_MODE_GCM &&
markrad 0:cdf462088d13 140 cipher_info->mode != MBEDTLS_MODE_CCM )
markrad 0:cdf462088d13 141 {
markrad 0:cdf462088d13 142 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
markrad 0:cdf462088d13 143 }
markrad 0:cdf462088d13 144
markrad 0:cdf462088d13 145 if( cipher_info->key_bitlen > 8 * MAX_KEY_BYTES )
markrad 0:cdf462088d13 146 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
markrad 0:cdf462088d13 147
markrad 0:cdf462088d13 148 if( ( ret = mbedtls_cipher_setup( &ctx->keys[0].ctx, cipher_info ) ) != 0 ||
markrad 0:cdf462088d13 149 ( ret = mbedtls_cipher_setup( &ctx->keys[1].ctx, cipher_info ) ) != 0 )
markrad 0:cdf462088d13 150 {
markrad 0:cdf462088d13 151 return( ret );
markrad 0:cdf462088d13 152 }
markrad 0:cdf462088d13 153
markrad 0:cdf462088d13 154 if( ( ret = ssl_ticket_gen_key( ctx, 0 ) ) != 0 ||
markrad 0:cdf462088d13 155 ( ret = ssl_ticket_gen_key( ctx, 1 ) ) != 0 )
markrad 0:cdf462088d13 156 {
markrad 0:cdf462088d13 157 return( ret );
markrad 0:cdf462088d13 158 }
markrad 0:cdf462088d13 159
markrad 0:cdf462088d13 160 return( 0 );
markrad 0:cdf462088d13 161 }
markrad 0:cdf462088d13 162
markrad 0:cdf462088d13 163 /*
markrad 0:cdf462088d13 164 * Serialize a session in the following format:
markrad 0:cdf462088d13 165 * 0 . n-1 session structure, n = sizeof(mbedtls_ssl_session)
markrad 0:cdf462088d13 166 * n . n+2 peer_cert length = m (0 if no certificate)
markrad 0:cdf462088d13 167 * n+3 . n+2+m peer cert ASN.1
markrad 0:cdf462088d13 168 */
markrad 0:cdf462088d13 169 static int ssl_save_session( const mbedtls_ssl_session *session,
markrad 0:cdf462088d13 170 unsigned char *buf, size_t buf_len,
markrad 0:cdf462088d13 171 size_t *olen )
markrad 0:cdf462088d13 172 {
markrad 0:cdf462088d13 173 unsigned char *p = buf;
markrad 0:cdf462088d13 174 size_t left = buf_len;
markrad 0:cdf462088d13 175 #if defined(MBEDTLS_X509_CRT_PARSE_C)
markrad 0:cdf462088d13 176 size_t cert_len;
markrad 0:cdf462088d13 177 #endif /* MBEDTLS_X509_CRT_PARSE_C */
markrad 0:cdf462088d13 178
markrad 0:cdf462088d13 179 if( left < sizeof( mbedtls_ssl_session ) )
markrad 0:cdf462088d13 180 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
markrad 0:cdf462088d13 181
markrad 0:cdf462088d13 182 memcpy( p, session, sizeof( mbedtls_ssl_session ) );
markrad 0:cdf462088d13 183 p += sizeof( mbedtls_ssl_session );
markrad 0:cdf462088d13 184 left -= sizeof( mbedtls_ssl_session );
markrad 0:cdf462088d13 185
markrad 0:cdf462088d13 186 #if defined(MBEDTLS_X509_CRT_PARSE_C)
markrad 0:cdf462088d13 187 if( session->peer_cert == NULL )
markrad 0:cdf462088d13 188 cert_len = 0;
markrad 0:cdf462088d13 189 else
markrad 0:cdf462088d13 190 cert_len = session->peer_cert->raw.len;
markrad 0:cdf462088d13 191
markrad 0:cdf462088d13 192 if( left < 3 + cert_len )
markrad 0:cdf462088d13 193 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
markrad 0:cdf462088d13 194
markrad 0:cdf462088d13 195 *p++ = (unsigned char)( cert_len >> 16 & 0xFF );
markrad 0:cdf462088d13 196 *p++ = (unsigned char)( cert_len >> 8 & 0xFF );
markrad 0:cdf462088d13 197 *p++ = (unsigned char)( cert_len & 0xFF );
markrad 0:cdf462088d13 198
markrad 0:cdf462088d13 199 if( session->peer_cert != NULL )
markrad 0:cdf462088d13 200 memcpy( p, session->peer_cert->raw.p, cert_len );
markrad 0:cdf462088d13 201
markrad 0:cdf462088d13 202 p += cert_len;
markrad 0:cdf462088d13 203 #endif /* MBEDTLS_X509_CRT_PARSE_C */
markrad 0:cdf462088d13 204
markrad 0:cdf462088d13 205 *olen = p - buf;
markrad 0:cdf462088d13 206
markrad 0:cdf462088d13 207 return( 0 );
markrad 0:cdf462088d13 208 }
markrad 0:cdf462088d13 209
markrad 0:cdf462088d13 210 /*
markrad 0:cdf462088d13 211 * Unserialise session, see ssl_save_session()
markrad 0:cdf462088d13 212 */
markrad 0:cdf462088d13 213 static int ssl_load_session( mbedtls_ssl_session *session,
markrad 0:cdf462088d13 214 const unsigned char *buf, size_t len )
markrad 0:cdf462088d13 215 {
markrad 0:cdf462088d13 216 const unsigned char *p = buf;
markrad 0:cdf462088d13 217 const unsigned char * const end = buf + len;
markrad 0:cdf462088d13 218 #if defined(MBEDTLS_X509_CRT_PARSE_C)
markrad 0:cdf462088d13 219 size_t cert_len;
markrad 0:cdf462088d13 220 #endif /* MBEDTLS_X509_CRT_PARSE_C */
markrad 0:cdf462088d13 221
markrad 0:cdf462088d13 222 if( p + sizeof( mbedtls_ssl_session ) > end )
markrad 0:cdf462088d13 223 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
markrad 0:cdf462088d13 224
markrad 0:cdf462088d13 225 memcpy( session, p, sizeof( mbedtls_ssl_session ) );
markrad 0:cdf462088d13 226 p += sizeof( mbedtls_ssl_session );
markrad 0:cdf462088d13 227
markrad 0:cdf462088d13 228 #if defined(MBEDTLS_X509_CRT_PARSE_C)
markrad 0:cdf462088d13 229 if( p + 3 > end )
markrad 0:cdf462088d13 230 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
markrad 0:cdf462088d13 231
markrad 0:cdf462088d13 232 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
markrad 0:cdf462088d13 233 p += 3;
markrad 0:cdf462088d13 234
markrad 0:cdf462088d13 235 if( cert_len == 0 )
markrad 0:cdf462088d13 236 {
markrad 0:cdf462088d13 237 session->peer_cert = NULL;
markrad 0:cdf462088d13 238 }
markrad 0:cdf462088d13 239 else
markrad 0:cdf462088d13 240 {
markrad 0:cdf462088d13 241 int ret;
markrad 0:cdf462088d13 242
markrad 0:cdf462088d13 243 if( p + cert_len > end )
markrad 0:cdf462088d13 244 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
markrad 0:cdf462088d13 245
markrad 0:cdf462088d13 246 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
markrad 0:cdf462088d13 247
markrad 0:cdf462088d13 248 if( session->peer_cert == NULL )
markrad 0:cdf462088d13 249 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
markrad 0:cdf462088d13 250
markrad 0:cdf462088d13 251 mbedtls_x509_crt_init( session->peer_cert );
markrad 0:cdf462088d13 252
markrad 0:cdf462088d13 253 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
markrad 0:cdf462088d13 254 p, cert_len ) ) != 0 )
markrad 0:cdf462088d13 255 {
markrad 0:cdf462088d13 256 mbedtls_x509_crt_free( session->peer_cert );
markrad 0:cdf462088d13 257 mbedtls_free( session->peer_cert );
markrad 0:cdf462088d13 258 session->peer_cert = NULL;
markrad 0:cdf462088d13 259 return( ret );
markrad 0:cdf462088d13 260 }
markrad 0:cdf462088d13 261
markrad 0:cdf462088d13 262 p += cert_len;
markrad 0:cdf462088d13 263 }
markrad 0:cdf462088d13 264 #endif /* MBEDTLS_X509_CRT_PARSE_C */
markrad 0:cdf462088d13 265
markrad 0:cdf462088d13 266 if( p != end )
markrad 0:cdf462088d13 267 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
markrad 0:cdf462088d13 268
markrad 0:cdf462088d13 269 return( 0 );
markrad 0:cdf462088d13 270 }
markrad 0:cdf462088d13 271
markrad 0:cdf462088d13 272 /*
markrad 0:cdf462088d13 273 * Create session ticket, with the following structure:
markrad 0:cdf462088d13 274 *
markrad 0:cdf462088d13 275 * struct {
markrad 0:cdf462088d13 276 * opaque key_name[4];
markrad 0:cdf462088d13 277 * opaque iv[12];
markrad 0:cdf462088d13 278 * opaque encrypted_state<0..2^16-1>;
markrad 0:cdf462088d13 279 * opaque tag[16];
markrad 0:cdf462088d13 280 * } ticket;
markrad 0:cdf462088d13 281 *
markrad 0:cdf462088d13 282 * The key_name, iv, and length of encrypted_state are the additional
markrad 0:cdf462088d13 283 * authenticated data.
markrad 0:cdf462088d13 284 */
markrad 0:cdf462088d13 285 int mbedtls_ssl_ticket_write( void *p_ticket,
markrad 0:cdf462088d13 286 const mbedtls_ssl_session *session,
markrad 0:cdf462088d13 287 unsigned char *start,
markrad 0:cdf462088d13 288 const unsigned char *end,
markrad 0:cdf462088d13 289 size_t *tlen,
markrad 0:cdf462088d13 290 uint32_t *ticket_lifetime )
markrad 0:cdf462088d13 291 {
markrad 0:cdf462088d13 292 int ret;
markrad 0:cdf462088d13 293 mbedtls_ssl_ticket_context *ctx = p_ticket;
markrad 0:cdf462088d13 294 mbedtls_ssl_ticket_key *key;
markrad 0:cdf462088d13 295 unsigned char *key_name = start;
markrad 0:cdf462088d13 296 unsigned char *iv = start + 4;
markrad 0:cdf462088d13 297 unsigned char *state_len_bytes = iv + 12;
markrad 0:cdf462088d13 298 unsigned char *state = state_len_bytes + 2;
markrad 0:cdf462088d13 299 unsigned char *tag;
markrad 0:cdf462088d13 300 size_t clear_len, ciph_len;
markrad 0:cdf462088d13 301
markrad 0:cdf462088d13 302 *tlen = 0;
markrad 0:cdf462088d13 303
markrad 0:cdf462088d13 304 if( ctx == NULL || ctx->f_rng == NULL )
markrad 0:cdf462088d13 305 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
markrad 0:cdf462088d13 306
markrad 0:cdf462088d13 307 /* We need at least 4 bytes for key_name, 12 for IV, 2 for len 16 for tag,
markrad 0:cdf462088d13 308 * in addition to session itself, that will be checked when writing it. */
markrad 0:cdf462088d13 309 if( end - start < 4 + 12 + 2 + 16 )
markrad 0:cdf462088d13 310 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
markrad 0:cdf462088d13 311
markrad 0:cdf462088d13 312 #if defined(MBEDTLS_THREADING_C)
markrad 0:cdf462088d13 313 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
markrad 0:cdf462088d13 314 return( ret );
markrad 0:cdf462088d13 315 #endif
markrad 0:cdf462088d13 316
markrad 0:cdf462088d13 317 if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 )
markrad 0:cdf462088d13 318 goto cleanup;
markrad 0:cdf462088d13 319
markrad 0:cdf462088d13 320 key = &ctx->keys[ctx->active];
markrad 0:cdf462088d13 321
markrad 0:cdf462088d13 322 *ticket_lifetime = ctx->ticket_lifetime;
markrad 0:cdf462088d13 323
markrad 0:cdf462088d13 324 memcpy( key_name, key->name, 4 );
markrad 0:cdf462088d13 325
markrad 0:cdf462088d13 326 if( ( ret = ctx->f_rng( ctx->p_rng, iv, 12 ) ) != 0 )
markrad 0:cdf462088d13 327 goto cleanup;
markrad 0:cdf462088d13 328
markrad 0:cdf462088d13 329 /* Dump session state */
markrad 0:cdf462088d13 330 if( ( ret = ssl_save_session( session,
markrad 0:cdf462088d13 331 state, end - state, &clear_len ) ) != 0 ||
markrad 0:cdf462088d13 332 (unsigned long) clear_len > 65535 )
markrad 0:cdf462088d13 333 {
markrad 0:cdf462088d13 334 goto cleanup;
markrad 0:cdf462088d13 335 }
markrad 0:cdf462088d13 336 state_len_bytes[0] = ( clear_len >> 8 ) & 0xff;
markrad 0:cdf462088d13 337 state_len_bytes[1] = ( clear_len ) & 0xff;
markrad 0:cdf462088d13 338
markrad 0:cdf462088d13 339 /* Encrypt and authenticate */
markrad 0:cdf462088d13 340 tag = state + clear_len;
markrad 0:cdf462088d13 341 if( ( ret = mbedtls_cipher_auth_encrypt( &key->ctx,
markrad 0:cdf462088d13 342 iv, 12, key_name, 4 + 12 + 2,
markrad 0:cdf462088d13 343 state, clear_len, state, &ciph_len, tag, 16 ) ) != 0 )
markrad 0:cdf462088d13 344 {
markrad 0:cdf462088d13 345 goto cleanup;
markrad 0:cdf462088d13 346 }
markrad 0:cdf462088d13 347 if( ciph_len != clear_len )
markrad 0:cdf462088d13 348 {
markrad 0:cdf462088d13 349 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
markrad 0:cdf462088d13 350 goto cleanup;
markrad 0:cdf462088d13 351 }
markrad 0:cdf462088d13 352
markrad 0:cdf462088d13 353 *tlen = 4 + 12 + 2 + 16 + ciph_len;
markrad 0:cdf462088d13 354
markrad 0:cdf462088d13 355 cleanup:
markrad 0:cdf462088d13 356 #if defined(MBEDTLS_THREADING_C)
markrad 0:cdf462088d13 357 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
markrad 0:cdf462088d13 358 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
markrad 0:cdf462088d13 359 #endif
markrad 0:cdf462088d13 360
markrad 0:cdf462088d13 361 return( ret );
markrad 0:cdf462088d13 362 }
markrad 0:cdf462088d13 363
markrad 0:cdf462088d13 364 /*
markrad 0:cdf462088d13 365 * Select key based on name
markrad 0:cdf462088d13 366 */
markrad 0:cdf462088d13 367 static mbedtls_ssl_ticket_key *ssl_ticket_select_key(
markrad 0:cdf462088d13 368 mbedtls_ssl_ticket_context *ctx,
markrad 0:cdf462088d13 369 const unsigned char name[4] )
markrad 0:cdf462088d13 370 {
markrad 0:cdf462088d13 371 unsigned char i;
markrad 0:cdf462088d13 372
markrad 0:cdf462088d13 373 for( i = 0; i < sizeof( ctx->keys ) / sizeof( *ctx->keys ); i++ )
markrad 0:cdf462088d13 374 if( memcmp( name, ctx->keys[i].name, 4 ) == 0 )
markrad 0:cdf462088d13 375 return( &ctx->keys[i] );
markrad 0:cdf462088d13 376
markrad 0:cdf462088d13 377 return( NULL );
markrad 0:cdf462088d13 378 }
markrad 0:cdf462088d13 379
markrad 0:cdf462088d13 380 /*
markrad 0:cdf462088d13 381 * Load session ticket (see mbedtls_ssl_ticket_write for structure)
markrad 0:cdf462088d13 382 */
markrad 0:cdf462088d13 383 int mbedtls_ssl_ticket_parse( void *p_ticket,
markrad 0:cdf462088d13 384 mbedtls_ssl_session *session,
markrad 0:cdf462088d13 385 unsigned char *buf,
markrad 0:cdf462088d13 386 size_t len )
markrad 0:cdf462088d13 387 {
markrad 0:cdf462088d13 388 int ret;
markrad 0:cdf462088d13 389 mbedtls_ssl_ticket_context *ctx = p_ticket;
markrad 0:cdf462088d13 390 mbedtls_ssl_ticket_key *key;
markrad 0:cdf462088d13 391 unsigned char *key_name = buf;
markrad 0:cdf462088d13 392 unsigned char *iv = buf + 4;
markrad 0:cdf462088d13 393 unsigned char *enc_len_p = iv + 12;
markrad 0:cdf462088d13 394 unsigned char *ticket = enc_len_p + 2;
markrad 0:cdf462088d13 395 unsigned char *tag;
markrad 0:cdf462088d13 396 size_t enc_len, clear_len;
markrad 0:cdf462088d13 397
markrad 0:cdf462088d13 398 if( ctx == NULL || ctx->f_rng == NULL )
markrad 0:cdf462088d13 399 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
markrad 0:cdf462088d13 400
markrad 0:cdf462088d13 401 /* See mbedtls_ssl_ticket_write() */
markrad 0:cdf462088d13 402 if( len < 4 + 12 + 2 + 16 )
markrad 0:cdf462088d13 403 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
markrad 0:cdf462088d13 404
markrad 0:cdf462088d13 405 #if defined(MBEDTLS_THREADING_C)
markrad 0:cdf462088d13 406 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
markrad 0:cdf462088d13 407 return( ret );
markrad 0:cdf462088d13 408 #endif
markrad 0:cdf462088d13 409
markrad 0:cdf462088d13 410 if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 )
markrad 0:cdf462088d13 411 goto cleanup;
markrad 0:cdf462088d13 412
markrad 0:cdf462088d13 413 enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1];
markrad 0:cdf462088d13 414 tag = ticket + enc_len;
markrad 0:cdf462088d13 415
markrad 0:cdf462088d13 416 if( len != 4 + 12 + 2 + enc_len + 16 )
markrad 0:cdf462088d13 417 {
markrad 0:cdf462088d13 418 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
markrad 0:cdf462088d13 419 goto cleanup;
markrad 0:cdf462088d13 420 }
markrad 0:cdf462088d13 421
markrad 0:cdf462088d13 422 /* Select key */
markrad 0:cdf462088d13 423 if( ( key = ssl_ticket_select_key( ctx, key_name ) ) == NULL )
markrad 0:cdf462088d13 424 {
markrad 0:cdf462088d13 425 /* We can't know for sure but this is a likely option unless we're
markrad 0:cdf462088d13 426 * under attack - this is only informative anyway */
markrad 0:cdf462088d13 427 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
markrad 0:cdf462088d13 428 goto cleanup;
markrad 0:cdf462088d13 429 }
markrad 0:cdf462088d13 430
markrad 0:cdf462088d13 431 /* Decrypt and authenticate */
markrad 0:cdf462088d13 432 if( ( ret = mbedtls_cipher_auth_decrypt( &key->ctx, iv, 12,
markrad 0:cdf462088d13 433 key_name, 4 + 12 + 2, ticket, enc_len,
markrad 0:cdf462088d13 434 ticket, &clear_len, tag, 16 ) ) != 0 )
markrad 0:cdf462088d13 435 {
markrad 0:cdf462088d13 436 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
markrad 0:cdf462088d13 437 ret = MBEDTLS_ERR_SSL_INVALID_MAC;
markrad 0:cdf462088d13 438
markrad 0:cdf462088d13 439 goto cleanup;
markrad 0:cdf462088d13 440 }
markrad 0:cdf462088d13 441 if( clear_len != enc_len )
markrad 0:cdf462088d13 442 {
markrad 0:cdf462088d13 443 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
markrad 0:cdf462088d13 444 goto cleanup;
markrad 0:cdf462088d13 445 }
markrad 0:cdf462088d13 446
markrad 0:cdf462088d13 447 /* Actually load session */
markrad 0:cdf462088d13 448 if( ( ret = ssl_load_session( session, ticket, clear_len ) ) != 0 )
markrad 0:cdf462088d13 449 goto cleanup;
markrad 0:cdf462088d13 450
markrad 0:cdf462088d13 451 #if defined(MBEDTLS_HAVE_TIME)
markrad 0:cdf462088d13 452 {
markrad 0:cdf462088d13 453 /* Check for expiration */
markrad 0:cdf462088d13 454 mbedtls_time_t current_time = mbedtls_time( NULL );
markrad 0:cdf462088d13 455
markrad 0:cdf462088d13 456 if( current_time < session->start ||
markrad 0:cdf462088d13 457 (uint32_t)( current_time - session->start ) > ctx->ticket_lifetime )
markrad 0:cdf462088d13 458 {
markrad 0:cdf462088d13 459 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
markrad 0:cdf462088d13 460 goto cleanup;
markrad 0:cdf462088d13 461 }
markrad 0:cdf462088d13 462 }
markrad 0:cdf462088d13 463 #endif
markrad 0:cdf462088d13 464
markrad 0:cdf462088d13 465 cleanup:
markrad 0:cdf462088d13 466 #if defined(MBEDTLS_THREADING_C)
markrad 0:cdf462088d13 467 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
markrad 0:cdf462088d13 468 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
markrad 0:cdf462088d13 469 #endif
markrad 0:cdf462088d13 470
markrad 0:cdf462088d13 471 return( ret );
markrad 0:cdf462088d13 472 }
markrad 0:cdf462088d13 473
markrad 0:cdf462088d13 474 /*
markrad 0:cdf462088d13 475 * Free context
markrad 0:cdf462088d13 476 */
markrad 0:cdf462088d13 477 void mbedtls_ssl_ticket_free( mbedtls_ssl_ticket_context *ctx )
markrad 0:cdf462088d13 478 {
markrad 0:cdf462088d13 479 mbedtls_cipher_free( &ctx->keys[0].ctx );
markrad 0:cdf462088d13 480 mbedtls_cipher_free( &ctx->keys[1].ctx );
markrad 0:cdf462088d13 481
markrad 0:cdf462088d13 482 #if defined(MBEDTLS_THREADING_C)
markrad 0:cdf462088d13 483 mbedtls_mutex_free( &ctx->mutex );
markrad 0:cdf462088d13 484 #endif
markrad 0:cdf462088d13 485
markrad 0:cdf462088d13 486 mbedtls_zeroize( ctx, sizeof( mbedtls_ssl_ticket_context ) );
markrad 0:cdf462088d13 487 }
markrad 0:cdf462088d13 488
markrad 0:cdf462088d13 489 #endif /* MBEDTLS_SSL_TICKET_C */