Skip to content

Commit 2ff41f4

Browse files
committed
pkey: unify error classes into PKeyError
Remove the following subclasses of OpenSSL::PKey::PKeyError and make them aliases of it. - OpenSSL::PKey::DHError - OpenSSL::PKey::DSAError - OpenSSL::PKey::ECError - OpenSSL::PKey::RSAError Historically, methods defined on OpenSSL::PKey and OpenSSL::PKey::PKey raise OpenSSL::PKey::PKeyError, while methods on the subclasses raise their respective exception classes. However, this distinction is not particularly useful since all those exception classes represent the same kind of errors from the underlying EVP_PKEY API. I think this convention comes from the fact that OpenSSL::PKey::{DH, DSA,RSA} originally wrapped the corresponding OpenSSL structs DH, DSA, and RSA, before they were unified to wrap EVP_PKEY, way back in 2002. OpenSSL::PKey::EC::Group::Error and OpenSSL::PKey::EC::Point::Error are out of scope of this change, as they are not subclasses of OpenSSL::PKey::PKeyError and do not represent errors from the EVP_PKEY API.
1 parent f7114e9 commit 2ff41f4

File tree

11 files changed

+117
-142
lines changed

11 files changed

+117
-142
lines changed

ext/openssl/ossl_pkey.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1717,7 +1717,16 @@ Init_ossl_pkey(void)
17171717

17181718
/* Document-class: OpenSSL::PKey::PKeyError
17191719
*
1720-
*Raised when errors occur during PKey#sign or PKey#verify.
1720+
* Raised when errors occur during PKey#sign or PKey#verify.
1721+
*
1722+
* Before version 4.0.0, OpenSSL::PKey::PKeyError had the following
1723+
* subclasses. These subclasses have been removed and the constants are
1724+
* now defined as aliases of OpenSSL::PKey::PKeyError.
1725+
*
1726+
* * OpenSSL::PKey::DHError
1727+
* * OpenSSL::PKey::DSAError
1728+
* * OpenSSL::PKey::ECError
1729+
* * OpenSSL::PKey::RSAError
17211730
*/
17221731
ePKeyError = rb_define_class_under(mPKey, "PKeyError", eOSSLError);
17231732

ext/openssl/ossl_pkey_dh.c

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
* Classes
2828
*/
2929
VALUE cDH;
30-
static VALUE eDHError;
3130

