signerBuilder = new JcaContentSignerBuilder("MD5withRSA");
}
// The following variables are used to represent the SCEP client
KeyPair idPair = KeyPairGenerator.getInstance("RSA").genKeyPair();
X500Name issuer = new X500Name("CN=entity");
BigInteger serial = new BigInteger(16, new SecureRandom());
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
Date notBefore = cal.getTime();
cal.add(Calendar.DATE, 2);
Date notAfter = cal.getTime();
X500Name subject = issuer;
PublicKey publicKey = idPair.getPublic();
JcaX509v3CertificateBuilder certBuilder = new JcaX509v3CertificateBuilder(
issuer, serial, notBefore, notAfter, subject, publicKey);
X509CertificateHolder idHolder = certBuilder.build(signerBuilder
.build(idPair.getPrivate()));
// Convert Bouncy Castle representation of X509Certificate into
// something usable
X509Certificate id = (X509Certificate) CertificateFactory.getInstance(
"X509").generateCertificate(
new ByteArrayInputStream(idHolder.getEncoded()));
// The following variables are used to represent the entity being
// enrolled
X500Name entityName = new X500Name("CN=entity");
KeyPair entityPair = KeyPairGenerator.getInstance("RSA").genKeyPair();
SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfo
.getInstance(entityPair.getPublic().getEncoded());
// Generate the certificate signing request
PKCS10CertificationRequestBuilder csrBuilder = new PKCS10CertificationRequestBuilder(
entityName, publicKeyInfo);
// SCEP servers usually require a challenge password
csrBuilder.addAttribute(
PKCSObjectIdentifiers.pkcs_9_at_challengePassword,
new DERPrintableString(new String("password".toCharArray())));
ContentSigner signer = signerBuilder.build(entityPair.getPrivate());
PKCS10CertificationRequest csr = csrBuilder.build(signer);
// Send the enrollment request
EnrollmentResponse response = client
.enrol(id, idPair.getPrivate(), csr);
if (response.isFailure()) {
// Our request was rejected!
System.out.println("Failed!");
} else if (response.isPending()) {
// The server hasn't enrolled us, but we should try again.
System.out.println("Pending!");
X500Principal entityPrincipal = new X500Principal(
entityName.getEncoded());
// We should deal with the response to the poll too. Since this a
// short-lived
// test, we conveniently stop processing here. Usually you'd
// schedule the poll
// to run at some point in the future.