1 package org.bouncycastle.asn1.x509;
4 import java.util.Enumeration;
5 import java.math.BigInteger;
7 import org.bouncycastle.asn1.*;
8 import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
11 * The object that contains the public key stored in a certficate.
13 * The getEncoded() method in the public keys in the JCE produces a DER
14 * encoded one of these.
16 public class SubjectPublicKeyInfo
17 implements DEREncodable
19 private AlgorithmIdentifier algId;
20 private DERBitString keyData;
22 public static SubjectPublicKeyInfo getInstance(
26 return getInstance(ASN1Sequence.getInstance(obj, explicit));
29 public static SubjectPublicKeyInfo getInstance(
32 if (obj instanceof SubjectPublicKeyInfo)
34 return (SubjectPublicKeyInfo)obj;
36 else if (obj instanceof ASN1Sequence)
38 return new SubjectPublicKeyInfo((ASN1Sequence)obj);
41 throw new IllegalArgumentException("unknown object in factory");
44 public SubjectPublicKeyInfo(
45 AlgorithmIdentifier algId,
46 DEREncodable publicKey)
48 this.keyData = new DERBitString(publicKey);
52 public SubjectPublicKeyInfo(
53 AlgorithmIdentifier algId,
56 this.keyData = new DERBitString(publicKey);
60 public SubjectPublicKeyInfo(
63 Enumeration e = seq.getObjects();
65 this.algId = AlgorithmIdentifier.getInstance(e.nextElement());
66 this.keyData = (DERBitString)e.nextElement();
69 public AlgorithmIdentifier getAlgorithmId()
75 * for when the public key is an encoded object - if the bitstring
76 * can't be decoded this routine throws an IOException.
78 * @exception IOException - if the bit string doesn't represent a DER
81 public DERObject getPublicKey()
84 ByteArrayInputStream bIn = new ByteArrayInputStream(keyData.getBytes());
85 DERInputStream dIn = new DERInputStream(bIn);
87 return dIn.readObject();
91 * for when the public key is raw bits...
93 public DERBitString getPublicKeyData()
100 * SubjectPublicKeyInfo ::= SEQUENCE {
101 * algorithm AlgorithmIdentifier,
102 * publicKey BIT STRING }
105 public DERObject getDERObject()
107 DERConstructedSequence seq = new DERConstructedSequence();
109 seq.addObject(algId);
110 seq.addObject(keyData);