blob: fc86542c1ea1c6e316198ad136fcfab5a973e619 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +020021 */
22#ifndef MBEDTLS_SSL_TICKET_H
23#define MBEDTLS_SSL_TICKET_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020024#include "mbedtls/private_access.h"
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +020025
Bence Szépkútic662b362021-05-27 11:25:03 +020026#include "mbedtls/build_info.h"
Ron Eldor8b0cf2e2018-02-14 16:02:41 +020027
Manuel Pégourié-Gonnard4214e3a2015-05-25 19:34:49 +020028/*
29 * This implementation of the session ticket callbacks includes key
30 * management, rotating the keys periodically in order to preserve forward
31 * secrecy, when MBEDTLS_HAVE_TIME is defined.
32 */
33
Jaeden Amero6609aef2019-07-04 20:01:14 +010034#include "mbedtls/ssl.h"
35#include "mbedtls/cipher.h"
Dave Rodgman392f7142022-08-17 11:19:41 +010036#include "mbedtls/platform_time.h"
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +020037
Gabor Mezei2a020512022-03-10 15:15:46 +010038#if defined(MBEDTLS_USE_PSA_CRYPTO)
39#include "psa/crypto.h"
40#endif
41
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +020042#if defined(MBEDTLS_THREADING_C)
Jaeden Amero6609aef2019-07-04 20:01:14 +010043#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +020044#endif
45
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +020046#ifdef __cplusplus
47extern "C" {
48#endif
49
Glenn Straussa941b622022-02-09 15:24:56 -050050#define MBEDTLS_SSL_TICKET_MAX_KEY_BYTES 32 /*!< Max supported key length in bytes */
51#define MBEDTLS_SSL_TICKET_KEY_NAME_BYTES 4 /*!< key name length in bytes */
52
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020053/**
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020054 * \brief Information for session ticket protection
55 */
Dawid Drozd428cc522018-07-24 10:02:47 +020056typedef struct mbedtls_ssl_ticket_key
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020057{
Glenn Straussa941b622022-02-09 15:24:56 -050058 unsigned char MBEDTLS_PRIVATE(name)[MBEDTLS_SSL_TICKET_KEY_NAME_BYTES];
59 /*!< random key identifier */
Dave Rodgman392f7142022-08-17 11:19:41 +010060 mbedtls_time_t MBEDTLS_PRIVATE(generation_time); /*!< key generation timestamp (seconds) */
Gabor Mezei2a020512022-03-10 15:15:46 +010061#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Mateusz Starzyk846f0212021-05-19 19:44:07 +020062 mbedtls_cipher_context_t MBEDTLS_PRIVATE(ctx); /*!< context for auth enc/decryption */
Gabor Mezei2a020512022-03-10 15:15:46 +010063#else
64 mbedtls_svc_key_id_t MBEDTLS_PRIVATE(key); /*!< key used for auth enc/decryption */
65 psa_algorithm_t MBEDTLS_PRIVATE(alg); /*!< algorithm of auth enc/decryption */
66 psa_key_type_t MBEDTLS_PRIVATE(key_type); /*!< key type */
67 size_t MBEDTLS_PRIVATE(key_bits); /*!< key length in bits */
68#endif
Manuel Pégourié-Gonnard887674a2015-05-25 11:00:19 +020069}
70mbedtls_ssl_ticket_key;
71
72/**
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020073 * \brief Context for session ticket handling functions
74 */
Dawid Drozd428cc522018-07-24 10:02:47 +020075typedef struct mbedtls_ssl_ticket_context
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020076{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020077 mbedtls_ssl_ticket_key MBEDTLS_PRIVATE(keys)[2]; /*!< ticket protection keys */
78 unsigned char MBEDTLS_PRIVATE(active); /*!< index of the currently active key */
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020079
Mateusz Starzyk846f0212021-05-19 19:44:07 +020080 uint32_t MBEDTLS_PRIVATE(ticket_lifetime); /*!< lifetime of tickets in seconds */
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020081
82 /** Callback for getting (pseudo-)random numbers */
Mateusz Starzyk846f0212021-05-19 19:44:07 +020083 int (*MBEDTLS_PRIVATE(f_rng))(void *, unsigned char *, size_t);
84 void *MBEDTLS_PRIVATE(p_rng); /*!< context for the RNG function */
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +020085
86#if defined(MBEDTLS_THREADING_C)
Mateusz Starzyk846f0212021-05-19 19:44:07 +020087 mbedtls_threading_mutex_t MBEDTLS_PRIVATE(mutex);
Manuel Pégourié-Gonnard0849a0a2015-05-20 11:34:54 +020088#endif
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +020089}
90mbedtls_ssl_ticket_context;
91
92/**
93 * \brief Initialize a ticket context.
94 * (Just make it ready for mbedtls_ssl_ticket_setup()
95 * or mbedtls_ssl_ticket_free().)
96 *
97 * \param ctx Context to be initialized
98 */
99void mbedtls_ssl_ticket_init( mbedtls_ssl_ticket_context *ctx );
100
101/**
102 * \brief Prepare context to be actually used
103 *
104 * \param ctx Context to be set up
Manuel Pégourié-Gonnardad5390f2021-06-15 11:29:26 +0200105 * \param f_rng RNG callback function (mandatory)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200106 * \param p_rng RNG callback context
Manuel Pégourié-Gonnarddc54ff82015-06-25 12:44:46 +0200107 * \param cipher AEAD cipher to use for ticket protection.
108 * Recommended value: MBEDTLS_CIPHER_AES_256_GCM.
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200109 * \param lifetime Tickets lifetime in seconds
Manuel Pégourié-Gonnarddc54ff82015-06-25 12:44:46 +0200110 * Recommended value: 86400 (one day).
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200111 *
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200112 * \note It is highly recommended to select a cipher that is at
Tobias Nießen1e8ca122021-05-10 19:53:15 +0200113 * least as strong as the strongest ciphersuite
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200114 * supported. Usually that means a 256-bit key.
115 *
Manuel Pégourié-Gonnarddc54ff82015-06-25 12:44:46 +0200116 * \note The lifetime of the keys is twice the lifetime of tickets.
Glenn Straussa9509382022-02-02 23:32:18 -0500117 * It is recommended to pick a reasonable lifetime so as not
Manuel Pégourié-Gonnarddc54ff82015-06-25 12:44:46 +0200118 * to negate the benefits of forward secrecy.
119 *
Manuel Pégourié-Gonnard81abefd2015-05-29 12:53:47 +0200120 * \return 0 if successful,
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200121 * or a specific MBEDTLS_ERR_XXX error code
122 */
123int mbedtls_ssl_ticket_setup( mbedtls_ssl_ticket_context *ctx,
124 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
Manuel Pégourié-Gonnarda0adc1b2015-05-25 10:35:16 +0200125 mbedtls_cipher_type_t cipher,
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200126 uint32_t lifetime );
127
128/**
Glenn Straussa9509382022-02-02 23:32:18 -0500129 * \brief Rotate session ticket encryption key to new specified key.
130 * Provides for external control of session ticket encryption
131 * key rotation, e.g. for synchronization between different
132 * machines. If this function is not used, or if not called
133 * before ticket lifetime expires, then a new session ticket
134 * encryption key is generated internally in order to avoid
135 * unbounded session ticket encryption key lifetimes.
136 *
137 * \param ctx Context to be set up
138 * \param name Session ticket encryption key name
139 * \param nlength Session ticket encryption key name length in bytes
140 * \param k Session ticket encryption key
141 * \param klength Session ticket encryption key length in bytes
142 * \param lifetime Tickets lifetime in seconds
143 * Recommended value: 86400 (one day).
144 *
145 * \note \c name and \c k are recommended to be cryptographically
146 * random data.
147 *
148 * \note \c nlength must match sizeof( ctx->name )
149 *
150 * \note \c klength must be sufficient for use by cipher specified
151 * to \c mbedtls_ssl_ticket_setup
152 *
153 * \note The lifetime of the keys is twice the lifetime of tickets.
154 * It is recommended to pick a reasonable lifetime so as not
155 * to negate the benefits of forward secrecy.
156 *
157 * \return 0 if successful,
158 * or a specific MBEDTLS_ERR_XXX error code
159 */
160int mbedtls_ssl_ticket_rotate( mbedtls_ssl_ticket_context *ctx,
161 const unsigned char *name, size_t nlength,
162 const unsigned char *k, size_t klength,
163 uint32_t lifetime );
164
165/**
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200166 * \brief Implementation of the ticket write callback
167 *
Antonin Décimo36e89b52019-01-23 15:24:37 +0100168 * \note See \c mbedtls_ssl_ticket_write_t for description
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200169 */
170mbedtls_ssl_ticket_write_t mbedtls_ssl_ticket_write;
171
172/**
173 * \brief Implementation of the ticket parse callback
174 *
Antonin Décimo36e89b52019-01-23 15:24:37 +0100175 * \note See \c mbedtls_ssl_ticket_parse_t for description
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +0200176 */
177mbedtls_ssl_ticket_parse_t mbedtls_ssl_ticket_parse;
178
179/**
180 * \brief Free a context's content and zeroize it.
181 *
182 * \param ctx Context to be cleaned up
183 */
184void mbedtls_ssl_ticket_free( mbedtls_ssl_ticket_context *ctx );
Manuel Pégourié-Gonnardfd6d8972015-05-15 12:09:00 +0200185
186#ifdef __cplusplus
187}
188#endif
189
190#endif /* ssl_ticket.h */