resolve darcs stupidity
[org.ibex.core.git] / src / org / bouncycastle / asn1 / x509 / SubjectPublicKeyInfo.java
index 2e65881..aa3a466 100644 (file)
@@ -2,10 +2,8 @@ package org.bouncycastle.asn1.x509;
 
 import java.io.*;
 import java.util.Enumeration;
-import java.math.BigInteger;
 
 import org.bouncycastle.asn1.*;
-import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
 
 /**
  * The object that contains the public key stored in a certficate.
@@ -17,36 +15,53 @@ public class SubjectPublicKeyInfo
     implements DEREncodable
 {
     private AlgorithmIdentifier     algId;
-    private DERObject               pubKey;
+    private DERBitString            keyData;
+
+    public static SubjectPublicKeyInfo getInstance(
+        ASN1TaggedObject obj,
+        boolean          explicit)
+    {
+        return getInstance(ASN1Sequence.getInstance(obj, explicit));
+    }
+
+    public static SubjectPublicKeyInfo getInstance(
+        Object  obj)
+    {
+        if (obj instanceof SubjectPublicKeyInfo)
+        {
+            return (SubjectPublicKeyInfo)obj;
+        }
+        else if (obj instanceof ASN1Sequence)
+        {
+            return new SubjectPublicKeyInfo((ASN1Sequence)obj);
+        }
+
+        throw new IllegalArgumentException("unknown object in factory");
+    }
 
     public SubjectPublicKeyInfo(
         AlgorithmIdentifier algId,
-        DERObject           publicKey)
+        DEREncodable        publicKey)
     {
-        this.pubKey = publicKey;
+        this.keyData = new DERBitString(publicKey);
         this.algId = algId;
     }
 
     public SubjectPublicKeyInfo(
-        DERConstructedSequence  seq)
+        AlgorithmIdentifier algId,
+        byte[]              publicKey)
     {
-        Enumeration         e = seq.getObjects();
-
-        algId = new AlgorithmIdentifier((DERConstructedSequence)e.nextElement());
-
-        byte[]  keyData = ((DERBitString)e.nextElement()).getBytes();
+        this.keyData = new DERBitString(publicKey);
+        this.algId = algId;
+    }
 
-        try
-        {
-            ByteArrayInputStream    bIn = new ByteArrayInputStream(keyData);
-            DERInputStream          dIn = new DERInputStream(bIn);
+    public SubjectPublicKeyInfo(
+        ASN1Sequence  seq)
+    {
+        Enumeration         e = seq.getObjects();
 
-            pubKey = (DERObject)dIn.readObject();
-        }
-        catch (IOException ex)
-        {
-            throw new IllegalArgumentException("error recovering public key");
-        }
+        this.algId = AlgorithmIdentifier.getInstance(e.nextElement());
+        this.keyData = (DERBitString)e.nextElement();
     }
 
     public AlgorithmIdentifier getAlgorithmId()
@@ -54,9 +69,28 @@ public class SubjectPublicKeyInfo
         return algId;
     }
 
+    /**
+     * for when the public key is an encoded object - if the bitstring
+     * can't be decoded this routine throws an IOException.
+     *
+     * @exception IOException - if the bit string doesn't represent a DER
+     * encoded object.
+     */
     public DERObject getPublicKey()
+        throws IOException
+    {
+        ByteArrayInputStream    bIn = new ByteArrayInputStream(keyData.getBytes());
+        DERInputStream          dIn = new DERInputStream(bIn);
+
+        return dIn.readObject();
+    }
+
+    /**
+     * for when the public key is raw bits...
+     */
+    public DERBitString getPublicKeyData()
     {
-        return pubKey;
+        return keyData;
     }
 
     /**
@@ -71,7 +105,7 @@ public class SubjectPublicKeyInfo
         DERConstructedSequence  seq = new DERConstructedSequence();
 
         seq.addObject(algId);
-        seq.addObject(new DERBitString(pubKey));
+        seq.addObject(keyData);
 
         return seq;
     }