ruby-changes:65551
From: Kazuki <ko1@a...>
Date: Tue, 16 Mar 2021 20:38:44 +0900 (JST)
Subject: [ruby-changes:65551] 27859c09a6 (master): [ruby/openssl] pkey: simplify ossl_pkey_new()
https://git.ruby-lang.org/ruby.git/commit/?id=27859c09a6 From 27859c09a67768b06ad1db11a145cd6c548bbdf3 Mon Sep 17 00:00:00 2001 From: Kazuki Yamaguchi <k@r...> Date: Thu, 16 Mar 2017 16:06:53 +0900 Subject: [ruby/openssl] pkey: simplify ossl_pkey_new() ossl_{rsa,dsa,dh,ec}_new() called from this function are not used anywhere else. Inline them into pkey_new0() and reduce code duplication. https://github.com/ruby/openssl/commit/94aeab2f26 --- ext/openssl/ossl_pkey.c | 22 +++++++++------------- ext/openssl/ossl_pkey.h | 3 --- ext/openssl/ossl_pkey_dh.c | 21 --------------------- ext/openssl/ossl_pkey_dsa.c | 21 --------------------- ext/openssl/ossl_pkey_ec.c | 20 -------------------- ext/openssl/ossl_pkey_rsa.c | 22 ---------------------- 6 files changed, 9 insertions(+), 100 deletions(-) diff --git a/ext/openssl/ossl_pkey.c b/ext/openssl/ossl_pkey.c index 2320408..c6dbf57 100644 --- a/ext/openssl/ossl_pkey.c +++ b/ext/openssl/ossl_pkey.c @@ -95,7 +95,7 @@ const rb_data_type_t ossl_evp_pkey_type = { https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_pkey.c#L95 static VALUE pkey_new0(EVP_PKEY *pkey) { - VALUE obj; + VALUE klass, obj; int type; if (!pkey || (type = EVP_PKEY_base_id(pkey)) == EVP_PKEY_NONE) @@ -103,26 +103,22 @@ pkey_new0(EVP_PKEY *pkey) https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_pkey.c#L103 switch (type) { #if !defined(OPENSSL_NO_RSA) - case EVP_PKEY_RSA: - return ossl_rsa_new(pkey); + case EVP_PKEY_RSA: klass = cRSA; break; #endif #if !defined(OPENSSL_NO_DSA) - case EVP_PKEY_DSA: - return ossl_dsa_new(pkey); + case EVP_PKEY_DSA: klass = cDSA; break; #endif #if !defined(OPENSSL_NO_DH) - case EVP_PKEY_DH: - return ossl_dh_new(pkey); + case EVP_PKEY_DH: klass = cDH; break; #endif #if !defined(OPENSSL_NO_EC) - case EVP_PKEY_EC: - return ossl_ec_new(pkey); + case EVP_PKEY_EC: klass = cEC; break; #endif - default: - obj = NewPKey(cPKey); - SetPKey(obj, pkey); - return obj; + default: klass = cPKey; break; } + obj = NewPKey(klass); + SetPKey(obj, pkey); + return obj; } VALUE diff --git a/ext/openssl/ossl_pkey.h b/ext/openssl/ossl_pkey.h index 0db5930..e363a26 100644 --- a/ext/openssl/ossl_pkey.h +++ b/ext/openssl/ossl_pkey.h @@ -56,7 +56,6 @@ void Init_ossl_pkey(void); https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_pkey.h#L56 extern VALUE cRSA; extern VALUE eRSAError; -VALUE ossl_rsa_new(EVP_PKEY *); void Init_ossl_rsa(void); /* @@ -65,7 +64,6 @@ void Init_ossl_rsa(void); https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_pkey.h#L64 extern VALUE cDSA; extern VALUE eDSAError; -VALUE ossl_dsa_new(EVP_PKEY *); void Init_ossl_dsa(void); /* @@ -74,7 +72,6 @@ void Init_ossl_dsa(void); https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_pkey.h#L72 extern VALUE cDH; extern VALUE eDHError; -VALUE ossl_dh_new(EVP_PKEY *); void Init_ossl_dh(void); /* diff --git a/ext/openssl/ossl_pkey_dh.c b/ext/openssl/ossl_pkey_dh.c index bf4e3f9..dff69cf 100644 --- a/ext/openssl/ossl_pkey_dh.c +++ b/ext/openssl/ossl_pkey_dh.c @@ -54,27 +54,6 @@ dh_instance(VALUE klass, DH *dh) https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_pkey_dh.c#L54 return obj; } -VALUE -ossl_dh_new(EVP_PKEY *pkey) -{ - VALUE obj; - - if (!pkey) { - obj = dh_instance(cDH, DH_new()); - } else { - obj = NewPKey(cDH); - if (EVP_PKEY_base_id(pkey) != EVP_PKEY_DH) { - ossl_raise(rb_eTypeError, "Not a DH key!"); - } - SetPKey(obj, pkey); - } - if (obj == Qfalse) { - ossl_raise(eDHError, NULL); - } - - return obj; -} - /* * Private */ diff --git a/ext/openssl/ossl_pkey_dsa.c b/ext/openssl/ossl_pkey_dsa.c index 431c20e..e9be9ac 100644 --- a/ext/openssl/ossl_pkey_dsa.c +++ b/ext/openssl/ossl_pkey_dsa.c @@ -68,27 +68,6 @@ dsa_instance(VALUE klass, DSA *dsa) https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_pkey_dsa.c#L68 return obj; } -VALUE -ossl_dsa_new(EVP_PKEY *pkey) -{ - VALUE obj; - - if (!pkey) { - obj = dsa_instance(cDSA, DSA_new()); - } else { - obj = NewPKey(cDSA); - if (EVP_PKEY_base_id(pkey) != EVP_PKEY_DSA) { - ossl_raise(rb_eTypeError, "Not a DSA key!"); - } - SetPKey(obj, pkey); - } - if (obj == Qfalse) { - ossl_raise(eDSAError, NULL); - } - - return obj; -} - /* * Private */ diff --git a/ext/openssl/ossl_pkey_ec.c b/ext/openssl/ossl_pkey_ec.c index fc2bc6c..eabf495 100644 --- a/ext/openssl/ossl_pkey_ec.c +++ b/ext/openssl/ossl_pkey_ec.c @@ -84,26 +84,6 @@ static VALUE ec_instance(VALUE klass, EC_KEY *ec) https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_pkey_ec.c#L84 return obj; } -VALUE ossl_ec_new(EVP_PKEY *pkey) -{ - VALUE obj; - - if (!pkey) { - obj = ec_instance(cEC, EC_KEY_new()); - } else { - obj = NewPKey(cEC); - if (EVP_PKEY_base_id(pkey) != EVP_PKEY_EC) { - ossl_raise(rb_eTypeError, "Not a EC key!"); - } - SetPKey(obj, pkey); - } - if (obj == Qfalse) { - ossl_raise(eECError, NULL); - } - - return obj; -} - /* * Creates a new EC_KEY on the EC group obj. arg can be an EC::Group or a String * representing an OID. diff --git a/ext/openssl/ossl_pkey_rsa.c b/ext/openssl/ossl_pkey_rsa.c index 761866c..c1ae44f 100644 --- a/ext/openssl/ossl_pkey_rsa.c +++ b/ext/openssl/ossl_pkey_rsa.c @@ -69,28 +69,6 @@ rsa_instance(VALUE klass, RSA *rsa) https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_pkey_rsa.c#L69 return obj; } -VALUE -ossl_rsa_new(EVP_PKEY *pkey) -{ - VALUE obj; - - if (!pkey) { - obj = rsa_instance(cRSA, RSA_new()); - } - else { - obj = NewPKey(cRSA); - if (EVP_PKEY_base_id(pkey) != EVP_PKEY_RSA) { - ossl_raise(rb_eTypeError, "Not a RSA key!"); - } - SetPKey(obj, pkey); - } - if (obj == Qfalse) { - ossl_raise(eRSAError, NULL); - } - - return obj; -} - /* * Private */ -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/