1 17 18 package org.apache.geronimo.util.jce.provider; 19 20 import java.io.ByteArrayOutputStream ; 21 import java.io.IOException ; 22 import java.math.BigInteger ; 23 import java.security.interfaces.DSAParams ; 24 import java.security.interfaces.DSAPrivateKey ; 25 import java.security.spec.DSAParameterSpec ; 26 import java.security.spec.DSAPrivateKeySpec ; 27 import java.util.Enumeration ; 28 import java.util.Hashtable ; 29 import java.util.Vector ; 30 31 import org.apache.geronimo.util.asn1.ASN1Sequence; 32 import org.apache.geronimo.util.asn1.DEREncodable; 33 import org.apache.geronimo.util.asn1.DERInteger; 34 import org.apache.geronimo.util.asn1.DERObjectIdentifier; 35 import org.apache.geronimo.util.asn1.DEROutputStream; 36 import org.apache.geronimo.util.asn1.pkcs.PrivateKeyInfo; 37 import org.apache.geronimo.util.asn1.x509.AlgorithmIdentifier; 38 import org.apache.geronimo.util.asn1.x509.DSAParameter; 39 import org.apache.geronimo.util.asn1.x9.X9ObjectIdentifiers; 40 import org.apache.geronimo.util.crypto.params.DSAPrivateKeyParameters; 41 import org.apache.geronimo.util.jce.interfaces.PKCS12BagAttributeCarrier; 42 43 public class JDKDSAPrivateKey 44 implements DSAPrivateKey , PKCS12BagAttributeCarrier 45 { 46 BigInteger x; 47 DSAParams dsaSpec; 48 49 private Hashtable pkcs12Attributes = new Hashtable (); 50 private Vector pkcs12Ordering = new Vector (); 51 52 protected JDKDSAPrivateKey() 53 { 54 } 55 56 JDKDSAPrivateKey( 57 DSAPrivateKey key) 58 { 59 this.x = key.getX(); 60 this.dsaSpec = key.getParams(); 61 } 62 63 JDKDSAPrivateKey( 64 DSAPrivateKeySpec spec) 65 { 66 this.x = spec.getX(); 67 this.dsaSpec = new DSAParameterSpec (spec.getP(), spec.getQ(), spec.getG()); 68 } 69 70 JDKDSAPrivateKey( 71 PrivateKeyInfo info) 72 { 73 DSAParameter params = new DSAParameter((ASN1Sequence)info.getAlgorithmId().getParameters()); 74 DERInteger derX = (DERInteger)info.getPrivateKey(); 75 76 this.x = derX.getValue(); 77 this.dsaSpec = new DSAParameterSpec (params.getP(), params.getQ(), params.getG()); 78 } 79 80 JDKDSAPrivateKey( 81 DSAPrivateKeyParameters params) 82 { 83 this.x = params.getX(); 84 this.dsaSpec = new DSAParameterSpec (params.getParameters().getP(), params.getParameters().getQ(), params.getParameters().getG()); 85 } 86 87 public String getAlgorithm() 88 { 89 return "DSA"; 90 } 91 92 97 public String getFormat() 98 { 99 return "PKCS#8"; 100 } 101 102 108 public byte[] getEncoded() 109 { 110 ByteArrayOutputStream bOut = new ByteArrayOutputStream (); 111 DEROutputStream dOut = new DEROutputStream(bOut); 112 PrivateKeyInfo info = new PrivateKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa, new DSAParameter(dsaSpec.getP(), dsaSpec.getQ(), dsaSpec.getG()).getDERObject()), new DERInteger(getX())); 113 114 try 115 { 116 dOut.writeObject(info); 117 dOut.close(); 118 } 119 catch (IOException e) 120 { 121 throw new RuntimeException ("Error encoding DSA private key"); 122 } 123 124 return bOut.toByteArray(); 125 } 126 127 public DSAParams getParams() 128 { 129 return dsaSpec; 130 } 131 132 public BigInteger getX() 133 { 134 return x; 135 } 136 137 public void setBagAttribute( 138 DERObjectIdentifier oid, 139 DEREncodable attribute) 140 { 141 pkcs12Attributes.put(oid, attribute); 142 pkcs12Ordering.addElement(oid); 143 } 144 145 public DEREncodable getBagAttribute( 146 DERObjectIdentifier oid) 147 { 148 return (DEREncodable)pkcs12Attributes.get(oid); 149 } 150 151 public Enumeration getBagAttributeKeys() 152 { 153 return pkcs12Ordering.elements(); 154 } 155 } 156 | Popular Tags |