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