3231
/*
3332
* Private
@@ -86,7 +85,7 @@ ossl_dh_initialize(int argc, VALUE *argv, VALUE self)
8685
if (rb_scan_args(argc, argv, "01", &arg) == 0) {
8786
dh = DH_new();
8887
if (!dh)
89-
ossl_raise(eDHError, "DH_new");
88+
ossl_raise(ePKeyError, "DH_new");
9089
goto legacy;
9190
}
9291

@@ -105,12 +104,12 @@ ossl_dh_initialize(int argc, VALUE *argv, VALUE self)
105104
pkey = ossl_pkey_read_generic(in, Qnil);
106105
BIO_free(in);
107106
if (!pkey)
108-
ossl_raise(eDHError, "could not parse pkey");
107+
ossl_raise(ePKeyError, "could not parse pkey");
109108

110109
type = EVP_PKEY_base_id(pkey);
111110
if (type != EVP_PKEY_DH) {
112111
EVP_PKEY_free(pkey);
113-
rb_raise(eDHError, "incorrect pkey type: %s", OBJ_nid2sn(type));
112+
rb_raise(ePKeyError, "incorrect pkey type: %s", OBJ_nid2sn(type));
114113
}
115114
RTYPEDDATA_DATA(self) = pkey;
116115
return self;
@@ -121,7 +120,7 @@ ossl_dh_initialize(int argc, VALUE *argv, VALUE self)
121120
if (!pkey || EVP_PKEY_assign_DH(pkey, dh) != 1) {
122121
EVP_PKEY_free(pkey);
123122
DH_free(dh);
124-
ossl_raise(eDHError, "EVP_PKEY_assign_DH");
123+
ossl_raise(ePKeyError, "EVP_PKEY_assign_DH");
125124
}
126125
RTYPEDDATA_DATA(self) = pkey;
127126
return self;
@@ -143,7 +142,7 @@ ossl_dh_initialize_copy(VALUE self, VALUE other)
143142

144143
dh = DHparams_dup(dh_other);
145144
if (!dh)
146-
ossl_raise(eDHError, "DHparams_dup");
145+
ossl_raise(ePKeyError, "DHparams_dup");
147146

148147
DH_get0_key(dh_other, &pub, &priv);
149148
if (pub) {
@@ -153,7 +152,7 @@ ossl_dh_initialize_copy(VALUE self, VALUE other)
153152
if (!pub2 || (priv && !priv2)) {
154153
BN_clear_free(pub2);
155154
BN_clear_free(priv2);
156-
ossl_raise(eDHError, "BN_dup");
155+
ossl_raise(ePKeyError, "BN_dup");
157156
}
158157
DH_set0_key(dh, pub2, priv2);
159158
}
@@ -162,7 +161,7 @@ ossl_dh_initialize_copy(VALUE self, VALUE other)
162161
if (!pkey || EVP_PKEY_assign_DH(pkey, dh) != 1) {
163162
EVP_PKEY_free(pkey);
164163
DH_free(dh);
165-
ossl_raise(eDHError, "EVP_PKEY_assign_DH");
164+
ossl_raise(ePKeyError, "EVP_PKEY_assign_DH");
166165
}
167166
RTYPEDDATA_DATA(self) = pkey;
168167
return self;
@@ -241,11 +240,11 @@ ossl_dh_export(VALUE self)
241240

242241
GetDH(self, dh);
243242
if (!(out = BIO_new(BIO_s_mem()))) {
244-
ossl_raise(eDHError, NULL);
243+
ossl_raise(ePKeyError, NULL);
245244
}
246245
if (!PEM_write_bio_DHparams(out, dh)) {
247246
BIO_free(out);
248-
ossl_raise(eDHError, NULL);
247+
ossl_raise(ePKeyError, NULL);
249248
}
250249
str = ossl_membio2str(out);
251250

@@ -275,11 +274,11 @@ ossl_dh_to_der(VALUE self)
275274

276275
GetDH(self, dh);
277276
if((len = i2d_DHparams(dh, NULL)) <= 0)
278-
ossl_raise(eDHError, NULL);
277+
ossl_raise(ePKeyError, NULL);
279278
str = rb_str_new(0, len);
280279
p = (unsigned char *)RSTRING_PTR(str);
281280
if(i2d_DHparams(dh, &p) < 0)
282-
ossl_raise(eDHError, NULL);
281+
ossl_raise(ePKeyError, NULL);
283282
ossl_str_adjust(str, p);
284283

285284
return str;
@@ -306,7 +305,7 @@ ossl_dh_check_params(VALUE self)
306305
GetPKey(self, pkey);
307306
pctx = EVP_PKEY_CTX_new(pkey, /* engine */NULL);
308307
if (!pctx)
309-
ossl_raise(eDHError, "EVP_PKEY_CTX_new");
308+
ossl_raise(ePKeyError, "EVP_PKEY_CTX_new");
310309
ret = EVP_PKEY_param_check(pctx);
311310
EVP_PKEY_CTX_free(pctx);
312311
#else
@@ -355,13 +354,6 @@ Init_ossl_dh(void)
355354
ePKeyError = rb_define_class_under(mPKey, "PKeyError", eOSSLError);
356355
#endif
357356

