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