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