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 | |
| 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 23 | #include "mbedtls/config.h" |
| 24 | #else |
| 25 | #include MBEDTLS_CONFIG_FILE |
| 26 | #endif |
| 27 | |
| 28 | #if defined(MBEDTLS_SSL_TICKET_C) |
| 29 | |
Manuel Pégourié-Gonnard | fd6d897 | 2015-05-15 12:09:00 +0200 | [diff] [blame] | 30 | #if defined(MBEDTLS_PLATFORM_C) |
| 31 | #include "mbedtls/platform.h" |
| 32 | #else |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 33 | #include <stdlib.h> |
| 34 | #define mbedtls_calloc calloc |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 35 | #define mbedtls_free free |
Manuel Pégourié-Gonnard | fd6d897 | 2015-05-15 12:09:00 +0200 | [diff] [blame] | 36 | #endif |
| 37 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 38 | #include "mbedtls/ssl_ticket.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 39 | #include "mbedtls/platform_util.h" |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 40 | |
Manuel Pégourié-Gonnard | fd6d897 | 2015-05-15 12:09:00 +0200 | [diff] [blame] | 41 | #include <string.h> |
| 42 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 43 | /* |
| 44 | * Initialze context |
| 45 | */ |
| 46 | void mbedtls_ssl_ticket_init( mbedtls_ssl_ticket_context *ctx ) |
| 47 | { |
| 48 | memset( ctx, 0, sizeof( mbedtls_ssl_ticket_context ) ); |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 49 | |
| 50 | #if defined(MBEDTLS_THREADING_C) |
| 51 | mbedtls_mutex_init( &ctx->mutex ); |
| 52 | #endif |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 53 | } |
| 54 | |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 55 | #define MAX_KEY_BYTES 32 /* 256 bits */ |
| 56 | |
Hanno Becker | d140d08 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 57 | #define TICKET_KEY_NAME_BYTES 4 |
| 58 | #define TICKET_IV_BYTES 12 |
| 59 | #define TICKET_CRYPT_LEN_BYTES 2 |
| 60 | #define TICKET_AUTH_TAG_BYTES 16 |
| 61 | |
| 62 | #define TICKET_MIN_LEN ( TICKET_KEY_NAME_BYTES + \ |
| 63 | TICKET_IV_BYTES + \ |
| 64 | TICKET_CRYPT_LEN_BYTES + \ |
| 65 | TICKET_AUTH_TAG_BYTES ) |
| 66 | #define TICKET_ADD_DATA_LEN ( TICKET_KEY_NAME_BYTES + \ |
| 67 | TICKET_IV_BYTES + \ |
| 68 | TICKET_CRYPT_LEN_BYTES ) |
| 69 | |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 70 | /* |
| 71 | * Generate/update a key |
| 72 | */ |
| 73 | static int ssl_ticket_gen_key( mbedtls_ssl_ticket_context *ctx, |
| 74 | unsigned char index ) |
| 75 | { |
| 76 | int ret; |
| 77 | unsigned char buf[MAX_KEY_BYTES]; |
| 78 | mbedtls_ssl_ticket_key *key = ctx->keys + index; |
| 79 | |
| 80 | #if defined(MBEDTLS_HAVE_TIME) |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 81 | key->generation_time = (uint32_t) mbedtls_time( NULL ); |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 82 | #endif |
| 83 | |
| 84 | if( ( ret = ctx->f_rng( ctx->p_rng, key->name, sizeof( key->name ) ) ) != 0 ) |
| 85 | return( ret ); |
| 86 | |
| 87 | if( ( ret = ctx->f_rng( ctx->p_rng, buf, sizeof( buf ) ) ) != 0 ) |
| 88 | return( ret ); |
| 89 | |
| 90 | /* With GCM and CCM, same context can encrypt & decrypt */ |
| 91 | ret = mbedtls_cipher_setkey( &key->ctx, buf, |
Manuel Pégourié-Gonnard | 097c7bb | 2015-06-18 16:43:38 +0200 | [diff] [blame] | 92 | mbedtls_cipher_get_key_bitlen( &key->ctx ), |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 93 | MBEDTLS_ENCRYPT ); |
| 94 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 95 | mbedtls_platform_zeroize( buf, sizeof( buf ) ); |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 96 | |
| 97 | return( ret ); |
| 98 | } |
| 99 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 100 | /* |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 101 | * Rotate/generate keys if necessary |
| 102 | */ |
| 103 | static int ssl_ticket_update_keys( mbedtls_ssl_ticket_context *ctx ) |
| 104 | { |
| 105 | #if !defined(MBEDTLS_HAVE_TIME) |
| 106 | ((void) ctx); |
| 107 | #else |
| 108 | if( ctx->ticket_lifetime != 0 ) |
| 109 | { |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 110 | uint32_t current_time = (uint32_t) mbedtls_time( NULL ); |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 111 | uint32_t key_time = ctx->keys[ctx->active].generation_time; |
| 112 | |
Hanno Becker | aa71500 | 2018-08-21 13:55:31 +0100 | [diff] [blame] | 113 | if( current_time >= key_time && |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 114 | current_time - key_time < ctx->ticket_lifetime ) |
| 115 | { |
| 116 | return( 0 ); |
| 117 | } |
| 118 | |
| 119 | ctx->active = 1 - ctx->active; |
| 120 | |
| 121 | return( ssl_ticket_gen_key( ctx, ctx->active ) ); |
| 122 | } |
| 123 | else |
| 124 | #endif /* MBEDTLS_HAVE_TIME */ |
| 125 | return( 0 ); |
| 126 | } |
| 127 | |
| 128 | /* |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 129 | * Setup context for actual use |
| 130 | */ |
| 131 | int mbedtls_ssl_ticket_setup( mbedtls_ssl_ticket_context *ctx, |
| 132 | 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] | 133 | mbedtls_cipher_type_t cipher, |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 134 | uint32_t lifetime ) |
| 135 | { |
| 136 | int ret; |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 137 | const mbedtls_cipher_info_t *cipher_info; |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 138 | |
| 139 | ctx->f_rng = f_rng; |
| 140 | ctx->p_rng = p_rng; |
| 141 | |
| 142 | ctx->ticket_lifetime = lifetime; |
| 143 | |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 144 | cipher_info = mbedtls_cipher_info_from_type( cipher); |
| 145 | if( cipher_info == NULL ) |
| 146 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 147 | |
| 148 | if( cipher_info->mode != MBEDTLS_MODE_GCM && |
| 149 | cipher_info->mode != MBEDTLS_MODE_CCM ) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 150 | { |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 151 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 154 | if( cipher_info->key_bitlen > 8 * MAX_KEY_BYTES ) |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 155 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 156 | |
Hanno Becker | 679d8ce | 2018-11-17 21:25:59 +0000 | [diff] [blame] | 157 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 158 | ret = mbedtls_cipher_setup_psa( &ctx->keys[0].ctx, |
| 159 | cipher_info, TICKET_AUTH_TAG_BYTES ); |
| 160 | if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ) |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 161 | return( ret ); |
Hanno Becker | 679d8ce | 2018-11-17 21:25:59 +0000 | [diff] [blame] | 162 | /* We don't yet expect to support all ciphers through PSA, |
| 163 | * so allow fallback to ordinary mbedtls_cipher_setup(). */ |
| 164 | if( ret == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ) |
| 165 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 166 | if( ( ret = mbedtls_cipher_setup( &ctx->keys[0].ctx, cipher_info ) ) != 0 ) |
| 167 | return( ret ); |
| 168 | |
| 169 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 170 | ret = mbedtls_cipher_setup_psa( &ctx->keys[1].ctx, |
| 171 | cipher_info, TICKET_AUTH_TAG_BYTES ); |
| 172 | if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ) |
| 173 | return( ret ); |
| 174 | if( ret == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ) |
| 175 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 176 | if( ( ret = mbedtls_cipher_setup( &ctx->keys[1].ctx, cipher_info ) ) != 0 ) |
| 177 | return( ret ); |
Manuel Pégourié-Gonnard | a0adc1b | 2015-05-25 10:35:16 +0200 | [diff] [blame] | 178 | |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 179 | if( ( ret = ssl_ticket_gen_key( ctx, 0 ) ) != 0 || |
| 180 | ( ret = ssl_ticket_gen_key( ctx, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | a0adc1b | 2015-05-25 10:35:16 +0200 | [diff] [blame] | 181 | { |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 182 | return( ret ); |
Manuel Pégourié-Gonnard | a0adc1b | 2015-05-25 10:35:16 +0200 | [diff] [blame] | 183 | } |
| 184 | |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 185 | return( 0 ); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 188 | /* |
| 189 | * Serialize a session in the following format: |
Hanno Becker | c5fcbb3 | 2019-02-06 15:23:38 +0000 | [diff] [blame^] | 190 | * 0 . n-1 session structure, n = sizeof(mbedtls_ssl_session) |
| 191 | * n . n+2 peer_cert length = m (0 if no certificate) |
| 192 | * n+3 . n+2+m peer cert ASN.1 |
| 193 | * n+3+m . n+3+m length of peer certificate digest = k (0 if n digest) |
| 194 | * n+4+m . n+4+k peer certificate digest (digest type encoded in session) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 195 | */ |
| 196 | static int ssl_save_session( const mbedtls_ssl_session *session, |
| 197 | unsigned char *buf, size_t buf_len, |
| 198 | size_t *olen ) |
| 199 | { |
| 200 | unsigned char *p = buf; |
| 201 | size_t left = buf_len; |
| 202 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 203 | size_t cert_len; |
Hanno Becker | c5fcbb3 | 2019-02-06 15:23:38 +0000 | [diff] [blame^] | 204 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 205 | size_t cert_digest_len; |
| 206 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 207 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 208 | |
| 209 | if( left < sizeof( mbedtls_ssl_session ) ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 210 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 211 | |
Hanno Becker | 0329f75 | 2019-02-06 15:04:32 +0000 | [diff] [blame] | 212 | /* This also copies the values of pointer fields in the |
| 213 | * session to be serialized, but they'll be ignored when |
| 214 | * loading the session through ssl_load_session(). */ |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 215 | memcpy( p, session, sizeof( mbedtls_ssl_session ) ); |
| 216 | p += sizeof( mbedtls_ssl_session ); |
| 217 | left -= sizeof( mbedtls_ssl_session ); |
| 218 | |
| 219 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 220 | if( session->peer_cert == NULL ) |
| 221 | cert_len = 0; |
| 222 | else |
| 223 | cert_len = session->peer_cert->raw.len; |
| 224 | |
| 225 | if( left < 3 + cert_len ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 226 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 227 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 228 | *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF ); |
| 229 | *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF ); |
| 230 | *p++ = (unsigned char)( ( cert_len ) & 0xFF ); |
Hanno Becker | c5fcbb3 | 2019-02-06 15:23:38 +0000 | [diff] [blame^] | 231 | left -= 3; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 232 | |
| 233 | if( session->peer_cert != NULL ) |
| 234 | memcpy( p, session->peer_cert->raw.p, cert_len ); |
| 235 | |
| 236 | p += cert_len; |
Hanno Becker | c5fcbb3 | 2019-02-06 15:23:38 +0000 | [diff] [blame^] | 237 | left -= cert_len; |
| 238 | |
| 239 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 240 | if( session->peer_cert_digest != NULL ) |
| 241 | cert_digest_len = 0; |
| 242 | else |
| 243 | cert_digest_len = session->peer_cert_digest_len; |
| 244 | |
| 245 | if( left < 1 + cert_digest_len ) |
| 246 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 247 | |
| 248 | *p++ = (unsigned char) cert_digest_len; |
| 249 | left--; |
| 250 | |
| 251 | if( session->peer_cert_digest != NULL ) |
| 252 | memcpy( p, session->peer_cert_digest, cert_digest_len ); |
| 253 | |
| 254 | p += cert_digest_len; |
| 255 | left -= cert_digest_len; |
| 256 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 257 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 258 | |
| 259 | *olen = p - buf; |
| 260 | |
| 261 | return( 0 ); |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * Unserialise session, see ssl_save_session() |
| 266 | */ |
| 267 | static int ssl_load_session( mbedtls_ssl_session *session, |
| 268 | const unsigned char *buf, size_t len ) |
| 269 | { |
| 270 | const unsigned char *p = buf; |
| 271 | const unsigned char * const end = buf + len; |
| 272 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 273 | size_t cert_len; |
Hanno Becker | c5fcbb3 | 2019-02-06 15:23:38 +0000 | [diff] [blame^] | 274 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 275 | size_t cert_digest_len; |
| 276 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 277 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 278 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 279 | if( sizeof( mbedtls_ssl_session ) > (size_t)( end - p ) ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 280 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 281 | |
| 282 | memcpy( session, p, sizeof( mbedtls_ssl_session ) ); |
| 283 | p += sizeof( mbedtls_ssl_session ); |
| 284 | |
Hanno Becker | 0329f75 | 2019-02-06 15:04:32 +0000 | [diff] [blame] | 285 | /* Non-NULL pointer fields of `session` are meaningless |
| 286 | * and potentially harmful. Zeroize them for safety. */ |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 287 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Hanno Becker | 0329f75 | 2019-02-06 15:04:32 +0000 | [diff] [blame] | 288 | session->peer_cert = NULL; |
Hanno Becker | c5fcbb3 | 2019-02-06 15:23:38 +0000 | [diff] [blame^] | 289 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 290 | session->peer_cert_digest = NULL; |
| 291 | #endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Hanno Becker | 0329f75 | 2019-02-06 15:04:32 +0000 | [diff] [blame] | 292 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 293 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 294 | session->ticket = NULL; |
| 295 | #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ |
| 296 | |
| 297 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 298 | /* Deserialize CRT from the end of the ticket. */ |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 299 | if( 3 > (size_t)( end - p ) ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 300 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 301 | |
| 302 | cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2]; |
| 303 | p += 3; |
| 304 | |
Hanno Becker | 0329f75 | 2019-02-06 15:04:32 +0000 | [diff] [blame] | 305 | if( cert_len != 0 ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 306 | { |
| 307 | int ret; |
| 308 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 309 | if( cert_len > (size_t)( end - p ) ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 310 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 311 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 312 | session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 313 | |
| 314 | if( session->peer_cert == NULL ) |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 315 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 316 | |
| 317 | mbedtls_x509_crt_init( session->peer_cert ); |
| 318 | |
| 319 | if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 320 | p, cert_len ) ) != 0 ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 321 | { |
| 322 | mbedtls_x509_crt_free( session->peer_cert ); |
| 323 | mbedtls_free( session->peer_cert ); |
| 324 | session->peer_cert = NULL; |
| 325 | return( ret ); |
| 326 | } |
| 327 | |
| 328 | p += cert_len; |
| 329 | } |
Hanno Becker | c5fcbb3 | 2019-02-06 15:23:38 +0000 | [diff] [blame^] | 330 | #if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 331 | /* Deserialize CRT digest from the end of the ticket. */ |
| 332 | if( 1 > (size_t)( end - p ) ) |
| 333 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 334 | |
| 335 | cert_digest_len = (size_t) p[0]; |
| 336 | p++; |
| 337 | |
| 338 | if( cert_digest_len != 0 ) |
| 339 | { |
| 340 | if( cert_digest_len > (size_t)( end - p ) || |
| 341 | cert_digest_len != session->peer_cert_digest_len ) |
| 342 | { |
| 343 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 344 | } |
| 345 | |
| 346 | session->peer_cert_digest = mbedtls_calloc( 1, cert_digest_len ); |
| 347 | if( session->peer_cert_digest == NULL ) |
| 348 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
| 349 | |
| 350 | memcpy( session->peer_cert_digest, p, cert_digest_len ); |
| 351 | p += cert_digest_len; |
| 352 | } |
| 353 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 354 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
| 355 | |
| 356 | if( p != end ) |
| 357 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 358 | |
| 359 | return( 0 ); |
| 360 | } |
| 361 | |
| 362 | /* |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 363 | * Create session ticket, with the following structure: |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 364 | * |
| 365 | * struct { |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 366 | * opaque key_name[4]; |
| 367 | * opaque iv[12]; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 368 | * opaque encrypted_state<0..2^16-1>; |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 369 | * opaque tag[16]; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 370 | * } ticket; |
| 371 | * |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 372 | * The key_name, iv, and length of encrypted_state are the additional |
| 373 | * authenticated data. |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 374 | */ |
Hanno Becker | d140d08 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 375 | |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 376 | int mbedtls_ssl_ticket_write( void *p_ticket, |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 377 | const mbedtls_ssl_session *session, |
| 378 | unsigned char *start, |
| 379 | const unsigned char *end, |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 380 | size_t *tlen, |
| 381 | uint32_t *ticket_lifetime ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 382 | { |
| 383 | int ret; |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 384 | mbedtls_ssl_ticket_context *ctx = p_ticket; |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 385 | mbedtls_ssl_ticket_key *key; |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 386 | unsigned char *key_name = start; |
Hanno Becker | d140d08 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 387 | unsigned char *iv = start + TICKET_KEY_NAME_BYTES; |
| 388 | unsigned char *state_len_bytes = iv + TICKET_IV_BYTES; |
| 389 | unsigned char *state = state_len_bytes + TICKET_CRYPT_LEN_BYTES; |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 390 | unsigned char *tag; |
| 391 | size_t clear_len, ciph_len; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 392 | |
| 393 | *tlen = 0; |
| 394 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 395 | if( ctx == NULL || ctx->f_rng == NULL ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 396 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 397 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 398 | /* We need at least 4 bytes for key_name, 12 for IV, 2 for len 16 for tag, |
| 399 | * in addition to session itself, that will be checked when writing it. */ |
Hanno Becker | d140d08 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 400 | if( end - start < TICKET_MIN_LEN ) |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 401 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 402 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 403 | #if defined(MBEDTLS_THREADING_C) |
| 404 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 405 | return( ret ); |
| 406 | #endif |
| 407 | |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 408 | if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 ) |
| 409 | goto cleanup; |
| 410 | |
| 411 | key = &ctx->keys[ctx->active]; |
| 412 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 413 | *ticket_lifetime = ctx->ticket_lifetime; |
| 414 | |
Hanno Becker | d140d08 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 415 | memcpy( key_name, key->name, TICKET_KEY_NAME_BYTES ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 416 | |
Hanno Becker | d140d08 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 417 | 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] | 418 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 419 | |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 420 | /* Dump session state */ |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 421 | if( ( ret = ssl_save_session( session, |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 422 | state, end - state, &clear_len ) ) != 0 || |
| 423 | (unsigned long) clear_len > 65535 ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 424 | { |
| 425 | goto cleanup; |
| 426 | } |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 427 | state_len_bytes[0] = ( clear_len >> 8 ) & 0xff; |
| 428 | state_len_bytes[1] = ( clear_len ) & 0xff; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 429 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 430 | /* Encrypt and authenticate */ |
| 431 | tag = state + clear_len; |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 432 | if( ( ret = mbedtls_cipher_auth_encrypt( &key->ctx, |
Hanno Becker | d140d08 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 433 | iv, TICKET_IV_BYTES, |
| 434 | /* Additional data: key name, IV and length */ |
| 435 | key_name, TICKET_ADD_DATA_LEN, |
| 436 | state, clear_len, state, &ciph_len, |
| 437 | tag, TICKET_AUTH_TAG_BYTES ) ) != 0 ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 438 | { |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 439 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 440 | } |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 441 | if( ciph_len != clear_len ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 442 | { |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 443 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 444 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 445 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 446 | |
Hanno Becker | d140d08 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 447 | *tlen = TICKET_MIN_LEN + ciph_len; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 448 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 449 | cleanup: |
| 450 | #if defined(MBEDTLS_THREADING_C) |
| 451 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
| 452 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
| 453 | #endif |
| 454 | |
| 455 | return( ret ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /* |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 459 | * Select key based on name |
| 460 | */ |
| 461 | static mbedtls_ssl_ticket_key *ssl_ticket_select_key( |
| 462 | mbedtls_ssl_ticket_context *ctx, |
| 463 | const unsigned char name[4] ) |
| 464 | { |
| 465 | unsigned char i; |
| 466 | |
| 467 | for( i = 0; i < sizeof( ctx->keys ) / sizeof( *ctx->keys ); i++ ) |
| 468 | if( memcmp( name, ctx->keys[i].name, 4 ) == 0 ) |
| 469 | return( &ctx->keys[i] ); |
| 470 | |
| 471 | return( NULL ); |
| 472 | } |
| 473 | |
| 474 | /* |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 475 | * Load session ticket (see mbedtls_ssl_ticket_write for structure) |
| 476 | */ |
Manuel Pégourié-Gonnard | b0394be | 2015-05-19 11:40:30 +0200 | [diff] [blame] | 477 | int mbedtls_ssl_ticket_parse( void *p_ticket, |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 478 | mbedtls_ssl_session *session, |
| 479 | unsigned char *buf, |
| 480 | size_t len ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 481 | { |
| 482 | int ret; |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 483 | mbedtls_ssl_ticket_context *ctx = p_ticket; |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 484 | mbedtls_ssl_ticket_key *key; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 485 | unsigned char *key_name = buf; |
Hanno Becker | d140d08 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 486 | unsigned char *iv = buf + TICKET_KEY_NAME_BYTES; |
| 487 | unsigned char *enc_len_p = iv + TICKET_IV_BYTES; |
| 488 | unsigned char *ticket = enc_len_p + TICKET_CRYPT_LEN_BYTES; |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 489 | unsigned char *tag; |
| 490 | size_t enc_len, clear_len; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 491 | |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 492 | if( ctx == NULL || ctx->f_rng == NULL ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 493 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 494 | |
Hanno Becker | d140d08 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 495 | if( len < TICKET_MIN_LEN ) |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 496 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 497 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 498 | #if defined(MBEDTLS_THREADING_C) |
| 499 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 500 | return( ret ); |
| 501 | #endif |
| 502 | |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 503 | if( ( ret = ssl_ticket_update_keys( ctx ) ) != 0 ) |
| 504 | goto cleanup; |
| 505 | |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 506 | 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] | 507 | tag = ticket + enc_len; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 508 | |
Hanno Becker | d140d08 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 509 | if( len != TICKET_MIN_LEN + enc_len ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 510 | { |
| 511 | ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 512 | goto cleanup; |
| 513 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 514 | |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 515 | /* Select key */ |
| 516 | if( ( key = ssl_ticket_select_key( ctx, key_name ) ) == NULL ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 517 | { |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 518 | /* We can't know for sure but this is a likely option unless we're |
| 519 | * under attack - this is only informative anyway */ |
| 520 | ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED; |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 521 | goto cleanup; |
| 522 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 523 | |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 524 | /* Decrypt and authenticate */ |
Hanno Becker | d140d08 | 2018-11-17 21:18:01 +0000 | [diff] [blame] | 525 | if( ( ret = mbedtls_cipher_auth_decrypt( &key->ctx, |
| 526 | iv, TICKET_IV_BYTES, |
| 527 | /* Additional data: key name, IV and length */ |
| 528 | key_name, TICKET_ADD_DATA_LEN, |
| 529 | ticket, enc_len, |
| 530 | ticket, &clear_len, |
| 531 | tag, TICKET_AUTH_TAG_BYTES ) ) != 0 ) |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 532 | { |
Manuel Pégourié-Gonnard | 1e9c4db | 2015-05-25 14:07:08 +0200 | [diff] [blame] | 533 | if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED ) |
| 534 | ret = MBEDTLS_ERR_SSL_INVALID_MAC; |
| 535 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 536 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 537 | } |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 538 | if( clear_len != enc_len ) |
| 539 | { |
| 540 | ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 541 | goto cleanup; |
Manuel Pégourié-Gonnard | 1041a39 | 2015-05-20 19:59:39 +0200 | [diff] [blame] | 542 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 543 | |
| 544 | /* Actually load session */ |
Manuel Pégourié-Gonnard | 69f1728 | 2015-05-18 14:35:08 +0200 | [diff] [blame] | 545 | if( ( ret = ssl_load_session( session, ticket, clear_len ) ) != 0 ) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 546 | goto cleanup; |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 547 | |
| 548 | #if defined(MBEDTLS_HAVE_TIME) |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 549 | { |
Manuel Pégourié-Gonnard | 8eff512 | 2015-05-20 11:41:36 +0200 | [diff] [blame] | 550 | /* Check for expiration */ |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 551 | mbedtls_time_t current_time = mbedtls_time( NULL ); |
Manuel Pégourié-Gonnard | 8eff512 | 2015-05-20 11:41:36 +0200 | [diff] [blame] | 552 | |
| 553 | if( current_time < session->start || |
| 554 | (uint32_t)( current_time - session->start ) > ctx->ticket_lifetime ) |
| 555 | { |
| 556 | ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED; |
| 557 | goto cleanup; |
| 558 | } |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 559 | } |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 560 | #endif |
| 561 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 562 | cleanup: |
| 563 | #if defined(MBEDTLS_THREADING_C) |
| 564 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
| 565 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
| 566 | #endif |
| 567 | |
| 568 | return( ret ); |
Manuel Pégourié-Gonnard | a4a4735 | 2015-05-15 15:14:54 +0200 | [diff] [blame] | 569 | } |
| 570 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 571 | /* |
| 572 | * Free context |
| 573 | */ |
| 574 | void mbedtls_ssl_ticket_free( mbedtls_ssl_ticket_context *ctx ) |
| 575 | { |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 576 | mbedtls_cipher_free( &ctx->keys[0].ctx ); |
| 577 | mbedtls_cipher_free( &ctx->keys[1].ctx ); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 578 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 579 | #if defined(MBEDTLS_THREADING_C) |
| 580 | mbedtls_mutex_free( &ctx->mutex ); |
| 581 | #endif |
| 582 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 583 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ssl_ticket_context ) ); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 584 | } |
| 585 | |
Manuel Pégourié-Gonnard | fd6d897 | 2015-05-15 12:09:00 +0200 | [diff] [blame] | 586 | #endif /* MBEDTLS_SSL_TICKET_C */ |