Skip to content

Commit d4d0d22

Browse files
rheniumjacob-shops
authored andcommitted
[ruby/openssl] 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. ruby/openssl@e74ff3e272
1 parent 6703dda commit d4d0d22

File tree

11 files changed

+121
-146
lines changed

11 files changed

+121
-146
lines changed

ext/openssl/lib/openssl/pkey.rb

Lines changed: 35 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
require_relative 'marshal'
88

99
module OpenSSL::PKey
10+
# Alias of PKeyError. Before version 4.0.0, this was a subclass of PKeyError.
11+
DHError = PKeyError
12+
1013
class DH
1114
include OpenSSL::Marshal
1215

@@ -102,7 +105,7 @@ def compute_key(pub_bn)
102105
# puts dh0.pub_key == dh.pub_key #=> false
103106
def generate_key!
104107
if OpenSSL::OPENSSL_VERSION_NUMBER >= 0x30000000
105-
raise DHError, "OpenSSL::PKey::DH is immutable on OpenSSL 3.0; " \
108+
raise PKeyError, "OpenSSL::PKey::DH is immutable on OpenSSL 3.0; " \
106109
"use OpenSSL::PKey.generate_key instead"
107110
end
108111

@@ -147,6 +150,9 @@ def new(*args, &blk) # :nodoc:
147150
end
148151
end
149152

153+
# Alias of PKeyError. Before version 4.0.0, this was a subclass of PKeyError.
154+
DSAError = PKeyError
155+
150156
class DSA
151157
include OpenSSL::Marshal
152158

@@ -242,13 +248,9 @@ def new(*args, &blk) # :nodoc:
242248
# sig = dsa.sign_raw(nil, digest)
243249
# p dsa.verify_raw(nil, sig, digest) #=> true
244250
def syssign(string)
245-
q or raise OpenSSL::PKey::DSAError, "incomplete DSA"
246-
private? or raise OpenSSL::PKey::DSAError, "Private DSA key needed!"
247-
begin
248-
sign_raw(nil, string)
249-
rescue OpenSSL::PKey::PKeyError
250-
raise OpenSSL::PKey::DSAError, $!.message
251-
end
251+
q or raise PKeyError, "incomplete DSA"
252+
private? or raise PKeyError, "Private DSA key needed!"
253+
sign_raw(nil, string)
252254
end
253255

254256
# :call-seq:
@@ -266,12 +268,13 @@ def syssign(string)
266268
# A \DSA signature value.
267269
def sysverify(digest, sig)
268270
verify_raw(nil, sig, digest)
269-
rescue OpenSSL::PKey::PKeyError
270-
raise OpenSSL::PKey::DSAError, $!.message
271271
end
272272
end
273273

274274
if defined?(EC)
275+
# Alias of PKeyError. Before version 4.0.0, this was a subclass of PKeyError.
276+
ECError = PKeyError
277+
275278
class EC
276279
include OpenSSL::Marshal
277280

@@ -282,8 +285,6 @@ class EC
282285
# Consider using PKey::PKey#sign_raw and PKey::PKey#verify_raw instead.
283286
def dsa_sign_asn1(data)
284287
sign_raw(nil, data)
285-
rescue OpenSSL::PKey::PKeyError
286-
raise OpenSSL::PKey::ECError, $!.message
287288
end
288289

289290
# :call-seq:
@@ -293,8 +294,6 @@ def dsa_sign_asn1(data)
293294
# Consider using PKey::PKey#sign_raw and PKey::PKey#verify_raw instead.
294295
def dsa_verify_asn1(data, sig)
295296
verify_raw(nil, sig, data)
296-
rescue OpenSSL::PKey::PKeyError
297-
raise OpenSSL::PKey::ECError, $!.message
298297
end
299298

300299
# :call-seq:
@@ -334,6 +333,9 @@ def to_bn(conversion_form = group.point_conversion_form)
334333
end
335334
end
336335

336+
# Alias of PKeyError. Before version 4.0.0, this was a subclass of PKeyError.
337+
RSAError = PKeyError
338+
337339
class RSA
338340
include OpenSSL::Marshal
339341

