1 17 18 package org.apache.geronimo.util.jce.provider; 19 20 import java.io.ByteArrayOutputStream ; 21 import java.io.IOException ; 22 import java.io.ObjectInputStream ; 23 import java.io.ObjectOutputStream ; 24 import java.math.BigInteger ; 25 import java.util.Enumeration ; 26 import java.util.Hashtable ; 27 import java.util.Vector ; 28 29 import javax.crypto.interfaces.DHPrivateKey; 30 import javax.crypto.spec.DHParameterSpec; 31 import javax.crypto.spec.DHPrivateKeySpec; 32 33 import org.apache.geronimo.util.asn1.ASN1Sequence; 34 import org.apache.geronimo.util.asn1.DEREncodable; 35 import org.apache.geronimo.util.asn1.DERInteger; 36 import org.apache.geronimo.util.asn1.DERObjectIdentifier; 37 import org.apache.geronimo.util.asn1.DEROutputStream; 38 import org.apache.geronimo.util.asn1.pkcs.DHParameter; 39 import org.apache.geronimo.util.asn1.pkcs.PKCSObjectIdentifiers; 40 import org.apache.geronimo.util.asn1.pkcs.PrivateKeyInfo; 41 import org.apache.geronimo.util.asn1.x509.AlgorithmIdentifier; 42 import org.apache.geronimo.util.crypto.params.DHPrivateKeyParameters; 43 import org.apache.geronimo.util.jce.interfaces.PKCS12BagAttributeCarrier; 44 45 public class JCEDHPrivateKey 46 implements DHPrivateKey, PKCS12BagAttributeCarrier 47 { 48 BigInteger x; 49 50 DHParameterSpec dhSpec; 51 52 private Hashtable pkcs12Attributes = new Hashtable (); 53 private Vector pkcs12Ordering = new Vector (); 54 55 protected JCEDHPrivateKey() 56 { 57 } 58 59 JCEDHPrivateKey( 60 DHPrivateKey key) 61 { 62 this.x = key.getX(); 63 this.dhSpec = key.getParams(); 64 } 65 66 JCEDHPrivateKey( 67 DHPrivateKeySpec spec) 68 { 69 this.x = spec.getX(); 70 this.dhSpec = new DHParameterSpec(spec.getP(), spec.getG()); 71 } 72 73 JCEDHPrivateKey( 74 PrivateKeyInfo info) 75 { 76 DHParameter params = new DHParameter((ASN1Sequence)info.getAlgorithmId().getParameters()); 77 DERInteger derX = (DERInteger)info.getPrivateKey(); 78 79 this.x = derX.getValue(); 80 if (params.getL() != null) 81 { 82 this.dhSpec = new DHParameterSpec(params.getP(), params.getG(), params.getL().intValue()); 83 } 84 else 85 { 86 this.dhSpec = new DHParameterSpec(params.getP(), params.getG()); 87 } 88 } 89 90 JCEDHPrivateKey( 91 DHPrivateKeyParameters params) 92 { 93 this.x = params.getX(); 94 this.dhSpec = new DHParameterSpec(params.getParameters().getP(), params.getParameters().getG()); 95 } 96 97 public String getAlgorithm() 98 { 99 return "DH"; 100 } 101 102 107 public String getFormat() 108 { 109 return "PKCS#8"; 110 } 111 112 118 public byte[] getEncoded() 119 { 120 ByteArrayOutputStream bOut = new ByteArrayOutputStream (); 121 DEROutputStream dOut = new DEROutputStream(bOut); 122 PrivateKeyInfo info = new PrivateKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.dhKeyAgreement, new DHParameter(dhSpec.getP(), dhSpec.getG(), dhSpec.getL()).getDERObject()), new DERInteger(getX())); 123 124 try 125 { 126 dOut.writeObject(info); 127 dOut.close(); 128 } 129 catch (IOException e) 130 { 131 throw new RuntimeException ("Error encoding DH private key"); 132 } 133 134 return bOut.toByteArray(); 135 } 136 137 public DHParameterSpec getParams() 138 { 139 return dhSpec; 140 } 141 142 public BigInteger getX() 143 { 144 return x; 145 } 146 147 private void readObject( 148 ObjectInputStream in) 149 throws IOException , ClassNotFoundException 150 { 151 x = (BigInteger )in.readObject(); 152 153 this.dhSpec = new DHParameterSpec((BigInteger )in.readObject(), (BigInteger )in.readObject(), in.readInt()); 154 } 155 156 private void writeObject( 157 ObjectOutputStream out) 158 throws IOException 159 { 160 out.writeObject(this.getX()); 161 out.writeObject(dhSpec.getP()); 162 out.writeObject(dhSpec.getG()); 163 out.writeInt(dhSpec.getL()); 164 } 165 166 public void setBagAttribute( 167 DERObjectIdentifier oid, 168 DEREncodable attribute) 169 { 170 pkcs12Attributes.put(oid, attribute); 171 pkcs12Ordering.addElement(oid); 172 } 173 174 public DEREncodable getBagAttribute( 175 DERObjectIdentifier oid) 176 { 177 return (DEREncodable)pkcs12Attributes.get(oid); 178 } 179 180 public Enumeration getBagAttributeKeys() 181 { 182 return pkcs12Ordering.elements(); 183 } 184 } 185 | Popular Tags |