Example usage for org.bouncycastle.asn1.x500 X500NameBuilder addRDN

List of usage examples for org.bouncycastle.asn1.x500 X500NameBuilder addRDN

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.x500 X500NameBuilder addRDN.

Prototype

public X500NameBuilder addRDN(AttributeTypeAndValue attrTAndV) 

Source Link

Document

Add an RDN based on the passed in AttributeTypeAndValue.

Usage

From source file:org.ejbca.core.model.ca.publisher.LdapPublisher.java

License:Open Source License

/**
 * Constructs the LDAP DN for a certificate to be published. Only DN objects defined by the publisher is used.
 * For each DN object to be published:/* ww w.j  a  v a  2  s.  c  om*/
 *  First the certificate DN is search for this object.
 *  If no such certificate object then the userdata DN is searched.
 *  If no such userdata object either the object will not be a part of the LDAP DN.
 * @param certDN certificate DN
 * @param userDataDN user data DN
 * @return LDAP DN to be used.
 */
protected String constructLDAPDN(String certDN, String userDataDN) {
    if (log.isDebugEnabled()) {
        log.debug("DN in certificate '" + certDN + "'. DN in user data '" + userDataDN + "'.");
    }
    final DNFieldExtractor certExtractor = new DNFieldExtractor(certDN, DNFieldExtractor.TYPE_SUBJECTDN);
    final DNFieldExtractor userDataExtractor = userDataDN != null
            ? new DNFieldExtractor(userDataDN, DNFieldExtractor.TYPE_SUBJECTDN)
            : null;

    Collection<Integer> usefields = getUseFieldInLdapDN();
    if (usefields instanceof List<?>) {
        Collections.sort((List<Integer>) usefields);
    }
    final X500NameBuilder nameBuilder = new X500NameBuilder(LdapNameStyle.INSTANCE);
    for (Integer fieldNum : usefields) { // There must be at least one
        String dnField = certExtractor.getFieldString(fieldNum);
        if (StringUtils.isEmpty(dnField) && userDataExtractor != null) {
            dnField = userDataExtractor.getFieldString(fieldNum);
        }

        if (StringUtils.isNotEmpty(dnField)) {
            RDN rdn = new X500Name(LdapNameStyle.INSTANCE, dnField).getRDNs()[0];
            nameBuilder.addRDN(rdn.getFirst());
        }
    }

    String retval = nameBuilder.build().toString() + "," + this.getBaseDN();
    if (log.isDebugEnabled()) {
        log.debug("LdapPublisher: constructed DN: " + retval);
    }
    return retval;
}

From source file:org.ejbca.util.LdapTools.java

License:Open Source License

/**
 * Returns all intermediate DNs in a given DN under a base DN, in the order from the
 * first one below the base DN and further down.
 *///  ww  w.j  av  a 2  s .co  m
public static List<String> getIntermediateDNs(String dn, String baseDN) {
    // Remove the base DN
    if (!dn.endsWith(baseDN))
        return new ArrayList<String>();
    final String subDN = dn.substring(0, dn.length() - baseDN.length());

    // Split and escape the DN (but ignore the lowest level component)
    final List<String> components = new ArrayList<String>();
    for (String comp : CertTools.getX500NameComponents(CertTools.getParentDN(subDN))) {
        if (!StringUtils.isEmpty(comp)) {
            components.add(LDAPDN.escapeRDN(comp));
        }
    }

    // Add each intermediate DN
    final List<String> ret = new ArrayList<String>();
    for (int start = components.size() - 1; start >= 0; start--) {
        final List<String> intermComps = components.subList(start, components.size());
        final X500NameBuilder nameBuilder = new X500NameBuilder(LdapNameStyle.INSTANCE);
        for (String comp : intermComps) {
            final RDN rdn = new X500Name(LdapNameStyle.INSTANCE, comp).getRDNs()[0];
            nameBuilder.addRDN(rdn.getFirst());
        }
        ret.add(nameBuilder.build().toString() + "," + baseDN);
    }
    return ret;
}

From source file:org.ejbca.util.LdapToolsTest.java

License:Open Source License

/**
 * Tests name builder with LdapNameStyle class which is used by the LdapTools class.
 *//* w  w w .j a  v a2  s.  c o m*/
@Test
public void test05BuildLdapNameStyle() {
    X500Name ldapName = new X500Name(LdapNameStyle.INSTANCE, LDAP_TEST_DN);

    // LdapNameStyle should return a DN with MAIL and SERIALNUMBER
    X500NameBuilder ldapNameBuilder = new X500NameBuilder(LdapNameStyle.INSTANCE);
    for (RDN rdn : ldapName.getRDNs()) {
        for (AttributeTypeAndValue atv : rdn.getTypesAndValues()) {
            ldapNameBuilder.addRDN(atv);
        }
    }
    assertEquals(LDAP_TEST_DN.toLowerCase(), ldapNameBuilder.build().toString().toLowerCase());

    // CesecoreNameStyle should return a DN with E and SN
    X500NameBuilder cesecoreNameBuilder = new X500NameBuilder(CeSecoreNameStyle.INSTANCE);
    for (RDN rdn : ldapName.getRDNs()) {
        for (AttributeTypeAndValue atv : rdn.getTypesAndValues()) {
            cesecoreNameBuilder.addRDN(atv);
        }
    }
    assertEquals("cn=test person,e=test@example.com,sn=123456-7890",
            cesecoreNameBuilder.build().toString().toLowerCase());
}

From source file:org.metaeffekt.dcc.commons.pki.CertificateManager.java

License:Apache License

protected X500NameBuilder createNameBuilder(String prefix) {
    final X500NameBuilder nameBuilder = new X500NameBuilder();

    String subjectString = getProperty(prefix);
    if (subjectString != null) {
        RDN[] rdns = BCStyle.INSTANCE.fromString(subjectString);
        for (RDN rdn : rdns) {
            if (rdn.isMultiValued()) {
                nameBuilder.addMultiValuedRDN(rdn.getTypesAndValues());
            } else {
                nameBuilder.addRDN(rdn.getFirst());
            }//www.  j  a v  a  2 s  . c om
        }
    }

    // multiple attributes can be added using an array-like notation
    for (Object key : componentProperties.keySet()) {
        final String attributeKey = String.valueOf(key);
        if (attributeKey.startsWith(prefix + ".")) {
            String attributeName = attributeKey.substring(prefix.length() + 1);
            if (attributeName.contains("[")) {
                attributeName = attributeName.substring(0, attributeName.indexOf("["));
                final ASN1ObjectIdentifier oid = BCStyle.INSTANCE.attrNameToOID(attributeName);
                nameBuilder.addRDN(oid, getProperty(attributeKey));
            }
        }
    }

    // the prefix.CN specifies the main CN. Per default it is the component
    // name in upper case.
    String componentCN = getProperty(prefix + "." + PROPERTY_CN, componentName.toUpperCase());
    nameBuilder.addRDN(BCStyle.CN, componentCN);

    return nameBuilder;
}