1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.asn1;
2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
3b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.io.IOException;
4b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.math.BigInteger;
5b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
6c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport org.bouncycastle.util.Arrays;
7c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
85db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Root/**
95db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Root * Use ASN1Integer instead of this,
105db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Root */
11b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic class DERInteger
124c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    extends ASN1Primitive
13b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{
14b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    byte[]      bytes;
15b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
16b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
17b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * return an integer from the passed in object
18b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
19b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @exception IllegalArgumentException if the object cannot be converted.
20b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
214c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    public static ASN1Integer getInstance(
22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        Object  obj)
23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
244c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        if (obj == null || obj instanceof ASN1Integer)
25b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
264c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom            return (ASN1Integer)obj;
274c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        }
284c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        if (obj instanceof DERInteger)
294c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        {
304c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom            return new ASN1Integer((((DERInteger)obj).getValue()));
31b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
32b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
3370c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom        if (obj instanceof byte[])
3470c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom        {
3570c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom            try
3670c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom            {
3770c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom                return (ASN1Integer)fromByteArray((byte[])obj);
3870c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom            }
3970c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom            catch (Exception e)
4070c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom            {
4170c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom                throw new IllegalArgumentException("encoding error in getInstance: " + e.toString());
4270c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom            }
4370c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom        }
4470c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom
45b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
46b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
47b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
48b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
49b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * return an Integer from a tagged object.
50b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *
51b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param obj the tagged object holding the object we want
52b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @param explicit true if the object is meant to be explicitly
53b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *              tagged false otherwise.
54b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * @exception IllegalArgumentException if the tagged object cannot
55b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     *               be converted.
56b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
574c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    public static ASN1Integer getInstance(
58b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1TaggedObject obj,
59b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        boolean          explicit)
60b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
614c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        ASN1Primitive o = obj.getObject();
626e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom
636e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        if (explicit || o instanceof DERInteger)
646e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        {
656e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom            return getInstance(o);
666e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        }
676e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        else
686e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        {
696e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom            return new ASN1Integer(ASN1OctetString.getInstance(obj.getObject()).getOctets());
706e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        }
71b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
72b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
735db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Root    /**
745db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Root     * @deprecated use ASN1Integer constructor
755db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Root     */
76b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public DERInteger(
7770c8287138e69a98c2f950036f9f703ee37228c8Brian Carlstrom        long         value)
78b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
79b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        bytes = BigInteger.valueOf(value).toByteArray();
80b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
81b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
825db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Root    /**
835db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Root     * @deprecated use ASN1Integer constructor
845db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Root     */
85b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public DERInteger(
86b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        BigInteger   value)
87b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
88b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        bytes = value.toByteArray();
89b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
90b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
915db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Root    /**
925db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Root     * @deprecated use ASN1Integer constructor
935db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Root     */
94b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public DERInteger(
95b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        byte[]   bytes)
96b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
97b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.bytes = bytes;
98b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
99b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
100b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public BigInteger getValue()
101b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
102b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return new BigInteger(bytes);
103b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
104b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
105b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
106b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * in some cases positive values get crammed into a space,
107b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * that's not quite big enough...
108b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
109b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public BigInteger getPositiveValue()
110b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
111b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return new BigInteger(1, bytes);
112b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
113b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
1144c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    boolean isConstructed()
1154c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    {
1164c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        return false;
1174c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    }
1184c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom
1194c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    int encodedLength()
1204c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    {
1214c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        return 1 + StreamUtil.calculateBodyLength(bytes.length) + bytes.length;
1224c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    }
1234c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom
124b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    void encode(
1254c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        ASN1OutputStream out)
126b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        throws IOException
127b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
1284c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        out.writeEncoded(BERTags.INTEGER, bytes);
129b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
130b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
131b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public int hashCode()
132b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
133b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam         int     value = 0;
134b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
135b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam         for (int i = 0; i != bytes.length; i++)
136b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam         {
137b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam             value ^= (bytes[i] & 0xff) << (i % 4);
138b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam         }
139b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
140b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam         return value;
141b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
142b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
143c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    boolean asn1Equals(
1444c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        ASN1Primitive  o)
145b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
146b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (!(o instanceof DERInteger))
147b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
148b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return false;
149b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
150b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
151b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        DERInteger other = (DERInteger)o;
152b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
153c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return Arrays.areEqual(bytes, other.bytes);
154b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
155b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
156b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public String toString()
157b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
158b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam      return getValue().toString();
159b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
160b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
161