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