1 /**
2  * \file ssl_cache.h
3  *
4  * \brief SSL session cache implementation
5  *
6  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
7  *  SPDX-License-Identifier: Apache-2.0
8  *
9  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
10  *  not use this file except in compliance with the License.
11  *  You may obtain a copy of the License at
12  *
13  *  http://www.apache.org/licenses/LICENSE-2.0
14  *
15  *  Unless required by applicable law or agreed to in writing, software
16  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  *  See the License for the specific language governing permissions and
19  *  limitations under the License.
20  *
21  *  This file is part of mbed TLS (https://tls.mbed.org)
22  */
23 #ifndef MBEDTLS_SSL_CACHE_H
24 #define MBEDTLS_SSL_CACHE_H
25 
26 #include "ssl.h"
27 
28 #if defined(MBEDTLS_THREADING_C)
29 #include "threading.h"
30 #endif
31 
32 /**
33  * \name SECTION: Module settings
34  *
35  * The configuration options you can set for this module are in this section.
36  * Either change them in config.h or define them on the compiler command line.
37  * \{
38  */
39 
40 #if !defined(MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT)
41 #define MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT       86400   /*!< 1 day  */
42 #endif
43 
44 #if !defined(MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES)
45 #define MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES      50   /*!< Maximum entries in cache */
46 #endif
47 
48 /* \} name SECTION: Module settings */
49 
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 
54 typedef struct mbedtls_ssl_cache_context mbedtls_ssl_cache_context;
55 typedef struct mbedtls_ssl_cache_entry mbedtls_ssl_cache_entry;
56 
57 /**
58  * \brief   This structure is used for storing cache entries
59  */
60 struct mbedtls_ssl_cache_entry
61 {
62 #if defined(MBEDTLS_HAVE_TIME)
63     mbedtls_time_t timestamp;           /*!< entry timestamp    */
64 #endif
65     mbedtls_ssl_session session;        /*!< entry session      */
66 #if defined(MBEDTLS_X509_CRT_PARSE_C)
67     mbedtls_x509_buf peer_cert;         /*!< entry peer_cert    */
68 #endif
69     mbedtls_ssl_cache_entry *next;      /*!< chain pointer      */
70 };
71 
72 /**
73  * \brief Cache context
74  */
75 struct mbedtls_ssl_cache_context
76 {
77     mbedtls_ssl_cache_entry *chain;     /*!< start of the chain     */
78     int timeout;                /*!< cache entry timeout    */
79     int max_entries;            /*!< maximum entries        */
80 #if defined(MBEDTLS_THREADING_C)
81     mbedtls_threading_mutex_t mutex;    /*!< mutex                  */
82 #endif
83 };
84 
85 /**
86  * \brief          Initialize an SSL cache context
87  *
88  * \param cache    SSL cache context
89  */
90 void mbedtls_ssl_cache_init( mbedtls_ssl_cache_context *cache );
91 
92 /**
93  * \brief          Cache get callback implementation
94  *                 (Thread-safe if MBEDTLS_THREADING_C is enabled)
95  *
96  * \param data     SSL cache context
97  * \param session  session to retrieve entry for
98  */
99 int mbedtls_ssl_cache_get( void *data, mbedtls_ssl_session *session );
100 
101 /**
102  * \brief          Cache set callback implementation
103  *                 (Thread-safe if MBEDTLS_THREADING_C is enabled)
104  *
105  * \param data     SSL cache context
106  * \param session  session to store entry for
107  */
108 int mbedtls_ssl_cache_set( void *data, const mbedtls_ssl_session *session );
109 
110 #if defined(MBEDTLS_HAVE_TIME)
111 /**
112  * \brief          Set the cache timeout
113  *                 (Default: MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT (1 day))
114  *
115  *                 A timeout of 0 indicates no timeout.
116  *
117  * \param cache    SSL cache context
118  * \param timeout  cache entry timeout in seconds
119  */
120 void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout );
121 #endif /* MBEDTLS_HAVE_TIME */
122 
123 /**
124  * \brief          Set the maximum number of cache entries
125  *                 (Default: MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES (50))
126  *
127  * \param cache    SSL cache context
128  * \param max      cache entry maximum
129  */
130 void mbedtls_ssl_cache_set_max_entries( mbedtls_ssl_cache_context *cache, int max );
131 
132 /**
133  * \brief          Free referenced items in a cache context and clear memory
134  *
135  * \param cache    SSL cache context
136  */
137 void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache );
138 
139 #ifdef __cplusplus
140 }
141 #endif
142 
143 #endif /* ssl_cache.h */
144