xref: /curl/lib/vtls/vtls_int.h (revision c31041b1)
1 #ifndef HEADER_CURL_VTLS_INT_H
2 #define HEADER_CURL_VTLS_INT_H
3 /***************************************************************************
4  *                                  _   _ ____  _
5  *  Project                     ___| | | |  _ \| |
6  *                             / __| | | | |_) | |
7  *                            | (__| |_| |  _ <| |___
8  *                             \___|\___/|_| \_\_____|
9  *
10  * Copyright (C) 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 #include "cfilters.h"
28 #include "urldata.h"
29 
30 #ifdef USE_SSL
31 
32 /* see https://www.iana.org/assignments/tls-extensiontype-values/ */
33 #define ALPN_HTTP_1_1_LENGTH 8
34 #define ALPN_HTTP_1_1 "http/1.1"
35 #define ALPN_H2_LENGTH 2
36 #define ALPN_H2 "h2"
37 #define ALPN_H3_LENGTH 2
38 #define ALPN_H3 "h3"
39 
40 /* conservative sizes on the ALPN entries and count we are handling,
41  * we can increase these if we ever feel the need or have to accommodate
42  * ALPN strings from the "outside". */
43 #define ALPN_NAME_MAX     10
44 #define ALPN_ENTRIES_MAX  3
45 #define ALPN_PROTO_BUF_MAX   (ALPN_ENTRIES_MAX * (ALPN_NAME_MAX + 1))
46 
47 struct alpn_spec {
48   const char entries[ALPN_ENTRIES_MAX][ALPN_NAME_MAX];
49   size_t count; /* number of entries */
50 };
51 
52 struct alpn_proto_buf {
53   unsigned char data[ALPN_PROTO_BUF_MAX];
54   int len;
55 };
56 
57 CURLcode Curl_alpn_to_proto_buf(struct alpn_proto_buf *buf,
58                                 const struct alpn_spec *spec);
59 CURLcode Curl_alpn_to_proto_str(struct alpn_proto_buf *buf,
60                                 const struct alpn_spec *spec);
61 
62 CURLcode Curl_alpn_set_negotiated(struct Curl_cfilter *cf,
63                                   struct Curl_easy *data,
64                                   const unsigned char *proto,
65                                   size_t proto_len);
66 
67 /* enum for the nonblocking SSL connection state machine */
68 typedef enum {
69   ssl_connect_1,
70   ssl_connect_2,
71   ssl_connect_3,
72   ssl_connect_done
73 } ssl_connect_state;
74 
75 typedef enum {
76   ssl_connection_none,
77   ssl_connection_negotiating,
78   ssl_connection_complete
79 } ssl_connection_state;
80 
81 #define CURL_SSL_IO_NEED_NONE   (0)
82 #define CURL_SSL_IO_NEED_RECV   (1<<0)
83 #define CURL_SSL_IO_NEED_SEND   (1<<1)
84 
85 /* Information in each SSL cfilter context: cf->ctx */
86 struct ssl_connect_data {
87   struct ssl_peer peer;
88   const struct alpn_spec *alpn;     /* ALPN to use or NULL for none */
89   void *backend;                    /* vtls backend specific props */
90   struct cf_call_data call_data;    /* data handle used in current call */
91   struct curltime handshake_done;   /* time when handshake finished */
92   ssl_connection_state state;
93   ssl_connect_state connecting_state;
94   int io_need;                      /* TLS signals special SEND/RECV needs */
95   BIT(use_alpn);                    /* if ALPN shall be used in handshake */
96   BIT(peer_closed);                 /* peer has closed connection */
97   BIT(shutdown);                    /* graceful close notify finished */
98 };
99 
100 
101 #undef CF_CTX_CALL_DATA
102 #define CF_CTX_CALL_DATA(cf)  \
103   ((struct ssl_connect_data *)(cf)->ctx)->call_data
104 
105 
106 /* Definitions for SSL Implementations */
107 
108 struct Curl_ssl {
109   /*
110    * This *must* be the first entry to allow returning the list of available
111    * backends in curl_global_sslset().
112    */
113   curl_ssl_backend info;
114   unsigned int supports; /* bitfield, see above */
115   size_t sizeof_ssl_backend_data;
116 
117   int (*init)(void);
118   void (*cleanup)(void);
119 
120   size_t (*version)(char *buffer, size_t size);
121   int (*check_cxn)(struct Curl_cfilter *cf, struct Curl_easy *data);
122   CURLcode (*shut_down)(struct Curl_cfilter *cf, struct Curl_easy *data,
123                         bool send_shutdown, bool *done);
124   bool (*data_pending)(struct Curl_cfilter *cf,
125                        const struct Curl_easy *data);
126 
127   /* return 0 if a find random is filled in */
128   CURLcode (*random)(struct Curl_easy *data, unsigned char *entropy,
129                      size_t length);
130   bool (*cert_status_request)(void);
131 
132   CURLcode (*connect_blocking)(struct Curl_cfilter *cf,
133                                struct Curl_easy *data);
134   CURLcode (*connect_nonblocking)(struct Curl_cfilter *cf,
135                                   struct Curl_easy *data,
136                                   bool *done);
137 
138   /* During handshake/shutdown, adjust the pollset to include the socket
139    * for POLLOUT or POLLIN as needed. Mandatory. */
140   void (*adjust_pollset)(struct Curl_cfilter *cf, struct Curl_easy *data,
141                           struct easy_pollset *ps);
142   void *(*get_internals)(struct ssl_connect_data *connssl, CURLINFO info);
143   void (*close)(struct Curl_cfilter *cf, struct Curl_easy *data);
144   void (*close_all)(struct Curl_easy *data);
145 
146   CURLcode (*set_engine)(struct Curl_easy *data, const char *engine);
147   CURLcode (*set_engine_default)(struct Curl_easy *data);
148   struct curl_slist *(*engines_list)(struct Curl_easy *data);
149 
150   bool (*false_start)(void);
151   CURLcode (*sha256sum)(const unsigned char *input, size_t inputlen,
152                     unsigned char *sha256sum, size_t sha256sumlen);
153 
154   bool (*attach_data)(struct Curl_cfilter *cf, struct Curl_easy *data);
155   void (*detach_data)(struct Curl_cfilter *cf, struct Curl_easy *data);
156 
157   ssize_t (*recv_plain)(struct Curl_cfilter *cf, struct Curl_easy *data,
158                         char *buf, size_t len, CURLcode *code);
159   ssize_t (*send_plain)(struct Curl_cfilter *cf, struct Curl_easy *data,
160                         const void *mem, size_t len, CURLcode *code);
161 
162 };
163 
164 extern const struct Curl_ssl *Curl_ssl;
165 
166 
167 int Curl_none_init(void);
168 void Curl_none_cleanup(void);
169 CURLcode Curl_none_shutdown(struct Curl_cfilter *cf, struct Curl_easy *data,
170                             bool send_shutdown, bool *done);
171 int Curl_none_check_cxn(struct Curl_cfilter *cf, struct Curl_easy *data);
172 CURLcode Curl_none_random(struct Curl_easy *data, unsigned char *entropy,
173                           size_t length);
174 void Curl_none_close_all(struct Curl_easy *data);
175 void Curl_none_session_free(void *ptr);
176 bool Curl_none_data_pending(struct Curl_cfilter *cf,
177                             const struct Curl_easy *data);
178 bool Curl_none_cert_status_request(void);
179 CURLcode Curl_none_set_engine(struct Curl_easy *data, const char *engine);
180 CURLcode Curl_none_set_engine_default(struct Curl_easy *data);
181 struct curl_slist *Curl_none_engines_list(struct Curl_easy *data);
182 bool Curl_none_false_start(void);
183 void Curl_ssl_adjust_pollset(struct Curl_cfilter *cf, struct Curl_easy *data,
184                               struct easy_pollset *ps);
185 
186 /**
187  * Get the SSL filter below the given one or NULL if there is none.
188  */
189 bool Curl_ssl_cf_is_proxy(struct Curl_cfilter *cf);
190 
191 /* extract a session ID
192  * Sessionid mutex must be locked (see Curl_ssl_sessionid_lock).
193  * Caller must make sure that the ownership of returned sessionid object
194  * is properly taken (e.g. its refcount is incremented
195  * under sessionid mutex).
196  */
197 bool Curl_ssl_getsessionid(struct Curl_cfilter *cf,
198                            struct Curl_easy *data,
199                            const struct ssl_peer *peer,
200                            void **ssl_sessionid,
201                            size_t *idsize); /* set 0 if unknown */
202 /* add a new session ID
203  * Sessionid mutex must be locked (see Curl_ssl_sessionid_lock).
204  * Caller must ensure that it has properly shared ownership of this sessionid
205  * object with cache (e.g. incrementing refcount on success)
206  * Call takes ownership of `ssl_sessionid`, using `sessionid_free_cb`
207  * to destroy it in case of failure or later removal.
208  */
209 CURLcode Curl_ssl_addsessionid(struct Curl_cfilter *cf,
210                                struct Curl_easy *data,
211                                const struct ssl_peer *peer,
212                                void *ssl_sessionid,
213                                size_t idsize,
214                                Curl_ssl_sessionid_dtor *sessionid_free_cb);
215 
216 #include "openssl.h"        /* OpenSSL versions */
217 #include "gtls.h"           /* GnuTLS versions */
218 #include "wolfssl.h"        /* wolfSSL versions */
219 #include "schannel.h"       /* Schannel SSPI version */
220 #include "sectransp.h"      /* SecureTransport (Darwin) version */
221 #include "mbedtls.h"        /* mbedTLS versions */
222 #include "bearssl.h"        /* BearSSL versions */
223 #include "rustls.h"         /* rustls versions */
224 
225 #endif /* USE_SSL */
226 
227 #endif /* HEADER_CURL_VTLS_INT_H */
228