vtls.h 15 KB

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