Skip to content

Commit ea229e0

Browse files
committed
Add @bbrowning fix for #94.
Still no good test for this, since I can't get it to reproduce locally.
1 parent 13e964a commit ea229e0

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
public abstract class SecurityHelper {
100100

101101
private static String BC_PROVIDER_CLASS = "org.bouncycastle.jce.provider.BouncyCastleProvider";
102+
private static String BC_PROVIDER_NAME = "BC";
102103
static boolean setBouncyCastleProvider = true; // (package access for tests)
103104
static Provider securityProvider; // 'BC' provider (package access for tests)
104105
private static volatile Boolean registerProvider = null;
@@ -192,13 +193,27 @@ public static CertificateFactory getCertificateFactory(final String type)
192193
final Provider provider = getSecurityProvider();
193194
if ( provider != null ) return getCertificateFactory(type, provider);
194195
}
195-
catch (CertificateException e) { }
196+
catch (CertificateException e) {e.printStackTrace(); }
196197
return CertificateFactory.getInstance(type);
197198
}
198199

199200
static CertificateFactory getCertificateFactory(final String type, final Provider provider)
200201
throws CertificateException {
201-
final CertificateFactorySpi spi = (CertificateFactorySpi) getImplEngine("CertificateFactory", type);
202+
final CertificateFactorySpi spi;
203+
boolean addedBC = false;
204+
synchronized(SecurityHelper.class) {
205+
try {
206+
if (provider.getName().equals(BC_PROVIDER_NAME) && Security.getProvider(BC_PROVIDER_NAME) == null) {
207+
Security.addProvider(provider);
208+
addedBC = true;
209+
}
210+
spi = (CertificateFactorySpi) getImplEngine("CertificateFactory", type);
211+
} finally {
212+
if (addedBC) {
213+
Security.removeProvider(BC_PROVIDER_NAME);
214+
}
215+
}
216+
}
202217
if ( spi == null ) throw new CertificateException(type + " not found");
203218
return newInstance(CertificateFactory.class,
204219
new Class[]{ CertificateFactorySpi.class, Provider.class, String.class },

0 commit comments

Comments
 (0)