1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
nobu 2011-02-05 02:48:55 +00:00
parent 1069e5d665
commit 74e6dd4078
3 changed files with 35 additions and 7 deletions

View file

@ -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> Sat Feb 5 11:29:10 2011 Nobuyoshi Nakada <nobu@ruby-lang.org>
* ext/json/parser/parser.h (GET_PARSER): raise TypeError. * ext/json/parser/parser.h (GET_PARSER): raise TypeError.

View file

@ -10,10 +10,17 @@
*/ */
#include "ossl.h" #include "ossl.h"
#define WrapCipher(obj, klass, ctx) \
obj = Data_Wrap_Struct(klass, 0, ossl_cipher_free, ctx)
#define MakeCipher(obj, klass, ctx) \ #define MakeCipher(obj, klass, ctx) \
obj = Data_Make_Struct(klass, EVP_CIPHER_CTX, 0, ossl_cipher_free, 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); \ Data_Get_Struct(obj, EVP_CIPHER_CTX, ctx); \
} while (0)
#define GetCipher(obj, ctx) do { \
GetCipherInit(obj, ctx); \
if (!ctx) { \ if (!ctx) { \
ossl_raise(rb_eRuntimeError, "Cipher not inititalized!"); \ ossl_raise(rb_eRuntimeError, "Cipher not inititalized!"); \
} \ } \
@ -51,7 +58,7 @@ ossl_cipher_new(const EVP_CIPHER *cipher)
EVP_CIPHER_CTX *ctx; EVP_CIPHER_CTX *ctx;
ret = ossl_cipher_alloc(cCipher); ret = ossl_cipher_alloc(cCipher);
GetCipher(ret, ctx); AllocCipher(ret, ctx);
EVP_CIPHER_CTX_init(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); ossl_raise(eCipherError, NULL);
@ -74,11 +81,9 @@ ossl_cipher_free(EVP_CIPHER_CTX *ctx)
static VALUE static VALUE
ossl_cipher_alloc(VALUE klass) ossl_cipher_alloc(VALUE klass)
{ {
EVP_CIPHER_CTX *ctx;
VALUE obj; VALUE obj;
MakeCipher(obj, klass, ctx); WrapCipher(obj, klass, 0);
EVP_CIPHER_CTX_init(ctx);
return obj; return obj;
} }
@ -99,7 +104,12 @@ ossl_cipher_initialize(VALUE self, VALUE str)
char *name; char *name;
name = StringValuePtr(str); 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))) { if (!(cipher = EVP_get_cipherbyname(name))) {
ossl_raise(rb_eRuntimeError, "unsupported cipher algorithm (%s)", 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); rb_check_frozen(self);
if (self == other) return self; if (self == other) return self;
GetCipher(self, ctx1); GetCipherInit(self, ctx1);
if (!ctx1) {
AllocCipher(self, ctx1);
}
SafeGetCipher(other, ctx2); SafeGetCipher(other, ctx2);
if (EVP_CIPHER_CTX_copy(ctx1, ctx2) != 1) if (EVP_CIPHER_CTX_copy(ctx1, ctx2) != 1)
ossl_raise(eCipherError, NULL); ossl_raise(eCipherError, NULL);

View file

@ -64,6 +64,11 @@ class OpenSSL::TestCipher < Test::Unit::TestCase
assert_raise(ArgumentError){ @c1.update("") } assert_raise(ArgumentError){ @c1.update("") }
end 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 if OpenSSL::OPENSSL_VERSION_NUMBER > 0x00907000
def test_ciphers def test_ciphers
OpenSSL::Cipher.ciphers.each{|name| OpenSSL::Cipher.ciphers.each{|name|