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)
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, "", CBPTR(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(CBPTR(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_cleanse(data_read, data_read_len);
532 OPENSSL_free(data_read);
533 if (!BIO_free(bio))
534 {
535 ret = false;
536 }
537
538 return ret;
539}
540
541/*
542 *
543 * Random number functions, used in cases where we want
544 * reasonably strong cryptographic random number generation
545 * without depleting our entropy pool. Used for random
546 * IV values and a number of other miscellaneous tasks.
547 *
548 */
549
550int
551rand_bytes(uint8_t *output, int len)
552{
553 if (unlikely(1 != RAND_bytes(output, len)))
554 {
555 crypto_msg(D_CRYPT_ERRORS, "RAND_bytes() failed");
556 return 0;
557 }
558 return 1;
559}
560
561/*
562 *
563 * Generic cipher key type functions
564 *
565 */
566
567static evp_cipher_type *
568cipher_get(const char *ciphername)
569{
570 ASSERT(ciphername);
571
572 /* "none" is a valid OpenVPN cipher name that OpenSSL does not know.
573 * Return NULL without asking OpenSSL: a failed EVP_CIPHER_fetch() would
574 * leave an "unsupported" entry on the error queue that the cipher_kt_*()
575 * callers never clear. */
576 if (strcmp("none", ciphername) == 0)
577 {
578 return NULL;
579 }
580
581 ciphername = translate_cipher_name_from_openvpn(ciphername);
582 return EVP_CIPHER_fetch(NULL, ciphername, NULL);
583}
584
585bool
586cipher_valid_reason(const char *ciphername, const char **reason)
587{
588 bool ret = false;
589 evp_cipher_type *cipher = cipher_get(ciphername);
590 if (!cipher)
591 {
592 crypto_msg(D_LOW, "Cipher algorithm '%s' not found", ciphername);
593 *reason = "disabled because unknown";
594 goto out;
595 }
596
597#ifdef OPENSSL_FIPS
598 /* Rhel 8/CentOS 8 have a patched OpenSSL version that return a cipher
599 * here that is actually not usable if in FIPS mode */
600
601 if (FIPS_mode() && !(EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_FIPS))
602 {
603 msg(D_LOW,
604 "Cipher algorithm '%s' is known by OpenSSL library but "
605 "currently disabled by running in FIPS mode.",
606 ciphername);
607 *reason = "disabled by FIPS mode";
608 goto out;
609 }
610#endif
611 if (EVP_CIPHER_key_length(cipher) > MAX_CIPHER_KEY_LENGTH)
612 {
613 msg(D_LOW,
614 "Cipher algorithm '%s' uses a default key size (%d bytes) "
615 "which is larger than " PACKAGE_NAME "'s current maximum key size "
616 "(%d bytes)",
617 ciphername, EVP_CIPHER_key_length(cipher), MAX_CIPHER_KEY_LENGTH);
618 *reason = "disabled due to key size too large";
619 goto out;
620 }
621
622 ret = true;
623 *reason = NULL;
624out:
625 EVP_CIPHER_free(cipher);
626 return ret;
627}
628
629const char *
630cipher_kt_name(const char *ciphername)
631{
632 ASSERT(ciphername);
633 if (strcmp("none", ciphername) == 0)
634 {
635 return "[null-cipher]";
636 }
637
638 evp_cipher_type *cipher_kt = cipher_get(ciphername);
639 if (!cipher_kt)
640 {
641 return NULL;
642 }
643
644 const char *name = EVP_CIPHER_name(cipher_kt);
645 EVP_CIPHER_free(cipher_kt);
647}
648
649unsigned int
650cipher_kt_key_size(const char *ciphername)
651{
652 evp_cipher_type *cipher = cipher_get(ciphername);
653 int size = EVP_CIPHER_key_length(cipher);
654 ASSERT(size >= 0);
655 EVP_CIPHER_free(cipher);
656 return size;
657}
658
659unsigned int
660cipher_kt_iv_size(const char *ciphername)
661{
662 evp_cipher_type *cipher = cipher_get(ciphername);
663 int ivsize = EVP_CIPHER_iv_length(cipher);
664 ASSERT(ivsize >= 0);
665 EVP_CIPHER_free(cipher);
666 return ivsize;
667}
668
669unsigned int
670cipher_kt_block_size(const char *ciphername)
671{
672 /*
673 * OpenSSL reports OFB/CFB/GCM cipher block sizes as '1 byte'. To work
674 * around that, try to replace the mode with 'CBC' and return the block size
675 * reported for that cipher, if possible. If that doesn't work, just return
676 * the value reported by OpenSSL.
677 */
678 char *name = NULL;
679 char *mode_str = NULL;
680 const char *orig_name = NULL;
681 evp_cipher_type *cbc_cipher = NULL;
682 evp_cipher_type *cipher = cipher_get(ciphername);
683 if (!cipher)
684 {
685 return 0;
686 }
687
688 int block_size = EVP_CIPHER_block_size(cipher);
689
690 orig_name = EVP_CIPHER_name(cipher);
691 if (!orig_name)
692 {
693 goto cleanup;
694 }
695
696 name = string_alloc(translate_cipher_name_to_openvpn(orig_name), NULL);
697 mode_str = strrchr(name, '-');
698 if (!mode_str || strlen(mode_str) < 4)
699 {
700 goto cleanup;
701 }
702
703 strcpy(mode_str, "-CBC");
704
705 cbc_cipher = EVP_CIPHER_fetch(NULL, translate_cipher_name_from_openvpn(name), NULL);
706 if (cbc_cipher)
707 {
708 block_size = EVP_CIPHER_block_size(cbc_cipher);
709 }
710
711cleanup:
712 EVP_CIPHER_free(cbc_cipher);
713 EVP_CIPHER_free(cipher);
714 free(name);
715 ASSERT(block_size >= 0);
716 return block_size;
717}
718
719unsigned int
720cipher_kt_tag_size(const char *ciphername)
721{
722 if (cipher_kt_mode_aead(ciphername))
723 {
725 }
726 else
727 {
728 return 0;
729 }
730}
731
732bool
733cipher_kt_insecure(const char *ciphername)
734{
735 if (cipher_kt_block_size(ciphername) >= 128 / 8)
736 {
737 return false;
738 }
739#ifdef NID_chacha20_poly1305
740 evp_cipher_type *cipher = cipher_get(ciphername);
741 if (cipher)
742 {
743 bool ischachapoly = (EVP_CIPHER_nid(cipher) == NID_chacha20_poly1305);
744 EVP_CIPHER_free(cipher);
745 if (ischachapoly)
746 {
747 return false;
748 }
749 }
750#endif
751 return true;
752}
753
754int
755cipher_kt_mode(const EVP_CIPHER *cipher_kt)
756{
757 ASSERT(NULL != cipher_kt);
758 return EVP_CIPHER_mode(cipher_kt);
759}
760
761bool
762cipher_kt_mode_cbc(const char *ciphername)
763{
764 evp_cipher_type *cipher = cipher_get(ciphername);
765
766 bool ret = cipher
767 && (cipher_kt_mode(cipher) == OPENVPN_MODE_CBC
768 /* Exclude AEAD cipher modes, they require a different API */
769#ifdef EVP_CIPH_FLAG_CTS
770 && !(EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_CTS)
771#endif
772 && !(EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
773 && !(EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_ENC_THEN_MAC));
774 EVP_CIPHER_free(cipher);
775 return ret;
776}
777
778bool
779cipher_kt_mode_ofb_cfb(const char *ciphername)
780{
781 evp_cipher_type *cipher = cipher_get(ciphername);
782 bool ofb_cfb = cipher
783 && (cipher_kt_mode(cipher) == OPENVPN_MODE_OFB
784 || cipher_kt_mode(cipher) == OPENVPN_MODE_CFB)
785 /* Exclude AEAD cipher modes, they require a different API */
786 && !(EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER);
787 EVP_CIPHER_free(cipher);
788 return ofb_cfb;
789}
790
791bool
792cipher_kt_mode_aead(const char *ciphername)
793{
794 bool isaead = false;
795
796 evp_cipher_type *cipher = cipher_get(ciphername);
797 if (cipher)
798 {
799 if (EVP_CIPHER_mode(cipher) == OPENVPN_MODE_GCM)
800 {
801 isaead = true;
802 }
803
804#ifdef NID_chacha20_poly1305
805 if (EVP_CIPHER_nid(cipher) == NID_chacha20_poly1305)
806 {
807 isaead = true;
808 }
809#endif
810 }
811
812 EVP_CIPHER_free(cipher);
813
814 return isaead;
815}
816
817/*
818 *
819 * Generic cipher context functions
820 *
821 */
822
825{
826 EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
828 return ctx;
829}
830
831void
832cipher_ctx_free(EVP_CIPHER_CTX *ctx)
833{
834 EVP_CIPHER_CTX_free(ctx);
835}
836
837void
838cipher_ctx_init(EVP_CIPHER_CTX *ctx, const uint8_t *key, const char *ciphername,
840{
841 ASSERT(NULL != ciphername && NULL != ctx);
842 evp_cipher_type *kt = cipher_get(ciphername);
843
844 EVP_CIPHER_CTX_reset(ctx);
845 if (!EVP_CipherInit_ex(ctx, kt, NULL, key, NULL, enc))
846 {
847 crypto_msg(M_FATAL, "EVP cipher init #2");
848 }
849
850 /* make sure we used a big enough key */
851 ASSERT(EVP_CIPHER_CTX_key_length(ctx) <= EVP_CIPHER_key_length(kt));
852 EVP_CIPHER_free(kt);
853}
854
855unsigned int
856cipher_ctx_iv_length(const EVP_CIPHER_CTX *ctx)
857{
858 return EVP_CIPHER_CTX_iv_length(ctx);
859}
860
861int
862cipher_ctx_get_tag(EVP_CIPHER_CTX *ctx, uint8_t *tag_buf, int tag_size)
863{
864 return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, tag_size, tag_buf);
865}
866
867unsigned int
868cipher_ctx_block_size(const EVP_CIPHER_CTX *ctx)
869{
870 return EVP_CIPHER_CTX_block_size(ctx);
871}
872
873int
874cipher_ctx_mode(const EVP_CIPHER_CTX *ctx)
875{
876 return EVP_CIPHER_CTX_mode(ctx);
877}
878
879bool
881{
882 if (!ctx)
883 {
884 return false;
885 }
886
887 unsigned long flags = EVP_CIPHER_CTX_flags(ctx);
888 int mode = EVP_CIPHER_CTX_mode(ctx);
889
890 return mode == EVP_CIPH_CBC_MODE
891 /* Exclude AEAD cipher modes, they require a different API */
892#ifdef EVP_CIPH_FLAG_CTS
893 && !(flags & EVP_CIPH_FLAG_CTS)
894#endif
895 && !(flags & EVP_CIPH_FLAG_AEAD_CIPHER);
896}
897
898bool
900{
901 if (!ctx)
902 {
903 return false;
904 }
905
906 int mode = EVP_CIPHER_CTX_get_mode(ctx);
907
908 return (mode == EVP_CIPH_OFB_MODE || mode == EVP_CIPH_CFB_MODE)
909 /* Exclude AEAD cipher modes, they require a different API */
910 && !(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_FLAG_AEAD_CIPHER);
911}
912
913bool
915{
916 if (ctx)
917 {
918 unsigned long flags = EVP_CIPHER_CTX_flags(ctx);
919 if (flags & EVP_CIPH_FLAG_AEAD_CIPHER)
920 {
921 return true;
922 }
923
924#if defined(NID_chacha20_poly1305) && OPENSSL_VERSION_NUMBER < 0x30000000L
925 if (EVP_CIPHER_CTX_nid(ctx) == NID_chacha20_poly1305)
926 {
927 return true;
928 }
929#endif
930 }
931
932 return false;
933}
934
935
936int
937cipher_ctx_reset(EVP_CIPHER_CTX *ctx, const uint8_t *iv_buf)
938{
939 return EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv_buf, -1);
940}
941
942int
943cipher_ctx_update_ad(EVP_CIPHER_CTX *ctx, const uint8_t *src, int src_len)
944{
945 int len;
946 if (!EVP_CipherUpdate(ctx, NULL, &len, src, src_len))
947 {
948 crypto_msg(M_FATAL, "%s: EVP_CipherUpdate() failed", __func__);
949 }
950 return 1;
951}
952
953int
954cipher_ctx_update(EVP_CIPHER_CTX *ctx, uint8_t *dst, int *dst_len, const uint8_t *src, int src_len)
955{
956 if (!EVP_CipherUpdate(ctx, dst, dst_len, src, src_len))
957 {
958 crypto_msg(M_FATAL, "%s: EVP_CipherUpdate() failed", __func__);
959 }
960 return 1;
961}
962
963int
964cipher_ctx_final(EVP_CIPHER_CTX *ctx, uint8_t *dst, int *dst_len)
965{
966 return EVP_CipherFinal(ctx, dst, dst_len);
967}
968
969int
970cipher_ctx_final_check_tag(EVP_CIPHER_CTX *ctx, uint8_t *dst, int *dst_len, uint8_t *tag,
971 size_t tag_len)
972{
973 ASSERT(tag_len < INT_MAX);
974 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, (int)tag_len, tag))
975 {
976 return 0;
977 }
978
979 return cipher_ctx_final(ctx, dst, dst_len);
980}
981
982/*
983 *
984 * Generic message digest information functions
985 *
986 */
987
988
989static evp_md_type *
990md_get(const char *digest)
991{
992 evp_md_type *md = NULL;
993 ASSERT(digest);
994 md = EVP_MD_fetch(NULL, digest, NULL);
995 if (!md)
996 {
997 crypto_msg(M_FATAL, "Message hash algorithm '%s' not found", digest);
998 }
999 if (EVP_MD_size(md) > MAX_HMAC_KEY_LENGTH)
1000 {
1002 "Message hash algorithm '%s' uses a default hash "
1003 "size (%d bytes) which is larger than " PACKAGE_NAME "'s current "
1004 "maximum hash size (%d bytes)",
1005 digest, (int)EVP_MD_size(md), MAX_HMAC_KEY_LENGTH);
1006 }
1007 return md;
1008}
1009
1010
1011bool
1012md_valid(const char *digest)
1013{
1014 evp_md_type *md = EVP_MD_fetch(NULL, digest, NULL);
1015 bool valid = (md != NULL);
1016 EVP_MD_free(md);
1017 return valid;
1018}
1019
1020
1021/* Since we used the OpenSSL <=1.1 names as part of our OCC message, they
1022 * are now unfortunately part of our wire protocol.
1023 *
1024 * OpenSSL 3.0 will still accept the "old" names so we do not need to use
1025 * this translation table for forward lookup, only for returning the name
1026 * with md_kt_name() */
1028 { "BLAKE2s256", "BLAKE2S-256" },
1029 { "BLAKE2b512", "BLAKE2B-512" },
1030 { "RIPEMD160", "RIPEMD-160" },
1031 { "SHA224", "SHA2-224" },
1032 { "SHA256", "SHA2-256" },
1033 { "SHA384", "SHA2-384" },
1034 { "SHA512", "SHA2-512" },
1035 { "SHA512-224", "SHA2-512/224" },
1036 { "SHA512-256", "SHA2-512/256" },
1037 { "SHAKE128", "SHAKE-128" },
1038 { "SHAKE256", "SHAKE-256" },
1039};
1042
1043const char *
1044md_kt_name(const char *mdname)
1045{
1046 if (!strcmp("none", mdname))
1047 {
1048 return "[null-digest]";
1049 }
1050 evp_md_type *kt = md_get(mdname);
1051 const char *name = EVP_MD_get0_name(kt);
1052
1053 /* Search for a digest name translation */
1054 for (size_t i = 0; i < digest_name_translation_table_count; i++)
1055 {
1057 if (!strcmp(name, pair->lib_name))
1058 {
1059 name = pair->openvpn_name;
1060 }
1061 }
1062
1063 EVP_MD_free(kt);
1064 return name;
1065}
1066
1067unsigned char
1068md_kt_size(const char *mdname)
1069{
1070 if (!strcmp("none", mdname))
1071 {
1072 return 0;
1073 }
1074 evp_md_type *kt = md_get(mdname);
1075 unsigned char size = (unsigned char)EVP_MD_size(kt);
1076 EVP_MD_free(kt);
1077 return size;
1078}
1079
1080
1081/*
1082 *
1083 * Generic message digest functions
1084 *
1085 */
1086
1087EVP_MD_CTX *
1089{
1090 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
1092 return ctx;
1093}
1094
1095void
1096md_ctx_free(EVP_MD_CTX *ctx)
1097{
1098 EVP_MD_CTX_free(ctx);
1099}
1100
1101void
1102md_ctx_init(EVP_MD_CTX *ctx, const char *mdname)
1103{
1104 evp_md_type *kt = md_get(mdname);
1105 ASSERT(NULL != ctx && NULL != kt);
1106
1107 EVP_MD_CTX_init(ctx);
1108 if (!EVP_DigestInit(ctx, kt))
1109 {
1110 crypto_msg(M_FATAL, "EVP_DigestInit failed");
1111 }
1112 EVP_MD_free(kt);
1113}
1114
1115void
1116md_ctx_cleanup(EVP_MD_CTX *ctx)
1117{
1118 EVP_MD_CTX_reset(ctx);
1119}
1120
1121int
1122md_ctx_size(const EVP_MD_CTX *ctx)
1123{
1124 return (int)EVP_MD_CTX_size(ctx);
1125}
1126
1127void
1128md_ctx_update(EVP_MD_CTX *ctx, const uint8_t *src, size_t src_len)
1129{
1130 EVP_DigestUpdate(ctx, src, src_len);
1131}
1132
1133void
1134md_ctx_final(EVP_MD_CTX *ctx, uint8_t *dst)
1135{
1136 unsigned int in_md_len = 0;
1137
1138 EVP_DigestFinal(ctx, dst, &in_md_len);
1139}
1140
1141
1142/*
1143 *
1144 * Generic HMAC functions
1145 *
1146 */
1147#if OPENSSL_VERSION_NUMBER < 0x30000000L
1148HMAC_CTX *
1150{
1151 HMAC_CTX *ctx = HMAC_CTX_new();
1153 return ctx;
1154}
1155
1156void
1157hmac_ctx_free(HMAC_CTX *ctx)
1158{
1159 HMAC_CTX_free(ctx);
1160}
1161
1162void
1163hmac_ctx_init(HMAC_CTX *ctx, const uint8_t *key, const char *mdname)
1164{
1165 evp_md_type *kt = md_get(mdname);
1166 ASSERT(NULL != kt && NULL != ctx);
1167
1168 int key_len = (int)EVP_MD_size(kt);
1169 HMAC_CTX_reset(ctx);
1170 if (!HMAC_Init_ex(ctx, key, key_len, kt, NULL))
1171 {
1172 crypto_msg(M_FATAL, "HMAC_Init_ex failed");
1173 }
1174
1175 /* make sure we used a big enough key */
1176 ASSERT((ssize_t)HMAC_size(ctx) <= key_len);
1177}
1178
1179void
1180hmac_ctx_cleanup(HMAC_CTX *ctx)
1181{
1182 HMAC_CTX_reset(ctx);
1183}
1184
1185int
1186hmac_ctx_size(HMAC_CTX *ctx)
1187{
1188 return (int)HMAC_size(ctx);
1189}
1190
1191void
1192hmac_ctx_reset(HMAC_CTX *ctx)
1193{
1194 if (!HMAC_Init_ex(ctx, NULL, 0, NULL, NULL))
1195 {
1196 crypto_msg(M_FATAL, "HMAC_Init_ex failed");
1197 }
1198}
1199
1200void
1201hmac_ctx_update(HMAC_CTX *ctx, const uint8_t *src, int src_len)
1202{
1203 HMAC_Update(ctx, src, src_len);
1204}
1205
1206void
1207hmac_ctx_final(HMAC_CTX *ctx, uint8_t *dst)
1208{
1209 unsigned int in_hmac_len = 0;
1210
1211 HMAC_Final(ctx, dst, &in_hmac_len);
1212}
1213#else /* if OPENSSL_VERSION_NUMBER < 0x30000000L */
1214hmac_ctx_t *
1215hmac_ctx_new(void)
1216{
1217 hmac_ctx_t *ctx;
1219 EVP_MAC *hmac = EVP_MAC_fetch(NULL, "HMAC", NULL);
1220 ctx->ctx = EVP_MAC_CTX_new(hmac);
1221 check_malloc_return(ctx->ctx);
1222
1223 EVP_MAC_free(hmac);
1224
1225 return ctx;
1226}
1227
1228void
1230{
1231 EVP_MAC_CTX_free(ctx->ctx);
1232 secure_memzero(ctx, sizeof(hmac_ctx_t));
1233 free(ctx);
1234}
1235
1236void
1237hmac_ctx_init(hmac_ctx_t *ctx, const uint8_t *key, const char *mdname)
1238{
1239 evp_md_type *kt = md_get(mdname);
1240 ASSERT(NULL != kt && NULL != ctx && ctx->ctx != NULL);
1241
1242 /* We need to make a copy of the key since the OSSL parameters
1243 * only reference it */
1244 memcpy(ctx->key, key, (size_t)EVP_MD_size(kt));
1245
1246 /* Lookup/setting of parameters in OpenSSL 3.0 are string based
1247 *
1248 * The OSSL_PARAM_construct_utf8_string needs a non const str but this
1249 * only used for lookup so we cast (as OpenSSL also does internally)
1250 * the constness away here.
1251 */
1252 ctx->params[0] = OSSL_PARAM_construct_utf8_string("digest", (char *)EVP_MD_get0_name(kt), 0);
1253 ctx->params[1] = OSSL_PARAM_construct_octet_string("key", ctx->key, (size_t)EVP_MD_size(kt));
1254 ctx->params[2] = OSSL_PARAM_construct_end();
1255
1256 if (!EVP_MAC_init(ctx->ctx, NULL, 0, ctx->params))
1257 {
1258 crypto_msg(M_FATAL, "EVP_MAC_init failed");
1259 }
1260
1261 EVP_MD_free(kt);
1262}
1263
1264void
1266{
1267 EVP_MAC_init(ctx->ctx, NULL, 0, NULL);
1268}
1269
1270int
1272{
1273 return (int)EVP_MAC_CTX_get_mac_size(ctx->ctx);
1274}
1275
1276void
1278{
1279 /* OpenSSL 3.0.3 fixed EVP_MAC reinitialization with an existing key.
1280 * Older versions need the parameters, including the key, to reset. */
1281#if OPENSSL_VERSION_NUMBER >= 0x30000030L
1282 const OSSL_PARAM *params = NULL;
1283#else
1284 const OSSL_PARAM *params = ctx->params;
1285#endif
1286 if (!EVP_MAC_init(ctx->ctx, NULL, 0, params))
1287 {
1288 crypto_msg(M_FATAL, "EVP_MAC_init failed");
1289 }
1290}
1291
1292void
1293hmac_ctx_update(hmac_ctx_t *ctx, const uint8_t *src, int src_len)
1294{
1295 EVP_MAC_update(ctx->ctx, src, src_len);
1296}
1297
1298void
1299hmac_ctx_final(hmac_ctx_t *ctx, uint8_t *dst)
1300{
1301 /* The calling code always gives us a buffer that has the size of our
1302 * algorithm */
1303 size_t in_hmac_len = EVP_MAC_CTX_get_mac_size(ctx->ctx);
1304
1305 EVP_MAC_final(ctx->ctx, dst, &in_hmac_len, in_hmac_len);
1306}
1307#endif /* if OPENSSL_VERSION_NUMBER < 0x30000000L */
1308
1309int
1310memcmp_constant_time(const void *a, const void *b, size_t size)
1311{
1312 return CRYPTO_memcmp(a, b, size);
1313}
1314#if (OPENSSL_VERSION_NUMBER >= 0x30000000L) && !defined(LIBRESSL_VERSION_NUMBER)
1315bool
1316ssl_tls1_PRF(const uint8_t *seed, size_t seed_len, const uint8_t *secret, size_t secret_len,
1317 uint8_t *output, size_t output_len)
1318{
1319 bool ret = true;
1320 EVP_KDF_CTX *kctx = NULL;
1321
1322
1323 EVP_KDF *kdf = EVP_KDF_fetch(NULL, "TLS1-PRF", NULL);
1324 if (!kdf)
1325 {
1326 goto err;
1327 }
1328
1329 kctx = EVP_KDF_CTX_new(kdf);
1330
1331 if (!kctx)
1332 {
1333 goto err;
1334 }
1335
1336 OSSL_PARAM params[4];
1337
1338 /* The OpenSSL APIs require us to cast the const aways even though the
1339 * strings are never changed and only read */
1340 params[0] =
1341 OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, SN_md5_sha1, strlen(SN_md5_sha1));
1342 params[1] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SECRET, (uint8_t *)secret,
1343 secret_len);
1344 params[2] =
1345 OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED, (uint8_t *)seed, seed_len);
1346 params[3] = OSSL_PARAM_construct_end();
1347
1348 if (EVP_KDF_derive(kctx, output, output_len, params) <= 0)
1349 {
1350 crypto_msg(D_TLS_DEBUG_LOW, "Generating TLS 1.0 PRF using "
1351 "EVP_KDF_derive failed");
1352 goto err;
1353 }
1354
1355 goto out;
1356
1357err:
1358 ret = false;
1359out:
1360 EVP_KDF_CTX_free(kctx);
1361 EVP_KDF_free(kdf);
1362
1363 return ret;
1364}
1365#elif defined(OPENSSL_IS_AWSLC)
1366bool
1367ssl_tls1_PRF(const uint8_t *label, size_t label_len, const uint8_t *sec, size_t slen, uint8_t *out1,
1368 size_t olen)
1369{
1370 return CRYPTO_tls1_prf(EVP_md5_sha1(), out1, olen, sec, slen,
1371 (const char *)label, label_len, NULL, 0, NULL, 0);
1372}
1373#elif !defined(LIBRESSL_VERSION_NUMBER) && !defined(ENABLE_CRYPTO_WOLFSSL)
1374#if defined(__GNUC__) || defined(__clang__)
1375#pragma GCC diagnostic push
1376#pragma GCC diagnostic ignored "-Wconversion"
1377#endif
1378
1379bool
1380ssl_tls1_PRF(const uint8_t *seed, size_t seed_len, const uint8_t *secret, size_t secret_len,
1381 uint8_t *output, size_t output_len)
1382{
1383 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_TLS1_PRF, NULL);
1384 if (!pctx)
1385 {
1386 return false;
1387 }
1388
1389 bool ret = false;
1390 if (!EVP_PKEY_derive_init(pctx))
1391 {
1392 goto out;
1393 }
1394
1395 if (!EVP_PKEY_CTX_set_tls1_prf_md(pctx, EVP_md5_sha1()))
1396 {
1397 goto out;
1398 }
1399
1400 if (!EVP_PKEY_CTX_set1_tls1_prf_secret(pctx, secret, secret_len))
1401 {
1402 goto out;
1403 }
1404
1405 if (!EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed, seed_len))
1406 {
1407 goto out;
1408 }
1409
1410 size_t out_len = output_len;
1411 if (!EVP_PKEY_derive(pctx, output, &out_len))
1412 {
1413 goto out;
1414 }
1415 if (out_len != output_len)
1416 {
1417 goto out;
1418 }
1419 ret = true;
1420out:
1421 EVP_PKEY_CTX_free(pctx);
1422 return ret;
1423}
1424
1425#if defined(__GNUC__) || defined(__clang__)
1426#pragma GCC diagnostic pop
1427#endif
1428
1429#else /* if defined(LIBRESSL_VERSION_NUMBER) */
1430/* LibreSSL and wolfSSL do not expose a TLS 1.0/1.1 PRF via the same APIs as
1431 * OpenSSL does. As result they will only be able to support
1432 * peers that support TLS EKM like when running with OpenSSL 3.x FIPS */
1433bool
1434ssl_tls1_PRF(const uint8_t *label, size_t label_len, const uint8_t *sec, size_t slen, uint8_t *out1,
1435 size_t olen)
1436{
1437 return false;
1438}
1439#endif /* if LIBRESSL_VERSION_NUMBER */
1440#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:601
Buffer management functions and garbage collection.
#define CBPTR(buf)
Return a const pointer to the start of the buffer content.
Definition buffer.h:141
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
Definition buffer.h:748
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:1189
static bool buf_write(struct buffer *dest, const void *src, size_t size)
Append data to a buffer.
Definition buffer.h:1239
#define BLEN(buf)
Return the length of the buffer content in bytes.
Definition buffer.h:151
#define BCAP(buf)
Return the number of bytes available for appending to the buffer.
Definition buffer.h:161
static void check_malloc_return(void *p)
Abort if a memory allocation returned NULL.
Definition buffer.h:2135
#define ALLOC_OBJ_CLEAR(dptr, type)
Allocate and zero-initialise memory for a single object of the given type.
Definition buffer.h:2027
#define PACKAGE_NAME
Definition config.h:492
#define ENABLE_OFB_CFB_MODE
Definition config.h:56
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:1763
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:1776
void print_cipher(const char *ciphername)
Print a cipher list entry.
Definition crypto.c:1717
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.
int cipher_ctx_update(EVP_CIPHER_CTX *ctx, uint8_t *dst, int *dst_len, const uint8_t *src, int src_len)
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_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 const EVP_MD * EVP_MD_fetch(const void *ctx, const char *algorithm, const char *properties)
static void EVP_CIPHER_free(const EVP_CIPHER *cipher)
void OSSL_PROVIDER
#define EVP_MD_get0_name
#define EVP_CIPH_FLAG_ENC_THEN_MAC
static void EVP_MD_free(const EVP_MD *md)
unsigned long openssl_err_t
#define EVP_CIPHER_get0_name
#define EVP_CIPHER_CTX_get_mode
static const EVP_CIPHER * EVP_CIPHER_fetch(const void *ctx, const char *algorithm, const char *properties)
#define SIZE(x)
Definition basic.h:29
static bool check_debug_level(msglvl_t level)
Definition error.h:253
#define M_FATAL
Definition error.h:90
#define dmsg(flags,...)
Definition error.h:164
#define msg(flags,...)
Definition error.h:152
#define ASSERT(x)
Definition error.h:221
#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:138