values.addElement("feedback-crypto@bouncycastle.org");
//
// create base certificate - version 3
//
X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();
certGen.setSerialNumber(BigInteger.valueOf(1));
certGen.setIssuerDN(new X509Principal(ord, values));
certGen.setNotBefore(new Date(System.currentTimeMillis() - 50000));
certGen.setNotAfter(new Date(System.currentTimeMillis() + 50000));
certGen.setSubjectDN(new X509Principal(ord, values));
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")));
X509Certificate baseCert = certGen.generate(privKey, "BC");
//
// copy certificate
//
certGen = new X509V3CertificateGenerator();
certGen.setSerialNumber(BigInteger.valueOf(1));
certGen.setIssuerDN(new X509Principal(ord, values));
certGen.setNotBefore(new Date(System.currentTimeMillis() - 50000));
certGen.setNotAfter(new Date(System.currentTimeMillis() + 50000));
certGen.setSubjectDN(new X509Principal(ord, values));
certGen.setPublicKey(pubKey);
certGen.setSignatureAlgorithm("MD5WithRSAEncryption");
certGen.copyAndAddExtension(new DERObjectIdentifier("2.5.29.15"), true, baseCert);
certGen.copyAndAddExtension("2.5.29.37", false, baseCert);
X509Certificate cert = certGen.generate(privKey, "BC");
cert.checkValidity(new Date());
cert.verify(pubKey);
if (!areEqual(baseCert.getExtensionValue("2.5.29.15"), cert.getExtensionValue("2.5.29.15")))
{
fail("2.5.29.15 differs");
}
if (!areEqual(baseCert.getExtensionValue("2.5.29.37"), cert.getExtensionValue("2.5.29.37")))
{
fail("2.5.29.37 differs");
}
//
// exception test
//
try
{
certGen.copyAndAddExtension("2.5.99.99", true, baseCert);
fail("exception not thrown on dud extension copy");
}
catch (CertificateParsingException e)
{
// expected
}
try
{
certGen.setPublicKey(dudPublicKey);
certGen.generate(privKey, "BC");
fail("key without encoding not detected in v3");
}
catch (IllegalArgumentException e)
{