Created
November 27, 2018 14:50
-
-
Save markscottwright/e0154f610f5af5c3078a820e52d8b6ab to your computer and use it in GitHub Desktop.
Get the CA Issuer URLs from a X509Cert in java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.io.InputStream; | |
import java.security.cert.CertificateException; | |
import java.security.cert.CertificateFactory; | |
import java.security.cert.X509Certificate; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.bouncycastle.asn1.ASN1Primitive; | |
import org.bouncycastle.asn1.x509.AccessDescription; | |
import org.bouncycastle.asn1.x509.AuthorityInformationAccess; | |
import org.bouncycastle.asn1.x509.Extension; | |
import org.bouncycastle.asn1.x509.GeneralName; | |
import org.bouncycastle.cert.jcajce.JcaX509ExtensionUtils; | |
public class AIAFetcher { | |
public static void main(String[] args) throws IOException, | |
CertificateException { | |
try (InputStream certContents = AIAFetcher.class | |
.getResourceAsStream("/somecert.cer")) { | |
X509Certificate cert = (X509Certificate) CertificateFactory | |
.getInstance("X509").generateCertificate(certContents); | |
List<String> caIssuers = getCaIssuers(cert); | |
System.out.println(caIssuers); | |
} | |
} | |
private static List<String> getCaIssuers(X509Certificate cert) | |
throws IOException { | |
List<String> caIssuers = new ArrayList<>(); | |
ASN1Primitive aiaDer = JcaX509ExtensionUtils.parseExtensionValue( | |
cert.getExtensionValue(Extension.authorityInfoAccess.getId())); | |
AuthorityInformationAccess aia = AuthorityInformationAccess | |
.getInstance(aiaDer); | |
for (AccessDescription desc : aia.getAccessDescriptions()) { | |
if (desc.getAccessMethod() | |
.equals(AccessDescription.id_ad_caIssuers)) { | |
GeneralName loc = desc.getAccessLocation(); | |
if (loc.getTagNo() == GeneralName.uniformResourceIdentifier) | |
caIssuers.add(loc.getName().toString()); | |
} | |
} | |
return caIssuers; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment