Skip to content

Commit 9ae920b

Browse files
committed
if JCE is used to verify a CRL then we need to find suitable SecurityProvider
since we do not know which security providers are registered we need to find one which can verify a CRL against its public key. just try all registered until it either succeeds or fails with a SignatureException. fixes #20 Sponsored by Lookout Inc.
1 parent 287213a commit 9ae920b

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

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

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
*/
2424
package org.jruby.ext.openssl;
2525

26+
import static org.jruby.ext.openssl.OpenSSL.debugStackTrace;
27+
2628
import java.lang.reflect.Constructor;
2729
import java.lang.reflect.Field;
2830
import java.lang.reflect.InvocationTargetException;
2931
import java.lang.reflect.Method;
30-
import java.util.Locale;
31-
3232
import java.security.InvalidKeyException;
3333
import java.security.KeyFactory;
3434
import java.security.KeyFactorySpi;
@@ -39,7 +39,6 @@
3939
import java.security.MessageDigest;
4040
import java.security.MessageDigestSpi;
4141
import java.security.NoSuchAlgorithmException;
42-
import java.security.NoSuchProviderException;
4342
import java.security.Provider;
4443
import java.security.PublicKey;
4544
import java.security.SecureRandom;
@@ -53,6 +52,9 @@
5352
import java.security.cert.CertificateFactory;
5453
import java.security.cert.CertificateFactorySpi;
5554
import java.security.cert.X509CRL;
55+
import java.util.LinkedList;
56+
import java.util.List;
57+
import java.util.Locale;
5658
import java.util.Map;
5759
import java.util.StringTokenizer;
5860
import java.util.concurrent.ConcurrentHashMap;
@@ -72,8 +74,6 @@
7274
import org.bouncycastle.asn1.x509.CertificateList;
7375
import org.bouncycastle.jce.provider.X509CRLObject;
7476

75-
import static org.jruby.ext.openssl.OpenSSL.debugStackTrace;
76-
7777
/**
7878
* Java Security (and JCE) helpers.
7979
*
@@ -563,26 +563,28 @@ static boolean verify(final X509CRL crl, final PublicKey publicKey, final boolea
563563
return true;
564564
}
565565

566-
try {
567-
crl.verify(publicKey);
568-
return true;
569-
}
570-
catch (NoSuchAlgorithmException ex) {
571-
if ( silent ) return false; throw ex;
572-
}
573-
catch (CRLException ex) {
574-
if ( silent ) return false; throw ex;
575-
}
576-
catch (InvalidKeyException ex) {
577-
if ( silent ) return false; throw ex;
578-
}
579-
catch (SignatureException ex) {
580-
if ( silent ) return false; throw ex;
566+
// since we are using JCE here and BC might not be registered as Provider
567+
// we need to find a provider which supports such CRL verification
568+
// if we find a provider we will ignore the collected errors
569+
// otherwise the errors get displayed
570+
// TODO use BC directly for verifing CRL (probably needs quite some refactoring)
571+
List<Exception> errors = new LinkedList<Exception>();
572+
for(Provider p: Security.getProviders()) {
573+
try {
574+
crl.verify(publicKey, p.getName());
575+
return true;
576+
}
577+
catch(SignatureException e) {
578+
return false;
579+
}
580+
catch(Exception e) {
581+
errors.add(e);
582+
}
581583
}
582-
catch (NoSuchProviderException e) {
584+
for(Exception e: errors) {
583585
debugStackTrace(e);
584-
throw new RuntimeException(e); // unexpected - might hide a bug
585-
}
586+
}
587+
return false;
586588
}
587589

588590
private static Object getCertificateList(final Object crl) { // X509CRLObject

0 commit comments

Comments
 (0)