blob: a1307b450876236c7a7e10d53c895a002443d049 [file] [log] [blame]
Paul Bakker0a597072012-09-25 21:55:46 +00001/**
2 * \file ssl_cache.h
3 *
4 * \brief SSL session cache 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
Paul Bakker0a597072012-09-25 21:55:46 +00009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#ifndef MBEDTLS_SSL_CACHE_H
11#define MBEDTLS_SSL_CACHE_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020012#include "mbedtls/private_access.h"
Paul Bakker0a597072012-09-25 21:55:46 +000013
Bence Szépkútic662b362021-05-27 11:25:03 +020014#include "mbedtls/build_info.h"
Ron Eldor9cbd1b22018-12-16 12:14:37 +020015
Jaeden Amero6609aef2019-07-04 20:01:14 +010016#include "mbedtls/ssl.h"
Paul Bakker0a597072012-09-25 21:55:46 +000017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020018#if defined(MBEDTLS_THREADING_C)
Jaeden Amero6609aef2019-07-04 20:01:14 +010019#include "mbedtls/threading.h"
Paul Bakkerc5598842013-09-28 15:01:27 +020020#endif
21
Paul Bakker088c5c52014-04-25 11:11:10 +020022/**
23 * \name SECTION: Module settings
24 *
25 * The configuration options you can set for this module are in this section.
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020026 * Either change them in mbedtls_config.h or define them on the compiler command line.
Paul Bakker088c5c52014-04-25 11:11:10 +020027 * \{
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT)
31#define MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT 86400 /*!< 1 day */
Paul Bakker088c5c52014-04-25 11:11:10 +020032#endif
33
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if !defined(MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES)
35#define MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES 50 /*!< Maximum entries in cache */
Paul Bakker088c5c52014-04-25 11:11:10 +020036#endif
37
Andrzej Kurek38d4fdd2021-12-28 16:22:52 +010038/** \} name SECTION: Module settings */
Paul Bakker0a597072012-09-25 21:55:46 +000039
40#ifdef __cplusplus
41extern "C" {
42#endif
43
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044typedef struct mbedtls_ssl_cache_context mbedtls_ssl_cache_context;
45typedef struct mbedtls_ssl_cache_entry mbedtls_ssl_cache_entry;
Paul Bakker0a597072012-09-25 21:55:46 +000046
47/**
48 * \brief This structure is used for storing cache entries
49 */
Gilles Peskine449bd832023-01-11 14:50:10 +010050struct mbedtls_ssl_cache_entry {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_HAVE_TIME)
Mateusz Starzyk846f0212021-05-19 19:44:07 +020052 mbedtls_time_t MBEDTLS_PRIVATE(timestamp); /*!< entry timestamp */
Paul Bakkerfa9b1002013-07-03 15:31:03 +020053#endif
Hanno Becker7e6eb9f2021-04-15 10:26:06 +010054
Mateusz Starzyk846f0212021-05-19 19:44:07 +020055 unsigned char MBEDTLS_PRIVATE(session_id)[32]; /*!< session ID */
56 size_t MBEDTLS_PRIVATE(session_id_len);
Hanno Becker7e6eb9f2021-04-15 10:26:06 +010057
Mateusz Starzyk846f0212021-05-19 19:44:07 +020058 unsigned char *MBEDTLS_PRIVATE(session); /*!< serialized session */
59 size_t MBEDTLS_PRIVATE(session_len);
Hanno Becker7e6eb9f2021-04-15 10:26:06 +010060
Mateusz Starzyk846f0212021-05-19 19:44:07 +020061 mbedtls_ssl_cache_entry *MBEDTLS_PRIVATE(next); /*!< chain pointer */
Paul Bakker0a597072012-09-25 21:55:46 +000062};
63
64/**
65 * \brief Cache context
66 */
Gilles Peskine449bd832023-01-11 14:50:10 +010067struct mbedtls_ssl_cache_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020068 mbedtls_ssl_cache_entry *MBEDTLS_PRIVATE(chain); /*!< start of the chain */
69 int MBEDTLS_PRIVATE(timeout); /*!< cache entry timeout */
70 int MBEDTLS_PRIVATE(max_entries); /*!< maximum entries */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if defined(MBEDTLS_THREADING_C)
Mateusz Starzyk846f0212021-05-19 19:44:07 +020072 mbedtls_threading_mutex_t MBEDTLS_PRIVATE(mutex); /*!< mutex */
Paul Bakkerc5598842013-09-28 15:01:27 +020073#endif
Paul Bakker0a597072012-09-25 21:55:46 +000074};
75
76/**
77 * \brief Initialize an SSL cache context
78 *
79 * \param cache SSL cache context
80 */
Gilles Peskine449bd832023-01-11 14:50:10 +010081void mbedtls_ssl_cache_init(mbedtls_ssl_cache_context *cache);
Paul Bakker0a597072012-09-25 21:55:46 +000082
83/**
84 * \brief Cache get callback implementation
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 * (Thread-safe if MBEDTLS_THREADING_C is enabled)
Paul Bakker0a597072012-09-25 21:55:46 +000086 *
Hanno Beckerccdaf6e2021-04-15 09:26:17 +010087 * \param data The SSL cache context to use.
88 * \param session_id The pointer to the buffer holding the session ID
89 * for the session to load.
90 * \param session_id_len The length of \p session_id in bytes.
91 * \param session The address at which to store the session
92 * associated with \p session_id, if present.
Pengyu Lv4e707242023-03-27 11:29:49 +080093 *
94 * \return \c 0 on success.
Pengyu Lve3746d72023-04-10 14:40:03 +080095 * \return #MBEDTLS_ERR_SSL_CACHE_ENTRY_NOT_FOUND if there is
Pengyu Lv723ac262023-04-11 09:19:08 +080096 * no cache entry with specified session ID found, or
97 * any other negative error code for other failures.
Paul Bakker0a597072012-09-25 21:55:46 +000098 */
Gilles Peskine449bd832023-01-11 14:50:10 +010099int mbedtls_ssl_cache_get(void *data,
100 unsigned char const *session_id,
101 size_t session_id_len,
102 mbedtls_ssl_session *session);
Paul Bakker0a597072012-09-25 21:55:46 +0000103
104/**
105 * \brief Cache set callback implementation
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 * (Thread-safe if MBEDTLS_THREADING_C is enabled)
Paul Bakker0a597072012-09-25 21:55:46 +0000107 *
Hanno Beckerccdaf6e2021-04-15 09:26:17 +0100108 * \param data The SSL cache context to use.
109 * \param session_id The pointer to the buffer holding the session ID
110 * associated to \p session.
111 * \param session_id_len The length of \p session_id in bytes.
112 * \param session The session to store.
Pengyu Lv4e707242023-03-27 11:29:49 +0800113 *
114 * \return \c 0 on success.
115 * \return A negative error code on failure.
Paul Bakker0a597072012-09-25 21:55:46 +0000116 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100117int mbedtls_ssl_cache_set(void *data,
118 unsigned char const *session_id,
119 size_t session_id_len,
120 const mbedtls_ssl_session *session);
Paul Bakker0a597072012-09-25 21:55:46 +0000121
Pengyu Lv7b6299b2023-03-07 14:38:45 +0800122/**
123 * \brief Remove the cache entry by the session ID
124 * (Thread-safe if MBEDTLS_THREADING_C is enabled)
125 *
126 * \param data The SSL cache context to use.
127 * \param session_id The pointer to the buffer holding the session ID
Andrzej Kurek00b54e62023-05-06 09:38:57 -0400128 * associated to session.
Pengyu Lv7b6299b2023-03-07 14:38:45 +0800129 * \param session_id_len The length of \p session_id in bytes.
130 *
Pengyu Lvcdf06f62023-03-23 11:15:24 +0800131 * \return \c 0 on success. This indicates the cache entry for
132 * the session with provided ID is removed or does not
133 * exist.
134 * \return A negative error code on failure.
Pengyu Lv7b6299b2023-03-07 14:38:45 +0800135 */
136int mbedtls_ssl_cache_remove(void *data,
137 unsigned char const *session_id,
138 size_t session_id_len);
139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000141/**
142 * \brief Set the cache timeout
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 * (Default: MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT (1 day))
Paul Bakker0a597072012-09-25 21:55:46 +0000144 *
145 * A timeout of 0 indicates no timeout.
146 *
147 * \param cache SSL cache context
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100148 * \param timeout cache entry timeout in seconds
Paul Bakker0a597072012-09-25 21:55:46 +0000149 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100150void mbedtls_ssl_cache_set_timeout(mbedtls_ssl_cache_context *cache, int timeout);
Pengyu Lvaf724dd2023-03-06 11:56:10 +0800151
152/**
153 * \brief Get the cache timeout
154 *
155 * A timeout of 0 indicates no timeout.
156 *
157 * \param cache SSL cache context
158 *
159 * \return cache entry timeout in seconds
160 */
161static inline int mbedtls_ssl_cache_get_timeout(mbedtls_ssl_cache_context *cache)
162{
163 return cache->MBEDTLS_PRIVATE(timeout);
164}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000166
167/**
Manuel Pégourié-Gonnarddb90c822015-10-20 09:36:39 +0200168 * \brief Set the maximum number of cache entries
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 * (Default: MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES (50))
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000170 *
171 * \param cache SSL cache context
172 * \param max cache entry maximum
173 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100174void mbedtls_ssl_cache_set_max_entries(mbedtls_ssl_cache_context *cache, int max);
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000175
176/**
Paul Bakker0a597072012-09-25 21:55:46 +0000177 * \brief Free referenced items in a cache context and clear memory
178 *
179 * \param cache SSL cache context
180 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100181void mbedtls_ssl_cache_free(mbedtls_ssl_cache_context *cache);
Paul Bakker0a597072012-09-25 21:55:46 +0000182
183#ifdef __cplusplus
184}
185#endif
186
187#endif /* ssl_cache.h */