|
| 1 | +package com.acuity.iot.dsa.dslink.sys.cert; |
| 2 | + |
| 3 | +import java.io.ByteArrayInputStream; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStream; |
| 6 | +import java.net.MalformedURLException; |
| 7 | +import java.net.URL; |
| 8 | +import java.security.cert.CRLException; |
| 9 | +import java.security.cert.CertificateException; |
| 10 | +import java.security.cert.CertificateFactory; |
| 11 | +import java.security.cert.CertificateParsingException; |
| 12 | +import java.security.cert.X509CRL; |
| 13 | +import java.security.cert.X509Certificate; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.Hashtable; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +import javax.naming.Context; |
| 19 | +import javax.naming.NamingException; |
| 20 | +import javax.naming.directory.Attribute; |
| 21 | +import javax.naming.directory.Attributes; |
| 22 | +import javax.naming.directory.DirContext; |
| 23 | +import javax.naming.directory.InitialDirContext; |
| 24 | + |
| 25 | +import org.bouncycastle.asn1.ASN1InputStream; |
| 26 | +import org.bouncycastle.asn1.ASN1Primitive; |
| 27 | +import org.bouncycastle.asn1.DERIA5String; |
| 28 | +import org.bouncycastle.asn1.DEROctetString; |
| 29 | +import org.bouncycastle.asn1.x509.CRLDistPoint; |
| 30 | +import org.bouncycastle.asn1.x509.DistributionPoint; |
| 31 | +import org.bouncycastle.asn1.x509.DistributionPointName; |
| 32 | +import org.bouncycastle.asn1.x509.GeneralName; |
| 33 | +import org.bouncycastle.asn1.x509.GeneralNames; |
| 34 | +import org.bouncycastle.asn1.x509.X509Extensions; |
| 35 | + |
| 36 | +/** |
| 37 | + * Class that verifies CRLs for given X509 certificate. Extracts the CRL |
| 38 | + * distribution points from the certificate (if available) and checks the |
| 39 | + * certificate revocation status against the CRLs coming from the |
| 40 | + * distribution points. Supports HTTP, HTTPS, FTP and LDAP based URLs. |
| 41 | + * |
| 42 | + * @author Svetlin Nakov |
| 43 | + */ |
| 44 | +public class CRLVerifier { |
| 45 | + |
| 46 | + /** |
| 47 | + * Extracts the CRL distribution points from the certificate (if available) |
| 48 | + * and checks the certificate revocation status against the CRLs coming from |
| 49 | + * the distribution points. Supports HTTP, HTTPS, FTP and LDAP based URLs. |
| 50 | + * |
| 51 | + * @param cert the certificate to be checked for revocation |
| 52 | + * @throws CertificateVerificationException if the certificate is revoked |
| 53 | + */ |
| 54 | + public static void verifyCertificateCRLs(X509Certificate cert) |
| 55 | + throws CertificateVerificationException { |
| 56 | + try { |
| 57 | + List<String> crlDistPoints = getCrlDistributionPoints(cert); |
| 58 | + for (String crlDP : crlDistPoints) { |
| 59 | + X509CRL crl = downloadCRL(crlDP); |
| 60 | + if (crl.isRevoked(cert)) { |
| 61 | + throw new CertificateVerificationException( |
| 62 | + "The certificate is revoked by CRL: " + crlDP); |
| 63 | + } |
| 64 | + } |
| 65 | + } catch (Exception ex) { |
| 66 | + if (ex instanceof CertificateVerificationException) { |
| 67 | + throw (CertificateVerificationException) ex; |
| 68 | + } else { |
| 69 | + throw new CertificateVerificationException( |
| 70 | + "Can not verify CRL for certificate: " + |
| 71 | + cert.getSubjectX500Principal()); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Downloads CRL from given URL. Supports http, https, ftp and ldap based URLs. |
| 78 | + */ |
| 79 | + private static X509CRL downloadCRL(String crlURL) throws IOException, |
| 80 | + CertificateException, CRLException, |
| 81 | + CertificateVerificationException, NamingException { |
| 82 | + if (crlURL.startsWith("http://") || crlURL.startsWith("https://") |
| 83 | + || crlURL.startsWith("ftp://")) { |
| 84 | + X509CRL crl = downloadCRLFromWeb(crlURL); |
| 85 | + return crl; |
| 86 | + } else if (crlURL.startsWith("ldap://")) { |
| 87 | + X509CRL crl = downloadCRLFromLDAP(crlURL); |
| 88 | + return crl; |
| 89 | + } else { |
| 90 | + throw new CertificateVerificationException( |
| 91 | + "Can not download CRL from certificate " + |
| 92 | + "distribution point: " + crlURL); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Downloads a CRL from given LDAP url, e.g. |
| 98 | + * ldap://ldap.infonotary.com/dc=identity-ca,dc=infonotary,dc=com |
| 99 | + */ |
| 100 | + private static X509CRL downloadCRLFromLDAP(String ldapURL) |
| 101 | + throws CertificateException, NamingException, CRLException, |
| 102 | + CertificateVerificationException { |
| 103 | + Hashtable<String , String> env = new Hashtable<String , String>(); |
| 104 | + env.put(Context.INITIAL_CONTEXT_FACTORY, |
| 105 | + "com.sun.jndi.ldap.LdapCtxFactory"); |
| 106 | + env.put(Context.PROVIDER_URL, ldapURL); |
| 107 | + |
| 108 | + DirContext ctx = new InitialDirContext(env); |
| 109 | + Attributes avals = ctx.getAttributes(""); |
| 110 | + Attribute aval = avals.get("certificateRevocationList;binary"); |
| 111 | + byte[] val = (byte[])aval.get(); |
| 112 | + if ((val == null) || (val.length == 0)) { |
| 113 | + throw new CertificateVerificationException( |
| 114 | + "Can not download CRL from: " + ldapURL); |
| 115 | + } else { |
| 116 | + InputStream inStream = new ByteArrayInputStream(val); |
| 117 | + CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
| 118 | + X509CRL crl = (X509CRL)cf.generateCRL(inStream); |
| 119 | + return crl; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Downloads a CRL from given HTTP/HTTPS/FTP URL, e.g. |
| 125 | + * http://crl.infonotary.com/crl/identity-ca.crl |
| 126 | + */ |
| 127 | + private static X509CRL downloadCRLFromWeb(String crlURL) |
| 128 | + throws MalformedURLException, IOException, CertificateException, |
| 129 | + CRLException { |
| 130 | + URL url = new URL(crlURL); |
| 131 | + InputStream crlStream = url.openStream(); |
| 132 | + try { |
| 133 | + CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
| 134 | + X509CRL crl = (X509CRL) cf.generateCRL(crlStream); |
| 135 | + return crl; |
| 136 | + } finally { |
| 137 | + crlStream.close(); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Extracts all CRL distribution point URLs from the "CRL Distribution Point" |
| 143 | + * extension in a X.509 certificate. If CRL distribution point extension is |
| 144 | + * unavailable, returns an empty list. |
| 145 | + */ |
| 146 | + public static List<String> getCrlDistributionPoints( |
| 147 | + X509Certificate cert) throws CertificateParsingException, IOException { |
| 148 | + byte[] crldpExt = cert.getExtensionValue( |
| 149 | + X509Extensions.CRLDistributionPoints.getId()); |
| 150 | + if (crldpExt == null) { |
| 151 | + List<String> emptyList = new ArrayList<String>(); |
| 152 | + return emptyList; |
| 153 | + } |
| 154 | + ASN1InputStream oAsnInStream = new ASN1InputStream( |
| 155 | + new ByteArrayInputStream(crldpExt)); |
| 156 | + ASN1Primitive derObjCrlDP = oAsnInStream.readObject(); |
| 157 | + DEROctetString dosCrlDP = (DEROctetString) derObjCrlDP; |
| 158 | + byte[] crldpExtOctets = dosCrlDP.getOctets(); |
| 159 | + ASN1InputStream oAsnInStream2 = new ASN1InputStream( |
| 160 | + new ByteArrayInputStream(crldpExtOctets)); |
| 161 | + ASN1Primitive derObj2 = oAsnInStream2.readObject(); |
| 162 | + CRLDistPoint distPoint = CRLDistPoint.getInstance(derObj2); |
| 163 | + |
| 164 | + oAsnInStream.close(); |
| 165 | + oAsnInStream2.close(); |
| 166 | + |
| 167 | + List<String> crlUrls = new ArrayList<String>(); |
| 168 | + for (DistributionPoint dp : distPoint.getDistributionPoints()) { |
| 169 | + DistributionPointName dpn = dp.getDistributionPoint(); |
| 170 | + // Look for URIs in fullName |
| 171 | + if (dpn != null) { |
| 172 | + if (dpn.getType() == DistributionPointName.FULL_NAME) { |
| 173 | + GeneralName[] genNames = GeneralNames.getInstance( |
| 174 | + dpn.getName()).getNames(); |
| 175 | + // Look for an URI |
| 176 | + for (int j = 0; j < genNames.length; j++) { |
| 177 | + if (genNames[j].getTagNo() == GeneralName.uniformResourceIdentifier) { |
| 178 | + String url = DERIA5String.getInstance( |
| 179 | + genNames[j].getName()).getString(); |
| 180 | + crlUrls.add(url); |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + return crlUrls; |
| 187 | + } |
| 188 | + |
| 189 | +} |
0 commit comments