blob: 612d81776ef2f1c556e6c0465949a4740759c71e [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útia2947ac2020-08-19 16:37:36 +02007 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9 *
10 * This file is provided under the Apache License 2.0, or the
11 * GNU General Public License v2.0 or later.
12 *
13 * **********
14 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020015 *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
Paul Bakker0a597072012-09-25 21:55:46 +000027 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020028 * **********
29 *
30 * **********
31 * GNU General Public License v2.0 or later:
32 *
33 * This program is free software; you can redistribute it and/or modify
34 * it under the terms of the GNU General Public License as published by
35 * the Free Software Foundation; either version 2 of the License, or
36 * (at your option) any later version.
37 *
38 * This program is distributed in the hope that it will be useful,
39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 * GNU General Public License for more details.
42 *
43 * You should have received a copy of the GNU General Public License along
44 * with this program; if not, write to the Free Software Foundation, Inc.,
45 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
46 *
47 * **********
Paul Bakker0a597072012-09-25 21:55:46 +000048 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#ifndef MBEDTLS_SSL_CACHE_H
50#define MBEDTLS_SSL_CACHE_H
Paul Bakker0a597072012-09-25 21:55:46 +000051
Ron Eldor9cbd1b22018-12-16 12:14:37 +020052#if !defined(MBEDTLS_CONFIG_FILE)
53#include "config.h"
54#else
55#include MBEDTLS_CONFIG_FILE
56#endif
57
Paul Bakker0a597072012-09-25 21:55:46 +000058#include "ssl.h"
59
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_THREADING_C)
Paul Bakkerc5598842013-09-28 15:01:27 +020061#include "threading.h"
62#endif
63
Paul Bakker088c5c52014-04-25 11:11:10 +020064/**
65 * \name SECTION: Module settings
66 *
67 * The configuration options you can set for this module are in this section.
68 * Either change them in config.h or define them on the compiler command line.
69 * \{
70 */
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if !defined(MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT)
73#define MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT 86400 /*!< 1 day */
Paul Bakker088c5c52014-04-25 11:11:10 +020074#endif
75
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#if !defined(MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES)
77#define MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES 50 /*!< Maximum entries in cache */
Paul Bakker088c5c52014-04-25 11:11:10 +020078#endif
79
80/* \} name SECTION: Module settings */
Paul Bakker0a597072012-09-25 21:55:46 +000081
82#ifdef __cplusplus
83extern "C" {
84#endif
85
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086typedef struct mbedtls_ssl_cache_context mbedtls_ssl_cache_context;
87typedef struct mbedtls_ssl_cache_entry mbedtls_ssl_cache_entry;
Paul Bakker0a597072012-09-25 21:55:46 +000088
89/**
90 * \brief This structure is used for storing cache entries
91 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092struct mbedtls_ssl_cache_entry
Paul Bakker0a597072012-09-25 21:55:46 +000093{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +010095 mbedtls_time_t timestamp; /*!< entry timestamp */
Paul Bakkerfa9b1002013-07-03 15:31:03 +020096#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 mbedtls_ssl_session session; /*!< entry session */
98#if defined(MBEDTLS_X509_CRT_PARSE_C)
99 mbedtls_x509_buf peer_cert; /*!< entry peer_cert */
Paul Bakkered27a042013-04-18 22:46:23 +0200100#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_ssl_cache_entry *next; /*!< chain pointer */
Paul Bakker0a597072012-09-25 21:55:46 +0000102};
103
104/**
105 * \brief Cache context
106 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107struct mbedtls_ssl_cache_context
Paul Bakker0a597072012-09-25 21:55:46 +0000108{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 mbedtls_ssl_cache_entry *chain; /*!< start of the chain */
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000110 int timeout; /*!< cache entry timeout */
111 int max_entries; /*!< maximum entries */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112#if defined(MBEDTLS_THREADING_C)
113 mbedtls_threading_mutex_t mutex; /*!< mutex */
Paul Bakkerc5598842013-09-28 15:01:27 +0200114#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000115};
116
117/**
118 * \brief Initialize an SSL cache context
119 *
120 * \param cache SSL cache context
121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122void mbedtls_ssl_cache_init( mbedtls_ssl_cache_context *cache );
Paul Bakker0a597072012-09-25 21:55:46 +0000123
124/**
125 * \brief Cache get callback implementation
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 * (Thread-safe if MBEDTLS_THREADING_C is enabled)
Paul Bakker0a597072012-09-25 21:55:46 +0000127 *
128 * \param data SSL cache context
129 * \param session session to retrieve entry for
130 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131int mbedtls_ssl_cache_get( void *data, mbedtls_ssl_session *session );
Paul Bakker0a597072012-09-25 21:55:46 +0000132
133/**
134 * \brief Cache set callback implementation
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 * (Thread-safe if MBEDTLS_THREADING_C is enabled)
Paul Bakker0a597072012-09-25 21:55:46 +0000136 *
137 * \param data SSL cache context
138 * \param session session to store entry for
139 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140int mbedtls_ssl_cache_set( void *data, const mbedtls_ssl_session *session );
Paul Bakker0a597072012-09-25 21:55:46 +0000141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000143/**
144 * \brief Set the cache timeout
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 * (Default: MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT (1 day))
Paul Bakker0a597072012-09-25 21:55:46 +0000146 *
147 * A timeout of 0 indicates no timeout.
148 *
149 * \param cache SSL cache context
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100150 * \param timeout cache entry timeout in seconds
Paul Bakker0a597072012-09-25 21:55:46 +0000151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout );
153#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000154
155/**
Manuel Pégourié-Gonnarddb90c822015-10-20 09:36:39 +0200156 * \brief Set the maximum number of cache entries
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 * (Default: MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES (50))
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000158 *
159 * \param cache SSL cache context
160 * \param max cache entry maximum
161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162void mbedtls_ssl_cache_set_max_entries( mbedtls_ssl_cache_context *cache, int max );
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000163
164/**
Paul Bakker0a597072012-09-25 21:55:46 +0000165 * \brief Free referenced items in a cache context and clear memory
166 *
167 * \param cache SSL cache context
168 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache );
Paul Bakker0a597072012-09-25 21:55:46 +0000170
171#ifdef __cplusplus
172}
173#endif
174
175#endif /* ssl_cache.h */