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