blob: daa07acb633007d6dd0b6bca27ce7250e8e0d36b [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 Bakker9bcf16c2013-06-24 19:31:17 +020036#if !defined(POLARSSL_CONFIG_OPTIONS)
Paul Bakkerba26e9e2012-10-23 22:18:28 +000037#define SSL_CACHE_DEFAULT_TIMEOUT 86400 /*!< 1 day */
38#define SSL_CACHE_DEFAULT_MAX_ENTRIES 50 /*!< Maximum entries in cache */
Paul Bakker9bcf16c2013-06-24 19:31:17 +020039#endif /* !POLARSSL_CONFIG_OPTIONS */
Paul Bakker0a597072012-09-25 21:55:46 +000040
41#ifdef __cplusplus
42extern "C" {
43#endif
44
45typedef struct _ssl_cache_context ssl_cache_context;
46typedef struct _ssl_cache_entry ssl_cache_entry;
47
48/**
49 * \brief This structure is used for storing cache entries
50 */
51struct _ssl_cache_entry
52{
Paul Bakkerfa9b1002013-07-03 15:31:03 +020053#if defined(POLARSSL_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +000054 time_t timestamp; /*!< entry timestamp */
Paul Bakkerfa9b1002013-07-03 15:31:03 +020055#endif
Paul Bakker0a597072012-09-25 21:55:46 +000056 ssl_session session; /*!< entry session */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020057#if defined(POLARSSL_X509_CRT_PARSE_C)
Paul Bakkere81beda2013-03-06 17:40:46 +010058 x509_buf peer_cert; /*!< entry peer_cert */
Paul Bakkered27a042013-04-18 22:46:23 +020059#endif
Paul Bakker0a597072012-09-25 21:55:46 +000060 ssl_cache_entry *next; /*!< chain pointer */
61};
62
63/**
64 * \brief Cache context
65 */
66struct _ssl_cache_context
67{
Paul Bakkerba26e9e2012-10-23 22:18:28 +000068 ssl_cache_entry *chain; /*!< start of the chain */
69 int timeout; /*!< cache entry timeout */
70 int max_entries; /*!< maximum entries */
Paul Bakkerc5598842013-09-28 15:01:27 +020071#if defined(POLARSSL_THREADING_C)
72 threading_mutex_t mutex; /*!< mutex */
73#endif
Paul Bakker0a597072012-09-25 21:55:46 +000074};
75
76/**
77 * \brief Initialize an SSL cache context
78 *
79 * \param cache SSL cache context
80 */
81void ssl_cache_init( ssl_cache_context *cache );
82
83/**
84 * \brief Cache get callback implementation
Paul Bakkerc5598842013-09-28 15:01:27 +020085 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakker0a597072012-09-25 21:55:46 +000086 *
87 * \param data SSL cache context
88 * \param session session to retrieve entry for
89 */
90int ssl_cache_get( void *data, ssl_session *session );
91
92/**
93 * \brief Cache set callback implementation
Paul Bakkerc5598842013-09-28 15:01:27 +020094 * (Thread-safe if POLARSSL_THREADING_C is enabled)
Paul Bakker0a597072012-09-25 21:55:46 +000095 *
96 * \param data SSL cache context
97 * \param session session to store entry for
98 */
99int ssl_cache_set( void *data, const ssl_session *session );
100
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200101#if defined(POLARSSL_HAVE_TIME)
Paul Bakker0a597072012-09-25 21:55:46 +0000102/**
103 * \brief Set the cache timeout
104 * (Default: SSL_CACHE_DEFAULT_TIMEOUT (1 day))
105 *
106 * A timeout of 0 indicates no timeout.
107 *
108 * \param cache SSL cache context
109 * \param timeout cache entry timeout
110 */
111void ssl_cache_set_timeout( ssl_cache_context *cache, int timeout );
Paul Bakkerfa9b1002013-07-03 15:31:03 +0200112#endif /* POLARSSL_HAVE_TIME */
Paul Bakker0a597072012-09-25 21:55:46 +0000113
114/**
Paul Bakkerba26e9e2012-10-23 22:18:28 +0000115 * \brief Set the cache timeout
116 * (Default: SSL_CACHE_DEFAULT_MAX_ENTRIES (50))
117 *
118 * \param cache SSL cache context
119 * \param max cache entry maximum
120 */
121void ssl_cache_set_max_entries( ssl_cache_context *cache, int max );
122
123/**
Paul Bakker0a597072012-09-25 21:55:46 +0000124 * \brief Free referenced items in a cache context and clear memory
125 *
126 * \param cache SSL cache context
127 */
128void ssl_cache_free( ssl_cache_context *cache );
129
130#ifdef __cplusplus
131}
132#endif
133
134#endif /* ssl_cache.h */