Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmake/findcoredeps.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set(CORE_DIR ${CMAKE_CURRENT_LIST_DIR}/..)

set(DEP_DIR ${CORE_DIR}/../deps CACHE PATH "Dependencies")
option(USE_MBEDTLS "Use mbed TLS instead of OpenSSL")
option(USE_AWSLC "Use AWS-LC instead of OpenSSL")

option(USE_WERROR "Treat compiler warnings as errors (-Werror)")
option(USE_WCONVERSION "Enable -Wconversion")
Expand Down Expand Up @@ -43,6 +44,10 @@ function(add_ssl_library target)
set(SSL_LIBRARY mbedTLS::mbedTLS)
endif ()
target_compile_definitions(${target} PRIVATE -DUSE_MBEDTLS)
elseif (${USE_AWSLC})
find_package(ssl CONFIG REQUIRED)
set(SSL_LIBRARY AWS::ssl)
target_compile_definitions(${target} PRIVATE -DUSE_OPENSSL)
Comment thread
prasden marked this conversation as resolved.
Resolved
else ()
pkg_search_module(OpenSSL REQUIRED IMPORTED_TARGET openssl)
SET(SSL_LIBRARY PkgConfig::OpenSSL)
Expand Down
16 changes: 16 additions & 0 deletions openvpn/openssl/aws-lc-compat.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012- OpenVPN Inc.
//
// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
//

#pragma once

#define BIOerr(f, r) OPENSSL_PUT_ERROR(BIO, r)

#define SSL_R_TLSV13_ALERT_CERTIFICATE_REQUIRED SSL_R_TLSV1_ALERT_CERTIFICATE_REQUIRED
4 changes: 4 additions & 0 deletions openvpn/openssl/compat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,7 @@ SSL_get0_group_name(SSL *s)
return SSL_group_to_name(s, nid);
}
#endif

#ifdef OPENSSL_IS_AWSLC
#include <openvpn/openssl/aws-lc-compat.hpp>
#endif
4 changes: 2 additions & 2 deletions openvpn/openssl/crypto/cipheraead.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class CipherContextAEAD
openssl_clear_error_stack();
throw openssl_gcm_error("EVP_EncryptInit_ex (reset)");
}
if (!EVP_EncryptUpdate(ctx, nullptr, &len, ad, int(ad_len)))
if (ad_len && !EVP_EncryptUpdate(ctx, nullptr, &len, ad, int(ad_len)))
{
openssl_clear_error_stack();
throw openssl_gcm_error("EVP_EncryptUpdate AD");
Expand Down Expand Up @@ -228,7 +228,7 @@ class CipherContextAEAD
}

