From be1baf4a9a62aade5a411eab3ebb704f3afd2cc8 Mon Sep 17 00:00:00 2001 From: rhe Date: Sun, 19 Jun 2016 09:29:59 +0000 Subject: 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 --- ext/openssl/ossl_pkey_dsa.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'ext/openssl/ossl_pkey_dsa.c') diff --git a/ext/openssl/ossl_pkey_dsa.c b/ext/openssl/ossl_pkey_dsa.c index 333beae844..1ddc0d48f1 100644 --- a/ext/openssl/ossl_pkey_dsa.c +++ b/ext/openssl/ossl_pkey_dsa.c @@ -269,6 +269,26 @@ ossl_dsa_initialize(int argc, VALUE *argv, VALUE self) return self; } +static VALUE +ossl_dsa_initialize_copy(VALUE self, VALUE other) +{ + EVP_PKEY *pkey; + DSA *dsa, *dsa_new; + + GetPKey(self, pkey); + if (EVP_PKEY_base_id(pkey) != EVP_PKEY_NONE) + ossl_raise(eDSAError, "DSA already initialized"); + GetDSA(other, dsa); + + dsa_new = ASN1_dup((i2d_of_void *)i2d_DSAPrivateKey, (d2i_of_void *)d2i_DSAPrivateKey, (char *)dsa); + if (!dsa_new) + ossl_raise(eDSAError, "ASN1_dup"); + + EVP_PKEY_assign_DSA(pkey, dsa_new); + + return self; +} + /* * call-seq: * dsa.public? -> true | false @@ -610,6 +630,7 @@ Init_ossl_dsa(void) rb_define_singleton_method(cDSA, "generate", ossl_dsa_s_generate, 1); rb_define_method(cDSA, "initialize", ossl_dsa_initialize, -1); + rb_define_copy_func(cDSA, ossl_dsa_initialize_copy); rb_define_method(cDSA, "public?", ossl_dsa_is_public, 0); rb_define_method(cDSA, "private?", ossl_dsa_is_private, 0); -- cgit v1.2.3