6daba0
From 46ca47060ca8ef3419ec36c2326a81b442d9b43b Mon Sep 17 00:00:00 2001
6daba0
From: Kazuki Yamaguchi <k@rhe.jp>
6daba0
Date: Sun, 12 Dec 2021 01:25:20 +0900
6daba0
Subject: [PATCH 1/5] pkey/dh: avoid using DH#set_key in DH#compute_key
6daba0
6daba0
DH#set_key will not work on OpenSSL 3.0 because keys are immutable.
6daba0
For now, let's reimplement DH#compute_key by manually constructing a
6daba0
DER-encoded SubjectPublicKeyInfo structure and feeding it to
6daba0
OpenSSL::PKey.read.
6daba0
6daba0
Eventually, we should implement a new method around EVP_PKEY_fromdata()
6daba0
and use it instead.
6daba0
---
6daba0
 ext/openssl/lib/openssl/pkey.rb | 16 +++++++++++++---
6daba0
 1 file changed, 13 insertions(+), 3 deletions(-)
6daba0
6daba0
diff --git a/ext/openssl/lib/openssl/pkey.rb b/ext/openssl/lib/openssl/pkey.rb
6daba0
index f6bf5892..5864faa9 100644
6daba0
--- a/ext/openssl/lib/openssl/pkey.rb
6daba0
+++ b/ext/openssl/lib/openssl/pkey.rb
6daba0
@@ -47,9 +47,19 @@ def public_key
6daba0
     # * _pub_bn_ is a OpenSSL::BN, *not* the DH instance returned by
6daba0
     #   DH#public_key as that contains the DH parameters only.
6daba0
     def compute_key(pub_bn)
6daba0
-      peer = dup
6daba0
-      peer.set_key(pub_bn, nil)
6daba0
-      derive(peer)
6daba0
+      # FIXME: This is constructing an X.509 SubjectPublicKeyInfo and is very
6daba0
+      # inefficient
6daba0
+      obj = OpenSSL::ASN1.Sequence([
6daba0
+        OpenSSL::ASN1.Sequence([
6daba0
+          OpenSSL::ASN1.ObjectId("dhKeyAgreement"),
6daba0
+          OpenSSL::ASN1.Sequence([
6daba0
+            OpenSSL::ASN1.Integer(p),
6daba0
+            OpenSSL::ASN1.Integer(g),
6daba0
+          ]),
6daba0
+        ]),
6daba0
+        OpenSSL::ASN1.BitString(OpenSSL::ASN1.Integer(pub_bn).to_der),
6daba0
+      ])
6daba0
+      derive(OpenSSL::PKey.read(obj.to_der))
6daba0
     end
6daba0
 
6daba0
     # :call-seq:
6daba0
6daba0
From fc9aabc18df3c189cc6a76a1470ca908c4f16480 Mon Sep 17 00:00:00 2001
6daba0
From: Kazuki Yamaguchi <k@rhe.jp>
6daba0
Date: Fri, 17 Dec 2021 02:22:25 +0900
6daba0
Subject: [PATCH 2/5] pkey/ec: avoid using EC#public_key= in EC#dh_compute_key
6daba0
6daba0
Similarly to DH#compute_key, work around it by constructing a
6daba0
SubjectPublicKeyInfo. This should be considered as a temporary
6daba0
implementation.
6daba0
---
6daba0
 ext/openssl/lib/openssl/pkey.rb | 11 ++++++++---
6daba0
 1 file changed, 8 insertions(+), 3 deletions(-)
6daba0
6daba0
diff --git a/ext/openssl/lib/openssl/pkey.rb b/ext/openssl/lib/openssl/pkey.rb
6daba0
index 5864faa9..ba04cf4b 100644
6daba0
--- a/ext/openssl/lib/openssl/pkey.rb
6daba0
+++ b/ext/openssl/lib/openssl/pkey.rb
6daba0
@@ -259,9 +259,14 @@ def dsa_verify_asn1(data, sig)
6daba0
     # This method is provided for backwards compatibility, and calls #derive
6daba0
     # internally.
6daba0
     def dh_compute_key(pubkey)
6daba0
-      peer = OpenSSL::PKey::EC.new(group)
6daba0
-      peer.public_key = pubkey
6daba0
-      derive(peer)
6daba0
+      obj = OpenSSL::ASN1.Sequence([
6daba0
+        OpenSSL::ASN1.Sequence([
6daba0
+          OpenSSL::ASN1.ObjectId("id-ecPublicKey"),
6daba0
+          group.to_der,
6daba0
+        ]),
6daba0
+        OpenSSL::ASN1.BitString(pubkey.to_octet_string(:uncompressed)),
6daba0
+      ])
6daba0
+      derive(OpenSSL::PKey.read(obj.to_der))
6daba0
     end
6daba0
   end
6daba0
 
6daba0
6daba0
From 8ee6a582c7e4614eec4f5ca5ab59898fbcb50d2a Mon Sep 17 00:00:00 2001
6daba0
From: Kazuki Yamaguchi <k@rhe.jp>
6daba0
Date: Fri, 22 Oct 2021 16:24:07 +0900
6daba0
Subject: [PATCH 3/5] pkey/dh: deprecate OpenSSL::PKey::DH#generate_key!
6daba0
6daba0
OpenSSL::PKey::DH#generate_key! will not work on OpenSSL 3.0 because
6daba0
keys are made immutable. Users should use OpenSSL::PKey.generate_key
6daba0
instead.
6daba0
---
6daba0
 ext/openssl/lib/openssl/pkey.rb | 23 +++++++++++++++++++----
