/src/curl/lib/vtls/vtls.c
Line | Count | Source |
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 | | #include "urldata.h" |
44 | | #include "cfilters.h" |
45 | | |
46 | | #include "vtls/vtls.h" /* generic SSL protos etc */ |
47 | | #include "vtls/vtls_int.h" |
48 | | #include "vtls/vtls_scache.h" |
49 | | #include "vtls/keylog.h" |
50 | | |
51 | | #include "vtls/openssl.h" /* OpenSSL versions */ |
52 | | #include "vtls/gtls.h" /* GnuTLS versions */ |
53 | | #include "vtls/wolfssl.h" /* wolfSSL versions */ |
54 | | #include "vtls/schannel.h" /* Schannel SSPI version */ |
55 | | #include "vtls/mbedtls.h" /* mbedTLS versions */ |
56 | | #include "vtls/rustls.h" /* Rustls versions */ |
57 | | |
58 | | #include "slist.h" |
59 | | #include "curl_trc.h" |
60 | | #include "strcase.h" |
61 | | #include "url.h" |
62 | | #include "progress.h" |
63 | | #include "curlx/win32-fopen.h" |
64 | | #include "curl_sha256.h" |
65 | | #include "curlx/base64.h" |
66 | | #include "curlx/inet_pton.h" |
67 | | #include "connect.h" |
68 | | #include "select.h" |
69 | | #include "setopt.h" |
70 | | #include "vdns/cf-dns.h" |
71 | | #include "curlx/strdup.h" |
72 | | #include "curlx/strcopy.h" |
73 | | |
74 | | #ifdef USE_APPLE_SECTRUST |
75 | | #include <Security/Security.h> |
76 | | #endif |
77 | | |
78 | | #ifdef USE_SSL |
79 | | #if !defined(CURL_DISABLE_HTTP) || !defined(CURL_DISABLE_PROXY) |
80 | | static const struct alpn_spec ALPN_SPEC_H11 = { |
81 | | { ALPN_HTTP_1_1 }, 1 |
82 | | }; |
83 | | static const struct alpn_spec ALPN_SPEC_H10_H11 = { |
84 | | { ALPN_HTTP_1_0, ALPN_HTTP_1_1 }, 2 |
85 | | }; |
86 | | #ifdef USE_HTTP2 |
87 | | static const struct alpn_spec ALPN_SPEC_H2 = { |
88 | | { ALPN_H2 }, 1 |
89 | | }; |
90 | | static const struct alpn_spec ALPN_SPEC_H2_H11 = { |
91 | | { ALPN_H2, ALPN_HTTP_1_1 }, 2 |
92 | | }; |
93 | | static const struct alpn_spec ALPN_SPEC_H11_H2 = { |
94 | | { ALPN_HTTP_1_1, ALPN_H2 }, 2 |
95 | | }; |
96 | | #endif /* USE_HTTP2 */ |
97 | | |
98 | | static const struct alpn_spec *alpn_get_spec(http_majors wanted, |
99 | | http_majors preferred, |
100 | | bool only_http_10, |
101 | | bool use_alpn) |
102 | 0 | { |
103 | 0 | if(!use_alpn) |
104 | 0 | return NULL; |
105 | | /* If HTTP/1.0 is the wanted protocol then use ALPN http/1.0 and http/1.1. |
106 | | This is for compatibility reasons since some HTTP/1.0 servers with old |
107 | | ALPN implementations understand ALPN http/1.1 but not http/1.0. */ |
108 | 0 | if(only_http_10 && (wanted & CURL_HTTP_V1x)) |
109 | 0 | return &ALPN_SPEC_H10_H11; |
110 | 0 | #ifdef USE_HTTP2 |
111 | 0 | if(wanted & CURL_HTTP_V2x) { |
112 | 0 | if(wanted & CURL_HTTP_V1x) |
113 | 0 | return (preferred == CURL_HTTP_V1x) ? |
114 | 0 | &ALPN_SPEC_H11_H2 : &ALPN_SPEC_H2_H11; |
115 | 0 | return &ALPN_SPEC_H2; |
116 | 0 | } |
117 | | #else |
118 | | (void)wanted; |
119 | | (void)preferred; |
120 | | #endif |
121 | 0 | return &ALPN_SPEC_H11; |
122 | 0 | } |
123 | | #endif /* !CURL_DISABLE_HTTP || !CURL_DISABLE_PROXY */ |
124 | | #endif /* USE_SSL */ |
125 | | |
126 | | #ifdef USE_SSL |
127 | | static int multissl_setup(const struct Curl_ssl *backend); |
128 | | #endif |
129 | | |
130 | | curl_sslbackend Curl_ssl_backend(void) |
131 | 0 | { |
132 | 0 | #ifdef USE_SSL |
133 | 0 | multissl_setup(NULL); |
134 | 0 | return Curl_ssl->info.id; |
135 | | #else |
136 | | return CURLSSLBACKEND_NONE; |
137 | | #endif |
138 | 0 | } |
139 | | |
140 | | #ifdef USE_SSL |
141 | | |
142 | | /* "global" init done? */ |
143 | | static bool init_ssl = FALSE; |
144 | | |
145 | | /** |
146 | | * Global SSL init |
147 | | * |
148 | | * @retval 0 error initializing SSL |
149 | | * @retval 1 SSL initialized successfully |
150 | | */ |
151 | | int Curl_ssl_init(void) |
152 | 1 | { |
153 | | /* make sure this is only done once */ |
154 | 1 | if(init_ssl) |
155 | 0 | return 1; |
156 | 1 | init_ssl = TRUE; /* never again */ |
157 | | |
158 | 1 | if(Curl_ssl->init) |
159 | 1 | return Curl_ssl->init(); |
160 | 0 | return 1; |
161 | 1 | } |
162 | | |
163 | | static bool ssl_prefs_check(struct Curl_cfilter *cf, |
164 | | struct Curl_easy *data) |
165 | 0 | { |
166 | 0 | struct ssl_filter_config *conn_config = Curl_ssl_cf_get_filter_config(cf); |
167 | | /* check for CURLOPT_SSLVERSION invalid parameter value */ |
168 | 0 | if(conn_config->version >= CURL_SSLVERSION_LAST) { |
169 | 0 | failf(data, "Unrecognized parameter value passed via CURLOPT_SSLVERSION"); |
170 | 0 | return FALSE; |
171 | 0 | } |
172 | | |
173 | 0 | switch(conn_config->version_max) { |
174 | 0 | case CURL_SSLVERSION_MAX_NONE: |
175 | 0 | case CURL_SSLVERSION_MAX_DEFAULT: |
176 | 0 | break; |
177 | | |
178 | 0 | default: |
179 | 0 | if((conn_config->version_max >> 16) < conn_config->version) { |
180 | 0 | failf(data, "CURL_SSLVERSION_MAX incompatible with CURL_SSLVERSION"); |
181 | 0 | return FALSE; |
182 | 0 | } |
183 | 0 | } |
184 | | |
185 | 0 | return TRUE; |
186 | 0 | } |
187 | | |
188 | | static struct ssl_connect_data *cf_ctx_new(struct Curl_easy *data, |
189 | | const struct alpn_spec *alpn) |
190 | 0 | { |
191 | 0 | struct ssl_connect_data *ctx; |
192 | |
|
193 | 0 | (void)data; |
194 | 0 | ctx = curlx_calloc(1, sizeof(*ctx)); |
195 | 0 | if(!ctx) |
196 | 0 | return NULL; |
197 | | |
198 | 0 | ctx->ssl_impl = Curl_ssl; |
199 | 0 | ctx->alpn = alpn; |
200 | 0 | Curl_bufq_init2(&ctx->earlydata, CURL_SSL_EARLY_MAX, 1, BUFQ_OPT_NO_SPARES); |
201 | 0 | ctx->backend = curlx_calloc(1, ctx->ssl_impl->sizeof_ssl_backend_data); |
202 | 0 | if(!ctx->backend) { |
203 | 0 | curlx_free(ctx); |
204 | 0 | return NULL; |
205 | 0 | } |
206 | 0 | return ctx; |
207 | 0 | } |
208 | | |
209 | | static void cf_ctx_free(struct ssl_connect_data *ctx) |
210 | 0 | { |
211 | 0 | if(ctx) { |
212 | 0 | curlx_safefree(ctx->negotiated.alpn); |
213 | 0 | Curl_bufq_free(&ctx->earlydata); |
214 | 0 | curlx_free(ctx->backend); |
215 | 0 | curlx_free(ctx); |
216 | 0 | } |
217 | 0 | } |
218 | | |
219 | | CURLcode Curl_ssl_get_channel_binding(struct Curl_easy *data, int8_t sockindex, |
220 | | struct dynbuf *binding) |
221 | 0 | { |
222 | 0 | if(Curl_ssl->get_channel_binding) |
223 | 0 | return Curl_ssl->get_channel_binding(data, sockindex, binding); |
224 | 0 | return CURLE_OK; |
225 | 0 | } |
226 | | |
227 | | void Curl_ssl_close_all(struct Curl_easy *data) |
228 | 1 | { |
229 | 1 | if(Curl_ssl->close_all) |
230 | 1 | Curl_ssl->close_all(data); |
231 | 1 | } |
232 | | |
233 | | CURLcode Curl_ssl_adjust_pollset(struct Curl_cfilter *cf, |
234 | | struct Curl_easy *data, |
235 | | struct easy_pollset *ps) |
236 | 0 | { |
237 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
238 | |
|
239 | 0 | if(connssl->io_need) { |
240 | 0 | curl_socket_t sock = Curl_conn_cf_get_socket(cf->next, data); |
241 | 0 | CURLcode result = CURLE_OK; |
242 | 0 | if(sock != CURL_SOCKET_BAD) { |
243 | 0 | if(connssl->io_need & CURL_SSL_IO_NEED_SEND) { |
244 | 0 | result = Curl_pollset_set_out_only(data, ps, sock); |
245 | 0 | CURL_TRC_CF(data, cf, "adjust_pollset, POLLOUT fd=%" FMT_SOCKET_T, |
246 | 0 | sock); |
247 | 0 | } |
248 | 0 | else { |
249 | 0 | result = Curl_pollset_set_in_only(data, ps, sock); |
250 | 0 | CURL_TRC_CF(data, cf, "adjust_pollset, POLLIN fd=%" FMT_SOCKET_T, |
251 | 0 | sock); |
252 | 0 | } |
253 | 0 | } |
254 | 0 | return result; |
255 | 0 | } |
256 | 0 | return CURLE_OK; |
257 | 0 | } |
258 | | |
259 | | /* Return list of OpenSSL crypto engine names. */ |
260 | | struct curl_slist *Curl_ssl_engines_list(struct Curl_easy *data) |
261 | 0 | { |
262 | 0 | if(Curl_ssl->engines_list) |
263 | 0 | return Curl_ssl->engines_list(data); |
264 | 0 | return NULL; |
265 | 0 | } |
266 | | |
267 | | static size_t multissl_version(char *buffer, size_t size); |
268 | | |
269 | | void Curl_ssl_version(char *buffer, size_t size) |
270 | 0 | { |
271 | | #ifdef CURL_WITH_MULTI_SSL |
272 | | (void)multissl_version(buffer, size); |
273 | | #else |
274 | 0 | (void)Curl_ssl->version(buffer, size); |
275 | 0 | #endif |
276 | 0 | } |
277 | | |
278 | | void Curl_ssl_free_certinfo(struct Curl_easy *data) |
279 | 2 | { |
280 | 2 | struct curl_certinfo *ci = &data->info.certs; |
281 | | |
282 | 2 | if(ci->num_of_certs) { |
283 | | /* free all individual lists used */ |
284 | 0 | int i; |
285 | 0 | for(i = 0; i < ci->num_of_certs; i++) { |
286 | 0 | curl_slist_free_all(ci->certinfo[i]); |
287 | 0 | ci->certinfo[i] = NULL; |
288 | 0 | } |
289 | |
|
290 | 0 | curlx_safefree(ci->certinfo); /* free the actual array too */ |
291 | 0 | ci->num_of_certs = 0; |
292 | 0 | } |
293 | 2 | } |
294 | | |
295 | | CURLcode Curl_ssl_init_certinfo(struct Curl_easy *data, int num) |
296 | 0 | { |
297 | 0 | struct curl_certinfo *ci = &data->info.certs; |
298 | 0 | struct curl_slist **table; |
299 | | |
300 | | /* Free any previous certificate information structures */ |
301 | 0 | Curl_ssl_free_certinfo(data); |
302 | | |
303 | | /* Allocate the required certificate information structures */ |
304 | 0 | table = curlx_calloc((size_t)num, sizeof(struct curl_slist *)); |
305 | 0 | if(!table) |
306 | 0 | return CURLE_OUT_OF_MEMORY; |
307 | | |
308 | 0 | ci->num_of_certs = num; |
309 | 0 | ci->certinfo = table; |
310 | |
|
311 | 0 | return CURLE_OK; |
312 | 0 | } |
313 | | |
314 | | /* |
315 | | * 'value' is NOT a null-terminated string |
316 | | */ |
317 | | CURLcode Curl_ssl_push_certinfo_len(struct Curl_easy *data, |
318 | | int certnum, |
319 | | const char *label, |
320 | | const char *value, |
321 | | size_t valuelen) |
322 | 0 | { |
323 | 0 | struct curl_certinfo *ci = &data->info.certs; |
324 | 0 | struct curl_slist *nl; |
325 | 0 | CURLcode result = CURLE_OK; |
326 | 0 | struct dynbuf build; |
327 | |
|
328 | 0 | DEBUGASSERT(certnum < ci->num_of_certs); |
329 | |
|
330 | 0 | curlx_dyn_init(&build, CURL_X509_STR_MAX); |
331 | |
|
332 | 0 | if(curlx_dyn_add(&build, label) || |
333 | 0 | curlx_dyn_addn(&build, ":", 1) || |
334 | 0 | curlx_dyn_addn(&build, value, valuelen)) |
335 | 0 | return CURLE_OUT_OF_MEMORY; |
336 | | |
337 | 0 | nl = Curl_slist_append_nodup(ci->certinfo[certnum], curlx_dyn_ptr(&build)); |
338 | 0 | if(!nl) { |
339 | 0 | curlx_dyn_free(&build); |
340 | 0 | curl_slist_free_all(ci->certinfo[certnum]); |
341 | 0 | result = CURLE_OUT_OF_MEMORY; |
342 | 0 | } |
343 | |
|
344 | 0 | ci->certinfo[certnum] = nl; |
345 | 0 | return result; |
346 | 0 | } |
347 | | |
348 | | /* get length bytes of randomness */ |
349 | | CURLcode Curl_ssl_random(struct Curl_easy *data, |
350 | | unsigned char *buffer, size_t length) |
351 | 2 | { |
352 | 2 | DEBUGASSERT(length == sizeof(int)); |
353 | 2 | if(Curl_ssl->random) |
354 | 2 | return Curl_ssl->random(data, buffer, length); |
355 | 0 | else |
356 | 0 | return CURLE_NOT_BUILT_IN; |
357 | 2 | } |
358 | | |
359 | | /* |
360 | | * Public key pem to der conversion |
361 | | */ |
362 | | |
363 | | static CURLcode pubkey_pem_to_der(const char *pem, |
364 | | unsigned char **der, size_t *der_len) |
365 | 0 | { |
366 | 0 | const char *begin_pos, *end_pos; |
367 | 0 | size_t pem_count, pem_len; |
368 | 0 | CURLcode result; |
369 | 0 | struct dynbuf pbuf; |
370 | | |
371 | | /* if no pem, exit. */ |
372 | 0 | if(!pem) |
373 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
374 | | |
375 | 0 | curlx_dyn_init(&pbuf, MAX_PINNED_PUBKEY_SIZE); |
376 | |
|
377 | 0 | begin_pos = strstr(pem, "-----BEGIN PUBLIC KEY-----"); |
378 | 0 | if(!begin_pos) |
379 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
380 | | |
381 | 0 | pem_count = begin_pos - pem; |
382 | | /* Invalid if not at beginning AND not directly following \n */ |
383 | 0 | if(pem_count && '\n' != pem[pem_count - 1]) |
384 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
385 | | |
386 | | /* 26 is length of "-----BEGIN PUBLIC KEY-----" */ |
387 | 0 | pem_count += 26; |
388 | | |
389 | | /* Invalid if not directly following \n */ |
390 | 0 | end_pos = strstr(pem + pem_count, "\n-----END PUBLIC KEY-----"); |
391 | 0 | if(!end_pos) |
392 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
393 | | |
394 | 0 | pem_len = end_pos - pem; |
395 | | |
396 | | /* |
397 | | * Here we loop through the pem array one character at a time between the |
398 | | * correct indices, and place each character that is not '\n' or '\r' |
399 | | * into the stripped_pem array, which should represent the raw base64 string |
400 | | */ |
401 | 0 | while(pem_count < pem_len) { |
402 | 0 | if('\n' != pem[pem_count] && '\r' != pem[pem_count]) { |
403 | 0 | result = curlx_dyn_addn(&pbuf, &pem[pem_count], 1); |
404 | 0 | if(result) |
405 | 0 | return result; |
406 | 0 | } |
407 | 0 | ++pem_count; |
408 | 0 | } |
409 | | |
410 | 0 | if(curlx_dyn_len(&pbuf)) { |
411 | 0 | result = curlx_base64_decode(curlx_dyn_ptr(&pbuf), der, der_len); |
412 | 0 | curlx_dyn_free(&pbuf); |
413 | 0 | } |
414 | 0 | else |
415 | 0 | result = CURLE_BAD_CONTENT_ENCODING; |
416 | |
|
417 | 0 | return result; |
418 | 0 | } |
419 | | |
420 | | /* |
421 | | * Generic pinned public key check. |
422 | | */ |
423 | | |
424 | | CURLcode Curl_pin_peer_pubkey(struct Curl_easy *data, |
425 | | const char *pinnedpubkey, |
426 | | const unsigned char *pubkey, size_t pubkeylen) |
427 | 0 | { |
428 | 0 | CURLcode result = CURLE_SSL_PINNEDPUBKEYNOTMATCH; |
429 | | |
430 | | /* if a path was not specified, do not pin */ |
431 | 0 | if(!pinnedpubkey) |
432 | 0 | return CURLE_OK; |
433 | 0 | if(!pubkey || !pubkeylen) |
434 | 0 | return result; |
435 | | |
436 | | /* only do this if pinnedpubkey starts with "sha256//", length 8 */ |
437 | 0 | if(!strncmp(pinnedpubkey, "sha256//", 8)) { |
438 | 0 | CURLcode encode; |
439 | 0 | char *cert_hash = NULL; |
440 | 0 | const char *pinned_hash, *end_pos; |
441 | 0 | size_t cert_hash_len = 0, pinned_hash_len; |
442 | 0 | unsigned char *sha256sumdigest; |
443 | |
|
444 | 0 | if(!Curl_ssl->sha256sum) { |
445 | | /* without sha256 support, this cannot match */ |
446 | 0 | return result; |
447 | 0 | } |
448 | | |
449 | | /* compute sha256sum of public key */ |
450 | 0 | sha256sumdigest = curlx_malloc(CURL_SHA256_DIGEST_LENGTH); |
451 | 0 | if(!sha256sumdigest) |
452 | 0 | return CURLE_OUT_OF_MEMORY; |
453 | 0 | encode = Curl_ssl->sha256sum(pubkey, pubkeylen, |
454 | 0 | sha256sumdigest, CURL_SHA256_DIGEST_LENGTH); |
455 | |
|
456 | 0 | if(!encode) |
457 | 0 | encode = curlx_base64_encode(sha256sumdigest, |
458 | 0 | CURL_SHA256_DIGEST_LENGTH, |
459 | 0 | &cert_hash, &cert_hash_len); |
460 | 0 | curlx_safefree(sha256sumdigest); |
461 | |
|
462 | 0 | if(encode) |
463 | 0 | return encode; |
464 | | |
465 | 0 | infof(data, " public key hash: sha256//%s", cert_hash); |
466 | |
|
467 | 0 | pinned_hash = pinnedpubkey; |
468 | 0 | while(pinned_hash && |
469 | 0 | !strncmp(pinned_hash, "sha256//", CURL_CSTRLEN("sha256//"))) { |
470 | 0 | pinned_hash = pinned_hash + CURL_CSTRLEN("sha256//"); |
471 | 0 | end_pos = strchr(pinned_hash, ';'); |
472 | 0 | pinned_hash_len = end_pos ? |
473 | 0 | (size_t)(end_pos - pinned_hash) : strlen(pinned_hash); |
474 | | |
475 | | /* compare base64 sha256 digests */ |
476 | 0 | if(cert_hash_len == pinned_hash_len && |
477 | 0 | !memcmp(cert_hash, pinned_hash, cert_hash_len)) { |
478 | 0 | DEBUGF(infof(data, "public key hash matches pinned value")); |
479 | 0 | result = CURLE_OK; |
480 | 0 | break; |
481 | 0 | } |
482 | | |
483 | 0 | DEBUGF(infof(data, "public key hash does not match 'sha256//%.*s'", |
484 | 0 | (int)pinned_hash_len, pinned_hash)); |
485 | | /* next one or we are at the end */ |
486 | 0 | pinned_hash = end_pos ? (end_pos + 1) : NULL; |
487 | 0 | } |
488 | 0 | curlx_safefree(cert_hash); |
489 | 0 | } |
490 | 0 | else { |
491 | 0 | long filesize; |
492 | 0 | size_t size, pem_len; |
493 | 0 | CURLcode pem_read; |
494 | 0 | struct dynbuf buf; |
495 | 0 | unsigned char *pem_ptr = NULL; |
496 | 0 | size_t left; |
497 | 0 | FILE *fp = curlx_fopen(pinnedpubkey, "rb"); |
498 | 0 | if(!fp) |
499 | 0 | return result; |
500 | | |
501 | 0 | curlx_dyn_init(&buf, MAX_PINNED_PUBKEY_SIZE); |
502 | | |
503 | | /* Determine the file's size */ |
504 | 0 | if(fseek(fp, 0, SEEK_END)) |
505 | 0 | goto end; |
506 | 0 | filesize = ftell(fp); |
507 | 0 | if(fseek(fp, 0, SEEK_SET)) |
508 | 0 | goto end; |
509 | 0 | if(filesize < 0 || filesize > MAX_PINNED_PUBKEY_SIZE) |
510 | 0 | goto end; |
511 | | |
512 | | /* |
513 | | * if the size of our certificate is bigger than the file |
514 | | * size then it cannot match |
515 | | */ |
516 | 0 | size = curlx_sotouz((curl_off_t)filesize); |
517 | 0 | if(pubkeylen > size) |
518 | 0 | goto end; |
519 | | |
520 | | /* |
521 | | * Read the file into the dynbuf |
522 | | */ |
523 | 0 | left = size; |
524 | 0 | do { |
525 | 0 | char buffer[1024]; |
526 | 0 | size_t want = left > sizeof(buffer) ? sizeof(buffer) : left; |
527 | 0 | if(fread(buffer, 1, want, fp) != want) |
528 | 0 | goto end; |
529 | 0 | if(curlx_dyn_addn(&buf, buffer, want)) |
530 | 0 | goto end; |
531 | 0 | left -= want; |
532 | 0 | } while(left); |
533 | | |
534 | | /* If the sizes are the same, it cannot be base64 encoded, must be der */ |
535 | 0 | if(pubkeylen == size) { |
536 | 0 | if(!memcmp(pubkey, curlx_dyn_ptr(&buf), pubkeylen)) |
537 | 0 | result = CURLE_OK; |
538 | 0 | goto end; |
539 | 0 | } |
540 | | |
541 | | /* |
542 | | * Otherwise we assume it is PEM and try to decode it after placing |
543 | | * null-terminator |
544 | | */ |
545 | 0 | pem_read = pubkey_pem_to_der(curlx_dyn_ptr(&buf), &pem_ptr, &pem_len); |
546 | | /* if it was not read successfully, exit */ |
547 | 0 | if(pem_read) |
548 | 0 | goto end; |
549 | | |
550 | | /* |
551 | | * if the size of our certificate does not match the size of |
552 | | * the decoded file, they cannot be the same, otherwise compare |
553 | | */ |
554 | 0 | if(pubkeylen == pem_len && !memcmp(pubkey, pem_ptr, pubkeylen)) |
555 | 0 | result = CURLE_OK; |
556 | 0 | end: |
557 | 0 | curlx_dyn_free(&buf); |
558 | 0 | curlx_safefree(pem_ptr); |
559 | 0 | curlx_fclose(fp); |
560 | 0 | } |
561 | | |
562 | 0 | return result; |
563 | 0 | } |
564 | | |
565 | | /* |
566 | | * Check whether the SSL backend supports the status_request extension. |
567 | | */ |
568 | | bool Curl_ssl_cert_status_request(void) |
569 | 0 | { |
570 | 0 | if(Curl_ssl->cert_status_request) |
571 | 0 | return Curl_ssl->cert_status_request(); |
572 | 0 | return FALSE; |
573 | 0 | } |
574 | | |
575 | | static int multissl_init(void) |
576 | 0 | { |
577 | 0 | if(multissl_setup(NULL)) |
578 | 0 | return 1; |
579 | 0 | if(Curl_ssl->init) |
580 | 0 | return Curl_ssl->init(); |
581 | 0 | return 1; |
582 | 0 | } |
583 | | |
584 | | static CURLcode multissl_random(struct Curl_easy *data, |
585 | | unsigned char *entropy, size_t length) |
586 | 0 | { |
587 | 0 | if(multissl_setup(NULL)) |
588 | 0 | return CURLE_FAILED_INIT; |
589 | 0 | return Curl_ssl->random(data, entropy, length); |
590 | 0 | } |
591 | | |
592 | | static CURLcode multissl_connect(struct Curl_cfilter *cf, |
593 | | struct Curl_easy *data, bool *done) |
594 | 0 | { |
595 | 0 | if(multissl_setup(NULL)) |
596 | 0 | return CURLE_FAILED_INIT; |
597 | 0 | return Curl_ssl->do_connect(cf, data, done); |
598 | 0 | } |
599 | | |
600 | | static CURLcode multissl_adjust_pollset(struct Curl_cfilter *cf, |
601 | | struct Curl_easy *data, |
602 | | struct easy_pollset *ps) |
603 | 0 | { |
604 | 0 | if(multissl_setup(NULL)) |
605 | 0 | return CURLE_OK; |
606 | 0 | return Curl_ssl->adjust_pollset(cf, data, ps); |
607 | 0 | } |
608 | | |
609 | | static void *multissl_get_internals(struct ssl_connect_data *connssl, |
610 | | CURLINFO info) |
611 | 0 | { |
612 | 0 | if(multissl_setup(NULL)) |
613 | 0 | return NULL; |
614 | 0 | return Curl_ssl->get_internals(connssl, info); |
615 | 0 | } |
616 | | |
617 | | static void multissl_close(struct Curl_cfilter *cf, struct Curl_easy *data) |
618 | 0 | { |
619 | 0 | if(multissl_setup(NULL)) |
620 | 0 | return; |
621 | 0 | Curl_ssl->close(cf, data); |
622 | 0 | } |
623 | | |
624 | | static CURLcode multissl_recv_plain(struct Curl_cfilter *cf, |
625 | | struct Curl_easy *data, |
626 | | char *buf, size_t len, size_t *pnread) |
627 | 0 | { |
628 | 0 | if(multissl_setup(NULL)) |
629 | 0 | return CURLE_FAILED_INIT; |
630 | 0 | return Curl_ssl->recv_plain(cf, data, buf, len, pnread); |
631 | 0 | } |
632 | | |
633 | | static CURLcode multissl_send_plain(struct Curl_cfilter *cf, |
634 | | struct Curl_easy *data, |
635 | | const void *mem, size_t len, |
636 | | size_t *pnwritten) |
637 | 0 | { |
638 | 0 | if(multissl_setup(NULL)) |
639 | 0 | return CURLE_FAILED_INIT; |
640 | 0 | return Curl_ssl->send_plain(cf, data, mem, len, pnwritten); |
641 | 0 | } |
642 | | |
643 | | static const struct Curl_ssl Curl_ssl_multi = { |
644 | | { CURLSSLBACKEND_NONE, "multi" }, /* info */ |
645 | | 0, /* supports nothing */ |
646 | | (size_t)-1, /* something insanely large to be on the safe side */ |
647 | | |
648 | | multissl_init, /* init */ |
649 | | NULL, /* cleanup */ |
650 | | multissl_version, /* version */ |
651 | | NULL, /* shutdown */ |
652 | | NULL, /* data_pending */ |
653 | | multissl_random, /* random */ |
654 | | NULL, /* cert_status_request */ |
655 | | multissl_connect, /* connect */ |
656 | | multissl_adjust_pollset, /* adjust_pollset */ |
657 | | multissl_get_internals, /* get_internals */ |
658 | | multissl_close, /* close_one */ |
659 | | NULL, /* close_all */ |
660 | | NULL, /* engines_list */ |
661 | | NULL, /* sha256sum */ |
662 | | multissl_recv_plain, /* recv decrypted data */ |
663 | | multissl_send_plain, /* send data to encrypt */ |
664 | | NULL, /* get_channel_binding */ |
665 | | }; |
666 | | |
667 | | const struct Curl_ssl *Curl_ssl = |
668 | | #ifdef CURL_WITH_MULTI_SSL |
669 | | &Curl_ssl_multi; |
670 | | #elif defined(USE_WOLFSSL) |
671 | | &Curl_ssl_wolfssl; |
672 | | #elif defined(USE_GNUTLS) |
673 | | &Curl_ssl_gnutls; |
674 | | #elif defined(USE_MBEDTLS) |
675 | | &Curl_ssl_mbedtls; |
676 | | #elif defined(USE_RUSTLS) |
677 | | &Curl_ssl_rustls; |
678 | | #elif defined(USE_OPENSSL) |
679 | | &Curl_ssl_openssl; |
680 | | #elif defined(USE_SCHANNEL) |
681 | | &Curl_ssl_schannel; |
682 | | #else |
683 | | #error "Missing struct Curl_ssl for selected SSL backend" |
684 | | #endif |
685 | | |
686 | | static const struct Curl_ssl *available_backends[] = { |
687 | | #ifdef USE_WOLFSSL |
688 | | &Curl_ssl_wolfssl, |
689 | | #endif |
690 | | #ifdef USE_GNUTLS |
691 | | &Curl_ssl_gnutls, |
692 | | #endif |
693 | | #ifdef USE_MBEDTLS |
694 | | &Curl_ssl_mbedtls, |
695 | | #endif |
696 | | #ifdef USE_OPENSSL |
697 | | &Curl_ssl_openssl, |
698 | | #endif |
699 | | #ifdef USE_SCHANNEL |
700 | | &Curl_ssl_schannel, |
701 | | #endif |
702 | | #ifdef USE_RUSTLS |
703 | | &Curl_ssl_rustls, |
704 | | #endif |
705 | | NULL |
706 | | }; |
707 | | |
708 | | /* Global cleanup */ |
709 | | void Curl_ssl_cleanup(void) |
710 | 0 | { |
711 | 0 | if(init_ssl) { |
712 | | /* only cleanup if we did a previous init */ |
713 | 0 | if(Curl_ssl->cleanup) |
714 | 0 | Curl_ssl->cleanup(); |
715 | | #ifdef CURL_WITH_MULTI_SSL |
716 | | Curl_ssl = &Curl_ssl_multi; |
717 | | #endif |
718 | 0 | init_ssl = FALSE; |
719 | 0 | } |
720 | 0 | } |
721 | | |
722 | | static size_t multissl_version(char *buffer, size_t size) |
723 | 0 | { |
724 | 0 | static const struct Curl_ssl *selected; |
725 | 0 | static char backends[200]; |
726 | 0 | static size_t backends_len; |
727 | 0 | const struct Curl_ssl *current; |
728 | |
|
729 | 0 | current = Curl_ssl == &Curl_ssl_multi ? available_backends[0] : Curl_ssl; |
730 | |
|
731 | 0 | if(current != selected) { |
732 | 0 | char *p = backends; |
733 | 0 | const char *end = backends + sizeof(backends); |
734 | 0 | int i; |
735 | |
|
736 | 0 | selected = current; |
737 | |
|
738 | 0 | backends[0] = '\0'; |
739 | |
|
740 | 0 | for(i = 0; available_backends[i]; ++i) { |
741 | 0 | char vb[200]; |
742 | 0 | bool paren = (selected != available_backends[i]); |
743 | |
|
744 | 0 | if(available_backends[i]->version(vb, sizeof(vb))) { |
745 | 0 | p += curl_msnprintf(p, end - p, "%s%s%s%s", (p != backends ? " " : ""), |
746 | 0 | (paren ? "(" : ""), vb, (paren ? ")" : "")); |
747 | 0 | } |
748 | 0 | } |
749 | |
|
750 | 0 | backends_len = p - backends; |
751 | 0 | } |
752 | |
|
753 | 0 | if(size) { |
754 | 0 | curlx_strcopy(buffer, size, backends, backends_len); |
755 | 0 | } |
756 | 0 | return 0; |
757 | 0 | } |
758 | | |
759 | | static int multissl_setup(const struct Curl_ssl *backend) |
760 | 0 | { |
761 | 0 | int i; |
762 | 0 | char *env; |
763 | |
|
764 | 0 | if(Curl_ssl != &Curl_ssl_multi) |
765 | 0 | return 1; |
766 | | |
767 | 0 | if(backend) { |
768 | 0 | Curl_ssl = backend; |
769 | 0 | return 0; |
770 | 0 | } |
771 | | |
772 | 0 | if(!available_backends[0]) |
773 | 0 | return 1; |
774 | | |
775 | 0 | env = curl_getenv("CURL_SSL_BACKEND"); |
776 | 0 | if(env) { |
777 | 0 | for(i = 0; available_backends[i]; i++) { |
778 | 0 | if(curl_strequal(env, available_backends[i]->info.name)) { |
779 | 0 | Curl_ssl = available_backends[i]; |
780 | 0 | curlx_free(env); |
781 | 0 | return 0; |
782 | 0 | } |
783 | 0 | } |
784 | 0 | } |
785 | | |
786 | | #ifdef CURL_DEFAULT_SSL_BACKEND |
787 | | for(i = 0; available_backends[i]; i++) { |
788 | | if(curl_strequal(CURL_DEFAULT_SSL_BACKEND, |
789 | | available_backends[i]->info.name)) { |
790 | | Curl_ssl = available_backends[i]; |
791 | | curlx_free(env); |
792 | | return 0; |
793 | | } |
794 | | } |
795 | | #endif |
796 | | |
797 | | /* Fall back to first available backend */ |
798 | 0 | Curl_ssl = available_backends[0]; |
799 | 0 | curlx_free(env); |
800 | 0 | return 0; |
801 | 0 | } |
802 | | |
803 | | /* This function is used to select the SSL backend to use. It is called by |
804 | | curl_global_sslset (easy.c) which uses the global init lock. */ |
805 | | CURLsslset Curl_init_sslset_nolock(curl_sslbackend id, const char *name, |
806 | | const curl_ssl_backend ***avail) |
807 | 0 | { |
808 | 0 | int i; |
809 | |
|
810 | 0 | if(avail) |
811 | 0 | *avail = (const curl_ssl_backend **)&available_backends; |
812 | |
|
813 | 0 | if(Curl_ssl != &Curl_ssl_multi) |
814 | 0 | return id == Curl_ssl->info.id || |
815 | 0 | (name && curl_strequal(name, Curl_ssl->info.name)) ? |
816 | 0 | CURLSSLSET_OK : |
817 | | #ifdef CURL_WITH_MULTI_SSL |
818 | | CURLSSLSET_TOO_LATE; |
819 | | #else |
820 | 0 | CURLSSLSET_UNKNOWN_BACKEND; |
821 | 0 | #endif |
822 | | |
823 | 0 | for(i = 0; available_backends[i]; i++) { |
824 | 0 | if(available_backends[i]->info.id == id || |
825 | 0 | (name && curl_strequal(available_backends[i]->info.name, name))) { |
826 | 0 | multissl_setup(available_backends[i]); |
827 | 0 | return CURLSSLSET_OK; |
828 | 0 | } |
829 | 0 | } |
830 | | |
831 | 0 | return CURLSSLSET_UNKNOWN_BACKEND; |
832 | 0 | } |
833 | | |
834 | | #else /* USE_SSL */ |
835 | | CURLsslset Curl_init_sslset_nolock(curl_sslbackend id, const char *name, |
836 | | const curl_ssl_backend ***avail) |
837 | | { |
838 | | (void)id; |
839 | | (void)name; |
840 | | (void)avail; |
841 | | return CURLSSLSET_NO_BACKENDS; |
842 | | } |
843 | | |
844 | | #endif /* !USE_SSL */ |
845 | | |
846 | | #ifdef USE_SSL |
847 | | |
848 | | void Curl_ssl_peer_cleanup(struct ssl_peer *peer) |
849 | 0 | { |
850 | 0 | Curl_peer_unlink(&peer->origin); |
851 | 0 | Curl_peer_unlink(&peer->peer); |
852 | 0 | curlx_safefree(peer->sni); |
853 | 0 | curlx_safefree(peer->scache_key); |
854 | 0 | peer->transport = TRNSPRT_NONE; |
855 | 0 | peer->type = CURL_SSL_PEER_DNS; |
856 | 0 | } |
857 | | |
858 | | static ssl_peer_type get_peer_type(const char *hostname) |
859 | 0 | { |
860 | 0 | if(hostname && hostname[0]) { |
861 | 0 | #ifdef USE_IPV6 |
862 | 0 | struct in6_addr addr; |
863 | | #else |
864 | | struct in_addr addr; |
865 | | #endif |
866 | 0 | if(curlx_inet_pton(AF_INET, hostname, &addr)) |
867 | 0 | return CURL_SSL_PEER_IPV4; |
868 | 0 | #ifdef USE_IPV6 |
869 | 0 | else if(curlx_inet_pton(AF_INET6, hostname, &addr)) { |
870 | 0 | return CURL_SSL_PEER_IPV6; |
871 | 0 | } |
872 | 0 | #endif |
873 | 0 | } |
874 | 0 | return CURL_SSL_PEER_DNS; |
875 | 0 | } |
876 | | |
877 | | CURLcode Curl_ssl_peer_init(struct ssl_peer *ssl_peer, |
878 | | struct Curl_peer *origin, |
879 | | struct Curl_peer *peer, |
880 | | struct ssl_filter_config *sslc, |
881 | | const char *tls_id, |
882 | | uint8_t transport) |
883 | 0 | { |
884 | 0 | CURLcode result = CURLE_OUT_OF_MEMORY; |
885 | | |
886 | | /* We expect a clean struct, e.g. called only ONCE */ |
887 | 0 | if(!ssl_peer || !origin) { |
888 | 0 | DEBUGASSERT(0); |
889 | 0 | return CURLE_FAILED_INIT; |
890 | 0 | } |
891 | 0 | DEBUGASSERT(!ssl_peer->origin); |
892 | 0 | DEBUGASSERT(!ssl_peer->peer); |
893 | 0 | DEBUGASSERT(!ssl_peer->sni); |
894 | 0 | ssl_peer->transport = transport; |
895 | |
|
896 | 0 | Curl_peer_link(&ssl_peer->origin, origin); |
897 | 0 | Curl_peer_link(&ssl_peer->peer, peer); |
898 | 0 | ssl_peer->type = get_peer_type(origin->hostname); |
899 | 0 | if(ssl_peer->type == CURL_SSL_PEER_DNS) { |
900 | | /* not an IP address, normalize according to RCC 6066 ch. 3, |
901 | | * max len of SNI is 2^16-1, no trailing dot */ |
902 | 0 | size_t len = strlen(origin->hostname); |
903 | 0 | if(len && (origin->hostname[len - 1] == '.')) |
904 | 0 | len--; |
905 | 0 | if(len < USHRT_MAX) { |
906 | 0 | ssl_peer->sni = curlx_calloc(1, len + 1); |
907 | 0 | if(!ssl_peer->sni) |
908 | 0 | goto out; |
909 | 0 | Curl_strntolower(ssl_peer->sni, origin->hostname, len); |
910 | 0 | ssl_peer->sni[len] = 0; |
911 | 0 | } |
912 | 0 | } |
913 | | |
914 | 0 | result = Curl_ssl_peer_key_make(ssl_peer, sslc, tls_id, |
915 | 0 | &ssl_peer->scache_key); |
916 | |
|
917 | 0 | out: |
918 | 0 | if(result) |
919 | 0 | Curl_ssl_peer_cleanup(ssl_peer); |
920 | 0 | return result; |
921 | 0 | } |
922 | | |
923 | | static void ssl_cf_destroy(struct Curl_cfilter *cf, struct Curl_easy *data) |
924 | 0 | { |
925 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
926 | 0 | if(connssl) { |
927 | 0 | connssl->ssl_impl->close(cf, data); |
928 | 0 | connssl->state = ssl_connection_none; |
929 | 0 | connssl->connecting_state = ssl_connect_1; |
930 | 0 | connssl->prefs_checked = FALSE; |
931 | 0 | Curl_ssl_peer_cleanup(&connssl->peer); |
932 | 0 | Curl_ssl_session_destroy(connssl->session); |
933 | 0 | cf_ctx_free(connssl); |
934 | 0 | cf->ctx = NULL; |
935 | 0 | } |
936 | 0 | cf->connected = FALSE; |
937 | 0 | } |
938 | | |
939 | | static CURLcode ssl_cf_connect(struct Curl_cfilter *cf, |
940 | | struct Curl_easy *data, |
941 | | bool *done) |
942 | 0 | { |
943 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
944 | 0 | struct cf_call_data save; |
945 | 0 | CURLcode result; |
946 | |
|
947 | 0 | if(cf->connected && (connssl->state != ssl_connection_deferred)) { |
948 | 0 | *done = TRUE; |
949 | 0 | return CURLE_OK; |
950 | 0 | } |
951 | | |
952 | 0 | if(!cf->next || !connssl->peer.origin) { |
953 | 0 | *done = FALSE; |
954 | 0 | return CURLE_FAILED_INIT; |
955 | 0 | } |
956 | | |
957 | 0 | if(!cf->next->connected) { |
958 | 0 | result = cf->next->cft->do_connect(cf->next, data, done); |
959 | 0 | if(result || !*done) |
960 | 0 | return result; |
961 | 0 | } |
962 | | |
963 | 0 | CF_DATA_SAVE(save, cf, data); |
964 | 0 | CURL_TRC_CF(data, cf, "cf_connect()"); |
965 | 0 | DEBUGASSERT(connssl); |
966 | |
|
967 | 0 | *done = FALSE; |
968 | |
|
969 | 0 | if(!connssl->prefs_checked) { |
970 | 0 | if(!ssl_prefs_check(cf, data)) { |
971 | 0 | result = CURLE_SSL_CONNECT_ERROR; |
972 | 0 | goto out; |
973 | 0 | } |
974 | 0 | connssl->prefs_checked = TRUE; |
975 | 0 | } |
976 | | |
977 | 0 | result = connssl->ssl_impl->do_connect(cf, data, done); |
978 | |
|
979 | 0 | if(!result && *done) { |
980 | 0 | cf->connected = TRUE; |
981 | 0 | if(connssl->state == ssl_connection_complete) { |
982 | 0 | connssl->handshake_done = *Curl_pgrs_now(data); |
983 | 0 | } |
984 | 0 | if(Curl_tls_keylog_enabled()) { |
985 | 0 | infof(data, "SSLKEYLOGFILE set, all TLS secrets are logged to '%s'", |
986 | 0 | Curl_tls_keylog_file_name()); |
987 | | #ifdef LIBRESSL_VERSION_NUMBER |
988 | | infof(data, "Note LibreSSL only supports SSLKEYLOGFILE for TLS <= 1.2"); |
989 | | #endif |
990 | 0 | } |
991 | | /* Connection can be deferred when sending early data */ |
992 | 0 | DEBUGASSERT(connssl->state == ssl_connection_complete || |
993 | 0 | connssl->state == ssl_connection_deferred); |
994 | 0 | DEBUGASSERT(connssl->state != ssl_connection_deferred || |
995 | 0 | connssl->earlydata_state > ssl_earlydata_none); |
996 | 0 | } |
997 | 0 | out: |
998 | 0 | CURL_TRC_CF(data, cf, "cf_connect() -> %d, done=%d", (int)result, *done); |
999 | 0 | CF_DATA_RESTORE(cf, save); |
1000 | 0 | return result; |
1001 | 0 | } |
1002 | | |
1003 | | static CURLcode ssl_cf_set_earlydata(struct Curl_cfilter *cf, |
1004 | | struct Curl_easy *data, |
1005 | | const void *buf, size_t blen) |
1006 | 0 | { |
1007 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1008 | 0 | size_t nwritten = 0; |
1009 | 0 | CURLcode result = CURLE_OK; |
1010 | |
|
1011 | 0 | DEBUGASSERT(connssl->earlydata_state == ssl_earlydata_await); |
1012 | 0 | DEBUGASSERT(Curl_bufq_is_empty(&connssl->earlydata)); |
1013 | 0 | if(blen) { |
1014 | 0 | if(blen > connssl->earlydata_max) |
1015 | 0 | blen = connssl->earlydata_max; |
1016 | 0 | result = Curl_bufq_write(&connssl->earlydata, buf, blen, &nwritten); |
1017 | 0 | CURL_TRC_CF(data, cf, "ssl_cf_set_earlydata(len=%zu) -> %zu", |
1018 | 0 | blen, nwritten); |
1019 | 0 | if(result) |
1020 | 0 | return result; |
1021 | 0 | } |
1022 | 0 | return CURLE_OK; |
1023 | 0 | } |
1024 | | |
1025 | | static CURLcode ssl_cf_connect_deferred(struct Curl_cfilter *cf, |
1026 | | struct Curl_easy *data, |
1027 | | const void *buf, size_t blen, |
1028 | | bool *done) |
1029 | 0 | { |
1030 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1031 | 0 | CURLcode result = CURLE_OK; |
1032 | |
|
1033 | 0 | DEBUGASSERT(connssl->state == ssl_connection_deferred); |
1034 | 0 | *done = FALSE; |
1035 | 0 | if(connssl->earlydata_state == ssl_earlydata_await) { |
1036 | 0 | result = ssl_cf_set_earlydata(cf, data, buf, blen); |
1037 | 0 | if(result) |
1038 | 0 | return result; |
1039 | | /* we buffered any early data we would like to send. Actually |
1040 | | * do the connect now which sends it and performs the handshake. */ |
1041 | 0 | connssl->earlydata_state = ssl_earlydata_sending; |
1042 | 0 | connssl->earlydata_skip = Curl_bufq_len(&connssl->earlydata); |
1043 | 0 | } |
1044 | | |
1045 | 0 | result = ssl_cf_connect(cf, data, done); |
1046 | |
|
1047 | 0 | if(!result && *done) { |
1048 | 0 | if(!connssl->stats_reported && (cf->cft == &Curl_cft_ssl)) { |
1049 | 0 | Curl_pgrsTimeWas(data, TIMER_APPCONNECT, connssl->handshake_done); |
1050 | 0 | connssl->stats_reported = TRUE; |
1051 | 0 | } |
1052 | 0 | switch(connssl->earlydata_state) { |
1053 | 0 | case ssl_earlydata_none: |
1054 | 0 | break; |
1055 | 0 | case ssl_earlydata_accepted: |
1056 | 0 | if(!Curl_ssl_cf_is_proxy(cf)) |
1057 | 0 | Curl_pgrsEarlyData(data, (curl_off_t)connssl->earlydata_skip); |
1058 | 0 | infof(data, "Server accepted %zu bytes of TLS early data.", |
1059 | 0 | connssl->earlydata_skip); |
1060 | 0 | break; |
1061 | 0 | case ssl_earlydata_rejected: |
1062 | 0 | if(!Curl_ssl_cf_is_proxy(cf)) |
1063 | 0 | Curl_pgrsEarlyData(data, -(curl_off_t)connssl->earlydata_skip); |
1064 | 0 | infof(data, "Server rejected TLS early data."); |
1065 | 0 | connssl->earlydata_skip = 0; |
1066 | 0 | break; |
1067 | 0 | default: |
1068 | | /* This should not happen. Either we do not use early data or we |
1069 | | * should know if it was accepted or not. */ |
1070 | 0 | DEBUGASSERT(NULL); |
1071 | 0 | break; |
1072 | 0 | } |
1073 | 0 | } |
1074 | 0 | return result; |
1075 | 0 | } |
1076 | | |
1077 | | static bool ssl_cf_data_pending(struct Curl_cfilter *cf, |
1078 | | const struct Curl_easy *data) |
1079 | 0 | { |
1080 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1081 | 0 | struct cf_call_data save; |
1082 | 0 | bool pending; |
1083 | |
|
1084 | 0 | CF_DATA_SAVE(save, cf, data); |
1085 | 0 | if(connssl->ssl_impl->data_pending && |
1086 | 0 | connssl->ssl_impl->data_pending(cf, data)) |
1087 | 0 | pending = TRUE; |
1088 | 0 | else |
1089 | 0 | pending = cf->next->cft->has_data_pending(cf->next, data); |
1090 | 0 | CF_DATA_RESTORE(cf, save); |
1091 | 0 | return pending; |
1092 | 0 | } |
1093 | | |
1094 | | static CURLcode ssl_cf_send(struct Curl_cfilter *cf, |
1095 | | struct Curl_easy *data, |
1096 | | const uint8_t *buf, size_t blen, |
1097 | | bool eos, size_t *pnwritten) |
1098 | 0 | { |
1099 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1100 | 0 | struct cf_call_data save; |
1101 | 0 | CURLcode result = CURLE_OK; |
1102 | |
|
1103 | 0 | (void)eos; |
1104 | 0 | *pnwritten = 0; |
1105 | 0 | CF_DATA_SAVE(save, cf, data); |
1106 | |
|
1107 | 0 | if(connssl->state == ssl_connection_deferred) { |
1108 | 0 | bool done = FALSE; |
1109 | 0 | result = ssl_cf_connect_deferred(cf, data, buf, blen, &done); |
1110 | 0 | if(result) |
1111 | 0 | goto out; |
1112 | 0 | else if(!done) { |
1113 | 0 | result = CURLE_AGAIN; |
1114 | 0 | goto out; |
1115 | 0 | } |
1116 | 0 | DEBUGASSERT(connssl->state == ssl_connection_complete); |
1117 | 0 | } |
1118 | | |
1119 | 0 | if(connssl->earlydata_skip) { |
1120 | 0 | if(connssl->earlydata_skip >= blen) { |
1121 | 0 | connssl->earlydata_skip -= blen; |
1122 | 0 | result = CURLE_OK; |
1123 | 0 | *pnwritten = blen; |
1124 | 0 | goto out; |
1125 | 0 | } |
1126 | 0 | else { |
1127 | 0 | *pnwritten = connssl->earlydata_skip; |
1128 | 0 | buf = buf + connssl->earlydata_skip; |
1129 | 0 | blen -= connssl->earlydata_skip; |
1130 | 0 | connssl->earlydata_skip = 0; |
1131 | 0 | } |
1132 | 0 | } |
1133 | | |
1134 | | /* OpenSSL and maybe other TLS libs do not like 0-length writes. Skip. */ |
1135 | 0 | if(blen > 0) { |
1136 | 0 | size_t nwritten; |
1137 | 0 | result = connssl->ssl_impl->send_plain(cf, data, buf, blen, &nwritten); |
1138 | 0 | if(!result) |
1139 | 0 | *pnwritten += nwritten; |
1140 | 0 | } |
1141 | |
|
1142 | 0 | out: |
1143 | 0 | CF_DATA_RESTORE(cf, save); |
1144 | 0 | return result; |
1145 | 0 | } |
1146 | | |
1147 | | static CURLcode ssl_cf_recv(struct Curl_cfilter *cf, |
1148 | | struct Curl_easy *data, char *buf, size_t len, |
1149 | | size_t *pnread) |
1150 | 0 | { |
1151 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1152 | 0 | struct cf_call_data save; |
1153 | 0 | CURLcode result = CURLE_OK; |
1154 | |
|
1155 | 0 | CF_DATA_SAVE(save, cf, data); |
1156 | 0 | *pnread = 0; |
1157 | 0 | if(connssl->state == ssl_connection_deferred) { |
1158 | 0 | bool done = FALSE; |
1159 | 0 | result = ssl_cf_connect_deferred(cf, data, NULL, 0, &done); |
1160 | 0 | if(result) |
1161 | 0 | goto out; |
1162 | 0 | else if(!done) { |
1163 | 0 | result = CURLE_AGAIN; |
1164 | 0 | goto out; |
1165 | 0 | } |
1166 | 0 | DEBUGASSERT(connssl->state == ssl_connection_complete); |
1167 | 0 | } |
1168 | | |
1169 | 0 | result = connssl->ssl_impl->recv_plain(cf, data, buf, len, pnread); |
1170 | |
|
1171 | 0 | out: |
1172 | 0 | CF_DATA_RESTORE(cf, save); |
1173 | 0 | return result; |
1174 | 0 | } |
1175 | | |
1176 | | static CURLcode ssl_cf_shutdown(struct Curl_cfilter *cf, |
1177 | | struct Curl_easy *data, |
1178 | | bool *done) |
1179 | 0 | { |
1180 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1181 | 0 | CURLcode result = CURLE_OK; |
1182 | |
|
1183 | 0 | *done = TRUE; |
1184 | | /* If we have done the SSL handshake, shut down the connection cleanly */ |
1185 | 0 | if(cf->connected && (connssl->state == ssl_connection_complete) && |
1186 | 0 | !cf->shutdown && Curl_ssl->shut_down) { |
1187 | 0 | struct cf_call_data save; |
1188 | |
|
1189 | 0 | CF_DATA_SAVE(save, cf, data); |
1190 | 0 | result = connssl->ssl_impl->shut_down(cf, data, TRUE, done); |
1191 | 0 | CURL_TRC_CF(data, cf, "cf_shutdown -> %d, done=%d", (int)result, *done); |
1192 | 0 | CF_DATA_RESTORE(cf, save); |
1193 | 0 | cf->shutdown = (result || *done); |
1194 | 0 | } |
1195 | 0 | return result; |
1196 | 0 | } |
1197 | | |
1198 | | static CURLcode ssl_cf_adjust_pollset(struct Curl_cfilter *cf, |
1199 | | struct Curl_easy *data, |
1200 | | struct easy_pollset *ps) |
1201 | 0 | { |
1202 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1203 | 0 | struct cf_call_data save; |
1204 | 0 | CURLcode result; |
1205 | |
|
1206 | 0 | CF_DATA_SAVE(save, cf, data); |
1207 | 0 | result = connssl->ssl_impl->adjust_pollset(cf, data, ps); |
1208 | 0 | CF_DATA_RESTORE(cf, save); |
1209 | 0 | return result; |
1210 | 0 | } |
1211 | | |
1212 | | static CURLcode ssl_cf_query(struct Curl_cfilter *cf, |
1213 | | struct Curl_easy *data, |
1214 | | int query, int *pres1, void *pres2) |
1215 | 0 | { |
1216 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1217 | |
|
1218 | 0 | switch(query) { |
1219 | 0 | case CF_QUERY_SSL_INFO: |
1220 | 0 | case CF_QUERY_SSL_CTX_INFO: |
1221 | 0 | if(!Curl_ssl_cf_is_proxy(cf)) { |
1222 | 0 | struct curl_tlssessioninfo *info = pres2; |
1223 | 0 | struct cf_call_data save; |
1224 | 0 | CF_DATA_SAVE(save, cf, data); |
1225 | 0 | info->backend = Curl_ssl_backend(); |
1226 | 0 | info->internals = connssl->ssl_impl->get_internals( |
1227 | 0 | cf->ctx, (query == CF_QUERY_SSL_INFO) ? |
1228 | 0 | CURLINFO_TLS_SSL_PTR : CURLINFO_TLS_SESSION); |
1229 | 0 | CF_DATA_RESTORE(cf, save); |
1230 | 0 | return CURLE_OK; |
1231 | 0 | } |
1232 | 0 | break; |
1233 | 0 | case CF_QUERY_ALPN_NEGOTIATED: { |
1234 | 0 | const char **palpn = pres2; |
1235 | 0 | DEBUGASSERT(palpn); |
1236 | 0 | *palpn = connssl->negotiated.alpn; |
1237 | 0 | CURL_TRC_CF(data, cf, "query ALPN: returning '%s'", *palpn); |
1238 | 0 | return CURLE_OK; |
1239 | 0 | } |
1240 | 0 | default: |
1241 | 0 | break; |
1242 | 0 | } |
1243 | 0 | return cf->next ? |
1244 | 0 | cf->next->cft->query(cf->next, data, query, pres1, pres2) : |
1245 | 0 | CURLE_UNKNOWN_OPTION; |
1246 | 0 | } |
1247 | | |
1248 | | static CURLcode ssl_cf_cntrl(struct Curl_cfilter *cf, |
1249 | | struct Curl_easy *data, |
1250 | | int event, int arg1, void *arg2) |
1251 | 0 | { |
1252 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1253 | |
|
1254 | 0 | (void)arg1; |
1255 | 0 | (void)arg2; |
1256 | 0 | (void)data; |
1257 | 0 | switch(event) { |
1258 | 0 | case CF_CTRL_CONN_INFO_UPDATE: |
1259 | 0 | if(connssl->negotiated.alpn && !cf->sockindex) { |
1260 | 0 | if(!strcmp("http/1.1", connssl->negotiated.alpn)) |
1261 | 0 | cf->conn->httpversion_seen = 11; |
1262 | 0 | else if(!strcmp("h2", connssl->negotiated.alpn)) |
1263 | 0 | cf->conn->httpversion_seen = 20; |
1264 | 0 | else if(!strcmp("h3", connssl->negotiated.alpn)) |
1265 | 0 | cf->conn->httpversion_seen = 30; |
1266 | 0 | } |
1267 | 0 | break; |
1268 | 0 | case CF_CTRL_REPORT_STATS: |
1269 | 0 | if(cf->connected && !connssl->stats_reported && |
1270 | 0 | (cf->cft == &Curl_cft_ssl) && |
1271 | 0 | (connssl->handshake_done.tv_sec || connssl->handshake_done.tv_usec)) { |
1272 | 0 | Curl_pgrsTimeWas(data, TIMER_APPCONNECT, connssl->handshake_done); |
1273 | 0 | connssl->stats_reported = TRUE; |
1274 | 0 | } |
1275 | 0 | break; |
1276 | 0 | } |
1277 | 0 | return CURLE_OK; |
1278 | 0 | } |
1279 | | |
1280 | | static bool cf_ssl_is_alive(struct Curl_cfilter *cf, struct Curl_easy *data, |
1281 | | bool *input_pending) |
1282 | 0 | { |
1283 | | /* |
1284 | | * This function tries to determine connection status. |
1285 | | */ |
1286 | 0 | return cf->next ? |
1287 | 0 | cf->next->cft->is_alive(cf->next, data, input_pending) : |
1288 | 0 | FALSE; /* pessimistic in absence of data */ |
1289 | 0 | } |
1290 | | |
1291 | | struct Curl_cftype Curl_cft_ssl = { |
1292 | | "SSL", |
1293 | | CF_TYPE_SSL, |
1294 | | CURL_LOG_LVL_NONE, |
1295 | | ssl_cf_destroy, |
1296 | | ssl_cf_connect, |
1297 | | ssl_cf_shutdown, |
1298 | | ssl_cf_adjust_pollset, |
1299 | | ssl_cf_data_pending, |
1300 | | ssl_cf_send, |
1301 | | ssl_cf_recv, |
1302 | | ssl_cf_cntrl, |
1303 | | cf_ssl_is_alive, |
1304 | | Curl_cf_def_conn_keep_alive, |
1305 | | ssl_cf_query, |
1306 | | }; |
1307 | | |
1308 | | #ifndef CURL_DISABLE_PROXY |
1309 | | |
1310 | | struct Curl_cftype Curl_cft_ssl_proxy = { |
1311 | | "SSL-PROXY", |
1312 | | CF_TYPE_SSL | CF_TYPE_PROXY, |
1313 | | CURL_LOG_LVL_NONE, |
1314 | | ssl_cf_destroy, |
1315 | | ssl_cf_connect, |
1316 | | ssl_cf_shutdown, |
1317 | | ssl_cf_adjust_pollset, |
1318 | | ssl_cf_data_pending, |
1319 | | ssl_cf_send, |
1320 | | ssl_cf_recv, |
1321 | | Curl_cf_def_cntrl, |
1322 | | cf_ssl_is_alive, |
1323 | | Curl_cf_def_conn_keep_alive, |
1324 | | ssl_cf_query, |
1325 | | }; |
1326 | | |
1327 | | #endif /* !CURL_DISABLE_PROXY */ |
1328 | | |
1329 | | static CURLcode cf_ssl_create(struct Curl_cfilter **pcf, |
1330 | | struct Curl_easy *data, |
1331 | | struct connectdata *conn) |
1332 | 0 | { |
1333 | 0 | struct Curl_cfilter *cf = NULL; |
1334 | 0 | struct ssl_connect_data *ctx; |
1335 | 0 | CURLcode result; |
1336 | |
|
1337 | 0 | DEBUGASSERT(data->conn); |
1338 | |
|
1339 | | #ifdef CURL_DISABLE_HTTP |
1340 | | (void)conn; |
1341 | | /* We only support ALPN for HTTP so far. */ |
1342 | | DEBUGASSERT(!conn->bits.tls_enable_alpn); |
1343 | | ctx = cf_ctx_new(data, NULL); |
1344 | | #else |
1345 | 0 | ctx = cf_ctx_new(data, alpn_get_spec(data->state.http_neg.wanted, |
1346 | 0 | data->state.http_neg.preferred, |
1347 | 0 | (bool)data->state.http_neg.only_10, |
1348 | 0 | (bool)conn->bits.tls_enable_alpn)); |
1349 | 0 | #endif |
1350 | 0 | if(!ctx) { |
1351 | 0 | result = CURLE_OUT_OF_MEMORY; |
1352 | 0 | goto out; |
1353 | 0 | } |
1354 | | |
1355 | 0 | result = Curl_cf_create(&cf, &Curl_cft_ssl, ctx); |
1356 | |
|
1357 | 0 | out: |
1358 | 0 | if(result) |
1359 | 0 | cf_ctx_free(ctx); |
1360 | 0 | *pcf = result ? NULL : cf; |
1361 | 0 | return result; |
1362 | 0 | } |
1363 | | |
1364 | | static CURLcode cf_ssl_peer_init(struct Curl_cfilter *cf, |
1365 | | struct Curl_peer *origin, |
1366 | | struct Curl_peer *peer, |
1367 | | struct ssl_filter_config *sslc) |
1368 | 0 | { |
1369 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1370 | 0 | char tls_id[80]; |
1371 | 0 | connssl->ssl_impl->version(tls_id, sizeof(tls_id) - 1); |
1372 | 0 | return Curl_ssl_peer_init(&connssl->peer, origin, peer, sslc, |
1373 | 0 | tls_id, TRNSPRT_TCP); |
1374 | 0 | } |
1375 | | |
1376 | | CURLcode Curl_ssl_cfilter_add(struct Curl_easy *data, |
1377 | | struct Curl_peer *origin, |
1378 | | struct connectdata *conn, |
1379 | | int8_t sockindex) |
1380 | 0 | { |
1381 | 0 | struct Curl_cfilter *cf; |
1382 | 0 | struct Curl_peer *peer = (sockindex == SECONDARYSOCKET) ? |
1383 | 0 | conn->via_peer2 : conn->via_peer; |
1384 | 0 | CURLcode result; |
1385 | |
|
1386 | 0 | result = cf_ssl_create(&cf, data, conn); |
1387 | 0 | if(!result) |
1388 | 0 | result = cf_ssl_peer_init(cf, origin, peer, &conn->ssl_config); |
1389 | 0 | if(!result) |
1390 | 0 | Curl_conn_cf_add(data, conn, sockindex, cf); |
1391 | 0 | else if(cf) |
1392 | 0 | Curl_conn_cf_discard_chain(&cf, data); |
1393 | 0 | return result; |
1394 | 0 | } |
1395 | | |
1396 | | CURLcode Curl_cf_ssl_insert_after(struct Curl_cfilter *cf_at, |
1397 | | struct Curl_easy *data, |
1398 | | struct Curl_peer *origin, |
1399 | | struct Curl_peer *peer) |
1400 | 0 | { |
1401 | 0 | struct Curl_cfilter *cf; |
1402 | 0 | CURLcode result; |
1403 | |
|
1404 | 0 | result = cf_ssl_create(&cf, data, cf_at->conn); |
1405 | 0 | if(!result) |
1406 | 0 | result = cf_ssl_peer_init(cf, origin, peer, &cf_at->conn->ssl_config); |
1407 | 0 | if(!result) { |
1408 | 0 | Curl_conn_cf_insert_after(cf_at, cf); |
1409 | 0 | #if defined(USE_HTTPSRR) && defined(USE_ECH) |
1410 | | /* When using ECH, kick off the HTTPS-RR resolve */ |
1411 | 0 | if((origin->scheme->family == CURLPROTO_HTTP) && |
1412 | 0 | Curl_ssl_need_httpsrr(cf)) { |
1413 | 0 | result = Curl_conn_dns_add_https_resolve(data, cf->conn, cf->sockindex, |
1414 | 0 | origin); |
1415 | 0 | } |
1416 | 0 | #endif /* USE_HTTPSRR && USE_ECH */ |
1417 | 0 | } |
1418 | 0 | else if(cf) |
1419 | 0 | Curl_conn_cf_discard_chain(&cf, data); |
1420 | 0 | return result; |
1421 | 0 | } |
1422 | | |
1423 | | #ifndef CURL_DISABLE_PROXY |
1424 | | |
1425 | | static CURLcode cf_ssl_proxy_create(struct Curl_cfilter **pcf, |
1426 | | struct Curl_easy *data, |
1427 | | struct connectdata *conn) |
1428 | 0 | { |
1429 | 0 | struct Curl_cfilter *cf = NULL; |
1430 | 0 | struct ssl_connect_data *ctx; |
1431 | 0 | CURLcode result; |
1432 | | /* ALPN is default, but if user explicitly disables it, obey */ |
1433 | 0 | bool use_alpn = (bool)data->set.ssl_enable_alpn; |
1434 | 0 | http_majors wanted = CURL_HTTP_V1x; |
1435 | |
|
1436 | 0 | (void)conn; |
1437 | 0 | #ifdef USE_HTTP2 |
1438 | 0 | if(conn->http_proxy.proxytype == CURLPROXY_HTTPS2) { |
1439 | 0 | use_alpn = TRUE; |
1440 | 0 | wanted = (CURL_HTTP_V1x | CURL_HTTP_V2x); |
1441 | 0 | } |
1442 | 0 | #endif |
1443 | |
|
1444 | 0 | ctx = cf_ctx_new(data, alpn_get_spec(wanted, 0, FALSE, use_alpn)); |
1445 | 0 | if(!ctx) { |
1446 | 0 | result = CURLE_OUT_OF_MEMORY; |
1447 | 0 | goto out; |
1448 | 0 | } |
1449 | 0 | result = Curl_cf_create(&cf, &Curl_cft_ssl_proxy, ctx); |
1450 | |
|
1451 | 0 | out: |
1452 | 0 | if(result) |
1453 | 0 | cf_ctx_free(ctx); |
1454 | 0 | *pcf = result ? NULL : cf; |
1455 | 0 | return result; |
1456 | 0 | } |
1457 | | |
1458 | | CURLcode Curl_cf_ssl_proxy_insert_after(struct Curl_cfilter *cf_at, |
1459 | | struct Curl_easy *data, |
1460 | | struct Curl_peer *peer) |
1461 | 0 | { |
1462 | 0 | struct Curl_cfilter *cf; |
1463 | 0 | CURLcode result; |
1464 | |
|
1465 | 0 | result = cf_ssl_proxy_create(&cf, data, cf_at->conn); |
1466 | 0 | if(!result) |
1467 | 0 | result = cf_ssl_peer_init(cf, peer, NULL, &cf_at->conn->proxy_ssl_config); |
1468 | 0 | if(!result) |
1469 | 0 | Curl_conn_cf_insert_after(cf_at, cf); |
1470 | 0 | else if(cf) |
1471 | 0 | Curl_conn_cf_discard_chain(&cf, data); |
1472 | 0 | return result; |
1473 | 0 | } |
1474 | | |
1475 | | #endif /* !CURL_DISABLE_PROXY */ |
1476 | | |
1477 | | bool Curl_ssl_supports(struct Curl_easy *data, unsigned int ssl_option) |
1478 | 0 | { |
1479 | 0 | (void)data; |
1480 | 0 | return (Curl_ssl->supports & ssl_option); |
1481 | 0 | } |
1482 | | |
1483 | | #ifdef USE_ECH |
1484 | | bool Curl_ssl_ech_enabled(struct Curl_cfilter *cf) |
1485 | 0 | { |
1486 | 0 | if(Curl_ssl->supports & SSLSUPP_ECH) { |
1487 | 0 | struct ssl_filter_config *conn_config = Curl_ssl_cf_get_filter_config(cf); |
1488 | 0 | return !!conn_config->ech; |
1489 | 0 | } |
1490 | 0 | return FALSE; |
1491 | 0 | } |
1492 | | |
1493 | | bool Curl_ssl_need_httpsrr(struct Curl_cfilter *cf) |
1494 | 0 | { |
1495 | 0 | if(Curl_ssl->supports & SSLSUPP_ECH) { |
1496 | 0 | struct ssl_filter_config *conn_config = Curl_ssl_cf_get_filter_config(cf); |
1497 | 0 | if(!conn_config->ech) |
1498 | 0 | return FALSE; |
1499 | 0 | if((conn_config->ech == CURLECH_GREASE) || conn_config->ech_config) |
1500 | 0 | return FALSE; |
1501 | 0 | return TRUE; |
1502 | 0 | } |
1503 | 0 | return FALSE; |
1504 | 0 | } |
1505 | | #endif |
1506 | | |
1507 | | static CURLcode vtls_shutdown_blocking(struct Curl_cfilter *cf, |
1508 | | struct Curl_easy *data, |
1509 | | bool send_shutdown, bool *done) |
1510 | 0 | { |
1511 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1512 | 0 | struct cf_call_data save; |
1513 | 0 | CURLcode result = CURLE_OK; |
1514 | 0 | timediff_t timeout_ms; |
1515 | 0 | int what, loop = 10; |
1516 | |
|
1517 | 0 | if(cf->shutdown) { |
1518 | 0 | *done = TRUE; |
1519 | 0 | return CURLE_OK; |
1520 | 0 | } |
1521 | 0 | CF_DATA_SAVE(save, cf, data); |
1522 | |
|
1523 | 0 | *done = FALSE; |
1524 | 0 | while(!result && !*done && loop--) { |
1525 | 0 | timeout_ms = Curl_cshutdn_timeleft_ms(data, cf->conn, cf->sockindex); |
1526 | |
|
1527 | 0 | if(timeout_ms < 0) { |
1528 | | /* no need to continue if time is already up */ |
1529 | 0 | failf(data, "SSL shutdown timeout"); |
1530 | 0 | result = CURLE_OPERATION_TIMEDOUT; |
1531 | 0 | goto out; |
1532 | 0 | } |
1533 | | |
1534 | 0 | result = connssl->ssl_impl->shut_down(cf, data, send_shutdown, done); |
1535 | 0 | if(result || *done) |
1536 | 0 | goto out; |
1537 | | |
1538 | 0 | if(connssl->io_need) { |
1539 | 0 | what = Curl_conn_cf_poll(cf, data, timeout_ms); |
1540 | 0 | if(what < 0) { |
1541 | | /* fatal error */ |
1542 | 0 | failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO); |
1543 | 0 | result = CURLE_RECV_ERROR; |
1544 | 0 | goto out; |
1545 | 0 | } |
1546 | 0 | else if(what == 0) { |
1547 | | /* timeout */ |
1548 | 0 | failf(data, "SSL shutdown timeout"); |
1549 | 0 | result = CURLE_OPERATION_TIMEDOUT; |
1550 | 0 | goto out; |
1551 | 0 | } |
1552 | | /* socket is readable or writable */ |
1553 | 0 | } |
1554 | 0 | } |
1555 | 0 | out: |
1556 | 0 | CF_DATA_RESTORE(cf, save); |
1557 | 0 | cf->shutdown = (result || *done); |
1558 | 0 | return result; |
1559 | 0 | } |
1560 | | |
1561 | | CURLcode Curl_ssl_cfilter_remove(struct Curl_easy *data, |
1562 | | int8_t sockindex, bool send_shutdown) |
1563 | 0 | { |
1564 | 0 | struct Curl_cfilter *cf, *head; |
1565 | 0 | CURLcode result = CURLE_OK; |
1566 | |
|
1567 | 0 | head = data->conn ? data->conn->cfilter[sockindex] : NULL; |
1568 | 0 | for(cf = head; cf; cf = cf->next) { |
1569 | 0 | if(cf->cft == &Curl_cft_ssl) { |
1570 | 0 | bool done; |
1571 | 0 | CURL_TRC_CF(data, cf, "shutdown and remove SSL, start"); |
1572 | 0 | Curl_cshutdn_start_timer(data, sockindex, 0); |
1573 | 0 | result = vtls_shutdown_blocking(cf, data, send_shutdown, &done); |
1574 | 0 | Curl_cshutdn_clear_timer(data, sockindex); |
1575 | 0 | if(!result && !done) /* blocking failed? */ |
1576 | 0 | result = CURLE_SSL_SHUTDOWN_FAILED; |
1577 | 0 | Curl_conn_cf_discard(&cf, data); |
1578 | 0 | CURL_TRC_CF(data, cf, "shutdown and remove SSL, done -> %d", |
1579 | 0 | (int)result); |
1580 | 0 | break; |
1581 | 0 | } |
1582 | 0 | } |
1583 | 0 | return result; |
1584 | 0 | } |
1585 | | |
1586 | | bool Curl_ssl_cf_is_proxy(struct Curl_cfilter *cf) |
1587 | 0 | { |
1588 | 0 | return (cf->cft->flags & CF_TYPE_SSL) && (cf->cft->flags & CF_TYPE_PROXY); |
1589 | 0 | } |
1590 | | |
1591 | | struct ssl_easy_config * |
1592 | | Curl_ssl_cf_get_easy_config(struct Curl_cfilter *cf, |
1593 | | struct Curl_easy *data) |
1594 | 0 | { |
1595 | | #ifdef CURL_DISABLE_PROXY |
1596 | | (void)cf; |
1597 | | return &data->set.ssl; |
1598 | | #else |
1599 | 0 | return Curl_ssl_cf_is_proxy(cf) ? &data->set.proxy_ssl : &data->set.ssl; |
1600 | 0 | #endif |
1601 | 0 | } |
1602 | | |
1603 | | struct ssl_filter_config * |
1604 | | Curl_ssl_cf_get_filter_config(struct Curl_cfilter *cf) |
1605 | 0 | { |
1606 | | #ifdef CURL_DISABLE_PROXY |
1607 | | return &cf->conn->ssl_config; |
1608 | | #else |
1609 | 0 | return Curl_ssl_cf_is_proxy(cf) ? |
1610 | 0 | &cf->conn->proxy_ssl_config : &cf->conn->ssl_config; |
1611 | 0 | #endif |
1612 | 0 | } |
1613 | | |
1614 | | CURLcode Curl_alpn_to_proto_buf(struct alpn_proto_buf *buf, |
1615 | | const struct alpn_spec *spec) |
1616 | 0 | { |
1617 | 0 | size_t i, len; |
1618 | 0 | int off = 0; |
1619 | 0 | unsigned char blen; |
1620 | |
|
1621 | 0 | memset(buf, 0, sizeof(*buf)); |
1622 | 0 | for(i = 0; spec && i < spec->count; ++i) { |
1623 | 0 | len = strlen(spec->entries[i]); |
1624 | 0 | if(len >= ALPN_NAME_MAX) |
1625 | 0 | return CURLE_FAILED_INIT; |
1626 | 0 | blen = (unsigned char)len; |
1627 | 0 | if(off + blen + 1 >= (int)sizeof(buf->data)) |
1628 | 0 | return CURLE_FAILED_INIT; |
1629 | 0 | buf->data[off++] = blen; |
1630 | 0 | memcpy(buf->data + off, spec->entries[i], blen); |
1631 | 0 | off += blen; |
1632 | 0 | } |
1633 | 0 | buf->len = off; |
1634 | 0 | return CURLE_OK; |
1635 | 0 | } |
1636 | | |
1637 | | CURLcode Curl_alpn_to_proto_str(struct alpn_proto_buf *buf, |
1638 | | const struct alpn_spec *spec) |
1639 | 0 | { |
1640 | 0 | size_t i, len; |
1641 | 0 | size_t off = 0; |
1642 | |
|
1643 | 0 | memset(buf, 0, sizeof(*buf)); |
1644 | 0 | for(i = 0; spec && i < spec->count; ++i) { |
1645 | 0 | len = strlen(spec->entries[i]); |
1646 | 0 | if(len >= ALPN_NAME_MAX) |
1647 | 0 | return CURLE_FAILED_INIT; |
1648 | 0 | if(off + len + 2 >= sizeof(buf->data)) |
1649 | 0 | return CURLE_FAILED_INIT; |
1650 | 0 | if(off) |
1651 | 0 | buf->data[off++] = ','; |
1652 | 0 | memcpy(buf->data + off, spec->entries[i], len); |
1653 | 0 | off += len; |
1654 | 0 | } |
1655 | 0 | buf->data[off] = '\0'; |
1656 | 0 | buf->len = (int)off; |
1657 | 0 | return CURLE_OK; |
1658 | 0 | } |
1659 | | |
1660 | | bool Curl_alpn_contains_proto(const struct alpn_spec *spec, |
1661 | | const char *proto) |
1662 | 0 | { |
1663 | 0 | size_t i, plen = proto ? strlen(proto) : 0; |
1664 | 0 | for(i = 0; spec && plen && i < spec->count; ++i) { |
1665 | 0 | size_t slen = strlen(spec->entries[i]); |
1666 | 0 | if((slen == plen) && !memcmp(proto, spec->entries[i], plen)) |
1667 | 0 | return TRUE; |
1668 | 0 | } |
1669 | 0 | return FALSE; |
1670 | 0 | } |
1671 | | |
1672 | | void Curl_alpn_restrict_to(struct alpn_spec *spec, const char *proto) |
1673 | 0 | { |
1674 | 0 | size_t plen = strlen(proto); |
1675 | 0 | DEBUGASSERT(plen < sizeof(spec->entries[0])); |
1676 | 0 | if(plen < sizeof(spec->entries[0])) { |
1677 | 0 | memcpy(spec->entries[0], proto, plen + 1); |
1678 | 0 | spec->count = 1; |
1679 | 0 | } |
1680 | 0 | } |
1681 | | |
1682 | | void Curl_alpn_copy(struct alpn_spec *dest, const struct alpn_spec *src) |
1683 | 0 | { |
1684 | 0 | if(src) |
1685 | 0 | memcpy(dest, src, sizeof(*dest)); |
1686 | 0 | else |
1687 | 0 | memset(dest, 0, sizeof(*dest)); |
1688 | 0 | } |
1689 | | |
1690 | | CURLcode Curl_alpn_set_negotiated(struct Curl_cfilter *cf, |
1691 | | struct Curl_easy *data, |
1692 | | struct ssl_connect_data *connssl, |
1693 | | const unsigned char *proto, |
1694 | | size_t proto_len) |
1695 | 0 | { |
1696 | 0 | CURLcode result = CURLE_OK; |
1697 | 0 | (void)cf; |
1698 | |
|
1699 | 0 | if(connssl->negotiated.alpn) { |
1700 | | /* When we ask for a specific ALPN protocol, we need the confirmation |
1701 | | * of it by the server, as we have installed protocol handler and |
1702 | | * connection filter chain for exactly this protocol. */ |
1703 | 0 | if(!proto_len) { |
1704 | 0 | failf(data, "ALPN: asked for '%s' from previous session, " |
1705 | 0 | "but server did not confirm it. Refusing to continue.", |
1706 | 0 | connssl->negotiated.alpn); |
1707 | 0 | result = CURLE_SSL_CONNECT_ERROR; |
1708 | 0 | goto out; |
1709 | 0 | } |
1710 | 0 | else if(!proto) { |
1711 | 0 | DEBUGASSERT(0); /* with length, we need a pointer */ |
1712 | 0 | result = CURLE_SSL_CONNECT_ERROR; |
1713 | 0 | goto out; |
1714 | 0 | } |
1715 | 0 | else if((strlen(connssl->negotiated.alpn) != proto_len) || |
1716 | 0 | memcmp(connssl->negotiated.alpn, proto, proto_len)) { |
1717 | 0 | failf(data, "ALPN: asked for '%s' from previous session, but server " |
1718 | 0 | "selected '%.*s'. Refusing to continue.", |
1719 | 0 | connssl->negotiated.alpn, (int)proto_len, proto); |
1720 | 0 | result = CURLE_SSL_CONNECT_ERROR; |
1721 | 0 | goto out; |
1722 | 0 | } |
1723 | | /* ALPN is exactly what we asked for, done. */ |
1724 | 0 | infof(data, "ALPN: server confirmed to use '%s'", |
1725 | 0 | connssl->negotiated.alpn); |
1726 | 0 | goto out; |
1727 | 0 | } |
1728 | | |
1729 | 0 | if(proto && proto_len) { |
1730 | 0 | if(memchr(proto, '\0', proto_len)) { |
1731 | 0 | failf(data, "ALPN: server selected protocol contains NUL. " |
1732 | 0 | "Refusing to continue."); |
1733 | 0 | result = CURLE_SSL_CONNECT_ERROR; |
1734 | 0 | goto out; |
1735 | 0 | } |
1736 | 0 | connssl->negotiated.alpn = curlx_memdup0((const char *)proto, proto_len); |
1737 | 0 | if(!connssl->negotiated.alpn) |
1738 | 0 | return CURLE_OUT_OF_MEMORY; |
1739 | 0 | } |
1740 | | |
1741 | 0 | if(proto && proto_len) { |
1742 | 0 | if(connssl->state == ssl_connection_deferred) |
1743 | 0 | infof(data, VTLS_INFOF_ALPN_DEFERRED, (int)proto_len, proto); |
1744 | 0 | else |
1745 | 0 | infof(data, VTLS_INFOF_ALPN_ACCEPTED, (int)proto_len, proto); |
1746 | 0 | } |
1747 | 0 | else { |
1748 | 0 | if(connssl->state == ssl_connection_deferred) |
1749 | 0 | infof(data, VTLS_INFOF_NO_ALPN_DEFERRED); |
1750 | 0 | else |
1751 | 0 | infof(data, VTLS_INFOF_NO_ALPN); |
1752 | 0 | } |
1753 | |
|
1754 | 0 | out: |
1755 | 0 | return result; |
1756 | 0 | } |
1757 | | |
1758 | | CURLcode Curl_on_session_reuse(struct Curl_cfilter *cf, |
1759 | | struct Curl_easy *data, |
1760 | | struct alpn_spec *alpns, |
1761 | | struct Curl_ssl_session *scs, |
1762 | | bool *do_early_data, bool early_data_allowed) |
1763 | 0 | { |
1764 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1765 | 0 | CURLcode result = CURLE_OK; |
1766 | |
|
1767 | 0 | *do_early_data = FALSE; |
1768 | |
|
1769 | 0 | if(!early_data_allowed) { |
1770 | 0 | CURL_TRC_CF(data, cf, "SSL session does not allow earlydata"); |
1771 | 0 | } |
1772 | 0 | else if(!Curl_alpn_contains_proto(alpns, scs->alpn)) { |
1773 | 0 | CURL_TRC_CF(data, cf, "SSL session has different ALPN, no early data"); |
1774 | 0 | } |
1775 | 0 | else { |
1776 | 0 | infof(data, "SSL session allows %zu bytes of early data, " |
1777 | 0 | "reusing ALPN '%s'", connssl->earlydata_max, scs->alpn); |
1778 | 0 | connssl->earlydata_state = ssl_earlydata_await; |
1779 | 0 | connssl->state = ssl_connection_deferred; |
1780 | 0 | result = Curl_alpn_set_negotiated(cf, data, connssl, |
1781 | 0 | (const unsigned char *)scs->alpn, |
1782 | 0 | scs->alpn ? strlen(scs->alpn) : 0); |
1783 | 0 | *do_early_data = !result; |
1784 | 0 | } |
1785 | 0 | return result; |
1786 | 0 | } |
1787 | | |
1788 | | struct Curl_ssl_session *Curl_ssl_get_cf_session(struct Curl_easy *data, |
1789 | | const struct Curl_cftype *cft, |
1790 | | int8_t sockindex) |
1791 | 0 | { |
1792 | 0 | if(data->conn && |
1793 | 0 | #ifndef CURL_DISABLE_PROXY |
1794 | 0 | ((cft == &Curl_cft_ssl) || (cft == &Curl_cft_ssl_proxy))) { |
1795 | | #else |
1796 | | (cft == &Curl_cft_ssl)) { |
1797 | | #endif |
1798 | 0 | struct Curl_cfilter *cf1 = data->conn->cfilter[sockindex]; |
1799 | 0 | for(; cf1; cf1 = cf1->next) { |
1800 | | /* A tunneling proxy does not offer end2end encryption, even if |
1801 | | * it does SSL itself (e.g. QUIC H3 proxy) */ |
1802 | 0 | if(cf1->cft == cft) |
1803 | 0 | break; |
1804 | 0 | } |
1805 | 0 | if(cf1) { |
1806 | 0 | struct ssl_connect_data *connssl = cf1->ctx; |
1807 | 0 | if(connssl) |
1808 | 0 | return connssl->session; |
1809 | 0 | } |
1810 | 0 | } |
1811 | 0 | return NULL; |
1812 | 0 | } |
1813 | | |
1814 | | #endif /* USE_SSL */ |