Coverage Report

Created: 2025-11-24 06:58

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