Skip to content

Commit

Permalink
Fix build failure against OpenSSL 1.1 built with no-deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
markwright committed Oct 14, 2017
1 parent 5c1c0fa commit e103636
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ext/openssl/ossl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1111,10 +1111,14 @@ Init_openssl(void)
*/
/* CRYPTO_malloc_init(); */
/* ENGINE_load_builtin_engines(); */
#if !defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x10100000L)
OPENSSL_init_ssl(0, NULL);
#else
OpenSSL_add_ssl_algorithms();
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
SSL_load_error_strings();
#endif

/*
* FIXME:
Expand Down Expand Up @@ -1149,7 +1153,11 @@ Init_openssl(void)
/*
* Version of OpenSSL the ruby OpenSSL extension is running with
*/
#if !defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x10100000L)
rb_define_const(mOSSL, "OPENSSL_LIBRARY_VERSION", rb_str_new2(OpenSSL_version(OPENSSL_VERSION)));
#else
rb_define_const(mOSSL, "OPENSSL_LIBRARY_VERSION", rb_str_new2(SSLeay_version(SSLEAY_VERSION)));
#endif

/*
* Version number of OpenSSL the ruby OpenSSL extension was built with
Expand Down
7 changes: 7 additions & 0 deletions ext/openssl/ossl.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@
#if !defined(OPENSSL_NO_OCSP)
# include <openssl/ocsp.h>
#endif
#if !defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x10100000L)
# include <openssl/bn.h>
# include <openssl/rsa.h>
# include <openssl/dsa.h>
# include <openssl/evp.h>
# include <openssl/dh.h>
#endif

/*
* Common Module
Expand Down
28 changes: 28 additions & 0 deletions ext/openssl/ossl_cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,11 @@ ossl_cipher_set_iv(VALUE self, VALUE iv)
StringValue(iv);
GetCipher(self, ctx);

#if !defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x10100000L)
if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_AEAD_CIPHER)
#else
if (EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_FLAG_AEAD_CIPHER)
#endif
iv_len = (int)(VALUE)EVP_CIPHER_CTX_get_app_data(ctx);
if (!iv_len)
iv_len = EVP_CIPHER_CTX_iv_length(ctx);
Expand All @@ -535,7 +539,11 @@ ossl_cipher_is_authenticated(VALUE self)

GetCipher(self, ctx);

#if !defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x10100000L)
return (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_AEAD_CIPHER) ? Qtrue : Qfalse;
#else
return (EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_FLAG_AEAD_CIPHER) ? Qtrue : Qfalse;
#endif
}

/*
Expand Down Expand Up @@ -606,7 +614,11 @@ ossl_cipher_get_auth_tag(int argc, VALUE *argv, VALUE self)

GetCipher(self, ctx);

#if !defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x10100000L)
if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_AEAD_CIPHER))
#else
if (!(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_FLAG_AEAD_CIPHER))
#endif
ossl_raise(eCipherError, "authentication tag not supported by this cipher");

ret = rb_str_new(NULL, tag_len);
Expand Down Expand Up @@ -641,7 +653,11 @@ ossl_cipher_set_auth_tag(VALUE self, VALUE vtag)
tag_len = RSTRING_LENINT(vtag);

GetCipher(self, ctx);
#if !defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x10100000L)
if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_AEAD_CIPHER))
#else
if (!(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_FLAG_AEAD_CIPHER))
#endif
ossl_raise(eCipherError, "authentication tag not supported by this cipher");

if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, tag_len, tag))
Expand All @@ -668,7 +684,11 @@ ossl_cipher_set_auth_tag_len(VALUE self, VALUE vlen)
EVP_CIPHER_CTX *ctx;

GetCipher(self, ctx);
#if !defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x10100000L)
if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_AEAD_CIPHER))
#else
if (!(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_FLAG_AEAD_CIPHER))
#endif
ossl_raise(eCipherError, "AEAD not supported by this cipher");

if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, tag_len, NULL))
Expand All @@ -695,7 +715,11 @@ ossl_cipher_set_iv_length(VALUE self, VALUE iv_length)
EVP_CIPHER_CTX *ctx;

GetCipher(self, ctx);
#if !defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x10100000L)
if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_AEAD_CIPHER))
#else
if (!(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_FLAG_AEAD_CIPHER))
#endif
ossl_raise(eCipherError, "cipher does not support AEAD");

if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, len, NULL))
Expand Down Expand Up @@ -786,7 +810,11 @@ ossl_cipher_iv_length(VALUE self)
int len = 0;

GetCipher(self, ctx);
#if !defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x10100000L)
if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_AEAD_CIPHER)
#else
if (EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_FLAG_AEAD_CIPHER)
#endif
len = (int)(VALUE)EVP_CIPHER_CTX_get_app_data(ctx);
if (!len)
len = EVP_CIPHER_CTX_iv_length(ctx);
Expand Down
32 changes: 32 additions & 0 deletions ext/openssl/ossl_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,23 @@ VALUE eEngineError;
/*
* Private
*/
#if !defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x10100000L)
#define OSSL_ENGINE_LOAD_IF_MATCH(x) \
do{\
if(!strcmp(#x, RSTRING_PTR(name))){\
OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_##x, NULL); \
return Qtrue;\
}\
}while(0)
#else
#define OSSL_ENGINE_LOAD_IF_MATCH(x) \
do{\
if(!strcmp(#x, RSTRING_PTR(name))){\
ENGINE_load_##x();\
return Qtrue;\
}\
}while(0)
#endif

static void
ossl_engine_free(void *engine)
Expand Down Expand Up @@ -94,8 +104,12 @@ ossl_engine_s_load(int argc, VALUE *argv, VALUE klass)
StringValueCStr(name);
#ifndef OPENSSL_NO_STATIC_ENGINE
#if HAVE_ENGINE_LOAD_DYNAMIC
#if !defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x10100000L)
OSSL_ENGINE_LOAD_IF_MATCH(DYNAMIC);
#else
OSSL_ENGINE_LOAD_IF_MATCH(dynamic);
#endif
#endif
#if HAVE_ENGINE_LOAD_4758CCA
OSSL_ENGINE_LOAD_IF_MATCH(4758cca);
#endif
Expand All @@ -121,28 +135,44 @@ ossl_engine_s_load(int argc, VALUE *argv, VALUE klass)
OSSL_ENGINE_LOAD_IF_MATCH(ubsec);
#endif
#if HAVE_ENGINE_LOAD_PADLOCK
#if !defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x10100000L)
OSSL_ENGINE_LOAD_IF_MATCH(PADLOCK);
#else
OSSL_ENGINE_LOAD_IF_MATCH(padlock);
#endif
#endif
#if HAVE_ENGINE_LOAD_CAPI
#if !defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x10100000L)
OSSL_ENGINE_LOAD_IF_MATCH(CAPI);
#else
OSSL_ENGINE_LOAD_IF_MATCH(capi);
#endif
#endif
#if HAVE_ENGINE_LOAD_GMP
OSSL_ENGINE_LOAD_IF_MATCH(gmp);
#endif
#if HAVE_ENGINE_LOAD_GOST
OSSL_ENGINE_LOAD_IF_MATCH(gost);
#endif
#if HAVE_ENGINE_LOAD_CRYPTODEV
#if !defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x10100000L)
OSSL_ENGINE_LOAD_IF_MATCH(CRYPTODEV);
#else
OSSL_ENGINE_LOAD_IF_MATCH(cryptodev);
#endif
#endif
#if HAVE_ENGINE_LOAD_AESNI
OSSL_ENGINE_LOAD_IF_MATCH(aesni);
#endif
#endif
#ifdef HAVE_ENGINE_LOAD_OPENBSD_DEV_CRYPTO
OSSL_ENGINE_LOAD_IF_MATCH(openbsd_dev_crypto);
#endif
#if !defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x10100000L)
OSSL_ENGINE_LOAD_IF_MATCH(OPENSSL);
#else
OSSL_ENGINE_LOAD_IF_MATCH(openssl);
#endif
rb_warning("no such builtin loader for `%"PRIsVALUE"'", name);
return Qnil;
#endif /* HAVE_ENGINE_LOAD_BUILTIN_ENGINES */
Expand All @@ -160,7 +190,9 @@ ossl_engine_s_load(int argc, VALUE *argv, VALUE klass)
static VALUE
ossl_engine_s_cleanup(VALUE self)
{
#if defined(LIBRESSL_VERSION_NUMBER) || (OPENSSL_VERSION_NUMBER < 0x10100000L)
ENGINE_cleanup();
#endif
return Qnil;
}

Expand Down
8 changes: 8 additions & 0 deletions ext/openssl/ossl_x509cert.c
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,11 @@ ossl_x509_set_not_before(VALUE self, VALUE time)

GetX509(self, x509);
asn1time = ossl_x509_time_adjust(NULL, time);
#if !defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x10100000L)
if (!X509_set1_notBefore(x509, asn1time)) {
#else
if (!X509_set_notBefore(x509, asn1time)) {
#endif
ASN1_TIME_free(asn1time);
ossl_raise(eX509CertError, "X509_set_notBefore");
}
Expand Down Expand Up @@ -479,7 +483,11 @@ ossl_x509_set_not_after(VALUE self, VALUE time)

GetX509(self, x509);
asn1time = ossl_x509_time_adjust(NULL, time);
#if !defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x10100000L)
if (!X509_set1_notAfter(x509, asn1time)) {
#else
if (!X509_set_notAfter(x509, asn1time)) {
#endif
ASN1_TIME_free(asn1time);
ossl_raise(eX509CertError, "X509_set_notAfter");
}
Expand Down
8 changes: 8 additions & 0 deletions ext/openssl/ossl_x509crl.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,11 @@ ossl_x509crl_set_last_update(VALUE self, VALUE time)

GetX509CRL(self, crl);
asn1time = ossl_x509_time_adjust(NULL, time);
#if !defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x10100000L)
if (!X509_CRL_set1_lastUpdate(crl, asn1time)) {
#else
if (!X509_CRL_set_lastUpdate(crl, asn1time)) {
#endif
ASN1_TIME_free(asn1time);
ossl_raise(eX509CRLError, "X509_CRL_set_lastUpdate");
}
Expand Down Expand Up @@ -257,7 +261,11 @@ ossl_x509crl_set_next_update(VALUE self, VALUE time)

GetX509CRL(self, crl);
asn1time = ossl_x509_time_adjust(NULL, time);
#if !defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x10100000L)
if (!X509_CRL_set1_nextUpdate(crl, asn1time)) {
#else
if (!X509_CRL_set_nextUpdate(crl, asn1time)) {
#endif
ASN1_TIME_free(asn1time);
ossl_raise(eX509CRLError, "X509_CRL_set_nextUpdate");
}
Expand Down

0 comments on commit e103636

Please sign in to comment.