1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.asn1.x509;
2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
3b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1Choice;
4b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1Encodable;
5b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1Set;
6b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1TaggedObject;
7b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.DEREncodable;
8b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.DERObject;
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
21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    extends ASN1Encodable
22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    implements ASN1Choice
23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{
24b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    DEREncodable        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    /*
53b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @deprecated use ASN1Encodable
54b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
55b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public DistributionPointName(
56b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        int             type,
57b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        DEREncodable    name)
58b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
59b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.type = type;
60b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.name = name;
61b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
62b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
63b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public DistributionPointName(
64b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        int             type,
65b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1Encodable   name)
66b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
67b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.type = type;
68b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.name = name;
69b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
70c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
71c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public DistributionPointName(
72c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        GeneralNames name)
73c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
74c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        this(FULL_NAME, name);
75c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
76c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
77b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
78b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * Return the tag number applying to the underlying choice.
79b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
80b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @return the tag number for this point name.
81b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
82b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public int getType()
83b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
84b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return this.type;
85b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
86b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
87b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
88b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * Return the tagged object inside the distribution point name.
89b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
90b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @return the underlying choice item.
91b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
92b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public ASN1Encodable getName()
93b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
94b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return (ASN1Encodable)name;
95b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
96b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
97b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public DistributionPointName(
98b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1TaggedObject    obj)
99b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
100b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.type = obj.getTagNo();
101b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
102b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (type == 0)
103b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
104b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            this.name = GeneralNames.getInstance(obj, false);
105b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
106b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        else
107b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
108b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            this.name = ASN1Set.getInstance(obj, false);
109b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
110b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
111b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
112b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public DERObject toASN1Object()
113b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
114b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return new DERTaggedObject(false, type, name);
115b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
116c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
117c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public String toString()
118c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
119c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        String       sep = System.getProperty("line.separator");
120c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        StringBuffer buf = new StringBuffer();
121c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        buf.append("DistributionPointName: [");
122c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        buf.append(sep);
123c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (type == FULL_NAME)
124c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
125c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            appendObject(buf, sep, "fullName", name.toString());
126c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
127c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        else
128c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
129c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            appendObject(buf, sep, "nameRelativeToCRLIssuer", name.toString());
130c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
131c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        buf.append("]");
132c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        buf.append(sep);
133c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return buf.toString();
134c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
135c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
136c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    private void appendObject(StringBuffer buf, String sep, String name, String value)
137c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
138c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        String       indent = "    ";
139c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
140c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        buf.append(indent);
141c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        buf.append(name);
142c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        buf.append(":");
143c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        buf.append(sep);
144c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        buf.append(indent);
145c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        buf.append(indent);
146c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        buf.append(value);
147c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        buf.append(sep);
148c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
149b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
150