ssl_cache.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**
  2. * \file ssl_cache.h
  3. *
  4. * \brief SSL session cache implementation
  5. */
  6. /*
  7. * Copyright The Mbed TLS Contributors
  8. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  9. */
  10. #ifndef MBEDTLS_SSL_CACHE_H
  11. #define MBEDTLS_SSL_CACHE_H
  12. #include "mbedtls/private_access.h"
  13. #include "mbedtls/build_info.h"
  14. #include "mbedtls/ssl.h"
  15. #if defined(MBEDTLS_THREADING_C)
  16. #include "mbedtls/threading.h"
  17. #endif
  18. /**
  19. * \name SECTION: Module settings
  20. *
  21. * The configuration options you can set for this module are in this section.
  22. * Either change them in mbedtls_config.h or define them on the compiler command line.
  23. * \{
  24. */
  25. #if !defined(MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT)
  26. #define MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT 86400 /*!< 1 day */
  27. #endif
  28. #if !defined(MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES)
  29. #define MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES 50 /*!< Maximum entries in cache */
  30. #endif
  31. /** \} name SECTION: Module settings */
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. typedef struct mbedtls_ssl_cache_context mbedtls_ssl_cache_context;
  36. typedef struct mbedtls_ssl_cache_entry mbedtls_ssl_cache_entry;
  37. /**
  38. * \brief This structure is used for storing cache entries
  39. */
  40. struct mbedtls_ssl_cache_entry {
  41. #if defined(MBEDTLS_HAVE_TIME)
  42. mbedtls_time_t MBEDTLS_PRIVATE(timestamp); /*!< entry timestamp */
  43. #endif
  44. unsigned char MBEDTLS_PRIVATE(session_id)[32]; /*!< session ID */
  45. size_t MBEDTLS_PRIVATE(session_id_len);
  46. unsigned char *MBEDTLS_PRIVATE(session); /*!< serialized session */
  47. size_t MBEDTLS_PRIVATE(session_len);
  48. mbedtls_ssl_cache_entry *MBEDTLS_PRIVATE(next); /*!< chain pointer */
  49. };
  50. /**
  51. * \brief Cache context
  52. */
  53. struct mbedtls_ssl_cache_context {
  54. mbedtls_ssl_cache_entry *MBEDTLS_PRIVATE(chain); /*!< start of the chain */
  55. int MBEDTLS_PRIVATE(timeout); /*!< cache entry timeout */
  56. int MBEDTLS_PRIVATE(max_entries); /*!< maximum entries */
  57. #if defined(MBEDTLS_THREADING_C)
  58. mbedtls_threading_mutex_t MBEDTLS_PRIVATE(mutex); /*!< mutex */
  59. #endif
  60. };
  61. /**
  62. * \brief Initialize an SSL cache context
  63. *
  64. * \param cache SSL cache context
  65. */
  66. void mbedtls_ssl_cache_init(mbedtls_ssl_cache_context *cache);
  67. /**
  68. * \brief Cache get callback implementation
  69. * (Thread-safe if MBEDTLS_THREADING_C is enabled)
  70. *
  71. * \param data The SSL cache context to use.
  72. * \param session_id The pointer to the buffer holding the session ID
  73. * for the session to load.
  74. * \param session_id_len The length of \p session_id in bytes.
  75. * \param session The address at which to store the session
  76. * associated with \p session_id, if present.
  77. *
  78. * \return \c 0 on success.
  79. * \return #MBEDTLS_ERR_SSL_CACHE_ENTRY_NOT_FOUND if there is
  80. * no cache entry with specified session ID found, or
  81. * any other negative error code for other failures.
  82. */
  83. int mbedtls_ssl_cache_get(void *data,
  84. unsigned char const *session_id,
  85. size_t session_id_len,
  86. mbedtls_ssl_session *session);
  87. /**
  88. * \brief Cache set callback implementation
  89. * (Thread-safe if MBEDTLS_THREADING_C is enabled)
  90. *
  91. * \param data The SSL cache context to use.
  92. * \param session_id The pointer to the buffer holding the session ID
  93. * associated to \p session.
  94. * \param session_id_len The length of \p session_id in bytes.
  95. * \param session The session to store.
  96. *
  97. * \return \c 0 on success.
  98. * \return A negative error code on failure.
  99. */
  100. int mbedtls_ssl_cache_set(void *data,
  101. unsigned char const *session_id,
  102. size_t session_id_len,
  103. const mbedtls_ssl_session *session);
  104. /**
  105. * \brief Remove the cache entry by the session ID
  106. * (Thread-safe if MBEDTLS_THREADING_C is enabled)
  107. *
  108. * \param data The SSL cache context to use.
  109. * \param session_id The pointer to the buffer holding the session ID
  110. * associated to session.
  111. * \param session_id_len The length of \p session_id in bytes.
  112. *
  113. * \return \c 0 on success. This indicates the cache entry for
  114. * the session with provided ID is removed or does not
  115. * exist.
  116. * \return A negative error code on failure.
  117. */
  118. int mbedtls_ssl_cache_remove(void *data,
  119. unsigned char const *session_id,
  120. size_t session_id_len);
  121. #if defined(MBEDTLS_HAVE_TIME)
  122. /**
  123. * \brief Set the cache timeout
  124. * (Default: MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT (1 day))
  125. *
  126. * A timeout of 0 indicates no timeout.
  127. *
  128. * \param cache SSL cache context
  129. * \param timeout cache entry timeout in seconds
  130. */
  131. void mbedtls_ssl_cache_set_timeout(mbedtls_ssl_cache_context *cache, int timeout);
  132. /**
  133. * \brief Get the cache timeout
  134. *
  135. * A timeout of 0 indicates no timeout.
  136. *
  137. * \param cache SSL cache context
  138. *
  139. * \return cache entry timeout in seconds
  140. */
  141. static inline int mbedtls_ssl_cache_get_timeout(mbedtls_ssl_cache_context *cache)
  142. {
  143. return cache->MBEDTLS_PRIVATE(timeout);
  144. }
  145. #endif /* MBEDTLS_HAVE_TIME */
  146. /**
  147. * \brief Set the maximum number of cache entries
  148. * (Default: MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES (50))
  149. *
  150. * \param cache SSL cache context
  151. * \param max cache entry maximum
  152. */
  153. void mbedtls_ssl_cache_set_max_entries(mbedtls_ssl_cache_context *cache, int max);
  154. /**
  155. * \brief Free referenced items in a cache context and clear memory
  156. *
  157. * \param cache SSL cache context
  158. */
  159. void mbedtls_ssl_cache_free(mbedtls_ssl_cache_context *cache);
  160. #ifdef __cplusplus
  161. }
  162. #endif
  163. #endif /* ssl_cache.h */