Skip to content

Commit 94aeab2

Browse files
committed
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.
1 parent 41587f6 commit 94aeab2

File tree

6 files changed

+9
-100
lines changed

6 files changed

+9
-100
lines changed

ext/openssl/ossl_pkey.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,34 +95,30 @@ const rb_data_type_t ossl_evp_pkey_type = {
9595
static VALUE
9696
pkey_new0(EVP_PKEY *pkey)
9797
{
98-
VALUE obj;
98+
VALUE klass, obj;
9999
int type;
100100

101101
if (!pkey || (type = EVP_PKEY_base_id(pkey)) == EVP_PKEY_NONE)
102102
ossl_raise(rb_eRuntimeError, "pkey is empty");
103103

104104
switch (type) {
105105
#if !defined(OPENSSL_NO_RSA)
106-
case EVP_PKEY_RSA:
107-
return ossl_rsa_new(pkey);
106+
case EVP_PKEY_RSA: klass = cRSA; break;
108107
#endif
109108
#if !defined(OPENSSL_NO_DSA)
110-
case EVP_PKEY_DSA:
111-
return ossl_dsa_new(pkey);
109+
case EVP_PKEY_DSA: klass = cDSA; break;
112110
#endif
113111
#if !defined(OPENSSL_NO_DH)
114-
case EVP_PKEY_DH:
115-
return ossl_dh_new(pkey);
112+
case EVP_PKEY_DH: klass = cDH; break;
116113
#endif
117114
#if !defined(OPENSSL_NO_EC)
118-
case EVP_PKEY_EC:
119-
return ossl_ec_new(pkey);
115+
case EVP_PKEY_EC: klass = cEC; break;
120116
#endif
121-
default:
122-
obj = NewPKey(cPKey);
123-
SetPKey(obj, pkey);
124-
return obj;
117+
default: klass = cPKey; break;
125118
}
119+
obj = NewPKey(klass);
120+
SetPKey(obj, pkey);
121+
return obj;
126122
}
127123

128124
VALUE

ext/openssl/ossl_pkey.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ void Init_ossl_pkey(void);
5656
extern VALUE cRSA;
5757
extern VALUE eRSAError;
5858

59-
VALUE ossl_rsa_new(EVP_PKEY *);
6059
void Init_ossl_rsa(void);
6160

6261
/*
@@ -65,7 +64,6 @@ void Init_ossl_rsa(void);
6564
extern VALUE cDSA;
6665
extern VALUE eDSAError;
6766

68-
VALUE ossl_dsa_new(EVP_PKEY *);
6967
void Init_ossl_dsa(void);
7068

7169
/*
@@ -74,7 +72,6 @@ void Init_ossl_dsa(void);
7472
extern VALUE cDH;
7573
extern VALUE eDHError;
7674

77-
VALUE ossl_dh_new(EVP_PKEY *);
7875
void Init_ossl_dh(void);
7976

8077
/*

ext/openssl/ossl_pkey_dh.c

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,27 +54,6 @@ dh_instance(VALUE klass, DH *dh)
5454
return obj;
5555
}
5656

57-
VALUE
58-
ossl_dh_new(EVP_PKEY *pkey)
59-
{
60-
VALUE obj;
61-
62-
if (!pkey) {
63-
obj = dh_instance(cDH, DH_new());
64-
} else {
65-
obj = NewPKey(cDH);
66-
if (EVP_PKEY_base_id(pkey) != EVP_PKEY_DH) {
67-
ossl_raise(rb_eTypeError, "Not a DH key!");
68-
}
69-
SetPKey(obj, pkey);
70-
}
71-
if (obj == Qfalse) {
72-
ossl_raise(eDHError, NULL);
73-
}
74-
75-
return obj;
76-
}
77-
7857
/*
7958
* Private
8059
*/

ext/openssl/ossl_pkey_dsa.c

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,27 +68,6 @@ dsa_instance(VALUE klass, DSA *dsa)
6868
return obj;
6969
}
7070

71-
VALUE
72-
ossl_dsa_new(EVP_PKEY *pkey)
73-
{
74-
VALUE obj;
75-
76-
if (!pkey) {
77-
obj = dsa_instance(cDSA, DSA_new());
78-
} else {
79-
obj = NewPKey(cDSA);
80-
if (EVP_PKEY_base_id(pkey) != EVP_PKEY_DSA) {
81-
ossl_raise(rb_eTypeError, "Not a DSA key!");
82-
}
83-
SetPKey(obj, pkey);
84-
}
85-
if (obj == Qfalse) {
86-
ossl_raise(eDSAError, NULL);
87-
}
88-
89-
return obj;
90-
}
91-
9271
/*
9372
* Private
9473
*/

ext/openssl/ossl_pkey_ec.c

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -84,26 +84,6 @@ static VALUE ec_instance(VALUE klass, EC_KEY *ec)
8484
return obj;
8585
}
8686

87-
VALUE ossl_ec_new(EVP_PKEY *pkey)
88-
{
89-
VALUE obj;
90-
91-
if (!pkey) {
92-
obj = ec_instance(cEC, EC_KEY_new());
93-
} else {
94-
obj = NewPKey(cEC);
95-
if (EVP_PKEY_base_id(pkey) != EVP_PKEY_EC) {
96-
ossl_raise(rb_eTypeError, "Not a EC key!");
97-
}
98-
SetPKey(obj, pkey);
99-
}
100-
if (obj == Qfalse) {
101-
ossl_raise(eECError, NULL);
102-
}
103-
104-
return obj;
105-
}
106-
10787
/*
10888
* Creates a new EC_KEY on the EC group obj. arg can be an EC::Group or a String
10989
* representing an OID.

ext/openssl/ossl_pkey_rsa.c

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,28 +69,6 @@ rsa_instance(VALUE klass, RSA *rsa)
6969
return obj;
7070
}
7171

72-
VALUE
73-
ossl_rsa_new(EVP_PKEY *pkey)
74-
{
75-
VALUE obj;
76-
77-
if (!pkey) {
78-
obj = rsa_instance(cRSA, RSA_new());
79-
}
80-
else {
81-
obj = NewPKey(cRSA);
82-
if (EVP_PKEY_base_id(pkey) != EVP_PKEY_RSA) {
83-
ossl_raise(rb_eTypeError, "Not a RSA key!");
84-
}
85-
SetPKey(obj, pkey);
86-
}
87-
if (obj == Qfalse) {
88-
ossl_raise(eRSAError, NULL);
89-
}
90-
91-
return obj;
92-
}
93-
9472
/*
9573
* Private
9674
*/

0 commit comments

Comments
 (0)