JCEDHPrivateKey.java revision e6bf3e8dfa2804891a82075cb469b736321b4827
1package org.bouncycastle.jce.provider; 2 3import java.io.IOException; 4import java.io.ObjectInputStream; 5import java.io.ObjectOutputStream; 6import java.math.BigInteger; 7import java.util.Enumeration; 8 9import javax.crypto.interfaces.DHPrivateKey; 10import javax.crypto.spec.DHParameterSpec; 11import javax.crypto.spec.DHPrivateKeySpec; 12 13import org.bouncycastle.asn1.ASN1Encodable; 14import org.bouncycastle.asn1.ASN1Encoding; 15import org.bouncycastle.asn1.ASN1ObjectIdentifier; 16import org.bouncycastle.asn1.ASN1Sequence; 17import org.bouncycastle.asn1.DERInteger; 18import org.bouncycastle.asn1.DERObjectIdentifier; 19import org.bouncycastle.asn1.pkcs.DHParameter; 20import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers; 21import org.bouncycastle.asn1.pkcs.PrivateKeyInfo; 22import org.bouncycastle.asn1.x509.AlgorithmIdentifier; 23import org.bouncycastle.asn1.x9.DHDomainParameters; 24import org.bouncycastle.asn1.x9.X9ObjectIdentifiers; 25import org.bouncycastle.crypto.params.DHPrivateKeyParameters; 26import org.bouncycastle.jcajce.provider.asymmetric.util.PKCS12BagAttributeCarrierImpl; 27import org.bouncycastle.jce.interfaces.PKCS12BagAttributeCarrier; 28 29public class JCEDHPrivateKey 30 implements DHPrivateKey, PKCS12BagAttributeCarrier 31{ 32 static final long serialVersionUID = 311058815616901812L; 33 34 BigInteger x; 35 36 private DHParameterSpec dhSpec; 37 private PrivateKeyInfo info; 38 39 private PKCS12BagAttributeCarrier attrCarrier = new PKCS12BagAttributeCarrierImpl(); 40 41 protected JCEDHPrivateKey() 42 { 43 } 44 45 JCEDHPrivateKey( 46 DHPrivateKey key) 47 { 48 this.x = key.getX(); 49 this.dhSpec = key.getParams(); 50 } 51 52 JCEDHPrivateKey( 53 DHPrivateKeySpec spec) 54 { 55 this.x = spec.getX(); 56 this.dhSpec = new DHParameterSpec(spec.getP(), spec.getG()); 57 } 58 59 JCEDHPrivateKey( 60 PrivateKeyInfo info) 61 throws IOException 62 { 63 ASN1Sequence seq = ASN1Sequence.getInstance(info.getAlgorithmId().getParameters()); 64 DERInteger derX = DERInteger.getInstance(info.parsePrivateKey()); 65 DERObjectIdentifier id = info.getAlgorithmId().getAlgorithm(); 66 67 this.info = info; 68 this.x = derX.getValue(); 69 70 if (id.equals(PKCSObjectIdentifiers.dhKeyAgreement)) 71 { 72 DHParameter params = DHParameter.getInstance(seq); 73 74 if (params.getL() != null) 75 { 76 this.dhSpec = new DHParameterSpec(params.getP(), params.getG(), params.getL().intValue()); 77 } 78 else 79 { 80 this.dhSpec = new DHParameterSpec(params.getP(), params.getG()); 81 } 82 } 83 else if (id.equals(X9ObjectIdentifiers.dhpublicnumber)) 84 { 85 DHDomainParameters params = DHDomainParameters.getInstance(seq); 86 87 this.dhSpec = new DHParameterSpec(params.getP().getValue(), params.getG().getValue()); 88 } 89 else 90 { 91 throw new IllegalArgumentException("unknown algorithm type: " + id); 92 } 93 } 94 95 JCEDHPrivateKey( 96 DHPrivateKeyParameters params) 97 { 98 this.x = params.getX(); 99 this.dhSpec = new DHParameterSpec(params.getParameters().getP(), params.getParameters().getG(), params.getParameters().getL()); 100 } 101 102 public String getAlgorithm() 103 { 104 return "DH"; 105 } 106 107 /** 108 * return the encoding format we produce in getEncoded(). 109 * 110 * @return the string "PKCS#8" 111 */ 112 public String getFormat() 113 { 114 return "PKCS#8"; 115 } 116 117 /** 118 * Return a PKCS8 representation of the key. The sequence returned 119 * represents a full PrivateKeyInfo object. 120 * 121 * @return a PKCS8 representation of the key. 122 */ 123 public byte[] getEncoded() 124 { 125 try 126 { 127 if (info != null) 128 { 129 return info.getEncoded(ASN1Encoding.DER); 130 } 131 132 PrivateKeyInfo info = new PrivateKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.dhKeyAgreement, new DHParameter(dhSpec.getP(), dhSpec.getG(), dhSpec.getL())), new DERInteger(getX())); 133 134 return info.getEncoded(ASN1Encoding.DER); 135 } 136 catch (IOException e) 137 { 138 return null; 139 } 140 } 141 142 public DHParameterSpec getParams() 143 { 144 return dhSpec; 145 } 146 147 public BigInteger getX() 148 { 149 return x; 150 } 151 152 private void readObject( 153 ObjectInputStream in) 154 throws IOException, ClassNotFoundException 155 { 156 x = (BigInteger)in.readObject(); 157 158 this.dhSpec = new DHParameterSpec((BigInteger)in.readObject(), (BigInteger)in.readObject(), in.readInt()); 159 } 160 161 private void writeObject( 162 ObjectOutputStream out) 163 throws IOException 164 { 165 out.writeObject(this.getX()); 166 out.writeObject(dhSpec.getP()); 167 out.writeObject(dhSpec.getG()); 168 out.writeInt(dhSpec.getL()); 169 } 170 171 public void setBagAttribute( 172 ASN1ObjectIdentifier oid, 173 ASN1Encodable attribute) 174 { 175 attrCarrier.setBagAttribute(oid, attribute); 176 } 177 178 public ASN1Encodable getBagAttribute( 179 DERObjectIdentifier oid) 180 { 181 return attrCarrier.getBagAttribute(oid); 182 } 183 184 public Enumeration getBagAttributeKeys() 185 { 186 return attrCarrier.getBagAttributeKeys(); 187 } 188} 189