Coverage Report

Created: 2026-05-30 06:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/vtls/openssl.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
/*
25
 * Source file for all OpenSSL-specific code for the TLS/SSL layer. No code
26
 * but vtls.c should ever call or use these functions.
27
 */
28
#include "curl_setup.h"
29
30
#ifdef USE_OPENSSL
31
32
#include "urldata.h"
33
#include "curl_trc.h"
34
#include "httpsrr.h"
35
#include "formdata.h" /* for the boundary function */
36
#include "url.h" /* for the SSL config check function */
37
#include "curlx/inet_pton.h"
38
#include "vtls/openssl.h"
39
#include "connect.h"
40
#include "cf-dns.h"
41
#include "progress.h"
42
#include "vtls/vtls.h"
43
#include "vtls/vtls_int.h"
44
#include "vtls/vtls_scache.h"
45
#include "vauth/vauth.h"
46
#include "vtls/keylog.h"
47
#include "vtls/hostcheck.h"
48
#include "transfer.h"
49
#include "multiif.h"
50
#include "curlx/strerr.h"
51
#include "curlx/strparse.h"
52
#include "curlx/strcopy.h"
53
#include "curlx/strdup.h"
54
#include "vtls/apple.h"
55
#ifdef USE_ECH
56
#include "curlx/base64.h"
57
#endif
58
59
#include <openssl/rand.h>
60
#include <openssl/x509v3.h>
61
#ifndef OPENSSL_NO_DSA
62
#include <openssl/dsa.h>
63
#endif
64
#include <openssl/dh.h>
65
#include <openssl/err.h>
66
#include <openssl/conf.h>
67
#include <openssl/bn.h>
68
#include <openssl/rsa.h>
69
#include <openssl/bio.h>
70
#include <openssl/pkcs12.h>
71
#include <openssl/tls1.h>
72
#include <openssl/evp.h>
73
74
#if defined(HAVE_SSL_SET1_ECH_CONFIG_LIST) && !defined(HAVE_BORINGSSL_LIKE)
75
#include <openssl/ech.h>
76
#endif
77
78
#ifndef OPENSSL_NO_OCSP
79
#include <openssl/ocsp.h>
80
#endif
81
82
#if !defined(OPENSSL_NO_ENGINE) && !defined(OPENSSL_NO_UI_CONSOLE)
83
#define USE_OPENSSL_ENGINE
84
#include <openssl/engine.h>
85
#endif
86
87
#ifdef LIBRESSL_VERSION_NUMBER
88
/* As of LibreSSL 2.0.0-4.0.0: OPENSSL_VERSION_NUMBER == 0x20000000L */
89
#  if LIBRESSL_VERSION_NUMBER < 0x2090100fL /* 2019-04-13 */
90
#    error "LibreSSL 2.9.1 or later required"
91
#  endif
92
#elif !defined(HAVE_BORINGSSL_LIKE)
93
#  ifndef HAVE_OPENSSL3 /* 2021-09-07 */
94
#    error "OpenSSL 3.0.0 or later required"
95
#  endif
96
#endif
97
98
#if defined(HAVE_OPENSSL3) && !defined(OPENSSL_NO_UI_CONSOLE)
99
#include <openssl/provider.h>
100
#include <openssl/store.h>
101
/* this is used in the following conditions to make them easier to read */
102
#define OPENSSL_HAS_PROVIDERS
103
#endif
104
105
/* AWS-LC fixed a bug with large buffers in v1.61.0 which also introduced
106
 * X509_V_ERR_EC_KEY_EXPLICIT_PARAMS. */
107
#if !defined(LIBRESSL_VERSION_NUMBER) && !defined(OPENSSL_IS_BORINGSSL) && \
108
  (!defined(OPENSSL_IS_AWSLC) || defined(X509_V_ERR_EC_KEY_EXPLICIT_PARAMS))
109
#define HAVE_SSL_CTX_SET_DEFAULT_READ_BUFFER_LEN 1
110
#endif
111
112
#if defined(USE_OPENSSL_ENGINE) || defined(OPENSSL_HAS_PROVIDERS)
113
#include <openssl/ui.h>
114
#endif
115
116
#ifdef HAVE_OPENSSL3
117
#define HAVE_EVP_PKEY_GET_PARAMS 1
118
#endif
119
120
#ifdef HAVE_EVP_PKEY_GET_PARAMS
121
#include <openssl/core_names.h>
122
0
#define DECLARE_PKEY_PARAM_BIGNUM(name) BIGNUM *name = NULL
123
0
#define FREE_PKEY_PARAM_BIGNUM(name) BN_clear_free(name)
124
#else
125
#define DECLARE_PKEY_PARAM_BIGNUM(name) const BIGNUM *name
126
#define FREE_PKEY_PARAM_BIGNUM(name)
127
#endif
128
129
/* Whether SSL_CTX_set_ciphersuites is available.
130
 * BoringSSL: no
131
 * LibreSSL: supported since 3.4.1 (released 2021-10-14)
132
 * OpenSSL: supported since 1.1.1 (commit a53b5be6a05)
133
 */
134
#if (!defined(LIBRESSL_VERSION_NUMBER) || \
135
     (defined(LIBRESSL_VERSION_NUMBER) && \
136
      LIBRESSL_VERSION_NUMBER >= 0x3040100fL)) && \
137
    !defined(OPENSSL_IS_BORINGSSL)
138
#  define HAVE_SSL_CTX_SET_CIPHERSUITES
139
#  ifndef OPENSSL_IS_AWSLC
140
#    define HAVE_SSL_CTX_SET_POST_HANDSHAKE_AUTH
141
#  endif
142
#endif
143
144
/* Whether SSL_CTX_set1_sigalgs_list is available
145
 * BoringSSL: supported since 0.20240913.0 (commit 826ce15)
146
 * LibreSSL: no
147
 * OpenSSL: supported since 1.0.2 (commit 0b362de5f575)
148
 */
149
#ifndef LIBRESSL_VERSION_NUMBER
150
#define HAVE_SSL_CTX_SET1_SIGALGS
151
#endif
152
153
#ifdef LIBRESSL_VERSION_NUMBER
154
#define OSSL_PACKAGE "LibreSSL"
155
#elif defined(OPENSSL_IS_AWSLC)
156
#define OSSL_PACKAGE "AWS-LC"
157
#elif defined(OPENSSL_IS_BORINGSSL)
158
#define OSSL_PACKAGE "BoringSSL"
159
#elif defined(USE_NGTCP2) && defined(USE_NGHTTP3) && \
160
  !defined(OPENSSL_QUIC_API2)
161
#define OSSL_PACKAGE "quictls"
162
#else
163
0
#define OSSL_PACKAGE "OpenSSL"
164
#endif
165
166
#ifdef HAVE_BORINGSSL_LIKE
167
typedef size_t numcert_t;
168
typedef uint32_t sslerr_t;
169
#else
170
typedef int numcert_t;
171
typedef unsigned long sslerr_t;
172
#endif
173
#define ossl_valsize_t numcert_t
174
175
static CURLcode push_certinfo(struct Curl_easy *data,
176
                              BIO *mem, const char *label, int num)
177
  WARN_UNUSED_RESULT;
178
179
static CURLcode push_certinfo(struct Curl_easy *data,
180
                              BIO *mem, const char *label, int num)
181
0
{
182
0
  char *ptr;
183
0
  long len = BIO_get_mem_data(mem, &ptr);
184
0
  CURLcode result = Curl_ssl_push_certinfo_len(data, num, label, ptr, len);
185
0
  (void)BIO_reset(mem);
186
0
  return result;
187
0
}
188
189
static CURLcode pubkey_show(struct Curl_easy *data,
190
                            BIO *mem,
191
                            int num,
192
                            const char *type,
193
                            const char *name,
194
                            const BIGNUM *bn) WARN_UNUSED_RESULT;
195
196
static CURLcode pubkey_show(struct Curl_easy *data,
197
                            BIO *mem,
198
                            int num,
199
                            const char *type,
200
                            const char *name,
201
                            const BIGNUM *bn)
202
0
{
203
0
  char namebuf[32];
204
205
0
  curl_msnprintf(namebuf, sizeof(namebuf), "%s(%s)", type, name);
206
207
0
  if(bn)
208
0
    BN_print(mem, bn);
209
0
  return push_certinfo(data, mem, namebuf, num);
210
0
}
211
212
#define print_pubkey_BN(_type, _name, _num)           \
213
0
  pubkey_show(data, mem, _num, #_type, #_name, _name)
214
215
static int asn1_object_dump(const ASN1_OBJECT *a, char *buf, size_t len)
216
0
{
217
0
  int i = i2t_ASN1_OBJECT(buf, (int)len, a);
218
0
  return (i >= (int)len);  /* buffer too small */
219
0
}
220
221
static CURLcode X509V3_ext(struct Curl_easy *data,
222
                           int certnum,
223
                           const STACK_OF(X509_EXTENSION) *extsarg)
224
0
{
225
0
  int i;
226
0
  CURLcode result = CURLE_OK;
227
#ifdef LIBRESSL_VERSION_NUMBER
228
  STACK_OF(X509_EXTENSION) *exts = CURL_UNCONST(extsarg);
229
#else
230
0
  const STACK_OF(X509_EXTENSION) *exts = extsarg;
231
0
#endif
232
233
0
  if((int)sk_X509_EXTENSION_num(exts) <= 0)
234
    /* no extensions, bail out */
235
0
    return result;
236
237
0
  for(i = 0; i < (int)sk_X509_EXTENSION_num(exts); i++) {
238
0
    const ASN1_OBJECT *obj;
239
0
    X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, (ossl_valsize_t)i);
240
0
    BUF_MEM *biomem;
241
0
    char namebuf[128];
242
0
    BIO *bio_out = BIO_new(BIO_s_mem());
243
244
0
    if(!bio_out)
245
0
      return result;
246
247
0
    obj = X509_EXTENSION_get_object(ext);
248
249
0
    if(asn1_object_dump(obj, namebuf, sizeof(namebuf)))
250
      /* make sure the name is null-terminated */
251
0
      namebuf[sizeof(namebuf) - 1] = 0;
252
253
0
    if(!X509V3_EXT_print(bio_out, ext, 0, 0))
254
0
      ASN1_STRING_print(bio_out,
255
0
                        (const ASN1_STRING *)X509_EXTENSION_get_data(ext));
256
257
0
    BIO_get_mem_ptr(bio_out, &biomem);
258
0
    result = Curl_ssl_push_certinfo_len(data, certnum, namebuf, biomem->data,
259
0
                                        biomem->length);
260
0
    BIO_free(bio_out);
261
0
    if(result)
262
0
      break;
263
0
  }
264
0
  return result;
265
0
}
266
267
static CURLcode get_pkey_rsa(struct Curl_easy *data,
268
                             EVP_PKEY *pubkey, BIO *mem, int i)
269
0
{
270
0
  CURLcode result = CURLE_OK;
271
#ifndef HAVE_EVP_PKEY_GET_PARAMS
272
  RSA *rsa = EVP_PKEY_get0_RSA(pubkey);
273
#endif /* !HAVE_EVP_PKEY_GET_PARAMS */
274
0
  DECLARE_PKEY_PARAM_BIGNUM(n);
275
0
  DECLARE_PKEY_PARAM_BIGNUM(e);
276
0
#ifdef HAVE_EVP_PKEY_GET_PARAMS
277
0
  EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_RSA_N, &n);
278
0
  EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_RSA_E, &e);
279
#else
280
  RSA_get0_key(rsa, &n, &e, NULL);
281
#endif /* HAVE_EVP_PKEY_GET_PARAMS */
282
0
  BIO_printf(mem, "%d", (int)(n ? BN_num_bits(n) : 0));
283
0
  result = push_certinfo(data, mem, "RSA Public Key", i);
284
0
  if(!result) {
285
0
    result = print_pubkey_BN(rsa, n, i);
286
0
    if(!result)
287
0
      result = print_pubkey_BN(rsa, e, i);
288
0
  }
289
0
  FREE_PKEY_PARAM_BIGNUM(n);
290
0
  FREE_PKEY_PARAM_BIGNUM(e);
291
0
  return result;
292
0
}
293
294
#ifndef OPENSSL_NO_DSA
295
static CURLcode get_pkey_dsa(struct Curl_easy *data,
296
                             EVP_PKEY *pubkey, BIO *mem, int i)
297
0
{
298
0
  CURLcode result = CURLE_OK;
299
#ifndef HAVE_EVP_PKEY_GET_PARAMS
300
  DSA *dsa = EVP_PKEY_get0_DSA(pubkey);
301
#endif /* !HAVE_EVP_PKEY_GET_PARAMS */
302
0
  DECLARE_PKEY_PARAM_BIGNUM(p);
303
0
  DECLARE_PKEY_PARAM_BIGNUM(q);
304
0
  DECLARE_PKEY_PARAM_BIGNUM(g);
305
0
  DECLARE_PKEY_PARAM_BIGNUM(pub_key);
306
0
#ifdef HAVE_EVP_PKEY_GET_PARAMS
307
0
  EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_FFC_P, &p);
308
0
  EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_FFC_Q, &q);
309
0
  EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_FFC_G, &g);
310
0
  EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_PUB_KEY, &pub_key);
311
#else
312
  DSA_get0_pqg(dsa, &p, &q, &g);
313
  DSA_get0_key(dsa, &pub_key, NULL);
314
#endif /* HAVE_EVP_PKEY_GET_PARAMS */
315
0
  result = print_pubkey_BN(dsa, p, i);
316
0
  if(!result)
317
0
    result = print_pubkey_BN(dsa, q, i);
318
0
  if(!result)
319
0
    result = print_pubkey_BN(dsa, g, i);
320
0
  if(!result)
321
0
    result = print_pubkey_BN(dsa, pub_key, i);
322
0
  FREE_PKEY_PARAM_BIGNUM(p);
323
0
  FREE_PKEY_PARAM_BIGNUM(q);
324
0
  FREE_PKEY_PARAM_BIGNUM(g);
325
0
  FREE_PKEY_PARAM_BIGNUM(pub_key);
326
0
  return result;
327
0
}
328
#endif /* !OPENSSL_NO_DSA */
329
330
static CURLcode get_pkey_dh(struct Curl_easy *data,
331
                            EVP_PKEY *pubkey, BIO *mem, int i)
332
0
{
333
0
  CURLcode result;
334
#ifndef HAVE_EVP_PKEY_GET_PARAMS
335
  DH *dh = EVP_PKEY_get0_DH(pubkey);
336
#endif /* !HAVE_EVP_PKEY_GET_PARAMS */
337
0
  DECLARE_PKEY_PARAM_BIGNUM(p);
338
0
  DECLARE_PKEY_PARAM_BIGNUM(q);
339
0
  DECLARE_PKEY_PARAM_BIGNUM(g);
340
0
  DECLARE_PKEY_PARAM_BIGNUM(pub_key);
341
0
#ifdef HAVE_EVP_PKEY_GET_PARAMS
342
0
  EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_FFC_P, &p);
343
0
  EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_FFC_Q, &q);
344
0
  EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_FFC_G, &g);
345
0
  EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_PUB_KEY, &pub_key);
346
#else
347
  DH_get0_pqg(dh, &p, &q, &g);
348
  DH_get0_key(dh, &pub_key, NULL);
349
#endif /* HAVE_EVP_PKEY_GET_PARAMS */
350
0
  result = print_pubkey_BN(dh, p, i);
351
0
  if(!result)
352
0
    result = print_pubkey_BN(dh, q, i);
353
0
  if(!result)
354
0
    result = print_pubkey_BN(dh, g, i);
355
0
  if(!result)
356
0
    result = print_pubkey_BN(dh, pub_key, i);
357
0
  FREE_PKEY_PARAM_BIGNUM(p);
358
0
  FREE_PKEY_PARAM_BIGNUM(q);
359
0
  FREE_PKEY_PARAM_BIGNUM(g);
360
0
  FREE_PKEY_PARAM_BIGNUM(pub_key);
361
0
  return result;
362
0
}
363
364
#ifdef HAVE_OPENSSL3
365
/* from OpenSSL commit fc756e594ed5a27af378 */
366
typedef const X509_PUBKEY pubkeytype_t;
367
#else
368
typedef X509_PUBKEY pubkeytype_t;
369
#endif
370
371
static CURLcode ossl_certchain(struct Curl_easy *data, SSL *ssl)
372
0
{
373
0
  CURLcode result;
374
0
  STACK_OF(X509) *sk;
375
0
  int i;
376
0
  numcert_t numcerts;
377
0
  BIO *mem;
378
379
0
  DEBUGASSERT(ssl);
380
381
0
  sk = SSL_get_peer_cert_chain(ssl);
382
0
  if(!sk)
383
0
    return CURLE_SSL_CONNECT_ERROR;
384
385
0
  numcerts = sk_X509_num(sk);
386
0
  if(numcerts > MAX_ALLOWED_CERT_AMOUNT) {
387
0
    failf(data, "%d certificates is more than allowed (%d)", (int)numcerts,
388
0
          MAX_ALLOWED_CERT_AMOUNT);
389
0
    return CURLE_SSL_CONNECT_ERROR;
390
0
  }
391
392
0
  result = Curl_ssl_init_certinfo(data, (int)numcerts);
393
0
  if(result)
394
0
    return result;
395
396
0
  mem = BIO_new(BIO_s_mem());
397
0
  if(!mem)
398
0
    result = CURLE_OUT_OF_MEMORY;
399
400
0
  for(i = 0; !result && (i < (int)numcerts); i++) {
401
0
    ASN1_INTEGER *num;
402
0
    const unsigned char *numdata;
403
0
    X509 *x = sk_X509_value(sk, (ossl_valsize_t)i);
404
0
    EVP_PKEY *pubkey = NULL;
405
0
    int j;
406
0
    const ASN1_BIT_STRING *psig = NULL;
407
408
0
    X509_NAME_print_ex(mem, X509_get_subject_name(x), 0, XN_FLAG_ONELINE);
409
0
    result = push_certinfo(data, mem, "Subject", i);
410
0
    if(result)
411
0
      break;
412
413
0
    X509_NAME_print_ex(mem, X509_get_issuer_name(x), 0, XN_FLAG_ONELINE);
414
0
    result = push_certinfo(data, mem, "Issuer", i);
415
0
    if(result)
416
0
      break;
417
418
0
    BIO_printf(mem, "%lx", X509_get_version(x));
419
0
    result = push_certinfo(data, mem, "Version", i);
420
0
    if(result)
421
0
      break;
422
423
0
    num = X509_get_serialNumber(x);
424
0
    if(ASN1_STRING_type(num) == V_ASN1_NEG_INTEGER)
425
0
      BIO_puts(mem, "-");
426
0
    numdata = ASN1_STRING_get0_data(num);
427
0
    for(j = 0; j < ASN1_STRING_length(num); j++)
428
0
      BIO_printf(mem, "%02x", numdata[j]);
429
0
    result = push_certinfo(data, mem, "Serial Number", i);
430
0
    if(result)
431
0
      break;
432
433
0
    {
434
0
      const X509_ALGOR *sigalg = NULL;
435
0
      pubkeytype_t *xpubkey = NULL;
436
0
      ASN1_OBJECT *pubkeyoid = NULL;
437
438
0
      X509_get0_signature(&psig, &sigalg, x);
439
0
      if(sigalg) {
440
0
        const ASN1_OBJECT *sigalgoid = NULL;
441
0
        X509_ALGOR_get0(&sigalgoid, NULL, NULL, sigalg);
442
0
        i2a_ASN1_OBJECT(mem, sigalgoid);
443
0
        result = push_certinfo(data, mem, "Signature Algorithm", i);
444
0
        if(result)
445
0
          break;
446
0
      }
447
448
0
      xpubkey = X509_get_X509_PUBKEY(x);
449
0
      if(xpubkey) {
450
0
        X509_PUBKEY_get0_param(&pubkeyoid, NULL, NULL, NULL, xpubkey);
451
0
        if(pubkeyoid) {
452
0
          i2a_ASN1_OBJECT(mem, pubkeyoid);
453
0
          result = push_certinfo(data, mem, "Public Key Algorithm", i);
454
0
          if(result)
455
0
            break;
456
0
        }
457
0
      }
458
459
0
      result = X509V3_ext(data, i, X509_get0_extensions(x));
460
0
      if(result)
461
0
        break;
462
0
    }
463
464
0
    ASN1_TIME_print(mem, X509_get0_notBefore(x));
465
0
    result = push_certinfo(data, mem, "Start date", i);
466
0
    if(result)
467
0
      break;
468
469
0
    ASN1_TIME_print(mem, X509_get0_notAfter(x));
470
0
    result = push_certinfo(data, mem, "Expire date", i);
471
0
    if(result)
472
0
      break;
473
474
0
    pubkey = X509_get_pubkey(x);
475
0
    if(!pubkey)
476
0
      infof(data, "   Unable to load public key");
477
0
    else {
478
0
      switch(EVP_PKEY_id(pubkey)) {
479
0
      case EVP_PKEY_RSA:
480
0
        result = get_pkey_rsa(data, pubkey, mem, i);
481
0
        break;
482
483
0
#ifndef OPENSSL_NO_DSA
484
0
      case EVP_PKEY_DSA:
485
0
        result = get_pkey_dsa(data, pubkey, mem, i);
486
0
        break;
487
0
#endif
488
489
0
      case EVP_PKEY_DH:
490
0
        result = get_pkey_dh(data, pubkey, mem, i);
491
0
        break;
492
0
      }
493
0
      EVP_PKEY_free(pubkey);
494
0
    }
495
496
0
    if(!result && psig) {
497
0
      const unsigned char *psigdata = ASN1_STRING_get0_data(psig);
498
0
      for(j = 0; j < ASN1_STRING_length(psig); j++)
499
0
        BIO_printf(mem, "%02x:", psigdata[j]);
500
0
      result = push_certinfo(data, mem, "Signature", i);
501
0
    }
502
503
0
    if(!result) {
504
0
      PEM_write_bio_X509(mem, x);
505
0
      result = push_certinfo(data, mem, "Cert", i);
506
0
    }
507
0
  }
508
509
0
  BIO_free(mem);
510
511
0
  if(result)
512
    /* cleanup all leftovers */
513
0
    Curl_ssl_free_certinfo(data);
514
515
0
  return result;
516
0
}
517
518
static int ossl_bio_cf_create(BIO *bio)
519
0
{
520
0
  BIO_set_shutdown(bio, 1);
521
0
  BIO_set_init(bio, 1);
522
0
  BIO_set_data(bio, NULL);
523
0
  return 1;
524
0
}
525
526
static int ossl_bio_cf_destroy(BIO *bio)
527
0
{
528
0
  if(!bio)
529
0
    return 0;
530
0
  return 1;
531
0
}
532
533
static long ossl_bio_cf_ctrl(BIO *bio, int cmd, long num, void *ptr)
534
0
{
535
0
  struct Curl_cfilter *cf = BIO_get_data(bio);
536
0
  long ret = 1;
537
538
0
  (void)cf;
539
0
  (void)ptr;
540
0
  switch(cmd) {
541
0
  case BIO_CTRL_GET_CLOSE:
542
0
    ret = (long)BIO_get_shutdown(bio);
543
0
    break;
544
0
  case BIO_CTRL_SET_CLOSE:
545
0
    BIO_set_shutdown(bio, (int)num);
546
0
    break;
547
0
  case BIO_CTRL_FLUSH:
548
    /* we do no delayed writes, but if we ever would, this
549
     * needs to trigger it. */
550
0
    ret = 1;
551
0
    break;
552
0
  case BIO_CTRL_DUP:
553
0
    ret = 1;
554
0
    break;
555
0
  case BIO_CTRL_EOF: {
556
    /* EOF has been reached on input? */
557
0
    struct ssl_connect_data *connssl = cf->ctx;
558
0
    return connssl->peer_closed;
559
0
  }
560
0
  default:
561
0
    ret = 0;
562
0
    break;
563
0
  }
564
0
  return ret;
565
0
}
566
567
static int ossl_bio_cf_out_write(BIO *bio, const char *buf, int blen)
568
0
{
569
0
  struct Curl_cfilter *cf = BIO_get_data(bio);
570
0
  struct ssl_connect_data *connssl = cf->ctx;
571
0
  struct ossl_ctx *octx = (struct ossl_ctx *)connssl->backend;
572
0
  struct Curl_easy *data = CF_DATA_CURRENT(cf);
573
0
  size_t nwritten;
574
0
  CURLcode result;
575
576
0
  DEBUGASSERT(data);
577
0
  if(blen < 0)
578
0
    return 0;
579
580
0
  result = Curl_conn_cf_send(cf->next, data,
581
0
                             (const uint8_t *)buf, (size_t)blen, FALSE,
582
0
                             &nwritten);
583
0
  CURL_TRC_CF(data, cf, "ossl_bio_cf_out_write(len=%d) -> %d, %zu",
584
0
              blen, result, nwritten);
585
0
  BIO_clear_retry_flags(bio);
586
0
  octx->io_result = result;
587
0
  if(result) {
588
0
    if(result == CURLE_AGAIN)
589
0
      BIO_set_retry_write(bio);
590
0
    return -1;
591
0
  }
592
0
  return (int)nwritten;
593
0
}
594
595
static int ossl_bio_cf_in_read(BIO *bio, char *buf, int blen)
596
0
{
597
0
  struct Curl_cfilter *cf = BIO_get_data(bio);
598
0
  struct ssl_connect_data *connssl = cf->ctx;
599
0
  struct ossl_ctx *octx = (struct ossl_ctx *)connssl->backend;
600
0
  struct Curl_easy *data = CF_DATA_CURRENT(cf);
601
0
  size_t nread;
602
0
  CURLcode result, r2;
603
604
0
  DEBUGASSERT(data);
605
  /* OpenSSL catches this case, so should we. */
606
0
  if(!buf)
607
0
    return 0;
608
0
  if(blen < 0)
609
0
    return 0;
610
611
0
  result = Curl_conn_cf_recv(cf->next, data, buf, (size_t)blen, &nread);
612
0
  CURL_TRC_CF(data, cf, "ossl_bio_cf_in_read(len=%d) -> %d, %zu",
613
0
              blen, result, nread);
614
0
  BIO_clear_retry_flags(bio);
615
0
  octx->io_result = result;
616
0
  if(result) {
617
0
    if(result == CURLE_AGAIN)
618
0
      BIO_set_retry_read(bio);
619
0
  }
620
0
  else {
621
    /* feeding data to OpenSSL means SSL_read() might succeed */
622
0
    connssl->input_pending = TRUE;
623
0
    if(nread == 0)
624
0
      connssl->peer_closed = TRUE;
625
0
  }
626
627
  /* Before returning server replies to the SSL instance, we need
628
   * to have setup the x509 store or verification fails. */
629
0
  if(!octx->x509_store_setup) {
630
0
    r2 = Curl_ssl_setup_x509_store(cf, data, octx);
631
0
    if(r2) {
632
0
      BIO_clear_retry_flags(bio);
633
0
      octx->io_result = r2;
634
0
      return -1;
635
0
    }
636
0
    octx->x509_store_setup = TRUE;
637
0
  }
638
0
  return result ? -1 : (int)nread;
639
0
}
640
641
static BIO_METHOD *ossl_bio_cf_method_create(void)
642
0
{
643
0
  BIO_METHOD *m = BIO_meth_new(BIO_TYPE_MEM, "OpenSSL CF BIO");
644
0
  if(m) {
645
0
    BIO_meth_set_write(m, &ossl_bio_cf_out_write);
646
0
    BIO_meth_set_read(m, &ossl_bio_cf_in_read);
647
0
    BIO_meth_set_ctrl(m, &ossl_bio_cf_ctrl);
648
0
    BIO_meth_set_create(m, &ossl_bio_cf_create);
649
0
    BIO_meth_set_destroy(m, &ossl_bio_cf_destroy);
650
0
  }
651
0
  return m;
652
0
}
653
654
static void ossl_bio_cf_method_free(BIO_METHOD *m)
655
0
{
656
0
  if(m)
657
0
    BIO_meth_free(m);
658
0
}
659
660
#ifndef HAVE_KEYLOG_UPSTREAM
661
#ifdef HAVE_KEYLOG_CALLBACK
662
static void ossl_keylog_callback(const SSL *ssl, const char *line)
663
0
{
664
0
  (void)ssl;
665
666
0
  Curl_tls_keylog_write_line(line);
667
0
}
668
#else
669
/*
670
 * ossl_log_tls12_secret is called by libcurl to make the CLIENT_RANDOMs if the
671
 * OpenSSL being used does not have native support for doing that.
672
 */
673
static void ossl_log_tls12_secret(const SSL *ssl, bool *keylog_done)
674
{
675
  const SSL_SESSION *session;
676
  unsigned char client_random[SSL3_RANDOM_SIZE];
677
  unsigned char master_key[SSL_MAX_MASTER_KEY_LENGTH];
678
  int master_key_length = 0;
679
680
  ERR_set_mark();
681
682
  session = SSL_get_session(ssl);
683
684
  if(!session || *keylog_done) {
685
    ERR_pop_to_mark();
686
    return;
687
  }
688
689
  SSL_get_client_random(ssl, client_random, SSL3_RANDOM_SIZE);
690
  master_key_length = (int)
691
    SSL_SESSION_get_master_key(session, master_key, SSL_MAX_MASTER_KEY_LENGTH);
692
693
  ERR_pop_to_mark();
694
695
  /* The handshake has not progressed sufficiently yet, or this is a TLS 1.3
696
   * session (when curl was built with older OpenSSL headers and running with
697
   * newer OpenSSL runtime libraries). */
698
  if(master_key_length <= 0)
699
    return;
700
701
  *keylog_done = TRUE;
702
  Curl_tls_keylog_write("CLIENT_RANDOM", client_random,
703
                        master_key, master_key_length);
704
}
705
#endif /* !HAVE_KEYLOG_CALLBACK */
706
#endif /* HAVE_KEYLOG_UPSTREAM */
707
708
static const char *SSL_ERROR_to_str(int err)
709
0
{
710
0
  switch(err) {
711
0
  case SSL_ERROR_NONE:
712
0
    return "SSL_ERROR_NONE";
713
0
  case SSL_ERROR_SSL:
714
0
    return "SSL_ERROR_SSL";
715
0
  case SSL_ERROR_WANT_READ:
716
0
    return "SSL_ERROR_WANT_READ";
717
0
  case SSL_ERROR_WANT_WRITE:
718
0
    return "SSL_ERROR_WANT_WRITE";
719
0
  case SSL_ERROR_WANT_X509_LOOKUP:
720
0
    return "SSL_ERROR_WANT_X509_LOOKUP";
721
0
  case SSL_ERROR_SYSCALL:
722
0
    return "SSL_ERROR_SYSCALL";
723
0
  case SSL_ERROR_ZERO_RETURN:
724
0
    return "SSL_ERROR_ZERO_RETURN";
725
0
  case SSL_ERROR_WANT_CONNECT:
726
0
    return "SSL_ERROR_WANT_CONNECT";
727
0
  case SSL_ERROR_WANT_ACCEPT:
728
0
    return "SSL_ERROR_WANT_ACCEPT";
729
0
#ifdef SSL_ERROR_WANT_ASYNC  /* OpenSSL 1.1.0+, LibreSSL 3.6.0+ */
730
0
  case SSL_ERROR_WANT_ASYNC:
731
0
    return "SSL_ERROR_WANT_ASYNC";
732
0
#endif
733
0
#ifdef SSL_ERROR_WANT_ASYNC_JOB  /* OpenSSL 1.1.0+, LibreSSL 3.6.0+ */
734
0
  case SSL_ERROR_WANT_ASYNC_JOB:
735
0
    return "SSL_ERROR_WANT_ASYNC_JOB";
736
0
#endif
737
0
#ifdef SSL_ERROR_WANT_CLIENT_HELLO_CB  /* OpenSSL 1.1.1, LibreSSL 3.6.0+ */
738
0
  case SSL_ERROR_WANT_CLIENT_HELLO_CB:
739
0
    return "SSL_ERROR_WANT_CLIENT_HELLO_CB";
740
0
#endif
741
0
  default:
742
0
    return "SSL_ERROR unknown";
743
0
  }
744
0
}
745
746
/* Return error string for last OpenSSL error
747
 */
