36 #if defined(ENABLE_CRYPTO_OPENSSL)
49 #ifdef ENABLE_CRYPTOAPI
56 #include <openssl/bn.h>
57 #include <openssl/crypto.h>
58 #include <openssl/dh.h>
59 #include <openssl/dsa.h>
60 #include <openssl/err.h>
61 #include <openssl/pkcs12.h>
62 #include <openssl/rsa.h>
63 #include <openssl/x509.h>
64 #include <openssl/ssl.h>
66 #include <openssl/ec.h>
69 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
70 #define HAVE_OPENSSL_STORE_API
71 #include <openssl/ui.h>
72 #include <openssl/store.h>
75 #if defined(_MSC_VER) && !defined(_M_ARM64)
76 #include <openssl/applink.c>
94 mydata_index = SSL_get_ex_new_index(0,
"struct session *", NULL, NULL, NULL);
110 if (ctx->
ctx == NULL)
114 if (ERR_peek_error() != 0)
128 if (ctx->
ctx == NULL)
132 if (ERR_peek_error() != 0)
143 SSL_CTX_free(ctx->
ctx);
152 return NULL != ctx->
ctx;
157 const char *label,
size_t label_size,
158 void *ekm,
size_t ekm_size)
163 if (SSL_export_keying_material(ssl, ekm, ekm_size, label,
164 label_size, NULL, 0, 0) == 1)
179 #ifndef INFO_CALLBACK_SSL_CONST
180 #define INFO_CALLBACK_SSL_CONST const
185 if (where & SSL_CB_LOOP)
188 where & SSL_ST_CONNECT ?
"connect" :
189 where &SSL_ST_ACCEPT ?
"accept" :
190 "undefined", SSL_state_string_long(s));
192 else if (where & SSL_CB_ALERT)
195 where & SSL_CB_READ ?
"Received" :
"Sent",
196 SSL_alert_type_string_long(ret),
197 SSL_alert_desc_string_long(ret));
209 #if defined(TLS1_3_VERSION)
212 #elif OPENSSL_VERSION_NUMBER >= 0x10100000L
224 if (OpenSSL_version_num() >= 0x1010100fL)
232 #elif defined(TLS1_2_VERSION) || defined(SSL_OP_NO_TLSv1_2)
234 #elif defined(TLS1_1_VERSION) || defined(SSL_OP_NO_TLSv1_1)
251 return TLS1_1_VERSION;
255 return TLS1_2_VERSION;
265 #ifndef TLS1_3_VERSION
273 return TLS1_3_VERSION;
290 int cur_min = SSL_CTX_get_min_proto_version(ctx->
ctx);
291 tls_ver_min = cur_min < TLS1_VERSION ? TLS1_VERSION : cur_min;
294 if (!SSL_CTX_set_min_proto_version(ctx->
ctx, tls_ver_min))
300 if (tls_ver_max && !SSL_CTX_set_max_proto_version(ctx->
ctx, tls_ver_max))
315 long sslopt = SSL_OP_SINGLE_DH_USE | SSL_OP_NO_TICKET;
316 #ifdef SSL_OP_CIPHER_SERVER_PREFERENCE
317 sslopt |= SSL_OP_CIPHER_SERVER_PREFERENCE;
319 sslopt |= SSL_OP_NO_COMPRESSION;
323 #ifdef SSL_OP_NO_RENEGOTIATION
324 sslopt |= SSL_OP_NO_RENEGOTIATION;
327 SSL_CTX_set_options(ctx->
ctx, sslopt);
334 #ifdef SSL_MODE_RELEASE_BUFFERS
335 SSL_CTX_set_mode(ctx->
ctx, SSL_MODE_RELEASE_BUFFERS);
337 SSL_CTX_set_session_cache_mode(ctx->
ctx, SSL_SESS_CACHE_OFF);
341 int verify_flags = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
348 verify_flags = SSL_VERIFY_PEER;
361 size_t begin_of_cipher, end_of_cipher;
363 const char *current_cipher;
364 size_t current_cipher_len;
368 size_t openssl_ciphers_len = 0;
369 openssl_ciphers[0] =
'\0';
372 begin_of_cipher = end_of_cipher = 0;
373 for (; begin_of_cipher < strlen(ciphers); begin_of_cipher = end_of_cipher)
375 end_of_cipher += strcspn(&ciphers[begin_of_cipher],
":");
378 if (NULL == cipher_pair)
381 current_cipher = &ciphers[begin_of_cipher];
382 current_cipher_len = end_of_cipher - begin_of_cipher;
387 msg(
D_LOW,
"No valid translation found for TLS cipher '%.*s'",
394 current_cipher_len = strlen(current_cipher);
396 if (end_of_cipher - begin_of_cipher == current_cipher_len
397 && 0 != memcmp(&ciphers[begin_of_cipher], cipher_pair->
iana_name,
398 end_of_cipher - begin_of_cipher))
406 if ((SIZE_MAX - openssl_ciphers_len) < current_cipher_len
407 || (len - 1) < (openssl_ciphers_len + current_cipher_len))
410 "Failed to set restricted TLS cipher list, too long (>%d).",
415 memcpy(&openssl_ciphers[openssl_ciphers_len], current_cipher, current_cipher_len);
416 openssl_ciphers_len += current_cipher_len;
417 openssl_ciphers[openssl_ciphers_len] =
':';
418 openssl_ciphers_len++;
423 if (openssl_ciphers_len > 0)
425 openssl_ciphers[openssl_ciphers_len-1] =
'\0';
435 if (!SSL_CTX_set_cipher_list(ctx->
ctx,
452 char openssl_ciphers[4096];
458 if (!SSL_CTX_set_cipher_list(ctx->
ctx, openssl_ciphers))
460 crypto_msg(
M_FATAL,
"Failed to set restricted TLS cipher list: %s", openssl_ciphers);
473 if (strlen(ciphers) >= (len - 1))
476 "Failed to set restricted TLS 1.3 cipher list, too long (>%d).",
480 strncpy(openssl_ciphers, ciphers, len);
482 for (
size_t i = 0; i < strlen(openssl_ciphers); i++)
484 if (openssl_ciphers[i] ==
'-')
486 openssl_ciphers[i] =
'_';
501 #if !defined(TLS1_3_VERSION)
503 "Ignoring TLS 1.3 only tls-ciphersuites '%s' setting.",
508 char openssl_ciphers[4096];
512 if (!SSL_CTX_set_ciphersuites(ctx->
ctx, openssl_ciphers))
523 #if OPENSSL_VERSION_NUMBER > 0x10100000L \
524 && (!defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER > 0x3060000fL)
529 if (!profile || 0 == strcmp(profile,
"legacy"))
531 SSL_CTX_set_security_level(ctx->
ctx, 1);
533 else if (0 == strcmp(profile,
"insecure"))
535 SSL_CTX_set_security_level(ctx->
ctx, 0);
537 else if (0 == strcmp(profile,
"preferred"))
539 SSL_CTX_set_security_level(ctx->
ctx, 2);
541 else if (0 == strcmp(profile,
"suiteb"))
543 SSL_CTX_set_security_level(ctx->
ctx, 3);
544 SSL_CTX_set_cipher_list(ctx->
ctx,
"SUITEB128");
548 msg(
M_FATAL,
"ERROR: Invalid cert profile: %s", profile);
553 msg(
M_WARN,
"WARNING: OpenSSL 1.1.0 and LibreSSL do not support "
554 "--tls-cert-profile, ignoring user-set profile: '%s'", profile);
563 #if OPENSSL_VERSION_NUMBER < 0x30000000L
585 while ((token =
strsep(&tmp_groups,
":")))
587 if (
streq(token,
"secp256r1"))
589 token =
"prime256v1";
591 int nid = OBJ_sn2nid(token);
595 msg(
M_WARN,
"Warning unknown curve/group specified: %s", token);
599 glist[glistlen] = nid;
611 if (!SSL_CTX_set1_groups_list(ctx->
ctx, groups))
627 cert = SSL_CTX_get0_certificate(ctx->
ctx);
634 ret = X509_cmp_time(X509_get0_notBefore(cert), NULL);
641 msg(
M_WARN,
"WARNING: Your certificate is not yet valid!");
644 ret = X509_cmp_time(X509_get0_notAfter(cert), NULL);
651 msg(
M_WARN,
"WARNING: Your certificate has expired!");
665 if (!(bio = BIO_new_mem_buf((
char *)dh_file, -1)))
673 if (!(bio = BIO_new_file(dh_file,
"r")))
679 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
680 EVP_PKEY *dh = PEM_read_bio_Parameters(bio, NULL);
688 if (!SSL_CTX_set0_tmp_dh_pkey(ctx->
ctx, dh))
694 8 * EVP_PKEY_get_size(dh));
696 DH *dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
704 if (!SSL_CTX_set_tmp_dh(ctx->
ctx, dh))
719 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
720 if (curve_name != NULL)
722 msg(
M_WARN,
"WARNING: OpenSSL 3.0+ builds do not support specifying an "
723 "ECDH curve with --ecdh-curve, using default curves. Use "
724 "--tls-groups to specify groups.");
726 #elif !defined(OPENSSL_NO_EC)
729 const char *sname = NULL;
732 SSL_CTX_set_options(ctx->
ctx, SSL_OP_SINGLE_ECDH_USE);
734 if (curve_name != NULL)
737 msg(
D_TLS_DEBUG,
"Using user specified ECDH curve (%s)", curve_name);
738 nid = OBJ_sn2nid(curve_name);
746 sname = OBJ_nid2sn(nid);
753 if (NID_undef == nid || NULL == (ecdh = EC_KEY_new_by_curve_name(nid)))
756 ecdh = EC_KEY_new_by_curve_name(NID_secp384r1);
757 const char *source = (NULL == curve_name) ?
758 "extract curve from certificate" :
"use supplied curve";
760 "Failed to %s (%s), using secp384r1 instead.", source, sname);
761 sname = OBJ_nid2sn(NID_secp384r1);
764 if (!SSL_CTX_set_tmp_ecdh(ctx->
ctx, ecdh))
773 msg(
D_LOW,
"Your OpenSSL library was built without elliptic curve support."
774 " Skipping ECDH parameter loading.");
778 #if defined(HAVE_OPENSSL_STORE_API)
785 ui_reader(UI *ui, UI_STRING *uis)
787 SSL_CTX *ctx = UI_get0_user_data(ui);
789 if (UI_get_string_type(uis) == UIT_PROMPT)
791 const char *prompt = UI_get0_output_string(uis);
794 if (strstr(prompt,
"PKCS#11"))
799 UI_set_result(ui, uis, up.password);
805 pem_password_cb *cb = SSL_CTX_get_default_passwd_cb(ctx);
806 void *d = SSL_CTX_get_default_passwd_cb_userdata(ctx);
819 clear_ossl_store_error(OSSL_STORE_CTX *store_ctx)
821 if (OSSL_STORE_error(store_ctx))
839 EVP_PKEY *pkey = NULL;
841 #if !defined(HAVE_OPENSSL_STORE_API)
844 BIO *in = BIO_new_file(uri,
"r");
849 pkey = PEM_read_bio_PrivateKey(in, NULL,
850 SSL_CTX_get_default_passwd_cb(ssl_ctx),
851 SSL_CTX_get_default_passwd_cb_userdata(ssl_ctx));
856 OSSL_STORE_CTX *store_ctx = NULL;
857 OSSL_STORE_INFO *info = NULL;
859 UI_METHOD *ui_method = UI_create_method(
"openvpn");
862 msg(
M_WARN,
"OpenSSL UI creation failed");
865 UI_method_set_reader(ui_method, ui_reader);
867 store_ctx = OSSL_STORE_open_ex(uri,
tls_libctx, NULL, ui_method, ssl_ctx,
873 if (OSSL_STORE_expect(store_ctx, OSSL_STORE_INFO_PKEY) != 1)
879 info = OSSL_STORE_load(store_ctx);
880 if (info || OSSL_STORE_eof(store_ctx))
888 clear_ossl_store_error(store_ctx);
894 pkey = OSSL_STORE_INFO_get1_PKEY(info);
895 OSSL_STORE_INFO_free(info);
899 OSSL_STORE_close(store_ctx);
900 UI_destroy_method(ui_method);
909 bool pkcs12_file_inline,
bool load_ca_file)
914 STACK_OF(X509) *ca = NULL;
921 if (pkcs12_file_inline)
923 BIO *b64 = BIO_new(BIO_f_base64());
924 BIO *bio = BIO_new_mem_buf((
void *) pkcs12_file,
925 (
int) strlen(pkcs12_file));
928 p12 = d2i_PKCS12_bio(b64, NULL);
943 p12 = d2i_PKCS12_fp(fp, NULL);
952 if (!PKCS12_parse(p12,
"", &pkey, &cert, &ca))
957 if (!PKCS12_parse(p12,
password, &pkey, &cert, &ca))
960 "or unsupported/legacy encryption");
961 #ifdef ENABLE_MANAGEMENT
962 if (
management && (ERR_GET_REASON(ERR_peek_error()) == PKCS12_R_MAC_VERIFY_FAILURE))
974 if (!SSL_CTX_use_certificate(ctx->
ctx, cert))
981 if (!SSL_CTX_use_PrivateKey(ctx->
ctx, pkey))
987 if (!SSL_CTX_check_private_key(ctx->
ctx))
999 if (ca && sk_X509_num(ca))
1001 for (i = 0; i < sk_X509_num(ca); i++)
1003 X509_STORE *cert_store = SSL_CTX_get_cert_store(ctx->
ctx);
1004 if (!X509_STORE_add_cert(cert_store, sk_X509_value(ca, i)))
1006 crypto_msg(
M_FATAL,
"Cannot add certificate to certificate chain (X509_STORE_add_cert)");
1008 if (!SSL_CTX_add_client_CA(ctx->
ctx, sk_X509_value(ca, i)))
1010 crypto_msg(
M_FATAL,
"Cannot add certificate to client CA list (SSL_CTX_add_client_CA)");
1022 if (ca && sk_X509_num(ca))
1024 for (i = 0; i < sk_X509_num(ca); i++)
1026 if (!SSL_CTX_add_extra_chain_cert(ctx->
ctx, sk_X509_value(ca, i)))
1028 crypto_msg(
M_FATAL,
"Cannot add extra certificate to chain (SSL_CTX_add_extra_chain_cert)");
1036 #ifdef ENABLE_CRYPTOAPI
1045 crypto_msg(
M_FATAL,
"Cannot load certificate \"%s\" from Microsoft Certificate Store", cryptoapi_cert);
1057 if (!PEM_read_bio_X509(bio, &cert, NULL, NULL))
1064 && ERR_GET_REASON(ERR_peek_error()) == PEM_R_NO_START_LINE)
1067 (void)ERR_get_error();
1075 if (SSL_CTX_add_extra_chain_cert(ctx->
ctx, cert) != 1)
1087 #if defined(HAVE_OPENSSL_STORE_API)
1097 #if defined(HAVE_OPENSSL_STORE_API)
1100 OSSL_STORE_CTX *store_ctx = NULL;
1101 OSSL_STORE_INFO *info = NULL;
1105 UI_METHOD *ui_method = UI_create_method(
"openvpn");
1108 msg(
M_WARN,
"OpenSSL UI method creation failed");
1111 UI_method_set_reader(ui_method, ui_reader);
1113 store_ctx = OSSL_STORE_open_ex(uri,
tls_libctx, NULL, ui_method, tls_ctx->
ctx,
1119 if (OSSL_STORE_expect(store_ctx, OSSL_STORE_INFO_CERT) != 1)
1126 info = OSSL_STORE_load(store_ctx);
1127 if (info || OSSL_STORE_eof(store_ctx))
1135 clear_ossl_store_error(store_ctx);
1142 x = OSSL_STORE_INFO_get0_CERT(info);
1149 ret = SSL_CTX_use_certificate(tls_ctx->
ctx, x);
1154 OSSL_STORE_INFO_free(info);
1157 while (!OSSL_STORE_eof(store_ctx))
1159 info = OSSL_STORE_load(store_ctx);
1162 clear_ossl_store_error(store_ctx);
1165 x = OSSL_STORE_INFO_get1_CERT(info);
1166 if (x && SSL_CTX_add_extra_chain_cert(tls_ctx->
ctx, x) != 1)
1172 OSSL_STORE_INFO_free(info);
1186 UI_destroy_method(ui_method);
1187 OSSL_STORE_INFO_free(info);
1188 OSSL_STORE_close(store_ctx);
1196 bool cert_file_inline)
1204 if (cert_file_inline)
1206 in = BIO_new_mem_buf((
char *) cert_file, -1);
1210 in = BIO_new_file((
char *) cert_file,
"r");
1215 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
1219 x = PEM_read_bio_X509(in, NULL,
1220 SSL_CTX_get_default_passwd_cb(ctx->
ctx),
1221 SSL_CTX_get_default_passwd_cb_userdata(ctx->
ctx));
1224 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_PEM_LIB);
1228 ret = SSL_CTX_use_certificate(ctx->
ctx, x);
1238 if (cert_file_inline)
1258 bool cert_file_inline)
1272 bool priv_key_file_inline)
1274 SSL_CTX *ssl_ctx = NULL;
1276 EVP_PKEY *pkey = NULL;
1283 if (priv_key_file_inline)
1285 in = BIO_new_mem_buf((
char *) priv_key_file, -1);
1290 pkey = PEM_read_bio_PrivateKey(in, NULL,
1291 SSL_CTX_get_default_passwd_cb(ctx->
ctx),
1292 SSL_CTX_get_default_passwd_cb_userdata(ctx->
ctx));
1299 if (!pkey || !SSL_CTX_use_PrivateKey(ssl_ctx, pkey))
1301 #ifdef ENABLE_MANAGEMENT
1302 if (
management && (ERR_GET_REASON(ERR_peek_error()) == EVP_R_BAD_DECRYPT))
1313 if (!SSL_CTX_check_private_key(ssl_ctx))
1320 EVP_PKEY_free(pkey);
1331 X509_STORE *store = SSL_CTX_get_cert_store(ssl_ctx->
ctx);
1340 STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store);
1341 for (
int i = 0; i < sk_X509_OBJECT_num(objs); i++)
1343 X509_OBJECT *obj = sk_X509_OBJECT_value(objs, i);
1345 if (X509_OBJECT_get_type(obj) == X509_LU_CRL)
1347 sk_X509_OBJECT_delete(objs, i);
1348 X509_OBJECT_free(obj);
1352 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
1356 in = BIO_new_mem_buf((
char *) crl_file, -1);
1360 in = BIO_new_file(crl_file,
"r");
1370 int num_crls_loaded = 0;
1373 X509_CRL *crl = PEM_read_bio_X509_CRL(in, NULL, NULL, NULL);
1379 bool eof = ERR_GET_REASON(ERR_peek_error()) == PEM_R_NO_START_LINE;
1381 if (num_crls_loaded > 0 && eof)
1384 (void)ERR_get_error();
1393 if (!X509_STORE_add_crl(store, crl))
1403 msg(
M_INFO,
"CRL: loaded %d CRLs from file %s", num_crls_loaded, crl_file);
1409 #if defined(ENABLE_MANAGEMENT) && !defined(HAVE_XKEY_PROVIDER)
1413 rsa_pub_enc(
int flen,
const unsigned char *from,
unsigned char *to, RSA *rsa,
int padding)
1421 rsa_pub_dec(
int flen,
const unsigned char *from,
unsigned char *to, RSA *rsa,
int padding)
1429 rsa_priv_dec(
int flen,
const unsigned char *from,
unsigned char *to, RSA *rsa,
int padding)
1442 const RSA_METHOD *meth = RSA_get_method(rsa);
1443 RSA_meth_free((RSA_METHOD *)meth);
1456 case RSA_PKCS1_PADDING:
1457 return "RSA_PKCS1_PADDING";
1459 case RSA_NO_PADDING:
1460 return "RSA_NO_PADDING";
1480 unsigned char *sig,
unsigned int siglen,
1481 const char *algorithm)
1483 char *in_b64 = NULL;
1484 char *out_b64 = NULL;
1506 rsa_priv_enc(
int flen,
const unsigned char *from,
unsigned char *to, RSA *rsa,
1509 unsigned int len = RSA_size(rsa);
1512 if (padding != RSA_PKCS1_PADDING && padding != RSA_NO_PADDING)
1514 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
1520 return (ret == len) ? ret : -1;
1527 RSA_METHOD *rsa_meth;
1531 const RSA *pub_rsa = EVP_PKEY_get0_RSA(pkey);
1535 rsa_meth = RSA_meth_new(
"OpenVPN external private key RSA Method",
1536 RSA_METHOD_FLAG_NO_CHECK);
1542 RSA_meth_set_init(rsa_meth, NULL);
1544 RSA_meth_set0_app_data(rsa_meth, NULL);
1550 SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_MALLOC_FAILURE);
1555 const BIGNUM *n = NULL;
1556 const BIGNUM *e = NULL;
1557 RSA_get0_key(pub_rsa, &n, &e, NULL);
1558 RSA_set0_key(rsa, BN_dup(n), BN_dup(e), NULL);
1559 RSA_set_flags(rsa, RSA_flags(rsa) | RSA_FLAG_EXT_PKEY);
1560 if (!RSA_set_method(rsa, rsa_meth))
1562 RSA_meth_free(rsa_meth);
1568 if (!SSL_CTX_use_RSAPrivateKey(ctx->
ctx, rsa))
1583 RSA_meth_free(rsa_meth);
1588 #if !defined(OPENSSL_NO_EC)
1595 const EC_KEY_METHOD *ec_meth = EC_KEY_get_method(ec);
1596 EC_KEY_METHOD_free((EC_KEY_METHOD *) ec_meth);
1604 ecdsa_sign(
int type,
const unsigned char *dgst,
int dgstlen,
unsigned char *sig,
1605 unsigned int *siglen,
const BIGNUM *kinv,
const BIGNUM *r, EC_KEY *ec)
1607 int capacity = ECDSA_size(ec);
1636 const BIGNUM *in_r, EC_KEY *ec)
1638 ECDSA_SIG *ecsig = NULL;
1639 unsigned int len = ECDSA_size(ec);
1643 if (
ecdsa_sign(0, dgst, dgstlen, buf, &len, NULL, NULL, ec) != 1)
1648 ecsig = d2i_ECDSA_SIG(NULL, (
const unsigned char **)&buf, len);
1659 EVP_PKEY *privkey = NULL;
1660 EC_KEY_METHOD *ec_method;
1664 ec_method = EC_KEY_METHOD_new(EC_KEY_OpenSSL());
1672 #ifdef OPENSSL_IS_AWSLC
1678 ec = EC_KEY_dup(EVP_PKEY_get0_EC_KEY(pkey));
1681 EC_KEY_METHOD_free(ec_method);
1684 if (!EC_KEY_set_method(ec, ec_method))
1686 EC_KEY_METHOD_free(ec_method);
1691 privkey = EVP_PKEY_new();
1692 if (!EVP_PKEY_assign_EC_KEY(privkey, ec))
1698 if (!SSL_CTX_use_PrivateKey(ctx->
ctx, privkey))
1704 EVP_PKEY_free(privkey);
1709 EVP_PKEY_free(privkey);
1716 #ifdef ENABLE_MANAGEMENT
1724 X509 *cert = SSL_CTX_get0_certificate(ctx->
ctx);
1729 EVP_PKEY *pkey = X509_get0_pubkey(cert);
1732 #ifdef HAVE_XKEY_PROVIDER
1733 EVP_PKEY *privkey = xkey_load_management_key(
tls_libctx, pkey);
1735 || !SSL_CTX_use_PrivateKey(ctx->
ctx, privkey))
1737 EVP_PKEY_free(privkey);
1740 EVP_PKEY_free(privkey);
1742 #if OPENSSL_VERSION_NUMBER < 0x30000000L
1743 if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA)
1745 if (EVP_PKEY_is_a(pkey,
"RSA"))
1753 #if !defined(OPENSSL_NO_EC)
1754 #if OPENSSL_VERSION_NUMBER < 0x30000000L
1755 else if (EVP_PKEY_id(pkey) == EVP_PKEY_EC)
1757 else if (EVP_PKEY_is_a(pkey,
"EC"))
1767 crypto_msg(
M_WARN,
"management-external-key requires an RSA or EC certificate");
1794 return X509_NAME_cmp(*a, *b);
1799 bool ca_file_inline,
const char *ca_path,
bool tls_server)
1801 STACK_OF(X509_INFO) *info_stack = NULL;
1802 STACK_OF(X509_NAME) *cert_names = NULL;
1803 X509_LOOKUP *lookup = NULL;
1804 X509_STORE *store = NULL;
1805 X509_NAME *xn = NULL;
1807 int i, added = 0, prev = 0;
1811 store = SSL_CTX_get_cert_store(ctx->
ctx);
1822 in = BIO_new_mem_buf((
char *)ca_file, -1);
1826 in = BIO_new_file(ca_file,
"r");
1831 info_stack = PEM_X509_INFO_read_bio(in, NULL, NULL, NULL);
1836 for (i = 0; i < sk_X509_INFO_num(info_stack); i++)
1838 X509_INFO *info = sk_X509_INFO_value(info_stack, i);
1841 X509_STORE_add_crl(store, info->crl);
1844 if (tls_server && !info->x509)
1851 X509_STORE_add_cert(store, info->x509);
1860 if (cert_names == NULL)
1869 xn = X509_get_subject_name(info->x509);
1876 if (sk_X509_NAME_find(cert_names, xn) == -1)
1878 xn = X509_NAME_dup(xn);
1883 sk_X509_NAME_push(cert_names, xn);
1889 int cnum = sk_X509_NAME_num(cert_names);
1890 if (cnum != (prev + 1))
1893 "Cannot load CA certificate file %s (entry %d did not validate)",
1900 sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
1905 cnum = sk_X509_NAME_num(cert_names);
1906 SSL_CTX_set_client_CA_list(ctx->
ctx, cert_names);
1912 "Cannot load CA certificate file %s (no entries were read)",
1921 "of %d entries were valid X509 names)",
1933 lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
1934 if (lookup && X509_LOOKUP_add_dir(lookup, ca_path, X509_FILETYPE_PEM))
1936 msg(
M_WARN,
"WARNING: experimental option --capath %s", ca_path);
1942 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
1948 bool extra_certs_file_inline)
1951 if (extra_certs_file_inline)
1953 in = BIO_new_mem_buf((
char *)extra_certs_file, -1);
1957 in = BIO_new_file(extra_certs_file,
"r");
1964 extra_certs_file_inline));
1988 #warning BIO_DEBUG defined
1991 static bool biofp_toggle;
1992 static time_t biofp_last_open;
1993 static const int biofp_reopen_interval = 600;
2008 const time_t current = time(NULL);
2009 const pid_t pid = getpid();
2011 if (biofp_last_open + biofp_reopen_interval < current)
2018 snprintf(fn,
sizeof(fn),
"bio/%d-%d.log", pid, biofp_toggle);
2019 biofp = fopen(fn,
"w");
2021 biofp_last_open = time(NULL);
2027 bio_debug_data(
const char *mode, BIO *bio,
const uint8_t *buf,
int len,
const char *desc)
2033 fprintf(biofp,
"BIO_%s %s time=%" PRIi64
" bio=" ptr_format " len=%d data=%s\n",
2041 bio_debug_oc(
const char *mode, BIO *bio)
2044 fprintf(biofp,
"BIO %s time=%" PRIi64
" bio=" ptr_format "\n",
2045 mode, (int64_t)time(NULL), (
ptr_type)bio);
2055 bio_write(BIO *bio,
const uint8_t *data,
int size,
const char *desc)
2069 bio_debug_data(
"write", bio, data, size, desc);
2071 i = BIO_write(bio, data, size);
2075 if (!BIO_should_retry(bio))
2132 int i = BIO_read(bio,
BPTR(buf), len);
2137 bio_debug_data(
"read", bio,
BPTR(buf), i, desc);
2143 if (!BIO_should_retry(bio))
2172 ks_ssl->
ssl = SSL_new(ssl_ctx->
ctx);
2187 bio_debug_oc(
"open ssl_bio", ks_ssl->
ssl_bio);
2188 bio_debug_oc(
"open ct_in", ks_ssl->
ct_in);
2189 bio_debug_oc(
"open ct_out", ks_ssl->
ct_out);
2194 SSL_set_accept_state(ks_ssl->
ssl);
2198 SSL_set_connect_state(ks_ssl->
ssl);
2202 BIO_set_ssl(ks_ssl->
ssl_bio, ks_ssl->
ssl, BIO_NOCLOSE);
2208 SSL_set_shutdown(ks_ssl->
ssl, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
2217 bio_debug_oc(
"close ssl_bio", ks_ssl->
ssl_bio);
2218 bio_debug_oc(
"close ct_in", ks_ssl->
ct_in);
2219 bio_debug_oc(
"close ct_out", ks_ssl->
ct_out);
2221 BIO_free_all(ks_ssl->
ssl_bio);
2222 SSL_free(ks_ssl->
ssl);
2235 "tls_write_plaintext");
2302 const char *curve =
"";
2303 const char *type =
"(error getting type)";
2311 int typeid = EVP_PKEY_id(pkey);
2312 #if OPENSSL_VERSION_NUMBER < 0x30000000L
2313 bool is_ec =
typeid == EVP_PKEY_EC;
2315 bool is_ec = EVP_PKEY_is_a(pkey,
"EC");
2318 #ifndef OPENSSL_NO_EC
2329 curve =
"(error getting curve name)";
2335 #if OPENSSL_VERSION_NUMBER < 0x30000000L
2336 type = OBJ_nid2sn(
typeid);
2340 if (
typeid == EVP_PKEY_RSA)
2344 else if (
typeid == EVP_PKEY_DSA)
2348 else if (
typeid == EVP_PKEY_EC)
2351 type =
"EC, curve ";
2353 else if (type == NULL)
2355 type =
"unknown type";
2358 type = EVP_PKEY_get0_type_name(pkey);
2361 type =
"(error getting public key type)";
2366 snprintf(buf, buflen,
"%d bits %s%s",
2367 EVP_PKEY_bits(pkey), type, curve);
2379 EVP_PKEY *pkey = X509_get_pubkey(cert);
2380 char pkeybuf[64] = { 0 };
2383 char sig[128] = { 0 };
2384 int signature_nid = X509_get_signature_nid(cert);
2385 if (signature_nid != 0)
2387 snprintf(sig,
sizeof(sig),
", signature: %s",
2388 OBJ_nid2sn(signature_nid));
2391 snprintf(buf, buflen,
", peer certificate: %s%s",
2394 EVP_PKEY_free(pkey);
2400 EVP_PKEY *pkey = NULL;
2401 SSL_get_peer_tmp_key(ssl, &pkey);
2407 char pkeybuf[128] = { 0 };
2410 snprintf(buf, buflen,
", peer temporary key: %s",
2413 EVP_PKEY_free(pkey);
2416 #if !defined(LIBRESSL_VERSION_NUMBER) \
2417 || (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER >= 0x3090000fL)
2442 return "(error getting name)";
2445 return OBJ_nid2sn(nid);
2457 int peer_sig_nid = NID_undef, peer_sig_type_nid = NID_undef;
2458 const char *peer_sig =
"unknown";
2459 const char *peer_sig_type =
"unknown type";
2464 #if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER > 0x3050400fL
2465 if (SSL_get_peer_signature_nid(ssl, &peer_sig_nid)
2466 && peer_sig_nid != NID_undef)
2468 peer_sig = OBJ_nid2sn(peer_sig_nid);
2472 #if !defined(LIBRESSL_VERSION_NUMBER) \
2473 || (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER >= 0x3090000fL)
2476 if (SSL_get_peer_signature_type_nid(ssl, &peer_sig_type_nid)
2477 && peer_sig_type_nid != NID_undef)
2483 if (peer_sig_nid == NID_undef && peer_sig_type_nid == NID_undef)
2488 snprintf(buf, buflen,
", peer signing digest/type: %s %s",
2489 peer_sig, peer_sig_type);
2504 const SSL_CIPHER *ciph;
2510 s1[0] = s2[0] = s3[0] = s4[0] = 0;
2511 ciph = SSL_get_current_cipher(ks_ssl->
ssl);
2512 snprintf(s1,
sizeof(s1),
"%s %s, cipher %s %s",
2514 SSL_get_version(ks_ssl->
ssl),
2515 SSL_CIPHER_get_version(ciph),
2516 SSL_CIPHER_get_name(ciph));
2517 X509 *cert = SSL_get_peer_certificate(ks_ssl->
ssl);
2532 const char *tls_cert_profile,
2537 tls_ctx.
ctx = SSL_CTX_new(SSLv23_method());
2543 #if defined(TLS1_3_VERSION)
2546 SSL_CTX_set_min_proto_version(tls_ctx.
ctx,
2553 SSL_CTX_set_max_proto_version(tls_ctx.
ctx, TLS1_2_VERSION);
2559 SSL *ssl = SSL_new(tls_ctx.
ctx);
2565 #if OPENSSL_VERSION_NUMBER < 0x1010000fL || defined(OPENSSL_IS_AWSLC)
2566 STACK_OF(SSL_CIPHER) *sk = SSL_get_ciphers(ssl);
2568 STACK_OF(SSL_CIPHER) *sk = SSL_get1_supported_ciphers(ssl);
2570 for (
int i = 0; i < sk_SSL_CIPHER_num(sk); i++)
2572 const SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, i);
2574 const char *cipher_name = SSL_CIPHER_get_name(c);
2581 printf(
"%s\n", cipher_name);
2583 else if (NULL == pair)
2586 printf(
"%s (No IANA name known to OpenVPN, use OpenSSL name.)\n",
2594 #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL)
2595 sk_SSL_CIPHER_free(sk);
2598 SSL_CTX_free(tls_ctx.
ctx);
2608 printf(
"Consider using 'openssl ecparam -list_curves' as alternative to running\n"
2610 "Note this output does only list curves/groups that OpenSSL considers as\n"
2611 "builtin EC curves. It does not list additional curves nor X448 or X25519\n");
2612 #ifndef OPENSSL_NO_EC
2613 EC_builtin_curve *curves = NULL;
2617 crv_len = EC_get_builtin_curves(NULL, 0);
2619 if (EC_get_builtin_curves(curves, crv_len))
2621 printf(
"\nAvailable Elliptic curves/groups:\n");
2622 for (n = 0; n < crv_len; n++)
2625 sname = OBJ_nid2sn(curves[n].nid);
2631 printf(
"%s\n", sname);
2640 msg(
M_WARN,
"Your OpenSSL library was built without elliptic curve support. "
2641 "No curves available.");
2648 return OpenSSL_version(OPENSSL_VERSION);
2653 #ifdef HAVE_XKEY_PROVIDER
2657 const char *name = OSSL_PROVIDER_get0_name(prov);
2658 OSSL_PROVIDER_load(dest_libctx, name);
2666 OSSL_PROVIDER_unload(prov);
2681 #ifdef HAVE_XKEY_PROVIDER
2694 OSSL_PROVIDER_do_all(NULL, provider_load,
tls_libctx);
2697 if (!OSSL_PROVIDER_available(
tls_libctx,
"ovpn.xkey"))
2699 OSSL_PROVIDER_add_builtin(
tls_libctx,
"ovpn.xkey", xkey_provider_init);
2700 if (!OSSL_PROVIDER_load(
tls_libctx,
"ovpn.xkey"))
2702 msg(
M_NONFATAL,
"ERROR: failed loading external key provider: "
2703 "Signing with external keys will not work.");
2713 EVP_set_default_properties(
tls_libctx,
"?provider!=ovpn.xkey");
2724 #ifdef HAVE_XKEY_PROVIDER
2727 OSSL_PROVIDER_do_all(
tls_libctx, provider_unload, NULL);