SubjectPublicKeyInfo.java revision e1142c149e244797ce73b0e7fad40816e447a817
1package org.bouncycastle.asn1.x509; 2 3import java.io.IOException; 4import java.util.Enumeration; 5 6import org.bouncycastle.asn1.ASN1Encodable; 7import org.bouncycastle.asn1.ASN1EncodableVector; 8import org.bouncycastle.asn1.ASN1InputStream; 9import org.bouncycastle.asn1.ASN1Object; 10import org.bouncycastle.asn1.ASN1Primitive; 11import org.bouncycastle.asn1.ASN1Sequence; 12import org.bouncycastle.asn1.ASN1TaggedObject; 13import org.bouncycastle.asn1.DERBitString; 14import org.bouncycastle.asn1.DERSequence; 15 16/** 17 * The object that contains the public key stored in a certficate. 18 * <p> 19 * The getEncoded() method in the public keys in the JCE produces a DER 20 * encoded one of these. 21 */ 22public class SubjectPublicKeyInfo 23 extends ASN1Object 24{ 25 private AlgorithmIdentifier algId; 26 private DERBitString keyData; 27 28 public static SubjectPublicKeyInfo getInstance( 29 ASN1TaggedObject obj, 30 boolean explicit) 31 { 32 return getInstance(ASN1Sequence.getInstance(obj, explicit)); 33 } 34 35 public static SubjectPublicKeyInfo getInstance( 36 Object obj) 37 { 38 if (obj instanceof SubjectPublicKeyInfo) 39 { 40 return (SubjectPublicKeyInfo)obj; 41 } 42 else if (obj != null) 43 { 44 return new SubjectPublicKeyInfo(ASN1Sequence.getInstance(obj)); 45 } 46 47 return null; 48 } 49 50 public SubjectPublicKeyInfo( 51 AlgorithmIdentifier algId, 52 ASN1Encodable publicKey) 53 throws IOException 54 { 55 this.keyData = new DERBitString(publicKey); 56 this.algId = algId; 57 } 58 59 public SubjectPublicKeyInfo( 60 AlgorithmIdentifier algId, 61 byte[] publicKey) 62 { 63 this.keyData = new DERBitString(publicKey); 64 this.algId = algId; 65 } 66 67 public SubjectPublicKeyInfo( 68 ASN1Sequence seq) 69 { 70 if (seq.size() != 2) 71 { 72 throw new IllegalArgumentException("Bad sequence size: " 73 + seq.size()); 74 } 75 76 Enumeration e = seq.getObjects(); 77 78 this.algId = AlgorithmIdentifier.getInstance(e.nextElement()); 79 this.keyData = DERBitString.getInstance(e.nextElement()); 80 } 81 82 public AlgorithmIdentifier getAlgorithm() 83 { 84 return algId; 85 } 86 87 /** 88 * @deprecated use getAlgorithm() 89 * @return alg ID. 90 */ 91 public AlgorithmIdentifier getAlgorithmId() 92 { 93 return algId; 94 } 95 96 /** 97 * for when the public key is an encoded object - if the bitstring 98 * can't be decoded this routine throws an IOException. 99 * 100 * @exception IOException - if the bit string doesn't represent a DER 101 * encoded object. 102 * @return the public key as an ASN.1 primitive. 103 */ 104 public ASN1Primitive parsePublicKey() 105 throws IOException 106 { 107 ASN1InputStream aIn = new ASN1InputStream(keyData.getBytes()); 108 109 return aIn.readObject(); 110 } 111 112 /** 113 * for when the public key is an encoded object - if the bitstring 114 * can't be decoded this routine throws an IOException. 115 * 116 * @exception IOException - if the bit string doesn't represent a DER 117 * encoded object. 118 * @deprecated use parsePublicKey 119 * @return the public key as an ASN.1 primitive. 120 */ 121 public ASN1Primitive getPublicKey() 122 throws IOException 123 { 124 ASN1InputStream aIn = new ASN1InputStream(keyData.getBytes()); 125 126 return aIn.readObject(); 127 } 128 129 /** 130 * for when the public key is raw bits. 131 * 132 * @return the public key as the raw bit string... 133 */ 134 public DERBitString getPublicKeyData() 135 { 136 return keyData; 137 } 138 139 /** 140 * Produce an object suitable for an ASN1OutputStream. 141 * <pre> 142 * SubjectPublicKeyInfo ::= SEQUENCE { 143 * algorithm AlgorithmIdentifier, 144 * publicKey BIT STRING } 145 * </pre> 146 */ 147 public ASN1Primitive toASN1Primitive() 148 { 149 ASN1EncodableVector v = new ASN1EncodableVector(); 150 151 v.add(algId); 152 v.add(keyData); 153 154 return new DERSequence(v); 155 } 156} 157