748
static char *ossl_strerror(unsigned long error, char *buf, size_t size)
749
0
{
750
0
  size_t len;
751
0
  DEBUGASSERT(size);
752
0
  *buf = '\0';
753
754
0
  len = Curl_ossl_version(buf, size);
755
0
  DEBUGASSERT(len < (size - 2));
756
0
  if(len < (size - 2)) {
757
0
    buf += len;
758
0
    size -= (len + 2);
759
0
    *buf++ = ':';
760
0
    *buf++ = ' ';
761
0
    *buf = '\0';
762
0
  }
763
764
#ifdef HAVE_BORINGSSL_LIKE
765
  ERR_error_string_n((uint32_t)error, buf, size);
766
#else
767
0
  ERR_error_string_n(error, buf, size);
768
0
#endif
769
770
0
  if(!*buf) {
771
0
    const char *msg = error ? "Unknown error" : "No error";
772
0
    curlx_strcopy(buf, size, msg, strlen(msg));
773
0
  }
774
775
0
  return buf;
776
0
}
777
778
static int passwd_callback(char *buf, int num, int encrypting, void *password)
779
0
{
780
0
  DEBUGASSERT(encrypting == 0);
781
782
0
  if(!encrypting && num >= 0 && password) {
783
0
    int klen = curlx_uztosi(strlen((char *)password));
784
0
    if(num > klen) {
785
0
      memcpy(buf, password, klen + 1);
786
0
      return klen;
787
0
    }
788
0
  }
789
0
  return 0;
790
0
}
791
792
/*
793
 * rand_enough() returns TRUE if we have seeded the random engine properly.
794
 */
795
static bool rand_enough(void)
796
0
{
797
0
  return RAND_status() != 0;
798
0
}
799
800
static CURLcode ossl_seed(struct Curl_easy *data)
801
0
{
802
  /* This might get called before it has been added to a multi handle */
803
0
  if(data->multi && data->multi->ssl_seeded)
804
0
    return CURLE_OK;
805
806
0
  if(rand_enough()) {
807
    /* OpenSSL 1.1.0+ should return here */
808
0
    if(data->multi)
809
0
      data->multi->ssl_seeded = TRUE;
810
0
    return CURLE_OK;
811
0
  }
812
0
  failf(data, "Insufficient randomness");
813
0
  return CURLE_SSL_CONNECT_ERROR;
814
0
}
815
816
#ifndef SSL_FILETYPE_ENGINE
817
0
#define SSL_FILETYPE_ENGINE 42
818
#endif
819
#ifndef SSL_FILETYPE_PKCS12
820
0
#define SSL_FILETYPE_PKCS12 43
821
#endif
822
#ifndef SSL_FILETYPE_PROVIDER
823
0
#define SSL_FILETYPE_PROVIDER 44
824
#endif
825
static int ossl_do_file_type(const char *type)
826
0
{
827
0
  if(!type || !type[0])
828
0
    return SSL_FILETYPE_PEM;
829
0
  if(curl_strequal(type, "PEM"))
830
0
    return SSL_FILETYPE_PEM;
831
0
  if(curl_strequal(type, "DER"))
832
0
    return SSL_FILETYPE_ASN1;
833
0
  if(curl_strequal(type, "PROV"))
834
0
    return SSL_FILETYPE_PROVIDER;
835
0
  if(curl_strequal(type, "ENG"))
836
0
    return SSL_FILETYPE_ENGINE;
837
0
  if(curl_strequal(type, "P12"))
838
0
    return SSL_FILETYPE_PKCS12;
839
0
  return -1;
840
0
}
841
842
#if defined(USE_OPENSSL_ENGINE) || defined(OPENSSL_HAS_PROVIDERS)
843
/*
844
 * Supply default password to the engine user interface conversation.
845
 * The password is passed by OpenSSL engine from ENGINE_load_private_key()
846
 * last argument to the ui and can be obtained by UI_get0_user_data(ui) here.
847
 */
848
static int ssl_ui_reader(UI *ui, UI_STRING *uis)
849
0
{
850
0
  const char *password;
851
0
  switch(UI_get_string_type(uis)) {
852
0
  case UIT_PROMPT:
853
0
  case UIT_VERIFY:
854
0
    password = (const char *)UI_get0_user_data(ui);
855
0
    if(password && (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD)) {
856
0
      UI_set_result(ui, uis, password);
857
0
      return 1;
858
0
    }
859
0
    FALLTHROUGH();
860
0
  default:
861
0
    break;
862
0
  }
863
0
  return (UI_method_get_reader(UI_OpenSSL()))(ui, uis);
864
0
}
865
866
/*
867
 * Suppress interactive request for a default password if available.
868
 */
869
static int ssl_ui_writer(UI *ui, UI_STRING *uis)
870
0
{
871
0
  switch(UI_get_string_type(uis)) {
872
0
  case UIT_PROMPT:
873
0
  case UIT_VERIFY:
874
0
    if(UI_get0_user_data(ui) &&
875
0
       (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD)) {
876
0
      return 1;
877
0
    }
878
0
    FALLTHROUGH();
879
0
  default:
880
0
    break;
881
0
  }
882
0
  return (UI_method_get_writer(UI_OpenSSL()))(ui, uis);
883
0
}
884
885
/*
886
 * Check if a given string is a PKCS#11 URI
887
 */
888
static bool is_pkcs11_uri(const char *string)
889
0
{
890
0
  return string && curl_strnequal(string, "pkcs11:", 7);
891
0
}
892
893
#endif
894
895
static CURLcode ossl_set_engine(struct Curl_easy *data, const char *name);
896
#ifdef OPENSSL_HAS_PROVIDERS
897
static CURLcode ossl_set_provider(struct Curl_easy *data, const char *iname);
898
#endif
899
900
static int use_certificate_blob(SSL_CTX *ctx, const struct curl_blob *blob,
901
                                int type, const char *key_passwd)
902
0
{
903
0
  int ret = 0;
904
0
  X509 *x = NULL;
905
  /* the typecast of blob->len is fine since it is guaranteed to never be
906
     larger than CURL_MAX_INPUT_LENGTH */
907
0
  BIO *in = BIO_new_mem_buf(blob->data, (int)(blob->len));
908
0
  if(!in)
909
0
    return CURLE_OUT_OF_MEMORY;
910
911
0
  if(type == SSL_FILETYPE_ASN1) {
912
    /* j = ERR_R_ASN1_LIB; */
913
0
    x = d2i_X509_bio(in, NULL);
914
0
  }
915
0
  else if(type == SSL_FILETYPE_PEM) {
916
    /* ERR_R_PEM_LIB; */
917
0
    x = PEM_read_bio_X509(in, NULL, passwd_callback, CURL_UNCONST(key_passwd));
918
0
  }
919
0
  else {
920
0
    ret = 0;
921
0
    goto end;
922
0
  }
923
924
0
  if(!x) {
925
0
    ret = 0;
926
0
    goto end;
927
0
  }
928
929
0
  ret = SSL_CTX_use_certificate(ctx, x);
930
0
end:
931
0
  X509_free(x);
932
0
  BIO_free(in);
933
0
  return ret;
934
0
}
935
936
static int use_privatekey_blob(SSL_CTX *ctx, const struct curl_blob *blob,
937
                               int type, const char *key_passwd)
938
0
{
939
0
  int ret = 0;
940
0
  EVP_PKEY *pkey = NULL;
941
0
  BIO *in = BIO_new_mem_buf(blob->data, (int)(blob->len));
942
0
  if(!in)
943
0
    return CURLE_OUT_OF_MEMORY;
944
945
0
  if(type == SSL_FILETYPE_PEM)
946
0
    pkey = PEM_read_bio_PrivateKey(in, NULL, passwd_callback,
947
0
                                   CURL_UNCONST(key_passwd));
948
0
  else if(type == SSL_FILETYPE_ASN1)
949
0
    pkey = d2i_PrivateKey_bio(in, NULL);
950
0
  else
951
0
    goto end;
952
953
0
  if(!pkey)
954
0
    goto end;
955
956
0
  ret = SSL_CTX_use_PrivateKey(ctx, pkey);
957
0
  EVP_PKEY_free(pkey);
958
0
end:
959
0
  BIO_free(in);
960
0
  return ret;
961
0
}
962
963
static int use_certificate_chain_blob(SSL_CTX *ctx,
964
                                      const struct curl_blob *blob,
965
                                      const char *key_passwd)
966
0
{
967
0
  int ret = 0;
968
0
  X509 *x = NULL;
969
0
  BIO *in = BIO_new_mem_buf(blob->data, (int)(blob->len));
970
0
  if(!in)
971
0
    return CURLE_OUT_OF_MEMORY;
972
973
0
  ERR_clear_error();
974
975
0
  x = PEM_read_bio_X509_AUX(in, NULL,
976
0
                            passwd_callback, CURL_UNCONST(key_passwd));
977
0
  if(!x)
978
0
    goto end;
979
980
0
  ret = SSL_CTX_use_certificate(ctx, x);
981
982
0
  if(ERR_peek_error() != 0)
983
0
    ret = 0;
984
985
0
  if(ret) {
986
0
    X509 *ca;
987
0
    sslerr_t err;
988
989
0
    if(!SSL_CTX_clear_chain_certs(ctx)) {
990
0
      ret = 0;
991
0
      goto end;
992
0
    }
993
994
0
    while((ca = PEM_read_bio_X509(in, NULL, passwd_callback,
995
0
                                  CURL_UNCONST(key_passwd))) != NULL) {
996
997
0
      if(!SSL_CTX_add0_chain_cert(ctx, ca)) {
998
0
        X509_free(ca);
999
0
        ret = 0;
1000
0
        goto end;
1001
0
      }
1002
0
    }
1003
1004
0
    err = ERR_peek_last_error();
1005
0
    if((ERR_GET_LIB(err) == ERR_LIB_PEM) &&
1006
0
       (ERR_GET_REASON(err) == PEM_R_NO_START_LINE))
1007
0
      ERR_clear_error();
1008
0
    else
1009
0
      ret = 0;
1010
0
  }
1011
1012
0
end:
1013
0
  X509_free(x);
1014
0
  BIO_free(in);
1015
0
  return ret;
1016
0
}
1017
1018
static int enginecheck(struct Curl_easy *data,
1019
                       SSL_CTX* ctx,
1020
                       const char *key_file,
1021
                       const char *key_passwd)
1022
0
{
1023
0
#ifdef USE_OPENSSL_ENGINE
1024
0
  EVP_PKEY *priv_key = NULL;
1025
1026
  /* Implicitly use pkcs11 engine if none was provided and the
1027
   * key_file is a PKCS#11 URI */
1028
0
  if(!data->state.engine) {
1029
0
    if(is_pkcs11_uri(key_file)) {
1030
0
      if(ossl_set_engine(data, "pkcs11") != CURLE_OK) {
1031
0
        return 0;
1032
0
      }
1033
0
    }
1034
0
  }
1035
1036
0
  if(data->state.engine) {
1037
0
    UI_METHOD *ui_method = UI_create_method("curl user interface");
1038
0
    if(!ui_method) {
1039
0
      failf(data, "unable to create " OSSL_PACKAGE " user-interface method");
1040
0
      return 0;
1041
0
    }
1042
0
    UI_method_set_opener(ui_method, UI_method_get_opener(UI_OpenSSL()));
1043
0
    UI_method_set_closer(ui_method, UI_method_get_closer(UI_OpenSSL()));
1044
0
    UI_method_set_reader(ui_method, ssl_ui_reader);
1045
0
    UI_method_set_writer(ui_method, ssl_ui_writer);
1046
0
    priv_key = ENGINE_load_private_key(data->state.engine, key_file,
1047
0
                                       ui_method,
1048
0
                                       CURL_UNCONST(key_passwd));
1049
0
    UI_destroy_method(ui_method);
1050
0
    if(!priv_key) {
1051
0
      failf(data, "failed to load private key from crypto engine");
1052
0
      return 0;
1053
0
    }
1054
0
    if(SSL_CTX_use_PrivateKey(ctx, priv_key) != 1) {
1055
0
      failf(data, "unable to set private key");
1056
0
      EVP_PKEY_free(priv_key);
1057
0
      return 0;
1058
0
    }
1059
0
    EVP_PKEY_free(priv_key);  /* we do not need the handle any more... */
1060
0
  }
1061
0
  else {
1062
0
    failf(data, "crypto engine not set, cannot load private key");
1063
0
    return 0;
1064
0
  }
1065
0
  return 1;
1066
#else
1067
  (void)ctx;
1068
  (void)key_file;
1069
  (void)key_passwd;
1070
  failf(data, "SSL_FILETYPE_ENGINE not supported for private key");
1071
  return 0;
1072
#endif
1073
0
}
1074
1075
static int providercheck(struct Curl_easy *data,
1076
                         SSL_CTX* ctx,
1077
                         const char *key_file)
1078
0
{
1079
0
#ifdef OPENSSL_HAS_PROVIDERS
1080
0
  char error_buffer[256];
1081
  /* Implicitly use pkcs11 provider if none was provided and the
1082
   * key_file is a PKCS#11 URI */
1083
0
  if(!data->state.provider_loaded) {
1084
0
    if(is_pkcs11_uri(key_file)) {
1085
0
      if(ossl_set_provider(data, "pkcs11") != CURLE_OK) {
1086
0
        return 0;
1087
0
      }
1088
0
    }
1089
0
  }
1090
1091
0
  if(data->state.provider_loaded) {
1092
    /* Load the private key from the provider */
1093
0
    EVP_PKEY *priv_key = NULL;
1094
0
    OSSL_STORE_CTX *store = NULL;
1095
0
    OSSL_STORE_INFO *info = NULL;
1096
0
    UI_METHOD *ui_method = UI_create_method("curl user interface");
1097
0
    if(!ui_method) {
1098
0
      failf(data, "unable to create " OSSL_PACKAGE " user-interface method");
1099
0
      return 0;
1100
0
    }
1101
0
    UI_method_set_opener(ui_method, UI_method_get_opener(UI_OpenSSL()));
1102
0
    UI_method_set_closer(ui_method, UI_method_get_closer(UI_OpenSSL()));
1103
0
    UI_method_set_reader(ui_method, ssl_ui_reader);
1104
0
    UI_method_set_writer(ui_method, ssl_ui_writer);
1105
1106
0
    store = OSSL_STORE_open_ex(key_file, data->state.libctx,
1107
0
                               data->state.propq, ui_method, NULL, NULL,
1108
0
                               NULL, NULL);
1109
0
    if(!store) {
1110
0
      failf(data, "Failed to open OpenSSL store: %s",
1111
0
            ossl_strerror(ERR_get_error(), error_buffer,
1112
0
                          sizeof(error_buffer)));
1113
0
      UI_destroy_method(ui_method);
1114
0
      return 0;
1115
0
    }
1116
0
    if(OSSL_STORE_expect(store, OSSL_STORE_INFO_PKEY) != 1) {
1117
0
      failf(data, "Failed to set store preference. Ignoring the error: %s",
1118
0
            ossl_strerror(ERR_get_error(), error_buffer,
1119
0
                          sizeof(error_buffer)));
1120
0
    }
1121
1122
0
    info = OSSL_STORE_load(store);
1123
0
    if(info) {
1124
0
      int ossl_type = OSSL_STORE_INFO_get_type(info);
1125
1126
0
      if(ossl_type == OSSL_STORE_INFO_PKEY)
1127
0
        priv_key = OSSL_STORE_INFO_get1_PKEY(info);
1128
0
      OSSL_STORE_INFO_free(info);
1129
0
    }
1130
0
    OSSL_STORE_close(store);
1131
0
    UI_destroy_method(ui_method);
1132
0
    if(!priv_key) {
1133
0
      failf(data, "No private key found in the openssl store: %s",
1134
0
            ossl_strerror(ERR_get_error(), error_buffer,
1135
0
                          sizeof(error_buffer)));
1136
0
      return 0;
1137
0
    }
1138
1139
0
    if(SSL_CTX_use_PrivateKey(ctx, priv_key) != 1) {
1140
0
      failf(data, "unable to set private key [%s]",
1141
0
            ossl_strerror(ERR_get_error(), error_buffer,
1142
0
                          sizeof(error_buffer)));
1143
0
      EVP_PKEY_free(priv_key);
1144
0
      return 0;
1145
0
    }
1146
0
    EVP_PKEY_free(priv_key); /* we do not need the handle any more... */
1147
0
  }
1148
0
  else {
1149
0
    failf(data, "crypto provider not set, cannot load private key");
1150
0
    return 0;
1151
0
  }
1152
0
  return 1;
1153
#else
1154
  (void)ctx;
1155
  (void)key_file;
1156
  failf(data, "SSL_FILETYPE_PROVIDER not supported for private key");
1157
  return 0;
1158
#endif
1159
0
}
1160
1161
static int engineload(struct Curl_easy *data,
1162
                      SSL_CTX* ctx,
1163
                      const char *cert_file)
1164
0
{
1165
/* ENGINE_CTRL_GET_CMD_FROM_NAME supported by OpenSSL, LibreSSL <=3.8.3 */
1166
0
#if defined(USE_OPENSSL_ENGINE) && defined(ENGINE_CTRL_GET_CMD_FROM_NAME)
1167
0
  char error_buffer[256];
1168
  /* Implicitly use pkcs11 engine if none was provided and the
1169
   * cert_file is a PKCS#11 URI */
1170
0
  if(!data->state.engine) {
1171
0
    if(is_pkcs11_uri(cert_file)) {
1172
0
      if(ossl_set_engine(data, "pkcs11") != CURLE_OK) {
1173
0
        return 0;
1174
0
      }
1175
0
    }
1176
0
  }
1177
1178
0
  if(data->state.engine) {
1179
0
    const char *cmd_name = "LOAD_CERT_CTRL";
1180
0
    struct {
1181
0
      const char *cert_id;
1182
0
      X509 *cert;
1183
0
    } params;
1184
1185
0
    params.cert_id = cert_file;
1186
0
    params.cert = NULL;
1187
1188
    /* Does the engine supports LOAD_CERT_CTRL ? */
1189
0
    if(!ENGINE_ctrl(data->state.engine, ENGINE_CTRL_GET_CMD_FROM_NAME,
1190
0
                    0, CURL_UNCONST(cmd_name), NULL)) {
1191
0
      failf(data, "SSL engine does not support loading certificates");
1192
0
      return 0;
1193
0
    }
1194
1195
    /* Load the certificate from the engine */
1196
0
    if(!ENGINE_ctrl_cmd(data->state.engine, cmd_name, 0, &params, NULL, 1)) {
1197
0
      failf(data, "SSL engine cannot load client cert with id '%s' [%s]",
1198
0
            cert_file,
1199
0
            ossl_strerror(ERR_get_error(), error_buffer,
1200
0
                          sizeof(error_buffer)));
1201
0
      return 0;
1202
0
    }
1203
1204
0
    if(!params.cert) {
1205
0
      failf(data, "SSL engine did not initialized the certificate properly.");
1206
0
      return 0;
1207
0
    }
1208
1209
0
    if(SSL_CTX_use_certificate(ctx, params.cert) != 1) {
1210
0
      failf(data, "unable to set client certificate [%s]",
1211
0
            ossl_strerror(ERR_get_error(), error_buffer,
1212
0
                          sizeof(error_buffer)));
1213
0
      X509_free(params.cert);
1214
0
      return 0;
1215
0
    }
1216
0
    X509_free(params.cert); /* we do not need the handle any more... */
1217
0
  }
1218
0
  else {
1219
0
    failf(data, "crypto engine not set, cannot load certificate");
1220
0
    return 0;
1221
0
  }
1222
0
  return 1;
1223
#else
1224
  (void)ctx;
1225
  (void)cert_file;
1226
  failf(data, "SSL_FILETYPE_ENGINE not supported for certificate");
1227
  return 0;
1228
#endif
1229
0
}
1230
1231
static int providerload(struct Curl_easy *data,
1232
                        SSL_CTX* ctx,
1233
                        const char *cert_file)
1234
0
{
1235
0
#ifdef OPENSSL_HAS_PROVIDERS
1236
0
  char error_buffer[256];
1237
  /* Implicitly use pkcs11 provider if none was provided and the
1238
   * cert_file is a PKCS#11 URI */
1239
0
  if(!data->state.provider_loaded) {
1240
0
    if(is_pkcs11_uri(cert_file)) {
1241
0
      if(ossl_set_provider(data, "pkcs11") != CURLE_OK) {
1242
0
        return 0;
1243
0
      }
1244
0
    }
1245
0
  }
1246
1247
0
  if(data->state.provider_loaded) {
1248
    /* Load the certificate from the provider */
1249
0
    OSSL_STORE_INFO *info = NULL;
1250
0
    X509 *cert = NULL;
1251
0
    OSSL_STORE_CTX *store =
1252
0
      OSSL_STORE_open_ex(cert_file, data->state.libctx,
1253
0
                         NULL, NULL, NULL, NULL, NULL, NULL);
1254
0
    int rc;
1255
1256
0
    if(!store) {
1257
0
      failf(data, "Failed to open OpenSSL store: %s",
1258
0
            ossl_strerror(ERR_get_error(), error_buffer,
1259
0
                          sizeof(error_buffer)));
1260
0
      return 0;
1261
0
    }
1262
0
    if(OSSL_STORE_expect(store, OSSL_STORE_INFO_CERT) != 1) {
1263
0
      failf(data, "Failed to set store preference. Ignoring the error: %s",
1264
0
            ossl_strerror(ERR_get_error(), error_buffer,
1265
0
                          sizeof(error_buffer)));
1266
0
    }
1267
1268
0
    info = OSSL_STORE_load(store);
1269
0
    if(info) {
1270
0
      int ossl_type = OSSL_STORE_INFO_get_type(info);
1271
1272
0
      if(ossl_type == OSSL_STORE_INFO_CERT)
1273
0
        cert = OSSL_STORE_INFO_get1_CERT(info);
1274
0
      OSSL_STORE_INFO_free(info);
1275
0
    }
1276
0
    OSSL_STORE_close(store);
1277
0
    if(!cert) {
1278
0
      failf(data, "No cert found in the openssl store: %s",
1279
0
            ossl_strerror(ERR_get_error(), error_buffer,
1280
0
                          sizeof(error_buffer)));
1281
0
      return 0;
1282
0
    }
1283
1284
0
    rc = SSL_CTX_use_certificate(ctx, cert);
1285
0
    X509_free(cert); /* we do not need the handle any more... */
1286
1287
0
    if(rc != 1) {
1288
0
      failf(data, "unable to set client certificate [%s]",
1289
0
            ossl_strerror(ERR_get_error(), error_buffer,
1290
0
                          sizeof(error_buffer)));
1291
0
      return 0;
1292
0
    }
1293
0
  }
1294
0
  else {
1295
0
    failf(data, "crypto provider not set, cannot load certificate");
1296
0
    return 0;
1297
0
  }
1298
0
  return 1;
1299
#else
1300
  (void)ctx;
1301
  (void)cert_file;
1302
  failf(data, "SSL_FILETYPE_PROVIDER not supported for certificate");
1303
  return 0;
1304
#endif
1305
0
}
1306
1307
static int pkcs12load(struct Curl_easy *data,
1308
                      SSL_CTX* ctx,
1309
                      const struct curl_blob *cert_blob,
1310
                      const char *cert_file,
1311
                      const char *key_passwd)
1312
0
{
1313
0
  char error_buffer[256];
1314
0
  BIO *cert_bio = NULL;
1315
0
  PKCS12 *p12 = NULL;
1316
0
  EVP_PKEY *pri;
1317
0
  X509 *x509;
1318
0
  int cert_done = 0;
1319
0
  STACK_OF(X509) *ca = NULL;
1320
0
  if(cert_blob) {
1321
0
    cert_bio = BIO_new_mem_buf(cert_blob->data, (int)(cert_blob->len));
1322
0
    if(!cert_bio) {
1323
0
      failf(data, "BIO_new_mem_buf NULL, " OSSL_PACKAGE " error %s",
1324
0
            ossl_strerror(ERR_get_error(), error_buffer,
1325
0
                          sizeof(error_buffer)));
1326
0
      return 0;
1327
0
    }
1328
0
  }
1329
0
  else {
1330
0
    cert_bio = BIO_new(BIO_s_file());
1331
0
    if(!cert_bio) {
1332
0
      failf(data, "BIO_new return NULL, " OSSL_PACKAGE " error %s",
1333
0
            ossl_strerror(ERR_get_error(), error_buffer,
1334
0
                          sizeof(error_buffer)));
1335
0
      return 0;
1336
0
    }
1337
1338
0
    if(BIO_read_filename(cert_bio, CURL_UNCONST(cert_file)) <= 0) {
1339
0
      failf(data, "could not open PKCS12 file '%s'", cert_file);
1340
0
      BIO_free(cert_bio);
1341
0
      return 0;
1342
0
    }
1343
0
  }
1344
1345
0
  p12 = d2i_PKCS12_bio(cert_bio, NULL);
1346
0
  BIO_free(cert_bio);
1347
1348
0
  if(!p12) {
1349
0
    failf(data, "error reading PKCS12 file '%s'",
1350
0
          cert_blob ? "(memory blob)" : cert_file);
1351
0
    return 0;
1352
0
  }
1353
1354
0
  if(!PKCS12_parse(p12, key_passwd, &pri, &x509, &ca)) {
1355
0
    failf(data, "could not parse PKCS12 file, check password, " OSSL_PACKAGE
1356
0
          " error %s",
1357
0
          ossl_strerror(ERR_get_error(), error_buffer, sizeof(error_buffer)));
1358
0
    PKCS12_free(p12);
1359
0
    return 0;
1360
0
  }
1361
1362
0
  PKCS12_free(p12);
1363
1364
0
  if(SSL_CTX_use_certificate(ctx, x509) != 1) {
1365
0
    failf(data, "could not load PKCS12 client certificate, " OSSL_PACKAGE
1366
0
          " error %s",
1367
0
          ossl_strerror(ERR_get_error(), error_buffer, sizeof(error_buffer)));
1368
0
    goto fail;
1369
0
  }
1370
1371
0
  if(SSL_CTX_use_PrivateKey(ctx, pri) != 1) {
1372
0
    failf(data, "unable to use private key from PKCS12 file '%s'", cert_file);
1373
0
    goto fail;
1374
0
  }
1375
1376
0
  if(!SSL_CTX_check_private_key(ctx)) {
1377
0
    failf(data, "private key from PKCS12 file '%s' "
1378
0
          "does not match certificate in same file", cert_file);
1379
0
    goto fail;
1380
0
  }
1381
  /* Set Certificate Verification chain */
1382
0
  if(ca) {
1383
0
    while(sk_X509_num(ca)) {
1384
      /*
1385
       * Note that sk_X509_pop() is used below to make sure the cert is
1386
       * removed from the stack properly before getting passed to
1387
       * SSL_CTX_add_extra_chain_cert(), which takes ownership. Previously
1388
       * we used sk_X509_value() instead, but then we would clean it in the
1389
       * subsequent sk_X509_pop_free() call.
1390
       */
1391
0
      X509 *x = sk_X509_pop(ca);
1392
0
      if(!SSL_CTX_add_client_CA(ctx, x)) {
1393
0
        X509_free(x);
1394
0
        failf(data, "cannot add certificate to client CA list");
1395
0
        goto fail;
1396
0
      }
1397
0
      if(!SSL_CTX_add_extra_chain_cert(ctx, x)) {
1398
0
        X509_free(x);
1399
0
        failf(data, "cannot add certificate to certificate chain");
1400
0
        goto fail;
1401
0
      }
1402
0
    }
1403
0
  }
1404
1405
0
  cert_done = 1;
1406
0
fail:
1407
0
  EVP_PKEY_free(pri);
1408
0
  X509_free(x509);
1409
0
#if defined(__clang__) && __clang_major__ >= 16
1410
0
#pragma clang diagnostic push
1411
0
#pragma clang diagnostic ignored "-Wcast-function-type-strict"
1412
0
#endif
1413
0
  sk_X509_pop_free(ca, X509_free);
1414
0
#if defined(__clang__) && __clang_major__ >= 16
1415
0
#pragma clang diagnostic pop
1416
0
#endif
1417
0
  if(!cert_done)
1418
0
    return 0; /* failure! */
1419
0
  return 1;
1420
0
}
1421
1422
static CURLcode client_cert(struct Curl_easy *data,
1423
                            SSL_CTX* ctx,
1424
                            char *cert_file,
1425
                            const struct curl_blob *cert_blob,
1426
                            const char *cert_type,
1427
                            char *key_file,
1428
                            const struct curl_blob *key_blob,
1429
                            const char *key_type,
1430
                            char *key_passwd)
