private static CertPath verifyCertificate(X509Certificate cert, CertStore store, KeyStore trustedStore)
throws InvalidAlgorithmParameterException, KeyStoreException, MessagingException, CertPathBuilderException {
if (cert == null || store == null || trustedStore == null) throw new IllegalArgumentException("cert == "+cert+", store == "+store+", trustedStore == "+trustedStore);
CertPathBuilder pathBuilder;
// I create the CertPathBuilder object. It will be used to find a
// certification path that starts from the signer's certificate and
// leads to a trusted root certificate.
try {
pathBuilder = CertPathBuilder.getInstance("PKIX", "BC");
} catch (Exception e) {
throw new MessagingException("Error during the creation of the certpathbuilder.", e);
}
X509CertSelector xcs = new X509CertSelector();
xcs.setCertificate(cert);
PKIXBuilderParameters params = new PKIXBuilderParameters(trustedStore, xcs);
params.addCertStore(store);
params.setRevocationEnabled(false);
try {
CertPathBuilderResult result = pathBuilder.build(params);
CertPath path = result.getCertPath();
return path;
} catch (CertPathBuilderException e) {
// A certification path is not found, so null is returned.
return null;