The WDCInterface is is a drop-in replacement for an EthernetInterface class that allows the user to connect to the Internet with a Wistron NeWeb Corporation (WNC) M14A2A Series data module using the standard network Socket API's. This interface class is used in the AT&T Cellular IoT Starter Kit which is sold by Avnet (http://cloudconnectkits.org/product/att-cellular-iot-starter-kit).

Dependencies:   WncControllerK64F

Dependents:   WNCProximityMqtt Pubnub_ATT_IoT_SK_WNC_sync BluemixDemo BluemixQS ... more

See the WNCInterface README in the Wiki tab for detailed information on this library.

Committer:
JMF
Date:
Tue Nov 01 14:22:56 2016 +0000
Revision:
12:0071cb144c7a
Adding mbedtls files

Who changed what in which revision?

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