what-the-bank/sources/org/bouncycastle/jce/provider/JCEDHPrivateKey.java

129 lines
5.6 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package org.bouncycastle.jce.provider;
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.DHDomainParameters;
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 JCEDHPrivateKey implements DHPrivateKey, PKCS12BagAttributeCarrier {
static final long serialVersionUID = 311058815616901812L;
private PKCS12BagAttributeCarrier attrCarrier = new PKCS12BagAttributeCarrierImpl();
private DHParameterSpec dhSpec;
private PrivateKeyInfo info;
BigInteger x;
@Override // org.bouncycastle.jce.interfaces.PKCS12BagAttributeCarrier
public void setBagAttribute(ASN1ObjectIdentifier aSN1ObjectIdentifier, ASN1Encodable aSN1Encodable) {
this.attrCarrier.setBagAttribute(aSN1ObjectIdentifier, aSN1Encodable);
}
@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())), new ASN1Integer(getX())).getEncoded(ASN1Encoding.DER);
} catch (IOException 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";
}
private void writeObject(ObjectOutputStream objectOutputStream) throws IOException {
objectOutputStream.writeObject(getX());
objectOutputStream.writeObject(this.dhSpec.getP());
objectOutputStream.writeObject(this.dhSpec.getG());
objectOutputStream.writeInt(this.dhSpec.getL());
}
private void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
this.x = (BigInteger) objectInputStream.readObject();
this.dhSpec = new DHParameterSpec((BigInteger) objectInputStream.readObject(), (BigInteger) objectInputStream.readObject(), objectInputStream.readInt());
}
JCEDHPrivateKey(DHPrivateKeyParameters dHPrivateKeyParameters) {
this.x = dHPrivateKeyParameters.getX();
this.dhSpec = new DHParameterSpec(dHPrivateKeyParameters.getParameters().getP(), dHPrivateKeyParameters.getParameters().getG(), dHPrivateKeyParameters.getParameters().getL());
}
JCEDHPrivateKey(PrivateKeyInfo privateKeyInfo) throws IOException {
DHParameterSpec dHParameterSpec;
ASN1Sequence aSN1Sequence = ASN1Sequence.getInstance(privateKeyInfo.getAlgorithmId().getParameters());
ASN1Integer aSN1Integer = ASN1Integer.getInstance(privateKeyInfo.parsePrivateKey());
ASN1ObjectIdentifier algorithm = privateKeyInfo.getAlgorithmId().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)));
}
DHDomainParameters dHDomainParameters = DHDomainParameters.getInstance(aSN1Sequence);
dHParameterSpec = new DHParameterSpec(dHDomainParameters.getP().getValue(), dHDomainParameters.getG().getValue());
}
this.dhSpec = dHParameterSpec;
}
JCEDHPrivateKey(DHPrivateKeySpec dHPrivateKeySpec) {
this.x = dHPrivateKeySpec.getX();
this.dhSpec = new DHParameterSpec(dHPrivateKeySpec.getP(), dHPrivateKeySpec.getG());
}
JCEDHPrivateKey(DHPrivateKey dHPrivateKey) {
this.x = dHPrivateKey.getX();
this.dhSpec = dHPrivateKey.getParams();
}
protected JCEDHPrivateKey() {
}
}