1431
0
{
1432
0
  char error_buffer[256];
1433
0
  bool check_privkey = TRUE;
1434
0
  int file_type = ossl_do_file_type(cert_type);
1435
1436
0
  if(cert_file || cert_blob || (file_type == SSL_FILETYPE_ENGINE) ||
1437
0
     (file_type == SSL_FILETYPE_PROVIDER)) {
1438
0
    SSL *ssl;
1439
0
    X509 *x509;
1440
0
    bool pcks12_done = FALSE;
1441
0
    int cert_use_result;
1442
1443
0
    if(key_passwd) {
1444
      /* set the password in the callback userdata */
1445
0
      SSL_CTX_set_default_passwd_cb_userdata(ctx, key_passwd);
1446
      /* Set passwd callback: */
1447
0
      SSL_CTX_set_default_passwd_cb(ctx, passwd_callback);
1448
0
    }
1449
1450
0
    switch(file_type) {
1451
0
    case SSL_FILETYPE_PEM:
1452
      /* SSL_CTX_use_certificate_chain_file() only works on PEM files */
1453
0
      cert_use_result = cert_blob ?
1454
0
        use_certificate_chain_blob(ctx, cert_blob, key_passwd) :
1455
0
        SSL_CTX_use_certificate_chain_file(ctx, cert_file);
1456
0
      if(cert_use_result != 1) {
1457
0
        failf(data,
1458
0
              "could not load PEM client certificate from %s, " OSSL_PACKAGE
1459
0
              " error %s, "
1460
0
              "(no key found, wrong passphrase, or wrong file format?)",
1461
0
              (cert_blob ? "CURLOPT_SSLCERT_BLOB" : cert_file),
1462
0
              ossl_strerror(ERR_get_error(), error_buffer,
1463
0
                            sizeof(error_buffer)));
1464
0
        return CURLE_SSL_CERTPROBLEM;
1465
0
      }
1466
0
      break;
1467
1468
0
    case SSL_FILETYPE_ASN1:
1469
      /* SSL_CTX_use_certificate_file() works with either PEM or ASN1, but
1470
         we use the case above for PEM so this can only be performed with
1471
         ASN1 files. */
1472
1473
0
      cert_use_result = cert_blob ?
1474
0
        use_certificate_blob(ctx, cert_blob, file_type, key_passwd) :
1475
0
      SSL_CTX_use_certificate_file(ctx, cert_file, file_type);
1476
0
      if(cert_use_result != 1) {
1477
0
        failf(data,
1478
0
              "could not load ASN1 client certificate from %s, " OSSL_PACKAGE
1479
0
              " error %s, "
1480
0
              "(no key found, wrong passphrase, or wrong file format?)",
1481
0
              (cert_blob ? "CURLOPT_SSLCERT_BLOB" : cert_file),
1482
0
              ossl_strerror(ERR_get_error(), error_buffer,
1483
0
                            sizeof(error_buffer)));
1484
0
        return CURLE_SSL_CERTPROBLEM;
1485
0
      }
1486
0
      break;
1487
1488
0
    case SSL_FILETYPE_ENGINE:
1489
0
      if(!cert_file || !engineload(data, ctx, cert_file))
1490
0
        return CURLE_SSL_CERTPROBLEM;
1491
0
      break;
1492
1493
0
    case SSL_FILETYPE_PROVIDER:
1494
0
      if(!cert_file || !providerload(data, ctx, cert_file))
1495
0
        return CURLE_SSL_CERTPROBLEM;
1496
0
      break;
1497
1498
0
    case SSL_FILETYPE_PKCS12:
1499
0
      if(!pkcs12load(data, ctx, cert_blob, cert_file, key_passwd))
1500
0
        return CURLE_SSL_CERTPROBLEM;
1501
0
      pcks12_done = TRUE;
1502
0
      break;
1503
1504
0
    default:
1505
0
      failf(data, "not supported file type '%s' for certificate", cert_type);
1506
0
      return CURLE_BAD_FUNCTION_ARGUMENT;
1507
0
    }
1508
1509
0
    if(!key_file && !key_blob) {
1510
0
      key_file = cert_file;
1511
0
      key_blob = cert_blob;
1512
0
    }
1513
0
    else
1514
0
      file_type = ossl_do_file_type(key_type);
1515
1516
0
    switch(file_type) {
1517
0
    case SSL_FILETYPE_PEM:
1518
0
    case SSL_FILETYPE_ASN1:
1519
0
      cert_use_result = key_blob ?
1520
0
        use_privatekey_blob(ctx, key_blob, file_type, key_passwd) :
1521
0
      SSL_CTX_use_PrivateKey_file(ctx, key_file, file_type);
1522
0
      if(cert_use_result != 1) {
1523
0
        failf(data, "unable to set private key file: '%s' type %s",
1524
0
              key_file ? key_file : "(memory blob)",
1525
0
              key_type ? key_type : "PEM");
1526
0
        return CURLE_BAD_FUNCTION_ARGUMENT;
1527
0
      }
1528
0
      break;
1529
0
    case SSL_FILETYPE_ENGINE:
1530
0
      if(!enginecheck(data, ctx, key_file, key_passwd))
1531
0
        return CURLE_SSL_CERTPROBLEM;
1532
0
      break;
1533
1534
0
    case SSL_FILETYPE_PROVIDER:
1535
0
      if(!providercheck(data, ctx, key_file))
1536
0
        return CURLE_SSL_CERTPROBLEM;
1537
0
      break;
1538
1539
0
    case SSL_FILETYPE_PKCS12:
1540
0
      if(!pcks12_done) {
1541
0
        failf(data, "file type P12 for private key not supported");
1542
0
        return CURLE_SSL_CERTPROBLEM;
1543
0
      }
1544
0
      break;
1545
0
    default:
1546
0
      failf(data, "not supported file type for private key");
1547
0
      return CURLE_BAD_FUNCTION_ARGUMENT;
1548
0
    }
1549
1550
0
    ssl = SSL_new(ctx);
1551
0
    if(!ssl) {
1552
0
      failf(data, "unable to create an SSL structure");
1553
0
      return CURLE_OUT_OF_MEMORY;
1554
0
    }
1555
1556
0
    x509 = SSL_get_certificate(ssl);
1557
1558
0
    if(x509) {
1559
0
      EVP_PKEY *pktmp = X509_get_pubkey(x509);
1560
0
      EVP_PKEY_copy_parameters(pktmp, SSL_get_privatekey(ssl));
1561
0
      EVP_PKEY_free(pktmp);
1562
0
    }
1563
1564
0
#if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1565
0
    {
1566
      /* If RSA is used, do not check the private key if its flags indicate
1567
       * it does not support it. */
1568
0
      EVP_PKEY *priv_key = SSL_get_privatekey(ssl);
1569
0
      if(EVP_PKEY_id(priv_key) == EVP_PKEY_RSA) {
1570
0
        RSA *rsa = EVP_PKEY_get1_RSA(priv_key);
1571
0
        if(RSA_flags(rsa) & RSA_METHOD_FLAG_NO_CHECK)
1572
0
          check_privkey = FALSE;
1573
0
        RSA_free(rsa); /* Decrement reference count */
1574
0
      }
1575
0
    }
1576
0
#endif
1577
1578
0
    SSL_free(ssl);
1579
1580
    /* If we are using DSA, we can copy the parameters from
1581
     * the private key */
1582
1583
0
    if(check_privkey == TRUE) {
1584
      /* Now we know that a key and cert have been set against
1585
       * the SSL context */
1586
0
      if(!SSL_CTX_check_private_key(ctx)) {
1587
0
        failf(data, "Private key does not match the certificate public key");
1588
0
        return CURLE_SSL_CERTPROBLEM;
1589
0
      }
1590
0
    }
1591
0
  }
1592
0
  return CURLE_OK;
1593
0
}
1594
1595
#ifdef CURLVERBOSE
1596
/* returns non-zero on failure */
1597
static CURLcode x509_name_oneline(const X509_NAME *a, struct dynbuf *d)
1598
0
{
1599
0
  BIO *bio_out = BIO_new(BIO_s_mem());
1600
0
  BUF_MEM *biomem;
1601
0
  int rc;
1602
0
  CURLcode result = CURLE_OUT_OF_MEMORY;
1603
1604
0
  if(bio_out) {
1605
0
    unsigned long flags = XN_FLAG_SEP_SPLUS_SPC |
1606
0
      (XN_FLAG_ONELINE & ~ASN1_STRFLGS_ESC_MSB & ~XN_FLAG_SPC_EQ);
1607
0
    curlx_dyn_reset(d);
1608
0
    rc = X509_NAME_print_ex(bio_out, a, 0, flags);
1609
0
    if(rc != -1) {
1610
0
      BIO_get_mem_ptr(bio_out, &biomem);
1611
0
      result = curlx_dyn_addn(d, biomem->data, biomem->length);
1612
0
    }
1613
0
    BIO_free(bio_out);
1614
0
  }
1615
0
  return result;
1616
0
}
1617
#endif
1618
1619
/**
1620
 * Global SSL init
1621
 *
1622
 * @retval 0 error initializing SSL
1623
 * @retval 1 SSL initialized successfully
1624
 */
1625
static int ossl_init(void)
1626
0
{
1627
0
  const uint64_t flags =
1628
0
#ifdef OPENSSL_INIT_ENGINE_ALL_BUILTIN
1629
    /* not present in BoringSSL */
1630
0
    OPENSSL_INIT_ENGINE_ALL_BUILTIN |
1631
0
#endif
1632
#ifdef CURL_DISABLE_OPENSSL_AUTO_LOAD_CONFIG
1633
    OPENSSL_INIT_NO_LOAD_CONFIG |
1634
#else
1635
0
    OPENSSL_INIT_LOAD_CONFIG |
1636
0
#endif
1637
0
    0;
1638
0
  OPENSSL_init_ssl(flags, NULL);
1639
1640
0
#ifndef HAVE_KEYLOG_UPSTREAM
1641
0
  Curl_tls_keylog_open();
1642
0
#endif
1643
1644
0
  return 1;
1645
0
}
1646
1647
/* Global cleanup */
1648
static void ossl_cleanup(void)
1649
0
{
1650
0
#ifndef HAVE_KEYLOG_UPSTREAM
1651
0
  Curl_tls_keylog_close();
1652
0
#endif
1653
0
}
1654
1655
/* Selects an OpenSSL crypto engine or provider.
1656
 */
1657
static CURLcode ossl_set_engine(struct Curl_easy *data, const char *name)
1658
0
{
1659
0
#ifdef USE_OPENSSL_ENGINE
1660
0
  CURLcode result = CURLE_SSL_ENGINE_NOTFOUND;
1661
0
  ENGINE *e = ENGINE_by_id(name);
1662
1663
0
  if(e) {
1664
1665
0
    if(data->state.engine) {
1666
0
      ENGINE_finish(data->state.engine);
1667
0
      ENGINE_free(data->state.engine);
1668
0
      data->state.engine = NULL;
1669
0
    }
1670
0
    if(!ENGINE_init(e)) {
1671
0
      char buf[256];
1672
1673
0
      ENGINE_free(e);
1674
0
      failf(data, "Failed to initialize SSL Engine '%s': %s",
1675
0
            name, ossl_strerror(ERR_get_error(), buf, sizeof(buf)));
1676
0
      result = CURLE_SSL_ENGINE_INITFAILED;
1677
0
      e = NULL;
1678
0
    }
1679
0
    else {
1680
0
      result = CURLE_OK;
1681
0
    }
1682
0
    data->state.engine = e;
1683
0
    return result;
1684
0
  }
1685
0
#endif
1686
0
#ifdef OPENSSL_HAS_PROVIDERS
1687
0
  return ossl_set_provider(data, name);
1688
#else
1689
  (void)name;
1690
  failf(data, "OpenSSL engine not found");
1691
  return CURLE_SSL_ENGINE_NOTFOUND;
1692
#endif
1693
0
}
1694
1695
/* Sets engine as default for all SSL operations
1696
 */
1697
static CURLcode ossl_set_engine_default(struct Curl_easy *data)
1698
0
{
1699
0
#ifdef USE_OPENSSL_ENGINE
1700
0
  if(data->state.engine) {
1701
0
    if(ENGINE_set_default(data->state.engine, ENGINE_METHOD_ALL) > 0) {
1702
0
      infof(data, "set default crypto engine '%s'",
1703
0
            ENGINE_get_id(data->state.engine));
1704
0
    }
1705
0
    else {
1706
0
      failf(data, "set default crypto engine '%s' failed",
1707
0
            ENGINE_get_id(data->state.engine));
1708
0
      return CURLE_SSL_ENGINE_SETFAILED;
1709
0
    }
1710
0
  }
1711
#else
1712
  (void)data;
1713
#endif
1714
0
  return CURLE_OK;
1715
0
}
1716
1717
/* Return list of OpenSSL crypto engine names.
1718
 */
1719
static struct curl_slist *ossl_engines_list(struct Curl_easy *data)
1720
0
{
1721
0
  struct curl_slist *list = NULL;
1722
0
#ifdef USE_OPENSSL_ENGINE
1723
0
  struct curl_slist *beg;
1724
0
  ENGINE *e;
1725
1726
0
  for(e = ENGINE_get_first(); e; e = ENGINE_get_next(e)) {
1727
0
    beg = curl_slist_append(list, ENGINE_get_id(e));
1728
0
    if(!beg) {
1729
0
      curl_slist_free_all(list);
1730
0
      return NULL;
1731
0
    }
1732
0
    list = beg;
1733
0
  }
1734
0
#endif
1735
0
  (void)data;
1736
0
  return list;
1737
0
}
1738
1739
#ifdef OPENSSL_HAS_PROVIDERS
1740
1741
static void ossl_provider_cleanup(struct Curl_easy *data)
1742
0
{
1743
0
  if(data->state.baseprov) {
1744
0
    OSSL_PROVIDER_unload(data->state.baseprov);
1745
0
    data->state.baseprov = NULL;
1746
0
  }
1747
0
  if(data->state.provider) {
1748
0
    OSSL_PROVIDER_unload(data->state.provider);
1749
0
    data->state.provider = NULL;
1750
0
  }
1751
0
  OSSL_LIB_CTX_free(data->state.libctx);
1752
0
  data->state.libctx = NULL;
1753
0
  curlx_safefree(data->state.propq);
1754
0
  data->state.provider_loaded = FALSE;
1755
0
}
1756
1757
0
#define MAX_PROVIDER_LEN 128 /* reasonable */
1758
1759
/* Selects an OpenSSL crypto provider.
1760
 *
1761
 * A provider might need an associated property, a string passed on to
1762
 * OpenSSL. Specify this as [PROVIDER][:PROPERTY]: separate the name and the
1763
 * property with a colon. No colon means no property is set.
1764
 *
1765
 * An example provider + property looks like "tpm2:?provider=tpm2".
1766
 */
1767
static CURLcode ossl_set_provider(struct Curl_easy *data, const char *iname)
1768
0
{
1769
0
  char name[MAX_PROVIDER_LEN + 1];
1770
0
  struct Curl_str prov;
1771
0
  const char *propq = NULL;
1772
1773
0
  if(!iname) {
1774
    /* clear and cleanup provider use */
1775
0
    ossl_provider_cleanup(data);
1776
0
    return CURLE_OK;
1777
0
  }
1778
0
  if(curlx_str_until(&iname, &prov, MAX_PROVIDER_LEN, ':'))
1779
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
1780
1781
0
  if(!curlx_str_single(&iname, ':'))
1782
    /* there was a colon, get the propq until the end of string */
1783
0
    propq = iname;
1784
1785
  /* we need the name in a buffer, null-terminated */
1786
0
  memcpy(name, curlx_str(&prov), curlx_strlen(&prov));
1787
0
  name[curlx_strlen(&prov)] = 0;
1788
1789
0
  if(!data->state.libctx) {
1790
0
    OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
1791
0
    if(!libctx)
1792
0
      return CURLE_OUT_OF_MEMORY;
1793
0
    if(propq) {
1794
0
      data->state.propq = curlx_strdup(propq);
1795
0
      if(!data->state.propq) {
1796
0
        OSSL_LIB_CTX_free(libctx);
1797
0
        return CURLE_OUT_OF_MEMORY;
1798
0
      }
1799
0
    }
1800
0
    data->state.libctx = libctx;
1801
0
  }
1802
1803
0
#ifndef CURL_DISABLE_OPENSSL_AUTO_LOAD_CONFIG
1804
  /* load the configuration file into the library context before checking the
1805
   * provider availability */
1806
0
  if(!OSSL_LIB_CTX_load_config(data->state.libctx, NULL)) {
1807
0
    infof(data, "Failed to load default openssl config. Proceeding.");
1808
0
  }
1809
0
#endif
1810
1811
0
  if(OSSL_PROVIDER_available(data->state.libctx, name)) {
1812
    /* already loaded through the configuration - no action needed */
1813
0
    data->state.provider_loaded = TRUE;
1814
0
    return CURLE_OK;
1815
0
  }
1816
1817
0
  data->state.provider = OSSL_PROVIDER_try_load(data->state.libctx, name, 1);
1818
0
  if(!data->state.provider) {
1819
0
    char error_buffer[256];
1820
0
    failf(data, "Failed to initialize provider: %s",
1821
0
          ossl_strerror(ERR_get_error(), error_buffer, sizeof(error_buffer)));
1822
0
    ossl_provider_cleanup(data);
1823
0
    return CURLE_SSL_ENGINE_NOTFOUND;
1824
0
  }
1825
1826
  /* load the base provider as well */
1827
0
  data->state.baseprov = OSSL_PROVIDER_try_load(data->state.libctx, "base", 1);
1828
0
  if(!data->state.baseprov) {
1829
0
    ossl_provider_cleanup(data);
1830
0
    failf(data, "Failed to load base");
1831
0
    return CURLE_SSL_ENGINE_NOTFOUND;
1832
0
  }
1833
0
  else
1834
0
    data->state.provider_loaded = TRUE;
1835
0
  return CURLE_OK;
1836
0
}
1837
#endif
1838
1839
static CURLcode ossl_shutdown(struct Curl_cfilter *cf,
1840
                              struct Curl_easy *data,
1841
                              bool send_shutdown, bool *done)
1842
0
{
1843
0
  struct ssl_connect_data *connssl = cf->ctx;
1844
0
  struct ossl_ctx *octx = (struct ossl_ctx *)connssl->backend;
1845
0
  CURLcode result = CURLE_OK;
1846
0
  char buf[1024];
1847
0
  int nread = -1, err;
1848
0
  size_t i;
1849
1850
0
  DEBUGASSERT(octx);
1851
0
  if(!octx->ssl || cf->shutdown) {
1852
0
    *done = TRUE;
1853
0
    goto out;
1854
0
  }
1855
1856
0
  connssl->io_need = CURL_SSL_IO_NEED_NONE;
1857
0
  *done = FALSE;
1858
0
  if(!(SSL_get_shutdown(octx->ssl) & SSL_SENT_SHUTDOWN)) {
1859
    /* We have not started the shutdown from our side yet. Check
1860
     * if the server already sent us one. */
1861
0
    ERR_clear_error();
1862
0
    for(i = 0; i < 10; ++i) {
1863
0
      nread = SSL_read(octx->ssl, buf, (int)sizeof(buf));
1864
0
      CURL_TRC_CF(data, cf, "SSL shutdown not sent, read -> %d", nread);
1865
0
      if(nread <= 0)
1866
0
        break;
1867
0
    }
1868
0
    err = SSL_get_error(octx->ssl, nread);
1869
0
    if(!nread && err == SSL_ERROR_ZERO_RETURN) {
1870
0
      bool input_pending;
1871
      /* Yes, it did. */
1872
0
      if(!send_shutdown) {
1873
0
        CURL_TRC_CF(data, cf, "SSL shutdown received, not sending");
1874
0
        *done = TRUE;
1875
0
        goto out;
1876
0
      }
1877
0
      else if(!cf->next->cft->is_alive(cf->next, data, &input_pending)) {
1878
        /* Server closed the connection after its closy notify. It
1879
         * seems not interested to see our close notify, so do not
1880
         * send it. We are done. */
1881
0
        connssl->peer_closed = TRUE;
1882
0
        CURL_TRC_CF(data, cf, "peer closed connection");
1883
0
        *done = TRUE;
1884
0
        goto out;
1885
0
      }
1886
0
    }
1887
0
  }
1888
1889
  /* SSL should now have started the shutdown from our side. Since it
1890
   * was not complete, we are lacking the close notify from the server. */
1891
0
  if(send_shutdown && !(SSL_get_shutdown(octx->ssl) & SSL_SENT_SHUTDOWN)) {
1892
0
    int rc;
1893
0
    ERR_clear_error();
1894
0
    CURL_TRC_CF(data, cf, "send SSL close notify");
1895
0
    rc = SSL_shutdown(octx->ssl);
1896
0
    if(rc == 1) {
1897
0
      CURL_TRC_CF(data, cf, "SSL shutdown finished");
1898
0
      *done = TRUE;
1899
0
      goto out;
1900
0
    }
1901
0
    if(SSL_ERROR_WANT_WRITE == SSL_get_error(octx->ssl, rc)) {
1902
0
      CURL_TRC_CF(data, cf, "SSL shutdown still wants to send");
1903
0
      connssl->io_need = CURL_SSL_IO_NEED_SEND;
1904
0
      goto out;
1905
0
    }
1906
    /* Having sent the close notify, we use SSL_read() to get the
1907
     * missing close notify from the server. */
1908
0
  }
1909
1910
0
  for(i = 0; i < 10; ++i) {
1911
0
    ERR_clear_error();
1912
0
    nread = SSL_read(octx->ssl, buf, (int)sizeof(buf));
1913
0
    CURL_TRC_CF(data, cf, "SSL shutdown read -> %d", nread);
1914
0
    if(nread <= 0)
1915
0
      break;
1916
0
  }
1917
0
  err = SSL_get_error(octx->ssl, nread);
1918
0
  switch(err) {
1919
0
  case SSL_ERROR_ZERO_RETURN: /* no more data */
1920
0
    if(SSL_shutdown(octx->ssl) == 1)
1921
0
      CURL_TRC_CF(data, cf, "SSL shutdown finished");
1922
0
    else
1923
0
      CURL_TRC_CF(data, cf, "SSL shutdown not received, but closed");
1924
0
    *done = TRUE;
1925
0
    break;
1926
0
  case SSL_ERROR_NONE: /* did not get anything */
1927
0
  case SSL_ERROR_WANT_READ:
1928
    /* SSL has send its notify and now wants to read the reply
1929
     * from the server. We are not really interested in that. */
1930
0
    CURL_TRC_CF(data, cf, "SSL shutdown sent, want receive");
1931
0
    connssl->io_need = CURL_SSL_IO_NEED_RECV;
1932
0
    break;
1933
0
  case SSL_ERROR_WANT_WRITE:
1934
0
    CURL_TRC_CF(data, cf, "SSL shutdown send blocked");
1935
0
    connssl->io_need = CURL_SSL_IO_NEED_SEND;
1936
0
    break;
1937
0
  default:
1938
    /* Server seems to have closed the connection without sending us
1939
     * a close notify. */
1940
0
    {
1941
0
      VERBOSE(unsigned long sslerr = ERR_get_error());
1942
0
      CURL_TRC_CF(data, cf, "SSL shutdown, ignore recv error: '%s', errno %d",
1943
0
                  (sslerr ?
1944
0
                   ossl_strerror(sslerr, buf, sizeof(buf)) :
1945
0
                   SSL_ERROR_to_str(err)),
1946
0
                  SOCKERRNO);
1947
0
    }
1948
0
    *done = TRUE;
1949
0
    result = CURLE_OK;
1950
0
    break;
1951
0
  }
1952
1953
0
out:
1954
0
  cf->shutdown = (result || *done);
1955
0
  if(cf->shutdown || (connssl->io_need != CURL_SSL_IO_NEED_NONE))
1956
0
    connssl->input_pending = FALSE;
1957
0
  return result;
1958
0
}
1959
1960
static void ossl_close(struct Curl_cfilter *cf, struct Curl_easy *data)
1961
0
{
1962
0
  struct ssl_connect_data *connssl = cf->ctx;
1963
0
  struct ossl_ctx *octx = (struct ossl_ctx *)connssl->backend;
1964
1965
0
  (void)data;
1966
0
  DEBUGASSERT(octx);
1967
1968
0
  connssl->input_pending = FALSE;
1969
0
  if(octx->ssl) {
1970
0
    SSL_free(octx->ssl);
1971
0
    octx->ssl = NULL;
1972
0
  }
1973
0
  if(octx->ssl_ctx) {
1974
0
    SSL_CTX_free(octx->ssl_ctx);
1975
0
    octx->ssl_ctx = NULL;
1976
0
    octx->x509_store_setup = FALSE;
1977
0
  }
1978
0
  if(octx->bio_method) {
1979
0
    ossl_bio_cf_method_free(octx->bio_method);
1980
0
    octx->bio_method = NULL;
1981
0
  }
1982
0
}
1983
1984
/*
1985
 * This function is called when the 'data' struct is going away. Close
1986
 * down everything and free all resources!
1987
 */
1988
static void ossl_close_all(struct Curl_easy *data)
1989
0
{
1990
0
#ifdef USE_OPENSSL_ENGINE
1991
0
  if(data->state.engine) {
1992
0
    ENGINE_finish(data->state.engine);
1993
0
    ENGINE_free(data->state.engine);
1994
0
    data->state.engine = NULL;
1995
0
  }
1996
#else
1997
  (void)data;
1998
#endif
1999
0
#ifdef OPENSSL_HAS_PROVIDERS
2000
0
  ossl_provider_cleanup(data);
2001
0
#endif
2002
0
}
2003
2004
/* ====================================================== */
2005
2006
/* Quote from RFC2818 section 3.1 "Server Identity"
2007
2008
   If a subjectAltName extension of type dNSName is present, that MUST
2009
   be used as the identity. Otherwise, the (most specific) Common Name
2010
   field in the Subject field of the certificate MUST be used. Although
2011
   the use of the Common Name is existing practice, it is deprecated and
2012
   Certification Authorities are encouraged to use the dNSName instead.
2013
2014
   Matching is performed using the matching rules specified by
2015
   [RFC2459]. If more than one identity of a given type is present in
2016
   the certificate (e.g., more than one dNSName name, a match in any one
2017
   of the set is considered acceptable.) Names may contain the wildcard
2018
   character * which is considered to match any single domain name
2019
   component or component fragment. E.g., *.a.com matches foo.a.com but
2020
   not bar.foo.a.com. f*.com matches foo.com but not bar.com.
2021
2022
   In some cases, the URI is specified as an IP address rather than a
2023
   hostname. In this case, the iPAddress subjectAltName must be present
2024
   in the certificate and must exactly match the IP in the URI.
2025
2026
   This function is now used from ngtcp2 (QUIC) as well.
2027
*/
2028
static CURLcode ossl_verifyhost(struct Curl_easy *data,
2029
                                struct connectdata *conn,
2030
                                struct ssl_peer *peer,
2031
                                X509 *server_cert)
2032
0
{
2033
0
  bool matched = FALSE;
2034
0
  int target; /* target type, GEN_DNS or GEN_IPADD */
2035
0
  size_t addrlen = 0;
2036
0
  STACK_OF(GENERAL_NAME) *altnames;
2037
0
#ifdef USE_IPV6
2038
0
  struct in6_addr addr;
2039
#else
2040
  struct in_addr addr;
2041
#endif
2042
0
  CURLcode result = CURLE_OK;
2043
0
  bool dNSName = FALSE; /* if a dNSName field exists in the cert */
2044
0
  bool iPAddress = FALSE; /* if an iPAddress field exists in the cert */
2045
0
  size_t hostlen = strlen(peer->dest->hostname);
2046
2047
0
  (void)conn;
2048
0
  switch(peer->type) {
2049
0
  case CURL_SSL_PEER_IPV4:
2050
0
    if(!curlx_inet_pton(AF_INET, peer->dest->hostname, &addr))
2051
0
      return CURLE_PEER_FAILED_VERIFICATION;
2052
0
    target = GEN_IPADD;
2053
0
    addrlen = sizeof(struct in_addr);
2054
0
    break;
2055
0
#ifdef USE_IPV6
2056
0
  case CURL_SSL_PEER_IPV6:
2057
0
    if(!curlx_inet_pton(AF_INET6, peer->dest->hostname, &addr))
2058
0
      return CURLE_PEER_FAILED_VERIFICATION;
2059
0
    target = GEN_IPADD;
2060
0
    addrlen = sizeof(struct in6_addr);
2061
0
    break;
2062
0
#endif
2063
0
  case CURL_SSL_PEER_DNS:
2064
0
    target = GEN_DNS;
2065
0
    break;
2066
0
  default:
2067
0
    DEBUGASSERT(0);
2068
0
    failf(data, "unexpected SSL peer type: %d", peer->type);
2069
0
    return CURLE_PEER_FAILED_VERIFICATION;
2070
0
  }
2071
2072
  /* get a "list" of alternative names */
2073
0
  altnames = X509_get_ext_d2i(server_cert, NID_subject_alt_name, NULL, NULL);
2074
2075
0
  if(altnames) {
2076
#ifdef HAVE_BORINGSSL_LIKE
2077
    size_t numalts;
2078
    size_t i;
2079
#else
2080
0
    int numalts;
2081
0
    int i;
2082
0
#endif
2083
2084
    /* get amount of alternatives, RFC2459 claims there MUST be at least
2085
       one, but we do not depend on it... */
2086
0
    numalts = sk_GENERAL_NAME_num(altnames);
2087
2088
    /* loop through all alternatives - until a dnsmatch */
2089
0
    for(i = 0; (i < numalts) && !matched; i++) {
2090
      /* get a handle to alternative name number i */
2091
0
      const GENERAL_NAME *check = sk_GENERAL_NAME_value(altnames, i);
2092
2093
0
      if(check->type == GEN_DNS)
2094
0
        dNSName = TRUE;
2095
0
      else if(check->type == GEN_IPADD)
2096
0
        iPAddress = TRUE;
2097
2098
      /* only check alternatives of the same type the target is */
2099
0
      if(check->type == target) {
2100
        /* get data and length */
2101
0
        const char *altptr = (const char *)ASN1_STRING_get0_data(check->d.ia5);
2102
0
        size_t altlen = (size_t)ASN1_STRING_length(check->d.ia5);
2103
2104
0
        switch(target) {
2105
0
        case GEN_DNS: /* name/pattern comparison */
2106
          /* The OpenSSL man page explicitly says: "In general it cannot be
2107
             assumed that the data returned by ASN1_STRING_data() is null
2108
             terminated or does not contain embedded nulls.", but also that
2109
             "The actual format of the data depends on the actual string
2110
             type itself: for example for an IA5String the data is ASCII"
2111
2112
             It has been however verified that in 0.9.6 and 0.9.7, IA5String
2113
             is always null-terminated.
2114
          */
2115
0
          if((altlen == strlen(altptr)) &&
2116
             /* if this is not true, there was an embedded zero in the name
2117
                string and we cannot match it. */
2118
0
             Curl_cert_hostcheck(altptr, altlen,
2119
0
                                 peer->dest->hostname, hostlen)) {
2120
0
            matched = TRUE;
2121
0
            infof(data, "  subjectAltName: \"%s\" matches cert's \"%.*s\"",
2122
0
                  peer->dest->user_hostname, (int)altlen, altptr);
2123
0
          }
2124
0
          break;
2125
2126
0
        case GEN_IPADD: /* IP address comparison */
2127
          /* compare alternative IP address if the data chunk is the same size
2128
             our server IP address is */
2129
0
          if((altlen == addrlen) && !memcmp(altptr, &addr, altlen)) {
2130
0
            matched = TRUE;
2131
0
            infof(data, "  subjectAltName: \"%s\" matches cert's IP address!",
2132
0
                  peer->dest->user_hostname);
2133
0
          }
2134
0
          break;
2135
0
        }
2136
0
      }
2137
0
    }
2138
0
    GENERAL_NAMES_free(altnames);
2139
0
  }
2140
2141
0
  if(matched)
2142
    /* an alternative name matched */
2143
0
    ;
2144
0
  else if(dNSName || iPAddress) {
2145
0
    const char *tname = (peer->type == CURL_SSL_PEER_DNS) ? "hostname" :
2146
0
                        (peer->type == CURL_SSL_PEER_IPV4) ?
2147
0
                        "ipv4 address" : "ipv6 address";
2148
0
    infof(data, " subjectAltName does not match %s %s", tname,
2149
0
          peer->dest->user_hostname);
2150
0
    failf(data, "SSL: no alternative certificate subject name matches "
2151
0
          "target %s '%s'", tname, peer->dest->user_hostname);
2152
0
    result = CURLE_PEER_FAILED_VERIFICATION;
2153
0
  }
2154
0
  else {
2155
    /* we have to look to the last occurrence of a commonName in the
2156
       distinguished one to get the most significant one. */
2157
0
    int i = -1;
2158
0
    unsigned char *cn = NULL;
2159
0
    int cnlen = 0;
2160
0
    bool free_cn = FALSE;
2161
2162
    /* The following is done because of a bug in 0.9.6b */
2163
0
    const X509_NAME *name = X509_get_subject_name(server_cert);
2164
0
    if(name) {
2165
0
      int j;
2166
0
      while((j = X509_NAME_get_index_by_NID(name, NID_commonName, i)) >= 0)
2167
0
        i = j;
2168
0
    }
2169
2170
    /* we have the name entry and we now convert this to a string
2171
       that we can use for comparison. Doing this we support BMPstring,
2172
       UTF8, etc. */
2173
2174
0
    if(i >= 0) {
2175
0
      const ASN1_STRING *tmp =
2176
0
        X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name, i));
2177
2178
      /* In OpenSSL 0.9.7d and earlier, ASN1_STRING_to_UTF8 fails if the input
2179
         is already UTF-8 encoded. We check for this case and copy the raw
2180
         string manually to avoid the problem. This code can be made
2181
         conditional in the future when OpenSSL has been fixed. */
2182
0
      if(tmp) {
2183
0
        if(ASN1_STRING_type(tmp) == V_ASN1_UTF8STRING) {
2184
0
          cnlen = ASN1_STRING_length(tmp);
2185
0
          cn = (unsigned char *)CURL_UNCONST(ASN1_STRING_get0_data(tmp));
2186
0
        }
2187
0
        else { /* not a UTF8 name */
2188
0
          cnlen = ASN1_STRING_to_UTF8(&cn, tmp);
2189
0
          free_cn = TRUE;
2190
0
        }
2191
2192
0
        if((cnlen <= 0) || !cn)
2193
0
          result = CURLE_OUT_OF_MEMORY;
2194
0
        else if((size_t)cnlen != strlen((char *)cn)) {
2195
          /* there was a terminating zero before the end of string, this
2196
             cannot match and we return failure! */
2197
0
          failf(data, "SSL: illegal cert name field");
2198
0
          result = CURLE_PEER_FAILED_VERIFICATION;
2199
0
        }
2200
0
      }
2201
0
    }