6daba0
 ext/openssl/ossl_pkey_dh.c      |  9 +++++----
6daba0
 test/openssl/test_pkey_dh.rb    | 18 ++++++++++--------
6daba0
 3 files changed, 34 insertions(+), 16 deletions(-)
6daba0
6daba0
diff --git a/ext/openssl/lib/openssl/pkey.rb b/ext/openssl/lib/openssl/pkey.rb
6daba0
index ba04cf4b..c3e06290 100644
6daba0
--- a/ext/openssl/lib/openssl/pkey.rb
6daba0
+++ b/ext/openssl/lib/openssl/pkey.rb
6daba0
@@ -71,14 +71,29 @@ def compute_key(pub_bn)
6daba0
     # called first in order to generate the per-session keys before performing
6daba0
     # the actual key exchange.
6daba0
     #
6daba0
+    # Deprecated in version 3.0. This method is incompatible with
6daba0
+    # OpenSSL 3.0.0 or later.
6daba0
+    #
6daba0
     # See also OpenSSL::PKey.generate_key.
6daba0
     #
6daba0
     # Example:
6daba0
-    #   dh = OpenSSL::PKey::DH.new(2048)
6daba0
-    #   public_key = dh.public_key #contains no private/public key yet
6daba0
-    #   public_key.generate_key!
6daba0
-    #   puts public_key.private? # => true
6daba0
+    #   # DEPRECATED USAGE: This will not work on OpenSSL 3.0 or later
6daba0
+    #   dh0 = OpenSSL::PKey::DH.new(2048)
6daba0
+    #   dh = dh0.public_key # #public_key only copies the DH parameters (contrary to the name)
6daba0
+    #   dh.generate_key!
6daba0
+    #   puts dh.private? # => true
6daba0
+    #   puts dh0.pub_key == dh.pub_key #=> false
6daba0
+    #
6daba0
+    #   # With OpenSSL::PKey.generate_key
6daba0
+    #   dh0 = OpenSSL::PKey::DH.new(2048)
6daba0
+    #   dh = OpenSSL::PKey.generate_key(dh0)
6daba0
+    #   puts dh0.pub_key == dh.pub_key #=> false
6daba0
     def generate_key!
6daba0
+      if OpenSSL::OPENSSL_VERSION_NUMBER >= 0x30000000
6daba0
+        raise DHError, "OpenSSL::PKey::DH is immutable on OpenSSL 3.0; " \
6daba0
+        "use OpenSSL::PKey.generate_key instead"
6daba0
+      end
6daba0
+
6daba0
       unless priv_key
6daba0
         tmp = OpenSSL::PKey.generate_key(self)
6daba0
         set_key(tmp.pub_key, tmp.priv_key)
6daba0
diff --git a/ext/openssl/ossl_pkey_dh.c b/ext/openssl/ossl_pkey_dh.c
6daba0
index 04c11b2157..e70d60ed19 100644
6daba0
--- a/ext/openssl/ossl_pkey_dh.c
6daba0
+++ b/ext/openssl/ossl_pkey_dh.c
6daba0
@@ -58,15 +58,16 @@ VALUE eDHError;
6daba0
  *
6daba0
  * Examples:
6daba0
  *   # Creating an instance from scratch
6daba0
- *   dh = DH.new
6daba0
+ *   # Note that this is deprecated and will not work on OpenSSL 3.0 or later.
6daba0
+ *   dh = OpenSSL::PKey::DH.new
6daba0
  *   dh.set_pqg(bn_p, nil, bn_g)
6daba0
  *
6daba0
  *   # Generating a parameters and a key pair
6daba0
- *   dh = DH.new(2048) # An alias of DH.generate(2048)
6daba0
+ *   dh = OpenSSL::PKey::DH.new(2048) # An alias of OpenSSL::PKey::DH.generate(2048)
6daba0
  *
6daba0
  *   # Reading DH parameters
6daba0
- *   dh = DH.new(File.read('parameters.pem')) # -> dh, but no public/private key yet
6daba0
- *   dh.generate_key! # -> dh with public and private key
6daba0
+ *   dh_params = OpenSSL::PKey::DH.new(File.read('parameters.pem')) # loads parameters only
6daba0
+ *   dh = OpenSSL::PKey.generate_key(dh_params) # generates a key pair
6daba0
  */
6daba0
 static VALUE
6daba0
 ossl_dh_initialize(int argc, VALUE *argv, VALUE self)
6daba0
diff --git a/test/openssl/test_pkey_dh.rb b/test/openssl/test_pkey_dh.rb
6daba0
index 757704ca..ac11af38 100644
6daba0
--- a/test/openssl/test_pkey_dh.rb
6daba0
+++ b/test/openssl/test_pkey_dh.rb
6daba0
@@ -26,14 +26,19 @@ def test_new_break
6daba0
   end
6daba0
 
6daba0
   def test_derive_key
