1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.asn1;
2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
3b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.io.ByteArrayOutputStream;
4b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.io.IOException;
5b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
64c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.util.Arrays;
74c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom
8b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam/**
9b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * DER UniversalString object.
10b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */
11b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic class DERUniversalString
124c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    extends ASN1Primitive
134c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    implements ASN1String
14b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{
15b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    private static final char[]  table = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
16b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    private byte[] string;
17b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
18b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
19b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * return a Universal String from the passed in object.
20b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @exception IllegalArgumentException if the object cannot be converted.
22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static DERUniversalString getInstance(
24b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        Object  obj)
25b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
26b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (obj == null || obj instanceof DERUniversalString)
27b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return (DERUniversalString)obj;
29b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
30b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
3170c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom        if (obj instanceof byte[])
3270c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom        {
3370c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom            try
3470c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom            {
3570c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom                return (DERUniversalString)fromByteArray((byte[])obj);
3670c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom            }
3770c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom            catch (Exception e)
3870c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom            {
3970c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom                throw new IllegalArgumentException("encoding error getInstance: " + e.toString());
4070c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom            }
4170c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom        }
4270c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom
43b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
44b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
45b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
46b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
47b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * return a Universal String from a tagged object.
48b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
49b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param obj the tagged object holding the object we want
50b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param explicit true if the object is meant to be explicitly
51b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *              tagged false otherwise.
52b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @exception IllegalArgumentException if the tagged object cannot
53b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *               be converted.
54b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
55b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static DERUniversalString getInstance(
56b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1TaggedObject obj,
57b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        boolean          explicit)
58b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
594c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        ASN1Primitive o = obj.getObject();
606e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom
616e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        if (explicit || o instanceof DERUniversalString)
626e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        {
636e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom            return getInstance(o);
646e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        }
656e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        else
666e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        {
676e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom            return new DERUniversalString(((ASN1OctetString)o).getOctets());
686e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        }
69b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
70b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
71b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
72b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * basic constructor - byte encoded string.
73b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
74b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public DERUniversalString(
75b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        byte[]   string)
76b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
77b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.string = string;
78b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
79b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
80b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public String getString()
81b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
82b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        StringBuffer    buf = new StringBuffer("#");
83b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
84b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1OutputStream            aOut = new ASN1OutputStream(bOut);
85b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
86b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        try
87b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
88b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            aOut.writeObject(this);
89b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
90b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        catch (IOException e)
91b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
92b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam           throw new RuntimeException("internal error encoding BitString");
93b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
94b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
95b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        byte[]    string = bOut.toByteArray();
96b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
97b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        for (int i = 0; i != string.length; i++)
98b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
99c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            buf.append(table[(string[i] >>> 4) & 0xf]);
100b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            buf.append(table[string[i] & 0xf]);
101b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
102b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
103b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return buf.toString();
104b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
105b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
106c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public String toString()
107c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
108c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return getString();
109c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
110c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
111b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public byte[] getOctets()
112b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
113b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return string;
114b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
115b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
1164c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    boolean isConstructed()
1174c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    {
1184c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        return false;
1194c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    }
1204c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom
1214c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    int encodedLength()
1224c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    {
1234c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        return 1 + StreamUtil.calculateBodyLength(string.length) + string.length;
1244c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    }
1254c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom
126b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    void encode(
1274c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        ASN1OutputStream out)
128b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        throws IOException
129b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
1304c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        out.writeEncoded(BERTags.UNIVERSAL_STRING, this.getOctets());
131b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
132b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
133c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    boolean asn1Equals(
1344c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        ASN1Primitive o)
135b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
136c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (!(o instanceof DERUniversalString))
137b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
138b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return false;
139b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
140b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
1414c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        return Arrays.areEqual(string, ((DERUniversalString)o).string);
142b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
143b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
144b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public int hashCode()
145b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
1464c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        return Arrays.hashCode(string);
147b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
148b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
149