cd5466
From 316cb2a41f154e4663d7e7fead60cfc0bfa86af9 Mon Sep 17 00:00:00 2001
cd5466
From: Kazuki Yamaguchi <k@rhe.jp>
cd5466
Date: Mon, 12 Apr 2021 13:55:10 +0900
cd5466
Subject: [PATCH 1/2] pkey: do not check NULL argument in ossl_pkey_new()
cd5466
cd5466
Passing NULL to ossl_pkey_new() makes no sense in the first place, and
cd5466
in fact it is ensured not to be NULL in all cases.
cd5466
---
cd5466
 ext/openssl/ossl_pkey.c | 6 +-----
cd5466
 ext/openssl/ossl_pkey.h | 1 +
cd5466
 2 files changed, 2 insertions(+), 5 deletions(-)
cd5466
cd5466
diff --git a/ext/openssl/ossl_pkey.c b/ext/openssl/ossl_pkey.c
cd5466
index f9f5162e..820e4a2c 100644
cd5466
--- a/ext/openssl/ossl_pkey.c
cd5466
+++ b/ext/openssl/ossl_pkey.c
cd5466
@@ -38,12 +38,8 @@ static VALUE
cd5466
 pkey_new0(EVP_PKEY *pkey)
cd5466
 {
cd5466
     VALUE klass, obj;
cd5466
-    int type;
cd5466
 
cd5466
-    if (!pkey || (type = EVP_PKEY_base_id(pkey)) == EVP_PKEY_NONE)
cd5466
-	ossl_raise(rb_eRuntimeError, "pkey is empty");
cd5466
-
cd5466
-    switch (type) {
cd5466
+    switch (EVP_PKEY_base_id(pkey)) {
cd5466
 #if !defined(OPENSSL_NO_RSA)
cd5466
       case EVP_PKEY_RSA: klass = cRSA; break;
cd5466
 #endif
cd5466
diff --git a/ext/openssl/ossl_pkey.h b/ext/openssl/ossl_pkey.h
cd5466
index 4beede22..f0476780 100644
cd5466
--- a/ext/openssl/ossl_pkey.h
cd5466
+++ b/ext/openssl/ossl_pkey.h
cd5466
@@ -35,6 +35,7 @@ extern const rb_data_type_t ossl_evp_pkey_type;
cd5466
     } \
cd5466
 } while (0)
cd5466
 
cd5466
+/* Takes ownership of the EVP_PKEY */
cd5466
 VALUE ossl_pkey_new(EVP_PKEY *);
cd5466
 void ossl_pkey_check_public_key(const EVP_PKEY *);
cd5466
 EVP_PKEY *ossl_pkey_read_generic(BIO *, VALUE);
cd5466
cd5466
From 74f6c6175688502a5bf27ae35367616858630c0f Mon Sep 17 00:00:00 2001
cd5466
From: Kazuki Yamaguchi <k@rhe.jp>
cd5466
Date: Mon, 12 Apr 2021 18:32:40 +0900
cd5466
Subject: [PATCH 2/2] pkey: allocate EVP_PKEY on #initialize
cd5466
cd5466
Allocate an EVP_PKEY when the content is ready: when #initialize
cd5466
or #initialize_copy is called, rather than when a T_DATA is allocated.
cd5466
This is more natural because the lower level API has been deprecated
cd5466
and an EVP_PKEY is becoming the minimum unit of handling keys.
cd5466
---
cd5466
 ext/openssl/ossl_pkey.c     | 15 ++----
cd5466
 ext/openssl/ossl_pkey.h     | 15 ++----
cd5466
 ext/openssl/ossl_pkey_dh.c  | 71 +++++++++++++++++++--------
cd5466
 ext/openssl/ossl_pkey_dsa.c | 93 ++++++++++++++++++++---------------
cd5466
 ext/openssl/ossl_pkey_ec.c  | 91 +++++++++++++++++++----------------
cd5466
 ext/openssl/ossl_pkey_rsa.c | 96 ++++++++++++++++++++++---------------
cd5466
 6 files changed, 218 insertions(+), 163 deletions(-)
cd5466
cd5466
diff --git a/ext/openssl/ossl_pkey.c b/ext/openssl/ossl_pkey.c
cd5466
index 820e4a2c..ea75d63f 100644
cd5466
--- a/ext/openssl/ossl_pkey.c
cd5466
+++ b/ext/openssl/ossl_pkey.c
cd5466
@@ -54,8 +54,8 @@ pkey_new0(EVP_PKEY *pkey)
cd5466
 #endif
cd5466
       default:           klass = cPKey; break;
cd5466
     }
