ECPrivateKey.java revision e6bf3e8dfa2804891a82075cb469b736321b4827
1package org.bouncycastle.asn1.sec;
2
3import java.math.BigInteger;
4import java.util.Enumeration;
5
6import org.bouncycastle.asn1.ASN1Encodable;
7import org.bouncycastle.asn1.ASN1EncodableVector;
8import org.bouncycastle.asn1.ASN1Integer;
9import org.bouncycastle.asn1.ASN1Object;
10import org.bouncycastle.asn1.ASN1OctetString;
11import org.bouncycastle.asn1.ASN1Primitive;
12import org.bouncycastle.asn1.ASN1Sequence;
13import org.bouncycastle.asn1.ASN1TaggedObject;
14import org.bouncycastle.asn1.DERBitString;
15import org.bouncycastle.asn1.DEROctetString;
16import org.bouncycastle.asn1.DERSequence;
17import org.bouncycastle.asn1.DERTaggedObject;
18import org.bouncycastle.util.BigIntegers;
19
20/**
21 * the elliptic curve private key object from SEC 1
22 */
23public class ECPrivateKey
24    extends ASN1Object
25{
26    private ASN1Sequence seq;
27
28    private ECPrivateKey(
29        ASN1Sequence seq)
30    {
31        this.seq = seq;
32    }
33
34    public static ECPrivateKey getInstance(
35        Object obj)
36    {
37        if (obj instanceof ECPrivateKey)
38        {
39            return (ECPrivateKey)obj;
40        }
41
42        if (obj != null)
43        {
44            return new ECPrivateKey(ASN1Sequence.getInstance(obj));
45        }
46
47        return null;
48    }
49
50    public ECPrivateKey(
51        BigInteger key)
52    {
53        byte[] bytes = BigIntegers.asUnsignedByteArray(key);
54
55        ASN1EncodableVector v = new ASN1EncodableVector();
56
57        v.add(new ASN1Integer(1));
58        v.add(new DEROctetString(bytes));
59
60        seq = new DERSequence(v);
61    }
62
63    public ECPrivateKey(
64        BigInteger key,
65        ASN1Object parameters)
66    {
67        this(key, null, parameters);
68    }
69
70    public ECPrivateKey(
71        BigInteger key,
72        DERBitString publicKey,
73        ASN1Object parameters)
74    {
75        byte[] bytes = BigIntegers.asUnsignedByteArray(key);
76
77        ASN1EncodableVector v = new ASN1EncodableVector();
78
79        v.add(new ASN1Integer(1));
80        v.add(new DEROctetString(bytes));
81
82        if (parameters != null)
83        {
84            v.add(new DERTaggedObject(true, 0, parameters));
85        }
86
87        if (publicKey != null)
88        {
89            v.add(new DERTaggedObject(true, 1, publicKey));
90        }
91
92        seq = new DERSequence(v);
93    }
94
95    public BigInteger getKey()
96    {
97        ASN1OctetString octs = (ASN1OctetString)seq.getObjectAt(1);
98
99        return new BigInteger(1, octs.getOctets());
100    }
101
102    public DERBitString getPublicKey()
103    {
104        return (DERBitString)getObjectInTag(1);
105    }
106
107    public ASN1Primitive getParameters()
108    {
109        return getObjectInTag(0);
110    }
111
112    private ASN1Primitive getObjectInTag(int tagNo)
113    {
114        Enumeration e = seq.getObjects();
115
116        while (e.hasMoreElements())
117        {
118            ASN1Encodable obj = (ASN1Encodable)e.nextElement();
119
120            if (obj instanceof ASN1TaggedObject)
121            {
122                ASN1TaggedObject tag = (ASN1TaggedObject)obj;
123                if (tag.getTagNo() == tagNo)
124                {
125                    return tag.getObject().toASN1Primitive();
126                }
127            }
128        }
129        return null;
130    }
131
132    /**
133     * ECPrivateKey ::= SEQUENCE {
134     *     version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
135     *     privateKey OCTET STRING,
136     *     parameters [0] Parameters OPTIONAL,
137     *     publicKey [1] BIT STRING OPTIONAL }
138     */
139    public ASN1Primitive toASN1Primitive()
140    {
141        return seq;
142    }
143}
144