PrivateKeyInfo.java revision e6bf3e8dfa2804891a82075cb469b736321b4827
1package org.bouncycastle.asn1.pkcs;
2
3import java.io.IOException;
4import java.math.BigInteger;
5import java.util.Enumeration;
6
7import org.bouncycastle.asn1.ASN1Encodable;
8import org.bouncycastle.asn1.ASN1EncodableVector;
9import org.bouncycastle.asn1.ASN1Encoding;
10import org.bouncycastle.asn1.ASN1Integer;
11import org.bouncycastle.asn1.ASN1Object;
12import org.bouncycastle.asn1.ASN1OctetString;
13import org.bouncycastle.asn1.ASN1Primitive;
14import org.bouncycastle.asn1.ASN1Sequence;
15import org.bouncycastle.asn1.ASN1Set;
16import org.bouncycastle.asn1.ASN1TaggedObject;
17import org.bouncycastle.asn1.DEROctetString;
18import org.bouncycastle.asn1.DERSequence;
19import org.bouncycastle.asn1.DERTaggedObject;
20import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
21
22public class PrivateKeyInfo
23    extends ASN1Object
24{
25    private ASN1OctetString         privKey;
26    private AlgorithmIdentifier     algId;
27    private ASN1Set                 attributes;
28
29    public static PrivateKeyInfo getInstance(
30        ASN1TaggedObject obj,
31        boolean          explicit)
32    {
33        return getInstance(ASN1Sequence.getInstance(obj, explicit));
34    }
35
36    public static PrivateKeyInfo getInstance(
37        Object  obj)
38    {
39        if (obj instanceof PrivateKeyInfo)
40        {
41            return (PrivateKeyInfo)obj;
42        }
43        else if (obj != null)
44        {
45            return new PrivateKeyInfo(ASN1Sequence.getInstance(obj));
46        }
47
48        return null;
49    }
50
51    public PrivateKeyInfo(
52        AlgorithmIdentifier algId,
53        ASN1Encodable       privateKey)
54        throws IOException
55    {
56        this(algId, privateKey, null);
57    }
58
59    public PrivateKeyInfo(
60        AlgorithmIdentifier algId,
61        ASN1Encodable       privateKey,
62        ASN1Set             attributes)
63        throws IOException
64    {
65        this.privKey = new DEROctetString(privateKey.toASN1Primitive().getEncoded(ASN1Encoding.DER));
66        this.algId = algId;
67        this.attributes = attributes;
68    }
69
70    public PrivateKeyInfo(
71        ASN1Sequence  seq)
72    {
73        Enumeration e = seq.getObjects();
74
75        BigInteger  version = ((ASN1Integer)e.nextElement()).getValue();
76        if (version.intValue() != 0)
77        {
78            throw new IllegalArgumentException("wrong version for private key info");
79        }
80
81        algId = AlgorithmIdentifier.getInstance(e.nextElement());
82        privKey = ASN1OctetString.getInstance(e.nextElement());
83
84        if (e.hasMoreElements())
85        {
86           attributes = ASN1Set.getInstance((ASN1TaggedObject)e.nextElement(), false);
87        }
88    }
89
90    public AlgorithmIdentifier getPrivateKeyAlgorithm()
91    {
92        return algId;
93    }
94        /**
95          * @deprecated use getPrivateKeyAlgorithm()
96     */
97    public AlgorithmIdentifier getAlgorithmId()
98    {
99        return algId;
100    }
101
102    public ASN1Encodable parsePrivateKey()
103        throws IOException
104    {
105        return ASN1Primitive.fromByteArray(privKey.getOctets());
106    }
107
108    /**
109          * @deprecated use parsePrivateKey()
110     */
111    public ASN1Primitive getPrivateKey()
112    {
113        try
114        {
115            return parsePrivateKey().toASN1Primitive();
116        }
117        catch (IOException e)
118        {
119            throw new IllegalStateException("unable to parse private key");
120        }
121    }
122
123    public ASN1Set getAttributes()
124    {
125        return attributes;
126    }
127
128    /**
129     * write out an RSA private key with its associated information
130     * as described in PKCS8.
131     * <pre>
132     *      PrivateKeyInfo ::= SEQUENCE {
133     *                              version Version,
134     *                              privateKeyAlgorithm AlgorithmIdentifier {{PrivateKeyAlgorithms}},
135     *                              privateKey PrivateKey,
136     *                              attributes [0] IMPLICIT Attributes OPTIONAL
137     *                          }
138     *      Version ::= INTEGER {v1(0)} (v1,...)
139     *
140     *      PrivateKey ::= OCTET STRING
141     *
142     *      Attributes ::= SET OF Attribute
143     * </pre>
144     */
145    public ASN1Primitive toASN1Primitive()
146    {
147        ASN1EncodableVector v = new ASN1EncodableVector();
148
149        v.add(new ASN1Integer(0));
150        v.add(algId);
151        v.add(privKey);
152
153        if (attributes != null)
154        {
155            v.add(new DERTaggedObject(false, 0, attributes));
156        }
157
158        return new DERSequence(v);
159    }
160}
161