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