120 lines
5.3 KiB
Java
120 lines
5.3 KiB
Java
package org.bouncycastle.jce.provider;
|
|
|
|
import java.io.IOException;
|
|
import java.io.ObjectInputStream;
|
|
import java.io.ObjectOutputStream;
|
|
import java.math.BigInteger;
|
|
import javax.crypto.interfaces.DHPublicKey;
|
|
import javax.crypto.spec.DHParameterSpec;
|
|
import javax.crypto.spec.DHPublicKeySpec;
|
|
import org.bouncycastle.asn1.ASN1Integer;
|
|
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
|
|
import org.bouncycastle.asn1.ASN1Sequence;
|
|
import org.bouncycastle.asn1.pkcs.DHParameter;
|
|
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
|
|
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
|
|
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
|
|
import org.bouncycastle.asn1.x9.DHDomainParameters;
|
|
import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
|
|
import org.bouncycastle.crypto.params.DHPublicKeyParameters;
|
|
import org.bouncycastle.jcajce.provider.asymmetric.util.KeyUtil;
|
|
|
|
/* loaded from: classes6.dex */
|
|
public class JCEDHPublicKey implements DHPublicKey {
|
|
static final long serialVersionUID = -216691575254424324L;
|
|
private DHParameterSpec dhSpec;
|
|
private SubjectPublicKeyInfo info;
|
|
private BigInteger y;
|
|
|
|
@Override // javax.crypto.interfaces.DHPublicKey
|
|
public BigInteger getY() {
|
|
return this.y;
|
|
}
|
|
|
|
@Override // javax.crypto.interfaces.DHKey
|
|
public DHParameterSpec getParams() {
|
|
return this.dhSpec;
|
|
}
|
|
|
|
@Override // java.security.Key
|
|
public String getFormat() {
|
|
return "X.509";
|
|
}
|
|
|
|
@Override // java.security.Key
|
|
public byte[] getEncoded() {
|
|
SubjectPublicKeyInfo subjectPublicKeyInfo = this.info;
|
|
return subjectPublicKeyInfo != null ? KeyUtil.getEncodedSubjectPublicKeyInfo(subjectPublicKeyInfo) : KeyUtil.getEncodedSubjectPublicKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.dhKeyAgreement, new DHParameter(this.dhSpec.getP(), this.dhSpec.getG(), this.dhSpec.getL())), new ASN1Integer(this.y));
|
|
}
|
|
|
|
@Override // java.security.Key
|
|
public String getAlgorithm() {
|
|
return "DH";
|
|
}
|
|
|
|
private void writeObject(ObjectOutputStream objectOutputStream) throws IOException {
|
|
objectOutputStream.writeObject(getY());
|
|
objectOutputStream.writeObject(this.dhSpec.getP());
|
|
objectOutputStream.writeObject(this.dhSpec.getG());
|
|
objectOutputStream.writeInt(this.dhSpec.getL());
|
|
}
|
|
|
|
private void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
|
|
this.y = (BigInteger) objectInputStream.readObject();
|
|
this.dhSpec = new DHParameterSpec((BigInteger) objectInputStream.readObject(), (BigInteger) objectInputStream.readObject(), objectInputStream.readInt());
|
|
}
|
|
|
|
private boolean isPKCSParam(ASN1Sequence aSN1Sequence) {
|
|
if (aSN1Sequence.size() == 2) {
|
|
return true;
|
|
}
|
|
if (aSN1Sequence.size() > 3) {
|
|
return false;
|
|
}
|
|
return ASN1Integer.getInstance(aSN1Sequence.getObjectAt(2)).getValue().compareTo(BigInteger.valueOf((long) ASN1Integer.getInstance(aSN1Sequence.getObjectAt(0)).getValue().bitLength())) <= 0;
|
|
}
|
|
|
|
JCEDHPublicKey(DHPublicKeyParameters dHPublicKeyParameters) {
|
|
this.y = dHPublicKeyParameters.getY();
|
|
this.dhSpec = new DHParameterSpec(dHPublicKeyParameters.getParameters().getP(), dHPublicKeyParameters.getParameters().getG(), dHPublicKeyParameters.getParameters().getL());
|
|
}
|
|
|
|
JCEDHPublicKey(SubjectPublicKeyInfo subjectPublicKeyInfo) {
|
|
DHParameterSpec dHParameterSpec;
|
|
this.info = subjectPublicKeyInfo;
|
|
try {
|
|
this.y = ((ASN1Integer) subjectPublicKeyInfo.parsePublicKey()).getValue();
|
|
ASN1Sequence aSN1Sequence = ASN1Sequence.getInstance(subjectPublicKeyInfo.getAlgorithmId().getParameters());
|
|
ASN1ObjectIdentifier algorithm = subjectPublicKeyInfo.getAlgorithmId().getAlgorithm();
|
|
if (algorithm.equals(PKCSObjectIdentifiers.dhKeyAgreement) || isPKCSParam(aSN1Sequence)) {
|
|
DHParameter dHParameter = DHParameter.getInstance(aSN1Sequence);
|
|
dHParameterSpec = dHParameter.getL() != null ? new DHParameterSpec(dHParameter.getP(), dHParameter.getG(), dHParameter.getL().intValue()) : new DHParameterSpec(dHParameter.getP(), dHParameter.getG());
|
|
} else {
|
|
if (!algorithm.equals(X9ObjectIdentifiers.dhpublicnumber)) {
|
|
throw new IllegalArgumentException("unknown algorithm type: ".concat(String.valueOf(algorithm)));
|
|
}
|
|
DHDomainParameters dHDomainParameters = DHDomainParameters.getInstance(aSN1Sequence);
|
|
dHParameterSpec = new DHParameterSpec(dHDomainParameters.getP().getValue(), dHDomainParameters.getG().getValue());
|
|
}
|
|
this.dhSpec = dHParameterSpec;
|
|
} catch (IOException unused) {
|
|
throw new IllegalArgumentException("invalid info structure in DH public key");
|
|
}
|
|
}
|
|
|
|
JCEDHPublicKey(DHPublicKeySpec dHPublicKeySpec) {
|
|
this.y = dHPublicKeySpec.getY();
|
|
this.dhSpec = new DHParameterSpec(dHPublicKeySpec.getP(), dHPublicKeySpec.getG());
|
|
}
|
|
|
|
JCEDHPublicKey(DHPublicKey dHPublicKey) {
|
|
this.y = dHPublicKey.getY();
|
|
this.dhSpec = dHPublicKey.getParams();
|
|
}
|
|
|
|
JCEDHPublicKey(BigInteger bigInteger, DHParameterSpec dHParameterSpec) {
|
|
this.y = bigInteger;
|
|
this.dhSpec = dHParameterSpec;
|
|
}
|
|
}
|