Skip to content

Commit d54c751

Browse files
committed
pkey/dh: avoid DH#set_key in DH#compute_key
DH#set_key will not work on OpenSSL 3.0 because keys are immutable. For now, let's reimplement DH#compute_key by manually constructing a DER-encoded SubjectPublicKeyInfo structure and feeding it to OpenSSL::PKey.read. Eventually we should implement a new method around EVP_PKEY_fromdata() and use it instead.
1 parent c122961 commit d54c751

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

lib/openssl/pkey.rb

+13-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,19 @@ def public_key
4747
# * _pub_bn_ is a OpenSSL::BN, *not* the DH instance returned by
4848
# DH#public_key as that contains the DH parameters only.
4949
def compute_key(pub_bn)
50-
peer = dup
51-
peer.set_key(pub_bn, nil)
52-
derive(peer)
50+
# FIXME: This is constructing an X.509 SubjectPublicKeyInfo and is very
51+
# inefficient
52+
obj = OpenSSL::ASN1.Sequence([
53+
OpenSSL::ASN1.Sequence([
54+
OpenSSL::ASN1.ObjectId("dhKeyAgreement"),
55+
OpenSSL::ASN1.Sequence([
56+
OpenSSL::ASN1.Integer(p),
57+
OpenSSL::ASN1.Integer(g),
58+
]),
59+
]),
60+
OpenSSL::ASN1.BitString(OpenSSL::ASN1.Integer(pub_bn).to_der),
61+
])
62+
derive(OpenSSL::PKey.read(obj.to_der))
5363
end
5464

5565
# :call-seq:

0 commit comments

Comments
 (0)