Example usage for org.bouncycastle.asn1.x500 RDN getTypesAndValues

List of usage examples for org.bouncycastle.asn1.x500 RDN getTypesAndValues

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.x500 RDN getTypesAndValues.

Prototype

public AttributeTypeAndValue[] getTypesAndValues() 

Source Link

Usage

From source file:mitm.common.security.certificate.X500PrincipalInspector.java

License:Open Source License

private String rDNToString(RDN rdn) {
    String result = null;/*  w  ww.  j  a v  a 2s  .c om*/

    if (rdn.isMultiValued()) {
        /*
         * We currently do not support multi-value RDNs so if multi valued, combine them into one 
         * string with +
         */
        AttributeTypeAndValue[] values = rdn.getTypesAndValues();

        StrBuilder sb = new StrBuilder();

        for (AttributeTypeAndValue value : values) {
            sb.appendSeparator('+');
            sb.append(IETFUtils.valueToString(value.getValue()));

            result = sb.toString();
        }
    } else {
        result = IETFUtils.valueToString(rdn.getFirst().getValue());
    }

    return StringUtils.defaultString(result);
}

From source file:net.sf.keystore_explorer.crypto.x509.X509Ext.java

License:Open Source License

private String getDistributionPointNameString(DistributionPointName distributionPointName, String baseIndent)
        throws IOException {
    // @formatter:off

    /*//from   w w w  . j a v  a  2 s  .com
     * DistributionPointName ::= CHOICE { fullname [0] GeneralNames,
     * nameRelativeToCRLIssuer [1] RelativeDistinguishedName }
     *
     * RelativeDistinguishedName ::= SET SIZE (1 .. MAX) OF
     * AttributeTypeAndValue
     *
     * AttributeTypeAndValue ::= ASN1Sequence { type AttributeType, value
     * AttributeValue }
     */

    // @formatter: on

    StringBuilder sb = new StringBuilder();

    sb.append(baseIndent);
    sb.append(res.getString("DistributionPointName"));
    sb.append(NEWLINE);

    if (distributionPointName.getType() == DistributionPointName.FULL_NAME) {
        sb.append(baseIndent);
        sb.append(INDENT);
        sb.append(res.getString("DistributionPointFullName"));
        sb.append(NEWLINE);

        GeneralNames generalNames = GeneralNames.getInstance(distributionPointName.getName());

        for (GeneralName generalName : generalNames.getNames()) {
            sb.append(baseIndent);
            sb.append(INDENT);
            sb.append(INDENT);
            sb.append(GeneralNameUtil.toString(generalName));
            sb.append(NEWLINE);
        }
    } else {
        // DistributionPointName.TAG_NAMERELATIVETOCRLISSUER
        sb.append(baseIndent);
        sb.append(INDENT);
        sb.append(res.getString("DistributionPointNameRelativeToCrlIssuer"));
        sb.append(NEWLINE);

        RDN rdn = RDN.getInstance(distributionPointName.getName());

        for (AttributeTypeAndValue attributeTypeAndValue : rdn.getTypesAndValues()) {
            ASN1ObjectIdentifier attributeType = attributeTypeAndValue.getType();
            ASN1Encodable attributeValue = attributeTypeAndValue.getValue();

            String attributeTypeStr = getAttributeTypeString(attributeType);
            String attributeValueStr = getAttributeValueString(attributeType, attributeValue);

            sb.append(baseIndent);
            sb.append(INDENT);
            sb.append(INDENT);
            sb.append(MessageFormat.format("{0}={1}", attributeTypeStr, attributeValueStr));
            sb.append(NEWLINE);
        }
    }

    return sb.toString();
}

From source file:org.apache.jmeter.assertions.SMIMEAssertion.java

License:Apache License

/**
 * Extract email addresses from a certificate
 * /*from   w  ww. j  av a2 s  .  c  o m*/
 * @param cert the X509 certificate holder
 * @return a List of all email addresses found
 * @throws CertificateException
 */
private static List<String> getEmailFromCert(X509CertificateHolder cert) throws CertificateException {
    List<String> res = new ArrayList<>();

    X500Name subject = cert.getSubject();
    for (RDN emails : subject.getRDNs(BCStyle.EmailAddress)) {
        for (AttributeTypeAndValue emailAttr : emails.getTypesAndValues()) {
            log.debug("Add email from RDN: " + IETFUtils.valueToString(emailAttr.getValue()));
            res.add(IETFUtils.valueToString(emailAttr.getValue()));
        }
    }

    Extension subjectAlternativeNames = cert.getExtension(Extension.subjectAlternativeName);
    if (subjectAlternativeNames != null) {
        for (GeneralName name : GeneralNames.getInstance(subjectAlternativeNames.getParsedValue()).getNames()) {
            if (name.getTagNo() == GeneralName.rfc822Name) {
                String email = IETFUtils.valueToString(name.getName());
                log.debug("Add email from subjectAlternativeName: " + email);
                res.add(email);
            }
        }
    }

    return res;
}

From source file:org.cesecore.certificates.ca.X509CATest.java

License:Open Source License

private static ASN1Encodable getValueFromDN(Certificate cert, ASN1ObjectIdentifier oid) {
    final X500Principal principal = ((X509Certificate) cert).getSubjectX500Principal();
    final X500Name xname = X500Name.getInstance(principal.getEncoded());
    final RDN rdn = xname.getRDNs(oid)[0];
    return rdn.getTypesAndValues()[0].getValue();
}

