@Override
public ValidationError[] validate(final Certificate[] chain) {
Assert.notEmpty(chain, "chain");
if (chain.length < 2) {
return new ValidationError[] { new ValidationError(OCSPCertificateValidator.VALIDATOR_NAME, "Certificate chain must be greater than 1(certificate and issuer certificate") };
}
try {
X509Certificate x509Certificate = (X509Certificate) chain[0];
X509Certificate x509Issuer = (X509Certificate) chain[1];
URL url = null;
if (this.url == null) {
URL[] urls = OCSPCertificateValidator.getOCSPUrl(x509Certificate);
if (ConditionUtils.isNotEmpty(urls)) {
url = urls[0];
}
} else {
url = this.url;
}
if (url == null) {
return new ValidationError[] { new ValidationError(OCSPCertificateValidator.VALIDATOR_NAME, "No url found for validation") };
}
OCSPReq req = this.buildRequest(x509Certificate, x509Issuer);
OCSPResp resp = this.sendRequest(req, url);
if (resp.getStatus() != OCSPResponseStatus.SUCCESSFUL) {
return new ValidationError[] { new ValidationError(OCSPCertificateValidator.VALIDATOR_NAME, "Response invalid") };
}
Object responseObject = resp.getResponseObject();
if (responseObject instanceof BasicOCSPResp) {
BasicOCSPResp basicOCSPResp = (BasicOCSPResp) responseObject;
SingleResp[] singleResps = basicOCSPResp.getResponses();
List<ValidationError> errors = new ArrayList<ValidationError>();
for (SingleResp singleResp : singleResps) {
CertificateStatus status = singleResp.getCertStatus();
if (status != null) {
RevokeReason revokeReason = null;
if (status instanceof RevokedStatus) {
RevokedStatus revokedStatus = (RevokedStatus) status;
revokeReason = RevokeReason.getByCode(revokedStatus.getRevocationReason());
}
if (revokeReason != null) {
errors.add(new ValidationError(OCSPCertificateValidator.VALIDATOR_NAME, "Certificate revoked(" + revokeReason.name() + ")"));
} else {
errors.add(new ValidationError(OCSPCertificateValidator.VALIDATOR_NAME, "Certificate revoked(Unknow)"));
}
}
}
return CollectionUtils.toArray(errors, ValidationError.class);