ASN1OctetString.java revision c37f4a04ef89e73a39a59f3c5a179af8c8ab5974
1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.asn1;
2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
3c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport org.bouncycastle.util.encoders.Hex;
4c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport org.bouncycastle.util.Arrays;
5c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
6c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.io.ByteArrayInputStream;
7b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.io.IOException;
8c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.io.InputStream;
9b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.util.Enumeration;
10b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.util.Vector;
11b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
12b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic abstract class ASN1OctetString
13c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    extends ASN1Object
14c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    implements ASN1OctetStringParser
15b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{
16b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    byte[]  string;
17b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
18b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
19b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * return an Octet String from a tagged object.
20b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param obj the tagged object holding the object we want.
22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param explicit true if the object is meant to be explicitly
23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *              tagged false otherwise.
24b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @exception IllegalArgumentException if the tagged object cannot
25b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *              be converted.
26b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
27b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static ASN1OctetString getInstance(
28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1TaggedObject    obj,
29b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        boolean             explicit)
30b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
31b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return getInstance(obj.getObject());
32b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
33b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
34b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
35b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * return an Octet String from the given object.
36b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
37b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param obj the object we want converted.
38b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @exception IllegalArgumentException if the object cannot be converted.
39b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
40b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static ASN1OctetString getInstance(
41b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        Object  obj)
42b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
43b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (obj == null || obj instanceof ASN1OctetString)
44b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
45b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return (ASN1OctetString)obj;
46b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
47b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
48b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (obj instanceof ASN1TaggedObject)
49b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
50b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return getInstance(((ASN1TaggedObject)obj).getObject());
51b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
52b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
53b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (obj instanceof ASN1Sequence)
54b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
55b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            Vector      v = new Vector();
56b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            Enumeration e = ((ASN1Sequence)obj).getObjects();
57b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
58b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            while (e.hasMoreElements())
59b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            {
60b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam                v.addElement(e.nextElement());
61b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            }
62b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
63b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return new BERConstructedOctetString(v);
64b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
65b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
66b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
67b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
68b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
69b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
70b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param string the octets making up the octet string.
71b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
72b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public ASN1OctetString(
73b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        byte[]  string)
74b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
75c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (string == null)
76c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
77c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            throw new NullPointerException("string cannot be null");
78c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
79b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.string = string;
80b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
81b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
82b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public ASN1OctetString(
83b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        DEREncodable obj)
84b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
85b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        try
86b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
87c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            this.string = obj.getDERObject().getEncoded(ASN1Encodable.DER);
88b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
89b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        catch (IOException e)
90b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
91b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            throw new IllegalArgumentException("Error processing object : " + e.toString());
92b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
93b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
94b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
95c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public InputStream getOctetStream()
96c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
97c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return new ByteArrayInputStream(string);
98c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
99c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
100c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public ASN1OctetStringParser parser()
101c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
102c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return this;
103c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
104c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
105b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public byte[] getOctets()
106b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
107b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return string;
108b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
109b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
110b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public int hashCode()
111b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
112c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return Arrays.hashCode(this.getOctets());
113b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
114b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
115c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    boolean asn1Equals(
116c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        DERObject  o)
117b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
118c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (!(o instanceof ASN1OctetString))
119b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
120b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return false;
121b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
122b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
123c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        ASN1OctetString  other = (ASN1OctetString)o;
124b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
125c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return Arrays.areEqual(string, other.string);
126b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
127b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
128b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    abstract void encode(DEROutputStream out)
129b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        throws IOException;
130b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
131b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public String toString()
132b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
133b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam      return "#"+new String(Hex.encode(string));
134b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
135b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
136