1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.asn1;
2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
3b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.io.ByteArrayOutputStream;
4b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.io.IOException;
5b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
6b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam/**
7b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * DER UniversalString object.
8b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */
9b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic class DERUniversalString
10c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    extends ASN1Object
11b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    implements DERString
12b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{
13b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    private static final char[]  table = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
14b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    private byte[] string;
15b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
16b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
17b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * return a Universal String from the passed in object.
18b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
19b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @exception IllegalArgumentException if the object cannot be converted.
20b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static DERUniversalString getInstance(
22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        Object  obj)
23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
24b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (obj == null || obj instanceof DERUniversalString)
25b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
26b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return (DERUniversalString)obj;
27b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
29b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
30b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
31b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
32b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
33b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * return a Universal String from a tagged object.
34b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
35b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param obj the tagged object holding the object we want
36b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param explicit true if the object is meant to be explicitly
37b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *              tagged false otherwise.
38b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @exception IllegalArgumentException if the tagged object cannot
39b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *               be converted.
40b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
41b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static DERUniversalString getInstance(
42b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1TaggedObject obj,
43b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        boolean          explicit)
44b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
456e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        DERObject o = obj.getObject();
466e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom
476e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        if (explicit || o instanceof DERUniversalString)
486e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        {
496e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom            return getInstance(o);
506e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        }
516e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        else
526e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        {
536e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom            return new DERUniversalString(((ASN1OctetString)o).getOctets());
546e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        }
55b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
56b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
57b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
58b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * basic constructor - byte encoded string.
59b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
60b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public DERUniversalString(
61b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        byte[]   string)
62b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
63b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.string = string;
64b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
65b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
66b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public String getString()
67b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
68b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        StringBuffer    buf = new StringBuffer("#");
69b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
70b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1OutputStream            aOut = new ASN1OutputStream(bOut);
71b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
72b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        try
73b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
74b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            aOut.writeObject(this);
75b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
76b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        catch (IOException e)
77b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
78b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam           throw new RuntimeException("internal error encoding BitString");
79b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
80b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
81b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        byte[]    string = bOut.toByteArray();
82b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
83b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        for (int i = 0; i != string.length; i++)
84b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
85c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            buf.append(table[(string[i] >>> 4) & 0xf]);
86b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            buf.append(table[string[i] & 0xf]);
87b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
88b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
89b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return buf.toString();
90b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
91b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
92c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public String toString()
93c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
94c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return getString();
95c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
96c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
97b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public byte[] getOctets()
98b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
99b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return string;
100b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
101b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
102b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    void encode(
103b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        DEROutputStream  out)
104b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        throws IOException
105b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
106b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        out.writeEncoded(UNIVERSAL_STRING, this.getOctets());
107b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
108b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
109c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    boolean asn1Equals(
110c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        DERObject  o)
111b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
112c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (!(o instanceof DERUniversalString))
113b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
114b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return false;
115b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
116b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
117b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return this.getString().equals(((DERUniversalString)o).getString());
118b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
119b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
120b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public int hashCode()
121b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
122b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return this.getString().hashCode();
123b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
124b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
125