📄 ldapstorehelper.java
字号:
package org.bouncycastle.x509.util;import org.bouncycastle.asn1.ASN1InputStream;import org.bouncycastle.asn1.x509.CertificatePair;import org.bouncycastle.asn1.x509.X509CertificateStructure;import org.bouncycastle.jce.X509LDAPCertStoreParameters;import org.bouncycastle.jce.provider.X509AttrCertParser;import org.bouncycastle.jce.provider.X509CRLParser;import org.bouncycastle.jce.provider.X509CertPairParser;import org.bouncycastle.jce.provider.X509CertParser;import org.bouncycastle.util.StoreException;import org.bouncycastle.x509.X509AttributeCertStoreSelector;import org.bouncycastle.x509.X509AttributeCertificate;import org.bouncycastle.x509.X509CRLStoreSelector;import org.bouncycastle.x509.X509CertPairStoreSelector;import org.bouncycastle.x509.X509CertStoreSelector;import org.bouncycastle.x509.X509CertificatePair;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.Principal;import java.security.cert.CertificateParsingException;import java.security.cert.X509CRL;import java.security.cert.X509Certificate;import java.sql.Date;import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set;/** * This is a general purpose implementation to get X.509 certificates, CRLs, * attribute certificates and cross certificates 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 {@link org.bouncycastle.x509.X509CertStoreSelector} or * {@link org.bouncycastle.x509.X509AttributeCertificate} is given with that * details. * <p/> * For the used schemes see: * <ul> * <li><a href="http://www.ietf.org/rfc/rfc2587.txt">RFC 2587</a> * <li><a * href="http://www3.ietf.org/proceedings/01mar/I-D/pkix-ldap-schema-01.txt">Internet * X.509 Public Key Infrastructure Additional LDAP Schema for PKIs and PMIs</a> * </ul> */public class LDAPStoreHelper{ // TODO: cache results private X509LDAPCertStoreParameters params; public LDAPStoreHelper(X509LDAPCertStoreParameters params) { this.params = 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 dNAttributeName) { String temp = subject; int begin = temp.toLowerCase().indexOf( dNAttributeName.toLowerCase() + "="); if (begin == -1) { return ""; } temp = temp.substring(begin + dNAttributeName.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; } private Set createCerts(List list, X509CertStoreSelector xselector) throws StoreException { Set certSet = new HashSet(); Iterator it = list.iterator(); X509CertParser parser = new X509CertParser(); while (it.hasNext()) { try { parser.engineInit(new ByteArrayInputStream((byte[])it .next())); X509Certificate cert = (X509Certificate)parser .engineRead(); if (xselector.match((Object)cert)) { certSet.add(cert); } } catch (Exception e) { } } return certSet; } /** * Can use the subject and serial and the subject and serialNumber of the * certificate of the given of the X509CertStoreSelector. If a certificate * for checking is given this has higher precedence. * * @param xselector The selector with the search criteria. * @param attrs Attributes which contain the certificates in the LDAP * directory. * @param attrNames Attribute names in teh LDAP directory which correspond to the * subjectAttributeNames. * @param subjectAttributeNames Subject attribute names (like "CN", "O", "OU") to use to * search in the LDAP directory * @return A list of found DER encoded certificates. * @throws StoreException if an error occurs while searching. */ private List certSubjectSerialSearch(X509CertStoreSelector xselector, String[] attrs, String attrNames[], String subjectAttributeNames[]) throws StoreException { // TODO: support also subjectAltNames? List list = new ArrayList(); String subject = null; String serial = null; subject = getSubjectAsString(xselector); if (xselector.getSerialNumber() != null) { serial = xselector.getSerialNumber().toString(); } if (xselector.getCertificate() != null) { subject = xselector.getCertificate().getSubjectX500Principal().getName("RFC1779"); serial = xselector.getCertificate().getSerialNumber().toString(); } String attrValue = null; if (subject != null) { for (int i = 0; i < subjectAttributeNames.length; i++) { attrValue = parseDN(subject, subjectAttributeNames[i]); list .addAll(search(attrNames, "*" + attrValue + "*", attrs)); } } if (serial != null && params.getSearchForSerialNumberIn() != null) { attrValue = serial; list.addAll(search( splitString(params.getSearchForSerialNumberIn()), attrValue, attrs)); } if (serial == null && subject == null) { list.addAll(search(attrNames, "*", attrs)); } return list; } /** * Can use the subject of the forward certificate of the set certificate * pair or the subject of the forward * {@link org.bouncycastle.x509.X509CertStoreSelector} of the given * selector. * * @param xselector The selector with the search criteria. * @param attrs Attributes which contain the attribute certificates in the * LDAP directory. * @param attrNames Attribute names in the LDAP directory which correspond to the * subjectAttributeNames. * @param subjectAttributeNames Subject attribute names (like "CN", "O", "OU") to use to * search in the LDAP directory * @return A list of found DER encoded certificate pairs. * @throws StoreException if an error occurs while searching. */ private List crossCertificatePairSubjectSearch( X509CertPairStoreSelector xselector, String[] attrs, String attrNames[], String subjectAttributeNames[]) throws StoreException { List list = new ArrayList(); // search for subject String subject = null; if (xselector.getForwardSelector() != null) { subject = getSubjectAsString(xselector.getForwardSelector()); } if (xselector.getCertPair() != null) { if (xselector.getCertPair().getForward() != null) { subject = xselector.getCertPair().getForward() .getSubjectX500Principal().getName("RFC1779"); } } String attrValue = null; if (subject != null) { for (int i = 0; i < subjectAttributeNames.length; i++) { attrValue = parseDN(subject, subjectAttributeNames[i]); list .addAll(search(attrNames, "*" + attrValue + "*", attrs)); } } if (subject == null) { list.addAll(search(attrNames, "*", attrs)); } return list; } /** * Can use the entityName of the holder of the attribute certificate, the * serialNumber of attribute certificate and the serialNumber of the * associated certificate of the given of the X509AttributeCertSelector. * * @param xselector The selector with the search criteria. * @param attrs Attributes which contain the attribute certificates in the * LDAP directory. * @param attrNames Attribute names in the LDAP directory which correspond to the * subjectAttributeNames. * @param subjectAttributeNames Subject attribute names (like "CN", "O", "OU") to use to * search in the LDAP directory * @return A list of found DER encoded attribute certificates. * @throws StoreException if an error occurs while searching. */ private List attrCertSubjectSerialSearch( X509AttributeCertStoreSelector xselector, String[] attrs, String attrNames[], String subjectAttributeNames[]) throws StoreException { List list = new ArrayList(); // search for serialNumber of associated cert, // serialNumber of the attribute certificate or DN in the entityName // of the holder String subject = null; String serial = null; Collection serials = new HashSet(); Principal principals[] = null; if (xselector.getHolder() != null) { // serialNumber of associated cert if (xselector.getHolder().getSerialNumber() != null) { serials.add(xselector.getHolder().getSerialNumber() .toString()); } // DN in the entityName of the holder if (xselector.getHolder().getEntityNames() != null) { principals = xselector.getHolder().getEntityNames(); } } if (xselector.getAttributeCert() != null) { if (xselector.getAttributeCert().getHolder().getEntityNames() != null) { principals = xselector.getAttributeCert().getHolder() .getEntityNames(); } // serialNumber of the attribute certificate serials.add(xselector.getAttributeCert().getSerialNumber() .toString()); } if (principals != null) { // only first should be relevant if (principals[0] instanceof X500Principal) { subject = ((X500Principal)principals[0]) .getName("RFC1779"); } else { // strange ...
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -