blob: e987c29e11052eee6a7b5caa22e036f25b90edac [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/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Bence Szépkúti4e9f7122020-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úti4e9f7122020-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 * **********
48 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000049 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker0a597072012-09-25 21:55:46 +000050 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#ifndef MBEDTLS_SSL_CACHE_H
52#define MBEDTLS_SSL_CACHE_H
Paul Bakker0a597072012-09-25 21:55:46 +000053
Ron Eldor3625e562018-12-16 12:14:37 +020054#if !defined(MBEDTLS_CONFIG_FILE)
55#include "config.h"
56#else
57#include MBEDTLS_CONFIG_FILE
58#endif
59
Paul Bakker0a597072012-09-25 21:55:46 +000060#include "ssl.h"
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_THREADING_C)
Paul Bakkerc5598842013-09-28 15:01:27 +020063#include "threading.h"
64#endif
65
Paul Bakker088c5c52014-04-25 11:11:10 +020066/**
67 * \name SECTION: Module settings
68 *
69 * The configuration options you can set for this module are in this section.
70 * Either change them in config.h or define them on the compiler command line.
71 * \{
72 */
73
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#if !defined(MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT)
75#define MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT 86400 /*!< 1 day */
Paul Bakker088c5c52014-04-25 11:11:10 +020076#endif
77
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078#if !defined(MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES)
79#define MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES 50 /*!< Maximum entries in cache */
Paul Bakker088c5c52014-04-25 11:11:10 +020080#endif
81
82/* \} name SECTION: Module settings */
Paul Bakker0a597072012-09-25 21:55:46 +000083
84#ifdef __cplusplus
85extern "C" {
86#endif
87
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088typedef struct mbedtls_ssl_cache_context mbedtls_ssl_cache_context;
89typedef struct mbedtls_ssl_cache_entry mbedtls_ssl_cache_entry;
Paul Bakker0a597072012-09-25 21:55:46 +000090
91/**
92 * \brief This structure is used for storing cache entries
93 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094struct mbedtls_ssl_cache_entry
Paul Bakker0a597072012-09-25 21:55:46 +000095{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096#if defined(MBEDTLS_HAVE_TIME)
SimonBd5800b72016-04-26 07:43:27 +010097 mbedtls_time_t timestamp; /*!< entry timestamp */
Paul Bakkerfa9b1002013-07-03 15:31:03 +020098#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 mbedtls_ssl_session session; /*!< entry session */
100#if defined(MBEDTLS_X509_CRT_PARSE_C)
101 mbedtls_x509_buf peer_cert; /*!< entry peer_cert */
Paul Bakkered27a042013-04-18 22:46:23 +0200102#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_ssl_cache_entry *next; /*!< chain pointer */
Paul Bakker0a597072012-09-25 21:55:46 +0000104};
105
106/**
107 * \brief Cache context
108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109struct mbedtls_ssl_cache_context
Paul Bakker0a597072012-09-25 21:55:46 +0000110{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 mbedtls_ssl_cache_entry *chain; /*!< start of the chain */
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000112 int timeout; /*!< cache entry timeout */
113 int max_entries; /*!< maximum entries */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114#if defined(MBEDTLS_THREADING_C)
115 mbedtls_threading_mutex_t mutex; /*!< mutex */
Paul Bakkerc5598842013-09-28 15:01:27 +0200116#endif
Paul Bakker0a597072012-09-25 21:55:46 +0000117};
118
119/**
120 * \brief Initialize an SSL cache context
121 *
122 * \param cache SSL cache context
123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124void mbedtls_ssl_cache_init( mbedtls_ssl_cache_context *cache );
Paul Bakker0a597072012-09-25 21:55:46 +0000125
126/**
127 * \brief Cache get callback implementation
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 * (Thread-safe if MBEDTLS_THREADING_C is enabled)
Paul Bakker0a597072012-09-25 21:55:46 +0000129 *
130 * \param data SSL cache context
131 * \param session session to retrieve entry for
132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133int mbedtls_ssl_cache_get( void *data, mbedtls_ssl_session *session );
Paul Bakker0a597072012-09-25 21:55:46 +0000134
135/**
136 * \brief Cache set callback implementation
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 * (Thread-safe if MBEDTLS_THREADING_C is enabled)
Paul Bakker0a597072012-09-25 21:55:46 +0000138 *
139 * \param data SSL cache context
140 * \param session session to store entry for
141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142int mbedtls_ssl_cache_set( void *data, const mbedtls_ssl_session *session );
Paul Bakker0a597072012-09-25 21:55:46 +0000143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#if defined(MBEDTLS_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000145/**
146 * \brief Set the cache timeout
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 * (Default: MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT (1 day))
Paul Bakker0a597072012-09-25 21:55:46 +0000148 *
149 * A timeout of 0 indicates no timeout.
150 *
151 * \param cache SSL cache context
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100152 * \param timeout cache entry timeout in seconds
Paul Bakker0a597072012-09-25 21:55:46 +0000153 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout );
155#endif /* MBEDTLS_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000156
157/**
Manuel Pégourié-Gonnarddb90c822015-10-20 09:36:39 +0200158 * \brief Set the maximum number of cache entries
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 * (Default: MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES (50))
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000160 *
161 * \param cache SSL cache context
162 * \param max cache entry maximum
163 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164void mbedtls_ssl_cache_set_max_entries( mbedtls_ssl_cache_context *cache, int max );
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000165
166/**
Paul Bakker0a597072012-09-25 21:55:46 +0000167 * \brief Free referenced items in a cache context and clear memory
168 *
169 * \param cache SSL cache context
170 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache );
Paul Bakker0a597072012-09-25 21:55:46 +0000172
173#ifdef __cplusplus
174}
175#endif
176
177#endif /* ssl_cache.h */