Skip to content

Commit 2cba2db

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 fe5f2aa commit 2cba2db

File tree

11 files changed

+121
-146
lines changed

11 files changed

+121
-146
lines changed

ext/openssl/ossl_pkey.c

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

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

ext/openssl/ossl_pkey_dh.c

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@
2222
GetPKeyDH((obj), _pkey); \
2323
(dh) = EVP_PKEY_get0_DH(_pkey); \
2424
if ((dh) == NULL) \
25-
ossl_raise(eDHError, "failed to get DH from EVP_PKEY"); \
25+
ossl_raise(ePKeyError, "failed to get DH from EVP_PKEY"); \
2626
} while (0)
2727

2828
/*
2929
* Classes
3030
*/
3131
VALUE cDH;
32-
static VALUE eDHError;
3332

3433
/*
3534
* Private
@@ -94,7 +93,7 @@ ossl_dh_initialize(int argc, VALUE *argv, VALUE self)
9493
#else
9594
dh = DH_new();
9695
if (!dh)
97-
ossl_raise(eDHError, "DH_new");
96+
ossl_raise(ePKeyError, "DH_new");
9897
goto legacy;
9998
#endif
10099
}
@@ -114,12 +113,12 @@ ossl_dh_initialize(int argc, VALUE *argv, VALUE self)
114113
pkey = ossl_pkey_read_generic(in, Qnil);
115114
BIO_free(in);
116115
if (!pkey)
117-
ossl_raise(eDHError, "could not parse pkey");
116+
ossl_raise(ePKeyError, "could not parse pkey");
118117

119118
type = EVP_PKEY_base_id(pkey);
120119
if (type != EVP_PKEY_DH) {
121120
EVP_PKEY_free(pkey);
122-
rb_raise(eDHError, "incorrect pkey type: %s", OBJ_nid2sn(type));
121+
rb_raise(ePKeyError, "incorrect pkey type: %s", OBJ_nid2sn(type));
123122
}
124123
RTYPEDDATA_DATA(self) = pkey;
125124
return self;
@@ -130,7 +129,7 @@ ossl_dh_initialize(int argc, VALUE *argv, VALUE self)
130129
if (!pkey || EVP_PKEY_assign_DH(pkey, dh) != 1) {
131130
EVP_PKEY_free(pkey);
132131
DH_free(dh);
133-
ossl_raise(eDHError, "EVP_PKEY_assign_DH");
132+
ossl_raise(ePKeyError, "EVP_PKEY_assign_DH");
134133
}
135134
RTYPEDDATA_DATA(self) = pkey;
136135
return self;
@@ -152,7 +151,7 @@ ossl_dh_initialize_copy(VALUE self, VALUE other)
152151

153152
dh = DHparams_dup(dh_other);
154153
if (!dh)
155-
ossl_raise(eDHError, "DHparams_dup");
154+
ossl_raise(ePKeyError, "DHparams_dup");
156155

157156
DH_get0_key(dh_other, &pub, &priv);
158157
if (pub) {
@@ -162,7 +161,7 @@ ossl_dh_initialize_copy(VALUE self, VALUE other)
162161
if (!pub2 || (priv && !priv2)) {
163162
BN_clear_free(pub2);
164163
BN_clear_free(priv2);
165-
ossl_raise(eDHError, "BN_dup");
164+
ossl_raise(ePKeyError, "BN_dup");
166165
}
167166
DH_set0_key(dh, pub2, priv2);
168167
}
@@ -171,7 +170,7 @@ ossl_dh_initialize_copy(VALUE self, VALUE other)
171170
if (!pkey || EVP_PKEY_assign_DH(pkey, dh) != 1) {
172171
EVP_PKEY_free(pkey);
173172
DH_free(dh);
174-
ossl_raise(eDHError, "EVP_PKEY_assign_DH");
173+
ossl_raise(ePKeyError, "EVP_PKEY_assign_DH");
175174
}
176175
RTYPEDDATA_DATA(self) = pkey;
177176
return self;
@@ -250,11 +249,11 @@ ossl_dh_export(VALUE self)
250249

251250
GetDH(self, dh);
252251
if (!(out = BIO_new(BIO_s_mem()))) {
253-
ossl_raise(eDHError, NULL);
252+
ossl_raise(ePKeyError, NULL);
254253
}
255254
if (!PEM_write_bio_DHparams(out, dh)) {
256255
BIO_free(out);
257-
ossl_raise(eDHError, NULL);
256+
ossl_raise(ePKeyError, NULL);
258257
}
259258
str = ossl_membio2str(out);
260259

@@ -284,11 +283,11 @@ ossl_dh_to_der(VALUE self)
284283

285284
GetDH(self, dh);
286285
if((len = i2d_DHparams(dh, NULL)) <= 0)
287-
ossl_raise(eDHError, NULL);
286+
ossl_raise(ePKeyError, NULL);
288287
str = rb_str_new(0, len);
289288
p = (unsigned char *)RSTRING_PTR(str);
290289
if(i2d_DHparams(dh, &p) < 0)
291-
ossl_raise(eDHError, NULL);
290+
ossl_raise(ePKeyError, NULL);
292291
ossl_str_adjust(str, p);
293292

294293
return str;
@@ -315,7 +314,7 @@ ossl_dh_check_params(VALUE self)
315314
GetPKey(self, pkey);
316315
pctx = EVP_PKEY_CTX_new(pkey, /* engine */NULL);
317316
if (!pctx)
318-
ossl_raise(eDHError, "EVP_PKEY_CTX_new");
317+
ossl_raise(ePKeyError, "EVP_PKEY_CTX_new");
319318
ret = EVP_PKEY_param_check(pctx);
320319
EVP_PKEY_CTX_free(pctx);
321320
#else
@@ -364,13 +363,6 @@ Init_ossl_dh(void)
364363
ePKeyError = rb_define_class_under(mPKey, "PKeyError", eOSSLError);
365364
#endif
366365

