1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.asn1;
2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
3c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.io.ByteArrayInputStream;
4b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.io.IOException;
5c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.io.InputStream;
66e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom
76e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstromimport org.bouncycastle.util.Arrays;
86e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstromimport org.bouncycastle.util.encoders.Hex;
9b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
10b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic abstract class ASN1OctetString
11c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    extends ASN1Object
12c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    implements ASN1OctetStringParser
13b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{
14b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    byte[]  string;
15b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
16b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
17b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * return an Octet String from a tagged object.
18b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
19b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param obj the tagged object holding the object we want.
20b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param explicit true if the object is meant to be explicitly
21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *              tagged false otherwise.
22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @exception IllegalArgumentException if the tagged object cannot
23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *              be converted.
24b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
25b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static ASN1OctetString getInstance(
26b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1TaggedObject    obj,
27b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        boolean             explicit)
28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
296e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        DERObject o = obj.getObject();
306e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom
316e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        if (explicit || o instanceof ASN1OctetString)
326e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        {
336e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom            return getInstance(o);
346e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        }
356e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        else
366e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        {
376e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom            return BERConstructedOctetString.fromSequence(ASN1Sequence.getInstance(o));
386e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        }
39b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
40b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
41b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
42b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * return an Octet String from the given object.
43b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
44b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param obj the object we want converted.
45b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @exception IllegalArgumentException if the object cannot be converted.
46b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
47b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static ASN1OctetString getInstance(
48b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        Object  obj)
49b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
50b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (obj == null || obj instanceof ASN1OctetString)
51b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
52b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return (ASN1OctetString)obj;
53b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
54b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
556e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // TODO: this needs to be deleted in V2
56b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (obj instanceof ASN1TaggedObject)
57b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
58b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return getInstance(((ASN1TaggedObject)obj).getObject());
59b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
60b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
61b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
62b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
63b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
64b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
65b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param string the octets making up the octet string.
66b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
67b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public ASN1OctetString(
68b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        byte[]  string)
69b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
70c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (string == null)
71c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
72c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            throw new NullPointerException("string cannot be null");
73c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
74b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.string = string;
75b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
76b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
77b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public ASN1OctetString(
78b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        DEREncodable obj)
79b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
80b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        try
81b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
82c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            this.string = obj.getDERObject().getEncoded(ASN1Encodable.DER);
83b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
84b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        catch (IOException e)
85b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
86b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            throw new IllegalArgumentException("Error processing object : " + e.toString());
87b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
88b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
89b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
90c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public InputStream getOctetStream()
91c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
92c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return new ByteArrayInputStream(string);
93c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
94c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
95c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public ASN1OctetStringParser parser()
96c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
97c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return this;
98c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
99c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
100b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public byte[] getOctets()
101b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
102b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return string;
103b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
104b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
105b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public int hashCode()
106b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
107c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return Arrays.hashCode(this.getOctets());
108b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
109b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
110c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    boolean asn1Equals(
111c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        DERObject  o)
112b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
113c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (!(o instanceof ASN1OctetString))
114b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
115b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return false;
116b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
117b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
118c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        ASN1OctetString  other = (ASN1OctetString)o;
119b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
120c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return Arrays.areEqual(string, other.string);
121b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
122b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
1236e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom    public DERObject getLoadedObject()
1246e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom    {
1256e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        return this.getDERObject();
1266e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom    }
1276e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom
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