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