6daba0
-    dh1 = Fixtures.pkey("dh1024").generate_key!
6daba0
-    dh2 = Fixtures.pkey("dh1024").generate_key!
6daba0
+    params = Fixtures.pkey("dh1024")
6daba0
+    dh1 = OpenSSL::PKey.generate_key(params)
6daba0
+    dh2 = OpenSSL::PKey.generate_key(params)
6daba0
     dh1_pub = OpenSSL::PKey.read(dh1.public_to_der)
6daba0
     dh2_pub = OpenSSL::PKey.read(dh2.public_to_der)
6daba0
+
6daba0
     z = dh1.g.mod_exp(dh1.priv_key, dh1.p).mod_exp(dh2.priv_key, dh1.p).to_s(2)
6daba0
     assert_equal z, dh1.derive(dh2_pub)
6daba0
     assert_equal z, dh2.derive(dh1_pub)
6daba0
 
6daba0
+    assert_raise(OpenSSL::PKey::PKeyError) { params.derive(dh1_pub) }
6daba0
+    assert_raise(OpenSSL::PKey::PKeyError) { dh1_pub.derive(params) }
6daba0
+
6daba0
     assert_equal z, dh1.compute_key(dh2.pub_key)
6daba0
     assert_equal z, dh2.compute_key(dh1.pub_key)
6daba0
   end
6daba0
@@ -74,19 +79,16 @@ def test_public_key
6daba0
   end
6daba0
 
6daba0
   def test_generate_key
6daba0
-    dh = Fixtures.pkey("dh1024").public_key # creates a copy
6daba0
+    # Deprecated in v3.0.0; incompatible with OpenSSL 3.0
6daba0
+    dh = Fixtures.pkey("dh1024").public_key # creates a copy with params only
6daba0
     assert_no_key(dh)
6daba0
     dh.generate_key!
6daba0
     assert_key(dh)
6daba0
-  end
6daba0
 
6daba0
-  def test_key_exchange
6daba0
-    dh = Fixtures.pkey("dh1024")
6daba0
     dh2 = dh.public_key
6daba0
-    dh.generate_key!
6daba0
     dh2.generate_key!
6daba0
     assert_equal(dh.compute_key(dh2.pub_key), dh2.compute_key(dh.pub_key))
6daba0
-  end
6daba0
+  end if !openssl?(3, 0, 0)
6daba0
 
6daba0
   def test_params_ok?
6daba0
     dh0 = Fixtures.pkey("dh1024")
6daba0
6daba0
From 5e2e66cce870ea86001dbb0eaa3092badfd37994 Mon Sep 17 00:00:00 2001
6daba0
From: Kazuki Yamaguchi <k@rhe.jp>
6daba0
Date: Fri, 17 Dec 2021 02:21:42 +0900
6daba0
Subject: [PATCH 4/5] pkey/ec: deprecate OpenSSL::PKey::EC#generate_key!
6daba0
6daba0
OpenSSL::PKey::EC#generate_key! will not work on OpenSSL 3.0 because
6daba0
keys are made immutable. Users should use OpenSSL::PKey.generate_key
6daba0
instead.
6daba0
---
6daba0
 ext/openssl/ossl_pkey_ec.c   |  4 ++++
6daba0
 test/openssl/test_pkey_ec.rb | 21 +++++++++++++--------
6daba0
 2 files changed, 17 insertions(+), 8 deletions(-)
6daba0
6daba0
diff --git a/ext/openssl/ossl_pkey_ec.c b/ext/openssl/ossl_pkey_ec.c
6daba0
index db80d112..398a550a 100644
6daba0
--- a/ext/openssl/ossl_pkey_ec.c
6daba0
+++ b/ext/openssl/ossl_pkey_ec.c
6daba0
@@ -442,6 +442,9 @@ ossl_ec_key_to_der(VALUE self)
6daba0
  */
6daba0
 static VALUE ossl_ec_key_generate_key(VALUE self)
6daba0
 {
6daba0
+#if OSSL_OPENSSL_PREREQ(3, 0, 0)
6daba0
+    rb_raise(ePKeyError, "pkeys are immutable on OpenSSL 3.0");
6daba0
+#else
6daba0
     EC_KEY *ec;
6daba0
 
6daba0
     GetEC(self, ec);
6daba0
@@ -449,6 +452,7 @@ static VALUE ossl_ec_key_generate_key(VALUE self)
6daba0
 	ossl_raise(eECError, "EC_KEY_generate_key");
6daba0
 
6daba0
     return self;
6daba0
+#endif
6daba0
 }
6daba0
 
