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