diff options
5 files changed, 127 insertions, 3 deletions
diff --git a/src/main/java/org/bouncycastle/asn1/x500/X500NameStyle.java b/src/main/java/org/bouncycastle/asn1/x500/X500NameStyle.java index 7a7c8370..704ea72e 100644 --- a/src/main/java/org/bouncycastle/asn1/x500/X500NameStyle.java +++ b/src/main/java/org/bouncycastle/asn1/x500/X500NameStyle.java @@ -16,19 +16,64 @@ public interface X500NameStyle * Convert the passed in String value into the appropriate ASN.1 * encoded object. * - * @param oid the oid associated with the value in the DN. + * @param oid the OID associated with the value in the DN. * @param value the value of the particular DN component. * @return the ASN.1 equivalent for the value. */ ASN1Encodable stringToValue(ASN1ObjectIdentifier oid, String value); + /** + * Return the OID associated with the passed in name. + * + * @param attrName the string to match. + * @return an OID + */ ASN1ObjectIdentifier attrNameToOID(String attrName); - boolean areEqual(X500Name name1, X500Name name2); - + /** + * Return an array of RDN generated from the passed in String. + * @param dirName the String representation. + * @return an array of corresponding RDNs. + */ RDN[] fromString(String dirName); + /** + * Return true if the two names are equal. + * + * @param name1 first name for comparison. + * @param name2 second name for comparison. + * @return true if name1 = name 2, false otherwise. + */ + boolean areEqual(X500Name name1, X500Name name2); + + /** + * Calculate a hashCode for the passed in name. + * + * @param name the name the hashCode is required for. + * @return the calculated hashCode. + */ int calculateHashCode(X500Name name); + /** + * Convert the passed in X500Name to a String. + * @param name the name to convert. + * @return a String representation. + */ String toString(X500Name name); + + /** + * Return the display name for toString() associated with the OID. + * + * @param oid the OID of interest. + * @return the name displayed in toString(), null if no mapping provided. + */ + String oidToDisplayName(ASN1ObjectIdentifier oid); + + /** + * Return the acceptable names in a String DN that map to OID. + * + * @param oid the OID of interest. + * @return an array of String aliases for the OID, zero length if there are none. + */ + String[] oidToAttrNames(ASN1ObjectIdentifier oid); } diff --git a/src/main/java/org/bouncycastle/asn1/x500/style/BCStyle.java b/src/main/java/org/bouncycastle/asn1/x500/style/BCStyle.java index 777cc563..714a32c0 100644 --- a/src/main/java/org/bouncycastle/asn1/x500/style/BCStyle.java +++ b/src/main/java/org/bouncycastle/asn1/x500/style/BCStyle.java @@ -315,6 +315,16 @@ public class BCStyle return new DERUTF8String(value); } + public String oidToDisplayName(ASN1ObjectIdentifier oid) + { + return (String)DefaultSymbols.get(oid); + } + + public String[] oidToAttrNames(ASN1ObjectIdentifier oid) + { + return IETFUtils.findAttrNamesForOID(oid, DefaultLookUp); + } + public ASN1ObjectIdentifier attrNameToOID(String attrName) { return IETFUtils.decodeAttrName(attrName, DefaultLookUp); diff --git a/src/main/java/org/bouncycastle/asn1/x500/style/IETFUtils.java b/src/main/java/org/bouncycastle/asn1/x500/style/IETFUtils.java index 0cbad08e..c73107eb 100644 --- a/src/main/java/org/bouncycastle/asn1/x500/style/IETFUtils.java +++ b/src/main/java/org/bouncycastle/asn1/x500/style/IETFUtils.java @@ -1,6 +1,7 @@ package org.bouncycastle.asn1.x500.style; import java.io.IOException; +import java.util.Enumeration; import java.util.Hashtable; import java.util.Vector; @@ -228,6 +229,34 @@ public class IETFUtils return tmp; } + public static String[] findAttrNamesForOID( + ASN1ObjectIdentifier oid, + Hashtable lookup) + { + int count = 0; + for (Enumeration en = lookup.elements(); en.hasMoreElements();) + { + if (oid.equals(en.nextElement())) + { + count++; + } + } + + String[] aliases = new String[count]; + count = 0; + + for (Enumeration en = lookup.keys(); en.hasMoreElements();) + { + String key = (String)en.nextElement(); + if (oid.equals(lookup.get(key))) + { + aliases[count++] = key; + } + } + + return aliases; + } + public static ASN1ObjectIdentifier decodeAttrName( String name, Hashtable lookUp) diff --git a/src/main/java/org/bouncycastle/asn1/x500/style/RFC4519Style.java b/src/main/java/org/bouncycastle/asn1/x500/style/RFC4519Style.java index 430d3794..84869895 100644 --- a/src/main/java/org/bouncycastle/asn1/x500/style/RFC4519Style.java +++ b/src/main/java/org/bouncycastle/asn1/x500/style/RFC4519Style.java @@ -204,6 +204,16 @@ public class RFC4519Style return new DERUTF8String(value); } + public String oidToDisplayName(ASN1ObjectIdentifier oid) + { + return (String)DefaultSymbols.get(oid); + } + + public String[] oidToAttrNames(ASN1ObjectIdentifier oid) + { + return IETFUtils.findAttrNamesForOID(oid, DefaultLookUp); + } + public ASN1ObjectIdentifier attrNameToOID(String attrName) { return IETFUtils.decodeAttrName(attrName, DefaultLookUp); diff --git a/src/test/java/org/bouncycastle/asn1/test/X500NameTest.java b/src/test/java/org/bouncycastle/asn1/test/X500NameTest.java index 76762aad..90ae14d6 100644 --- a/src/test/java/org/bouncycastle/asn1/test/X500NameTest.java +++ b/src/test/java/org/bouncycastle/asn1/test/X500NameTest.java @@ -635,6 +635,36 @@ public class X500NameTest { fail("telephonenumber escaped + not reduced properly"); } + + // test query methods + if (!"E".equals(BCStyle.INSTANCE.oidToDisplayName(BCStyle.EmailAddress))) + { + fail("display name for E incorrect"); + } + + String[] aliases = BCStyle.INSTANCE.oidToAttrNames(BCStyle.EmailAddress); + if (aliases.length != 2) + { + fail("no aliases found"); + } + if (!("e".equals(aliases[0]) || "e".equals(aliases[1]))) + { + fail("first alias name for E incorrect"); + } + if (!("emailaddress".equals(aliases[0]) || "emailaddress".equals(aliases[1]))) + { + fail("second alias name for E incorrect"); + } + + if (BCStyle.INSTANCE.oidToDisplayName(new ASN1ObjectIdentifier("1.2.1")) != null) + { + fail("unknown oid matched!"); + } + + if (BCStyle.INSTANCE.oidToAttrNames(new ASN1ObjectIdentifier("1.2.1")).length != 0) + { + fail("unknown oid matched aliases!"); + } } private String getValue(RDN vl) |