📄 x509ldapcertstorespi.java
字号:
package org.bouncycastle.jce.provider;import org.bouncycastle.asn1.ASN1InputStream;import org.bouncycastle.asn1.x509.CertificatePair;import org.bouncycastle.jce.X509LDAPCertStoreParameters;import javax.naming.Context;import javax.naming.NamingEnumeration;import javax.naming.NamingException;import javax.naming.directory.Attribute;import javax.naming.directory.DirContext;import javax.naming.directory.InitialDirContext;import javax.naming.directory.SearchControls;import javax.naming.directory.SearchResult;import javax.security.auth.x500.X500Principal;import java.io.ByteArrayInputStream;import java.io.IOException;import java.security.InvalidAlgorithmParameterException;import java.security.cert.CRL;import java.security.cert.CRLSelector;import java.security.cert.CertSelector;import java.security.cert.CertStoreException;import java.security.cert.CertStoreParameters;import java.security.cert.CertStoreSpi;import java.security.cert.Certificate;import java.security.cert.CertificateFactory;import java.security.cert.X509CRLSelector;import java.security.cert.X509CertSelector;import java.util.ArrayList;import java.util.Collection;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Properties;import java.util.Set;/** * * This is a general purpose implementation to get X.509 certificates and CRLs * from a LDAP location. * <p> * At first a search is performed in the ldap*AttributeNames of the * {@link org.bouncycastle.jce.X509LDAPCertStoreParameters} with the given * information of the subject (for all kind of certificates) or issuer (for * CRLs), respectively, if a X509CertSelector is given with that details. For * CRLs, CA certificates and cross certificates a coarse search is made only for * entries with that content to get more possibly matchign results. */public class X509LDAPCertStoreSpi extends CertStoreSpi{ private X509LDAPCertStoreParameters params; public X509LDAPCertStoreSpi(CertStoreParameters params) throws InvalidAlgorithmParameterException { super(params); if (!(params instanceof X509LDAPCertStoreParameters)) { throw new InvalidAlgorithmParameterException( X509LDAPCertStoreSpi.class.getName() + ": parameter must be a " + X509LDAPCertStoreParameters.class.getName() + " object\n" + params.toString()); } this.params = (X509LDAPCertStoreParameters)params; } /** * Initial Context Factory. */ private static String LDAP_PROVIDER = "com.sun.jndi.ldap.LdapCtxFactory"; /** * Processing referrals.. */ private static String REFERRALS_IGNORE = "ignore"; /** * Security level to be used for LDAP connections. */ private static final String SEARCH_SECURITY_LEVEL = "none"; /** * Package Prefix for loading URL context factories. */ private static final String URL_CONTEXT_PREFIX = "com.sun.jndi.url"; private DirContext connectLDAP() throws NamingException { Properties props = new Properties(); props.setProperty(Context.INITIAL_CONTEXT_FACTORY, LDAP_PROVIDER); props.setProperty(Context.BATCHSIZE, "0"); props.setProperty(Context.PROVIDER_URL, params.getLdapURL()); props.setProperty(Context.URL_PKG_PREFIXES, URL_CONTEXT_PREFIX); props.setProperty(Context.REFERRAL, REFERRALS_IGNORE); props.setProperty(Context.SECURITY_AUTHENTICATION, SEARCH_SECURITY_LEVEL); DirContext ctx = new InitialDirContext(props); return ctx; } private String parseDN(String subject, String subjectAttributeName) { String temp = subject; int begin = temp.toLowerCase().indexOf( subjectAttributeName.toLowerCase()); temp = temp.substring(begin + subjectAttributeName.length()); int end = temp.indexOf(','); if (end == -1) { end = temp.length(); } while (temp.charAt(end - 1) == '\\') { end = temp.indexOf(',', end + 1); if (end == -1) { end = temp.length(); } } temp = temp.substring(0, end); begin = temp.indexOf('='); temp = temp.substring(begin + 1); if (temp.charAt(0) == ' ') { temp = temp.substring(1); } if (temp.startsWith("\"")) { temp = temp.substring(1); } if (temp.endsWith("\"")) { temp = temp.substring(0, temp.length() - 1); } return temp; } public Collection engineGetCertificates(CertSelector selector) throws CertStoreException { if (!(selector instanceof X509CertSelector)) { throw new CertStoreException("selector is not a X509CertSelector"); } X509CertSelector xselector = (X509CertSelector)selector; Set certSet = new HashSet(); Set set = getEndCertificates(xselector); set.addAll(getCACertificates(xselector)); set.addAll(getCrossCertificates(xselector)); Iterator it = set.iterator(); try { CertificateFactory cf = CertificateFactory.getInstance("X.509", "BC"); while (it.hasNext()) { byte[] bytes = (byte[])it.next(); if (bytes == null || bytes.length == 0) { continue; } List bytesList = new ArrayList(); bytesList.add(bytes); try { CertificatePair pair = CertificatePair .getInstance(new ASN1InputStream(bytes) .readObject()); bytesList.clear(); if (pair.getForward() != null) { bytesList.add(pair.getForward().getEncoded()); } if (pair.getReverse() != null) { bytesList.add(pair.getReverse().getEncoded()); } } catch (IOException e) { } catch (IllegalArgumentException e) { } for (Iterator it2 = bytesList.iterator(); it2.hasNext();) { ByteArrayInputStream bIn = new ByteArrayInputStream( (byte[])it2.next()); try { Certificate cert = cf.generateCertificate(bIn); // System.out.println(((X509Certificate) // cert).getSubjectX500Principal()); if (xselector.match(cert)) { certSet.add(cert); } } catch (Exception e) { } } } } catch (Exception e) { throw new CertStoreException( "certificate cannot be constructed from LDAP result: " + e); } return certSet; } private Set certSubjectSerialSearch(X509CertSelector xselector, String[] attrs, String attrName, String subjectAttributeName) throws CertStoreException { Set set = new HashSet(); try { if (xselector.getSubjectAsBytes() != null || xselector.getSubjectAsString() != null || xselector.getCertificate() != null) { String subject = null; String serial = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -