Skip to content

pkey: unify error classes into PKeyError #929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion ext/openssl/ossl_pkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -1717,7 +1717,16 @@ Init_ossl_pkey(void)

/* Document-class: OpenSSL::PKey::PKeyError
*
*Raised when errors occur during PKey#sign or PKey#verify.
* Raised when errors occur during PKey#sign or PKey#verify.
*
* Before version 4.0.0, OpenSSL::PKey::PKeyError had the following
* subclasses. These subclasses have been removed and the constants are
* now defined as aliases of OpenSSL::PKey::PKeyError.
*
* * OpenSSL::PKey::DHError
* * OpenSSL::PKey::DSAError
* * OpenSSL::PKey::ECError
* * OpenSSL::PKey::RSAError
*/
ePKeyError = rb_define_class_under(mPKey, "PKeyError", eOSSLError);

Expand Down
32 changes: 12 additions & 20 deletions ext/openssl/ossl_pkey_dh.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
* Classes
*/
VALUE cDH;
static VALUE eDHError;

/*
* Private
Expand Down Expand Up @@ -86,7 +85,7 @@ ossl_dh_initialize(int argc, VALUE *argv, VALUE self)
if (rb_scan_args(argc, argv, "01", &arg) == 0) {
dh = DH_new();
if (!dh)
ossl_raise(eDHError, "DH_new");
ossl_raise(ePKeyError, "DH_new");
goto legacy;
}

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

type = EVP_PKEY_base_id(pkey);
if (type != EVP_PKEY_DH) {
EVP_PKEY_free(pkey);
rb_raise(eDHError, "incorrect pkey type: %s", OBJ_nid2sn(type));
rb_raise(ePKeyError, "incorrect pkey type: %s", OBJ_nid2sn(type));
}
RTYPEDDATA_DATA(self) = pkey;
return self;
Expand All @@ -121,7 +120,7 @@ ossl_dh_initialize(int argc, VALUE *argv, VALUE self)
if (!pkey || EVP_PKEY_assign_DH(pkey, dh) != 1) {
EVP_PKEY_free(pkey);
DH_free(dh);
ossl_raise(eDHError, "EVP_PKEY_assign_DH");
ossl_raise(ePKeyError, "EVP_PKEY_assign_DH");
}
RTYPEDDATA_DATA(self) = pkey;
return self;
Expand All @@ -143,7 +142,7 @@ ossl_dh_initialize_copy(VALUE self, VALUE other)

dh = DHparams_dup(dh_other);
if (!dh)
ossl_raise(eDHError, "DHparams_dup");
ossl_raise(ePKeyError, "DHparams_dup");

DH_get0_key(dh_other, &pub, &priv);
if (pub) {
Expand All @@ -153,7 +152,7 @@ ossl_dh_initialize_copy(VALUE self, VALUE other)
if (!pub2 || (priv && !priv2)) {
BN_clear_free(pub2);
BN_clear_free(priv2);
ossl_raise(eDHError, "BN_dup");
ossl_raise(ePKeyError, "BN_dup");
}
DH_set0_key(dh, pub2, priv2);
}
Expand All @@ -162,7 +161,7 @@ ossl_dh_initialize_copy(VALUE self, VALUE other)
if (!pkey || EVP_PKEY_assign_DH(pkey, dh) != 1) {
EVP_PKEY_free(pkey);
DH_free(dh);
ossl_raise(eDHError, "EVP_PKEY_assign_DH");
ossl_raise(ePKeyError, "EVP_PKEY_assign_DH");
}
RTYPEDDATA_DATA(self) = pkey;
return self;
Expand Down Expand Up @@ -241,11 +240,11 @@ ossl_dh_export(VALUE self)

GetDH(self, dh);
if (!(out = BIO_new(BIO_s_mem()))) {
ossl_raise(eDHError, NULL);
ossl_raise(ePKeyError, NULL);
}
if (!PEM_write_bio_DHparams(out, dh)) {
BIO_free(out);
ossl_raise(eDHError, NULL);
ossl_raise(ePKeyError, NULL);
}
str = ossl_membio2str(out);

Expand Down Expand Up @@ -275,11 +274,11 @@ ossl_dh_to_der(VALUE self)

GetDH(self, dh);
if((len = i2d_DHparams(dh, NULL)) <= 0)
ossl_raise(eDHError, NULL);
ossl_raise(ePKeyError, NULL);
str = rb_str_new(0, len);
p = (unsigned char *)RSTRING_PTR(str);
if(i2d_DHparams(dh, &p) < 0)
ossl_raise(eDHError, NULL);
ossl_raise(ePKeyError, NULL);
ossl_str_adjust(str, p);

return str;
Expand All @@ -306,7 +305,7 @@ ossl_dh_check_params(VALUE self)
GetPKey(self, pkey);
pctx = EVP_PKEY_CTX_new(pkey, /* engine */NULL);
if (!pctx)
ossl_raise(eDHError, "EVP_PKEY_CTX_new");
ossl_raise(ePKeyError, "EVP_PKEY_CTX_new");
ret = EVP_PKEY_param_check(pctx);
EVP_PKEY_CTX_free(pctx);
#else
Expand Down Expand Up @@ -355,13 +354,6 @@ Init_ossl_dh(void)
ePKeyError = rb_define_class_under(mPKey, "PKeyError", eOSSLError);
#endif

