Skip to content

Commit 7bafbb5

Browse files
committed
[feat] implement OpenSSL::X509::Request#signature_algorithm
1 parent ab5d342 commit 7bafbb5

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
4545
import org.bouncycastle.asn1.pkcs.Attribute;
4646
import org.bouncycastle.asn1.x500.X500Name;
47+
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
48+
import org.bouncycastle.operator.DefaultSignatureNameFinder;
4749

4850
import org.jruby.Ruby;
4951
import org.jruby.RubyArray;
@@ -53,6 +55,7 @@
5355
import org.jruby.RubyString;
5456
import org.jruby.anno.JRubyMethod;
5557
import org.jruby.exceptions.RaiseException;
58+
import org.jruby.ext.openssl.impl.ASN1Registry;
5659
import org.jruby.ext.openssl.x509store.PEMInputOutput;
5760
import org.jruby.runtime.Arity;
5861
import org.jruby.runtime.ThreadContext;
@@ -91,7 +94,7 @@ static RubyClass _RequestError(final Ruby runtime) {
9194

9295
public X509Request(Ruby runtime, RubyClass type) {
9396
super(runtime, type);
94-
attributes = new ArrayList<X509Attribute>(4);
97+
attributes = new ArrayList<>(4);
9598
}
9699

97100
@JRubyMethod(name = "initialize", rest = true, visibility = Visibility.PRIVATE)
@@ -286,8 +289,10 @@ public IRubyObject set_subject(final IRubyObject val) {
286289

287290
@JRubyMethod
288291
public IRubyObject signature_algorithm(final ThreadContext context) {
289-
warn(context, "WARNING: unimplemented method called: OpenSSL::X509::Request#signature_algorithm");
290-
return context.runtime.getNil();
292+
AlgorithmIdentifier signatureAlgId = request == null ? null : request.getSignatureAlgorithm();
293+
if (signatureAlgId == null) return context.runtime.newString("NULL");
294+
final String name = ASN1Registry.o2a(signatureAlgId.getAlgorithm());
295+
return context.runtime.newString(name == null ? "" : name);
291296
}
292297

293298
@JRubyMethod
@@ -316,7 +321,8 @@ public IRubyObject sign(final ThreadContext context,
316321

317322
final String digAlg = ((Digest) digest).getShortAlgorithm();
318323
try {
319-
request = null; getRequest().sign( privateKey, digAlg );
324+
request = null;
325+
getRequest().sign( privateKey, digAlg );
320326
}
321327
catch (InvalidKeyException e) {
322328
debug(runtime, "X509Request#sign invalid key:", e);
@@ -330,15 +336,14 @@ public IRubyObject sign(final ThreadContext context,
330336
}
331337

332338
private List<Attribute> newAttributesImpl(final ThreadContext context) {
333-
ArrayList<Attribute> attrs = new ArrayList<Attribute>(attributes.size());
339+
ArrayList<Attribute> attrs = new ArrayList<>(attributes.size());
334340
for ( X509Attribute attribute : attributes ) {
335341
attrs.add( newAttributeImpl(context, attribute) );
336342
}
337343
return attrs;
338344
}
339345

340-
private Attribute newAttributeImpl(final ThreadContext context,
341-
final X509Attribute attribute) {
346+
private static Attribute newAttributeImpl(final ThreadContext context, final X509Attribute attribute) {
342347
return Attribute.getInstance( attribute.toASN1( context ) );
343348
}
344349

@@ -372,7 +377,7 @@ public IRubyObject attributes() {
372377
return getRuntime().newArray(attributes);
373378
}
374379

375-
@JRubyMethod(name="attributes=")
380+
@JRubyMethod(name = "attributes=")
376381
public IRubyObject set_attributes(final ThreadContext context,final IRubyObject attributes) {
377382
this.attributes.clear();
378383
final RubyArray attrs = (RubyArray) attributes;

src/main/java/org/jruby/ext/openssl/impl/PKCS10Request.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,14 @@ private void resetSignedRequest() {
126126

127127
// sign
128128

129-
public PKCS10CertificationRequest sign(final PrivateKey privateKey,
130-
final AlgorithmIdentifier signatureAlg)
129+
public PKCS10CertificationRequest sign(final PrivateKey privateKey, final AlgorithmIdentifier signatureAlg)
131130
throws NoSuchAlgorithmException, InvalidKeyException {
132131
final ContentSigner signer = new PKCS10Signer(privateKey, signatureAlg);
133132
signedRequest = newBuilder().build(signer); // valid = true;
134133
return signedRequest;
135134
}
136135

137-
public PKCS10CertificationRequest sign(final PrivateKey privateKey,
138-
final String digestAlg)
136+
public PKCS10CertificationRequest sign(final PrivateKey privateKey, final String digestAlg)
139137
throws NoSuchAlgorithmException, InvalidKeyException {
140138
String sigAlg = digestAlg + "WITH" + getPublicKeyAlgorithm();
141139
return sign(privateKey, new DefaultSignatureAlgorithmIdentifierFinder().find(sigAlg));
@@ -296,6 +294,10 @@ public BigInteger getVersion() {
296294
getVersion().getValue();
297295
}
298296

297+
public AlgorithmIdentifier getSignatureAlgorithm() {
298+
if ( signedRequest == null ) return null;
299+
return signedRequest.getSignatureAlgorithm();
300+
}
299301

300302
private static class PKCS10Signer implements ContentSigner {
301303

src/test/ruby/x509/test_x509req.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ def test_csr_request_extensions
1818

1919
csr.sign(key, OpenSSL::Digest::SHA256.new)
2020

21+
assert_equal 'sha256WithRSAEncryption', csr.signature_algorithm
22+
2123
# The combination of the extreq and the stringification / revivification
2224
# is what triggers the bad behaviour in the extension. (Any extended
2325
# request type should do, but this matches my observed problems)
@@ -35,8 +37,13 @@ def test_csr_request_ec_key
3537
csr.public_key = key
3638
csr.subject = OpenSSL::X509::Name.new([['CN', 'foo.bar.cat', OpenSSL::ASN1::UTF8STRING]])
3739
csr.version = 2
40+
41+
assert_equal 'NULL', csr.signature_algorithm
42+
3843
csr.sign key, OpenSSL::Digest::SHA256.new # does not raise
3944

45+
assert_equal 'ecdsa-with-SHA256', csr.signature_algorithm
46+
4047
assert_true csr.verify(key)
4148
end
4249

0 commit comments

Comments
 (0)