2002/03/21 01:19:32
[org.ibex.core.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 import java.math.BigInteger;
6
7 import org.bouncycastle.asn1.*;
8 import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
9
10 /**
11  * The object that contains the public key stored in a certficate.
12  * <p>
13  * The getEncoded() method in the public keys in the JCE produces a DER
14  * encoded one of these.
15  */
16 public class SubjectPublicKeyInfo
17     implements DEREncodable
18 {
19     private AlgorithmIdentifier     algId;
20     private DERObject               pubKey;
21
22     public SubjectPublicKeyInfo(
23         AlgorithmIdentifier algId,
24         DERObject           publicKey)
25     {
26         this.pubKey = publicKey;
27         this.algId = algId;
28     }
29
30     public SubjectPublicKeyInfo(
31         DERConstructedSequence  seq)
32     {
33         Enumeration         e = seq.getObjects();
34
35         algId = new AlgorithmIdentifier((DERConstructedSequence)e.nextElement());
36
37         byte[]  keyData = ((DERBitString)e.nextElement()).getBytes();
38
39         try
40         {
41             ByteArrayInputStream    bIn = new ByteArrayInputStream(keyData);
42             DERInputStream          dIn = new DERInputStream(bIn);
43
44             pubKey = (DERObject)dIn.readObject();
45         }
46         catch (IOException ex)
47         {
48             throw new IllegalArgumentException("error recovering public key");
49         }
50     }
51
52     public AlgorithmIdentifier getAlgorithmId()
53     {
54         return algId;
55     }
56
57     public DERObject getPublicKey()
58     {
59         return pubKey;
60     }
61
62     /**
63      * <pre>
64      * SubjectPublicKeyInfo ::= SEQUENCE {
65      *                          algorithm AlgorithmIdentifier,
66      *                          publicKey BIT STRING }
67      * </pre>
68      */
69     public DERObject getDERObject()
70     {
71         DERConstructedSequence  seq = new DERConstructedSequence();
72
73         seq.addObject(algId);
74         seq.addObject(new DERBitString(pubKey));
75
76         return seq;
77     }
78 }