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 | 7ff7965 | 2023-11-03 12:04:52 +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 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 8 | #include "common.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 | |
Hanno Becker | 261602c | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 14 | #include "mbedtls/ssl_internal.h" |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 15 | #include "mbedtls/ssl_ticket.h" |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 16 | #include "mbedtls/error.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 17 | #include "mbedtls/platform_util.h" |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 18 | |
Manuel Pégourié-Gonnard | fd6d897 | 2015-05-15 12:09:00 +0200 | [diff] [blame] | 19 | #include <string.h> |
| 20 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 21 | /* |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 22 | * Initialize context |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 23 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 24 | void mbedtls_ssl_ticket_init(mbedtls_ssl_ticket_context *ctx) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 25 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 26 | memset(ctx, 0, sizeof(mbedtls_ssl_ticket_context)); |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 27 | |
| 28 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 29 | mbedtls_mutex_init(&ctx->mutex); |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 30 | #endif |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 31 | } |
| 32 | |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 33 | #define MAX_KEY_BYTES 32 /* 256 bits */ |
| 34 | |
Hanno Becker | d140d08 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 35 | #define TICKET_KEY_NAME_BYTES 4 |
| 36 | #define TICKET_IV_BYTES 12 |
| 37 | #define TICKET_CRYPT_LEN_BYTES 2 |
| 38 | #define TICKET_AUTH_TAG_BYTES 16 |
| 39 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 40 | #define TICKET_MIN_LEN (TICKET_KEY_NAME_BYTES + \ |
| 41 | TICKET_IV_BYTES + \ |
| 42 | TICKET_CRYPT_LEN_BYTES + \ |
| 43 | TICKET_AUTH_TAG_BYTES) |
| 44 | #define TICKET_ADD_DATA_LEN (TICKET_KEY_NAME_BYTES + \ |
| 45 | TICKET_IV_BYTES + \ |
| 46 | TICKET_CRYPT_LEN_BYTES) |
Hanno Becker | d140d08 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 47 | |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 48 | /* |
| 49 | * Generate/update a key |
| 50 | */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 51 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 52 | static int ssl_ticket_gen_key(mbedtls_ssl_ticket_context *ctx, |
| 53 | unsigned char index) |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 54 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 55 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 56 | unsigned char buf[MAX_KEY_BYTES]; |
| 57 | mbedtls_ssl_ticket_key *key = ctx->keys + index; |
| 58 | |
| 59 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 60 | key->generation_time = (uint32_t) mbedtls_time(NULL); |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 61 | #endif |
| 62 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 63 | if ((ret = ctx->f_rng(ctx->p_rng, key->name, sizeof(key->name))) != 0) { |
| 64 | return ret; |
| 65 | } |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 66 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 67 | if ((ret = ctx->f_rng(ctx->p_rng, buf, sizeof(buf))) != 0) { |
| 68 | return ret; |
| 69 | } |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 70 | |
| 71 | /* With GCM and CCM, same context can encrypt & decrypt */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 72 | ret = mbedtls_cipher_setkey(&key->ctx, buf, |
| 73 | mbedtls_cipher_get_key_bitlen(&key->ctx), |
| 74 | MBEDTLS_ENCRYPT); |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 75 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 76 | mbedtls_platform_zeroize(buf, sizeof(buf)); |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 77 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 78 | return ret; |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 81 | /* |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 82 | * Rotate/generate keys if necessary |
| 83 | */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 84 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 85 | 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] | 86 | { |
| 87 | #if !defined(MBEDTLS_HAVE_TIME) |
| 88 | ((void) ctx); |
| 89 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 90 | if (ctx->ticket_lifetime != 0) { |
| 91 | uint32_t current_time = (uint32_t) mbedtls_time(NULL); |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 92 | uint32_t key_time = ctx->keys[ctx->active].generation_time; |
| 93 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 94 | if (current_time >= key_time && |
| 95 | current_time - key_time < ctx->ticket_lifetime) { |
| 96 | return 0; |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | ctx->active = 1 - ctx->active; |
| 100 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 101 | return ssl_ticket_gen_key(ctx, ctx->active); |
| 102 | } else |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 103 | #endif /* MBEDTLS_HAVE_TIME */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 104 | return 0; |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 108 | * Setup context for actual use |
| 109 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 110 | int mbedtls_ssl_ticket_setup(mbedtls_ssl_ticket_context *ctx, |
| 111 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 112 | mbedtls_cipher_type_t cipher, |
| 113 | uint32_t lifetime) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 114 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 115 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 116 | const mbedtls_cipher_info_t *cipher_info; |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 117 | |
| 118 | ctx->f_rng = f_rng; |
| 119 | ctx->p_rng = p_rng; |
| 120 | |
| 121 | ctx->ticket_lifetime = lifetime; |
| 122 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 123 | cipher_info = mbedtls_cipher_info_from_type(cipher); |
| 124 | if (cipher_info == NULL) { |
| 125 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 128 | if (cipher_info->mode != MBEDTLS_MODE_GCM && |
| 129 | cipher_info->mode != MBEDTLS_MODE_CCM) { |
| 130 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 131 | } |
| 132 | |
| 133 | if (cipher_info->key_bitlen > 8 * MAX_KEY_BYTES) { |
| 134 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 135 | } |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 136 | |
David Horstmann | 04020ab | 2022-10-27 11:30:09 +0100 | [diff] [blame] | 137 | int do_mbedtls_cipher_setup = 1; |
Hanno Becker | 679d8ce | 2018-11-17 21:25:59 +0000 | [diff] [blame] | 138 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 139 | ret = mbedtls_cipher_setup_psa(&ctx->keys[0].ctx, |
| 140 | cipher_info, TICKET_AUTH_TAG_BYTES); |
David Horstmann | bcc18f2 | 2022-11-07 14:41:13 +0000 | [diff] [blame] | 141 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 142 | switch (ret) { |
David Horstmann | bcc18f2 | 2022-11-07 14:41:13 +0000 | [diff] [blame] | 143 | case 0: |
| 144 | do_mbedtls_cipher_setup = 0; |
| 145 | break; |
| 146 | case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE: |
| 147 | /* We don't yet expect to support all ciphers through PSA, |
| 148 | * so allow fallback to ordinary mbedtls_cipher_setup(). */ |
| 149 | do_mbedtls_cipher_setup = 1; |
| 150 | break; |
| 151 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 152 | return ret; |
David Horstmann | bcc18f2 | 2022-11-07 14:41:13 +0000 | [diff] [blame] | 153 | } |
Hanno Becker | 679d8ce | 2018-11-17 21:25:59 +0000 | [diff] [blame] | 154 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 155 | if (do_mbedtls_cipher_setup) { |
| 156 | if ((ret = mbedtls_cipher_setup(&ctx->keys[0].ctx, cipher_info)) |
| 157 | != 0) { |
| 158 | return ret; |
| 159 | } |
| 160 | } |
Hanno Becker | 679d8ce | 2018-11-17 21:25:59 +0000 | [diff] [blame] | 161 | |
David Horstmann | 04020ab | 2022-10-27 11:30:09 +0100 | [diff] [blame] | 162 | do_mbedtls_cipher_setup = 1; |
Hanno Becker | 679d8ce | 2018-11-17 21:25:59 +0000 | [diff] [blame] | 163 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
David Horstmann | 04020ab | 2022-10-27 11:30:09 +0100 | [diff] [blame] | 164 | do_mbedtls_cipher_setup = 0; |
| 165 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 166 | ret = mbedtls_cipher_setup_psa(&ctx->keys[1].ctx, |
| 167 | cipher_info, TICKET_AUTH_TAG_BYTES); |
| 168 | if (ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE) { |
| 169 | return ret; |
| 170 | } |
| 171 | if (ret == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE) { |
David Horstmann | 04020ab | 2022-10-27 11:30:09 +0100 | [diff] [blame] | 172 | do_mbedtls_cipher_setup = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 173 | } |
Hanno Becker | 679d8ce | 2018-11-17 21:25:59 +0000 | [diff] [blame] | 174 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 175 | if (do_mbedtls_cipher_setup) { |
| 176 | if ((ret = mbedtls_cipher_setup(&ctx->keys[1].ctx, cipher_info)) |
| 177 | != 0) { |
| 178 | return ret; |
| 179 | } |
Manuel Pégourié-Gonnard | a0adc1b | 2015-05-25 10:35:16 +0200 | [diff] [blame] | 180 | } |
| 181 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 182 | if ((ret = ssl_ticket_gen_key(ctx, 0)) != 0 || |
| 183 | (ret = ssl_ticket_gen_key(ctx, 1)) != 0) { |
| 184 | return ret; |
| 185 | } |
| 186 | |
| 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 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 204 | int mbedtls_ssl_ticket_write(void *p_ticket, |
| 205 | const mbedtls_ssl_session *session, |
| 206 | unsigned char *start, |
| 207 | const unsigned char *end, |
| 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 | size_t clear_len, ciph_len; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 219 | |
| 220 | *tlen = 0; |
| 221 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 222 | if (ctx == NULL || ctx->f_rng == NULL) { |
| 223 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 224 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 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. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +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) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 231 | if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) { |
| 232 | return ret; |
| 233 | } |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 234 | #endif |
| 235 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 236 | if ((ret = ssl_ticket_update_keys(ctx)) != 0) { |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 237 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 238 | } |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 239 | |
| 240 | key = &ctx->keys[ctx->active]; |
| 241 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 242 | *ticket_lifetime = ctx->ticket_lifetime; |
| 243 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 244 | memcpy(key_name, key->name, TICKET_KEY_NAME_BYTES); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 245 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 246 | 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] | 247 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 248 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 249 | |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 250 | /* Dump session state */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 251 | if ((ret = mbedtls_ssl_session_save(session, |
| 252 | state, end - state, |
| 253 | &clear_len)) != 0 || |
| 254 | (unsigned long) clear_len > 65535) { |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 255 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 256 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 257 | MBEDTLS_PUT_UINT16_BE(clear_len, state_len_bytes, 0); |
| 258 | |
| 259 | /* Encrypt and authenticate */ |
| 260 | if ((ret = mbedtls_cipher_auth_encrypt_ext(&key->ctx, |
| 261 | iv, TICKET_IV_BYTES, |
| 262 | /* Additional data: key name, IV and length */ |
| 263 | key_name, TICKET_ADD_DATA_LEN, |
| 264 | state, clear_len, |
| 265 | state, end - state, &ciph_len, |
| 266 | TICKET_AUTH_TAG_BYTES)) != 0) { |
| 267 | goto cleanup; |
| 268 | } |
| 269 | if (ciph_len != clear_len + TICKET_AUTH_TAG_BYTES) { |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 270 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 271 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 272 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 273 | |
Manuel Pégourié-Gonnard | f5cf71e | 2020-12-01 11:43:40 +0100 | [diff] [blame] | 274 | *tlen = TICKET_MIN_LEN + ciph_len - TICKET_AUTH_TAG_BYTES; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 275 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 276 | cleanup: |
| 277 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 278 | if (mbedtls_mutex_unlock(&ctx->mutex) != 0) { |
| 279 | return MBEDTLS_ERR_THREADING_MUTEX_ERROR; |
| 280 | } |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 281 | #endif |
| 282 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 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( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 290 | mbedtls_ssl_ticket_context *ctx, |
| 291 | const unsigned char name[4]) |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 292 | { |
| 293 | unsigned char i; |
| 294 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 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 | } |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 300 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 301 | return NULL; |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /* |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 305 | * Load session ticket (see mbedtls_ssl_ticket_write for structure) |
| 306 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 307 | int mbedtls_ssl_ticket_parse(void *p_ticket, |
| 308 | mbedtls_ssl_session *session, |
| 309 | unsigned char *buf, |
| 310 | size_t len) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 311 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 312 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 313 | mbedtls_ssl_ticket_context *ctx = p_ticket; |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 314 | mbedtls_ssl_ticket_key *key; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 315 | unsigned char *key_name = buf; |
Hanno Becker | d140d08 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 316 | unsigned char *iv = buf + TICKET_KEY_NAME_BYTES; |
| 317 | unsigned char *enc_len_p = iv + TICKET_IV_BYTES; |
| 318 | unsigned char *ticket = enc_len_p + TICKET_CRYPT_LEN_BYTES; |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 319 | size_t enc_len, clear_len; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 320 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 321 | if (ctx == NULL || ctx->f_rng == NULL) { |
| 322 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 323 | } |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 324 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 325 | if (len < TICKET_MIN_LEN) { |
| 326 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 327 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 328 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 329 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 330 | if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) { |
| 331 | return ret; |
| 332 | } |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 333 | #endif |
| 334 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 335 | if ((ret = ssl_ticket_update_keys(ctx)) != 0) { |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 336 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 337 | } |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 338 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 339 | 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] | 340 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 341 | if (len != TICKET_MIN_LEN + enc_len) { |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 342 | ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 343 | goto cleanup; |
| 344 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 345 | |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 346 | /* Select key */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 347 | if ((key = ssl_ticket_select_key(ctx, key_name)) == NULL) { |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 348 | /* We can't know for sure but this is a likely option unless we're |
| 349 | * under attack - this is only informative anyway */ |
| 350 | ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED; |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 351 | goto cleanup; |
| 352 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 353 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 354 | /* Decrypt and authenticate */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 355 | if ((ret = mbedtls_cipher_auth_decrypt_ext(&key->ctx, |
| 356 | iv, TICKET_IV_BYTES, |
| 357 | /* Additional data: key name, IV and length */ |
| 358 | key_name, TICKET_ADD_DATA_LEN, |
| 359 | ticket, enc_len + TICKET_AUTH_TAG_BYTES, |
| 360 | ticket, enc_len, &clear_len, |
| 361 | TICKET_AUTH_TAG_BYTES)) != 0) { |
| 362 | if (ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED) { |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 363 | ret = MBEDTLS_ERR_SSL_INVALID_MAC; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 364 | } |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 365 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 366 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 367 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 368 | if (clear_len != enc_len) { |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 369 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 370 | goto cleanup; |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 371 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 372 | |
| 373 | /* Actually load session */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 374 | 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] | 375 | goto cleanup; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 376 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 377 | |
| 378 | #if defined(MBEDTLS_HAVE_TIME) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 379 | { |
Manuel Pégourié-Gonnard | 8eff512 | 2015-05-20 11:41:36 +0200 | [diff] [blame] | 380 | /* Check for expiration */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 381 | mbedtls_time_t current_time = mbedtls_time(NULL); |
Manuel Pégourié-Gonnard | 8eff512 | 2015-05-20 11:41:36 +0200 | [diff] [blame] | 382 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 383 | if (current_time < session->start || |
| 384 | (uint32_t) (current_time - session->start) > ctx->ticket_lifetime) { |
Manuel Pégourié-Gonnard | 8eff512 | 2015-05-20 11:41:36 +0200 | [diff] [blame] | 385 | ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED; |
| 386 | goto cleanup; |
| 387 | } |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 388 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 389 | #endif |
| 390 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 391 | cleanup: |
| 392 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 393 | if (mbedtls_mutex_unlock(&ctx->mutex) != 0) { |
| 394 | return MBEDTLS_ERR_THREADING_MUTEX_ERROR; |
| 395 | } |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 396 | #endif |
| 397 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 398 | return ret; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 399 | } |
| 400 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 401 | /* |
| 402 | * Free context |
| 403 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 404 | void mbedtls_ssl_ticket_free(mbedtls_ssl_ticket_context *ctx) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 405 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 406 | mbedtls_cipher_free(&ctx->keys[0].ctx); |
| 407 | mbedtls_cipher_free(&ctx->keys[1].ctx); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 408 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 409 | #if defined(MBEDTLS_THREADING_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 410 | mbedtls_mutex_free(&ctx->mutex); |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 411 | #endif |
| 412 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 413 | mbedtls_platform_zeroize(ctx, sizeof(mbedtls_ssl_ticket_context)); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 414 | } |
| 415 | |
Manuel Pégourié-Gonnard | fd6d897 | 2015-05-15 12:09:00 +0200 | [diff] [blame] | 416 | #endif /* MBEDTLS_SSL_TICKET_C */ |