vtls.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #ifndef HEADER_CURL_VTLS_H
  2. #define HEADER_CURL_VTLS_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) 1998 - 2022, 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_connect_data;
  29. #define SSLSUPP_CA_PATH (1<<0) /* supports CAPATH */
  30. #define SSLSUPP_CERTINFO (1<<1) /* supports CURLOPT_CERTINFO */
  31. #define SSLSUPP_PINNEDPUBKEY (1<<2) /* supports CURLOPT_PINNEDPUBLICKEY */
  32. #define SSLSUPP_SSL_CTX (1<<3) /* supports CURLOPT_SSL_CTX */
  33. #define SSLSUPP_HTTPS_PROXY (1<<4) /* supports access via HTTPS proxies */
  34. #define SSLSUPP_TLS13_CIPHERSUITES (1<<5) /* supports TLS 1.3 ciphersuites */
  35. #define SSLSUPP_CAINFO_BLOB (1<<6)
  36. #define ALPN_ACCEPTED "ALPN: server accepted "
  37. #define VTLS_INFOF_NO_ALPN \
  38. "ALPN: server did not agree on a protocol. Uses default."
  39. #define VTLS_INFOF_ALPN_OFFER_1STR \
  40. "ALPN: offers %s"
  41. #define VTLS_INFOF_ALPN_ACCEPTED_1STR \
  42. ALPN_ACCEPTED "%s"
  43. #define VTLS_INFOF_ALPN_ACCEPTED_LEN_1STR \
  44. ALPN_ACCEPTED "%.*s"
  45. /* Curl_multi SSL backend-specific data; declared differently by each SSL
  46. backend */
  47. struct multi_ssl_backend_data;
  48. struct Curl_ssl {
  49. /*
  50. * This *must* be the first entry to allow returning the list of available
  51. * backends in curl_global_sslset().
  52. */
  53. curl_ssl_backend info;
  54. unsigned int supports; /* bitfield, see above */
  55. size_t sizeof_ssl_backend_data;
  56. int (*init)(void);
  57. void (*cleanup)(void);
  58. size_t (*version)(char *buffer, size_t size);
  59. int (*check_cxn)(struct connectdata *cxn);
  60. int (*shut_down)(struct Curl_easy *data, struct connectdata *conn,
  61. int sockindex);
  62. bool (*data_pending)(const struct connectdata *conn,
  63. int connindex);
  64. /* return 0 if a find random is filled in */
  65. CURLcode (*random)(struct Curl_easy *data, unsigned char *entropy,
  66. size_t length);
  67. bool (*cert_status_request)(void);
  68. CURLcode (*connect_blocking)(struct Curl_easy *data,
  69. struct connectdata *conn, int sockindex);
  70. CURLcode (*connect_nonblocking)(struct Curl_easy *data,
  71. struct connectdata *conn, int sockindex,
  72. bool *done);
  73. /* If the SSL backend wants to read or write on this connection during a
  74. handshake, set socks[0] to the connection's FIRSTSOCKET, and return
  75. a bitmap indicating read or write with GETSOCK_WRITESOCK(0) or
  76. GETSOCK_READSOCK(0). Otherwise return GETSOCK_BLANK.
  77. Mandatory. */
  78. int (*getsock)(struct connectdata *conn, curl_socket_t *socks);
  79. void *(*get_internals)(struct ssl_connect_data *connssl, CURLINFO info);
  80. void (*close_one)(struct Curl_easy *data, struct connectdata *conn,
  81. int sockindex);
  82. void (*close_all)(struct Curl_easy *data);
  83. void (*session_free)(void *ptr);
  84. CURLcode (*set_engine)(struct Curl_easy *data, const char *engine);
  85. CURLcode (*set_engine_default)(struct Curl_easy *data);
  86. struct curl_slist *(*engines_list)(struct Curl_easy *data);
  87. bool (*false_start)(void);
  88. CURLcode (*sha256sum)(const unsigned char *input, size_t inputlen,
  89. unsigned char *sha256sum, size_t sha256sumlen);
  90. bool (*associate_connection)(struct Curl_easy *data,
  91. struct connectdata *conn,
  92. int sockindex);
  93. void (*disassociate_connection)(struct Curl_easy *data, int sockindex);
  94. void (*free_multi_ssl_backend_data)(struct multi_ssl_backend_data *mbackend);
  95. };
  96. #ifdef USE_SSL
  97. extern const struct Curl_ssl *Curl_ssl;
  98. #endif
  99. int Curl_none_init(void);
  100. void Curl_none_cleanup(void);
  101. int Curl_none_shutdown(struct Curl_easy *data, struct connectdata *conn,
  102. int sockindex);
  103. int Curl_none_check_cxn(struct connectdata *conn);
  104. CURLcode Curl_none_random(struct Curl_easy *data, unsigned char *entropy,
  105. size_t length);
  106. void Curl_none_close_all(struct Curl_easy *data);
  107. void Curl_none_session_free(void *ptr);
  108. bool Curl_none_data_pending(const struct connectdata *conn, int connindex);
  109. bool Curl_none_cert_status_request(void);
  110. CURLcode Curl_none_set_engine(struct Curl_easy *data, const char *engine);
  111. CURLcode Curl_none_set_engine_default(struct Curl_easy *data);
  112. struct curl_slist *Curl_none_engines_list(struct Curl_easy *data);
  113. bool Curl_none_false_start(void);
  114. bool Curl_ssl_tls13_ciphersuites(void);
  115. CURLsslset Curl_init_sslset_nolock(curl_sslbackend id, const char *name,
  116. const curl_ssl_backend ***avail);
  117. #include "openssl.h" /* OpenSSL versions */
  118. #include "gtls.h" /* GnuTLS versions */
  119. #include "nssg.h" /* NSS versions */
  120. #include "gskit.h" /* Global Secure ToolKit versions */
  121. #include "wolfssl.h" /* wolfSSL versions */
  122. #include "schannel.h" /* Schannel SSPI version */
  123. #include "sectransp.h" /* SecureTransport (Darwin) version */
  124. #include "mbedtls.h" /* mbedTLS versions */
  125. #include "bearssl.h" /* BearSSL versions */
  126. #include "rustls.h" /* rustls versions */
  127. #ifndef MAX_PINNED_PUBKEY_SIZE
  128. #define MAX_PINNED_PUBKEY_SIZE 1048576 /* 1MB */
  129. #endif
  130. #ifndef CURL_SHA256_DIGEST_LENGTH
  131. #define CURL_SHA256_DIGEST_LENGTH 32 /* fixed size */
  132. #endif
  133. /* see https://www.iana.org/assignments/tls-extensiontype-values/ */
  134. #define ALPN_HTTP_1_1_LENGTH 8
  135. #define ALPN_HTTP_1_1 "http/1.1"
  136. #define ALPN_H2_LENGTH 2
  137. #define ALPN_H2 "h2"
  138. /* set of helper macros for the backends to access the correct fields. For the
  139. proxy or for the remote host - to properly support HTTPS proxy */
  140. #ifndef CURL_DISABLE_PROXY
  141. #define SSL_IS_PROXY() \
  142. (CURLPROXY_HTTPS == conn->http_proxy.proxytype && \
  143. ssl_connection_complete != \
  144. conn->proxy_ssl[conn->sock[SECONDARYSOCKET] == \
  145. CURL_SOCKET_BAD ? FIRSTSOCKET : SECONDARYSOCKET].state)
  146. #define SSL_SET_OPTION(var) \
  147. (SSL_IS_PROXY() ? data->set.proxy_ssl.var : data->set.ssl.var)
  148. #define SSL_SET_OPTION_LVALUE(var) \
  149. (*(SSL_IS_PROXY() ? &data->set.proxy_ssl.var : &data->set.ssl.var))
  150. #define SSL_CONN_CONFIG(var) \
  151. (SSL_IS_PROXY() ? conn->proxy_ssl_config.var : conn->ssl_config.var)
  152. #define SSL_HOST_NAME() \
  153. (SSL_IS_PROXY() ? conn->http_proxy.host.name : conn->host.name)
  154. #define SSL_HOST_DISPNAME() \
  155. (SSL_IS_PROXY() ? conn->http_proxy.host.dispname : conn->host.dispname)
  156. #define SSL_HOST_PORT() \
  157. (SSL_IS_PROXY() ? conn->port : conn->remote_port)
  158. #define SSL_PINNED_PUB_KEY() (SSL_IS_PROXY() \
  159. ? data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY] \
  160. : data->set.str[STRING_SSL_PINNEDPUBLICKEY])
  161. #else
  162. #define SSL_IS_PROXY() FALSE
  163. #define SSL_SET_OPTION(var) data->set.ssl.var
  164. #define SSL_SET_OPTION_LVALUE(var) data->set.ssl.var
  165. #define SSL_CONN_CONFIG(var) conn->ssl_config.var
  166. #define SSL_HOST_NAME() conn->host.name
  167. #define SSL_HOST_DISPNAME() conn->host.dispname
  168. #define SSL_HOST_PORT() conn->remote_port
  169. #define SSL_PINNED_PUB_KEY() \
  170. data->set.str[STRING_SSL_PINNEDPUBLICKEY]
  171. #endif
  172. char *Curl_ssl_snihost(struct Curl_easy *data, const char *host, size_t *olen);
  173. bool Curl_ssl_config_matches(struct ssl_primary_config *data,
  174. struct ssl_primary_config *needle);
  175. bool Curl_clone_primary_ssl_config(struct ssl_primary_config *source,
  176. struct ssl_primary_config *dest);
  177. void Curl_free_primary_ssl_config(struct ssl_primary_config *sslc);
  178. /* An implementation of the getsock field of Curl_ssl that relies
  179. on the ssl_connect_state enum. Asks for read or write depending
  180. on whether conn->state is ssl_connect_2_reading or
  181. ssl_connect_2_writing. */
  182. int Curl_ssl_getsock(struct connectdata *conn, curl_socket_t *socks);
  183. curl_sslbackend Curl_ssl_backend(void);
  184. #ifdef USE_SSL
  185. int Curl_ssl_init(void);
  186. void Curl_ssl_cleanup(void);
  187. CURLcode Curl_ssl_connect(struct Curl_easy *data, struct connectdata *conn,
  188. int sockindex);
  189. CURLcode Curl_ssl_connect_nonblocking(struct Curl_easy *data,
  190. struct connectdata *conn,
  191. bool isproxy,
  192. int sockindex,
  193. bool *done);
  194. /* tell the SSL stuff to close down all open information regarding
  195. connections (and thus session ID caching etc) */
  196. void Curl_ssl_close_all(struct Curl_easy *data);
  197. void Curl_ssl_close(struct Curl_easy *data, struct connectdata *conn,
  198. int sockindex);
  199. CURLcode Curl_ssl_shutdown(struct Curl_easy *data, struct connectdata *conn,
  200. int sockindex);
  201. CURLcode Curl_ssl_set_engine(struct Curl_easy *data, const char *engine);
  202. /* Sets engine as default for all SSL operations */
  203. CURLcode Curl_ssl_set_engine_default(struct Curl_easy *data);
  204. struct curl_slist *Curl_ssl_engines_list(struct Curl_easy *data);
  205. /* init the SSL session ID cache */
  206. CURLcode Curl_ssl_initsessions(struct Curl_easy *, size_t);
  207. void Curl_ssl_version(char *buffer, size_t size);
  208. bool Curl_ssl_data_pending(const struct connectdata *conn,
  209. int connindex);
  210. int Curl_ssl_check_cxn(struct connectdata *conn);
  211. /* Certificate information list handling. */
  212. void Curl_ssl_free_certinfo(struct Curl_easy *data);
  213. CURLcode Curl_ssl_init_certinfo(struct Curl_easy *data, int num);
  214. CURLcode Curl_ssl_push_certinfo_len(struct Curl_easy *data, int certnum,
  215. const char *label, const char *value,
  216. size_t valuelen);
  217. CURLcode Curl_ssl_push_certinfo(struct Curl_easy *data, int certnum,
  218. const char *label, const char *value);
  219. /* Functions to be used by SSL library adaptation functions */
  220. /* Lock session cache mutex.
  221. * Call this before calling other Curl_ssl_*session* functions
  222. * Caller should unlock this mutex as soon as possible, as it may block
  223. * other SSL connection from making progress.
  224. * The purpose of explicitly locking SSL session cache data is to allow
  225. * individual SSL engines to manage session lifetime in their specific way.
  226. */
  227. void Curl_ssl_sessionid_lock(struct Curl_easy *data);
  228. /* Unlock session cache mutex */
  229. void Curl_ssl_sessionid_unlock(struct Curl_easy *data);
  230. /* extract a session ID
  231. * Sessionid mutex must be locked (see Curl_ssl_sessionid_lock).
  232. * Caller must make sure that the ownership of returned sessionid object
  233. * is properly taken (e.g. its refcount is incremented
  234. * under sessionid mutex).
  235. */
  236. bool Curl_ssl_getsessionid(struct Curl_easy *data,
  237. struct connectdata *conn,
  238. const bool isProxy,
  239. void **ssl_sessionid,
  240. size_t *idsize, /* set 0 if unknown */
  241. int sockindex);
  242. /* add a new session ID
  243. * Sessionid mutex must be locked (see Curl_ssl_sessionid_lock).
  244. * Caller must ensure that it has properly shared ownership of this sessionid
  245. * object with cache (e.g. incrementing refcount on success)
  246. */
  247. CURLcode Curl_ssl_addsessionid(struct Curl_easy *data,
  248. struct connectdata *conn,
  249. const bool isProxy,
  250. void *ssl_sessionid,
  251. size_t idsize,
  252. int sockindex,
  253. bool *added);
  254. /* Kill a single session ID entry in the cache
  255. * Sessionid mutex must be locked (see Curl_ssl_sessionid_lock).
  256. * This will call engine-specific curlssl_session_free function, which must
  257. * take sessionid object ownership from sessionid cache
  258. * (e.g. decrement refcount).
  259. */
  260. void Curl_ssl_kill_session(struct Curl_ssl_session *session);
  261. /* delete a session from the cache
  262. * Sessionid mutex must be locked (see Curl_ssl_sessionid_lock).
  263. * This will call engine-specific curlssl_session_free function, which must
  264. * take sessionid object ownership from sessionid cache
  265. * (e.g. decrement refcount).
  266. */
  267. void Curl_ssl_delsessionid(struct Curl_easy *data, void *ssl_sessionid);
  268. /* get N random bytes into the buffer */
  269. CURLcode Curl_ssl_random(struct Curl_easy *data, unsigned char *buffer,
  270. size_t length);
  271. /* Check pinned public key. */
  272. CURLcode Curl_pin_peer_pubkey(struct Curl_easy *data,
  273. const char *pinnedpubkey,
  274. const unsigned char *pubkey, size_t pubkeylen);
  275. bool Curl_ssl_cert_status_request(void);
  276. bool Curl_ssl_false_start(void);
  277. void Curl_ssl_associate_conn(struct Curl_easy *data,
  278. struct connectdata *conn);
  279. void Curl_ssl_detach_conn(struct Curl_easy *data,
  280. struct connectdata *conn);
  281. void Curl_free_multi_ssl_backend_data(struct multi_ssl_backend_data *mbackend);
  282. #define SSL_SHUTDOWN_TIMEOUT 10000 /* ms */
  283. #else /* if not USE_SSL */
  284. /* When SSL support is not present, just define away these function calls */
  285. #define Curl_ssl_init() 1
  286. #define Curl_ssl_cleanup() Curl_nop_stmt
  287. #define Curl_ssl_connect(x,y,z) CURLE_NOT_BUILT_IN
  288. #define Curl_ssl_close_all(x) Curl_nop_stmt
  289. #define Curl_ssl_close(x,y,z) Curl_nop_stmt
  290. #define Curl_ssl_shutdown(x,y,z) CURLE_NOT_BUILT_IN
  291. #define Curl_ssl_set_engine(x,y) CURLE_NOT_BUILT_IN
  292. #define Curl_ssl_set_engine_default(x) CURLE_NOT_BUILT_IN
  293. #define Curl_ssl_engines_list(x) NULL
  294. #define Curl_ssl_send(a,b,c,d,e) -1
  295. #define Curl_ssl_recv(a,b,c,d,e) -1
  296. #define Curl_ssl_initsessions(x,y) CURLE_OK
  297. #define Curl_ssl_data_pending(x,y) 0
  298. #define Curl_ssl_check_cxn(x) 0
  299. #define Curl_ssl_free_certinfo(x) Curl_nop_stmt
  300. #define Curl_ssl_connect_nonblocking(x,y,z,w,a) CURLE_NOT_BUILT_IN
  301. #define Curl_ssl_kill_session(x) Curl_nop_stmt
  302. #define Curl_ssl_random(x,y,z) ((void)x, CURLE_NOT_BUILT_IN)
  303. #define Curl_ssl_cert_status_request() FALSE
  304. #define Curl_ssl_false_start() FALSE
  305. #define Curl_ssl_tls13_ciphersuites() FALSE
  306. #define Curl_ssl_associate_conn(a,b) Curl_nop_stmt
  307. #define Curl_ssl_detach_conn(a,b) Curl_nop_stmt
  308. #endif
  309. #endif /* HEADER_CURL_VTLS_H */