Manuel Pégourié-Gonnard | fd6d897 | 2015-05-15 12:09:00 +0200 | [diff] [blame] | 1 | /** |
| 2 | * \file ssl_ticket.h |
| 3 | * |
| 4 | * \brief TLS server ticket callbacks implementation |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 5 | */ |
| 6 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 7 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 8 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Manuel Pégourié-Gonnard | fd6d897 | 2015-05-15 12:09:00 +0200 | [diff] [blame] | 9 | */ |
| 10 | #ifndef MBEDTLS_SSL_TICKET_H |
| 11 | #define MBEDTLS_SSL_TICKET_H |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 12 | #include "mbedtls/private_access.h" |
Manuel Pégourié-Gonnard | fd6d897 | 2015-05-15 12:09:00 +0200 | [diff] [blame] | 13 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 14 | #include "mbedtls/build_info.h" |
Ron Eldor | 8b0cf2e | 2018-02-14 16:02:41 +0200 | [diff] [blame] | 15 | |
Manuel Pégourié-Gonnard | 4214e3a | 2015-05-25 19:34:49 +0200 | [diff] [blame] | 16 | /* |
| 17 | * This implementation of the session ticket callbacks includes key |
| 18 | * management, rotating the keys periodically in order to preserve forward |
| 19 | * secrecy, when MBEDTLS_HAVE_TIME is defined. |
| 20 | */ |
| 21 | |
Jaeden Amero | 6609aef | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 22 | #include "mbedtls/ssl.h" |
| 23 | #include "mbedtls/cipher.h" |
Dave Rodgman | 536f28c | 2022-08-17 14:20:36 +0100 | [diff] [blame] | 24 | |
| 25 | #if defined(MBEDTLS_HAVE_TIME) |
Dave Rodgman | 392f714 | 2022-08-17 11:19:41 +0100 | [diff] [blame] | 26 | #include "mbedtls/platform_time.h" |
Dave Rodgman | 536f28c | 2022-08-17 14:20:36 +0100 | [diff] [blame] | 27 | #endif |
Manuel Pégourié-Gonnard | fd6d897 | 2015-05-15 12:09:00 +0200 | [diff] [blame] | 28 | |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 29 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 30 | #include "psa/crypto.h" |
| 31 | #endif |
| 32 | |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 33 | #if defined(MBEDTLS_THREADING_C) |
Jaeden Amero | 6609aef | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 34 | #include "mbedtls/threading.h" |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 35 | #endif |
| 36 | |
Manuel Pégourié-Gonnard | fd6d897 | 2015-05-15 12:09:00 +0200 | [diff] [blame] | 37 | #ifdef __cplusplus |
| 38 | extern "C" { |
| 39 | #endif |
| 40 | |
Glenn Strauss | a941b62 | 2022-02-09 15:24:56 -0500 | [diff] [blame] | 41 | #define MBEDTLS_SSL_TICKET_MAX_KEY_BYTES 32 /*!< Max supported key length in bytes */ |
| 42 | #define MBEDTLS_SSL_TICKET_KEY_NAME_BYTES 4 /*!< key name length in bytes */ |
| 43 | |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 44 | /** |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 45 | * \brief Information for session ticket protection |
| 46 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 47 | typedef struct mbedtls_ssl_ticket_key { |
Glenn Strauss | a941b62 | 2022-02-09 15:24:56 -0500 | [diff] [blame] | 48 | unsigned char MBEDTLS_PRIVATE(name)[MBEDTLS_SSL_TICKET_KEY_NAME_BYTES]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 49 | /*!< random key identifier */ |
Dave Rodgman | 536f28c | 2022-08-17 14:20:36 +0100 | [diff] [blame] | 50 | #if defined(MBEDTLS_HAVE_TIME) |
Dave Rodgman | 392f714 | 2022-08-17 11:19:41 +0100 | [diff] [blame] | 51 | mbedtls_time_t MBEDTLS_PRIVATE(generation_time); /*!< key generation timestamp (seconds) */ |
Dave Rodgman | 536f28c | 2022-08-17 14:20:36 +0100 | [diff] [blame] | 52 | #endif |
Ronald Cron | ba5165e | 2023-11-21 13:53:18 +0100 | [diff] [blame^] | 53 | /*! Lifetime of the key in seconds. This is also the lifetime of the |
| 54 | * tickets created under that key. |
| 55 | */ |
| 56 | uint32_t MBEDTLS_PRIVATE(lifetime); |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 57 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 58 | mbedtls_cipher_context_t MBEDTLS_PRIVATE(ctx); /*!< context for auth enc/decryption */ |
Gabor Mezei | 2a02051 | 2022-03-10 15:15:46 +0100 | [diff] [blame] | 59 | #else |
| 60 | mbedtls_svc_key_id_t MBEDTLS_PRIVATE(key); /*!< key used for auth enc/decryption */ |
| 61 | psa_algorithm_t MBEDTLS_PRIVATE(alg); /*!< algorithm of auth enc/decryption */ |
| 62 | psa_key_type_t MBEDTLS_PRIVATE(key_type); /*!< key type */ |
| 63 | size_t MBEDTLS_PRIVATE(key_bits); /*!< key length in bits */ |
| 64 | #endif |
Manuel Pégourié-Gonnard | 887674a | 2015-05-25 11:00:19 +0200 | [diff] [blame] | 65 | } |
| 66 | mbedtls_ssl_ticket_key; |
| 67 | |
| 68 | /** |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 69 | * \brief Context for session ticket handling functions |
| 70 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 71 | typedef struct mbedtls_ssl_ticket_context { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 72 | mbedtls_ssl_ticket_key MBEDTLS_PRIVATE(keys)[2]; /*!< ticket protection keys */ |
| 73 | unsigned char MBEDTLS_PRIVATE(active); /*!< index of the currently active key */ |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 74 | |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 75 | uint32_t MBEDTLS_PRIVATE(ticket_lifetime); /*!< lifetime of tickets in seconds */ |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 76 | |
| 77 | /** Callback for getting (pseudo-)random numbers */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 78 | int(*MBEDTLS_PRIVATE(f_rng))(void *, unsigned char *, size_t); |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 79 | void *MBEDTLS_PRIVATE(p_rng); /*!< context for the RNG function */ |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 80 | |
| 81 | #if defined(MBEDTLS_THREADING_C) |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 82 | mbedtls_threading_mutex_t MBEDTLS_PRIVATE(mutex); |
Manuel Pégourié-Gonnard | 0849a0a | 2015-05-20 11:34:54 +0200 | [diff] [blame] | 83 | #endif |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 84 | } |
| 85 | mbedtls_ssl_ticket_context; |
| 86 | |
| 87 | /** |
| 88 | * \brief Initialize a ticket context. |
| 89 | * (Just make it ready for mbedtls_ssl_ticket_setup() |
| 90 | * or mbedtls_ssl_ticket_free().) |
| 91 | * |
| 92 | * \param ctx Context to be initialized |
| 93 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 94 | void mbedtls_ssl_ticket_init(mbedtls_ssl_ticket_context *ctx); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * \brief Prepare context to be actually used |
| 98 | * |
| 99 | * \param ctx Context to be set up |
Manuel Pégourié-Gonnard | ad5390f | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 100 | * \param f_rng RNG callback function (mandatory) |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 101 | * \param p_rng RNG callback context |
Manuel Pégourié-Gonnard | dc54ff8 | 2015-06-25 12:44:46 +0200 | [diff] [blame] | 102 | * \param cipher AEAD cipher to use for ticket protection. |
| 103 | * Recommended value: MBEDTLS_CIPHER_AES_256_GCM. |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 104 | * \param lifetime Tickets lifetime in seconds |
Manuel Pégourié-Gonnard | dc54ff8 | 2015-06-25 12:44:46 +0200 | [diff] [blame] | 105 | * Recommended value: 86400 (one day). |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 106 | * |
Manuel Pégourié-Gonnard | a0adc1b | 2015-05-25 10:35:16 +0200 | [diff] [blame] | 107 | * \note It is highly recommended to select a cipher that is at |
Tobias Nießen | 1e8ca12 | 2021-05-10 19:53:15 +0200 | [diff] [blame] | 108 | * least as strong as the strongest ciphersuite |
Manuel Pégourié-Gonnard | a0adc1b | 2015-05-25 10:35:16 +0200 | [diff] [blame] | 109 | * supported. Usually that means a 256-bit key. |
| 110 | * |
Manuel Pégourié-Gonnard | dc54ff8 | 2015-06-25 12:44:46 +0200 | [diff] [blame] | 111 | * \note The lifetime of the keys is twice the lifetime of tickets. |
Glenn Strauss | a950938 | 2022-02-02 23:32:18 -0500 | [diff] [blame] | 112 | * It is recommended to pick a reasonable lifetime so as not |
Manuel Pégourié-Gonnard | dc54ff8 | 2015-06-25 12:44:46 +0200 | [diff] [blame] | 113 | * to negate the benefits of forward secrecy. |
| 114 | * |
Manuel Pégourié-Gonnard | 81abefd | 2015-05-29 12:53:47 +0200 | [diff] [blame] | 115 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 116 | * or a specific MBEDTLS_ERR_XXX error code |
| 117 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 118 | int mbedtls_ssl_ticket_setup(mbedtls_ssl_ticket_context *ctx, |
| 119 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 120 | mbedtls_cipher_type_t cipher, |
| 121 | uint32_t lifetime); |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 122 | |
| 123 | /** |
Glenn Strauss | a950938 | 2022-02-02 23:32:18 -0500 | [diff] [blame] | 124 | * \brief Rotate session ticket encryption key to new specified key. |
| 125 | * Provides for external control of session ticket encryption |
| 126 | * key rotation, e.g. for synchronization between different |
| 127 | * machines. If this function is not used, or if not called |
| 128 | * before ticket lifetime expires, then a new session ticket |
| 129 | * encryption key is generated internally in order to avoid |
| 130 | * unbounded session ticket encryption key lifetimes. |
| 131 | * |
| 132 | * \param ctx Context to be set up |
| 133 | * \param name Session ticket encryption key name |
| 134 | * \param nlength Session ticket encryption key name length in bytes |
| 135 | * \param k Session ticket encryption key |
| 136 | * \param klength Session ticket encryption key length in bytes |
| 137 | * \param lifetime Tickets lifetime in seconds |
| 138 | * Recommended value: 86400 (one day). |
| 139 | * |
| 140 | * \note \c name and \c k are recommended to be cryptographically |
| 141 | * random data. |
| 142 | * |
| 143 | * \note \c nlength must match sizeof( ctx->name ) |
| 144 | * |
| 145 | * \note \c klength must be sufficient for use by cipher specified |
| 146 | * to \c mbedtls_ssl_ticket_setup |
| 147 | * |
| 148 | * \note The lifetime of the keys is twice the lifetime of tickets. |
| 149 | * It is recommended to pick a reasonable lifetime so as not |
| 150 | * to negate the benefits of forward secrecy. |
| 151 | * |
| 152 | * \return 0 if successful, |
| 153 | * or a specific MBEDTLS_ERR_XXX error code |
| 154 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 155 | int mbedtls_ssl_ticket_rotate(mbedtls_ssl_ticket_context *ctx, |
| 156 | const unsigned char *name, size_t nlength, |
| 157 | const unsigned char *k, size_t klength, |
| 158 | uint32_t lifetime); |
Glenn Strauss | a950938 | 2022-02-02 23:32:18 -0500 | [diff] [blame] | 159 | |
| 160 | /** |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 161 | * \brief Implementation of the ticket write callback |
| 162 | * |
Antonin Décimo | 36e89b5 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 163 | * \note See \c mbedtls_ssl_ticket_write_t for description |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 164 | */ |
| 165 | mbedtls_ssl_ticket_write_t mbedtls_ssl_ticket_write; |
| 166 | |
| 167 | /** |
| 168 | * \brief Implementation of the ticket parse callback |
| 169 | * |
Antonin Décimo | 36e89b5 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 170 | * \note See \c mbedtls_ssl_ticket_parse_t for description |
Manuel Pégourié-Gonnard | d59675d | 2015-05-19 15:28:00 +0200 | [diff] [blame] | 171 | */ |
| 172 | mbedtls_ssl_ticket_parse_t mbedtls_ssl_ticket_parse; |
| 173 | |
| 174 | /** |
| 175 | * \brief Free a context's content and zeroize it. |
| 176 | * |
| 177 | * \param ctx Context to be cleaned up |
| 178 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 179 | void mbedtls_ssl_ticket_free(mbedtls_ssl_ticket_context *ctx); |
Manuel Pégourié-Gonnard | fd6d897 | 2015-05-15 12:09:00 +0200 | [diff] [blame] | 180 | |
| 181 | #ifdef __cplusplus |
| 182 | } |
| 183 | #endif |
| 184 | |
| 185 | #endif /* ssl_ticket.h */ |