int len;
if (!EVP_DecryptUpdate(ctx, nullptr, &len, ad, int(ad_len)))
if (ad_len && !EVP_DecryptUpdate(ctx, nullptr, &len, ad, int(ad_len)))
{
openssl_clear_error_stack();
throw openssl_gcm_error("EVP_DecryptUpdate AD");
Expand Down
21 changes: 21 additions & 0 deletions openvpn/openssl/crypto/tls1prf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,27 @@ class TLS1PRF

return true;
}
#elif defined(OPENSSL_IS_AWSLC)
static bool PRF(unsigned char *label,
const size_t label_len,
const unsigned char *sec,
const size_t slen,
unsigned char *out1,
const size_t olen)
{
return CRYPTO_tls1_prf(EVP_md5_sha1(),
out1,
olen,
sec,
slen,
reinterpret_cast<const char *>(label),
label_len,
nullptr,
0,
nullptr,
0)
== 1;
}
#else
Comment thread
prasden marked this conversation as resolved.
Resolved
static bool PRF(unsigned char *label,
const size_t label_len,
Expand Down
28 changes: 18 additions & 10 deletions openvpn/openssl/pki/extpki.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class ExternalPKIRsaImpl : public ExternalPKIImpl
ExternalPKIRsaImpl(SSL_CTX *ssl_ctx, ::X509 *cert, ExternalPKIBase *external_pki_arg, const std::string &alias)
: external_pki(external_pki_arg), alias(alias), n_errors(0)
{
if (rsa_self_data_index < 0)
throw ssl_external_pki("ExternalPKIRsaImpl::rsa_self_data_index is uninitialized");

RSA *rsa = nullptr;
const RSA *pub_rsa = nullptr;
RSA_METHOD *rsa_meth = nullptr;
Expand All @@ -46,7 +49,6 @@ class ExternalPKIRsaImpl : public ExternalPKIImpl
RSA_meth_set_priv_dec(rsa_meth, rsa_priv_dec);
RSA_meth_set_init(rsa_meth, nullptr);
RSA_meth_set_finish(rsa_meth, rsa_finish);
RSA_meth_set0_app_data(rsa_meth, this);


/* get the public key */
Expand Down Expand Up @@ -86,6 +88,12 @@ class ExternalPKIRsaImpl : public ExternalPKIImpl
*/
rsa_meth = nullptr;

if (!RSA_set_ex_data(rsa, rsa_self_data_index, this))
{
errtext = "RSA_set_ex_data";
goto err;
}

/* bind our custom RSA object to ssl_ctx */
if (!SSL_CTX_use_RSAPrivateKey(ssl_ctx, rsa))
{
Expand All @@ -110,6 +118,11 @@ class ExternalPKIRsaImpl : public ExternalPKIImpl
return n_errors;
}

static void init_static()
{
rsa_self_data_index = RSA_get_ex_new_index(0, (char *)"ExternalPKIRsaImpl", nullptr, nullptr, nullptr);
}

private:
/* called at RSA_free */
static int rsa_finish(RSA *rsa)
Expand All @@ -122,7 +135,7 @@ class ExternalPKIRsaImpl : public ExternalPKIImpl
static int
rsa_priv_enc(int flen, const unsigned char *from, unsigned char *to, RSA *rsa, int padding)
{
ExternalPKIRsaImpl *self = (ExternalPKIRsaImpl *)(RSA_meth_get0_app_data(RSA_get_method(rsa)));
ExternalPKIRsaImpl *self = (ExternalPKIRsaImpl *)(RSA_get_ex_data(rsa, rsa_self_data_index));

try
{
Expand Down Expand Up @@ -173,7 +186,7 @@ class ExternalPKIRsaImpl : public ExternalPKIImpl

static void not_implemented(RSA *rsa)
{
ExternalPKIRsaImpl *self = (ExternalPKIRsaImpl *)(RSA_meth_get0_app_data(RSA_get_method(rsa)));
ExternalPKIRsaImpl *self = (ExternalPKIRsaImpl *)(RSA_get_ex_data(rsa, rsa_self_data_index));
++self->n_errors;
}

Expand Down Expand Up @@ -204,6 +217,7 @@ class ExternalPKIRsaImpl : public ExternalPKIImpl
ExternalPKIBase *external_pki;
std::string alias;
unsigned int n_errors;
inline static int rsa_self_data_index = -1;
};

/* The OpenSSL EC_* methods we are using here are only available for OpenSSL 1.1.0 and later */
Expand All @@ -227,7 +241,7 @@ class ExternalPKIECImpl : public ExternalPKIImpl

/* we only need to override a small number of methods */
EC_KEY_METHOD_set_init(ec_method, NULL, ec_finish, NULL, NULL, NULL, NULL);
EC_KEY_METHOD_set_sign(ec_method, ecdsa_sign, ecdsa_sign_setup, ecdsa_sign_sig);
EC_KEY_METHOD_set_sign(ec_method, ecdsa_sign, NULL, ecdsa_sign_sig);

/* get the public key */
EVP_PKEY *pubkey = X509_get0_pubkey(cert);
Expand Down Expand Up @@ -335,12 +349,6 @@ class ExternalPKIECImpl : public ExternalPKIImpl
}
}

static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
{
/* No precomputation, return success */
return 1;
}

static ECDSA_SIG *
ecdsa_sign_sig(const unsigned char *dgst, int dgstlen, const BIGNUM *kinvp, const BIGNUM *rp, EC_KEY *eckey)
{
Expand Down
2 changes: 2 additions & 0 deletions openvpn/openssl/pki/pkey.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,12 @@ class PKey
case EVP_PKEY_EC:
return PKType::PK_EC;
case EVP_PKEY_DSA:
#ifdef EVP_PKEY_DSA1
case EVP_PKEY_DSA1:
case EVP_PKEY_DSA2:
case EVP_PKEY_DSA3:
case EVP_PKEY_DSA4:
#endif
return PKType::PK_DSA;
case EVP_PKEY_NONE:
return PKType::PK_NONE;
Expand Down
7 changes: 5 additions & 2 deletions openvpn/openssl/sign/pkcs7verify.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ inline void verify_pkcs7(const std::list<OpenSSLPKI::X509> &certs,
STACK_OF(X509) *x509_stack = nullptr;
BIO *in = nullptr;
PKCS7 *p7 = nullptr;
X509_STORE *store = X509_STORE_new();

auto clean = Cleanup([&]()
{
Expand All @@ -46,7 +47,9 @@ inline void verify_pkcs7(const std::list<OpenSSLPKI::X509> &certs,
if (in)
BIO_free(in);
if (p7)
PKCS7_free(p7); });
PKCS7_free(p7);
if (store)
X509_STORE_free(store); });

/* create x509_stack from cert */
x509_stack = sk_X509_new_null();
Expand All @@ -71,7 +74,7 @@ inline void verify_pkcs7(const std::list<OpenSSLPKI::X509> &certs,
PKCS7_set_detached(p7, 1);

/* do the verify */
if (PKCS7_verify(p7, x509_stack, nullptr, in, nullptr, PKCS7_NOVERIFY) != 1)
if (PKCS7_verify(p7, x509_stack, store, in, nullptr, PKCS7_NOVERIFY) != 1)
throw OpenSSLException("OpenSSLSign::verify_pkcs7: verification failed");
}
} // namespace openvpn::OpenSSLSign
Expand Down
9 changes: 6 additions & 3 deletions openvpn/openssl/ssl/sslctx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,9 @@ class OpenSSLContext : public SSLFactoryAPI
static void init_static()
{
bmq_stream::init_static();
#if OPENSSL_VERSION_NUMBER < 0x30000010L && defined(ENABLE_EXTERNAL_PKI)
ExternalPKIRsaImpl::init_static();
#endif
#if OPENSSL_VERSION_NUMBER < 0x30000010L && !defined(OPENSSL_NO_EC) && defined(ENABLE_EXTERNAL_PKI)
ExternalPKIECImpl::init_static();
#endif
Expand Down Expand Up @@ -1100,7 +1103,8 @@ class OpenSSLContext : public SSLFactoryAPI
const SSL_CIPHER *ciph = SSL_get_current_cipher(ssl);
if (ciph)
{
char *desc = SSL_CIPHER_description(ciph, nullptr, 0);
char desc_buf[256];
const char *desc = SSL_CIPHER_description(ciph, desc_buf, sizeof(desc_buf));
if (!desc)
{
os << ", cipher: Error getting TLS cipher description from SSL_CIPHER_description";
Expand All @@ -1111,7 +1115,6 @@ class OpenSSLContext : public SSLFactoryAPI
// remove \n at the end and any other excess whitespace
string::trim(cipher_str);
os << ", cipher: " << cipher_str;
OPENSSL_free(desc);
}
}

Expand Down Expand Up @@ -1426,7 +1429,7 @@ class OpenSSLContext : public SSLFactoryAPI
SSL_CTX_set_ecdh_auto(ctx.get(), 1); // this method becomes a no-op in OpenSSL 1.1
#endif

#if OPENSSL_VERSION_NUMBER >= 0x10100000L
#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(OPENSSL_IS_AWSLC)
switch (TLSCertProfile::default_if_undef(config->tls_cert_profile))
{
case TLSCertProfile::UNDEF:
Expand Down
7 changes: 7 additions & 0 deletions openvpn/openssl/util/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <openvpn/common/exception.hpp>
#include <openvpn/error/error.hpp>
#include <openvpn/error/excode.hpp>
#include <openvpn/openssl/compat.hpp>

namespace openvpn {

Expand Down Expand Up @@ -147,21 +148,27 @@ class OpenSSLException : public ExceptionCode
case SSL_R_UNSUPPORTED_PROTOCOL:
set_code(Error::TLS_VERSION_MIN, true);
break;
#ifdef SSL_R_CA_MD_TOO_WEAK
case SSL_R_CA_MD_TOO_WEAK:
set_code(Error::SSL_CA_MD_TOO_WEAK, true);
break;
#endif
#ifdef SSL_R_CA_KEY_TOO_SMALL
case SSL_R_CA_KEY_TOO_SMALL:
set_code(Error::SSL_CA_KEY_TOO_SMALL, true);
break;
#endif
#ifdef SSL_R_LEGACY_SIGALG_DISALLOWED_OR_UNSUPPORTED
/* This error code has been added in OpenSSL 3.0.8 */
case SSL_R_LEGACY_SIGALG_DISALLOWED_OR_UNSUPPORTED:
set_code(Error::TLS_SIGALG_DISALLOWED_OR_UNSUPPORTED, true);
break;
#endif
#ifdef SSL_R_DH_KEY_TOO_SMALL
case SSL_R_DH_KEY_TOO_SMALL:
set_code(Error::SSL_DH_KEY_TOO_SMALL, true);
break;
#endif
case SSL_R_TLSV1_ALERT_PROTOCOL_VERSION:
set_code(Error::TLS_ALERT_PROTOCOL_VERSION, true);
break;
Expand Down