use of java.security.cert.CertPath in project zm-mailbox by Zimbra.
the class CertValidationUtil method validateCertificate.
public static void validateCertificate(X509Certificate cert, boolean revocationCheckEnabled, Set<TrustAnchor> trustedCertsSet) throws CertificateException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, CertPathValidatorException {
cert.checkValidity();
if (revocationCheckEnabled) {
List<X509Certificate> certificates = new ArrayList<X509Certificate>();
certificates.add(cert);
CertificateFactory cf;
CertPath cp;
cf = CertificateFactory.getInstance("X509");
cp = cf.generateCertPath(certificates);
// init PKIX parameters
PKIXParameters params;
params = new PKIXParameters(trustedCertsSet);
params.setRevocationEnabled(revocationCheckEnabled);
// perform validation
CertPathValidator cpv;
cpv = CertPathValidator.getInstance("PKIX");
PKIXCertPathValidatorResult cpv_result = (PKIXCertPathValidatorResult) cpv.validate(cp, params);
ZimbraLog.account.debug("Certificate Validation Result %s", cpv_result.toString());
}
}
use of java.security.cert.CertPath in project hono by eclipse.
the class DeviceRegistryBasedCertificateVerifier method validateCertificateAndLoadDevice.
/**
* Validates a device's client certificate and completes the DTLS handshake result handler.
*
* @param cid the connection id to report the result.
* @param certPath certificate path.
* @param session session.
* @see #setResultHandler(HandshakeResultHandler)
*/
private void validateCertificateAndLoadDevice(final ConnectionId cid, final CertPath certPath, final DTLSSession session) {
LOG.debug("validating client's X.509 certificate");
final Span span = tracer.buildSpan("validate client certificate").withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT).withTag(Tags.COMPONENT.getKey(), adapter.getTypeName()).start();
validateCertificateAndLoadDevice(session, certPath, span).map(info -> {
// set AdditionalInfo as customArgument here
return new CertificateVerificationResult(cid, certPath, info);
}).otherwise(t -> {
TracingHelper.logError(span, "could not validate X509 for device", t);
LOG.debug("error validating X509", t);
final AlertMessage alert = new AlertMessage(AlertLevel.FATAL, AlertDescription.BAD_CERTIFICATE, session.getPeer());
return new CertificateVerificationResult(cid, new HandshakeException("error validating X509", alert), null);
}).onSuccess(result -> {
span.finish();
californiumResultHandler.apply(result);
});
}
use of java.security.cert.CertPath in project hono by eclipse.
the class DeviceCertificateValidator method validate.
/**
* {@inheritDoc}
*/
@Override
public Future<Void> validate(final List<X509Certificate> chain, final Set<TrustAnchor> trustAnchors) {
Objects.requireNonNull(chain);
Objects.requireNonNull(trustAnchors);
if (chain.isEmpty()) {
throw new IllegalArgumentException("certificate chain must not be empty");
} else if (trustAnchors.isEmpty()) {
throw new IllegalArgumentException("trust anchor list must not be empty");
}
final Promise<Void> result = Promise.promise();
try {
final PKIXParameters params = new PKIXParameters(trustAnchors);
// TODO do we need to check for revocation?
params.setRevocationEnabled(false);
final CertificateFactory factory = CertificateFactory.getInstance("X.509");
final CertPath path = factory.generateCertPath(chain);
final CertPathValidator validator = CertPathValidator.getInstance("PKIX");
validator.validate(path, params);
LOG.debug("validation of device certificate [subject DN: {}] succeeded", chain.get(0).getSubjectX500Principal().getName());
result.complete();
} catch (GeneralSecurityException e) {
LOG.debug("validation of device certificate [subject DN: {}] failed", chain.get(0).getSubjectX500Principal().getName(), e);
if (e instanceof CertificateException) {
result.fail(e);
} else {
result.fail(new CertificateException("validation of device certificate failed", e));
}
}
return result.future();
}
use of java.security.cert.CertPath in project fabric-sdk-java by hyperledger.
the class CryptoPrimitives method validateCertificate.
boolean validateCertificate(Certificate cert) {
boolean isValidated;
if (cert == null) {
return false;
}
try {
KeyStore keyStore = getTrustStore();
PKIXParameters parms = new PKIXParameters(keyStore);
parms.setRevocationEnabled(false);
// PKIX
CertPathValidator certValidator = CertPathValidator.getInstance(CertPathValidator.getDefaultType());
ArrayList<Certificate> start = new ArrayList<>();
start.add(cert);
CertificateFactory certFactory = CertificateFactory.getInstance(CERTIFICATE_FORMAT);
CertPath certPath = certFactory.generateCertPath(start);
certValidator.validate(certPath, parms);
isValidated = true;
} catch (KeyStoreException | InvalidAlgorithmParameterException | NoSuchAlgorithmException | CertificateException | CertPathValidatorException | CryptoException e) {
logger.error("Cannot validate certificate. Error is: " + e.getMessage() + "\r\nCertificate" + cert.toString());
isValidated = false;
}
return isValidated;
}
use of java.security.cert.CertPath in project open-ecard by ecsec.
the class JavaSecVerifier method isValid.
@Override
public void isValid(TlsServerCertificate chain, String hostname) throws CertificateVerificationException {
try {
CertPath certPath = convertChain(chain);
// create the parameters for the validator
PKIXParameters params = new PKIXParameters(getTrustStore());
if (checkRevocation) {
params.setRevocationEnabled(true);
System.setProperty("com.sun.security.enableCRLDP", "true");
} else {
// disable CRL checking since we are not supplying any CRLs yet
params.setRevocationEnabled(false);
}
// validate - exception marks failure
certPathValidator.validate(certPath, params);
} catch (CertPathValidatorException ex) {
throw new CertificateVerificationException(ex.getMessage());
} catch (GeneralSecurityException ex) {
throw new CertificateVerificationException(ex.getMessage());
} catch (IOException ex) {
throw new CertificateVerificationException("Error converting certificate chain to java.security format.");
}
}
Aggregations