Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file ssl_cache.h |
| 3 | * |
| 4 | * \brief SSL session cache 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 | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame] | 8 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 9 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10 | #ifndef MBEDTLS_SSL_CACHE_H |
| 11 | #define MBEDTLS_SSL_CACHE_H |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 12 | |
Ron Eldor | 9cbd1b2 | 2018-12-16 12:14:37 +0200 | [diff] [blame] | 13 | #if !defined(MBEDTLS_CONFIG_FILE) |
Jaeden Amero | 6609aef | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 14 | #include "mbedtls/config.h" |
Ron Eldor | 9cbd1b2 | 2018-12-16 12:14:37 +0200 | [diff] [blame] | 15 | #else |
| 16 | #include MBEDTLS_CONFIG_FILE |
| 17 | #endif |
| 18 | |
Jaeden Amero | 6609aef | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 19 | #include "mbedtls/ssl.h" |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 20 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 21 | #if defined(MBEDTLS_THREADING_C) |
Jaeden Amero | 6609aef | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 22 | #include "mbedtls/threading.h" |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 23 | #endif |
| 24 | |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 25 | /** |
| 26 | * \name SECTION: Module settings |
| 27 | * |
| 28 | * The configuration options you can set for this module are in this section. |
| 29 | * Either change them in config.h or define them on the compiler command line. |
| 30 | * \{ |
| 31 | */ |
| 32 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | #if !defined(MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT) |
| 34 | #define MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT 86400 /*!< 1 day */ |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 35 | #endif |
| 36 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 37 | #if !defined(MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES) |
| 38 | #define MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES 50 /*!< Maximum entries in cache */ |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame] | 39 | #endif |
| 40 | |
Andrzej Kurek | 73afe27 | 2022-01-24 10:31:06 -0500 | [diff] [blame] | 41 | /** \} name SECTION: Module settings */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 42 | |
| 43 | #ifdef __cplusplus |
| 44 | extern "C" { |
| 45 | #endif |
| 46 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 47 | typedef struct mbedtls_ssl_cache_context mbedtls_ssl_cache_context; |
| 48 | typedef struct mbedtls_ssl_cache_entry mbedtls_ssl_cache_entry; |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * \brief This structure is used for storing cache entries |
| 52 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 53 | struct mbedtls_ssl_cache_entry { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 54 | #if defined(MBEDTLS_HAVE_TIME) |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 55 | mbedtls_time_t timestamp; /*!< entry timestamp */ |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 56 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 57 | mbedtls_ssl_session session; /*!< entry session */ |
Hanno Becker | a887d1a | 2019-02-06 15:57:49 +0000 | [diff] [blame] | 58 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 59 | defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 60 | mbedtls_x509_buf peer_cert; /*!< entry peer_cert */ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 61 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 62 | mbedtls_ssl_cache_entry *next; /*!< chain pointer */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | /** |
| 66 | * \brief Cache context |
| 67 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 68 | struct mbedtls_ssl_cache_context { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 69 | mbedtls_ssl_cache_entry *chain; /*!< start of the chain */ |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 70 | int timeout; /*!< cache entry timeout */ |
| 71 | int max_entries; /*!< maximum entries */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 72 | #if defined(MBEDTLS_THREADING_C) |
| 73 | mbedtls_threading_mutex_t mutex; /*!< mutex */ |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 74 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | /** |
| 78 | * \brief Initialize an SSL cache context |
| 79 | * |
| 80 | * \param cache SSL cache context |
| 81 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 82 | void mbedtls_ssl_cache_init(mbedtls_ssl_cache_context *cache); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * \brief Cache get callback implementation |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 86 | * (Thread-safe if MBEDTLS_THREADING_C is enabled) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 87 | * |
| 88 | * \param data SSL cache context |
| 89 | * \param session session to retrieve entry for |
Gilles Peskine | fe4d93a | 2023-09-29 13:40:33 +0200 | [diff] [blame] | 90 | * |
| 91 | * \return \c 0 on success. |
| 92 | * \return #MBEDTLS_ERR_SSL_CACHE_ENTRY_NOT_FOUND if there is |
| 93 | * no cache entry with specified session ID found, or |
| 94 | * any other negative error code for other failures. |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 95 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 96 | int mbedtls_ssl_cache_get(void *data, mbedtls_ssl_session *session); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * \brief Cache set callback implementation |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 100 | * (Thread-safe if MBEDTLS_THREADING_C is enabled) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 101 | * |
| 102 | * \param data SSL cache context |
| 103 | * \param session session to store entry for |
Gilles Peskine | fe4d93a | 2023-09-29 13:40:33 +0200 | [diff] [blame] | 104 | * |
| 105 | * \return \c 0 on success. |
| 106 | * \return A negative error code on failure. |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 107 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 108 | int mbedtls_ssl_cache_set(void *data, const mbedtls_ssl_session *session); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 109 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 110 | #if defined(MBEDTLS_HAVE_TIME) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 111 | /** |
| 112 | * \brief Set the cache timeout |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 113 | * (Default: MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT (1 day)) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 114 | * |
| 115 | * A timeout of 0 indicates no timeout. |
| 116 | * |
| 117 | * \param cache SSL cache context |
Manuel Pégourié-Gonnard | 274a12e | 2014-02-20 21:32:08 +0100 | [diff] [blame] | 118 | * \param timeout cache entry timeout in seconds |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 119 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 120 | void mbedtls_ssl_cache_set_timeout(mbedtls_ssl_cache_context *cache, int timeout); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 121 | #endif /* MBEDTLS_HAVE_TIME */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 122 | |
| 123 | /** |
Manuel Pégourié-Gonnard | db90c82 | 2015-10-20 09:36:39 +0200 | [diff] [blame] | 124 | * \brief Set the maximum number of cache entries |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 125 | * (Default: MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES (50)) |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 126 | * |
| 127 | * \param cache SSL cache context |
| 128 | * \param max cache entry maximum |
| 129 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 130 | void mbedtls_ssl_cache_set_max_entries(mbedtls_ssl_cache_context *cache, int max); |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 131 | |
| 132 | /** |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 133 | * \brief Free referenced items in a cache context and clear memory |
| 134 | * |
| 135 | * \param cache SSL cache context |
| 136 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 137 | void mbedtls_ssl_cache_free(mbedtls_ssl_cache_context *cache); |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 138 | |
| 139 | #ifdef __cplusplus |
| 140 | } |
| 141 | #endif |
| 142 | |
| 143 | #endif /* ssl_cache.h */ |