Coverage Report

Created: 2026-09-14 07:06

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