summaryrefslogtreecommitdiff
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
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
-rw-r--r--ChangeLog10
-rw-r--r--ext/openssl/ossl_cipher.c27
-rw-r--r--test/openssl/test_cipher.rb5
3 files changed, 35 insertions, 7 deletions
diff --git a/ChangeLog b/ChangeLog
index 4a36a1c9da..5bd57fd4b1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+Sat Feb 5 11:48:47 2011 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * 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.
+
Sat Feb 5 11:29:10 2011 Nobuyoshi Nakada <nobu@ruby-lang.org>
* ext/json/parser/parser.h (GET_PARSER): raise TypeError.
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);
diff --git a/test/openssl/test_cipher.rb b/test/openssl/test_cipher.rb
index 5cf41879bd..70c963bbb0 100644
--- a/test/openssl/test_cipher.rb
+++ b/test/openssl/test_cipher.rb
@@ -64,6 +64,11 @@ class OpenSSL::TestCipher < Test::Unit::TestCase
assert_raise(ArgumentError){ @c1.update("") }
end
+ def test_initialize
+ assert_raise(RuntimeError) {@c1.__send__(:initialize, "DES-EDE3-CBC")}
+ assert_raise(RuntimeError) {OpenSSL::Cipher.allocate.final}
+ end
+
if OpenSSL::OPENSSL_VERSION_NUMBER > 0x00907000
def test_ciphers
OpenSSL::Cipher.ciphers.each{|name|