6daba0
 /*
6daba0
diff --git a/test/openssl/test_pkey_ec.rb b/test/openssl/test_pkey_ec.rb
6daba0
index 3f5958af..33f78a4c 100644
6daba0
--- a/test/openssl/test_pkey_ec.rb
6daba0
+++ b/test/openssl/test_pkey_ec.rb
6daba0
@@ -13,15 +13,13 @@ def test_ec_key
6daba0
       # FIPS-selftest failure on some environment, so skip for now.
6daba0
       next if ["Oakley", "X25519"].any? { |n| curve_name.start_with?(n) }
6daba0
 
6daba0
-      key = OpenSSL::PKey::EC.new(curve_name)
6daba0
-      key.generate_key!
6daba0
-
6daba0
+      key = OpenSSL::PKey::EC.generate(curve_name)
6daba0
       assert_predicate key, :private?
6daba0
       assert_predicate key, :public?
6daba0
       assert_nothing_raised { key.check_key }
6daba0
     end
6daba0
 
6daba0
-    key1 = OpenSSL::PKey::EC.new("prime256v1").generate_key!
6daba0
+    key1 = OpenSSL::PKey::EC.generate("prime256v1")
6daba0
 
6daba0
     key2 = OpenSSL::PKey::EC.new
6daba0
     key2.group = key1.group
6daba0
@@ -52,6 +50,13 @@ def test_generate
6daba0
     assert_equal(true, ec.private?)
6daba0
   end
6daba0
 
6daba0
+  def test_generate_key
6daba0
+    ec = OpenSSL::PKey::EC.new("prime256v1")
6daba0
+    assert_equal false, ec.private?
6daba0
+    ec.generate_key!
6daba0
+    assert_equal true, ec.private?
6daba0
+  end if !openssl?(3, 0, 0)
6daba0
+
6daba0
   def test_marshal
6daba0
     key = Fixtures.pkey("p256")
6daba0
     deserialized = Marshal.load(Marshal.dump(key))
6daba0
@@ -136,7 +141,7 @@ def test_sign_verify_raw
6daba0
   end
6daba0
 
6daba0
   def test_dsa_sign_asn1_FIPS186_3
6daba0
-    key = OpenSSL::PKey::EC.new("prime256v1").generate_key!
6daba0
+    key = OpenSSL::PKey::EC.generate("prime256v1")
6daba0
     size = key.group.order.num_bits / 8 + 1
6daba0
     dgst = (1..size).to_a.pack('C*')
6daba0
     sig = key.dsa_sign_asn1(dgst)
6daba0
@@ -145,8 +150,8 @@ def test_dsa_sign_asn1_FIPS186_3
6daba0
   end
6daba0
 
6daba0
   def test_dh_compute_key
6daba0
-    key_a = OpenSSL::PKey::EC.new("prime256v1").generate_key!
6daba0
-    key_b = OpenSSL::PKey::EC.new(key_a.group).generate_key!
6daba0
+    key_a = OpenSSL::PKey::EC.generate("prime256v1")
6daba0
+    key_b = OpenSSL::PKey::EC.generate(key_a.group)
6daba0
 
6daba0
     pub_a = key_a.public_key
6daba0
     pub_b = key_b.public_key
6daba0
@@ -276,7 +281,7 @@ def test_ec_group
6daba0
 
6daba0
   def test_ec_point
6daba0
     group = OpenSSL::PKey::EC::Group.new("prime256v1")
6daba0
-    key = OpenSSL::PKey::EC.new(group).generate_key!
6daba0
+    key = OpenSSL::PKey::EC.generate(group)
6daba0
     point = key.public_key
6daba0
 
6daba0
     point2 = OpenSSL::PKey::EC::Point.new(group, point.to_bn)
6daba0
6daba0
From 6848d2d969d90e6a400d89848ecec21076b87888 Mon Sep 17 00:00:00 2001
6daba0
From: Kazuki Yamaguchi <k@rhe.jp>
6daba0
Date: Tue, 21 Sep 2021 18:29:59 +0900
6daba0
Subject: [PATCH 5/5] pkey: deprecate PKey#set_* methods
6daba0
6daba0
OpenSSL 3.0 made EVP_PKEY immutable. This means we can only have a const
6daba0
pointer of the low level struct and the following methods can no longer
6daba0
be provided when linked against OpenSSL 3.0:
6daba0
6daba0
 - OpenSSL::PKey::RSA#set_key
6daba0
 - OpenSSL::PKey::RSA#set_factors
6daba0
 - OpenSSL::PKey::RSA#set_crt_params
6daba0
 - OpenSSL::PKey::DSA#set_pqg
6daba0
 - OpenSSL::PKey::DSA#set_key
6daba0
 - OpenSSL::PKey::DH#set_pqg
6daba0
 - OpenSSL::PKey::DH#set_key
6daba0
 - OpenSSL::PKey::EC#group=
6daba0
 - OpenSSL::PKey::EC#private_key=
6daba0
 - OpenSSL::PKey::EC#public_key=
6daba0
6daba0
There is no direct replacement for this functionality at the moment.
6daba0
I plan to introduce a wrapper around EVP_PKEY_fromdata(), which takes
6daba0
all key components at once to construct an EVP_PKEY.
6daba0
---
6daba0
 ext/openssl/ossl_pkey.h       | 16 +++++++
6daba0
 ext/openssl/ossl_pkey_ec.c    | 12 +++++
6daba0
 test/openssl/test_pkey_dh.rb  | 38 +++++++++++-----
6daba0
 test/openssl/test_pkey_dsa.rb |  8 +++-
6daba0
 test/openssl/test_pkey_ec.rb  | 58 ++++++++++++++----------
6daba0
 test/openssl/test_pkey_rsa.rb | 85 ++++++++++++++++++++++-------------
6daba0
 6 files changed, 149 insertions(+), 68 deletions(-)
6daba0
6daba0
diff --git a/ext/openssl/ossl_pkey.h b/ext/openssl/ossl_pkey.h
6daba0
index 4beede22..4536e58e 100644
6daba0
--- a/ext/openssl/ossl_pkey.h
6daba0
+++ b/ext/openssl/ossl_pkey.h
6daba0
@@ -116,6 +116,7 @@ static VALUE ossl_##_keytype##_get_##_name(VALUE self)			\
6daba0
 	OSSL_PKEY_BN_DEF_GETTER0(_keytype, _type, a2,			\
6daba0
 		_type##_get0_##_group(obj, NULL, &bn))
6daba0
 
6daba0
+#if !OSSL_OPENSSL_PREREQ(3, 0, 0)
6daba0
 #define OSSL_PKEY_BN_DEF_SETTER3(_keytype, _type, _group, a1, a2, a3)	\
6daba0
 /*									\
6daba0
  *  call-seq:								\
6daba0
@@ -173,6 +174,21 @@ static VALUE ossl_##_keytype##_set_##_group(VALUE self, VALUE v1, VALUE v2) \
6daba0
 	}								\
6daba0
 	return self;							\
6daba0
 }
6daba0
+#else
6daba0
+#define OSSL_PKEY_BN_DEF_SETTER3(_keytype, _type, _group, a1, a2, a3)	\
6daba0
+static VALUE ossl_##_keytype##_set_##_group(VALUE self, VALUE v1, VALUE v2, VALUE v3) \
6daba0
+{									\
6daba0
+        rb_raise(ePKeyError,						\
6daba0
+                 #_keytype"#set_"#_group"= is incompatible with OpenSSL 3.0"); \
6daba0
+}
6daba0
+
6daba0
+#define OSSL_PKEY_BN_DEF_SETTER2(_keytype, _type, _group, a1, a2)	\
6daba0
+static VALUE ossl_##_keytype##_set_##_group(VALUE self, VALUE v1, VALUE v2) \
6daba0
+{									\
6daba0
+        rb_raise(ePKeyError,						\
6daba0
+                 #_keytype"#set_"#_group"= is incompatible with OpenSSL 3.0"); \
6daba0
+}
6daba0
+#endif
6daba0
 
6daba0
 #define OSSL_PKEY_BN_DEF_SETTER_OLD(_keytype, _type, _group, _name)	\
6daba0
 /*									\
6daba0
diff --git a/ext/openssl/ossl_pkey_ec.c b/ext/openssl/ossl_pkey_ec.c
6daba0
index 398a550a..7a6ed1c9 100644
6daba0
--- a/ext/openssl/ossl_pkey_ec.c
6daba0
+++ b/ext/openssl/ossl_pkey_ec.c
6daba0
@@ -251,6 +251,9 @@ ossl_ec_key_get_group(VALUE self)
6daba0
 static VALUE
6daba0
 ossl_ec_key_set_group(VALUE self, VALUE group_v)
6daba0
 {
6daba0
+#if OSSL_OPENSSL_PREREQ(3, 0, 0)
6daba0
+    rb_raise(ePKeyError, "pkeys are immutable on OpenSSL 3.0");
6daba0
+#else
6daba0
     EC_KEY *ec;
6daba0
     EC_GROUP *group;
6daba0
 
6daba0
@@ -261,6 +264,7 @@ ossl_ec_key_set_group(VALUE self, VALUE group_v)
6daba0
         ossl_raise(eECError, "EC_KEY_set_group");
6daba0
 
6daba0
     return group_v;
6daba0
+#endif
6daba0
 }
6daba0
 
6daba0
 /*
6daba0
@@ -289,6 +293,9 @@ static VALUE ossl_ec_key_get_private_key(VALUE self)
6daba0
  */
