1 package org.bouncycastle.asn1.x509;
4 import java.util.Enumeration;
6 import org.bouncycastle.asn1.*;
9 * The object that contains the public key stored in a certficate.
11 * The getEncoded() method in the public keys in the JCE produces a DER
12 * encoded one of these.
14 public class SubjectPublicKeyInfo
15 implements DEREncodable
17 private AlgorithmIdentifier algId;
18 private DERBitString keyData;
20 public static SubjectPublicKeyInfo getInstance(
24 return getInstance(ASN1Sequence.getInstance(obj, explicit));
27 public static SubjectPublicKeyInfo getInstance(
30 if (obj instanceof SubjectPublicKeyInfo)
32 return (SubjectPublicKeyInfo)obj;
34 else if (obj instanceof ASN1Sequence)
36 return new SubjectPublicKeyInfo((ASN1Sequence)obj);
39 throw new IllegalArgumentException("unknown object in factory");
42 public SubjectPublicKeyInfo(
43 AlgorithmIdentifier algId,
44 DEREncodable publicKey)
46 this.keyData = new DERBitString(publicKey);
50 public SubjectPublicKeyInfo(
51 AlgorithmIdentifier algId,
54 this.keyData = new DERBitString(publicKey);
58 public SubjectPublicKeyInfo(
61 Enumeration e = seq.getObjects();
63 this.algId = AlgorithmIdentifier.getInstance(e.nextElement());
64 this.keyData = (DERBitString)e.nextElement();
67 public AlgorithmIdentifier getAlgorithmId()
73 * for when the public key is an encoded object - if the bitstring
74 * can't be decoded this routine throws an IOException.
76 * @exception IOException - if the bit string doesn't represent a DER
79 public DERObject getPublicKey()
82 ByteArrayInputStream bIn = new ByteArrayInputStream(keyData.getBytes());
83 DERInputStream dIn = new DERInputStream(bIn);
85 return dIn.readObject();
89 * for when the public key is raw bits...
91 public DERBitString getPublicKeyData()
98 * SubjectPublicKeyInfo ::= SEQUENCE {
99 * algorithm AlgorithmIdentifier,
100 * publicKey BIT STRING }
103 public DERObject getDERObject()
105 DERConstructedSequence seq = new DERConstructedSequence();
107 seq.addObject(algId);
108 seq.addObject(keyData);