1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.asn1.x509;
2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
34c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.ASN1Object;
44c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.ASN1Primitive;
5b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1Sequence;
6b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1TaggedObject;
7b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.DERSequence;
8b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
9b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic class GeneralNames
104c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    extends ASN1Object
11b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{
12c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    private final GeneralName[] names;
13b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
14b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static GeneralNames getInstance(
15b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        Object  obj)
16b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
174c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        if (obj instanceof GeneralNames)
18b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
19b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return (GeneralNames)obj;
20b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
224c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        if (obj != null)
23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
244c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom            return new GeneralNames(ASN1Sequence.getInstance(obj));
25b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
26b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
274c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        return null;
28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
29b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
30b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static GeneralNames getInstance(
31b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1TaggedObject obj,
32b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        boolean          explicit)
33b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
34b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return getInstance(ASN1Sequence.getInstance(obj, explicit));
35b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
36b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
37b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
38b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * Construct a GeneralNames object containing one GeneralName.
39b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
40b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param name the name to be contained.
41b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
42b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public GeneralNames(
43b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        GeneralName  name)
44b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
45c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        this.names = new GeneralName[] { name };
46b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
474c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom
484c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom
49b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public GeneralNames(
504c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        GeneralName[]  names)
514c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    {
524c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        this.names = names;
534c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    }
544c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom
554c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    private GeneralNames(
56b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1Sequence  seq)
57b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
58c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        this.names = new GeneralName[seq.size()];
59b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
60b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        for (int i = 0; i != seq.size(); i++)
61b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
62b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            names[i] = GeneralName.getInstance(seq.getObjectAt(i));
63b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
64c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
65c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
66c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public GeneralName[] getNames()
67c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
68c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        GeneralName[] tmp = new GeneralName[names.length];
69c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
70c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        System.arraycopy(names, 0, tmp, 0, names.length);
71c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
72c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return tmp;
73b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
74b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
75b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
76b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * Produce an object suitable for an ASN1OutputStream.
77b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * <pre>
78b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * GeneralNames ::= SEQUENCE SIZE {1..MAX} OF GeneralName
79b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * </pre>
80b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
814c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    public ASN1Primitive toASN1Primitive()
82b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
83c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return new DERSequence(names);
84c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
85c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
86c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public String toString()
87c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
88c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        StringBuffer  buf = new StringBuffer();
89c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        String        sep = System.getProperty("line.separator");
90c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
91c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        buf.append("GeneralNames:");
92c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        buf.append(sep);
93c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
94c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        for (int i = 0; i != names.length; i++)
95c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
96c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            buf.append("    ");
97c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            buf.append(names[i]);
98c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            buf.append(sep);
99c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
100c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return buf.toString();
101b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
102b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
103