From 02c4176ed41907623211d8ca9e41fcacac3d0811 Mon Sep 17 00:00:00 2001 From: Kazuki Yamaguchi Date: Tue, 19 Jul 2016 15:37:16 +0900 Subject: 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. --- ext/openssl/ossl_pkey.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/ext/openssl/ossl_pkey.c b/ext/openssl/ossl_pkey.c index 9e6c6157..6ab1b618 100644 --- a/ext/openssl/ossl_pkey.c +++ b/ext/openssl/ossl_pkey.c @@ -73,10 +73,13 @@ const rb_data_type_t ossl_evp_pkey_type = { static VALUE pkey_new0(EVP_PKEY *pkey) { - if (!pkey) - ossl_raise(ePKeyError, "cannot make new key from NULL"); + VALUE obj; + int type; - switch (EVP_PKEY_base_id(pkey)) { + if (!pkey || (type = EVP_PKEY_base_id(pkey)) == EVP_PKEY_NONE) + ossl_raise(rb_eRuntimeError, "pkey is empty"); + + switch (type) { #if !defined(OPENSSL_NO_RSA) case EVP_PKEY_RSA: return ossl_rsa_new(pkey); @@ -94,7 +97,9 @@ pkey_new0(EVP_PKEY *pkey) return ossl_ec_new(pkey); #endif default: - ossl_raise(ePKeyError, "unsupported key type"); + obj = NewPKey(cPKey); + SetPKey(obj, pkey); + return obj; } } @@ -260,7 +265,7 @@ static VALUE ossl_pkey_initialize(VALUE self) { if (rb_obj_is_instance_of(self, cPKey)) { - ossl_raise(rb_eNotImpError, "OpenSSL::PKey::PKey is an abstract class."); + ossl_raise(rb_eTypeError, "OpenSSL::PKey::PKey can't be instantiated directly"); } return self; } -- cgit v1.2.3