From source file:org.cesecore.util.CertTools.java

License:Open Source License

/**
 * Method used to insert a CN postfix into DN by extracting the first found CN appending cnpostfix and then replacing the original CN with the new
 * one in DN./*from   w w  w  .  ja  v a 2  s .co m*/
 * 
 * If no CN could be found in DN then should the given DN be returned untouched
 * 
 * @param dn the DN to manipulate, cannot be null
 * @param cnpostfix the postfix to insert, cannot be null
 * @param nameStyle Controls how the name is encoded. Usually it should be a CeSecoreNameStyle.
 * @return the new DN
 */
public static String insertCNPostfix(String dn, String cnpostfix, X500NameStyle nameStyle) {
    if (log.isTraceEnabled()) {
        log.trace(">insertCNPostfix: dn=" + dn + ", cnpostfix=" + cnpostfix);
    }
    if (dn == null) {
        return null;
    }
    final RDN[] rdns = IETFUtils.rDNsFromString(dn, nameStyle);
    final X500NameBuilder nameBuilder = new X500NameBuilder(nameStyle);
    boolean replaced = false;
    for (final RDN rdn : rdns) {
        final AttributeTypeAndValue[] attributeTypeAndValues = rdn.getTypesAndValues();
        for (final AttributeTypeAndValue atav : attributeTypeAndValues) {
            if (atav.getType() != null) {
                final String currentSymbol = CeSecoreNameStyle.DefaultSymbols.get(atav.getType());
                if (!replaced && "CN".equals(currentSymbol)) {
                    nameBuilder.addRDN(atav.getType(), IETFUtils.valueToString(atav.getValue()) + cnpostfix);
                    replaced = true;
                } else {
                    nameBuilder.addRDN(atav);
                }
            }
        }
    }
    final String ret = nameBuilder.build().toString();
    if (log.isTraceEnabled()) {
        log.trace("<reverseDN: " + ret);
    }
    return ret;
}

From source file:org.cryptacular.x509.dn.NameReader.java

License:Open Source License

/**
 * Converts the given X.500 principal to a list of relative distinguished
 * names that contains the attributes comprising the DN.
 *
 * @param  principal  Principal to convert.
 *
 * @return  X500 principal as an RDN sequence.
 *///from w ww.j  a v  a  2 s.  com
public static RDNSequence readX500Principal(final X500Principal principal) {
    final X500Name name = X500Name.getInstance(principal.getEncoded());
    final RDNSequence sequence = new RDNSequence();
    for (org.bouncycastle.asn1.x500.RDN rdn : name.getRDNs()) {
        final Attributes attributes = new Attributes();
        for (AttributeTypeAndValue tv : rdn.getTypesAndValues()) {
            attributes.add(tv.getType().getId(), tv.getValue().toString());
        }
        sequence.add(new RDN(attributes));
    }
    return sequence;
}

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.
 *///from  ww w .j ava  2 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.jruby.ext.openssl.Request.java

License:LGPL

private IRubyObject makeRubyName(X500Name name) {
    if (name == null)
        return getRuntime().getNil();

    IRubyObject newName = Utils.newRubyInstance(getRuntime(), "OpenSSL::X509::Name");

    for (RDN rdn : name.getRDNs()) {
        for (AttributeTypeAndValue tv : rdn.getTypesAndValues()) {
            ASN1ObjectIdentifier oid = tv.getType();
            String val = null;
            if (tv.getValue() instanceof ASN1String) {
                val = ((ASN1String) tv.getValue()).getString();
            }/*from w  w  w  .  ja  va2 s.  com*/
            RubyFixnum typef = getRuntime().newFixnum(ASN1.idForClass(tv.getValue().getClass())); //TODO correct?
            ((X509Name) newName).addEntry(oid, val, typef);
        }
    }

    return newName;
}

From source file:org.jruby.ext.openssl.X509Extension.java

License:LGPL

public static ByteList appendRDN(final ByteList out, final RDN rdn,
        final Map<ASN1ObjectIdentifier, String> oidSymbols) {

    if (rdn.isMultiValued()) {
        AttributeTypeAndValue[] atv = rdn.getTypesAndValues();

        boolean firstAtv = true;
        for (int j = 0; j != atv.length; j++) {
            if (firstAtv)
                firstAtv = false;/*from   w w w  .ja v a  2 s .c om*/
            else
                out.append('+');

            appendTypeAndValue(out, atv[j], oidSymbols);
        }
        return out;
    }
    return appendTypeAndValue(out, rdn.getFirst(), oidSymbols);
}

From source file:org.jruby.ext.openssl.X509Name.java

License:LGPL

private void fromRDNElement(Object element) {
    RDN rdn = (RDN) element;
    for (AttributeTypeAndValue tv : rdn.getTypesAndValues()) {
        oids.add(tv.getType());/*  w  w w .ja  v a2  s. com*/
        if (tv.getValue() instanceof ASN1String) {
            values.add(((ASN1String) tv.getValue()).getString());
        } else {
            values.add(null); //TODO really?
        }
        types.add(getRuntime().newFixnum(ASN1.idForClass(tv.getValue().getClass())));
    }
}