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