diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2016-08-22 17:27:11 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2016-08-22 17:27:11 +0900 |
commit | 5c1045ea40aa0ca76d8288fa0e91fdaa412bbb83 (patch) | |
tree | a22359f0241cdaba325da7ba8f10b277b86e6e9c /ext | |
parent | 6f37a000ecc4730a36899fc29a3477a8c9e3cf38 (diff) | |
download | ruby-openssl-5c1045ea40aa0ca76d8288fa0e91fdaa412bbb83.tar.gz |
cipher: allow cipher name in GetCipherPtr()
The function GetCipherPtr() is used when we want a const EVP_CIPHER that
represents a cipher algorithm. This change allows users to write a code
that exports a PKey encrypted without creating an OpenSSL::Cipher
instance:
pkey = OpenSSL::PKey.read(...)
pkey.export("aes-128-cbc") { password }
This is the same as what happened to GetDigestPtr() in r12128.
Diffstat (limited to 'ext')
-rw-r--r-- | ext/openssl/ossl_cipher.c | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/ext/openssl/ossl_cipher.c b/ext/openssl/ossl_cipher.c index 66d65e3f..c23105ab 100644 --- a/ext/openssl/ossl_cipher.c +++ b/ext/openssl/ossl_cipher.c @@ -54,11 +54,24 @@ static const rb_data_type_t ossl_cipher_type = { const EVP_CIPHER * GetCipherPtr(VALUE obj) { - EVP_CIPHER_CTX *ctx; + if (rb_obj_is_kind_of(obj, cCipher)) { + EVP_CIPHER_CTX *ctx; + + GetCipher(obj, ctx); + + return EVP_CIPHER_CTX_cipher(ctx); + } + else { + const EVP_CIPHER *cipher; - SafeGetCipher(obj, ctx); + StringValueCStr(obj); + cipher = EVP_get_cipherbyname(RSTRING_PTR(obj)); + if (!cipher) + ossl_raise(rb_eArgError, + "unsupported cipher algorithm: %"PRIsVALUE, obj); - return EVP_CIPHER_CTX_cipher(ctx); + return cipher; + } } VALUE |