initial checkin
[org.ibex.nanogoat.git] / src / org / bouncycastle / asn1 / x509 / SubjectPublicKeyInfo.java
1 package org.bouncycastle.asn1.x509;
2
3 import java.io.*;
4 import java.util.Enumeration;
5
6 import org.bouncycastle.asn1.*;
7
8 /**
9  * The object that contains the public key stored in a certficate.
10  * <p>
11  * The getEncoded() method in the public keys in the JCE produces a DER
12  * encoded one of these.
13  */
14 public class SubjectPublicKeyInfo
15     implements DEREncodable
16 {
17     private AlgorithmIdentifier     algId;
18     private DERBitString            keyData;
19
20     public static SubjectPublicKeyInfo getInstance(
21         ASN1TaggedObject obj,
22         boolean          explicit)
23     {
24         return getInstance(ASN1Sequence.getInstance(obj, explicit));
25     }
26
27     public static SubjectPublicKeyInfo getInstance(
28         Object  obj)
29     {
30         if (obj instanceof SubjectPublicKeyInfo)
31         {
32             return (SubjectPublicKeyInfo)obj;
33         }
34         else if (obj instanceof ASN1Sequence)
35         {
36             return new SubjectPublicKeyInfo((ASN1Sequence)obj);
37         }
38
39         throw new IllegalArgumentException("unknown object in factory");
40     }
41
42     public SubjectPublicKeyInfo(
43         AlgorithmIdentifier algId,
44         DEREncodable        publicKey)
45     {
46         this.keyData = new DERBitString(publicKey);
47         this.algId = algId;
48     }
49
50     public SubjectPublicKeyInfo(
51         AlgorithmIdentifier algId,
52         byte[]              publicKey)
53     {
54         this.keyData = new DERBitString(publicKey);
55         this.algId = algId;
56     }
57
58     public SubjectPublicKeyInfo(
59         ASN1Sequence  seq)
60     {
61         Enumeration         e = seq.getObjects();
62
63         this.algId = AlgorithmIdentifier.getInstance(e.nextElement());
64         this.keyData = (DERBitString)e.nextElement();
65     }
66
67     public AlgorithmIdentifier getAlgorithmId()
68     {
69         return algId;
70     }
71
72     /**
73      * for when the public key is an encoded object - if the bitstring
74      * can't be decoded this routine throws an IOException.
75      *
76      * @exception IOException - if the bit string doesn't represent a DER
77      * encoded object.
78      */
79     public DERObject getPublicKey()
80         throws IOException
81     {
82         ByteArrayInputStream    bIn = new ByteArrayInputStream(keyData.getBytes());
83         DERInputStream          dIn = new DERInputStream(bIn);
84
85         return dIn.readObject();
86     }
87
88     /**
89      * for when the public key is raw bits...
90      */
91     public DERBitString getPublicKeyData()
92     {
93         return keyData;
94     }
95
96     /**
97      * <pre>
98      * SubjectPublicKeyInfo ::= SEQUENCE {
99      *                          algorithm AlgorithmIdentifier,
100      *                          publicKey BIT STRING }
101      * </pre>
102      */
103     public DERObject getDERObject()
104     {
105         DERConstructedSequence  seq = new DERConstructedSequence();
106
107         seq.addObject(algId);
108         seq.addObject(keyData);
109
110         return seq;
111     }
112 }