PublicKey caPubKey = caKeyPair.getPublic();
PrivateKey caKey = caKeyPair.getPrivate();
Date begin = new Date();
Date ends = new Date(begin.getTime() + (long) 1000 * 60 * 60 * 24 * 30);
BigInteger serialNo = BigInteger.valueOf(1234);
JcaX509ExtensionUtils jxeu = new JcaX509ExtensionUtils();
// operate
X509Certificate resultCert = SunCertificateUtils.sign(subject, pubKey, issuer, caPubKey, caKey, begin, ends, serialNo, null);
// verify
assertNotNull(resultCert);
LOG.debug("result certificate: " + resultCert);
resultCert.verify(caPubKey);
assertEquals(subject, resultCert.getSubjectX500Principal());
assertEquals(issuer, resultCert.getIssuerX500Principal());
assertEquals(serialNo, resultCert.getSerialNumber());
assertEquals(pubKey, resultCert.getPublicKey());
LOG.debug("expected begin: " + begin.getTime());
LOG.debug("actual begin: " + resultCert.getNotBefore().getTime());
/*
* BouncyCastle drops the milliseconds.
*/
assertTrue(Math.abs(begin.getTime() - resultCert.getNotBefore().getTime()) < 1000);
assertTrue(Math.abs(ends.getTime() - resultCert.getNotAfter().getTime()) < 1000);
byte[] subjectKeyIdentifierExtValue = resultCert.getExtensionValue(X509Extension.subjectKeyIdentifier.getId());
assertNotNull(subjectKeyIdentifierExtValue);
ASN1Primitive subjectKeyIdentifier = JcaX509ExtensionUtils.parseExtensionValue(
subjectKeyIdentifierExtValue);
ASN1Primitive expSKI = jxeu.createSubjectKeyIdentifier(pubKey).toASN1Primitive();
assertArrayEquals(expSKI.getEncoded(), subjectKeyIdentifier.getEncoded());
byte[] authorityKeyIdentifierExtValue = resultCert.getExtensionValue(X509Extension.authorityKeyIdentifier.getId());
ASN1Primitive authorityKeyIdentifier = JcaX509ExtensionUtils.parseExtensionValue(
authorityKeyIdentifierExtValue);
ASN1Primitive expAKI = jxeu.createAuthorityKeyIdentifier(caPubKey).toASN1Primitive();
assertArrayEquals(expAKI.getEncoded(), authorityKeyIdentifier.getEncoded());
assertEquals(-1, resultCert.getBasicConstraints());
byte[] netscapeCertTypeExtValue = resultCert.getExtensionValue(MiscObjectIdentifiers.netscapeCertType.getId());