// (send crap message and get good error)
KeyPair keys = createUserCert(caid);
// And an OCSP request
OCSPReqGenerator gen = new OCSPReqGenerator();
gen.addRequest(new CertificateID(CertificateID.HASH_SHA1, cacert, ocspTestCert.getSerialNumber()));
Hashtable exts = new Hashtable();
X509Extension ext = new X509Extension(false, new DEROctetString("123456789".getBytes()));
exts.put(OCSPObjectIdentifiers.id_pkix_ocsp_nonce, ext);
gen.setRequestExtensions(new X509Extensions(exts));
X509Certificate chain[] = new X509Certificate[2];
chain[0] = ocspTestCert;
chain[1] = cacert;
gen.setRequestorName(ocspTestCert.getSubjectX500Principal());
OCSPReq req = gen.generate("SHA1WithRSA", keys.getPrivate(), chain, "BC");
// First test with a signed OCSP request that can be verified
Collection<Certificate> cacerts = new ArrayList<Certificate>();
cacerts.add(cacert);
ICertificateCache certcache = CertificateCacheTstFactory.getInstance(cacerts);
X509Certificate signer = OCSPUtil.checkRequestSignature("127.0.0.1", req, certcache);
assertNotNull(signer);
assertEquals(ocspTestCert.getSerialNumber().toString(16), signer.getSerialNumber().toString(16));
// Try with an unsigned request, we should get a SignRequestException
req = gen.generate();
boolean caught = false;
try {
signer = OCSPUtil.checkRequestSignature("127.0.0.1", req, certcache);
} catch (SignRequestException e) {
caught = true;
}
assertTrue(caught);
// sign with a keystore where the CA-certificate is not known
KeyStore store = KeyStore.getInstance("PKCS12", "BC");
ByteArrayInputStream fis = new ByteArrayInputStream(ks3);
store.load(fis, "foo123".toCharArray());
Certificate[] certs = KeyTools.getCertChain(store, "privateKey");
chain[0] = (X509Certificate) certs[0];
chain[1] = (X509Certificate) certs[1];
PrivateKey pk = (PrivateKey) store.getKey("privateKey", "foo123".toCharArray());
req = gen.generate("SHA1WithRSA", pk, chain, "BC");
// Send the request and receive a singleResponse, this response should
// throw an SignRequestSignatureException
caught = false;
try {
signer = OCSPUtil.checkRequestSignature("127.0.0.1", req, certcache);
} catch (SignRequestSignatureException e) {
caught = true;
}
assertTrue(caught);
// sign with a keystore where the signing certificate has expired
store = KeyStore.getInstance("PKCS12", "BC");
fis = new ByteArrayInputStream(ksexpired);
store.load(fis, "foo123".toCharArray());
certs = KeyTools.getCertChain(store, "ocspclient");
chain[0] = (X509Certificate) certs[0];
chain[1] = (X509Certificate) certs[1];
pk = (PrivateKey) store.getKey("ocspclient", "foo123".toCharArray());
req = gen.generate("SHA1WithRSA", pk, chain, "BC");
// Send the request and receive a singleResponse, this response should
// throw an SignRequestSignatureException
caught = false;
try {
signer = OCSPUtil.checkRequestSignature("127.0.0.1", req, certcache);