/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* A private key in PKCS#8 format for the Diffie-Hellman key agreement
* algorithm.
*
* @author Jan Luehe
*
*
* @see DHPublicKey
* @see java.security.KeyAgreement
*/
// only supported version of PKCS#8 PrivateKeyInfo
// the private key
private BigInteger x;
// the key bytes, without the algorithm information
private byte[] key;
// the encoded key
private byte[] encodedKey;
// the prime modulus
private BigInteger p;
// the base generator
private BigInteger g;
// the private-value length
private int l;
/**
* Make a DH private key out of a private value <code>x</code>, a prime
* modulus <code>p</code>, and a base generator <code>g</code>.
*
* @param x the private value
* @param p the prime modulus
* @param g the base generator
*
* @exception ProviderException if the key cannot be encoded
*/
throws InvalidKeyException {
this(x, p, g, 0);
}
/**
* Make a DH private key out of a private value <code>x</code>, a prime
* modulus <code>p</code>, a base generator <code>g</code>, and a
* private-value length <code>l</code>.
*
* @param x the private value
* @param p the prime modulus
* @param g the base generator
* @param l the private-value length
*
* @exception InvalidKeyException if the key cannot be encoded
*/
this.x = x;
this.p = p;
this.g = g;
this.l = l;
try {
this.x.toByteArray()).toByteArray();
this.encodedKey = getEncoded();
} catch (IOException e) {
throw new ProviderException("Cannot produce ASN.1 encoding", e);
}
}
/**
* Make a DH private key from its DER encoding (PKCS #8).
*
* @param encodedKey the encoded key
*
* @exception InvalidKeyException if the encoded key does not represent
* a Diffie-Hellman private key
*/
try {
throw new InvalidKeyException ("Key not a SEQUENCE");
}
//
// version
//
throw new IOException("version mismatch: (supported: " +
PKCS8_VERSION + ", parsed: " +
}
//
// privateKeyAlgorithm
//
throw new InvalidKeyException("AlgId is not a SEQUENCE");
}
throw new InvalidKeyException("Null OID");
}
throw new InvalidKeyException("Parameters missing");
}
// parse the parameters
throw new InvalidKeyException("Null parameters");
}
throw new InvalidKeyException("Parameters not a SEQUENCE");
}
// Private-value length is OPTIONAL
}
throw new InvalidKeyException("Extra parameter data");
}
//
// privateKey
//
parseKeyBits();
// ignore OPTIONAL attributes
} catch (NumberFormatException e) {
"Private-value length too big");
throw ike;
} catch (IOException e) {
"Error parsing key encoding: " + e.getMessage());
throw ike;
}
}
/**
* Returns the encoding format of this key: "PKCS#8"
*/
return "PKCS#8";
}
/**
* Returns the name of the algorithm associated with this key: "DH"
*/
return "DH";
}
/**
* Get the encoding of the key.
*/
public synchronized byte[] getEncoded() {
if (this.encodedKey == null) {
try {
//
// version
//
//
// privateKeyAlgorithm
//
// store OID
// encode parameters
params.putInteger(this.p);
params.putInteger(this.g);
if (this.l != 0)
params.putInteger(this.l);
// wrap parameters into SEQUENCE
params.toByteArray());
// store parameter SEQUENCE in algid
// wrap algid into SEQUENCE
// privateKey
// make it a SEQUENCE
} catch (IOException e) {
return null;
}
}
return (byte[])this.encodedKey.clone();
}
/**
* Returns the private value, <code>x</code>.
*
* @return the private value, <code>x</code>
*/
return this.x;
}
/**
* Returns the key parameters.
*
* @return the key parameters
*/
if (this.l != 0)
return new DHParameterSpec(this.p, this.g, this.l);
else
return new DHParameterSpec(this.p, this.g);
}
= new StringBuffer("SunJCE Diffie-Hellman Private Key:"
+ Debug.toHexString(this.x)
+ Debug.toHexString(this.p)
+ Debug.toHexString(this.g));
if (this.l != 0)
}
try {
this.x = in.getBigInteger();
} catch (IOException e) {
"Error parsing key encoding: " + e.getMessage());
throw ike;
}
}
/**
* Calculates a hash code value for the object.
* Objects that are equal will also have the same hashcode.
*/
public int hashCode() {
int retval = 0;
byte[] enc = getEncoded();
}
return(retval);
}
if (this == obj)
return true;
if (!(obj instanceof PrivateKey))
return false;
byte[] thisEncoded = this.getEncoded();
}
/**
* Replace the DH private key to be serialized.
*
* @return the standard KeyRep object to be serialized
*
* @throws java.io.ObjectStreamException if a new object representing
* this DH private key could not be created
*/
getAlgorithm(),
getFormat(),
getEncoded());
}
}