Coverage Report

Created: 2025-10-30 06:17

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