From 74e6dd4078051efea4144fd289dc4c2b30fc9891 Mon Sep 17 00:00:00 2001 From: nobu Date: Sat, 5 Feb 2011 02:48:55 +0000 Subject: * ext/openssl/ossl_cipher.c (ossl_cipher_alloc): leave data ptr NULL. * ext/openssl/ossl_cipher.c (ossl_cipher_new, ossl_cipher_initialize): allocate internal structure. [ruby-core:35094] * ext/openssl/ossl_cipher.c (ossl_cipher_copy): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@30793 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/openssl/ossl_cipher.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) (limited to 'ext/openssl/ossl_cipher.c') diff --git a/ext/openssl/ossl_cipher.c b/ext/openssl/ossl_cipher.c index 8b24df1452..c164b8b0a3 100644 --- a/ext/openssl/ossl_cipher.c +++ b/ext/openssl/ossl_cipher.c @@ -10,10 +10,17 @@ */ #include "ossl.h" +#define WrapCipher(obj, klass, ctx) \ + obj = Data_Wrap_Struct(klass, 0, ossl_cipher_free, ctx) #define MakeCipher(obj, klass, ctx) \ obj = Data_Make_Struct(klass, EVP_CIPHER_CTX, 0, ossl_cipher_free, ctx) -#define GetCipher(obj, ctx) do { \ +#define AllocCipher(obj, ctx) \ + memset(DATA_PTR(obj) = (ctx) = ALLOC(EVP_CIPHER_CTX), 0, sizeof(EVP_CIPHER_CTX)) +#define GetCipherInit(obj, ctx) do { \ Data_Get_Struct(obj, EVP_CIPHER_CTX, ctx); \ +} while (0) +#define GetCipher(obj, ctx) do { \ + GetCipherInit(obj, ctx); \ if (!ctx) { \ ossl_raise(rb_eRuntimeError, "Cipher not inititalized!"); \ } \ @@ -51,7 +58,7 @@ ossl_cipher_new(const EVP_CIPHER *cipher) EVP_CIPHER_CTX *ctx; ret = ossl_cipher_alloc(cCipher); - GetCipher(ret, ctx); + AllocCipher(ret, ctx); EVP_CIPHER_CTX_init(ctx); if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, -1) != 1) ossl_raise(eCipherError, NULL); @@ -74,11 +81,9 @@ ossl_cipher_free(EVP_CIPHER_CTX *ctx) static VALUE ossl_cipher_alloc(VALUE klass) { - EVP_CIPHER_CTX *ctx; VALUE obj; - MakeCipher(obj, klass, ctx); - EVP_CIPHER_CTX_init(ctx); + WrapCipher(obj, klass, 0); return obj; } @@ -99,7 +104,12 @@ ossl_cipher_initialize(VALUE self, VALUE str) char *name; name = StringValuePtr(str); - GetCipher(self, ctx); + GetCipherInit(self, ctx); + if (ctx) { + 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); } @@ -116,7 +126,10 @@ ossl_cipher_copy(VALUE self, VALUE other) rb_check_frozen(self); if (self == other) return self; - GetCipher(self, ctx1); + GetCipherInit(self, ctx1); + if (!ctx1) { + AllocCipher(self, ctx1); + } SafeGetCipher(other, ctx2); if (EVP_CIPHER_CTX_copy(ctx1, ctx2) != 1) ossl_raise(eCipherError, NULL); -- cgit v1.2.3