Coverage Report

Created: 2025-08-03 06:36

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