171 lines
8.0 KiB
Java
171 lines
8.0 KiB
Java
|
package org.bouncycastle.jcajce.provider.asymmetric.gost;
|
||
|
|
||
|
import java.io.IOException;
|
||
|
import java.io.ObjectInputStream;
|
||
|
import java.io.ObjectOutputStream;
|
||
|
import java.math.BigInteger;
|
||
|
import java.util.Enumeration;
|
||
|
import org.bouncycastle.asn1.ASN1Encodable;
|
||
|
import org.bouncycastle.asn1.ASN1Encoding;
|
||
|
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
|
||
|
import org.bouncycastle.asn1.ASN1OctetString;
|
||
|
import org.bouncycastle.asn1.ASN1Sequence;
|
||
|
import org.bouncycastle.asn1.DEROctetString;
|
||
|
import org.bouncycastle.asn1.cryptopro.CryptoProObjectIdentifiers;
|
||
|
import org.bouncycastle.asn1.cryptopro.GOST3410PublicKeyAlgParameters;
|
||
|
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
|
||
|
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
|
||
|
import org.bouncycastle.crypto.params.GOST3410PrivateKeyParameters;
|
||
|
import org.bouncycastle.jcajce.provider.asymmetric.util.PKCS12BagAttributeCarrierImpl;
|
||
|
import org.bouncycastle.jce.interfaces.GOST3410Params;
|
||
|
import org.bouncycastle.jce.interfaces.GOST3410PrivateKey;
|
||
|
import org.bouncycastle.jce.interfaces.PKCS12BagAttributeCarrier;
|
||
|
import org.bouncycastle.jce.spec.GOST3410ParameterSpec;
|
||
|
import org.bouncycastle.jce.spec.GOST3410PrivateKeySpec;
|
||
|
import org.bouncycastle.jce.spec.GOST3410PublicKeyParameterSetSpec;
|
||
|
|
||
|
/* loaded from: classes6.dex */
|
||
|
public class BCGOST3410PrivateKey implements GOST3410PrivateKey, PKCS12BagAttributeCarrier {
|
||
|
static final long serialVersionUID = 8581661527592305464L;
|
||
|
private transient PKCS12BagAttributeCarrier attrCarrier = new PKCS12BagAttributeCarrierImpl();
|
||
|
private transient GOST3410Params gost3410Spec;
|
||
|
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() ^ this.gost3410Spec.hashCode();
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.jce.interfaces.GOST3410PrivateKey
|
||
|
public BigInteger getX() {
|
||
|
return this.x;
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.jce.interfaces.GOST3410Key
|
||
|
public GOST3410Params getParameters() {
|
||
|
return this.gost3410Spec;
|
||
|
}
|
||
|
|
||
|
@Override // java.security.Key
|
||
|
public String getFormat() {
|
||
|
return "PKCS#8";
|
||
|
}
|
||
|
|
||
|
@Override // java.security.Key
|
||
|
public byte[] getEncoded() {
|
||
|
byte[] byteArray = getX().toByteArray();
|
||
|
int length = byteArray[0] == 0 ? byteArray.length - 1 : byteArray.length;
|
||
|
byte[] bArr = new byte[length];
|
||
|
for (int i = 0; i != length; i++) {
|
||
|
bArr[i] = byteArray[(byteArray.length - 1) - i];
|
||
|
}
|
||
|
try {
|
||
|
return (this.gost3410Spec instanceof GOST3410ParameterSpec ? new PrivateKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_94, new GOST3410PublicKeyAlgParameters(new ASN1ObjectIdentifier(this.gost3410Spec.getPublicKeyParamSetOID()), new ASN1ObjectIdentifier(this.gost3410Spec.getDigestParamSetOID()))), new DEROctetString(bArr)) : new PrivateKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_94), new DEROctetString(bArr))).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 "GOST3410";
|
||
|
}
|
||
|
|
||
|
public boolean equals(Object obj) {
|
||
|
if (!(obj instanceof GOST3410PrivateKey)) {
|
||
|
return false;
|
||
|
}
|
||
|
GOST3410PrivateKey gOST3410PrivateKey = (GOST3410PrivateKey) obj;
|
||
|
return getX().equals(gOST3410PrivateKey.getX()) && getParameters().getPublicKeyParameters().equals(gOST3410PrivateKey.getParameters().getPublicKeyParameters()) && getParameters().getDigestParamSetOID().equals(gOST3410PrivateKey.getParameters().getDigestParamSetOID()) && compareObj(getParameters().getEncryptionParamSetOID(), gOST3410PrivateKey.getParameters().getEncryptionParamSetOID());
|
||
|
}
|
||
|
|
||
|
private void writeObject(ObjectOutputStream objectOutputStream) throws IOException {
|
||
|
Object a;
|
||
|
objectOutputStream.defaultWriteObject();
|
||
|
if (this.gost3410Spec.getPublicKeyParamSetOID() != null) {
|
||
|
a = this.gost3410Spec.getPublicKeyParamSetOID();
|
||
|
} else {
|
||
|
objectOutputStream.writeObject(null);
|
||
|
objectOutputStream.writeObject(this.gost3410Spec.getPublicKeyParameters().getP());
|
||
|
objectOutputStream.writeObject(this.gost3410Spec.getPublicKeyParameters().getQ());
|
||
|
a = this.gost3410Spec.getPublicKeyParameters().getA();
|
||
|
}
|
||
|
objectOutputStream.writeObject(a);
|
||
|
objectOutputStream.writeObject(this.gost3410Spec.getDigestParamSetOID());
|
||
|
objectOutputStream.writeObject(this.gost3410Spec.getEncryptionParamSetOID());
|
||
|
}
|
||
|
|
||
|
private void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
|
||
|
objectInputStream.defaultReadObject();
|
||
|
String str = (String) objectInputStream.readObject();
|
||
|
if (str != null) {
|
||
|
this.gost3410Spec = new GOST3410ParameterSpec(str, (String) objectInputStream.readObject(), (String) objectInputStream.readObject());
|
||
|
} else {
|
||
|
this.gost3410Spec = new GOST3410ParameterSpec(new GOST3410PublicKeyParameterSetSpec((BigInteger) objectInputStream.readObject(), (BigInteger) objectInputStream.readObject(), (BigInteger) objectInputStream.readObject()));
|
||
|
objectInputStream.readObject();
|
||
|
objectInputStream.readObject();
|
||
|
}
|
||
|
this.attrCarrier = new PKCS12BagAttributeCarrierImpl();
|
||
|
}
|
||
|
|
||
|
private boolean compareObj(Object obj, Object obj2) {
|
||
|
if (obj == obj2) {
|
||
|
return true;
|
||
|
}
|
||
|
if (obj == null) {
|
||
|
return false;
|
||
|
}
|
||
|
return obj.equals(obj2);
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
public BCGOST3410PrivateKey(GOST3410PrivateKeySpec gOST3410PrivateKeySpec) {
|
||
|
this.x = gOST3410PrivateKeySpec.getX();
|
||
|
this.gost3410Spec = new GOST3410ParameterSpec(new GOST3410PublicKeyParameterSetSpec(gOST3410PrivateKeySpec.getP(), gOST3410PrivateKeySpec.getQ(), gOST3410PrivateKeySpec.getA()));
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
public BCGOST3410PrivateKey(GOST3410PrivateKey gOST3410PrivateKey) {
|
||
|
this.x = gOST3410PrivateKey.getX();
|
||
|
this.gost3410Spec = gOST3410PrivateKey.getParameters();
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
public BCGOST3410PrivateKey(GOST3410PrivateKeyParameters gOST3410PrivateKeyParameters, GOST3410ParameterSpec gOST3410ParameterSpec) {
|
||
|
this.x = gOST3410PrivateKeyParameters.getX();
|
||
|
this.gost3410Spec = gOST3410ParameterSpec;
|
||
|
if (gOST3410ParameterSpec == null) {
|
||
|
throw new IllegalArgumentException("spec is null");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
public BCGOST3410PrivateKey(PrivateKeyInfo privateKeyInfo) throws IOException {
|
||
|
GOST3410PublicKeyAlgParameters gOST3410PublicKeyAlgParameters = new GOST3410PublicKeyAlgParameters((ASN1Sequence) privateKeyInfo.getAlgorithmId().getParameters());
|
||
|
byte[] octets = ASN1OctetString.getInstance(privateKeyInfo.parsePrivateKey()).getOctets();
|
||
|
byte[] bArr = new byte[octets.length];
|
||
|
for (int i = 0; i != octets.length; i++) {
|
||
|
bArr[i] = octets[(octets.length - 1) - i];
|
||
|
}
|
||
|
this.x = new BigInteger(1, bArr);
|
||
|
this.gost3410Spec = GOST3410ParameterSpec.fromPublicKeyAlg(gOST3410PublicKeyAlgParameters);
|
||
|
}
|
||
|
|
||
|
protected BCGOST3410PrivateKey() {
|
||
|
}
|
||
|
}
|