From 3889a0d192d484e2d0a27ebdda3311a2d7073c1c Mon Sep 17 00:00:00 2001 From: Kazuki Yamaguchi Date: Thu, 5 May 2016 15:50:41 +0900 Subject: ext/openssl: use EVP_CIPHER_CTX_{new,free} to allocate EVP_CIPHER_CTX EVP_CIPHER_CTX was made opaque in OpenSSL 1.1.0 --- ext/openssl/extconf.rb | 2 ++ ext/openssl/openssl_missing.c | 22 ++++++++++++++++++++++ ext/openssl/openssl_missing.h | 8 ++++++++ ext/openssl/ossl_cipher.c | 27 ++++++++++----------------- 4 files changed, 42 insertions(+), 17 deletions(-) diff --git a/ext/openssl/extconf.rb b/ext/openssl/extconf.rb index b72f605288..8af5e50e8a 100644 --- a/ext/openssl/extconf.rb +++ b/ext/openssl/extconf.rb @@ -78,6 +78,8 @@ engines.each { |name| } # added in 0.9.8X +have_func("EVP_CIPHER_CTX_new") +have_func("EVP_CIPHER_CTX_free") OpenSSL.check_func_or_macro("SSL_CTX_clear_options", "openssl/ssl.h") # added in 1.0.0 diff --git a/ext/openssl/openssl_missing.c b/ext/openssl/openssl_missing.c index 47c5ee8166..4222ce65bf 100644 --- a/ext/openssl/openssl_missing.c +++ b/ext/openssl/openssl_missing.c @@ -18,6 +18,28 @@ #include "openssl_missing.h" +/* added in 0.9.8X */ +#if !defined(HAVE_EVP_CIPHER_CTX_NEW) +EVP_CIPHER_CTX * +EVP_CIPHER_CTX_new(void) +{ + EVP_CIPHER_CTX *ctx = OPENSSL_malloc(sizeof(EVP_CIPHER_CTX)); + if (!ctx) + return NULL; + EVP_CIPHER_CTX_init(ctx); + return ctx; +} +#endif + +#if !defined(HAVE_EVP_CIPHER_CTX_FREE) +void +EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx) +{ + EVP_CIPHER_CTX_cleanup(ctx); + OPENSSL_free(ctx); +} +#endif + /* added in 1.0.0 */ #if !defined(HAVE_EVP_CIPHER_CTX_COPY) /* diff --git a/ext/openssl/openssl_missing.h b/ext/openssl/openssl_missing.h index e7ec792626..1261d78a79 100644 --- a/ext/openssl/openssl_missing.h +++ b/ext/openssl/openssl_missing.h @@ -11,6 +11,14 @@ #define _OSSL_OPENSSL_MISSING_H_ /* added in 0.9.8X */ +#if !defined(HAVE_EVP_CIPHER_CTX_NEW) +EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void); +#endif + +#if !defined(HAVE_EVP_CIPHER_CTX_FREE) +void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx); +#endif + #if !defined(HAVE_SSL_CTX_CLEAR_OPTIONS) # define SSL_CTX_clear_options(ctx, op) do \ (ctx)->options &= ~(op); while (0) diff --git a/ext/openssl/ossl_cipher.c b/ext/openssl/ossl_cipher.c index a09921a73d..e25871e45c 100644 --- a/ext/openssl/ossl_cipher.c +++ b/ext/openssl/ossl_cipher.c @@ -11,10 +11,12 @@ #define NewCipher(klass) \ TypedData_Wrap_Struct((klass), &ossl_cipher_type, 0) -#define MakeCipher(obj, klass, ctx) \ - (obj) = TypedData_Make_Struct((klass), EVP_CIPHER_CTX, &ossl_cipher_type, (ctx)) -#define AllocCipher(obj, ctx) \ - (DATA_PTR(obj) = (ctx) = ZALLOC(EVP_CIPHER_CTX)) +#define AllocCipher(obj, ctx) do { \ + (ctx) = EVP_CIPHER_CTX_new(); \ + if (!(ctx)) \ + ossl_raise(rb_eRuntimeError, NULL); \ + RTYPEDDATA_DATA(obj) = (ctx); \ +} while (0) #define GetCipherInit(obj, ctx) do { \ TypedData_Get_Struct((obj), EVP_CIPHER_CTX, &ossl_cipher_type, (ctx)); \ } while (0) @@ -37,13 +39,13 @@ VALUE eCipherError; static VALUE ossl_cipher_alloc(VALUE klass); static void ossl_cipher_free(void *ptr); -static size_t ossl_cipher_memsize(const void *ptr); static const rb_data_type_t ossl_cipher_type = { "OpenSSL/Cipher", - {0, ossl_cipher_free, ossl_cipher_memsize,}, - 0, 0, - RUBY_TYPED_FREE_IMMEDIATELY, + { + 0, ossl_cipher_free, + }, + 0, 0, RUBY_TYPED_FREE_IMMEDIATELY, }; /* @@ -67,7 +69,6 @@ ossl_cipher_new(const EVP_CIPHER *cipher) ret = ossl_cipher_alloc(cCipher); AllocCipher(ret, ctx); - EVP_CIPHER_CTX_init(ctx); if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, -1) != 1) ossl_raise(eCipherError, NULL); @@ -87,13 +88,6 @@ ossl_cipher_free(void *ptr) } } -static size_t -ossl_cipher_memsize(const void *ptr) -{ - const EVP_CIPHER_CTX *ctx = ptr; - return sizeof(*ctx); -} - static VALUE ossl_cipher_alloc(VALUE klass) { @@ -122,7 +116,6 @@ ossl_cipher_initialize(VALUE self, VALUE str) ossl_raise(rb_eRuntimeError, "Cipher already inititalized!"); } AllocCipher(self, ctx); - EVP_CIPHER_CTX_init(ctx); if (!(cipher = EVP_get_cipherbyname(name))) { ossl_raise(rb_eRuntimeError, "unsupported cipher algorithm (%s)", name); } -- cgit v1.2.3