what-the-bank/sources/org/bouncycastle/jcajce/provider/asymmetric/dh/BCDHPublicKey.java

137 lines
6.2 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package org.bouncycastle.jcajce.provider.asymmetric.dh;
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.DomainParameters;
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 BCDHPublicKey implements DHPublicKey {
static final long serialVersionUID = -216691575254424324L;
private transient DHParameterSpec dhSpec;
private transient SubjectPublicKeyInfo info;
private BigInteger y;
public int hashCode() {
return ((getY().hashCode() ^ getParams().getG().hashCode()) ^ getParams().getP().hashCode()) ^ getParams().getL();
}
@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()).toASN1Primitive()), new ASN1Integer(this.y));
}
@Override // java.security.Key
public String getAlgorithm() {
return "DH";
}
public boolean equals(Object obj) {
if (!(obj instanceof DHPublicKey)) {
return false;
}
DHPublicKey dHPublicKey = (DHPublicKey) obj;
return getY().equals(dHPublicKey.getY()) && getParams().getG().equals(dHPublicKey.getParams().getG()) && getParams().getP().equals(dHPublicKey.getParams().getP()) && getParams().getL() == dHPublicKey.getParams().getL();
}
private void writeObject(ObjectOutputStream objectOutputStream) throws IOException {
objectOutputStream.defaultWriteObject();
objectOutputStream.writeObject(this.dhSpec.getP());
objectOutputStream.writeObject(this.dhSpec.getG());
objectOutputStream.writeInt(this.dhSpec.getL());
}
private void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
objectInputStream.defaultReadObject();
this.dhSpec = new DHParameterSpec((BigInteger) objectInputStream.readObject(), (BigInteger) objectInputStream.readObject(), objectInputStream.readInt());
this.info = null;
}
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;
}
/* JADX INFO: Access modifiers changed from: package-private */
public BCDHPublicKey(DHPublicKeyParameters dHPublicKeyParameters) {
this.y = dHPublicKeyParameters.getY();
this.dhSpec = new DHParameterSpec(dHPublicKeyParameters.getParameters().getP(), dHPublicKeyParameters.getParameters().getG(), dHPublicKeyParameters.getParameters().getL());
}
public BCDHPublicKey(SubjectPublicKeyInfo subjectPublicKeyInfo) {
DHParameterSpec dHParameterSpec;
this.info = subjectPublicKeyInfo;
try {
this.y = ((ASN1Integer) subjectPublicKeyInfo.parsePublicKey()).getValue();
ASN1Sequence aSN1Sequence = ASN1Sequence.getInstance(subjectPublicKeyInfo.getAlgorithm().getParameters());
ASN1ObjectIdentifier algorithm = subjectPublicKeyInfo.getAlgorithm().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)));
}
DomainParameters domainParameters = DomainParameters.getInstance(aSN1Sequence);
dHParameterSpec = new DHParameterSpec(domainParameters.getP(), domainParameters.getG());
}
this.dhSpec = dHParameterSpec;
} catch (IOException unused) {
throw new IllegalArgumentException("invalid info structure in DH public key");
}
}
/* JADX INFO: Access modifiers changed from: package-private */
public BCDHPublicKey(DHPublicKeySpec dHPublicKeySpec) {
this.y = dHPublicKeySpec.getY();
this.dhSpec = new DHParameterSpec(dHPublicKeySpec.getP(), dHPublicKeySpec.getG());
}
/* JADX INFO: Access modifiers changed from: package-private */
public BCDHPublicKey(DHPublicKey dHPublicKey) {
this.y = dHPublicKey.getY();
this.dhSpec = dHPublicKey.getParams();
}
/* JADX INFO: Access modifiers changed from: package-private */
public BCDHPublicKey(BigInteger bigInteger, DHParameterSpec dHParameterSpec) {
this.y = bigInteger;
this.dhSpec = dHParameterSpec;
}
}