2202
2203
0
    if(result)
2204
      /* error already detected, pass through */
2205
0
      ;
2206
0
    else if(!cn) {
2207
0
      failf(data, "SSL: unable to obtain common name from peer certificate");
2208
0
      result = CURLE_PEER_FAILED_VERIFICATION;
2209
0
    }
2210
0
    else if(!Curl_cert_hostcheck((const char *)cn, cnlen,
2211
0
                                 peer->dest->hostname, hostlen)) {
2212
0
      failf(data, "SSL: certificate subject name '%s' does not match "
2213
0
            "target hostname '%s'", cn, peer->dest->user_hostname);
2214
0
      result = CURLE_PEER_FAILED_VERIFICATION;
2215
0
    }
2216
0
    else {
2217
0
      infof(data, " common name: %s (matched)", cn);
2218
0
    }
2219
0
    if(free_cn)
2220
0
      OPENSSL_free(cn);
2221
0
  }
2222
2223
0
  return result;
2224
0
}
2225
2226
#ifndef OPENSSL_NO_OCSP
2227
static CURLcode verifystatus(struct Curl_cfilter *cf,
2228
                             struct Curl_easy *data,
2229
                             struct ossl_ctx *octx)
2230
0
{
2231
0
  int i, ocsp_status;
2232
#ifdef HAVE_BORINGSSL_LIKE
2233
  const uint8_t *status;
2234
#else
2235
0
  unsigned char *status;
2236
0
#endif
2237
0
  const unsigned char *p;
2238
0
  CURLcode result = CURLE_OK;
2239
0
  OCSP_RESPONSE *rsp = NULL;
2240
0
  OCSP_BASICRESP *br = NULL;
2241
0
  X509_STORE     *st = NULL;
2242
0
  STACK_OF(X509) *ch = NULL;
2243
0
  X509 *cert;
2244
0
  OCSP_CERTID *id = NULL;
2245
0
  int cert_status, crl_reason;
2246
0
  ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
2247
0
  int ret;
2248
0
  long len;
2249
2250
0
  (void)cf;
2251
0
  DEBUGASSERT(octx);
2252
2253
0
  len = (long)SSL_get_tlsext_status_ocsp_resp(octx->ssl, &status);
2254
2255
0
  if(!status) {
2256
0
    failf(data, "No OCSP response received");
2257
0
    result = CURLE_SSL_INVALIDCERTSTATUS;
2258
0
    goto end;
2259
0
  }
2260
0
  p = status;
2261
0
  rsp = d2i_OCSP_RESPONSE(NULL, &p, len);
2262
0
  if(!rsp) {
2263
0
    failf(data, "Invalid OCSP response");
2264
0
    result = CURLE_SSL_INVALIDCERTSTATUS;
2265
0
    goto end;
2266
0
  }
2267
2268
0
  ocsp_status = OCSP_response_status(rsp);
2269
0
  if(ocsp_status != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
2270
0
    failf(data, "Invalid OCSP response status: %s (%d)",
2271
0
          OCSP_response_status_str(ocsp_status), ocsp_status);
2272
0
    result = CURLE_SSL_INVALIDCERTSTATUS;
2273
0
    goto end;
2274
0
  }
2275
2276
0
  br = OCSP_response_get1_basic(rsp);
2277
0
  if(!br) {
2278
0
    failf(data, "Invalid OCSP response");
2279
0
    result = CURLE_SSL_INVALIDCERTSTATUS;
2280
0
    goto end;
2281
0
  }
2282
2283
0
  ch = SSL_get_peer_cert_chain(octx->ssl);
2284
0
  if(!ch) {
2285
0
    failf(data, "Could not get peer certificate chain");
2286
0
    result = CURLE_SSL_INVALIDCERTSTATUS;
2287
0
    goto end;
2288
0
  }
2289
0
  st = SSL_CTX_get_cert_store(octx->ssl_ctx);
2290
2291
0
  if(OCSP_basic_verify(br, ch, st, 0) <= 0) {
2292
0
    failf(data, "OCSP response verification failed");
2293
0
    result = CURLE_SSL_INVALIDCERTSTATUS;
2294
0
    goto end;
2295
0
  }
2296
2297
  /* Compute the certificate's ID */
2298
0
  cert = SSL_get1_peer_certificate(octx->ssl);
2299
0
  if(!cert) {
2300
0
    failf(data, "Error getting peer certificate");
2301
0
    result = CURLE_SSL_INVALIDCERTSTATUS;
2302
0
    goto end;
2303
0
  }
2304
2305
0
  for(i = 0; i < (int)sk_X509_num(ch); i++) {
2306
0
    X509 *issuer = sk_X509_value(ch, (ossl_valsize_t)i);
2307
0
    if(X509_check_issued(issuer, cert) == X509_V_OK) {
2308
      /* Note to analysis tools: using SHA1 here is fine. The `id`
2309
       * generated is used as a hash lookup key, not as a verifier
2310
       * of the OCSP data itself. This all according to RFC 5019. */
2311
0
      id = OCSP_cert_to_id(EVP_sha1(), cert, issuer);
2312
0
      break;
2313
0
    }
2314
0
  }
2315
0
  X509_free(cert);
2316
2317
0
  if(!id) {
2318
0
    failf(data, "Error computing OCSP ID");
2319
0
    result = CURLE_SSL_INVALIDCERTSTATUS;
2320
0
    goto end;
2321
0
  }
2322
2323
  /* Find the single OCSP response corresponding to the certificate ID */
2324
0
  ret = OCSP_resp_find_status(br, id, &cert_status, &crl_reason, &rev,
2325
0
                              &thisupd, &nextupd);
2326
0
  OCSP_CERTID_free(id);
2327
0
  if(ret != 1) {
2328
0
    failf(data, "Could not find certificate ID in OCSP response");
2329
0
    result = CURLE_SSL_INVALIDCERTSTATUS;
2330
0
    goto end;
2331
0
  }
2332
2333
  /* Validate the OCSP response issuing and update times.
2334
   * - `thisupd` is the time the OCSP response was issued
2335
   * - `nextupd` is the time the OCSP response should be updated
2336
   *    (valid life time assigned by the OCSP responder)
2337
   * - 3rd param: how many seconds of clock skew we allow between
2338
   *   our clock and the instance that issued the OCSP response
2339
   * - 4th param: how many seconds in the past `thisupd` may be, with
2340
   *   -1 meaning there is no limit. */
2341
0
  if(!OCSP_check_validity(thisupd, nextupd, 300L, -1L)) {
2342
0
    failf(data, "OCSP response has expired");
2343
0
    result = CURLE_SSL_INVALIDCERTSTATUS;
2344
0
    goto end;
2345
0
  }
2346
2347
0
  infof(data, "SSL certificate status: %s (%d)",
2348
0
        OCSP_cert_status_str(cert_status), cert_status);
2349
2350
0
  switch(cert_status) {
2351
0
  case V_OCSP_CERTSTATUS_GOOD:
2352
0
    break;
2353
2354
0
  case V_OCSP_CERTSTATUS_REVOKED:
2355
0
    result = CURLE_SSL_INVALIDCERTSTATUS;
2356
0
    failf(data, "SSL certificate revocation reason: %s (%d)",
2357
0
          OCSP_crl_reason_str(crl_reason), crl_reason);
2358
0
    goto end;
2359
2360
0
  case V_OCSP_CERTSTATUS_UNKNOWN:
2361
0
  default:
2362
0
    result = CURLE_SSL_INVALIDCERTSTATUS;
2363
0
    goto end;
2364
0
  }
2365
2366
0
end:
2367
0
  if(br)
2368
0
    OCSP_BASICRESP_free(br);
2369
0
  OCSP_RESPONSE_free(rsp);
2370
2371
0
  return result;
2372
0
}
2373
#endif
2374
2375
static const char *ssl_msg_type(int ssl_ver, int msg)
2376
0
{
2377
0
  if(ssl_ver == SSL3_VERSION_MAJOR) {
2378
0
    switch(msg) {
2379
0
    case SSL3_MT_HELLO_REQUEST:
2380
0
      return "Hello request";
2381
0
    case SSL3_MT_CLIENT_HELLO:
2382
0
      return "Client hello";
2383
0
    case SSL3_MT_SERVER_HELLO:
2384
0
      return "Server hello";
2385
0
#ifdef SSL3_MT_NEWSESSION_TICKET
2386
0
    case SSL3_MT_NEWSESSION_TICKET:
2387
0
      return "Newsession Ticket";
2388
0
#endif
2389
0
    case SSL3_MT_CERTIFICATE:
2390
0
      return "Certificate";
2391
0
    case SSL3_MT_SERVER_KEY_EXCHANGE:
2392
0
      return "Server key exchange";
2393
0
    case SSL3_MT_CLIENT_KEY_EXCHANGE:
2394
0
      return "Client key exchange";
2395
0
    case SSL3_MT_CERTIFICATE_REQUEST:
2396
0
      return "Request CERT";
2397
0
    case SSL3_MT_SERVER_DONE:
2398
0
      return "Server finished";
2399
0
    case SSL3_MT_CERTIFICATE_VERIFY:
2400
0
      return "CERT verify";
2401
0
    case SSL3_MT_FINISHED:
2402
0
      return "Finished";
2403
0
#ifdef SSL3_MT_CERTIFICATE_STATUS
2404
0
    case SSL3_MT_CERTIFICATE_STATUS:
2405
0
      return "Certificate Status";
2406
0
#endif
2407
0
#ifdef SSL3_MT_ENCRYPTED_EXTENSIONS
2408
0
    case SSL3_MT_ENCRYPTED_EXTENSIONS:
2409
0
      return "Encrypted Extensions";
2410
0
#endif
2411
0
#ifdef SSL3_MT_SUPPLEMENTAL_DATA
2412
0
    case SSL3_MT_SUPPLEMENTAL_DATA:
2413
0
      return "Supplemental data";
2414
0
#endif
2415
0
#ifdef SSL3_MT_END_OF_EARLY_DATA
2416
0
    case SSL3_MT_END_OF_EARLY_DATA:
2417
0
      return "End of early data";
2418
0
#endif
2419
0
#ifdef SSL3_MT_KEY_UPDATE
2420
0
    case SSL3_MT_KEY_UPDATE:
2421
0
      return "Key update";
2422
0
#endif
2423
0
#ifdef SSL3_MT_NEXT_PROTO
2424
0
    case SSL3_MT_NEXT_PROTO:
2425
0
      return "Next protocol";
2426
0
#endif
2427
0
#ifdef SSL3_MT_MESSAGE_HASH
2428
0
    case SSL3_MT_MESSAGE_HASH:
2429
0
      return "Message hash";
2430
0
#endif
2431
0
    }
2432
0
  }
2433
0
  return "Unknown";
2434
0
}
2435
2436
static const char *tls_rt_type(int type)
2437
0
{
2438
0
  switch(type) {
2439
0
#ifdef SSL3_RT_HEADER
2440
0
  case SSL3_RT_HEADER:
2441
0
    return "TLS header";
2442
0
#endif
2443
0
  case SSL3_RT_CHANGE_CIPHER_SPEC:
2444
0
    return "TLS change cipher";
2445
0
  case SSL3_RT_ALERT:
2446
0
    return "TLS alert";
2447
0
  case SSL3_RT_HANDSHAKE:
2448
0
    return "TLS handshake";
2449
0
  case SSL3_RT_APPLICATION_DATA:
2450
0
    return "TLS app data";
2451
0
  default:
2452
0
    return "TLS Unknown";
2453
0
  }
2454
0
}
2455
2456
/*
2457
 * Our callback from the SSL/TLS layers.
2458
 */
2459
static void ossl_trace(int direction, int ssl_ver, int content_type,
2460
                       const void *buf, size_t len, SSL *ssl,
2461
                       void *userp)
2462
0
{
2463
0
  const char *verstr;
2464
0
  struct Curl_cfilter *cf = userp;
2465
0
  struct Curl_easy *data = NULL;
2466
0
  char unknown[32];
2467
2468
0
  if(!cf)
2469
0
    return;
2470
0
  data = CF_DATA_CURRENT(cf);
2471
0
  if(!data || !data->set.fdebug || (direction && direction != 1))
2472
0
    return;
2473
2474
0
  switch(ssl_ver) {
2475
0
#ifdef SSL3_VERSION
2476
0
  case SSL3_VERSION:
2477
0
    verstr = "SSLv3";
2478
0
    break;
2479
0
#endif
2480
0
  case TLS1_VERSION:
2481
0
    verstr = "TLSv1.0";
2482
0
    break;
2483
0
#ifdef TLS1_1_VERSION
2484
0
  case TLS1_1_VERSION:
2485
0
    verstr = "TLSv1.1";
2486
0
    break;
2487
0
#endif
2488
0
#ifdef TLS1_2_VERSION
2489
0
  case TLS1_2_VERSION:
2490
0
    verstr = "TLSv1.2";
2491
0
    break;
2492
0
#endif
2493
0
  case TLS1_3_VERSION:
2494
0
    verstr = "TLSv1.3";
2495
0
    break;
2496
0
  default:
2497
0
    curl_msnprintf(unknown, sizeof(unknown), "(%x)", ssl_ver);
2498
0
    verstr = unknown;
2499
0
    break;
2500
0
  }
2501
2502
  /* Log progress for interesting records only (like Handshake or Alert), skip
2503
   * all raw record headers (content_type == SSL3_RT_HEADER or ssl_ver == 0).
2504
   * For TLS 1.3, skip notification of the decrypted inner Content-Type.
2505
   */
2506
0
  if(ssl_ver
2507
0
#ifdef SSL3_RT_HEADER
2508
0
     && content_type != SSL3_RT_HEADER
2509
0
#endif
2510
0
#ifdef SSL3_RT_INNER_CONTENT_TYPE
2511
0
     && content_type != SSL3_RT_INNER_CONTENT_TYPE
2512
0
#endif
2513
0
    ) {
2514
0
    const char *msg_name = "Truncated message";
2515
0
    const char *tls_rt_name;
2516
0
    char ssl_buf[1024];
2517
0
    int msg_type = 0;
2518
0
    int txt_len;
2519
2520
    /* the info given when the version is zero is not that useful for us */
2521
2522
0
    ssl_ver >>= 8; /* check the upper 8 bits only below */
2523
2524
    /* SSLv2 does not seem to have TLS record-type headers, so OpenSSL
2525
     * always pass-up content-type as 0, but the interesting message-type
2526
     * is at 'buf[0]'.
2527
     */
2528
0
    if(ssl_ver == SSL3_VERSION_MAJOR && content_type)
2529
0
      tls_rt_name = tls_rt_type(content_type);
2530
0
    else
2531
0
      tls_rt_name = "";
2532
2533
0
    if(content_type == SSL3_RT_CHANGE_CIPHER_SPEC) {
2534
0
      if(len) {
2535
0
        msg_type = *(const unsigned char *)buf;
2536
0
        msg_name = "Change cipher spec";
2537
0
      }
2538
0
    }
2539
0
    else if(content_type == SSL3_RT_ALERT) {
2540
0
      if(len >= 2) {
2541
0
        msg_type =
2542
0
          (((const unsigned char *)buf)[0] << 8) +
2543
0
           ((const unsigned char *)buf)[1];
2544
0
        msg_name = SSL_alert_desc_string_long(msg_type);
2545
0
      }
2546
0
    }
2547
0
    else if(len) {
2548
0
      msg_type = *(const unsigned char *)buf;
2549
0
      msg_name = ssl_msg_type(ssl_ver, msg_type);
2550
0
    }
2551
2552
0
    txt_len = curl_msnprintf(ssl_buf, sizeof(ssl_buf),
2553
0
                             "%s (%s), %s, %s (%d):\n",
2554
0
                             verstr, direction ? "OUT" : "IN",
2555
0
                             tls_rt_name, msg_name, msg_type);
2556
0
    Curl_debug(data, CURLINFO_TEXT, ssl_buf, (size_t)txt_len);
2557
0
  }
2558
2559
0
  Curl_debug(data, (direction == 1) ? CURLINFO_SSL_DATA_OUT :
2560
0
             CURLINFO_SSL_DATA_IN, (const char *)buf, len);
2561
0
  (void)ssl;
2562
0
}
2563
2564
static CURLcode ossl_set_ssl_version_min_max(struct Curl_cfilter *cf,
2565
                                             SSL_CTX *ctx,
2566
                                             unsigned int ssl_version_min)
2567
0
{
2568
0
  struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
2569
  /* first, TLS min version... */
2570
0
  long curl_ssl_version_min = (long)ssl_version_min;
2571
0
  long curl_ssl_version_max;
2572
2573
  /* convert curl min SSL version option to OpenSSL constant */
2574
#if defined(HAVE_BORINGSSL_LIKE) || defined(LIBRESSL_VERSION_NUMBER)
2575
  uint16_t ossl_ssl_version_min = 0;
2576
  uint16_t ossl_ssl_version_max = 0;
2577
#else
2578
0
  long ossl_ssl_version_min = 0;
2579
0
  long ossl_ssl_version_max = 0;
2580
0
#endif
2581
  /* it cannot be default here */
2582
0
  DEBUGASSERT(curl_ssl_version_min != CURL_SSLVERSION_DEFAULT);
2583
0
  switch(curl_ssl_version_min) {
2584
0
  case CURL_SSLVERSION_TLSv1: /* TLS 1.x */
2585
0
  case CURL_SSLVERSION_TLSv1_0:
2586
0
    ossl_ssl_version_min = TLS1_VERSION;
2587
0
    break;
2588
0
  case CURL_SSLVERSION_TLSv1_1:
2589
0
    ossl_ssl_version_min = TLS1_1_VERSION;
2590
0
    break;
2591
0
  case CURL_SSLVERSION_TLSv1_2:
2592
0
    ossl_ssl_version_min = TLS1_2_VERSION;
2593
0
    break;
2594
0
  case CURL_SSLVERSION_TLSv1_3:
2595
0
    ossl_ssl_version_min = TLS1_3_VERSION;
2596
0
    break;
2597
0
  }
2598
2599
  /* ... then, TLS max version */
2600
0
  curl_ssl_version_max = (long)conn_config->version_max;
2601
2602
  /* convert curl max SSL version option to OpenSSL constant */
2603
0
  switch(curl_ssl_version_max) {
2604
0
  case CURL_SSLVERSION_MAX_TLSv1_0:
2605
0
    ossl_ssl_version_max = TLS1_VERSION;
2606
0
    break;
2607
0
  case CURL_SSLVERSION_MAX_TLSv1_1:
2608
0
    ossl_ssl_version_max = TLS1_1_VERSION;
2609
0
    break;
2610
0
  case CURL_SSLVERSION_MAX_TLSv1_2:
2611
0
    ossl_ssl_version_max = TLS1_2_VERSION;
2612
0
    break;
2613
0
  case CURL_SSLVERSION_MAX_TLSv1_3:
2614
0
    ossl_ssl_version_max = TLS1_3_VERSION;
2615
0
    break;
2616
0
  case CURL_SSLVERSION_MAX_NONE:  /* none selected */
2617
0
  case CURL_SSLVERSION_MAX_DEFAULT:  /* max selected */
2618
0
  default:
2619
    /* SSL_CTX_set_max_proto_version states that: setting the maximum to 0
2620
       enables protocol versions up to the highest version supported by
2621
       the library */
2622
0
    ossl_ssl_version_max = 0;
2623
0
    break;
2624
0
  }
2625
2626
0
  if(!SSL_CTX_set_min_proto_version(ctx, ossl_ssl_version_min) ||
2627
0
     !SSL_CTX_set_max_proto_version(ctx, ossl_ssl_version_max))
2628
0
    return CURLE_SSL_CONNECT_ERROR;
2629
2630
0
  return CURLE_OK;
2631
0
}
2632
2633
#ifdef HAVE_BORINGSSL_LIKE
2634
typedef uint32_t ctx_option_t;
2635
#elif defined(HAVE_OPENSSL3)
2636
typedef uint64_t ctx_option_t;
2637
#elif defined(LIBRESSL_VERSION_NUMBER)
2638
typedef long ctx_option_t;
2639
#else
2640
typedef unsigned long ctx_option_t;
2641
#endif
2642
2643
CURLcode Curl_ossl_add_session(struct Curl_cfilter *cf,
2644
                               struct Curl_easy *data,
2645
                               const char *ssl_peer_key,
2646
                               SSL_SESSION *session,
2647
                               int ietf_tls_id,
2648
                               const char *alpn,
2649
                               unsigned char *quic_tp,
2650
                               size_t quic_tp_len)
2651
0
{
2652
0
  unsigned char *der_session_buf = NULL;
2653
0
  unsigned char *qtp_clone = NULL;
2654
0
  CURLcode result = CURLE_OK;
2655
2656
0
  if(!cf || !data)
2657
0
    goto out;
2658
2659
0
  if(Curl_ssl_scache_use(cf, data)) {
2660
0
    struct Curl_ssl_session *sc_session = NULL;
2661
0
    size_t der_session_size;
2662
0
    unsigned char *der_session_ptr;
2663
0
    size_t earlydata_max = 0;
2664
2665
0
    der_session_size = i2d_SSL_SESSION(session, NULL);
2666
0
    if(der_session_size == 0) {
2667
0
      result = CURLE_OUT_OF_MEMORY;
2668
0
      goto out;
2669
0
    }
2670
2671
0
    der_session_buf = der_session_ptr = curlx_malloc(der_session_size);
2672
0
    if(!der_session_buf) {
2673
0
      result = CURLE_OUT_OF_MEMORY;
2674
0
      goto out;
2675
0
    }
2676
2677
0
    der_session_size = i2d_SSL_SESSION(session, &der_session_ptr);
2678
0
    if(der_session_size == 0) {
2679
0
      result = CURLE_OUT_OF_MEMORY;
2680
0
      goto out;
2681
0
    }
2682
2683
0
#ifdef HAVE_OPENSSL_EARLYDATA
2684
0
    earlydata_max = SSL_SESSION_get_max_early_data(session);
2685
0
#endif
2686
0
    if(quic_tp && quic_tp_len) {
2687
0
      qtp_clone = curlx_memdup0((const char *)quic_tp, quic_tp_len);
2688
0
      if(!qtp_clone) {
2689
0
        result = CURLE_OUT_OF_MEMORY;
2690
0
        goto out;
2691
0
      }
2692
0
    }
2693
2694
0
    result = Curl_ssl_session_create2(der_session_buf, der_session_size,
2695
0
                                      ietf_tls_id, alpn,
2696
0
                                      (curl_off_t)time(NULL) +
2697
0
                                        SSL_SESSION_get_timeout(session),
2698
0
                                      earlydata_max, qtp_clone, quic_tp_len,
2699
0
                                      &sc_session);
2700
0
    der_session_buf = NULL;  /* took ownership of sdata */
2701
0
    if(!result) {
2702
0
      result = Curl_ssl_scache_put(cf, data, ssl_peer_key, sc_session);
2703
      /* took ownership of `sc_session` */
2704
0
    }
2705
0
  }
2706
2707
0
out:
2708
0
  curlx_free(der_session_buf);
2709
0
  return result;
2710
0
}
2711
2712
/* The "new session" callback must return zero if the session can be removed
2713
 * or non-zero if the session has been put into the session cache.
2714
 */
2715
static int ossl_new_session_cb(SSL *ssl, SSL_SESSION *ssl_sessionid)
2716
0
{
2717
0
  struct Curl_cfilter *cf = (struct Curl_cfilter *)SSL_get_app_data(ssl);
2718
0
  if(cf) {
2719
0
    struct Curl_easy *data = CF_DATA_CURRENT(cf);
2720
0
    struct ssl_connect_data *connssl = cf->ctx;
2721
0
    Curl_ossl_add_session(cf, data, connssl->peer.scache_key, ssl_sessionid,
2722
0
                          SSL_version(ssl), connssl->negotiated.alpn, NULL, 0);
2723
0
  }
2724
0
  return 0;
2725
0
}
2726
2727
static CURLcode load_cacert_from_memory(X509_STORE *store,
2728
                                        const struct curl_blob *ca_info_blob)
2729
0
{
2730
  /* these need to be freed at the end */
2731
0
  BIO *cbio = NULL;
2732
0
  STACK_OF(X509_INFO) *inf = NULL;
2733
2734
  /* everything else is a reference */
2735
0
  int i, count = 0;
2736
0
  X509_INFO *itmp = NULL;
2737
2738
0
  if(ca_info_blob->len > (size_t)INT_MAX)
2739
0
    return CURLE_SSL_CACERT_BADFILE;
2740
2741
0
  cbio = BIO_new_mem_buf(ca_info_blob->data, (int)ca_info_blob->len);
2742
0
  if(!cbio)
2743
0
    return CURLE_OUT_OF_MEMORY;
2744
2745
0
  inf = PEM_X509_INFO_read_bio(cbio, NULL, NULL, NULL);
2746
0
  if(!inf) {
2747
0
    BIO_free(cbio);
2748
0
    return CURLE_SSL_CACERT_BADFILE;
2749
0
  }
2750
2751
  /* add each entry from PEM file to x509_store */
2752
0
  for(i = 0; i < (int)sk_X509_INFO_num(inf); ++i) {
2753
0
    itmp = sk_X509_INFO_value(inf, (ossl_valsize_t)i);
2754
0
    if(itmp->x509) {
2755
0
      if(X509_STORE_add_cert(store, itmp->x509)) {
2756
0
        ++count;
2757
0
      }
2758
0
      else {
2759
        /* set count to 0 to return an error */
2760
0
        count = 0;
2761
0
        break;
2762
0
      }
2763
0
    }
2764
0
    if(itmp->crl) {
2765
0
      if(X509_STORE_add_crl(store, itmp->crl)) {
2766
0
        ++count;
2767
0
      }
2768
0
      else {
2769
        /* set count to 0 to return an error */
2770
0
        count = 0;
2771
0
        break;
2772
0
      }
2773
0
    }
2774
0
  }
2775
2776
0
#if defined(__clang__) && __clang_major__ >= 16
2777
0
#pragma clang diagnostic push
2778
0
#pragma clang diagnostic ignored "-Wcast-function-type-strict"
2779
0
#endif
2780
0
  sk_X509_INFO_pop_free(inf, X509_INFO_free);
2781
0
#if defined(__clang__) && __clang_major__ >= 16
2782
0
#pragma clang diagnostic pop
2783
0
#endif
2784
0
  BIO_free(cbio);
2785
2786
  /* if we did not end up importing anything, treat that as an error */
2787
0
  return (count > 0) ? CURLE_OK : CURLE_SSL_CACERT_BADFILE;
2788
0
}
2789
2790
#ifdef USE_WIN32_CRYPTO
2791
static CURLcode ossl_win_load_store(struct Curl_easy *data,
2792
                                    struct Curl_cfilter *cf,
2793
                                    const char *win_store,
2794
                                    X509_STORE *store,
2795
                                    bool *padded)
