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