1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.asn1.x509;
2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
36e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstromimport java.io.IOException;
4c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.util.StringTokenizer;
5c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
6b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1Choice;
7b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1Encodable;
86e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstromimport org.bouncycastle.asn1.ASN1Object;
94c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.ASN1ObjectIdentifier;
10b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1OctetString;
114c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.ASN1Primitive;
12b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1Sequence;
13b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1TaggedObject;
14b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.DERIA5String;
15c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport org.bouncycastle.asn1.DEROctetString;
16b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.DERTaggedObject;
176e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstromimport org.bouncycastle.asn1.x500.X500Name;
18c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport org.bouncycastle.util.IPAddress;
19b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
20b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam/**
21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * The GeneralName object.
22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * <pre>
23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * GeneralName ::= CHOICE {
24b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *      otherName                       [0]     OtherName,
25b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *      rfc822Name                      [1]     IA5String,
26b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *      dNSName                         [2]     IA5String,
27b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *      x400Address                     [3]     ORAddress,
28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *      directoryName                   [4]     Name,
29b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *      ediPartyName                    [5]     EDIPartyName,
30b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *      uniformResourceIdentifier       [6]     IA5String,
31b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *      iPAddress                       [7]     OCTET STRING,
32b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *      registeredID                    [8]     OBJECT IDENTIFIER}
33b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *
34b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * OtherName ::= SEQUENCE {
35b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *      type-id    OBJECT IDENTIFIER,
36b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *      value      [0] EXPLICIT ANY DEFINED BY type-id }
37b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *
38b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * EDIPartyName ::= SEQUENCE {
39b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *      nameAssigner            [0]     DirectoryString OPTIONAL,
40b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *      partyName               [1]     DirectoryString }
41b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *
42b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * Name ::= CHOICE { RDNSequence }
43b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * </pre>
44b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */
45b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic class GeneralName
464c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    extends ASN1Object
47b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    implements ASN1Choice
48b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{
49b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static final int otherName                     = 0;
50b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static final int rfc822Name                    = 1;
51b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static final int dNSName                       = 2;
52b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static final int x400Address                   = 3;
53b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static final int directoryName                 = 4;
54b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static final int ediPartyName                  = 5;
55b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static final int uniformResourceIdentifier     = 6;
56b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static final int iPAddress                     = 7;
57b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static final int registeredID                  = 8;
58b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
594c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    private ASN1Encodable obj;
604c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    private int           tag;
614c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom
624c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    /**
634c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom     * @deprecated use X500Name constructor.
644c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom     * @param dirName
654c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom     */
664c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        public GeneralName(
67b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        X509Name  dirName)
68b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
694c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        this.obj = X500Name.getInstance(dirName);
70b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.tag = 4;
71b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
72b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
736e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom    public GeneralName(
744c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        X500Name  dirName)
756e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom    {
766e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        this.obj = dirName;
776e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        this.tag = 4;
786e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom    }
796e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom
80b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
81b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * When the subjectAltName extension contains an Internet mail address,
82b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * the address MUST be included as an rfc822Name. The format of an
83b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * rfc822Name is an "addr-spec" as defined in RFC 822 [RFC 822].
84b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
85b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * When the subjectAltName extension contains a domain name service
86b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * label, the domain name MUST be stored in the dNSName (an IA5String).
87b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * The name MUST be in the "preferred name syntax," as specified by RFC
88b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * 1034 [RFC 1034].
89b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
90b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * When the subjectAltName extension contains a URI, the name MUST be
91b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * stored in the uniformResourceIdentifier (an IA5String). The name MUST
92b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * be a non-relative URL, and MUST follow the URL syntax and encoding
93b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * rules specified in [RFC 1738].  The name must include both a scheme
94b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * (e.g., "http" or "ftp") and a scheme-specific-part.  The scheme-
95b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * specific-part must include a fully qualified domain name or IP
96b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * address as the host.
97b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
98b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * When the subjectAltName extension contains a iPAddress, the address
99b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * MUST be stored in the octet string in "network byte order," as
100b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * specified in RFC 791 [RFC 791]. The least significant bit (LSB) of
101b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * each octet is the LSB of the corresponding byte in the network
102b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * address. For IP Version 4, as specified in RFC 791, the octet string
103b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * MUST contain exactly four octets.  For IP Version 6, as specified in
104b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * RFC 1883, the octet string MUST contain exactly sixteen octets [RFC
105b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * 1883].
106b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
107b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public GeneralName(
108b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        int           tag,
109b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1Encodable name)
110b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
111b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.obj = name;
112b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.tag = tag;
113b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
114b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
115b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
116c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * Create a GeneralName for the given tag from the passed in String.
117c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * <p>
118c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * This constructor can handle:
119c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * <ul>
120c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * <li>rfc822Name
121c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * <li>iPAddress
122c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * <li>directoryName
123c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * <li>dNSName
124c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * <li>uniformResourceIdentifier
125c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * <li>registeredID
126c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * </ul>
127c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * For x400Address, otherName and ediPartyName there is no common string
128c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * format defined.
129c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * <p>
130c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * Note: A directory name can be encoded in different ways into a byte
131c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * representation. Be aware of this if the byte representation is used for
132c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * comparing results.
133c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     *
134b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param tag tag number
135b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param name string representation of name
136c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * @throws IllegalArgumentException if the string encoding is not correct or     *             not supported.
137b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
138b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public GeneralName(
139b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        int       tag,
140b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        String    name)
141b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
142c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        this.tag = tag;
143c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
144b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (tag == rfc822Name || tag == dNSName || tag == uniformResourceIdentifier)
145b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
146b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            this.obj = new DERIA5String(name);
147b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
148b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        else if (tag == registeredID)
149b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
1504c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom            this.obj = new ASN1ObjectIdentifier(name);
151b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
152c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        else if (tag == directoryName)
153c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
1544c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom            this.obj = new X500Name(name);
155c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
156c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        else if (tag == iPAddress)
157c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
158c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            byte[] enc = toGeneralNameEncoding(name);
159c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            if (enc != null)
160c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            {
161c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                this.obj = new DEROctetString(enc);
162c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            }
163c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            else
164c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            {
165c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                throw new IllegalArgumentException("IP Address is invalid");
166c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            }
167c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
168b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        else
169b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
170b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            throw new IllegalArgumentException("can't process String for tag: " + tag);
171b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
172b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
173b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
174b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static GeneralName getInstance(
175b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        Object obj)
176b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
177b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (obj == null || obj instanceof GeneralName)
178b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
179b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return (GeneralName)obj;
180b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
181b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
182b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (obj instanceof ASN1TaggedObject)
183b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
184b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            ASN1TaggedObject    tagObj = (ASN1TaggedObject)obj;
185b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            int                 tag = tagObj.getTagNo();
186b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
187b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            switch (tag)
188b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            {
189b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            case otherName:
190c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                return new GeneralName(tag, ASN1Sequence.getInstance(tagObj, false));
191b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            case rfc822Name:
192c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                return new GeneralName(tag, DERIA5String.getInstance(tagObj, false));
193b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            case dNSName:
194c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                return new GeneralName(tag, DERIA5String.getInstance(tagObj, false));
195b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            case x400Address:
196b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                throw new IllegalArgumentException("unknown tag: " + tag);
197b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            case directoryName:
1984c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom                return new GeneralName(tag, X500Name.getInstance(tagObj, true));
199b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            case ediPartyName:
200c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                return new GeneralName(tag, ASN1Sequence.getInstance(tagObj, false));
201b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            case uniformResourceIdentifier:
202c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                return new GeneralName(tag, DERIA5String.getInstance(tagObj, false));
203b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            case iPAddress:
204c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                return new GeneralName(tag, ASN1OctetString.getInstance(tagObj, false));
205b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            case registeredID:
2064c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom                return new GeneralName(tag, ASN1ObjectIdentifier.getInstance(tagObj, false));
207b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            }
208b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
209b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
2106e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        if (obj instanceof byte[])
2116e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        {
2126e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom            try
2136e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom            {
2144c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom                return getInstance(ASN1Primitive.fromByteArray((byte[])obj));
2156e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom            }
2166e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom            catch (IOException e)
2176e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom            {
2186e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom                throw new IllegalArgumentException("unable to parse encoded general name");
2196e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom            }
2206e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        }
2216e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom
222c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        throw new IllegalArgumentException("unknown object in getInstance: " + obj.getClass().getName());
223b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
224b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
225b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static GeneralName getInstance(
226b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1TaggedObject tagObj,
227b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        boolean          explicit)
228b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
229b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return GeneralName.getInstance(ASN1TaggedObject.getInstance(tagObj, true));
230b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
231b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
232b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public int getTagNo()
233b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
234b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return tag;
235b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
236b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
2374c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    public ASN1Encodable getName()
238b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
239b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return obj;
240b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
241b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
242c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public String toString()
243c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
244c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        StringBuffer buf = new StringBuffer();
245c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
246c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        buf.append(tag);
247c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        buf.append(": ");
248c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        switch (tag)
249c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
250c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        case rfc822Name:
251c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        case dNSName:
252c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        case uniformResourceIdentifier:
253c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            buf.append(DERIA5String.getInstance(obj).getString());
254c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            break;
255c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        case directoryName:
2564c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom            buf.append(X500Name.getInstance(obj).toString());
257c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            break;
258c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        default:
259c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            buf.append(obj.toString());
260c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
261c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return buf.toString();
262c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
263c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
264c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    private byte[] toGeneralNameEncoding(String ip)
265c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
266c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (IPAddress.isValidIPv6WithNetmask(ip) || IPAddress.isValidIPv6(ip))
267c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
268c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            int    slashIndex = ip.indexOf('/');
269c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
270c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            if (slashIndex < 0)
271c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            {
272c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                byte[] addr = new byte[16];
273c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                int[]  parsedIp = parseIPv6(ip);
274c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                copyInts(parsedIp, addr, 0);
275c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
276c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                return addr;
277c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            }
278c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            else
279c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            {
280c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                byte[] addr = new byte[32];
281c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                int[]  parsedIp = parseIPv6(ip.substring(0, slashIndex));
282c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                copyInts(parsedIp, addr, 0);
283c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                String mask = ip.substring(slashIndex + 1);
284c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                if (mask.indexOf(':') > 0)
285c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                {
286c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                    parsedIp = parseIPv6(mask);
287c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                }
288c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                else
289c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                {
290c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                    parsedIp = parseMask(mask);
291c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                }
292c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                copyInts(parsedIp, addr, 16);
293c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
294c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                return addr;
295c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            }
296c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
297c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        else if (IPAddress.isValidIPv4WithNetmask(ip) || IPAddress.isValidIPv4(ip))
298c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
299c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            int    slashIndex = ip.indexOf('/');
300c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
301c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            if (slashIndex < 0)
302c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            {
303c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                byte[] addr = new byte[4];
304c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
305c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                parseIPv4(ip, addr, 0);
306c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
307c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                return addr;
308c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            }
309c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            else
310c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            {
311c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                byte[] addr = new byte[8];
312c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
313c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                parseIPv4(ip.substring(0, slashIndex), addr, 0);
314c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
315c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                String mask = ip.substring(slashIndex + 1);
316c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                if (mask.indexOf('.') > 0)
317c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                {
318c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                    parseIPv4(mask, addr, 4);
319c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                }
320c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                else
321c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                {
322c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                    parseIPv4Mask(mask, addr, 4);
323c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                }
324c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
325c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                return addr;
326c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            }
327c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
328c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
329c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return null;
330c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
331c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
332c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    private void parseIPv4Mask(String mask, byte[] addr, int offset)
333c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
334c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        int   maskVal = Integer.parseInt(mask);
335c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
336c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        for (int i = 0; i != maskVal; i++)
337c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
3384c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom            addr[(i / 8) + offset] |= 1 << (7 - (i % 8));
339c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
340c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
341c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
342c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    private void parseIPv4(String ip, byte[] addr, int offset)
343c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
344c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        StringTokenizer sTok = new StringTokenizer(ip, "./");
345c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        int    index = 0;
346c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
347c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        while (sTok.hasMoreTokens())
348c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
349c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            addr[offset + index++] = (byte)Integer.parseInt(sTok.nextToken());
350c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
351c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
352c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
353c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    private int[] parseMask(String mask)
354c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
355c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        int[] res = new int[8];
356c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        int   maskVal = Integer.parseInt(mask);
357c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
358c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        for (int i = 0; i != maskVal; i++)
359c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
3604c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom            res[i / 16] |= 1 << (15 - (i % 16));
361c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
362c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return res;
363c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
364c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
365c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    private void copyInts(int[] parsedIp, byte[] addr, int offSet)
366c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
367c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        for (int i = 0; i != parsedIp.length; i++)
368c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
369c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            addr[(i * 2) + offSet] = (byte)(parsedIp[i] >> 8);
370c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            addr[(i * 2 + 1) + offSet] = (byte)parsedIp[i];
371c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
372c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
373c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
374c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    private int[] parseIPv6(String ip)
375c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
376c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        StringTokenizer sTok = new StringTokenizer(ip, ":", true);
377c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        int index = 0;
378c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        int[] val = new int[8];
379c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
380c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (ip.charAt(0) == ':' && ip.charAt(1) == ':')
381c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
382c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom           sTok.nextToken(); // skip the first one
383c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
384c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
385c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        int doubleColon = -1;
386c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
387c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        while (sTok.hasMoreTokens())
388c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
389c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            String e = sTok.nextToken();
390c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
391c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            if (e.equals(":"))
392c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            {
393c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                doubleColon = index;
394c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                val[index++] = 0;
395c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            }
396c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            else
397c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            {
398c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                if (e.indexOf('.') < 0)
399c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                {
400c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                    val[index++] = Integer.parseInt(e, 16);
401c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                    if (sTok.hasMoreTokens())
402c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                    {
403c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                        sTok.nextToken();
404c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                    }
405c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                }
406c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                else
407c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                {
408c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                    StringTokenizer eTok = new StringTokenizer(e, ".");
409c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
410c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                    val[index++] = (Integer.parseInt(eTok.nextToken()) << 8) | Integer.parseInt(eTok.nextToken());
411c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                    val[index++] = (Integer.parseInt(eTok.nextToken()) << 8) | Integer.parseInt(eTok.nextToken());
412c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                }
413c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            }
414c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
415c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
416c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (index != val.length)
417c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
418c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            System.arraycopy(val, doubleColon, val, val.length - (index - doubleColon), index - doubleColon);
419c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            for (int i = doubleColon; i != val.length - (index - doubleColon); i++)
420c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            {
421c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                val[i] = 0;
422c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            }
423c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
424c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
425c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return val;
426c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
427c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
4284c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    public ASN1Primitive toASN1Primitive()
429b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
430b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (tag == directoryName)       // directoryName is explicitly tagged as it is a CHOICE
431b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
432b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return new DERTaggedObject(true, tag, obj);
433b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
434b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        else
435b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
436b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return new DERTaggedObject(false, tag, obj);
437b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
438b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
439b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
440