@@ -407,15 +409,11 @@ def new(*args, &blk) # :nodoc:
407409
# Consider using PKey::PKey#sign_raw and PKey::PKey#verify_raw, and
408410
# PKey::PKey#verify_recover instead.
409411
def private_encrypt(string, padding = PKCS1_PADDING)
410-
n or raise OpenSSL::PKey::RSAError, "incomplete RSA"
411-
private? or raise OpenSSL::PKey::RSAError, "private key needed."
412-
begin
413-
sign_raw(nil, string, {
414-
"rsa_padding_mode" => translate_padding_mode(padding),
415-
})
416-
rescue OpenSSL::PKey::PKeyError
417-
raise OpenSSL::PKey::RSAError, $!.message
418-
end
412+
n or raise PKeyError, "incomplete RSA"
413+
private? or raise PKeyError, "private key needed."
414+
sign_raw(nil, string, {
415+
"rsa_padding_mode" => translate_padding_mode(padding),
416+
})
419417
end
420418

421419
# :call-seq:
@@ -430,14 +428,10 @@ def private_encrypt(string, padding = PKCS1_PADDING)
430428
# Consider using PKey::PKey#sign_raw and PKey::PKey#verify_raw, and
431429
# PKey::PKey#verify_recover instead.
432430
def public_decrypt(string, padding = PKCS1_PADDING)
433-
n or raise OpenSSL::PKey::RSAError, "incomplete RSA"
434-
begin
435-
verify_recover(nil, string, {
436-
"rsa_padding_mode" => translate_padding_mode(padding),
437-
})
438-
rescue OpenSSL::PKey::PKeyError
439-
raise OpenSSL::PKey::RSAError, $!.message
440-
end
431+
n or raise PKeyError, "incomplete RSA"
432+
verify_recover(nil, string, {
433+
"rsa_padding_mode" => translate_padding_mode(padding),
434+
})
441435
end
442436

443437
# :call-seq:
@@ -452,14 +446,10 @@ def public_decrypt(string, padding = PKCS1_PADDING)
452446
# <b>Deprecated in version 3.0</b>.
453447
# Consider using PKey::PKey#encrypt and PKey::PKey#decrypt instead.
454448
def public_encrypt(data, padding = PKCS1_PADDING)
455-
n or raise OpenSSL::PKey::RSAError, "incomplete RSA"
456-
begin
457-
encrypt(data, {
458-
"rsa_padding_mode" => translate_padding_mode(padding),
459-
})
460-
rescue OpenSSL::PKey::PKeyError
461-
raise OpenSSL::PKey::RSAError, $!.message
462-
end
449+
n or raise PKeyError, "incomplete RSA"
450+
encrypt(data, {
451+
"rsa_padding_mode" => translate_padding_mode(padding),
452+
})
463453
end
464454

465455
# :call-seq:
@@ -473,15 +463,11 @@ def public_encrypt(data, padding = PKCS1_PADDING)
473463
# <b>Deprecated in version 3.0</b>.
474464
# Consider using PKey::PKey#encrypt and PKey::PKey#decrypt instead.
475465
def private_decrypt(data, padding = PKCS1_PADDING)
476-
n or raise OpenSSL::PKey::RSAError, "incomplete RSA"
477-
private? or raise OpenSSL::PKey::RSAError, "private key needed."
478-
begin
479-
decrypt(data, {
480-
"rsa_padding_mode" => translate_padding_mode(padding),
481-
})
482-
rescue OpenSSL::PKey::PKeyError
483-
raise OpenSSL::PKey::RSAError, $!.message
484-
end
466+
n or raise PKeyError, "incomplete RSA"
467+
private? or raise PKeyError, "private key needed."
468+
decrypt(data, {
469+
"rsa_padding_mode" => translate_padding_mode(padding),
470+
})
485471
end
486472

487473
PKCS1_PADDING = 1
@@ -500,7 +486,7 @@ def private_decrypt(data, padding = PKCS1_PADDING)
500486
when PKCS1_OAEP_PADDING
501487
"oaep"
502488
else
503-
raise OpenSSL::PKey::PKeyError, "unsupported padding mode"
489+
raise PKeyError, "unsupported padding mode"
504490
end
505491
end
506492
end

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

0 commit comments

Comments
 (0)