diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2016-04-09 20:54:53 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2016-04-21 00:46:34 +0900 |
commit | eeede322991bf2e1e8caad4b2933e7e37bd63a77 (patch) | |
tree | 251495371e973404b409408d436208570b32d6f8 /ext/openssl/ossl_cipher.c | |
parent | 20cd25c86fd28eb1b5068d0db607e6aa33107f65 (diff) | |
download | ruby-eeede322991bf2e1e8caad4b2933e7e37bd63a77.tar.gz |
wip
Diffstat (limited to 'ext/openssl/ossl_cipher.c')
-rw-r--r-- | ext/openssl/ossl_cipher.c | 44 |
1 files changed, 19 insertions, 25 deletions
diff --git a/ext/openssl/ossl_cipher.c b/ext/openssl/ossl_cipher.c index 09b021d987..e9808fb996 100644 --- a/ext/openssl/ossl_cipher.c +++ b/ext/openssl/ossl_cipher.c @@ -11,10 +11,6 @@ #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 GetCipherInit(obj, ctx) do { \ TypedData_Get_Struct((obj), EVP_CIPHER_CTX, &ossl_cipher_type, (ctx)); \ } while (0) @@ -37,13 +33,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, }; /* @@ -62,14 +58,16 @@ GetCipherPtr(VALUE obj) VALUE ossl_cipher_new(const EVP_CIPHER *cipher) { - VALUE ret; - EVP_CIPHER_CTX *ctx; + VALUE ret = ossl_cipher_alloc(cCipher); + EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); + if (!ctx) { + ossl_raise(rb_eRuntimeError, "EVP_CIPHER_CTX_new() failed"); + } + RTYPEDDATA_DATA(ret) = ctx; - ret = ossl_cipher_alloc(cCipher); - AllocCipher(ret, ctx); - EVP_CIPHER_CTX_init(ctx); - if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, -1) != 1) + if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, -1) != 1) { ossl_raise(eCipherError, NULL); + } return ret; } @@ -82,18 +80,10 @@ ossl_cipher_free(void *ptr) { EVP_CIPHER_CTX *ctx = ptr; if (ctx) { - EVP_CIPHER_CTX_cleanup(ctx); - ruby_xfree(ctx); + EVP_CIPHER_CTX_free(ctx); } } -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) { @@ -121,8 +111,12 @@ ossl_cipher_initialize(VALUE self, VALUE str) if (ctx) { ossl_raise(rb_eRuntimeError, "Cipher already inititalized!"); } - AllocCipher(self, ctx); - EVP_CIPHER_CTX_init(ctx); + + ctx = EVP_CIPHER_CTX_new(); + if (!ctx) { + ossl_raise(rb_eRuntimeError, "EVP_CIPHER_CTX_new() failed"); + } + RTYPEDDATA_DATA(self) = ctx; if (!(cipher = EVP_get_cipherbyname(name))) { ossl_raise(rb_eRuntimeError, "unsupported cipher algorithm (%s)", name); } |