vtls.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #ifndef HEADER_CURL_VTLS_H
  2. #define HEADER_CURL_VTLS_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) 1998 - 2019, 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.haxx.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 "polarssl.h" /* PolarSSL versions */
  93. #include "wolfssl.h" /* wolfSSL versions */
  94. #include "schannel.h" /* Schannel SSPI version */
  95. #include "sectransp.h" /* SecureTransport (Darwin) version */
  96. #include "mbedtls.h" /* mbedTLS versions */
  97. #include "mesalink.h" /* MesaLink versions */
  98. #ifndef MAX_PINNED_PUBKEY_SIZE
  99. #define MAX_PINNED_PUBKEY_SIZE 1048576 /* 1MB */
  100. #endif
  101. #ifndef MD5_DIGEST_LENGTH
  102. #ifndef LIBWOLFSSL_VERSION_HEX /* because WolfSSL borks this */
  103. #define MD5_DIGEST_LENGTH 16 /* fixed size */
  104. #endif
  105. #endif
  106. #ifndef CURL_SHA256_DIGEST_LENGTH
  107. #define CURL_SHA256_DIGEST_LENGTH 32 /* fixed size */
  108. #endif
  109. /* see https://tools.ietf.org/html/draft-ietf-tls-applayerprotoneg-04 */
  110. #define ALPN_HTTP_1_1_LENGTH 8
  111. #define ALPN_HTTP_1_1 "http/1.1"
  112. /* set of helper macros for the backends to access the correct fields. For the
  113. proxy or for the remote host - to properly support HTTPS proxy */
  114. #define SSL_IS_PROXY() (CURLPROXY_HTTPS == conn->http_proxy.proxytype && \
  115. ssl_connection_complete != conn->proxy_ssl[conn->sock[SECONDARYSOCKET] == \
  116. CURL_SOCKET_BAD ? FIRSTSOCKET : SECONDARYSOCKET].state)
  117. #define SSL_SET_OPTION(var) (SSL_IS_PROXY() ? data->set.proxy_ssl.var : \
  118. data->set.ssl.var)
  119. #define SSL_CONN_CONFIG(var) (SSL_IS_PROXY() ? \
  120. conn->proxy_ssl_config.var : conn->ssl_config.var)
  121. bool Curl_ssl_config_matches(struct ssl_primary_config* data,
  122. struct ssl_primary_config* needle);
  123. bool Curl_clone_primary_ssl_config(struct ssl_primary_config *source,
  124. struct ssl_primary_config *dest);
  125. void Curl_free_primary_ssl_config(struct ssl_primary_config* sslc);
  126. int Curl_ssl_getsock(struct connectdata *conn, curl_socket_t *socks);
  127. int Curl_ssl_backend(void);
  128. #ifdef USE_SSL
  129. int Curl_ssl_init(void);
  130. void Curl_ssl_cleanup(void);
  131. CURLcode Curl_ssl_connect(struct connectdata *conn, int sockindex);
  132. CURLcode Curl_ssl_connect_nonblocking(struct connectdata *conn,
  133. int sockindex,
  134. bool *done);
  135. /* tell the SSL stuff to close down all open information regarding
  136. connections (and thus session ID caching etc) */
  137. void Curl_ssl_close_all(struct Curl_easy *data);
  138. void Curl_ssl_close(struct connectdata *conn, int sockindex);
  139. CURLcode Curl_ssl_shutdown(struct connectdata *conn, int sockindex);
  140. CURLcode Curl_ssl_set_engine(struct Curl_easy *data, const char *engine);
  141. /* Sets engine as default for all SSL operations */
  142. CURLcode Curl_ssl_set_engine_default(struct Curl_easy *data);
  143. struct curl_slist *Curl_ssl_engines_list(struct Curl_easy *data);
  144. /* init the SSL session ID cache */
  145. CURLcode Curl_ssl_initsessions(struct Curl_easy *, size_t);
  146. size_t Curl_ssl_version(char *buffer, size_t size);
  147. bool Curl_ssl_data_pending(const struct connectdata *conn,
  148. int connindex);
  149. int Curl_ssl_check_cxn(struct connectdata *conn);
  150. /* Certificate information list handling. */
  151. void Curl_ssl_free_certinfo(struct Curl_easy *data);
  152. CURLcode Curl_ssl_init_certinfo(struct Curl_easy *data, int num);
  153. CURLcode Curl_ssl_push_certinfo_len(struct Curl_easy *data, int certnum,
  154. const char *label, const char *value,
  155. size_t valuelen);
  156. CURLcode Curl_ssl_push_certinfo(struct Curl_easy *data, int certnum,
  157. const char *label, const char *value);
  158. /* Functions to be used by SSL library adaptation functions */
  159. /* Lock session cache mutex.
  160. * Call this before calling other Curl_ssl_*session* functions
  161. * Caller should unlock this mutex as soon as possible, as it may block
  162. * other SSL connection from making progress.
  163. * The purpose of explicitly locking SSL session cache data is to allow
  164. * individual SSL engines to manage session lifetime in their specific way.
  165. */
  166. void Curl_ssl_sessionid_lock(struct connectdata *conn);
  167. /* Unlock session cache mutex */
  168. void Curl_ssl_sessionid_unlock(struct connectdata *conn);
  169. /* extract a session ID
  170. * Sessionid mutex must be locked (see Curl_ssl_sessionid_lock).
  171. * Caller must make sure that the ownership of returned sessionid object
  172. * is properly taken (e.g. its refcount is incremented
  173. * under sessionid mutex).
  174. */
  175. bool Curl_ssl_getsessionid(struct connectdata *conn,
  176. void **ssl_sessionid,
  177. size_t *idsize, /* set 0 if unknown */
  178. int sockindex);
  179. /* add a new session ID
  180. * Sessionid mutex must be locked (see Curl_ssl_sessionid_lock).
  181. * Caller must ensure that it has properly shared ownership of this sessionid
  182. * object with cache (e.g. incrementing refcount on success)
  183. */
  184. CURLcode Curl_ssl_addsessionid(struct connectdata *conn,
  185. void *ssl_sessionid,
  186. size_t idsize,
  187. int sockindex);
  188. /* Kill a single session ID entry in the cache
  189. * Sessionid mutex must be locked (see Curl_ssl_sessionid_lock).
  190. * This will call engine-specific curlssl_session_free function, which must
  191. * take sessionid object ownership from sessionid cache
  192. * (e.g. decrement refcount).
  193. */
  194. void Curl_ssl_kill_session(struct curl_ssl_session *session);
  195. /* delete a session from the cache
  196. * Sessionid mutex must be locked (see Curl_ssl_sessionid_lock).
  197. * This will call engine-specific curlssl_session_free function, which must
  198. * take sessionid object ownership from sessionid cache
  199. * (e.g. decrement refcount).
  200. */
  201. void Curl_ssl_delsessionid(struct connectdata *conn, void *ssl_sessionid);
  202. /* get N random bytes into the buffer */
  203. CURLcode Curl_ssl_random(struct Curl_easy *data, unsigned char *buffer,
  204. size_t length);
  205. CURLcode Curl_ssl_md5sum(unsigned char *tmp, /* input */
  206. size_t tmplen,
  207. unsigned char *md5sum, /* output */
  208. size_t md5len);
  209. /* Check pinned public key. */
  210. CURLcode Curl_pin_peer_pubkey(struct Curl_easy *data,
  211. const char *pinnedpubkey,
  212. const unsigned char *pubkey, size_t pubkeylen);
  213. bool Curl_ssl_cert_status_request(void);
  214. bool Curl_ssl_false_start(void);
  215. #define SSL_SHUTDOWN_TIMEOUT 10000 /* ms */
  216. #else /* if not USE_SSL */
  217. /* When SSL support is not present, just define away these function calls */
  218. #define Curl_ssl_init() 1
  219. #define Curl_ssl_cleanup() Curl_nop_stmt
  220. #define Curl_ssl_connect(x,y) CURLE_NOT_BUILT_IN
  221. #define Curl_ssl_close_all(x) Curl_nop_stmt
  222. #define Curl_ssl_close(x,y) Curl_nop_stmt
  223. #define Curl_ssl_shutdown(x,y) CURLE_NOT_BUILT_IN
  224. #define Curl_ssl_set_engine(x,y) CURLE_NOT_BUILT_IN
  225. #define Curl_ssl_set_engine_default(x) CURLE_NOT_BUILT_IN
  226. #define Curl_ssl_engines_list(x) NULL
  227. #define Curl_ssl_send(a,b,c,d,e) -1
  228. #define Curl_ssl_recv(a,b,c,d,e) -1
  229. #define Curl_ssl_initsessions(x,y) CURLE_OK
  230. #define Curl_ssl_version(x,y) 0
  231. #define Curl_ssl_data_pending(x,y) 0
  232. #define Curl_ssl_check_cxn(x) 0
  233. #define Curl_ssl_free_certinfo(x) Curl_nop_stmt
  234. #define Curl_ssl_connect_nonblocking(x,y,z) CURLE_NOT_BUILT_IN
  235. #define Curl_ssl_kill_session(x) Curl_nop_stmt
  236. #define Curl_ssl_random(x,y,z) ((void)x, CURLE_NOT_BUILT_IN)
  237. #define Curl_ssl_cert_status_request() FALSE
  238. #define Curl_ssl_false_start() FALSE
  239. #define Curl_ssl_tls13_ciphersuites() FALSE
  240. #endif
  241. #endif /* HEADER_CURL_VTLS_H */