001: package org.bouncycastle.jce.provider;
002:
003: import org.bouncycastle.asn1.ASN1Sequence;
004: import org.bouncycastle.asn1.DEREncodable;
005: import org.bouncycastle.asn1.DERInteger;
006: import org.bouncycastle.asn1.DERObjectIdentifier;
007: import org.bouncycastle.asn1.pkcs.DHParameter;
008: import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
009: import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
010: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
011: import org.bouncycastle.crypto.params.DHPrivateKeyParameters;
012: import org.bouncycastle.jce.interfaces.PKCS12BagAttributeCarrier;
013:
014: import javax.crypto.interfaces.DHPrivateKey;
015: import javax.crypto.spec.DHParameterSpec;
016: import javax.crypto.spec.DHPrivateKeySpec;
017: import java.io.IOException;
018: import java.io.ObjectInputStream;
019: import java.io.ObjectOutputStream;
020: import java.math.BigInteger;
021: import java.util.Enumeration;
022:
023: public class JCEDHPrivateKey implements DHPrivateKey,
024: PKCS12BagAttributeCarrier {
025: static final long serialVersionUID = 311058815616901812L;
026:
027: BigInteger x;
028:
029: DHParameterSpec dhSpec;
030:
031: private PKCS12BagAttributeCarrier attrCarrier = new PKCS12BagAttributeCarrierImpl();
032:
033: protected JCEDHPrivateKey() {
034: }
035:
036: JCEDHPrivateKey(DHPrivateKey key) {
037: this .x = key.getX();
038: this .dhSpec = key.getParams();
039: }
040:
041: JCEDHPrivateKey(DHPrivateKeySpec spec) {
042: this .x = spec.getX();
043: this .dhSpec = new DHParameterSpec(spec.getP(), spec.getG());
044: }
045:
046: JCEDHPrivateKey(PrivateKeyInfo info) {
047: DHParameter params = new DHParameter((ASN1Sequence) info
048: .getAlgorithmId().getParameters());
049: DERInteger derX = (DERInteger) info.getPrivateKey();
050:
051: this .x = derX.getValue();
052: if (params.getL() != null) {
053: this .dhSpec = new DHParameterSpec(params.getP(), params
054: .getG(), params.getL().intValue());
055: } else {
056: this .dhSpec = new DHParameterSpec(params.getP(), params
057: .getG());
058: }
059: }
060:
061: JCEDHPrivateKey(DHPrivateKeyParameters params) {
062: this .x = params.getX();
063: this .dhSpec = new DHParameterSpec(
064: params.getParameters().getP(), params.getParameters()
065: .getG(), params.getParameters().getL());
066: }
067:
068: public String getAlgorithm() {
069: return "DH";
070: }
071:
072: /**
073: * return the encoding format we produce in getEncoded().
074: *
075: * @return the string "PKCS#8"
076: */
077: public String getFormat() {
078: return "PKCS#8";
079: }
080:
081: /**
082: * Return a PKCS8 representation of the key. The sequence returned
083: * represents a full PrivateKeyInfo object.
084: *
085: * @return a PKCS8 representation of the key.
086: */
087: public byte[] getEncoded() {
088: PrivateKeyInfo info = new PrivateKeyInfo(
089: new AlgorithmIdentifier(
090: PKCSObjectIdentifiers.dhKeyAgreement,
091: new DHParameter(dhSpec.getP(), dhSpec.getG(),
092: dhSpec.getL()).getDERObject()),
093: new DERInteger(getX()));
094:
095: return info.getDEREncoded();
096: }
097:
098: public DHParameterSpec getParams() {
099: return dhSpec;
100: }
101:
102: public BigInteger getX() {
103: return x;
104: }
105:
106: private void readObject(ObjectInputStream in) throws IOException,
107: ClassNotFoundException {
108: x = (BigInteger) in.readObject();
109:
110: this .dhSpec = new DHParameterSpec((BigInteger) in.readObject(),
111: (BigInteger) in.readObject(), in.readInt());
112: }
113:
114: private void writeObject(ObjectOutputStream out) throws IOException {
115: out.writeObject(this .getX());
116: out.writeObject(dhSpec.getP());
117: out.writeObject(dhSpec.getG());
118: out.writeInt(dhSpec.getL());
119: }
120:
121: public void setBagAttribute(DERObjectIdentifier oid,
122: DEREncodable attribute) {
123: attrCarrier.setBagAttribute(oid, attribute);
124: }
125:
126: public DEREncodable getBagAttribute(DERObjectIdentifier oid) {
127: return attrCarrier.getBagAttribute(oid);
128: }
129:
130: public Enumeration getBagAttributeKeys() {
131: return attrCarrier.getBagAttributeKeys();
132: }
133: }
|