0

Here is my piece of code to for generating X509Certificate with BouncyCastle API

private static X509Certificate createCertificate(String dn, String issuer,
        PublicKey publicKey, PrivateKey privateKey) throws Exception {
    X509V3CertificateGenerator certGenerator = new X509V3CertificateGenerator();
    certGenerator.setSerialNumber(BigInteger.valueOf(Math.abs(new Random()
            .nextLong())));
    certGenerator.setIssuerDN(new X509Name(dn));
    certGenerator.setSubjectDN(new X509Name(dn));
    certGenerator.setIssuerDN(new X509Name(issuer)); // Set issuer!
    certGenerator.setNotBefore(Calendar.getInstance().getTime());
    certGenerator.setNotAfter(Calendar.getInstance().getTime());
    certGenerator.setPublicKey(publicKey);
    certGenerator.setSignatureAlgorithm("SHA1WithRSAEncryption");
    **certGenerator..... ??? what for  key usage ?** 
    X509Certificate certificate = (X509Certificate) certGenerator.generate(
            privateKey, "BC");
    return certificate;
}

Full code you can see here

My question is there is no way to set the key usage for the generated Digital Certificate.

I am trying to set the usage as Encryption.. There is no such method/way in X509V3CertificateGenerator class.

How to go about it.

Thanks for any hints.

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • You want to set KeyUsage or ExtendedKeyUsage usage attributes? In any case, look at this question http://stackoverflow.com/questions/12884995/error-while-creating-x-509-certificate – user1516873 Oct 17 '13 at 10:49
  • @user1516873 Thanks for the link, I remember you. seen many posts regarding certificates .Time to give some name to your profile :). keep up. – Suresh Atta Oct 17 '13 at 11:09
  • What part of your guestion is not answered by this [Bouncy Castle documentation](http://www.bouncycastle.org/wiki/display/JA1/X.509+Public+Key+Certificate+and+Certification+Request+Generation#X.509PublicKeyCertificateandCertificationRequestGeneration-ExtendedKeyUsage) and onwards? – Maarten Bodewes Oct 19 '13 at 17:17

0 Answers0