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()
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;
}
/**
DERConstructedSequence seq = new DERConstructedSequence();
seq.addObject(algId);
- seq.addObject(new DERBitString(pubKey));
+ seq.addObject(keyData);
return seq;
}