/src/PROJ/curl/lib/vtls/vtls.c
Line | Count | Source (jump to first uncovered line) |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | |
25 | | /* This file is for implementing all "generic" SSL functions that all libcurl |
26 | | internals should use. It is then responsible for calling the proper |
27 | | "backend" function. |
28 | | |
29 | | SSL-functions in libcurl should call functions in this source file, and not |
30 | | to any specific SSL-layer. |
31 | | |
32 | | Curl_ssl_ - prefix for generic ones |
33 | | |
34 | | Note that this source code uses the functions of the configured SSL |
35 | | backend via the global Curl_ssl instance. |
36 | | |
37 | | "SSL/TLS Strong Encryption: An Introduction" |
38 | | https://httpd.apache.org/docs/2.0/ssl/ssl_intro.html |
39 | | */ |
40 | | |
41 | | #include "../curl_setup.h" |
42 | | |
43 | | #ifdef HAVE_SYS_TYPES_H |
44 | | #include <sys/types.h> |
45 | | #endif |
46 | | #ifdef HAVE_FCNTL_H |
47 | | #include <fcntl.h> |
48 | | #endif |
49 | | |
50 | | #include "../urldata.h" |
51 | | #include "../cfilters.h" |
52 | | |
53 | | #include "vtls.h" /* generic SSL protos etc */ |
54 | | #include "vtls_int.h" |
55 | | #include "vtls_scache.h" |
56 | | |
57 | | #include "openssl.h" /* OpenSSL versions */ |
58 | | #include "gtls.h" /* GnuTLS versions */ |
59 | | #include "wolfssl.h" /* wolfSSL versions */ |
60 | | #include "schannel.h" /* Schannel SSPI version */ |
61 | | #include "mbedtls.h" /* mbedTLS versions */ |
62 | | #include "rustls.h" /* Rustls versions */ |
63 | | |
64 | | #include "../slist.h" |
65 | | #include "../sendf.h" |
66 | | #include "../strcase.h" |
67 | | #include "../url.h" |
68 | | #include "../progress.h" |
69 | | #include "../share.h" |
70 | | #include "../multiif.h" |
71 | | #include "../curlx/timeval.h" |
72 | | #include "../curl_md5.h" |
73 | | #include "../curl_sha256.h" |
74 | | #include "../curlx/warnless.h" |
75 | | #include "../curlx/base64.h" |
76 | | #include "../curl_printf.h" |
77 | | #include "../curlx/inet_pton.h" |
78 | | #include "../connect.h" |
79 | | #include "../select.h" |
80 | | #include "../strdup.h" |
81 | | #include "../rand.h" |
82 | | |
83 | | /* The last #include files should be: */ |
84 | | #include "../curl_memory.h" |
85 | | #include "../memdebug.h" |
86 | | |
87 | | |
88 | | #define CLONE_STRING(var) \ |
89 | 0 | do { \ |
90 | 0 | if(source->var) { \ |
91 | 0 | dest->var = strdup(source->var); \ |
92 | 0 | if(!dest->var) \ |
93 | 0 | return FALSE; \ |
94 | 0 | } \ |
95 | 0 | else \ |
96 | 0 | dest->var = NULL; \ |
97 | 0 | } while(0) |
98 | | |
99 | | #define CLONE_BLOB(var) \ |
100 | 0 | do { \ |
101 | 0 | if(blobdup(&dest->var, source->var)) \ |
102 | 0 | return FALSE; \ |
103 | 0 | } while(0) |
104 | | |
105 | | static CURLcode blobdup(struct curl_blob **dest, |
106 | | struct curl_blob *src) |
107 | 0 | { |
108 | 0 | DEBUGASSERT(dest); |
109 | 0 | DEBUGASSERT(!*dest); |
110 | 0 | if(src) { |
111 | | /* only if there is data to dupe! */ |
112 | 0 | struct curl_blob *d; |
113 | 0 | d = malloc(sizeof(struct curl_blob) + src->len); |
114 | 0 | if(!d) |
115 | 0 | return CURLE_OUT_OF_MEMORY; |
116 | 0 | d->len = src->len; |
117 | | /* Always duplicate because the connection may survive longer than the |
118 | | handle that passed in the blob. */ |
119 | 0 | d->flags = CURL_BLOB_COPY; |
120 | 0 | d->data = (void *)((char *)d + sizeof(struct curl_blob)); |
121 | 0 | memcpy(d->data, src->data, src->len); |
122 | 0 | *dest = d; |
123 | 0 | } |
124 | 0 | return CURLE_OK; |
125 | 0 | } |
126 | | |
127 | | /* returns TRUE if the blobs are identical */ |
128 | | static bool blobcmp(struct curl_blob *first, struct curl_blob *second) |
129 | 0 | { |
130 | 0 | if(!first && !second) /* both are NULL */ |
131 | 0 | return TRUE; |
132 | 0 | if(!first || !second) /* one is NULL */ |
133 | 0 | return FALSE; |
134 | 0 | if(first->len != second->len) /* different sizes */ |
135 | 0 | return FALSE; |
136 | 0 | return !memcmp(first->data, second->data, first->len); /* same data */ |
137 | 0 | } |
138 | | |
139 | | #ifdef USE_SSL |
140 | | static const struct alpn_spec ALPN_SPEC_H11 = { |
141 | | { ALPN_HTTP_1_1 }, 1 |
142 | | }; |
143 | | #ifdef USE_HTTP2 |
144 | | static const struct alpn_spec ALPN_SPEC_H2 = { |
145 | | { ALPN_H2 }, 1 |
146 | | }; |
147 | | static const struct alpn_spec ALPN_SPEC_H2_H11 = { |
148 | | { ALPN_H2, ALPN_HTTP_1_1 }, 2 |
149 | | }; |
150 | | #endif |
151 | | |
152 | | static const struct alpn_spec * |
153 | | alpn_get_spec(http_majors allowed, bool use_alpn) |
154 | 0 | { |
155 | 0 | if(!use_alpn) |
156 | 0 | return NULL; |
157 | | #ifdef USE_HTTP2 |
158 | | if(allowed & CURL_HTTP_V2x) { |
159 | | if(allowed & CURL_HTTP_V1x) |
160 | | return &ALPN_SPEC_H2_H11; |
161 | | return &ALPN_SPEC_H2; |
162 | | } |
163 | | #else |
164 | 0 | (void)allowed; |
165 | 0 | #endif |
166 | | /* Use the ALPN protocol "http/1.1" for HTTP/1.x. |
167 | | Avoid "http/1.0" because some servers do not support it. */ |
168 | 0 | return &ALPN_SPEC_H11; |
169 | 0 | } |
170 | | #endif /* USE_SSL */ |
171 | | |
172 | | |
173 | | void Curl_ssl_easy_config_init(struct Curl_easy *data) |
174 | 0 | { |
175 | | /* |
176 | | * libcurl 7.10 introduced SSL verification *by default*! This needs to be |
177 | | * switched off unless wanted. |
178 | | */ |
179 | 0 | data->set.ssl.primary.verifypeer = TRUE; |
180 | 0 | data->set.ssl.primary.verifyhost = TRUE; |
181 | 0 | data->set.ssl.primary.cache_session = TRUE; /* caching by default */ |
182 | 0 | #ifndef CURL_DISABLE_PROXY |
183 | 0 | data->set.proxy_ssl = data->set.ssl; |
184 | 0 | #endif |
185 | 0 | } |
186 | | |
187 | | static bool |
188 | | match_ssl_primary_config(struct Curl_easy *data, |
189 | | struct ssl_primary_config *c1, |
190 | | struct ssl_primary_config *c2) |
191 | 0 | { |
192 | 0 | (void)data; |
193 | 0 | if((c1->version == c2->version) && |
194 | 0 | (c1->version_max == c2->version_max) && |
195 | 0 | (c1->ssl_options == c2->ssl_options) && |
196 | 0 | (c1->verifypeer == c2->verifypeer) && |
197 | 0 | (c1->verifyhost == c2->verifyhost) && |
198 | 0 | (c1->verifystatus == c2->verifystatus) && |
199 | 0 | blobcmp(c1->cert_blob, c2->cert_blob) && |
200 | 0 | blobcmp(c1->ca_info_blob, c2->ca_info_blob) && |
201 | 0 | blobcmp(c1->issuercert_blob, c2->issuercert_blob) && |
202 | 0 | Curl_safecmp(c1->CApath, c2->CApath) && |
203 | 0 | Curl_safecmp(c1->CAfile, c2->CAfile) && |
204 | 0 | Curl_safecmp(c1->issuercert, c2->issuercert) && |
205 | 0 | Curl_safecmp(c1->clientcert, c2->clientcert) && |
206 | 0 | #ifdef USE_TLS_SRP |
207 | 0 | !Curl_timestrcmp(c1->username, c2->username) && |
208 | 0 | !Curl_timestrcmp(c1->password, c2->password) && |
209 | 0 | #endif |
210 | 0 | curl_strequal(c1->cipher_list, c2->cipher_list) && |
211 | 0 | curl_strequal(c1->cipher_list13, c2->cipher_list13) && |
212 | 0 | curl_strequal(c1->curves, c2->curves) && |
213 | 0 | curl_strequal(c1->signature_algorithms, c2->signature_algorithms) && |
214 | 0 | curl_strequal(c1->CRLfile, c2->CRLfile) && |
215 | 0 | curl_strequal(c1->pinned_key, c2->pinned_key)) |
216 | 0 | return TRUE; |
217 | | |
218 | 0 | return FALSE; |
219 | 0 | } |
220 | | |
221 | | bool Curl_ssl_conn_config_match(struct Curl_easy *data, |
222 | | struct connectdata *candidate, |
223 | | bool proxy) |
224 | 0 | { |
225 | 0 | #ifndef CURL_DISABLE_PROXY |
226 | 0 | if(proxy) |
227 | 0 | return match_ssl_primary_config(data, &data->set.proxy_ssl.primary, |
228 | 0 | &candidate->proxy_ssl_config); |
229 | | #else |
230 | | (void)proxy; |
231 | | #endif |
232 | 0 | return match_ssl_primary_config(data, &data->set.ssl.primary, |
233 | 0 | &candidate->ssl_config); |
234 | 0 | } |
235 | | |
236 | | static bool clone_ssl_primary_config(struct ssl_primary_config *source, |
237 | | struct ssl_primary_config *dest) |
238 | 0 | { |
239 | 0 | dest->version = source->version; |
240 | 0 | dest->version_max = source->version_max; |
241 | 0 | dest->verifypeer = source->verifypeer; |
242 | 0 | dest->verifyhost = source->verifyhost; |
243 | 0 | dest->verifystatus = source->verifystatus; |
244 | 0 | dest->cache_session = source->cache_session; |
245 | 0 | dest->ssl_options = source->ssl_options; |
246 | |
|
247 | 0 | CLONE_BLOB(cert_blob); |
248 | 0 | CLONE_BLOB(ca_info_blob); |
249 | 0 | CLONE_BLOB(issuercert_blob); |
250 | 0 | CLONE_STRING(CApath); |
251 | 0 | CLONE_STRING(CAfile); |
252 | 0 | CLONE_STRING(issuercert); |
253 | 0 | CLONE_STRING(clientcert); |
254 | 0 | CLONE_STRING(cipher_list); |
255 | 0 | CLONE_STRING(cipher_list13); |
256 | 0 | CLONE_STRING(pinned_key); |
257 | 0 | CLONE_STRING(curves); |
258 | 0 | CLONE_STRING(signature_algorithms); |
259 | 0 | CLONE_STRING(CRLfile); |
260 | 0 | #ifdef USE_TLS_SRP |
261 | 0 | CLONE_STRING(username); |
262 | 0 | CLONE_STRING(password); |
263 | 0 | #endif |
264 | | |
265 | 0 | return TRUE; |
266 | 0 | } |
267 | | |
268 | | static void free_primary_ssl_config(struct ssl_primary_config *sslc) |
269 | 0 | { |
270 | 0 | Curl_safefree(sslc->CApath); |
271 | 0 | Curl_safefree(sslc->CAfile); |
272 | 0 | Curl_safefree(sslc->issuercert); |
273 | 0 | Curl_safefree(sslc->clientcert); |
274 | 0 | Curl_safefree(sslc->cipher_list); |
275 | 0 | Curl_safefree(sslc->cipher_list13); |
276 | 0 | Curl_safefree(sslc->pinned_key); |
277 | 0 | Curl_safefree(sslc->cert_blob); |
278 | 0 | Curl_safefree(sslc->ca_info_blob); |
279 | 0 | Curl_safefree(sslc->issuercert_blob); |
280 | 0 | Curl_safefree(sslc->curves); |
281 | 0 | Curl_safefree(sslc->signature_algorithms); |
282 | 0 | Curl_safefree(sslc->CRLfile); |
283 | 0 | #ifdef USE_TLS_SRP |
284 | 0 | Curl_safefree(sslc->username); |
285 | 0 | Curl_safefree(sslc->password); |
286 | 0 | #endif |
287 | 0 | } |
288 | | |
289 | | CURLcode Curl_ssl_easy_config_complete(struct Curl_easy *data) |
290 | 0 | { |
291 | 0 | data->set.ssl.primary.CApath = data->set.str[STRING_SSL_CAPATH]; |
292 | 0 | data->set.ssl.primary.CAfile = data->set.str[STRING_SSL_CAFILE]; |
293 | 0 | data->set.ssl.primary.CRLfile = data->set.str[STRING_SSL_CRLFILE]; |
294 | 0 | data->set.ssl.primary.issuercert = data->set.str[STRING_SSL_ISSUERCERT]; |
295 | 0 | data->set.ssl.primary.issuercert_blob = data->set.blobs[BLOB_SSL_ISSUERCERT]; |
296 | 0 | data->set.ssl.primary.cipher_list = |
297 | 0 | data->set.str[STRING_SSL_CIPHER_LIST]; |
298 | 0 | data->set.ssl.primary.cipher_list13 = |
299 | 0 | data->set.str[STRING_SSL_CIPHER13_LIST]; |
300 | 0 | data->set.ssl.primary.signature_algorithms = |
301 | 0 | data->set.str[STRING_SSL_SIGNATURE_ALGORITHMS]; |
302 | 0 | data->set.ssl.primary.pinned_key = |
303 | 0 | data->set.str[STRING_SSL_PINNEDPUBLICKEY]; |
304 | 0 | data->set.ssl.primary.cert_blob = data->set.blobs[BLOB_CERT]; |
305 | 0 | data->set.ssl.primary.ca_info_blob = data->set.blobs[BLOB_CAINFO]; |
306 | 0 | data->set.ssl.primary.curves = data->set.str[STRING_SSL_EC_CURVES]; |
307 | 0 | #ifdef USE_TLS_SRP |
308 | 0 | data->set.ssl.primary.username = data->set.str[STRING_TLSAUTH_USERNAME]; |
309 | 0 | data->set.ssl.primary.password = data->set.str[STRING_TLSAUTH_PASSWORD]; |
310 | 0 | #endif |
311 | 0 | data->set.ssl.cert_type = data->set.str[STRING_CERT_TYPE]; |
312 | 0 | data->set.ssl.key = data->set.str[STRING_KEY]; |
313 | 0 | data->set.ssl.key_type = data->set.str[STRING_KEY_TYPE]; |
314 | 0 | data->set.ssl.key_passwd = data->set.str[STRING_KEY_PASSWD]; |
315 | 0 | data->set.ssl.primary.clientcert = data->set.str[STRING_CERT]; |
316 | 0 | data->set.ssl.key_blob = data->set.blobs[BLOB_KEY]; |
317 | |
|
318 | 0 | #ifndef CURL_DISABLE_PROXY |
319 | 0 | data->set.proxy_ssl.primary.CApath = data->set.str[STRING_SSL_CAPATH_PROXY]; |
320 | 0 | data->set.proxy_ssl.primary.CAfile = data->set.str[STRING_SSL_CAFILE_PROXY]; |
321 | 0 | data->set.proxy_ssl.primary.cipher_list = |
322 | 0 | data->set.str[STRING_SSL_CIPHER_LIST_PROXY]; |
323 | 0 | data->set.proxy_ssl.primary.cipher_list13 = |
324 | 0 | data->set.str[STRING_SSL_CIPHER13_LIST_PROXY]; |
325 | 0 | data->set.proxy_ssl.primary.pinned_key = |
326 | 0 | data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY]; |
327 | 0 | data->set.proxy_ssl.primary.cert_blob = data->set.blobs[BLOB_CERT_PROXY]; |
328 | 0 | data->set.proxy_ssl.primary.ca_info_blob = |
329 | 0 | data->set.blobs[BLOB_CAINFO_PROXY]; |
330 | 0 | data->set.proxy_ssl.primary.issuercert = |
331 | 0 | data->set.str[STRING_SSL_ISSUERCERT_PROXY]; |
332 | 0 | data->set.proxy_ssl.primary.issuercert_blob = |
333 | 0 | data->set.blobs[BLOB_SSL_ISSUERCERT_PROXY]; |
334 | 0 | data->set.proxy_ssl.primary.CRLfile = |
335 | 0 | data->set.str[STRING_SSL_CRLFILE_PROXY]; |
336 | 0 | data->set.proxy_ssl.cert_type = data->set.str[STRING_CERT_TYPE_PROXY]; |
337 | 0 | data->set.proxy_ssl.key = data->set.str[STRING_KEY_PROXY]; |
338 | 0 | data->set.proxy_ssl.key_type = data->set.str[STRING_KEY_TYPE_PROXY]; |
339 | 0 | data->set.proxy_ssl.key_passwd = data->set.str[STRING_KEY_PASSWD_PROXY]; |
340 | 0 | data->set.proxy_ssl.primary.clientcert = data->set.str[STRING_CERT_PROXY]; |
341 | 0 | data->set.proxy_ssl.key_blob = data->set.blobs[BLOB_KEY_PROXY]; |
342 | 0 | #ifdef USE_TLS_SRP |
343 | 0 | data->set.proxy_ssl.primary.username = |
344 | 0 | data->set.str[STRING_TLSAUTH_USERNAME_PROXY]; |
345 | 0 | data->set.proxy_ssl.primary.password = |
346 | 0 | data->set.str[STRING_TLSAUTH_PASSWORD_PROXY]; |
347 | 0 | #endif |
348 | 0 | #endif /* CURL_DISABLE_PROXY */ |
349 | |
|
350 | 0 | return CURLE_OK; |
351 | 0 | } |
352 | | |
353 | | CURLcode Curl_ssl_conn_config_init(struct Curl_easy *data, |
354 | | struct connectdata *conn) |
355 | 0 | { |
356 | | /* Clone "primary" SSL configurations from the esay handle to |
357 | | * the connection. They are used for connection cache matching and |
358 | | * probably outlive the easy handle */ |
359 | 0 | if(!clone_ssl_primary_config(&data->set.ssl.primary, &conn->ssl_config)) |
360 | 0 | return CURLE_OUT_OF_MEMORY; |
361 | 0 | #ifndef CURL_DISABLE_PROXY |
362 | 0 | if(!clone_ssl_primary_config(&data->set.proxy_ssl.primary, |
363 | 0 | &conn->proxy_ssl_config)) |
364 | 0 | return CURLE_OUT_OF_MEMORY; |
365 | 0 | #endif |
366 | 0 | return CURLE_OK; |
367 | 0 | } |
368 | | |
369 | | void Curl_ssl_conn_config_cleanup(struct connectdata *conn) |
370 | 0 | { |
371 | 0 | free_primary_ssl_config(&conn->ssl_config); |
372 | 0 | #ifndef CURL_DISABLE_PROXY |
373 | 0 | free_primary_ssl_config(&conn->proxy_ssl_config); |
374 | 0 | #endif |
375 | 0 | } |
376 | | |
377 | | void Curl_ssl_conn_config_update(struct Curl_easy *data, bool for_proxy) |
378 | 0 | { |
379 | | /* May be called on an easy that has no connection yet */ |
380 | 0 | if(data->conn) { |
381 | 0 | struct ssl_primary_config *src, *dest; |
382 | 0 | #ifndef CURL_DISABLE_PROXY |
383 | 0 | src = for_proxy ? &data->set.proxy_ssl.primary : &data->set.ssl.primary; |
384 | 0 | dest = for_proxy ? &data->conn->proxy_ssl_config : &data->conn->ssl_config; |
385 | | #else |
386 | | (void)for_proxy; |
387 | | src = &data->set.ssl.primary; |
388 | | dest = &data->conn->ssl_config; |
389 | | #endif |
390 | 0 | dest->verifyhost = src->verifyhost; |
391 | 0 | dest->verifypeer = src->verifypeer; |
392 | 0 | dest->verifystatus = src->verifystatus; |
393 | 0 | } |
394 | 0 | } |
395 | | |
396 | | #ifdef USE_SSL |
397 | | static int multissl_setup(const struct Curl_ssl *backend); |
398 | | #endif |
399 | | |
400 | | curl_sslbackend Curl_ssl_backend(void) |
401 | 0 | { |
402 | 0 | #ifdef USE_SSL |
403 | 0 | multissl_setup(NULL); |
404 | 0 | return Curl_ssl->info.id; |
405 | | #else |
406 | | return CURLSSLBACKEND_NONE; |
407 | | #endif |
408 | 0 | } |
409 | | |
410 | | #ifdef USE_SSL |
411 | | |
412 | | /* "global" init done? */ |
413 | | static bool init_ssl = FALSE; |
414 | | |
415 | | /** |
416 | | * Global SSL init |
417 | | * |
418 | | * @retval 0 error initializing SSL |
419 | | * @retval 1 SSL initialized successfully |
420 | | */ |
421 | | int Curl_ssl_init(void) |
422 | 0 | { |
423 | | /* make sure this is only done once */ |
424 | 0 | if(init_ssl) |
425 | 0 | return 1; |
426 | 0 | init_ssl = TRUE; /* never again */ |
427 | |
|
428 | 0 | if(Curl_ssl->init) |
429 | 0 | return Curl_ssl->init(); |
430 | 0 | return 1; |
431 | 0 | } |
432 | | |
433 | | static bool ssl_prefs_check(struct Curl_easy *data) |
434 | 0 | { |
435 | | /* check for CURLOPT_SSLVERSION invalid parameter value */ |
436 | 0 | const unsigned char sslver = data->set.ssl.primary.version; |
437 | 0 | if(sslver >= CURL_SSLVERSION_LAST) { |
438 | 0 | failf(data, "Unrecognized parameter value passed via CURLOPT_SSLVERSION"); |
439 | 0 | return FALSE; |
440 | 0 | } |
441 | | |
442 | 0 | switch(data->set.ssl.primary.version_max) { |
443 | 0 | case CURL_SSLVERSION_MAX_NONE: |
444 | 0 | case CURL_SSLVERSION_MAX_DEFAULT: |
445 | 0 | break; |
446 | | |
447 | 0 | default: |
448 | 0 | if((data->set.ssl.primary.version_max >> 16) < sslver) { |
449 | 0 | failf(data, "CURL_SSLVERSION_MAX incompatible with CURL_SSLVERSION"); |
450 | 0 | return FALSE; |
451 | 0 | } |
452 | 0 | } |
453 | | |
454 | 0 | return TRUE; |
455 | 0 | } |
456 | | |
457 | | static struct ssl_connect_data *cf_ctx_new(struct Curl_easy *data, |
458 | | const struct alpn_spec *alpn) |
459 | 0 | { |
460 | 0 | struct ssl_connect_data *ctx; |
461 | |
|
462 | 0 | (void)data; |
463 | 0 | ctx = calloc(1, sizeof(*ctx)); |
464 | 0 | if(!ctx) |
465 | 0 | return NULL; |
466 | | |
467 | 0 | ctx->ssl_impl = Curl_ssl; |
468 | 0 | ctx->alpn = alpn; |
469 | 0 | Curl_bufq_init2(&ctx->earlydata, CURL_SSL_EARLY_MAX, 1, BUFQ_OPT_NO_SPARES); |
470 | 0 | ctx->backend = calloc(1, ctx->ssl_impl->sizeof_ssl_backend_data); |
471 | 0 | if(!ctx->backend) { |
472 | 0 | free(ctx); |
473 | 0 | return NULL; |
474 | 0 | } |
475 | 0 | return ctx; |
476 | 0 | } |
477 | | |
478 | | static void cf_ctx_free(struct ssl_connect_data *ctx) |
479 | 0 | { |
480 | 0 | if(ctx) { |
481 | 0 | Curl_safefree(ctx->negotiated.alpn); |
482 | 0 | Curl_bufq_free(&ctx->earlydata); |
483 | 0 | free(ctx->backend); |
484 | 0 | free(ctx); |
485 | 0 | } |
486 | 0 | } |
487 | | |
488 | | CURLcode Curl_ssl_get_channel_binding(struct Curl_easy *data, int sockindex, |
489 | | struct dynbuf *binding) |
490 | 0 | { |
491 | 0 | if(Curl_ssl->get_channel_binding) |
492 | 0 | return Curl_ssl->get_channel_binding(data, sockindex, binding); |
493 | 0 | return CURLE_OK; |
494 | 0 | } |
495 | | |
496 | | void Curl_ssl_close_all(struct Curl_easy *data) |
497 | 0 | { |
498 | 0 | if(Curl_ssl->close_all) |
499 | 0 | Curl_ssl->close_all(data); |
500 | 0 | } |
501 | | |
502 | | void Curl_ssl_adjust_pollset(struct Curl_cfilter *cf, struct Curl_easy *data, |
503 | | struct easy_pollset *ps) |
504 | 0 | { |
505 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
506 | |
|
507 | 0 | if(connssl->io_need) { |
508 | 0 | curl_socket_t sock = Curl_conn_cf_get_socket(cf->next, data); |
509 | 0 | if(sock != CURL_SOCKET_BAD) { |
510 | 0 | if(connssl->io_need & CURL_SSL_IO_NEED_SEND) { |
511 | 0 | Curl_pollset_set_out_only(data, ps, sock); |
512 | 0 | CURL_TRC_CF(data, cf, "adjust_pollset, POLLOUT fd=%" FMT_SOCKET_T, |
513 | 0 | sock); |
514 | 0 | } |
515 | 0 | else { |
516 | 0 | Curl_pollset_set_in_only(data, ps, sock); |
517 | 0 | CURL_TRC_CF(data, cf, "adjust_pollset, POLLIN fd=%" FMT_SOCKET_T, |
518 | 0 | sock); |
519 | 0 | } |
520 | 0 | } |
521 | 0 | } |
522 | 0 | } |
523 | | |
524 | | /* Selects an SSL crypto engine |
525 | | */ |
526 | | CURLcode Curl_ssl_set_engine(struct Curl_easy *data, const char *engine) |
527 | 0 | { |
528 | 0 | if(Curl_ssl->set_engine) |
529 | 0 | return Curl_ssl->set_engine(data, engine); |
530 | 0 | return CURLE_NOT_BUILT_IN; |
531 | 0 | } |
532 | | |
533 | | /* Selects the default SSL crypto engine |
534 | | */ |
535 | | CURLcode Curl_ssl_set_engine_default(struct Curl_easy *data) |
536 | 0 | { |
537 | 0 | if(Curl_ssl->set_engine_default) |
538 | 0 | return Curl_ssl->set_engine_default(data); |
539 | 0 | return CURLE_NOT_BUILT_IN; |
540 | 0 | } |
541 | | |
542 | | /* Return list of OpenSSL crypto engine names. */ |
543 | | struct curl_slist *Curl_ssl_engines_list(struct Curl_easy *data) |
544 | 0 | { |
545 | 0 | if(Curl_ssl->engines_list) |
546 | 0 | return Curl_ssl->engines_list(data); |
547 | 0 | return NULL; |
548 | 0 | } |
549 | | |
550 | | static size_t multissl_version(char *buffer, size_t size); |
551 | | |
552 | | void Curl_ssl_version(char *buffer, size_t size) |
553 | 0 | { |
554 | | #ifdef CURL_WITH_MULTI_SSL |
555 | | (void)multissl_version(buffer, size); |
556 | | #else |
557 | 0 | (void)Curl_ssl->version(buffer, size); |
558 | 0 | #endif |
559 | 0 | } |
560 | | |
561 | | void Curl_ssl_free_certinfo(struct Curl_easy *data) |
562 | 0 | { |
563 | 0 | struct curl_certinfo *ci = &data->info.certs; |
564 | |
|
565 | 0 | if(ci->num_of_certs) { |
566 | | /* free all individual lists used */ |
567 | 0 | int i; |
568 | 0 | for(i = 0; i < ci->num_of_certs; i++) { |
569 | 0 | curl_slist_free_all(ci->certinfo[i]); |
570 | 0 | ci->certinfo[i] = NULL; |
571 | 0 | } |
572 | |
|
573 | 0 | free(ci->certinfo); /* free the actual array too */ |
574 | 0 | ci->certinfo = NULL; |
575 | 0 | ci->num_of_certs = 0; |
576 | 0 | } |
577 | 0 | } |
578 | | |
579 | | CURLcode Curl_ssl_init_certinfo(struct Curl_easy *data, int num) |
580 | 0 | { |
581 | 0 | struct curl_certinfo *ci = &data->info.certs; |
582 | 0 | struct curl_slist **table; |
583 | | |
584 | | /* Free any previous certificate information structures */ |
585 | 0 | Curl_ssl_free_certinfo(data); |
586 | | |
587 | | /* Allocate the required certificate information structures */ |
588 | 0 | table = calloc((size_t) num, sizeof(struct curl_slist *)); |
589 | 0 | if(!table) |
590 | 0 | return CURLE_OUT_OF_MEMORY; |
591 | | |
592 | 0 | ci->num_of_certs = num; |
593 | 0 | ci->certinfo = table; |
594 | |
|
595 | 0 | return CURLE_OK; |
596 | 0 | } |
597 | | |
598 | | /* |
599 | | * 'value' is NOT a null-terminated string |
600 | | */ |
601 | | CURLcode Curl_ssl_push_certinfo_len(struct Curl_easy *data, |
602 | | int certnum, |
603 | | const char *label, |
604 | | const char *value, |
605 | | size_t valuelen) |
606 | 0 | { |
607 | 0 | struct curl_certinfo *ci = &data->info.certs; |
608 | 0 | struct curl_slist *nl; |
609 | 0 | CURLcode result = CURLE_OK; |
610 | 0 | struct dynbuf build; |
611 | |
|
612 | 0 | DEBUGASSERT(certnum < ci->num_of_certs); |
613 | |
|
614 | 0 | curlx_dyn_init(&build, CURL_X509_STR_MAX); |
615 | |
|
616 | 0 | if(curlx_dyn_add(&build, label) || |
617 | 0 | curlx_dyn_addn(&build, ":", 1) || |
618 | 0 | curlx_dyn_addn(&build, value, valuelen)) |
619 | 0 | return CURLE_OUT_OF_MEMORY; |
620 | | |
621 | 0 | nl = Curl_slist_append_nodup(ci->certinfo[certnum], |
622 | 0 | curlx_dyn_ptr(&build)); |
623 | 0 | if(!nl) { |
624 | 0 | curlx_dyn_free(&build); |
625 | 0 | curl_slist_free_all(ci->certinfo[certnum]); |
626 | 0 | result = CURLE_OUT_OF_MEMORY; |
627 | 0 | } |
628 | |
|
629 | 0 | ci->certinfo[certnum] = nl; |
630 | 0 | return result; |
631 | 0 | } |
632 | | |
633 | | /* get length bytes of randomness */ |
634 | | CURLcode Curl_ssl_random(struct Curl_easy *data, |
635 | | unsigned char *entropy, |
636 | | size_t length) |
637 | 0 | { |
638 | 0 | DEBUGASSERT(length == sizeof(int)); |
639 | 0 | if(Curl_ssl->random) |
640 | 0 | return Curl_ssl->random(data, entropy, length); |
641 | 0 | else |
642 | 0 | return CURLE_NOT_BUILT_IN; |
643 | 0 | } |
644 | | |
645 | | /* |
646 | | * Public key pem to der conversion |
647 | | */ |
648 | | |
649 | | static CURLcode pubkey_pem_to_der(const char *pem, |
650 | | unsigned char **der, size_t *der_len) |
651 | 0 | { |
652 | 0 | char *begin_pos, *end_pos; |
653 | 0 | size_t pem_count, pem_len; |
654 | 0 | CURLcode result; |
655 | 0 | struct dynbuf pbuf; |
656 | | |
657 | | /* if no pem, exit. */ |
658 | 0 | if(!pem) |
659 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
660 | | |
661 | 0 | curlx_dyn_init(&pbuf, MAX_PINNED_PUBKEY_SIZE); |
662 | |
|
663 | 0 | begin_pos = strstr(pem, "-----BEGIN PUBLIC KEY-----"); |
664 | 0 | if(!begin_pos) |
665 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
666 | | |
667 | 0 | pem_count = begin_pos - pem; |
668 | | /* Invalid if not at beginning AND not directly following \n */ |
669 | 0 | if(0 != pem_count && '\n' != pem[pem_count - 1]) |
670 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
671 | | |
672 | | /* 26 is length of "-----BEGIN PUBLIC KEY-----" */ |
673 | 0 | pem_count += 26; |
674 | | |
675 | | /* Invalid if not directly following \n */ |
676 | 0 | end_pos = strstr(pem + pem_count, "\n-----END PUBLIC KEY-----"); |
677 | 0 | if(!end_pos) |
678 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
679 | | |
680 | 0 | pem_len = end_pos - pem; |
681 | | |
682 | | /* |
683 | | * Here we loop through the pem array one character at a time between the |
684 | | * correct indices, and place each character that is not '\n' or '\r' |
685 | | * into the stripped_pem array, which should represent the raw base64 string |
686 | | */ |
687 | 0 | while(pem_count < pem_len) { |
688 | 0 | if('\n' != pem[pem_count] && '\r' != pem[pem_count]) { |
689 | 0 | result = curlx_dyn_addn(&pbuf, &pem[pem_count], 1); |
690 | 0 | if(result) |
691 | 0 | return result; |
692 | 0 | } |
693 | 0 | ++pem_count; |
694 | 0 | } |
695 | | |
696 | 0 | if(curlx_dyn_len(&pbuf)) { |
697 | 0 | result = curlx_base64_decode(curlx_dyn_ptr(&pbuf), der, der_len); |
698 | 0 | curlx_dyn_free(&pbuf); |
699 | 0 | } |
700 | 0 | else |
701 | 0 | result = CURLE_BAD_CONTENT_ENCODING; |
702 | |
|
703 | 0 | return result; |
704 | 0 | } |
705 | | |
706 | | /* |
707 | | * Generic pinned public key check. |
708 | | */ |
709 | | |
710 | | CURLcode Curl_pin_peer_pubkey(struct Curl_easy *data, |
711 | | const char *pinnedpubkey, |
712 | | const unsigned char *pubkey, size_t pubkeylen) |
713 | 0 | { |
714 | 0 | CURLcode result = CURLE_SSL_PINNEDPUBKEYNOTMATCH; |
715 | | #ifdef CURL_DISABLE_VERBOSE_STRINGS |
716 | | (void)data; |
717 | | #endif |
718 | | |
719 | | /* if a path was not specified, do not pin */ |
720 | 0 | if(!pinnedpubkey) |
721 | 0 | return CURLE_OK; |
722 | 0 | if(!pubkey || !pubkeylen) |
723 | 0 | return result; |
724 | | |
725 | | /* only do this if pinnedpubkey starts with "sha256//", length 8 */ |
726 | 0 | if(!strncmp(pinnedpubkey, "sha256//", 8)) { |
727 | 0 | CURLcode encode; |
728 | 0 | size_t encodedlen = 0; |
729 | 0 | char *encoded = NULL, *pinkeycopy, *begin_pos, *end_pos; |
730 | 0 | unsigned char *sha256sumdigest; |
731 | |
|
732 | 0 | if(!Curl_ssl->sha256sum) { |
733 | | /* without sha256 support, this cannot match */ |
734 | 0 | return result; |
735 | 0 | } |
736 | | |
737 | | /* compute sha256sum of public key */ |
738 | 0 | sha256sumdigest = malloc(CURL_SHA256_DIGEST_LENGTH); |
739 | 0 | if(!sha256sumdigest) |
740 | 0 | return CURLE_OUT_OF_MEMORY; |
741 | 0 | encode = Curl_ssl->sha256sum(pubkey, pubkeylen, |
742 | 0 | sha256sumdigest, CURL_SHA256_DIGEST_LENGTH); |
743 | |
|
744 | 0 | if(!encode) |
745 | 0 | encode = curlx_base64_encode((char *)sha256sumdigest, |
746 | 0 | CURL_SHA256_DIGEST_LENGTH, &encoded, |
747 | 0 | &encodedlen); |
748 | 0 | Curl_safefree(sha256sumdigest); |
749 | |
|
750 | 0 | if(encode) |
751 | 0 | return encode; |
752 | | |
753 | 0 | infof(data, " public key hash: sha256//%s", encoded); |
754 | | |
755 | | /* it starts with sha256//, copy so we can modify it */ |
756 | 0 | pinkeycopy = strdup(pinnedpubkey); |
757 | 0 | if(!pinkeycopy) { |
758 | 0 | Curl_safefree(encoded); |
759 | 0 | return CURLE_OUT_OF_MEMORY; |
760 | 0 | } |
761 | | /* point begin_pos to the copy, and start extracting keys */ |
762 | 0 | begin_pos = pinkeycopy; |
763 | 0 | do { |
764 | 0 | end_pos = strstr(begin_pos, ";sha256//"); |
765 | | /* |
766 | | * if there is an end_pos, null-terminate, otherwise it will go to the |
767 | | * end of the original string |
768 | | */ |
769 | 0 | if(end_pos) |
770 | 0 | end_pos[0] = '\0'; |
771 | | |
772 | | /* compare base64 sha256 digests, 8 is the length of "sha256//" */ |
773 | 0 | if(encodedlen == strlen(begin_pos + 8) && |
774 | 0 | !memcmp(encoded, begin_pos + 8, encodedlen)) { |
775 | 0 | result = CURLE_OK; |
776 | 0 | break; |
777 | 0 | } |
778 | | |
779 | | /* |
780 | | * change back the null-terminator we changed earlier, |
781 | | * and look for next begin |
782 | | */ |
783 | 0 | if(end_pos) { |
784 | 0 | end_pos[0] = ';'; |
785 | 0 | begin_pos = strstr(end_pos, "sha256//"); |
786 | 0 | } |
787 | 0 | } while(end_pos && begin_pos); |
788 | 0 | Curl_safefree(encoded); |
789 | 0 | Curl_safefree(pinkeycopy); |
790 | 0 | } |
791 | 0 | else { |
792 | 0 | long filesize; |
793 | 0 | size_t size, pem_len; |
794 | 0 | CURLcode pem_read; |
795 | 0 | struct dynbuf buf; |
796 | 0 | char unsigned *pem_ptr = NULL; |
797 | 0 | size_t left; |
798 | 0 | FILE *fp = fopen(pinnedpubkey, "rb"); |
799 | 0 | if(!fp) |
800 | 0 | return result; |
801 | | |
802 | 0 | curlx_dyn_init(&buf, MAX_PINNED_PUBKEY_SIZE); |
803 | | |
804 | | /* Determine the file's size */ |
805 | 0 | if(fseek(fp, 0, SEEK_END)) |
806 | 0 | goto end; |
807 | 0 | filesize = ftell(fp); |
808 | 0 | if(fseek(fp, 0, SEEK_SET)) |
809 | 0 | goto end; |
810 | 0 | if(filesize < 0 || filesize > MAX_PINNED_PUBKEY_SIZE) |
811 | 0 | goto end; |
812 | | |
813 | | /* |
814 | | * if the size of our certificate is bigger than the file |
815 | | * size then it cannot match |
816 | | */ |
817 | 0 | size = curlx_sotouz((curl_off_t) filesize); |
818 | 0 | if(pubkeylen > size) |
819 | 0 | goto end; |
820 | | |
821 | | /* |
822 | | * Read the file into the dynbuf |
823 | | */ |
824 | 0 | left = size; |
825 | 0 | do { |
826 | 0 | char buffer[1024]; |
827 | 0 | size_t want = left > sizeof(buffer) ? sizeof(buffer) : left; |
828 | 0 | if(want != fread(buffer, 1, want, fp)) |
829 | 0 | goto end; |
830 | 0 | if(curlx_dyn_addn(&buf, buffer, want)) |
831 | 0 | goto end; |
832 | 0 | left -= want; |
833 | 0 | } while(left); |
834 | | |
835 | | /* If the sizes are the same, it cannot be base64 encoded, must be der */ |
836 | 0 | if(pubkeylen == size) { |
837 | 0 | if(!memcmp(pubkey, curlx_dyn_ptr(&buf), pubkeylen)) |
838 | 0 | result = CURLE_OK; |
839 | 0 | goto end; |
840 | 0 | } |
841 | | |
842 | | /* |
843 | | * Otherwise we will assume it is PEM and try to decode it after placing |
844 | | * null-terminator |
845 | | */ |
846 | 0 | pem_read = pubkey_pem_to_der(curlx_dyn_ptr(&buf), &pem_ptr, &pem_len); |
847 | | /* if it was not read successfully, exit */ |
848 | 0 | if(pem_read) |
849 | 0 | goto end; |
850 | | |
851 | | /* |
852 | | * if the size of our certificate does not match the size of |
853 | | * the decoded file, they cannot be the same, otherwise compare |
854 | | */ |
855 | 0 | if(pubkeylen == pem_len && !memcmp(pubkey, pem_ptr, pubkeylen)) |
856 | 0 | result = CURLE_OK; |
857 | 0 | end: |
858 | 0 | curlx_dyn_free(&buf); |
859 | 0 | Curl_safefree(pem_ptr); |
860 | 0 | fclose(fp); |
861 | 0 | } |
862 | | |
863 | 0 | return result; |
864 | 0 | } |
865 | | |
866 | | /* |
867 | | * Check whether the SSL backend supports the status_request extension. |
868 | | */ |
869 | | bool Curl_ssl_cert_status_request(void) |
870 | 0 | { |
871 | 0 | if(Curl_ssl->cert_status_request) |
872 | 0 | return Curl_ssl->cert_status_request(); |
873 | 0 | return FALSE; |
874 | 0 | } |
875 | | |
876 | | static int multissl_init(void) |
877 | 0 | { |
878 | 0 | if(multissl_setup(NULL)) |
879 | 0 | return 1; |
880 | 0 | if(Curl_ssl->init) |
881 | 0 | return Curl_ssl->init(); |
882 | 0 | return 1; |
883 | 0 | } |
884 | | |
885 | | static CURLcode multissl_connect(struct Curl_cfilter *cf, |
886 | | struct Curl_easy *data, bool *done) |
887 | 0 | { |
888 | 0 | if(multissl_setup(NULL)) |
889 | 0 | return CURLE_FAILED_INIT; |
890 | 0 | return Curl_ssl->do_connect(cf, data, done); |
891 | 0 | } |
892 | | |
893 | | static void multissl_adjust_pollset(struct Curl_cfilter *cf, |
894 | | struct Curl_easy *data, |
895 | | struct easy_pollset *ps) |
896 | 0 | { |
897 | 0 | if(multissl_setup(NULL)) |
898 | 0 | return; |
899 | 0 | Curl_ssl->adjust_pollset(cf, data, ps); |
900 | 0 | } |
901 | | |
902 | | static void *multissl_get_internals(struct ssl_connect_data *connssl, |
903 | | CURLINFO info) |
904 | 0 | { |
905 | 0 | if(multissl_setup(NULL)) |
906 | 0 | return NULL; |
907 | 0 | return Curl_ssl->get_internals(connssl, info); |
908 | 0 | } |
909 | | |
910 | | static void multissl_close(struct Curl_cfilter *cf, struct Curl_easy *data) |
911 | 0 | { |
912 | 0 | if(multissl_setup(NULL)) |
913 | 0 | return; |
914 | 0 | Curl_ssl->close(cf, data); |
915 | 0 | } |
916 | | |
917 | | static CURLcode multissl_recv_plain(struct Curl_cfilter *cf, |
918 | | struct Curl_easy *data, |
919 | | char *buf, size_t len, size_t *pnread) |
920 | 0 | { |
921 | 0 | if(multissl_setup(NULL)) |
922 | 0 | return CURLE_FAILED_INIT; |
923 | 0 | return Curl_ssl->recv_plain(cf, data, buf, len, pnread); |
924 | 0 | } |
925 | | |
926 | | static CURLcode multissl_send_plain(struct Curl_cfilter *cf, |
927 | | struct Curl_easy *data, |
928 | | const void *mem, size_t len, |
929 | | size_t *pnwritten) |
930 | 0 | { |
931 | 0 | if(multissl_setup(NULL)) |
932 | 0 | return CURLE_FAILED_INIT; |
933 | 0 | return Curl_ssl->send_plain(cf, data, mem, len, pnwritten); |
934 | 0 | } |
935 | | |
936 | | static const struct Curl_ssl Curl_ssl_multi = { |
937 | | { CURLSSLBACKEND_NONE, "multi" }, /* info */ |
938 | | 0, /* supports nothing */ |
939 | | (size_t)-1, /* something insanely large to be on the safe side */ |
940 | | |
941 | | multissl_init, /* init */ |
942 | | NULL, /* cleanup */ |
943 | | multissl_version, /* version */ |
944 | | NULL, /* shutdown */ |
945 | | NULL, /* data_pending */ |
946 | | NULL, /* random */ |
947 | | NULL, /* cert_status_request */ |
948 | | multissl_connect, /* connect */ |
949 | | multissl_adjust_pollset, /* adjust_pollset */ |
950 | | multissl_get_internals, /* get_internals */ |
951 | | multissl_close, /* close_one */ |
952 | | NULL, /* close_all */ |
953 | | NULL, /* set_engine */ |
954 | | NULL, /* set_engine_default */ |
955 | | NULL, /* engines_list */ |
956 | | NULL, /* sha256sum */ |
957 | | multissl_recv_plain, /* recv decrypted data */ |
958 | | multissl_send_plain, /* send data to encrypt */ |
959 | | NULL, /* get_channel_binding */ |
960 | | }; |
961 | | |
962 | | const struct Curl_ssl *Curl_ssl = |
963 | | #if defined(CURL_WITH_MULTI_SSL) |
964 | | &Curl_ssl_multi; |
965 | | #elif defined(USE_WOLFSSL) |
966 | | &Curl_ssl_wolfssl; |
967 | | #elif defined(USE_GNUTLS) |
968 | | &Curl_ssl_gnutls; |
969 | | #elif defined(USE_MBEDTLS) |
970 | | &Curl_ssl_mbedtls; |
971 | | #elif defined(USE_RUSTLS) |
972 | | &Curl_ssl_rustls; |
973 | | #elif defined(USE_OPENSSL) |
974 | | &Curl_ssl_openssl; |
975 | | #elif defined(USE_SCHANNEL) |
976 | | &Curl_ssl_schannel; |
977 | | #else |
978 | | #error "Missing struct Curl_ssl for selected SSL backend" |
979 | | #endif |
980 | | |
981 | | static const struct Curl_ssl *available_backends[] = { |
982 | | #if defined(USE_WOLFSSL) |
983 | | &Curl_ssl_wolfssl, |
984 | | #endif |
985 | | #if defined(USE_GNUTLS) |
986 | | &Curl_ssl_gnutls, |
987 | | #endif |
988 | | #if defined(USE_MBEDTLS) |
989 | | &Curl_ssl_mbedtls, |
990 | | #endif |
991 | | #if defined(USE_OPENSSL) |
992 | | &Curl_ssl_openssl, |
993 | | #endif |
994 | | #if defined(USE_SCHANNEL) |
995 | | &Curl_ssl_schannel, |
996 | | #endif |
997 | | #if defined(USE_RUSTLS) |
998 | | &Curl_ssl_rustls, |
999 | | #endif |
1000 | | NULL |
1001 | | }; |
1002 | | |
1003 | | /* Global cleanup */ |
1004 | | void Curl_ssl_cleanup(void) |
1005 | 0 | { |
1006 | 0 | if(init_ssl) { |
1007 | | /* only cleanup if we did a previous init */ |
1008 | 0 | if(Curl_ssl->cleanup) |
1009 | 0 | Curl_ssl->cleanup(); |
1010 | | #if defined(CURL_WITH_MULTI_SSL) |
1011 | | Curl_ssl = &Curl_ssl_multi; |
1012 | | #endif |
1013 | 0 | init_ssl = FALSE; |
1014 | 0 | } |
1015 | 0 | } |
1016 | | |
1017 | | static size_t multissl_version(char *buffer, size_t size) |
1018 | 0 | { |
1019 | 0 | static const struct Curl_ssl *selected; |
1020 | 0 | static char backends[200]; |
1021 | 0 | static size_t backends_len; |
1022 | 0 | const struct Curl_ssl *current; |
1023 | |
|
1024 | 0 | current = Curl_ssl == &Curl_ssl_multi ? available_backends[0] : Curl_ssl; |
1025 | |
|
1026 | 0 | if(current != selected) { |
1027 | 0 | char *p = backends; |
1028 | 0 | char *end = backends + sizeof(backends); |
1029 | 0 | int i; |
1030 | |
|
1031 | 0 | selected = current; |
1032 | |
|
1033 | 0 | backends[0] = '\0'; |
1034 | |
|
1035 | 0 | for(i = 0; available_backends[i]; ++i) { |
1036 | 0 | char vb[200]; |
1037 | 0 | bool paren = (selected != available_backends[i]); |
1038 | |
|
1039 | 0 | if(available_backends[i]->version(vb, sizeof(vb))) { |
1040 | 0 | p += msnprintf(p, end - p, "%s%s%s%s", (p != backends ? " " : ""), |
1041 | 0 | (paren ? "(" : ""), vb, (paren ? ")" : "")); |
1042 | 0 | } |
1043 | 0 | } |
1044 | |
|
1045 | 0 | backends_len = p - backends; |
1046 | 0 | } |
1047 | |
|
1048 | 0 | if(size) { |
1049 | 0 | if(backends_len < size) |
1050 | 0 | strcpy(buffer, backends); |
1051 | 0 | else |
1052 | 0 | *buffer = 0; /* did not fit */ |
1053 | 0 | } |
1054 | 0 | return 0; |
1055 | 0 | } |
1056 | | |
1057 | | static int multissl_setup(const struct Curl_ssl *backend) |
1058 | 0 | { |
1059 | 0 | int i; |
1060 | 0 | char *env; |
1061 | |
|
1062 | 0 | if(Curl_ssl != &Curl_ssl_multi) |
1063 | 0 | return 1; |
1064 | | |
1065 | 0 | if(backend) { |
1066 | 0 | Curl_ssl = backend; |
1067 | 0 | return 0; |
1068 | 0 | } |
1069 | | |
1070 | 0 | if(!available_backends[0]) |
1071 | 0 | return 1; |
1072 | | |
1073 | 0 | env = curl_getenv("CURL_SSL_BACKEND"); |
1074 | 0 | if(env) { |
1075 | 0 | for(i = 0; available_backends[i]; i++) { |
1076 | 0 | if(curl_strequal(env, available_backends[i]->info.name)) { |
1077 | 0 | Curl_ssl = available_backends[i]; |
1078 | 0 | free(env); |
1079 | 0 | return 0; |
1080 | 0 | } |
1081 | 0 | } |
1082 | 0 | } |
1083 | | |
1084 | | #ifdef CURL_DEFAULT_SSL_BACKEND |
1085 | | for(i = 0; available_backends[i]; i++) { |
1086 | | if(curl_strequal(CURL_DEFAULT_SSL_BACKEND, |
1087 | | available_backends[i]->info.name)) { |
1088 | | Curl_ssl = available_backends[i]; |
1089 | | free(env); |
1090 | | return 0; |
1091 | | } |
1092 | | } |
1093 | | #endif |
1094 | | |
1095 | | /* Fall back to first available backend */ |
1096 | 0 | Curl_ssl = available_backends[0]; |
1097 | 0 | free(env); |
1098 | 0 | return 0; |
1099 | 0 | } |
1100 | | |
1101 | | /* This function is used to select the SSL backend to use. It is called by |
1102 | | curl_global_sslset (easy.c) which uses the global init lock. */ |
1103 | | CURLsslset Curl_init_sslset_nolock(curl_sslbackend id, const char *name, |
1104 | | const curl_ssl_backend ***avail) |
1105 | 0 | { |
1106 | 0 | int i; |
1107 | |
|
1108 | 0 | if(avail) |
1109 | 0 | *avail = (const curl_ssl_backend **)&available_backends; |
1110 | |
|
1111 | 0 | if(Curl_ssl != &Curl_ssl_multi) |
1112 | 0 | return id == Curl_ssl->info.id || |
1113 | 0 | (name && curl_strequal(name, Curl_ssl->info.name)) ? |
1114 | 0 | CURLSSLSET_OK : |
1115 | | #if defined(CURL_WITH_MULTI_SSL) |
1116 | | CURLSSLSET_TOO_LATE; |
1117 | | #else |
1118 | 0 | CURLSSLSET_UNKNOWN_BACKEND; |
1119 | 0 | #endif |
1120 | | |
1121 | 0 | for(i = 0; available_backends[i]; i++) { |
1122 | 0 | if(available_backends[i]->info.id == id || |
1123 | 0 | (name && curl_strequal(available_backends[i]->info.name, name))) { |
1124 | 0 | multissl_setup(available_backends[i]); |
1125 | 0 | return CURLSSLSET_OK; |
1126 | 0 | } |
1127 | 0 | } |
1128 | | |
1129 | 0 | return CURLSSLSET_UNKNOWN_BACKEND; |
1130 | 0 | } |
1131 | | |
1132 | | #else /* USE_SSL */ |
1133 | | CURLsslset Curl_init_sslset_nolock(curl_sslbackend id, const char *name, |
1134 | | const curl_ssl_backend ***avail) |
1135 | | { |
1136 | | (void)id; |
1137 | | (void)name; |
1138 | | (void)avail; |
1139 | | return CURLSSLSET_NO_BACKENDS; |
1140 | | } |
1141 | | |
1142 | | #endif /* !USE_SSL */ |
1143 | | |
1144 | | #ifdef USE_SSL |
1145 | | |
1146 | | void Curl_ssl_peer_cleanup(struct ssl_peer *peer) |
1147 | 0 | { |
1148 | 0 | Curl_safefree(peer->sni); |
1149 | 0 | if(peer->dispname != peer->hostname) |
1150 | 0 | free(peer->dispname); |
1151 | 0 | peer->dispname = NULL; |
1152 | 0 | Curl_safefree(peer->hostname); |
1153 | 0 | Curl_safefree(peer->scache_key); |
1154 | 0 | peer->type = CURL_SSL_PEER_DNS; |
1155 | 0 | } |
1156 | | |
1157 | | static void cf_close(struct Curl_cfilter *cf, struct Curl_easy *data) |
1158 | 0 | { |
1159 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1160 | 0 | if(connssl) { |
1161 | 0 | connssl->ssl_impl->close(cf, data); |
1162 | 0 | connssl->state = ssl_connection_none; |
1163 | 0 | Curl_ssl_peer_cleanup(&connssl->peer); |
1164 | 0 | } |
1165 | 0 | cf->connected = FALSE; |
1166 | 0 | } |
1167 | | |
1168 | | static ssl_peer_type get_peer_type(const char *hostname) |
1169 | 0 | { |
1170 | 0 | if(hostname && hostname[0]) { |
1171 | 0 | #ifdef USE_IPV6 |
1172 | 0 | struct in6_addr addr; |
1173 | | #else |
1174 | | struct in_addr addr; |
1175 | | #endif |
1176 | 0 | if(curlx_inet_pton(AF_INET, hostname, &addr)) |
1177 | 0 | return CURL_SSL_PEER_IPV4; |
1178 | 0 | #ifdef USE_IPV6 |
1179 | 0 | else if(curlx_inet_pton(AF_INET6, hostname, &addr)) { |
1180 | 0 | return CURL_SSL_PEER_IPV6; |
1181 | 0 | } |
1182 | 0 | #endif |
1183 | 0 | } |
1184 | 0 | return CURL_SSL_PEER_DNS; |
1185 | 0 | } |
1186 | | |
1187 | | CURLcode Curl_ssl_peer_init(struct ssl_peer *peer, |
1188 | | struct Curl_cfilter *cf, |
1189 | | const char *tls_id, |
1190 | | int transport) |
1191 | 0 | { |
1192 | 0 | const char *ehostname, *edispname; |
1193 | 0 | CURLcode result = CURLE_OUT_OF_MEMORY; |
1194 | | |
1195 | | /* We expect a clean struct, e.g. called only ONCE */ |
1196 | 0 | DEBUGASSERT(peer); |
1197 | 0 | DEBUGASSERT(!peer->hostname); |
1198 | 0 | DEBUGASSERT(!peer->dispname); |
1199 | 0 | DEBUGASSERT(!peer->sni); |
1200 | | /* We need the hostname for SNI negotiation. Once handshaked, this remains |
1201 | | * the SNI hostname for the TLS connection. When the connection is reused, |
1202 | | * the settings in cf->conn might change. We keep a copy of the hostname we |
1203 | | * use for SNI. |
1204 | | */ |
1205 | 0 | peer->transport = transport; |
1206 | 0 | #ifndef CURL_DISABLE_PROXY |
1207 | 0 | if(Curl_ssl_cf_is_proxy(cf)) { |
1208 | 0 | ehostname = cf->conn->http_proxy.host.name; |
1209 | 0 | edispname = cf->conn->http_proxy.host.dispname; |
1210 | 0 | peer->port = cf->conn->http_proxy.port; |
1211 | 0 | } |
1212 | 0 | else |
1213 | 0 | #endif |
1214 | 0 | { |
1215 | 0 | ehostname = cf->conn->host.name; |
1216 | 0 | edispname = cf->conn->host.dispname; |
1217 | 0 | peer->port = cf->conn->remote_port; |
1218 | 0 | } |
1219 | | |
1220 | | /* hostname MUST exist and not be empty */ |
1221 | 0 | if(!ehostname || !ehostname[0]) { |
1222 | 0 | result = CURLE_FAILED_INIT; |
1223 | 0 | goto out; |
1224 | 0 | } |
1225 | | |
1226 | 0 | peer->hostname = strdup(ehostname); |
1227 | 0 | if(!peer->hostname) |
1228 | 0 | goto out; |
1229 | 0 | if(!edispname || !strcmp(ehostname, edispname)) |
1230 | 0 | peer->dispname = peer->hostname; |
1231 | 0 | else { |
1232 | 0 | peer->dispname = strdup(edispname); |
1233 | 0 | if(!peer->dispname) |
1234 | 0 | goto out; |
1235 | 0 | } |
1236 | 0 | peer->type = get_peer_type(peer->hostname); |
1237 | 0 | if(peer->type == CURL_SSL_PEER_DNS) { |
1238 | | /* not an IP address, normalize according to RCC 6066 ch. 3, |
1239 | | * max len of SNI is 2^16-1, no trailing dot */ |
1240 | 0 | size_t len = strlen(peer->hostname); |
1241 | 0 | if(len && (peer->hostname[len-1] == '.')) |
1242 | 0 | len--; |
1243 | 0 | if(len < USHRT_MAX) { |
1244 | 0 | peer->sni = calloc(1, len + 1); |
1245 | 0 | if(!peer->sni) |
1246 | 0 | goto out; |
1247 | 0 | Curl_strntolower(peer->sni, peer->hostname, len); |
1248 | 0 | peer->sni[len] = 0; |
1249 | 0 | } |
1250 | 0 | } |
1251 | | |
1252 | 0 | result = Curl_ssl_peer_key_make(cf, peer, tls_id, &peer->scache_key); |
1253 | |
|
1254 | 0 | out: |
1255 | 0 | if(result) |
1256 | 0 | Curl_ssl_peer_cleanup(peer); |
1257 | 0 | return result; |
1258 | 0 | } |
1259 | | |
1260 | | static void ssl_cf_destroy(struct Curl_cfilter *cf, struct Curl_easy *data) |
1261 | 0 | { |
1262 | 0 | struct cf_call_data save; |
1263 | |
|
1264 | 0 | CF_DATA_SAVE(save, cf, data); |
1265 | 0 | cf_close(cf, data); |
1266 | 0 | CF_DATA_RESTORE(cf, save); |
1267 | 0 | cf_ctx_free(cf->ctx); |
1268 | 0 | cf->ctx = NULL; |
1269 | 0 | } |
1270 | | |
1271 | | static void ssl_cf_close(struct Curl_cfilter *cf, |
1272 | | struct Curl_easy *data) |
1273 | 0 | { |
1274 | 0 | struct cf_call_data save; |
1275 | |
|
1276 | 0 | CF_DATA_SAVE(save, cf, data); |
1277 | 0 | cf_close(cf, data); |
1278 | 0 | if(cf->next) |
1279 | 0 | cf->next->cft->do_close(cf->next, data); |
1280 | 0 | CF_DATA_RESTORE(cf, save); |
1281 | 0 | } |
1282 | | |
1283 | | static CURLcode ssl_cf_connect(struct Curl_cfilter *cf, |
1284 | | struct Curl_easy *data, |
1285 | | bool *done) |
1286 | 0 | { |
1287 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1288 | 0 | struct cf_call_data save; |
1289 | 0 | CURLcode result; |
1290 | |
|
1291 | 0 | if(cf->connected && (connssl->state != ssl_connection_deferred)) { |
1292 | 0 | *done = TRUE; |
1293 | 0 | return CURLE_OK; |
1294 | 0 | } |
1295 | | |
1296 | 0 | if(!cf->next) { |
1297 | 0 | *done = FALSE; |
1298 | 0 | return CURLE_FAILED_INIT; |
1299 | 0 | } |
1300 | | |
1301 | 0 | if(!cf->next->connected) { |
1302 | 0 | result = cf->next->cft->do_connect(cf->next, data, done); |
1303 | 0 | if(result || !*done) |
1304 | 0 | return result; |
1305 | 0 | } |
1306 | | |
1307 | 0 | CF_DATA_SAVE(save, cf, data); |
1308 | 0 | CURL_TRC_CF(data, cf, "cf_connect()"); |
1309 | 0 | DEBUGASSERT(connssl); |
1310 | |
|
1311 | 0 | *done = FALSE; |
1312 | 0 | if(!connssl->peer.hostname) { |
1313 | 0 | char tls_id[80]; |
1314 | 0 | connssl->ssl_impl->version(tls_id, sizeof(tls_id) - 1); |
1315 | 0 | result = Curl_ssl_peer_init(&connssl->peer, cf, tls_id, TRNSPRT_TCP); |
1316 | 0 | if(result) |
1317 | 0 | goto out; |
1318 | 0 | } |
1319 | | |
1320 | 0 | if(!connssl->prefs_checked) { |
1321 | 0 | if(!ssl_prefs_check(data)) |
1322 | 0 | return CURLE_SSL_CONNECT_ERROR; |
1323 | 0 | connssl->prefs_checked = TRUE; |
1324 | 0 | } |
1325 | | |
1326 | 0 | result = connssl->ssl_impl->do_connect(cf, data, done); |
1327 | |
|
1328 | 0 | if(!result && *done) { |
1329 | 0 | cf->connected = TRUE; |
1330 | 0 | if(connssl->state == ssl_connection_complete) |
1331 | 0 | connssl->handshake_done = curlx_now(); |
1332 | | /* Connection can be deferred when sending early data */ |
1333 | 0 | DEBUGASSERT(connssl->state == ssl_connection_complete || |
1334 | 0 | connssl->state == ssl_connection_deferred); |
1335 | 0 | DEBUGASSERT(connssl->state != ssl_connection_deferred || |
1336 | 0 | connssl->earlydata_state > ssl_earlydata_none); |
1337 | 0 | } |
1338 | 0 | out: |
1339 | 0 | CURL_TRC_CF(data, cf, "cf_connect() -> %d, done=%d", result, *done); |
1340 | 0 | CF_DATA_RESTORE(cf, save); |
1341 | 0 | return result; |
1342 | 0 | } |
1343 | | |
1344 | | static CURLcode ssl_cf_set_earlydata(struct Curl_cfilter *cf, |
1345 | | struct Curl_easy *data, |
1346 | | const void *buf, size_t blen) |
1347 | 0 | { |
1348 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1349 | 0 | size_t nwritten = 0; |
1350 | 0 | CURLcode result = CURLE_OK; |
1351 | |
|
1352 | 0 | DEBUGASSERT(connssl->earlydata_state == ssl_earlydata_await); |
1353 | 0 | DEBUGASSERT(Curl_bufq_is_empty(&connssl->earlydata)); |
1354 | 0 | if(blen) { |
1355 | 0 | if(blen > connssl->earlydata_max) |
1356 | 0 | blen = connssl->earlydata_max; |
1357 | 0 | result = Curl_bufq_write(&connssl->earlydata, buf, blen, &nwritten); |
1358 | 0 | CURL_TRC_CF(data, cf, "ssl_cf_set_earlydata(len=%zu) -> %zd", |
1359 | 0 | blen, nwritten); |
1360 | 0 | if(result) |
1361 | 0 | return result; |
1362 | 0 | } |
1363 | 0 | return CURLE_OK; |
1364 | 0 | } |
1365 | | |
1366 | | static CURLcode ssl_cf_connect_deferred(struct Curl_cfilter *cf, |
1367 | | struct Curl_easy *data, |
1368 | | const void *buf, size_t blen, |
1369 | | bool *done) |
1370 | 0 | { |
1371 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1372 | 0 | CURLcode result = CURLE_OK; |
1373 | |
|
1374 | 0 | DEBUGASSERT(connssl->state == ssl_connection_deferred); |
1375 | 0 | *done = FALSE; |
1376 | 0 | if(connssl->earlydata_state == ssl_earlydata_await) { |
1377 | 0 | result = ssl_cf_set_earlydata(cf, data, buf, blen); |
1378 | 0 | if(result) |
1379 | 0 | return result; |
1380 | | /* we buffered any early data we'd like to send. Actually |
1381 | | * do the connect now which sends it and performs the handshake. */ |
1382 | 0 | connssl->earlydata_state = ssl_earlydata_sending; |
1383 | 0 | connssl->earlydata_skip = Curl_bufq_len(&connssl->earlydata); |
1384 | 0 | } |
1385 | | |
1386 | 0 | result = ssl_cf_connect(cf, data, done); |
1387 | |
|
1388 | 0 | if(!result && *done) { |
1389 | 0 | Curl_pgrsTimeWas(data, TIMER_APPCONNECT, connssl->handshake_done); |
1390 | 0 | switch(connssl->earlydata_state) { |
1391 | 0 | case ssl_earlydata_none: |
1392 | 0 | break; |
1393 | 0 | case ssl_earlydata_accepted: |
1394 | 0 | if(!Curl_ssl_cf_is_proxy(cf)) |
1395 | 0 | Curl_pgrsEarlyData(data, (curl_off_t)connssl->earlydata_skip); |
1396 | 0 | infof(data, "Server accepted %zu bytes of TLS early data.", |
1397 | 0 | connssl->earlydata_skip); |
1398 | 0 | break; |
1399 | 0 | case ssl_earlydata_rejected: |
1400 | 0 | if(!Curl_ssl_cf_is_proxy(cf)) |
1401 | 0 | Curl_pgrsEarlyData(data, -(curl_off_t)connssl->earlydata_skip); |
1402 | 0 | infof(data, "Server rejected TLS early data."); |
1403 | 0 | connssl->earlydata_skip = 0; |
1404 | 0 | break; |
1405 | 0 | default: |
1406 | | /* This should not happen. Either we do not use early data or we |
1407 | | * should know if it was accepted or not. */ |
1408 | 0 | DEBUGASSERT(NULL); |
1409 | 0 | break; |
1410 | 0 | } |
1411 | 0 | } |
1412 | 0 | return result; |
1413 | 0 | } |
1414 | | |
1415 | | static bool ssl_cf_data_pending(struct Curl_cfilter *cf, |
1416 | | const struct Curl_easy *data) |
1417 | 0 | { |
1418 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1419 | 0 | struct cf_call_data save; |
1420 | 0 | bool result; |
1421 | |
|
1422 | 0 | CF_DATA_SAVE(save, cf, data); |
1423 | 0 | if(connssl->ssl_impl->data_pending && |
1424 | 0 | connssl->ssl_impl->data_pending(cf, data)) |
1425 | 0 | result = TRUE; |
1426 | 0 | else |
1427 | 0 | result = cf->next->cft->has_data_pending(cf->next, data); |
1428 | 0 | CF_DATA_RESTORE(cf, save); |
1429 | 0 | return result; |
1430 | 0 | } |
1431 | | |
1432 | | static CURLcode ssl_cf_send(struct Curl_cfilter *cf, |
1433 | | struct Curl_easy *data, |
1434 | | const void *buf, size_t blen, |
1435 | | bool eos, size_t *pnwritten) |
1436 | 0 | { |
1437 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1438 | 0 | struct cf_call_data save; |
1439 | 0 | CURLcode result = CURLE_OK; |
1440 | |
|
1441 | 0 | (void)eos; |
1442 | 0 | *pnwritten = 0; |
1443 | 0 | CF_DATA_SAVE(save, cf, data); |
1444 | |
|
1445 | 0 | if(connssl->state == ssl_connection_deferred) { |
1446 | 0 | bool done = FALSE; |
1447 | 0 | result = ssl_cf_connect_deferred(cf, data, buf, blen, &done); |
1448 | 0 | if(result) |
1449 | 0 | goto out; |
1450 | 0 | else if(!done) { |
1451 | 0 | result = CURLE_AGAIN; |
1452 | 0 | goto out; |
1453 | 0 | } |
1454 | 0 | DEBUGASSERT(connssl->state == ssl_connection_complete); |
1455 | 0 | } |
1456 | | |
1457 | 0 | if(connssl->earlydata_skip) { |
1458 | 0 | if(connssl->earlydata_skip >= blen) { |
1459 | 0 | connssl->earlydata_skip -= blen; |
1460 | 0 | result = CURLE_OK; |
1461 | 0 | *pnwritten = blen; |
1462 | 0 | goto out; |
1463 | 0 | } |
1464 | 0 | else { |
1465 | 0 | *pnwritten = connssl->earlydata_skip; |
1466 | 0 | buf = ((const char *)buf) + connssl->earlydata_skip; |
1467 | 0 | blen -= connssl->earlydata_skip; |
1468 | 0 | connssl->earlydata_skip = 0; |
1469 | 0 | } |
1470 | 0 | } |
1471 | | |
1472 | | /* OpenSSL and maybe other TLS libs do not like 0-length writes. Skip. */ |
1473 | 0 | if(blen > 0) { |
1474 | 0 | size_t nwritten; |
1475 | 0 | result = connssl->ssl_impl->send_plain(cf, data, buf, blen, &nwritten); |
1476 | 0 | if(!result) |
1477 | 0 | *pnwritten += nwritten; |
1478 | 0 | } |
1479 | |
|
1480 | 0 | out: |
1481 | 0 | CF_DATA_RESTORE(cf, save); |
1482 | 0 | return result; |
1483 | 0 | } |
1484 | | |
1485 | | static CURLcode ssl_cf_recv(struct Curl_cfilter *cf, |
1486 | | struct Curl_easy *data, char *buf, size_t len, |
1487 | | size_t *pnread) |
1488 | 0 | { |
1489 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1490 | 0 | struct cf_call_data save; |
1491 | 0 | CURLcode result = CURLE_OK; |
1492 | |
|
1493 | 0 | CF_DATA_SAVE(save, cf, data); |
1494 | 0 | *pnread = 0; |
1495 | 0 | if(connssl->state == ssl_connection_deferred) { |
1496 | 0 | bool done = FALSE; |
1497 | 0 | result = ssl_cf_connect_deferred(cf, data, NULL, 0, &done); |
1498 | 0 | if(result) |
1499 | 0 | goto out; |
1500 | 0 | else if(!done) { |
1501 | 0 | result = CURLE_AGAIN; |
1502 | 0 | goto out; |
1503 | 0 | } |
1504 | 0 | DEBUGASSERT(connssl->state == ssl_connection_complete); |
1505 | 0 | } |
1506 | | |
1507 | 0 | result = connssl->ssl_impl->recv_plain(cf, data, buf, len, pnread); |
1508 | |
|
1509 | 0 | out: |
1510 | 0 | CF_DATA_RESTORE(cf, save); |
1511 | 0 | return result; |
1512 | 0 | } |
1513 | | |
1514 | | static CURLcode ssl_cf_shutdown(struct Curl_cfilter *cf, |
1515 | | struct Curl_easy *data, |
1516 | | bool *done) |
1517 | 0 | { |
1518 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1519 | 0 | CURLcode result = CURLE_OK; |
1520 | |
|
1521 | 0 | *done = TRUE; |
1522 | | /* If we have done the SSL handshake, shut down the connection cleanly */ |
1523 | 0 | if(cf->connected && (connssl->state == ssl_connection_complete) && |
1524 | 0 | !cf->shutdown && Curl_ssl->shut_down) { |
1525 | 0 | struct cf_call_data save; |
1526 | |
|
1527 | 0 | CF_DATA_SAVE(save, cf, data); |
1528 | 0 | result = connssl->ssl_impl->shut_down(cf, data, TRUE, done); |
1529 | 0 | CURL_TRC_CF(data, cf, "cf_shutdown -> %d, done=%d", result, *done); |
1530 | 0 | CF_DATA_RESTORE(cf, save); |
1531 | 0 | cf->shutdown = (result || *done); |
1532 | 0 | } |
1533 | 0 | return result; |
1534 | 0 | } |
1535 | | |
1536 | | static void ssl_cf_adjust_pollset(struct Curl_cfilter *cf, |
1537 | | struct Curl_easy *data, |
1538 | | struct easy_pollset *ps) |
1539 | 0 | { |
1540 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1541 | 0 | struct cf_call_data save; |
1542 | |
|
1543 | 0 | CF_DATA_SAVE(save, cf, data); |
1544 | 0 | connssl->ssl_impl->adjust_pollset(cf, data, ps); |
1545 | 0 | CF_DATA_RESTORE(cf, save); |
1546 | 0 | } |
1547 | | |
1548 | | static CURLcode ssl_cf_query(struct Curl_cfilter *cf, |
1549 | | struct Curl_easy *data, |
1550 | | int query, int *pres1, void *pres2) |
1551 | 0 | { |
1552 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1553 | |
|
1554 | 0 | switch(query) { |
1555 | 0 | case CF_QUERY_TIMER_APPCONNECT: { |
1556 | 0 | struct curltime *when = pres2; |
1557 | 0 | if(cf->connected && !Curl_ssl_cf_is_proxy(cf)) |
1558 | 0 | *when = connssl->handshake_done; |
1559 | 0 | return CURLE_OK; |
1560 | 0 | } |
1561 | 0 | case CF_QUERY_SSL_INFO: |
1562 | 0 | case CF_QUERY_SSL_CTX_INFO: { |
1563 | 0 | struct curl_tlssessioninfo *info = pres2; |
1564 | 0 | struct cf_call_data save; |
1565 | 0 | CF_DATA_SAVE(save, cf, data); |
1566 | 0 | info->backend = Curl_ssl_backend(); |
1567 | 0 | info->internals = connssl->ssl_impl->get_internals( |
1568 | 0 | cf->ctx, (query == CF_QUERY_SSL_INFO) ? |
1569 | 0 | CURLINFO_TLS_SSL_PTR : CURLINFO_TLS_SESSION); |
1570 | 0 | CF_DATA_RESTORE(cf, save); |
1571 | 0 | return CURLE_OK; |
1572 | 0 | } |
1573 | 0 | default: |
1574 | 0 | break; |
1575 | 0 | } |
1576 | 0 | return cf->next ? |
1577 | 0 | cf->next->cft->query(cf->next, data, query, pres1, pres2) : |
1578 | 0 | CURLE_UNKNOWN_OPTION; |
1579 | 0 | } |
1580 | | |
1581 | | static bool cf_ssl_is_alive(struct Curl_cfilter *cf, struct Curl_easy *data, |
1582 | | bool *input_pending) |
1583 | 0 | { |
1584 | | /* |
1585 | | * This function tries to determine connection status. |
1586 | | */ |
1587 | 0 | return cf->next ? |
1588 | 0 | cf->next->cft->is_alive(cf->next, data, input_pending) : |
1589 | 0 | FALSE; /* pessimistic in absence of data */ |
1590 | 0 | } |
1591 | | |
1592 | | struct Curl_cftype Curl_cft_ssl = { |
1593 | | "SSL", |
1594 | | CF_TYPE_SSL, |
1595 | | CURL_LOG_LVL_NONE, |
1596 | | ssl_cf_destroy, |
1597 | | ssl_cf_connect, |
1598 | | ssl_cf_close, |
1599 | | ssl_cf_shutdown, |
1600 | | ssl_cf_adjust_pollset, |
1601 | | ssl_cf_data_pending, |
1602 | | ssl_cf_send, |
1603 | | ssl_cf_recv, |
1604 | | Curl_cf_def_cntrl, |
1605 | | cf_ssl_is_alive, |
1606 | | Curl_cf_def_conn_keep_alive, |
1607 | | ssl_cf_query, |
1608 | | }; |
1609 | | |
1610 | | #ifndef CURL_DISABLE_PROXY |
1611 | | |
1612 | | struct Curl_cftype Curl_cft_ssl_proxy = { |
1613 | | "SSL-PROXY", |
1614 | | CF_TYPE_SSL|CF_TYPE_PROXY, |
1615 | | CURL_LOG_LVL_NONE, |
1616 | | ssl_cf_destroy, |
1617 | | ssl_cf_connect, |
1618 | | ssl_cf_close, |
1619 | | ssl_cf_shutdown, |
1620 | | ssl_cf_adjust_pollset, |
1621 | | ssl_cf_data_pending, |
1622 | | ssl_cf_send, |
1623 | | ssl_cf_recv, |
1624 | | Curl_cf_def_cntrl, |
1625 | | cf_ssl_is_alive, |
1626 | | Curl_cf_def_conn_keep_alive, |
1627 | | Curl_cf_def_query, |
1628 | | }; |
1629 | | |
1630 | | #endif /* !CURL_DISABLE_PROXY */ |
1631 | | |
1632 | | static CURLcode cf_ssl_create(struct Curl_cfilter **pcf, |
1633 | | struct Curl_easy *data, |
1634 | | struct connectdata *conn) |
1635 | 0 | { |
1636 | 0 | struct Curl_cfilter *cf = NULL; |
1637 | 0 | struct ssl_connect_data *ctx; |
1638 | 0 | CURLcode result; |
1639 | |
|
1640 | 0 | DEBUGASSERT(data->conn); |
1641 | |
|
1642 | | #ifdef CURL_DISABLE_HTTP |
1643 | | /* We only support ALPN for HTTP so far. */ |
1644 | | DEBUGASSERT(!conn->bits.tls_enable_alpn); |
1645 | | ctx = cf_ctx_new(data, NULL); |
1646 | | #else |
1647 | 0 | ctx = cf_ctx_new(data, alpn_get_spec(data->state.http_neg.wanted, |
1648 | 0 | conn->bits.tls_enable_alpn)); |
1649 | 0 | #endif |
1650 | 0 | if(!ctx) { |
1651 | 0 | result = CURLE_OUT_OF_MEMORY; |
1652 | 0 | goto out; |
1653 | 0 | } |
1654 | | |
1655 | 0 | result = Curl_cf_create(&cf, &Curl_cft_ssl, ctx); |
1656 | |
|
1657 | 0 | out: |
1658 | 0 | if(result) |
1659 | 0 | cf_ctx_free(ctx); |
1660 | 0 | *pcf = result ? NULL : cf; |
1661 | 0 | return result; |
1662 | 0 | } |
1663 | | |
1664 | | CURLcode Curl_ssl_cfilter_add(struct Curl_easy *data, |
1665 | | struct connectdata *conn, |
1666 | | int sockindex) |
1667 | 0 | { |
1668 | 0 | struct Curl_cfilter *cf; |
1669 | 0 | CURLcode result; |
1670 | |
|
1671 | 0 | result = cf_ssl_create(&cf, data, conn); |
1672 | 0 | if(!result) |
1673 | 0 | Curl_conn_cf_add(data, conn, sockindex, cf); |
1674 | 0 | return result; |
1675 | 0 | } |
1676 | | |
1677 | | CURLcode Curl_cf_ssl_insert_after(struct Curl_cfilter *cf_at, |
1678 | | struct Curl_easy *data) |
1679 | 0 | { |
1680 | 0 | struct Curl_cfilter *cf; |
1681 | 0 | CURLcode result; |
1682 | |
|
1683 | 0 | result = cf_ssl_create(&cf, data, cf_at->conn); |
1684 | 0 | if(!result) |
1685 | 0 | Curl_conn_cf_insert_after(cf_at, cf); |
1686 | 0 | return result; |
1687 | 0 | } |
1688 | | |
1689 | | #ifndef CURL_DISABLE_PROXY |
1690 | | |
1691 | | static CURLcode cf_ssl_proxy_create(struct Curl_cfilter **pcf, |
1692 | | struct Curl_easy *data, |
1693 | | struct connectdata *conn) |
1694 | 0 | { |
1695 | 0 | struct Curl_cfilter *cf = NULL; |
1696 | 0 | struct ssl_connect_data *ctx; |
1697 | 0 | CURLcode result; |
1698 | 0 | bool use_alpn = conn->bits.tls_enable_alpn; |
1699 | 0 | http_majors allowed = CURL_HTTP_V1x; |
1700 | |
|
1701 | | #ifdef USE_HTTP2 |
1702 | | if(conn->http_proxy.proxytype == CURLPROXY_HTTPS2) { |
1703 | | use_alpn = TRUE; |
1704 | | allowed = (CURL_HTTP_V1x|CURL_HTTP_V2x); |
1705 | | } |
1706 | | #endif |
1707 | |
|
1708 | 0 | ctx = cf_ctx_new(data, alpn_get_spec(allowed, use_alpn)); |
1709 | 0 | if(!ctx) { |
1710 | 0 | result = CURLE_OUT_OF_MEMORY; |
1711 | 0 | goto out; |
1712 | 0 | } |
1713 | 0 | result = Curl_cf_create(&cf, &Curl_cft_ssl_proxy, ctx); |
1714 | |
|
1715 | 0 | out: |
1716 | 0 | if(result) |
1717 | 0 | cf_ctx_free(ctx); |
1718 | 0 | *pcf = result ? NULL : cf; |
1719 | 0 | return result; |
1720 | 0 | } |
1721 | | |
1722 | | CURLcode Curl_cf_ssl_proxy_insert_after(struct Curl_cfilter *cf_at, |
1723 | | struct Curl_easy *data) |
1724 | 0 | { |
1725 | 0 | struct Curl_cfilter *cf; |
1726 | 0 | CURLcode result; |
1727 | |
|
1728 | 0 | result = cf_ssl_proxy_create(&cf, data, cf_at->conn); |
1729 | 0 | if(!result) |
1730 | 0 | Curl_conn_cf_insert_after(cf_at, cf); |
1731 | 0 | return result; |
1732 | 0 | } |
1733 | | |
1734 | | #endif /* !CURL_DISABLE_PROXY */ |
1735 | | |
1736 | | bool Curl_ssl_supports(struct Curl_easy *data, unsigned int ssl_option) |
1737 | 0 | { |
1738 | 0 | (void)data; |
1739 | 0 | return (Curl_ssl->supports & ssl_option); |
1740 | 0 | } |
1741 | | |
1742 | | static CURLcode vtls_shutdown_blocking(struct Curl_cfilter *cf, |
1743 | | struct Curl_easy *data, |
1744 | | bool send_shutdown, bool *done) |
1745 | 0 | { |
1746 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1747 | 0 | struct cf_call_data save; |
1748 | 0 | CURLcode result = CURLE_OK; |
1749 | 0 | timediff_t timeout_ms; |
1750 | 0 | int what, loop = 10; |
1751 | |
|
1752 | 0 | if(cf->shutdown) { |
1753 | 0 | *done = TRUE; |
1754 | 0 | return CURLE_OK; |
1755 | 0 | } |
1756 | 0 | CF_DATA_SAVE(save, cf, data); |
1757 | |
|
1758 | 0 | *done = FALSE; |
1759 | 0 | while(!result && !*done && loop--) { |
1760 | 0 | timeout_ms = Curl_shutdown_timeleft(cf->conn, cf->sockindex, NULL); |
1761 | |
|
1762 | 0 | if(timeout_ms < 0) { |
1763 | | /* no need to continue if time is already up */ |
1764 | 0 | failf(data, "SSL shutdown timeout"); |
1765 | 0 | return CURLE_OPERATION_TIMEDOUT; |
1766 | 0 | } |
1767 | | |
1768 | 0 | result = connssl->ssl_impl->shut_down(cf, data, send_shutdown, done); |
1769 | 0 | if(result ||*done) |
1770 | 0 | goto out; |
1771 | | |
1772 | 0 | if(connssl->io_need) { |
1773 | 0 | what = Curl_conn_cf_poll(cf, data, timeout_ms); |
1774 | 0 | if(what < 0) { |
1775 | | /* fatal error */ |
1776 | 0 | failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO); |
1777 | 0 | result = CURLE_RECV_ERROR; |
1778 | 0 | goto out; |
1779 | 0 | } |
1780 | 0 | else if(0 == what) { |
1781 | | /* timeout */ |
1782 | 0 | failf(data, "SSL shutdown timeout"); |
1783 | 0 | result = CURLE_OPERATION_TIMEDOUT; |
1784 | 0 | goto out; |
1785 | 0 | } |
1786 | | /* socket is readable or writable */ |
1787 | 0 | } |
1788 | 0 | } |
1789 | 0 | out: |
1790 | 0 | CF_DATA_RESTORE(cf, save); |
1791 | 0 | cf->shutdown = (result || *done); |
1792 | 0 | return result; |
1793 | 0 | } |
1794 | | |
1795 | | CURLcode Curl_ssl_cfilter_remove(struct Curl_easy *data, |
1796 | | int sockindex, bool send_shutdown) |
1797 | 0 | { |
1798 | 0 | struct Curl_cfilter *cf, *head; |
1799 | 0 | CURLcode result = CURLE_OK; |
1800 | |
|
1801 | 0 | head = data->conn ? data->conn->cfilter[sockindex] : NULL; |
1802 | 0 | for(cf = head; cf; cf = cf->next) { |
1803 | 0 | if(cf->cft == &Curl_cft_ssl) { |
1804 | 0 | bool done; |
1805 | 0 | CURL_TRC_CF(data, cf, "shutdown and remove SSL, start"); |
1806 | 0 | Curl_shutdown_start(data, sockindex, 0, NULL); |
1807 | 0 | result = vtls_shutdown_blocking(cf, data, send_shutdown, &done); |
1808 | 0 | Curl_shutdown_clear(data, sockindex); |
1809 | 0 | if(!result && !done) /* blocking failed? */ |
1810 | 0 | result = CURLE_SSL_SHUTDOWN_FAILED; |
1811 | 0 | Curl_conn_cf_discard_sub(head, cf, data, FALSE); |
1812 | 0 | CURL_TRC_CF(data, cf, "shutdown and remove SSL, done -> %d", result); |
1813 | 0 | break; |
1814 | 0 | } |
1815 | 0 | } |
1816 | 0 | return result; |
1817 | 0 | } |
1818 | | |
1819 | | bool Curl_ssl_cf_is_proxy(struct Curl_cfilter *cf) |
1820 | 0 | { |
1821 | 0 | return (cf->cft->flags & CF_TYPE_SSL) && (cf->cft->flags & CF_TYPE_PROXY); |
1822 | 0 | } |
1823 | | |
1824 | | struct ssl_config_data * |
1825 | | Curl_ssl_cf_get_config(struct Curl_cfilter *cf, struct Curl_easy *data) |
1826 | 0 | { |
1827 | | #ifdef CURL_DISABLE_PROXY |
1828 | | (void)cf; |
1829 | | return &data->set.ssl; |
1830 | | #else |
1831 | 0 | return Curl_ssl_cf_is_proxy(cf) ? &data->set.proxy_ssl : &data->set.ssl; |
1832 | 0 | #endif |
1833 | 0 | } |
1834 | | |
1835 | | struct ssl_primary_config * |
1836 | | Curl_ssl_cf_get_primary_config(struct Curl_cfilter *cf) |
1837 | 0 | { |
1838 | | #ifdef CURL_DISABLE_PROXY |
1839 | | return &cf->conn->ssl_config; |
1840 | | #else |
1841 | 0 | return Curl_ssl_cf_is_proxy(cf) ? |
1842 | 0 | &cf->conn->proxy_ssl_config : &cf->conn->ssl_config; |
1843 | 0 | #endif |
1844 | 0 | } |
1845 | | |
1846 | | CURLcode Curl_alpn_to_proto_buf(struct alpn_proto_buf *buf, |
1847 | | const struct alpn_spec *spec) |
1848 | 0 | { |
1849 | 0 | size_t i, len; |
1850 | 0 | int off = 0; |
1851 | 0 | unsigned char blen; |
1852 | |
|
1853 | 0 | memset(buf, 0, sizeof(*buf)); |
1854 | 0 | for(i = 0; spec && i < spec->count; ++i) { |
1855 | 0 | len = strlen(spec->entries[i]); |
1856 | 0 | if(len >= ALPN_NAME_MAX) |
1857 | 0 | return CURLE_FAILED_INIT; |
1858 | 0 | blen = (unsigned char)len; |
1859 | 0 | if(off + blen + 1 >= (int)sizeof(buf->data)) |
1860 | 0 | return CURLE_FAILED_INIT; |
1861 | 0 | buf->data[off++] = blen; |
1862 | 0 | memcpy(buf->data + off, spec->entries[i], blen); |
1863 | 0 | off += blen; |
1864 | 0 | } |
1865 | 0 | buf->len = off; |
1866 | 0 | return CURLE_OK; |
1867 | 0 | } |
1868 | | |
1869 | | CURLcode Curl_alpn_to_proto_str(struct alpn_proto_buf *buf, |
1870 | | const struct alpn_spec *spec) |
1871 | 0 | { |
1872 | 0 | size_t i, len; |
1873 | 0 | size_t off = 0; |
1874 | |
|
1875 | 0 | memset(buf, 0, sizeof(*buf)); |
1876 | 0 | for(i = 0; spec && i < spec->count; ++i) { |
1877 | 0 | len = strlen(spec->entries[i]); |
1878 | 0 | if(len >= ALPN_NAME_MAX) |
1879 | 0 | return CURLE_FAILED_INIT; |
1880 | 0 | if(off + len + 2 >= sizeof(buf->data)) |
1881 | 0 | return CURLE_FAILED_INIT; |
1882 | 0 | if(off) |
1883 | 0 | buf->data[off++] = ','; |
1884 | 0 | memcpy(buf->data + off, spec->entries[i], len); |
1885 | 0 | off += len; |
1886 | 0 | } |
1887 | 0 | buf->data[off] = '\0'; |
1888 | 0 | buf->len = (int)off; |
1889 | 0 | return CURLE_OK; |
1890 | 0 | } |
1891 | | |
1892 | | bool Curl_alpn_contains_proto(const struct alpn_spec *spec, |
1893 | | const char *proto) |
1894 | 0 | { |
1895 | 0 | size_t i, plen = proto ? strlen(proto) : 0; |
1896 | 0 | for(i = 0; spec && plen && i < spec->count; ++i) { |
1897 | 0 | size_t slen = strlen(spec->entries[i]); |
1898 | 0 | if((slen == plen) && !memcmp(proto, spec->entries[i], plen)) |
1899 | 0 | return TRUE; |
1900 | 0 | } |
1901 | 0 | return FALSE; |
1902 | 0 | } |
1903 | | |
1904 | | void Curl_alpn_restrict_to(struct alpn_spec *spec, const char *proto) |
1905 | 0 | { |
1906 | 0 | size_t plen = strlen(proto); |
1907 | 0 | DEBUGASSERT(plen < sizeof(spec->entries[0])); |
1908 | 0 | if(plen < sizeof(spec->entries[0])) { |
1909 | 0 | memcpy(spec->entries[0], proto, plen + 1); |
1910 | 0 | spec->count = 1; |
1911 | 0 | } |
1912 | 0 | } |
1913 | | |
1914 | | void Curl_alpn_copy(struct alpn_spec *dest, const struct alpn_spec *src) |
1915 | 0 | { |
1916 | 0 | if(src) |
1917 | 0 | memcpy(dest, src, sizeof(*dest)); |
1918 | 0 | else |
1919 | 0 | memset(dest, 0, sizeof(*dest)); |
1920 | 0 | } |
1921 | | |
1922 | | CURLcode Curl_alpn_set_negotiated(struct Curl_cfilter *cf, |
1923 | | struct Curl_easy *data, |
1924 | | struct ssl_connect_data *connssl, |
1925 | | const unsigned char *proto, |
1926 | | size_t proto_len) |
1927 | 0 | { |
1928 | 0 | CURLcode result = CURLE_OK; |
1929 | 0 | unsigned char *palpn = |
1930 | 0 | #ifndef CURL_DISABLE_PROXY |
1931 | 0 | (cf->conn->bits.tunnel_proxy && Curl_ssl_cf_is_proxy(cf)) ? |
1932 | 0 | &cf->conn->proxy_alpn : &cf->conn->alpn |
1933 | | #else |
1934 | | &cf->conn->alpn |
1935 | | #endif |
1936 | 0 | ; |
1937 | |
|
1938 | 0 | if(connssl->negotiated.alpn) { |
1939 | | /* When we ask for a specific ALPN protocol, we need the confirmation |
1940 | | * of it by the server, as we have installed protocol handler and |
1941 | | * connection filter chain for exactly this protocol. */ |
1942 | 0 | if(!proto_len) { |
1943 | 0 | failf(data, "ALPN: asked for '%s' from previous session, " |
1944 | 0 | "but server did not confirm it. Refusing to continue.", |
1945 | 0 | connssl->negotiated.alpn); |
1946 | 0 | result = CURLE_SSL_CONNECT_ERROR; |
1947 | 0 | goto out; |
1948 | 0 | } |
1949 | 0 | else if((strlen(connssl->negotiated.alpn) != proto_len) || |
1950 | 0 | memcmp(connssl->negotiated.alpn, proto, proto_len)) { |
1951 | 0 | failf(data, "ALPN: asked for '%s' from previous session, but server " |
1952 | 0 | "selected '%.*s'. Refusing to continue.", |
1953 | 0 | connssl->negotiated.alpn, (int)proto_len, proto); |
1954 | 0 | result = CURLE_SSL_CONNECT_ERROR; |
1955 | 0 | goto out; |
1956 | 0 | } |
1957 | | /* ALPN is exactly what we asked for, done. */ |
1958 | 0 | infof(data, "ALPN: server confirmed to use '%s'", |
1959 | 0 | connssl->negotiated.alpn); |
1960 | 0 | goto out; |
1961 | 0 | } |
1962 | | |
1963 | 0 | if(proto && proto_len) { |
1964 | 0 | if(memchr(proto, '\0', proto_len)) { |
1965 | 0 | failf(data, "ALPN: server selected protocol contains NUL. " |
1966 | 0 | "Refusing to continue."); |
1967 | 0 | result = CURLE_SSL_CONNECT_ERROR; |
1968 | 0 | goto out; |
1969 | 0 | } |
1970 | 0 | connssl->negotiated.alpn = malloc(proto_len + 1); |
1971 | 0 | if(!connssl->negotiated.alpn) |
1972 | 0 | return CURLE_OUT_OF_MEMORY; |
1973 | 0 | memcpy(connssl->negotiated.alpn, proto, proto_len); |
1974 | 0 | connssl->negotiated.alpn[proto_len] = 0; |
1975 | 0 | } |
1976 | | |
1977 | 0 | if(proto && proto_len) { |
1978 | 0 | if(proto_len == ALPN_HTTP_1_1_LENGTH && |
1979 | 0 | !memcmp(ALPN_HTTP_1_1, proto, ALPN_HTTP_1_1_LENGTH)) { |
1980 | 0 | *palpn = CURL_HTTP_VERSION_1_1; |
1981 | 0 | } |
1982 | | #ifdef USE_HTTP2 |
1983 | | else if(proto_len == ALPN_H2_LENGTH && |
1984 | | !memcmp(ALPN_H2, proto, ALPN_H2_LENGTH)) { |
1985 | | *palpn = CURL_HTTP_VERSION_2; |
1986 | | } |
1987 | | #endif |
1988 | | #ifdef USE_HTTP3 |
1989 | | else if(proto_len == ALPN_H3_LENGTH && |
1990 | | !memcmp(ALPN_H3, proto, ALPN_H3_LENGTH)) { |
1991 | | *palpn = CURL_HTTP_VERSION_3; |
1992 | | } |
1993 | | #endif |
1994 | 0 | else { |
1995 | 0 | *palpn = CURL_HTTP_VERSION_NONE; |
1996 | 0 | failf(data, "unsupported ALPN protocol: '%.*s'", (int)proto_len, proto); |
1997 | | /* Previous code just ignored it and some vtls backends even ignore the |
1998 | | * return code of this function. */ |
1999 | | /* return CURLE_NOT_BUILT_IN; */ |
2000 | 0 | goto out; |
2001 | 0 | } |
2002 | | |
2003 | 0 | if(connssl->state == ssl_connection_deferred) |
2004 | 0 | infof(data, VTLS_INFOF_ALPN_DEFERRED, (int)proto_len, proto); |
2005 | 0 | else |
2006 | 0 | infof(data, VTLS_INFOF_ALPN_ACCEPTED, (int)proto_len, proto); |
2007 | 0 | } |
2008 | 0 | else { |
2009 | 0 | *palpn = CURL_HTTP_VERSION_NONE; |
2010 | 0 | if(connssl->state == ssl_connection_deferred) |
2011 | 0 | infof(data, VTLS_INFOF_NO_ALPN_DEFERRED); |
2012 | 0 | else |
2013 | 0 | infof(data, VTLS_INFOF_NO_ALPN); |
2014 | 0 | } |
2015 | | |
2016 | 0 | out: |
2017 | 0 | return result; |
2018 | 0 | } |
2019 | | |
2020 | | #endif /* USE_SSL */ |