6daba0
 static VALUE ossl_ec_key_set_private_key(VALUE self, VALUE private_key)
6daba0
 {
6daba0
+#if OSSL_OPENSSL_PREREQ(3, 0, 0)
6daba0
+    rb_raise(ePKeyError, "pkeys are immutable on OpenSSL 3.0");
6daba0
+#else
6daba0
     EC_KEY *ec;
6daba0
     BIGNUM *bn = NULL;
6daba0
 
6daba0
@@ -307,6 +314,7 @@ static VALUE ossl_ec_key_set_private_key(VALUE self, VALUE private_key)
6daba0
     }
6daba0
 
6daba0
     return private_key;
6daba0
+#endif
6daba0
 }
6daba0
 
6daba0
 /*
6daba0
@@ -335,6 +343,9 @@ static VALUE ossl_ec_key_get_public_key(VALUE self)
6daba0
  */
6daba0
 static VALUE ossl_ec_key_set_public_key(VALUE self, VALUE public_key)
6daba0
 {
6daba0
+#if OSSL_OPENSSL_PREREQ(3, 0, 0)
6daba0
+    rb_raise(ePKeyError, "pkeys are immutable on OpenSSL 3.0");
6daba0
+#else
6daba0
     EC_KEY *ec;
6daba0
     EC_POINT *point = NULL;
6daba0
 
6daba0
@@ -353,6 +364,7 @@ static VALUE ossl_ec_key_set_public_key(VALUE self, VALUE public_key)
6daba0
     }
6daba0
 
6daba0
     return public_key;
6daba0
+#endif
6daba0
 }
6daba0
 