cd5466
-    obj = NewPKey(klass);
cd5466
-    SetPKey(obj, pkey);
cd5466
+    obj = rb_obj_alloc(klass);
cd5466
+    RTYPEDDATA_DATA(obj) = pkey;
cd5466
     return obj;
cd5466
 }
cd5466
 
cd5466
@@ -511,16 +511,7 @@ DupPKeyPtr(VALUE obj)
cd5466
 static VALUE
cd5466
 ossl_pkey_alloc(VALUE klass)
cd5466
 {
cd5466
-    EVP_PKEY *pkey;
cd5466
-    VALUE obj;
cd5466
-
cd5466
-    obj = NewPKey(klass);
cd5466
-    if (!(pkey = EVP_PKEY_new())) {
cd5466
-	ossl_raise(ePKeyError, NULL);
cd5466
-    }
cd5466
-    SetPKey(obj, pkey);
cd5466
-
cd5466
-    return obj;
cd5466
+    return TypedData_Wrap_Struct(klass, &ossl_evp_pkey_type, NULL);
cd5466
 }
cd5466
 
cd5466
 /*
cd5466
diff --git a/ext/openssl/ossl_pkey.h b/ext/openssl/ossl_pkey.h
cd5466
index f0476780..ed18bc69 100644
cd5466
--- a/ext/openssl/ossl_pkey.h
cd5466
+++ b/ext/openssl/ossl_pkey.h
cd5466
@@ -15,19 +15,10 @@ extern VALUE cPKey;
cd5466
 extern VALUE ePKeyError;
cd5466
 extern const rb_data_type_t ossl_evp_pkey_type;
cd5466
 
cd5466
-#define OSSL_PKEY_SET_PRIVATE(obj) rb_iv_set((obj), "private", Qtrue)
cd5466
-#define OSSL_PKEY_SET_PUBLIC(obj)  rb_iv_set((obj), "private", Qfalse)
cd5466
-#define OSSL_PKEY_IS_PRIVATE(obj)  (rb_iv_get((obj), "private") == Qtrue)
cd5466
+/* For ENGINE */
cd5466
+#define OSSL_PKEY_SET_PRIVATE(obj) rb_ivar_set((obj), rb_intern("private"), Qtrue)
cd5466
+#define OSSL_PKEY_IS_PRIVATE(obj)  (rb_attr_get((obj), rb_intern("private")) == Qtrue)
cd5466
 
cd5466
-#define NewPKey(klass) \
cd5466
-    TypedData_Wrap_Struct((klass), &ossl_evp_pkey_type, 0)
cd5466
-#define SetPKey(obj, pkey) do { \
cd5466
-    if (!(pkey)) { \
cd5466
-	rb_raise(rb_eRuntimeError, "PKEY wasn't initialized!"); \
cd5466
-    } \
cd5466
-    RTYPEDDATA_DATA(obj) = (pkey); \
cd5466
-    OSSL_PKEY_SET_PUBLIC(obj); \
cd5466
-} while (0)
cd5466
 #define GetPKey(obj, pkey) do {\
cd5466
     TypedData_Get_Struct((obj), EVP_PKEY, &ossl_evp_pkey_type, (pkey)); \
