vtls.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #ifndef HEADER_CURL_VTLS_H
  2. #define HEADER_CURL_VTLS_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, 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. struct connectdata;
  28. struct ssl_config_data;
  29. struct ssl_primary_config;
  30. struct Curl_ssl_session;
  31. #define SSLSUPP_CA_PATH (1<<0) /* supports CAPATH */
  32. #define SSLSUPP_CERTINFO (1<<1) /* supports CURLOPT_CERTINFO */
  33. #define SSLSUPP_PINNEDPUBKEY (1<<2) /* supports CURLOPT_PINNEDPUBLICKEY */
  34. #define SSLSUPP_SSL_CTX (1<<3) /* supports CURLOPT_SSL_CTX */
  35. #define SSLSUPP_HTTPS_PROXY (1<<4) /* supports access via HTTPS proxies */
  36. #define SSLSUPP_TLS13_CIPHERSUITES (1<<5) /* supports TLS 1.3 ciphersuites */
  37. #define SSLSUPP_CAINFO_BLOB (1<<6)
  38. #define ALPN_ACCEPTED "ALPN: server accepted "
  39. #define VTLS_INFOF_NO_ALPN \
  40. "ALPN: server did not agree on a protocol. Uses default."
  41. #define VTLS_INFOF_ALPN_OFFER_1STR \
  42. "ALPN: offers %s"
  43. #define VTLS_INFOF_ALPN_ACCEPTED_1STR \
  44. ALPN_ACCEPTED "%s"
  45. #define VTLS_INFOF_ALPN_ACCEPTED_LEN_1STR \
  46. ALPN_ACCEPTED "%.*s"
  47. /* Curl_multi SSL backend-specific data; declared differently by each SSL
  48. backend */
  49. struct multi_ssl_backend_data;
  50. struct Curl_cfilter;
  51. CURLsslset Curl_init_sslset_nolock(curl_sslbackend id, const char *name,
  52. const curl_ssl_backend ***avail);
  53. #ifndef MAX_PINNED_PUBKEY_SIZE
  54. #define MAX_PINNED_PUBKEY_SIZE 1048576 /* 1MB */
  55. #endif
  56. #ifndef CURL_SHA256_DIGEST_LENGTH
  57. #define CURL_SHA256_DIGEST_LENGTH 32 /* fixed size */
  58. #endif
  59. /* see https://www.iana.org/assignments/tls-extensiontype-values/ */
  60. #define ALPN_HTTP_1_1_LENGTH 8
  61. #define ALPN_HTTP_1_1 "http/1.1"
  62. #define ALPN_HTTP_1_0_LENGTH 8
  63. #define ALPN_HTTP_1_0 "http/1.0"
  64. #define ALPN_H2_LENGTH 2
  65. #define ALPN_H2 "h2"
  66. #define ALPN_H3_LENGTH 2
  67. #define ALPN_H3 "h3"
  68. /* conservative sizes on the ALPN entries and count we are handling,
  69. * we can increase these if we ever feel the need or have to accommodate
  70. * ALPN strings from the "outside". */
  71. #define ALPN_NAME_MAX 10
  72. #define ALPN_ENTRIES_MAX 3
  73. #define ALPN_PROTO_BUF_MAX (ALPN_ENTRIES_MAX * (ALPN_NAME_MAX + 1))
  74. struct alpn_spec {
  75. const char entries[ALPN_ENTRIES_MAX][ALPN_NAME_MAX];
  76. size_t count; /* number of entries */
  77. };
  78. struct alpn_proto_buf {
  79. unsigned char data[ALPN_PROTO_BUF_MAX];
  80. int len;
  81. };
  82. CURLcode Curl_alpn_to_proto_buf(struct alpn_proto_buf *buf,
  83. const struct alpn_spec *spec);
  84. CURLcode Curl_alpn_to_proto_str(struct alpn_proto_buf *buf,
  85. const struct alpn_spec *spec);
  86. CURLcode Curl_alpn_set_negotiated(struct Curl_cfilter *cf,
  87. struct Curl_easy *data,
  88. const unsigned char *proto,
  89. size_t proto_len);
  90. /**
  91. * Get the ALPN specification to use for talking to remote host.
  92. * May return NULL if ALPN is disabled on the connection.
  93. */
  94. const struct alpn_spec *
  95. Curl_alpn_get_spec(struct Curl_easy *data, struct connectdata *conn);
  96. /**
  97. * Get the ALPN specification to use for talking to the proxy.
  98. * May return NULL if ALPN is disabled on the connection.
  99. */
  100. const struct alpn_spec *
  101. Curl_alpn_get_proxy_spec(struct Curl_easy *data, struct connectdata *conn);
  102. char *Curl_ssl_snihost(struct Curl_easy *data, const char *host, size_t *olen);
  103. bool Curl_ssl_config_matches(struct ssl_primary_config *data,
  104. struct ssl_primary_config *needle);
  105. bool Curl_clone_primary_ssl_config(struct ssl_primary_config *source,
  106. struct ssl_primary_config *dest);
  107. void Curl_free_primary_ssl_config(struct ssl_primary_config *sslc);
  108. curl_sslbackend Curl_ssl_backend(void);
  109. #ifdef USE_SSL
  110. int Curl_ssl_init(void);
  111. void Curl_ssl_cleanup(void);
  112. /* tell the SSL stuff to close down all open information regarding
  113. connections (and thus session ID caching etc) */
  114. void Curl_ssl_close_all(struct Curl_easy *data);
  115. CURLcode Curl_ssl_set_engine(struct Curl_easy *data, const char *engine);
  116. /* Sets engine as default for all SSL operations */
  117. CURLcode Curl_ssl_set_engine_default(struct Curl_easy *data);
  118. struct curl_slist *Curl_ssl_engines_list(struct Curl_easy *data);
  119. /* init the SSL session ID cache */
  120. CURLcode Curl_ssl_initsessions(struct Curl_easy *, size_t);
  121. void Curl_ssl_version(char *buffer, size_t size);
  122. /* Certificate information list handling. */
  123. void Curl_ssl_free_certinfo(struct Curl_easy *data);
  124. CURLcode Curl_ssl_init_certinfo(struct Curl_easy *data, int num);
  125. CURLcode Curl_ssl_push_certinfo_len(struct Curl_easy *data, int certnum,
  126. const char *label, const char *value,
  127. size_t valuelen);
  128. CURLcode Curl_ssl_push_certinfo(struct Curl_easy *data, int certnum,
  129. const char *label, const char *value);
  130. /* Functions to be used by SSL library adaptation functions */
  131. /* Lock session cache mutex.
  132. * Call this before calling other Curl_ssl_*session* functions
  133. * Caller should unlock this mutex as soon as possible, as it may block
  134. * other SSL connection from making progress.
  135. * The purpose of explicitly locking SSL session cache data is to allow
  136. * individual SSL engines to manage session lifetime in their specific way.
  137. */
  138. void Curl_ssl_sessionid_lock(struct Curl_easy *data);
  139. /* Unlock session cache mutex */
  140. void Curl_ssl_sessionid_unlock(struct Curl_easy *data);
  141. /* Kill a single session ID entry in the cache
  142. * Sessionid mutex must be locked (see Curl_ssl_sessionid_lock).
  143. * This will call engine-specific curlssl_session_free function, which must
  144. * take sessionid object ownership from sessionid cache
  145. * (e.g. decrement refcount).
  146. */
  147. void Curl_ssl_kill_session(struct Curl_ssl_session *session);
  148. /* delete a session from the cache
  149. * Sessionid mutex must be locked (see Curl_ssl_sessionid_lock).
  150. * This will call engine-specific curlssl_session_free function, which must
  151. * take sessionid object ownership from sessionid cache
  152. * (e.g. decrement refcount).
  153. */
  154. void Curl_ssl_delsessionid(struct Curl_easy *data, void *ssl_sessionid);
  155. /* get N random bytes into the buffer */
  156. CURLcode Curl_ssl_random(struct Curl_easy *data, unsigned char *buffer,
  157. size_t length);
  158. /* Check pinned public key. */
  159. CURLcode Curl_pin_peer_pubkey(struct Curl_easy *data,
  160. const char *pinnedpubkey,
  161. const unsigned char *pubkey, size_t pubkeylen);
  162. bool Curl_ssl_cert_status_request(void);
  163. bool Curl_ssl_false_start(struct Curl_easy *data);
  164. void Curl_free_multi_ssl_backend_data(struct multi_ssl_backend_data *mbackend);
  165. #define SSL_SHUTDOWN_TIMEOUT 10000 /* ms */
  166. CURLcode Curl_ssl_cfilter_add(struct Curl_easy *data,
  167. struct connectdata *conn,
  168. int sockindex);
  169. CURLcode Curl_cf_ssl_insert_after(struct Curl_cfilter *cf_at,
  170. struct Curl_easy *data);
  171. CURLcode Curl_ssl_cfilter_remove(struct Curl_easy *data,
  172. int sockindex);
  173. #ifndef CURL_DISABLE_PROXY
  174. CURLcode Curl_ssl_cfilter_proxy_add(struct Curl_easy *data,
  175. struct connectdata *conn,
  176. int sockindex);
  177. CURLcode Curl_cf_ssl_proxy_insert_after(struct Curl_cfilter *cf_at,
  178. struct Curl_easy *data);
  179. #endif /* !CURL_DISABLE_PROXY */
  180. /**
  181. * Get the SSL configuration that is used on the connection.
  182. * This returns NULL if no SSL is configured.
  183. * Otherwise it returns the config of the first (highest) one that is
  184. * either connected, in handshake or about to start
  185. * (e.g. all filters below it are connected). If SSL filters are present,
  186. * but neither can start operating, return the config of the lowest one
  187. * that will first come into effect when connecting.
  188. */
  189. struct ssl_config_data *Curl_ssl_get_config(struct Curl_easy *data,
  190. int sockindex);
  191. /**
  192. * Get the primary SSL configuration from the connection.
  193. * This returns NULL if no SSL is configured.
  194. * Otherwise it returns the config of the first (highest) one that is
  195. * either connected, in handshake or about to start
  196. * (e.g. all filters below it are connected). If SSL filters are present,
  197. * but neither can start operating, return the config of the lowest one
  198. * that will first come into effect when connecting.
  199. */
  200. struct ssl_primary_config *
  201. Curl_ssl_get_primary_config(struct Curl_easy *data,
  202. struct connectdata *conn,
  203. int sockindex);
  204. /**
  205. * True iff the underlying SSL implementation supports the option.
  206. * Option is one of the defined SSLSUPP_* values.
  207. * `data` maybe NULL for the features of the default implementation.
  208. */
  209. bool Curl_ssl_supports(struct Curl_easy *data, int ssl_option);
  210. /**
  211. * Get the internal ssl instance (like OpenSSL's SSL*) from the filter
  212. * chain at `sockindex` of type specified by `info`.
  213. * For `n` == 0, the first active (top down) instance is returned.
  214. * 1 gives the second active, etc.
  215. * NULL is returned when no active SSL filter is present.
  216. */
  217. void *Curl_ssl_get_internals(struct Curl_easy *data, int sockindex,
  218. CURLINFO info, int n);
  219. extern struct Curl_cftype Curl_cft_ssl;
  220. extern struct Curl_cftype Curl_cft_ssl_proxy;
  221. #else /* if not USE_SSL */
  222. /* When SSL support is not present, just define away these function calls */
  223. #define Curl_ssl_init() 1
  224. #define Curl_ssl_cleanup() Curl_nop_stmt
  225. #define Curl_ssl_close_all(x) Curl_nop_stmt
  226. #define Curl_ssl_set_engine(x,y) CURLE_NOT_BUILT_IN
  227. #define Curl_ssl_set_engine_default(x) CURLE_NOT_BUILT_IN
  228. #define Curl_ssl_engines_list(x) NULL
  229. #define Curl_ssl_initsessions(x,y) CURLE_OK
  230. #define Curl_ssl_free_certinfo(x) Curl_nop_stmt
  231. #define Curl_ssl_kill_session(x) Curl_nop_stmt
  232. #define Curl_ssl_random(x,y,z) ((void)x, CURLE_NOT_BUILT_IN)
  233. #define Curl_ssl_cert_status_request() FALSE
  234. #define Curl_ssl_false_start(a) FALSE
  235. #define Curl_ssl_get_internals(a,b,c,d) NULL
  236. #define Curl_ssl_supports(a,b) FALSE
  237. #define Curl_ssl_cfilter_add(a,b,c) CURLE_NOT_BUILT_IN
  238. #define Curl_ssl_cfilter_proxy_add(a,b,c) CURLE_NOT_BUILT_IN
  239. #define Curl_ssl_get_config(a,b) NULL
  240. #define Curl_ssl_cfilter_remove(a,b) CURLE_OK
  241. #endif
  242. #endif /* HEADER_CURL_VTLS_H */