vtls.h 13 KB

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