OpenVPN
ssl_openssl.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2026 OpenVPN Inc <sales@openvpn.net>
9 * Copyright (C) 2010-2026 Sentyron B.V. <openvpn@sentyron.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, see <https://www.gnu.org/licenses/>.
22 */
23
29#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif
32
33#include "syshead.h"
34
35#if defined(ENABLE_CRYPTO_OPENSSL)
36
37#include "errlevel.h"
38#include "buffer.h"
39#include "misc.h"
40#include "manage.h"
41#include "memdbg.h"
42#include "ssl_backend.h"
43#include "ssl_common.h"
44#include "base64.h"
45#include "openssl_compat.h"
46#include "xkey_common.h"
47
48#ifdef ENABLE_CRYPTOAPI
49#include "cryptoapi.h"
50#endif
51
52#include "ssl_verify_openssl.h"
53#include "ssl_util.h"
54
55#include <openssl/bn.h>
56#include <openssl/crypto.h>
57#include <openssl/dh.h>
58#include <openssl/dsa.h>
59#include <openssl/err.h>
60#include <openssl/pkcs12.h>
61#include <openssl/rsa.h>
62#include <openssl/x509.h>
63#include <openssl/ssl.h>
64#ifndef OPENSSL_NO_EC
65#include <openssl/ec.h>
66#endif
67
68#if OPENSSL_VERSION_NUMBER >= 0x30000000L
69#define HAVE_OPENSSL_STORE_API
70#include <openssl/ui.h>
71#include <openssl/store.h>
72#endif
73
74#if defined(_MSC_VER) && !defined(_M_ARM64)
75#include <openssl/applink.c>
76#endif
77
79
80static void unload_xkey_provider(void);
81
82/*
83 * Allocate space in SSL objects in which to store a struct tls_session
84 * pointer back to parent.
85 *
86 */
87
88int mydata_index; /* GLOBAL */
89
90void
92{
93 mydata_index = SSL_get_ex_new_index(0, "struct session *", NULL, NULL, NULL);
94 ASSERT(mydata_index >= 0);
95}
96
97void
99{
100}
101
102void
104{
105 ASSERT(NULL != ctx);
106
107 ctx->ctx = SSL_CTX_new_ex(tls_libctx, NULL, SSLv23_server_method());
108
109 if (ctx->ctx == NULL)
110 {
111 crypto_msg(M_FATAL, "SSL_CTX_new SSLv23_server_method");
112 }
113 if (ERR_peek_error() != 0)
114 {
115 crypto_msg(M_WARN, "Warning: TLS server context initialisation "
116 "has warnings.");
117 }
118}
119
120void
122{
123 ASSERT(NULL != ctx);
124
125 ctx->ctx = SSL_CTX_new_ex(tls_libctx, NULL, SSLv23_client_method());
126
127 if (ctx->ctx == NULL)
128 {
129 crypto_msg(M_FATAL, "SSL_CTX_new SSLv23_client_method");
130 }
131 if (ERR_peek_error() != 0)
132 {
133 crypto_msg(M_WARN, "Warning: TLS client context initialisation "
134 "has warnings.");
135 }
136}
137
138void
140{
141 ASSERT(NULL != ctx);
142 SSL_CTX_free(ctx->ctx);
143 ctx->ctx = NULL;
144 sk_X509_CRL_pop_free(ctx->crls, X509_CRL_free);
145 ctx->crls = NULL;
146 unload_xkey_provider(); /* in case it is loaded */
147}
148
149bool
151{
152 /* either this should be NULL or should be non-null and then have a
153 * valid TLS ctx inside as well */
154 ASSERT(ctx == NULL || ctx->ctx != NULL);
155 return ctx != NULL;
156}
157
158bool
159key_state_export_keying_material(struct tls_session *session, const char *label, size_t label_size,
160 void *ekm, size_t ekm_size)
161
162{
163 SSL *ssl = session->key[KS_PRIMARY].ks_ssl.ssl;
164
165 if (SSL_export_keying_material(ssl, ekm, ekm_size, label, label_size, NULL, 0, 0) == 1)
166 {
167 return true;
168 }
169 else
170 {
171 secure_memzero(ekm, ekm_size);
172 return false;
173 }
174}
175
176/*
177 * Print debugging information on SSL/TLS session negotiation.
178 */
179
180#ifndef INFO_CALLBACK_SSL_CONST
181#define INFO_CALLBACK_SSL_CONST const
182#endif
183static void
184info_callback(INFO_CALLBACK_SSL_CONST SSL *s, int where, int ret)
185{
186 if (where & SSL_CB_LOOP)
187 {
188 dmsg(D_HANDSHAKE_VERBOSE, "SSL state (%s): %s",
189 where & SSL_ST_CONNECT ? "connect"
190 : where & SSL_ST_ACCEPT ? "accept"
191 : "undefined",
192 SSL_state_string_long(s));
193 }
194 else if (where & SSL_CB_ALERT)
195 {
196 dmsg(D_TLS_DEBUG_LOW, "%s %s SSL alert: %s", where & SSL_CB_READ ? "Received" : "Sent",
197 SSL_alert_type_string_long(ret), SSL_alert_desc_string_long(ret));
198 }
199}
200
201/*
202 * Return maximum TLS version supported by local OpenSSL library.
203 * Assume that presence of SSL_OP_NO_TLSvX macro indicates that
204 * TLSvX is supported.
205 */
206int
208{
209#if defined(TLS1_3_VERSION)
210 /* If this is defined we can safely assume TLS 1.3 support */
211 return TLS_VER_1_3;
212#elif OPENSSL_VERSION_NUMBER >= 0x10100000L
213 /*
214 * If TLS_VER_1_3 is not defined, we were compiled against a version that
215 * did not support TLS 1.3.
216 *
217 * However, the library we are *linked* against might be OpenSSL 1.1.1
218 * and therefore supports TLS 1.3. This needs to be checked at runtime
219 * since we can be compiled against 1.1.0 and then the library can be
220 * upgraded to 1.1.1.
221 * We only need to check this for OpenSSL versions that can be
222 * upgraded to 1.1.1 without recompile (>= 1.1.0)
223 */
224 if (OpenSSL_version_num() >= 0x1010100fL)
225 {
226 return TLS_VER_1_3;
227 }
228 else
229 {
230 return TLS_VER_1_2;
231 }
232#elif defined(TLS1_2_VERSION) || defined(SSL_OP_NO_TLSv1_2)
233 return TLS_VER_1_2;
234#elif defined(TLS1_1_VERSION) || defined(SSL_OP_NO_TLSv1_1)
235 return TLS_VER_1_1;
236#else /* if defined(TLS1_3_VERSION) */
237 return TLS_VER_1_0;
238#endif
239}
240
242static uint16_t
243openssl_tls_version(unsigned int ver)
244{
245 if (ver == TLS_VER_1_0)
246 {
247 return TLS1_VERSION;
248 }
249 else if (ver == TLS_VER_1_1)
250 {
251 return TLS1_1_VERSION;
252 }
253 else if (ver == TLS_VER_1_2)
254 {
255 return TLS1_2_VERSION;
256 }
257 else if (ver == TLS_VER_1_3)
258 {
259 /*
260 * Supporting the library upgraded to TLS1.3 without recompile
261 * is enough to support here with a simple constant that the same
262 * as in the TLS 1.3, so spec it is very unlikely that OpenSSL
263 * will change this constant
264 */
265#ifndef TLS1_3_VERSION
266 /*
267 * We do not want to define TLS_VER_1_3 if not defined
268 * since other parts of the code use the existance of this macro
269 * as proxy for TLS 1.3 support
270 */
271 return 0x0304;
272#else
273 return TLS1_3_VERSION;
274#endif
275 }
276 return 0;
277}
278
279static bool
280tls_ctx_set_tls_versions(struct tls_root_ctx *ctx, unsigned int ssl_flags)
281{
282 uint16_t tls_ver_min =
284 uint16_t tls_ver_max =
286
287 if (!tls_ver_min)
288 {
289 /* Enforce at least TLS 1.0 */
290 uint16_t cur_min = (uint16_t)SSL_CTX_get_min_proto_version(ctx->ctx);
291 tls_ver_min = cur_min < TLS1_VERSION ? TLS1_VERSION : cur_min;
292 }
293
294 if (!SSL_CTX_set_min_proto_version(ctx->ctx, tls_ver_min))
295 {
296 msg(D_TLS_ERRORS, "%s: failed to set minimum TLS version", __func__);
297 return false;
298 }
299
300 if (tls_ver_max && !SSL_CTX_set_max_proto_version(ctx->ctx, tls_ver_max))
301 {
302 msg(D_TLS_ERRORS, "%s: failed to set maximum TLS version", __func__);
303 return false;
304 }
305
306 return true;
307}
308
309static int
310cert_verify_callback(X509_STORE_CTX *ctx, void *arg)
311{
312 struct tls_session *session;
313 SSL *ssl;
314
315 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
316 ASSERT(ssl);
317 session = SSL_get_ex_data(ssl, mydata_index);
319
320 /* Configure CRLs. */
321 X509_STORE_CTX_set0_crls(ctx, session->opt->ssl_ctx->crls);
322 return X509_verify_cert(ctx);
323}
324
325bool
326tls_ctx_set_options(struct tls_root_ctx *ctx, unsigned int ssl_flags)
327{
328 ASSERT(NULL != ctx);
329
330 /* process SSL options */
331 openssl_opt_t sslopt = SSL_OP_SINGLE_DH_USE | SSL_OP_NO_TICKET;
332#ifdef SSL_OP_CIPHER_SERVER_PREFERENCE
333 sslopt |= SSL_OP_CIPHER_SERVER_PREFERENCE;
334#endif
335 sslopt |= SSL_OP_NO_COMPRESSION;
336 /* Disable TLS renegotiations. OpenVPN's renegotiation creates new SSL
337 * session and does not depend on this feature. And TLS renegotiations have
338 * been problematic in the past */
339#ifdef SSL_OP_NO_RENEGOTIATION
340 sslopt |= SSL_OP_NO_RENEGOTIATION;
341#endif
342
343 SSL_CTX_set_options(ctx->ctx, sslopt);
344
345 if (!tls_ctx_set_tls_versions(ctx, ssl_flags))
346 {
347 return false;
348 }
349
350#ifdef SSL_MODE_RELEASE_BUFFERS
351 SSL_CTX_set_mode(ctx->ctx, SSL_MODE_RELEASE_BUFFERS);
352#endif
353 SSL_CTX_set_session_cache_mode(ctx->ctx, SSL_SESS_CACHE_OFF);
354 SSL_CTX_set_default_passwd_cb(ctx->ctx, pem_password_callback);
355
356 /* Require peer certificate verification */
357 int verify_flags = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
358 if (ssl_flags & SSLF_CLIENT_CERT_NOT_REQUIRED)
359 {
360 verify_flags = 0;
361 }
362 else if (ssl_flags & SSLF_CLIENT_CERT_OPTIONAL)
363 {
364 verify_flags = SSL_VERIFY_PEER;
365 }
366 SSL_CTX_set_verify(ctx->ctx, verify_flags, verify_callback);
367 SSL_CTX_set_cert_verify_callback(ctx->ctx, cert_verify_callback, NULL);
368
369 SSL_CTX_set_info_callback(ctx->ctx, info_callback);
370
371 return true;
372}
373
374static void
375convert_tls_list_to_openssl(char *openssl_ciphers, size_t len, const char *ciphers)
376{
377 /* Parse supplied cipher list and pass on to OpenSSL */
378 size_t begin_of_cipher, end_of_cipher;
379
380 const char *current_cipher;
381 size_t current_cipher_len;
382
383 const tls_cipher_name_pair *cipher_pair;
384
385 size_t openssl_ciphers_len = 0;
386 openssl_ciphers[0] = '\0';
387
388 /* Translate IANA cipher suite names to OpenSSL names */
389 begin_of_cipher = end_of_cipher = 0;
390 for (; begin_of_cipher < strlen(ciphers); begin_of_cipher = end_of_cipher)
391 {
392 end_of_cipher += strcspn(&ciphers[begin_of_cipher], ":");
393 cipher_pair =
394 tls_get_cipher_name_pair(&ciphers[begin_of_cipher], end_of_cipher - begin_of_cipher);
395
396 if (NULL == cipher_pair)
397 {
398 /* No translation found, use original */
399 current_cipher = &ciphers[begin_of_cipher];
400 current_cipher_len = end_of_cipher - begin_of_cipher;
401
402 /* Issue warning on missing translation */
403 /* %.*s format specifier expects length of type int, so guarantee */
404 /* that length is small enough and cast to int. */
405 msg(D_LOW, "No valid translation found for TLS cipher '%.*s'",
406 constrain_int((int)current_cipher_len, 0, 256), current_cipher);
407 }
408 else
409 {
410 /* Use OpenSSL name */
411 current_cipher = cipher_pair->openssl_name;
412 current_cipher_len = strlen(current_cipher);
413
414 if (end_of_cipher - begin_of_cipher == current_cipher_len
415 && 0
416 != memcmp(&ciphers[begin_of_cipher], cipher_pair->iana_name,
417 end_of_cipher - begin_of_cipher))
418 {
419 /* Non-IANA name used, show warning */
420 msg(M_WARN, "Deprecated TLS cipher name '%s', please use IANA name '%s'",
421 cipher_pair->openssl_name, cipher_pair->iana_name);
422 }
423 }
424
425 /* Make sure new cipher name fits in cipher string */
426 if ((SIZE_MAX - openssl_ciphers_len) < current_cipher_len
427 || (len - 1) < (openssl_ciphers_len + current_cipher_len))
428 {
429 msg(M_FATAL, "Failed to set restricted TLS cipher list, too long (>%d).",
430 (int)(len - 1));
431 }
432
433 /* Concatenate cipher name to OpenSSL cipher string */
434 memcpy(&openssl_ciphers[openssl_ciphers_len], current_cipher, current_cipher_len);
435 openssl_ciphers_len += current_cipher_len;
436 openssl_ciphers[openssl_ciphers_len] = ':';
437 openssl_ciphers_len++;
438
439 end_of_cipher++;
440 }
441
442 if (openssl_ciphers_len > 0)
443 {
444 openssl_ciphers[openssl_ciphers_len - 1] = '\0';
445 }
446}
447
448void
449tls_ctx_restrict_ciphers(struct tls_root_ctx *ctx, const char *ciphers)
450{
451 if (ciphers == NULL)
452 {
453 /* Use sane default TLS cipher list */
454 if (!SSL_CTX_set_cipher_list(
455 ctx->ctx,
456 /* Use openssl's default list as a basis */
457 "DEFAULT"
458 /* Disable export ciphers and openssl's 'low' and 'medium' ciphers */
459 ":!EXP:!LOW:!MEDIUM"
460 /* Disable static (EC)DH keys (no forward secrecy) */
461 ":!kDH:!kECDH"
462 /* Disable DSA private keys */
463 ":!DSS"
464 /* Disable unsupported TLS modes */
465 ":!PSK:!SRP:!kRSA"))
466 {
467 crypto_msg(M_FATAL, "Failed to set default TLS cipher list.");
468 }
469 return;
470 }
471
472 char openssl_ciphers[4096];
473 convert_tls_list_to_openssl(openssl_ciphers, sizeof(openssl_ciphers), ciphers);
474
475 ASSERT(NULL != ctx);
476
477 /* Set OpenSSL cipher list */
478 if (!SSL_CTX_set_cipher_list(ctx->ctx, openssl_ciphers))
479 {
480 crypto_msg(M_FATAL, "Failed to set restricted TLS cipher list: %s", openssl_ciphers);
481 }
482}
483
484static void
485convert_tls13_list_to_openssl(char *openssl_ciphers, size_t len, const char *ciphers)
486{
487 /*
488 * OpenSSL (and official IANA) cipher names have _ in them. We
489 * historically used names with - in them. Silently convert names
490 * with - to names with _ to support both
491 */
492 if (strlen(ciphers) >= (len - 1))
493 {
494 msg(M_FATAL, "Failed to set restricted TLS 1.3 cipher list, too long (>%d).",
495 (int)(len - 1));
496 }
497
498 strncpy(openssl_ciphers, ciphers, len);
499
500 for (size_t i = 0; i < strlen(openssl_ciphers); i++)
501 {
502 if (openssl_ciphers[i] == '-')
503 {
504 openssl_ciphers[i] = '_';
505 }
506 }
507}
508
509void
510tls_ctx_restrict_ciphers_tls13(struct tls_root_ctx *ctx, const char *ciphers)
511{
512 if (ciphers == NULL)
513 {
514 /* default cipher list of OpenSSL 1.1.1 is sane, do not set own
515 * default as we do with tls-cipher */
516 return;
517 }
518
519#if !defined(TLS1_3_VERSION)
521 "Not compiled with OpenSSL 1.1.1 or higher. "
522 "Ignoring TLS 1.3 only tls-ciphersuites '%s' setting.",
523 ciphers);
524#else
525 ASSERT(NULL != ctx);
526
527 char openssl_ciphers[4096];
528 convert_tls13_list_to_openssl(openssl_ciphers, sizeof(openssl_ciphers), ciphers);
529
530 if (!SSL_CTX_set_ciphersuites(ctx->ctx, openssl_ciphers))
531 {
532 crypto_msg(M_FATAL, "Failed to set restricted TLS 1.3 cipher list: %s", openssl_ciphers);
533 }
534#endif
535}
536
537void
538tls_ctx_set_cert_profile(struct tls_root_ctx *ctx, const char *profile)
539{
540#if OPENSSL_VERSION_NUMBER > 0x10100000L \
541 && (!defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER > 0x3060000fL) \
542 && !defined(OPENSSL_IS_AWSLC)
543 /* OpenSSL does not have certificate profiles, but a complex set of
544 * callbacks that we could try to implement to achieve something similar.
545 * For now, use OpenSSL's security levels to achieve similar (but not equal)
546 * behaviour. */
547 if (!profile || 0 == strcmp(profile, "legacy"))
548 {
549 SSL_CTX_set_security_level(ctx->ctx, 1);
550 }
551 else if (0 == strcmp(profile, "insecure"))
552 {
553 SSL_CTX_set_security_level(ctx->ctx, 0);
554 }
555 else if (0 == strcmp(profile, "preferred"))
556 {
557 SSL_CTX_set_security_level(ctx->ctx, 2);
558 }
559 else if (0 == strcmp(profile, "suiteb"))
560 {
561 SSL_CTX_set_security_level(ctx->ctx, 3);
562 SSL_CTX_set_cipher_list(ctx->ctx, "SUITEB128");
563 }
564 else
565 {
566 msg(M_FATAL, "ERROR: Invalid cert profile: %s", profile);
567 }
568#else /* if OPENSSL_VERSION_NUMBER > 0x10100000L */
569 if (profile)
570 {
571 msg(M_WARN,
572 "WARNING: OpenSSL 1.1.0, AWS-LC and LibreSSL < 3.6.0 do not "
573 "support --tls-cert-profile, ignoring user-set profile: '%s'",
574 profile);
575 }
576#endif /* if OPENSSL_VERSION_NUMBER > 0x10100000L */
577}
578
579void
580tls_ctx_set_tls_groups(struct tls_root_ctx *ctx, const char *groups)
581{
582 ASSERT(ctx);
583#if OPENSSL_VERSION_NUMBER < 0x30000000L && !defined(ENABLE_CRYPTO_WOLFSSL)
584 struct gc_arena gc = gc_new();
585 /* This method could be as easy as
586 * SSL_CTX_set1_groups_list(ctx->ctx, groups)
587 * but OpenSSL (< 3.0) does not like the name secp256r1 for prime256v1
588 * This is one of the important curves.
589 * To support the same name for OpenSSL and mbedTLS, we do
590 * this dance.
591 * Also note that the code is wrong in the presence of OpenSSL3 providers.
592 */
593
594 int groups_count = get_num_elements(groups, ':');
595
596 int *glist;
597 /* Allocate an array for them */
598 ALLOC_ARRAY_CLEAR_GC(glist, int, groups_count, &gc);
599
600 /* Parse allowed ciphers, getting IDs */
601 int glistlen = 0;
602 char *tmp_groups = string_alloc(groups, &gc);
603
604 const char *token;
605 while ((token = strsep(&tmp_groups, ":")))
606 {
607 if (streq(token, "secp256r1"))
608 {
609 token = "prime256v1";
610 }
611 int nid = OBJ_sn2nid(token);
612
613 if (nid == 0)
614 {
615 msg(M_WARN, "Warning unknown curve/group specified: %s", token);
616 }
617 else
618 {
619 glist[glistlen] = nid;
620 glistlen++;
621 }
622 }
623
624 if (!SSL_CTX_set1_groups(ctx->ctx, glist, glistlen))
625 {
626 crypto_msg(M_FATAL, "Failed to set allowed TLS group list: %s", groups);
627 }
628 gc_free(&gc);
629#else /* if OPENSSL_VERSION_NUMBER < 0x30000000L */
630 if (!SSL_CTX_set1_groups_list(ctx->ctx, groups))
631 {
632 crypto_msg(M_FATAL, "Failed to set allowed TLS group list: %s", groups);
633 }
634#endif /* if OPENSSL_VERSION_NUMBER < 0x30000000L */
635}
636
637#if OPENSSL_VERSION_NUMBER < 0x40000000L
638void
640{
641 int ret;
642 const X509 *cert;
643
644 ASSERT(ctx);
645
646 cert = SSL_CTX_get0_certificate(ctx->ctx);
647
648 if (cert == NULL)
649 {
650 return; /* Nothing to check if there is no certificate */
651 }
652
653 ret = X509_cmp_time(X509_get0_notBefore(cert), NULL);
654 if (ret == 0)
655 {
656 msg(D_TLS_DEBUG_MED, "Failed to read certificate notBefore field.");
657 }
658 if (ret > 0)
659 {
660 msg(M_WARN, "WARNING: Your certificate is not yet valid!");
661 }
662
663 ret = X509_cmp_time(X509_get0_notAfter(cert), NULL);
664 if (ret == 0)
665 {
666 msg(D_TLS_DEBUG_MED, "Failed to read certificate notAfter field.");
667 }
668 if (ret < 0)
669 {
670 msg(M_WARN, "WARNING: Your certificate has expired!");
671 }
672}
673#else
674void
675tls_ctx_check_cert_time(const struct tls_root_ctx *ctx)
676{
677 const X509 *cert;
678 ASSERT(ctx);
679
680 cert = SSL_CTX_get0_certificate(ctx->ctx);
681
682 if (cert == NULL)
683 {
684 return; /* Nothing to check if there is no certificate */
685 }
686
687 X509_VERIFY_PARAM *vpm = X509_VERIFY_PARAM_new();
688
689 if (vpm == NULL)
690 {
691 msg(D_TLS_DEBUG_MED, "Failed to initialise certificate verification parameters.");
692 return;
693 }
694
695 X509_VERIFY_PARAM_set_flags(vpm, X509_V_FLAG_USE_CHECK_TIME);
696 X509_VERIFY_PARAM_set_time(vpm, now);
697
698 int error = 0;
699 int ret = X509_check_certificate_times(vpm, cert, &error);
700 X509_VERIFY_PARAM_free(vpm);
701
702 if (ret == 1)
703 {
704 return;
705 }
706
707 switch (error)
708 {
709 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
710 msg(D_TLS_DEBUG_MED, "Failed to read certificate notBefore field.");
711 break;
712
713 case X509_V_ERR_CERT_NOT_YET_VALID:
714 msg(M_WARN, "WARNING: Your certificate is not yet valid!");
715 break;
716
717 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
718 msg(D_TLS_DEBUG_MED, "Failed to read certificate notAfter field.");
719 break;
720
721 case X509_V_ERR_CERT_HAS_EXPIRED:
722 msg(M_WARN, "WARNING: Your certificate has expired!");
723 break;
724 }
725}
726#endif
727
728void
729tls_ctx_load_dh_params(struct tls_root_ctx *ctx, const char *dh_file, bool dh_file_inline)
730{
731 BIO *bio;
732
733 ASSERT(NULL != ctx);
734
735 if (dh_file_inline)
736 {
737 if (!(bio = BIO_new_mem_buf((char *)dh_file, -1)))
738 {
739 crypto_msg(M_FATAL, "Cannot open memory BIO for inline DH parameters");
740 }
741 }
742 else
743 {
744 /* Get Diffie Hellman Parameters */
745 if (!(bio = BIO_new_file(dh_file, "r")))
746 {
747 crypto_msg(M_FATAL, "Cannot open %s for DH parameters", dh_file);
748 }
749 }
750
751#if OPENSSL_VERSION_NUMBER >= 0x30000000L
752 EVP_PKEY *dh = PEM_read_bio_Parameters(bio, NULL);
753 BIO_free(bio);
754
755 if (!dh)
756 {
757 crypto_msg(M_FATAL, "Cannot load DH parameters from %s",
758 print_key_filename(dh_file, dh_file_inline));
759 }
760 if (!SSL_CTX_set0_tmp_dh_pkey(ctx->ctx, dh))
761 {
762 crypto_msg(M_FATAL, "SSL_CTX_set0_tmp_dh_pkey");
763 }
764
765 msg(D_TLS_DEBUG_LOW, "Diffie-Hellman initialized with %d bit key", 8 * EVP_PKEY_get_size(dh));
766#else /* if OPENSSL_VERSION_NUMBER >= 0x30000000L */
767 DH *dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
768 BIO_free(bio);
769
770 if (!dh)
771 {
772 crypto_msg(M_FATAL, "Cannot load DH parameters from %s",
773 print_key_filename(dh_file, dh_file_inline));
774 }
775 if (!SSL_CTX_set_tmp_dh(ctx->ctx, dh))
776 {
777 crypto_msg(M_FATAL, "SSL_CTX_set_tmp_dh");
778 }
779
780 msg(D_TLS_DEBUG_LOW, "Diffie-Hellman initialized with %d bit key", 8 * DH_size(dh));
781
782 DH_free(dh);
783#endif /* if OPENSSL_VERSION_NUMBER >= 0x30000000L */
784}
785
786void
787tls_ctx_load_ecdh_params(struct tls_root_ctx *ctx, const char *curve_name)
788{
789#if OPENSSL_VERSION_NUMBER >= 0x30000000L
790 if (curve_name != NULL)
791 {
792 msg(M_WARN, "WARNING: OpenSSL 3.0+ builds do not support specifying an "
793 "ECDH curve with --ecdh-curve, using default curves. Use "
794 "--tls-groups to specify groups.");
795 }
796#elif !defined(OPENSSL_NO_EC)
797 int nid = NID_undef;
798 EC_KEY *ecdh = NULL;
799 const char *sname = NULL;
800
801 /* Generate a new ECDH key for each SSL session (for non-ephemeral ECDH) */
802 SSL_CTX_set_options(ctx->ctx, SSL_OP_SINGLE_ECDH_USE);
803
804 if (curve_name != NULL)
805 {
806 /* Use user supplied curve if given */
807 msg(D_TLS_DEBUG, "Using user specified ECDH curve (%s)", curve_name);
808 nid = OBJ_sn2nid(curve_name);
809 }
810 else
811 {
812 return;
813 }
814
815 /* Translate NID back to name , just for kicks */
816 sname = OBJ_nid2sn(nid);
817 if (sname == NULL)
818 {
819 sname = "(Unknown)";
820 }
821
822 /* Create new EC key and set as ECDH key */
823 if (NID_undef == nid || NULL == (ecdh = EC_KEY_new_by_curve_name(nid)))
824 {
825 /* Creating key failed, fall back on sane default */
826 ecdh = EC_KEY_new_by_curve_name(NID_secp384r1);
827 const char *source =
828 (NULL == curve_name) ? "extract curve from certificate" : "use supplied curve";
829 msg(D_TLS_DEBUG_LOW, "Failed to %s (%s), using secp384r1 instead.", source, sname);
830 sname = OBJ_nid2sn(NID_secp384r1);
831 }
832
833 if (!SSL_CTX_set_tmp_ecdh(ctx->ctx, ecdh))
834 {
835 crypto_msg(M_FATAL, "SSL_CTX_set_tmp_ecdh: cannot add curve");
836 }
837
838 msg(D_TLS_DEBUG_LOW, "ECDH curve %s added", sname);
839
840 EC_KEY_free(ecdh);
841#else /* ifndef OPENSSL_NO_EC */
842 msg(D_LOW, "Your OpenSSL library was built without elliptic curve support."
843 " Skipping ECDH parameter loading.");
844#endif /* OPENSSL_NO_EC */
845}
846
847#if defined(HAVE_OPENSSL_STORE_API)
853static int
854ui_reader(UI *ui, UI_STRING *uis)
855{
856 SSL_CTX *ctx = UI_get0_user_data(ui);
857
858 if (UI_get_string_type(uis) == UIT_PROMPT)
859 {
860 const char *prompt = UI_get0_output_string(uis);
861
862 /* If pkcs#11 Use custom prompt similar to pkcs11-helper */
863 if (strstr(prompt, "PKCS#11"))
864 {
865 struct user_pass up;
866 CLEAR(up);
867 get_user_pass(&up, NULL, "PKCS#11 token",
869 UI_set_result(ui, uis, up.password);
870 purge_user_pass(&up, true);
871 }
872 else /* use our generic 'Private Key' passphrase callback */
873 {
875 pem_password_cb *cb = SSL_CTX_get_default_passwd_cb(ctx);
876 void *d = SSL_CTX_get_default_passwd_cb_userdata(ctx);
877
878 cb(password, sizeof(password), 0, d);
879 UI_set_result(ui, uis, password);
881 }
882
883 return 1;
884 }
885 return 0;
886}
887
888static void
889clear_ossl_store_error(OSSL_STORE_CTX *store_ctx)
890{
891 if (OSSL_STORE_error(store_ctx))
892 {
893 ERR_clear_error();
894 }
895}
896#endif /* defined(HAVE_OPENSSL_STORE_API) */
897
906static void *
907load_pkey_from_uri(const char *uri, SSL_CTX *ssl_ctx)
908{
909 EVP_PKEY *pkey = NULL;
910
911#if !defined(HAVE_OPENSSL_STORE_API)
912
913 /* Treat the uri as file name */
914 BIO *in = BIO_new_file(uri, "r");
915 if (!in)
916 {
917 return NULL;
918 }
919 pkey = PEM_read_bio_PrivateKey(in, NULL, SSL_CTX_get_default_passwd_cb(ssl_ctx),
920 SSL_CTX_get_default_passwd_cb_userdata(ssl_ctx));
921 BIO_free(in);
922
923#else /* defined(HAVE_OPENSSL_STORE_API) */
924
925 OSSL_STORE_CTX *store_ctx = NULL;
926 OSSL_STORE_INFO *info = NULL;
927
928 UI_METHOD *ui_method = UI_create_method("openvpn");
929 if (!ui_method)
930 {
931 msg(M_WARN, "OpenSSL UI creation failed");
932 return NULL;
933 }
934 UI_method_set_reader(ui_method, ui_reader);
935
936 store_ctx = OSSL_STORE_open_ex(uri, tls_libctx, NULL, ui_method, ssl_ctx, NULL, NULL, NULL);
937 if (!store_ctx)
938 {
939 goto end;
940 }
941 if (OSSL_STORE_expect(store_ctx, OSSL_STORE_INFO_PKEY) != 1)
942 {
943 goto end;
944 }
945 while (1)
946 {
947 info = OSSL_STORE_load(store_ctx);
948 if (info || OSSL_STORE_eof(store_ctx))
949 {
950 break;
951 }
952 /* OPENSSL_STORE_load can return error and still have usable objects to follow.
953 * ref: man OPENSSL_STORE_open
954 * Clear error and recurse through the file if info = NULL and eof not reached
955 */
956 clear_ossl_store_error(store_ctx);
957 }
958 if (!info)
959 {
960 goto end;
961 }
962 pkey = OSSL_STORE_INFO_get1_PKEY(info);
963 OSSL_STORE_INFO_free(info);
964 msg(D_TLS_DEBUG_MED, "Found pkey in store using URI: %s", uri);
965
966end:
967 OSSL_STORE_close(store_ctx);
968 UI_destroy_method(ui_method);
969
970#endif /* defined(HAVE_OPENSSL_STORE_API) */
971
972 return pkey;
973}
974
975int
976tls_ctx_load_pkcs12(struct tls_root_ctx *ctx, const char *pkcs12_file, bool pkcs12_file_inline,
977 bool load_ca_file)
978{
979 FILE *fp;
980 EVP_PKEY *pkey;
981 X509 *cert;
982 STACK_OF(X509) *ca = NULL;
983 PKCS12 *p12;
984 char password[256];
985
986 ASSERT(NULL != ctx);
987
988 if (pkcs12_file_inline)
989 {
990 BIO *b64 = BIO_new(BIO_f_base64());
991 BIO *bio = BIO_new_mem_buf((void *)pkcs12_file, (int)strlen(pkcs12_file));
992 ASSERT(b64 && bio);
993 BIO_push(b64, bio);
994 p12 = d2i_PKCS12_bio(b64, NULL);
995 if (!p12)
996 {
997 crypto_msg(M_FATAL, "Error reading inline PKCS#12 file");
998 }
999 BIO_free(b64);
1000 BIO_free(bio);
1001 }
1002 else
1003 {
1004 /* Load the PKCS #12 file */
1005 if (!(fp = platform_fopen(pkcs12_file, "rb")))
1006 {
1007 crypto_msg(M_FATAL, "Error opening file %s", pkcs12_file);
1008 }
1009 p12 = d2i_PKCS12_fp(fp, NULL);
1010 fclose(fp);
1011 if (!p12)
1012 {
1013 crypto_msg(M_FATAL, "Error reading PKCS#12 file %s", pkcs12_file);
1014 }
1015 }
1016
1017 /* Parse the PKCS #12 file */
1018 if (!PKCS12_parse(p12, "", &pkey, &cert, &ca))
1019 {
1020 pem_password_callback(password, sizeof(password) - 1, 0, NULL);
1021 /* Reparse the PKCS #12 file with password */
1022 ca = NULL;
1023 if (!PKCS12_parse(p12, password, &pkey, &cert, &ca))
1024 {
1025 crypto_msg(M_WARN, "Decoding PKCS12 failed. Probably wrong password "
1026 "or unsupported/legacy encryption");
1027#ifdef ENABLE_MANAGEMENT
1028 if (management && (ERR_GET_REASON(ERR_peek_error()) == PKCS12_R_MAC_VERIFY_FAILURE))
1029 {
1031 }
1032#endif
1033 PKCS12_free(p12);
1034 return 1;
1035 }
1036 }
1037 PKCS12_free(p12);
1038
1039 /* Load Certificate */
1040 if (!SSL_CTX_use_certificate(ctx->ctx, cert))
1041 {
1043 crypto_msg(M_FATAL, "Cannot use certificate");
1044 }
1045
1046 /* Load Private Key */
1047 if (!SSL_CTX_use_PrivateKey(ctx->ctx, pkey))
1048 {
1049 crypto_msg(M_FATAL, "Cannot use private key");
1050 }
1051
1052 /* Check Private Key */
1053 if (!SSL_CTX_check_private_key(ctx->ctx))
1054 {
1055 crypto_msg(M_FATAL, "Private key does not match the certificate");
1056 }
1057
1058 /* Set Certificate Verification chain */
1059 if (load_ca_file)
1060 {
1061 /* Add CAs from PKCS12 to the cert store and mark them as trusted.
1062 * They're also used to fill in the chain of intermediate certs as
1063 * necessary.
1064 */
1065 if (ca && sk_X509_num(ca))
1066 {
1067 for (openssl_stack_size_t i = 0; i < sk_X509_num(ca); i++)
1068 {
1069 X509_STORE *cert_store = SSL_CTX_get_cert_store(ctx->ctx);
1070 if (!X509_STORE_add_cert(cert_store, sk_X509_value(ca, i)))
1071 {
1073 "Cannot add certificate to certificate chain (X509_STORE_add_cert)");
1074 }
1075 if (!SSL_CTX_add_client_CA(ctx->ctx, sk_X509_value(ca, i)))
1076 {
1078 "Cannot add certificate to client CA list (SSL_CTX_add_client_CA)");
1079 }
1080 }
1081 }
1082 }
1083 else
1084 {
1085 /* If trusted CA certs were loaded from a PEM file, and we ignore the
1086 * ones in PKCS12, do load PKCS12-provided certs to the client extra
1087 * certs chain just in case they include intermediate CAs needed to
1088 * prove my identity to the other end. This does not make them trusted.
1089 */
1090 if (ca && sk_X509_num(ca))
1091 {
1092 for (openssl_stack_size_t i = 0; i < sk_X509_num(ca); i++)
1093 {
1094 if (!SSL_CTX_add_extra_chain_cert(ctx->ctx, sk_X509_value(ca, i)))
1095 {
1096 crypto_msg(
1097 M_FATAL,
1098 "Cannot add extra certificate to chain (SSL_CTX_add_extra_chain_cert)");
1099 }
1100 }
1101 }
1102 }
1103 return 0;
1104}
1105
1106#ifdef ENABLE_CRYPTOAPI
1107void
1108tls_ctx_load_cryptoapi(struct tls_root_ctx *ctx, const char *cryptoapi_cert)
1109{
1110 ASSERT(NULL != ctx);
1111
1112 /* Load Certificate and Private Key */
1113 if (!SSL_CTX_use_CryptoAPI_certificate(ctx->ctx, cryptoapi_cert))
1114 {
1115 crypto_msg(M_FATAL, "Cannot load certificate \"%s\" from Microsoft Certificate Store",
1116 cryptoapi_cert);
1117 }
1118}
1119#endif /* ENABLE_CRYPTOAPI */
1120
1121static void
1122tls_ctx_add_extra_certs(struct tls_root_ctx *ctx, BIO *bio, bool optional)
1123{
1124 X509 *cert;
1125 while (true)
1126 {
1127 cert = NULL;
1128 if (!PEM_read_bio_X509(bio, &cert, NULL, NULL))
1129 {
1130 /* a PEM_R_NO_START_LINE "Error" indicates that no certificate
1131 * is found in the buffer. If loading more certificates is
1132 * optional, break without raising an error
1133 */
1134 if (optional && ERR_GET_REASON(ERR_peek_error()) == PEM_R_NO_START_LINE)
1135 {
1136 /* remove that error from error stack */
1137 (void)ERR_get_error();
1138 break;
1139 }
1140
1141 /* Otherwise, bail out with error */
1142 crypto_msg(M_FATAL, "Error reading extra certificate");
1143 }
1144 /* takes ownership of cert like a set1 method */
1145 if (SSL_CTX_add_extra_chain_cert(ctx->ctx, cert) != 1)
1146 {
1147 crypto_msg(M_FATAL, "Error adding extra certificate");
1148 }
1149 /* We loaded at least one certificate, so loading more is optional */
1150 optional = true;
1151 }
1152}
1153
1154static bool
1156{
1157#if defined(HAVE_OPENSSL_STORE_API)
1158 return 1;
1159#else
1160 return 0;
1161#endif
1162}
1163
1164static void
1165tls_ctx_load_cert_uri(struct tls_root_ctx *tls_ctx, const char *uri)
1166{
1167#if defined(HAVE_OPENSSL_STORE_API)
1168 X509 *x = NULL;
1169 int ret = 0;
1170 OSSL_STORE_CTX *store_ctx = NULL;
1171 OSSL_STORE_INFO *info = NULL;
1172
1173 ASSERT(NULL != tls_ctx);
1174
1175 UI_METHOD *ui_method = UI_create_method("openvpn");
1176 if (!ui_method)
1177 {
1178 msg(M_WARN, "OpenSSL UI method creation failed");
1179 goto end;
1180 }
1181 UI_method_set_reader(ui_method, ui_reader);
1182
1183 store_ctx =
1184 OSSL_STORE_open_ex(uri, tls_libctx, NULL, ui_method, tls_ctx->ctx, NULL, NULL, NULL);
1185 if (!store_ctx)
1186 {
1187 goto end;
1188 }
1189 if (OSSL_STORE_expect(store_ctx, OSSL_STORE_INFO_CERT) != 1)
1190 {
1191 goto end;
1192 }
1193
1194 while (1)
1195 {
1196 info = OSSL_STORE_load(store_ctx);
1197 if (info || OSSL_STORE_eof(store_ctx))
1198 {
1199 break;
1200 }
1201 /* OPENSSL_STORE_load can return error and still have usable objects to follow.
1202 * ref: man OPENSSL_STORE_open
1203 * Clear error and recurse through the file if info = NULL and eof not reached.
1204 */
1205 clear_ossl_store_error(store_ctx);
1206 }
1207 if (!info)
1208 {
1209 goto end;
1210 }
1211
1212 x = OSSL_STORE_INFO_get0_CERT(info);
1213 if (x == NULL)
1214 {
1215 goto end;
1216 }
1217 msg(D_TLS_DEBUG_MED, "Found cert in store using URI: %s", uri);
1218
1219 ret = SSL_CTX_use_certificate(tls_ctx->ctx, x);
1220 if (!ret)
1221 {
1222 goto end;
1223 }
1224 OSSL_STORE_INFO_free(info);
1225 info = NULL;
1226
1227 /* iterate through the store and add extra certificates if any to the chain */
1228 while (!OSSL_STORE_eof(store_ctx))
1229 {
1230 info = OSSL_STORE_load(store_ctx);
1231 if (!info)
1232 {
1233 clear_ossl_store_error(store_ctx);
1234 continue;
1235 }
1236 x = OSSL_STORE_INFO_get1_CERT(info);
1237 if (x && SSL_CTX_add_extra_chain_cert(tls_ctx->ctx, x) != 1)
1238 {
1239 X509_free(x);
1240 crypto_msg(M_FATAL, "Error adding extra certificate");
1241 break;
1242 }
1243 OSSL_STORE_INFO_free(info);
1244 info = NULL;
1245 }
1246
1247end:
1248 if (!ret)
1249 {
1251 crypto_msg(M_FATAL, "Cannot load certificate from URI <%s>", uri);
1252 }
1253 else
1254 {
1256 }
1257
1258 UI_destroy_method(ui_method);
1259 OSSL_STORE_INFO_free(info);
1260 OSSL_STORE_close(store_ctx);
1261#else /* defined(HAVE_OPENSSL_STORE_API */
1262 ASSERT(0);
1263#endif /* defined(HAVE_OPENSSL_STORE_API */
1264}
1265
1266static void
1267tls_ctx_load_cert_pem_file(struct tls_root_ctx *ctx, const char *cert_file, bool cert_file_inline)
1268{
1269 BIO *in = NULL;
1270 X509 *x = NULL;
1271 int ret = 0;
1272
1273 ASSERT(NULL != ctx);
1274
1275 if (cert_file_inline)
1276 {
1277 in = BIO_new_mem_buf((char *)cert_file, -1);
1278 }
1279 else
1280 {
1281 in = BIO_new_file((char *)cert_file, "r");
1282 }
1283
1284 if (in == NULL)
1285 {
1286 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
1287 goto end;
1288 }
1289
1290 x = PEM_read_bio_X509(in, NULL, SSL_CTX_get_default_passwd_cb(ctx->ctx),
1291 SSL_CTX_get_default_passwd_cb_userdata(ctx->ctx));
1292 if (x == NULL)
1293 {
1294 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_PEM_LIB);
1295 goto end;
1296 }
1297
1298 ret = SSL_CTX_use_certificate(ctx->ctx, x);
1299 if (ret)
1300 {
1301 tls_ctx_add_extra_certs(ctx, in, true);
1302 }
1303
1304end:
1305 if (!ret)
1306 {
1308 if (cert_file_inline)
1309 {
1310 crypto_msg(M_FATAL, "Cannot load inline certificate file");
1311 }
1312 else
1313 {
1314 crypto_msg(M_FATAL, "Cannot load certificate file %s", cert_file);
1315 }
1316 }
1317 else
1318 {
1320 }
1321
1322 BIO_free(in);
1323 X509_free(x);
1324}
1325
1326void
1327tls_ctx_load_cert_file(struct tls_root_ctx *ctx, const char *cert_file, bool cert_file_inline)
1328{
1329 if (cert_uri_supported() && !cert_file_inline)
1330 {
1331 tls_ctx_load_cert_uri(ctx, cert_file);
1332 }
1333 else
1334 {
1335 tls_ctx_load_cert_pem_file(ctx, cert_file, cert_file_inline);
1336 }
1337}
1338
1339int
1340tls_ctx_load_priv_file(struct tls_root_ctx *ctx, const char *priv_key_file,
1341 bool priv_key_file_inline)
1342{
1343 SSL_CTX *ssl_ctx = NULL;
1344 BIO *in = NULL;
1345 EVP_PKEY *pkey = NULL;
1346 int ret = 1;
1347
1348 ASSERT(NULL != ctx);
1349
1350 ssl_ctx = ctx->ctx;
1351
1352 if (priv_key_file_inline)
1353 {
1354 in = BIO_new_mem_buf((char *)priv_key_file, -1);
1355 if (in == NULL)
1356 {
1357 goto end;
1358 }
1359 pkey = PEM_read_bio_PrivateKey(in, NULL, SSL_CTX_get_default_passwd_cb(ctx->ctx),
1360 SSL_CTX_get_default_passwd_cb_userdata(ctx->ctx));
1361 }
1362 else
1363 {
1364 pkey = load_pkey_from_uri(priv_key_file, ssl_ctx);
1365 }
1366
1367 if (!pkey || !SSL_CTX_use_PrivateKey(ssl_ctx, pkey))
1368 {
1369#ifdef ENABLE_MANAGEMENT
1370 if (management && (ERR_GET_REASON(ERR_peek_error()) == EVP_R_BAD_DECRYPT))
1371 {
1373 }
1374#endif
1375 crypto_msg(M_WARN, "Cannot load private key file %s",
1376 print_key_filename(priv_key_file, priv_key_file_inline));
1377 goto end;
1378 }
1379
1380 /* Check Private Key */
1381 if (!SSL_CTX_check_private_key(ssl_ctx))
1382 {
1383 crypto_msg(M_FATAL, "Private key does not match the certificate");
1384 }
1385 ret = 0;
1386
1387end:
1388 EVP_PKEY_free(pkey);
1389 BIO_free(in);
1390 return ret;
1391}
1392
1393void
1394backend_tls_ctx_reload_crl(struct tls_root_ctx *ssl_ctx, const char *crl_file, bool crl_inline)
1395{
1396 BIO *in = NULL;
1397 STACK_OF(X509_CRL) *crls = NULL;
1398
1399 X509_STORE *store = SSL_CTX_get_cert_store(ssl_ctx->ctx);
1400 if (!store)
1401 {
1402 crypto_msg(M_FATAL, "Cannot get certificate store");
1403 }
1404
1405 sk_X509_CRL_pop_free(ssl_ctx->crls, X509_CRL_free);
1406 ssl_ctx->crls = NULL;
1407
1408 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
1409
1410 if (crl_inline)
1411 {
1412 in = BIO_new_mem_buf((char *)crl_file, -1);
1413 }
1414 else
1415 {
1416 in = BIO_new_file(crl_file, "r");
1417 }
1418
1419 if (in == NULL)
1420 {
1421 msg(M_WARN, "CRL: cannot read: %s", print_key_filename(crl_file, crl_inline));
1422 return;
1423 }
1424
1425 crls = sk_X509_CRL_new_null();
1426 if (crls == NULL)
1427 {
1428 crypto_msg(M_FATAL, "CRL: cannot create CRL list");
1429 }
1430
1431 int num_crls_loaded = 0;
1432 while (true)
1433 {
1434 X509_CRL *crl = PEM_read_bio_X509_CRL(in, NULL, NULL, NULL);
1435 if (crl == NULL)
1436 {
1437 /*
1438 * PEM_R_NO_START_LINE can be considered equivalent to EOF.
1439 */
1440 bool eof = ERR_GET_REASON(ERR_peek_error()) == PEM_R_NO_START_LINE;
1441 /* but warn if no CRLs have been loaded */
1442 if (num_crls_loaded > 0 && eof)
1443 {
1444 /* remove that error from error stack */
1445 (void)ERR_get_error();
1446 break;
1447 }
1448
1449 crypto_msg(M_WARN, "CRL: cannot read CRL from file %s",
1450 print_key_filename(crl_file, crl_inline));
1451 break;
1452 }
1453
1454 if (!sk_X509_CRL_push(crls, crl))
1455 {
1456 crypto_msg(M_FATAL, "CRL: cannot add CRL to list");
1457 }
1458 num_crls_loaded++;
1459 }
1460 msg(M_INFO, "CRL: loaded %d CRLs from file %s", num_crls_loaded, crl_file);
1461 ssl_ctx->crls = crls;
1462 BIO_free(in);
1463}
1464
1465
1466#if defined(ENABLE_MANAGEMENT) && !defined(HAVE_XKEY_PROVIDER)
1467
1468/* encrypt */
1469static int
1470rsa_pub_enc(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding)
1471{
1472 ASSERT(0);
1473 return -1;
1474}
1475
1476/* verify arbitrary data */
1477static int
1478rsa_pub_dec(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding)
1479{
1480 ASSERT(0);
1481 return -1;
1482}
1483
1484/* decrypt */
1485static int
1486rsa_priv_dec(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding)
1487{
1488 ASSERT(0);
1489 return -1;
1490}
1491
1492/* called at RSA_free */
1493static int
1495{
1496 /* meth was allocated in tls_ctx_use_management_external_key() ; since
1497 * this function is called when the parent RSA object is destroyed,
1498 * it is no longer used after this point so kill it. */
1499 const RSA_METHOD *meth = RSA_get_method(rsa);
1500 RSA_meth_free((RSA_METHOD *)meth);
1501 return 1;
1502}
1503
1504/*
1505 * Convert OpenSSL's constant to the strings used in the management
1506 * interface query
1507 */
1508const char *
1509get_rsa_padding_name(const int padding)
1510{
1511 switch (padding)
1512 {
1513 case RSA_PKCS1_PADDING:
1514 return "RSA_PKCS1_PADDING";
1515
1516 case RSA_NO_PADDING:
1517 return "RSA_NO_PADDING";
1518
1519 default:
1520 return "UNKNOWN";
1521 }
1522}
1523
1535static int
1536get_sig_from_man(const unsigned char *dgst, unsigned int dgstlen, unsigned char *sig,
1537 unsigned int siglen, const char *algorithm)
1538{
1539 char *in_b64 = NULL;
1540 char *out_b64 = NULL;
1541 int len = -1;
1542
1543 int bencret = openvpn_base64_encode(dgst, dgstlen, &in_b64);
1544
1545 if (management && bencret > 0)
1546 {
1547 out_b64 = management_query_pk_sig(management, in_b64, algorithm);
1548 }
1549 if (out_b64)
1550 {
1551 len = openvpn_base64_decode(out_b64, sig, siglen);
1552 }
1553
1554 free(in_b64);
1555 free(out_b64);
1556 return len;
1557}
1558
1559/* sign arbitrary data */
1560static int
1561rsa_priv_enc(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding)
1562{
1563 int len = RSA_size(rsa);
1564
1565 if (padding != RSA_PKCS1_PADDING && padding != RSA_NO_PADDING)
1566 {
1567 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
1568 return -1;
1569 }
1570
1571 int ret = get_sig_from_man(from, flen, to, len, get_rsa_padding_name(padding));
1572
1573 return (ret == len) ? ret : -1;
1574}
1575
1576static int
1577tls_ctx_use_external_rsa_key(struct tls_root_ctx *ctx, EVP_PKEY *pkey)
1578{
1579 RSA *rsa = NULL;
1580 RSA_METHOD *rsa_meth;
1581
1582 ASSERT(NULL != ctx);
1583
1584 const RSA *pub_rsa = EVP_PKEY_get0_RSA(pkey);
1585 ASSERT(NULL != pub_rsa);
1586
1587 /* allocate custom RSA method object */
1588 rsa_meth = RSA_meth_new("OpenVPN external private key RSA Method", RSA_METHOD_FLAG_NO_CHECK);
1589 check_malloc_return(rsa_meth);
1590 RSA_meth_set_pub_enc(rsa_meth, rsa_pub_enc);
1591 RSA_meth_set_pub_dec(rsa_meth, rsa_pub_dec);
1592 RSA_meth_set_priv_enc(rsa_meth, rsa_priv_enc);
1593 RSA_meth_set_priv_dec(rsa_meth, rsa_priv_dec);
1594 RSA_meth_set_init(rsa_meth, NULL);
1595 RSA_meth_set_finish(rsa_meth, openvpn_extkey_rsa_finish);
1596 RSA_meth_set0_app_data(rsa_meth, NULL);
1597
1598 /* allocate RSA object */
1599 rsa = RSA_new();
1600 if (rsa == NULL)
1601 {
1602 SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_MALLOC_FAILURE);
1603 goto err;
1604 }
1605
1606 /* initialize RSA object */
1607 const BIGNUM *n = NULL;
1608 const BIGNUM *e = NULL;
1609 RSA_get0_key(pub_rsa, &n, &e, NULL);
1610 RSA_set0_key(rsa, BN_dup(n), BN_dup(e), NULL);
1611 RSA_set_flags(rsa, RSA_flags(rsa) | RSA_FLAG_EXT_PKEY);
1612 if (!RSA_set_method(rsa, rsa_meth))
1613 {
1614 RSA_meth_free(rsa_meth);
1615 goto err;
1616 }
1617 /* from this point rsa_meth will get freed with rsa */
1618
1619 /* bind our custom RSA object to ssl_ctx */
1620 if (!SSL_CTX_use_RSAPrivateKey(ctx->ctx, rsa))
1621 {
1622 goto err;
1623 }
1624
1625 RSA_free(rsa); /* doesn't necessarily free, just decrements refcount */
1626 return 1;
1627
1628err:
1629 if (rsa)
1630 {
1631 RSA_free(rsa);
1632 }
1633 else if (rsa_meth)
1634 {
1635 RSA_meth_free(rsa_meth);
1636 }
1637 return 0;
1638}
1639
1640#if !defined(OPENSSL_NO_EC)
1641
1642/* called when EC_KEY is destroyed */
1643static void
1645{
1646 /* release the method structure */
1647 const EC_KEY_METHOD *ec_meth = EC_KEY_get_method(ec);
1648 EC_KEY_METHOD_free((EC_KEY_METHOD *)ec_meth);
1649}
1650
1651/* EC_KEY_METHOD callback: sign().
1652 * Sign the hash using EC key and return DER encoded signature in sig,
1653 * its length in siglen. Return value is 1 on success, 0 on error.
1654 */
1655static int
1656ecdsa_sign(int type, const unsigned char *dgst, int dgstlen, unsigned char *sig,
1657 unsigned int *siglen, const BIGNUM *kinv, const BIGNUM *r, EC_KEY *ec)
1658{
1659 int capacity = (int)ECDSA_size(ec);
1660 /*
1661 * ECDSA does not seem to have proper constants for paddings since
1662 * there are only signatures without padding at the moment, use
1663 * a generic ECDSA for the moment
1664 */
1665 int len = get_sig_from_man(dgst, dgstlen, sig, capacity, "ECDSA");
1666
1667 if (len > 0)
1668 {
1669 *siglen = len;
1670 return 1;
1671 }
1672 return 0;
1673}
1674
1675#ifndef OPENSSL_IS_AWSLC
1676/* EC_KEY_METHOD callback: sign_setup(). We do no precomputations */
1677static int
1678ecdsa_sign_setup(EC_KEY *ec, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
1679{
1680 return 1;
1681}
1682#endif
1683
1684/* EC_KEY_METHOD callback: sign_sig().
1685 * Sign the hash and return the result as a newly allocated ECDS_SIG
1686 * struct or NULL on error.
1687 */
1688static ECDSA_SIG *
1689ecdsa_sign_sig(const unsigned char *dgst, int dgstlen, const BIGNUM *in_kinv, const BIGNUM *in_r,
1690 EC_KEY *ec)
1691{
1692 ECDSA_SIG *ecsig = NULL;
1693 unsigned int len = (unsigned int)ECDSA_size(ec);
1694 struct gc_arena gc = gc_new();
1695
1696 unsigned char *buf = gc_malloc(len, false, &gc);
1697 if (ecdsa_sign(0, dgst, dgstlen, buf, &len, NULL, NULL, ec) != 1)
1698 {
1699 goto out;
1700 }
1701 /* const char ** should be avoided: not up to us, so we cast our way through */
1702 ecsig = d2i_ECDSA_SIG(NULL, (const unsigned char **)&buf, len);
1703
1704out:
1705 gc_free(&gc);
1706 return ecsig;
1707}
1708
1709static int
1710tls_ctx_use_external_ec_key(struct tls_root_ctx *ctx, EVP_PKEY *pkey)
1711{
1712 EC_KEY *ec = NULL;
1713 EVP_PKEY *privkey = NULL;
1714 EC_KEY_METHOD *ec_method;
1715
1716 ASSERT(ctx);
1717
1718 ec_method = EC_KEY_METHOD_new(EC_KEY_OpenSSL());
1719 if (!ec_method)
1720 {
1721 goto err;
1722 }
1723
1724 /* Among init methods, we only need the finish method */
1725 EC_KEY_METHOD_set_init(ec_method, NULL, openvpn_extkey_ec_finish, NULL, NULL, NULL, NULL);
1726#ifdef OPENSSL_IS_AWSLC
1727 EC_KEY_METHOD_set_sign(ec_method, ecdsa_sign, NULL, ecdsa_sign_sig);
1728#else
1729 EC_KEY_METHOD_set_sign(ec_method, ecdsa_sign, ecdsa_sign_setup, ecdsa_sign_sig);
1730#endif
1731
1732 ec = EC_KEY_dup(EVP_PKEY_get0_EC_KEY(pkey));
1733 if (!ec)
1734 {
1735 EC_KEY_METHOD_free(ec_method);
1736 goto err;
1737 }
1738 if (!EC_KEY_set_method(ec, ec_method))
1739 {
1740 EC_KEY_METHOD_free(ec_method);
1741 goto err;
1742 }
1743 /* from this point ec_method will get freed when ec is freed */
1744
1745 privkey = EVP_PKEY_new();
1746 if (!EVP_PKEY_assign_EC_KEY(privkey, ec))
1747 {
1748 goto err;
1749 }
1750 /* from this point ec will get freed when privkey is freed */
1751
1752 if (!SSL_CTX_use_PrivateKey(ctx->ctx, privkey))
1753 {
1754 ec = NULL; /* avoid double freeing it below */
1755 goto err;
1756 }
1757
1758 EVP_PKEY_free(privkey); /* this will down ref privkey and ec */
1759 return 1;
1760
1761err:
1762 /* Reach here only when ec and privkey can be independenly freed */
1763 EVP_PKEY_free(privkey);
1764 EC_KEY_free(ec);
1765 return 0;
1766}
1767#endif /* !defined(OPENSSL_NO_EC) */
1768#endif /* ENABLE_MANAGEMENT && !HAVE_XKEY_PROVIDER */
1769
1770#ifdef ENABLE_MANAGEMENT
1771int
1773{
1774 int ret = 1;
1775
1776 ASSERT(NULL != ctx);
1777
1778 X509 *cert = SSL_CTX_get0_certificate(ctx->ctx);
1779
1780 ASSERT(NULL != cert);
1781
1782 /* get the public key */
1783 EVP_PKEY *pkey = X509_get0_pubkey(cert);
1784 ASSERT(pkey); /* NULL before SSL_CTX_use_certificate() is called */
1785
1786#ifdef HAVE_XKEY_PROVIDER
1787 EVP_PKEY *privkey = xkey_load_management_key(tls_libctx, pkey);
1788 if (!privkey || !SSL_CTX_use_PrivateKey(ctx->ctx, privkey))
1789 {
1790 EVP_PKEY_free(privkey);
1791 goto cleanup;
1792 }
1793 EVP_PKEY_free(privkey);
1794#else /* ifdef HAVE_XKEY_PROVIDER */
1795#if OPENSSL_VERSION_NUMBER < 0x30000000L
1796 if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA)
1797#else /* OPENSSL_VERSION_NUMBER < 0x30000000L */
1798 if (EVP_PKEY_is_a(pkey, "RSA"))
1799#endif /* OPENSSL_VERSION_NUMBER < 0x30000000L */
1800 {
1801 if (!tls_ctx_use_external_rsa_key(ctx, pkey))
1802 {
1803 goto cleanup;
1804 }
1805 }
1806#if !defined(OPENSSL_NO_EC)
1807#if OPENSSL_VERSION_NUMBER < 0x30000000L
1808 else if (EVP_PKEY_id(pkey) == EVP_PKEY_EC)
1809#else /* OPENSSL_VERSION_NUMBER < 0x30000000L */
1810 else if (EVP_PKEY_is_a(pkey, "EC"))
1811#endif /* OPENSSL_VERSION_NUMBER < 0x30000000L */
1812 {
1813 if (!tls_ctx_use_external_ec_key(ctx, pkey))
1814 {
1815 goto cleanup;
1816 }
1817 }
1818 else
1819 {
1820 crypto_msg(M_WARN, "management-external-key requires an RSA or EC certificate");
1821 goto cleanup;
1822 }
1823#else /* !defined(OPENSSL_NO_EC) */
1824 else
1825 {
1826 crypto_msg(M_WARN, "management-external-key requires an RSA certificate");
1827 goto cleanup;
1828 }
1829#endif /* !defined(OPENSSL_NO_EC) */
1830
1831#endif /* HAVE_XKEY_PROVIDER */
1832
1833 ret = 0;
1834cleanup:
1835 if (ret)
1836 {
1837 crypto_msg(M_FATAL, "Cannot enable SSL external private key capability");
1838 }
1839 return ret;
1840}
1841
1842#endif /* ifdef ENABLE_MANAGEMENT */
1843
1844static int
1845sk_x509_name_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
1846{
1847 return X509_NAME_cmp(*a, *b);
1848}
1849
1850void
1851tls_ctx_load_ca(struct tls_root_ctx *ctx, const char *ca_file, bool ca_file_inline,
1852 const char *ca_path, bool tls_server)
1853{
1854 STACK_OF(X509_INFO) *info_stack = NULL;
1855 STACK_OF(X509_NAME) *cert_names = NULL;
1856 X509_LOOKUP *lookup = NULL;
1857 X509_STORE *store = NULL;
1858 BIO *in = NULL;
1859 openssl_stack_size_t added = 0, prev = 0;
1860
1861 ASSERT(NULL != ctx);
1862
1863 store = SSL_CTX_get_cert_store(ctx->ctx);
1864 if (!store)
1865 {
1866 crypto_msg(M_FATAL, "Cannot get certificate store");
1867 }
1868
1869 /* Try to add certificates and CRLs from ca_file */
1870 if (ca_file)
1871 {
1872 if (ca_file_inline)
1873 {
1874 in = BIO_new_mem_buf((char *)ca_file, -1);
1875 }
1876 else
1877 {
1878 in = BIO_new_file(ca_file, "r");
1879 }
1880
1881 if (in)
1882 {
1883 info_stack = PEM_X509_INFO_read_bio(in, NULL, NULL, NULL);
1884 }
1885
1886 if (info_stack)
1887 {
1888 for (openssl_stack_size_t i = 0; i < sk_X509_INFO_num(info_stack); i++)
1889 {
1890 X509_INFO *info = sk_X509_INFO_value(info_stack, i);
1891 if (info->crl)
1892 {
1893 X509_STORE_add_crl(store, info->crl);
1894 }
1895
1896 if (tls_server && !info->x509)
1897 {
1898 crypto_msg(M_FATAL, "X509 name was missing in TLS mode");
1899 }
1900
1901 if (info->x509)
1902 {
1903 X509_STORE_add_cert(store, info->x509);
1904 added++;
1905
1906 if (!tls_server)
1907 {
1908 continue;
1909 }
1910
1911 /* Use names of CAs as a client CA list */
1912 if (cert_names == NULL)
1913 {
1914 cert_names = sk_X509_NAME_new(sk_x509_name_cmp);
1915 if (!cert_names)
1916 {
1917 continue;
1918 }
1919 }
1920
1921 /* OpenSSL 4.0 has made X509_get_subject_name return const
1922 * but not adjusted the other functions to take const
1923 * arguments, and other libraries do not have const
1924 * arguments, so just ignore const here */
1925 X509_NAME *xn = (X509_NAME *)X509_get_subject_name(info->x509);
1926 if (!xn)
1927 {
1928 continue;
1929 }
1930
1931
1932 /* Don't add duplicate CA names */
1933 if (sk_X509_NAME_find(cert_names, (X509_NAME *)xn) == -1)
1934 {
1935 X509_NAME *xn_dup = X509_NAME_dup(xn);
1936 if (!xn_dup)
1937 {
1938 continue;
1939 }
1940 sk_X509_NAME_push(cert_names, xn_dup);
1941 }
1942 }
1943
1944 if (tls_server)
1945 {
1946 openssl_stack_size_t cnum = sk_X509_NAME_num(cert_names);
1947 if (cnum != (prev + 1))
1948 {
1950 "Cannot load CA certificate file %s (entry %" PRI_OPENSSL_STACK " did not validate)",
1951 print_key_filename(ca_file, ca_file_inline), added);
1952 }
1953 prev = cnum;
1954 }
1955 }
1956 sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
1957 }
1959 if (tls_server)
1960 {
1961 cnum = sk_X509_NAME_num(cert_names);
1962 SSL_CTX_set_client_CA_list(ctx->ctx, cert_names);
1963 }
1964
1965 if (!added)
1966 {
1967 crypto_msg(M_FATAL, "Cannot load CA certificate file %s (no entries were read)",
1968 print_key_filename(ca_file, ca_file_inline));
1969 }
1970
1971 if (tls_server)
1972 {
1973 if (cnum != added)
1974 {
1976 "Cannot load CA certificate file %s (only %" PRI_OPENSSL_STACK
1977 "of %" PRI_OPENSSL_STACK "entries were valid X509 names)",
1978 print_key_filename(ca_file, ca_file_inline), cnum, added);
1979 }
1980 }
1981
1982 BIO_free(in);
1983 }
1984
1985 /* Set a store for certs (CA & CRL) with a lookup on the "capath" hash directory */
1986 if (ca_path)
1987 {
1988 lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
1989 if (lookup && X509_LOOKUP_add_dir(lookup, ca_path, X509_FILETYPE_PEM))
1990 {
1991 msg(M_WARN, "WARNING: experimental option --capath %s", ca_path);
1992 }
1993 else
1994 {
1995 crypto_msg(M_FATAL, "Cannot add lookup at --capath %s", ca_path);
1996 }
1997 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
1998 }
1999}
2000
2001void
2002tls_ctx_load_extra_certs(struct tls_root_ctx *ctx, const char *extra_certs_file,
2003 bool extra_certs_file_inline)
2004{
2005 BIO *in;
2006 if (extra_certs_file_inline)
2007 {
2008 in = BIO_new_mem_buf((char *)extra_certs_file, -1);
2009 }
2010 else
2011 {
2012 in = BIO_new_file(extra_certs_file, "r");
2013 }
2014
2015 if (in == NULL)
2016 {
2017 crypto_msg(M_FATAL, "Cannot load extra-certs file: %s",
2018 print_key_filename(extra_certs_file, extra_certs_file_inline));
2019 }
2020 else
2021 {
2022 tls_ctx_add_extra_certs(ctx, in, false);
2023 }
2024
2025 BIO_free(in);
2026}
2027
2028/* **************************************
2029 *
2030 * Key-state specific functions
2031 *
2032 ***************************************/
2033/*
2034 *
2035 * BIO functions
2036 *
2037 */
2038
2039#ifdef BIO_DEBUG
2040
2041#warning BIO_DEBUG defined
2042
2043static FILE *biofp; /* GLOBAL */
2044static bool biofp_toggle; /* GLOBAL */
2045static time_t biofp_last_open; /* GLOBAL */
2046static const int biofp_reopen_interval = 600; /* GLOBAL */
2047
2048static void
2049close_biofp(void)
2050{
2051 if (biofp)
2052 {
2053 ASSERT(!fclose(biofp));
2054 biofp = NULL;
2055 }
2056}
2057
2058static void
2059open_biofp(void)
2060{
2061 const time_t current = time(NULL);
2062 const pid_t pid = getpid();
2063
2064 if (biofp_last_open + biofp_reopen_interval < current)
2065 {
2066 close_biofp();
2067 }
2068 if (!biofp)
2069 {
2070 char fn[256];
2071 snprintf(fn, sizeof(fn), "bio/%d-%d.log", pid, biofp_toggle);
2072 biofp = fopen(fn, "w");
2073 ASSERT(biofp);
2074 biofp_last_open = time(NULL);
2075 biofp_toggle ^= 1;
2076 }
2077}
2078
2079static void
2080bio_debug_data(const char *mode, BIO *bio, const uint8_t *buf, int len, const char *desc)
2081{
2082 struct gc_arena gc = gc_new();
2083 if (len > 0)
2084 {
2085 open_biofp();
2086 fprintf(biofp, "BIO_%s %s time=%" PRIi64 " bio=" ptr_format " len=%d data=%s\n", mode, desc,
2087 (int64_t)time(NULL), (ptr_type)bio, len, format_hex(buf, len, 0, &gc));
2088 fflush(biofp);
2089 }
2090 gc_free(&gc);
2091}
2092
2093static void
2094bio_debug_oc(const char *mode, BIO *bio)
2095{
2096 open_biofp();
2097 fprintf(biofp, "BIO %s time=%" PRIi64 " bio=" ptr_format "\n", mode, (int64_t)time(NULL),
2098 (ptr_type)bio);
2099 fflush(biofp);
2100}
2101
2102#endif /* ifdef BIO_DEBUG */
2103
2104/*
2105 * Write to an OpenSSL BIO in non-blocking mode.
2106 */
2107static int
2108bio_write(BIO *bio, const uint8_t *data, int size, const char *desc)
2109{
2110 int i;
2111 int ret = 0;
2112 ASSERT(size >= 0);
2113 if (size)
2114 {
2115 /*
2116 * Free the L_TLS lock prior to calling BIO routines
2117 * so that foreground thread can still call
2118 * tls_pre_decrypt or tls_pre_encrypt,
2119 * allowing tunnel packet forwarding to continue.
2120 */
2121#ifdef BIO_DEBUG
2122 bio_debug_data("write", bio, data, size, desc);
2123#endif
2124 i = BIO_write(bio, data, size);
2125
2126 if (i < 0)
2127 {
2128 if (!BIO_should_retry(bio))
2129 {
2130 crypto_msg(D_TLS_ERRORS, "TLS ERROR: BIO write %s error", desc);
2131 ret = -1;
2132 ERR_clear_error();
2133 }
2134 }
2135 else if (i != size)
2136 {
2137 crypto_msg(D_TLS_ERRORS, "TLS ERROR: BIO write %s incomplete %d/%d", desc, i, size);
2138 ret = -1;
2139 ERR_clear_error();
2140 }
2141 else
2142 { /* successful write */
2143 dmsg(D_HANDSHAKE_VERBOSE, "BIO write %s %d bytes", desc, i);
2144 ret = 1;
2145 }
2146 }
2147 return ret;
2148}
2149
2150/*
2151 * Inline functions for reading from and writing
2152 * to BIOs.
2153 */
2154
2155static void
2156bio_write_post(const int status, struct buffer *buf)
2157{
2158 /* success status return from bio_write? */
2159 if (status == 1)
2160 {
2161 memset(BPTR(buf), 0, BLENZ(buf)); /* erase data just written */
2162 buf->len = 0;
2163 }
2164}
2165
2166/*
2167 * Read from an OpenSSL BIO in non-blocking mode.
2168 */
2169static int
2170bio_read(BIO *bio, struct buffer *buf, const char *desc)
2171{
2172 ASSERT(buf->len >= 0);
2173 if (buf->len)
2174 {
2175 /* we only want to write empty buffers, ignore read request
2176 * if the buffer is not empty */
2177 return 0;
2178 }
2179 int len = buf_forward_capacity(buf);
2180
2181 /*
2182 * BIO_read brackets most of the serious RSA
2183 * key negotiation number crunching.
2184 */
2185 int i = BIO_read(bio, BPTR(buf), len);
2186
2187 VALGRIND_MAKE_READABLE((void *)&i, sizeof(i));
2188
2189#ifdef BIO_DEBUG
2190 bio_debug_data("read", bio, BPTR(buf), i, desc);
2191#endif
2192
2193 int ret = 0;
2194 if (i < 0)
2195 {
2196 if (!BIO_should_retry(bio))
2197 {
2198 crypto_msg(D_TLS_ERRORS, "TLS_ERROR: BIO read %s error", desc);
2199 buf->len = 0;
2200 ret = -1;
2201 ERR_clear_error();
2202 }
2203 }
2204 else if (!i)
2205 {
2206 buf->len = 0;
2207 }
2208 else
2209 { /* successful read */
2210 dmsg(D_HANDSHAKE_VERBOSE, "BIO read %s %d bytes", desc, i);
2211 buf->len = i;
2212 ret = 1;
2213 VALGRIND_MAKE_READABLE((void *)BPTR(buf), BLEN(buf));
2214 }
2215 return ret;
2216}
2217
2218void
2219key_state_ssl_init(struct key_state_ssl *ks_ssl, const struct tls_root_ctx *ssl_ctx, bool is_server,
2220 struct tls_session *session)
2221{
2222 ASSERT(NULL != ssl_ctx);
2223 ASSERT(ks_ssl);
2224 CLEAR(*ks_ssl);
2225
2226 ks_ssl->ssl = SSL_new(ssl_ctx->ctx);
2227 if (!ks_ssl->ssl)
2228 {
2229 crypto_msg(M_FATAL, "SSL_new failed");
2230 }
2231
2232 /* put session * in ssl object so we can access it
2233 * from verify callback*/
2234 SSL_set_ex_data(ks_ssl->ssl, mydata_index, session);
2235
2236 ASSERT((ks_ssl->ssl_bio = BIO_new(BIO_f_ssl())));
2237 ASSERT((ks_ssl->ct_in = BIO_new(BIO_s_mem())));
2238 ASSERT((ks_ssl->ct_out = BIO_new(BIO_s_mem())));
2239
2240#ifdef BIO_DEBUG
2241 bio_debug_oc("open ssl_bio", ks_ssl->ssl_bio);
2242 bio_debug_oc("open ct_in", ks_ssl->ct_in);
2243 bio_debug_oc("open ct_out", ks_ssl->ct_out);
2244#endif
2245
2246 if (is_server)
2247 {
2248 SSL_set_accept_state(ks_ssl->ssl);
2249 }
2250 else
2251 {
2252 SSL_set_connect_state(ks_ssl->ssl);
2253 }
2254
2255 SSL_set_bio(ks_ssl->ssl, ks_ssl->ct_in, ks_ssl->ct_out);
2256 BIO_set_ssl(ks_ssl->ssl_bio, ks_ssl->ssl, BIO_NOCLOSE);
2257}
2258
2259void
2261{
2262 SSL_set_shutdown(ks_ssl->ssl, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
2263}
2264
2265void
2267{
2268 if (ks_ssl->ssl)
2269 {
2270#ifdef BIO_DEBUG
2271 bio_debug_oc("close ssl_bio", ks_ssl->ssl_bio);
2272 bio_debug_oc("close ct_in", ks_ssl->ct_in);
2273 bio_debug_oc("close ct_out", ks_ssl->ct_out);
2274#endif
2275 BIO_free_all(ks_ssl->ssl_bio);
2276 SSL_free(ks_ssl->ssl);
2277 }
2278}
2279
2280int
2282{
2283 int ret = 0;
2284
2285 ASSERT(NULL != ks_ssl);
2286
2287 ret = bio_write(ks_ssl->ssl_bio, BPTR(buf), BLEN(buf), "tls_write_plaintext");
2288 bio_write_post(ret, buf);
2289
2290 return ret;
2291}
2292
2293int
2294key_state_write_plaintext_const(struct key_state_ssl *ks_ssl, const uint8_t *data, int len)
2295{
2296 int ret = 0;
2297
2298 ASSERT(NULL != ks_ssl);
2299
2300 ret = bio_write(ks_ssl->ssl_bio, data, len, "tls_write_plaintext_const");
2301
2302 return ret;
2303}
2304
2305int
2307{
2308 int ret = 0;
2309
2310 ASSERT(NULL != ks_ssl);
2311
2312 ret = bio_read(ks_ssl->ct_out, buf, "tls_read_ciphertext");
2313
2314 return ret;
2315}
2316
2317int
2319{
2320 int ret = 0;
2321
2322 ASSERT(NULL != ks_ssl);
2323
2324 ret = bio_write(ks_ssl->ct_in, BPTR(buf), BLEN(buf), "tls_write_ciphertext");
2325 bio_write_post(ret, buf);
2326
2327 return ret;
2328}
2329
2330int
2332{
2333 int ret = 0;
2334
2335 ASSERT(NULL != ks_ssl);
2336
2337 ret = bio_read(ks_ssl->ssl_bio, buf, "tls_read_plaintext");
2338
2339 return ret;
2340}
2341
2342static void
2343print_pkey_details(EVP_PKEY *pkey, char *buf, size_t buflen)
2344{
2345 const char *curve = "";
2346 const char *type = "(error getting type)";
2347
2348 if (pkey == NULL)
2349 {
2350 buf[0] = 0;
2351 return;
2352 }
2353
2354 int typeid = EVP_PKEY_id(pkey);
2355#if OPENSSL_VERSION_NUMBER < 0x30000000L
2356 bool is_ec = typeid == EVP_PKEY_EC;
2357#else
2358 bool is_ec = EVP_PKEY_is_a(pkey, "EC");
2359#endif
2360
2361#ifndef OPENSSL_NO_EC
2362 char groupname[64];
2363 if (is_ec)
2364 {
2365 size_t len;
2366 if (EVP_PKEY_get_group_name(pkey, groupname, sizeof(groupname), &len))
2367 {
2368 curve = groupname;
2369 }
2370 else
2371 {
2372 curve = "(error getting curve name)";
2373 }
2374 }
2375#endif
2376 if (typeid != 0)
2377 {
2378#if OPENSSL_VERSION_NUMBER < 0x30000000L
2379 type = OBJ_nid2sn(typeid);
2380
2381 /* OpenSSL reports rsaEncryption, dsaEncryption and
2382 * id-ecPublicKey, map these values to nicer ones */
2383 if (typeid == EVP_PKEY_RSA)
2384 {
2385 type = "RSA";
2386 }
2387 else if (typeid == EVP_PKEY_DSA)
2388 {
2389 type = "DSA";
2390 }
2391 else if (typeid == EVP_PKEY_EC)
2392 {
2393 /* EC gets the curve appended after the type */
2394 type = "EC, curve ";
2395 }
2396 else if (type == NULL)
2397 {
2398 type = "unknown type";
2399 }
2400#else /* OpenSSL >= 3 */
2401 type = EVP_PKEY_get0_type_name(pkey);
2402 if (type == NULL)
2403 {
2404 type = "(error getting public key type)";
2405 }
2406#endif /* if OPENSSL_VERSION_NUMBER < 0x30000000L */
2407 }
2408
2409 snprintf(buf, buflen, "%d bits %s%s", EVP_PKEY_bits(pkey), type, curve);
2410}
2411
2418static void
2419print_cert_details(X509 *cert, char *buf, size_t buflen)
2420{
2421 EVP_PKEY *pkey = X509_get_pubkey(cert);
2422 char pkeybuf[64] = { 0 };
2423 print_pkey_details(pkey, pkeybuf, sizeof(pkeybuf));
2424
2425 char sig[128] = { 0 };
2426 int signature_nid = X509_get_signature_nid(cert);
2427 if (signature_nid != 0)
2428 {
2429 snprintf(sig, sizeof(sig), ", signature: %s", OBJ_nid2sn(signature_nid));
2430 }
2431
2432 snprintf(buf, buflen, ", peer certificate: %s%s", pkeybuf, sig);
2433
2434 EVP_PKEY_free(pkey);
2435}
2436
2437static void
2438print_server_tempkey(SSL *ssl, char *buf, size_t buflen)
2439{
2440 EVP_PKEY *pkey = NULL;
2441 SSL_get_peer_tmp_key(ssl, &pkey);
2442 if (!pkey)
2443 {
2444 return;
2445 }
2446
2447 char pkeybuf[128] = { 0 };
2448 print_pkey_details(pkey, pkeybuf, sizeof(pkeybuf));
2449
2450 snprintf(buf, buflen, ", peer temporary key: %s", pkeybuf);
2451
2452 EVP_PKEY_free(pkey);
2453}
2454
2455#if !defined(LIBRESSL_VERSION_NUMBER) \
2456 || (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER >= 0x3090000fL)
2462static const char *
2464{
2465 /* Fix a few OpenSSL names to be better understandable */
2466 switch (nid)
2467 {
2468 case EVP_PKEY_RSA:
2469 /* will otherwise say rsaEncryption */
2470 return "RSA";
2471
2472 case EVP_PKEY_DSA:
2473 /* dsaEncryption otherwise */
2474 return "DSA";
2475
2476 case EVP_PKEY_EC:
2477 /* will say id-ecPublicKey */
2478 return "ECDSA";
2479
2480 case -1:
2481 return "(error getting name)";
2482
2483 default:
2484 {
2485 const char *type = OBJ_nid2sn(nid);
2486 if (!type)
2487 {
2488 /* This is unlikely to ever happen as OpenSSL is unlikely to
2489 * return an NID it cannot resolve itself but we silence
2490 * linter/code checkers here */
2491 type = "(error getting name, OBJ_nid2sn failed)";
2492 }
2493 return type;
2494 }
2495 }
2496}
2497#endif /* ifndef LIBRESSL_VERSION_NUMBER */
2498
2503static void
2504print_peer_signature(SSL *ssl, char *buf, size_t buflen)
2505{
2506 int peer_sig_type_nid = NID_undef;
2507 const char *peer_sig_unknown = "unknown";
2508 const char *peer_sig = peer_sig_unknown;
2509 const char *peer_sig_type = "unknown type";
2510
2511 const char *signame = NULL;
2513 if (signame)
2514 {
2515 peer_sig = signame;
2516 }
2517
2518#if !defined(LIBRESSL_VERSION_NUMBER) \
2519 || (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER >= 0x3090000fL)
2520 /* LibreSSL 3.7.x and 3.8.x implement this function but do not export it
2521 * and fail linking with an unresolved symbol */
2522 if (SSL_get_peer_signature_type_nid(ssl, &peer_sig_type_nid) && peer_sig_type_nid != NID_undef)
2523 {
2524 peer_sig_type = get_sigtype(peer_sig_type_nid);
2525 }
2526#endif
2527
2528 if (peer_sig == peer_sig_unknown && peer_sig_type_nid == NID_undef)
2529 {
2530 return;
2531 }
2532
2533 snprintf(buf, buflen, ", peer signing digest/type: %s %s", peer_sig, peer_sig_type);
2534}
2535
2536#if OPENSSL_VERSION_NUMBER >= 0x30000000L
2537void
2538print_tls_key_agreement_group(SSL *ssl, char *buf, size_t buflen)
2539{
2540 const char *groupname = SSL_get0_group_name(ssl);
2541 if (!groupname)
2542 {
2543 snprintf(buf, buflen, ", key agreement: (error fetching group)");
2544 }
2545 else
2546 {
2547 snprintf(buf, buflen, ", key agreement: %s", groupname);
2548 }
2549}
2550#endif
2551
2552/* **************************************
2553 *
2554 * Information functions
2555 *
2556 * Print information for the end user.
2557 *
2558 ***************************************/
2559void
2560print_details(struct key_state_ssl *ks_ssl, const char *prefix)
2561{
2562 const SSL_CIPHER *ciph;
2563 char s1[256];
2564 char s2[256];
2565 char s3[256];
2566 char s4[256];
2567 char s5[256];
2568
2569 s1[0] = s2[0] = s3[0] = s4[0] = s5[0] = 0;
2570 ciph = SSL_get_current_cipher(ks_ssl->ssl);
2571 snprintf(s1, sizeof(s1), "%s %s, cipher %s %s", prefix, SSL_get_version(ks_ssl->ssl),
2572 SSL_CIPHER_get_version(ciph), SSL_CIPHER_get_name(ciph));
2573 X509 *cert = SSL_get_peer_certificate(ks_ssl->ssl);
2574
2575 if (cert)
2576 {
2577 print_cert_details(cert, s2, sizeof(s2));
2578 X509_free(cert);
2579 }
2580 print_server_tempkey(ks_ssl->ssl, s3, sizeof(s3));
2581 print_peer_signature(ks_ssl->ssl, s4, sizeof(s4));
2582#if OPENSSL_VERSION_NUMBER >= 0x30000000L
2583 print_tls_key_agreement_group(ks_ssl->ssl, s5, sizeof(s5));
2584#endif
2585
2586 msg(D_HANDSHAKE, "%s%s%s%s%s", s1, s2, s3, s4, s5);
2587}
2588
2589void
2590show_available_tls_ciphers_list(const char *cipher_list, const char *tls_cert_profile, bool tls13)
2591{
2592 struct tls_root_ctx tls_ctx;
2593
2594 tls_ctx.ctx = SSL_CTX_new(SSLv23_method());
2595 if (!tls_ctx.ctx)
2596 {
2597 crypto_msg(M_FATAL, "Cannot create SSL_CTX object");
2598 }
2599
2600#if defined(TLS1_3_VERSION)
2601 if (tls13)
2602 {
2603 SSL_CTX_set_min_proto_version(tls_ctx.ctx, TLS1_3_VERSION);
2604 tls_ctx_restrict_ciphers_tls13(&tls_ctx, cipher_list);
2605 }
2606 else
2607#endif
2608 {
2609 SSL_CTX_set_max_proto_version(tls_ctx.ctx, TLS1_2_VERSION);
2610 tls_ctx_restrict_ciphers(&tls_ctx, cipher_list);
2611 }
2612
2613 tls_ctx_set_cert_profile(&tls_ctx, tls_cert_profile);
2614
2615 SSL *ssl = SSL_new(tls_ctx.ctx);
2616 if (!ssl)
2617 {
2618 crypto_msg(M_FATAL, "Cannot create SSL object");
2619 }
2620
2621#if OPENSSL_VERSION_NUMBER < 0x1010000fL || defined(OPENSSL_IS_AWSLC) || defined(ENABLE_CRYPTO_WOLFSSL)
2622 STACK_OF(SSL_CIPHER) *sk = SSL_get_ciphers(ssl);
2623#else
2624 STACK_OF(SSL_CIPHER) *sk = SSL_get1_supported_ciphers(ssl);
2625#endif
2626 for (openssl_stack_size_t i = 0; i < sk_SSL_CIPHER_num(sk); i++)
2627 {
2628 const SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, i);
2629
2630 const char *cipher_name = SSL_CIPHER_get_name(c);
2631
2632 const tls_cipher_name_pair *pair =
2633 tls_get_cipher_name_pair(cipher_name, strlen(cipher_name));
2634
2635 if (tls13)
2636 {
2637 printf("%s\n", cipher_name);
2638 }
2639 else if (NULL == pair)
2640 {
2641 /* No translation found, print warning */
2642 printf("%s (No IANA name known to OpenVPN, use OpenSSL name.)\n", cipher_name);
2643 }
2644 else
2645 {
2646 printf("%s\n", pair->iana_name);
2647 }
2648 }
2649#if (OPENSSL_VERSION_NUMBER >= 0x1010000fL)
2650 sk_SSL_CIPHER_free(sk);
2651#endif
2652 SSL_free(ssl);
2653 SSL_CTX_free(tls_ctx.ctx);
2654}
2655
2656/*
2657 * Show the Elliptic curves that are available for us to use
2658 * in the OpenSSL library.
2659 */
2660void
2662{
2663 printf("Consider using 'openssl ecparam -list_curves' as alternative to running\n"
2664 "this command.\n"
2665 "Note this output does only list curves/groups that OpenSSL considers as\n"
2666 "builtin EC curves. It does not list additional curves nor X448 or X25519\n");
2667#ifndef OPENSSL_NO_EC
2668 EC_builtin_curve *curves = NULL;
2669 size_t crv_len = 0;
2670 size_t n = 0;
2671
2672 crv_len = EC_get_builtin_curves(NULL, 0);
2673 ALLOC_ARRAY(curves, EC_builtin_curve, crv_len);
2674 if (EC_get_builtin_curves(curves, crv_len))
2675 {
2676 printf("\nAvailable Elliptic curves/groups:\n");
2677 for (n = 0; n < crv_len; n++)
2678 {
2679 const char *sname;
2680 sname = OBJ_nid2sn(curves[n].nid);
2681 if (sname == NULL)
2682 {
2683 sname = "";
2684 }
2685
2686 printf("%s\n", sname);
2687 }
2688 }
2689 else
2690 {
2691 crypto_msg(M_FATAL, "Cannot get list of builtin curves");
2692 }
2693 free(curves);
2694#else /* ifndef OPENSSL_NO_EC */
2695 msg(M_WARN, "Your OpenSSL library was built without elliptic curve support. "
2696 "No curves available.");
2697#endif /* ifndef OPENSSL_NO_EC */
2698}
2699
2700const char *
2702{
2703 return OpenSSL_version(OPENSSL_VERSION);
2704}
2705
2706
2708#ifdef HAVE_XKEY_PROVIDER
2709static int
2710provider_load(OSSL_PROVIDER *prov, void *dest_libctx)
2711{
2712 const char *name = OSSL_PROVIDER_get0_name(prov);
2713 OSSL_PROVIDER_load(dest_libctx, name);
2714 return 1;
2715}
2716
2717static int
2718provider_unload(OSSL_PROVIDER *prov, void *unused)
2719{
2720 (void)unused;
2721 OSSL_PROVIDER_unload(prov);
2722 return 1;
2723}
2724#endif /* HAVE_XKEY_PROVIDER */
2725
2733void
2735{
2736#ifdef HAVE_XKEY_PROVIDER
2737
2738 /* Make a new library context for use in TLS context */
2739 if (!tls_libctx)
2740 {
2741 tls_libctx = OSSL_LIB_CTX_new();
2743
2744 /* Load all providers in default LIBCTX into this libctx.
2745 * OpenSSL has a child libctx functionality to automate this,
2746 * but currently that is usable only from within providers.
2747 * So we do something close to it manually here.
2748 */
2749 OSSL_PROVIDER_do_all(NULL, provider_load, tls_libctx);
2750 }
2751
2752 if (!OSSL_PROVIDER_available(tls_libctx, "ovpn.xkey"))
2753 {
2754 OSSL_PROVIDER_add_builtin(tls_libctx, "ovpn.xkey", xkey_provider_init);
2755 if (!OSSL_PROVIDER_load(tls_libctx, "ovpn.xkey"))
2756 {
2757 msg(M_NONFATAL, "ERROR: failed loading external key provider: "
2758 "Signing with external keys will not work.");
2759 }
2760 }
2761
2762 /* We only implement minimal functionality in ovpn.xkey, so we do not want
2763 * methods in xkey to be picked unless absolutely required (i.e, when the key
2764 * is external). Ensure this by setting a default propquery for the custom
2765 * libctx that unprefers, but does not forbid, ovpn.xkey. See also man page
2766 * of "property" in OpenSSL 3.0.
2767 */
2768 EVP_set_default_properties(tls_libctx, "?provider!=ovpn.xkey");
2769
2770#endif /* HAVE_XKEY_PROVIDER */
2771}
2772
2776static void
2778{
2779#ifdef HAVE_XKEY_PROVIDER
2780 if (tls_libctx)
2781 {
2782 OSSL_PROVIDER_do_all(tls_libctx, provider_unload, NULL);
2783 OSSL_LIB_CTX_free(tls_libctx);
2784 }
2785#endif /* HAVE_XKEY_PROVIDER */
2786 tls_libctx = NULL;
2787}
2788
2789#endif /* defined(ENABLE_CRYPTO_OPENSSL) */
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
Definition buffer.c:341
char * string_alloc(const char *str, struct gc_arena *gc)
Definition buffer.c:653
#define BPTR(buf)
Definition buffer.h:123
#define ALLOC_ARRAY_CLEAR_GC(dptr, type, n, gc)
Definition buffer.h:1110
static int buf_forward_capacity(const struct buffer *buf)
Definition buffer.h:540
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
Definition buffer.h:415
#define BLEN(buf)
Definition buffer.h:126
static char * format_hex(const uint8_t *data, size_t size, size_t maxoutput, struct gc_arena *gc)
Definition buffer.h:504
#define BLENZ(buf)
Definition buffer.h:127
static void check_malloc_return(void *p)
Definition buffer.h:1131
static void gc_free(struct gc_arena *a)
Definition buffer.h:1049
#define ALLOC_ARRAY(dptr, type, n)
Definition buffer.h:1094
static struct gc_arena gc_new(void)
Definition buffer.h:1041
unsigned long ptr_type
Definition common.h:59
#define ptr_format
Definition common.h:50
char * strsep(char **stringp, const char *delim)
const char * print_key_filename(const char *str, bool is_inline)
To be used when printing a string that may contain inline data.
Definition crypto.c:1279
void crypto_print_openssl_errors(const unsigned int flags)
Retrieve any occurred OpenSSL errors and print those errors.
#define crypto_msg(flags,...)
Retrieve any OpenSSL errors, then print the supplied error message.
int SSL_CTX_use_CryptoAPI_certificate(SSL_CTX *ssl_ctx, const char *cert_prop)
Definition cryptoapi.c:58
#define D_TLS_DEBUG_LOW
Definition errlevel.h:76
#define D_TLS_DEBUG_MED
Definition errlevel.h:156
#define D_HANDSHAKE_VERBOSE
Definition errlevel.h:155
#define D_HANDSHAKE
Definition errlevel.h:71
#define D_TLS_ERRORS
Definition errlevel.h:58
#define D_LOW
Definition errlevel.h:96
#define M_INFO
Definition errlevel.h:54
#define D_TLS_DEBUG
Definition errlevel.h:164
#define KS_PRIMARY
Primary key state index.
Definition ssl_common.h:465
int key_state_read_plaintext(struct key_state_ssl *ks_ssl, struct buffer *buf)
Extract plaintext data from the TLS module.
int key_state_write_ciphertext(struct key_state_ssl *ks_ssl, struct buffer *buf)
Insert a ciphertext buffer into the TLS module.
int key_state_read_ciphertext(struct key_state_ssl *ks_ssl, struct buffer *buf)
Extract ciphertext data from the TLS module.
int key_state_write_plaintext_const(struct key_state_ssl *ks_ssl, const uint8_t *data, int len)
Insert plaintext data into the TLS module.
int key_state_write_plaintext(struct key_state_ssl *ks_ssl, struct buffer *buf)
Insert a plaintext buffer into the TLS module.
int verify_callback(void *session_obj, mbedtls_x509_crt *cert, int cert_depth, uint32_t *flags)
Verify that the remote OpenVPN peer's certificate allows setting up a VPN tunnel.
static int constrain_int(int x, int min, int max)
Definition integer.h:118
static SERVICE_STATUS status
Definition interactive.c:51
void management_auth_failure(struct management *man, const char *type, const char *reason)
Definition manage.c:3212
char * management_query_pk_sig(struct management *man, const char *b64_data, const char *algorithm)
Definition manage.c:3879
#define VALGRIND_MAKE_READABLE(addr, len)
Definition memdbg.h:53
void purge_user_pass(struct user_pass *up, const bool force)
Definition misc.c:474
#define USER_PASS_LEN
Definition misc.h:67
#define GET_USER_PASS_MANAGEMENT
Definition misc.h:113
#define GET_USER_PASS_PASSWORD_ONLY
Definition misc.h:115
static bool get_user_pass(struct user_pass *up, const char *auth_file, const char *prefix, const unsigned int flags)
Retrieves the user credentials from various sources depending on the flags.
Definition misc.h:155
OpenSSL compatibility stub.
void OSSL_PROVIDER
static int SSL_get0_peer_signature_name(SSL *ssl, const char **sigalg)
int openssl_stack_size_t
void OSSL_LIB_CTX
static int EVP_PKEY_get_group_name(EVP_PKEY *pkey, char *gname, size_t gname_sz, size_t *gname_len)
#define SSL_CTX_set1_groups
#define PRI_OPENSSL_STACK
#define SSL_CTX_new_ex(libctx, propq, method)
Reduce SSL_CTX_new_ex() to SSL_CTX_new() for OpenSSL < 3.
uint64_t openssl_opt_t
#define CLEAR(x)
Definition basic.h:32
#define M_FATAL
Definition error.h:90
#define M_NONFATAL
Definition error.h:91
#define dmsg(flags,...)
Definition error.h:172
#define msg(flags,...)
Definition error.h:152
#define ASSERT(x)
Definition error.h:219
#define M_DEBUG
Definition error.h:93
#define M_WARN
Definition error.h:92
#define streq(x, y)
Definition options.h:721
time_t now
Definition otime.c:33
FILE * platform_fopen(const char *path, const char *mode)
Definition platform.c:500
int openvpn_base64_decode(const char *str, void *data, int size)
Definition base64.c:160
int openvpn_base64_encode(const void *data, int size, char **str)
Definition base64.c:51
int pem_password_callback(char *buf, int size, int rwflag, void *u)
Callback to retrieve the user's password.
Definition ssl.c:259
Control Channel SSL library backend module.
#define TLS_VER_1_0
#define TLS_VER_1_2
#define TLS_VER_1_3
#define TLS_VER_1_1
Control Channel Common Data Structures.
#define SSLF_TLS_VERSION_MAX_SHIFT
Definition ssl_common.h:432
#define UP_TYPE_PRIVATE_KEY
Definition ssl_common.h:42
#define SSLF_CLIENT_CERT_OPTIONAL
Definition ssl_common.h:425
#define SSLF_CLIENT_CERT_NOT_REQUIRED
Definition ssl_common.h:424
#define SSLF_TLS_VERSION_MAX_MASK
Definition ssl_common.h:433
#define SSLF_TLS_VERSION_MIN_SHIFT
Definition ssl_common.h:430
#define SSLF_TLS_VERSION_MIN_MASK
Definition ssl_common.h:431
void tls_ctx_set_tls_groups(struct tls_root_ctx *ctx, const char *groups)
Set the (elliptic curve) group allowed for signatures and key exchange.
void tls_ctx_free(struct tls_root_ctx *ctx)
Frees the library-specific TLSv1 context.
static int bio_read(BIO *bio, struct buffer *buf, const char *desc)
const char * get_ssl_library_version(void)
return a pointer to a static memory area containing the name and version number of the SSL library in...
static void openvpn_extkey_ec_finish(EC_KEY *ec)
static bool tls_ctx_set_tls_versions(struct tls_root_ctx *ctx, unsigned int ssl_flags)
static int bio_write(BIO *bio, const uint8_t *data, int size, const char *desc)
static int openvpn_extkey_rsa_finish(RSA *rsa)
static int tls_ctx_use_external_ec_key(struct tls_root_ctx *ctx, EVP_PKEY *pkey)
bool key_state_export_keying_material(struct tls_session *session, const char *label, size_t label_size, void *ekm, size_t ekm_size)
Keying Material Exporters [RFC 5705] allows additional keying material to be derived from existing TL...
void load_xkey_provider(void)
Some helper routines for provider load/unload.
static void print_pkey_details(EVP_PKEY *pkey, char *buf, size_t buflen)
static void print_server_tempkey(SSL *ssl, char *buf, size_t buflen)
static int rsa_pub_enc(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding)
static void tls_ctx_add_extra_certs(struct tls_root_ctx *ctx, BIO *bio, bool optional)
void show_available_tls_ciphers_list(const char *cipher_list, const char *tls_cert_profile, bool tls13)
Show the TLS ciphers that are available for us to use in the library depending on the TLS version.
static void * load_pkey_from_uri(const char *uri, SSL_CTX *ssl_ctx)
Load private key from OSSL_STORE URI or file uri : URI of object or filename ssl_ctx : SSL_CTX for UI...
void tls_ctx_server_new(struct tls_root_ctx *ctx)
Initialise a library-specific TLS context for a server.
void show_available_curves(void)
Show the available elliptic curves in the crypto library.
static uint16_t openssl_tls_version(unsigned int ver)
Convert internal version number to openssl version number.
void key_state_ssl_free(struct key_state_ssl *ks_ssl)
Free the SSL channel part of the given key state.
static int ecdsa_sign(int type, const unsigned char *dgst, int dgstlen, unsigned char *sig, unsigned int *siglen, const BIGNUM *kinv, const BIGNUM *r, EC_KEY *ec)
int tls_ctx_load_priv_file(struct tls_root_ctx *ctx, const char *priv_key_file, bool priv_key_file_inline)
Load private key file into the given TLS context.
static int cert_verify_callback(X509_STORE_CTX *ctx, void *arg)
void key_state_ssl_shutdown(struct key_state_ssl *ks_ssl)
Sets a TLS session to be shutdown state, so the TLS library will generate a shutdown alert.
void tls_ctx_load_extra_certs(struct tls_root_ctx *ctx, const char *extra_certs_file, bool extra_certs_file_inline)
Load extra certificate authority certificates from the given file or path.
static void print_peer_signature(SSL *ssl, char *buf, size_t buflen)
Get the type of the signature that is used by the peer during the TLS handshake.
OSSL_LIB_CTX * tls_libctx
Definition ssl_openssl.c:78
static const char * get_sigtype(int nid)
Translate an OpenSSL NID into a more human readable name.
int mydata_index
Allocate space in SSL objects in which to store a struct tls_session pointer back to parent.
Definition ssl_openssl.c:88
static void print_cert_details(X509 *cert, char *buf, size_t buflen)
Print human readable information about the certificate into buf.
static int ecdsa_sign_setup(EC_KEY *ec, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
void tls_ctx_check_cert_time(const struct tls_root_ctx *ctx)
Check our certificate notBefore and notAfter fields, and warn if the cert is either not yet valid or ...
void tls_ctx_restrict_ciphers_tls13(struct tls_root_ctx *ctx, const char *ciphers)
Restrict the list of ciphers that can be used within the TLS context for TLS 1.3 and higher.
static bool cert_uri_supported(void)
static int rsa_priv_dec(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding)
#define INFO_CALLBACK_SSL_CONST
static int rsa_pub_dec(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding)
static void bio_write_post(const int status, struct buffer *buf)
int tls_ctx_load_pkcs12(struct tls_root_ctx *ctx, const char *pkcs12_file, bool pkcs12_file_inline, bool load_ca_file)
Load PKCS #12 file for key, cert and (optionally) CA certs, and add to library-specific TLS context.
bool tls_ctx_initialised(struct tls_root_ctx *ctx)
Checks whether the given TLS context is initialised.
void key_state_ssl_init(struct key_state_ssl *ks_ssl, const struct tls_root_ctx *ssl_ctx, bool is_server, struct tls_session *session)
Initialise the SSL channel part of the given key state.
void tls_free_lib(void)
Free any global SSL library-specific data structures.
Definition ssl_openssl.c:98
static void unload_xkey_provider(void)
Undo steps in load_xkey_provider.
const char * get_rsa_padding_name(const int padding)
void tls_ctx_load_ecdh_params(struct tls_root_ctx *ctx, const char *curve_name)
Load Elliptic Curve Parameters, and load them into the library-specific TLS context.
static int get_sig_from_man(const unsigned char *dgst, unsigned int dgstlen, unsigned char *sig, unsigned int siglen, const char *algorithm)
Pass the input hash in 'dgst' to management and get the signature back.
static void tls_ctx_load_cert_uri(struct tls_root_ctx *tls_ctx, const char *uri)
static int rsa_priv_enc(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding)
static void convert_tls_list_to_openssl(char *openssl_ciphers, size_t len, const char *ciphers)
static void tls_ctx_load_cert_pem_file(struct tls_root_ctx *ctx, const char *cert_file, bool cert_file_inline)
void tls_init_lib(void)
Perform any static initialisation necessary by the library.
Definition ssl_openssl.c:91
void print_details(struct key_state_ssl *ks_ssl, const char *prefix)
Print a one line summary of SSL/TLS session handshake.
static void info_callback(INFO_CALLBACK_SSL_CONST SSL *s, int where, int ret)
int tls_version_max(void)
Return the maximum TLS version (as a TLS_VER_x constant) supported by current SSL implementation.
void backend_tls_ctx_reload_crl(struct tls_root_ctx *ssl_ctx, const char *crl_file, bool crl_inline)
Reload the Certificate Revocation List for the SSL channel.
void tls_ctx_restrict_ciphers(struct tls_root_ctx *ctx, const char *ciphers)
Restrict the list of ciphers that can be used within the TLS context for TLS 1.2 and below.
void tls_ctx_load_ca(struct tls_root_ctx *ctx, const char *ca_file, bool ca_file_inline, const char *ca_path, bool tls_server)
Load certificate authority certificates from the given file or path.
void tls_ctx_set_cert_profile(struct tls_root_ctx *ctx, const char *profile)
Set the TLS certificate profile.
int tls_ctx_use_management_external_key(struct tls_root_ctx *ctx)
Tell the management interface to load the given certificate and the external private key matching the...
static int tls_ctx_use_external_rsa_key(struct tls_root_ctx *ctx, EVP_PKEY *pkey)
static ECDSA_SIG * ecdsa_sign_sig(const unsigned char *dgst, int dgstlen, const BIGNUM *in_kinv, const BIGNUM *in_r, EC_KEY *ec)
void tls_ctx_load_cryptoapi(struct tls_root_ctx *ctx, const char *cryptoapi_cert)
Use Windows cryptoapi for key and cert, and add to library-specific TLS context.
static void convert_tls13_list_to_openssl(char *openssl_ciphers, size_t len, const char *ciphers)
bool tls_ctx_set_options(struct tls_root_ctx *ctx, unsigned int ssl_flags)
Set any library specific options.
void tls_ctx_load_dh_params(struct tls_root_ctx *ctx, const char *dh_file, bool dh_file_inline)
Load Diffie Hellman Parameters, and load them into the library-specific TLS context.
void tls_ctx_client_new(struct tls_root_ctx *ctx)
Initialises a library-specific TLS context for a client.
static int sk_x509_name_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
void tls_ctx_load_cert_file(struct tls_root_ctx *ctx, const char *cert_file, bool cert_file_inline)
Load certificate file into the given TLS context.
int get_num_elements(const char *string, char delimiter)
Returns the occurrences of 'delimiter' in a string +1 This is typically used to find out the number e...
Definition ssl_util.c:304
const tls_cipher_name_pair * tls_get_cipher_name_pair(const char *cipher_name, size_t len)
Definition ssl_util.c:285
SSL utility functions.
Control Channel Verification Module OpenSSL backend.
Wrapper structure for dynamically allocated memory.
Definition buffer.h:60
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:65
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:116
Definition sig.c:47
Get a tls_cipher_name_pair containing OpenSSL and IANA names for supplied TLS cipher name.
Definition ssl_util.h:77
const char * iana_name
Definition ssl_util.h:79
const char * openssl_name
Definition ssl_util.h:78
Structure that wraps the TLS context.
STACK_OF(X509_CRL) *crls
SSL_CTX * ctx
Definition ssl_openssl.h:41
Security parameter state of a single session within a VPN tunnel.
Definition ssl_common.h:490
char password[USER_PASS_LEN]
Definition misc.h:71
static int cleanup(void **state)
struct gc_arena gc
Definition test_ssl.c:133