Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file ssl_cache.h |
| 3 | * |
| 4 | * \brief SSL session cache implementation |
| 5 | * |
Paul Bakker | 9bcf16c | 2013-06-24 19:31:17 +0200 | [diff] [blame] | 6 | * Copyright (C) 2006-2013, Brainspark B.V. |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 7 | * |
| 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 Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 32 | #if defined(POLARSSL_THREADING_C) |
| 33 | #include "threading.h" |
| 34 | #endif |
| 35 | |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame^] | 36 | /** |
| 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 Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 45 | #define SSL_CACHE_DEFAULT_TIMEOUT 86400 /*!< 1 day */ |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame^] | 46 | #endif |
| 47 | |
| 48 | #if !defined(SSL_CACHE_DEFAULT_MAX_ENTRIES) |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 49 | #define SSL_CACHE_DEFAULT_MAX_ENTRIES 50 /*!< Maximum entries in cache */ |
Paul Bakker | 088c5c5 | 2014-04-25 11:11:10 +0200 | [diff] [blame^] | 50 | #endif |
| 51 | |
| 52 | /* \} name SECTION: Module settings */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 53 | |
| 54 | #ifdef __cplusplus |
| 55 | extern "C" { |
| 56 | #endif |
| 57 | |
| 58 | typedef struct _ssl_cache_context ssl_cache_context; |
| 59 | typedef struct _ssl_cache_entry ssl_cache_entry; |
| 60 | |
| 61 | /** |
| 62 | * \brief This structure is used for storing cache entries |
| 63 | */ |
| 64 | struct _ssl_cache_entry |
| 65 | { |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 66 | #if defined(POLARSSL_HAVE_TIME) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 67 | time_t timestamp; /*!< entry timestamp */ |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 68 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 69 | ssl_session session; /*!< entry session */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 70 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 71 | x509_buf peer_cert; /*!< entry peer_cert */ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 72 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 73 | ssl_cache_entry *next; /*!< chain pointer */ |
| 74 | }; |
| 75 | |
| 76 | /** |
| 77 | * \brief Cache context |
| 78 | */ |
| 79 | struct _ssl_cache_context |
| 80 | { |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 81 | ssl_cache_entry *chain; /*!< start of the chain */ |
| 82 | int timeout; /*!< cache entry timeout */ |
| 83 | int max_entries; /*!< maximum entries */ |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 84 | #if defined(POLARSSL_THREADING_C) |
| 85 | threading_mutex_t mutex; /*!< mutex */ |
| 86 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | /** |
| 90 | * \brief Initialize an SSL cache context |
| 91 | * |
| 92 | * \param cache SSL cache context |
| 93 | */ |
| 94 | void ssl_cache_init( ssl_cache_context *cache ); |
| 95 | |
| 96 | /** |
| 97 | * \brief Cache get callback implementation |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 98 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 99 | * |
| 100 | * \param data SSL cache context |
| 101 | * \param session session to retrieve entry for |
| 102 | */ |
| 103 | int ssl_cache_get( void *data, ssl_session *session ); |
| 104 | |
| 105 | /** |
| 106 | * \brief Cache set callback implementation |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 107 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 108 | * |
| 109 | * \param data SSL cache context |
| 110 | * \param session session to store entry for |
| 111 | */ |
| 112 | int ssl_cache_set( void *data, const ssl_session *session ); |
| 113 | |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 114 | #if defined(POLARSSL_HAVE_TIME) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 115 | /** |
| 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é-Gonnard | 274a12e | 2014-02-20 21:32:08 +0100 | [diff] [blame] | 122 | * \param timeout cache entry timeout in seconds |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 123 | */ |
| 124 | void ssl_cache_set_timeout( ssl_cache_context *cache, int timeout ); |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 125 | #endif /* POLARSSL_HAVE_TIME */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 126 | |
| 127 | /** |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 128 | * \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 | */ |
| 134 | void ssl_cache_set_max_entries( ssl_cache_context *cache, int max ); |
| 135 | |
| 136 | /** |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 137 | * \brief Free referenced items in a cache context and clear memory |
| 138 | * |
| 139 | * \param cache SSL cache context |
| 140 | */ |
| 141 | void ssl_cache_free( ssl_cache_context *cache ); |
| 142 | |
| 143 | #ifdef __cplusplus |
| 144 | } |
| 145 | #endif |
| 146 | |
| 147 | #endif /* ssl_cache.h */ |