OpenVPN
crypto_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 "basic.h"
38#include "buffer.h"
39#include "integer.h"
40#include "crypto.h"
41#include "crypto_backend.h"
42#include "memdbg.h"
43#include "openssl_compat.h"
44
45#include <openssl/conf.h>
46#include <openssl/des.h>
47#include <openssl/err.h>
48#include <openssl/evp.h>
49#include <openssl/objects.h>
50#include <openssl/rand.h>
51#include <openssl/ssl.h>
52
53#if !defined(LIBRESSL_VERSION_NUMBER)
54#include <openssl/kdf.h>
55#endif
56#if OPENSSL_VERSION_NUMBER >= 0x30000000L
57#include <openssl/provider.h>
58#include <openssl/core_names.h>
59#endif
60
61#if defined(_WIN32) && defined(OPENSSL_NO_EC)
62#error Windows build with OPENSSL_NO_EC: disabling EC key is not supported.
63#endif
64
65#ifdef _MSC_VER
66/* mute ossl3 deprecation warnings treated as errors in msvc */
67#pragma warning(disable : 4996)
68#endif
69
70/*
71 * Check for key size creepage.
72 */
73
74#if MAX_CIPHER_KEY_LENGTH < EVP_MAX_KEY_LENGTH
75#warning Some OpenSSL EVP ciphers now support key lengths greater than MAX_CIPHER_KEY_LENGTH -- consider increasing MAX_CIPHER_KEY_LENGTH
76#endif
77
78#if MAX_HMAC_KEY_LENGTH < EVP_MAX_MD_SIZE
79#warning Some OpenSSL HMAC message digests now support key lengths greater than MAX_HMAC_KEY_LENGTH -- consider increasing MAX_HMAC_KEY_LENGTH
80#endif
81
82#if HAVE_OPENSSL_ENGINE
83#include <openssl/ui.h>
84#include <openssl/engine.h>
85
86static bool engine_initialized = false; /* GLOBAL */
87
88static ENGINE *engine_persist = NULL; /* GLOBAL */
89
90/* Try to load an engine in a shareable library */
91static ENGINE *
92try_load_engine(const char *engine)
93{
94 ENGINE *e = ENGINE_by_id("dynamic");
95 if (e)
96 {
97 if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", engine, 0)
98 || !ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0))
99 {
100 ENGINE_free(e);
101 e = NULL;
102 }
103 }
104 return e;
105}
106
107static ENGINE *
108setup_engine(const char *engine)
109{
110 ENGINE *e = NULL;
111
112 ENGINE_load_builtin_engines();
113
114 if (engine)
115 {
116 if (strcmp(engine, "auto") == 0)
117 {
118 msg(M_INFO, "Initializing OpenSSL auto engine support");
119 ENGINE_register_all_complete();
120 return NULL;
121 }
122 if ((e = ENGINE_by_id(engine)) == NULL && (e = try_load_engine(engine)) == NULL)
123 {
124 crypto_msg(M_FATAL, "OpenSSL error: cannot load engine '%s'", engine);
125 }
126
127 if (!ENGINE_set_default(e, ENGINE_METHOD_ALL))
128 {
129 crypto_msg(M_FATAL, "OpenSSL error: ENGINE_set_default failed on engine '%s'", engine);
130 }
131
132 msg(M_INFO, "Initializing OpenSSL support for engine '%s'", ENGINE_get_id(e));
133 }
134 return e;
135}
136
137#endif /* HAVE_OPENSSL_ENGINE */
138
139void
140crypto_init_lib_engine(const char *engine_name)
141{
142#if HAVE_OPENSSL_ENGINE
143 if (!engine_initialized)
144 {
145 ASSERT(engine_name);
146 ASSERT(!engine_persist);
147 engine_persist = setup_engine(engine_name);
148 engine_initialized = true;
149 }
150#else /* if HAVE_OPENSSL_ENGINE */
151 msg(M_WARN, "Note: OpenSSL hardware crypto engine functionality is not available");
152#endif
153}
154
156crypto_load_provider(const char *provider)
157{
158#if OPENSSL_VERSION_NUMBER >= 0x30000000L
159 /* Load providers into the default (NULL) library context */
160 OSSL_PROVIDER *prov = OSSL_PROVIDER_load(NULL, provider);
161 if (!prov)
162 {
163 crypto_msg(M_FATAL, "failed to load provider '%s'", provider);
164 }
165 return prov;
166#else /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
167 msg(M_WARN, "Note: OpenSSL provider functionality is not available");
168 return NULL;
169#endif
170}
171
172void
173crypto_unload_provider(const char *provname, provider_t *provider)
174{
175#if OPENSSL_VERSION_NUMBER >= 0x30000000L
176 if (!OSSL_PROVIDER_unload(provider))
177 {
178 crypto_msg(M_FATAL, "failed to unload provider '%s'", provname);
179 }
180#endif
181}
182
183/*
184 *
185 * Functions related to the core crypto library
186 *
187 */
188
189void
191{
192 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
193 /*
194 * If you build the OpenSSL library and OpenVPN with
195 * CRYPTO_MDEBUG, you will get a listing of OpenSSL
196 * memory leaks on program termination.
197 */
198
199#ifdef CRYPTO_MDEBUG
200 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
201#endif
202}
203
204void
206{
207#ifdef CRYPTO_MDEBUG
208 FILE *fp = fopen("sdlog", "w");
209 ASSERT(fp);
210 CRYPTO_mem_leaks_fp(fp);
211 fclose(fp);
212#endif
213
214#if HAVE_OPENSSL_ENGINE
215 if (engine_initialized)
216 {
217 ENGINE_cleanup();
218 engine_persist = NULL;
219 engine_initialized = false;
220 }
221#endif
222}
223
224void
226{
227 ERR_clear_error();
228}
229
230void
231crypto_print_openssl_errors(const unsigned int flags)
232{
233 openssl_err_t err = 0;
234 int line, errflags;
235 const char *file, *data, *func;
236
237 while ((err = ERR_get_error_all(&file, &line, &func, &data, &errflags)) != 0)
238 {
239 if (!(errflags & ERR_TXT_STRING))
240 {
241 data = "";
242 }
243
244 /* Be more clear about frequently occurring "no shared cipher" error */
245 if (ERR_GET_REASON(err) == SSL_R_NO_SHARED_CIPHER)
246 {
247 msg(D_CRYPT_ERRORS, "TLS error: The server has no TLS ciphersuites "
248 "in common with the client. Your --tls-cipher setting might be "
249 "too restrictive.");
250 }
251 else if (ERR_GET_REASON(err) == SSL_R_UNSUPPORTED_PROTOCOL)
252 {
254 "TLS error: Unsupported protocol. This typically "
255 "indicates that client and server have no common TLS version enabled. "
256 "This can be caused by mismatched tls-version-min and tls-version-max "
257 "options on client and server. "
258 "If your OpenVPN client is between v2.3.6 and v2.3.2 try adding "
259 "tls-version-min 1.0 to the client configuration to use TLS 1.0+ "
260 "instead of TLS 1.0 only");
261 }
262
263 /* print file and line if verb >=8 */
265 {
266 msg(flags, "OpenSSL: %s:%s", ERR_error_string(err, NULL), data);
267 }
268 else
269 {
270 msg(flags, "OpenSSL: %s:%s:%s:%d:%s", ERR_error_string(err, NULL), data, file, line,
271 func);
272 }
273 }
274}
275
276
278 { "AES-128-GCM", "id-aes128-GCM" },
279 { "AES-192-GCM", "id-aes192-GCM" },
280 { "AES-256-GCM", "id-aes256-GCM" },
281 { "CHACHA20-POLY1305", "ChaCha20-Poly1305" },
282};
285
286
287static int
288cipher_name_cmp(const void *a, const void *b)
289{
290 const EVP_CIPHER *const *cipher_a = a;
291 const EVP_CIPHER *const *cipher_b = b;
292
293 return strcmp(EVP_CIPHER_get0_name(*cipher_a), EVP_CIPHER_get0_name(*cipher_b));
294}
295
297{
298 /* If we ever exceed this, we must be more selective */
299 const EVP_CIPHER *list[1000];
300 size_t num;
301};
302
303static void
304collect_ciphers(EVP_CIPHER *cipher, void *list)
305{
306 if (!cipher)
307 {
308 return;
309 }
310 struct collect_ciphers *cipher_list = list;
311 if (cipher_list->num == SIZE(cipher_list->list))
312 {
313 msg(M_WARN, "WARNING: Too many ciphers, not showing all");
314 return;
315 }
316
317 const char *ciphername = EVP_CIPHER_get0_name(cipher);
318
319 if (ciphername
320 && (cipher_kt_mode_cbc(ciphername)
321#ifdef ENABLE_OFB_CFB_MODE
322 || cipher_kt_mode_ofb_cfb(ciphername)
323#endif
324 || cipher_kt_mode_aead(ciphername)))
325 {
326 cipher_list->list[cipher_list->num++] = cipher;
327 }
328}
329
330void
332{
333 struct collect_ciphers cipher_list = { 0 };
334
335#ifndef ENABLE_SMALL
336 printf("The following ciphers and cipher modes are available for use\n"
337 "with " PACKAGE_NAME ". Each cipher shown below may be used as a\n"
338 "parameter to the --data-ciphers (or --cipher) option. In static \n"
339 "key mode only CBC mode is allowed.\n");
340 printf("See also openssl list -cipher-algorithms\n\n");
341#endif
342
343#if OPENSSL_VERSION_NUMBER >= 0x30000000L
344 EVP_CIPHER_do_all_provided(NULL, collect_ciphers, &cipher_list);
345#else
346 for (int nid = 0; nid < 10000; ++nid)
347 {
348#if defined(LIBRESSL_VERSION_NUMBER)
349 /* OpenBSD/LibreSSL reimplemented EVP_get_cipherbyname and broke
350 * calling EVP_get_cipherbynid with an invalid nid in the process
351 * so that it would segfault. */
352 const EVP_CIPHER *cipher = NULL;
353 const char *name = OBJ_nid2sn(nid);
354 if (name)
355 {
356 cipher = EVP_get_cipherbyname(name);
357 }
358#else /* if defined(LIBRESSL_VERSION_NUMBER) */
359 const EVP_CIPHER *cipher = EVP_get_cipherbynid(nid);
360#endif
361 /* We cast the const away so we can keep the function prototype
362 * compatible with EVP_CIPHER_do_all_provided */
363 collect_ciphers((EVP_CIPHER *)cipher, &cipher_list);
364 }
365#endif
366
367 /* cast to non-const to prevent warning */
368 qsort((EVP_CIPHER *)cipher_list.list, cipher_list.num, sizeof(*cipher_list.list),
370
371 for (size_t i = 0; i < cipher_list.num; i++)
372 {
373 if (!cipher_kt_insecure(EVP_CIPHER_get0_name(cipher_list.list[i])))
374 {
375 print_cipher(EVP_CIPHER_get0_name(cipher_list.list[i]));
376 }
377 }
378
379 printf("\nThe following ciphers have a block size of less than 128 bits, \n"
380 "and are therefore deprecated. Do not use unless you have to.\n\n");
381 for (size_t i = 0; i < cipher_list.num; i++)
382 {
383 if (cipher_kt_insecure(EVP_CIPHER_get0_name(cipher_list.list[i])))
384 {
385 print_cipher(EVP_CIPHER_get0_name(cipher_list.list[i]));
386 }
387 }
388 printf("\n");
389}
390
391void
392print_digest(EVP_MD *digest, void *unused)
393{
394 printf("%s %d bit digest size\n", md_kt_name(EVP_MD_get0_name(digest)),
395 (int)EVP_MD_size(digest) * 8);
396}
397
398void
400{
401#ifndef ENABLE_SMALL
402 printf("The following message digests are available for use with\n" PACKAGE_NAME
403 ". A message digest is used in conjunction with\n"
404 "the HMAC function, to authenticate received packets.\n"
405 "You can specify a message digest as parameter to\n"
406 "the --auth option.\n");
407 printf("See also openssl list -digest-algorithms\n\n");
408#endif
409
410#if OPENSSL_VERSION_NUMBER >= 0x30000000L
411 EVP_MD_do_all_provided(NULL, print_digest, NULL);
412#else
413 for (int nid = 0; nid < 10000; ++nid)
414 {
415 /* OpenBSD/LibreSSL reimplemented EVP_get_digestbyname and broke
416 * calling EVP_get_digestbynid with an invalid nid in the process
417 * so that it would segfault. */
418#ifdef LIBRESSL_VERSION_NUMBER
419 const EVP_MD *digest = NULL;
420 const char *name = OBJ_nid2sn(nid);
421 if (name)
422 {
423 digest = EVP_get_digestbyname(name);
424 }
425#else /* ifdef LIBRESSL_VERSION_NUMBER */
426 const EVP_MD *digest = EVP_get_digestbynid(nid);
427#endif
428 if (digest)
429 {
430 /* We cast the const away so we can keep the function prototype
431 * compatible with EVP_MD_do_all_provided */
432 print_digest((EVP_MD *)digest, NULL);
433 }
434 }
435#endif /* if OPENSSL_VERSION_NUMBER >= 0x30000000L */
436 printf("\n");
437}
438
439void
441{
442#if HAVE_OPENSSL_ENGINE /* Only defined for OpenSSL */
443 ENGINE *e;
444
445 printf("OpenSSL Crypto Engines\n\n");
446
447 ENGINE_load_builtin_engines();
448
449 e = ENGINE_get_first();
450 while (e)
451 {
452 printf("%s [%s]\n", ENGINE_get_name(e), ENGINE_get_id(e));
453 e = ENGINE_get_next(e);
454 }
455 ENGINE_cleanup();
456#else /* if HAVE_OPENSSL_ENGINE */
457 printf("Sorry, OpenSSL hardware crypto engine functionality is not available.\n");
458#endif
459}
460
461
462bool
463crypto_pem_encode(const char *name, struct buffer *dst, const struct buffer *src,
464 struct gc_arena *gc)
465{
466 bool ret = false;
467 BIO *bio = BIO_new(BIO_s_mem());
468 if (!bio || !PEM_write_bio(bio, name, "", BPTR(src), BLEN(src)))
469 {
470 ret = false;
471 goto cleanup;
472 }
473
474 BUF_MEM *bptr;
475 BIO_get_mem_ptr(bio, &bptr);
476
477 *dst = alloc_buf_gc(bptr->length, gc);
478 ASSERT(buf_write(dst, bptr->data, bptr->length));
479
480 ret = true;
481cleanup:
482 if (!BIO_free(bio))
483 {
484 ret = false;
485 }
486
487 return ret;
488}
489
490bool
491crypto_pem_decode(const char *name, struct buffer *dst, const struct buffer *src)
492{
493 bool ret = false;
494
495 BIO *bio = BIO_new_mem_buf((char *)BPTR(src), BLEN(src));
496 if (!bio)
497 {
498 crypto_msg(M_FATAL, "Cannot open memory BIO for PEM decode");
499 }
500
501 char *name_read = NULL;
502 char *header_read = NULL;
503 uint8_t *data_read = NULL;
504 long data_read_len = 0;
505 if (!PEM_read_bio(bio, &name_read, &header_read, &data_read, &data_read_len))
506 {
507 dmsg(D_CRYPT_ERRORS, "%s: PEM decode failed", __func__);
508 goto cleanup;
509 }
510
511 if (strcmp(name, name_read))
512 {
513 dmsg(D_CRYPT_ERRORS, "%s: unexpected PEM name (got '%s', expected '%s')", __func__,
514 name_read, name);
515 goto cleanup;
516 }
517
518 uint8_t *dst_data = buf_write_alloc(dst, data_read_len);
519 if (!dst_data)
520 {
521 dmsg(D_CRYPT_ERRORS, "%s: dst too small (%i, needs %li)", __func__, BCAP(dst),
522 data_read_len);
523 goto cleanup;
524 }
525 memcpy(dst_data, data_read, data_read_len);
526
527 ret = true;
528cleanup:
529 OPENSSL_free(name_read);
530 OPENSSL_free(header_read);
531 OPENSSL_free(data_read);
532 if (!BIO_free(bio))
533 {
534 ret = false;
535 }
536
537 return ret;
538}
539
540/*
541 *
542 * Random number functions, used in cases where we want
543 * reasonably strong cryptographic random number generation
544 * without depleting our entropy pool. Used for random
545 * IV values and a number of other miscellaneous tasks.
546 *
547 */
548
549int
550rand_bytes(uint8_t *output, int len)
551{
552 if (unlikely(1 != RAND_bytes(output, len)))
553 {
554 crypto_msg(D_CRYPT_ERRORS, "RAND_bytes() failed");
555 return 0;
556 }
557 return 1;
558}
559
560/*
561 *
562 * Generic cipher key type functions
563 *
564 */
565
566static evp_cipher_type *
567cipher_get(const char *ciphername)
568{
569 ASSERT(ciphername);
570
571 ciphername = translate_cipher_name_from_openvpn(ciphername);
572 return EVP_CIPHER_fetch(NULL, ciphername, NULL);
573}
574
575bool
576cipher_valid_reason(const char *ciphername, const char **reason)
577{
578 bool ret = false;
579 evp_cipher_type *cipher = cipher_get(ciphername);
580 if (!cipher)
581 {
582 crypto_msg(D_LOW, "Cipher algorithm '%s' not found", ciphername);
583 *reason = "disabled because unknown";
584 goto out;
585 }
586
587#ifdef OPENSSL_FIPS
588 /* Rhel 8/CentOS 8 have a patched OpenSSL version that return a cipher
589 * here that is actually not usable if in FIPS mode */
590
591 if (FIPS_mode() && !(EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_FIPS))
592 {
593 msg(D_LOW,
594 "Cipher algorithm '%s' is known by OpenSSL library but "
595 "currently disabled by running in FIPS mode.",
596 ciphername);
597 *reason = "disabled by FIPS mode";
598 goto out;
599 }
600#endif
601 if (EVP_CIPHER_key_length(cipher) > MAX_CIPHER_KEY_LENGTH)
602 {
603 msg(D_LOW,
604 "Cipher algorithm '%s' uses a default key size (%d bytes) "
605 "which is larger than " PACKAGE_NAME "'s current maximum key size "
606 "(%d bytes)",
607 ciphername, EVP_CIPHER_key_length(cipher), MAX_CIPHER_KEY_LENGTH);
608 *reason = "disabled due to key size too large";
609 goto out;
610 }
611
612 ret = true;
613 *reason = NULL;
614out:
615 EVP_CIPHER_free(cipher);
616 return ret;
617}
618
619const char *
620cipher_kt_name(const char *ciphername)
621{
622 ASSERT(ciphername);
623 if (strcmp("none", ciphername) == 0)
624 {
625 return "[null-cipher]";
626 }
627
628 evp_cipher_type *cipher_kt = cipher_get(ciphername);
629 if (!cipher_kt)
630 {
631 return NULL;
632 }
633
634 const char *name = EVP_CIPHER_name(cipher_kt);
635 EVP_CIPHER_free(cipher_kt);
637}
638
639unsigned int
640cipher_kt_key_size(const char *ciphername)
641{
642 evp_cipher_type *cipher = cipher_get(ciphername);
643 int size = EVP_CIPHER_key_length(cipher);
644 ASSERT(size >= 0);
645 EVP_CIPHER_free(cipher);
646 return size;
647}
648
649unsigned int
650cipher_kt_iv_size(const char *ciphername)
651{
652 evp_cipher_type *cipher = cipher_get(ciphername);
653 int ivsize = EVP_CIPHER_iv_length(cipher);
654 ASSERT(ivsize >= 0);
655 EVP_CIPHER_free(cipher);
656 return ivsize;
657}
658
659unsigned int
660cipher_kt_block_size(const char *ciphername)
661{
662 /*
663 * OpenSSL reports OFB/CFB/GCM cipher block sizes as '1 byte'. To work
664 * around that, try to replace the mode with 'CBC' and return the block size
665 * reported for that cipher, if possible. If that doesn't work, just return
666 * the value reported by OpenSSL.
667 */
668 char *name = NULL;
669 char *mode_str = NULL;
670 const char *orig_name = NULL;
671 evp_cipher_type *cbc_cipher = NULL;
672 evp_cipher_type *cipher = cipher_get(ciphername);
673 if (!cipher)
674 {
675 return 0;
676 }
677
678 int block_size = EVP_CIPHER_block_size(cipher);
679
680 orig_name = EVP_CIPHER_name(cipher);
681 if (!orig_name)
682 {
683 goto cleanup;
684 }
685
686 name = string_alloc(translate_cipher_name_to_openvpn(orig_name), NULL);
687 mode_str = strrchr(name, '-');
688 if (!mode_str || strlen(mode_str) < 4)
689 {
690 goto cleanup;
691 }
692
693 strcpy(mode_str, "-CBC");
694
695 cbc_cipher = EVP_CIPHER_fetch(NULL, translate_cipher_name_from_openvpn(name), NULL);
696 if (cbc_cipher)
697 {
698 block_size = EVP_CIPHER_block_size(cbc_cipher);
699 }
700
701cleanup:
702 EVP_CIPHER_free(cbc_cipher);
703 EVP_CIPHER_free(cipher);
704 free(name);
705 ASSERT(block_size >= 0);
706 return block_size;
707}
708
709unsigned int
710cipher_kt_tag_size(const char *ciphername)
711{
712 if (cipher_kt_mode_aead(ciphername))
713 {
715 }
716 else
717 {
718 return 0;
719 }
720}
721
722bool
723cipher_kt_insecure(const char *ciphername)
724{
725 if (cipher_kt_block_size(ciphername) >= 128 / 8)
726 {
727 return false;
728 }
729#ifdef NID_chacha20_poly1305
730 evp_cipher_type *cipher = cipher_get(ciphername);
731 if (cipher)
732 {
733 bool ischachapoly = (EVP_CIPHER_nid(cipher) == NID_chacha20_poly1305);
734 EVP_CIPHER_free(cipher);
735 if (ischachapoly)
736 {
737 return false;
738 }
739 }
740#endif
741 return true;
742}
743
744int
745cipher_kt_mode(const EVP_CIPHER *cipher_kt)
746{
747 ASSERT(NULL != cipher_kt);
748 return EVP_CIPHER_mode(cipher_kt);
749}
750
751bool
752cipher_kt_mode_cbc(const char *ciphername)
753{
754 evp_cipher_type *cipher = cipher_get(ciphername);
755
756 bool ret = cipher
757 && (cipher_kt_mode(cipher) == OPENVPN_MODE_CBC
758 /* Exclude AEAD cipher modes, they require a different API */
759#ifdef EVP_CIPH_FLAG_CTS
760 && !(EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_CTS)
761#endif
762 && !(EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
763 && !(EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_ENC_THEN_MAC));
764 EVP_CIPHER_free(cipher);
765 return ret;
766}
767
768bool
769cipher_kt_mode_ofb_cfb(const char *ciphername)
770{
771 evp_cipher_type *cipher = cipher_get(ciphername);
772 bool ofb_cfb = cipher
773 && (cipher_kt_mode(cipher) == OPENVPN_MODE_OFB
774 || cipher_kt_mode(cipher) == OPENVPN_MODE_CFB)
775 /* Exclude AEAD cipher modes, they require a different API */
776 && !(EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER);
777 EVP_CIPHER_free(cipher);
778 return ofb_cfb;
779}
780
781bool
782cipher_kt_mode_aead(const char *ciphername)
783{
784 bool isaead = false;
785
786 evp_cipher_type *cipher = cipher_get(ciphername);
787 if (cipher)
788 {
789 if (EVP_CIPHER_mode(cipher) == OPENVPN_MODE_GCM)
790 {
791 isaead = true;
792 }
793
794#ifdef NID_chacha20_poly1305
795 if (EVP_CIPHER_nid(cipher) == NID_chacha20_poly1305)
796 {
797 isaead = true;
798 }
799#endif
800 }
801
802 EVP_CIPHER_free(cipher);
803
804 return isaead;
805}
806
807/*
808 *
809 * Generic cipher context functions
810 *
811 */
812
815{
816 EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
818 return ctx;
819}
820
821void
822cipher_ctx_free(EVP_CIPHER_CTX *ctx)
823{
824 EVP_CIPHER_CTX_free(ctx);
825}
826
827void
828cipher_ctx_init(EVP_CIPHER_CTX *ctx, const uint8_t *key, const char *ciphername,
830{
831 ASSERT(NULL != ciphername && NULL != ctx);
832 evp_cipher_type *kt = cipher_get(ciphername);
833
834 EVP_CIPHER_CTX_reset(ctx);
835 if (!EVP_CipherInit_ex(ctx, kt, NULL, key, NULL, enc))
836 {
837 crypto_msg(M_FATAL, "EVP cipher init #2");
838 }
839
840 /* make sure we used a big enough key */
841 ASSERT(EVP_CIPHER_CTX_key_length(ctx) <= EVP_CIPHER_key_length(kt));
842 EVP_CIPHER_free(kt);
843}
844
845unsigned int
846cipher_ctx_iv_length(const EVP_CIPHER_CTX *ctx)
847{
848 return EVP_CIPHER_CTX_iv_length(ctx);
849}
850
851int
852cipher_ctx_get_tag(EVP_CIPHER_CTX *ctx, uint8_t *tag_buf, int tag_size)
853{
854 return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, tag_size, tag_buf);
855}
856
857unsigned int
858cipher_ctx_block_size(const EVP_CIPHER_CTX *ctx)
859{
860 return EVP_CIPHER_CTX_block_size(ctx);
861}
862
863int
864cipher_ctx_mode(const EVP_CIPHER_CTX *ctx)
865{
866 return EVP_CIPHER_CTX_mode(ctx);
867}
868
869bool
871{
872 if (!ctx)
873 {
874 return false;
875 }
876
877 unsigned long flags = EVP_CIPHER_CTX_flags(ctx);
878 int mode = EVP_CIPHER_CTX_mode(ctx);
879
880 return mode == EVP_CIPH_CBC_MODE
881 /* Exclude AEAD cipher modes, they require a different API */
882#ifdef EVP_CIPH_FLAG_CTS
883 && !(flags & EVP_CIPH_FLAG_CTS)
884#endif
885 && !(flags & EVP_CIPH_FLAG_AEAD_CIPHER);
886}
887
888bool
890{
891 if (!ctx)
892 {
893 return false;
894 }
895
896 int mode = EVP_CIPHER_CTX_get_mode(ctx);
897
898 return (mode == EVP_CIPH_OFB_MODE || mode == EVP_CIPH_CFB_MODE)
899 /* Exclude AEAD cipher modes, they require a different API */
900 && !(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_FLAG_AEAD_CIPHER);
901}
902
903bool
905{
906 if (ctx)
907 {
908 unsigned long flags = EVP_CIPHER_CTX_flags(ctx);
909 if (flags & EVP_CIPH_FLAG_AEAD_CIPHER)
910 {
911 return true;
912 }
913
914#if defined(NID_chacha20_poly1305) && OPENSSL_VERSION_NUMBER < 0x30000000L
915 if (EVP_CIPHER_CTX_nid(ctx) == NID_chacha20_poly1305)
916 {
917 return true;
918 }
919#endif
920 }
921
922 return false;
923}
924
925
926int
927cipher_ctx_reset(EVP_CIPHER_CTX *ctx, const uint8_t *iv_buf)
928{
929 return EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv_buf, -1);
930}
931
932int
933cipher_ctx_update_ad(EVP_CIPHER_CTX *ctx, const uint8_t *src, int src_len)
934{
935 int len;
936 if (!EVP_CipherUpdate(ctx, NULL, &len, src, src_len))
937 {
938 crypto_msg(M_FATAL, "%s: EVP_CipherUpdate() failed", __func__);
939 }
940 return 1;
941}
942
943int
944cipher_ctx_update(EVP_CIPHER_CTX *ctx, uint8_t *dst, int *dst_len, uint8_t *src, int src_len)
945{
946 if (!EVP_CipherUpdate(ctx, dst, dst_len, src, src_len))
947 {
948 crypto_msg(M_FATAL, "%s: EVP_CipherUpdate() failed", __func__);
949 }
950 return 1;
951}
952
953int
954cipher_ctx_final(EVP_CIPHER_CTX *ctx, uint8_t *dst, int *dst_len)
955{
956 return EVP_CipherFinal(ctx, dst, dst_len);
957}
958
959int
960cipher_ctx_final_check_tag(EVP_CIPHER_CTX *ctx, uint8_t *dst, int *dst_len, uint8_t *tag,
961 size_t tag_len)
962{
963 ASSERT(tag_len < INT_MAX);
964 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, (int)tag_len, tag))
965 {
966 return 0;
967 }
968
969 return cipher_ctx_final(ctx, dst, dst_len);
970}
971
972/*
973 *
974 * Generic message digest information functions
975 *
976 */
977
978
979static evp_md_type *
980md_get(const char *digest)
981{
982 evp_md_type *md = NULL;
983 ASSERT(digest);
984 md = EVP_MD_fetch(NULL, digest, NULL);
985 if (!md)
986 {
987 crypto_msg(M_FATAL, "Message hash algorithm '%s' not found", digest);
988 }
989 if (EVP_MD_size(md) > MAX_HMAC_KEY_LENGTH)
990 {
992 "Message hash algorithm '%s' uses a default hash "
993 "size (%d bytes) which is larger than " PACKAGE_NAME "'s current "
994 "maximum hash size (%d bytes)",
995 digest, (int)EVP_MD_size(md), MAX_HMAC_KEY_LENGTH);
996 }
997 return md;
998}
999
1000
1001bool
1002md_valid(const char *digest)
1003{
1004 evp_md_type *md = EVP_MD_fetch(NULL, digest, NULL);
1005 bool valid = (md != NULL);
1006 EVP_MD_free(md);
1007 return valid;
1008}
1009
1010
1011/* Since we used the OpenSSL <=1.1 names as part of our OCC message, they
1012 * are now unfortunately part of our wire protocol.
1013 *
1014 * OpenSSL 3.0 will still accept the "old" names so we do not need to use
1015 * this translation table for forward lookup, only for returning the name
1016 * with md_kt_name() */
1018 { "BLAKE2s256", "BLAKE2S-256" },
1019 { "BLAKE2b512", "BLAKE2B-512" },
1020 { "RIPEMD160", "RIPEMD-160" },
1021 { "SHA224", "SHA2-224" },
1022 { "SHA256", "SHA2-256" },
1023 { "SHA384", "SHA2-384" },
1024 { "SHA512", "SHA2-512" },
1025 { "SHA512-224", "SHA2-512/224" },
1026 { "SHA512-256", "SHA2-512/256" },
1027 { "SHAKE128", "SHAKE-128" },
1028 { "SHAKE256", "SHAKE-256" },
1029};
1032
1033const char *
1034md_kt_name(const char *mdname)
1035{
1036 if (!strcmp("none", mdname))
1037 {
1038 return "[null-digest]";
1039 }
1040 evp_md_type *kt = md_get(mdname);
1041 const char *name = EVP_MD_get0_name(kt);
1042
1043 /* Search for a digest name translation */
1044 for (size_t i = 0; i < digest_name_translation_table_count; i++)
1045 {
1047 if (!strcmp(name, pair->lib_name))
1048 {
1049 name = pair->openvpn_name;
1050 }
1051 }
1052
1053 EVP_MD_free(kt);
1054 return name;
1055}
1056
1057unsigned char
1058md_kt_size(const char *mdname)
1059{
1060 if (!strcmp("none", mdname))
1061 {
1062 return 0;
1063 }
1064 evp_md_type *kt = md_get(mdname);
1065 unsigned char size = (unsigned char)EVP_MD_size(kt);
1066 EVP_MD_free(kt);
1067 return size;
1068}
1069
1070
1071/*
1072 *
1073 * Generic message digest functions
1074 *
1075 */
1076
1077EVP_MD_CTX *
1079{
1080 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
1082 return ctx;
1083}
1084
1085void
1086md_ctx_free(EVP_MD_CTX *ctx)
1087{
1088 EVP_MD_CTX_free(ctx);
1089}
1090
1091void
1092md_ctx_init(EVP_MD_CTX *ctx, const char *mdname)
1093{
1094 evp_md_type *kt = md_get(mdname);
1095 ASSERT(NULL != ctx && NULL != kt);
1096
1097 EVP_MD_CTX_init(ctx);
1098 if (!EVP_DigestInit(ctx, kt))
1099 {
1100 crypto_msg(M_FATAL, "EVP_DigestInit failed");
1101 }
1102 EVP_MD_free(kt);
1103}
1104
1105void
1106md_ctx_cleanup(EVP_MD_CTX *ctx)
1107{
1108 EVP_MD_CTX_reset(ctx);
1109}
1110
1111int
1112md_ctx_size(const EVP_MD_CTX *ctx)
1113{
1114 return (int)EVP_MD_CTX_size(ctx);
1115}
1116
1117void
1118md_ctx_update(EVP_MD_CTX *ctx, const uint8_t *src, size_t src_len)
1119{
1120 EVP_DigestUpdate(ctx, src, src_len);
1121}
1122
1123void
1124md_ctx_final(EVP_MD_CTX *ctx, uint8_t *dst)
1125{
1126 unsigned int in_md_len = 0;
1127
1128 EVP_DigestFinal(ctx, dst, &in_md_len);
1129}
1130
1131
1132/*
1133 *
1134 * Generic HMAC functions
1135 *
1136 */
1137#if OPENSSL_VERSION_NUMBER < 0x30000000L
1138HMAC_CTX *
1140{
1141 HMAC_CTX *ctx = HMAC_CTX_new();
1143 return ctx;
1144}
1145
1146void
1147hmac_ctx_free(HMAC_CTX *ctx)
1148{
1149 HMAC_CTX_free(ctx);
1150}
1151
1152void
1153hmac_ctx_init(HMAC_CTX *ctx, const uint8_t *key, const char *mdname)
1154{
1155 evp_md_type *kt = md_get(mdname);
1156 ASSERT(NULL != kt && NULL != ctx);
1157
1158 int key_len = (int)EVP_MD_size(kt);
1159 HMAC_CTX_reset(ctx);
1160 if (!HMAC_Init_ex(ctx, key, key_len, kt, NULL))
1161 {
1162 crypto_msg(M_FATAL, "HMAC_Init_ex failed");
1163 }
1164
1165 /* make sure we used a big enough key */
1166 ASSERT((ssize_t)HMAC_size(ctx) <= key_len);
1167}
1168
1169void
1170hmac_ctx_cleanup(HMAC_CTX *ctx)
1171{
1172 HMAC_CTX_reset(ctx);
1173}
1174
1175#if defined(__GNUC__) || defined(__clang__)
1176#pragma GCC diagnostic push
1177#pragma GCC diagnostic ignored "-Wconversion"
1178#endif
1179
1180int
1181hmac_ctx_size(HMAC_CTX *ctx)
1182{
1183 return HMAC_size(ctx);
1184}
1185
1186#if defined(__GNUC__) || defined(__clang__)
1187#pragma GCC diagnostic pop
1188#endif
1189
1190void
1191hmac_ctx_reset(HMAC_CTX *ctx)
1192{
1193 if (!HMAC_Init_ex(ctx, NULL, 0, NULL, NULL))
1194 {
1195 crypto_msg(M_FATAL, "HMAC_Init_ex failed");
1196 }
1197}
1198
1199void
1200hmac_ctx_update(HMAC_CTX *ctx, const uint8_t *src, int src_len)
1201{
1202 HMAC_Update(ctx, src, src_len);
1203}
1204
1205void
1206hmac_ctx_final(HMAC_CTX *ctx, uint8_t *dst)
1207{
1208 unsigned int in_hmac_len = 0;
1209
1210 HMAC_Final(ctx, dst, &in_hmac_len);
1211}
1212#else /* if OPENSSL_VERSION_NUMBER < 0x30000000L */
1213hmac_ctx_t *
1214hmac_ctx_new(void)
1215{
1216 hmac_ctx_t *ctx;
1218 EVP_MAC *hmac = EVP_MAC_fetch(NULL, "HMAC", NULL);
1219 ctx->ctx = EVP_MAC_CTX_new(hmac);
1220 check_malloc_return(ctx->ctx);
1221
1222 EVP_MAC_free(hmac);
1223
1224 return ctx;
1225}
1226
1227void
1229{
1230 EVP_MAC_CTX_free(ctx->ctx);
1231 secure_memzero(ctx, sizeof(hmac_ctx_t));
1232 free(ctx);
1233}
1234
1235void
1236hmac_ctx_init(hmac_ctx_t *ctx, const uint8_t *key, const char *mdname)
1237{
1238 evp_md_type *kt = md_get(mdname);
1239 ASSERT(NULL != kt && NULL != ctx && ctx->ctx != NULL);
1240
1241 /* We need to make a copy of the key since the OSSL parameters
1242 * only reference it */
1243 memcpy(ctx->key, key, (size_t)EVP_MD_size(kt));
1244
1245 /* Lookup/setting of parameters in OpenSSL 3.0 are string based
1246 *
1247 * The OSSL_PARAM_construct_utf8_string needs a non const str but this
1248 * only used for lookup so we cast (as OpenSSL also does internally)
1249 * the constness away here.
1250 */
1251 ctx->params[0] = OSSL_PARAM_construct_utf8_string("digest", (char *)EVP_MD_get0_name(kt), 0);
1252 ctx->params[1] = OSSL_PARAM_construct_octet_string("key", ctx->key, (size_t)EVP_MD_size(kt));
1253 ctx->params[2] = OSSL_PARAM_construct_end();
1254
1255 if (!EVP_MAC_init(ctx->ctx, NULL, 0, ctx->params))
1256 {
1257 crypto_msg(M_FATAL, "EVP_MAC_init failed");
1258 }
1259
1260 EVP_MD_free(kt);
1261}
1262
1263void
1265{
1266 EVP_MAC_init(ctx->ctx, NULL, 0, NULL);
1267}
1268
1269int
1271{
1272 return (int)EVP_MAC_CTX_get_mac_size(ctx->ctx);
1273}
1274
1275void
1277{
1278 /* The OpenSSL MAC API lacks a reset method and passing NULL as params
1279 * does not reset it either, so use the params array to reinitialise it the
1280 * same way as before */
1281 if (!EVP_MAC_init(ctx->ctx, NULL, 0, ctx->params))
1282 {
1283 crypto_msg(M_FATAL, "EVP_MAC_init failed");
1284 }
1285}
1286
1287void
1288hmac_ctx_update(hmac_ctx_t *ctx, const uint8_t *src, int src_len)
1289{
1290 EVP_MAC_update(ctx->ctx, src, src_len);
1291}
1292
1293void
1294hmac_ctx_final(hmac_ctx_t *ctx, uint8_t *dst)
1295{
1296 /* The calling code always gives us a buffer that has the size of our
1297 * algorithm */
1298 size_t in_hmac_len = EVP_MAC_CTX_get_mac_size(ctx->ctx);
1299
1300 EVP_MAC_final(ctx->ctx, dst, &in_hmac_len, in_hmac_len);
1301}
1302#endif /* if OPENSSL_VERSION_NUMBER < 0x30000000L */
1303
1304int
1305memcmp_constant_time(const void *a, const void *b, size_t size)
1306{
1307 return CRYPTO_memcmp(a, b, size);
1308}
1309#if (OPENSSL_VERSION_NUMBER >= 0x30000000L) && !defined(LIBRESSL_VERSION_NUMBER)
1310bool
1311ssl_tls1_PRF(const uint8_t *seed, size_t seed_len, const uint8_t *secret, size_t secret_len,
1312 uint8_t *output, size_t output_len)
1313{
1314 bool ret = true;
1315 EVP_KDF_CTX *kctx = NULL;
1316
1317
1318 EVP_KDF *kdf = EVP_KDF_fetch(NULL, "TLS1-PRF", NULL);
1319 if (!kdf)
1320 {
1321 goto err;
1322 }
1323
1324 kctx = EVP_KDF_CTX_new(kdf);
1325
1326 if (!kctx)
1327 {
1328 goto err;
1329 }
1330
1331 OSSL_PARAM params[4];
1332
1333 /* The OpenSSL APIs require us to cast the const aways even though the
1334 * strings are never changed and only read */
1335 params[0] =
1336 OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, SN_md5_sha1, strlen(SN_md5_sha1));
1337 params[1] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SECRET, (uint8_t *)secret,
1338 secret_len);
1339 params[2] =
1340 OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED, (uint8_t *)seed, seed_len);
1341 params[3] = OSSL_PARAM_construct_end();
1342
1343 if (EVP_KDF_derive(kctx, output, output_len, params) <= 0)
1344 {
1345 crypto_msg(D_TLS_DEBUG_LOW, "Generating TLS 1.0 PRF using "
1346 "EVP_KDF_derive failed");
1347 goto err;
1348 }
1349
1350 goto out;
1351
1352err:
1353 ret = false;
1354out:
1355 EVP_KDF_CTX_free(kctx);
1356 EVP_KDF_free(kdf);
1357
1358 return ret;
1359}
1360#elif defined(OPENSSL_IS_AWSLC)
1361bool
1362ssl_tls1_PRF(const uint8_t *label, size_t label_len, const uint8_t *sec, size_t slen, uint8_t *out1,
1363 size_t olen)
1364{
1365 return CRYPTO_tls1_prf(EVP_md5_sha1(), out1, olen, sec, slen,
1366 (const char *)label, label_len, NULL, 0, NULL, 0);
1367}
1368#elif !defined(LIBRESSL_VERSION_NUMBER) && !defined(ENABLE_CRYPTO_WOLFSSL)
1369#if defined(__GNUC__) || defined(__clang__)
1370#pragma GCC diagnostic push
1371#pragma GCC diagnostic ignored "-Wconversion"
1372#endif
1373
1374bool
1375ssl_tls1_PRF(const uint8_t *seed, size_t seed_len, const uint8_t *secret, size_t secret_len,
1376 uint8_t *output, size_t output_len)
1377{
1378 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_TLS1_PRF, NULL);
1379 if (!pctx)
1380 {
1381 return false;
1382 }
1383
1384 bool ret = false;
1385 if (!EVP_PKEY_derive_init(pctx))
1386 {
1387 goto out;
1388 }
1389
1390 if (!EVP_PKEY_CTX_set_tls1_prf_md(pctx, EVP_md5_sha1()))
1391 {
1392 goto out;
1393 }
1394
1395 if (!EVP_PKEY_CTX_set1_tls1_prf_secret(pctx, secret, secret_len))
1396 {
1397 goto out;
1398 }
1399
1400 if (!EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed, seed_len))
1401 {
1402 goto out;
1403 }
1404
1405 size_t out_len = output_len;
1406 if (!EVP_PKEY_derive(pctx, output, &out_len))
1407 {
1408 goto out;
1409 }
1410 if (out_len != output_len)
1411 {
1412 goto out;
1413 }
1414 ret = true;
1415out:
1416 EVP_PKEY_CTX_free(pctx);
1417 return ret;
1418}
1419
1420#if defined(__GNUC__) || defined(__clang__)
1421#pragma GCC diagnostic pop
1422#endif
1423
1424#else /* if defined(LIBRESSL_VERSION_NUMBER) */
1425/* LibreSSL and wolfSSL do not expose a TLS 1.0/1.1 PRF via the same APIs as
1426 * OpenSSL does. As result they will only be able to support
1427 * peers that support TLS EKM like when running with OpenSSL 3.x FIPS */
1428bool
1429ssl_tls1_PRF(const uint8_t *label, size_t label_len, const uint8_t *sec, size_t slen, uint8_t *out1,
1430 size_t olen)
1431{
1432 return false;
1433}
1434#endif /* if LIBRESSL_VERSION_NUMBER */
1435#endif /* ENABLE_CRYPTO_OPENSSL */
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Allocate a buffer of the given size under garbage collection.
Definition buffer.c:77
char * string_alloc(const char *str, struct gc_arena *gc)
Duplicate a string, allocating memory under garbage collection.
Definition buffer.c:616
Buffer management functions and garbage collection.
#define BPTR(buf)
Return a pointer to the start of the buffer content.
Definition buffer.h:139
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
Definition buffer.h:705
static uint8_t * buf_write_alloc(struct buffer *buf, size_t size)
Reserve space at the end of a buffer for writing.
Definition buffer.h:1148
static bool buf_write(struct buffer *dest, const void *src, size_t size)
Append data to a buffer.
Definition buffer.h:1198
#define BLEN(buf)
Return the length of the buffer content in bytes.
Definition buffer.h:145
#define BCAP(buf)
Return the number of bytes available for appending to the buffer.
Definition buffer.h:153
static void check_malloc_return(void *p)
Abort if a memory allocation returned NULL.
Definition buffer.h:2079
#define ALLOC_OBJ_CLEAR(dptr, type)
Allocate and zero-initialise memory for a single object of the given type.
Definition buffer.h:1971
const char * translate_cipher_name_from_openvpn(const char *cipher_name)
Translate an OpenVPN cipher name to a crypto library cipher name.
Definition crypto.c:1798
const char * translate_cipher_name_to_openvpn(const char *cipher_name)
Translate a crypto library cipher name to an OpenVPN cipher name.
Definition crypto.c:1811
void print_cipher(const char *ciphername)
Print a cipher list entry.
Definition crypto.c:1749
Data Channel Cryptography Module.
Data Channel Cryptography SSL library-specific backend interface.
#define MAX_CIPHER_KEY_LENGTH
#define MAX_HMAC_KEY_LENGTH
#define OPENVPN_AEAD_TAG_LENGTH
#define OPENVPN_MODE_OFB
Cipher is in OFB mode.
int crypto_operation_t
#define OPENVPN_MODE_CFB
Cipher is in CFB mode.
#define OPENVPN_MODE_CBC
Cipher is in CBC mode.
void provider_t
#define OPENVPN_MODE_GCM
Cipher is in GCM mode.
void crypto_print_openssl_errors(const unsigned int flags)
Retrieve any occurred OpenSSL errors and print those errors.
bool ssl_tls1_PRF(const uint8_t *seed, size_t seed_len, const uint8_t *secret, size_t secret_len, uint8_t *output, size_t output_len)
Calculates the TLS 1.0-1.1 PRF function.
bool cipher_kt_mode_cbc(const char *ciphername)
Check if the supplied cipher is a supported CBC mode cipher.
void md_ctx_update(EVP_MD_CTX *ctx, const uint8_t *src, size_t src_len)
void show_available_engines(void)
const cipher_name_pair digest_name_translation_table[]
unsigned int cipher_kt_iv_size(const char *ciphername)
Returns the size of the IV used by the cipher, in bytes, or 0 if no IV is used.
void crypto_unload_provider(const char *provname, provider_t *provider)
Unloads the given (OpenSSL) provider.
void crypto_uninit_lib(void)
void hmac_ctx_reset(HMAC_CTX *ctx)
bool cipher_kt_mode_aead(const char *ciphername)
Check if the supplied cipher is a supported AEAD mode cipher.
void show_available_ciphers(void)
bool md_valid(const char *digest)
Return if a message digest parameters is valid given the name of the digest.
bool cipher_ctx_mode_cbc(const cipher_ctx_t *ctx)
Check if the supplied cipher is a supported CBC mode cipher.
void crypto_init_lib(void)
cipher_ctx_t * cipher_ctx_new(void)
Generic cipher functions.
bool cipher_kt_mode_ofb_cfb(const char *ciphername)
Check if the supplied cipher is a supported OFB or CFB mode cipher.
void print_digest(EVP_MD *digest, void *unused)
const char * md_kt_name(const char *mdname)
Retrieve a string describing the digest digest (e.g.
bool cipher_kt_insecure(const char *ciphername)
Returns true if we consider this cipher to be insecure.
int cipher_ctx_reset(EVP_CIPHER_CTX *ctx, const uint8_t *iv_buf)
static int cipher_name_cmp(const void *a, const void *b)
int cipher_kt_mode(const EVP_CIPHER *cipher_kt)
void crypto_clear_error(void)
const size_t digest_name_translation_table_count
int md_ctx_size(const EVP_MD_CTX *ctx)
unsigned int cipher_ctx_block_size(const EVP_CIPHER_CTX *ctx)
bool crypto_pem_decode(const char *name, struct buffer *dst, const struct buffer *src)
Decode a PEM buffer to binary data.
provider_t * crypto_load_provider(const char *provider)
Load the given (OpenSSL) providers.
static evp_cipher_type * cipher_get(const char *ciphername)
int cipher_ctx_update_ad(EVP_CIPHER_CTX *ctx, const uint8_t *src, int src_len)
bool cipher_ctx_mode_ofb_cfb(const cipher_ctx_t *ctx)
Check if the supplied cipher is a supported OFB or CFB mode cipher.
bool cipher_ctx_mode_aead(const cipher_ctx_t *ctx)
Check if the supplied cipher is a supported AEAD mode cipher.
int cipher_ctx_mode(const EVP_CIPHER_CTX *ctx)
EVP_MD_CTX * md_ctx_new(void)
unsigned int cipher_kt_tag_size(const char *ciphername)
Returns the MAC tag size of the cipher, in bytes.
int rand_bytes(uint8_t *output, int len)
Wrapper for secure random number generator.
void hmac_ctx_init(HMAC_CTX *ctx, const uint8_t *key, const char *mdname)
void cipher_ctx_init(EVP_CIPHER_CTX *ctx, const uint8_t *key, const char *ciphername, crypto_operation_t enc)
void md_ctx_free(EVP_MD_CTX *ctx)
HMAC_CTX * hmac_ctx_new(void)
const size_t cipher_name_translation_table_count
const char * cipher_kt_name(const char *ciphername)
Retrieve a normalised string describing the cipher (e.g.
void hmac_ctx_update(HMAC_CTX *ctx, const uint8_t *src, int src_len)
unsigned int cipher_kt_key_size(const char *ciphername)
Returns the size of keys used by the cipher, in bytes.
void hmac_ctx_free(HMAC_CTX *ctx)
void md_ctx_cleanup(EVP_MD_CTX *ctx)
void cipher_ctx_free(EVP_CIPHER_CTX *ctx)
void md_ctx_init(EVP_MD_CTX *ctx, const char *mdname)
void md_ctx_final(EVP_MD_CTX *ctx, uint8_t *dst)
int memcmp_constant_time(const void *a, const void *b, size_t size)
As memcmp(), but constant-time.
void crypto_init_lib_engine(const char *engine_name)
int cipher_ctx_final_check_tag(EVP_CIPHER_CTX *ctx, uint8_t *dst, int *dst_len, uint8_t *tag, size_t tag_len)
int cipher_ctx_final(EVP_CIPHER_CTX *ctx, uint8_t *dst, int *dst_len)
unsigned int cipher_ctx_iv_length(const EVP_CIPHER_CTX *ctx)
const cipher_name_pair cipher_name_translation_table[]
Cipher name translation table.
int cipher_ctx_update(EVP_CIPHER_CTX *ctx, uint8_t *dst, int *dst_len, uint8_t *src, int src_len)
int cipher_ctx_get_tag(EVP_CIPHER_CTX *ctx, uint8_t *tag_buf, int tag_size)
unsigned int cipher_kt_block_size(const char *ciphername)
Returns the block size of the cipher, in bytes.
int hmac_ctx_size(HMAC_CTX *ctx)
static evp_md_type * md_get(const char *digest)
void hmac_ctx_final(HMAC_CTX *ctx, uint8_t *dst)
void hmac_ctx_cleanup(HMAC_CTX *ctx)
unsigned char md_kt_size(const char *mdname)
Returns the size of the message digest, in bytes.
void show_available_digests(void)
bool cipher_valid_reason(const char *ciphername, const char **reason)
Returns if the cipher is valid, based on the given cipher name and provides a reason if invalid.
bool crypto_pem_encode(const char *name, struct buffer *dst, const struct buffer *src, struct gc_arena *gc)
Encode binary data as PEM.
#define crypto_msg(flags,...)
Retrieve any OpenSSL errors, then print the supplied error message.
const EVP_CIPHER evp_cipher_type
const EVP_MD evp_md_type
#define D_TLS_DEBUG_LOW
Definition errlevel.h:76
#define D_TLS_DEBUG_MED
Definition errlevel.h:156
#define D_CRYPT_ERRORS
Definition errlevel.h:57
#define D_LOW
Definition errlevel.h:96
#define M_INFO
Definition errlevel.h:54
OpenSSL compatibility stub.
static openssl_err_t ERR_get_error_all(const char **file, int *line, const char **func, const char **data, int *flags)
static void EVP_CIPHER_free(const EVP_CIPHER *cipher)
void OSSL_PROVIDER
#define EVP_MD_get0_name
static const EVP_CIPHER * EVP_CIPHER_fetch(void *ctx, const char *algorithm, const char *properties)
#define EVP_CIPH_FLAG_ENC_THEN_MAC
static void EVP_MD_free(const EVP_MD *md)
static const EVP_MD * EVP_MD_fetch(void *ctx, const char *algorithm, const char *properties)
unsigned long openssl_err_t
#define EVP_CIPHER_get0_name
#define EVP_CIPHER_CTX_get_mode
#define SIZE(x)
Definition basic.h:29
static bool check_debug_level(msglvl_t level)
Definition error.h:251
#define M_FATAL
Definition error.h:90
#define dmsg(flags,...)
Definition error.h:172
#define msg(flags,...)
Definition error.h:152
#define ASSERT(x)
Definition error.h:219
#define M_WARN
Definition error.h:92
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
uint8_t * data
Pointer to the allocated memory.
Definition buffer.h:78
Struct used in cipher name translation table.
const char * openvpn_name
Cipher name used by OpenVPN.
const char * lib_name
Cipher name used by crypto library.
const EVP_CIPHER * list[1000]
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
mbedtls_svc_key_id_t key
Container for unidirectional cipher and HMAC key material.
Definition crypto.h:152
#define unlikely(x)
Definition syshead.h:35
static int cleanup(void **state)
struct gc_arena gc
Definition test_ssl.c:135