1 19 20 package com.maverick.crypto.asn1.x509; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.IOException ; 24 import java.util.Enumeration ; 25 26 import com.maverick.crypto.asn1.ASN1EncodableVector; 27 import com.maverick.crypto.asn1.ASN1Sequence; 28 import com.maverick.crypto.asn1.ASN1TaggedObject; 29 import com.maverick.crypto.asn1.DERBitString; 30 import com.maverick.crypto.asn1.DEREncodable; 31 import com.maverick.crypto.asn1.DERInputStream; 32 import com.maverick.crypto.asn1.DERObject; 33 import com.maverick.crypto.asn1.DERSequence; 34 35 41 public class SubjectPublicKeyInfo 42 implements DEREncodable 43 { 44 private AlgorithmIdentifier algId; 45 private DERBitString keyData; 46 47 public static SubjectPublicKeyInfo getInstance( 48 ASN1TaggedObject obj, 49 boolean explicit) 50 { 51 return getInstance(ASN1Sequence.getInstance(obj, explicit)); 52 } 53 54 public static SubjectPublicKeyInfo getInstance( 55 Object obj) 56 { 57 if (obj instanceof SubjectPublicKeyInfo) 58 { 59 return (SubjectPublicKeyInfo)obj; 60 } 61 else if (obj instanceof ASN1Sequence) 62 { 63 return new SubjectPublicKeyInfo((ASN1Sequence)obj); 64 } 65 66 throw new IllegalArgumentException ("unknown object in factory"); 67 } 68 69 public SubjectPublicKeyInfo( 70 AlgorithmIdentifier algId, 71 DEREncodable publicKey) 72 { 73 this.keyData = new DERBitString(publicKey); 74 this.algId = algId; 75 } 76 77 public SubjectPublicKeyInfo( 78 AlgorithmIdentifier algId, 79 byte[] publicKey) 80 { 81 this.keyData = new DERBitString(publicKey); 82 this.algId = algId; 83 } 84 85 public SubjectPublicKeyInfo( 86 ASN1Sequence seq) 87 { 88 Enumeration e = seq.getObjects(); 89 90 this.algId = AlgorithmIdentifier.getInstance(e.nextElement()); 91 this.keyData = (DERBitString)e.nextElement(); 92 } 93 94 public AlgorithmIdentifier getAlgorithmId() 95 { 96 return algId; 97 } 98 99 106 public DERObject getPublicKey() 107 throws IOException 108 { 109 ByteArrayInputStream bIn = new ByteArrayInputStream (keyData.getBytes()); 110 DERInputStream dIn = new DERInputStream(bIn); 111 112 return dIn.readObject(); 113 } 114 115 118 public DERBitString getPublicKeyData() 119 { 120 return keyData; 121 } 122 123 131 public DERObject getDERObject() 132 { 133 ASN1EncodableVector v = new ASN1EncodableVector(); 134 135 v.add(algId); 136 v.add(keyData); 137 138 return new DERSequence(v); 139 } 140 } 141 | Popular Tags |