blob: 918fb608eb76924c0d3bfb49cc1cd35c75ad4c83 [file] [log] [blame]
Paul Bakker0a597072012-09-25 21:55:46 +00001/**
2 * \file ssl_cache.h
3 *
4 * \brief SSL session cache implementation
5 *
Paul Bakker9bcf16c2013-06-24 19:31:17 +02006 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakker0a597072012-09-25 21:55:46 +00007 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
10 *
11 * All rights reserved.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 */
27#ifndef POLARSSL_SSL_CACHE_H
28#define POLARSSL_SSL_CACHE_H
29
30#include "ssl.h"
31
Paul Bakkerc5598842013-09-28 15:01:27 +020032#if defined(POLARSSL_THREADING_C)
33#include "threading.h"
34#endif
35
Paul Bakker088c5c52014-04-25 11:11:10 +020036/**
37 * \name SECTION: Module settings
38 *
39 * The configuration options you can set for this module are in this section.
40 * Either change them in config.h or define them on the compiler command line.
41 * \{
42 */
43
44#if !defined(SSL_CACHE_DEFAULT_TIMEOUT)
Paul Bakkerba26e9e2012-10-23 22:18:28 +000045#define SSL_CACHE_DEFAULT_TIMEOUT 86400 /*!< 1 day */
Paul Bakker088c5c52014-04-25 11:11:10 +020046#endif
47
48#if !defined(SSL_CACHE_DEFAULT_MAX_ENTRIES)
Paul Bakkerba26e9e2012-10-23 22:18:28 +000049#define SSL_CACHE_DEFAULT_MAX_ENTRIES 50 /*!< Maximum entries in cache */
Paul Bakker088c5c52014-04-25 11:11:10 +020050#endif
51
52/* \} name SECTION: Module settings */
Paul Bakker0a597072012-09-25 21:55:46 +000053
54#ifdef __cplusplus
55extern "C" {
56#endif
57
58typedef struct _ssl_cache_context ssl_cache_context;
59typedef struct _ssl_cache_entry ssl_cache_entry;
60
61/**
62 * \brief This structure is used for storing cache entries
63 */
64struct _ssl_cache_entry
65{
Paul Bakkerfa9b1002013-07-03 15:31:03 +020066#if defined(POLARSSL_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +000067 time_t timestamp; /*!< entry timestamp */
Paul Bakkerfa9b1002013-07-03 15:31:03 +020068#endif
Paul Bakker0a597072012-09-25 21:55:46 +000069 ssl_session session; /*!< entry session */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkere81beda2013-03-06 17:40:46 +010071 x509_buf peer_cert; /*!< entry peer_cert */
Paul Bakkered27a042013-04-18 22:46:23 +020072#endif
Paul Bakker0a597072012-09-25 21:55:46 +000073 ssl_cache_entry *next; /*!< chain pointer */
74};
75
76/**
77 * \brief Cache context
78 */
79struct _ssl_cache_context
80{
Paul Bakkerba26e9e2012-10-23 22:18:28 +000081 ssl_cache_entry *chain; /*!< start of the chain */
82 int timeout; /*!< cache entry timeout */
83 int max_entries; /*!< maximum entries */
Paul Bakkerc5598842013-09-28 15:01:27 +020084#if defined(POLARSSL_THREADING_C)
85 threading_mutex_t mutex; /*!< mutex */
86#endif
Paul Bakker0a597072012-09-25 21:55:46 +000087};
88
89/**
90 * \brief Initialize an SSL cache context
91 *
92 * \param cache SSL cache context
93 */
94void ssl_cache_init( ssl_cache_context *cache );
95
96/**
97 * \brief Cache get callback implementation
Paul Bakkerc5598842013-09-28 15:01:27 +020098 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakker0a597072012-09-25 21:55:46 +000099 *
100 * \param data SSL cache context
101 * \param session session to retrieve entry for
102 */
103int ssl_cache_get( void *data, ssl_session *session );
104
105/**
106 * \brief Cache set callback implementation
Paul Bakkerc5598842013-09-28 15:01:27 +0200107 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakker0a597072012-09-25 21:55:46 +0000108 *
109 * \param data SSL cache context
110 * \param session session to store entry for
111 */
112int ssl_cache_set( void *data, const ssl_session *session );
113
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200114#if defined(POLARSSL_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000115/**
116 * \brief Set the cache timeout
117 * (Default: SSL_CACHE_DEFAULT_TIMEOUT (1 day))
118 *
119 * A timeout of 0 indicates no timeout.
120 *
121 * \param cache SSL cache context
Manuel Pégourié-Gonnard274a12e2014-02-20 21:32:08 +0100122 * \param timeout cache entry timeout in seconds
Paul Bakker0a597072012-09-25 21:55:46 +0000123 */
124void ssl_cache_set_timeout( ssl_cache_context *cache, int timeout );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200125#endif /* POLARSSL_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000126
127/**
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000128 * \brief Set the cache timeout
129 * (Default: SSL_CACHE_DEFAULT_MAX_ENTRIES (50))
130 *
131 * \param cache SSL cache context
132 * \param max cache entry maximum
133 */
134void ssl_cache_set_max_entries( ssl_cache_context *cache, int max );
135
136/**
Paul Bakker0a597072012-09-25 21:55:46 +0000137 * \brief Free referenced items in a cache context and clear memory
138 *
139 * \param cache SSL cache context
140 */
141void ssl_cache_free( ssl_cache_context *cache );
142
143#ifdef __cplusplus
144}
145#endif
146
147#endif /* ssl_cache.h */