@Override
public ValidationError[] validate(final Certificate[] chain) {
Assert.notEmpty(chain, "chain");
if (chain.length < 2) {
return new ValidationError[] { new ValidationError(HierarchyCertificateValidator.VALIDATOR_NAME, "Certificate chain must be greater than 1(certificate and issuer certificate") };
}
List<ValidationError> list = new ArrayList<ValidationError>();
for (int i = 0; i < (chain.length - 1); i++) {
X509Certificate certificate = (X509Certificate) chain[i];
X509Certificate issuer = (X509Certificate) chain[i + 1];
try {
certificate.verify(issuer.getPublicKey());
int pathLen = issuer.getBasicConstraints();
if (pathLen == -1) {
list.add(new ValidationError(HierarchyCertificateValidator.VALIDATOR_NAME, "Certificate issuer '" + BouncyCastleProviderHelper.toString(issuer.getSubjectX500Principal()) + "' is not a CA"));
}
boolean[] keyUsage = issuer.getKeyUsage();
boolean keyCertSign = keyUsage[5];
if (!keyCertSign) {
list.add(new ValidationError(HierarchyCertificateValidator.VALIDATOR_NAME, "Certificate issuer '" + BouncyCastleProviderHelper.toString(issuer.getSubjectX500Principal()) + "' dont have 'keyCertSign' in key usage"));
}
} catch (SignatureException e) {
list.add(new ValidationError(HierarchyCertificateValidator.VALIDATOR_NAME, "Certificate '" + BouncyCastleProviderHelper.toString(certificate.getSubjectX500Principal()) + "' not signed by '" + BouncyCastleProviderHelper.toString(issuer.getSubjectX500Principal()) + "'"));
} catch (Exception e) {
throw new CertificateException(e);
}
}
try {
if (Conditions.isNotEmpty(this.certificates)) {
boolean requiredOk = false;
outer: for (int i = 1; i < chain.length; i++) {
X509Certificate issuer = (X509Certificate) chain[i];
for (Certificate required : this.certificates) {
X509Certificate x509Required = (X509Certificate) required;
if (Arrays.equals(issuer.getEncoded(), x509Required.getEncoded())) {
RockFrameworkLogger.getLogger().info("Matches " + BouncyCastleProviderHelper.toString(x509Required.getSubjectX500Principal()));
requiredOk = true;
break outer;
}
}
}
if (!requiredOk) {
list.add(new ValidationError(HierarchyCertificateValidator.VALIDATOR_NAME, "Certificate chain is invalid, a required certificate could not be found"));
}
}
} catch (CertificateEncodingException e) {
throw new CertificateException(e);
}