Home | History | Annotate | Download | only in x509
      1 package org.bouncycastle.asn1.x509;
      2 
      3 import java.io.IOException;
      4 import java.util.Enumeration;
      5 
      6 import org.bouncycastle.asn1.ASN1Encodable;
      7 import org.bouncycastle.asn1.ASN1EncodableVector;
      8 import org.bouncycastle.asn1.ASN1InputStream;
      9 import org.bouncycastle.asn1.ASN1Sequence;
     10 import org.bouncycastle.asn1.ASN1TaggedObject;
     11 import org.bouncycastle.asn1.DERBitString;
     12 import org.bouncycastle.asn1.DEREncodable;
     13 import org.bouncycastle.asn1.DERObject;
     14 import 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  */
     22 public class SubjectPublicKeyInfo
     23     extends ASN1Encodable
     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 instanceof ASN1Sequence)
     43         {
     44             return new SubjectPublicKeyInfo((ASN1Sequence)obj);
     45         }
     46 
     47         throw new IllegalArgumentException("unknown object in factory");
     48     }
     49 
     50     public SubjectPublicKeyInfo(
     51         AlgorithmIdentifier algId,
     52         DEREncodable        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 getAlgorithmId()
     82     {
     83         return algId;
     84     }
     85 
     86     /**
     87      * for when the public key is an encoded object - if the bitstring
     88      * can't be decoded this routine throws an IOException.
     89      *
     90      * @exception IOException - if the bit string doesn't represent a DER
     91      * encoded object.
     92      */
     93     public DERObject getPublicKey()
     94         throws IOException
     95     {
     96         ASN1InputStream         aIn = new ASN1InputStream(keyData.getBytes());
     97 
     98         return aIn.readObject();
     99     }
    100 
    101     /**
    102      * for when the public key is raw bits...
    103      */
    104     public DERBitString getPublicKeyData()
    105     {
    106         return keyData;
    107     }
    108 
    109     /**
    110      * Produce an object suitable for an ASN1OutputStream.
    111      * <pre>
    112      * SubjectPublicKeyInfo ::= SEQUENCE {
    113      *                          algorithm AlgorithmIdentifier,
    114      *                          publicKey BIT STRING }
    115      * </pre>
    116      */
    117     public DERObject toASN1Object()
    118     {
    119         ASN1EncodableVector  v = new ASN1EncodableVector();
    120 
    121         v.add(algId);
    122         v.add(keyData);
    123 
    124         return new DERSequence(v);
    125     }
    126 }
    127