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 | 9bcf16c | 2013-06-24 19:31:17 +0200 | [diff] [blame] | 36 | #if !defined(POLARSSL_CONFIG_OPTIONS) |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 37 | #define SSL_CACHE_DEFAULT_TIMEOUT 86400 /*!< 1 day */ |
| 38 | #define SSL_CACHE_DEFAULT_MAX_ENTRIES 50 /*!< Maximum entries in cache */ |
Paul Bakker | 9bcf16c | 2013-06-24 19:31:17 +0200 | [diff] [blame] | 39 | #endif /* !POLARSSL_CONFIG_OPTIONS */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 40 | |
| 41 | #ifdef __cplusplus |
| 42 | extern "C" { |
| 43 | #endif |
| 44 | |
| 45 | typedef struct _ssl_cache_context ssl_cache_context; |
| 46 | typedef struct _ssl_cache_entry ssl_cache_entry; |
| 47 | |
| 48 | /** |
| 49 | * \brief This structure is used for storing cache entries |
| 50 | */ |
| 51 | struct _ssl_cache_entry |
| 52 | { |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 53 | #if defined(POLARSSL_HAVE_TIME) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 54 | time_t timestamp; /*!< entry timestamp */ |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 55 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 56 | ssl_session session; /*!< entry session */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 57 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
Paul Bakker | e81beda | 2013-03-06 17:40:46 +0100 | [diff] [blame] | 58 | x509_buf peer_cert; /*!< entry peer_cert */ |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 59 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 60 | ssl_cache_entry *next; /*!< chain pointer */ |
| 61 | }; |
| 62 | |
| 63 | /** |
| 64 | * \brief Cache context |
| 65 | */ |
| 66 | struct _ssl_cache_context |
| 67 | { |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 68 | ssl_cache_entry *chain; /*!< start of the chain */ |
| 69 | int timeout; /*!< cache entry timeout */ |
| 70 | int max_entries; /*!< maximum entries */ |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 71 | #if defined(POLARSSL_THREADING_C) |
| 72 | threading_mutex_t mutex; /*!< mutex */ |
| 73 | #endif |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | /** |
| 77 | * \brief Initialize an SSL cache context |
| 78 | * |
| 79 | * \param cache SSL cache context |
| 80 | */ |
| 81 | void ssl_cache_init( ssl_cache_context *cache ); |
| 82 | |
| 83 | /** |
| 84 | * \brief Cache get callback implementation |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 85 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 86 | * |
| 87 | * \param data SSL cache context |
| 88 | * \param session session to retrieve entry for |
| 89 | */ |
| 90 | int ssl_cache_get( void *data, ssl_session *session ); |
| 91 | |
| 92 | /** |
| 93 | * \brief Cache set callback implementation |
Paul Bakker | c559884 | 2013-09-28 15:01:27 +0200 | [diff] [blame] | 94 | * (Thread-safe if POLARSSL_THREADING_C is enabled) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 95 | * |
| 96 | * \param data SSL cache context |
| 97 | * \param session session to store entry for |
| 98 | */ |
| 99 | int ssl_cache_set( void *data, const ssl_session *session ); |
| 100 | |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 101 | #if defined(POLARSSL_HAVE_TIME) |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 102 | /** |
| 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 | */ |
| 111 | void ssl_cache_set_timeout( ssl_cache_context *cache, int timeout ); |
Paul Bakker | fa9b100 | 2013-07-03 15:31:03 +0200 | [diff] [blame] | 112 | #endif /* POLARSSL_HAVE_TIME */ |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 113 | |
| 114 | /** |
Paul Bakker | ba26e9e | 2012-10-23 22:18:28 +0000 | [diff] [blame] | 115 | * \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 | */ |
| 121 | void ssl_cache_set_max_entries( ssl_cache_context *cache, int max ); |
| 122 | |
| 123 | /** |
Paul Bakker | 0a59707 | 2012-09-25 21:55:46 +0000 | [diff] [blame] | 124 | * \brief Free referenced items in a cache context and clear memory |
| 125 | * |
| 126 | * \param cache SSL cache context |
| 127 | */ |
| 128 | void ssl_cache_free( ssl_cache_context *cache ); |
| 129 | |
| 130 | #ifdef __cplusplus |
| 131 | } |
| 132 | #endif |
| 133 | |
| 134 | #endif /* ssl_cache.h */ |