001: package org.bouncycastle.jce.provider;
002:
003: import org.bouncycastle.asn1.ASN1Sequence;
004: import org.bouncycastle.asn1.DERInteger;
005: import org.bouncycastle.asn1.pkcs.DHParameter;
006: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
007: import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
008: import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
009: import org.bouncycastle.crypto.params.DHPublicKeyParameters;
010:
011: import javax.crypto.interfaces.DHPublicKey;
012: import javax.crypto.spec.DHParameterSpec;
013: import javax.crypto.spec.DHPublicKeySpec;
014: import java.io.IOException;
015: import java.io.ObjectInputStream;
016: import java.io.ObjectOutputStream;
017: import java.math.BigInteger;
018:
019: public class JCEDHPublicKey implements DHPublicKey {
020: static final long serialVersionUID = -216691575254424324L;
021:
022: private BigInteger y;
023: private DHParameterSpec dhSpec;
024:
025: JCEDHPublicKey(DHPublicKeySpec spec) {
026: this .y = spec.getY();
027: this .dhSpec = new DHParameterSpec(spec.getP(), spec.getG());
028: }
029:
030: JCEDHPublicKey(DHPublicKey key) {
031: this .y = key.getY();
032: this .dhSpec = key.getParams();
033: }
034:
035: JCEDHPublicKey(DHPublicKeyParameters params) {
036: this .y = params.getY();
037: this .dhSpec = new DHParameterSpec(
038: params.getParameters().getP(), params.getParameters()
039: .getG(), params.getParameters().getL());
040: }
041:
042: JCEDHPublicKey(BigInteger y, DHParameterSpec dhSpec) {
043: this .y = y;
044: this .dhSpec = dhSpec;
045: }
046:
047: JCEDHPublicKey(SubjectPublicKeyInfo info) {
048: DHParameter params = new DHParameter((ASN1Sequence) info
049: .getAlgorithmId().getParameters());
050: DERInteger derY = null;
051:
052: try {
053: derY = (DERInteger) info.getPublicKey();
054: } catch (IOException e) {
055: throw new IllegalArgumentException(
056: "invalid info structure in DH public key");
057: }
058:
059: this .y = derY.getValue();
060: if (params.getL() != null) {
061: this .dhSpec = new DHParameterSpec(params.getP(), params
062: .getG(), params.getL().intValue());
063: } else {
064: this .dhSpec = new DHParameterSpec(params.getP(), params
065: .getG());
066: }
067: }
068:
069: public String getAlgorithm() {
070: return "DH";
071: }
072:
073: public String getFormat() {
074: return "X.509";
075: }
076:
077: public byte[] getEncoded() {
078: SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(
079: new AlgorithmIdentifier(
080: X9ObjectIdentifiers.dhpublicnumber,
081: new DHParameter(dhSpec.getP(), dhSpec.getG(),
082: dhSpec.getL()).getDERObject()),
083: new DERInteger(y));
084:
085: return info.getDEREncoded();
086: }
087:
088: public DHParameterSpec getParams() {
089: return dhSpec;
090: }
091:
092: public BigInteger getY() {
093: return y;
094: }
095:
096: private void readObject(ObjectInputStream in) throws IOException,
097: ClassNotFoundException {
098: this .y = (BigInteger) in.readObject();
099: this .dhSpec = new DHParameterSpec((BigInteger) in.readObject(),
100: (BigInteger) in.readObject(), in.readInt());
101: }
102:
103: private void writeObject(ObjectOutputStream out) throws IOException {
104: out.writeObject(this.getY());
105: out.writeObject(dhSpec.getP());
106: out.writeObject(dhSpec.getG());
107: out.writeInt(dhSpec.getL());
108: }
109: }
|