Coverage Report

Created: 2025-12-04 06:52

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