vtls.h 14 KB

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