367-
/* Document-class: OpenSSL::PKey::DHError
368-
*
369-
* Generic exception that is raised if an operation on a DH PKey
370-
* fails unexpectedly or in case an instantiation of an instance of DH
371-
* fails due to non-conformant input data.
372-
*/
373-
eDHError = rb_define_class_under(mPKey, "DHError", ePKeyError);
374366
/* Document-class: OpenSSL::PKey::DH
375367
*
376368
* An implementation of the Diffie-Hellman key exchange protocol based on

ext/openssl/ossl_pkey_dsa.c

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
GetPKeyDSA((obj), _pkey); \
2323
(dsa) = EVP_PKEY_get0_DSA(_pkey); \
2424
if ((dsa) == NULL) \
25-
ossl_raise(eDSAError, "failed to get DSA from EVP_PKEY"); \
25+
ossl_raise(ePKeyError, "failed to get DSA from EVP_PKEY"); \
2626
} while (0)
2727

2828
static inline int
@@ -43,7 +43,6 @@ DSA_PRIVATE(VALUE obj, OSSL_3_const DSA *dsa)
4343
* Classes
4444
*/
4545
VALUE cDSA;
46-
static VALUE eDSAError;
4746

4847
/*
4948
* Private
@@ -105,7 +104,7 @@ ossl_dsa_initialize(int argc, VALUE *argv, VALUE self)
105104
#else
106105
dsa = DSA_new();
107106
if (!dsa)
108-
ossl_raise(eDSAError, "DSA_new");
107+
ossl_raise(ePKeyError, "DSA_new");
109108
goto legacy;
110109
#endif
111110
}
@@ -125,12 +124,12 @@ ossl_dsa_initialize(int argc, VALUE *argv, VALUE self)
125124
pkey = ossl_pkey_read_generic(in, pass);
126125
BIO_free(in);
127126
if (!pkey)
128-
ossl_raise(eDSAError, "Neither PUB key nor PRIV key");
127+
ossl_raise(ePKeyError, "Neither PUB key nor PRIV key");
129128

130129
type = EVP_PKEY_base_id(pkey);
131130
if (type != EVP_PKEY_DSA) {
132131
EVP_PKEY_free(pkey);
133-
rb_raise(eDSAError, "incorrect pkey type: %s", OBJ_nid2sn(type));
132+
rb_raise(ePKeyError, "incorrect pkey type: %s", OBJ_nid2sn(type));
134133
}
135134
RTYPEDDATA_DATA(self) = pkey;
136135
return self;
@@ -141,7 +140,7 @@ ossl_dsa_initialize(int argc, VALUE *argv, VALUE self)
141140
if (!pkey || EVP_PKEY_assign_DSA(pkey, dsa) != 1) {
142141
EVP_PKEY_free(pkey);
143142
DSA_free(dsa);
144-
ossl_raise(eDSAError, "EVP_PKEY_assign_DSA");
143+
ossl_raise(ePKeyError, "EVP_PKEY_assign_DSA");
145144
}
146145
RTYPEDDATA_DATA(self) = pkey;
147146
return self;
@@ -164,13 +163,13 @@ ossl_dsa_initialize_copy(VALUE self, VALUE other)
164163
(d2i_of_void *)d2i_DSAPrivateKey,
165164
(char *)dsa);
166165
if (!dsa_new)
167-
ossl_raise(eDSAError, "ASN1_dup");
166+
ossl_raise(ePKeyError, "ASN1_dup");
168167

169168
pkey = EVP_PKEY_new();
170169
if (!pkey || EVP_PKEY_assign_DSA(pkey, dsa_new) != 1) {
171170
EVP_PKEY_free(pkey);
172171
DSA_free(dsa_new);
173-
ossl_raise(eDSAError, "EVP_PKEY_assign_DSA");
172+
ossl_raise(ePKeyError, "EVP_PKEY_assign_DSA");
174173
}
175174
RTYPEDDATA_DATA(self) = pkey;
176175

@@ -341,14 +340,6 @@ Init_ossl_dsa(void)
341340
ePKeyError = rb_define_class_under(mPKey, "PKeyError", eOSSLError);
342341
#endif
343342

344-
/* Document-class: OpenSSL::PKey::DSAError
345-
*
346-
* Generic exception that is raised if an operation on a DSA PKey
347-
* fails unexpectedly or in case an instantiation of an instance of DSA
348-
* fails due to non-conformant input data.
349-
*/
350-
eDSAError = rb_define_class_under(mPKey, "DSAError", ePKeyError);
351-
352343
/* Document-class: OpenSSL::PKey::DSA
353344
*
354345
* DSA, the Digital Signature Algorithm, is specified in NIST's

0 commit comments

Comments
 (0)