2796
{
2797
  CURLcode result = CURLE_OK;
2798
  HCERTSTORE hStore;
2799
2800
  *padded = FALSE;
2801
2802
  hStore = CertOpenSystemStoreA(0, win_store);
2803
  if(hStore) {
2804
    PCCERT_CONTEXT pContext = NULL;
2805
    /* The array of enhanced key usage OIDs varies per certificate and
2806
       is declared outside of the loop so that rather than malloc/free each
2807
       iteration we can grow it with realloc, when necessary. */
2808
    CERT_ENHKEY_USAGE *enhkey_usage = NULL;
2809
    DWORD enhkey_usage_size = 0;
2810
    VERBOSE(size_t total = 0);
2811
    VERBOSE(size_t imported = 0);
2812
2813
    /* This loop makes a best effort to import all valid certificates from
2814
       the MS root store. If a certificate cannot be imported it is
2815
       skipped. 'result' is used to store only hard-fail conditions (such
2816
       as out of memory) that cause an early break. */
2817
    result = CURLE_OK;
2818
    for(;;) {
2819
      X509 *x509;
2820
      FILETIME now;
2821
      BYTE key_usage[2];
2822
      DWORD req_size;
2823
      const unsigned char *encoded_cert;
2824
      pContext = CertEnumCertificatesInStore(hStore, pContext);
2825
      if(!pContext)
2826
        break;
2827
2828
      VERBOSE(++total);
2829
2830
#if defined(DEBUGBUILD) && defined(CURLVERBOSE)
2831
      {
2832
        char cert_name[256];
2833
        if(!CertGetNameStringA(pContext, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0,
2834
                               NULL, cert_name, sizeof(cert_name)))
2835
          infof(data, "SSL: unknown cert name");
2836
        else
2837
          infof(data, "SSL: Checking cert \"%s\"", cert_name);
2838
      }
2839
#endif
2840
      encoded_cert = (const unsigned char *)pContext->pbCertEncoded;
2841
      if(!encoded_cert)
2842
        continue;
2843
2844
      GetSystemTimeAsFileTime(&now);
2845
      if(CompareFileTime(&pContext->pCertInfo->NotBefore, &now) > 0 ||
2846
         CompareFileTime(&now, &pContext->pCertInfo->NotAfter) > 0)
2847
        continue;
2848
2849
      /* If key usage exists check for signing attribute */
2850
      if(CertGetIntendedKeyUsage(pContext->dwCertEncodingType,
2851
                                 pContext->pCertInfo,
2852
                                 key_usage, sizeof(key_usage))) {
2853
        if(!(key_usage[0] & CERT_KEY_CERT_SIGN_KEY_USAGE))
2854
          continue;
2855
      }
2856
      else if(GetLastError())
2857
        continue;
2858
2859
      /* If enhanced key usage exists check for server auth attribute.
2860
       *
2861
       * Note "In a Microsoft environment, a certificate might also have
2862
       * EKU extended properties that specify valid uses for the
2863
       * certificate."  The call below checks both, and behavior varies
2864
       * depending on what is found. For more details see
2865
       * CertGetEnhancedKeyUsage doc.
2866
       */
2867
      if(CertGetEnhancedKeyUsage(pContext, 0, NULL, &req_size) && req_size) {
2868
        if(req_size > enhkey_usage_size) {
2869
          void *tmp = curlx_realloc(enhkey_usage, req_size);
2870
2871
          if(!tmp) {
2872
            failf(data, "SSL: Out of memory allocating for OID list");
2873
            result = CURLE_OUT_OF_MEMORY;
2874
            break;
2875
          }
2876
2877
          enhkey_usage = (CERT_ENHKEY_USAGE *)tmp;
2878
          enhkey_usage_size = req_size;
2879
        }
2880
2881
        if(CertGetEnhancedKeyUsage(pContext, 0, enhkey_usage, &req_size)) {
2882
          if(!enhkey_usage->cUsageIdentifier) {
2883
            /* "If GetLastError returns CRYPT_E_NOT_FOUND, the certificate
2884
               is good for all uses. If it returns zero, the certificate
2885
               has no valid uses." */
2886
            if((HRESULT)GetLastError() != CRYPT_E_NOT_FOUND)
2887
              continue;
2888
          }
2889
          else {
2890
            DWORD i;
2891
            bool found = FALSE;
2892
2893
            for(i = 0; i < enhkey_usage->cUsageIdentifier; ++i) {
2894
              if(!strcmp("1.3.6.1.5.5.7.3.1" /* OID server auth */,
2895
                         enhkey_usage->rgpszUsageIdentifier[i])) {
2896
                found = TRUE;
2897
                break;
2898
              }
2899
            }
2900
2901
            if(!found)
2902
              continue;
2903
          }
2904
        }
2905
        else
2906
          continue;
2907
      }
2908
      else
2909
        continue;
2910
2911
      x509 = d2i_X509(NULL, &encoded_cert, (long)pContext->cbCertEncoded);
2912
      if(!x509)
2913
        continue;
2914
2915
      /* Try to import the certificate. This may fail for legitimate reasons
2916
         such as duplicate certificate, which is allowed by MS but not
2917
         OpenSSL. */
2918
      if(X509_STORE_add_cert(store, x509) == 1) {
2919
        VERBOSE(++imported);
2920
#ifdef DEBUGBUILD
2921
        infof(data, "SSL: Imported cert");
2922
#endif
2923
        *padded = TRUE;
2924
      }
2925
      X509_free(x509);
2926
    }
2927
2928
    curlx_free(enhkey_usage);
2929
    CertFreeCertificateContext(pContext);
2930
    CertCloseStore(hStore, 0);
2931
2932
    CURL_TRC_CF(data, cf,
2933
                "ossl_win_load_store() found: %zu imported: %zu certs in %s.",
2934
                total, imported, win_store);
2935
2936
    if(result)
2937
      return result;
2938
  }
2939
2940
  return result;
2941
}
2942
2943
static CURLcode ossl_windows_load_anchors(struct Curl_cfilter *cf,
2944
                                          struct Curl_easy *data,
2945
                                          X509_STORE *store,
2946
                                          bool *padded)
2947
{
2948
  /* Import certificates from the Windows root certificate store if
2949
     requested.
2950
     https://stackoverflow.com/questions/9507184/
2951
     https://github.com/d3x0r/SACK/blob/ff15424d3c581b86d40f818532e5a400c516d39d/src/netlib/ssl_layer.c#L1410
2952
     https://datatracker.ietf.org/doc/html/rfc5280 */
2953
  const char *win_stores[] = {
2954
    "ROOT",   /* Trusted Root Certification Authorities */
2955
    "CA"      /* Intermediate Certification Authorities */
2956
  };
2957
  size_t i;
2958
  CURLcode result = CURLE_OK;
2959
2960
  *padded = FALSE;
2961
  for(i = 0; i < CURL_ARRAYSIZE(win_stores); ++i) {
2962
    bool store_added = FALSE;
2963
    result = ossl_win_load_store(data, cf, win_stores[i], store, &store_added);
2964
    if(result)
2965
      return result;
2966
    if(store_added) {
2967
      CURL_TRC_CF(data, cf, "added trust anchors from Windows %s store",
2968
                  win_stores[i]);
2969
      *padded = TRUE;
2970
    }
2971
    else
2972
      infof(data, "error importing Windows %s store, continuing anyway",
2973
            win_stores[i]);
2974
  }
2975
  return result;
2976
}
2977
2978
#endif /* USE_WIN32_CRYPTO */
2979
2980
static CURLcode ossl_load_trust_anchors(struct Curl_cfilter *cf,
2981
                                        struct Curl_easy *data,
2982
                                        struct ossl_ctx *octx,
2983
                                        X509_STORE *store)
2984
0
{
2985
0
  struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
2986
0
  struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
2987
0
  CURLcode result = CURLE_OK;
2988
0
  const char * const ssl_cafile =
2989
    /* CURLOPT_CAINFO_BLOB overrides CURLOPT_CAINFO */
2990
0
    (conn_config->ca_info_blob ? NULL : conn_config->CAfile);
2991
0
  const char * const ssl_capath = conn_config->CApath;
2992
0
  bool have_native_check = FALSE;
2993
2994
0
  octx->store_is_empty = TRUE;
2995
0
  if(ssl_config->native_ca_store) {
2996
#ifdef USE_WIN32_CRYPTO
2997
    bool added = FALSE;
2998
    result = ossl_windows_load_anchors(cf, data, store, &added);
2999
    if(result)
3000
      return result;
3001
    if(added) {
3002
      infof(data, "  Native: Windows System Stores ROOT+CA");
3003
      octx->store_is_empty = FALSE;
3004
    }
3005
#elif defined(USE_APPLE_SECTRUST)
3006
    infof(data, "  Native: Apple SecTrust");
3007
    have_native_check = TRUE;
3008
#endif
3009
0
  }
3010
3011
0
  if(conn_config->ca_info_blob) {
3012
0
    result = load_cacert_from_memory(store, conn_config->ca_info_blob);
3013
0
    if(result) {
3014
0
      failf(data, "error adding trust anchors from certificate blob: %d",
3015
0
            result);
3016
0
      return result;
3017
0
    }
3018
0
    infof(data, "  CA Blob from configuration");
3019
0
    octx->store_is_empty = FALSE;
3020
0
  }
3021
3022
0
  if(ssl_cafile || ssl_capath) {
3023
0
#ifdef HAVE_OPENSSL3
3024
    /* OpenSSL 3.0.0 has deprecated SSL_CTX_load_verify_locations */
3025
0
    if(ssl_cafile) {
3026
0
      if(!X509_STORE_load_file(store, ssl_cafile)) {
3027
0
        if(octx->store_is_empty && !have_native_check) {
3028
          /* Fail if we insist on successfully verifying the server. */
3029
0
          failf(data, "error adding trust anchors from file: %s", ssl_cafile);
3030
0
          return CURLE_SSL_CACERT_BADFILE;
3031
0
        }
3032
0
        else
3033
0
          infof(data, "error setting certificate file, continuing anyway");
3034
0
      }
3035
0
      infof(data, "  CAfile: %s", ssl_cafile);
3036
0
      octx->store_is_empty = FALSE;
3037
0
    }
3038
0
    if(ssl_capath) {
3039
0
      if(!X509_STORE_load_path(store, ssl_capath)) {
3040
0
        if(octx->store_is_empty && !have_native_check) {
3041
          /* Fail if we insist on successfully verifying the server. */
3042
0
          failf(data, "error adding trust anchors from path: %s", ssl_capath);
3043
0
          return CURLE_SSL_CACERT_BADFILE;
3044
0
        }
3045
0
        else
3046
0
          infof(data, "error setting certificate path, continuing anyway");
3047
0
      }
3048
0
      infof(data, "  CApath: %s", ssl_capath);
3049
0
      octx->store_is_empty = FALSE;
3050
0
    }
3051
#else
3052
    /* tell OpenSSL where to find CA certificates that are used to verify the
3053
       server's certificate. */
3054
    if(!X509_STORE_load_locations(store, ssl_cafile, ssl_capath)) {
3055
      if(octx->store_is_empty && !have_native_check) {
3056
        /* Fail if we insist on successfully verifying the server. */
3057
        failf(data, "error adding trust anchors from locations:"
3058
              "  CAfile: %s CApath: %s",
3059
              ssl_cafile ? ssl_cafile : "none",
3060
              ssl_capath ? ssl_capath : "none");
3061
        return CURLE_SSL_CACERT_BADFILE;
3062
      }
3063
      else {
3064
        infof(data, "error setting certificate verify locations,"
3065
              " continuing anyway");
3066
      }
3067
    }
3068
    if(ssl_cafile)
3069
      infof(data, "  CAfile: %s", ssl_cafile);
3070
    if(ssl_capath)
3071
      infof(data, "  CApath: %s", ssl_capath);
3072
    octx->store_is_empty = FALSE;
3073
#endif
3074
0
  }
3075
3076
#ifdef CURL_CA_FALLBACK
3077
  if(octx->store_is_empty) {
3078
    /* verifying the peer without any CA certificates does not
3079
       work so use OpenSSL's built-in default as fallback */
3080
    X509_STORE_set_default_paths(store);
3081
    infof(data, "  OpenSSL default paths (fallback)");
3082
    octx->store_is_empty = FALSE;
3083
  }
3084
#endif
3085
0
  if(octx->store_is_empty && !have_native_check)
3086
0
    infof(data, "  no trust anchors configured");
3087
3088
0
  return result;
3089
0
}
3090
3091
static CURLcode ossl_populate_x509_store(struct Curl_cfilter *cf,
3092
                                         struct Curl_easy *data,
3093
                                         struct ossl_ctx *octx,
3094
                                         X509_STORE *store)
3095
0
{
3096
0
  struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
3097
0
  struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
3098
0
  CURLcode result = CURLE_OK;
3099
0
  X509_LOOKUP *lookup = NULL;
3100
0
  const char * const ssl_crlfile = ssl_config->primary.CRLfile;
3101
0
  unsigned long x509flags = 0;
3102
3103
0
  CURL_TRC_CF(data, cf, "configuring OpenSSL's x509 trust store");
3104
0
  if(!store)
3105
0
    return CURLE_OUT_OF_MEMORY;
3106
3107
0
  if(!conn_config->verifypeer) {
3108
0
    infof(data, "SSL Trust: peer verification disabled");
3109
0
    return CURLE_OK;
3110
0
  }
3111
3112
0
  infof(data, "SSL Trust Anchors:");
3113
0
  result = ossl_load_trust_anchors(cf, data, octx, store);
3114
0
  if(result)
3115
0
    return result;
3116
3117
  /* Does not make sense to load a CRL file without peer verification */
3118
0
  if(ssl_crlfile) {
3119
    /* tell OpenSSL where to find CRL file that is used to check certificate
3120
     * revocation */
3121
0
    lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
3122
0
    if(!lookup ||
3123
0
       (!X509_load_crl_file(lookup, ssl_crlfile, X509_FILETYPE_PEM))) {
3124
0
      failf(data, "error loading CRL file: %s", ssl_crlfile);
3125
0
      return CURLE_SSL_CRL_BADFILE;
3126
0
    }
3127
0
    x509flags = X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL;
3128
0
    infof(data, " CRLfile: %s", ssl_crlfile);
3129
0
  }
3130
3131
  /* Try building a chain using issuers in the trusted store first to avoid
3132
     problems with server-sent legacy intermediates. Newer versions of
3133
     OpenSSL do alternate chain checking by default but we do not know how to
3134
     determine that in a reliable manner.
3135
     https://web.archive.org/web/20190422050538/rt.openssl.org/Ticket/Display.html?id=3621
3136
  */
3137
0
  x509flags |= X509_V_FLAG_TRUSTED_FIRST;
3138
3139
0
  if(!ssl_config->no_partialchain && !ssl_crlfile) {
3140
    /* Have intermediate certificates in the trust store be treated as
3141
       trust-anchors, in the same way as self-signed root CA certificates are.
3142
       This allows users to verify servers using the intermediate cert only,
3143
       instead of needing the whole chain.
3144
3145
       Due to OpenSSL bug https://github.com/openssl/openssl/issues/5081 we
3146
       cannot do partial chains with a CRL check.
3147
    */
3148
0
    x509flags |= X509_V_FLAG_PARTIAL_CHAIN;
3149
0
  }
3150
0
  (void)X509_STORE_set_flags(store, x509flags);
3151
3152
0
  return result;
3153
0
}
3154
3155
/* key to use at `multi->proto_hash` */
3156
0
#define MPROTO_OSSL_X509_KEY  "tls:ossl:x509:share"
3157
3158
struct ossl_x509_share {
3159
  char *CAfile;         /* CAfile path used to generate X509 store */
3160
  X509_STORE *store;    /* cached X509 store or NULL if none */
3161
  struct curltime time; /* when the cached store was created */
3162
  BIT(store_is_empty);  /* no certs/paths/blobs are in the store */
3163
  BIT(no_partialchain); /* keep partial chain state */
3164
};
3165
3166
static void oss_x509_share_free(void *key, size_t key_len, void *p)
3167
0
{
3168
0
  struct ossl_x509_share *share = p;
3169
0
  DEBUGASSERT(key_len == (sizeof(MPROTO_OSSL_X509_KEY) - 1));
3170
0
  DEBUGASSERT(!memcmp(MPROTO_OSSL_X509_KEY, key, key_len));
3171
0
  (void)key;
3172
0
  (void)key_len;
3173
0
  if(share->store) {
3174
0
    X509_STORE_free(share->store);
3175
0
  }
3176
0
  curlx_free(share->CAfile);
3177
0
  curlx_free(share);
3178
0
}
3179
3180
static bool ossl_cached_x509_store_expired(struct Curl_easy *data,
3181
                                           const struct ossl_x509_share *mb)
3182
0
{
3183
0
  const struct ssl_general_config *cfg = &data->set.general_ssl;
3184
0
  if(cfg->ca_cache_timeout < 0)
3185
0
    return FALSE;
3186
0
  else {
3187
0
    timediff_t elapsed_ms = curlx_ptimediff_ms(Curl_pgrs_now(data), &mb->time);
3188
0
    timediff_t timeout_ms = cfg->ca_cache_timeout * (timediff_t)1000;
3189
3190
0
    return elapsed_ms >= timeout_ms;
3191
0
  }
3192
0
}
3193
3194
static bool ossl_cached_x509_store_different(struct Curl_cfilter *cf,
3195
                                             const struct Curl_easy *data,
3196
                                             const struct ossl_x509_share *mb)
3197
0
{
3198
0
  struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
3199
0
  struct ssl_config_data *ssl_config =
3200
0
    Curl_ssl_cf_get_config(cf, CURL_UNCONST(data));
3201
0
  if(mb->no_partialchain != ssl_config->no_partialchain)
3202
0
    return TRUE;
3203
0
  if(!mb->CAfile || !conn_config->CAfile)
3204
0
    return mb->CAfile != conn_config->CAfile;
3205
0
  return strcmp(mb->CAfile, conn_config->CAfile);
3206
0
}
3207
3208
static X509_STORE *ossl_get_cached_x509_store(struct Curl_cfilter *cf,
3209
                                              struct Curl_easy *data,
3210
                                              bool *pempty)
3211
0
{
3212
0
  struct Curl_multi *multi = data->multi;
3213
0
  struct ossl_x509_share *share;
3214
0
  X509_STORE *store = NULL;
3215
3216
0
  DEBUGASSERT(multi);
3217
0
  *pempty = TRUE;
3218
0
  share = multi ? Curl_hash_pick(&multi->proto_hash,
3219
0
                                 CURL_UNCONST(MPROTO_OSSL_X509_KEY),
3220
0
                                 sizeof(MPROTO_OSSL_X509_KEY) - 1) : NULL;
3221
0
  if(share && share->store &&
3222
0
     !ossl_cached_x509_store_expired(data, share) &&
3223
0
     !ossl_cached_x509_store_different(cf, data, share)) {
3224
0
    store = share->store;
3225
0
    *pempty = (bool)share->store_is_empty;
3226
0
  }
3227
3228
0
  return store;
3229
0
}
3230
3231
static void ossl_set_cached_x509_store(struct Curl_cfilter *cf,
3232
                                       struct Curl_easy *data,
3233
                                       X509_STORE *store,
3234
                                       bool is_empty)
3235
0
{
3236
0
  struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
3237
0
  struct Curl_multi *multi = data->multi;
3238
0
  struct ossl_x509_share *share;
3239
3240
0
  DEBUGASSERT(multi);
3241
0
  if(!multi)
3242
0
    return;
3243
0
  share = Curl_hash_pick(&multi->proto_hash,
3244
0
                         CURL_UNCONST(MPROTO_OSSL_X509_KEY),
3245
0
                         sizeof(MPROTO_OSSL_X509_KEY) - 1);
3246
3247
0
  if(!share) {
3248
0
    share = curlx_calloc(1, sizeof(*share));
3249
0
    if(!share)
3250
0
      return;
3251
0
    if(!Curl_hash_add2(&multi->proto_hash,
3252
0
                       CURL_UNCONST(MPROTO_OSSL_X509_KEY),
3253
0
                       sizeof(MPROTO_OSSL_X509_KEY) - 1,
3254
0
                       share, oss_x509_share_free)) {
3255
0
      curlx_free(share);
3256
0
      return;
3257
0
    }
3258
0
  }
3259
3260
0
  if(X509_STORE_up_ref(store)) {
3261
0
    char *CAfile = NULL;
3262
0
    struct ssl_config_data *ssl_config =
3263
0
      Curl_ssl_cf_get_config(cf, CURL_UNCONST(data));
3264
3265
0
    if(conn_config->CAfile) {
3266
0
      CAfile = curlx_strdup(conn_config->CAfile);
3267
0
      if(!CAfile) {
3268
0
        X509_STORE_free(store);
3269
0
        return;
3270
0
      }
3271
0
    }
3272
3273
0
    if(share->store) {
3274
0
      X509_STORE_free(share->store);
3275
0
      curlx_free(share->CAfile);
3276
0
    }
3277
3278
0
    share->time = *Curl_pgrs_now(data);
3279
0
    share->store = store;
3280
0
    share->store_is_empty = is_empty;
3281
0
    share->CAfile = CAfile;
3282
0
    share->no_partialchain = ssl_config->no_partialchain;
3283
0
  }
3284
0
}
3285
3286
CURLcode Curl_ssl_setup_x509_store(struct Curl_cfilter *cf,
3287
                                   struct Curl_easy *data,
3288
                                   struct ossl_ctx *octx)
3289
0
{
3290
0
  struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
3291
0
  struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
3292
0
  CURLcode result = CURLE_OK;
3293
0
  X509_STORE *cached_store;
3294
0
  bool cache_criteria_met, is_empty;
3295
3296
  /* Consider the X509 store cacheable if it comes exclusively from a CAfile,
3297
     or no source is provided and we are falling back to OpenSSL's built-in
3298
     default. */
3299
0
  cache_criteria_met = (data->set.general_ssl.ca_cache_timeout != 0) &&
3300
0
    conn_config->verifypeer &&
3301
0
    !conn_config->CApath &&
3302
0
    !conn_config->ca_info_blob &&
3303
0
    !ssl_config->primary.CRLfile &&
3304
0
    !ssl_config->native_ca_store;
3305
3306
0
  ERR_set_mark();
3307
3308
0
  cached_store = ossl_get_cached_x509_store(cf, data, &is_empty);
3309
0
  if(cached_store && cache_criteria_met && X509_STORE_up_ref(cached_store)) {
3310
0
    SSL_CTX_set_cert_store(octx->ssl_ctx, cached_store);
3311
0
    octx->store_is_empty = is_empty;
3312
0
  }
3313
0
  else {
3314
0
    X509_STORE *store = SSL_CTX_get_cert_store(octx->ssl_ctx);
3315
3316
0
    result = ossl_populate_x509_store(cf, data, octx, store);
3317
0
    if(result == CURLE_OK && cache_criteria_met) {
3318
0
      ossl_set_cached_x509_store(cf, data, store, (bool)octx->store_is_empty);
3319
0
    }
3320
0
  }
3321
3322
0
  ERR_pop_to_mark();
3323
3324
0
  return result;
3325
0
}
3326
3327
static CURLcode ossl_init_session_and_alpns(
3328
  struct ossl_ctx *octx,
3329
  struct Curl_cfilter *cf,
3330
  struct Curl_easy *data,
3331
  struct ssl_peer *peer,
3332
  const struct alpn_spec *alpns_requested,
3333
  Curl_ossl_init_session_reuse_cb *sess_reuse_cb)
3334
0
{
3335
0
  struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
3336
0
  struct ssl_primary_config *conn_cfg = Curl_ssl_cf_get_primary_config(cf);
3337
0
  struct alpn_spec alpns;
3338
0
  CURLcode result;
3339
3340
0
  Curl_alpn_copy(&alpns, alpns_requested);
3341
3342
0
  octx->reused_session = FALSE;
3343
0
  if(Curl_ssl_scache_use(cf, data) && !conn_cfg->verifystatus) {
3344
0
    struct Curl_ssl_session *scs = NULL;
3345
3346
0
    result = Curl_ssl_scache_take(cf, data, peer->scache_key, &scs);
3347
0
    if(!result && scs && scs->sdata && scs->sdata_len) {
3348
0
      const unsigned char *der_sessionid = scs->sdata;
3349
0
      size_t der_sessionid_size = scs->sdata_len;
3350
0
      SSL_SESSION *ssl_session = NULL;
3351
3352
      /* If OpenSSL does not accept the session from the cache, this
3353
       * is not an error. We continue without it. */
3354
0
      ssl_session = d2i_SSL_SESSION(NULL, &der_sessionid,
3355
0
                                    (long)der_sessionid_size);
3356
0
      if(ssl_session) {
3357
0
        if(!SSL_set_session(octx->ssl, ssl_session)) {
3358
0
          VERBOSE(char error_buffer[256]);
3359
0
          infof(data, "SSL: SSL_set_session not accepted, "
3360
0
                "continuing without: %s",
3361
0
                ossl_strerror(ERR_get_error(), error_buffer,
3362
0
                              sizeof(error_buffer)));
3363
0
        }
3364
0
        else {
3365
0
          if(conn_cfg->verifypeer &&
3366
0
             (SSL_get_verify_result(octx->ssl) != X509_V_OK)) {
3367
            /* Session was from unverified connection, cannot reuse here */
3368
0
            SSL_set_session(octx->ssl, NULL);
3369
0
            infof(data, "SSL session not peer verified, not reusing");
3370
0
          }
3371
0
          else {
3372
0
            infof(data, "SSL reusing session with ALPN '%s'",
3373
0
                  scs->alpn ? scs->alpn : "-");
3374
0
            octx->reused_session = TRUE;
3375
0
            infof(data, "SSL verify result: %lx",
3376
0
                  SSL_get_verify_result(octx->ssl));
3377
0
#ifdef HAVE_OPENSSL_EARLYDATA
3378
0
            if(ssl_config->earlydata && scs->alpn &&
3379
0
               SSL_SESSION_get_max_early_data(ssl_session) &&
3380
0
               !cf->conn->bits.connect_only &&
3381
0
               (SSL_version(octx->ssl) == TLS1_3_VERSION)) {
3382
0
              bool do_early_data = FALSE;
3383
0
              if(sess_reuse_cb) {
3384
0
                result = sess_reuse_cb(cf, data, &alpns, scs, &do_early_data);
3385
0
                if(result) {
3386
0
                  SSL_SESSION_free(ssl_session);
3387
0
                  return result;
3388
0
                }
3389
0
              }
3390
0
              if(do_early_data) {
3391
                /* We only try the ALPN protocol the session used before,
3392
                 * otherwise we might send early data for the wrong protocol */
3393
0
                Curl_alpn_restrict_to(&alpns, scs->alpn);
3394
0
              }
3395
0
            }
3396
#else
3397
            (void)ssl_config;
3398
            (void)sess_reuse_cb;
3399
#endif
3400
0
          }
3401
0
        }
3402
0
        SSL_SESSION_free(ssl_session);
3403
0
      }
3404
0
      else {
3405
0
        infof(data, "SSL session not accepted by OpenSSL, continuing without");
3406
0
      }
3407
0
    }
3408
0
    Curl_ssl_scache_return(cf, data, peer->scache_key, scs);
3409
0
  }
3410
3411
0
  if(alpns.count) {
3412
0
    struct alpn_proto_buf proto;
3413
0
    memset(&proto, 0, sizeof(proto));
3414
0
    result = Curl_alpn_to_proto_buf(&proto, &alpns);
3415
0
    if(result) {
3416
0
      failf(data, "Error determining ALPN");
3417
0
      return CURLE_SSL_CONNECT_ERROR;
3418
0
    }
3419
0
    if(SSL_set_alpn_protos(octx->ssl, proto.data, proto.len)) {
3420
0
      failf(data, "Error setting ALPN");
3421
0
      return CURLE_SSL_CONNECT_ERROR;
3422
0
    }
3423
0
  }
3424
3425
0
  return CURLE_OK;
3426
0
}
3427
3428
#ifdef HAVE_SSL_SET1_ECH_CONFIG_LIST
3429
bool Curl_ossl_need_httpsrr(struct Curl_easy *data)
3430
{
3431
  if(!CURLECH_ENABLED(data))
3432
    return FALSE;
3433
  if((data->set.tls_ech == CURLECH_GREASE) ||
3434
     data->set.str[STRING_ECH_CONFIG])
3435
    return FALSE;
3436
  return TRUE;
3437
}
3438
3439
static CURLcode ossl_init_ech(struct ossl_ctx *octx,
3440
                              struct Curl_cfilter *cf,
3441
                              struct Curl_easy *data,
3442
                              struct ssl_peer *peer)
3443
{
3444
  unsigned char *ech_config = NULL;
3445
  size_t ech_config_len = 0;
3446
  char *outername = data->set.str[STRING_ECH_PUBLIC];
3447
  int trying_ech_now = 0;
3448
  CURLcode result = CURLE_OK;
3449
3450
  if(!CURLECH_ENABLED(data))
3451
    return CURLE_OK;
3452
3453
  if(data->set.tls_ech == CURLECH_GREASE) {
3454
    infof(data, "ECH: will GREASE ClientHello");
3455
#ifdef HAVE_BORINGSSL_LIKE
3456
    SSL_set_enable_ech_grease(octx->ssl, 1);
3457
#else
3458
    SSL_set_options(octx->ssl, SSL_OP_ECH_GREASE);
3459
#endif
3460
  }
3461
  else if(data->set.tls_ech && data->set.str[STRING_ECH_CONFIG]) {
3462
#ifdef HAVE_BORINGSSL_LIKE
3463
    /* have to do base64 decode here for BoringSSL */
3464
    const char *b64 = data->set.str[STRING_ECH_CONFIG];
3465
3466
    if(!b64) {
3467
      infof(data, "ECH: ECHConfig from command line empty");
3468
      return CURLE_SSL_CONNECT_ERROR;
3469
    }
3470
    ech_config_len = 2 * strlen(b64);
3471
    result = curlx_base64_decode(b64, &ech_config, &ech_config_len);
3472
    if(result || !ech_config) {
3473
      infof(data, "ECH: cannot base64 decode ECHConfig from command line");
3474
      if(data->set.tls_ech == CURLECH_HARD)
3475
        return result;
3476
    }
3477
    if(SSL_set1_ech_config_list(octx->ssl, ech_config, ech_config_len) != 1) {
3478
      infof(data, "ECH: SSL_ECH_set1_ech_config_list failed");
3479
      if(data->set.tls_ech == CURLECH_HARD) {
3480
        curlx_free(ech_config);
3481
        return CURLE_SSL_CONNECT_ERROR;
3482
      }
3483
    }
3484
    curlx_free(ech_config);
3485
    trying_ech_now = 1;
3486
#else
3487
    ech_config = (unsigned char *)data->set.str[STRING_ECH_CONFIG];
3488
    if(!ech_config) {
3489
      infof(data, "ECH: ECHConfig from command line empty");
3490
      return CURLE_SSL_CONNECT_ERROR;
3491
    }
3492
    ech_config_len = strlen(data->set.str[STRING_ECH_CONFIG]);
3493
    if(SSL_set1_ech_config_list(octx->ssl, ech_config, ech_config_len) != 1) {
3494
      infof(data, "ECH: SSL_ECH_set1_ech_config_list failed");
3495
      if(data->set.tls_ech == CURLECH_HARD)
3496
        return CURLE_SSL_CONNECT_ERROR;
3497
    }
3498
    else
3499
      trying_ech_now = 1;
3500
#endif /* HAVE_BORINGSSL_LIKE */
3501
    infof(data, "ECH: ECHConfig from command line");
3502
  }
3503
  else {
3504
    const struct Curl_https_rrinfo *rinfo =
3505
      Curl_conn_dns_get_https(data, cf->sockindex);
3506
3507
    if(rinfo && rinfo->echconfiglist) {
3508
      const unsigned char *ecl = rinfo->echconfiglist;
3509
      size_t elen = rinfo->echconfiglist_len;
3510
3511
      infof(data, "ECH: ECHConfig from HTTPS RR");
3512
      if(SSL_set1_ech_config_list(octx->ssl, ecl, elen) != 1) {
3513
        infof(data, "ECH: SSL_set1_ech_config_list failed");
3514
        if(data->set.tls_ech == CURLECH_HARD)
3515
          return CURLE_SSL_CONNECT_ERROR;
3516
      }
3517
      else {
3518
        trying_ech_now = 1;
3519
        infof(data, "ECH: imported ECHConfigList of length %zu", elen);
3520
      }
3521
    }
3522
    else {
3523
      infof(data, "ECH: requested but no ECHConfig available");
3524
      if(data->set.tls_ech == CURLECH_HARD)
3525
        return CURLE_SSL_CONNECT_ERROR;
3526
    }
3527
  }
3528
#ifdef HAVE_BORINGSSL_LIKE
3529
  (void)peer;
3530
  if(trying_ech_now && outername) {
3531
    infof(data, "ECH: setting public_name not supported with BoringSSL");
3532
    return CURLE_SSL_CONNECT_ERROR;
3533
  }
3534
#else
3535
  if(trying_ech_now && outername) {
3536
    infof(data, "ECH: inner: '%s', outer: '%s'",
3537
          peer->dest->hostname ? peer->dest->hostname : "NULL", outername);
3538
    result = SSL_ech_set1_server_names(octx->ssl,
3539
                                       peer->dest->hostname, outername,
3540
                                       0 /* do send outer */);
3541
    if(result != 1) {
3542
      infof(data, "ECH: rv failed to set server name(s) %d [ERROR]", result);
3543
      return CURLE_SSL_CONNECT_ERROR;
3544
    }
3545
  }
3546
#endif /* HAVE_BORINGSSL_LIKE */
3547
  if(trying_ech_now &&
3548
     SSL_set_min_proto_version(octx->ssl, TLS1_3_VERSION) != 1) {
3549
    infof(data, "ECH: cannot force TLSv1.3 [ERROR]");
3550
    return CURLE_SSL_CONNECT_ERROR;
3551
  }
3552
3553
  return CURLE_OK;
3554
}
3555
#else /* HAVE_SSL_SET1_ECH_CONFIG_LIST */
3556
bool Curl_ossl_need_httpsrr(struct Curl_easy *data)
3557
0
{
3558
0
  (void)data;
3559
0
  return FALSE;
3560
0
}
3561
#endif /* else HAVE_SSL_SET1_ECH_CONFIG_LIST */
3562
3563
static CURLcode ossl_init_ssl(struct ossl_ctx *octx,
3564
                              struct Curl_cfilter *cf,
3565
                              struct Curl_easy *data,
3566
                              struct ssl_peer *peer,
3567
                              const struct alpn_spec *alpns_requested,
3568
                              void *ssl_user_data,
3569
                              Curl_ossl_init_session_reuse_cb *sess_reuse_cb)
