7

Writing code to generate Digital Certificate using BouncyCastle.

Here is the essential part of code causing problem.

public X509Certificate generateCertWithKeypair(KeyPair caPair)
            throws InvalidKeyException, SecurityException, SignatureException {
        X509V3CertificateGenerator v3CertGen = new X509V3CertificateGenerator();
        v3CertGen
                .setSerialNumber(BigInteger.valueOf(System.currentTimeMillis()));
        v3CertGen
                .setIssuerDN(new X509Principal("CN=cn, O=o, L=L, ST=il, C= c"));
        v3CertGen.setNotBefore(new Date(System.currentTimeMillis() - 1000L * 60
                * 60 * 24));
        v3CertGen.setNotAfter(new Date(System.currentTimeMillis()
                + (1000L * 60 * 60 * 24 * 365 * 10)));
        v3CertGen
                .setSubjectDN(new X509Principal("CN=cn, O=o, L=L, ST=il, C= c"));
        v3CertGen.setPublicKey(caPair.getPublic());
        v3CertGen.setSignatureAlgorithm("SHA1WithRSAEncryption");
        X509Certificate generateX509Certificate = v3CertGen
                .generateX509Certificate(caPair.getPrivate());//**here**
        return generateX509Certificate;
    }

Exception facing

Exception in "main" java.lang.SecurityException: BC provider not installed!
    at X509V3CertificateGenerator.generateX509Certificate(Unknown Source)
    at chapter4.Dupe.generateCertWithKeypair(Dupe.java:74)
    at chapter4.Dupe.main(Dupe.java:32)

In search I found that the latest jar resolves the issue, But no luck.

Am I missing something ?

See Full Code Here.

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307

2 Answers2

11

You should "register" BC in JRE. You can do it in two ways: put bcprov.jar in $JRE/lib/ext folder and add in $JRE/lib/security/java.security line

security.provider.N=org.bouncycastle.jce.provider.BouncyCastleProvider

or put bcprov.jar in classpath, don't modify java.security, but add in code somewhere

static { Security.addProvider(new BouncyCastleProvider());  }

http://www.bouncycastle.org/wiki/display/JA1/Provider+Installation

user1516873
  • 5,060
  • 2
  • 37
  • 56
  • 1
    What I done is added the bouncy castle jar in lib folder. But not resolved. After this line added it registered. Thanks. – Suresh Atta Oct 11 '13 at 13:54
0

I came across the similar issue, adding to the accepted answer, here is the solution worked for me.

Solution 1 : Updating JRE/lib/security/java.security

security.provider.1=org.bouncycastle.jce.provider.BouncyCastleProvider
security.provider.2=org.bouncycastle.jsse.provider.BouncyCastleJsseProvider
security.provider.3=sun.security.provider.Sun
security.provider.4=sun.security.rsa.SunRsaSign
security.provider.5=com.sun.net.ssl.internal.ssl.Provider
security.provider.6=com.sun.crypto.provider.SunJCE
security.provider.7=sun.security.jgss.SunProvider
security.provider.8=com.sun.security.sasl.Provider

The BC libraries needs to be on top (1&2)

Solution 2 : Add it in the project

static {
 if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
                
       Security.insertProviderAt(new BouncyCastleProvider(), 1);
  }

 if (Security.getProvider(BouncyCastleJsseProvider.PROVIDER_NAME) == null) {
                Security.insertProviderAt(new BouncyCastleJsseProvider(), 2);
 }
}
jfk
  • 4,335
  • 34
  • 27