35 #if defined(ENABLE_CRYPTO_OPENSSL)
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>
52 #if !defined(LIBRESSL_VERSION_NUMBER)
53 #include <openssl/kdf.h>
55 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
56 #include <openssl/provider.h>
57 #include <openssl/core_names.h>
60 #if defined(_WIN32) && defined(OPENSSL_NO_EC)
61 #error Windows build with OPENSSL_NO_EC: disabling EC key is not supported.
66 #pragma warning(disable: 4996)
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
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
81 #if HAVE_OPENSSL_ENGINE
82 #include <openssl/ui.h>
83 #include <openssl/engine.h>
85 static bool engine_initialized =
false;
87 static ENGINE *engine_persist = NULL;
91 try_load_engine(
const char *engine)
93 ENGINE *e = ENGINE_by_id(
"dynamic");
96 if (!ENGINE_ctrl_cmd_string(e,
"SO_PATH", engine, 0)
97 || !ENGINE_ctrl_cmd_string(e,
"LOAD", NULL, 0))
107 setup_engine(
const char *engine)
111 ENGINE_load_builtin_engines();
115 if (strcmp(engine,
"auto") == 0)
117 msg(
M_INFO,
"Initializing OpenSSL auto engine support");
118 ENGINE_register_all_complete();
121 if ((e = ENGINE_by_id(engine)) == NULL
122 && (e = try_load_engine(engine)) == NULL)
128 if (!ENGINE_set_default(e, ENGINE_METHOD_ALL))
131 "OpenSSL error: ENGINE_set_default failed on engine '%s'",
135 msg(
M_INFO,
"Initializing OpenSSL support for engine '%s'",
146 #if HAVE_OPENSSL_ENGINE
147 if (!engine_initialized)
151 engine_persist = setup_engine(engine_name);
152 engine_initialized =
true;
155 msg(
M_WARN,
"Note: OpenSSL hardware crypto engine functionality is not available");
162 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
171 msg(
M_WARN,
"Note: OpenSSL provider functionality is not available");
179 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
180 if (!OSSL_PROVIDER_unload(provider))
196 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
204 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
212 FILE *fp = fopen(
"sdlog",
"w");
214 CRYPTO_mem_leaks_fp(fp);
218 #if HAVE_OPENSSL_ENGINE
219 if (engine_initialized)
222 engine_persist = NULL;
223 engine_initialized =
false;
237 unsigned long err = 0;
239 const char *file, *data, *func;
243 if (!(errflags & ERR_TXT_STRING))
249 if (ERR_GET_REASON(err) == SSL_R_NO_SHARED_CIPHER)
252 "in common with the client. Your --tls-cipher setting might be "
255 else if (ERR_GET_REASON(err) == SSL_R_UNSUPPORTED_PROTOCOL)
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");
269 msg(flags,
"OpenSSL: %s:%s", ERR_error_string(err, NULL), data);
273 msg(flags,
"OpenSSL: %s:%s:%s:%d:%s", ERR_error_string(err, NULL),
274 data, file, line, func);
290 crypto_malloc(
size_t size,
const char *file,
int line)
292 return dmalloc_malloc(file, line, size, DMALLOC_FUNC_MALLOC, 0, 0);
296 crypto_realloc(
void *ptr,
size_t size,
const char *file,
int line)
298 return dmalloc_realloc(file, line, ptr, size, DMALLOC_FUNC_REALLOC, 0);
302 crypto_free(
void *ptr)
304 dmalloc_free(__FILE__, __LINE__, ptr, DMALLOC_FUNC_FREE);
308 crypto_init_dmalloc(
void)
310 CRYPTO_set_mem_ex_functions(crypto_malloc,
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" },
329 const EVP_CIPHER *
const *cipher_a = a;
330 const EVP_CIPHER *
const *cipher_b = b;
351 msg(
M_WARN,
"WARNING: Too many ciphers, not showing all");
364 cipher_list->
list[cipher_list->
num++] = cipher;
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");
381 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
384 for (
int nid = 0; nid < 10000; ++nid)
386 #if defined(LIBRESSL_VERSION_NUMBER)
390 const EVP_CIPHER *cipher = NULL;
391 const char *name = OBJ_nid2sn(nid);
394 cipher = EVP_get_cipherbyname(name);
397 const EVP_CIPHER *cipher = EVP_get_cipherbynid(nid);
408 for (
size_t i = 0; i < cipher_list.
num; i++)
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++)
432 EVP_MD_size(digest) * 8);
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");
447 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
450 for (
int nid = 0; nid < 10000; ++nid)
455 #ifdef LIBRESSL_VERSION_NUMBER
456 const EVP_MD *digest = NULL;
457 const char *name = OBJ_nid2sn(nid);
460 digest = EVP_get_digestbyname(name);
463 const EVP_MD *digest = EVP_get_digestbynid(nid);
479 #if HAVE_OPENSSL_ENGINE
482 printf(
"OpenSSL Crypto Engines\n\n");
484 ENGINE_load_builtin_engines();
486 e = ENGINE_get_first();
492 e = ENGINE_get_next(e);
496 printf(
"Sorry, OpenSSL hardware crypto engine functionality is not available.\n");
506 BIO *bio = BIO_new(BIO_s_mem());
507 if (!bio || !PEM_write_bio(bio, name,
"",
BPTR(src),
BLEN(src)))
514 BIO_get_mem_ptr(bio, &bptr);
535 BIO *bio = BIO_new_mem_buf((
char *)
BPTR(src),
BLEN(src));
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,
552 if (strcmp(name, name_read))
555 "%s: unexpected PEM name (got '%s', expected '%s')",
556 __func__, name_read, name);
564 BCAP(dst), data_read_len);
567 memcpy(dst_data, data_read, data_read_len);
571 OPENSSL_free(name_read);
572 OPENSSL_free(header_read);
573 OPENSSL_free(data_read);
594 if (
unlikely(1 != RAND_bytes(output, len)))
625 *reason =
"disabled because unknown";
633 if (FIPS_mode() && !(EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_FIPS))
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";
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";
662 if (strcmp(
"none", ciphername) == 0)
664 return "[null-cipher]";
673 const char *name = EVP_CIPHER_name(cipher_kt);
682 int size = EVP_CIPHER_key_length(cipher);
691 int ivsize = EVP_CIPHER_iv_length(cipher);
706 char *mode_str = NULL;
707 const char *orig_name = NULL;
715 int block_size = EVP_CIPHER_block_size(cipher);
717 orig_name = EVP_CIPHER_name(cipher);
724 mode_str = strrchr(name,
'-');
725 if (!mode_str || strlen(mode_str) < 4)
730 strcpy(mode_str,
"-CBC");
735 block_size = EVP_CIPHER_block_size(cbc_cipher);
766 #ifdef NID_chacha20_poly1305
770 bool ischachapoly = (EVP_CIPHER_nid(cipher) == NID_chacha20_poly1305);
784 ASSERT(NULL != cipher_kt);
785 return EVP_CIPHER_mode(cipher_kt);
795 #ifdef EVP_CIPH_FLAG_CTS
796 && !(EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_CTS)
798 && !(EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER));
810 && !(EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER);
828 #ifdef NID_chacha20_poly1305
829 if (EVP_CIPHER_nid(cipher) == NID_chacha20_poly1305)
850 EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
858 EVP_CIPHER_CTX_free(ctx);
865 ASSERT(NULL != ciphername && NULL != ctx);
868 EVP_CIPHER_CTX_reset(ctx);
869 if (!EVP_CipherInit_ex(ctx, kt, NULL,
key, NULL, enc))
875 ASSERT(EVP_CIPHER_CTX_key_length(ctx) <= EVP_CIPHER_key_length(kt));
882 return EVP_CIPHER_CTX_iv_length(ctx);
888 return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, tag_size, tag_buf);
894 return EVP_CIPHER_CTX_block_size(ctx);
900 return EVP_CIPHER_CTX_mode(ctx);
912 int flags = EVP_CIPHER_CTX_flags(ctx);
913 int mode = EVP_CIPHER_CTX_mode(ctx);
915 return mode == EVP_CIPH_CBC_MODE
917 #ifdef EVP_CIPH_FLAG_CTS
918 && !(flags & EVP_CIPH_FLAG_CTS)
920 && !(flags & EVP_CIPH_FLAG_AEAD_CIPHER);
933 return (mode == EVP_CIPH_OFB_MODE || mode == EVP_CIPH_CFB_MODE)
935 && !(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_FLAG_AEAD_CIPHER);
943 int flags = EVP_CIPHER_CTX_flags(ctx);
944 if (flags & EVP_CIPH_FLAG_AEAD_CIPHER)
949 #if defined(NID_chacha20_poly1305) && OPENSSL_VERSION_NUMBER < 0x30000000L
950 if (EVP_CIPHER_CTX_nid(ctx) == NID_chacha20_poly1305)
964 return EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv_buf, -1);
971 if (!EVP_CipherUpdate(ctx, NULL, &len, src, src_len))
980 uint8_t *src,
int src_len)
982 if (!EVP_CipherUpdate(ctx, dst, dst_len, src, src_len))
992 return EVP_CipherFinal(ctx, dst, dst_len);
997 uint8_t *tag,
size_t tag_len)
999 ASSERT(tag_len < SIZE_MAX);
1000 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, tag_len, tag))
1029 "size (%d bytes) which is larger than " PACKAGE_NAME "'s current "
1030 "maximum hash size (%d bytes)",
1041 bool valid = (md != NULL);
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"},
1072 if (!strcmp(
"none", mdname))
1074 return "[null-digest]";
1096 if (!strcmp(
"none", mdname))
1101 unsigned char size = (
unsigned char)EVP_MD_size(kt);
1114 md_full(
const char *mdname,
const uint8_t *src,
int src_len, uint8_t *dst)
1116 unsigned int in_md_len = 0;
1119 int ret = EVP_Digest(src, src_len, dst, &in_md_len, kt, NULL);
1127 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
1135 EVP_MD_CTX_free(ctx);
1142 ASSERT(NULL != ctx && NULL != kt);
1144 EVP_MD_CTX_init(ctx);
1145 if (!EVP_DigestInit(ctx, kt))
1155 EVP_MD_CTX_reset(ctx);
1161 return EVP_MD_CTX_size(ctx);
1167 EVP_DigestUpdate(ctx, src, src_len);
1173 unsigned int in_md_len = 0;
1175 EVP_DigestFinal(ctx, dst, &in_md_len);
1184 #if OPENSSL_VERSION_NUMBER < 0x30000000L
1188 HMAC_CTX *ctx = HMAC_CTX_new();
1203 ASSERT(NULL != kt && NULL != ctx);
1205 int key_len = EVP_MD_size(kt);
1206 HMAC_CTX_reset(ctx);
1207 if (!HMAC_Init_ex(ctx,
key, key_len, kt, NULL))
1213 ASSERT(HMAC_size(ctx) <= key_len);
1219 HMAC_CTX_reset(ctx);
1225 return HMAC_size(ctx);
1231 if (!HMAC_Init_ex(ctx, NULL, 0, NULL, NULL))
1240 HMAC_Update(ctx, src, src_len);
1246 unsigned int in_hmac_len = 0;
1248 HMAC_Final(ctx, dst, &in_hmac_len);
1256 EVP_MAC *hmac = EVP_MAC_fetch(NULL,
"HMAC", NULL);
1257 ctx->ctx = EVP_MAC_CTX_new(hmac);
1268 EVP_MAC_CTX_free(ctx->ctx);
1277 ASSERT(NULL != kt && NULL != ctx && ctx->ctx != NULL);
1281 memcpy(ctx->key,
key, EVP_MD_size(kt));
1289 ctx->params[0] = OSSL_PARAM_construct_utf8_string(
"digest",
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();
1295 if (!EVP_MAC_init(ctx->ctx, NULL, 0, ctx->params))
1306 EVP_MAC_init(ctx->ctx, NULL, 0, NULL);
1312 return (
int)EVP_MAC_CTX_get_mac_size(ctx->ctx);
1321 if (!EVP_MAC_init(ctx->ctx, NULL, 0, ctx->params))
1330 EVP_MAC_update(ctx->ctx, src, src_len);
1338 size_t in_hmac_len = EVP_MAC_CTX_get_mac_size(ctx->ctx);
1340 EVP_MAC_final(ctx->ctx, dst, &in_hmac_len, in_hmac_len);
1347 return CRYPTO_memcmp(a, b, size);
1349 #if (OPENSSL_VERSION_NUMBER >= 0x30000000L) && !defined(LIBRESSL_VERSION_NUMBER)
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)
1355 EVP_KDF_CTX *kctx = NULL;
1358 EVP_KDF *kdf = EVP_KDF_fetch(NULL,
"TLS1-PRF", NULL);
1364 kctx = EVP_KDF_CTX_new(kdf);
1371 OSSL_PARAM params[4];
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();
1383 if (EVP_KDF_derive(kctx, output, output_len, params) <= 0)
1386 "EVP_KDF_derive failed");
1395 EVP_KDF_CTX_free(kctx);
1400 #elif !defined(LIBRESSL_VERSION_NUMBER) && !defined(ENABLE_CRYPTO_WOLFSSL)
1403 int secret_len, uint8_t *output,
int output_len)
1405 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_TLS1_PRF, NULL);
1412 if (!EVP_PKEY_derive_init(pctx))
1417 if (!EVP_PKEY_CTX_set_tls1_prf_md(pctx, EVP_md5_sha1()))
1422 if (!EVP_PKEY_CTX_set1_tls1_prf_secret(pctx, secret, secret_len))
1427 if (!EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed, seed_len))
1432 size_t out_len = output_len;
1433 if (!EVP_PKEY_derive(pctx, output, &out_len))
1437 if (out_len != output_len)
1443 EVP_PKEY_CTX_free(pctx);
1451 ssl_tls1_PRF(
const uint8_t *label,
int label_len,
const uint8_t *sec,
1452 int slen, uint8_t *out1,
int olen)