Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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