aboutsummaryrefslogtreecommitdiff
path: root/src/main/model/asn1/Int.java
blob: 4eeeedf782b566362a5110526cecd948cd01e414 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package model.asn1;

import model.asn1.exceptions.ParseException;
import model.asn1.parsing.BytesReader;
import model.pki.cert.TbsCertificate;
import ui.Utils;

import java.math.BigInteger;
import java.util.Arrays;

/**
 * An ASN.1 INTEGER type. By spec, it can be arbitrarily long. But just like it's impossible to have an
 * endless tape in a turing machine, this implementation uses fixed length internally to represent ints.
 */
public class Int extends ASN1Object {
    /**
     * The X.680 universal class tag assignment.
     */
    public static final Tag TAG = new Tag(TagClass.UNIVERSAL, false, 0x2);

    private final BigInteger value;

    /**
     * EFFECTS: Initiate the INTEGER object with the given tag and an optional context-specific tag number for explicit
     * encoding. For more information, consult {@link ASN1Object}.
     * REQUIRES: Consult {@link ASN1Object}.
     */
    public Int(Tag tag, Tag parentTag, BigInteger value) {
        super(tag, parentTag);
        this.value = value;
    }

    /**
     * EFFECTS: Initiate the INTEGER object with the given tag and an optional context-specific tag number for explicit
     * encoding. For more information, consult {@link ASN1Object}.
     * REQUIRES: Consult {@link ASN1Object}.
     */
    public Int(Tag tag, Tag parentTag, long value) {
        this(tag, parentTag, BigInteger.valueOf(value));
    }

    /**
     * EFFECTS: Parse input and get the int value. Tags are parsed in {@link ASN1Object}.
     *   Throws {@link  ParseException} if encoded value are invalid:
     *     - Early EOF (not enough bytes)
     *     - Zero bytes length
     *     - Other issues denoted in {@link ASN1Object}
     * MODIFIES: this, encoded
     */
    public Int(BytesReader encoded, boolean hasParentTag) throws ParseException {
        super(encoded, hasParentTag);
        if (getLength() == 0) {
            throw new ParseException("Integer with zero length");
        }
        this.value = new BigInteger(Utils.byteToByte(encoded.require(getLength(), true)));
    }

    /**
     * EFFECTS: Produce the big-endian two's complement encoding of the value, in the shortest possible way (i.e., no
     * leading 0x0 bytes, and no leading 0xFF bytes if negative). Notes, if a positive number is desired (or mandated
     * like {@link TbsCertificate#getSerialNumber()}, append 0x0 to the MSB manually. This method always results in a
     * signed integer. For simplicity, the first 0x0 is always removed except when the number itself is 0, and others
     * are kept.
     */
    @Override
    public Byte[] encodeValueDER() {
        Byte[] bytes = Utils.byteToByte(value.toByteArray());
        if (bytes.length == 1) {
            return bytes;
        }
        if (bytes[0] == 0x0) {
            return Arrays.stream(bytes)
                    .skip(1)
                    .toArray(Byte[]::new);
        }
        return bytes;
    }

    /**
     * EFFECTS: Get the value in long.
     *   Throws {@link ArithmeticException} if the value is too large for long.
     */
    public long getLong() throws ArithmeticException {
        return value.longValueExact();
    }

    public BigInteger getValue() {
        return value;
    }
}