private void x509StoreTest() throws Exception
{
X509Store cs = X509Store.getInstance("CERTIFICATE/LDAP", params1, "BC");
X509CertStoreSelector sl = new X509CertStoreSelector();
CertificateFactory cf = CertificateFactory.getInstance("X.509", "BC");
X509Certificate xcert = (X509Certificate)cf
.generateCertificate(new ByteArrayInputStream(cert1));
sl.setCertificate(xcert);
Collection coll = cs.getMatches(sl);
if (coll.isEmpty() || !coll.iterator().next().equals(xcert))
{
fail("certificate could not be picked from LDAP directory.");
}
// System.out.println(coll.toArray()[0]);
sl.setCertificate(null);
sl.setSubject(getSubject(xcert).getEncoded());
coll = cs.getMatches(sl);
if (coll.isEmpty() || !coll.iterator().next().equals(xcert))
{
fail("certificate could not be picked from LDAP directory.");
}
X509CRLStoreSelector sl2 = new X509CRLStoreSelector();
X509CRL crl = (X509CRL)cf.generateCRL(new
ByteArrayInputStream(directCRL));
sl2.setIssuers(Collections.singleton(crl.getIssuerX500Principal()));
cs = X509Store.getInstance("CRL/LDAP", params1, "BC");
coll = cs.getMatches(sl2);
if (!coll.iterator().hasNext())
{
fail("CRL could not be picked from LDAP directory.");
}
// System.out.println(coll.toArray()[0]);
cs = X509Store.getInstance("CERTIFICATE/LDAP", params2, "BC");
sl = new X509CertStoreSelector();
xcert = (X509Certificate)cf
.generateCertificate(new ByteArrayInputStream(cert2));
sl.setCertificate(xcert);
coll = cs.getMatches(sl);
if (coll.isEmpty() || !coll.iterator().next().equals(xcert))
{
fail("Certificate could not be picked from LDAP directory.");
}
// System.out.println(coll.toArray()[0]);
cs = X509Store.getInstance("CERTIFICATE/LDAP", params3, "BC");
sl = new X509CertStoreSelector();
xcert = (X509Certificate)cf
.generateCertificate(new ByteArrayInputStream(cert3));
sl.setCertificate(xcert);
coll = cs.getMatches(sl);
if (coll.isEmpty() || !coll.iterator().next().equals(xcert))
{
fail("Certificate could not be picked from LDAP directory.");
}
// System.out.println(coll.toArray()[0]);
xcert = (X509Certificate)cf
.generateCertificate(new ByteArrayInputStream(caCert3));
sl = new X509CertStoreSelector();
sl.setSubject(getSubject(xcert).getEncoded());
coll = cs.getMatches(sl);
boolean found = false;
if (coll.isEmpty())
{
fail("Certificate could not be picked from LDAP directory.");
}
for (Iterator it = coll.iterator(); it.hasNext();)
{
if (it.next().equals(xcert))
{
found = true;
break;
}
}
if (!found)
{
fail("Certificate could not be picked from LDAP directory.");
}
// System.out.println(coll.toArray()[0]);
sl = new X509CertStoreSelector();
xcert = (X509Certificate)cf
.generateCertificate(new ByteArrayInputStream(crossCert3));
sl.setSubject(getSubject(xcert).getEncoded());
coll = cs.getMatches(sl);
if (coll.isEmpty())
{
fail("Cross certificate pair could not be picked from LDAP directory.");
}