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

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

Introduction

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

Prototype

public boolean isMultiValued() 

Source Link

Usage

From source file:eu.emi.security.authn.x509.helpers.proxy.ProxyHelper.java

License:Open Source License

public static String getLastCN(X500Name x500Name) throws IllegalArgumentException {
    RDN[] rdns = x500Name.getRDNs();
    if (rdns.length == 0)
        throw new IllegalArgumentException("The DN is empty");
    RDN last = rdns[rdns.length - 1];

    if (last.isMultiValued())
        throw new IllegalArgumentException("The DN is ended with a multivalued RDN");
    AttributeTypeAndValue cn = last.getFirst();
    if (!cn.getType().equals(BCStyle.CN))
        throw new IllegalArgumentException("The DN is not ended with a CN AVA");

    return IETFUtils.valueToString(cn.getValue());
}

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

License:Open Source License

private String rDNToString(RDN rdn) {
    String result = null;/*from   w  w  w.j a v  a 2  s  .  co  m*/

    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: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  ww.j a va  2s  .co  m*/
            else
                out.append('+');

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

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());
            }/*from   ww  w  . j  a v  a2  s  .c o  m*/
        }
    }

    // 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;
}

From source file:org.xipki.commons.security.util.X509Util.java

License:Open Source License

public static String getCommonName(final X500Name name) {
    ParamUtil.requireNonNull("name", name);
    RDN[] rdns = name.getRDNs(ObjectIdentifiers.DN_CN);
    if (rdns != null && rdns.length > 0) {
        RDN rdn = rdns[0];
        AttributeTypeAndValue atv = null;
        if (rdn.isMultiValued()) {
            for (AttributeTypeAndValue m : rdn.getTypesAndValues()) {
                if (m.getType().equals(ObjectIdentifiers.DN_CN)) {
                    atv = m;//from  w w w.  j  a  v  a  2s  .  c  o  m
                    break;
                }
            }
        } else {
            atv = rdn.getFirst();
        }
        return (atv == null) ? null : rdnValueToString(atv.getValue());
    }
    return null;
}

From source file:org.xipki.commons.security.util.X509Util.java

License:Open Source License

public static String canonicalizName(final X500Name name) {
    ParamUtil.requireNonNull("name", name);
    ASN1ObjectIdentifier[] tmpTypes = name.getAttributeTypes();
    int len = tmpTypes.length;
    List<String> types = new ArrayList<>(len);
    for (ASN1ObjectIdentifier type : tmpTypes) {
        types.add(type.getId());// w w w  .j  a v a  2 s  . c  o  m
    }

    Collections.sort(types);

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < len; i++) {
        String type = types.get(i);
        if (i > 0) {
            sb.append(",");
        }
        sb.append(type).append("=");
        RDN[] rdns = name.getRDNs(new ASN1ObjectIdentifier(type));

        List<String> values = new ArrayList<>(1);
        for (int j = 0; j < rdns.length; j++) {
            RDN rdn = rdns[j];
            if (rdn.isMultiValued()) {
                AttributeTypeAndValue[] atvs = rdn.getTypesAndValues();
                for (AttributeTypeAndValue atv : atvs) {
                    if (type.equals(atv.getType().getId())) {
                        String textValue = IETFUtils.valueToString(atv.getValue()).toLowerCase();
                        values.add(textValue);
                    }
                }
            } else {
                String textValue = IETFUtils.valueToString(rdn.getFirst().getValue()).toLowerCase();
                values.add(textValue);
            }
        } // end for(j)

        sb.append(values.get(0));

        final int n2 = values.size();
        if (n2 > 1) {
            for (int j = 1; j < n2; j++) {
                sb.append(";").append(values.get(j));
            }
        }
    } // end for(i)

    return sb.toString();
}