I am newbie to Java security. I am required to verify entire certificate chain w using X509Certificate class. I am referring to Validate X509 certificates using Java APis and How to get server certificate chain then verify it's valid and trusted in Java. But now getting java.security.cert.CertPathValidatorException: Path does not chain with any of the trust anchors exception. Here is my code. Your help is greatly appreciated. Thanks.
for(java.security.cert.Certificate cert : certs){
EventLog.append("Certificate is "+cert);
if(cert instanceof X509Certificate){
try{
((X509Certificate)cert).checkValidity();
EventLog.append("Certifficate is valid for current date");
mylist.add((X509Certificate) cert);
}catch(CertificateExpiredException e){
EventLog.append("Certificate is expired");
}
}// if cert is instance of x509Certificate ends
}
//check the chain
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load( new
FileInputStream(keyStoreName),keyStorePassword.toCharArray());
try{
CertPath cp = cf.generateCert## Heading ##Path(mylist);
PKIXParameters params = new PKIXParameters(keyStore);
params.setRevocationEnabled(false);
CertPathValidator cpv =
CertPathValidator.getInstance(CertPathValidator.getDefaultType());
PKIXCertPathValidatorResult pkixCertPathValidatorResult =
(PKIXCertPathValidatorResult) cpv.validate(cp, params);
EventLog.append("Certificate is trusted");
}catch(Exception e){
EventLog.append(e);
}