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