Coverage Report

Created: 2025-10-10 06:31

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