xref: /curl/lib/vtls/vtls_int.h (revision fa0ccd9f)
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 #include "vtls.h"
30 
31 #ifdef USE_SSL
32 
33 struct Curl_ssl;
34 struct ssl_connect_data;
35 
36 /* see https://www.iana.org/assignments/tls-extensiontype-values/ */
37 #define ALPN_HTTP_1_1_LENGTH 8
38 #define ALPN_HTTP_1_1 "http/1.1"
39 #define ALPN_H2_LENGTH 2
40 #define ALPN_H2 "h2"
41 #define ALPN_H3_LENGTH 2
42 #define ALPN_H3 "h3"
43 
44 /* conservative sizes on the ALPN entries and count we are handling,
45  * we can increase these if we ever feel the need or have to accommodate
46  * ALPN strings from the "outside". */
47 #define ALPN_NAME_MAX     10
48 #define ALPN_ENTRIES_MAX  3
49 #define ALPN_PROTO_BUF_MAX   (ALPN_ENTRIES_MAX * (ALPN_NAME_MAX + 1))
50 
51 struct alpn_spec {
52   const char entries[ALPN_ENTRIES_MAX][ALPN_NAME_MAX];
53   size_t count; /* number of entries */
54 };
55 
56 struct alpn_proto_buf {
57   unsigned char data[ALPN_PROTO_BUF_MAX];
58   int len;
59 };
60 
61 CURLcode Curl_alpn_to_proto_buf(struct alpn_proto_buf *buf,
62                                 const struct alpn_spec *spec);
63 CURLcode Curl_alpn_to_proto_str(struct alpn_proto_buf *buf,
64                                 const struct alpn_spec *spec);
65 
66 CURLcode Curl_alpn_set_negotiated(struct Curl_cfilter *cf,
67                                   struct Curl_easy *data,
68                                   struct ssl_connect_data *connssl,
69                                   const unsigned char *proto,
70                                   size_t proto_len);
71 
72 bool Curl_alpn_contains_proto(const struct alpn_spec *spec,
73                               const char *proto);
74 
75 /* enum for the nonblocking SSL connection state machine */
76 typedef enum {
77   ssl_connect_1,
78   ssl_connect_2,
79   ssl_connect_3,
80   ssl_connect_done
81 } ssl_connect_state;
82 
83 typedef enum {
84   ssl_connection_none,
85   ssl_connection_deferred,
86   ssl_connection_negotiating,
87   ssl_connection_complete
88 } ssl_connection_state;
89 
90 typedef enum {
91   ssl_earlydata_none,
92   ssl_earlydata_use,
93   ssl_earlydata_sending,
94   ssl_earlydata_sent,
95   ssl_earlydata_accepted,
96   ssl_earlydata_rejected
97 } ssl_earlydata_state;
98 
99 #define CURL_SSL_IO_NEED_NONE   (0)
100 #define CURL_SSL_IO_NEED_RECV   (1<<0)
101 #define CURL_SSL_IO_NEED_SEND   (1<<1)
102 
103 /* Max earlydata payload we want to send */
104 #define CURL_SSL_EARLY_MAX       (64*1024)
105 
106 /* Information in each SSL cfilter context: cf->ctx */
107 struct ssl_connect_data {
108   const struct Curl_ssl *ssl_impl;  /* TLS backend for this filter */
109   struct ssl_peer peer;             /* peer the filter talks to */
110   const struct alpn_spec *alpn;     /* ALPN to use or NULL for none */
111   void *backend;                    /* vtls backend specific props */
112   struct cf_call_data call_data;    /* data handle used in current call */
113   struct curltime handshake_done;   /* time when handshake finished */
114   struct {
115     char *alpn;                     /* ALPN value or NULL */
116   } negotiated;
117   struct bufq earlydata;            /* earlydata to be send to peer */
118   size_t earlydata_max;             /* max earlydata allowed by peer */
119   size_t earlydata_skip;            /* sending bytes to skip when earlydata
120                                      * is accepted by peer */
121   ssl_connection_state state;
122   ssl_connect_state connecting_state;
123   ssl_earlydata_state earlydata_state;
124   int io_need;                      /* TLS signals special SEND/RECV needs */
125   BIT(use_alpn);                    /* if ALPN shall be used in handshake */
126   BIT(peer_closed);                 /* peer has closed connection */
127 };
128 
129 
130 #undef CF_CTX_CALL_DATA
131 #define CF_CTX_CALL_DATA(cf)  \
132   ((struct ssl_connect_data *)(cf)->ctx)->call_data
133 
134 
135 /* Definitions for SSL Implementations */
136 
137 struct Curl_ssl {
138   /*
139    * This *must* be the first entry to allow returning the list of available
140    * backends in curl_global_sslset().
141    */
142   curl_ssl_backend info;
143   unsigned int supports; /* bitfield, see above */
144   size_t sizeof_ssl_backend_data;
145 
146   int (*init)(void);
147   void (*cleanup)(void);
148 
149   size_t (*version)(char *buffer, size_t size);
150   CURLcode (*shut_down)(struct Curl_cfilter *cf, struct Curl_easy *data,
151                         bool send_shutdown, bool *done);
152   bool (*data_pending)(struct Curl_cfilter *cf,
153                        const struct Curl_easy *data);
154 
155   /* return 0 if a find random is filled in */
156   CURLcode (*random)(struct Curl_easy *data, unsigned char *entropy,
157                      size_t length);
158   bool (*cert_status_request)(void);
159 
160   CURLcode (*connect_blocking)(struct Curl_cfilter *cf,
161                                struct Curl_easy *data);
162   CURLcode (*connect_nonblocking)(struct Curl_cfilter *cf,
163                                   struct Curl_easy *data,
164                                   bool *done);
165 
166   /* During handshake/shutdown, adjust the pollset to include the socket
167    * for POLLOUT or POLLIN as needed. Mandatory. */
168   void (*adjust_pollset)(struct Curl_cfilter *cf, struct Curl_easy *data,
169                           struct easy_pollset *ps);
170   void *(*get_internals)(struct ssl_connect_data *connssl, CURLINFO info);
171   void (*close)(struct Curl_cfilter *cf, struct Curl_easy *data);
172   void (*close_all)(struct Curl_easy *data);
173 
174   CURLcode (*set_engine)(struct Curl_easy *data, const char *engine);
175   CURLcode (*set_engine_default)(struct Curl_easy *data);
176   struct curl_slist *(*engines_list)(struct Curl_easy *data);
177 
178   bool (*false_start)(void);
179   CURLcode (*sha256sum)(const unsigned char *input, size_t inputlen,
180                     unsigned char *sha256sum, size_t sha256sumlen);
181   ssize_t (*recv_plain)(struct Curl_cfilter *cf, struct Curl_easy *data,
182                         char *buf, size_t len, CURLcode *code);
183   ssize_t (*send_plain)(struct Curl_cfilter *cf, struct Curl_easy *data,
184                         const void *mem, size_t len, CURLcode *code);
185 
186   CURLcode (*get_channel_binding)(struct Curl_easy *data, int sockindex,
187                                   struct dynbuf *binding);
188 
189 };
190 
191 extern const struct Curl_ssl *Curl_ssl;
192 
193 void Curl_ssl_adjust_pollset(struct Curl_cfilter *cf, struct Curl_easy *data,
194                              struct easy_pollset *ps);
195 
196 /**
197  * Get the SSL filter below the given one or NULL if there is none.
198  */
199 bool Curl_ssl_cf_is_proxy(struct Curl_cfilter *cf);
200 
201 #endif /* USE_SSL */
202 
203 #endif /* HEADER_CURL_VTLS_INT_H */
204