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