Skip to content

Commit 629db48

Browse files
committed
added some debugging into Cert's verify + housekeeping
1 parent 1cc27ff commit 629db48

File tree

2 files changed

+32
-32
lines changed

2 files changed

+32
-32
lines changed

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

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@
8989
import static org.jruby.ext.openssl.X509CRL.extensions_to_text;
9090
import static org.jruby.ext.openssl.StringHelper.appendGMTDateTime;
9191
import static org.jruby.ext.openssl.StringHelper.appendLowerHexValue;
92-
import static org.jruby.ext.openssl.StringHelper.gsub;
9392
import static org.jruby.ext.openssl.StringHelper.lowerHexBytes;
93+
import static org.jruby.ext.openssl.OpenSSL.debug;
94+
import static org.jruby.ext.openssl.OpenSSL.debugStackTrace;
9495

9596
/**
9697
* @author <a href="mailto:[email protected]">Ola Bini</a>
@@ -412,7 +413,6 @@ public IRubyObject subject() {
412413
@JRubyMethod(name = "subject=")
413414
public IRubyObject set_subject(final IRubyObject subject) {
414415
if ( ! subject.equals(this.subject) ) this.changed = true;
415-
//generator.setSubjectDN( ((X509Name) subject).getRealName() );
416416
return this.subject = subject;
417417
}
418418

@@ -424,7 +424,6 @@ public IRubyObject issuer() {
424424
@JRubyMethod(name = "issuer=")
425425
public IRubyObject set_issuer(final IRubyObject issuer) {
426426
if ( ! issuer.equals(this.issuer) ) this.changed = true;
427-
//generator.setIssuerDN( ((X509Name) issuer).getRealName() );
428427
return this.issuer = issuer;
429428
}
430429

@@ -438,7 +437,6 @@ public IRubyObject set_not_before(final ThreadContext context, final IRubyObject
438437
changed = true;
439438
not_before = (RubyTime) time.callMethod(context, "getutc");
440439
not_before.setMicroseconds(0);
441-
//generator.setNotBefore( not_before.getJavaDate() );
442440
return time;
443441
}
444442

@@ -456,7 +454,6 @@ public IRubyObject set_not_after(final ThreadContext context, final IRubyObject
456454
changed = true;
457455
not_after = (RubyTime) time.callMethod(context, "getutc");
458456
not_after.setMicroseconds(0);
459-
//generator.setNotAfter( not_after.getJavaDate() );
460457
return time;
461458
}
462459

@@ -480,7 +477,6 @@ public IRubyObject set_public_key(IRubyObject public_key) {
480477
if ( ! public_key.equals(this.public_key) ) {
481478
this.changed = true;
482479
}
483-
//generator.setPublicKey(((PKey) public_key).getPublicKey());
484480
return this.public_key = (PKey) public_key;
485481
}
486482

@@ -547,21 +543,19 @@ public IRubyObject sign(final ThreadContext context, final IRubyObject key, fina
547543
}
548544
}
549545

550-
builder.setSignatureAlgorithm(digAlg + "WITH" + keyAlg);
546+
builder.setSignatureAlgorithm(digAlg + "WITH" + keyAlg); // "SHA1WITHRSA"
551547

552548
try {
553549
cert = builder.generate( ((PKey) key).getPrivateKey() );
554550
}
555551
catch (GeneralSecurityException e) {
556-
throw newCertificateError(getRuntime(), e);
557-
}
558-
if (cert == null) {
559-
throw newCertificateError(runtime, (String) null);
552+
throw newCertificateError(runtime, e);
560553
}
554+
555+
if (cert == null) throw newCertificateError(runtime, (String) null);
556+
561557
String name = ASN1Registry.o2a(cert.getSigAlgOID());
562-
if (name == null) {
563-
name = cert.getSigAlgOID();
564-
}
558+
if ( name == null ) name = cert.getSigAlgOID();
565559
this.sig_alg = runtime.newString(name);
566560
this.changed = false;
567561
return this;
@@ -587,36 +581,43 @@ private org.bouncycastle.x509.X509V3CertificateGenerator getCertificateBuilder()
587581
//private transient org.bouncycastle.x509.X509V3CertificateGenerator generator;
588582

589583
@JRubyMethod
590-
public IRubyObject verify(IRubyObject key) {
591-
if ( changed ) return getRuntime().getFalse();
584+
public RubyBoolean verify(final IRubyObject key) {
585+
final Ruby runtime = getRuntime();
586+
587+
if ( changed ) return runtime.getFalse();
592588

593589
try {
594-
cert.verify(((PKey)key).getPublicKey());
595-
return getRuntime().getTrue();
590+
cert.verify(((PKey) key).getPublicKey());
591+
return runtime.getTrue();
596592
}
597593
catch (CertificateException e) {
598-
throw newCertificateError(getRuntime(), e);
594+
debug(runtime, "Certificate#verify failed: ", e);
595+
throw newCertificateError(runtime, e);
599596
}
600597
catch (NoSuchAlgorithmException e) {
601-
throw newCertificateError(getRuntime(), e);
598+
debugStackTrace(runtime, e);
599+
throw newCertificateError(runtime, e);
602600
}
603601
catch (NoSuchProviderException e) {
604-
throw newCertificateError(getRuntime(), e);
602+
debugStackTrace(runtime, e);
603+
throw newCertificateError(runtime, e);
605604
}
606605
catch (SignatureException e) {
607-
return getRuntime().getFalse();
606+
debug(runtime, "Certificate#verify failed: ", e);
607+
return runtime.getFalse();
608608
}
609609
catch (InvalidKeyException e) {
610-
return getRuntime().getFalse();
610+
debug(runtime, "Certificate#verify failed: ", e);
611+
return runtime.getFalse();
611612
}
612613
}
613614

614615
@JRubyMethod
615-
public RubyBoolean check_private_key(IRubyObject arg) {
616-
PKey key = (PKey) arg;
617-
PublicKey pubKey = key.getPublicKey();
618-
PublicKey certPubKey = getAuxCert().getPublicKey();
619-
if ( certPubKey.equals(pubKey) ) return getRuntime().getTrue();
616+
public RubyBoolean check_private_key(final IRubyObject key) {
617+
final PublicKey certPublicKey = cert.getPublicKey();
618+
if ( certPublicKey.equals( ((PKey) key).getPublicKey() ) ) {
619+
return getRuntime().getTrue();
620+
}
620621
return getRuntime().getFalse();
621622
}
622623

@@ -628,7 +629,7 @@ public RubyArray extensions() {
628629
}
629630

630631
@SuppressWarnings("unchecked")
631-
@JRubyMethod(name="extensions=")
632+
@JRubyMethod(name = "extensions=")
632633
public IRubyObject set_extensions(final IRubyObject array) {
633634
extensions.clear(); // RubyArray is a List :
634635
extensions.addAll( (List<X509Extension>) array );

src/main/java/org/jruby/ext/openssl/x509store/X509AuxCertificate.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,8 @@ public boolean hasUnsupportedCriticalExtension() {
258258
return wrap.hasUnsupportedCriticalExtension();
259259
}
260260

261-
private static final String NS_CERT_TYPE_OID = "2.16.840.1.113730.1.1";
262-
263261
public Integer getNsCertType() throws CertificateException {
262+
final String NS_CERT_TYPE_OID = "2.16.840.1.113730.1.1";
264263
final byte[] bytes = getExtensionValue(NS_CERT_TYPE_OID);
265264
if ( bytes == null ) return null;
266265

@@ -269,7 +268,7 @@ public Integer getNsCertType() throws CertificateException {
269268
if ( o instanceof DERBitString ) {
270269
return ((DERBitString) o).intValue();
271270
}
272-
else if ( o instanceof DEROctetString ) {
271+
if ( o instanceof DEROctetString ) {
273272
// just reads initial object for nsCertType definition and ignores trailing objects.
274273
ASN1InputStream in = new ASN1InputStream(((DEROctetString) o).getOctets());
275274
o = in.readObject();

0 commit comments

Comments
 (0)