Skip to content

Commit 06443d1

Browse files
committed
[fix] SPKI#sign NPE due missing ASN1 registry fallback
1 parent b81798e commit 06443d1

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

src/main/java/org/jruby/ext/openssl/NetscapeSPKI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public IRubyObject sign(final IRubyObject key, final IRubyObject digest) {
239239
final String digAlg = ((Digest) digest).getShortAlgorithm();
240240
final String symKey = keyAlg.toLowerCase() + '-' + digAlg.toLowerCase();
241241
try {
242-
final ASN1ObjectIdentifier alg = ASN1.sym2Oid( getRuntime(), symKey );
242+
final ASN1ObjectIdentifier alg = ASN1.getObjectID( getRuntime(), symKey );
243243
final PublicKey publicKey = ( (PKey) this.public_key ).getPublicKey();
244244
final String challengeStr = challenge.toString();
245245
final NetscapeCertRequest cert;

src/test/ruby/test_ns_spki.rb

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,28 @@ class TestNSSPKI < TestCase
88
'r/7iJNroWlSzSMtTiQTEB+ADWHGj9u1xrUrOilq/o2cuQxIfZcNZkYAkWP4DubqW' \
99
'i0//rgBvmco='
1010

11+
def test_build_data
12+
key1 = Fixtures.pkey('rsa1024')
13+
key2 = Fixtures.pkey('rsa2048')
14+
spki = OpenSSL::Netscape::SPKI.new
15+
spki.challenge = 'RandomChallenge'
16+
spki.public_key = key1
17+
spki.sign(key1, OpenSSL::Digest.new('SHA256'))
18+
assert spki.verify(key1)
19+
assert !spki.verify(key2)
20+
assert_not_nil spki.to_text
21+
assert_not_nil spki.to_der
22+
end
23+
1124
def test_decode_data
1225
spki = OpenSSL::Netscape::SPKI.new(B64)
1326
assert_equal 'MozillaIsMyFriend', spki.challenge
1427
assert_instance_of OpenSSL::PKey::RSA, spki.public_key
28+
29+
# also accepts DER input
30+
spki = OpenSSL::Netscape::SPKI.new(B64.unpack1('m'))
31+
assert_equal 'MozillaIsMyFriend', spki.challenge
32+
assert_instance_of OpenSSL::PKey::RSA, spki.public_key
1533
end
1634

1735
def test_to_text
@@ -23,7 +41,20 @@ def test_to_text
2341
assert_match(/Public Key Algorithm: /, text)
2442
assert_match(/Challenge String: MozillaIsMyFriend/, text)
2543
assert_match(/Signature Algorithm: /, text)
26-
# signature hex bytes with : separators
44+
assert_match(/[0-9a-f]{2}(:[0-9a-f]{2})+/, text)
45+
end
46+
47+
def test_to_text_after_sign
48+
key = Fixtures.pkey('rsa1024')
49+
spki = OpenSSL::Netscape::SPKI.new
50+
spki.challenge = 'MyChallenge'
51+
spki.public_key = key
52+
spki.sign(key, OpenSSL::Digest.new('SHA256'))
53+
54+
text = spki.to_text
55+
assert_match(/\ANetscape SPKI:\n/, text)
56+
assert_match(/Challenge String: MyChallenge/, text)
57+
assert_match(/Signature Algorithm: /, text)
2758
assert_match(/[0-9a-f]{2}(:[0-9a-f]{2})+/, text)
2859
end
2960
end

0 commit comments

Comments
 (0)