mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* ext/openssl/extconf.rb: check for EVP_CIPHER_CTX_copy, ENGINE_add,
EVP_CIPHER_CTX_set_padding, EVP_CipherFinal_ex, EVP_CipherInit_ex, EVP_DigestFinal_ex and EVP_DigestInit_ex. * ext/openssl/openssl_missing.c (EVP_CIPHER_CTX_copy): new function. * ext/openssl/openssl_missing.h (EVP_DigestInit_ex, EVP_DigestFinal_ex, EVP_CipherInit_ex, EVP_CipherFinal_ex, HMAC_Init_ex): new macro for OpenSSL 0.9.6. * ext/openssl/ossl_cipher.c (ossl_cipher_alloc, ossl_cipher_initialize, ossl_cipher_copy, ossl_cipher_reset, ossl_cipher_encrypt, ossl_cipher_decrypt, ossl_cipher_final, ossl_cipher_set_key, ossl_cipher_set_iv): replace all EVP_CipherInit and EVP_CipherFinal into EVP_CipherInit_ex and EVP_CipherFinal_ex. and EVP_CIPHER_CTX_init should only be called once. * ext/openssl/ossl_cipher.c (ossl_cipher_set_padding): check for EVP_CIPHER_CTX_set_padding. * ext/openssl/ossl_cipher.c (Init_ossl_cipher): Cipher#<< is deprecated. * ext/openssl/ossl_digest.c: replace all EVP_DigestInit and EVP_DigestFinal into EVP_DigestInit_ex and EVP_DigestFinal_ex. and EVP_MD_CTX_init should only be called once. * ext/openssl/ossl_digest.c (digest_final): should call EVP_MD_CTX_cleanup to avoid memory leak. * ext/openssl/ossl_hmac.c (ossl_hmac_initialize): repalce HMAC_init into HMAC_init_ex. and HMAC_CTX_init is moved to ossl_hmac_alloc. * ext/openssl/ossl_hmac.c (hmac_final): should call HMAC_CTX_cleanup to avoid memory leak. * test/openssl/test_cipher.rb, test/openssl/test_digest.rb, test/openssl/test_hmac.rb: new file. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6548 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
ba0756ca01
commit
22ab58c03c
11 changed files with 319 additions and 68 deletions
|
@ -54,7 +54,7 @@ ossl_cipher_new(const EVP_CIPHER *cipher)
|
|||
ret = ossl_cipher_alloc(cCipher);
|
||||
GetCipher(ret, ctx);
|
||||
EVP_CIPHER_CTX_init(ctx);
|
||||
if (EVP_CipherInit(ctx, cipher, NULL, NULL, -1) != 1)
|
||||
if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, -1) != 1)
|
||||
ossl_raise(eCipherError, NULL);
|
||||
|
||||
return ret;
|
||||
|
@ -79,6 +79,7 @@ ossl_cipher_alloc(VALUE klass)
|
|||
VALUE obj;
|
||||
|
||||
MakeCipher(obj, klass, ctx);
|
||||
EVP_CIPHER_CTX_init(ctx);
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
@ -97,9 +98,8 @@ ossl_cipher_initialize(VALUE self, VALUE str)
|
|||
if (!(cipher = EVP_get_cipherbyname(name))) {
|
||||
ossl_raise(rb_eRuntimeError, "Unsupported cipher algorithm (%s).", name);
|
||||
}
|
||||
EVP_CIPHER_CTX_init(ctx);
|
||||
if (EVP_CipherInit(ctx, cipher, NULL, NULL, -1) != 1)
|
||||
ossl_raise(eCipherError, NULL);
|
||||
if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, -1) != 1)
|
||||
ossl_raise(eCipherError, NULL);
|
||||
|
||||
return self;
|
||||
}
|
||||
|
@ -113,8 +113,8 @@ ossl_cipher_copy(VALUE self, VALUE other)
|
|||
|
||||
GetCipher(self, ctx1);
|
||||
SafeGetCipher(other, ctx2);
|
||||
|
||||
memcpy(ctx1, ctx2, sizeof(EVP_CIPHER_CTX));
|
||||
if (EVP_CIPHER_CTX_copy(ctx1, ctx2) != 1)
|
||||
ossl_raise(eCipherError, NULL);
|
||||
|
||||
return self;
|
||||
}
|
||||
|
@ -125,7 +125,7 @@ ossl_cipher_reset(VALUE self)
|
|||
EVP_CIPHER_CTX *ctx;
|
||||
|
||||
GetCipher(self, ctx);
|
||||
if (EVP_CipherInit(ctx, NULL, NULL, NULL, -1) != 1)
|
||||
if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, NULL, -1) != 1)
|
||||
ossl_raise(eCipherError, NULL);
|
||||
|
||||
return self;
|
||||
|
@ -166,7 +166,7 @@ ossl_cipher_encrypt(int argc, VALUE *argv, VALUE self)
|
|||
}
|
||||
}
|
||||
|
||||
if (EVP_CipherInit(ctx, NULL, NULL, NULL, 1) != 1) {
|
||||
if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, NULL, 1) != 1) {
|
||||
ossl_raise(eCipherError, NULL);
|
||||
}
|
||||
|
||||
|
@ -175,7 +175,7 @@ ossl_cipher_encrypt(int argc, VALUE *argv, VALUE self)
|
|||
|
||||
EVP_BytesToKey(EVP_CIPHER_CTX_cipher(ctx), EVP_md5(), iv,
|
||||
RSTRING(pass)->ptr, RSTRING(pass)->len, 1, key, NULL);
|
||||
if (EVP_CipherInit(ctx, NULL, key, iv, -1) != 1) {
|
||||
if (EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, -1) != 1) {
|
||||
ossl_raise(eCipherError, NULL);
|
||||
}
|
||||
}
|
||||
|
@ -211,7 +211,7 @@ ossl_cipher_decrypt(int argc, VALUE *argv, VALUE self)
|
|||
}
|
||||
}
|
||||
|
||||
if (EVP_CipherInit(ctx, NULL, NULL, NULL, 0) != 1) {
|
||||
if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, NULL, 0) != 1) {
|
||||
ossl_raise(eCipherError, NULL);
|
||||
}
|
||||
|
||||
|
@ -220,7 +220,7 @@ ossl_cipher_decrypt(int argc, VALUE *argv, VALUE self)
|
|||
|
||||
EVP_BytesToKey(EVP_CIPHER_CTX_cipher(ctx), EVP_md5(), iv,
|
||||
RSTRING(pass)->ptr, RSTRING(pass)->len, 1, key, NULL);
|
||||
if (EVP_CipherInit(ctx, NULL, key, iv, -1) != 1) {
|
||||
if (EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, -1) != 1) {
|
||||
ossl_raise(eCipherError, NULL);
|
||||
}
|
||||
}
|
||||
|
@ -259,7 +259,7 @@ ossl_cipher_final(VALUE self)
|
|||
|
||||
GetCipher(self, ctx);
|
||||
str = rb_str_new(0, EVP_CIPHER_CTX_block_size(ctx));
|
||||
if (!EVP_CipherFinal(ctx, RSTRING(str)->ptr, &out_len))
|
||||
if (!EVP_CipherFinal_ex(ctx, RSTRING(str)->ptr, &out_len))
|
||||
ossl_raise(eCipherError, NULL);
|
||||
assert(out_len <= RSTRING(str)->len);
|
||||
RSTRING(str)->len = out_len;
|
||||
|
@ -289,7 +289,7 @@ ossl_cipher_set_key(VALUE self, VALUE key)
|
|||
if (RSTRING(key)->len < EVP_CIPHER_CTX_key_length(ctx))
|
||||
ossl_raise(eCipherError, "key length too short");
|
||||
|
||||
if (EVP_CipherInit(ctx, NULL, RSTRING(key)->ptr, NULL, -1) != 1)
|
||||
if (EVP_CipherInit_ex(ctx, NULL, NULL, RSTRING(key)->ptr, NULL, -1) != 1)
|
||||
ossl_raise(eCipherError, NULL);
|
||||
|
||||
return key;
|
||||
|
@ -306,8 +306,8 @@ ossl_cipher_set_iv(VALUE self, VALUE iv)
|
|||
if (RSTRING(iv)->len < EVP_CIPHER_CTX_iv_length(ctx))
|
||||
ossl_raise(eCipherError, "iv length too short");
|
||||
|
||||
if (EVP_CipherInit(ctx, NULL, NULL, RSTRING(iv)->ptr, -1) != 1)
|
||||
ossl_raise(eCipherError, NULL);
|
||||
if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, RSTRING(iv)->ptr, -1) != 1)
|
||||
ossl_raise(eCipherError, NULL);
|
||||
|
||||
return iv;
|
||||
}
|
||||
|
@ -315,13 +315,12 @@ ossl_cipher_set_iv(VALUE self, VALUE iv)
|
|||
static VALUE
|
||||
ossl_cipher_set_padding(VALUE self, VALUE padding)
|
||||
{
|
||||
#if defined(HAVE_ST_FLAGS)
|
||||
#if defined(HAVE_EVP_CIPHER_CTX_SET_PADDING)
|
||||
EVP_CIPHER_CTX *ctx;
|
||||
|
||||
GetCipher(self, ctx);
|
||||
|
||||
if (EVP_CIPHER_CTX_set_padding(ctx, NUM2INT(padding)) != 1)
|
||||
ossl_raise(eCipherError, NULL);
|
||||
ossl_raise(eCipherError, NULL);
|
||||
#else
|
||||
rb_notimplement();
|
||||
#endif
|
||||
|
@ -351,32 +350,18 @@ Init_ossl_cipher(void)
|
|||
cCipher = rb_define_class_under(mCipher, "Cipher", rb_cObject);
|
||||
|
||||
rb_define_alloc_func(cCipher, ossl_cipher_alloc);
|
||||
rb_define_method(cCipher, "initialize", ossl_cipher_initialize, 1);
|
||||
|
||||
rb_define_copy_func(cCipher, ossl_cipher_copy);
|
||||
|
||||
rb_define_method(cCipher, "initialize", ossl_cipher_initialize, 1);
|
||||
rb_define_method(cCipher, "reset", ossl_cipher_reset, 0);
|
||||
|
||||
rb_define_method(cCipher, "encrypt", ossl_cipher_encrypt, -1);
|
||||
rb_define_method(cCipher, "decrypt", ossl_cipher_decrypt, -1);
|
||||
rb_define_method(cCipher, "update", ossl_cipher_update, 1);
|
||||
rb_define_alias(cCipher, "<<", "update");
|
||||
rb_define_method(cCipher, "final", ossl_cipher_final, 0);
|
||||
|
||||
rb_define_method(cCipher, "name", ossl_cipher_name, 0);
|
||||
|
||||
rb_define_method(cCipher, "key=", ossl_cipher_set_key, 1);
|
||||
rb_define_method(cCipher, "key_len", ossl_cipher_key_length, 0);
|
||||
/*
|
||||
* TODO
|
||||
* int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *x, int keylen);
|
||||
*/
|
||||
rb_define_method(cCipher, "iv=", ossl_cipher_set_iv, 1);
|
||||
rb_define_method(cCipher, "iv_len", ossl_cipher_iv_length, 0);
|
||||
|
||||
rb_define_method(cCipher, "block_size", ossl_cipher_block_size, 0);
|
||||
|
||||
rb_define_method(cCipher, "padding=", ossl_cipher_set_padding, 1);
|
||||
|
||||
} /* Init_ossl_cipher */
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue