1 17 18 package org.apache.geronimo.util.asn1.pkcs; 19 20 import java.io.ByteArrayInputStream ; 21 import java.io.IOException ; 22 import java.math.BigInteger ; 23 import java.util.Enumeration ; 24 25 import org.apache.geronimo.util.asn1.ASN1Encodable; 26 import org.apache.geronimo.util.asn1.ASN1EncodableVector; 27 import org.apache.geronimo.util.asn1.ASN1InputStream; 28 import org.apache.geronimo.util.asn1.ASN1OctetString; 29 import org.apache.geronimo.util.asn1.ASN1Sequence; 30 import org.apache.geronimo.util.asn1.ASN1Set; 31 import org.apache.geronimo.util.asn1.ASN1TaggedObject; 32 import org.apache.geronimo.util.asn1.DERInteger; 33 import org.apache.geronimo.util.asn1.DERObject; 34 import org.apache.geronimo.util.asn1.DEROctetString; 35 import org.apache.geronimo.util.asn1.DERSequence; 36 import org.apache.geronimo.util.asn1.DERTaggedObject; 37 import org.apache.geronimo.util.asn1.x509.AlgorithmIdentifier; 38 39 public class PrivateKeyInfo 40 extends ASN1Encodable 41 { 42 private DERObject privKey; 43 private AlgorithmIdentifier algId; 44 private ASN1Set attributes; 45 46 public static PrivateKeyInfo getInstance( 47 ASN1TaggedObject obj, 48 boolean explicit) 49 { 50 return getInstance(ASN1Sequence.getInstance(obj, explicit)); 51 } 52 53 public static PrivateKeyInfo getInstance( 54 Object obj) 55 { 56 if (obj instanceof PrivateKeyInfo) 57 { 58 return (PrivateKeyInfo)obj; 59 } 60 else if (obj instanceof ASN1Sequence) 61 { 62 return new PrivateKeyInfo((ASN1Sequence)obj); 63 } 64 65 throw new IllegalArgumentException ("unknown object in factory"); 66 } 67 68 public PrivateKeyInfo( 69 AlgorithmIdentifier algId, 70 DERObject privateKey) 71 { 72 this.privKey = privateKey; 73 this.algId = algId; 74 } 75 76 public PrivateKeyInfo( 77 ASN1Sequence seq) 78 { 79 Enumeration e = seq.getObjects(); 80 81 BigInteger version = ((DERInteger)e.nextElement()).getValue(); 82 if (version.intValue() != 0) 83 { 84 throw new IllegalArgumentException ("wrong version for private key info"); 85 } 86 87 algId = new AlgorithmIdentifier((ASN1Sequence)e.nextElement()); 88 89 try 90 { 91 ByteArrayInputStream bIn = new ByteArrayInputStream (((ASN1OctetString)e.nextElement()).getOctets()); 92 ASN1InputStream aIn = new ASN1InputStream(bIn); 93 94 privKey = aIn.readObject(); 95 } 96 catch (IOException ex) 97 { 98 throw new IllegalArgumentException ("Error recoverying private key from sequence"); 99 } 100 101 if (e.hasMoreElements()) 102 { 103 attributes = ASN1Set.getInstance((ASN1TaggedObject)e.nextElement(), false); 104 } 105 } 106 107 public AlgorithmIdentifier getAlgorithmId() 108 { 109 return algId; 110 } 111 112 public DERObject getPrivateKey() 113 { 114 return privKey; 115 } 116 117 public ASN1Set getAttributes() 118 { 119 return attributes; 120 } 121 122 139 public DERObject toASN1Object() 140 { 141 ASN1EncodableVector v = new ASN1EncodableVector(); 142 143 v.add(new DERInteger(0)); 144 v.add(algId); 145 v.add(new DEROctetString(privKey)); 146 147 if (attributes != null) 148 { 149 v.add(new DERTaggedObject(false, 0, attributes)); 150 } 151 152 return new DERSequence(v); 153 } 154 } 155 | Popular Tags |