Date notBefore = new Date();
Date notAfter = new Date(new Date().getTime()+24*3600*1000);
KeyPair keys = KeyTools.genKeys("512", AlgorithmConstants.KEYALGORITHM_RSA);
String username = "cmpRaThrowAwayTestUser" + RND.nextLong(); // This is what we expect from the CMP configuration
String subjectDN = "CN=" + username;
PKIMessage one = genCertReq(CertTools.getSubjectDN(caCertificate), subjectDN, keys, caCertificate, nonce, transid, true, null, notBefore, notAfter, null);
PKIMessage req = protectPKIMessage(one, false, PBE_SECRET, "unusedKeyId", 567);
assertNotNull("Request was not created properly.", req);
int reqId = req.getBody().getIr().getCertReqMsg(0).getCertReq().getCertReqId().getValue().intValue();
ByteArrayOutputStream bao = new ByteArrayOutputStream();
new DEROutputStream(bao).writeObject(req);
byte[] resp = sendCmpHttp(bao.toByteArray(), 200);
checkCmpResponseGeneral(resp, CertTools.getSubjectDN(caCertificate), subjectDN, caCertificate, nonce, transid, false, PBE_SECRET);
X509Certificate cert = checkCmpCertRepMessage(subjectDN, caCertificate, resp, reqId);
assertEquals("Certificate history data was or wasn't stored: ", useCertReqHistory, InterfaceCache.getCertificateStoreSession().getCertReqHistory(ADMIN, CertTools.getSerialNumber(cert), CertTools.getIssuerDN(cert))!=null);
assertEquals("User data was or wasn't stored: ", useUserStorage, InterfaceCache.getUserAdminSession().existsUser(ADMIN, username));
assertEquals("Certificate data was or wasn't stored: ", useCertificateStorage, InterfaceCache.getCertificateStoreSession().findCertificateByFingerprint(ADMIN, CertTools.getFingerprintAsString(cert))!=null);
// Send a confirm message to the CA
String hash = "foo123";
PKIMessage confirm = genCertConfirm(subjectDN, caCertificate, nonce, transid, hash, reqId);
assertNotNull("Could not create confirmation message.", confirm);
PKIMessage req1 = protectPKIMessage(confirm, false, PBE_SECRET, "unusedKeyId", 567);
bao = new ByteArrayOutputStream();
new DEROutputStream(bao).writeObject(req1);
resp = sendCmpHttp(bao.toByteArray(), 200);
checkCmpResponseGeneral(resp, CertTools.getSubjectDN(caCertificate), subjectDN, caCertificate, nonce, transid, false, PBE_SECRET);
checkCmpPKIConfirmMessage(subjectDN, caCertificate, resp);
// We only expect revocation to work if we store certificate data and user data
// TODO: ECA-1916 should remove dependency on useUserStorage
if (useCertificateStorage && useUserStorage) {
// Now revoke the bastard using the CMPv1 reason code!
PKIMessage rev = genRevReq(CertTools.getSubjectDN(caCertificate), subjectDN, cert.getSerialNumber(), caCertificate, nonce, transid, false);
PKIMessage revReq = protectPKIMessage(rev, false, PBE_SECRET, "unusedKeyId", 567);
assertNotNull("Could not create revocation message.", revReq);
bao = new ByteArrayOutputStream();
new DEROutputStream(bao).writeObject(revReq);
resp = sendCmpHttp(bao.toByteArray(), 200);
checkCmpResponseGeneral(resp, CertTools.getSubjectDN(caCertificate), subjectDN, caCertificate, nonce, transid, false, PBE_SECRET);