ssl_cache.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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: GPL-2.0
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. *
  23. * This file is part of mbed TLS (https://tls.mbed.org)
  24. */
  25. #ifndef MBEDTLS_SSL_CACHE_H
  26. #define MBEDTLS_SSL_CACHE_H
  27. #include "ssl.h"
  28. #if defined(MBEDTLS_THREADING_C)
  29. #include "threading.h"
  30. #endif
  31. /**
  32. * \name SECTION: Module settings
  33. *
  34. * The configuration options you can set for this module are in this section.
  35. * Either change them in config.h or define them on the compiler command line.
  36. * \{
  37. */
  38. #if !defined(MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT)
  39. #define MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT 86400 /*!< 1 day */
  40. #endif
  41. #if !defined(MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES)
  42. #define MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES 50 /*!< Maximum entries in cache */
  43. #endif
  44. /* \} name SECTION: Module settings */
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. typedef struct mbedtls_ssl_cache_context mbedtls_ssl_cache_context;
  49. typedef struct mbedtls_ssl_cache_entry mbedtls_ssl_cache_entry;
  50. /**
  51. * \brief This structure is used for storing cache entries
  52. */
  53. struct mbedtls_ssl_cache_entry
  54. {
  55. #if defined(MBEDTLS_HAVE_TIME)
  56. mbedtls_time_t timestamp; /*!< entry timestamp */
  57. #endif
  58. mbedtls_ssl_session session; /*!< entry session */
  59. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  60. mbedtls_x509_buf peer_cert; /*!< entry peer_cert */
  61. #endif
  62. mbedtls_ssl_cache_entry *next; /*!< chain pointer */
  63. };
  64. /**
  65. * \brief Cache context
  66. */
  67. struct mbedtls_ssl_cache_context
  68. {
  69. mbedtls_ssl_cache_entry *chain; /*!< start of the chain */
  70. int timeout; /*!< cache entry timeout */
  71. int max_entries; /*!< maximum entries */
  72. #if defined(MBEDTLS_THREADING_C)
  73. mbedtls_threading_mutex_t mutex; /*!< mutex */
  74. #endif
  75. };
  76. /**
  77. * \brief Initialize an SSL cache context
  78. *
  79. * \param cache SSL cache context
  80. */
  81. void mbedtls_ssl_cache_init( mbedtls_ssl_cache_context *cache );
  82. /**
  83. * \brief Cache get callback implementation
  84. * (Thread-safe if MBEDTLS_THREADING_C is enabled)
  85. *
  86. * \param data SSL cache context
  87. * \param session session to retrieve entry for
  88. */
  89. int mbedtls_ssl_cache_get( void *data, mbedtls_ssl_session *session );
  90. /**
  91. * \brief Cache set callback implementation
  92. * (Thread-safe if MBEDTLS_THREADING_C is enabled)
  93. *
  94. * \param data SSL cache context
  95. * \param session session to store entry for
  96. */
  97. int mbedtls_ssl_cache_set( void *data, const mbedtls_ssl_session *session );
  98. #if defined(MBEDTLS_HAVE_TIME)
  99. /**
  100. * \brief Set the cache timeout
  101. * (Default: MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT (1 day))
  102. *
  103. * A timeout of 0 indicates no timeout.
  104. *
  105. * \param cache SSL cache context
  106. * \param timeout cache entry timeout in seconds
  107. */
  108. void mbedtls_ssl_cache_set_timeout( mbedtls_ssl_cache_context *cache, int timeout );
  109. #endif /* MBEDTLS_HAVE_TIME */
  110. /**
  111. * \brief Set the maximum number of cache entries
  112. * (Default: MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES (50))
  113. *
  114. * \param cache SSL cache context
  115. * \param max cache entry maximum
  116. */
  117. void mbedtls_ssl_cache_set_max_entries( mbedtls_ssl_cache_context *cache, int max );
  118. /**
  119. * \brief Free referenced items in a cache context and clear memory
  120. *
  121. * \param cache SSL cache context
  122. */
  123. void mbedtls_ssl_cache_free( mbedtls_ssl_cache_context *cache );
  124. #ifdef __cplusplus
  125. }
  126. #endif
  127. #endif /* ssl_cache.h */