2
0

vtls.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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_cfilter;
  31. struct Curl_easy;
  32. struct dynbuf;
  33. #define SSLSUPP_CA_PATH (1<<0) /* supports CAPATH */
  34. #define SSLSUPP_CERTINFO (1<<1) /* supports CURLOPT_CERTINFO */
  35. #define SSLSUPP_PINNEDPUBKEY (1<<2) /* supports CURLOPT_PINNEDPUBLICKEY */
  36. #define SSLSUPP_SSL_CTX (1<<3) /* supports CURLOPT_SSL_CTX */
  37. #define SSLSUPP_HTTPS_PROXY (1<<4) /* supports access via HTTPS proxies */
  38. #define SSLSUPP_TLS13_CIPHERSUITES (1<<5) /* supports TLS 1.3 ciphersuites */
  39. #define SSLSUPP_CAINFO_BLOB (1<<6)
  40. #define SSLSUPP_ECH (1<<7)
  41. #define SSLSUPP_CA_CACHE (1<<8)
  42. #define SSLSUPP_CIPHER_LIST (1<<9) /* supports TLS 1.0-1.2 ciphersuites */
  43. #ifdef USE_ECH
  44. # include "curl_base64.h"
  45. # define ECH_ENABLED(__data__) \
  46. (__data__->set.tls_ech && \
  47. !(__data__->set.tls_ech & CURLECH_DISABLE)\
  48. )
  49. #endif /* USE_ECH */
  50. #define ALPN_ACCEPTED "ALPN: server accepted "
  51. #define VTLS_INFOF_NO_ALPN \
  52. "ALPN: server did not agree on a protocol. Uses default."
  53. #define VTLS_INFOF_ALPN_OFFER_1STR \
  54. "ALPN: curl offers %s"
  55. #define VTLS_INFOF_ALPN_ACCEPTED \
  56. ALPN_ACCEPTED "%.*s"
  57. #define VTLS_INFOF_NO_ALPN_DEFERRED \
  58. "ALPN: deferred handshake for early data without specific protocol."
  59. #define VTLS_INFOF_ALPN_DEFERRED \
  60. "ALPN: deferred handshake for early data using '%.*s'."
  61. /* IETF defined version numbers used in TLS protocol negotiation */
  62. #define CURL_IETF_PROTO_UNKNOWN 0x0
  63. #define CURL_IETF_PROTO_SSL3 0x0300
  64. #define CURL_IETF_PROTO_TLS1 0x0301
  65. #define CURL_IETF_PROTO_TLS1_1 0x0302
  66. #define CURL_IETF_PROTO_TLS1_2 0x0303
  67. #define CURL_IETF_PROTO_TLS1_3 0x0304
  68. #define CURL_IETF_PROTO_DTLS1 0xFEFF
  69. #define CURL_IETF_PROTO_DTLS1_2 0xFEFD
  70. typedef enum {
  71. CURL_SSL_PEER_DNS,
  72. CURL_SSL_PEER_IPV4,
  73. CURL_SSL_PEER_IPV6
  74. } ssl_peer_type;
  75. struct ssl_peer {
  76. char *hostname; /* hostname for verification */
  77. char *dispname; /* display version of hostname */
  78. char *sni; /* SNI version of hostname or NULL if not usable */
  79. char *scache_key; /* for lookups in session cache */
  80. ssl_peer_type type; /* type of the peer information */
  81. int port; /* port we are talking to */
  82. int transport; /* one of TRNSPRT_* defines */
  83. };
  84. CURLsslset Curl_init_sslset_nolock(curl_sslbackend id, const char *name,
  85. const curl_ssl_backend ***avail);
  86. #ifndef MAX_PINNED_PUBKEY_SIZE
  87. #define MAX_PINNED_PUBKEY_SIZE 1048576 /* 1MB */
  88. #endif
  89. #ifndef CURL_SHA256_DIGEST_LENGTH
  90. #define CURL_SHA256_DIGEST_LENGTH 32 /* fixed size */
  91. #endif
  92. curl_sslbackend Curl_ssl_backend(void);
  93. /**
  94. * Init ssl config for a new easy handle.
  95. */
  96. void Curl_ssl_easy_config_init(struct Curl_easy *data);
  97. /**
  98. * Init the `data->set.ssl` and `data->set.proxy_ssl` for
  99. * connection matching use.
  100. */
  101. CURLcode Curl_ssl_easy_config_complete(struct Curl_easy *data);
  102. /**
  103. * Init SSL configs (main + proxy) for a new connection from the easy handle.
  104. */
  105. CURLcode Curl_ssl_conn_config_init(struct Curl_easy *data,
  106. struct connectdata *conn);
  107. /**
  108. * Free allocated resources in SSL configs (main + proxy) for
  109. * the given connection.
  110. */
  111. void Curl_ssl_conn_config_cleanup(struct connectdata *conn);
  112. /**
  113. * Return TRUE iff SSL configuration from `data` is functionally the
  114. * same as the one on `candidate`.
  115. * @param proxy match the proxy SSL config or the main one
  116. */
  117. bool Curl_ssl_conn_config_match(struct Curl_easy *data,
  118. struct connectdata *candidate,
  119. bool proxy);
  120. /* Update certain connection SSL config flags after they have
  121. * been changed on the easy handle. Will work for `verifypeer`,
  122. * `verifyhost` and `verifystatus`. */
  123. void Curl_ssl_conn_config_update(struct Curl_easy *data, bool for_proxy);
  124. /**
  125. * Init SSL peer information for filter. Can be called repeatedly.
  126. */
  127. CURLcode Curl_ssl_peer_init(struct ssl_peer *peer,
  128. struct Curl_cfilter *cf,
  129. const char *tls_id,
  130. int transport);
  131. /**
  132. * Free all allocated data and reset peer information.
  133. */
  134. void Curl_ssl_peer_cleanup(struct ssl_peer *peer);
  135. #ifdef USE_SSL
  136. int Curl_ssl_init(void);
  137. void Curl_ssl_cleanup(void);
  138. /* tell the SSL stuff to close down all open information regarding
  139. connections (and thus session ID caching etc) */
  140. void Curl_ssl_close_all(struct Curl_easy *data);
  141. CURLcode Curl_ssl_set_engine(struct Curl_easy *data, const char *engine);
  142. /* Sets engine as default for all SSL operations */
  143. CURLcode Curl_ssl_set_engine_default(struct Curl_easy *data);
  144. struct curl_slist *Curl_ssl_engines_list(struct Curl_easy *data);
  145. void Curl_ssl_version(char *buffer, size_t size);
  146. /* Certificate information list handling. */
  147. #define CURL_X509_STR_MAX 100000
  148. void Curl_ssl_free_certinfo(struct Curl_easy *data);
  149. CURLcode Curl_ssl_init_certinfo(struct Curl_easy *data, int num);
  150. CURLcode Curl_ssl_push_certinfo_len(struct Curl_easy *data, int certnum,
  151. const char *label, const char *value,
  152. size_t valuelen);
  153. CURLcode Curl_ssl_push_certinfo(struct Curl_easy *data, int certnum,
  154. const char *label, const char *value);
  155. /* Functions to be used by SSL library adaptation functions */
  156. /* get N random bytes into the buffer */
  157. CURLcode Curl_ssl_random(struct Curl_easy *data, unsigned char *buffer,
  158. size_t length);
  159. /* Check pinned public key. */
  160. CURLcode Curl_pin_peer_pubkey(struct Curl_easy *data,
  161. const char *pinnedpubkey,
  162. const unsigned char *pubkey, size_t pubkeylen);
  163. bool Curl_ssl_cert_status_request(void);
  164. bool Curl_ssl_false_start(void);
  165. /* The maximum size of the SSL channel binding is 85 bytes, as defined in
  166. * RFC 5929, Section 4.1. The 'tls-server-end-point:' prefix is 21 bytes long,
  167. * and SHA-512 is the longest supported hash algorithm, with a digest length of
  168. * 64 bytes.
  169. * The maximum size of the channel binding is therefore 21 + 64 = 85 bytes.
  170. */
  171. #define SSL_CB_MAX_SIZE 85
  172. /* Return the tls-server-end-point channel binding, including the
  173. * 'tls-server-end-point:' prefix.
  174. * If successful, the data is written to the dynbuf, and CURLE_OK is returned.
  175. * The dynbuf MUST HAVE a minimum toobig size of SSL_CB_MAX_SIZE.
  176. * If the dynbuf is too small, CURLE_OUT_OF_MEMORY is returned.
  177. * If channel binding is not supported, binding stays empty and CURLE_OK is
  178. * returned.
  179. */
  180. CURLcode Curl_ssl_get_channel_binding(struct Curl_easy *data, int sockindex,
  181. struct dynbuf *binding);
  182. #define SSL_SHUTDOWN_TIMEOUT 10000 /* ms */
  183. CURLcode Curl_ssl_cfilter_add(struct Curl_easy *data,
  184. struct connectdata *conn,
  185. int sockindex);
  186. CURLcode Curl_cf_ssl_insert_after(struct Curl_cfilter *cf_at,
  187. struct Curl_easy *data);
  188. CURLcode Curl_ssl_cfilter_remove(struct Curl_easy *data,
  189. int sockindex, bool send_shutdown);
  190. #ifndef CURL_DISABLE_PROXY
  191. CURLcode Curl_cf_ssl_proxy_insert_after(struct Curl_cfilter *cf_at,
  192. struct Curl_easy *data);
  193. #endif /* !CURL_DISABLE_PROXY */
  194. /**
  195. * True iff the underlying SSL implementation supports the option.
  196. * Option is one of the defined SSLSUPP_* values.
  197. * `data` maybe NULL for the features of the default implementation.
  198. */
  199. bool Curl_ssl_supports(struct Curl_easy *data, unsigned int ssl_option);
  200. /**
  201. * Get the internal ssl instance (like OpenSSL's SSL*) from the filter
  202. * chain at `sockindex` of type specified by `info`.
  203. * For `n` == 0, the first active (top down) instance is returned.
  204. * 1 gives the second active, etc.
  205. * NULL is returned when no active SSL filter is present.
  206. */
  207. void *Curl_ssl_get_internals(struct Curl_easy *data, int sockindex,
  208. CURLINFO info, int n);
  209. /**
  210. * Get the ssl_config_data in `data` that is relevant for cfilter `cf`.
  211. */
  212. struct ssl_config_data *Curl_ssl_cf_get_config(struct Curl_cfilter *cf,
  213. struct Curl_easy *data);
  214. /**
  215. * Get the primary config relevant for the filter from its connection.
  216. */
  217. struct ssl_primary_config *
  218. Curl_ssl_cf_get_primary_config(struct Curl_cfilter *cf);
  219. extern struct Curl_cftype Curl_cft_ssl;
  220. #ifndef CURL_DISABLE_PROXY
  221. extern struct Curl_cftype Curl_cft_ssl_proxy;
  222. #endif
  223. #else /* if not USE_SSL */
  224. /* When SSL support is not present, just define away these function calls */
  225. #define Curl_ssl_init() 1
  226. #define Curl_ssl_cleanup() Curl_nop_stmt
  227. #define Curl_ssl_close_all(x) Curl_nop_stmt
  228. #define Curl_ssl_set_engine(x,y) CURLE_NOT_BUILT_IN
  229. #define Curl_ssl_set_engine_default(x) CURLE_NOT_BUILT_IN
  230. #define Curl_ssl_engines_list(x) NULL
  231. #define Curl_ssl_free_certinfo(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() 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_remove(a,b,c) CURLE_OK
  239. #define Curl_ssl_cf_get_config(a,b) NULL
  240. #define Curl_ssl_cf_get_primary_config(a) NULL
  241. #endif
  242. #endif /* HEADER_CURL_VTLS_H */