summaryrefslogtreecommitdiff
path: root/ext/openssl/ossl_cipher.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-02-05 02:48:55 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-02-05 02:48:55 +0000
commit74e6dd4078051efea4144fd289dc4c2b30fc9891 (patch)
treefc61a42138642f77115615950fb033558cd0a41d /ext/openssl/ossl_cipher.c
parent1069e5d665599ce60086f284b575d3a850ed2b01 (diff)
* 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
Diffstat (limited to 'ext/openssl/ossl_cipher.c')
-rw-r--r--ext/openssl/ossl_cipher.c27
1 files changed, 20 insertions, 7 deletions
diff --git a/ext/openssl/ossl_cipher.c b/ext/openssl/ossl_cipher.c
index 8b24df1..c164b8b 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);