1 /**
2  * \file ssl_cache.h
3  *
4  * \brief SSL session cache implementation
5  */
6 /*
7  *  Copyright The Mbed TLS Contributors
8  *  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:
15  *
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.
27  *
28  *  **********
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  */
49 #ifndef MBEDTLS_SSL_CACHE_H
50 #define MBEDTLS_SSL_CACHE_H
51 
52 #if !defined(MBEDTLS_CONFIG_FILE)
53 #include "config.h"
54 #else
55 #include MBEDTLS_CONFIG_FILE
56 #endif
57 
58 #include "ssl.h"
59 
60 #if defined(MBEDTLS_THREADING_C)
61 #include "threading.h"
62 #endif
63 
64 /**
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 
72 #if !defined(MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT)
73 #define MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT       86400   /*!< 1 day  */
74 #endif
75 
76 #if !defined(MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES)
77 #define MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES      50   /*!< Maximum entries in cache */
78 #endif
79 
80 /* \} name SECTION: Module settings */
81 
82 #ifdef __cplusplus
83 extern "C" {
84 #endif
85 
86 typedef struct mbedtls_ssl_cache_context mbedtls_ssl_cache_context;
87 typedef struct mbedtls_ssl_cache_entry mbedtls_ssl_cache_entry;
88 
89 /**
90  * \brief   This structure is used for storing cache entries
91  */
92 struct mbedtls_ssl_cache_entry
93 {
94 #if defined(MBEDTLS_HAVE_TIME)
95     mbedtls_time_t timestamp;           /*!< entry timestamp    */
96 #endif
97     mbedtls_ssl_session session;        /*!< entry session      */
98 #if defined(MBEDTLS_X509_CRT_PARSE_C)
99     mbedtls_x509_buf peer_cert;         /*!< entry peer_cert    */
100 #endif
101     mbedtls_ssl_cache_entry *next;      /*!< chain pointer      */
102 };
103 
104 /**
105  * \brief Cache context
106  */
107 struct mbedtls_ssl_cache_context
108 {
109     mbedtls_ssl_cache_entry *chain;     /*!< start of the chain     */
110     int timeout;                /*!< cache entry timeout    */
111     int max_entries;            /*!< maximum entries        */
112 #if defined(MBEDTLS_THREADING_C)
113     mbedtls_threading_mutex_t mutex;    /*!< mutex                  */
114 #endif
115 };
116 
117 /**
118  * \brief          Initialize an SSL cache context
119  *
120  * \param cache    SSL cache context
121  */
122 void mbedtls_ssl_cache_init( mbedtls_ssl_cache_context *cache );
123 
124 /**
125  * \brief          Cache get callback implementation
126  *                 (Thread-safe if MBEDTLS_THREADING_C is enabled)
127  *
128  * \param data     SSL cache context
129  * \param session  session to retrieve entry for
130  */
131 int mbedtls_ssl_cache_get( void *data, mbedtls_ssl_session *session );
132 
133 /**
134  * \brief          Cache set callback implementation
135  *                 (Thread-safe if MBEDTLS_THREADING_C is enabled)
136  *
137  * \param data     SSL cache context
138  * \param session  session to store entry for
139  */
140 int mbedtls_ssl_cache_set( void *data, const mbedtls_ssl_session *session );
141 
142 #if defined(MBEDTLS_HAVE_TIME)
143 /**
144  * \brief          Set the cache timeout
145  *                 (Default: MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT (1 day))
146  *
147  *                 A timeout of 0 indicates no timeout.
148  *
149  * \param cache    SSL cache context
150  * \param timeout  cache entry timeout in seconds
151  */
152 void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout );
153 #endif /* MBEDTLS_HAVE_TIME */
154 
155 /**
156  * \brief          Set the maximum number of cache entries
157  *                 (Default: MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES (50))
158  *
159  * \param cache    SSL cache context
160  * \param max      cache entry maximum
161  */
162 void mbedtls_ssl_cache_set_max_entries( mbedtls_ssl_cache_context *cache, int max );
163 
164 /**
165  * \brief          Free referenced items in a cache context and clear memory
166  *
167  * \param cache    SSL cache context
168  */
169 void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache );
170 
171 #ifdef __cplusplus
172 }
173 #endif
174 
175 #endif /* ssl_cache.h */
176