blob: 6dfe371a2a3e31e3128f74fc321cbb98badbc46f [file] [log] [blame]
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +02001/**
2 * \file ssl_ticket.h
3 *
4 * \brief TLS server ticket callbacks implementation
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +02009 */
10#ifndef MBEDTLS_SSL_TICKET_H
11#define MBEDTLS_SSL_TICKET_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020012#include "mbedtls/private_access.h"
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +020013
Bence Szépkútic662b362021-05-27 11:25:03 +020014#include "mbedtls/build_info.h"
Ron Eldor8b0cf2e2018-02-14 16:02:41 +020015
Manuel Pégourié-Gonnard4214e3a2015-05-25 19:34:49 +020016/*
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 Amero6609aef2019-07-04 20:01:14 +010022#include "mbedtls/ssl.h"
Dave Rodgman536f28c2022-08-17 14:20:36 +010023
24#if defined(MBEDTLS_HAVE_TIME)
Dave Rodgman392f7142022-08-17 11:19:41 +010025#include "mbedtls/platform_time.h"
Dave Rodgman536f28c2022-08-17 14:20:36 +010026#endif
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +020027
Gabor Mezei2a020512022-03-10 15:15:46 +010028#include "psa/crypto.h"
Gabor Mezei2a020512022-03-10 15:15:46 +010029
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +020030#if defined(MBEDTLS_THREADING_C)
Jaeden Amero6609aef2019-07-04 20:01:14 +010031#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +020032#endif
33
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +020034#ifdef __cplusplus
35extern "C" {
36#endif
37
Glenn Straussa941b622022-02-09 15:24:56 -050038#define MBEDTLS_SSL_TICKET_MAX_KEY_BYTES 32 /*!< Max supported key length in bytes */
39#define MBEDTLS_SSL_TICKET_KEY_NAME_BYTES 4 /*!< key name length in bytes */
40
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020041/**
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020042 * \brief Information for session ticket protection
43 */
Gilles Peskine449bd832023-01-11 14:50:10 +010044typedef struct mbedtls_ssl_ticket_key {
Glenn Straussa941b622022-02-09 15:24:56 -050045 unsigned char MBEDTLS_PRIVATE(name)[MBEDTLS_SSL_TICKET_KEY_NAME_BYTES];
Gilles Peskine449bd832023-01-11 14:50:10 +010046 /*!< random key identifier */
Dave Rodgman536f28c2022-08-17 14:20:36 +010047#if defined(MBEDTLS_HAVE_TIME)
Dave Rodgman392f7142022-08-17 11:19:41 +010048 mbedtls_time_t MBEDTLS_PRIVATE(generation_time); /*!< key generation timestamp (seconds) */
Dave Rodgman536f28c2022-08-17 14:20:36 +010049#endif
Ronald Cronba5165e2023-11-21 13:53:18 +010050 /*! Lifetime of the key in seconds. This is also the lifetime of the
51 * tickets created under that key.
52 */
53 uint32_t MBEDTLS_PRIVATE(lifetime);
Gabor Mezei2a020512022-03-10 15:15:46 +010054 mbedtls_svc_key_id_t MBEDTLS_PRIVATE(key); /*!< key used for auth enc/decryption */
55 psa_algorithm_t MBEDTLS_PRIVATE(alg); /*!< algorithm of auth enc/decryption */
56 psa_key_type_t MBEDTLS_PRIVATE(key_type); /*!< key type */
57 size_t MBEDTLS_PRIVATE(key_bits); /*!< key length in bits */
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020058}
59mbedtls_ssl_ticket_key;
60
61/**
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020062 * \brief Context for session ticket handling functions
63 */
Gilles Peskine449bd832023-01-11 14:50:10 +010064typedef struct mbedtls_ssl_ticket_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020065 mbedtls_ssl_ticket_key MBEDTLS_PRIVATE(keys)[2]; /*!< ticket protection keys */
66 unsigned char MBEDTLS_PRIVATE(active); /*!< index of the currently active key */
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020067
Mateusz Starzyk846f0212021-05-19 19:44:07 +020068 uint32_t MBEDTLS_PRIVATE(ticket_lifetime); /*!< lifetime of tickets in seconds */
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020069
70 /** Callback for getting (pseudo-)random numbers */
Gilles Peskine449bd832023-01-11 14:50:10 +010071 int(*MBEDTLS_PRIVATE(f_rng))(void *, unsigned char *, size_t);
Mateusz Starzyk846f0212021-05-19 19:44:07 +020072 void *MBEDTLS_PRIVATE(p_rng); /*!< context for the RNG function */
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +020073
74#if defined(MBEDTLS_THREADING_C)
Mateusz Starzyk846f0212021-05-19 19:44:07 +020075 mbedtls_threading_mutex_t MBEDTLS_PRIVATE(mutex);
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +020076#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020077}
78mbedtls_ssl_ticket_context;
79
80/**
81 * \brief Initialize a ticket context.
82 * (Just make it ready for mbedtls_ssl_ticket_setup()
83 * or mbedtls_ssl_ticket_free().)
84 *
85 * \param ctx Context to be initialized
86 */
Gilles Peskine449bd832023-01-11 14:50:10 +010087void mbedtls_ssl_ticket_init(mbedtls_ssl_ticket_context *ctx);
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020088
89/**
90 * \brief Prepare context to be actually used
91 *
92 * \param ctx Context to be set up
Manuel Pégourié-Gonnardad5390f2021-06-15 11:29:26 +020093 * \param f_rng RNG callback function (mandatory)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020094 * \param p_rng RNG callback context
Ben Taylor0c29cf82025-01-29 08:18:43 +000095 * \param alg Cryptographic algorithm to use recomended value
96 * PSA_ALG_GCM from include/psa/crypto_values.h.
97 * \param key_type Cryptographic key type to use recomended value
98 * PSA_KEY_TYPE_AES from include/psa/crypto_values.h.
99 * \param key_bits Cryptographic key type to use recomended value
100 * PSA_KEY_TYPE_AES from include/psa/crypto_values.h.
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200101 * \param lifetime Tickets lifetime in seconds
Manuel Pégourié-Gonnarddc54ff82015-06-25 12:44:46 +0200102 * Recommended value: 86400 (one day).
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200103 *
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200104 * \note It is highly recommended to select a cipher that is at
Tobias Nießen1e8ca122021-05-10 19:53:15 +0200105 * least as strong as the strongest ciphersuite
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200106 * supported. Usually that means a 256-bit key.
107 *
Ronald Cron0050dff2024-03-08 16:30:22 +0100108 * \note It is recommended to pick a reasonable lifetime so as not
Manuel Pégourié-Gonnarddc54ff82015-06-25 12:44:46 +0200109 * to negate the benefits of forward secrecy.
110 *
Jerry Yuce794882023-11-22 15:01:18 +0800111 * \note The TLS 1.3 specification states that ticket lifetime must
112 * be smaller than seven days. If ticket lifetime has been
113 * set to a value greater than seven days in this module then
114 * if the TLS 1.3 is configured to send tickets after the
115 * handshake it will fail the connection when trying to send
116 * the first ticket.
117 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200118 * \return 0 if successful,
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200119 * or a specific MBEDTLS_ERR_XXX error code
120 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100121int mbedtls_ssl_ticket_setup(mbedtls_ssl_ticket_context *ctx,
122 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Ben Taylor0c29cf82025-01-29 08:18:43 +0000123 psa_algorithm_t alg, psa_key_type_t key_type, psa_key_bits_t key_bits,
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 uint32_t lifetime);
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200125
126/**
Glenn Straussa9509382022-02-02 23:32:18 -0500127 * \brief Rotate session ticket encryption key to new specified key.
128 * Provides for external control of session ticket encryption
129 * key rotation, e.g. for synchronization between different
130 * machines. If this function is not used, or if not called
131 * before ticket lifetime expires, then a new session ticket
132 * encryption key is generated internally in order to avoid
133 * unbounded session ticket encryption key lifetimes.
134 *
135 * \param ctx Context to be set up
136 * \param name Session ticket encryption key name
137 * \param nlength Session ticket encryption key name length in bytes
138 * \param k Session ticket encryption key
139 * \param klength Session ticket encryption key length in bytes
140 * \param lifetime Tickets lifetime in seconds
141 * Recommended value: 86400 (one day).
142 *
143 * \note \c name and \c k are recommended to be cryptographically
144 * random data.
145 *
146 * \note \c nlength must match sizeof( ctx->name )
147 *
148 * \note \c klength must be sufficient for use by cipher specified
149 * to \c mbedtls_ssl_ticket_setup
150 *
Ronald Cron0050dff2024-03-08 16:30:22 +0100151 * \note It is recommended to pick a reasonable lifetime so as not
Glenn Straussa9509382022-02-02 23:32:18 -0500152 * to negate the benefits of forward secrecy.
153 *
Jerry Yuce794882023-11-22 15:01:18 +0800154 * \note The TLS 1.3 specification states that ticket lifetime must
155 * be smaller than seven days. If ticket lifetime has been
156 * set to a value greater than seven days in this module then
157 * if the TLS 1.3 is configured to send tickets after the
158 * handshake it will fail the connection when trying to send
159 * the first ticket.
160 *
Glenn Straussa9509382022-02-02 23:32:18 -0500161 * \return 0 if successful,
162 * or a specific MBEDTLS_ERR_XXX error code
163 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100164int mbedtls_ssl_ticket_rotate(mbedtls_ssl_ticket_context *ctx,
165 const unsigned char *name, size_t nlength,
166 const unsigned char *k, size_t klength,
167 uint32_t lifetime);
Glenn Straussa9509382022-02-02 23:32:18 -0500168
169/**
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200170 * \brief Implementation of the ticket write callback
171 *
Antonin Décimo36e89b52019-01-23 15:24:37 +0100172 * \note See \c mbedtls_ssl_ticket_write_t for description
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200173 */
174mbedtls_ssl_ticket_write_t mbedtls_ssl_ticket_write;
175
176/**
177 * \brief Implementation of the ticket parse callback
178 *
Antonin Décimo36e89b52019-01-23 15:24:37 +0100179 * \note See \c mbedtls_ssl_ticket_parse_t for description
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200180 */
181mbedtls_ssl_ticket_parse_t mbedtls_ssl_ticket_parse;
182
183/**
184 * \brief Free a context's content and zeroize it.
185 *
186 * \param ctx Context to be cleaned up
187 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100188void mbedtls_ssl_ticket_free(mbedtls_ssl_ticket_context *ctx);
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +0200189
190#ifdef __cplusplus
191}
192#endif
193
194#endif /* ssl_ticket.h */