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