SubjectPublicKeyInfo.java revision e6bf3e8dfa2804891a82075cb469b736321b4827
1package org.bouncycastle.asn1.x509;
2
3import java.io.IOException;
4import java.util.Enumeration;
5
6import org.bouncycastle.asn1.ASN1Encodable;
7import org.bouncycastle.asn1.ASN1EncodableVector;
8import org.bouncycastle.asn1.ASN1InputStream;
9import org.bouncycastle.asn1.ASN1Object;
10import org.bouncycastle.asn1.ASN1Primitive;
11import org.bouncycastle.asn1.ASN1Sequence;
12import org.bouncycastle.asn1.ASN1TaggedObject;
13import org.bouncycastle.asn1.DERBitString;
14import org.bouncycastle.asn1.DERSequence;
15
16/**
17 * The object that contains the public key stored in a certficate.
18 * <p>
19 * The getEncoded() method in the public keys in the JCE produces a DER
20 * encoded one of these.
21 */
22public class SubjectPublicKeyInfo
23    extends ASN1Object
24{
25    private AlgorithmIdentifier     algId;
26    private DERBitString            keyData;
27
28    public static SubjectPublicKeyInfo getInstance(
29        ASN1TaggedObject obj,
30        boolean          explicit)
31    {
32        return getInstance(ASN1Sequence.getInstance(obj, explicit));
33    }
34
35    public static SubjectPublicKeyInfo getInstance(
36        Object  obj)
37    {
38        if (obj instanceof SubjectPublicKeyInfo)
39        {
40            return (SubjectPublicKeyInfo)obj;
41        }
42        else if (obj != null)
43        {
44            return new SubjectPublicKeyInfo(ASN1Sequence.getInstance(obj));
45        }
46
47        return null;
48    }
49
50    public SubjectPublicKeyInfo(
51        AlgorithmIdentifier algId,
52        ASN1Encodable       publicKey)
53    {
54        this.keyData = new DERBitString(publicKey);
55        this.algId = algId;
56    }
57
58    public SubjectPublicKeyInfo(
59        AlgorithmIdentifier algId,
60        byte[]              publicKey)
61    {
62        this.keyData = new DERBitString(publicKey);
63        this.algId = algId;
64    }
65
66    public SubjectPublicKeyInfo(
67        ASN1Sequence  seq)
68    {
69        if (seq.size() != 2)
70        {
71            throw new IllegalArgumentException("Bad sequence size: "
72                    + seq.size());
73        }
74
75        Enumeration         e = seq.getObjects();
76
77        this.algId = AlgorithmIdentifier.getInstance(e.nextElement());
78        this.keyData = DERBitString.getInstance(e.nextElement());
79    }
80
81    public AlgorithmIdentifier getAlgorithm()
82    {
83        return algId;
84    }
85
86    /**
87     * @deprecated use getAlgorithm()
88     * @return    alg ID.
89     */
90    public AlgorithmIdentifier getAlgorithmId()
91    {
92        return algId;
93    }
94
95    /**
96     * for when the public key is an encoded object - if the bitstring
97     * can't be decoded this routine throws an IOException.
98     *
99     * @exception IOException - if the bit string doesn't represent a DER
100     * encoded object.
101     * @return the public key as an ASN.1 primitive.
102     */
103    public ASN1Primitive parsePublicKey()
104        throws IOException
105    {
106        ASN1InputStream         aIn = new ASN1InputStream(keyData.getBytes());
107
108        return aIn.readObject();
109    }
110
111    /**
112     * for when the public key is an encoded object - if the bitstring
113     * can't be decoded this routine throws an IOException.
114     *
115     * @exception IOException - if the bit string doesn't represent a DER
116     * encoded object.
117     * @deprecated use parsePublicKey
118     * @return the public key as an ASN.1 primitive.
119     */
120    public ASN1Primitive getPublicKey()
121        throws IOException
122    {
123        ASN1InputStream         aIn = new ASN1InputStream(keyData.getBytes());
124
125        return aIn.readObject();
126    }
127
128    /**
129     * for when the public key is raw bits.
130     *
131     * @return the public key as the raw bit string...
132     */
133    public DERBitString getPublicKeyData()
134    {
135        return keyData;
136    }
137
138    /**
139     * Produce an object suitable for an ASN1OutputStream.
140     * <pre>
141     * SubjectPublicKeyInfo ::= SEQUENCE {
142     *                          algorithm AlgorithmIdentifier,
143     *                          publicKey BIT STRING }
144     * </pre>
145     */
146    public ASN1Primitive toASN1Primitive()
147    {
148        ASN1EncodableVector  v = new ASN1EncodableVector();
149
150        v.add(algId);
151        v.add(keyData);
152
153        return new DERSequence(v);
154    }
155}
156