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

146 lines
6.5 KiB
Java
Raw 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 java.util.Enumeration;
import javax.crypto.interfaces.DHPrivateKey;
import javax.crypto.spec.DHParameterSpec;
import javax.crypto.spec.DHPrivateKeySpec;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1Encoding;
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.pkcs.PrivateKeyInfo;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.asn1.x9.DomainParameters;
import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
import org.bouncycastle.crypto.params.DHPrivateKeyParameters;
import org.bouncycastle.jcajce.provider.asymmetric.util.PKCS12BagAttributeCarrierImpl;
import org.bouncycastle.jce.interfaces.PKCS12BagAttributeCarrier;
/* loaded from: classes6.dex */
public class BCDHPrivateKey implements DHPrivateKey, PKCS12BagAttributeCarrier {
static final long serialVersionUID = 311058815616901812L;
private transient PKCS12BagAttributeCarrierImpl attrCarrier = new PKCS12BagAttributeCarrierImpl();
private transient DHParameterSpec dhSpec;
private transient PrivateKeyInfo info;
private BigInteger x;
@Override // org.bouncycastle.jce.interfaces.PKCS12BagAttributeCarrier
public void setBagAttribute(ASN1ObjectIdentifier aSN1ObjectIdentifier, ASN1Encodable aSN1Encodable) {
this.attrCarrier.setBagAttribute(aSN1ObjectIdentifier, aSN1Encodable);
}
public int hashCode() {
return ((getX().hashCode() ^ getParams().getG().hashCode()) ^ getParams().getP().hashCode()) ^ getParams().getL();
}
@Override // javax.crypto.interfaces.DHPrivateKey
public BigInteger getX() {
return this.x;
}
@Override // javax.crypto.interfaces.DHKey
public DHParameterSpec getParams() {
return this.dhSpec;
}
@Override // java.security.Key
public String getFormat() {
return "PKCS#8";
}
@Override // java.security.Key
public byte[] getEncoded() {
try {
PrivateKeyInfo privateKeyInfo = this.info;
return privateKeyInfo != null ? privateKeyInfo.getEncoded(ASN1Encoding.DER) : new PrivateKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.dhKeyAgreement, new DHParameter(this.dhSpec.getP(), this.dhSpec.getG(), this.dhSpec.getL()).toASN1Primitive()), new ASN1Integer(getX())).getEncoded(ASN1Encoding.DER);
} catch (Exception unused) {
return null;
}
}
@Override // org.bouncycastle.jce.interfaces.PKCS12BagAttributeCarrier
public Enumeration getBagAttributeKeys() {
return this.attrCarrier.getBagAttributeKeys();
}
@Override // org.bouncycastle.jce.interfaces.PKCS12BagAttributeCarrier
public ASN1Encodable getBagAttribute(ASN1ObjectIdentifier aSN1ObjectIdentifier) {
return this.attrCarrier.getBagAttribute(aSN1ObjectIdentifier);
}
@Override // java.security.Key
public String getAlgorithm() {
return "DH";
}
public boolean equals(Object obj) {
if (!(obj instanceof DHPrivateKey)) {
return false;
}
DHPrivateKey dHPrivateKey = (DHPrivateKey) obj;
return getX().equals(dHPrivateKey.getX()) && getParams().getG().equals(dHPrivateKey.getParams().getG()) && getParams().getP().equals(dHPrivateKey.getParams().getP()) && getParams().getL() == dHPrivateKey.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;
this.attrCarrier = new PKCS12BagAttributeCarrierImpl();
}
/* JADX INFO: Access modifiers changed from: package-private */
public BCDHPrivateKey(DHPrivateKeyParameters dHPrivateKeyParameters) {
this.x = dHPrivateKeyParameters.getX();
this.dhSpec = new DHParameterSpec(dHPrivateKeyParameters.getParameters().getP(), dHPrivateKeyParameters.getParameters().getG(), dHPrivateKeyParameters.getParameters().getL());
}
public BCDHPrivateKey(PrivateKeyInfo privateKeyInfo) throws IOException {
DHParameterSpec dHParameterSpec;
ASN1Sequence aSN1Sequence = ASN1Sequence.getInstance(privateKeyInfo.getPrivateKeyAlgorithm().getParameters());
ASN1Integer aSN1Integer = (ASN1Integer) privateKeyInfo.parsePrivateKey();
ASN1ObjectIdentifier algorithm = privateKeyInfo.getPrivateKeyAlgorithm().getAlgorithm();
this.info = privateKeyInfo;
this.x = aSN1Integer.getValue();
if (algorithm.equals(PKCSObjectIdentifiers.dhKeyAgreement)) {
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;
}
/* JADX INFO: Access modifiers changed from: package-private */
public BCDHPrivateKey(DHPrivateKeySpec dHPrivateKeySpec) {
this.x = dHPrivateKeySpec.getX();
this.dhSpec = new DHParameterSpec(dHPrivateKeySpec.getP(), dHPrivateKeySpec.getG());
}
/* JADX INFO: Access modifiers changed from: package-private */
public BCDHPrivateKey(DHPrivateKey dHPrivateKey) {
this.x = dHPrivateKey.getX();
this.dhSpec = dHPrivateKey.getParams();
}
protected BCDHPrivateKey() {
}
}