1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.asn1.x509;
2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
3b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1Choice;
4b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1Encodable;
54c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.ASN1Object;
64c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.ASN1Primitive;
7b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1Set;
8b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1TaggedObject;
9b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.DERTaggedObject;
10b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
11b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam/**
12b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * The DistributionPointName object.
13b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * <pre>
14b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * DistributionPointName ::= CHOICE {
15b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *     fullName                 [0] GeneralNames,
166e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom *     nameRelativeToCRLIssuer  [1] RDN
17b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * }
18b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * </pre>
19b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */
20b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic class DistributionPointName
214c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    extends ASN1Object
22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    implements ASN1Choice
23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{
244c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    ASN1Encodable        name;
25b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    int                 type;
26b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
27b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static final int FULL_NAME = 0;
28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static final int NAME_RELATIVE_TO_CRL_ISSUER = 1;
29b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
30b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static DistributionPointName getInstance(
31b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1TaggedObject obj,
32b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        boolean          explicit)
33b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
34b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return getInstance(ASN1TaggedObject.getInstance(obj, true));
35b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
36b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
37b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static DistributionPointName getInstance(
38b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        Object  obj)
39b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
40b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (obj == null || obj instanceof DistributionPointName)
41b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
42b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return (DistributionPointName)obj;
43b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
44b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        else if (obj instanceof ASN1TaggedObject)
45b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
46b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return new DistributionPointName((ASN1TaggedObject)obj);
47b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
48b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
49c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        throw new IllegalArgumentException("unknown object in factory: " + obj.getClass().getName());
50b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
51b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
52b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public DistributionPointName(
53b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        int             type,
54b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1Encodable   name)
55b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
56b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.type = type;
57b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.name = name;
58b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
59c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
60c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public DistributionPointName(
61c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        GeneralNames name)
62c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
63c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        this(FULL_NAME, name);
64c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
65c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
66b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
67b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * Return the tag number applying to the underlying choice.
68b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
69b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @return the tag number for this point name.
70b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
71b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public int getType()
72b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
73b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return this.type;
74b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
75b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
76b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
77b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * Return the tagged object inside the distribution point name.
78b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
79b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @return the underlying choice item.
80b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
81b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public ASN1Encodable getName()
82b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
83b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return (ASN1Encodable)name;
84b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
85b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
86b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public DistributionPointName(
87b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1TaggedObject    obj)
88b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
89b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.type = obj.getTagNo();
90b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
91b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (type == 0)
92b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
93b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            this.name = GeneralNames.getInstance(obj, false);
94b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
95b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        else
96b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
97b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            this.name = ASN1Set.getInstance(obj, false);
98b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
99b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
100b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
1014c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    public ASN1Primitive toASN1Primitive()
102b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
103b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return new DERTaggedObject(false, type, name);
104b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
105c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
106c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public String toString()
107c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
108c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        String       sep = System.getProperty("line.separator");
109c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        StringBuffer buf = new StringBuffer();
110c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        buf.append("DistributionPointName: [");
111c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        buf.append(sep);
112c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (type == FULL_NAME)
113c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
114c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            appendObject(buf, sep, "fullName", name.toString());
115c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
116c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        else
117c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
118c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            appendObject(buf, sep, "nameRelativeToCRLIssuer", name.toString());
119c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
120c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        buf.append("]");
121c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        buf.append(sep);
122c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return buf.toString();
123c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
124c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
125c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    private void appendObject(StringBuffer buf, String sep, String name, String value)
126c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
127c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        String       indent = "    ";
128c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
129c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        buf.append(indent);
130c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        buf.append(name);
131c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        buf.append(":");
132c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        buf.append(sep);
133c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        buf.append(indent);
134c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        buf.append(indent);
135c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        buf.append(value);
136c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        buf.append(sep);
137c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
138b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
139