// create the certificate - version 3 - without extensions
//
X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();
certGen.setSerialNumber(BigInteger.valueOf(1));
certGen.setIssuerDN(new X509Principal(attrs));
certGen.setNotBefore(new Date(System.currentTimeMillis() - 50000));
certGen.setNotAfter(new Date(System.currentTimeMillis() + 50000));
certGen.setSubjectDN(new X509Principal(attrs));
certGen.setPublicKey(pubKey);
certGen.setSignatureAlgorithm("SHA256WithRSAEncryption");
X509Certificate cert = certGen.generate(privKey);
cert.checkValidity(new Date());
cert.verify(pubKey);
Set dummySet = cert.getNonCriticalExtensionOIDs();
if (dummySet != null)
{
fail("non-critical oid set should be null");
}
dummySet = cert.getCriticalExtensionOIDs();
if (dummySet != null)
{
fail("critical oid set should be null");
}
//
// create the certificate - version 3 - with extensions
//
certGen = new X509V3CertificateGenerator();
certGen.setSerialNumber(BigInteger.valueOf(1));
certGen.setIssuerDN(new X509Principal(attrs));
certGen.setNotBefore(new Date(System.currentTimeMillis() - 50000));
certGen.setNotAfter(new Date(System.currentTimeMillis() + 50000));
certGen.setSubjectDN(new X509Principal(attrs));
certGen.setPublicKey(pubKey);
certGen.setSignatureAlgorithm("MD5WithRSAEncryption");
certGen.addExtension("2.5.29.15", true,
new X509KeyUsage(X509KeyUsage.encipherOnly));
certGen.addExtension("2.5.29.37", true,
new DERSequence(KeyPurposeId.anyExtendedKeyUsage));
certGen.addExtension("2.5.29.17", true,
new GeneralNames(new GeneralName(GeneralName.rfc822Name, "test@test.test")));
cert = certGen.generate(privKey);
cert.checkValidity(new Date());
cert.verify(pubKey);
ByteArrayInputStream sbIn = new ByteArrayInputStream(cert.getEncoded());
ASN1InputStream sdIn = new ASN1InputStream(sbIn);
ByteArrayInputStream bIn = new ByteArrayInputStream(cert.getEncoded());
CertificateFactory certFact = CertificateFactory.getInstance("X.509", "BC");
cert = (X509Certificate)certFact.generateCertificate(bIn);
if (!cert.getKeyUsage()[7])
{
fail("error generating cert - key usage wrong.");
}
List l = cert.getExtendedKeyUsage();
if (!l.get(0).equals(KeyPurposeId.anyExtendedKeyUsage.getId()))
{
fail("failed extended key usage test");
}
Collection c = cert.getSubjectAlternativeNames();
Iterator it = c.iterator();
while (it.hasNext())
{
List gn = (List)it.next();
if (!gn.get(1).equals("test@test.test"))
{
fail("failed subject alternative names test");
}
}
// System.out.println(cert);
//
// create the certificate - version 1
//
X509V1CertificateGenerator certGen1 = new X509V1CertificateGenerator();
certGen1.setSerialNumber(BigInteger.valueOf(1));
certGen1.setIssuerDN(new X509Principal(ord, attrs));
certGen1.setNotBefore(new Date(System.currentTimeMillis() - 50000));
certGen1.setNotAfter(new Date(System.currentTimeMillis() + 50000));
certGen1.setSubjectDN(new X509Principal(ord, values));
certGen1.setPublicKey(pubKey);
certGen1.setSignatureAlgorithm("MD5WithRSAEncryption");
cert = certGen1.generate(privKey);