1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.asn1;
2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
3b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.io.IOException;
4b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
5b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam/**
6b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * DER NumericString object - this is an ascii string of characters {0,1,2,3,4,5,6,7,8,9, }.
7b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */
8b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic class DERNumericString
9c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    extends ASN1Object
10b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    implements DERString
11b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{
12b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    String  string;
13b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
14b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
15b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * return a Numeric string from the passed in object
16b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
17b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @exception IllegalArgumentException if the object cannot be converted.
18b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
19b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static DERNumericString getInstance(
20b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        Object  obj)
21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (obj == null || obj instanceof DERNumericString)
23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
24b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return (DERNumericString)obj;
25b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
26b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
27b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
29b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
30b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
31b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * return an Numeric String from a tagged object.
32b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
33b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param obj the tagged object holding the object we want
34b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param explicit true if the object is meant to be explicitly
35b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *              tagged false otherwise.
36b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @exception IllegalArgumentException if the tagged object cannot
37b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *               be converted.
38b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
39b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static DERNumericString getInstance(
40b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1TaggedObject obj,
41b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        boolean          explicit)
42b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
436e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        DERObject o = obj.getObject();
446e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom
456e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        if (explicit || o instanceof DERNumericString)
466e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        {
476e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom            return getInstance(o);
486e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        }
496e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        else
506e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        {
516e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom            return new DERNumericString(ASN1OctetString.getInstance(o).getOctets());
526e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        }
53b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
54b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
55b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
56b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * basic constructor - with bytes.
57b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
58b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public DERNumericString(
59b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        byte[]   string)
60b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
61b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        char[]  cs = new char[string.length];
62b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
63b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        for (int i = 0; i != cs.length; i++)
64b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
65b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            cs[i] = (char)(string[i] & 0xff);
66b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
67b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
68b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.string = new String(cs);
69b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
70b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
71b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
72c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * basic constructor -  without validation..
73b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
74b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public DERNumericString(
75b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        String   string)
76b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
77c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        this(string, false);
78c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
79c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
80c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    /**
81c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * Constructor with optional validation.
82c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     *
83c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * @param string the base string to wrap.
84c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * @param validate whether or not to check the string.
85c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * @throws IllegalArgumentException if validate is true and the string
86c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * contains characters that should not be in a NumericString.
87c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     */
88c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public DERNumericString(
89c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        String   string,
90c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        boolean  validate)
91c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
92c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (validate && !isNumericString(string))
93c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
94c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            throw new IllegalArgumentException("string contains illegal characters");
95c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
96c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
97b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.string = string;
98b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
99b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
100b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public String getString()
101b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
102b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return string;
103b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
104b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
105c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public String toString()
106c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
107c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return string;
108c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
109c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
110b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public byte[] getOctets()
111b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
112b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        char[]  cs = string.toCharArray();
113b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        byte[]  bs = new byte[cs.length];
114b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
115b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        for (int i = 0; i != cs.length; i++)
116b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
117b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            bs[i] = (byte)cs[i];
118b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
119b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
120b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return bs;
121b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
122b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
123b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    void encode(
124b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        DEROutputStream  out)
125b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        throws IOException
126b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
127b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        out.writeEncoded(NUMERIC_STRING, this.getOctets());
128b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
129b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
130b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public int hashCode()
131b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
132b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return this.getString().hashCode();
133b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
134b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
135c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    boolean asn1Equals(
136c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        DERObject  o)
137b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
138b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (!(o instanceof DERNumericString))
139b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
140b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return false;
141b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
142b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
143b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        DERNumericString  s = (DERNumericString)o;
144b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
145b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return this.getString().equals(s.getString());
146b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
147c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
148c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    /**
149c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * Return true if the string can be represented as a NumericString ('0'..'9', ' ')
150c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     *
151c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * @param str string to validate.
152c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * @return true if numeric, fale otherwise.
153c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     */
154c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    public static boolean isNumericString(
155c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        String  str)
156c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
157c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        for (int i = str.length() - 1; i >= 0; i--)
158c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
159c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            char    ch = str.charAt(i);
160c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
161c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            if (ch > 0x007f)
162c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            {
163c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                return false;
164c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            }
165c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
166c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            if (('0' <= ch && ch <= '9') || ch == ' ')
167c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            {
168c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                continue;
169c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            }
170c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
171c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            return false;
172c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
173c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
174c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return true;
175c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
176b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
177