Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support openssl 3 (Fixes: #100) #101

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 45 additions & 19 deletions lib/json/jwk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,29 @@ def to_rsa_key
OpenSSL::BN.new Base64.urlsafe_decode64(self[key]), 2
end
end
key = OpenSSL::PKey::RSA.new
if key.respond_to? :set_key
key.set_key n, e, d
key.set_factors p, q if p && q
key.set_crt_params dp, dq, qi if dp && dq && qi
else
key.e = e
key.n = n
key.d = d if d
key.p = p if p
key.q = q if q
key.dmp1 = dp if dp
key.dmq1 = dq if dq
key.iqmp = qi if qi

# Public key
data_sequence = OpenSSL::ASN1::Sequence([
OpenSSL::ASN1::Integer(n),
OpenSSL::ASN1::Integer(e),
])

if d && p && q && dp && dq && qi
data_sequence = OpenSSL::ASN1::Sequence([
OpenSSL::ASN1::Integer(0),
OpenSSL::ASN1::Integer(n),
OpenSSL::ASN1::Integer(e),
OpenSSL::ASN1::Integer(d),
OpenSSL::ASN1::Integer(p),
OpenSSL::ASN1::Integer(q),
OpenSSL::ASN1::Integer(dp),
OpenSSL::ASN1::Integer(dq),
OpenSSL::ASN1::Integer(qi),
])
end
key

asn1 = OpenSSL::ASN1::Sequence(data_sequence)
OpenSSL::PKey::RSA.new(asn1.to_der)
end

def to_ec_key
Expand All @@ -137,13 +144,32 @@ def to_ec_key
Base64.urlsafe_decode64(self[key])
end
end
key = OpenSSL::PKey::EC.new curve_name
key.private_key = OpenSSL::BN.new(d, 2) if d
key.public_key = OpenSSL::PKey::EC::Point.new(

point = OpenSSL::PKey::EC::Point.new(
OpenSSL::PKey::EC::Group.new(curve_name),
OpenSSL::BN.new(['04' + x.unpack('H*').first + y.unpack('H*').first].pack('H*'), 2)
)
key

# Public key
data_sequence = OpenSSL::ASN1::Sequence([
OpenSSL::ASN1::Sequence([
OpenSSL::ASN1::ObjectId("id-ecPublicKey"),
OpenSSL::ASN1::ObjectId(curve_name)
]),
OpenSSL::ASN1::BitString(point.to_octet_string(:uncompressed))
])

if d
# Private key
data_sequence = OpenSSL::ASN1::Sequence([
OpenSSL::ASN1::Integer(1),
OpenSSL::ASN1::OctetString(OpenSSL::BN.new(d, 2).to_s(2)),
OpenSSL::ASN1::ObjectId(curve_name, 0, :EXPLICIT),
OpenSSL::ASN1::BitString(point.to_octet_string(:uncompressed), 1, :EXPLICIT)
])
end

OpenSSL::PKey::EC.new(data_sequence.to_der)
end
end
end
4 changes: 2 additions & 2 deletions lib/json/jws.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ def verify_ecdsa_group!(key)
when 512
:secp521r1
end
key.group = OpenSSL::PKey::EC::Group.new group_name.to_s
key.check_key
newkey = OpenSSL::PKey::EC.generate(group_name.to_s)
newkey.check_key
end

def raw_to_asn1(signature, public_key)
Expand Down
4 changes: 2 additions & 2 deletions spec/json/jwk_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@

describe 'unknown curve' do
it do
key = OpenSSL::PKey::EC.new('secp112r2').generate_key
key = OpenSSL::PKey::EC.generate('secp112r2')
expect do
JSON::JWK.new key
end.to raise_error JSON::JWK::UnknownAlgorithm, 'Unknown EC Curve'
Expand Down Expand Up @@ -193,7 +193,7 @@

describe 'unknown key type' do
it do
key = OpenSSL::PKey::DSA.generate 256
key = OpenSSL::PKey::DSA.generate 2048
expect do
JSON::JWK.new key
end.to raise_error JSON::JWK::UnknownAlgorithm, 'Unknown Key Type'
Expand Down