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