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