3570
0
{
3571
  /* Let's make an SSL structure */
3572
0
  if(octx->ssl)
3573
0
    SSL_free(octx->ssl);
3574
0
  octx->ssl = SSL_new(octx->ssl_ctx);
3575
0
  if(!octx->ssl) {
3576
0
    failf(data, "SSL: could not create a context (handle)");
3577
0
    return CURLE_OUT_OF_MEMORY;
3578
0
  }
3579
3580
0
  SSL_set_app_data(octx->ssl, ssl_user_data);
3581
3582
0
#ifndef OPENSSL_NO_OCSP
3583
0
  if(Curl_ssl_cf_get_primary_config(cf)->verifystatus)
3584
0
    SSL_set_tlsext_status_type(octx->ssl, TLSEXT_STATUSTYPE_ocsp);
3585
0
#endif
3586
3587
0
  SSL_set_connect_state(octx->ssl);
3588
3589
0
  if(peer->sni) {
3590
0
    if(!SSL_set_tlsext_host_name(octx->ssl, peer->sni)) {
3591
0
      failf(data, "Failed set SNI");
3592
0
      return CURLE_SSL_CONNECT_ERROR;
3593
0
    }
3594
0
  }
3595
3596
#ifdef HAVE_SSL_SET1_ECH_CONFIG_LIST
3597
  {
3598
    CURLcode result = ossl_init_ech(octx, cf, data, peer);
3599
    if(result)
3600
      return result;
3601
  }
3602
#endif /* HAVE_SSL_SET1_ECH_CONFIG_LIST */
3603
3604
0
  return ossl_init_session_and_alpns(octx, cf, data, peer,
3605
0
                                     alpns_requested, sess_reuse_cb);
3606
0
}
3607
3608
static CURLcode ossl_init_method(struct Curl_cfilter *cf,
3609
                                 struct Curl_easy *data,
3610
                                 struct ssl_peer *peer,
3611
                                 const SSL_METHOD **pmethod,
3612
                                 unsigned int *pssl_version_min)
3613
0
{
3614
0
  struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
3615
3616
0
  *pmethod = NULL;
3617
0
  *pssl_version_min = conn_config->version;
3618
0
  DEBUGASSERT(conn_config->version != CURL_SSLVERSION_DEFAULT);
3619
0
  switch(peer->transport) {
3620
0
  case TRNSPRT_TCP:
3621
    /* check to see if we have been told to use an explicit SSL/TLS version */
3622
0
    switch(*pssl_version_min) {
3623
0
    case CURL_SSLVERSION_TLSv1:
3624
0
    case CURL_SSLVERSION_TLSv1_0:
3625
0
    case CURL_SSLVERSION_TLSv1_1:
3626
0
    case CURL_SSLVERSION_TLSv1_2:
3627
0
    case CURL_SSLVERSION_TLSv1_3:
3628
      /* it is handled later with the context options */
3629
0
      *pmethod = TLS_client_method();
3630
0
      break;
3631
0
    case CURL_SSLVERSION_SSLv2:
3632
0
      failf(data, "No SSLv2 support");
3633
0
      return CURLE_NOT_BUILT_IN;
3634
0
    case CURL_SSLVERSION_SSLv3:
3635
0
      failf(data, "No SSLv3 support");
3636
0
      return CURLE_NOT_BUILT_IN;
3637
0
    default:
3638
0
      failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
3639
0
      return CURLE_SSL_CONNECT_ERROR;
3640
0
    }
3641
0
    break;
3642
0
  case TRNSPRT_QUIC:
3643
0
    *pssl_version_min = CURL_SSLVERSION_TLSv1_3;
3644
0
    if(conn_config->version_max &&
3645
0
       (conn_config->version_max != CURL_SSLVERSION_MAX_DEFAULT) &&
3646
0
       (conn_config->version_max != CURL_SSLVERSION_MAX_TLSv1_3)) {
3647
0
      failf(data, "QUIC needs at least TLS version 1.3");
3648
0
      return CURLE_SSL_CONNECT_ERROR;
3649
0
    }
3650
3651
0
    *pmethod = TLS_method();
3652
0
    break;
3653
0
  default:
3654
0
    failf(data, "unsupported transport %d in SSL init", peer->transport);
3655
0
    return CURLE_SSL_CONNECT_ERROR;
3656
0
  }
3657
3658
0
  return *pmethod ? CURLE_OK : CURLE_SSL_CONNECT_ERROR;
3659
0
}
3660
3661
CURLcode Curl_ossl_ctx_init(struct ossl_ctx *octx,
3662
                            struct Curl_cfilter *cf,
3663
                            struct Curl_easy *data,
3664
                            struct ssl_peer *peer,
3665
                            const struct alpn_spec *alpns_requested,
3666
                            Curl_ossl_ctx_setup_cb *cb_setup,
3667
                            void *cb_user_data,
3668
                            Curl_ossl_new_session_cb *cb_new_session,
3669
                            void *ssl_user_data,
3670
                            Curl_ossl_init_session_reuse_cb *sess_reuse_cb)
3671
0
{
3672
0
  CURLcode result = CURLE_OK;
3673
0
  const char *ciphers;
3674
0
  const SSL_METHOD *req_method = NULL;
3675
0
  ctx_option_t ctx_options = 0;
3676
0
  struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
3677
0
  struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
3678
0
  char * const ssl_cert = ssl_config->primary.clientcert;
3679
0
  const struct curl_blob *ssl_cert_blob = ssl_config->primary.cert_blob;
3680
0
  const char * const ssl_cert_type = ssl_config->primary.cert_type;
3681
0
  unsigned int ssl_version_min;
3682
0
  char error_buffer[256];
3683
3684
  /* Make funny stuff to get random input */
3685
0
  result = ossl_seed(data);
3686
0
  if(result)
3687
0
    return result;
3688
3689
0
  ssl_config->certverifyresult = !X509_V_OK;
3690
3691
0
  result = ossl_init_method(cf, data, peer, &req_method, &ssl_version_min);
3692
0
  if(result)
3693
0
    return result;
3694
0
  DEBUGASSERT(req_method);
3695
3696
0
  DEBUGASSERT(!octx->ssl_ctx);
3697
0
  octx->ssl_ctx =
3698
0
#ifdef OPENSSL_HAS_PROVIDERS
3699
0
    data->state.libctx ?
3700
0
    SSL_CTX_new_ex(data->state.libctx, data->state.propq, req_method):
3701
0
#endif
3702
0
    SSL_CTX_new(req_method);
3703
3704
0
  if(!octx->ssl_ctx) {
3705
0
    failf(data, "SSL: could not create a context: %s",
3706
0
          ossl_strerror(ERR_peek_error(), error_buffer, sizeof(error_buffer)));
3707
0
    return CURLE_OUT_OF_MEMORY;
3708
0
  }
3709
3710
0
  if(cb_setup) {
3711
0
    result = cb_setup(cf, data, cb_user_data);
3712
0
    if(result)
3713
0
      return result;
3714
0
  }
3715
3716
0
  if(data->set.fdebug && data->set.verbose &&
3717
0
     (peer->transport != TRNSPRT_QUIC)) {
3718
    /* the SSL trace callback is only used for verbose logging;
3719
     * QUIC connections use a different TLS record format that
3720
     * ossl_trace cannot handle */
3721
0
    SSL_CTX_set_msg_callback(octx->ssl_ctx, ossl_trace);
3722
0
    SSL_CTX_set_msg_callback_arg(octx->ssl_ctx, cf);
3723
0
  }
3724
3725
  /* OpenSSL contains code to work around lots of bugs and flaws in various
3726
     SSL-implementations. SSL_CTX_set_options() is used to enabled those
3727
     work-arounds. The man page for this option states that SSL_OP_ALL enables
3728
     all the work-arounds and that "It is usually safe to use SSL_OP_ALL to
3729
     enable the bug workaround options if compatibility with somewhat broken
3730
     implementations is desired."
3731
3732
     The "-no_ticket" option was introduced in OpenSSL 0.9.8j. it is a flag to
3733
     disable "rfc4507bis session ticket support". rfc4507bis was later turned
3734
     into the proper RFC5077: https://datatracker.ietf.org/doc/html/rfc5077
3735
3736
     The enabled extension concerns the session management. I wonder how often
3737
     libcurl stops a connection and then resumes a TLS session. Also, sending
3738
     the session data is some overhead. I suggest that you use your proposed
3739
     patch (which explicitly disables TICKET).
3740
3741
     If someone writes an application with libcurl and OpenSSL who wants to
3742
     enable the feature, one can do this in the SSL callback.
3743
3744
     SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG option enabling allowed proper
3745
     interoperability with web server Netscape Enterprise Server 2.0.1 which
3746
     was released back in 1996.
3747
3748
     Due to CVE-2010-4180, option SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG has
3749
     become ineffective as of OpenSSL 0.9.8q and 1.0.0c. In order to mitigate
3750
     CVE-2010-4180 when using previous OpenSSL versions we no longer enable
3751
     this option regardless of OpenSSL version and SSL_OP_ALL definition.
3752
3753
     OpenSSL added a work-around for an SSL 3.0/TLS 1.0 CBC vulnerability:
3754
     https://web.archive.org/web/20240114184648/openssl.org/~bodo/tls-cbc.txt.
3755
     In 0.9.6e they added a bit to SSL_OP_ALL that _disables_ that work-around
3756
     despite the fact that SSL_OP_ALL is documented to do "rather harmless"
3757
     workarounds. In order to keep the secure work-around, the
3758
     SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS bit must not be set.
3759
  */
3760
3761
0
  ctx_options = SSL_OP_ALL | SSL_OP_NO_TICKET | SSL_OP_NO_COMPRESSION;
3762
3763
  /* mitigate CVE-2010-4180 */
3764
0
  ctx_options &= ~(ctx_option_t)SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG;
3765
3766
  /* unless the user explicitly asks to allow the protocol vulnerability we
3767
     use the work-around */
3768
0
  if(!ssl_config->enable_beast)
3769
0
    ctx_options &= ~(ctx_option_t)SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS;
3770
3771
0
  DEBUGASSERT(ssl_version_min != CURL_SSLVERSION_DEFAULT);
3772
0
  switch(ssl_version_min) {
3773
0
  case CURL_SSLVERSION_SSLv2:
3774
0
  case CURL_SSLVERSION_SSLv3:
3775
0
    return CURLE_NOT_BUILT_IN;
3776
3777
    /* "--tlsv<x.y>" options mean TLS >= version <x.y> */
3778
0
  case CURL_SSLVERSION_TLSv1:   /* TLS >= version 1.0 */
3779
0
  case CURL_SSLVERSION_TLSv1_0: /* TLS >= version 1.0 */
3780
0
  case CURL_SSLVERSION_TLSv1_1: /* TLS >= version 1.1 */
3781
0
  case CURL_SSLVERSION_TLSv1_2: /* TLS >= version 1.2 */
3782
0
  case CURL_SSLVERSION_TLSv1_3: /* TLS >= version 1.3 */
3783
    /* asking for any TLS version as the minimum, means no SSL versions
3784
       allowed */
3785
0
    ctx_options |= SSL_OP_NO_SSLv2;
3786
0
    ctx_options |= SSL_OP_NO_SSLv3;
3787
3788
0
    result = ossl_set_ssl_version_min_max(cf, octx->ssl_ctx, ssl_version_min);
3789
0
    if(result)
3790
0
      return result;
3791
0
    break;
3792
3793
0
  default:
3794
0
    failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
3795
0
    return CURLE_SSL_CONNECT_ERROR;
3796
0
  }
3797
3798
0
  SSL_CTX_set_options(octx->ssl_ctx, ctx_options);
3799
0
  SSL_CTX_set_read_ahead(octx->ssl_ctx, 1);
3800
3801
  /* Max TLS1.2 record size 0x4000 + 0x800.
3802
     OpenSSL supports processing "jumbo TLS record" (8 TLS records) in one go
3803
     for some algorithms, so match that here.
3804
     Experimentation shows that a slightly larger buffer is needed
3805
     to avoid short reads.
3806
3807
     However using a large buffer (8 packets) actually decreases performance.
3808
     4 packets is better.
3809
   */
3810
0
#ifdef HAVE_SSL_CTX_SET_DEFAULT_READ_BUFFER_LEN
3811
0
  SSL_CTX_set_default_read_buffer_len(octx->ssl_ctx, 0x401e * 4);
3812
0
#endif
3813
3814
  /* We do retry writes sometimes from another buffer address */
3815
0
  SSL_CTX_set_mode(octx->ssl_ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
3816
3817
0
  ciphers = conn_config->cipher_list;
3818
0
  if(!ciphers && (peer->transport != TRNSPRT_QUIC))
3819
0
    ciphers = NULL;
3820
0
  if(ciphers && (ssl_version_min < CURL_SSLVERSION_TLSv1_3)) {
3821
0
    if(!SSL_CTX_set_cipher_list(octx->ssl_ctx, ciphers)) {
3822
0
      failf(data, "failed setting cipher list: %s", ciphers);
3823
0
      return CURLE_SSL_CIPHER;
3824
0
    }
3825
0
    infof(data, "Cipher selection: %s", ciphers);
3826
0
  }
3827
3828
0
#ifdef HAVE_SSL_CTX_SET_CIPHERSUITES
3829
0
  {
3830
0
    const char *ciphers13 = conn_config->cipher_list13;
3831
0
    if(ciphers13 &&
3832
0
       (!conn_config->version_max ||
3833
0
        (conn_config->version_max == CURL_SSLVERSION_MAX_DEFAULT) ||
3834
0
        (conn_config->version_max >= CURL_SSLVERSION_MAX_TLSv1_3))) {
3835
0
      if(!SSL_CTX_set_ciphersuites(octx->ssl_ctx, ciphers13)) {
3836
0
        failf(data, "failed setting TLS 1.3 cipher suite: %s", ciphers13);
3837
0
        return CURLE_SSL_CIPHER;
3838
0
      }
3839
0
      infof(data, "TLS 1.3 cipher selection: %s", ciphers13);
3840
0
    }
3841
0
  }
3842
0
#endif
3843
3844
0
  if(ssl_cert || ssl_cert_blob || ssl_cert_type) {
3845
0
    result = client_cert(data, octx->ssl_ctx,
3846
0
                         ssl_cert, ssl_cert_blob, ssl_cert_type,
3847
0
                         ssl_config->primary.key, ssl_config->primary.key_blob,
3848
0
                         ssl_config->primary.key_type,
3849
0
                         ssl_config->primary.key_passwd);
3850
0
    if(result)
3851
      /* failf() is already done in client_cert() */
3852
0
      return result;
3853
0
  }
3854
3855
0
#ifdef HAVE_SSL_CTX_SET_POST_HANDSHAKE_AUTH
3856
  /* OpenSSL 1.1.1 requires clients to opt-in for PHA */
3857
0
  SSL_CTX_set_post_handshake_auth(octx->ssl_ctx, 1);
3858
0
#endif
3859
3860
0
  {
3861
0
    const char *curves = conn_config->curves;
3862
0
    if(curves) {
3863
#ifdef HAVE_BORINGSSL_LIKE
3864
#define OSSL_CURVE_CAST(x) (x)
3865
#else
3866
0
#define OSSL_CURVE_CAST(x) (char *)CURL_UNCONST(x)
3867
0
#endif
3868
0
      if(!SSL_CTX_set1_curves_list(octx->ssl_ctx, OSSL_CURVE_CAST(curves))) {
3869
0
        failf(data, "failed setting curves list: '%s'", curves);
3870
0
        return CURLE_SSL_CIPHER;
3871
0
      }
3872
0
    }
3873
0
  }
3874
3875
0
#ifdef HAVE_SSL_CTX_SET1_SIGALGS
3876
0
#define OSSL_SIGALG_CAST(x) OSSL_CURVE_CAST(x)
3877
0
  {
3878
0
    const char *signature_algorithms = conn_config->signature_algorithms;
3879
0
    if(signature_algorithms) {
3880
0
      if(!SSL_CTX_set1_sigalgs_list(octx->ssl_ctx,
3881
0
                                    OSSL_SIGALG_CAST(signature_algorithms))) {
3882
0
        failf(data, "failed setting signature algorithms: '%s'",
3883
0
              signature_algorithms);
3884
0
        return CURLE_SSL_CIPHER;
3885
0
      }
3886
0
    }
3887
0
  }
3888
0
#endif
3889
3890
0
#if defined(HAVE_OPENSSL_SRP) && defined(USE_TLS_SRP)
3891
0
  if(ssl_config->primary.username && Curl_auth_allowed_to_host(data)) {
3892
0
    char * const ssl_username = ssl_config->primary.username;
3893
0
    char * const ssl_password = ssl_config->primary.password;
3894
0
    infof(data, "Using TLS-SRP username: %s", ssl_username);
3895
3896
0
    if(!SSL_CTX_set_srp_username(octx->ssl_ctx, ssl_username)) {
3897
0
      failf(data, "Unable to set SRP username");
3898
0
      return CURLE_BAD_FUNCTION_ARGUMENT;
3899
0
    }
3900
0
    if(!SSL_CTX_set_srp_password(octx->ssl_ctx, ssl_password)) {
3901
0
      failf(data, "failed setting SRP password");
3902
0
      return CURLE_BAD_FUNCTION_ARGUMENT;
3903
0
    }
3904
0
    if(!conn_config->cipher_list) {
3905
0
      infof(data, "Setting cipher list SRP");
3906
3907
0
      if(!SSL_CTX_set_cipher_list(octx->ssl_ctx, "SRP")) {
3908
0
        failf(data, "failed setting SRP cipher list");
3909
0
        return CURLE_SSL_CIPHER;
3910
0
      }
3911
0
    }
3912
0
  }
3913
0
#endif /* HAVE_OPENSSL_SRP && USE_TLS_SRP */
3914
3915
  /* OpenSSL always tries to verify the peer. By setting the failure mode
3916
   * to NONE, we allow the connect to complete, regardless of the outcome.
3917
   * We then explicitly check the result and may try alternatives like
3918
   * Apple's SecTrust for verification. */
3919
0
  SSL_CTX_set_verify(octx->ssl_ctx, SSL_VERIFY_NONE, NULL);
3920
3921
  /* Enable logging of secrets to the file specified in env SSLKEYLOGFILE. */
3922
0
#if !defined(HAVE_KEYLOG_UPSTREAM) && defined(HAVE_KEYLOG_CALLBACK)
3923
0
  if(Curl_tls_keylog_enabled()) {
3924
0
    SSL_CTX_set_keylog_callback(octx->ssl_ctx, ossl_keylog_callback);
3925
0
  }
3926
0
#endif
3927
3928
0
  if(cb_new_session) {
3929
    /* Enable the session cache because it is a prerequisite for the
3930
     * "new session" callback. Use the "external storage" mode to prevent
3931
     * OpenSSL from creating an internal session cache.
3932
     */
3933
0
    SSL_CTX_set_session_cache_mode(octx->ssl_ctx,
3934
0
                                   SSL_SESS_CACHE_CLIENT |
3935
0
                                   SSL_SESS_CACHE_NO_INTERNAL);
3936
0
    SSL_CTX_sess_set_new_cb(octx->ssl_ctx, cb_new_session);
3937
0
  }
3938
3939
  /* give application a chance to interfere with SSL set up. */
3940
0
  if(data->set.ssl.fsslctx) {
3941
    /* When a user callback is installed to modify the SSL_CTX,
3942
     * we need to do the full initialization before calling it.
3943
     * See: #11800 */
3944
0
    if(!octx->x509_store_setup) {
3945
0
      result = Curl_ssl_setup_x509_store(cf, data, octx);
3946
0
      if(result)
3947
0
        return result;
3948
0
      octx->x509_store_setup = TRUE;
3949
0
    }
3950
0
    Curl_set_in_callback(data, TRUE);
3951
0
    result = (*data->set.ssl.fsslctx)(data, octx->ssl_ctx,
3952
0
                                      data->set.ssl.fsslctxp);
3953
0
    Curl_set_in_callback(data, FALSE);
3954
0
    if(result) {
3955
0
      failf(data, "error signaled by SSL ctx callback");
3956
0
      return result;
3957
0
    }
3958
0
  }
3959
3960
0
  return ossl_init_ssl(octx, cf, data, peer, alpns_requested,
3961
0
                       ssl_user_data, sess_reuse_cb);
3962
0
}
3963
3964
static CURLcode ossl_on_session_reuse(struct Curl_cfilter *cf,
3965
                                      struct Curl_easy *data,
3966
                                      struct alpn_spec *alpns,
3967
                                      struct Curl_ssl_session *scs,
3968
                                      bool *do_early_data)
3969
0
{
3970
0
  struct ssl_connect_data *connssl = cf->ctx;
3971
3972
0
  connssl->earlydata_max = scs->earlydata_max;
3973
3974
0
  return Curl_on_session_reuse(cf, data, alpns, scs, do_early_data,
3975
0
                               connssl->earlydata_max);
3976
0
}
3977
3978
void Curl_ossl_report_handshake(struct Curl_easy *data, struct ossl_ctx *octx)
3979
0
{
3980
0
#ifdef CURLVERBOSE
3981
0
  if(Curl_trc_is_verbose(data)) {
3982
0
    int psigtype_nid = NID_undef;
3983
0
    const char *negotiated_group_name = NULL;
3984
3985
0
#ifdef HAVE_OPENSSL3
3986
0
    SSL_get_peer_signature_type_nid(octx->ssl, &psigtype_nid);
3987
0
#if OPENSSL_VERSION_NUMBER >= 0x30200000L
3988
0
    negotiated_group_name = SSL_get0_group_name(octx->ssl);
3989
#else
3990
    negotiated_group_name =
3991
      OBJ_nid2sn(SSL_get_negotiated_group(octx->ssl) & 0x0000FFFF);
3992
#endif
3993
0
#endif
3994
3995
    /* Informational message */
3996
0
    infof(data, "SSL connection using %s / %s / %s / %s",
3997
0
          SSL_get_version(octx->ssl),
3998
0
          SSL_get_cipher(octx->ssl),
3999
0
          negotiated_group_name ? negotiated_group_name : "[blank]",
4000
0
          OBJ_nid2sn(psigtype_nid));
4001
0
  }
4002
#else
4003
  (void)data;
4004
  (void)octx;
4005
#endif /* CURLVERBOSE */
4006
0
}
4007
4008
static CURLcode ossl_connect_step1(struct Curl_cfilter *cf,
4009
                                   struct Curl_easy *data)
4010
0
{
4011
0
  struct ssl_connect_data *connssl = cf->ctx;
4012
0
  struct ossl_ctx *octx = (struct ossl_ctx *)connssl->backend;
4013
0
  char tls_id[80];
4014
0
  BIO *bio;
4015
0
  CURLcode result;
4016
4017
0
  DEBUGASSERT(ssl_connect_1 == connssl->connecting_state);
4018
0
  DEBUGASSERT(octx);
4019
4020
0
  if(!connssl->peer.dest) {
4021
0
    Curl_ossl_version(tls_id, sizeof(tls_id));
4022
0
    result = Curl_ssl_peer_init(&connssl->peer, cf, tls_id, TRNSPRT_TCP);
4023
0
    if(result)
4024
0
      return result;
4025
0
  }
4026
4027
0
  result = Curl_ossl_ctx_init(octx, cf, data, &connssl->peer,
4028
0
                              connssl->alpn, NULL, NULL,
4029
0
                              ossl_new_session_cb, cf,
4030
0
                              ossl_on_session_reuse);
4031
0
  if(result)
4032
0
    return result;
4033
4034
0
  octx->bio_method = ossl_bio_cf_method_create();
4035
0
  if(!octx->bio_method)
4036
0
    return CURLE_OUT_OF_MEMORY;
4037
0
  bio = BIO_new(octx->bio_method);
4038
0
  if(!bio)
4039
0
    return CURLE_OUT_OF_MEMORY;
4040
4041
0
  BIO_set_data(bio, cf);
4042
0
#ifdef HAVE_SSL_SET0_WBIO
4043
  /* with OpenSSL v1.1.1 we get an alternative to SSL_set_bio() that works
4044
   * without backward compat quirks. Every call takes one reference, so we
4045
   * up it and pass. SSL* then owns and frees it.
4046
   * We check on the function in configure, since LibreSSL and friends
4047
   * each have their own versions to add support for this. */
4048
0
  BIO_up_ref(bio);
4049
0
  SSL_set0_rbio(octx->ssl, bio);
4050
0
  SSL_set0_wbio(octx->ssl, bio);
4051
#else
4052
  SSL_set_bio(octx->ssl, bio, bio);
4053
#endif
4054
4055
0
  if(connssl->alpn && (connssl->state != ssl_connection_deferred)) {
4056
0
    struct alpn_proto_buf proto;
4057
0
    memset(&proto, 0, sizeof(proto));
4058
0
    Curl_alpn_to_proto_str(&proto, connssl->alpn);
4059
0
    infof(data, VTLS_INFOF_ALPN_OFFER_1STR, proto.data);
4060
0
  }
4061
4062
0
  connssl->connecting_state = ssl_connect_2;
4063
0
  return CURLE_OK;
4064
0
}
4065
4066
#ifdef HAVE_SSL_SET1_ECH_CONFIG_LIST
4067
/* If we have retry configs, then trace those out */
4068
static int ossl_trace_ech_retry_configs(struct Curl_easy *data, SSL *ssl,
4069
                                        int reason)
4070
{
4071
  CURLcode result = CURLE_OK;
4072
  size_t rcl = 0;
4073
  int rv = 1;
4074
#ifndef HAVE_BORINGSSL_LIKE
4075
  char *inner = NULL;
4076
  uint8_t *rcs = NULL;
4077
  char *outer = NULL;
4078
#else
4079
  const char *inner = NULL;
4080
  const uint8_t *rcs = NULL;
4081
  const char *outer = NULL;
4082
  size_t out_name_len = 0;
4083
  int servername_type = 0;
4084
#endif
4085
  NOVERBOSE((void)reason);
4086
4087
  /* nothing to trace if not doing ECH */
4088
  if(!CURLECH_ENABLED(data))
4089
    return rv;
4090
#ifndef HAVE_BORINGSSL_LIKE
4091
  rv = SSL_ech_get1_retry_config(ssl, &rcs, &rcl);
4092
#else
4093
  SSL_get0_ech_retry_configs(ssl, &rcs, &rcl);
4094
  rv = (int)rcl;
4095
#endif
4096
4097
  if(rv && rcs) {
4098
    char *b64str = NULL;
4099
    size_t blen = 0;
4100
4101
    result = curlx_base64_encode(rcs, rcl, &b64str, &blen);
4102
    if(!result && b64str) {
4103
      infof(data, "ECH: retry_configs %s", b64str);
4104
      curlx_free(b64str);
4105
#ifndef HAVE_BORINGSSL_LIKE
4106
      rv = SSL_ech_get1_status(ssl, &inner, &outer);
4107
      infof(data, "ECH: retry_configs for %s from %s, %d %d",
4108
            inner ? inner : "NULL", outer ? outer : "NULL", reason, rv);
4109
#else
4110
      rv = SSL_ech_accepted(ssl);
4111
      servername_type = SSL_get_servername_type(ssl);
4112
      inner = SSL_get_servername(ssl, servername_type);
4113
      SSL_get0_ech_name_override(ssl, &outer, &out_name_len);
4114
      infof(data, "ECH: retry_configs for %s from %s, %d %d",
4115
            inner ? inner : "NULL", outer ? outer : "NULL", reason, rv);
4116
#endif
4117
    }
4118
  }
4119
  else
4120
    infof(data, "ECH: no retry_configs (rv = %d)", rv);
4121
#ifndef HAVE_BORINGSSL_LIKE
4122
  OPENSSL_free(inner);
4123
  OPENSSL_free(rcs);
4124
  OPENSSL_free(outer);
4125
#endif
4126
  return rv;
4127
}
4128
4129
#endif
4130
4131
static CURLcode ossl_connect_step2(struct Curl_cfilter *cf,
4132
                                   struct Curl_easy *data)
4133
0
{
4134
0
  int err;
4135
0
  struct ssl_connect_data *connssl = cf->ctx;
4136
0
  struct ossl_ctx *octx = (struct ossl_ctx *)connssl->backend;
4137
0
  struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
4138
0
  DEBUGASSERT(ssl_connect_2 == connssl->connecting_state);
4139
0
  DEBUGASSERT(octx);
4140
4141
0
  connssl->io_need = CURL_SSL_IO_NEED_NONE;
4142
0
  ERR_clear_error();
4143
4144
0
  err = SSL_connect(octx->ssl);
4145
4146
0
  if(!octx->x509_store_setup) {
4147
    /* After having send off the ClientHello, we prepare the x509
4148
     * store to verify the coming certificate from the server */
4149
0
    CURLcode result = Curl_ssl_setup_x509_store(cf, data, octx);
4150
0
    if(result)
4151
0
      return result;
4152
0
    octx->x509_store_setup = TRUE;
4153
0
  }
4154
4155
#if !defined(HAVE_KEYLOG_UPSTREAM) && !defined(HAVE_KEYLOG_CALLBACK)
4156
  /* If key logging is enabled, wait for the handshake to complete and then
4157
   * proceed with logging secrets (for TLS 1.2 or older).
4158
   */
4159
  if(Curl_tls_keylog_enabled() && !octx->keylog_done)
4160
    ossl_log_tls12_secret(octx->ssl, &octx->keylog_done);
4161
#endif
4162
4163
  /* 1  is fine
4164
     0  is "not successful but was shut down controlled"
4165
     <0 is "handshake was not successful, because a fatal error occurred" */
4166
0
  if(err != 1) {
4167
0
    int detail = SSL_get_error(octx->ssl, err);
4168
0
    CURL_TRC_CF(data, cf, "SSL_connect() -> err=%d, detail=%d", err, detail);
4169
4170
0
    if(SSL_ERROR_WANT_READ == detail) {
4171
0
      CURL_TRC_CF(data, cf, "SSL_connect() -> want recv");
4172
0
      connssl->io_need = CURL_SSL_IO_NEED_RECV;
4173
0
      return CURLE_AGAIN;
4174
0
    }
4175
0
    if(SSL_ERROR_WANT_WRITE == detail) {
4176
0
      CURL_TRC_CF(data, cf, "SSL_connect() -> want send");
4177
0
      connssl->io_need = CURL_SSL_IO_NEED_SEND;
4178
0
      return CURLE_AGAIN;
4179
0
    }
4180
0
#ifdef SSL_ERROR_WANT_ASYNC
4181
0
    if(SSL_ERROR_WANT_ASYNC == detail) {
4182
0
      CURL_TRC_CF(data, cf, "SSL_connect() -> want async");
4183
0
      connssl->io_need = CURL_SSL_IO_NEED_RECV;
4184
0
      return CURLE_AGAIN;
4185
0
    }
4186
0
#endif
4187
0
#ifdef SSL_ERROR_WANT_RETRY_VERIFY
4188
0
    if(SSL_ERROR_WANT_RETRY_VERIFY == detail) {
4189
0
      CURL_TRC_CF(data, cf, "SSL_connect() -> want retry_verify");
4190
0
      Curl_xfer_pause_recv(data, TRUE);
4191
0
      return CURLE_AGAIN;
4192
0
    }
4193
0
#endif
4194
0
    else {
4195
      /* untreated error */
4196
0
      sslerr_t errdetail;
4197
0
      char error_buffer[256] = "";
4198
0
      CURLcode result;
4199
0
      long lerr;
4200
0
      int lib;
4201
0
      int reason;
4202
4203
      /* the connection failed, we are not waiting for anything else. */
4204
0
      connssl->connecting_state = ssl_connect_2;
4205
4206
      /* Get the earliest error code from the thread's error queue and remove
4207
         the entry. */
4208
0
      errdetail = ERR_get_error();
4209
4210
      /* Extract which lib and reason */
4211
0
      lib = ERR_GET_LIB(errdetail);
4212
0
      reason = ERR_GET_REASON(errdetail);
4213
4214
0
      if((lib == ERR_LIB_SSL) &&
4215
0
         ((reason == SSL_R_CERTIFICATE_VERIFY_FAILED)
4216
/* Missing from OpenSSL 4+ OPENSSL_NO_DEPRECATED_3_0 builds */
4217
0
#ifdef SSL_R_SSLV3_ALERT_CERTIFICATE_EXPIRED
4218
0
          || (reason == SSL_R_SSLV3_ALERT_CERTIFICATE_EXPIRED)
4219
0
#endif
4220
0
         )) {
4221
0
        result = CURLE_PEER_FAILED_VERIFICATION;
4222
4223
0
        lerr = SSL_get_verify_result(octx->ssl);
4224
0
        if(lerr != X509_V_OK) {
4225
0
          ssl_config->certverifyresult = lerr;
4226
0
          failf(data, "SSL certificate problem: %s",
4227
0
                X509_verify_cert_error_string(lerr));
4228
0
        }
4229
0
        else
4230
0
          failf(data, "%s", "SSL certificate verification failed");
4231
0
      }
4232
0
#ifdef SSL_R_TLSV13_ALERT_CERTIFICATE_REQUIRED
4233
      /* SSL_R_TLSV13_ALERT_CERTIFICATE_REQUIRED is only available on
4234
         OpenSSL version above v1.1.1, not AWS-LC, BoringSSL, or LibreSSL */
4235
0
      else if((lib == ERR_LIB_SSL) &&
4236
0
              (reason == SSL_R_TLSV13_ALERT_CERTIFICATE_REQUIRED)) {
4237
        /* If client certificate is required, communicate the
4238
           error to client */
4239
0
        result = CURLE_SSL_CLIENTCERT;
4240
0
        failf(data, "TLS cert problem: %s",
4241
0
              ossl_strerror(errdetail, error_buffer, sizeof(error_buffer)));
4242
0
      }
4243
0
#endif
4244
#ifdef HAVE_SSL_SET1_ECH_CONFIG_LIST
4245
      else if((lib == ERR_LIB_SSL) &&
4246
#ifndef HAVE_BORINGSSL_LIKE
4247
              (reason == SSL_R_ECH_REQUIRED)) {
4248
#else
4249
              (reason == SSL_R_ECH_REJECTED)) {
4250
#endif /* HAVE_BORINGSSL_LIKE */
4251
4252
        /* trace retry_configs if we got some */
4253
        ossl_trace_ech_retry_configs(data, octx->ssl, reason);
4254
4255
        result = CURLE_ECH_REQUIRED;
4256
        failf(data, "ECH required: %s",
4257
              ossl_strerror(errdetail, error_buffer, sizeof(error_buffer)));
4258
      }
4259
#endif
4260
0
      else {
4261
0
        result = CURLE_SSL_CONNECT_ERROR;
4262
0
        failf(data, "TLS connect error: %s",
4263
0
              ossl_strerror(errdetail, error_buffer, sizeof(error_buffer)));
4264
0
      }
4265
4266
      /* detail is already set to the SSL error above */
4267
4268
      /* If we e.g. use SSLv2 request-method and the server does not like us
4269
       * (RST connection, etc.), OpenSSL gives no explanation whatsoever and
4270
       * the SO_ERROR is also lost.
4271
       */
4272
0
      if(result == CURLE_SSL_CONNECT_ERROR && errdetail == 0) {
4273
0
        char extramsg[80] = "";
4274
0
        int sockerr = SOCKERRNO;
4275
4276
0
        if(sockerr && detail == SSL_ERROR_SYSCALL)
4277
0
          curlx_strerror(sockerr, extramsg, sizeof(extramsg));
4278
0
        failf(data, OSSL_PACKAGE " SSL_connect: %s in connection to %s:%d ",
4279
0
              extramsg[0] ? extramsg : SSL_ERROR_to_str(detail),
4280
0
              connssl->peer.dest->hostname, connssl->peer.dest->port);
4281
0
      }
4282
4283
0
      return result;
4284
0
    }
4285
0
  }
4286
0
  else {
4287
    /* we connected fine, we are not waiting for anything else. */
4288
0
    connssl->connecting_state = ssl_connect_3;
4289
0
    Curl_ossl_report_handshake(data, octx);
4290
4291
#if defined(HAVE_SSL_SET1_ECH_CONFIG_LIST) && !defined(HAVE_BORINGSSL_LIKE)
4292
    if(CURLECH_ENABLED(data)) {
4293
      char *inner = NULL, *outer = NULL;
4294
      int rv;
4295
      VERBOSE(const char *status);
4296
4297
      rv = SSL_ech_get1_status(octx->ssl, &inner, &outer);
4298
      switch(rv) {
4299
      case SSL_ECH_STATUS_SUCCESS:
4300
        VERBOSE(status = "succeeded");
4301
        break;
4302
      case SSL_ECH_STATUS_GREASE_ECH:
4303
        VERBOSE(status = "sent GREASE, got retry-configs");
4304
        break;
4305
      case SSL_ECH_STATUS_GREASE:
4306
        VERBOSE(status = "sent GREASE");
4307
        break;
4308
      case SSL_ECH_STATUS_NOT_TRIED:
4309
        VERBOSE(status = "not attempted");
4310
        break;
4311
      case SSL_ECH_STATUS_NOT_CONFIGURED:
4312
        VERBOSE(status = "not configured");
4313
        break;
4314
      case SSL_ECH_STATUS_BACKEND:
4315
        VERBOSE(status = "backend (unexpected)");
4316
        break;
4317
      case SSL_ECH_STATUS_FAILED:
4318
        VERBOSE(status = "failed");
4319
        break;
4320
      case SSL_ECH_STATUS_BAD_CALL:
4321
        VERBOSE(status = "bad call (unexpected)");
4322
        break;
4323
      case SSL_ECH_STATUS_BAD_NAME: {
4324
        struct ssl_primary_config *conn_config =
4325
          Curl_ssl_cf_get_primary_config(cf);
4326
        if(!conn_config->verifypeer && !conn_config->verifyhost &&
4327
           inner && !strcmp(inner, connssl->peer.dest->hostname)) {
4328
          VERBOSE(status = "bad name (tolerated without peer verification)");
4329
          rv = SSL_ECH_STATUS_SUCCESS;
4330
        }
4331
        else {
4332
          VERBOSE(status = "bad name (unexpected)");
4333
        }
4334
        break;
4335
      }
4336
      default:
4337
        VERBOSE(status = "unexpected status");
4338
        infof(data, "ECH: unexpected status %d", rv);
4339
      }
4340
      infof(data, "ECH: result: status is %s, inner is %s, outer is %s",
4341
            (status ? status : "NULL"),
4342
            (inner ? inner : "NULL"),
4343
            (outer ? outer : "NULL"));
4344
      OPENSSL_free(inner);
4345
      OPENSSL_free(outer);
4346
      if(rv == SSL_ECH_STATUS_GREASE_ECH) {
4347
        /* trace retry_configs if we got some */
4348
        ossl_trace_ech_retry_configs(data, octx->ssl, 0);
4349
      }
4350
      if(rv != SSL_ECH_STATUS_SUCCESS && (data->set.tls_ech == CURLECH_HARD)) {
4351
        infof(data, "ECH: ech-hard failed");
4352
        return CURLE_SSL_CONNECT_ERROR;
4353
      }
4354
    }
4355
    else {
4356
      infof(data, "ECH: result: status is not attempted");
4357
    }
4358
#endif /* HAVE_SSL_SET1_ECH_CONFIG_LIST && !HAVE_BORINGSSL_LIKE */
4359
4360
    /* Sets data and len to negotiated protocol, len is 0 if no protocol was
4361
     * negotiated
4362
     */
4363
0
    if(connssl->alpn) {
4364
0
      const unsigned char *neg_protocol;
4365
0
      unsigned int len;
4366
0
      SSL_get0_alpn_selected(octx->ssl, &neg_protocol, &len);
4367
4368
0
      return Curl_alpn_set_negotiated(cf, data, connssl, neg_protocol, len);
4369
0
    }
4370
4371
0
    return CURLE_OK;
4372
0
  }
4373
0
}
4374
4375
/*
4376
 * Heavily modified from:
4377
 * https://www.owasp.org/index.php/Certificate_and_Public_Key_Pinning#OpenSSL
4378
 */
4379
static CURLcode ossl_pkp_pin_peer_pubkey(struct Curl_easy *data, X509 *cert,
4380
                                         const char *pinnedpubkey)
4381
0
{
4382
  /* Scratch */
4383
0
  int len1 = 0, len2 = 0;
4384
0
  unsigned char *buff1 = NULL, *temp = NULL;
4385
4386
  /* Result is returned to caller */
4387
0
  CURLcode result = CURLE_SSL_PINNEDPUBKEYNOTMATCH;
4388
4389
  /* if a path was not specified, do not pin */
4390
0
  if(!pinnedpubkey)
4391
0
    return CURLE_OK;
4392
4393
0
  if(!cert)
4394
0
    return result;
4395
4396
0
  do {
4397
    /* Get the subjectPublicKeyInfo */
4398
    /* https://groups.google.com/group/mailing.openssl.users/browse_thread/thread/d61858dae102c6c7 */
4399
0
    len1 = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert), NULL);
4400
0
    if(len1 < 1)
4401
0
      break; /* failed */
4402
4403
0
    buff1 = temp = curlx_malloc(len1);
4404
0
    if(!buff1)
4405
0
      break; /* failed */
4406
4407
    /* https://docs.openssl.org/master/man3/d2i_X509/ */
4408
0
    len2 = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert), &temp);
