vtls_scache.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #ifndef HEADER_CURL_VTLS_SCACHE_H
  2. #define HEADER_CURL_VTLS_SCACHE_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  11. *
  12. * This software is licensed as described in the file COPYING, which
  13. * you should have received as part of this distribution. The terms
  14. * are also available at https://curl.se/docs/copyright.html.
  15. *
  16. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  17. * copies of the Software, and permit persons to whom the Software is
  18. * furnished to do so, under the terms of the COPYING file.
  19. *
  20. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  21. * KIND, either express or implied.
  22. *
  23. * SPDX-License-Identifier: curl
  24. *
  25. ***************************************************************************/
  26. #include "../curl_setup.h"
  27. #include "../cfilters.h"
  28. #include "../urldata.h"
  29. #ifdef USE_SSL
  30. struct Curl_cfilter;
  31. struct Curl_easy;
  32. struct Curl_ssl_scache;
  33. struct Curl_ssl_session;
  34. struct ssl_peer;
  35. /* RFC 8446 (TLSv1.3) restrict lifetime to one week max, for
  36. * other, less secure versions, we restrict it to a day */
  37. #define CURL_SCACHE_MAX_13_LIFETIME_SEC (60*60*24*7)
  38. #define CURL_SCACHE_MAX_12_LIFETIME_SEC (60*60*24)
  39. /* Create a session cache for up to max_peers endpoints with a total
  40. * of up to max_sessions SSL sessions per peer */
  41. CURLcode Curl_ssl_scache_create(size_t max_peers,
  42. size_t max_sessions_per_peer,
  43. struct Curl_ssl_scache **pscache);
  44. void Curl_ssl_scache_destroy(struct Curl_ssl_scache *scache);
  45. /* Create a key from peer and TLS configuration information that is
  46. * unique for how the connection filter wants to establish a TLS
  47. * connection to the peer.
  48. * If the filter is a TLS proxy filter, it will use the proxy relevant
  49. * information.
  50. * @param cf the connection filter wanting to use it
  51. * @param peer the peer the filter wants to talk to
  52. * @param tls_id identifier of TLS implementation for sessions. Should
  53. * include full version if session data from other versions
  54. * is to be avoided.
  55. * @param ppeer_key on successful return, the key generated
  56. */
  57. CURLcode Curl_ssl_peer_key_make(struct Curl_cfilter *cf,
  58. const struct ssl_peer *peer,
  59. const char *tls_id,
  60. char **ppeer_key);
  61. /* Return if there is a session cache shall be used.
  62. * An ssl session might not be configured or not available for
  63. * "connect-only" transfers.
  64. */
  65. bool Curl_ssl_scache_use(struct Curl_cfilter *cf, struct Curl_easy *data);
  66. /* Lock session cache mutex.
  67. * Call this before calling other Curl_ssl_*session* functions
  68. * Caller should unlock this mutex as soon as possible, as it may block
  69. * other SSL connection from making progress.
  70. * The purpose of explicitly locking SSL session cache data is to allow
  71. * individual SSL engines to manage session lifetime in their specific way.
  72. */
  73. void Curl_ssl_scache_lock(struct Curl_easy *data);
  74. /* Unlock session cache mutex */
  75. void Curl_ssl_scache_unlock(struct Curl_easy *data);
  76. /* Get TLS session object from the cache for the ssl_peer_ey.
  77. * scache mutex must be locked (see Curl_ssl_scache_lock).
  78. * Caller must make sure that the ownership of returned session object
  79. * is properly taken (e.g. its refcount is incremented
  80. * under scache mutex).
  81. * @param cf the connection filter wanting to use it
  82. * @param data the transfer involved
  83. * @param ssl_peer_key the key for lookup
  84. * @retval sobj the object for the peer key or NULL
  85. */
  86. void *Curl_ssl_scache_get_obj(struct Curl_cfilter *cf,
  87. struct Curl_easy *data,
  88. const char *ssl_peer_key);
  89. typedef void Curl_ssl_scache_obj_dtor(void *sobj);
  90. /* Add a TLS session related object to the cache.
  91. * Replaces an existing object with the same peer_key.
  92. * scache mutex must be locked (see Curl_ssl_scache_lock).
  93. * Call takes ownership of `sobj`, using `sobj_dtor_cb`
  94. * to deallocate it. Is called in all outcomes, either right away or
  95. * later when the session cache is cleaned up.
  96. * Caller must ensure that it has properly shared ownership of `sobj`
  97. * with cache (e.g. incrementing refcount on success)
  98. * @param cf the connection filter wanting to use it
  99. * @param data the transfer involved
  100. * @param ssl_peer_key the key for lookup
  101. * @param sobj the TLS session object
  102. * @param sobj_free_cb callback to free the session objectt
  103. */
  104. CURLcode Curl_ssl_scache_add_obj(struct Curl_cfilter *cf,
  105. struct Curl_easy *data,
  106. const char *ssl_peer_key,
  107. void *sobj,
  108. Curl_ssl_scache_obj_dtor *sobj_dtor_cb);
  109. /* All about an SSL session ticket */
  110. struct Curl_ssl_session {
  111. const void *sdata; /* session ticket data, plain bytes */
  112. size_t sdata_len; /* number of bytes in sdata */
  113. curl_off_t valid_until; /* seconds since EPOCH until ticket expires */
  114. int ietf_tls_id; /* TLS protocol identifier negotiated */
  115. char *alpn; /* APLN TLS negotiated protocol string */
  116. size_t earlydata_max; /* max 0-RTT data supported by peer */
  117. const unsigned char *quic_tp; /* Optional QUIC transport param bytes */
  118. size_t quic_tp_len; /* number of bytes in quic_tp */
  119. struct Curl_llist_node list; /* internal storage handling */
  120. };
  121. /* Create a `session` instance. Does NOT need locking.
  122. * Takes ownership of `sdata` and `sobj` regardless of return code.
  123. * @param sdata bytes of SSL session data or NULL (sobj then required)
  124. * @param sdata_len amount of session data bytes
  125. * @param ietf_tls_id IETF protocol version, e.g. 0x304 for TLSv1.3
  126. * @param alpn ALPN protocol selected or NULL
  127. * @param valid_until seconds since EPOCH when session expires, pass 0
  128. * in case this is not known.
  129. * @param psession on return the scached session instance created
  130. */
  131. CURLcode
  132. Curl_ssl_session_create(void *sdata, size_t sdata_len,
  133. int ietf_tls_id, const char *alpn,
  134. curl_off_t valid_until,
  135. size_t earlydata_max,
  136. struct Curl_ssl_session **psession);
  137. /* Variation of session creation with quic transport parameter bytes,
  138. * Takes ownership of `quic_tp` regardless of return code. */
  139. CURLcode
  140. Curl_ssl_session_create2(void *sdata, size_t sdata_len,
  141. int ietf_tls_id, const char *alpn,
  142. curl_off_t valid_until,
  143. size_t earlydata_max,
  144. unsigned char *quic_tp, size_t quic_tp_len,
  145. struct Curl_ssl_session **psession);
  146. /* Destroy a `session` instance. Can be called with NULL.
  147. * Does NOT need locking. */
  148. void Curl_ssl_session_destroy(struct Curl_ssl_session *s);
  149. /* Put the scache session into the cache. Does NOT need locking.
  150. * Call takes ownership of `s` in all outcomes.
  151. * @param cf the connection filter wanting to use it
  152. * @param data the transfer involved
  153. * @param ssl_peer_key the key for lookup
  154. * @param s the scache session object
  155. */
  156. CURLcode Curl_ssl_scache_put(struct Curl_cfilter *cf,
  157. struct Curl_easy *data,
  158. const char *ssl_peer_key,
  159. struct Curl_ssl_session *s);
  160. /* Take a matching scache session from the cache. Does NOT need locking.
  161. * @param cf the connection filter wanting to use it
  162. * @param data the transfer involved
  163. * @param ssl_peer_key the key for lookup
  164. * @param s on return, the scache session object or NULL
  165. */
  166. CURLcode Curl_ssl_scache_take(struct Curl_cfilter *cf,
  167. struct Curl_easy *data,
  168. const char *ssl_peer_key,
  169. struct Curl_ssl_session **ps);
  170. /* Return a taken scache session to the cache. Does NOT need locking.
  171. * Depending on TLS version and other criteria, it may cache it again
  172. * or destroy it. Maybe called with a NULL session.
  173. */
  174. void Curl_ssl_scache_return(struct Curl_cfilter *cf,
  175. struct Curl_easy *data,
  176. const char *ssl_peer_key,
  177. struct Curl_ssl_session *s);
  178. /* Remove all sessions and obj for the peer_key. Does NOT need locking. */
  179. void Curl_ssl_scache_remove_all(struct Curl_cfilter *cf,
  180. struct Curl_easy *data,
  181. const char *ssl_peer_key);
  182. #ifdef USE_SSLS_EXPORT
  183. CURLcode Curl_ssl_session_import(struct Curl_easy *data,
  184. const char *ssl_peer_key,
  185. const unsigned char *shmac, size_t shmac_len,
  186. const void *sdata, size_t sdata_len);
  187. CURLcode Curl_ssl_session_export(struct Curl_easy *data,
  188. curl_ssls_export_cb *export_fn,
  189. void *userptr);
  190. #endif /* USE_SSLS_EXPORT */
  191. #endif /* USE_SSL */
  192. #endif /* HEADER_CURL_VTLS_SCACHE_H */