Manuel Pégourié-Gonnard | fd6d897 | 2015-05-15 12:09:00 +0200 | [diff] [blame] | 1 | /* |
| 2 | * TLS server tickets callbacks implementation |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
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 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 20 | #include "common.h" |
Manuel Pégourié-Gonnard | fd6d897 | 2015-05-15 12:09:00 +0200 | [diff] [blame] | 21 | |
| 22 | #if defined(MBEDTLS_SSL_TICKET_C) |
| 23 | |
Manuel Pégourié-Gonnard | fd6d897 | 2015-05-15 12:09:00 +0200 | [diff] [blame] | 24 | #include "mbedtls/platform.h" |
Manuel Pégourié-Gonnard | fd6d897 | 2015-05-15 12:09:00 +0200 | [diff] [blame] | 25 | |
Chris Jones | 84a773f | 2021-03-05 18:38:47 +0000 | [diff] [blame] | 26 | #include "ssl_misc.h" |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 27 | #include "mbedtls/ssl_ticket.h" |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 28 | #include "mbedtls/error.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 29 | #include "mbedtls/platform_util.h" |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 30 | |
Manuel Pégourié-Gonnard | fd6d897 | 2015-05-15 12:09:00 +0200 | [diff] [blame] | 31 | #include <string.h> |
| 32 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 33 | /* |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 34 | * Initialize context |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 35 | */ |
| 36 | void mbedtls_ssl_ticket_init( mbedtls_ssl_ticket_context *ctx ) |
| 37 | { |
| 38 | memset( ctx, 0, sizeof( mbedtls_ssl_ticket_context ) ); |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 39 | |
| 40 | #if defined(MBEDTLS_THREADING_C) |
| 41 | mbedtls_mutex_init( &ctx->mutex ); |
| 42 | #endif |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 43 | } |
| 44 | |
Glenn Strauss | a941b62 | 2022-02-09 15:24:56 -0500 | [diff] [blame] | 45 | #define MAX_KEY_BYTES MBEDTLS_SSL_TICKET_MAX_KEY_BYTES |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 46 | |
Glenn Strauss | a941b62 | 2022-02-09 15:24:56 -0500 | [diff] [blame] | 47 | #define TICKET_KEY_NAME_BYTES MBEDTLS_SSL_TICKET_KEY_NAME_BYTES |
Hanno Becker | d140d08 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 48 | #define TICKET_IV_BYTES 12 |
| 49 | #define TICKET_CRYPT_LEN_BYTES 2 |
| 50 | #define TICKET_AUTH_TAG_BYTES 16 |
| 51 | |
| 52 | #define TICKET_MIN_LEN ( TICKET_KEY_NAME_BYTES + \ |
| 53 | TICKET_IV_BYTES + \ |
| 54 | TICKET_CRYPT_LEN_BYTES + \ |
| 55 | TICKET_AUTH_TAG_BYTES ) |
| 56 | #define TICKET_ADD_DATA_LEN ( TICKET_KEY_NAME_BYTES + \ |
| 57 | TICKET_IV_BYTES + \ |
| 58 | TICKET_CRYPT_LEN_BYTES ) |
| 59 | |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 60 | /* |
| 61 | * Generate/update a key |
| 62 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 63 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 64 | static int ssl_ticket_gen_key( mbedtls_ssl_ticket_context *ctx, |
| 65 | unsigned char index ) |
| 66 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 67 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Leonid Rozenboim | a3008e7 | 2022-04-21 17:28:18 -0700 | [diff] [blame] | 68 | unsigned char buf[MAX_KEY_BYTES] = {0}; |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 69 | mbedtls_ssl_ticket_key *key = ctx->keys + index; |
| 70 | |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 71 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 72 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 73 | #endif |
| 74 | |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 75 | #if defined(MBEDTLS_HAVE_TIME) |
Dave Rodgman | 392f714 | 2022-08-17 11:19:41 +0100 | [diff] [blame] | 76 | key->generation_time = mbedtls_time( NULL ); |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 77 | #endif |
| 78 | |
| 79 | if( ( ret = ctx->f_rng( ctx->p_rng, key->name, sizeof( key->name ) ) ) != 0 ) |
| 80 | return( ret ); |
| 81 | |
| 82 | if( ( ret = ctx->f_rng( ctx->p_rng, buf, sizeof( buf ) ) ) != 0 ) |
| 83 | return( ret ); |
| 84 | |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 85 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 86 | psa_set_key_usage_flags( &attributes, |
| 87 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 88 | psa_set_key_algorithm( &attributes, key->alg ); |
| 89 | psa_set_key_type( &attributes, key->key_type ); |
| 90 | psa_set_key_bits( &attributes, key->key_bits ); |
| 91 | |
| 92 | ret = psa_ssl_status_to_mbedtls( |
| 93 | psa_import_key( &attributes, buf, |
| 94 | PSA_BITS_TO_BYTES( key->key_bits ), |
| 95 | &key->key ) ); |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 96 | #else |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 97 | /* With GCM and CCM, same context can encrypt & decrypt */ |
| 98 | ret = mbedtls_cipher_setkey( &key->ctx, buf, |
Manuel Pégourié-Gonnard | 097c7bb | 2015-06-18 16:43:38 +0200 | [diff] [blame] | 99 | mbedtls_cipher_get_key_bitlen( &key->ctx ), |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 100 | MBEDTLS_ENCRYPT ); |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 101 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 102 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 103 | mbedtls_platform_zeroize( buf, sizeof( buf ) ); |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 104 | |
| 105 | return( ret ); |
| 106 | } |
| 107 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 108 | /* |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 109 | * Rotate/generate keys if necessary |
| 110 | */ |
Manuel Pégourié-Gonnard | a3115dc | 2022-06-17 10:52:54 +0200 | [diff] [blame] | 111 | MBEDTLS_CHECK_RETURN_CRITICAL |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 112 | static int ssl_ticket_update_keys( mbedtls_ssl_ticket_context *ctx ) |
| 113 | { |
| 114 | #if !defined(MBEDTLS_HAVE_TIME) |
| 115 | ((void) ctx); |
| 116 | #else |
| 117 | if( ctx->ticket_lifetime != 0 ) |
| 118 | { |
Dave Rodgman | 392f714 | 2022-08-17 11:19:41 +0100 | [diff] [blame] | 119 | mbedtls_time_t current_time = mbedtls_time( NULL ); |
| 120 | mbedtls_time_t key_time = ctx->keys[ctx->active].generation_time; |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 121 | |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 122 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gabor Mezei | 5b8b890 | 2022-03-16 12:56:58 +0100 | [diff] [blame] | 123 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 124 | #endif |
| 125 | |
Hanno Becker | aa71500 | 2018-08-21 13:55:31 +0100 | [diff] [blame] | 126 | if( current_time >= key_time && |
Dave Rodgman | 86c333e | 2022-08-17 16:57:26 +0100 | [diff] [blame] | 127 | (uint64_t) ( current_time - key_time ) < ctx->ticket_lifetime ) |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 128 | { |
| 129 | return( 0 ); |
| 130 | } |
| 131 | |
| 132 | ctx->active = 1 - ctx->active; |
| 133 | |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 134 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gabor Mezei | 5b8b890 | 2022-03-16 12:56:58 +0100 | [diff] [blame] | 135 | if( ( status = psa_destroy_key( ctx->keys[ctx->active].key ) ) != PSA_SUCCESS ) |
| 136 | { |
Gabor Mezei | 103e08a | 2022-03-16 13:40:11 +0100 | [diff] [blame] | 137 | return psa_ssl_status_to_mbedtls( status ); |
Gabor Mezei | 5b8b890 | 2022-03-16 12:56:58 +0100 | [diff] [blame] | 138 | } |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 139 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 140 | |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 141 | return( ssl_ticket_gen_key( ctx, ctx->active ) ); |
| 142 | } |
| 143 | else |
| 144 | #endif /* MBEDTLS_HAVE_TIME */ |
| 145 | return( 0 ); |
| 146 | } |
| 147 | |
| 148 | /* |
Glenn Strauss | a950938 | 2022-02-02 23:32:18 -0500 | [diff] [blame] | 149 | * Rotate active session ticket encryption key |
| 150 | */ |
| 151 | int mbedtls_ssl_ticket_rotate( mbedtls_ssl_ticket_context *ctx, |
| 152 | const unsigned char *name, size_t nlength, |
| 153 | const unsigned char *k, size_t klength, |
| 154 | uint32_t lifetime ) |
| 155 | { |
| 156 | const unsigned char idx = 1 - ctx->active; |
| 157 | mbedtls_ssl_ticket_key * const key = ctx->keys + idx; |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 158 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 159 | |
| 160 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gabor Mezei | 5b8b890 | 2022-03-16 12:56:58 +0100 | [diff] [blame] | 161 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 162 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gabor Mezei | 36c9f51 | 2022-03-16 12:55:32 +0100 | [diff] [blame] | 163 | const size_t bitlen = key->key_bits; |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 164 | #else |
Glenn Strauss | a950938 | 2022-02-02 23:32:18 -0500 | [diff] [blame] | 165 | const int bitlen = mbedtls_cipher_get_key_bitlen( &key->ctx ); |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 166 | #endif |
| 167 | |
Glenn Strauss | a950938 | 2022-02-02 23:32:18 -0500 | [diff] [blame] | 168 | if( nlength < TICKET_KEY_NAME_BYTES || klength * 8 < (size_t)bitlen ) |
| 169 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
| 170 | |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 171 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gabor Mezei | 5b8b890 | 2022-03-16 12:56:58 +0100 | [diff] [blame] | 172 | if( ( status = psa_destroy_key( key->key ) ) != PSA_SUCCESS ) |
| 173 | { |
| 174 | ret = psa_ssl_status_to_mbedtls( status ); |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 175 | return( ret ); |
Gabor Mezei | 5b8b890 | 2022-03-16 12:56:58 +0100 | [diff] [blame] | 176 | } |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 177 | |
| 178 | psa_set_key_usage_flags( &attributes, |
| 179 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 180 | psa_set_key_algorithm( &attributes, key->alg ); |
| 181 | psa_set_key_type( &attributes, key->key_type ); |
| 182 | psa_set_key_bits( &attributes, key->key_bits ); |
| 183 | |
Gabor Mezei | 103e08a | 2022-03-16 13:40:11 +0100 | [diff] [blame] | 184 | if( ( status = psa_import_key( &attributes, k, |
| 185 | PSA_BITS_TO_BYTES( key->key_bits ), |
| 186 | &key->key ) ) != PSA_SUCCESS ) |
Gabor Mezei | 5b8b890 | 2022-03-16 12:56:58 +0100 | [diff] [blame] | 187 | { |
| 188 | ret = psa_ssl_status_to_mbedtls( status ); |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 189 | return( ret ); |
Gabor Mezei | 5b8b890 | 2022-03-16 12:56:58 +0100 | [diff] [blame] | 190 | } |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 191 | #else |
Glenn Strauss | a950938 | 2022-02-02 23:32:18 -0500 | [diff] [blame] | 192 | ret = mbedtls_cipher_setkey( &key->ctx, k, bitlen, MBEDTLS_ENCRYPT ); |
| 193 | if( ret != 0 ) |
| 194 | return( ret ); |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 195 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 196 | |
Glenn Strauss | a950938 | 2022-02-02 23:32:18 -0500 | [diff] [blame] | 197 | ctx->active = idx; |
| 198 | ctx->ticket_lifetime = lifetime; |
| 199 | memcpy( key->name, name, TICKET_KEY_NAME_BYTES ); |
| 200 | #if defined(MBEDTLS_HAVE_TIME) |
Dave Rodgman | 392f714 | 2022-08-17 11:19:41 +0100 | [diff] [blame] | 201 | key->generation_time = mbedtls_time( NULL ); |
Glenn Strauss | a950938 | 2022-02-02 23:32:18 -0500 | [diff] [blame] | 202 | #endif |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | /* |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 207 | * Setup context for actual use |
| 208 | */ |
| 209 | int mbedtls_ssl_ticket_setup( mbedtls_ssl_ticket_context *ctx, |
| 210 | 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] | 211 | mbedtls_cipher_type_t cipher, |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 212 | uint32_t lifetime ) |
| 213 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 214 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Neil Armstrong | 3bf040e | 2022-04-27 10:35:24 +0200 | [diff] [blame] | 215 | size_t key_bits; |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 216 | |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 217 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 218 | psa_algorithm_t alg; |
| 219 | psa_key_type_t key_type; |
Neil Armstrong | 858581e | 2022-04-01 18:03:15 +0200 | [diff] [blame] | 220 | #else |
| 221 | const mbedtls_cipher_info_t *cipher_info; |
| 222 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 223 | |
Neil Armstrong | 858581e | 2022-04-01 18:03:15 +0200 | [diff] [blame] | 224 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 225 | if( mbedtls_ssl_cipher_to_psa( cipher, TICKET_AUTH_TAG_BYTES, |
| 226 | &alg, &key_type, &key_bits ) != PSA_SUCCESS ) |
| 227 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 228 | |
| 229 | if( PSA_ALG_IS_AEAD( alg ) == 0 ) |
| 230 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Neil Armstrong | 858581e | 2022-04-01 18:03:15 +0200 | [diff] [blame] | 231 | #else |
Gabor Mezei | e6d867f | 2022-03-10 15:04:58 +0100 | [diff] [blame] | 232 | cipher_info = mbedtls_cipher_info_from_type( cipher ); |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 233 | |
Gilles Peskine | e720dbe | 2021-07-19 17:37:46 +0200 | [diff] [blame] | 234 | if( mbedtls_cipher_info_get_mode( cipher_info ) != MBEDTLS_MODE_GCM && |
Gabor Mezei | 49c8eb3 | 2022-03-10 16:13:17 +0100 | [diff] [blame] | 235 | mbedtls_cipher_info_get_mode( cipher_info ) != MBEDTLS_MODE_CCM && |
| 236 | mbedtls_cipher_info_get_mode( cipher_info ) != MBEDTLS_MODE_CHACHAPOLY ) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 237 | { |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 238 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 239 | } |
| 240 | |
Neil Armstrong | 3bf040e | 2022-04-27 10:35:24 +0200 | [diff] [blame] | 241 | key_bits = mbedtls_cipher_info_get_key_bitlen( cipher_info ); |
| 242 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 243 | |
| 244 | if( key_bits > 8 * MAX_KEY_BYTES ) |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 245 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 246 | |
Neil Armstrong | 3bf040e | 2022-04-27 10:35:24 +0200 | [diff] [blame] | 247 | ctx->f_rng = f_rng; |
| 248 | ctx->p_rng = p_rng; |
| 249 | |
| 250 | ctx->ticket_lifetime = lifetime; |
| 251 | |
| 252 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 253 | ctx->keys[0].alg = alg; |
| 254 | ctx->keys[0].key_type = key_type; |
| 255 | ctx->keys[0].key_bits = key_bits; |
| 256 | |
| 257 | ctx->keys[1].alg = alg; |
| 258 | ctx->keys[1].key_type = key_type; |
| 259 | ctx->keys[1].key_bits = key_bits; |
| 260 | #else |
Hanno Becker | 679d8ce | 2018-11-17 21:25:59 +0000 | [diff] [blame] | 261 | if( ( ret = mbedtls_cipher_setup( &ctx->keys[0].ctx, cipher_info ) ) != 0 ) |
| 262 | return( ret ); |
| 263 | |
Hanno Becker | 679d8ce | 2018-11-17 21:25:59 +0000 | [diff] [blame] | 264 | if( ( ret = mbedtls_cipher_setup( &ctx->keys[1].ctx, cipher_info ) ) != 0 ) |
| 265 | return( ret ); |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 266 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 267 | |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 268 | if( ( ret = ssl_ticket_gen_key( ctx, 0 ) ) != 0 || |
| 269 | ( ret = ssl_ticket_gen_key( ctx, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | a0adc1b | 2015-05-25 10:35:16 +0200 | [diff] [blame] | 270 | { |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 271 | return( ret ); |
Manuel Pégourié-Gonnard | a0adc1b | 2015-05-25 10:35:16 +0200 | [diff] [blame] | 272 | } |
| 273 | |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 274 | return( 0 ); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 275 | } |
| 276 | |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 277 | /* |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 278 | * Create session ticket, with the following structure: |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 279 | * |
| 280 | * struct { |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 281 | * opaque key_name[4]; |
| 282 | * opaque iv[12]; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 283 | * opaque encrypted_state<0..2^16-1>; |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 284 | * opaque tag[16]; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 285 | * } ticket; |
| 286 | * |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 287 | * The key_name, iv, and length of encrypted_state are the additional |
| 288 | * authenticated data. |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 289 | */ |
Hanno Becker | d140d08 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 290 | |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 291 | int mbedtls_ssl_ticket_write( void *p_ticket, |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 292 | const mbedtls_ssl_session *session, |
| 293 | unsigned char *start, |
| 294 | const unsigned char *end, |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 295 | size_t *tlen, |
| 296 | uint32_t *ticket_lifetime ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 297 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 298 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 299 | mbedtls_ssl_ticket_context *ctx = p_ticket; |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 300 | mbedtls_ssl_ticket_key *key; |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 301 | unsigned char *key_name = start; |
Hanno Becker | d140d08 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 302 | unsigned char *iv = start + TICKET_KEY_NAME_BYTES; |
| 303 | unsigned char *state_len_bytes = iv + TICKET_IV_BYTES; |
| 304 | unsigned char *state = state_len_bytes + TICKET_CRYPT_LEN_BYTES; |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 305 | size_t clear_len, ciph_len; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 306 | |
Gabor Mezei | 5b8b890 | 2022-03-16 12:56:58 +0100 | [diff] [blame] | 307 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 308 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 309 | #endif |
| 310 | |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 311 | *tlen = 0; |
| 312 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 313 | if( ctx == NULL || ctx->f_rng == NULL ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 314 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 315 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 316 | /* We need at least 4 bytes for key_name, 12 for IV, 2 for len 16 for tag, |
| 317 | * in addition to session itself, that will be checked when writing it. */ |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 318 | MBEDTLS_SSL_CHK_BUF_PTR( start, end, TICKET_MIN_LEN ); |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 319 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 320 | #if defined(MBEDTLS_THREADING_C) |
| 321 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 322 | return( ret ); |
| 323 | #endif |
| 324 | |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 325 | if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 ) |
| 326 | goto cleanup; |
| 327 | |
| 328 | key = &ctx->keys[ctx->active]; |
| 329 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 330 | *ticket_lifetime = ctx->ticket_lifetime; |
| 331 | |
Hanno Becker | d140d08 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 332 | memcpy( key_name, key->name, TICKET_KEY_NAME_BYTES ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 333 | |
Hanno Becker | d140d08 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 334 | 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] | 335 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 336 | |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 337 | /* Dump session state */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 338 | if( ( ret = mbedtls_ssl_session_save( session, |
| 339 | state, end - state, |
| 340 | &clear_len ) ) != 0 || |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 341 | (unsigned long) clear_len > 65535 ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 342 | { |
| 343 | goto cleanup; |
| 344 | } |
Joe Subbiani | 6dd7364 | 2021-07-19 11:56:54 +0100 | [diff] [blame] | 345 | MBEDTLS_PUT_UINT16_BE( clear_len, state_len_bytes, 0 ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 346 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 347 | /* Encrypt and authenticate */ |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 348 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gabor Mezei | 5b8b890 | 2022-03-16 12:56:58 +0100 | [diff] [blame] | 349 | if( ( status = psa_aead_encrypt( key->key, key->alg, iv, TICKET_IV_BYTES, |
| 350 | key_name, TICKET_ADD_DATA_LEN, |
| 351 | state, clear_len, |
| 352 | state, end - state, |
| 353 | &ciph_len ) ) != PSA_SUCCESS ) |
| 354 | { |
| 355 | ret = psa_ssl_status_to_mbedtls( status ); |
| 356 | goto cleanup; |
| 357 | } |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 358 | #else |
Manuel Pégourié-Gonnard | f5cf71e | 2020-12-01 11:43:40 +0100 | [diff] [blame] | 359 | if( ( ret = mbedtls_cipher_auth_encrypt_ext( &key->ctx, |
Hanno Becker | d140d08 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 360 | iv, TICKET_IV_BYTES, |
| 361 | /* Additional data: key name, IV and length */ |
| 362 | key_name, TICKET_ADD_DATA_LEN, |
Manuel Pégourié-Gonnard | f5cf71e | 2020-12-01 11:43:40 +0100 | [diff] [blame] | 363 | state, clear_len, |
| 364 | state, end - state, &ciph_len, |
| 365 | TICKET_AUTH_TAG_BYTES ) ) != 0 ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 366 | { |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 367 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 368 | } |
Gabor Mezei | 5b8b890 | 2022-03-16 12:56:58 +0100 | [diff] [blame] | 369 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 370 | |
Manuel Pégourié-Gonnard | f5cf71e | 2020-12-01 11:43:40 +0100 | [diff] [blame] | 371 | if( ciph_len != clear_len + TICKET_AUTH_TAG_BYTES ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 372 | { |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 373 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
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 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 376 | |
Manuel Pégourié-Gonnard | f5cf71e | 2020-12-01 11:43:40 +0100 | [diff] [blame] | 377 | *tlen = TICKET_MIN_LEN + ciph_len - TICKET_AUTH_TAG_BYTES; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 378 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 379 | cleanup: |
| 380 | #if defined(MBEDTLS_THREADING_C) |
| 381 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
| 382 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
| 383 | #endif |
| 384 | |
| 385 | return( ret ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | /* |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 389 | * Select key based on name |
| 390 | */ |
| 391 | static mbedtls_ssl_ticket_key *ssl_ticket_select_key( |
| 392 | mbedtls_ssl_ticket_context *ctx, |
| 393 | const unsigned char name[4] ) |
| 394 | { |
| 395 | unsigned char i; |
| 396 | |
| 397 | for( i = 0; i < sizeof( ctx->keys ) / sizeof( *ctx->keys ); i++ ) |
| 398 | if( memcmp( name, ctx->keys[i].name, 4 ) == 0 ) |
| 399 | return( &ctx->keys[i] ); |
| 400 | |
| 401 | return( NULL ); |
| 402 | } |
| 403 | |
| 404 | /* |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 405 | * Load session ticket (see mbedtls_ssl_ticket_write for structure) |
| 406 | */ |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 407 | int mbedtls_ssl_ticket_parse( void *p_ticket, |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 408 | mbedtls_ssl_session *session, |
| 409 | unsigned char *buf, |
| 410 | size_t len ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 411 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 412 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 413 | mbedtls_ssl_ticket_context *ctx = p_ticket; |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 414 | mbedtls_ssl_ticket_key *key; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 415 | unsigned char *key_name = buf; |
Hanno Becker | d140d08 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 416 | unsigned char *iv = buf + TICKET_KEY_NAME_BYTES; |
| 417 | unsigned char *enc_len_p = iv + TICKET_IV_BYTES; |
| 418 | unsigned char *ticket = enc_len_p + TICKET_CRYPT_LEN_BYTES; |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 419 | size_t enc_len, clear_len; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 420 | |
Gabor Mezei | 5b8b890 | 2022-03-16 12:56:58 +0100 | [diff] [blame] | 421 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 422 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 423 | #endif |
| 424 | |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 425 | if( ctx == NULL || ctx->f_rng == NULL ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 426 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 427 | |
Hanno Becker | d140d08 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 428 | if( len < TICKET_MIN_LEN ) |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 429 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 430 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 431 | #if defined(MBEDTLS_THREADING_C) |
| 432 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 433 | return( ret ); |
| 434 | #endif |
| 435 | |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 436 | if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 ) |
| 437 | goto cleanup; |
| 438 | |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 439 | enc_len = ( enc_len_p[0] << 8 ) | enc_len_p[1]; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 440 | |
Hanno Becker | d140d08 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 441 | if( len != TICKET_MIN_LEN + enc_len ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 442 | { |
| 443 | ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 444 | goto cleanup; |
| 445 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 446 | |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 447 | /* Select key */ |
| 448 | if( ( key = ssl_ticket_select_key( ctx, key_name ) ) == NULL ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 449 | { |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 450 | /* We can't know for sure but this is a likely option unless we're |
| 451 | * under attack - this is only informative anyway */ |
| 452 | ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED; |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 453 | goto cleanup; |
| 454 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 455 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 456 | /* Decrypt and authenticate */ |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 457 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gabor Mezei | 5b8b890 | 2022-03-16 12:56:58 +0100 | [diff] [blame] | 458 | if( ( status = psa_aead_decrypt( key->key, key->alg, iv, TICKET_IV_BYTES, |
| 459 | key_name, TICKET_ADD_DATA_LEN, |
| 460 | ticket, enc_len + TICKET_AUTH_TAG_BYTES, |
| 461 | ticket, enc_len, &clear_len ) ) != PSA_SUCCESS ) |
| 462 | { |
| 463 | ret = psa_ssl_status_to_mbedtls( status ); |
| 464 | goto cleanup; |
| 465 | } |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 466 | #else |
Manuel Pégourié-Gonnard | f5cf71e | 2020-12-01 11:43:40 +0100 | [diff] [blame] | 467 | if( ( ret = mbedtls_cipher_auth_decrypt_ext( &key->ctx, |
Hanno Becker | d140d08 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 468 | iv, TICKET_IV_BYTES, |
| 469 | /* Additional data: key name, IV and length */ |
| 470 | key_name, TICKET_ADD_DATA_LEN, |
Manuel Pégourié-Gonnard | f5cf71e | 2020-12-01 11:43:40 +0100 | [diff] [blame] | 471 | ticket, enc_len + TICKET_AUTH_TAG_BYTES, |
| 472 | ticket, enc_len, &clear_len, |
| 473 | TICKET_AUTH_TAG_BYTES ) ) != 0 ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 474 | { |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 475 | if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED ) |
| 476 | ret = MBEDTLS_ERR_SSL_INVALID_MAC; |
| 477 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 478 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 479 | } |
Gabor Mezei | 5b8b890 | 2022-03-16 12:56:58 +0100 | [diff] [blame] | 480 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 481 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 482 | if( clear_len != enc_len ) |
| 483 | { |
| 484 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 485 | goto cleanup; |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 486 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 487 | |
| 488 | /* Actually load session */ |
Manuel Pégourié-Gonnard | a3e7c65 | 2019-05-16 10:08:35 +0200 | [diff] [blame] | 489 | 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] | 490 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 491 | |
| 492 | #if defined(MBEDTLS_HAVE_TIME) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 493 | { |
Manuel Pégourié-Gonnard | 8eff512 | 2015-05-20 11:41:36 +0200 | [diff] [blame] | 494 | /* Check for expiration */ |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 495 | mbedtls_time_t current_time = mbedtls_time( NULL ); |
Manuel Pégourié-Gonnard | 8eff512 | 2015-05-20 11:41:36 +0200 | [diff] [blame] | 496 | |
| 497 | if( current_time < session->start || |
| 498 | (uint32_t)( current_time - session->start ) > ctx->ticket_lifetime ) |
| 499 | { |
| 500 | ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED; |
| 501 | goto cleanup; |
| 502 | } |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 503 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 504 | #endif |
| 505 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 506 | cleanup: |
| 507 | #if defined(MBEDTLS_THREADING_C) |
| 508 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
| 509 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
| 510 | #endif |
| 511 | |
| 512 | return( ret ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 513 | } |
| 514 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 515 | /* |
| 516 | * Free context |
| 517 | */ |
| 518 | void mbedtls_ssl_ticket_free( mbedtls_ssl_ticket_context *ctx ) |
| 519 | { |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 520 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 521 | psa_destroy_key( ctx->keys[0].key ); |
| 522 | psa_destroy_key( ctx->keys[1].key ); |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 523 | #else |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 524 | mbedtls_cipher_free( &ctx->keys[0].ctx ); |
| 525 | mbedtls_cipher_free( &ctx->keys[1].ctx ); |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 526 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 527 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 528 | #if defined(MBEDTLS_THREADING_C) |
| 529 | mbedtls_mutex_free( &ctx->mutex ); |
| 530 | #endif |
| 531 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 532 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_ticket_context ) ); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 533 | } |
| 534 | |
Manuel Pégourié-Gonnard | fd6d897 | 2015-05-15 12:09:00 +0200 | [diff] [blame] | 535 | #endif /* MBEDTLS_SSL_TICKET_C */ |