258 lines
13 KiB
Java
258 lines
13 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.ECPublicKey;
|
|
import java.security.spec.ECParameterSpec;
|
|
import java.security.spec.EllipticCurve;
|
|
import org.bouncycastle.asn1.ASN1Null;
|
|
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
|
|
import org.bouncycastle.asn1.ASN1OctetString;
|
|
import org.bouncycastle.asn1.ASN1Primitive;
|
|
import org.bouncycastle.asn1.DERNull;
|
|
import org.bouncycastle.asn1.DEROctetString;
|
|
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.X9ECPoint;
|
|
import org.bouncycastle.asn1.x9.X9IntegerConverter;
|
|
import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
|
|
import org.bouncycastle.crypto.params.ECDomainParameters;
|
|
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
|
|
import org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util;
|
|
import org.bouncycastle.jcajce.provider.asymmetric.util.ECUtil;
|
|
import org.bouncycastle.jcajce.provider.asymmetric.util.KeyUtil;
|
|
import org.bouncycastle.jcajce.provider.config.ProviderConfiguration;
|
|
import org.bouncycastle.jce.interfaces.ECPointEncoder;
|
|
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
|
import org.bouncycastle.jce.spec.ECNamedCurveSpec;
|
|
import org.bouncycastle.jce.spec.ECPublicKeySpec;
|
|
import org.bouncycastle.math.ec.ECCurve;
|
|
import org.bouncycastle.math.ec.ECPoint;
|
|
import org.bouncycastle.util.Strings;
|
|
|
|
/* loaded from: classes6.dex */
|
|
public class BCECPublicKey implements ECPublicKey, org.bouncycastle.jce.interfaces.ECPublicKey, ECPointEncoder {
|
|
static final long serialVersionUID = 2422789860422731812L;
|
|
private String algorithm;
|
|
private transient ProviderConfiguration configuration;
|
|
private transient ECParameterSpec ecSpec;
|
|
private transient ECPoint q;
|
|
private boolean withCompression;
|
|
|
|
public String toString() {
|
|
StringBuffer stringBuffer = new StringBuffer("EC Public Key");
|
|
String lineSeparator = Strings.lineSeparator();
|
|
stringBuffer.append(lineSeparator);
|
|
stringBuffer.append(" X: ").append(this.q.getAffineXCoord().toBigInteger().toString(16)).append(lineSeparator);
|
|
stringBuffer.append(" Y: ").append(this.q.getAffineYCoord().toBigInteger().toString(16)).append(lineSeparator);
|
|
return stringBuffer.toString();
|
|
}
|
|
|
|
@Override // org.bouncycastle.jce.interfaces.ECPointEncoder
|
|
public void setPointFormat(String str) {
|
|
this.withCompression = !"UNCOMPRESSED".equalsIgnoreCase(str);
|
|
}
|
|
|
|
public int hashCode() {
|
|
return engineGetQ().hashCode() ^ engineGetSpec().hashCode();
|
|
}
|
|
|
|
@Override // java.security.interfaces.ECPublicKey
|
|
public java.security.spec.ECPoint getW() {
|
|
return new java.security.spec.ECPoint(this.q.getAffineXCoord().toBigInteger(), this.q.getAffineYCoord().toBigInteger());
|
|
}
|
|
|
|
@Override // org.bouncycastle.jce.interfaces.ECPublicKey
|
|
public ECPoint getQ() {
|
|
return this.ecSpec == null ? this.q.getDetachedPoint() : this.q;
|
|
}
|
|
|
|
@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 "X.509";
|
|
}
|
|
|
|
@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()));
|
|
}
|
|
ECCurve curve = engineGetQ().getCurve();
|
|
return KeyUtil.getEncodedSubjectPublicKeyInfo(new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, x962Parameters), ((ASN1OctetString) (this.ecSpec == null ? new X9ECPoint(curve.createPoint(getQ().getXCoord().toBigInteger(), getQ().getYCoord().toBigInteger(), this.withCompression)) : new X9ECPoint(curve.createPoint(getQ().getAffineXCoord().toBigInteger(), getQ().getAffineYCoord().toBigInteger(), this.withCompression))).toASN1Primitive()).getOctets()));
|
|
}
|
|
|
|
@Override // java.security.Key
|
|
public String getAlgorithm() {
|
|
return this.algorithm;
|
|
}
|
|
|
|
public boolean equals(Object obj) {
|
|
if (!(obj instanceof BCECPublicKey)) {
|
|
return false;
|
|
}
|
|
BCECPublicKey bCECPublicKey = (BCECPublicKey) obj;
|
|
return engineGetQ().equals(bCECPublicKey.engineGetQ()) && engineGetSpec().equals(bCECPublicKey.engineGetSpec());
|
|
}
|
|
|
|
org.bouncycastle.jce.spec.ECParameterSpec engineGetSpec() {
|
|
ECParameterSpec eCParameterSpec = this.ecSpec;
|
|
return eCParameterSpec != null ? EC5Util.convertSpec(eCParameterSpec, this.withCompression) : this.configuration.getEcImplicitlyCa();
|
|
}
|
|
|
|
public ECPoint engineGetQ() {
|
|
return this.q;
|
|
}
|
|
|
|
private void writeObject(ObjectOutputStream objectOutputStream) throws IOException {
|
|
objectOutputStream.defaultWriteObject();
|
|
objectOutputStream.writeObject(getEncoded());
|
|
}
|
|
|
|
private void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
|
|
objectInputStream.defaultReadObject();
|
|
populateFromPubKeyInfo(SubjectPublicKeyInfo.getInstance(ASN1Primitive.fromByteArray((byte[]) objectInputStream.readObject())));
|
|
this.configuration = BouncyCastleProvider.CONFIGURATION;
|
|
}
|
|
|
|
private void populateFromPubKeyInfo(SubjectPublicKeyInfo subjectPublicKeyInfo) {
|
|
byte b;
|
|
X962Parameters x962Parameters = new X962Parameters((ASN1Primitive) subjectPublicKeyInfo.getAlgorithm().getParameters());
|
|
ECCurve curve = EC5Util.getCurve(this.configuration, x962Parameters);
|
|
this.ecSpec = EC5Util.convertToSpec(x962Parameters, curve);
|
|
byte[] bytes = subjectPublicKeyInfo.getPublicKeyData().getBytes();
|
|
ASN1OctetString dEROctetString = new DEROctetString(bytes);
|
|
if (bytes[0] == 4 && bytes[1] == bytes.length - 2 && (((b = bytes[2]) == 2 || b == 3) && new X9IntegerConverter().getByteLength(curve) >= bytes.length - 3)) {
|
|
try {
|
|
dEROctetString = (ASN1OctetString) ASN1Primitive.fromByteArray(bytes);
|
|
} catch (IOException unused) {
|
|
throw new IllegalArgumentException("error recovering public key");
|
|
}
|
|
}
|
|
this.q = new X9ECPoint(curve, dEROctetString).getPoint();
|
|
}
|
|
|
|
private void extractBytes(byte[] bArr, int i, BigInteger bigInteger) {
|
|
byte[] byteArray = bigInteger.toByteArray();
|
|
if (byteArray.length < 32) {
|
|
byte[] bArr2 = new byte[32];
|
|
System.arraycopy(byteArray, 0, bArr2, 32 - byteArray.length, byteArray.length);
|
|
byteArray = bArr2;
|
|
}
|
|
for (int i2 = 0; i2 != 32; i2++) {
|
|
bArr[i + i2] = byteArray[(byteArray.length - 1) - i2];
|
|
}
|
|
}
|
|
|
|
private ECParameterSpec createSpec(EllipticCurve ellipticCurve, ECDomainParameters eCDomainParameters) {
|
|
return new ECParameterSpec(ellipticCurve, new java.security.spec.ECPoint(eCDomainParameters.getG().getAffineXCoord().toBigInteger(), eCDomainParameters.getG().getAffineYCoord().toBigInteger()), eCDomainParameters.getN(), eCDomainParameters.getH().intValue());
|
|
}
|
|
|
|
public BCECPublicKey(ECPublicKey eCPublicKey, ProviderConfiguration providerConfiguration) {
|
|
this.algorithm = "EC";
|
|
this.algorithm = eCPublicKey.getAlgorithm();
|
|
ECParameterSpec params = eCPublicKey.getParams();
|
|
this.ecSpec = params;
|
|
this.q = EC5Util.convertPoint(params, eCPublicKey.getW(), false);
|
|
}
|
|
|
|
public BCECPublicKey(String str, ECPublicKeySpec eCPublicKeySpec, ProviderConfiguration providerConfiguration) {
|
|
ECParameterSpec eCParameterSpec;
|
|
this.algorithm = str;
|
|
this.q = eCPublicKeySpec.getQ();
|
|
if (eCPublicKeySpec.getParams() != null) {
|
|
EllipticCurve convertCurve = EC5Util.convertCurve(eCPublicKeySpec.getParams().getCurve(), eCPublicKeySpec.getParams().getSeed());
|
|
this.q = EC5Util.convertCurve(convertCurve).createPoint(eCPublicKeySpec.getQ().getAffineXCoord().toBigInteger(), eCPublicKeySpec.getQ().getAffineYCoord().toBigInteger());
|
|
eCParameterSpec = EC5Util.convertSpec(convertCurve, eCPublicKeySpec.getParams());
|
|
} else {
|
|
if (this.q.getCurve() == null) {
|
|
this.q = providerConfiguration.getEcImplicitlyCa().getCurve().createPoint(this.q.getXCoord().toBigInteger(), this.q.getYCoord().toBigInteger(), false);
|
|
}
|
|
eCParameterSpec = null;
|
|
}
|
|
this.ecSpec = eCParameterSpec;
|
|
this.configuration = providerConfiguration;
|
|
}
|
|
|
|
public BCECPublicKey(String str, BCECPublicKey bCECPublicKey) {
|
|
this.algorithm = str;
|
|
this.q = bCECPublicKey.q;
|
|
this.ecSpec = bCECPublicKey.ecSpec;
|
|
this.withCompression = bCECPublicKey.withCompression;
|
|
this.configuration = bCECPublicKey.configuration;
|
|
}
|
|
|
|
public BCECPublicKey(String str, ECPublicKeyParameters eCPublicKeyParameters, org.bouncycastle.jce.spec.ECParameterSpec eCParameterSpec, ProviderConfiguration providerConfiguration) {
|
|
this.algorithm = "EC";
|
|
ECDomainParameters parameters = eCPublicKeyParameters.getParameters();
|
|
this.algorithm = str;
|
|
ECParameterSpec createSpec = eCParameterSpec == null ? createSpec(EC5Util.convertCurve(parameters.getCurve(), parameters.getSeed()), parameters) : EC5Util.convertSpec(EC5Util.convertCurve(eCParameterSpec.getCurve(), eCParameterSpec.getSeed()), eCParameterSpec);
|
|
this.ecSpec = createSpec;
|
|
this.q = EC5Util.convertCurve(createSpec.getCurve()).createPoint(eCPublicKeyParameters.getQ().getAffineXCoord().toBigInteger(), eCPublicKeyParameters.getQ().getAffineYCoord().toBigInteger());
|
|
this.configuration = providerConfiguration;
|
|
}
|
|
|
|
public BCECPublicKey(String str, ECPublicKeyParameters eCPublicKeyParameters, ProviderConfiguration providerConfiguration) {
|
|
this.algorithm = str;
|
|
this.q = eCPublicKeyParameters.getQ();
|
|
this.ecSpec = null;
|
|
this.configuration = providerConfiguration;
|
|
}
|
|
|
|
public BCECPublicKey(String str, ECPublicKeyParameters eCPublicKeyParameters, ECParameterSpec eCParameterSpec, ProviderConfiguration providerConfiguration) {
|
|
this.algorithm = "EC";
|
|
ECDomainParameters parameters = eCPublicKeyParameters.getParameters();
|
|
this.algorithm = str;
|
|
this.q = eCPublicKeyParameters.getQ();
|
|
if (eCParameterSpec == null) {
|
|
this.ecSpec = createSpec(EC5Util.convertCurve(parameters.getCurve(), parameters.getSeed()), parameters);
|
|
} else {
|
|
this.ecSpec = eCParameterSpec;
|
|
}
|
|
this.configuration = providerConfiguration;
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
public BCECPublicKey(String str, SubjectPublicKeyInfo subjectPublicKeyInfo, ProviderConfiguration providerConfiguration) {
|
|
this.algorithm = str;
|
|
this.configuration = providerConfiguration;
|
|
populateFromPubKeyInfo(subjectPublicKeyInfo);
|
|
}
|
|
|
|
public BCECPublicKey(String str, java.security.spec.ECPublicKeySpec eCPublicKeySpec, ProviderConfiguration providerConfiguration) {
|
|
this.algorithm = str;
|
|
ECParameterSpec params = eCPublicKeySpec.getParams();
|
|
this.ecSpec = params;
|
|
this.q = EC5Util.convertPoint(params, eCPublicKeySpec.getW(), false);
|
|
this.configuration = providerConfiguration;
|
|
}
|
|
}
|