358-
/* Document-class: OpenSSL::PKey::DHError
359-
*
360-
* Generic exception that is raised if an operation on a DH PKey
361-
* fails unexpectedly or in case an instantiation of an instance of DH
362-
* fails due to non-conformant input data.
363-
*/
364-
eDHError = rb_define_class_under(mPKey, "DHError", ePKeyError);
365357
/* Document-class: OpenSSL::PKey::DH
366358
*
367359
* An implementation of the Diffie-Hellman key exchange protocol based on

ext/openssl/ossl_pkey_dsa.c

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ DSA_PRIVATE(VALUE obj, OSSL_3_const DSA *dsa)
4141
* Classes
4242
*/
4343
VALUE cDSA;
44-
static VALUE eDSAError;
4544

4645
/*
4746
* Private
@@ -98,7 +97,7 @@ ossl_dsa_initialize(int argc, VALUE *argv, VALUE self)
9897
if (argc == 0) {
9998
dsa = DSA_new();
10099
if (!dsa)
101-
ossl_raise(eDSAError, "DSA_new");
100+
ossl_raise(ePKeyError, "DSA_new");
102101
goto legacy;
103102
}
104103

@@ -117,12 +116,12 @@ ossl_dsa_initialize(int argc, VALUE *argv, VALUE self)
117116
pkey = ossl_pkey_read_generic(in, pass);
118117
BIO_free(in);
119118
if (!pkey)
120-
ossl_raise(eDSAError, "Neither PUB key nor PRIV key");
119+
ossl_raise(ePKeyError, "Neither PUB key nor PRIV key");
121120

122121
type = EVP_PKEY_base_id(pkey);
123122
if (type != EVP_PKEY_DSA) {
124123
EVP_PKEY_free(pkey);
125-
rb_raise(eDSAError, "incorrect pkey type: %s", OBJ_nid2sn(type));
124+
rb_raise(ePKeyError, "incorrect pkey type: %s", OBJ_nid2sn(type));
126125
}
127126
RTYPEDDATA_DATA(self) = pkey;
128127
return self;
@@ -133,7 +132,7 @@ ossl_dsa_initialize(int argc, VALUE *argv, VALUE self)
133132
if (!pkey || EVP_PKEY_assign_DSA(pkey, dsa) != 1) {
134133
EVP_PKEY_free(pkey);
135134
DSA_free(dsa);
136-
ossl_raise(eDSAError, "EVP_PKEY_assign_DSA");
135+
ossl_raise(ePKeyError, "EVP_PKEY_assign_DSA");
137136
}
138137
RTYPEDDATA_DATA(self) = pkey;
139138
return self;
@@ -156,13 +155,13 @@ ossl_dsa_initialize_copy(VALUE self, VALUE other)
156155
(d2i_of_void *)d2i_DSAPrivateKey,
157156
(char *)dsa);
158157
if (!dsa_new)
159-
ossl_raise(eDSAError, "ASN1_dup");
158+
ossl_raise(ePKeyError, "ASN1_dup");
160159

161160
pkey = EVP_PKEY_new();
162161
if (!pkey || EVP_PKEY_assign_DSA(pkey, dsa_new) != 1) {
163162
EVP_PKEY_free(pkey);
164163
DSA_free(dsa_new);
165-
ossl_raise(eDSAError, "EVP_PKEY_assign_DSA");
164+
ossl_raise(ePKeyError, "EVP_PKEY_assign_DSA");
166165
}
167166
RTYPEDDATA_DATA(self) = pkey;
168167

@@ -333,14 +332,6 @@ Init_ossl_dsa(void)
333332
ePKeyError = rb_define_class_under(mPKey, "PKeyError", eOSSLError);
334333
#endif
335334

336-
/* Document-class: OpenSSL::PKey::DSAError
337-
*
338-
* Generic exception that is raised if an operation on a DSA PKey
339-
* fails unexpectedly or in case an instantiation of an instance of DSA
340-
* fails due to non-conformant input data.
341-
*/
342-
eDSAError = rb_define_class_under(mPKey, "DSAError", ePKeyError);
343-
344335
/* Document-class: OpenSSL::PKey::DSA
345336
*
346337
* DSA, the Digital Signature Algorithm, is specified in NIST's

0 commit comments

Comments
 (0)