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

358 lines
18 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.security.interfaces.ECPrivateKey;
import java.security.spec.ECParameterSpec;
import java.security.spec.ECPoint;
import java.util.Enumeration;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1Encoding;
import org.bouncycastle.asn1.ASN1Null;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.DERBitString;
import org.bouncycastle.asn1.DERNull;
import org.bouncycastle.asn1.cryptopro.CryptoProObjectIdentifiers;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.asn1.sec.ECPrivateKeyStructure;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.asn1.x9.X962Parameters;
import org.bouncycastle.asn1.x9.X9ECParameters;
import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
import org.bouncycastle.crypto.params.ECDomainParameters;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util;
import org.bouncycastle.jcajce.provider.asymmetric.util.ECUtil;
import org.bouncycastle.jcajce.provider.asymmetric.util.PKCS12BagAttributeCarrierImpl;
import org.bouncycastle.jce.interfaces.ECPointEncoder;
import org.bouncycastle.jce.interfaces.PKCS12BagAttributeCarrier;
import org.bouncycastle.jce.spec.ECNamedCurveSpec;
import org.bouncycastle.jce.spec.ECPrivateKeySpec;
import org.bouncycastle.math.ec.ECCurve;
import org.bouncycastle.util.Strings;
/* loaded from: classes6.dex */
public class JCEECPrivateKey implements ECPrivateKey, org.bouncycastle.jce.interfaces.ECPrivateKey, PKCS12BagAttributeCarrier, ECPointEncoder {
private String algorithm;
private PKCS12BagAttributeCarrierImpl attrCarrier;
private BigInteger d;
private ECParameterSpec ecSpec;
private DERBitString publicKey;
private boolean withCompression;
public String toString() {
StringBuffer stringBuffer = new StringBuffer("EC Private Key");
String lineSeparator = Strings.lineSeparator();
stringBuffer.append(lineSeparator);
stringBuffer.append(" S: ").append(this.d.toString(16)).append(lineSeparator);
return stringBuffer.toString();
}
@Override // org.bouncycastle.jce.interfaces.ECPointEncoder
public void setPointFormat(String str) {
this.withCompression = !"UNCOMPRESSED".equalsIgnoreCase(str);
}
@Override // org.bouncycastle.jce.interfaces.PKCS12BagAttributeCarrier
public void setBagAttribute(ASN1ObjectIdentifier aSN1ObjectIdentifier, ASN1Encodable aSN1Encodable) {
this.attrCarrier.setBagAttribute(aSN1ObjectIdentifier, aSN1Encodable);
}
public int hashCode() {
return getD().hashCode() ^ engineGetSpec().hashCode();
}
@Override // java.security.interfaces.ECPrivateKey
public BigInteger getS() {
return this.d;
}
@Override // java.security.interfaces.ECKey
public ECParameterSpec getParams() {
return this.ecSpec;
}
@Override // org.bouncycastle.jce.interfaces.ECKey
public org.bouncycastle.jce.spec.ECParameterSpec getParameters() {
ECParameterSpec eCParameterSpec = this.ecSpec;
if (eCParameterSpec == null) {
return null;
}
return EC5Util.convertSpec(eCParameterSpec, this.withCompression);
}
@Override // java.security.Key
public String getFormat() {
return "PKCS#8";
}
@Override // java.security.Key
public byte[] getEncoded() {
X962Parameters x962Parameters;
ECParameterSpec eCParameterSpec = this.ecSpec;
if (eCParameterSpec instanceof ECNamedCurveSpec) {
ASN1ObjectIdentifier namedCurveOid = ECUtil.getNamedCurveOid(((ECNamedCurveSpec) eCParameterSpec).getName());
if (namedCurveOid == null) {
namedCurveOid = new ASN1ObjectIdentifier(((ECNamedCurveSpec) this.ecSpec).getName());
}
x962Parameters = new X962Parameters(namedCurveOid);
} else if (eCParameterSpec == null) {
x962Parameters = new X962Parameters((ASN1Null) DERNull.INSTANCE);
} else {
ECCurve convertCurve = EC5Util.convertCurve(eCParameterSpec.getCurve());
x962Parameters = new X962Parameters(new X9ECParameters(convertCurve, EC5Util.convertPoint(convertCurve, this.ecSpec.getGenerator(), this.withCompression), this.ecSpec.getOrder(), BigInteger.valueOf(this.ecSpec.getCofactor()), this.ecSpec.getCurve().getSeed()));
}
ECPrivateKeyStructure eCPrivateKeyStructure = this.publicKey != null ? new ECPrivateKeyStructure(getS(), this.publicKey, x962Parameters) : new ECPrivateKeyStructure(getS(), x962Parameters);
try {
return (this.algorithm.equals("ECGOST3410") ? new PrivateKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_2001, x962Parameters.toASN1Primitive()), eCPrivateKeyStructure.toASN1Primitive()) : new PrivateKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, x962Parameters.toASN1Primitive()), eCPrivateKeyStructure.toASN1Primitive())).getEncoded(ASN1Encoding.DER);
} catch (IOException unused) {
return null;
}
}
@Override // org.bouncycastle.jce.interfaces.ECPrivateKey
public BigInteger getD() {
return this.d;
}
@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 this.algorithm;
}
public boolean equals(Object obj) {
if (!(obj instanceof JCEECPrivateKey)) {
return false;
}
JCEECPrivateKey jCEECPrivateKey = (JCEECPrivateKey) obj;
return getD().equals(jCEECPrivateKey.getD()) && engineGetSpec().equals(jCEECPrivateKey.engineGetSpec());
}
org.bouncycastle.jce.spec.ECParameterSpec engineGetSpec() {
ECParameterSpec eCParameterSpec = this.ecSpec;
return eCParameterSpec != null ? EC5Util.convertSpec(eCParameterSpec, this.withCompression) : BouncyCastleProvider.CONFIGURATION.getEcImplicitlyCa();
}
private void writeObject(ObjectOutputStream objectOutputStream) throws IOException {
objectOutputStream.writeObject(getEncoded());
objectOutputStream.writeObject(this.algorithm);
objectOutputStream.writeBoolean(this.withCompression);
this.attrCarrier.writeObject(objectOutputStream);
}
private void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
populateFromPrivKeyInfo(PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray((byte[]) objectInputStream.readObject())));
this.algorithm = (String) objectInputStream.readObject();
this.withCompression = objectInputStream.readBoolean();
PKCS12BagAttributeCarrierImpl pKCS12BagAttributeCarrierImpl = new PKCS12BagAttributeCarrierImpl();
this.attrCarrier = pKCS12BagAttributeCarrierImpl;
pKCS12BagAttributeCarrierImpl.readObject(objectInputStream);
}
/* JADX WARN: Removed duplicated region for block: B:12:0x0101 */
/* JADX WARN: Removed duplicated region for block: B:9:0x00f6 */
/*
Code decompiled incorrectly, please refer to instructions dump.
To view partially-correct add '--show-bad-code' argument
*/
private void populateFromPrivKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo r11) throws java.io.IOException {
/*
r10 = this;
org.bouncycastle.asn1.x9.X962Parameters r0 = new org.bouncycastle.asn1.x9.X962Parameters
org.bouncycastle.asn1.x509.AlgorithmIdentifier r1 = r11.getPrivateKeyAlgorithm()
org.bouncycastle.asn1.ASN1Encodable r1 = r1.getParameters()
org.bouncycastle.asn1.ASN1Primitive r1 = (org.bouncycastle.asn1.ASN1Primitive) r1
r0.<init>(r1)
boolean r1 = r0.isNamedCurve()
if (r1 == 0) goto La0
org.bouncycastle.asn1.ASN1Primitive r0 = r0.getParameters()
org.bouncycastle.asn1.ASN1ObjectIdentifier r0 = org.bouncycastle.asn1.ASN1ObjectIdentifier.getInstance(r0)
org.bouncycastle.asn1.x9.X9ECParameters r1 = org.bouncycastle.jcajce.provider.asymmetric.util.ECUtil.getNamedCurveByOid(r0)
if (r1 != 0) goto L64
org.bouncycastle.crypto.params.ECDomainParameters r1 = org.bouncycastle.asn1.cryptopro.ECGOST3410NamedCurves.getByOID(r0)
org.bouncycastle.math.ec.ECCurve r2 = r1.getCurve()
byte[] r3 = r1.getSeed()
java.security.spec.EllipticCurve r6 = org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util.convertCurve(r2, r3)
org.bouncycastle.jce.spec.ECNamedCurveSpec r2 = new org.bouncycastle.jce.spec.ECNamedCurveSpec
java.lang.String r5 = org.bouncycastle.asn1.cryptopro.ECGOST3410NamedCurves.getName(r0)
java.security.spec.ECPoint r7 = new java.security.spec.ECPoint
org.bouncycastle.math.ec.ECPoint r0 = r1.getG()
org.bouncycastle.math.ec.ECFieldElement r0 = r0.getAffineXCoord()
java.math.BigInteger r0 = r0.toBigInteger()
org.bouncycastle.math.ec.ECPoint r3 = r1.getG()
org.bouncycastle.math.ec.ECFieldElement r3 = r3.getAffineYCoord()
java.math.BigInteger r3 = r3.toBigInteger()
r7.<init>(r0, r3)
java.math.BigInteger r8 = r1.getN()
java.math.BigInteger r9 = r1.getH()
r4 = r2
r4.<init>(r5, r6, r7, r8, r9)
goto Lec
L64:
org.bouncycastle.math.ec.ECCurve r2 = r1.getCurve()
byte[] r3 = r1.getSeed()
java.security.spec.EllipticCurve r6 = org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util.convertCurve(r2, r3)
org.bouncycastle.jce.spec.ECNamedCurveSpec r2 = new org.bouncycastle.jce.spec.ECNamedCurveSpec
java.lang.String r5 = org.bouncycastle.jcajce.provider.asymmetric.util.ECUtil.getCurveName(r0)
java.security.spec.ECPoint r7 = new java.security.spec.ECPoint
org.bouncycastle.math.ec.ECPoint r0 = r1.getG()
org.bouncycastle.math.ec.ECFieldElement r0 = r0.getAffineXCoord()
java.math.BigInteger r0 = r0.toBigInteger()
org.bouncycastle.math.ec.ECPoint r3 = r1.getG()
org.bouncycastle.math.ec.ECFieldElement r3 = r3.getAffineYCoord()
java.math.BigInteger r3 = r3.toBigInteger()
r7.<init>(r0, r3)
java.math.BigInteger r8 = r1.getN()
java.math.BigInteger r9 = r1.getH()
r4 = r2
r4.<init>(r5, r6, r7, r8, r9)
goto Lec
La0:
boolean r1 = r0.isImplicitlyCA()
if (r1 == 0) goto Laa
r0 = 0
r10.ecSpec = r0
goto Lee
Laa:
org.bouncycastle.asn1.ASN1Primitive r0 = r0.getParameters()
org.bouncycastle.asn1.x9.X9ECParameters r0 = org.bouncycastle.asn1.x9.X9ECParameters.getInstance(r0)
java.security.spec.ECParameterSpec r2 = new java.security.spec.ECParameterSpec
org.bouncycastle.math.ec.ECCurve r1 = r0.getCurve()
byte[] r3 = r0.getSeed()
java.security.spec.EllipticCurve r1 = org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util.convertCurve(r1, r3)
java.security.spec.ECPoint r3 = new java.security.spec.ECPoint
org.bouncycastle.math.ec.ECPoint r4 = r0.getG()
org.bouncycastle.math.ec.ECFieldElement r4 = r4.getAffineXCoord()
java.math.BigInteger r4 = r4.toBigInteger()
org.bouncycastle.math.ec.ECPoint r5 = r0.getG()
org.bouncycastle.math.ec.ECFieldElement r5 = r5.getAffineYCoord()
java.math.BigInteger r5 = r5.toBigInteger()
r3.<init>(r4, r5)
java.math.BigInteger r4 = r0.getN()
java.math.BigInteger r0 = r0.getH()
int r0 = r0.intValue()
r2.<init>(r1, r3, r4, r0)
Lec:
r10.ecSpec = r2
Lee:
org.bouncycastle.asn1.ASN1Encodable r11 = r11.parsePrivateKey()
boolean r0 = r11 instanceof org.bouncycastle.asn1.ASN1Integer
if (r0 == 0) goto L101
org.bouncycastle.asn1.ASN1Integer r11 = org.bouncycastle.asn1.ASN1Integer.getInstance(r11)
java.math.BigInteger r11 = r11.getValue()
r10.d = r11
return
L101:
org.bouncycastle.asn1.sec.ECPrivateKeyStructure r0 = new org.bouncycastle.asn1.sec.ECPrivateKeyStructure
org.bouncycastle.asn1.ASN1Sequence r11 = (org.bouncycastle.asn1.ASN1Sequence) r11
r0.<init>(r11)
java.math.BigInteger r11 = r0.getKey()
r10.d = r11
org.bouncycastle.asn1.DERBitString r11 = r0.getPublicKey()
r10.publicKey = r11
return
*/
throw new UnsupportedOperationException("Method not decompiled: org.bouncycastle.jce.provider.JCEECPrivateKey.populateFromPrivKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo):void");
}
private DERBitString getPublicKeyDetails(JCEECPublicKey jCEECPublicKey) {
try {
return SubjectPublicKeyInfo.getInstance(ASN1Primitive.fromByteArray(jCEECPublicKey.getEncoded())).getPublicKeyData();
} catch (IOException unused) {
return null;
}
}
JCEECPrivateKey(PrivateKeyInfo privateKeyInfo) throws IOException {
this.algorithm = "EC";
this.attrCarrier = new PKCS12BagAttributeCarrierImpl();
populateFromPrivKeyInfo(privateKeyInfo);
}
public JCEECPrivateKey(ECPrivateKey eCPrivateKey) {
this.algorithm = "EC";
this.attrCarrier = new PKCS12BagAttributeCarrierImpl();
this.d = eCPrivateKey.getS();
this.algorithm = eCPrivateKey.getAlgorithm();
this.ecSpec = eCPrivateKey.getParams();
}
public JCEECPrivateKey(String str, ECPrivateKeySpec eCPrivateKeySpec) {
this.algorithm = "EC";
this.attrCarrier = new PKCS12BagAttributeCarrierImpl();
this.algorithm = str;
this.d = eCPrivateKeySpec.getD();
this.ecSpec = eCPrivateKeySpec.getParams() != null ? EC5Util.convertSpec(EC5Util.convertCurve(eCPrivateKeySpec.getParams().getCurve(), eCPrivateKeySpec.getParams().getSeed()), eCPrivateKeySpec.getParams()) : null;
}
public JCEECPrivateKey(String str, JCEECPrivateKey jCEECPrivateKey) {
this.algorithm = "EC";
this.attrCarrier = new PKCS12BagAttributeCarrierImpl();
this.algorithm = str;
this.d = jCEECPrivateKey.d;
this.ecSpec = jCEECPrivateKey.ecSpec;
this.withCompression = jCEECPrivateKey.withCompression;
this.attrCarrier = jCEECPrivateKey.attrCarrier;
this.publicKey = jCEECPrivateKey.publicKey;
}
public JCEECPrivateKey(String str, ECPrivateKeyParameters eCPrivateKeyParameters, JCEECPublicKey jCEECPublicKey, org.bouncycastle.jce.spec.ECParameterSpec eCParameterSpec) {
this.algorithm = "EC";
this.attrCarrier = new PKCS12BagAttributeCarrierImpl();
ECDomainParameters parameters = eCPrivateKeyParameters.getParameters();
this.algorithm = str;
this.d = eCPrivateKeyParameters.getD();
this.ecSpec = eCParameterSpec == null ? new ECParameterSpec(EC5Util.convertCurve(parameters.getCurve(), parameters.getSeed()), new ECPoint(parameters.getG().getAffineXCoord().toBigInteger(), parameters.getG().getAffineYCoord().toBigInteger()), parameters.getN(), parameters.getH().intValue()) : new ECParameterSpec(EC5Util.convertCurve(eCParameterSpec.getCurve(), eCParameterSpec.getSeed()), new ECPoint(eCParameterSpec.getG().getAffineXCoord().toBigInteger(), eCParameterSpec.getG().getAffineYCoord().toBigInteger()), eCParameterSpec.getN(), eCParameterSpec.getH().intValue());
this.publicKey = getPublicKeyDetails(jCEECPublicKey);
}
public JCEECPrivateKey(String str, ECPrivateKeyParameters eCPrivateKeyParameters, JCEECPublicKey jCEECPublicKey, ECParameterSpec eCParameterSpec) {
this.algorithm = "EC";
this.attrCarrier = new PKCS12BagAttributeCarrierImpl();
ECDomainParameters parameters = eCPrivateKeyParameters.getParameters();
this.algorithm = str;
this.d = eCPrivateKeyParameters.getD();
if (eCParameterSpec == null) {
this.ecSpec = new ECParameterSpec(EC5Util.convertCurve(parameters.getCurve(), parameters.getSeed()), new ECPoint(parameters.getG().getAffineXCoord().toBigInteger(), parameters.getG().getAffineYCoord().toBigInteger()), parameters.getN(), parameters.getH().intValue());
} else {
this.ecSpec = eCParameterSpec;
}
this.publicKey = getPublicKeyDetails(jCEECPublicKey);
}
public JCEECPrivateKey(String str, ECPrivateKeyParameters eCPrivateKeyParameters) {
this.algorithm = "EC";
this.attrCarrier = new PKCS12BagAttributeCarrierImpl();
this.algorithm = str;
this.d = eCPrivateKeyParameters.getD();
this.ecSpec = null;
}
public JCEECPrivateKey(String str, java.security.spec.ECPrivateKeySpec eCPrivateKeySpec) {
this.algorithm = "EC";
this.attrCarrier = new PKCS12BagAttributeCarrierImpl();
this.algorithm = str;
this.d = eCPrivateKeySpec.getS();
this.ecSpec = eCPrivateKeySpec.getParams();
}
protected JCEECPrivateKey() {
this.algorithm = "EC";
this.attrCarrier = new PKCS12BagAttributeCarrierImpl();
}
}