Skip to content

Instantly share code, notes, and snippets.

@venator85
Created October 24, 2016 13:50
Show Gist options
  • Save venator85/c677ebcd93ef4c339331314608acde25 to your computer and use it in GitHub Desktop.
Save venator85/c677ebcd93ef4c339331314608acde25 to your computer and use it in GitHub Desktop.
Manually verify a certificate (chain) against a root
public static void validateCertificate(X509Certificate toVerify, X509Certificate root) throws Exception {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
CertPath cp = cf.generateCertPath(Collections.singletonList(toVerify));
TrustAnchor trustAnchor = new TrustAnchor(root, null);
CertPathValidator cpv = CertPathValidator.getInstance("PKIX");
PKIXParameters pkixParams = new PKIXParameters(Collections.singleton(trustAnchor));
pkixParams.setRevocationEnabled(false);
cpv.validate(cp, pkixParams);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment