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