Coverage Report

Created: 2025-08-26 07:08

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