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