4409
4410
    /*
4411
     * These checks are verifying we got back the same values as when we
4412
     * sized the buffer. it is pretty weak since they should always be the
4413
     * same, but it gives us something to test.
4414
     */
4415
0
    if((len1 != len2) || !temp || ((temp - buff1) != len1))
4416
0
      break; /* failed */
4417
4418
    /* End Gyrations */
4419
4420
    /* The one good exit point */
4421
0
    result = Curl_pin_peer_pubkey(data, pinnedpubkey, buff1, len1);
4422
0
  } while(0);
4423
4424
0
  if(buff1)
4425
0
    curlx_free(buff1);
4426
4427
0
  return result;
4428
0
}
4429
4430
#ifdef CURLVERBOSE
4431
#if !defined(HAVE_BORINGSSL_LIKE) && \
4432
  !(defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x3060000fL)
4433
static void infof_certstack(struct Curl_easy *data, const SSL *ssl)
4434
0
{
4435
0
  STACK_OF(X509) *certstack;
4436
0
  long verify_result;
4437
0
  int num_cert_levels;
4438
0
  int cert_level;
4439
4440
0
  if(!Curl_trc_is_verbose(data))
4441
0
    return;
4442
4443
0
  verify_result = SSL_get_verify_result(ssl);
4444
0
  if(verify_result != X509_V_OK)
4445
0
    certstack = SSL_get_peer_cert_chain(ssl);
4446
0
  else
4447
0
    certstack = SSL_get0_verified_chain(ssl);
4448
0
  if(!certstack)
4449
0
    return;
4450
0
  num_cert_levels = sk_X509_num(certstack);
4451
4452
0
  for(cert_level = 0; cert_level < num_cert_levels; cert_level++) {
4453
0
    char cert_algorithm[80] = "";
4454
0
    char group_name_final[80] = "";
4455
0
    const X509_ALGOR *palg_cert = NULL;
4456
0
    const ASN1_OBJECT *paobj_cert = NULL;
4457
0
    X509 *current_cert;
4458
0
    EVP_PKEY *current_pkey;
4459
0
    int key_bits;
4460
0
    int key_sec_bits;
4461
0
    int get_group_name;
4462
0
    const char *type_name;
4463
4464
0
    current_cert = sk_X509_value(certstack, cert_level);
4465
0
    if(!current_cert)
4466
0
      continue;
4467
4468
0
    current_pkey = X509_get0_pubkey(current_cert);
4469
0
    if(!current_pkey)
4470
0
      continue;
4471
4472
0
    X509_get0_signature(NULL, &palg_cert, current_cert);
4473
0
    X509_ALGOR_get0(&paobj_cert, NULL, NULL, palg_cert);
4474
0
    OBJ_obj2txt(cert_algorithm, sizeof(cert_algorithm), paobj_cert, 0);
4475
4476
0
    key_bits = EVP_PKEY_bits(current_pkey);
4477
#ifndef HAVE_OPENSSL3
4478
#define EVP_PKEY_get_security_bits EVP_PKEY_security_bits
4479
#endif
4480
0
    key_sec_bits = EVP_PKEY_get_security_bits(current_pkey);
4481
0
#ifdef HAVE_OPENSSL3
4482
0
    {
4483
0
      char group_name[80] = "";
4484
0
      get_group_name = EVP_PKEY_get_group_name(current_pkey, group_name,
4485
0
                                               sizeof(group_name), NULL);
4486
0
      curl_msnprintf(group_name_final, sizeof(group_name_final), "/%s",
4487
0
                     group_name);
4488
0
    }
4489
0
    type_name = EVP_PKEY_get0_type_name(current_pkey);
4490
#else
4491
    get_group_name = 0;
4492
    type_name = NULL;
4493
#endif
4494
4495
0
    infof(data, "  Certificate level %d: "
4496
0
          "Public key type %s%s (%d/%d Bits/secBits), signed using %s",
4497
0
          cert_level, type_name ? type_name : "?",
4498
0
          get_group_name == 0 ? "" : group_name_final,
4499
0
          key_bits, key_sec_bits, cert_algorithm);
4500
0
  }
4501
0
}
4502
#else
4503
#define infof_certstack(data, ssl)
4504
#endif
4505
#endif /* CURLVERBOSE */
4506
4507
static CURLcode ossl_check_issuer(struct Curl_cfilter *cf,
4508
                                  struct Curl_easy *data,
4509
                                  X509 *server_cert)
4510
0
{
4511
0
  struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
4512
0
  X509 *issuer = NULL;
4513
0
  BIO *fp = NULL;
4514
0
  char err_buf[256] = "";
4515
0
  bool verify_enabled = (conn_config->verifypeer || conn_config->verifyhost);
4516
0
  CURLcode result = CURLE_OK;
4517
4518
  /* e.g. match issuer name with provided issuer certificate */
4519
0
  if(conn_config->issuercert_blob) {
4520
0
    fp = BIO_new_mem_buf(conn_config->issuercert_blob->data,
4521
0
                         (int)conn_config->issuercert_blob->len);
4522
0
    if(!fp) {
4523
0
      failf(data, "BIO_new_mem_buf NULL, " OSSL_PACKAGE " error %s",
4524
0
            ossl_strerror(ERR_get_error(), err_buf, sizeof(err_buf)));
4525
0
      result = CURLE_OUT_OF_MEMORY;
4526
0
      goto out;
4527
0
    }
4528
0
  }
4529
0
  else if(conn_config->issuercert) {
4530
0
    fp = BIO_new(BIO_s_file());
4531
0
    if(!fp) {
4532
0
      failf(data, "BIO_new return NULL, " OSSL_PACKAGE " error %s",
4533
0
            ossl_strerror(ERR_get_error(), err_buf, sizeof(err_buf)));
4534
0
      result = CURLE_OUT_OF_MEMORY;
4535
0
      goto out;
4536
0
    }
4537
4538
0
    if(BIO_read_filename(fp, conn_config->issuercert) <= 0) {
4539
0
      if(verify_enabled)
4540
0
        failf(data, "SSL: Unable to open issuer cert (%s)",
4541
0
              conn_config->issuercert);
4542
0
      result = CURLE_SSL_ISSUER_ERROR;
4543
0
      goto out;
4544
0
    }
4545
0
  }
4546
4547
0
  if(fp) {
4548
0
    issuer = PEM_read_bio_X509(fp, NULL, ZERO_NULL, NULL);
4549
0
    if(!issuer) {
4550
0
      if(verify_enabled)
4551
0
        failf(data, "SSL: Unable to read issuer cert (%s)",
4552
0
              conn_config->issuercert);
4553
0
      result = CURLE_SSL_ISSUER_ERROR;
4554
0
      goto out;
4555
0
    }
4556
4557
0
    if(X509_check_issued(issuer, server_cert) != X509_V_OK) {
4558
0
      if(verify_enabled)
4559
0
        failf(data, "SSL: Certificate issuer check failed (%s)",
4560
0
              conn_config->issuercert);
4561
0
      result = CURLE_SSL_ISSUER_ERROR;
4562
0
      goto out;
4563
0
    }
4564
4565
0
    infof(data, " SSL certificate issuer check ok (%s)",
4566
0
          conn_config->issuercert);
4567
0
  }
4568
4569
0
out:
4570
0
  if(fp)
4571
0
    BIO_free(fp);
4572
0
  if(issuer)
4573
0
    X509_free(issuer);
4574
0
  return result;
4575
0
}
4576
4577
static CURLcode ossl_check_pinned_key(struct Curl_cfilter *cf,
4578
                                      struct Curl_easy *data,
4579
                                      X509 *server_cert)
4580
0
{
4581
0
  const char *ptr;
4582
0
  CURLcode result = CURLE_OK;
4583
4584
0
  (void)cf;
4585
0
#ifndef CURL_DISABLE_PROXY
4586
0
  ptr = Curl_ssl_cf_is_proxy(cf) ?
4587
0
    data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY] :
4588
0
    data->set.str[STRING_SSL_PINNEDPUBLICKEY];
4589
#else
4590
  ptr = data->set.str[STRING_SSL_PINNEDPUBLICKEY];
4591
#endif
4592
0
  if(ptr) {
4593
0
    result = ossl_pkp_pin_peer_pubkey(data, server_cert, ptr);
4594
0
    if(result)
4595
0
      failf(data, "SSL: public key does not match pinned public key");
4596
0
  }
4597
0
  return result;
4598
0
}
4599
4600
#ifdef CURLVERBOSE
4601
0
#define MAX_CERT_NAME_LENGTH 2048
4602
static CURLcode ossl_infof_cert(struct Curl_cfilter *cf,
4603
                                struct Curl_easy *data,
4604
                                X509 *server_cert)
4605
0
{
4606
0
  BIO *mem = NULL;
4607
0
  struct dynbuf dname;
4608
0
  char err_buf[256] = "";
4609
0
  char *buf;
4610
0
  long len;
4611
0
  CURLcode result = CURLE_OK;
4612
4613
0
  if(!Curl_trc_is_verbose(data))
4614
0
    return CURLE_OK;
4615
4616
0
  curlx_dyn_init(&dname, MAX_CERT_NAME_LENGTH);
4617
0
  mem = BIO_new(BIO_s_mem());
4618
0
  if(!mem) {
4619
0
    failf(data, "BIO_new return NULL, " OSSL_PACKAGE " error %s",
4620
0
          ossl_strerror(ERR_get_error(), err_buf, sizeof(err_buf)));
4621
0
    result = CURLE_OUT_OF_MEMORY;
4622
0
    goto out;
4623
0
  }
4624
4625
0
  infof(data, "%s certificate:", Curl_ssl_cf_is_proxy(cf) ?
4626
0
        "Proxy" : "Server");
4627
4628
0
  result = x509_name_oneline(X509_get_subject_name(server_cert), &dname);
4629
0
  infof(data, "  subject: %s", result ? "[NONE]" : curlx_dyn_ptr(&dname));
4630
4631
0
  ASN1_TIME_print(mem, X509_get0_notBefore(server_cert));
4632
0
  len = BIO_get_mem_data(mem, (char **)&buf);
4633
0
  infof(data, "  start date: %.*s", (int)len, buf);
4634
0
  (void)BIO_reset(mem);
4635
4636
0
  ASN1_TIME_print(mem, X509_get0_notAfter(server_cert));
4637
0
  len = BIO_get_mem_data(mem, (char **)&buf);
4638
0
  infof(data, "  expire date: %.*s", (int)len, buf);
4639
0
  (void)BIO_reset(mem);
4640
4641
0
  result = x509_name_oneline(X509_get_issuer_name(server_cert), &dname);
4642
0
  if(result) /* should be only fatal stuff like OOM */
4643
0
    goto out;
4644
0
  infof(data, "  issuer: %s", curlx_dyn_ptr(&dname));
4645
4646
0
out:
4647
0
  BIO_free(mem);
4648
0
  curlx_dyn_free(&dname);
4649
0
  return result;
4650
0
}
4651
#endif /* CURLVERBOSE */
4652
4653
#ifdef USE_APPLE_SECTRUST
4654
struct ossl_certs_ctx {
4655
  STACK_OF(X509) *sk;
4656
  size_t num_certs;
4657
};
4658
4659
static CURLcode ossl_chain_get_der(struct Curl_cfilter *cf,
4660
                                   struct Curl_easy *data,
4661
                                   void *user_data,
4662
                                   size_t i,
4663
                                   unsigned char **pder,
4664
                                   size_t *pder_len)
4665
{
4666
  struct ossl_certs_ctx *chain = user_data;
4667
  X509 *cert;
4668
  int der_len;
4669
4670
  (void)cf;
4671
  (void)data;
4672
  *pder_len = 0;
4673
  *pder = NULL;
4674
4675
  if(i >= chain->num_certs)
4676
    return CURLE_TOO_LARGE;
4677
  cert = sk_X509_value(chain->sk, (int)i);
4678
  if(!cert)
4679
    return CURLE_FAILED_INIT;
4680
  der_len = i2d_X509(cert, pder);
4681
  if(der_len < 0)
4682
    return CURLE_FAILED_INIT;
4683
  *pder_len = (size_t)der_len;
4684
  return CURLE_OK;
4685
}
4686
4687
static CURLcode ossl_apple_verify(struct Curl_cfilter *cf,
4688
                                  struct Curl_easy *data,
4689
                                  struct ossl_ctx *octx,
4690
                                  struct ssl_peer *peer,
4691
                                  bool *pverified)
4692
{
4693
  struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
4694
  struct ossl_certs_ctx chain;
4695
  CURLcode result;
4696
4697
  memset(&chain, 0, sizeof(chain));
4698
  chain.sk = SSL_get_peer_cert_chain(octx->ssl);
4699
  chain.num_certs = chain.sk ? sk_X509_num(chain.sk) : 0;
4700
4701
  if(!chain.num_certs &&
4702
     (conn_config->verifypeer || conn_config->verifyhost)) {
4703
    if(!octx->reused_session) {
4704
      failf(data, "SSL: could not get peer certificate chain");
4705
      result = CURLE_PEER_FAILED_VERIFICATION;
4706
    }
4707
    else {
4708
      /* when session was reused, there is no peer cert chain */
4709
      *pverified = FALSE;
4710
      return CURLE_OK;
4711
    }
4712
  }
4713
  else {
4714
#ifdef HAVE_BORINGSSL_LIKE
4715
    const uint8_t *ocsp_data = NULL;
4716
#else
4717
    unsigned char *ocsp_data = NULL;
4718
#endif
4719
    long ocsp_len = 0;
4720
    bool ocsp_missing = FALSE;
4721
    if(conn_config->verifystatus && !octx->reused_session)
4722
      ocsp_len = (long)SSL_get_tlsext_status_ocsp_resp(octx->ssl, &ocsp_data);
4723
4724
    /* SSL_get_tlsext_status_ocsp_resp() returns the length of the OCSP
4725
       response data or -1 if there is no OCSP response data. */
4726
    if(ocsp_len < 0) {
4727
      ocsp_len = 0; /* no data available */
4728
      ocsp_missing = TRUE;
4729
    }
4730
    result = Curl_vtls_apple_verify(cf, data, peer, chain.num_certs,
4731
                                    ossl_chain_get_der, &chain,
4732
                                    ocsp_data, ocsp_len);
4733
    if(!result && ocsp_missing && conn_config->verifystatus &&
4734
       !octx->reused_session) {
4735
      /* verified, but OCSP stapling is required and server sent none */
4736
      *pverified = TRUE;
4737
      failf(data, "No OCSP response received");
4738
      return CURLE_SSL_INVALIDCERTSTATUS;
4739
    }
4740
  }
4741
  *pverified = !result;
4742
  return result;
4743
}
4744
#endif /* USE_APPLE_SECTRUST */
4745
4746
CURLcode Curl_ossl_check_peer_cert(struct Curl_cfilter *cf,
4747
                                   struct Curl_easy *data,
4748
                                   struct ossl_ctx *octx,
4749
                                   struct ssl_peer *peer)
4750
0
{
4751
0
  struct connectdata *conn = cf->conn;
4752
0
  struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
4753
0
  struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
4754
0
  CURLcode result = CURLE_OK;
4755
0
  long ossl_verify;
4756
0
  X509 *server_cert;
4757
0
  bool verified = FALSE;
4758
#if !defined(OPENSSL_NO_OCSP) && defined(USE_APPLE_SECTRUST)
4759
  bool sectrust_verified = FALSE;
4760
#endif
4761
4762
0
  if(data->set.ssl.certinfo && !octx->reused_session) {
4763
    /* asked to gather certificate info. Reused sessions do not have cert
4764
       chains */
4765
0
    result = ossl_certchain(data, octx->ssl);
4766
0
    if(result)
4767
0
      return result;
4768
0
  }
4769
4770
0
  server_cert = SSL_get1_peer_certificate(octx->ssl);
4771
0
  if(!server_cert) {
4772
    /* no verification at all, this maybe acceptable */
4773
0
    if(!(conn_config->verifypeer || conn_config->verifyhost))
4774
0
      goto out;
4775
4776
0
    failf(data, "SSL: could not get peer certificate");
4777
0
    result = CURLE_PEER_FAILED_VERIFICATION;
4778
0
    goto out;
4779
0
  }
4780
4781
0
#ifdef CURLVERBOSE
4782
0
  result = ossl_infof_cert(cf, data, server_cert);
4783
0
  if(result)
4784
0
    goto out;
4785
0
  infof_certstack(data, octx->ssl);
4786
0
#endif
4787
4788
0
  if(conn_config->verifyhost) {
4789
0
    result = ossl_verifyhost(data, conn, peer, server_cert);
4790
0
    if(result)
4791
0
      goto out;
4792
0
  }
4793
  /* `verifyhost` is either OK or not requested from here on */
4794
4795
0
  ossl_verify = SSL_get_verify_result(octx->ssl);
4796
0
  ssl_config->certverifyresult = ossl_verify;
4797
0
  infof(data, "OpenSSL verify result: %lx", ossl_verify);
4798
4799
0
  verified = (ossl_verify == X509_V_OK);
4800
0
  if(verified)
4801
0
    infof(data, "SSL certificate verified via OpenSSL.");
4802
4803
#ifdef USE_APPLE_SECTRUST
4804
  if(!verified && conn_config->verifypeer && ssl_config->native_ca_store) {
4805
    /* we verify using Apple SecTrust *unless* OpenSSL already verified.
4806
     * This may happen if the application intercepted the OpenSSL callback
4807
     * and installed its own. */
4808
    result = ossl_apple_verify(cf, data, octx, peer, &verified);
4809
    if(result && (result != CURLE_PEER_FAILED_VERIFICATION))
4810
      goto out; /* unexpected error */
4811
    if(verified) {
4812
      infof(data, "SSL certificate verified via Apple SecTrust.");
4813
      ssl_config->certverifyresult = X509_V_OK;
4814
#ifndef OPENSSL_NO_OCSP
4815
      sectrust_verified = TRUE;
4816
#endif
4817
    }
4818
  }
4819
#endif
4820
4821
0
  if(!verified) {
4822
    /* no trust established, report the OpenSSL status */
4823
0
    if(conn_config->verifypeer) {
4824
0
      failf(data, "SSL certificate OpenSSL verify result: %s (%ld)",
4825
0
            X509_verify_cert_error_string(ossl_verify), ossl_verify);
4826
0
      result = CURLE_PEER_FAILED_VERIFICATION;
4827
0
      goto out;
4828
0
    }
4829
0
    infof(data, " SSL certificate verification failed, continuing anyway!");
4830
0
  }
4831
4832
0
#ifndef OPENSSL_NO_OCSP
4833
0
  if(conn_config->verifystatus &&
4834
#ifdef USE_APPLE_SECTRUST
4835
     !sectrust_verified && /* already verified via apple sectrust, cannot
4836
                            * verifystate via OpenSSL in that case as it
4837
                            * does not have the trust anchors */
4838
#endif
4839
0
     !octx->reused_session) {
4840
    /* do not do this after Session ID reuse */
4841
0
    result = verifystatus(cf, data, octx);
4842
0
    if(result)
4843
0
      goto out;
4844
0
  }
4845
0
#endif
4846
4847
0
  result = ossl_check_issuer(cf, data, server_cert);
4848
0
  if(result)
4849
0
    goto out;
4850
4851
0
  result = ossl_check_pinned_key(cf, data, server_cert);
4852
4853
0
out:
4854
0
  X509_free(server_cert);
4855
0
  return result;
4856
0
}
4857
4858
static CURLcode ossl_connect_step3(struct Curl_cfilter *cf,
4859
                                   struct Curl_easy *data)
4860
0
{
4861
0
  CURLcode result = CURLE_OK;
4862
0
  struct ssl_connect_data *connssl = cf->ctx;
4863
0
  struct ossl_ctx *octx = (struct ossl_ctx *)connssl->backend;
4864
4865
0
  DEBUGASSERT(ssl_connect_3 == connssl->connecting_state);
4866
4867
  /*
4868
   * We check certificates to authenticate the server; otherwise we risk
4869
   * man-in-the-middle attack; NEVERTHELESS, if we are told explicitly not to
4870
   * verify the peer, ignore faults and failures from the server cert
4871
   * operations.
4872
   */
4873
4874
0
  result = Curl_ossl_check_peer_cert(cf, data, octx, &connssl->peer);
4875
0
  if(result)
4876
    /* on error, remove sessions we might have in the pool */
4877
0
    Curl_ssl_scache_remove_all(cf, data, connssl->peer.scache_key);
4878
4879
0
  return result;
4880
0
}
4881
4882
#ifdef HAVE_OPENSSL_EARLYDATA
4883
static CURLcode ossl_send_earlydata(struct Curl_cfilter *cf,
4884
                                    struct Curl_easy *data)
