blob: cadb30c18a06fee81030602777e12578de6b9b46 [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 Rodgman7ff79652023-11-03 12:04:52 +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
Paul Bakker0a597072012-09-25 21:55:46 +000012
Ron Eldor9cbd1b22018-12-16 12:14:37 +020013#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Amero6609aef2019-07-04 20:01:14 +010014#include "mbedtls/config.h"
Ron Eldor9cbd1b22018-12-16 12:14:37 +020015#else
16#include MBEDTLS_CONFIG_FILE
17#endif
18
Jaeden Amero6609aef2019-07-04 20:01:14 +010019#include "mbedtls/ssl.h"
Paul Bakker0a597072012-09-25 21:55:46 +000020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020021#if defined(MBEDTLS_THREADING_C)
Jaeden Amero6609aef2019-07-04 20:01:14 +010022#include "mbedtls/threading.h"
Paul Bakkerc5598842013-09-28 15:01:27 +020023#endif
24
Paul Bakker088c5c52014-04-25 11:11:10 +020025/**
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if !defined(MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT)
34#define MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT 86400 /*!< 1 day */
Paul Bakker088c5c52014-04-25 11:11:10 +020035#endif
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if !defined(MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES)
38#define MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES 50 /*!< Maximum entries in cache */
Paul Bakker088c5c52014-04-25 11:11:10 +020039#endif
40
Andrzej Kurek73afe272022-01-24 10:31:06 -050041/** \} name SECTION: Module settings */
Paul Bakker0a597072012-09-25 21:55:46 +000042
43#ifdef __cplusplus
44extern "C" {
45#endif
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047typedef struct mbedtls_ssl_cache_context mbedtls_ssl_cache_context;
48typedef struct mbedtls_ssl_cache_entry mbedtls_ssl_cache_entry;
Paul Bakker0a597072012-09-25 21:55:46 +000049
50/**
51 * \brief This structure is used for storing cache entries
52 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010053struct mbedtls_ssl_cache_entry {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +010055 mbedtls_time_t timestamp; /*!< entry timestamp */
Paul Bakkerfa9b1002013-07-03 15:31:03 +020056#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057 mbedtls_ssl_session session; /*!< entry session */
Hanno Beckera887d1a2019-02-06 15:57:49 +000058#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
59 defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 mbedtls_x509_buf peer_cert; /*!< entry peer_cert */
Paul Bakkered27a042013-04-18 22:46:23 +020061#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 mbedtls_ssl_cache_entry *next; /*!< chain pointer */
Paul Bakker0a597072012-09-25 21:55:46 +000063};
64
65/**
66 * \brief Cache context
67 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010068struct mbedtls_ssl_cache_context {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069 mbedtls_ssl_cache_entry *chain; /*!< start of the chain */
Paul Bakkerba26e9e2012-10-23 22:18:28 +000070 int timeout; /*!< cache entry timeout */
71 int max_entries; /*!< maximum entries */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_THREADING_C)
73 mbedtls_threading_mutex_t mutex; /*!< mutex */
Paul Bakkerc5598842013-09-28 15:01:27 +020074#endif
Paul Bakker0a597072012-09-25 21:55:46 +000075};
76
77/**
78 * \brief Initialize an SSL cache context
79 *
80 * \param cache SSL cache context
81 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082void mbedtls_ssl_cache_init(mbedtls_ssl_cache_context *cache);
Paul Bakker0a597072012-09-25 21:55:46 +000083
84/**
85 * \brief Cache get callback implementation
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 * (Thread-safe if MBEDTLS_THREADING_C is enabled)
Paul Bakker0a597072012-09-25 21:55:46 +000087 *
88 * \param data SSL cache context
89 * \param session session to retrieve entry for
Gilles Peskinefe4d93a2023-09-29 13:40:33 +020090 *
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 Bakker0a597072012-09-25 21:55:46 +000095 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010096int mbedtls_ssl_cache_get(void *data, mbedtls_ssl_session *session);
Paul Bakker0a597072012-09-25 21:55:46 +000097
98/**
99 * \brief Cache set callback implementation
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 * (Thread-safe if MBEDTLS_THREADING_C is enabled)
Paul Bakker0a597072012-09-25 21:55:46 +0000101 *
102 * \param data SSL cache context
103 * \param session session to store entry for
Gilles Peskinefe4d93a2023-09-29 13:40:33 +0200104 *
105 * \return \c 0 on success.
106 * \return A negative error code on failure.
Paul Bakker0a597072012-09-25 21:55:46 +0000107 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100108int mbedtls_ssl_cache_set(void *data, const mbedtls_ssl_session *session);
Paul Bakker0a597072012-09-25 21:55:46 +0000109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000111/**
112 * \brief Set the cache timeout
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 * (Default: MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT (1 day))
Paul Bakker0a597072012-09-25 21:55:46 +0000114 *
115 * A timeout of 0 indicates no timeout.
116 *
117 * \param cache SSL cache context
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100118 * \param timeout cache entry timeout in seconds
Paul Bakker0a597072012-09-25 21:55:46 +0000119 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100120void mbedtls_ssl_cache_set_timeout(mbedtls_ssl_cache_context *cache, int timeout);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000122
123/**
Manuel Pégourié-Gonnarddb90c822015-10-20 09:36:39 +0200124 * \brief Set the maximum number of cache entries
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 * (Default: MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES (50))
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000126 *
127 * \param cache SSL cache context
128 * \param max cache entry maximum
129 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100130void mbedtls_ssl_cache_set_max_entries(mbedtls_ssl_cache_context *cache, int max);
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000131
132/**
Paul Bakker0a597072012-09-25 21:55:46 +0000133 * \brief Free referenced items in a cache context and clear memory
134 *
135 * \param cache SSL cache context
136 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100137void mbedtls_ssl_cache_free(mbedtls_ssl_cache_context *cache);
Paul Bakker0a597072012-09-25 21:55:46 +0000138
139#ifdef __cplusplus
140}
141#endif
142
143#endif /* ssl_cache.h */