This is a fork due to permission issues

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of 6_songs-from-the-cloud by MakingMusicWorkshop

Committer:
timbeight
Date:
Thu May 19 16:02:10 2016 +0000
Revision:
1:0ddbe2d3319c
Parent:
0:f7c60d3e7b8a
This is my first commit while in the class.

Who changed what in which revision?

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