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 |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 6 | * |
| 7 | * This file is provided under the Apache License 2.0, or the |
| 8 | * GNU General Public License v2.0 or later. |
| 9 | * |
| 10 | * ********** |
| 11 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 12 | * |
| 13 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 14 | * not use this file except in compliance with the License. |
| 15 | * You may obtain a copy of the License at |
| 16 | * |
| 17 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | * |
| 19 | * Unless required by applicable law or agreed to in writing, software |
| 20 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 21 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | * See the License for the specific language governing permissions and |
| 23 | * limitations under the License. |
Manuel Pégourié-Gonnard | fd6d897 | 2015-05-15 12:09:00 +0200 | [diff] [blame] | 24 | * |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 25 | * ********** |
| 26 | * |
| 27 | * ********** |
| 28 | * GNU General Public License v2.0 or later: |
| 29 | * |
| 30 | * This program is free software; you can redistribute it and/or modify |
| 31 | * it under the terms of the GNU General Public License as published by |
| 32 | * the Free Software Foundation; either version 2 of the License, or |
| 33 | * (at your option) any later version. |
| 34 | * |
| 35 | * This program is distributed in the hope that it will be useful, |
| 36 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 37 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 38 | * GNU General Public License for more details. |
| 39 | * |
| 40 | * You should have received a copy of the GNU General Public License along |
| 41 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 42 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 43 | * |
| 44 | * ********** |
| 45 | * |
Manuel Pégourié-Gonnard | fd6d897 | 2015-05-15 12:09:00 +0200 | [diff] [blame] | 46 | * 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] | 47 | */ |
| 48 | |
| 49 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 50 | #include "mbedtls/config.h" |
| 51 | #else |
| 52 | #include MBEDTLS_CONFIG_FILE |
| 53 | #endif |
| 54 | |
| 55 | #if defined(MBEDTLS_SSL_TICKET_C) |
| 56 | |
Manuel Pégourié-Gonnard | fd6d897 | 2015-05-15 12:09:00 +0200 | [diff] [blame] | 57 | #if defined(MBEDTLS_PLATFORM_C) |
| 58 | #include "mbedtls/platform.h" |
| 59 | #else |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 60 | #include <stdlib.h> |
| 61 | #define mbedtls_calloc calloc |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 62 | #define mbedtls_free free |
Manuel Pégourié-Gonnard | fd6d897 | 2015-05-15 12:09:00 +0200 | [diff] [blame] | 63 | #endif |
| 64 | |
Hanno Becker | f8f61aa | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 65 | #include "mbedtls/ssl_internal.h" |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 66 | #include "mbedtls/ssl_ticket.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 67 | #include "mbedtls/platform_util.h" |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 68 | |
Manuel Pégourié-Gonnard | fd6d897 | 2015-05-15 12:09:00 +0200 | [diff] [blame] | 69 | #include <string.h> |
| 70 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 71 | /* |
| 72 | * Initialze context |
| 73 | */ |
| 74 | void mbedtls_ssl_ticket_init( mbedtls_ssl_ticket_context *ctx ) |
| 75 | { |
| 76 | memset( ctx, 0, sizeof( mbedtls_ssl_ticket_context ) ); |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 77 | |
| 78 | #if defined(MBEDTLS_THREADING_C) |
| 79 | mbedtls_mutex_init( &ctx->mutex ); |
| 80 | #endif |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 81 | } |
| 82 | |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 83 | #define MAX_KEY_BYTES 32 /* 256 bits */ |
| 84 | |
Hanno Becker | 8f39726 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 85 | #define TICKET_KEY_NAME_BYTES 4 |
| 86 | #define TICKET_IV_BYTES 12 |
| 87 | #define TICKET_CRYPT_LEN_BYTES 2 |
| 88 | #define TICKET_AUTH_TAG_BYTES 16 |
| 89 | |
| 90 | #define TICKET_MIN_LEN ( TICKET_KEY_NAME_BYTES + \ |
| 91 | TICKET_IV_BYTES + \ |
| 92 | TICKET_CRYPT_LEN_BYTES + \ |
| 93 | TICKET_AUTH_TAG_BYTES ) |
| 94 | #define TICKET_ADD_DATA_LEN ( TICKET_KEY_NAME_BYTES + \ |
| 95 | TICKET_IV_BYTES + \ |
| 96 | TICKET_CRYPT_LEN_BYTES ) |
| 97 | |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 98 | /* |
| 99 | * Generate/update a key |
| 100 | */ |
| 101 | static int ssl_ticket_gen_key( mbedtls_ssl_ticket_context *ctx, |
| 102 | unsigned char index ) |
| 103 | { |
| 104 | int ret; |
| 105 | unsigned char buf[MAX_KEY_BYTES]; |
| 106 | mbedtls_ssl_ticket_key *key = ctx->keys + index; |
| 107 | |
| 108 | #if defined(MBEDTLS_HAVE_TIME) |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 109 | key->generation_time = (uint32_t) mbedtls_time( NULL ); |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 110 | #endif |
| 111 | |
| 112 | if( ( ret = ctx->f_rng( ctx->p_rng, key->name, sizeof( key->name ) ) ) != 0 ) |
| 113 | return( ret ); |
| 114 | |
| 115 | if( ( ret = ctx->f_rng( ctx->p_rng, buf, sizeof( buf ) ) ) != 0 ) |
| 116 | return( ret ); |
| 117 | |
| 118 | /* With GCM and CCM, same context can encrypt & decrypt */ |
| 119 | ret = mbedtls_cipher_setkey( &key->ctx, buf, |
Manuel Pégourié-Gonnard | 097c7bb | 2015-06-18 16:43:38 +0200 | [diff] [blame] | 120 | mbedtls_cipher_get_key_bitlen( &key->ctx ), |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 121 | MBEDTLS_ENCRYPT ); |
| 122 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 123 | mbedtls_platform_zeroize( buf, sizeof( buf ) ); |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 124 | |
| 125 | return( ret ); |
| 126 | } |
| 127 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 128 | /* |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 129 | * Rotate/generate keys if necessary |
| 130 | */ |
| 131 | static int ssl_ticket_update_keys( mbedtls_ssl_ticket_context *ctx ) |
| 132 | { |
| 133 | #if !defined(MBEDTLS_HAVE_TIME) |
| 134 | ((void) ctx); |
| 135 | #else |
| 136 | if( ctx->ticket_lifetime != 0 ) |
| 137 | { |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 138 | uint32_t current_time = (uint32_t) mbedtls_time( NULL ); |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 139 | uint32_t key_time = ctx->keys[ctx->active].generation_time; |
| 140 | |
Hanno Becker | aa71500 | 2018-08-21 13:55:31 +0100 | [diff] [blame] | 141 | if( current_time >= key_time && |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 142 | current_time - key_time < ctx->ticket_lifetime ) |
| 143 | { |
| 144 | return( 0 ); |
| 145 | } |
| 146 | |
| 147 | ctx->active = 1 - ctx->active; |
| 148 | |
| 149 | return( ssl_ticket_gen_key( ctx, ctx->active ) ); |
| 150 | } |
| 151 | else |
| 152 | #endif /* MBEDTLS_HAVE_TIME */ |
| 153 | return( 0 ); |
| 154 | } |
| 155 | |
| 156 | /* |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 157 | * Setup context for actual use |
| 158 | */ |
| 159 | int mbedtls_ssl_ticket_setup( mbedtls_ssl_ticket_context *ctx, |
| 160 | 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] | 161 | mbedtls_cipher_type_t cipher, |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 162 | uint32_t lifetime ) |
| 163 | { |
| 164 | int ret; |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 165 | const mbedtls_cipher_info_t *cipher_info; |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 166 | |
| 167 | ctx->f_rng = f_rng; |
| 168 | ctx->p_rng = p_rng; |
| 169 | |
| 170 | ctx->ticket_lifetime = lifetime; |
| 171 | |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 172 | cipher_info = mbedtls_cipher_info_from_type( cipher); |
| 173 | if( cipher_info == NULL ) |
| 174 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 175 | |
| 176 | if( cipher_info->mode != MBEDTLS_MODE_GCM && |
| 177 | cipher_info->mode != MBEDTLS_MODE_CCM ) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 178 | { |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 179 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 180 | } |
| 181 | |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 182 | if( cipher_info->key_bitlen > 8 * MAX_KEY_BYTES ) |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 183 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 184 | |
| 185 | if( ( ret = mbedtls_cipher_setup( &ctx->keys[0].ctx, cipher_info ) ) != 0 || |
| 186 | ( ret = mbedtls_cipher_setup( &ctx->keys[1].ctx, cipher_info ) ) != 0 ) |
Manuel Pégourié-Gonnard | a0adc1b | 2015-05-25 10:35:16 +0200 | [diff] [blame] | 187 | { |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 188 | return( ret ); |
Manuel Pégourié-Gonnard | a0adc1b | 2015-05-25 10:35:16 +0200 | [diff] [blame] | 189 | } |
| 190 | |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 191 | if( ( ret = ssl_ticket_gen_key( ctx, 0 ) ) != 0 || |
| 192 | ( ret = ssl_ticket_gen_key( ctx, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | a0adc1b | 2015-05-25 10:35:16 +0200 | [diff] [blame] | 193 | { |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 194 | return( ret ); |
Manuel Pégourié-Gonnard | a0adc1b | 2015-05-25 10:35:16 +0200 | [diff] [blame] | 195 | } |
| 196 | |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 197 | return( 0 ); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 198 | } |
| 199 | |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 200 | /* |
| 201 | * Serialize a session in the following format: |
| 202 | * 0 . n-1 session structure, n = sizeof(mbedtls_ssl_session) |
| 203 | * n . n+2 peer_cert length = m (0 if no certificate) |
| 204 | * n+3 . n+2+m peer cert ASN.1 |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 205 | */ |
| 206 | static int ssl_save_session( const mbedtls_ssl_session *session, |
| 207 | unsigned char *buf, size_t buf_len, |
| 208 | size_t *olen ) |
| 209 | { |
| 210 | unsigned char *p = buf; |
| 211 | size_t left = buf_len; |
| 212 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 213 | size_t cert_len; |
| 214 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 215 | |
| 216 | if( left < sizeof( mbedtls_ssl_session ) ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 217 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 218 | |
| 219 | memcpy( p, session, sizeof( mbedtls_ssl_session ) ); |
| 220 | p += sizeof( mbedtls_ssl_session ); |
| 221 | left -= sizeof( mbedtls_ssl_session ); |
| 222 | |
| 223 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 224 | if( session->peer_cert == NULL ) |
| 225 | cert_len = 0; |
| 226 | else |
| 227 | cert_len = session->peer_cert->raw.len; |
| 228 | |
| 229 | if( left < 3 + cert_len ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 230 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 231 | |
Hanno Becker | 9543373 | 2018-10-24 13:31:49 +0100 | [diff] [blame] | 232 | *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF ); |
| 233 | *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF ); |
| 234 | *p++ = (unsigned char)( ( cert_len ) & 0xFF ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 235 | |
| 236 | if( session->peer_cert != NULL ) |
| 237 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 238 | |
| 239 | p += cert_len; |
| 240 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 241 | |
| 242 | *olen = p - buf; |
| 243 | |
| 244 | return( 0 ); |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * Unserialise session, see ssl_save_session() |
| 249 | */ |
| 250 | static int ssl_load_session( mbedtls_ssl_session *session, |
| 251 | const unsigned char *buf, size_t len ) |
| 252 | { |
| 253 | const unsigned char *p = buf; |
| 254 | const unsigned char * const end = buf + len; |
| 255 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 256 | size_t cert_len; |
| 257 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 258 | |
Hanno Becker | 137015c | 2018-10-24 13:33:02 +0100 | [diff] [blame] | 259 | if( sizeof( mbedtls_ssl_session ) > (size_t)( end - p ) ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 260 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 261 | |
| 262 | memcpy( session, p, sizeof( mbedtls_ssl_session ) ); |
| 263 | p += sizeof( mbedtls_ssl_session ); |
| 264 | |
| 265 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 137015c | 2018-10-24 13:33:02 +0100 | [diff] [blame] | 266 | if( 3 > (size_t)( end - p ) ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 267 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 268 | |
| 269 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 270 | p += 3; |
| 271 | |
| 272 | if( cert_len == 0 ) |
| 273 | { |
| 274 | session->peer_cert = NULL; |
| 275 | } |
| 276 | else |
| 277 | { |
| 278 | int ret; |
| 279 | |
Hanno Becker | 137015c | 2018-10-24 13:33:02 +0100 | [diff] [blame] | 280 | if( cert_len > (size_t)( end - p ) ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 281 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 282 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 283 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 284 | |
| 285 | if( session->peer_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 286 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 287 | |
| 288 | mbedtls_x509_crt_init( session->peer_cert ); |
| 289 | |
| 290 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
Hanno Becker | 6934e9b | 2018-10-24 13:41:37 +0100 | [diff] [blame] | 291 | p, cert_len ) ) != 0 ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 292 | { |
| 293 | mbedtls_x509_crt_free( session->peer_cert ); |
| 294 | mbedtls_free( session->peer_cert ); |
| 295 | session->peer_cert = NULL; |
| 296 | return( ret ); |
| 297 | } |
| 298 | |
| 299 | p += cert_len; |
| 300 | } |
| 301 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 302 | |
| 303 | if( p != end ) |
| 304 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 305 | |
| 306 | return( 0 ); |
| 307 | } |
| 308 | |
| 309 | /* |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 310 | * Create session ticket, with the following structure: |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 311 | * |
| 312 | * struct { |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 313 | * opaque key_name[4]; |
| 314 | * opaque iv[12]; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 315 | * opaque encrypted_state<0..2^16-1>; |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 316 | * opaque tag[16]; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 317 | * } ticket; |
| 318 | * |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 319 | * The key_name, iv, and length of encrypted_state are the additional |
| 320 | * authenticated data. |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 321 | */ |
Hanno Becker | 8f39726 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 322 | |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 323 | int mbedtls_ssl_ticket_write( void *p_ticket, |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 324 | const mbedtls_ssl_session *session, |
| 325 | unsigned char *start, |
| 326 | const unsigned char *end, |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 327 | size_t *tlen, |
| 328 | uint32_t *ticket_lifetime ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 329 | { |
| 330 | int ret; |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 331 | mbedtls_ssl_ticket_context *ctx = p_ticket; |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 332 | mbedtls_ssl_ticket_key *key; |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 333 | unsigned char *key_name = start; |
Hanno Becker | 8f39726 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 334 | unsigned char *iv = start + TICKET_KEY_NAME_BYTES; |
| 335 | unsigned char *state_len_bytes = iv + TICKET_IV_BYTES; |
| 336 | unsigned char *state = state_len_bytes + TICKET_CRYPT_LEN_BYTES; |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 337 | unsigned char *tag; |
| 338 | size_t clear_len, ciph_len; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 339 | |
| 340 | *tlen = 0; |
| 341 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 342 | if( ctx == NULL || ctx->f_rng == NULL ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 343 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 344 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 345 | /* We need at least 4 bytes for key_name, 12 for IV, 2 for len 16 for tag, |
| 346 | * in addition to session itself, that will be checked when writing it. */ |
Hanno Becker | f8f61aa | 2017-04-12 14:54:42 +0100 | [diff] [blame] | 347 | MBEDTLS_SSL_CHK_BUF_PTR( start, end, TICKET_MIN_LEN ); |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 348 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 349 | #if defined(MBEDTLS_THREADING_C) |
| 350 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 351 | return( ret ); |
| 352 | #endif |
| 353 | |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 354 | if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 ) |
| 355 | goto cleanup; |
| 356 | |
| 357 | key = &ctx->keys[ctx->active]; |
| 358 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 359 | *ticket_lifetime = ctx->ticket_lifetime; |
| 360 | |
Hanno Becker | 8f39726 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 361 | memcpy( key_name, key->name, TICKET_KEY_NAME_BYTES ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 362 | |
Hanno Becker | 8f39726 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 363 | 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] | 364 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 365 | |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 366 | /* Dump session state */ |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 367 | if( ( ret = ssl_save_session( session, |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 368 | state, end - state, &clear_len ) ) != 0 || |
| 369 | (unsigned long) clear_len > 65535 ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 370 | { |
| 371 | goto cleanup; |
| 372 | } |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 373 | state_len_bytes[0] = ( clear_len >> 8 ) & 0xff; |
| 374 | state_len_bytes[1] = ( clear_len ) & 0xff; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 375 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 376 | /* Encrypt and authenticate */ |
| 377 | tag = state + clear_len; |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 378 | if( ( ret = mbedtls_cipher_auth_encrypt( &key->ctx, |
Hanno Becker | 8f39726 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 379 | iv, TICKET_IV_BYTES, |
| 380 | /* Additional data: key name, IV and length */ |
| 381 | key_name, TICKET_ADD_DATA_LEN, |
| 382 | state, clear_len, state, &ciph_len, |
| 383 | tag, TICKET_AUTH_TAG_BYTES ) ) != 0 ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 384 | { |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 385 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 386 | } |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 387 | if( ciph_len != clear_len ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 388 | { |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 389 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 390 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 391 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 392 | |
Hanno Becker | 8f39726 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 393 | *tlen = TICKET_MIN_LEN + ciph_len; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 394 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 395 | cleanup: |
| 396 | #if defined(MBEDTLS_THREADING_C) |
| 397 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
| 398 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
| 399 | #endif |
| 400 | |
| 401 | return( ret ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | /* |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 405 | * Select key based on name |
| 406 | */ |
| 407 | static mbedtls_ssl_ticket_key *ssl_ticket_select_key( |
| 408 | mbedtls_ssl_ticket_context *ctx, |
| 409 | const unsigned char name[4] ) |
| 410 | { |
| 411 | unsigned char i; |
| 412 | |
| 413 | for( i = 0; i < sizeof( ctx->keys ) / sizeof( *ctx->keys ); i++ ) |
| 414 | if( memcmp( name, ctx->keys[i].name, 4 ) == 0 ) |
| 415 | return( &ctx->keys[i] ); |
| 416 | |
| 417 | return( NULL ); |
| 418 | } |
| 419 | |
| 420 | /* |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 421 | * Load session ticket (see mbedtls_ssl_ticket_write for structure) |
| 422 | */ |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 423 | int mbedtls_ssl_ticket_parse( void *p_ticket, |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 424 | mbedtls_ssl_session *session, |
| 425 | unsigned char *buf, |
| 426 | size_t len ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 427 | { |
| 428 | int ret; |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 429 | mbedtls_ssl_ticket_context *ctx = p_ticket; |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 430 | mbedtls_ssl_ticket_key *key; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 431 | unsigned char *key_name = buf; |
Hanno Becker | 8f39726 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 432 | unsigned char *iv = buf + TICKET_KEY_NAME_BYTES; |
| 433 | unsigned char *enc_len_p = iv + TICKET_IV_BYTES; |
| 434 | unsigned char *ticket = enc_len_p + TICKET_CRYPT_LEN_BYTES; |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 435 | unsigned char *tag; |
| 436 | size_t enc_len, clear_len; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 437 | |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 438 | if( ctx == NULL || ctx->f_rng == NULL ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 439 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 440 | |
Hanno Becker | 8f39726 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 441 | if( len < TICKET_MIN_LEN ) |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 442 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 443 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 444 | #if defined(MBEDTLS_THREADING_C) |
| 445 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 446 | return( ret ); |
| 447 | #endif |
| 448 | |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 449 | if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 ) |
| 450 | goto cleanup; |
| 451 | |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 452 | 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] | 453 | tag = ticket + enc_len; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 454 | |
Hanno Becker | 8f39726 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 455 | if( len != TICKET_MIN_LEN + enc_len ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 456 | { |
| 457 | ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 458 | goto cleanup; |
| 459 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 460 | |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 461 | /* Select key */ |
| 462 | if( ( key = ssl_ticket_select_key( ctx, key_name ) ) == NULL ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 463 | { |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 464 | /* We can't know for sure but this is a likely option unless we're |
| 465 | * under attack - this is only informative anyway */ |
| 466 | ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED; |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 467 | goto cleanup; |
| 468 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 469 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 470 | /* Decrypt and authenticate */ |
Hanno Becker | 8f39726 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 471 | if( ( ret = mbedtls_cipher_auth_decrypt( &key->ctx, |
| 472 | iv, TICKET_IV_BYTES, |
| 473 | /* Additional data: key name, IV and length */ |
| 474 | key_name, TICKET_ADD_DATA_LEN, |
| 475 | ticket, enc_len, |
| 476 | ticket, &clear_len, |
| 477 | tag, TICKET_AUTH_TAG_BYTES ) ) != 0 ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 478 | { |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 479 | if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED ) |
| 480 | ret = MBEDTLS_ERR_SSL_INVALID_MAC; |
| 481 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 482 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 483 | } |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 484 | if( clear_len != enc_len ) |
| 485 | { |
| 486 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 487 | goto cleanup; |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 488 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 489 | |
| 490 | /* Actually load session */ |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 491 | if( ( ret = ssl_load_session( session, ticket, clear_len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 492 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 493 | |
| 494 | #if defined(MBEDTLS_HAVE_TIME) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 495 | { |
Manuel Pégourié-Gonnard | 8eff512 | 2015-05-20 11:41:36 +0200 | [diff] [blame] | 496 | /* Check for expiration */ |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 497 | mbedtls_time_t current_time = mbedtls_time( NULL ); |
Manuel Pégourié-Gonnard | 8eff512 | 2015-05-20 11:41:36 +0200 | [diff] [blame] | 498 | |
| 499 | if( current_time < session->start || |
| 500 | (uint32_t)( current_time - session->start ) > ctx->ticket_lifetime ) |
| 501 | { |
| 502 | ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED; |
| 503 | goto cleanup; |
| 504 | } |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 505 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 506 | #endif |
| 507 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 508 | cleanup: |
| 509 | #if defined(MBEDTLS_THREADING_C) |
| 510 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
| 511 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
| 512 | #endif |
| 513 | |
| 514 | return( ret ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 515 | } |
| 516 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 517 | /* |
| 518 | * Free context |
| 519 | */ |
| 520 | void mbedtls_ssl_ticket_free( mbedtls_ssl_ticket_context *ctx ) |
| 521 | { |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 522 | mbedtls_cipher_free( &ctx->keys[0].ctx ); |
| 523 | mbedtls_cipher_free( &ctx->keys[1].ctx ); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 524 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 525 | #if defined(MBEDTLS_THREADING_C) |
| 526 | mbedtls_mutex_free( &ctx->mutex ); |
| 527 | #endif |
| 528 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 529 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_ticket_context ) ); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 530 | } |
| 531 | |
Manuel Pégourié-Gonnard | fd6d897 | 2015-05-15 12:09:00 +0200 | [diff] [blame] | 532 | #endif /* MBEDTLS_SSL_TICKET_C */ |