4885
0
{
4886
0
  struct ssl_connect_data *connssl = cf->ctx;
4887
0
  struct ossl_ctx *octx = (struct ossl_ctx *)connssl->backend;
4888
0
  CURLcode result = CURLE_OK;
4889
0
  const unsigned char *buf;
4890
0
  size_t blen, nwritten;
4891
0
  int rc;
4892
4893
0
  DEBUGASSERT(connssl->earlydata_state == ssl_earlydata_sending);
4894
0
  octx->io_result = CURLE_OK;
4895
0
  while(Curl_bufq_peek(&connssl->earlydata, &buf, &blen)) {
4896
0
    nwritten = 0;
4897
0
    rc = SSL_write_early_data(octx->ssl, buf, blen, &nwritten);
4898
0
    CURL_TRC_CF(data, cf, "SSL_write_early_data(len=%zu) -> %d, %zu",
4899
0
                blen, rc, nwritten);
4900
0
    if(rc <= 0) {
4901
0
      long sslerror;
4902
0
      char error_buffer[256];
4903
0
      int err = SSL_get_error(octx->ssl, rc);
4904
4905
0
      switch(err) {
4906
0
      case SSL_ERROR_WANT_READ:
4907
0
        connssl->io_need = CURL_SSL_IO_NEED_RECV;
4908
0
        result = CURLE_AGAIN;
4909
0
        goto out;
4910
0
      case SSL_ERROR_WANT_WRITE:
4911
0
        connssl->io_need = CURL_SSL_IO_NEED_SEND;
4912
0
        result = CURLE_AGAIN;
4913
0
        goto out;
4914
0
      case SSL_ERROR_SYSCALL: {
4915
0
        int sockerr = SOCKERRNO;
4916
4917
0
        if(octx->io_result == CURLE_AGAIN) {
4918
0
          result = CURLE_AGAIN;
4919
0
          goto out;
4920
0
        }
4921
0
        sslerror = ERR_get_error();
4922
0
        if(sslerror)
4923
0
          ossl_strerror(sslerror, error_buffer, sizeof(error_buffer));
4924
0
        else if(sockerr)
4925
0
          curlx_strerror(sockerr, error_buffer, sizeof(error_buffer));
4926
0
        else
4927
0
          curl_msnprintf(error_buffer, sizeof(error_buffer), "%s",
4928
0
                         SSL_ERROR_to_str(err));
4929
4930
0
        failf(data, OSSL_PACKAGE " SSL_write:early_data: %s, errno %d",
4931
0
              error_buffer, sockerr);
4932
0
        result = CURLE_SEND_ERROR;
4933
0
        goto out;
4934
0
      }
4935
0
      case SSL_ERROR_SSL: {
4936
        /*  A failure in the SSL library occurred, usually a protocol error.
4937
            The OpenSSL error queue contains more information on the error. */
4938
0
        sslerror = ERR_get_error();
4939
0
        failf(data, "SSL_write_early_data() error: %s",
4940
0
              ossl_strerror(sslerror, error_buffer, sizeof(error_buffer)));
4941
0
        result = CURLE_SEND_ERROR;
4942
0
        goto out;
4943
0
      }
4944
0
      default:
4945
        /* a true error */
4946
0
        failf(data, OSSL_PACKAGE " SSL_write_early_data: %s, errno %d",
4947
0
              SSL_ERROR_to_str(err), SOCKERRNO);
4948
0
        result = CURLE_SEND_ERROR;
4949
0
        goto out;
4950
0
      }
4951
0
    }
4952
0
    Curl_bufq_skip(&connssl->earlydata, nwritten);
4953
0
  }
4954
  /* sent everything there was */
4955
0
  infof(data, "SSL sending %zu bytes of early data", connssl->earlydata_skip);
4956
0
out:
4957
0
  return result;
4958
0
}
4959
#endif /* HAVE_OPENSSL_EARLYDATA */
4960
4961
static CURLcode ossl_connect(struct Curl_cfilter *cf,
4962
                             struct Curl_easy *data,
4963
                             bool *done)
4964
0
{
4965
0
  CURLcode result = CURLE_OK;
4966
0
  struct ssl_connect_data *connssl = cf->ctx;
4967
4968
  /* check if the connection has already been established */
4969
0
  if(ssl_connection_complete == connssl->state) {
4970
0
    *done = TRUE;
4971
0
    return CURLE_OK;
4972
0
  }
4973
4974
0
  *done = FALSE;
4975
0
  connssl->io_need = CURL_SSL_IO_NEED_NONE;
4976
4977
0
  if(ssl_connect_1 == connssl->connecting_state) {
4978
0
    if(Curl_ossl_need_httpsrr(data) &&
4979
0
       !Curl_conn_dns_resolved_https(data, cf->sockindex)) {
4980
0
      CURL_TRC_CF(data, cf, "need HTTPS-RR, delaying connect");
4981
0
      return CURLE_OK;
4982
0
    }
4983
0
    CURL_TRC_CF(data, cf, "ossl_connect, step1");
4984
0
    result = ossl_connect_step1(cf, data);
4985
0
    if(result)
4986
0
      goto out;
4987
0
  }
4988
4989
0
  if(ssl_connect_2 == connssl->connecting_state) {
4990
0
    CURL_TRC_CF(data, cf, "ossl_connect, step2");
4991
0
#ifdef HAVE_OPENSSL_EARLYDATA
4992
0
    if(connssl->earlydata_state == ssl_earlydata_await) {
4993
0
      goto out;
4994
0
    }
4995
0
    else if(connssl->earlydata_state == ssl_earlydata_sending) {
4996
0
      result = ossl_send_earlydata(cf, data);
4997
0
      if(result)
4998
0
        goto out;
4999
0
      connssl->earlydata_state = ssl_earlydata_sent;
5000
0
    }
5001
0
#endif
5002
0
    DEBUGASSERT((connssl->earlydata_state == ssl_earlydata_none) ||
5003
0
                (connssl->earlydata_state == ssl_earlydata_sent));
5004
5005
0
    result = ossl_connect_step2(cf, data);
5006
0
    if(result)
5007
0
      goto out;
5008
0
  }
5009
5010
0
  if(ssl_connect_3 == connssl->connecting_state) {
5011
0
    CURL_TRC_CF(data, cf, "ossl_connect, step3");
5012
0
    result = ossl_connect_step3(cf, data);
5013
0
    if(result)
5014
0
      goto out;
5015
0
    connssl->connecting_state = ssl_connect_done;
5016
0
#ifdef HAVE_OPENSSL_EARLYDATA
5017
0
    if(connssl->earlydata_state > ssl_earlydata_none) {
5018
0
      struct ossl_ctx *octx = (struct ossl_ctx *)connssl->backend;
5019
      /* We should be in this state by now */
5020
0
      DEBUGASSERT(connssl->earlydata_state == ssl_earlydata_sent);
5021
0
      connssl->earlydata_state =
5022
0
        (SSL_get_early_data_status(octx->ssl) == SSL_EARLY_DATA_ACCEPTED) ?
5023
0
        ssl_earlydata_accepted : ssl_earlydata_rejected;
5024
0
    }
5025
0
#endif
5026
0
  }
5027
5028
0
  if(ssl_connect_done == connssl->connecting_state) {
5029
0
    CURL_TRC_CF(data, cf, "ossl_connect, done");
5030
0
    connssl->state = ssl_connection_complete;
5031
0
  }
5032
5033
0
out:
5034
0
  if(result == CURLE_AGAIN) {
5035
0
    *done = FALSE;
5036
0
    return CURLE_OK;
5037
0
  }
5038
0
  *done = ((connssl->state == ssl_connection_complete) ||
5039
0
           (connssl->state == ssl_connection_deferred));
5040
0
  return result;
5041
0
}
5042
5043
static bool ossl_data_pending(struct Curl_cfilter *cf,
5044
                              const struct Curl_easy *data)
5045
0
{
5046
0
  struct ssl_connect_data *connssl = cf->ctx;
5047
0
  (void)data;
5048
0
  return (bool)connssl->input_pending;
5049
0
}
5050
5051
static CURLcode ossl_send(struct Curl_cfilter *cf,
5052
                          struct Curl_easy *data,
5053
                          const void *mem,
5054
                          size_t len,
5055
                          size_t *pnwritten)
5056
0
{
5057
  /* SSL_write() is said to return 'int' while write() and send() returns
5058
     'size_t' */
5059
0
  int err;
5060
0
  char error_buffer[256];
5061
0
  sslerr_t sslerror;
5062
0
  int memlen;
5063
0
  struct ssl_connect_data *connssl = cf->ctx;
5064
0
  struct ossl_ctx *octx = (struct ossl_ctx *)connssl->backend;
5065
0
  CURLcode result = CURLE_OK;
5066
0
  int nwritten;
5067
5068
0
  DEBUGASSERT(octx);
5069
0
  *pnwritten = 0;
5070
0
  ERR_clear_error();
5071
5072
0
  connssl->io_need = CURL_SSL_IO_NEED_NONE;
5073
0
  memlen = (len > (size_t)INT_MAX) ? INT_MAX : (int)len;
5074
0
  if(octx->blocked_ssl_write_len && (octx->blocked_ssl_write_len != memlen)) {
5075
    /* The previous SSL_write() call was blocked, using that length.
5076
     * We need to use that again or OpenSSL freaks out. A shorter
5077
     * length should not happen and is a bug in libcurl. */
5078
0
    if(octx->blocked_ssl_write_len > memlen) {
5079
0
      DEBUGASSERT(0);
5080
0
      return CURLE_BAD_FUNCTION_ARGUMENT;
5081
0
    }
5082
0
    memlen = octx->blocked_ssl_write_len;
5083
0
  }
5084
0
  octx->blocked_ssl_write_len = 0;
5085
0
  nwritten = SSL_write(octx->ssl, mem, memlen);
5086
5087
0
  if(nwritten > 0)
5088
0
    *pnwritten = (size_t)nwritten;
5089
0
  else {
5090
0
    err = SSL_get_error(octx->ssl, nwritten);
5091
5092
0
    switch(err) {
5093
0
    case SSL_ERROR_WANT_READ:
5094
0
      connssl->io_need = CURL_SSL_IO_NEED_RECV;
5095
0
      octx->blocked_ssl_write_len = memlen;
5096
0
      result = CURLE_AGAIN;
5097
0
      goto out;
5098
0
    case SSL_ERROR_WANT_WRITE:
5099
0
      result = CURLE_AGAIN;
5100
0
      octx->blocked_ssl_write_len = memlen;
5101
0
      goto out;
5102
0
    case SSL_ERROR_SYSCALL: {
5103
0
      int sockerr = SOCKERRNO;
5104
5105
0
      if(octx->io_result == CURLE_AGAIN) {
5106
0
        octx->blocked_ssl_write_len = memlen;
5107
0
        result = CURLE_AGAIN;
5108
0
        goto out;
5109
0
      }
5110
0
      sslerror = ERR_get_error();
5111
0
      if(sslerror)
5112
0
        ossl_strerror(sslerror, error_buffer, sizeof(error_buffer));
5113
0
      else if(sockerr)
5114
0
        curlx_strerror(sockerr, error_buffer, sizeof(error_buffer));
5115
0
      else
5116
0
        curl_msnprintf(error_buffer, sizeof(error_buffer), "%s",
5117
0
                       SSL_ERROR_to_str(err));
5118
5119
0
      failf(data, OSSL_PACKAGE " SSL_write: %s, errno %d",
5120
0
            error_buffer, sockerr);
5121
0
      result = CURLE_SEND_ERROR;
5122
0
      goto out;
5123
0
    }
5124
0
    case SSL_ERROR_SSL: {
5125
      /*  A failure in the SSL library occurred, usually a protocol error.
5126
          The OpenSSL error queue contains more information on the error. */
5127
0
      sslerror = ERR_get_error();
5128
0
      failf(data, "SSL_write() error: %s",
5129
0
            ossl_strerror(sslerror, error_buffer, sizeof(error_buffer)));
5130
0
      result = CURLE_SEND_ERROR;
5131
0
      goto out;
5132
0
    }
5133
0
    default:
5134
      /* a true error */
5135
0
      failf(data, OSSL_PACKAGE " SSL_write: %s, errno %d",
5136
0
            SSL_ERROR_to_str(err), SOCKERRNO);
5137
0
      result = CURLE_SEND_ERROR;
5138
0
      goto out;
5139
0
    }
5140
0
  }
5141
5142
0
out:
5143
0
  return result;
5144
0
}
5145
5146
static CURLcode ossl_recv(struct Curl_cfilter *cf,
5147
                          struct Curl_easy *data,   /* transfer */
5148
                          char *buf,                /* store read data here */
5149
                          size_t buffersize,        /* max amount to read */
5150
                          size_t *pnread)
5151
0
{
5152
0
  char error_buffer[256];
5153
0
  unsigned long sslerror;
5154
0
  int buffsize;
5155
0
  struct ssl_connect_data *connssl = cf->ctx;
5156
0
  struct ossl_ctx *octx = (struct ossl_ctx *)connssl->backend;
5157
0
  CURLcode result = CURLE_OK;
5158
0
  int nread;
5159
5160
0
  DEBUGASSERT(octx);
5161
5162
0
  *pnread = 0;
5163
0
  ERR_clear_error();
5164
5165
0
  connssl->io_need = CURL_SSL_IO_NEED_NONE;
5166
0
  buffsize = (buffersize > (size_t)INT_MAX) ? INT_MAX : (int)buffersize;
5167
0
  nread = SSL_read(octx->ssl, buf, buffsize);
5168
5169
0
  if(nread > 0)
5170
0
    *pnread = (size_t)nread;
5171
0
  else {
5172
    /* failed SSL_read */
5173
0
    int err = SSL_get_error(octx->ssl, nread);
5174
5175
0
    switch(err) {
5176
0
    case SSL_ERROR_NONE: /* this is not an error */
5177
0
      break;
5178
0
    case SSL_ERROR_ZERO_RETURN: /* no more data */
5179
      /* close_notify alert */
5180
0
      if(cf->sockindex == FIRSTSOCKET)
5181
        /* mark the connection for close if it is indeed the control
5182
           connection */
5183
0
        CURL_TRC_CF(data, cf, "TLS close_notify");
5184
0
      break;
5185
0
    case SSL_ERROR_WANT_READ:
5186
0
      connssl->io_need = CURL_SSL_IO_NEED_RECV;
5187
0
      result = CURLE_AGAIN;
5188
0
      goto out;
5189
0
    case SSL_ERROR_WANT_WRITE:
5190
0
      connssl->io_need = CURL_SSL_IO_NEED_SEND;
5191
0
      result = CURLE_AGAIN;
5192
0
      goto out;
5193
0
    default:
5194
      /* openssl/ssl.h for SSL_ERROR_SYSCALL says "look at error stack/return
5195
         value/errno" */
5196
      /* https://docs.openssl.org/master/man3/ERR_get_error/ */
5197
0
      if(octx->io_result == CURLE_AGAIN) {
5198
0
        result = CURLE_AGAIN;
5199
0
        goto out;
5200
0
      }
5201
0
      sslerror = ERR_get_error();
5202
0
      if((nread < 0) || sslerror) {
5203
        /* If the return code was negative or there actually is an error in the
5204
           queue */
5205
0
        int sockerr = SOCKERRNO;
5206
0
        if(sslerror)
5207
0
          ossl_strerror(sslerror, error_buffer, sizeof(error_buffer));
5208
0
        else if(sockerr && err == SSL_ERROR_SYSCALL)
5209
0
          curlx_strerror(sockerr, error_buffer, sizeof(error_buffer));
5210
0
        else
5211
0
          curl_msnprintf(error_buffer, sizeof(error_buffer), "%s",
5212
0
                         SSL_ERROR_to_str(err));
5213
0
        failf(data, OSSL_PACKAGE " SSL_read: %s, errno %d",
5214
0
              error_buffer, sockerr);
5215
0
        result = CURLE_RECV_ERROR;
5216
0
        goto out;
5217
0
      }
5218
0
      else if(err == SSL_ERROR_SYSCALL) {
5219
0
        if(octx->io_result) {
5220
          /* logging handling in underlying filter already */
5221
0
          result = octx->io_result;
5222
0
        }
5223
0
        else if(connssl->peer_closed) {
5224
0
          failf(data, "Connection closed abruptly");
5225
0
          result = CURLE_RECV_ERROR;
5226
0
        }
5227
0
        else {
5228
          /* We should no longer get here nowadays, but handle
5229
           * the error in case of some weirdness in the OSSL stack */
5230
0
          int sockerr = SOCKERRNO;
5231
0
          if(sockerr)
5232
0
            curlx_strerror(sockerr, error_buffer, sizeof(error_buffer));
5233
0
          else {
5234
0
            curl_msnprintf(error_buffer, sizeof(error_buffer),
5235
0
                           "Connection closed abruptly");
5236
0
          }
5237
0
          failf(data, OSSL_PACKAGE " SSL_read: %s, errno %d",
5238
0
                error_buffer, sockerr);
5239
0
          result = CURLE_RECV_ERROR;
5240
0
        }
5241
0
        goto out;
5242
0
      }
5243
0
    }
5244
0
  }
5245
5246
0
out:
5247
0
  if((!result && !*pnread) || (result == CURLE_AGAIN)) {
5248
    /* This happens when:
5249
     * - we read an EOF
5250
     * - OpenSSLs buffers are empty, there is no more data
5251
     * - OpenSSL read is blocked on writing something first
5252
     * - an incomplete TLS packet is buffered that cannot be read
5253
     *   until more data arrives */
5254
0
    connssl->input_pending = FALSE;
5255
0
  }
5256
0
  CURL_TRC_CF(data, cf, "ossl_recv(len=%zu) -> %d, %zu (in_pending=%d)",
5257
0
              buffersize, result, *pnread, connssl->input_pending);
5258
0
  return result;
5259
0
}
5260
5261
static CURLcode ossl_get_channel_binding(struct Curl_easy *data,
5262
                                         int sockindex,
5263
                                         struct dynbuf *binding)
5264
0
{
5265
0
  X509 *cert;
5266
0
  int mdnid;
5267
0
  bool no_digest_acceptable = FALSE;
5268
0
  const EVP_MD *algo_type = NULL;
5269
0
  const char *algo_name = NULL;
5270
0
  unsigned int length;
5271
0
  unsigned char buf[EVP_MAX_MD_SIZE];
5272
5273
0
  const char prefix[] = "tls-server-end-point:";
5274
0
  struct connectdata *conn = data->conn;
5275
0
  struct Curl_cfilter *cf = conn->cfilter[sockindex];
5276
0
  struct ossl_ctx *octx = NULL;
5277
0
  CURLcode result = CURLE_OK;
5278
5279
0
  do {
5280
0
    const struct Curl_cftype *cft = cf->cft;
5281
0
    struct ssl_connect_data *connssl = cf->ctx;
5282
5283
0
    if(cft->name && !strcmp(cft->name, "SSL")) {
5284
0
      octx = (struct ossl_ctx *)connssl->backend;
5285
0
      break;
5286
0
    }
5287
5288
0
    cf = cf->next;
5289
0
  } while(cf);
5290
5291
0
  if(!octx) {
5292
0
    failf(data, "Failed to find the SSL filter");
5293
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
5294
0
  }
5295
5296
0
  cert = SSL_get1_peer_certificate(octx->ssl);
5297
0
  if(!cert)
5298
    /* No server certificate, do not do channel binding */
5299
0
    return CURLE_OK;
5300
5301
0
#ifdef HAVE_OPENSSL3
5302
0
  {
5303
0
    int pknid, secbits;
5304
0
    uint32_t flags;
5305
0
    EVP_PKEY *pkey = X509_get0_pubkey(cert);
5306
5307
0
    if(!X509_get_signature_info(cert, &mdnid, &pknid, &secbits, &flags)) {
5308
0
      failf(data, "certificate signature algorithm not recognized");
5309
0
      result = CURLE_SSL_INVALIDCERTSTATUS;
5310
0
      goto out;
5311
0
    }
5312
5313
0
    if(mdnid != NID_undef) {
5314
0
      if(mdnid == NID_md5 || mdnid == NID_sha1) {
5315
0
        algo_type = EVP_sha256();
5316
0
      }
5317
0
      else
5318
0
        algo_type = EVP_get_digestbynid(mdnid);
5319
0
    }
5320
0
    else if(pkey && !EVP_PKEY_is_a(pkey, OBJ_nid2sn(pknid))) {
5321
      /* The cert's pkey is different from the algorithm used to sign
5322
       * the certificate. Since the reported `mdnid` is undefined, there
5323
       * is no digest algorithm available here. This happens in PQC
5324
       * and is accepted, resulting in no addition to the binding. */
5325
0
      no_digest_acceptable = TRUE;
5326
0
    }
5327
0
    else if(pkey) {
5328
      /* cert's pkey type is the same as the cert signer (or same family).
5329
       * Ask for the mandatory/advisory digest algorithm for the pkey.
5330
       */
5331
0
      char mdname[128] = "";
5332
0
      int rc = EVP_PKEY_get_default_digest_name(pkey, mdname, sizeof(mdname));
5333
0
      bool md_is_undef = !strcmp(mdname, "UNDEF");
5334
5335
0
      if(rc == 2 && md_is_undef) {
5336
        /* OpenSSL declares "undef" the *mandatory* digest for this key.
5337
         * This is some PQC shit, accept it, no addition to binding. */
5338
0
        no_digest_acceptable = TRUE;
5339
0
      }
5340
0
      else if(rc > 0 && mdname[0] != '\0' && !md_is_undef) {
5341
0
        infof(data, "Digest algorithm : %s%s (derived from public key)"
5342
0
              ", but unavailable",
5343
0
              mdname, rc == 2 ? " [mandatory]" : " [advisory]");
5344
0
      }
5345
0
    }
5346
0
  }
5347
#else /* HAVE_OPENSSL3 */
5348
5349
  if(!OBJ_find_sigid_algs(X509_get_signature_nid(cert), &mdnid, NULL)) {
5350
    failf(data,
5351
          "Unable to find digest NID for certificate signature algorithm");
5352
    result = CURLE_SSL_INVALIDCERTSTATUS;
5353
    goto out;
5354
  }
5355
5356
  /* https://datatracker.ietf.org/doc/html/rfc5929#section-4.1 */
5357
  if(mdnid == NID_md5 || mdnid == NID_sha1) {
5358
    algo_type = EVP_sha256();
5359
  }
5360
  else {
5361
    algo_type = EVP_get_digestbynid(mdnid);
5362
    if(!algo_type) {
5363
      algo_name = OBJ_nid2sn(mdnid);
5364
      failf(data, "Could not find digest algorithm %s (NID %d)",
5365
            algo_name ? algo_name : "(null)", mdnid);
5366
      result = CURLE_SSL_INVALIDCERTSTATUS;
5367
      goto out;
5368
    }
5369
  }
5370
5371
#endif /* HAVE_OPENSSL3, else */
5372
5373
0
  if(!algo_type) {
5374
0
    if(no_digest_acceptable) {
5375
0
      infof(data, "certificate exposes no signing digest algorithm, "
5376
0
            "nothing to add to channel binding");
5377
0
      result = CURLE_OK;
5378
0
      goto out;
5379
0
    }
5380
    /* unacceptable, something is wrong, fail */
5381
0
    algo_name = OBJ_nid2sn(mdnid);
5382
0
    failf(data, "Unable to find digest algorithm %s (NID %d) "
5383
0
          "for channel binding", algo_name ? algo_name : "(null)", mdnid);
5384
0
    result = CURLE_SSL_INVALIDCERTSTATUS;
5385
0
    goto out;
5386
0
  }
5387
5388
0
  if(!X509_digest(cert, algo_type, buf, &length)) {
5389
0
    failf(data, "X509_digest() failed for channel binding");
5390
0
    result = CURLE_SSL_INVALIDCERTSTATUS;
5391
0
    goto out;
5392
0
  }
5393
5394
  /* Append "tls-server-end-point:" */
5395
0
  result = curlx_dyn_addn(binding, prefix, sizeof(prefix) - 1);
5396
0
  if(result)
5397
0
    goto out;
5398
5399
  /* Append digest */
5400
0
  result = curlx_dyn_addn(binding, buf, length);
5401
5402
0
out:
5403
0
  X509_free(cert);
5404
0
  return result;
5405
0
}
5406
5407
size_t Curl_ossl_version(char *buffer, size_t size)
5408
0
{
5409
#ifdef LIBRESSL_VERSION_NUMBER
5410
  char *p;
5411
  size_t count;
5412
  const char *ver = OpenSSL_version(OPENSSL_VERSION);
5413
  const char expected[] = OSSL_PACKAGE " "; /* ie "LibreSSL " */
5414
  if(curl_strnequal(ver, expected, sizeof(expected) - 1)) {
5415
    ver += sizeof(expected) - 1;
5416
  }
5417
  count = curl_msnprintf(buffer, size, "%s/%s", OSSL_PACKAGE, ver);
5418
  for(p = buffer; *p; ++p) {
5419
    if(ISBLANK(*p))
5420
      *p = '_';
5421
  }
5422
  return count;
5423
#elif defined(OPENSSL_IS_AWSLC)
5424
  return curl_msnprintf(buffer, size, "%s/%s",
5425
                        OSSL_PACKAGE, AWSLC_VERSION_NUMBER_STRING);
5426
#elif defined(OPENSSL_IS_BORINGSSL)
5427
#ifdef CURL_BORINGSSL_VERSION
5428
  return curl_msnprintf(buffer, size, "%s/%s",
5429
                        OSSL_PACKAGE, CURL_BORINGSSL_VERSION);
5430
#else
5431
  return curl_msnprintf(buffer, size, OSSL_PACKAGE);
5432
#endif
5433
#else /* OpenSSL 3+ */
5434
0
  return curl_msnprintf(buffer, size, "%s/%s",
5435
0
                        OSSL_PACKAGE, OpenSSL_version(OPENSSL_VERSION_STRING));
5436
0
#endif
5437
0
}
5438
5439
/* can be called with data == NULL */
5440
static CURLcode ossl_random(struct Curl_easy *data,
5441
                            unsigned char *entropy, size_t length)
5442
0
{
5443
0
  int rc;
5444
0
  if(data) {
5445
0
    if(ossl_seed(data)) /* Initiate the seed if not already done */
5446
0
      return CURLE_FAILED_INIT; /* could not seed for some reason */
5447
0
  }
5448
0
  else {
5449
0
    if(!rand_enough())
5450
0
      return CURLE_FAILED_INIT;
5451
0
  }
5452
  /* RAND_bytes() returns 1 on success, 0 otherwise. */
5453
0
  rc = RAND_bytes(entropy, (ossl_valsize_t)curlx_uztosi(length));
5454
0
  return rc == 1 ? CURLE_OK : CURLE_FAILED_INIT;
5455
0
}
5456
5457
static CURLcode ossl_sha256sum(const unsigned char *input,
5458
                               size_t len,
5459
                               unsigned char *sha256sum /* output */,
5460
                               size_t unused)
5461
0
{
5462
0
  CURLcode result = CURLE_OK;
5463
0
  EVP_MD_CTX *mdctx;
5464
0
  (void)unused;
5465
5466
0
  mdctx = EVP_MD_CTX_create();
5467
0
  if(!mdctx)
5468
0
    return CURLE_OUT_OF_MEMORY;
5469
0
  if(!EVP_DigestInit(mdctx, EVP_sha256())) {
5470
0
    result = CURLE_FAILED_INIT;
5471
0
    goto out;
5472
0
  }
5473
0
  if(!EVP_DigestUpdate(mdctx, input, len) ||
5474
0
     !EVP_DigestFinal_ex(mdctx, sha256sum, NULL))
5475
0
    result = CURLE_BAD_FUNCTION_ARGUMENT;
5476
0
out:
5477
0
  EVP_MD_CTX_destroy(mdctx);
5478
0
  return result;
5479
0
}
5480
5481
static bool ossl_cert_status_request(void)
5482
0
{
5483
0
#ifndef OPENSSL_NO_OCSP
5484
0
  return TRUE;
5485
#else
5486
  return FALSE;
5487
#endif
5488
0
}
5489
5490
static void *ossl_get_internals(struct ssl_connect_data *connssl,
5491
                                CURLINFO info)
5492
0
{
5493
  /* Legacy: CURLINFO_TLS_SESSION must return an SSL_CTX pointer. */
5494
0
  struct ossl_ctx *octx = (struct ossl_ctx *)connssl->backend;
5495
0
  DEBUGASSERT(octx);
5496
0
  return info == CURLINFO_TLS_SESSION ?
5497
0
    (void *)octx->ssl_ctx : (void *)octx->ssl;
5498
0
}
5499
5500
const struct Curl_ssl Curl_ssl_openssl = {
5501
  { CURLSSLBACKEND_OPENSSL, "openssl" }, /* info */
5502
5503
  SSLSUPP_CA_PATH |
5504
  SSLSUPP_CAINFO_BLOB |
5505
  SSLSUPP_CERTINFO |
5506
  SSLSUPP_PINNEDPUBKEY |
5507
  SSLSUPP_SSL_CTX |
5508
#ifdef HAVE_SSL_CTX_SET_CIPHERSUITES
5509
  SSLSUPP_TLS13_CIPHERSUITES |
5510
#endif
5511
#ifdef HAVE_SSL_CTX_SET1_SIGALGS
5512
  SSLSUPP_SIGNATURE_ALGORITHMS |
5513
#endif
5514
#ifdef HAVE_SSL_SET1_ECH_CONFIG_LIST
5515
  SSLSUPP_ECH |
5516
#endif
5517
  SSLSUPP_CA_CACHE |
5518
  SSLSUPP_HTTPS_PROXY |
5519
  SSLSUPP_CIPHER_LIST |
5520
  SSLSUPP_ISSUERCERT |
5521
  SSLSUPP_ISSUERCERT_BLOB |
5522
  SSLSUPP_SSL_EC_CURVES |
5523
  SSLSUPP_CRLFILE,
5524
5525
  sizeof(struct ossl_ctx),
5526
5527
  ossl_init,                /* init */
5528
  ossl_cleanup,             /* cleanup */
5529
  Curl_ossl_version,        /* version */
5530
  ossl_shutdown,            /* shutdown */
5531
  ossl_data_pending,        /* data_pending */
5532
  ossl_random,              /* random */
5533
  ossl_cert_status_request, /* cert_status_request */
5534
  ossl_connect,             /* connect */
5535
  Curl_ssl_adjust_pollset,  /* adjust_pollset */
5536
  ossl_get_internals,       /* get_internals */
5537
  ossl_close,               /* close_one */
5538
  ossl_close_all,           /* close_all */
5539
  ossl_set_engine,          /* set_engine or provider */
5540
  ossl_set_engine_default,  /* set_engine_default */
5541
  ossl_engines_list,        /* engines_list */
5542
  ossl_sha256sum,           /* sha256sum */
5543
  ossl_recv,                /* recv decrypted data */
5544
  ossl_send,                /* send data to encrypt */
5545
  ossl_get_channel_binding  /* get_channel_binding */
5546
};
5547
5548
#endif /* USE_OPENSSL */