Skip to content

Open SSL v 1.1 support #204 #233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
7 changes: 6 additions & 1 deletion src/crypto/include/scy/crypto/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ class Crypto_API Hash
protected:
Hash& operator=(Hash const&);

EVP_MD_CTX _ctx;
#if OPENSSL_VERSION_NUMBER < 0x10100000L
EVP_MD_CTX _ctx;
#else
EVP_MD_CTX* _ctxPtr;
#endif

const EVP_MD* _md;
crypto::ByteVec _digest;
std::string _algorithm;
Expand Down
2 changes: 2 additions & 0 deletions src/crypto/src/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ void init()

if (++_refCount == 1) {
#if OPENSSL_VERSION_NUMBER >= 0x0907000L
#if OPENSSL_VERSION_NUMBER < 0x10100000L
OPENSSL_config(NULL);
#endif
#endif
SSL_library_init();
SSL_load_error_strings();
Expand Down
23 changes: 23 additions & 0 deletions src/crypto/src/hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,23 @@ Hash::Hash(const std::string& algorithm)
if (!_md)
throw std::runtime_error("Algorithm not supported: " + algorithm);

#if OPENSSL_VERSION_NUMBER < 0x10100000L
EVP_DigestInit(&_ctx, _md);
#else
EVP_DigestInit(_ctxPtr, _md);
#endif
}


Hash::~Hash()
{
crypto::uninitializeEngine();

#if OPENSSL_VERSION_NUMBER < 0x10100000L
EVP_MD_CTX_cleanup(&_ctx);
#else
EVP_MD_CTX_free(_ctxPtr);
#endif
//EVP_MD_CTX_free(_ctx);
}

Expand All @@ -49,15 +57,26 @@ void Hash::reset()
{
//EVP_MD_CTX_free(_ctx);
//_ctx = EVP_MD_CTX_new();
#if OPENSSL_VERSION_NUMBER < 0x10100000L
internal::api(EVP_MD_CTX_cleanup(&_ctx));
internal::api(EVP_DigestInit(&_ctx, _md));
#else
internal::api(EVP_MD_CTX_cleanup(_ctxPtr));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
internal::api(EVP_MD_CTX_cleanup(_ctxPtr));
EVP_MD_CTX_free(_ctxPtr);

For OpenSSL 1.1.1

internal::api(EVP_DigestInit(_ctxPtr, _md));
#endif

_digest.clear();
}


void Hash::update(const void* data, size_t length)
{
#if OPENSSL_VERSION_NUMBER < 0x10100000L
internal::api(EVP_DigestUpdate(&_ctx, data, length));
#else
internal::api(EVP_DigestUpdate(_ctxPtr, data, length));
#endif

}


Expand All @@ -79,7 +98,11 @@ const ByteVec& Hash::digest()
if (_digest.size() == 0) {
_digest.resize(EVP_MAX_MD_SIZE); // TODO: Get actual algorithm size
unsigned int len = 0;
#if OPENSSL_VERSION_NUMBER < 0x10100000L
internal::api(EVP_DigestFinal(&_ctx, &_digest[0], &len));
#else
internal::api(EVP_DigestFinal(_ctxPtr, &_digest[0], &len));
#endif
_digest.resize(len);
}
return _digest;
Expand Down
18 changes: 14 additions & 4 deletions src/crypto/src/x509certificate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,13 @@ X509Certificate::X509Certificate(X509* pCert, bool shared)
{
assert(_certificate);

if (shared)
if (shared) {
#if OPENSSL_VERSION_NUMBER < 0x10100000L
_certificate->references++;
// X509_up_ref(_certificate); // OpenSSL >= 1.1.0
#else
X509_up_ref(_certificate); // OpenSSL >= 1.1.0
#endif
}

init();
}
Expand Down Expand Up @@ -241,8 +245,14 @@ void X509Certificate::extractNames(std::string& cmnName,
for (int i = 0; i < sk_GENERAL_NAME_num(names); ++i) {
const GENERAL_NAME* name = sk_GENERAL_NAME_value(names, i);
if (name->type == GEN_DNS) {
const char* data =
#if OPENSSL_VERSION_NUMBER < 0x10100000L
const char* data =
reinterpret_cast<char*>(ASN1_STRING_data(name->d.ia5));
#else
const char* data =
reinterpret_cast<const char*>(ASN1_STRING_get0_data(name->d.ia5));
#endif

size_t len = ASN1_STRING_length(name->d.ia5);
domainNames.insert(std::string(data, len));
}
Expand Down Expand Up @@ -310,4 +320,4 @@ const X509* X509Certificate::certificate() const
} // namespace scy


/// @\}
/// @\}