01: package org.bouncycastle.asn1.sec;
02:
03: import org.bouncycastle.asn1.ASN1Encodable;
04: import org.bouncycastle.asn1.ASN1EncodableVector;
05: import org.bouncycastle.asn1.ASN1Object;
06: import org.bouncycastle.asn1.ASN1OctetString;
07: import org.bouncycastle.asn1.ASN1Sequence;
08: import org.bouncycastle.asn1.ASN1TaggedObject;
09: import org.bouncycastle.asn1.DERBitString;
10: import org.bouncycastle.asn1.DEREncodable;
11: import org.bouncycastle.asn1.DERInteger;
12: import org.bouncycastle.asn1.DERObject;
13: import org.bouncycastle.asn1.DEROctetString;
14: import org.bouncycastle.asn1.DERSequence;
15:
16: import java.math.BigInteger;
17: import java.util.Enumeration;
18:
19: /**
20: * the elliptic curve private key object from SEC 1
21: */
22: public class ECPrivateKeyStructure extends ASN1Encodable {
23: private ASN1Sequence seq;
24:
25: public ECPrivateKeyStructure(ASN1Sequence seq) {
26: this .seq = seq;
27: }
28:
29: public ECPrivateKeyStructure(BigInteger key) {
30: byte[] bytes = key.toByteArray();
31:
32: if (bytes[0] == 0) {
33: byte[] tmp = new byte[bytes.length - 1];
34:
35: System.arraycopy(bytes, 1, tmp, 0, tmp.length);
36: bytes = tmp;
37: }
38:
39: ASN1EncodableVector v = new ASN1EncodableVector();
40:
41: v.add(new DERInteger(1));
42: v.add(new DEROctetString(bytes));
43:
44: seq = new DERSequence(v);
45: }
46:
47: public BigInteger getKey() {
48: ASN1OctetString octs = (ASN1OctetString) seq.getObjectAt(1);
49:
50: return new BigInteger(1, octs.getOctets());
51: }
52:
53: public DERBitString getPublicKey() {
54: return (DERBitString) getObjectInTag(1);
55: }
56:
57: public ASN1Object getParameters() {
58: return getObjectInTag(0);
59: }
60:
61: private ASN1Object getObjectInTag(int tagNo) {
62: Enumeration e = seq.getObjects();
63:
64: while (e.hasMoreElements()) {
65: DEREncodable obj = (DEREncodable) e.nextElement();
66:
67: if (obj instanceof ASN1TaggedObject) {
68: ASN1TaggedObject tag = (ASN1TaggedObject) obj;
69: if (tag.getTagNo() == tagNo) {
70: return (ASN1Object) ((DEREncodable) tag.getObject())
71: .getDERObject();
72: }
73: }
74: }
75: return null;
76: }
77:
78: /**
79: * ECPrivateKey ::= SEQUENCE {
80: * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
81: * privateKey OCTET STRING,
82: * parameters [0] Parameters OPTIONAL,
83: * publicKey [1] BIT STRING OPTIONAL }
84: */
85: public DERObject toASN1Object() {
86: return seq;
87: }
88: }
|