35 #if defined(ENABLE_CRYPTO_OPENSSL)
48 #ifdef ENABLE_CRYPTOAPI
55 #include <openssl/bn.h>
56 #include <openssl/crypto.h>
57 #include <openssl/dh.h>
58 #include <openssl/dsa.h>
59 #include <openssl/err.h>
60 #include <openssl/pkcs12.h>
61 #include <openssl/rsa.h>
62 #include <openssl/x509.h>
63 #include <openssl/ssl.h>
65 #include <openssl/ec.h>
68 #if defined(_MSC_VER) && !defined(_M_ARM64)
69 #include <openssl/applink.c>
87 mydata_index = SSL_get_ex_new_index(0,
"struct session *", NULL, NULL, NULL);
103 if (ctx->
ctx == NULL)
107 if (ERR_peek_error() != 0)
121 if (ctx->
ctx == NULL)
125 if (ERR_peek_error() != 0)
136 SSL_CTX_free(ctx->
ctx);
145 return NULL != ctx->
ctx;
150 const char *label,
size_t label_size,
151 void *ekm,
size_t ekm_size)
156 if (SSL_export_keying_material(ssl, ekm, ekm_size, label,
157 label_size, NULL, 0, 0) == 1)
172 #ifndef INFO_CALLBACK_SSL_CONST
173 #define INFO_CALLBACK_SSL_CONST const
178 if (where & SSL_CB_LOOP)
181 where & SSL_ST_CONNECT ?
"connect" :
182 where &SSL_ST_ACCEPT ?
"accept" :
183 "undefined", SSL_state_string_long(s));
185 else if (where & SSL_CB_ALERT)
188 where & SSL_CB_READ ?
"Received" :
"Sent",
189 SSL_alert_type_string_long(ret),
190 SSL_alert_desc_string_long(ret));
202 #if defined(TLS1_3_VERSION)
205 #elif OPENSSL_VERSION_NUMBER >= 0x10100000L
217 if (OpenSSL_version_num() >= 0x1010100fL)
225 #elif defined(TLS1_2_VERSION) || defined(SSL_OP_NO_TLSv1_2)
227 #elif defined(TLS1_1_VERSION) || defined(SSL_OP_NO_TLSv1_1)
244 return TLS1_1_VERSION;
248 return TLS1_2_VERSION;
258 #ifndef TLS1_3_VERSION
266 return TLS1_3_VERSION;
283 int cur_min = SSL_CTX_get_min_proto_version(ctx->
ctx);
284 tls_ver_min = cur_min < TLS1_VERSION ? TLS1_VERSION : cur_min;
287 if (!SSL_CTX_set_min_proto_version(ctx->
ctx, tls_ver_min))
293 if (tls_ver_max && !SSL_CTX_set_max_proto_version(ctx->
ctx, tls_ver_max))
308 long sslopt = SSL_OP_SINGLE_DH_USE | SSL_OP_NO_TICKET;
309 #ifdef SSL_OP_CIPHER_SERVER_PREFERENCE
310 sslopt |= SSL_OP_CIPHER_SERVER_PREFERENCE;
312 sslopt |= SSL_OP_NO_COMPRESSION;
316 #ifdef SSL_OP_NO_RENEGOTIATION
317 sslopt |= SSL_OP_NO_RENEGOTIATION;
320 SSL_CTX_set_options(ctx->
ctx, sslopt);
327 #ifdef SSL_MODE_RELEASE_BUFFERS
328 SSL_CTX_set_mode(ctx->
ctx, SSL_MODE_RELEASE_BUFFERS);
330 SSL_CTX_set_session_cache_mode(ctx->
ctx, SSL_SESS_CACHE_OFF);
334 int verify_flags = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
341 verify_flags = SSL_VERIFY_PEER;
354 size_t begin_of_cipher, end_of_cipher;
356 const char *current_cipher;
357 size_t current_cipher_len;
361 size_t openssl_ciphers_len = 0;
362 openssl_ciphers[0] =
'\0';
365 begin_of_cipher = end_of_cipher = 0;
366 for (; begin_of_cipher < strlen(ciphers); begin_of_cipher = end_of_cipher)
368 end_of_cipher += strcspn(&ciphers[begin_of_cipher],
":");
371 if (NULL == cipher_pair)
374 current_cipher = &ciphers[begin_of_cipher];
375 current_cipher_len = end_of_cipher - begin_of_cipher;
380 msg(
D_LOW,
"No valid translation found for TLS cipher '%.*s'",
387 current_cipher_len = strlen(current_cipher);
389 if (end_of_cipher - begin_of_cipher == current_cipher_len
390 && 0 != memcmp(&ciphers[begin_of_cipher], cipher_pair->
iana_name,
391 end_of_cipher - begin_of_cipher))
399 if ((SIZE_MAX - openssl_ciphers_len) < current_cipher_len
400 || (len - 1) < (openssl_ciphers_len + current_cipher_len))
403 "Failed to set restricted TLS cipher list, too long (>%d).",
408 memcpy(&openssl_ciphers[openssl_ciphers_len], current_cipher, current_cipher_len);
409 openssl_ciphers_len += current_cipher_len;
410 openssl_ciphers[openssl_ciphers_len] =
':';
411 openssl_ciphers_len++;
416 if (openssl_ciphers_len > 0)
418 openssl_ciphers[openssl_ciphers_len-1] =
'\0';
428 if (!SSL_CTX_set_cipher_list(ctx->
ctx,
445 char openssl_ciphers[4096];
451 if (!SSL_CTX_set_cipher_list(ctx->
ctx, openssl_ciphers))
453 crypto_msg(
M_FATAL,
"Failed to set restricted TLS cipher list: %s", openssl_ciphers);
466 if (strlen(ciphers) >= (len - 1))
469 "Failed to set restricted TLS 1.3 cipher list, too long (>%d).",
473 strncpy(openssl_ciphers, ciphers, len);
475 for (
size_t i = 0; i < strlen(openssl_ciphers); i++)
477 if (openssl_ciphers[i] ==
'-')
479 openssl_ciphers[i] =
'_';
494 #if !defined(TLS1_3_VERSION)
496 "Ignoring TLS 1.3 only tls-ciphersuites '%s' setting.",
501 char openssl_ciphers[4096];
505 if (!SSL_CTX_set_ciphersuites(ctx->
ctx, openssl_ciphers))
516 #if OPENSSL_VERSION_NUMBER > 0x10100000L \
517 && (!defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER > 0x3060000fL)
522 if (!profile || 0 == strcmp(profile,
"legacy"))
524 SSL_CTX_set_security_level(ctx->
ctx, 1);
526 else if (0 == strcmp(profile,
"insecure"))
528 SSL_CTX_set_security_level(ctx->
ctx, 0);
530 else if (0 == strcmp(profile,
"preferred"))
532 SSL_CTX_set_security_level(ctx->
ctx, 2);
534 else if (0 == strcmp(profile,
"suiteb"))
536 SSL_CTX_set_security_level(ctx->
ctx, 3);
537 SSL_CTX_set_cipher_list(ctx->
ctx,
"SUITEB128");
541 msg(
M_FATAL,
"ERROR: Invalid cert profile: %s", profile);
546 msg(
M_WARN,
"WARNING: OpenSSL 1.1.0 and LibreSSL do not support "
547 "--tls-cert-profile, ignoring user-set profile: '%s'", profile);
556 #if OPENSSL_VERSION_NUMBER < 0x30000000L
578 while ((token =
strsep(&tmp_groups,
":")))
580 if (
streq(token,
"secp256r1"))
582 token =
"prime256v1";
584 int nid = OBJ_sn2nid(token);
588 msg(
M_WARN,
"Warning unknown curve/group specified: %s", token);
592 glist[glistlen] = nid;
604 if (!SSL_CTX_set1_groups_list(ctx->
ctx, groups))
620 cert = SSL_CTX_get0_certificate(ctx->
ctx);
627 ret = X509_cmp_time(X509_get0_notBefore(cert), NULL);
634 msg(
M_WARN,
"WARNING: Your certificate is not yet valid!");
637 ret = X509_cmp_time(X509_get0_notAfter(cert), NULL);
644 msg(
M_WARN,
"WARNING: Your certificate has expired!");
658 if (!(bio = BIO_new_mem_buf((
char *)dh_file, -1)))
666 if (!(bio = BIO_new_file(dh_file,
"r")))
672 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
673 EVP_PKEY *dh = PEM_read_bio_Parameters(bio, NULL);
681 if (!SSL_CTX_set0_tmp_dh_pkey(ctx->
ctx, dh))
687 8 * EVP_PKEY_get_size(dh));
689 DH *dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
697 if (!SSL_CTX_set_tmp_dh(ctx->
ctx, dh))
712 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
713 if (curve_name != NULL)
715 msg(
M_WARN,
"WARNING: OpenSSL 3.0+ builds do not support specifying an "
716 "ECDH curve with --ecdh-curve, using default curves. Use "
717 "--tls-groups to specify groups.");
719 #elif !defined(OPENSSL_NO_EC)
722 const char *sname = NULL;
725 SSL_CTX_set_options(ctx->
ctx, SSL_OP_SINGLE_ECDH_USE);
727 if (curve_name != NULL)
730 msg(
D_TLS_DEBUG,
"Using user specified ECDH curve (%s)", curve_name);
731 nid = OBJ_sn2nid(curve_name);
739 sname = OBJ_nid2sn(nid);
746 if (NID_undef == nid || NULL == (ecdh = EC_KEY_new_by_curve_name(nid)))
749 ecdh = EC_KEY_new_by_curve_name(NID_secp384r1);
750 const char *source = (NULL == curve_name) ?
751 "extract curve from certificate" :
"use supplied curve";
753 "Failed to %s (%s), using secp384r1 instead.", source, sname);
754 sname = OBJ_nid2sn(NID_secp384r1);
757 if (!SSL_CTX_set_tmp_ecdh(ctx->
ctx, ecdh))
766 msg(
D_LOW,
"Your OpenSSL library was built without elliptic curve support."
767 " Skipping ECDH parameter loading.");
773 bool pkcs12_file_inline,
bool load_ca_file)
778 STACK_OF(X509) *ca = NULL;
785 if (pkcs12_file_inline)
787 BIO *b64 = BIO_new(BIO_f_base64());
788 BIO *bio = BIO_new_mem_buf((
void *) pkcs12_file,
789 (
int) strlen(pkcs12_file));
792 p12 = d2i_PKCS12_bio(b64, NULL);
807 p12 = d2i_PKCS12_fp(fp, NULL);
816 if (!PKCS12_parse(p12,
"", &pkey, &cert, &ca))
821 if (!PKCS12_parse(p12, password, &pkey, &cert, &ca))
824 "or unsupported/legacy encryption");
825 #ifdef ENABLE_MANAGEMENT
826 if (
management && (ERR_GET_REASON(ERR_peek_error()) == PKCS12_R_MAC_VERIFY_FAILURE))
838 if (!SSL_CTX_use_certificate(ctx->
ctx, cert))
845 if (!SSL_CTX_use_PrivateKey(ctx->
ctx, pkey))
851 if (!SSL_CTX_check_private_key(ctx->
ctx))
863 if (ca && sk_X509_num(ca))
865 for (i = 0; i < sk_X509_num(ca); i++)
867 X509_STORE *cert_store = SSL_CTX_get_cert_store(ctx->
ctx);
868 if (!X509_STORE_add_cert(cert_store, sk_X509_value(ca, i)))
870 crypto_msg(
M_FATAL,
"Cannot add certificate to certificate chain (X509_STORE_add_cert)");
872 if (!SSL_CTX_add_client_CA(ctx->
ctx, sk_X509_value(ca, i)))
874 crypto_msg(
M_FATAL,
"Cannot add certificate to client CA list (SSL_CTX_add_client_CA)");
886 if (ca && sk_X509_num(ca))
888 for (i = 0; i < sk_X509_num(ca); i++)
890 if (!SSL_CTX_add_extra_chain_cert(ctx->
ctx, sk_X509_value(ca, i)))
892 crypto_msg(
M_FATAL,
"Cannot add extra certificate to chain (SSL_CTX_add_extra_chain_cert)");
900 #ifdef ENABLE_CRYPTOAPI
909 crypto_msg(
M_FATAL,
"Cannot load certificate \"%s\" from Microsoft Certificate Store", cryptoapi_cert);
921 if (!PEM_read_bio_X509(bio, &cert, NULL, NULL))
928 && ERR_GET_REASON(ERR_peek_error()) == PEM_R_NO_START_LINE)
931 (void)ERR_get_error();
939 if (SSL_CTX_add_extra_chain_cert(ctx->
ctx, cert) != 1)
950 bool cert_file_inline)
958 if (cert_file_inline)
960 in = BIO_new_mem_buf((
char *) cert_file, -1);
964 in = BIO_new_file(cert_file,
"r");
969 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_SYS_LIB);
973 x = PEM_read_bio_X509(in, NULL,
974 SSL_CTX_get_default_passwd_cb(ctx->
ctx),
975 SSL_CTX_get_default_passwd_cb_userdata(ctx->
ctx));
978 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE, ERR_R_PEM_LIB);
982 ret = SSL_CTX_use_certificate(ctx->
ctx, x);
992 if (cert_file_inline)
1012 bool priv_key_file_inline)
1014 SSL_CTX *ssl_ctx = NULL;
1016 EVP_PKEY *pkey = NULL;
1023 if (priv_key_file_inline)
1025 in = BIO_new_mem_buf((
char *) priv_key_file, -1);
1029 in = BIO_new_file(priv_key_file,
"r");
1037 pkey = PEM_read_bio_PrivateKey(in, NULL,
1038 SSL_CTX_get_default_passwd_cb(ctx->
ctx),
1039 SSL_CTX_get_default_passwd_cb_userdata(ctx->
ctx));
1041 if (!pkey || !SSL_CTX_use_PrivateKey(ssl_ctx, pkey))
1043 #ifdef ENABLE_MANAGEMENT
1044 if (
management && (ERR_GET_REASON(ERR_peek_error()) == EVP_R_BAD_DECRYPT))
1055 if (!SSL_CTX_check_private_key(ssl_ctx))
1062 EVP_PKEY_free(pkey);
1073 X509_STORE *store = SSL_CTX_get_cert_store(ssl_ctx->
ctx);
1082 STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store);
1083 for (
int i = 0; i < sk_X509_OBJECT_num(objs); i++)
1085 X509_OBJECT *obj = sk_X509_OBJECT_value(objs, i);
1087 if (X509_OBJECT_get_type(obj) == X509_LU_CRL)
1089 sk_X509_OBJECT_delete(objs, i);
1090 X509_OBJECT_free(obj);
1094 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
1098 in = BIO_new_mem_buf((
char *) crl_file, -1);
1102 in = BIO_new_file(crl_file,
"r");
1112 int num_crls_loaded = 0;
1115 X509_CRL *crl = PEM_read_bio_X509_CRL(in, NULL, NULL, NULL);
1121 bool eof = ERR_GET_REASON(ERR_peek_error()) == PEM_R_NO_START_LINE;
1123 if (num_crls_loaded > 0 && eof)
1126 (void)ERR_get_error();
1135 if (!X509_STORE_add_crl(store, crl))
1145 msg(
M_INFO,
"CRL: loaded %d CRLs from file %s", num_crls_loaded, crl_file);
1151 #if defined(ENABLE_MANAGEMENT) && !defined(HAVE_XKEY_PROVIDER)
1155 rsa_pub_enc(
int flen,
const unsigned char *from,
unsigned char *to, RSA *rsa,
int padding)
1163 rsa_pub_dec(
int flen,
const unsigned char *from,
unsigned char *to, RSA *rsa,
int padding)
1171 rsa_priv_dec(
int flen,
const unsigned char *from,
unsigned char *to, RSA *rsa,
int padding)
1184 const RSA_METHOD *meth = RSA_get_method(rsa);
1185 RSA_meth_free((RSA_METHOD *)meth);
1198 case RSA_PKCS1_PADDING:
1199 return "RSA_PKCS1_PADDING";
1201 case RSA_NO_PADDING:
1202 return "RSA_NO_PADDING";
1222 unsigned char *sig,
unsigned int siglen,
1223 const char *algorithm)
1225 char *in_b64 = NULL;
1226 char *out_b64 = NULL;
1248 rsa_priv_enc(
int flen,
const unsigned char *from,
unsigned char *to, RSA *rsa,
1251 unsigned int len = RSA_size(rsa);
1254 if (padding != RSA_PKCS1_PADDING && padding != RSA_NO_PADDING)
1256 RSAerr(RSA_F_RSA_OSSL_PRIVATE_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
1262 return (ret == len) ? ret : -1;
1269 RSA_METHOD *rsa_meth;
1273 const RSA *pub_rsa = EVP_PKEY_get0_RSA(pkey);
1277 rsa_meth = RSA_meth_new(
"OpenVPN external private key RSA Method",
1278 RSA_METHOD_FLAG_NO_CHECK);
1284 RSA_meth_set_init(rsa_meth, NULL);
1286 RSA_meth_set0_app_data(rsa_meth, NULL);
1292 SSLerr(SSL_F_SSL_USE_PRIVATEKEY, ERR_R_MALLOC_FAILURE);
1297 const BIGNUM *n = NULL;
1298 const BIGNUM *e = NULL;
1299 RSA_get0_key(pub_rsa, &n, &e, NULL);
1300 RSA_set0_key(rsa, BN_dup(n), BN_dup(e), NULL);
1301 RSA_set_flags(rsa, RSA_flags(rsa) | RSA_FLAG_EXT_PKEY);
1302 if (!RSA_set_method(rsa, rsa_meth))
1304 RSA_meth_free(rsa_meth);
1310 if (!SSL_CTX_use_RSAPrivateKey(ctx->
ctx, rsa))
1325 RSA_meth_free(rsa_meth);
1330 #if !defined(OPENSSL_NO_EC)
1337 const EC_KEY_METHOD *ec_meth = EC_KEY_get_method(ec);
1338 EC_KEY_METHOD_free((EC_KEY_METHOD *) ec_meth);
1346 ecdsa_sign(
int type,
const unsigned char *dgst,
int dgstlen,
unsigned char *sig,
1347 unsigned int *siglen,
const BIGNUM *kinv,
const BIGNUM *r, EC_KEY *ec)
1349 int capacity = ECDSA_size(ec);
1378 const BIGNUM *in_r, EC_KEY *ec)
1380 ECDSA_SIG *ecsig = NULL;
1381 unsigned int len = ECDSA_size(ec);
1384 unsigned char *buf =
gc_malloc(len,
false, &gc);
1385 if (
ecdsa_sign(0, dgst, dgstlen, buf, &len, NULL, NULL, ec) != 1)
1390 ecsig = d2i_ECDSA_SIG(NULL, (
const unsigned char **)&buf, len);
1401 EVP_PKEY *privkey = NULL;
1402 EC_KEY_METHOD *ec_method;
1406 ec_method = EC_KEY_METHOD_new(EC_KEY_OpenSSL());
1416 ec = EC_KEY_dup(EVP_PKEY_get0_EC_KEY(pkey));
1419 EC_KEY_METHOD_free(ec_method);
1422 if (!EC_KEY_set_method(ec, ec_method))
1424 EC_KEY_METHOD_free(ec_method);
1429 privkey = EVP_PKEY_new();
1430 if (!EVP_PKEY_assign_EC_KEY(privkey, ec))
1436 if (!SSL_CTX_use_PrivateKey(ctx->
ctx, privkey))
1442 EVP_PKEY_free(privkey);
1447 EVP_PKEY_free(privkey);
1454 #ifdef ENABLE_MANAGEMENT
1462 X509 *cert = SSL_CTX_get0_certificate(ctx->
ctx);
1467 EVP_PKEY *pkey = X509_get0_pubkey(cert);
1470 #ifdef HAVE_XKEY_PROVIDER
1471 EVP_PKEY *privkey = xkey_load_management_key(
tls_libctx, pkey);
1473 || !SSL_CTX_use_PrivateKey(ctx->
ctx, privkey))
1475 EVP_PKEY_free(privkey);
1478 EVP_PKEY_free(privkey);
1480 #if OPENSSL_VERSION_NUMBER < 0x30000000L
1481 if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA)
1483 if (EVP_PKEY_is_a(pkey,
"RSA"))
1491 #if !defined(OPENSSL_NO_EC)
1492 #if OPENSSL_VERSION_NUMBER < 0x30000000L
1493 else if (EVP_PKEY_id(pkey) == EVP_PKEY_EC)
1495 else if (EVP_PKEY_is_a(pkey,
"EC"))
1505 crypto_msg(
M_WARN,
"management-external-key requires an RSA or EC certificate");
1532 return X509_NAME_cmp(*a, *b);
1537 bool ca_file_inline,
const char *ca_path,
bool tls_server)
1539 STACK_OF(X509_INFO) *info_stack = NULL;
1540 STACK_OF(X509_NAME) *cert_names = NULL;
1541 X509_LOOKUP *lookup = NULL;
1542 X509_STORE *store = NULL;
1543 X509_NAME *xn = NULL;
1545 int i, added = 0, prev = 0;
1549 store = SSL_CTX_get_cert_store(ctx->
ctx);
1560 in = BIO_new_mem_buf((
char *)ca_file, -1);
1564 in = BIO_new_file(ca_file,
"r");
1569 info_stack = PEM_X509_INFO_read_bio(in, NULL, NULL, NULL);
1574 for (i = 0; i < sk_X509_INFO_num(info_stack); i++)
1576 X509_INFO *info = sk_X509_INFO_value(info_stack, i);
1579 X509_STORE_add_crl(store, info->crl);
1582 if (tls_server && !info->x509)
1589 X509_STORE_add_cert(store, info->x509);
1598 if (cert_names == NULL)
1607 xn = X509_get_subject_name(info->x509);
1614 if (sk_X509_NAME_find(cert_names, xn) == -1)
1616 xn = X509_NAME_dup(xn);
1621 sk_X509_NAME_push(cert_names, xn);
1627 int cnum = sk_X509_NAME_num(cert_names);
1628 if (cnum != (prev + 1))
1631 "Cannot load CA certificate file %s (entry %d did not validate)",
1638 sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
1643 SSL_CTX_set_client_CA_list(ctx->
ctx, cert_names);
1649 "Cannot load CA certificate file %s (no entries were read)",
1655 int cnum = sk_X509_NAME_num(cert_names);
1659 "of %d entries were valid X509 names)",
1671 lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
1672 if (lookup && X509_LOOKUP_add_dir(lookup, ca_path, X509_FILETYPE_PEM))
1674 msg(
M_WARN,
"WARNING: experimental option --capath %s", ca_path);
1680 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
1686 bool extra_certs_file_inline)
1689 if (extra_certs_file_inline)
1691 in = BIO_new_mem_buf((
char *)extra_certs_file, -1);
1695 in = BIO_new_file(extra_certs_file,
"r");
1702 extra_certs_file_inline));
1726 #warning BIO_DEBUG defined
1729 static bool biofp_toggle;
1730 static time_t biofp_last_open;
1731 static const int biofp_reopen_interval = 600;
1746 const time_t current = time(NULL);
1747 const pid_t pid = getpid();
1749 if (biofp_last_open + biofp_reopen_interval < current)
1756 snprintf(fn,
sizeof(fn),
"bio/%d-%d.log", pid, biofp_toggle);
1757 biofp = fopen(fn,
"w");
1759 biofp_last_open = time(NULL);
1765 bio_debug_data(
const char *mode, BIO *bio,
const uint8_t *buf,
int len,
const char *desc)
1771 fprintf(biofp,
"BIO_%s %s time=%" PRIi64
" bio=" ptr_format " len=%d data=%s\n",
1779 bio_debug_oc(
const char *mode, BIO *bio)
1782 fprintf(biofp,
"BIO %s time=%" PRIi64
" bio=" ptr_format "\n",
1783 mode, (int64_t)time(NULL), (
ptr_type)bio);
1793 bio_write(BIO *bio,
const uint8_t *data,
int size,
const char *desc)
1807 bio_debug_data(
"write", bio, data, size, desc);
1809 i = BIO_write(bio, data, size);
1813 if (!BIO_should_retry(bio))
1870 int i = BIO_read(bio,
BPTR(buf), len);
1875 bio_debug_data(
"read", bio,
BPTR(buf), i, desc);
1881 if (!BIO_should_retry(bio))
1910 ks_ssl->
ssl = SSL_new(ssl_ctx->
ctx);
1925 bio_debug_oc(
"open ssl_bio", ks_ssl->
ssl_bio);
1926 bio_debug_oc(
"open ct_in", ks_ssl->
ct_in);
1927 bio_debug_oc(
"open ct_out", ks_ssl->
ct_out);
1932 SSL_set_accept_state(ks_ssl->
ssl);
1936 SSL_set_connect_state(ks_ssl->
ssl);
1940 BIO_set_ssl(ks_ssl->
ssl_bio, ks_ssl->
ssl, BIO_NOCLOSE);
1946 SSL_set_shutdown(ks_ssl->
ssl, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN);
1955 bio_debug_oc(
"close ssl_bio", ks_ssl->
ssl_bio);
1956 bio_debug_oc(
"close ct_in", ks_ssl->
ct_in);
1957 bio_debug_oc(
"close ct_out", ks_ssl->
ct_out);
1959 BIO_free_all(ks_ssl->
ssl_bio);
1960 SSL_free(ks_ssl->
ssl);
1973 "tls_write_plaintext");
2040 const char *curve =
"";
2041 const char *type =
"(error getting type)";
2049 int typeid = EVP_PKEY_id(pkey);
2050 #if OPENSSL_VERSION_NUMBER < 0x30000000L
2051 bool is_ec =
typeid == EVP_PKEY_EC;
2053 bool is_ec = EVP_PKEY_is_a(pkey,
"EC");
2056 #ifndef OPENSSL_NO_EC
2067 curve =
"(error getting curve name)";
2073 #if OPENSSL_VERSION_NUMBER < 0x30000000L
2074 type = OBJ_nid2sn(
typeid);
2078 if (
typeid == EVP_PKEY_RSA)
2082 else if (
typeid == EVP_PKEY_DSA)
2086 else if (
typeid == EVP_PKEY_EC)
2089 type =
"EC, curve ";
2091 else if (type == NULL)
2093 type =
"unknown type";
2096 type = EVP_PKEY_get0_type_name(pkey);
2099 type =
"(error getting public key type)";
2104 snprintf(buf, buflen,
"%d bits %s%s",
2105 EVP_PKEY_bits(pkey), type, curve);
2117 EVP_PKEY *pkey = X509_get_pubkey(cert);
2118 char pkeybuf[64] = { 0 };
2121 char sig[128] = { 0 };
2122 int signature_nid = X509_get_signature_nid(cert);
2123 if (signature_nid != 0)
2125 snprintf(sig,
sizeof(sig),
", signature: %s",
2126 OBJ_nid2sn(signature_nid));
2129 snprintf(buf, buflen,
", peer certificate: %s%s",
2132 EVP_PKEY_free(pkey);
2138 EVP_PKEY *pkey = NULL;
2139 SSL_get_peer_tmp_key(ssl, &pkey);
2145 char pkeybuf[128] = { 0 };
2148 snprintf(buf, buflen,
", peer temporary key: %s",
2151 EVP_PKEY_free(pkey);
2154 #if !defined(LIBRESSL_VERSION_NUMBER) \
2155 || (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER >= 0x3090000fL)
2180 return "(error getting name)";
2183 return OBJ_nid2sn(nid);
2195 int peer_sig_nid = NID_undef, peer_sig_type_nid = NID_undef;
2196 const char *peer_sig =
"unknown";
2197 const char *peer_sig_type =
"unknown type";
2202 #if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER > 0x3050400fL
2203 if (SSL_get_peer_signature_nid(ssl, &peer_sig_nid)
2204 && peer_sig_nid != NID_undef)
2206 peer_sig = OBJ_nid2sn(peer_sig_nid);
2210 #if !defined(LIBRESSL_VERSION_NUMBER) \
2211 || (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER >= 0x3090000fL)
2214 if (SSL_get_peer_signature_type_nid(ssl, &peer_sig_type_nid)
2215 && peer_sig_type_nid != NID_undef)
2221 if (peer_sig_nid == NID_undef && peer_sig_type_nid == NID_undef)
2226 snprintf(buf, buflen,
", peer signing digest/type: %s %s",
2227 peer_sig, peer_sig_type);
2242 const SSL_CIPHER *ciph;
2248 s1[0] = s2[0] = s3[0] = s4[0] = 0;
2249 ciph = SSL_get_current_cipher(ks_ssl->
ssl);
2250 snprintf(s1,
sizeof(s1),
"%s %s, cipher %s %s",
2252 SSL_get_version(ks_ssl->
ssl),
2253 SSL_CIPHER_get_version(ciph),
2254 SSL_CIPHER_get_name(ciph));
2255 X509 *cert = SSL_get_peer_certificate(ks_ssl->
ssl);
2270 const char *tls_cert_profile,
2275 tls_ctx.
ctx = SSL_CTX_new(SSLv23_method());
2281 #if defined(TLS1_3_VERSION)
2284 SSL_CTX_set_min_proto_version(tls_ctx.
ctx,
2291 SSL_CTX_set_max_proto_version(tls_ctx.
ctx, TLS1_2_VERSION);
2297 SSL *ssl = SSL_new(tls_ctx.
ctx);
2303 #if OPENSSL_VERSION_NUMBER < 0x1010000fL
2304 STACK_OF(SSL_CIPHER) *sk = SSL_get_ciphers(ssl);
2306 STACK_OF(SSL_CIPHER) *sk = SSL_get1_supported_ciphers(ssl);
2308 for (
int i = 0; i < sk_SSL_CIPHER_num(sk); i++)
2310 const SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, i);
2312 const char *cipher_name = SSL_CIPHER_get_name(c);
2319 printf(
"%s\n", cipher_name);
2321 else if (NULL == pair)
2324 printf(
"%s (No IANA name known to OpenVPN, use OpenSSL name.)\n",
2332 #if (OPENSSL_VERSION_NUMBER >= 0x1010000fL)
2333 sk_SSL_CIPHER_free(sk);
2336 SSL_CTX_free(tls_ctx.
ctx);
2346 printf(
"Consider using 'openssl ecparam -list_curves' as alternative to running\n"
2348 "Note this output does only list curves/groups that OpenSSL considers as\n"
2349 "builtin EC curves. It does not list additional curves nor X448 or X25519\n");
2350 #ifndef OPENSSL_NO_EC
2351 EC_builtin_curve *curves = NULL;
2355 crv_len = EC_get_builtin_curves(NULL, 0);
2357 if (EC_get_builtin_curves(curves, crv_len))
2359 printf(
"\nAvailable Elliptic curves/groups:\n");
2360 for (n = 0; n < crv_len; n++)
2363 sname = OBJ_nid2sn(curves[n].nid);
2369 printf(
"%s\n", sname);
2378 msg(
M_WARN,
"Your OpenSSL library was built without elliptic curve support. "
2379 "No curves available.");
2388 const char *cipher_name;
2390 ctx = SSL_CTX_new(SSLv23_method());
2401 cipher_name = SSL_get_cipher_list(ssl, 0);
2411 return OpenSSL_version(OPENSSL_VERSION);
2416 #ifdef HAVE_XKEY_PROVIDER
2420 const char *name = OSSL_PROVIDER_get0_name(prov);
2421 OSSL_PROVIDER_load(dest_libctx, name);
2429 OSSL_PROVIDER_unload(prov);
2444 #ifdef HAVE_XKEY_PROVIDER
2457 OSSL_PROVIDER_do_all(NULL, provider_load,
tls_libctx);
2460 if (!OSSL_PROVIDER_available(
tls_libctx,
"ovpn.xkey"))
2462 OSSL_PROVIDER_add_builtin(
tls_libctx,
"ovpn.xkey", xkey_provider_init);
2463 if (!OSSL_PROVIDER_load(
tls_libctx,
"ovpn.xkey"))
2465 msg(
M_NONFATAL,
"ERROR: failed loading external key provider: "
2466 "Signing with external keys will not work.");
2476 EVP_set_default_properties(
tls_libctx,
"?provider!=ovpn.xkey");
2487 #ifdef HAVE_XKEY_PROVIDER
2490 OSSL_PROVIDER_do_all(
tls_libctx, provider_unload, NULL);