what-the-bank/sources/org/bouncycastle/jcajce/provider/asymmetric/ec/BCECPrivateKey.java

332 lines
16 KiB
Java

package org.bouncycastle.jcajce.provider.asymmetric.ec;
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.ASN1Integer;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.DERBitString;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.asn1.x9.X962Parameters;
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.PKCS12BagAttributeCarrierImpl;
import org.bouncycastle.jcajce.provider.config.ProviderConfiguration;
import org.bouncycastle.jce.interfaces.ECPointEncoder;
import org.bouncycastle.jce.interfaces.PKCS12BagAttributeCarrier;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.jce.spec.ECPrivateKeySpec;
import org.bouncycastle.util.Strings;
/* loaded from: classes6.dex */
public class BCECPrivateKey implements ECPrivateKey, org.bouncycastle.jce.interfaces.ECPrivateKey, PKCS12BagAttributeCarrier, ECPointEncoder {
static final long serialVersionUID = 994553197664784084L;
private String algorithm;
private transient PKCS12BagAttributeCarrierImpl attrCarrier;
private transient ProviderConfiguration configuration;
private transient BigInteger d;
private transient ECParameterSpec ecSpec;
private transient 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";
}
/* JADX WARN: Removed duplicated region for block: B:10:0x0087 */
/* JADX WARN: Removed duplicated region for block: B:17:0x0093 */
@Override // java.security.Key
/*
Code decompiled incorrectly, please refer to instructions dump.
To view partially-correct add '--show-bad-code' argument
*/
public byte[] getEncoded() {
/*
r10 = this;
java.security.spec.ECParameterSpec r0 = r10.ecSpec
boolean r1 = r0 instanceof org.bouncycastle.jce.spec.ECNamedCurveSpec
r2 = 0
if (r1 == 0) goto L26
org.bouncycastle.jce.spec.ECNamedCurveSpec r0 = (org.bouncycastle.jce.spec.ECNamedCurveSpec) r0
java.lang.String r0 = r0.getName()
org.bouncycastle.asn1.ASN1ObjectIdentifier r0 = org.bouncycastle.jcajce.provider.asymmetric.util.ECUtil.getNamedCurveOid(r0)
if (r0 != 0) goto L20
org.bouncycastle.asn1.ASN1ObjectIdentifier r0 = new org.bouncycastle.asn1.ASN1ObjectIdentifier
java.security.spec.ECParameterSpec r1 = r10.ecSpec
org.bouncycastle.jce.spec.ECNamedCurveSpec r1 = (org.bouncycastle.jce.spec.ECNamedCurveSpec) r1
java.lang.String r1 = r1.getName()
r0.<init>(r1)
L20:
org.bouncycastle.asn1.x9.X962Parameters r1 = new org.bouncycastle.asn1.x9.X962Parameters
r1.<init>(r0)
goto L72
L26:
if (r0 != 0) goto L38
org.bouncycastle.asn1.x9.X962Parameters r0 = new org.bouncycastle.asn1.x9.X962Parameters
org.bouncycastle.asn1.DERNull r1 = org.bouncycastle.asn1.DERNull.INSTANCE
r0.<init>(r1)
java.math.BigInteger r1 = r10.getS()
int r1 = org.bouncycastle.jcajce.provider.asymmetric.util.ECUtil.getOrderBitLength(r2, r1)
goto L83
L38:
java.security.spec.EllipticCurve r0 = r0.getCurve()
org.bouncycastle.math.ec.ECCurve r4 = org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util.convertCurve(r0)
org.bouncycastle.asn1.x9.X962Parameters r1 = new org.bouncycastle.asn1.x9.X962Parameters
org.bouncycastle.asn1.x9.X9ECParameters r0 = new org.bouncycastle.asn1.x9.X9ECParameters
java.security.spec.ECParameterSpec r3 = r10.ecSpec
java.security.spec.ECPoint r3 = r3.getGenerator()
boolean r5 = r10.withCompression
org.bouncycastle.math.ec.ECPoint r5 = org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util.convertPoint(r4, r3, r5)
java.security.spec.ECParameterSpec r3 = r10.ecSpec
java.math.BigInteger r6 = r3.getOrder()
java.security.spec.ECParameterSpec r3 = r10.ecSpec
int r3 = r3.getCofactor()
long r7 = (long) r3
java.math.BigInteger r7 = java.math.BigInteger.valueOf(r7)
java.security.spec.ECParameterSpec r3 = r10.ecSpec
java.security.spec.EllipticCurve r3 = r3.getCurve()
byte[] r8 = r3.getSeed()
r3 = r0
r3.<init>(r4, r5, r6, r7, r8)
r1.<init>(r0)
L72:
java.security.spec.ECParameterSpec r0 = r10.ecSpec
java.math.BigInteger r0 = r0.getOrder()
java.math.BigInteger r3 = r10.getS()
int r0 = org.bouncycastle.jcajce.provider.asymmetric.util.ECUtil.getOrderBitLength(r0, r3)
r9 = r1
r1 = r0
r0 = r9
L83:
org.bouncycastle.asn1.DERBitString r3 = r10.publicKey
if (r3 == 0) goto L93
org.bouncycastle.asn1.sec.ECPrivateKey r3 = new org.bouncycastle.asn1.sec.ECPrivateKey
java.math.BigInteger r4 = r10.getS()
org.bouncycastle.asn1.DERBitString r5 = r10.publicKey
r3.<init>(r1, r4, r5, r0)
goto L9c
L93:
org.bouncycastle.asn1.sec.ECPrivateKey r3 = new org.bouncycastle.asn1.sec.ECPrivateKey
java.math.BigInteger r4 = r10.getS()
r3.<init>(r1, r4, r0)
L9c:
org.bouncycastle.asn1.pkcs.PrivateKeyInfo r1 = new org.bouncycastle.asn1.pkcs.PrivateKeyInfo // Catch: java.io.IOException -> Laf
org.bouncycastle.asn1.x509.AlgorithmIdentifier r4 = new org.bouncycastle.asn1.x509.AlgorithmIdentifier // Catch: java.io.IOException -> Laf
org.bouncycastle.asn1.ASN1ObjectIdentifier r5 = org.bouncycastle.asn1.x9.X9ObjectIdentifiers.id_ecPublicKey // Catch: java.io.IOException -> Laf
r4.<init>(r5, r0) // Catch: java.io.IOException -> Laf
r1.<init>(r4, r3) // Catch: java.io.IOException -> Laf
java.lang.String r0 = "DER"
byte[] r0 = r1.getEncoded(r0) // Catch: java.io.IOException -> Laf
return r0
Laf:
return r2
*/
throw new UnsupportedOperationException("Method not decompiled: org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey.getEncoded():byte[]");
}
@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 BCECPrivateKey)) {
return false;
}
BCECPrivateKey bCECPrivateKey = (BCECPrivateKey) obj;
return getD().equals(bCECPrivateKey.getD()) && engineGetSpec().equals(bCECPrivateKey.engineGetSpec());
}
org.bouncycastle.jce.spec.ECParameterSpec engineGetSpec() {
ECParameterSpec eCParameterSpec = this.ecSpec;
return eCParameterSpec != null ? EC5Util.convertSpec(eCParameterSpec, this.withCompression) : this.configuration.getEcImplicitlyCa();
}
private void writeObject(ObjectOutputStream objectOutputStream) throws IOException {
objectOutputStream.defaultWriteObject();
objectOutputStream.writeObject(getEncoded());
}
private void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
objectInputStream.defaultReadObject();
populateFromPrivKeyInfo(PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray((byte[]) objectInputStream.readObject())));
this.configuration = BouncyCastleProvider.CONFIGURATION;
this.attrCarrier = new PKCS12BagAttributeCarrierImpl();
}
private void populateFromPrivKeyInfo(PrivateKeyInfo privateKeyInfo) throws IOException {
X962Parameters x962Parameters = X962Parameters.getInstance(privateKeyInfo.getPrivateKeyAlgorithm().getParameters());
this.ecSpec = EC5Util.convertToSpec(x962Parameters, EC5Util.getCurve(this.configuration, x962Parameters));
ASN1Encodable parsePrivateKey = privateKeyInfo.parsePrivateKey();
if (parsePrivateKey instanceof ASN1Integer) {
this.d = ASN1Integer.getInstance(parsePrivateKey).getValue();
return;
}
org.bouncycastle.asn1.sec.ECPrivateKey eCPrivateKey = org.bouncycastle.asn1.sec.ECPrivateKey.getInstance(parsePrivateKey);
this.d = eCPrivateKey.getKey();
this.publicKey = eCPrivateKey.getPublicKey();
}
private DERBitString getPublicKeyDetails(BCECPublicKey bCECPublicKey) {
try {
return SubjectPublicKeyInfo.getInstance(ASN1Primitive.fromByteArray(bCECPublicKey.getEncoded())).getPublicKeyData();
} catch (IOException unused) {
return null;
}
}
public BCECPrivateKey(ECPrivateKey eCPrivateKey, ProviderConfiguration providerConfiguration) {
this.algorithm = "EC";
this.attrCarrier = new PKCS12BagAttributeCarrierImpl();
this.d = eCPrivateKey.getS();
this.algorithm = eCPrivateKey.getAlgorithm();
this.ecSpec = eCPrivateKey.getParams();
this.configuration = providerConfiguration;
}
public BCECPrivateKey(String str, ECPrivateKeySpec eCPrivateKeySpec, ProviderConfiguration providerConfiguration) {
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;
this.configuration = providerConfiguration;
}
public BCECPrivateKey(String str, BCECPrivateKey bCECPrivateKey) {
this.algorithm = "EC";
this.attrCarrier = new PKCS12BagAttributeCarrierImpl();
this.algorithm = str;
this.d = bCECPrivateKey.d;
this.ecSpec = bCECPrivateKey.ecSpec;
this.withCompression = bCECPrivateKey.withCompression;
this.attrCarrier = bCECPrivateKey.attrCarrier;
this.publicKey = bCECPrivateKey.publicKey;
this.configuration = bCECPrivateKey.configuration;
}
public BCECPrivateKey(String str, ECPrivateKeyParameters eCPrivateKeyParameters, ProviderConfiguration providerConfiguration) {
this.algorithm = "EC";
this.attrCarrier = new PKCS12BagAttributeCarrierImpl();
this.algorithm = str;
this.d = eCPrivateKeyParameters.getD();
this.ecSpec = null;
this.configuration = providerConfiguration;
}
public BCECPrivateKey(String str, ECPrivateKeyParameters eCPrivateKeyParameters, BCECPublicKey bCECPublicKey, org.bouncycastle.jce.spec.ECParameterSpec eCParameterSpec, ProviderConfiguration providerConfiguration) {
this.algorithm = "EC";
this.attrCarrier = new PKCS12BagAttributeCarrierImpl();
ECDomainParameters parameters = eCPrivateKeyParameters.getParameters();
this.algorithm = str;
this.d = eCPrivateKeyParameters.getD();
this.configuration = providerConfiguration;
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()) : EC5Util.convertSpec(EC5Util.convertCurve(eCParameterSpec.getCurve(), eCParameterSpec.getSeed()), eCParameterSpec);
this.publicKey = getPublicKeyDetails(bCECPublicKey);
}
public BCECPrivateKey(String str, ECPrivateKeyParameters eCPrivateKeyParameters, BCECPublicKey bCECPublicKey, ECParameterSpec eCParameterSpec, ProviderConfiguration providerConfiguration) {
this.algorithm = "EC";
this.attrCarrier = new PKCS12BagAttributeCarrierImpl();
ECDomainParameters parameters = eCPrivateKeyParameters.getParameters();
this.algorithm = str;
this.d = eCPrivateKeyParameters.getD();
this.configuration = providerConfiguration;
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(bCECPublicKey);
}
/* JADX INFO: Access modifiers changed from: package-private */
public BCECPrivateKey(String str, PrivateKeyInfo privateKeyInfo, ProviderConfiguration providerConfiguration) throws IOException {
this.algorithm = "EC";
this.attrCarrier = new PKCS12BagAttributeCarrierImpl();
this.algorithm = str;
this.configuration = providerConfiguration;
populateFromPrivKeyInfo(privateKeyInfo);
}
public BCECPrivateKey(String str, java.security.spec.ECPrivateKeySpec eCPrivateKeySpec, ProviderConfiguration providerConfiguration) {
this.algorithm = "EC";
this.attrCarrier = new PKCS12BagAttributeCarrierImpl();
this.algorithm = str;
this.d = eCPrivateKeySpec.getS();
this.ecSpec = eCPrivateKeySpec.getParams();
this.configuration = providerConfiguration;
}
protected BCECPrivateKey() {
this.algorithm = "EC";
this.attrCarrier = new PKCS12BagAttributeCarrierImpl();
}
}