summaryrefslogtreecommitdiffstats
path: root/ext/openssl/ossl_pkey_rsa.c
diff options
context:
space:
mode:
authorrhe <rhe@ruby-lang.org>2016-06-19 09:29:59 +0000
committerrhe <rhe@ruby-lang.org>2016-06-19 09:29:59 +0000
commitdfeac124ed52b9727328df692c340d12e2eb2f2c (patch)
tree695f7221fbe33af7dce01e074c3f9cc51a542da3 /ext/openssl/ossl_pkey_rsa.c
parent6d34ff6c83ec2c8a264fdd6daf74bbab28311ef6 (diff)
downloadruby-openssl-history-dfeac124ed52b9727328df692c340d12e2eb2f2c.tar.gz
openssl: implement initialize_copy method for PKey classes
* ext/openssl/ossl_pkey_dh.c, ext/openssl/ossl_pkey_dsa.c, ext/openssl/ossl_pkey_ec.c, ext/openssl/ossl_pkey_rsa.c: Implement initialize_copy method for OpenSSL::PKey::*. [ruby-core:75504] [Bug #12381] * test/openssl/test_pkey_dh.rb, test/openssl/test_pkey_dsa.rb, test/openssl/test_pkey_ec.rb, test/openssl/test_pkey_rsa.rb: Test they actually copy the OpenSSL objects, and modifications to cloned object don't affect the original object. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55454 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/openssl/ossl_pkey_rsa.c')
-rw-r--r--ext/openssl/ossl_pkey_rsa.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/ext/openssl/ossl_pkey_rsa.c b/ext/openssl/ossl_pkey_rsa.c
index 60fa146..2326a70 100644
--- a/ext/openssl/ossl_pkey_rsa.c
+++ b/ext/openssl/ossl_pkey_rsa.c
@@ -271,6 +271,26 @@ ossl_rsa_initialize(int argc, VALUE *argv, VALUE self)
return self;
}
+static VALUE
+ossl_rsa_initialize_copy(VALUE self, VALUE other)
+{
+ EVP_PKEY *pkey;
+ RSA *rsa, *rsa_new;
+
+ GetPKey(self, pkey);
+ if (EVP_PKEY_base_id(pkey) != EVP_PKEY_NONE)
+ ossl_raise(eRSAError, "RSA already initialized");
+ GetRSA(other, rsa);
+
+ rsa_new = ASN1_dup((i2d_of_void *)i2d_RSAPrivateKey, (d2i_of_void *)d2i_RSAPrivateKey, (char *)rsa);
+ if (!rsa_new)
+ ossl_raise(eRSAError, "ASN1_dup");
+
+ EVP_PKEY_assign_RSA(pkey, rsa_new);
+
+ return self;
+}
+
/*
* call-seq:
* rsa.public? => true
@@ -675,6 +695,7 @@ Init_ossl_rsa(void)
rb_define_singleton_method(cRSA, "generate", ossl_rsa_s_generate, -1);
rb_define_method(cRSA, "initialize", ossl_rsa_initialize, -1);
+ rb_define_copy_func(cRSA, ossl_rsa_initialize_copy);
rb_define_method(cRSA, "public?", ossl_rsa_is_public, 0);
rb_define_method(cRSA, "private?", ossl_rsa_is_private, 0);