Skip to content

Commit 781b155

Browse files
committed
signature algorithm should be read as well when decoding certificate (fixes #39)
1 parent 7af1a89 commit 781b155

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,11 @@ static String oid2name(final Ruby runtime, final ASN1ObjectIdentifier oid, final
514514
return nid2ln(runtime, nid, true); */
515515
}
516516

517+
518+
static String oid2name(final Ruby runtime, final String oid) {
519+
return oid2name(runtime, new ASN1ObjectIdentifier(oid), false);
520+
}
521+
517522
static String nid2sn(final Ruby runtime, final Integer nid) {
518523
return nid2sn(runtime, nid, true);
519524
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ private void initialize(final ThreadContext context, final byte[] encoded, final
210210
this.subject = X509Name.newName(runtime, cert.getSubjectX500Principal());
211211
this.issuer = X509Name.newName(runtime, cert.getIssuerX500Principal());
212212
this.version = RubyFixnum.newFixnum(runtime, cert.getVersion() - 1);
213+
String sigAlgorithm = cert.getSigAlgOID();
214+
if ( sigAlgorithm == null ) sigAlgorithm = cert.getSigAlgName(); // e.g. SHA256withRSA
215+
else sigAlgorithm = ASN1.oid2name(runtime, sigAlgorithm); // "hot" path e.g. sha256WithRSAEncryption
216+
this.sig_alg = RubyString.newString(runtime, sigAlgorithm);
213217

214218
final Set<String> criticalExtOIDs = cert.getCriticalExtensionOIDs();
215219
if ( criticalExtOIDs != null ) {

src/test/ruby/x509/test_x509cert.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ def test_inspect_to_text
227227

228228
cert.to_text
229229
assert_equal 2, cert.version
230+
assert_equal 'sha1WithRSAEncryption', cert.signature_algorithm
230231

231232
unless defined? JRUBY_VERSION # TODO "/DC=org,/DC=ruby-lang,/CN=TestCA"
232233
assert_equal text_without_signature, cert.to_text[0, text_without_signature.size]
@@ -269,6 +270,9 @@ def test_to_text_regression
269270
-----END CERTIFICATE-----
270271
EOF
271272
assert_equal 0, cert.version
273+
assert_equal 'sha256WithRSAEncryption', cert.signature_algorithm
274+
assert cert.to_text.index('Version: 1 (0x0)')
275+
assert cert.to_text.index('Signature Algorithm: sha256WithRSAEncryption')
272276
end
273277

274278
TEST_KEY_RSA1024 = <<-_end_of_pem_

0 commit comments

Comments
 (0)