Skip to content

Commit 02c4176

Browse files
committed
pkey: allow instantiating OpenSSL::PKey::PKey with unsupported key type
Fix 'unsupported key type' error if OpenSSL::SSL::SSLSocket#tmp_key is called when X25519 is used for key exchange. EVP_PKEY may have a key type that we don't have have a dedicated subclass. Let's allow instantiating OpenSSL::PKey::PKey with such an EVP_PKEY, although the resulting instance is not so useful because it can't be exported at the moment.
1 parent 528fae6 commit 02c4176

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

ext/openssl/ossl_pkey.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,13 @@ const rb_data_type_t ossl_evp_pkey_type = {
7373
static VALUE
7474
pkey_new0(EVP_PKEY *pkey)
7575
{
76-
if (!pkey)
77-
ossl_raise(ePKeyError, "cannot make new key from NULL");
76+
VALUE obj;
77+
int type;
7878

79-
switch (EVP_PKEY_base_id(pkey)) {
79+
if (!pkey || (type = EVP_PKEY_base_id(pkey)) == EVP_PKEY_NONE)
80+
ossl_raise(rb_eRuntimeError, "pkey is empty");
81+
82+
switch (type) {
8083
#if !defined(OPENSSL_NO_RSA)
8184
case EVP_PKEY_RSA:
8285
return ossl_rsa_new(pkey);
@@ -94,7 +97,9 @@ pkey_new0(EVP_PKEY *pkey)
9497
return ossl_ec_new(pkey);
9598
#endif
9699
default:
97-
ossl_raise(ePKeyError, "unsupported key type");
100+
obj = NewPKey(cPKey);
101+
SetPKey(obj, pkey);
102+
return obj;
98103
}
99104
}
100105

@@ -260,7 +265,7 @@ static VALUE
260265
ossl_pkey_initialize(VALUE self)
261266
{
262267
if (rb_obj_is_instance_of(self, cPKey)) {
263-
ossl_raise(rb_eNotImpError, "OpenSSL::PKey::PKey is an abstract class.");
268+
ossl_raise(rb_eTypeError, "OpenSSL::PKey::PKey can't be instantiated directly");
264269
}
265270
return self;
266271
}

0 commit comments

Comments
 (0)