/** * Gets the common name attribute (CN) of the certificate subject distinguished name. * * @param cert Certificate to examine. * * @return Subject CN or null if no CN attribute is defined in the subject DN. * * @throws EncodingException on cert field extraction. */ public static String subjectCN(final X509Certificate cert) throws EncodingException { return new NameReader(cert).readSubject().getValue(StandardAttributeType.CommonName); }
/** * Gets the first value of the given type that appears in the attribute list of any RDN in the sequence. * * @param type Attribute type. * * @return Value of first attribute of given type or null if no attributes of given type exist. */ public String getValue(final AttributeType type) { final List<String> values = getValues(type); if (values.size() > 0) { return values.get(0); } return null; }
/** * Gets the values of the given attribute contained in the DN. * <p> * <p><strong>NOTE:</strong> no escaping is done on special characters in the * values, which could be different from what would appear in the string * representation of the DN.</p> * Iterates sequence in reverse order as specified in section 2.1 of RFC 2253. * * @param rdnSequence list of relative distinguished names * that contains the attributes comprising the DN. * @param attribute Attribute whose values will be retrieved. * @return The attribute values for the given attribute in the order they * appear would appear in the string representation of the DN or an empty * array if the given attribute does not exist. */ private static String[] getAttributeValues(final RDNSequence rdnSequence, final AttributeType attribute) { val values = new ArrayList<String>(); for (val rdn : rdnSequence.backward()) { for (val attr : rdn.getAttributes()) { if (attr.getType().equals(attribute)) { values.add(attr.getValue()); } } } return values.toArray(ArrayUtils.EMPTY_STRING_ARRAY); }
/** * 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. */ 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; } }
/** * Gets the values of the given attribute contained in the DN. * * <p><strong>NOTE:</strong> no escaping is done on special characters in the * values, which could be different from what would appear in the string * representation of the DN.</p> * * @param rdnSequence list of relative distinguished names * that contains the attributes comprising the DN. * @param attribute Attribute whose values will be retrieved. * @return The attribute values for the given attribute in the order they * appear would appear in the string representation of the DN or an empty * array if the given attribute does not exist. */ private static String[] getAttributeValues(final RDNSequence rdnSequence, final AttributeType attribute) { // Iterates sequence in reverse order as specified in section 2.1 of RFC 2253 final List<String> values = new ArrayList<>(); for (final RDN rdn : rdnSequence.backward()) { for (final Attribute attr : rdn.getAttributes()) { if (attr.getType().equals(attribute)) { values.add(attr.getValue()); } } } return values.toArray(new String[values.size()]); }
/** * Find an RDN with the specified OID. * * @param sequence the DN components * @param oid the OID to look for * * @return the first matching RDN value, or null */ @Nullable protected String findRDN(@Nonnull final RDNSequence sequence, @Nonnull @NotEmpty final String oid) { // We use backward() here because otherwise the library returns attributes in least to most-specific order. for (final RDN rdn : sequence.backward()) { for (final Attribute attribute : rdn.getAttributes()) { if (attribute.getType().getOid().equals(oid)) { return attribute.getValue(); } } } return null; }
@Override public String format(final X500Principal dn) { final StringBuilder builder = new StringBuilder(); final RDNSequence sequence = NameReader.readX500Principal(dn); int i = 0; for (RDN rdn : sequence.backward()) { if (i++ > 0) { builder.append(RDN_SEPARATOR); } int j = 0; for (Attribute attr : rdn.getAttributes()) { if (j++ > 0) { builder.append(ATV_SEPARATOR); } builder.append(attr.getType()).append('='); final AttributeType type = attr.getType(); if (type instanceof StandardAttributeType) { escape(attr.getValue(), builder); } else { encode(attr.getValue(), builder); } } } return builder.toString(); }