6daba0
 /*
6daba0
diff --git a/test/openssl/test_pkey_dh.rb b/test/openssl/test_pkey_dh.rb
6daba0
index ac11af38..161af189 100644
6daba0
--- a/test/openssl/test_pkey_dh.rb
6daba0
+++ b/test/openssl/test_pkey_dh.rb
6daba0
@@ -107,13 +107,32 @@ def test_params_ok?
6daba0
   end
6daba0
 
6daba0
   def test_dup
6daba0
-    dh = Fixtures.pkey("dh1024")
6daba0
-    dh2 = dh.dup
6daba0
-    assert_equal dh.to_der, dh2.to_der # params
6daba0
-    assert_equal_params dh, dh2 # keys
6daba0
-    dh2.set_pqg(dh2.p + 1, nil, dh2.g)
6daba0
-    assert_not_equal dh2.p, dh.p
6daba0
-    assert_equal dh2.g, dh.g
6daba0
+    # Parameters only
6daba0
+    dh1 = Fixtures.pkey("dh1024")
6daba0
+    dh2 = dh1.dup
6daba0
+    assert_equal dh1.to_der, dh2.to_der
6daba0
+    assert_not_equal nil, dh1.p
6daba0
+    assert_not_equal nil, dh1.g
6daba0
+    assert_equal [dh1.p, dh1.g], [dh2.p, dh2.g]
6daba0
+    assert_equal nil, dh1.pub_key
6daba0
+    assert_equal nil, dh1.priv_key
6daba0
+    assert_equal [dh1.pub_key, dh1.priv_key], [dh2.pub_key, dh2.priv_key]
6daba0
+
6daba0
+    # PKey is immutable in OpenSSL >= 3.0
6daba0
+    if !openssl?(3, 0, 0)
6daba0
+      dh2.set_pqg(dh2.p + 1, nil, dh2.g)
6daba0
+      assert_not_equal dh2.p, dh1.p
6daba0
+    end
6daba0
+
6daba0
+    # With a key pair
6daba0
+    dh3 = OpenSSL::PKey.generate_key(Fixtures.pkey("dh1024"))
6daba0
+    dh4 = dh3.dup
6daba0
+    assert_equal dh3.to_der, dh4.to_der
6daba0
+    assert_equal dh1.to_der, dh4.to_der # encodes parameters only
6daba0
+    assert_equal [dh1.p, dh1.g], [dh4.p, dh4.g]
6daba0
+    assert_not_equal nil, dh3.pub_key
6daba0
+    assert_not_equal nil, dh3.priv_key
6daba0
+    assert_equal [dh3.pub_key, dh3.priv_key], [dh4.pub_key, dh4.priv_key]
6daba0
   end
6daba0
 
6daba0
   def test_marshal
6daba0
@@ -125,11 +144,6 @@ def test_marshal
6daba0
 
6daba0
   private
6daba0
 
6daba0
-  def assert_equal_params(dh1, dh2)
6daba0
-    assert_equal(dh1.g, dh2.g)
6daba0
-    assert_equal(dh1.p, dh2.p)
6daba0
-  end
6daba0
-
6daba0
   def assert_no_key(dh)
6daba0
     assert_equal(false, dh.public?)
6daba0
     assert_equal(false, dh.private?)
6daba0
diff --git a/test/openssl/test_pkey_dsa.rb b/test/openssl/test_pkey_dsa.rb
6daba0
index 0994607f..726b7dbf 100644
6daba0
--- a/test/openssl/test_pkey_dsa.rb
6daba0
+++ b/test/openssl/test_pkey_dsa.rb
6daba0
@@ -208,8 +208,12 @@ def test_dup
6daba0
     key = Fixtures.pkey("dsa1024")
6daba0
     key2 = key.dup
6daba0
     assert_equal key.params, key2.params
6daba0
-    key2.set_pqg(key2.p + 1, key2.q, key2.g)
6daba0
-    assert_not_equal key.params, key2.params
6daba0
+
6daba0
+    # PKey is immutable in OpenSSL >= 3.0
6daba0
+    if !openssl?(3, 0, 0)
6daba0
+      key2.set_pqg(key2.p + 1, key2.q, key2.g)
6daba0
+      assert_not_equal key.params, key2.params
6daba0
+    end
6daba0
   end
6daba0
 
6daba0
   def test_marshal
6daba0
diff --git a/test/openssl/test_pkey_ec.rb b/test/openssl/test_pkey_ec.rb
6daba0
index 33f78a4c..ffe5a94e 100644
6daba0
--- a/test/openssl/test_pkey_ec.rb
6daba0
+++ b/test/openssl/test_pkey_ec.rb
6daba0
@@ -21,11 +21,15 @@ def test_ec_key
6daba0
 
6daba0
     key1 = OpenSSL::PKey::EC.generate("prime256v1")
6daba0
 
6daba0
-    key2 = OpenSSL::PKey::EC.new
6daba0
-    key2.group = key1.group
6daba0
-    key2.private_key = key1.private_key
6daba0
-    key2.public_key = key1.public_key
6daba0
-    assert_equal key1.to_der, key2.to_der
6daba0
+    # PKey is immutable in OpenSSL >= 3.0; constructing an empty EC object is
6daba0
+    # deprecated
6daba0
+    if !openssl?(3, 0, 0)
6daba0
+      key2 = OpenSSL::PKey::EC.new
6daba0
+      key2.group = key1.group
6daba0
+      key2.private_key = key1.private_key
6daba0
+      key2.public_key = key1.public_key
6daba0
+      assert_equal key1.to_der, key2.to_der
6daba0
+    end
6daba0
 
6daba0
     key3 = OpenSSL::PKey::EC.new(key1)
6daba0
     assert_equal key1.to_der, key3.to_der
6daba0
@@ -35,10 +39,14 @@ def test_ec_key
6daba0
 
6daba0
     key5 = key1.dup
6daba0
     assert_equal key1.to_der, key5.to_der
6daba0
-    key_tmp = OpenSSL::PKey::EC.new("prime256v1").generate_key!
6daba0
-    key5.private_key = key_tmp.private_key
6daba0
-    key5.public_key = key_tmp.public_key
6daba0
-    assert_not_equal key1.to_der, key5.to_der
6daba0
+
6daba0
+    # PKey is immutable in OpenSSL >= 3.0; EC object should not be modified
6daba0
+    if !openssl?(3, 0, 0)
6daba0
+      key_tmp = OpenSSL::PKey::EC.generate("prime256v1")
6daba0
+      key5.private_key = key_tmp.private_key
6daba0
+      key5.public_key = key_tmp.public_key
6daba0
+      assert_not_equal key1.to_der, key5.to_der
6daba0
+    end
6daba0
   end
6daba0
 
6daba0
   def test_generate
6daba0
@@ -65,22 +73,26 @@ def test_marshal
6daba0
   end
6daba0
 
6daba0
   def test_check_key
6daba0
-    key = OpenSSL::PKey::EC.new("prime256v1").generate_key!
6daba0
-    assert_equal(true, key.check_key)
6daba0
-    assert_equal(true, key.private?)
6daba0
-    assert_equal(true, key.public?)
6daba0
-    key2 = OpenSSL::PKey::EC.new(key.group)
6daba0
-    assert_equal(false, key2.private?)
6daba0
-    assert_equal(false, key2.public?)
6daba0
-    key2.public_key = key.public_key
6daba0
-    assert_equal(false, key2.private?)
6daba0
-    assert_equal(true, key2.public?)
6daba0
-    key2.private_key = key.private_key
6daba0
+    key0 = Fixtures.pkey("p256")
6daba0
+    assert_equal(true, key0.check_key)
6daba0
+    assert_equal(true, key0.private?)
6daba0
+    assert_equal(true, key0.public?)
6daba0
+
6daba0
+    key1 = OpenSSL::PKey.read(key0.public_to_der)
6daba0
+    assert_equal(true, key1.check_key)
6daba0
+    assert_equal(false, key1.private?)
6daba0
+    assert_equal(true, key1.public?)
6daba0
+
6daba0
+    key2 = OpenSSL::PKey.read(key0.private_to_der)
6daba0
     assert_equal(true, key2.private?)
6daba0
     assert_equal(true, key2.public?)
6daba0
     assert_equal(true, key2.check_key)
6daba0
-    key2.private_key += 1
6daba0
-    assert_raise(OpenSSL::PKey::ECError) { key2.check_key }
6daba0
+
6daba0
+    # EC#private_key= is deprecated in 3.0 and won't work on OpenSSL 3.0
6daba0
+    if !openssl?(3, 0, 0)
6daba0
+      key2.private_key += 1
6daba0
+      assert_raise(OpenSSL::PKey::ECError) { key2.check_key }
6daba0
+    end
6daba0
   end
6daba0
 
6daba0
   def test_sign_verify
6daba0
@@ -112,7 +124,7 @@ def test_derive_key
6daba0
     assert_equal [zIUT].pack("H*"), a.derive(b)
6daba0
 
6daba0
     assert_equal a.derive(b), a.dh_compute_key(b.public_key)
6daba0
-  end
6daba0
+  end if !openssl?(3, 0, 0) # TODO: Test it without using #private_key=
6daba0
 
6daba0
   def test_sign_verify_raw
6daba0
     key = Fixtures.pkey("p256")
6daba0
diff --git a/test/openssl/test_pkey_rsa.rb b/test/openssl/test_pkey_rsa.rb
6daba0
index dbe87ba4..1c7f9ccf 100644
6daba0
--- a/test/openssl/test_pkey_rsa.rb
6daba0
+++ b/test/openssl/test_pkey_rsa.rb
6daba0
@@ -31,15 +31,18 @@ def test_private
6daba0
     assert(!key4.private?)
6daba0
     rsa1024 = Fixtures.pkey("rsa1024")
6daba0
 
6daba0
-    # Generated by RSA#set_key
6daba0
-    key5 = OpenSSL::PKey::RSA.new
6daba0
-    key5.set_key(rsa1024.n, rsa1024.e, rsa1024.d)
6daba0
-    assert(key5.private?)
6daba0
-
6daba0
-    # Generated by RSA#set_key, without d
6daba0
-    key6 = OpenSSL::PKey::RSA.new
6daba0
-    key6.set_key(rsa1024.n, rsa1024.e, nil)
6daba0
-    assert(!key6.private?)
6daba0
+    if !openssl?(3, 0, 0)
6daba0
+      key = OpenSSL::PKey::RSA.new
6daba0
+      # Generated by RSA#set_key
6daba0
+      key5 = OpenSSL::PKey::RSA.new
6daba0
+      key5.set_key(rsa1024.n, rsa1024.e, rsa1024.d)
6daba0
+      assert(key5.private?)
6daba0
+
6daba0
+      # Generated by RSA#set_key, without d
6daba0
+      key6 = OpenSSL::PKey::RSA.new
6daba0
+      key6.set_key(rsa1024.n, rsa1024.e, nil)
6daba0
+      assert(!key6.private?)
6daba0
+    end
6daba0
   end
6daba0
 
6daba0
   def test_new
6daba0
@@ -235,36 +238,52 @@ def test_encrypt_decrypt_legacy
6daba0
 
6daba0
   def test_export
6daba0
     rsa1024 = Fixtures.pkey("rsa1024")
6daba0
-    key = OpenSSL::PKey::RSA.new
6daba0
 
6daba0
-    # key has only n, e and d
6daba0
-    key.set_key(rsa1024.n, rsa1024.e, rsa1024.d)
6daba0
-    assert_equal rsa1024.public_key.export, key.export
6daba0
+    pub = OpenSSL::PKey.read(rsa1024.public_to_der)
6daba0
+    assert_not_equal rsa1024.export, pub.export
6daba0
+    assert_equal rsa1024.public_to_pem, pub.export
6daba0
+
6daba0
+    # PKey is immutable in OpenSSL >= 3.0
6daba0
+    if !openssl?(3, 0, 0)
6daba0
+      key = OpenSSL::PKey::RSA.new
6daba0
 
6daba0
-    # key has only n, e, d, p and q
6daba0
-    key.set_factors(rsa1024.p, rsa1024.q)
6daba0
-    assert_equal rsa1024.public_key.export, key.export
6daba0
+      # key has only n, e and d
6daba0
+      key.set_key(rsa1024.n, rsa1024.e, rsa1024.d)
6daba0
+      assert_equal rsa1024.public_key.export, key.export
6daba0
 
6daba0
-    # key has n, e, d, p, q, dmp1, dmq1 and iqmp
6daba0
-    key.set_crt_params(rsa1024.dmp1, rsa1024.dmq1, rsa1024.iqmp)
6daba0
-    assert_equal rsa1024.export, key.export
6daba0
+      # key has only n, e, d, p and q
6daba0
+      key.set_factors(rsa1024.p, rsa1024.q)
6daba0
+      assert_equal rsa1024.public_key.export, key.export
6daba0
+
6daba0
+      # key has n, e, d, p, q, dmp1, dmq1 and iqmp
6daba0
+      key.set_crt_params(rsa1024.dmp1, rsa1024.dmq1, rsa1024.iqmp)
6daba0
+      assert_equal rsa1024.export, key.export
6daba0
+    end
6daba0
   end
6daba0
 
6daba0
   def test_to_der
6daba0
     rsa1024 = Fixtures.pkey("rsa1024")
6daba0
-    key = OpenSSL::PKey::RSA.new
6daba0
 
6daba0
-    # key has only n, e and d
6daba0
-    key.set_key(rsa1024.n, rsa1024.e, rsa1024.d)
6daba0
-    assert_equal rsa1024.public_key.to_der, key.to_der
6daba0
+    pub = OpenSSL::PKey.read(rsa1024.public_to_der)
6daba0
+    assert_not_equal rsa1024.to_der, pub.to_der
6daba0
+    assert_equal rsa1024.public_to_der, pub.to_der
6daba0
 
6daba0
-    # key has only n, e, d, p and q
6daba0
-    key.set_factors(rsa1024.p, rsa1024.q)
6daba0
-    assert_equal rsa1024.public_key.to_der, key.to_der
6daba0
+    # PKey is immutable in OpenSSL >= 3.0
6daba0
+    if !openssl?(3, 0, 0)
6daba0
+      key = OpenSSL::PKey::RSA.new
6daba0
 
6daba0
-    # key has n, e, d, p, q, dmp1, dmq1 and iqmp
6daba0
-    key.set_crt_params(rsa1024.dmp1, rsa1024.dmq1, rsa1024.iqmp)
6daba0
-    assert_equal rsa1024.to_der, key.to_der
6daba0
+      # key has only n, e and d
6daba0
+      key.set_key(rsa1024.n, rsa1024.e, rsa1024.d)
6daba0
+      assert_equal rsa1024.public_key.to_der, key.to_der
6daba0
+
6daba0
+      # key has only n, e, d, p and q
6daba0
+      key.set_factors(rsa1024.p, rsa1024.q)
6daba0
+      assert_equal rsa1024.public_key.to_der, key.to_der
6daba0
+
6daba0
+      # key has n, e, d, p, q, dmp1, dmq1 and iqmp
6daba0
+      key.set_crt_params(rsa1024.dmp1, rsa1024.dmq1, rsa1024.iqmp)
6daba0
+      assert_equal rsa1024.to_der, key.to_der
6daba0
+    end
6daba0
   end
6daba0
 
6daba0
   def test_RSAPrivateKey
6daba0
@@ -501,8 +520,12 @@ def test_dup
6daba0
     key = Fixtures.pkey("rsa1024")
6daba0
     key2 = key.dup
6daba0
     assert_equal key.params, key2.params
6daba0
-    key2.set_key(key2.n, 3, key2.d)
6daba0
-    assert_not_equal key.params, key2.params
6daba0
+
6daba0
+    # PKey is immutable in OpenSSL >= 3.0
6daba0
+    if !openssl?(3, 0, 0)
6daba0
+      key2.set_key(key2.n, 3, key2.d)
6daba0
+      assert_not_equal key.params, key2.params
6daba0
+    end
6daba0
   end
6daba0
 
6daba0
   def test_marshal