summaryrefslogtreecommitdiff
path: root/ext/openssl/ossl_pkey_dh.c
diff options
context:
space:
mode:
authorrhe <rhe@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-06-19 09:29:59 +0000
committerrhe <rhe@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-06-19 09:29:59 +0000
commitbe1baf4a9a62aade5a411eab3ebb704f3afd2cc8 (patch)
tree8d216d4f7bcfcbe7b29871745c642914c65ae124 /ext/openssl/ossl_pkey_dh.c
parentb67ead14521fb74bcf8ec28f8c78245dfb536b70 (diff)
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_dh.c')
-rw-r--r--ext/openssl/ossl_pkey_dh.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/ext/openssl/ossl_pkey_dh.c b/ext/openssl/ossl_pkey_dh.c
index 654d479..139af15 100644
--- a/ext/openssl/ossl_pkey_dh.c
+++ b/ext/openssl/ossl_pkey_dh.c
@@ -238,6 +238,39 @@ ossl_dh_initialize(int argc, VALUE *argv, VALUE self)
return self;
}
+static VALUE
+ossl_dh_initialize_copy(VALUE self, VALUE other)
+{
+ EVP_PKEY *pkey;
+ DH *dh, *dh_other;
+ const BIGNUM *pub, *priv;
+
+ GetPKey(self, pkey);
+ if (EVP_PKEY_base_id(pkey) != EVP_PKEY_NONE)
+ ossl_raise(eDHError, "DH already initialized");
+ GetDH(other, dh_other);
+
+ dh = DHparams_dup(dh_other);
+ if (!dh)
+ ossl_raise(eDHError, "DHparams_dup");
+ EVP_PKEY_assign_DH(pkey, dh);
+
+ DH_get0_key(dh_other, &pub, &priv);
+ if (pub) {
+ BIGNUM *pub2 = BN_dup(pub);
+ BIGNUM *priv2 = BN_dup(priv);
+
+ if (!pub2 || priv && !priv2) {
+ BN_clear_free(pub2);
+ BN_clear_free(priv2);
+ ossl_raise(eDHError, "BN_dup");
+ }
+ DH_set0_key(dh, pub2, priv2);
+ }
+
+ return self;
+}
+
/*
* call-seq:
* dh.public? -> true | false
@@ -568,6 +601,7 @@ Init_ossl_dh(void)
cDH = rb_define_class_under(mPKey, "DH", cPKey);
rb_define_singleton_method(cDH, "generate", ossl_dh_s_generate, -1);
rb_define_method(cDH, "initialize", ossl_dh_initialize, -1);
+ rb_define_copy_func(cDH, ossl_dh_initialize_copy);
rb_define_method(cDH, "public?", ossl_dh_is_public, 0);
rb_define_method(cDH, "private?", ossl_dh_is_private, 0);
rb_define_method(cDH, "to_text", ossl_dh_to_text, 0);