cd5466
     if (!(pkey)) { \
cd5466
diff --git a/ext/openssl/ossl_pkey_dh.c b/ext/openssl/ossl_pkey_dh.c
cd5466
index ca782bbe..04c11b21 100644
cd5466
--- a/ext/openssl/ossl_pkey_dh.c
cd5466
+++ b/ext/openssl/ossl_pkey_dh.c
cd5466
@@ -72,34 +72,57 @@ static VALUE
cd5466
 ossl_dh_initialize(int argc, VALUE *argv, VALUE self)
cd5466
 {
cd5466
     EVP_PKEY *pkey;
cd5466
+    int type;
cd5466
     DH *dh;
cd5466
-    BIO *in;
cd5466
+    BIO *in = NULL;
cd5466
     VALUE arg;
cd5466
 
cd5466
-    GetPKey(self, pkey);
cd5466
+    TypedData_Get_Struct(self, EVP_PKEY, &ossl_evp_pkey_type, pkey);
cd5466
+    if (pkey)
cd5466
+        rb_raise(rb_eTypeError, "pkey already initialized");
cd5466
+
cd5466
     /* The DH.new(size, generator) form is handled by lib/openssl/pkey.rb */
cd5466
     if (rb_scan_args(argc, argv, "01", &arg) == 0) {
cd5466
         dh = DH_new();
cd5466
         if (!dh)
cd5466
             ossl_raise(eDHError, "DH_new");
cd5466
+        goto legacy;
cd5466
     }
cd5466
-    else {
cd5466
-	arg = ossl_to_der_if_possible(arg);
cd5466
-	in = ossl_obj2bio(&arg;;
cd5466
-	dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
cd5466
-	if (!dh){
cd5466
-	    OSSL_BIO_reset(in);
cd5466
-	    dh = d2i_DHparams_bio(in, NULL);
cd5466
-	}
cd5466
-	BIO_free(in);
cd5466
-	if (!dh) {
cd5466
-	    ossl_raise(eDHError, NULL);
cd5466
-	}
cd5466
+
cd5466
+    arg = ossl_to_der_if_possible(arg);
cd5466
+    in = ossl_obj2bio(&arg;;
cd5466
+
cd5466
+    /*
cd5466
+     * On OpenSSL <= 1.1.1 and current versions of LibreSSL, the generic
cd5466
+     * routine does not support DER-encoded parameters
cd5466
+     */
cd5466
+    dh = d2i_DHparams_bio(in, NULL);
cd5466
+    if (dh)
cd5466
+        goto legacy;
cd5466
+    OSSL_BIO_reset(in);
cd5466
+
cd5466
+    pkey = ossl_pkey_read_generic(in, Qnil);
cd5466
+    BIO_free(in);
cd5466
+    if (!pkey)
cd5466
+        ossl_raise(eDHError, "could not parse pkey");
cd5466
+
cd5466
+    type = EVP_PKEY_base_id(pkey);
cd5466
+    if (type != EVP_PKEY_DH) {
cd5466
+        EVP_PKEY_free(pkey);
cd5466
+        rb_raise(eDHError, "incorrect pkey type: %s", OBJ_nid2sn(type));
cd5466
     }
cd5466
-    if (!EVP_PKEY_assign_DH(pkey, dh)) {
cd5466
-	DH_free(dh);
cd5466
-	ossl_raise(eDHError, NULL);
cd5466
+    RTYPEDDATA_DATA(self) = pkey;
cd5466
+    return self;
cd5466
+
cd5466
+  legacy:
cd5466
+    BIO_free(in);
cd5466
+    pkey = EVP_PKEY_new();
cd5466
+    if (!pkey || EVP_PKEY_assign_DH(pkey, dh) != 1) {
cd5466
+        EVP_PKEY_free(pkey);
cd5466
+        DH_free(dh);
cd5466
+        ossl_raise(eDHError, "EVP_PKEY_assign_DH");
cd5466
     }
cd5466
+    RTYPEDDATA_DATA(self) = pkey;
cd5466
     return self;
cd5466
 }
cd5466
 
cd5466
@@ -110,15 +133,14 @@ ossl_dh_initialize_copy(VALUE self, VALUE other)
cd5466
     DH *dh, *dh_other;
cd5466
     const BIGNUM *pub, *priv;
cd5466
 
cd5466
-    GetPKey(self, pkey);
cd5466
-    if (EVP_PKEY_base_id(pkey) != EVP_PKEY_NONE)
cd5466
-	ossl_raise(eDHError, "DH already initialized");
cd5466
+    TypedData_Get_Struct(self, EVP_PKEY, &ossl_evp_pkey_type, pkey);
cd5466
+    if (pkey)
cd5466
+        rb_raise(rb_eTypeError, "pkey already initialized");
cd5466
     GetDH(other, dh_other);
cd5466
 
cd5466
     dh = DHparams_dup(dh_other);
cd5466
     if (!dh)
cd5466
 	ossl_raise(eDHError, "DHparams_dup");
cd5466
-    EVP_PKEY_assign_DH(pkey, dh);
cd5466
 
cd5466
     DH_get0_key(dh_other, &pub, &priv;;
cd5466
     if (pub) {
cd5466
@@ -133,6 +155,13 @@ ossl_dh_initialize_copy(VALUE self, VALUE other)
cd5466
 	DH_set0_key(dh, pub2, priv2);
cd5466
     }
cd5466
 
cd5466
+    pkey = EVP_PKEY_new();
cd5466
+    if (!pkey || EVP_PKEY_assign_DH(pkey, dh) != 1) {
cd5466
+        EVP_PKEY_free(pkey);
cd5466
+        DH_free(dh);
cd5466
+        ossl_raise(eDHError, "EVP_PKEY_assign_DH");
cd5466
+    }
cd5466
+    RTYPEDDATA_DATA(self) = pkey;
cd5466
     return self;
cd5466
 }
cd5466
 
cd5466
diff --git a/ext/openssl/ossl_pkey_dsa.c b/ext/openssl/ossl_pkey_dsa.c
cd5466
index 7af00eeb..15724548 100644
cd5466
--- a/ext/openssl/ossl_pkey_dsa.c
cd5466
+++ b/ext/openssl/ossl_pkey_dsa.c
cd5466
@@ -83,50 +83,59 @@ VALUE eDSAError;
cd5466
 static VALUE
cd5466
 ossl_dsa_initialize(int argc, VALUE *argv, VALUE self)
cd5466
 {
cd5466
-    EVP_PKEY *pkey, *tmp;
cd5466
-    DSA *dsa = NULL;
cd5466
-    BIO *in;
cd5466
+    EVP_PKEY *pkey;
cd5466
+    DSA *dsa;
cd5466
+    BIO *in = NULL;
cd5466
     VALUE arg, pass;
cd5466
+    int type;
cd5466
+
cd5466
+    TypedData_Get_Struct(self, EVP_PKEY, &ossl_evp_pkey_type, pkey);
cd5466
+    if (pkey)
cd5466
+        rb_raise(rb_eTypeError, "pkey already initialized");
cd5466
 
cd5466
-    GetPKey(self, pkey);
cd5466
     /* The DSA.new(size, generator) form is handled by lib/openssl/pkey.rb */
cd5466
     rb_scan_args(argc, argv, "02", &arg, &pass);
cd5466
     if (argc == 0) {
cd5466
         dsa = DSA_new();
cd5466
         if (!dsa)
cd5466
             ossl_raise(eDSAError, "DSA_new");
cd5466
+        goto legacy;
cd5466
     }
cd5466
-    else {
cd5466
-	pass = ossl_pem_passwd_value(pass);
cd5466
-	arg = ossl_to_der_if_possible(arg);
cd5466
-	in = ossl_obj2bio(&arg;;
cd5466
-
cd5466
-        tmp = ossl_pkey_read_generic(in, pass);
cd5466
-        if (tmp) {
cd5466
-            if (EVP_PKEY_base_id(tmp) != EVP_PKEY_DSA)
cd5466
-                rb_raise(eDSAError, "incorrect pkey type: %s",
cd5466
-                         OBJ_nid2sn(EVP_PKEY_base_id(tmp)));
cd5466
-            dsa = EVP_PKEY_get1_DSA(tmp);
cd5466
-            EVP_PKEY_free(tmp);
cd5466
-        }
cd5466
-	if (!dsa) {
cd5466
-	    OSSL_BIO_reset(in);
cd5466
-#define PEM_read_bio_DSAPublicKey(bp,x,cb,u) (DSA *)PEM_ASN1_read_bio( \
cd5466
-	(d2i_of_void *)d2i_DSAPublicKey, PEM_STRING_DSA_PUBLIC, (bp), (void **)(x), (cb), (u))
cd5466
-	    dsa = PEM_read_bio_DSAPublicKey(in, NULL, NULL, NULL);
cd5466
-#undef PEM_read_bio_DSAPublicKey
cd5466
-	}
cd5466
-	BIO_free(in);
cd5466
-	if (!dsa) {
cd5466
-	    ossl_clear_error();
cd5466
-	    ossl_raise(eDSAError, "Neither PUB key nor PRIV key");
cd5466
-	}
cd5466
-    }
cd5466
-    if (!EVP_PKEY_assign_DSA(pkey, dsa)) {
cd5466
-	DSA_free(dsa);
cd5466
-	ossl_raise(eDSAError, NULL);
cd5466
+
cd5466
+    pass = ossl_pem_passwd_value(pass);
cd5466
+    arg = ossl_to_der_if_possible(arg);
cd5466
+    in = ossl_obj2bio(&arg;;
cd5466
+
cd5466
+    /* DER-encoded DSAPublicKey format isn't supported by the generic routine */
cd5466
+    dsa = (DSA *)PEM_ASN1_read_bio((d2i_of_void *)d2i_DSAPublicKey,
cd5466
+                                   PEM_STRING_DSA_PUBLIC,
cd5466
+                                   in, NULL, NULL, NULL);
cd5466
+    if (dsa)
cd5466
+        goto legacy;
cd5466
+    OSSL_BIO_reset(in);
cd5466
+
cd5466
+    pkey = ossl_pkey_read_generic(in, pass);
cd5466
+    BIO_free(in);
cd5466
+    if (!pkey)
cd5466
+        ossl_raise(eDSAError, "Neither PUB key nor PRIV key");
cd5466
+
cd5466
+    type = EVP_PKEY_base_id(pkey);
cd5466
+    if (type != EVP_PKEY_DSA) {
cd5466
+        EVP_PKEY_free(pkey);
cd5466
+        rb_raise(eDSAError, "incorrect pkey type: %s", OBJ_nid2sn(type));
cd5466
     }
cd5466
+    RTYPEDDATA_DATA(self) = pkey;
cd5466
+    return self;
cd5466
 
cd5466
+  legacy:
cd5466
+    BIO_free(in);
cd5466
+    pkey = EVP_PKEY_new();
cd5466
+    if (!pkey || EVP_PKEY_assign_DSA(pkey, dsa) != 1) {
cd5466
+        EVP_PKEY_free(pkey);
cd5466
+        DSA_free(dsa);
cd5466
+        ossl_raise(eDSAError, "EVP_PKEY_assign_DSA");
cd5466
+    }
cd5466
+    RTYPEDDATA_DATA(self) = pkey;
cd5466
     return self;
cd5466
 }
cd5466
 
cd5466
@@ -136,16 +145,24 @@ ossl_dsa_initialize_copy(VALUE self, VALUE other)
cd5466
     EVP_PKEY *pkey;
cd5466
     DSA *dsa, *dsa_new;
cd5466
 
cd5466
-    GetPKey(self, pkey);
cd5466
-    if (EVP_PKEY_base_id(pkey) != EVP_PKEY_NONE)
cd5466
-	ossl_raise(eDSAError, "DSA already initialized");
cd5466
+    TypedData_Get_Struct(self, EVP_PKEY, &ossl_evp_pkey_type, pkey);
cd5466
+    if (pkey)
cd5466
+        rb_raise(rb_eTypeError, "pkey already initialized");
cd5466
     GetDSA(other, dsa);
cd5466
 
cd5466
-    dsa_new = ASN1_dup((i2d_of_void *)i2d_DSAPrivateKey, (d2i_of_void *)d2i_DSAPrivateKey, (char *)dsa);
cd5466
+    dsa_new = (DSA *)ASN1_dup((i2d_of_void *)i2d_DSAPrivateKey,
cd5466
+                              (d2i_of_void *)d2i_DSAPrivateKey,
cd5466
+                              (char *)dsa);
cd5466
     if (!dsa_new)
cd5466
 	ossl_raise(eDSAError, "ASN1_dup");
cd5466
 
cd5466
-    EVP_PKEY_assign_DSA(pkey, dsa_new);
cd5466
+    pkey = EVP_PKEY_new();
cd5466
+    if (!pkey || EVP_PKEY_assign_DSA(pkey, dsa_new) != 1) {
cd5466
+        EVP_PKEY_free(pkey);
cd5466
+        DSA_free(dsa_new);
cd5466
+        ossl_raise(eDSAError, "EVP_PKEY_assign_DSA");
cd5466
+    }
cd5466
+    RTYPEDDATA_DATA(self) = pkey;
cd5466
 
cd5466
     return self;
cd5466
 }
cd5466
diff --git a/ext/openssl/ossl_pkey_ec.c b/ext/openssl/ossl_pkey_ec.c
cd5466
index db80d112..71e63969 100644
cd5466
--- a/ext/openssl/ossl_pkey_ec.c
cd5466
+++ b/ext/openssl/ossl_pkey_ec.c
cd5466
@@ -114,13 +114,16 @@ ossl_ec_key_s_generate(VALUE klass, VALUE arg)
cd5466
     VALUE obj;
cd5466
 
cd5466
     obj = rb_obj_alloc(klass);
cd5466
-    GetPKey(obj, pkey);
cd5466
 
cd5466
     ec = ec_key_new_from_group(arg);
cd5466
-    if (!EVP_PKEY_assign_EC_KEY(pkey, ec)) {
cd5466
+    pkey = EVP_PKEY_new();
cd5466
+    if (!pkey || EVP_PKEY_assign_EC_KEY(pkey, ec) != 1) {
cd5466
+        EVP_PKEY_free(pkey);
cd5466
         EC_KEY_free(ec);
cd5466
         ossl_raise(eECError, "EVP_PKEY_assign_EC_KEY");
cd5466
     }
cd5466
+    RTYPEDDATA_DATA(obj) = pkey;
cd5466
+
cd5466
     if (!EC_KEY_generate_key(ec))
cd5466
 	ossl_raise(eECError, "EC_KEY_generate_key");
cd5466
 
cd5466
@@ -141,51 +144,54 @@ ossl_ec_key_s_generate(VALUE klass, VALUE arg)
cd5466
 static VALUE ossl_ec_key_initialize(int argc, VALUE *argv, VALUE self)
cd5466
 {
cd5466
     EVP_PKEY *pkey;
cd5466
-    EC_KEY *ec = NULL;
cd5466
+    EC_KEY *ec;
cd5466
+    BIO *in;
cd5466
     VALUE arg, pass;
cd5466
+    int type;
cd5466
 
cd5466
-    GetPKey(self, pkey);
cd5466
-    if (EVP_PKEY_base_id(pkey) != EVP_PKEY_NONE)
cd5466
-        ossl_raise(eECError, "EC_KEY already initialized");
cd5466
+    TypedData_Get_Struct(self, EVP_PKEY, &ossl_evp_pkey_type, pkey);
cd5466
+    if (pkey)
cd5466
+        rb_raise(rb_eTypeError, "pkey already initialized");
cd5466
 
cd5466
     rb_scan_args(argc, argv, "02", &arg, &pass);
cd5466
-
cd5466
     if (NIL_P(arg)) {
cd5466
         if (!(ec = EC_KEY_new()))
cd5466
-	    ossl_raise(eECError, NULL);
cd5466
-    } else if (rb_obj_is_kind_of(arg, cEC)) {
cd5466
-	EC_KEY *other_ec = NULL;
cd5466
+            ossl_raise(eECError, "EC_KEY_new");
cd5466
+        goto legacy;
cd5466
+    }
cd5466
+    else if (rb_obj_is_kind_of(arg, cEC_GROUP)) {
cd5466
+        ec = ec_key_new_from_group(arg);
cd5466
+        goto legacy;
cd5466
+    }
cd5466
 
cd5466
-	GetEC(arg, other_ec);
cd5466
-	if (!(ec = EC_KEY_dup(other_ec)))
cd5466
-	    ossl_raise(eECError, NULL);
cd5466
-    } else if (rb_obj_is_kind_of(arg, cEC_GROUP)) {
cd5466
-	ec = ec_key_new_from_group(arg);
cd5466
-    } else {
cd5466
-        BIO *in = ossl_obj2bio(&arg;;
cd5466
-        EVP_PKEY *tmp;
cd5466
-        pass = ossl_pem_passwd_value(pass);
cd5466
-        tmp = ossl_pkey_read_generic(in, pass);
cd5466
-        if (tmp) {
cd5466
-            if (EVP_PKEY_base_id(tmp) != EVP_PKEY_EC)
cd5466
-                rb_raise(eECError, "incorrect pkey type: %s",
cd5466
-                         OBJ_nid2sn(EVP_PKEY_base_id(tmp)));
cd5466
-            ec = EVP_PKEY_get1_EC_KEY(tmp);
cd5466
-            EVP_PKEY_free(tmp);
cd5466
-        }
cd5466
-	BIO_free(in);
cd5466
+    pass = ossl_pem_passwd_value(pass);
cd5466
+    arg = ossl_to_der_if_possible(arg);
cd5466
+    in = ossl_obj2bio(&arg;;
cd5466
 
cd5466
-	if (!ec) {
cd5466
-	    ossl_clear_error();
cd5466
-	    ec = ec_key_new_from_group(arg);
cd5466
-	}
cd5466
+    pkey = ossl_pkey_read_generic(in, pass);
cd5466
+    BIO_free(in);
cd5466
+    if (!pkey) {
cd5466
+        ossl_clear_error();
cd5466
+        ec = ec_key_new_from_group(arg);
cd5466
+        goto legacy;
cd5466
     }
cd5466
 
cd5466
-    if (!EVP_PKEY_assign_EC_KEY(pkey, ec)) {
cd5466
-	EC_KEY_free(ec);
cd5466
-	ossl_raise(eECError, "EVP_PKEY_assign_EC_KEY");
cd5466
+    type = EVP_PKEY_base_id(pkey);
cd5466
+    if (type != EVP_PKEY_EC) {
cd5466
+        EVP_PKEY_free(pkey);
cd5466
+        rb_raise(eDSAError, "incorrect pkey type: %s", OBJ_nid2sn(type));
cd5466
     }
cd5466
+    RTYPEDDATA_DATA(self) = pkey;
cd5466
+    return self;
cd5466
 
cd5466
+  legacy:
cd5466
+    pkey = EVP_PKEY_new();
cd5466
+    if (!pkey || EVP_PKEY_assign_EC_KEY(pkey, ec) != 1) {
cd5466
+        EVP_PKEY_free(pkey);
cd5466
+        EC_KEY_free(ec);
cd5466
+        ossl_raise(eECError, "EVP_PKEY_assign_EC_KEY");
cd5466
+    }
cd5466
+    RTYPEDDATA_DATA(self) = pkey;
cd5466
     return self;
cd5466
 }
cd5466
 
cd5466
@@ -195,18 +201,21 @@ ossl_ec_key_initialize_copy(VALUE self, VALUE other)
cd5466
     EVP_PKEY *pkey;
cd5466
     EC_KEY *ec, *ec_new;
cd5466
 
cd5466
-    GetPKey(self, pkey);
cd5466
-    if (EVP_PKEY_base_id(pkey) != EVP_PKEY_NONE)
cd5466
-	ossl_raise(eECError, "EC already initialized");
cd5466
+    TypedData_Get_Struct(self, EVP_PKEY, &ossl_evp_pkey_type, pkey);
cd5466
+    if (pkey)
cd5466
+        rb_raise(rb_eTypeError, "pkey already initialized");
cd5466
     GetEC(other, ec);
cd5466
 
cd5466
     ec_new = EC_KEY_dup(ec);
cd5466
     if (!ec_new)
cd5466
 	ossl_raise(eECError, "EC_KEY_dup");
cd5466
-    if (!EVP_PKEY_assign_EC_KEY(pkey, ec_new)) {
cd5466
-	EC_KEY_free(ec_new);
cd5466
-	ossl_raise(eECError, "EVP_PKEY_assign_EC_KEY");
cd5466
+
cd5466
+    pkey = EVP_PKEY_new();
cd5466
+    if (!pkey || EVP_PKEY_assign_EC_KEY(pkey, ec_new) != 1) {
cd5466
+        EC_KEY_free(ec_new);
cd5466
+        ossl_raise(eECError, "EVP_PKEY_assign_EC_KEY");
cd5466
     }
cd5466
+    RTYPEDDATA_DATA(self) = pkey;
cd5466
 
cd5466
     return self;
cd5466
 }
cd5466
diff --git a/ext/openssl/ossl_pkey_rsa.c b/ext/openssl/ossl_pkey_rsa.c
cd5466
index 8ebd3ec5..b8dbc0e1 100644
cd5466
--- a/ext/openssl/ossl_pkey_rsa.c
cd5466
+++ b/ext/openssl/ossl_pkey_rsa.c
cd5466
@@ -76,51 +76,62 @@ VALUE eRSAError;
cd5466
 static VALUE
cd5466
 ossl_rsa_initialize(int argc, VALUE *argv, VALUE self)
cd5466
 {
cd5466
-    EVP_PKEY *pkey, *tmp;
cd5466
-    RSA *rsa = NULL;
cd5466
-    BIO *in;
cd5466
+    EVP_PKEY *pkey;
cd5466
+    RSA *rsa;
cd5466
+    BIO *in = NULL;
cd5466
     VALUE arg, pass;
cd5466
+    int type;
cd5466
+
cd5466
+    TypedData_Get_Struct(self, EVP_PKEY, &ossl_evp_pkey_type, pkey);
cd5466
+    if (pkey)
cd5466
+        rb_raise(rb_eTypeError, "pkey already initialized");
cd5466
 
cd5466
-    GetPKey(self, pkey);
cd5466
     /* The RSA.new(size, generator) form is handled by lib/openssl/pkey.rb */
cd5466
     rb_scan_args(argc, argv, "02", &arg, &pass);
cd5466
     if (argc == 0) {
cd5466
 	rsa = RSA_new();
cd5466
         if (!rsa)
cd5466
             ossl_raise(eRSAError, "RSA_new");
cd5466
+        goto legacy;
cd5466
     }
cd5466
-    else {
cd5466
-	pass = ossl_pem_passwd_value(pass);
cd5466
-	arg = ossl_to_der_if_possible(arg);
cd5466
-	in = ossl_obj2bio(&arg;;
cd5466
-
cd5466
-        tmp = ossl_pkey_read_generic(in, pass);
cd5466
-        if (tmp) {
cd5466
-            if (EVP_PKEY_base_id(tmp) != EVP_PKEY_RSA)
cd5466
-                rb_raise(eRSAError, "incorrect pkey type: %s",
cd5466
-                         OBJ_nid2sn(EVP_PKEY_base_id(tmp)));
cd5466
-            rsa = EVP_PKEY_get1_RSA(tmp);
cd5466
-            EVP_PKEY_free(tmp);
cd5466
-        }
cd5466
-	if (!rsa) {
cd5466
-	    OSSL_BIO_reset(in);
cd5466
-	    rsa = PEM_read_bio_RSAPublicKey(in, NULL, NULL, NULL);
cd5466
-	}
cd5466
-	if (!rsa) {
cd5466
-	    OSSL_BIO_reset(in);
cd5466
-	    rsa = d2i_RSAPublicKey_bio(in, NULL);
cd5466
-	}
cd5466
-	BIO_free(in);
cd5466
-	if (!rsa) {
cd5466
-            ossl_clear_error();
cd5466
-	    ossl_raise(eRSAError, "Neither PUB key nor PRIV key");
cd5466
-	}
cd5466
-    }
cd5466
-    if (!EVP_PKEY_assign_RSA(pkey, rsa)) {
cd5466
-	RSA_free(rsa);
cd5466
-	ossl_raise(eRSAError, "EVP_PKEY_assign_RSA");
cd5466
+
cd5466
+    pass = ossl_pem_passwd_value(pass);
cd5466
+    arg = ossl_to_der_if_possible(arg);
cd5466
+    in = ossl_obj2bio(&arg;;
cd5466
+
cd5466
+    /* First try RSAPublicKey format */
cd5466
+    rsa = d2i_RSAPublicKey_bio(in, NULL);
cd5466
+    if (rsa)
cd5466
+        goto legacy;
cd5466
+    OSSL_BIO_reset(in);
cd5466
+    rsa = PEM_read_bio_RSAPublicKey(in, NULL, NULL, NULL);
cd5466
+    if (rsa)
cd5466
+        goto legacy;
cd5466
+    OSSL_BIO_reset(in);
cd5466
+
cd5466
+    /* Use the generic routine */
cd5466
+    pkey = ossl_pkey_read_generic(in, pass);
cd5466
+    BIO_free(in);
cd5466
+    if (!pkey)
cd5466
+        ossl_raise(eRSAError, "Neither PUB key nor PRIV key");
cd5466
+
cd5466
+    type = EVP_PKEY_base_id(pkey);
cd5466
+    if (type != EVP_PKEY_RSA) {
cd5466
+        EVP_PKEY_free(pkey);
cd5466
+        rb_raise(eRSAError, "incorrect pkey type: %s", OBJ_nid2sn(type));
cd5466
     }
cd5466
+    RTYPEDDATA_DATA(self) = pkey;
cd5466
+    return self;
cd5466
 
cd5466
+  legacy:
cd5466
+    BIO_free(in);
cd5466
+    pkey = EVP_PKEY_new();
cd5466
+    if (!pkey || EVP_PKEY_assign_RSA(pkey, rsa) != 1) {
cd5466
+        EVP_PKEY_free(pkey);
cd5466
+        RSA_free(rsa);
cd5466
+        ossl_raise(eRSAError, "EVP_PKEY_assign_RSA");
cd5466
+    }
cd5466
+    RTYPEDDATA_DATA(self) = pkey;
cd5466
     return self;
cd5466
 }
cd5466
 
cd5466
@@ -130,16 +141,23 @@ ossl_rsa_initialize_copy(VALUE self, VALUE other)
cd5466
     EVP_PKEY *pkey;
cd5466
     RSA *rsa, *rsa_new;
cd5466
 
cd5466
-    GetPKey(self, pkey);
cd5466
-    if (EVP_PKEY_base_id(pkey) != EVP_PKEY_NONE)
cd5466
-	ossl_raise(eRSAError, "RSA already initialized");
cd5466
+    TypedData_Get_Struct(self, EVP_PKEY, &ossl_evp_pkey_type, pkey);
cd5466
+    if (pkey)
cd5466
+        rb_raise(rb_eTypeError, "pkey already initialized");
cd5466
     GetRSA(other, rsa);
cd5466
 
cd5466
-    rsa_new = ASN1_dup((i2d_of_void *)i2d_RSAPrivateKey, (d2i_of_void *)d2i_RSAPrivateKey, (char *)rsa);
cd5466
+    rsa_new = (RSA *)ASN1_dup((i2d_of_void *)i2d_RSAPrivateKey,
cd5466
+                              (d2i_of_void *)d2i_RSAPrivateKey,
cd5466
+                              (char *)rsa);
cd5466
     if (!rsa_new)
cd5466
 	ossl_raise(eRSAError, "ASN1_dup");
cd5466
 
cd5466
-    EVP_PKEY_assign_RSA(pkey, rsa_new);
cd5466
+    pkey = EVP_PKEY_new();
cd5466
+    if (!pkey || EVP_PKEY_assign_RSA(pkey, rsa_new) != 1) {
cd5466
+        RSA_free(rsa_new);
cd5466
+        ossl_raise(eRSAError, "EVP_PKEY_assign_RSA");
cd5466
+    }
cd5466
+    RTYPEDDATA_DATA(self) = pkey;
cd5466
 
cd5466
     return self;
cd5466
 }
cd5466