1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.asn1.pkcs;
2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
3b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.math.BigInteger;
4b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
5b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1EncodableVector;
64c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.ASN1Integer;
74c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.ASN1Object;
8b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1OctetString;
94c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.ASN1Primitive;
10b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.ASN1Sequence;
11b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.DEROctetString;
12b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.DERSequence;
13b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport org.bouncycastle.asn1.x509.DigestInfo;
14b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
15b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic class MacData
164c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    extends ASN1Object
17b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{
18c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    private static final BigInteger ONE = BigInteger.valueOf(1);
19c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
20b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    DigestInfo                  digInfo;
21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    byte[]                      salt;
22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    BigInteger                  iterationCount;
23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
24b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public static MacData getInstance(
25b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        Object  obj)
26b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
27b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (obj instanceof MacData)
28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
29b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return (MacData)obj;
30b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
314c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        else if (obj != null)
32b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
334c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom            return new MacData(ASN1Sequence.getInstance(obj));
34b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
35b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
364c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        return null;
37b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
38b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
394c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    private MacData(
40b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1Sequence seq)
41b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
42b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.digInfo = DigestInfo.getInstance(seq.getObjectAt(0));
43b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
44b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.salt = ((ASN1OctetString)seq.getObjectAt(1)).getOctets();
45b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
46b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (seq.size() == 3)
47b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
484c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom            this.iterationCount = ((ASN1Integer)seq.getObjectAt(2)).getValue();
49b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
50b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        else
51b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
52c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            this.iterationCount = ONE;
53b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
54b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
55b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
56b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public MacData(
57b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        DigestInfo  digInfo,
58b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        byte[]      salt,
59b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        int         iterationCount)
60b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
61b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.digInfo = digInfo;
62b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.salt = salt;
63b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        this.iterationCount = BigInteger.valueOf(iterationCount);
64b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
65b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
66b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public DigestInfo getMac()
67b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
68b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return digInfo;
69b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
70b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
71b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public byte[] getSalt()
72b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
73b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return salt;
74b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
75b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
76b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public BigInteger getIterationCount()
77b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
78b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return iterationCount;
79b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
80b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
81c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    /**
82c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * <pre>
83c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * MacData ::= SEQUENCE {
84c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     *     mac      DigestInfo,
85c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     *     macSalt  OCTET STRING,
86c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     *     iterations INTEGER DEFAULT 1
87c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     *     -- Note: The default is for historic reasons and its use is deprecated. A
88c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     *     -- higher value, like 1024 is recommended.
89c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * </pre>
904c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom     * @return the basic ASN1Primitive construction.
91c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     */
924c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    public ASN1Primitive toASN1Primitive()
93b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
94b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        ASN1EncodableVector  v = new ASN1EncodableVector();
95b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
96b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        v.add(digInfo);
97b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        v.add(new DEROctetString(salt));
98c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
99c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (!iterationCount.equals(ONE))
100c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
1014c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom            v.add(new ASN1Integer(iterationCount));
102c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
103b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
104b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return new DERSequence(v);
105b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
106b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
107