11package org .bouncycastle .asn1 .sec ;
22
33import java .math .BigInteger ;
4- import java .util .Enumeration ;
54
65import org .bouncycastle .asn1 .ASN1BitString ;
76import org .bouncycastle .asn1 .ASN1Encodable ;
2423public class ECPrivateKey
2524 extends ASN1Object
2625{
27- private ASN1Sequence seq ;
28-
29- private ECPrivateKey (
30- ASN1Sequence seq )
31- {
32- this .seq = seq ;
33- }
34-
35- public static ECPrivateKey getInstance (
36- Object obj )
26+ public static ECPrivateKey getInstance (Object obj )
3727 {
3828 if (obj instanceof ECPrivateKey )
3929 {
@@ -48,13 +38,21 @@ public static ECPrivateKey getInstance(
4838 return null ;
4939 }
5040
51- /**
52- * @deprecated use constructor which takes orderBitLength to guarantee correct encoding.
53- */
54- public ECPrivateKey (
55- BigInteger key )
41+ public static ECPrivateKey getInstance (ASN1TaggedObject taggedObject , boolean declaredExplicit )
5642 {
57- this (key .bitLength (), key );
43+ return new ECPrivateKey (ASN1Sequence .getInstance (taggedObject , declaredExplicit ));
44+ }
45+
46+ public static ECPrivateKey getTagged (ASN1TaggedObject taggedObject , boolean declaredExplicit )
47+ {
48+ return new ECPrivateKey (ASN1Sequence .getTagged (taggedObject , declaredExplicit ));
49+ }
50+
51+ private final ASN1Sequence seq ;
52+
53+ private ECPrivateKey (ASN1Sequence seq )
54+ {
55+ this .seq = seq ;
5856 }
5957
6058 /**
@@ -63,49 +61,19 @@ public ECPrivateKey(
6361 * @param orderBitLength the bitLength of the order of the curve.
6462 * @param key the private key value.
6563 */
66- public ECPrivateKey (
67- int orderBitLength ,
68- BigInteger key )
64+ public ECPrivateKey (int orderBitLength , BigInteger key )
6965 {
7066 byte [] bytes = BigIntegers .asUnsignedByteArray ((orderBitLength + 7 ) / 8 , key );
7167
7268 seq = new DERSequence (ASN1Integer .ONE , new DEROctetString (bytes ));
7369 }
7470
75- /**
76- * @deprecated use constructor which takes orderBitLength to guarantee correct encoding.
77- */
78- public ECPrivateKey (
79- BigInteger key ,
80- ASN1Encodable parameters )
81- {
82- this (key , null , parameters );
83- }
84-
85- /**
86- * @deprecated use constructor which takes orderBitLength to guarantee correct encoding.
87- */
88- public ECPrivateKey (
89- BigInteger key ,
90- ASN1BitString publicKey ,
91- ASN1Encodable parameters )
92- {
93- this (key .bitLength (), key , publicKey , parameters );
94- }
95-
96- public ECPrivateKey (
97- int orderBitLength ,
98- BigInteger key ,
99- ASN1Encodable parameters )
71+ public ECPrivateKey (int orderBitLength , BigInteger key , ASN1Encodable parameters )
10072 {
10173 this (orderBitLength , key , null , parameters );
10274 }
10375
104- public ECPrivateKey (
105- int orderBitLength ,
106- BigInteger key ,
107- ASN1BitString publicKey ,
108- ASN1Encodable parameters )
76+ public ECPrivateKey (int orderBitLength , BigInteger key , ASN1BitString publicKey , ASN1Encodable parameters )
10977 {
11078 byte [] bytes = BigIntegers .asUnsignedByteArray ((orderBitLength + 7 ) / 8 , key );
11179
@@ -127,25 +95,39 @@ public ECPrivateKey(
12795 seq = new DERSequence (v );
12896 }
12997
130- public BigInteger getKey ( )
98+ public ECPrivateKey ( ASN1OctetString privateKey , ASN1Encodable parameters , ASN1BitString publicKey )
13199 {
132- ASN1OctetString octs = ( ASN1OctetString ) seq . getObjectAt ( 1 );
100+ ASN1EncodableVector v = new ASN1EncodableVector ( 4 );
133101
134- return new BigInteger (1 , octs .getOctets ());
102+ v .add (ASN1Integer .ONE );
103+ v .add (privateKey );
104+
105+ if (parameters != null )
106+ {
107+ v .add (new DERTaggedObject (true , 0 , parameters ));
108+ }
109+
110+ if (publicKey != null )
111+ {
112+ v .add (new DERTaggedObject (true , 1 , publicKey ));
113+ }
114+
115+ seq = new DERSequence (v );
135116 }
136-
137- public ASN1BitString getPublicKey ()
117+
118+ public BigInteger getKey ()
138119 {
139- return ( ASN1BitString ) getObjectInTag ( 1 , BERTags . BIT_STRING );
120+ return new BigInteger ( 1 , getPrivateKey (). getOctets () );
140121 }
141122
142- /**
143- * @deprecated Use {@link #getParametersObject()} instead and getInstance
144- * methods or similar to get the object at the desired type.
145- */
146- public ASN1Primitive getParameters ()
123+ public ASN1OctetString getPrivateKey ()
147124 {
148- return getParametersObject ().toASN1Primitive ();
125+ return (ASN1OctetString )seq .getObjectAt (1 );
126+ }
127+
128+ public ASN1BitString getPublicKey ()
129+ {
130+ return (ASN1BitString )getObjectInTag (1 , BERTags .BIT_STRING );
149131 }
150132
151133 public ASN1Object getParametersObject ()
@@ -155,21 +137,15 @@ public ASN1Object getParametersObject()
155137
156138 private ASN1Object getObjectInTag (int tagNo , int baseTagNo )
157139 {
158- Enumeration e = seq .getObjects ();
159-
160- while (e .hasMoreElements ())
140+ for (int i = 0 , count = seq .size (); i < count ; ++i )
161141 {
162- ASN1Encodable obj = ( ASN1Encodable ) e . nextElement ( );
163-
164- if (obj instanceof ASN1TaggedObject )
142+ ASN1Encodable element = seq . getObjectAt ( i );
143+ ASN1TaggedObject taggedObject = ASN1TaggedObject . getOptional ( element , BERTags . CONTEXT_SPECIFIC , tagNo );
144+ if (taggedObject != null )
165145 {
166- ASN1TaggedObject tag = (ASN1TaggedObject )obj ;
167- if (tag .hasContextTag (tagNo ))
168- {
169- return baseTagNo < 0
170- ? tag .getExplicitBaseObject ().toASN1Primitive ()
171- : tag .getBaseUniversal (true , baseTagNo );
172- }
146+ return baseTagNo < 0
147+ ? taggedObject .getExplicitBaseObject ().toASN1Primitive ()
148+ : taggedObject .getBaseUniversal (true , baseTagNo );
173149 }
174150 }
175151 return null ;
0 commit comments