/* Document-class: OpenSSL::PKey::DHError
*
* Generic exception that is raised if an operation on a DH PKey
* fails unexpectedly or in case an instantiation of an instance of DH
* fails due to non-conformant input data.
*/
eDHError = rb_define_class_under(mPKey, "DHError", ePKeyError);
/* Document-class: OpenSSL::PKey::DH
*
* An implementation of the Diffie-Hellman key exchange protocol based on
Expand Down
21 changes: 6 additions & 15 deletions ext/openssl/ossl_pkey_dsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ DSA_PRIVATE(VALUE obj, OSSL_3_const DSA *dsa)
* Classes
*/
VALUE cDSA;
static VALUE eDSAError;

/*
* Private
Expand Down Expand Up @@ -98,7 +97,7 @@ ossl_dsa_initialize(int argc, VALUE *argv, VALUE self)
if (argc == 0) {
dsa = DSA_new();
if (!dsa)
ossl_raise(eDSAError, "DSA_new");
ossl_raise(ePKeyError, "DSA_new");
goto legacy;
}

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

type = EVP_PKEY_base_id(pkey);
if (type != EVP_PKEY_DSA) {
EVP_PKEY_free(pkey);
rb_raise(eDSAError, "incorrect pkey type: %s", OBJ_nid2sn(type));
rb_raise(ePKeyError, "incorrect pkey type: %s", OBJ_nid2sn(type));
}
RTYPEDDATA_DATA(self) = pkey;
return self;
Expand All @@ -133,7 +132,7 @@ ossl_dsa_initialize(int argc, VALUE *argv, VALUE self)
if (!pkey || EVP_PKEY_assign_DSA(pkey, dsa) != 1) {
EVP_PKEY_free(pkey);
DSA_free(dsa);
ossl_raise(eDSAError, "EVP_PKEY_assign_DSA");
ossl_raise(ePKeyError, "EVP_PKEY_assign_DSA");
}
RTYPEDDATA_DATA(self) = pkey;
return self;
Expand All @@ -156,13 +155,13 @@ ossl_dsa_initialize_copy(VALUE self, VALUE other)
(d2i_of_void *)d2i_DSAPrivateKey,
(char *)dsa);
if (!dsa_new)
ossl_raise(eDSAError, "ASN1_dup");
ossl_raise(ePKeyError, "ASN1_dup");

pkey = EVP_PKEY_new();
if (!pkey || EVP_PKEY_assign_DSA(pkey, dsa_new) != 1) {
EVP_PKEY_free(pkey);
DSA_free(dsa_new);
ossl_raise(eDSAError, "EVP_PKEY_assign_DSA");
ossl_raise(ePKeyError, "EVP_PKEY_assign_DSA");
}
RTYPEDDATA_DATA(self) = pkey;

Expand Down Expand Up @@ -333,14 +332,6 @@ Init_ossl_dsa(void)
ePKeyError = rb_define_class_under(mPKey, "PKeyError", eOSSLError);
#endif

/* Document-class: OpenSSL::PKey::DSAError
*
* Generic exception that is raised if an operation on a DSA PKey
* fails unexpectedly or in case an instantiation of an instance of DSA
* fails due to non-conformant input data.
*/
eDSAError = rb_define_class_under(mPKey, "DSAError", ePKeyError);

/* Document-class: OpenSSL::PKey::DSA
*
* DSA, the Digital Signature Algorithm, is specified in NIST's
Expand Down
Loading
Loading