001: package org.bouncycastle.asn1.x509;
002:
003: import java.io.IOException;
004: import java.util.Enumeration;
005:
006: import org.bouncycastle.asn1.ASN1Encodable;
007: import org.bouncycastle.asn1.ASN1EncodableVector;
008: import org.bouncycastle.asn1.ASN1InputStream;
009: import org.bouncycastle.asn1.ASN1Sequence;
010: import org.bouncycastle.asn1.ASN1TaggedObject;
011: import org.bouncycastle.asn1.DERBitString;
012: import org.bouncycastle.asn1.DEREncodable;
013: import org.bouncycastle.asn1.DERObject;
014: import org.bouncycastle.asn1.DERSequence;
015:
016: /**
017: * The object that contains the public key stored in a certficate.
018: * <p>
019: * The getEncoded() method in the public keys in the JCE produces a DER
020: * encoded one of these.
021: */
022: public class SubjectPublicKeyInfo extends ASN1Encodable {
023: private AlgorithmIdentifier algId;
024: private DERBitString keyData;
025:
026: public static SubjectPublicKeyInfo getInstance(
027: ASN1TaggedObject obj, boolean explicit) {
028: return getInstance(ASN1Sequence.getInstance(obj, explicit));
029: }
030:
031: public static SubjectPublicKeyInfo getInstance(Object obj) {
032: if (obj instanceof SubjectPublicKeyInfo) {
033: return (SubjectPublicKeyInfo) obj;
034: } else if (obj instanceof ASN1Sequence) {
035: return new SubjectPublicKeyInfo((ASN1Sequence) obj);
036: }
037:
038: throw new IllegalArgumentException("unknown object in factory");
039: }
040:
041: public SubjectPublicKeyInfo(AlgorithmIdentifier algId,
042: DEREncodable publicKey) {
043: this .keyData = new DERBitString(publicKey);
044: this .algId = algId;
045: }
046:
047: public SubjectPublicKeyInfo(AlgorithmIdentifier algId,
048: byte[] publicKey) {
049: this .keyData = new DERBitString(publicKey);
050: this .algId = algId;
051: }
052:
053: public SubjectPublicKeyInfo(ASN1Sequence seq) {
054: if (seq.size() != 2) {
055: throw new IllegalArgumentException("Bad sequence size: "
056: + seq.size());
057: }
058:
059: Enumeration e = seq.getObjects();
060:
061: this .algId = AlgorithmIdentifier.getInstance(e.nextElement());
062: this .keyData = DERBitString.getInstance(e.nextElement());
063: }
064:
065: public AlgorithmIdentifier getAlgorithmId() {
066: return algId;
067: }
068:
069: /**
070: * for when the public key is an encoded object - if the bitstring
071: * can't be decoded this routine throws an IOException.
072: *
073: * @exception IOException - if the bit string doesn't represent a DER
074: * encoded object.
075: */
076: public DERObject getPublicKey() throws IOException {
077: ASN1InputStream aIn = new ASN1InputStream(keyData.getBytes());
078:
079: return aIn.readObject();
080: }
081:
082: /**
083: * for when the public key is raw bits...
084: */
085: public DERBitString getPublicKeyData() {
086: return keyData;
087: }
088:
089: /**
090: * Produce an object suitable for an ASN1OutputStream.
091: * <pre>
092: * SubjectPublicKeyInfo ::= SEQUENCE {
093: * algorithm AlgorithmIdentifier,
094: * publicKey BIT STRING }
095: * </pre>
096: */
097: public DERObject toASN1Object() {
098: ASN1EncodableVector v = new ASN1EncodableVector();
099:
100: v.add(algId);
101: v.add(keyData);
102:
103: return new DERSequence(v);
104: }
105: }
|