// 
// Decompiled by Procyon v0.6.0
// 

package org.bouncycastle.crypto.util;

import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.asn1.x9.ECNamedCurveTable;
import org.bouncycastle.asn1.ASN1OctetString;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.crypto.params.ECGOST3410Parameters;
import org.bouncycastle.asn1.cryptopro.ECGOST3410NamedCurves;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.cryptopro.GOST3410PublicKeyAlgParameters;
import org.bouncycastle.internal.asn1.rosstandart.RosstandartObjectIdentifiers;
import org.bouncycastle.asn1.cryptopro.CryptoProObjectIdentifiers;
import org.bouncycastle.crypto.params.Ed448PrivateKeyParameters;
import org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters;
import org.bouncycastle.crypto.params.X448PrivateKeyParameters;
import org.bouncycastle.crypto.params.X25519PrivateKeyParameters;
import org.bouncycastle.internal.asn1.edec.EdECObjectIdentifiers;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.params.ECDomainParameters;
import org.bouncycastle.asn1.x9.X9ECParameters;
import org.bouncycastle.crypto.params.ECNamedDomainParameters;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.x9.X962Parameters;
import org.bouncycastle.asn1.sec.ECPrivateKey;
import org.bouncycastle.crypto.params.DSAPrivateKeyParameters;
import org.bouncycastle.crypto.params.DSAParameters;
import org.bouncycastle.asn1.x509.DSAParameter;
import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
import org.bouncycastle.crypto.params.ElGamalPrivateKeyParameters;
import org.bouncycastle.crypto.params.ElGamalParameters;
import org.bouncycastle.internal.asn1.oiw.ElGamalParameter;
import org.bouncycastle.internal.asn1.oiw.OIWObjectIdentifiers;
import org.bouncycastle.crypto.params.DHPrivateKeyParameters;
import java.math.BigInteger;
import org.bouncycastle.crypto.params.DHParameters;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.pkcs.DHParameter;
import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters;
import org.bouncycastle.asn1.pkcs.RSAPrivateKey;
import org.bouncycastle.asn1.x509.X509ObjectIdentifiers;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.asn1.ASN1InputStream;
import java.io.InputStream;
import java.io.IOException;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;

public class PrivateKeyFactory
{
    public static AsymmetricKeyParameter createKey(final byte[] array) throws IOException {
        if (array == null) {
            throw new IllegalArgumentException("privateKeyInfoData array null");
        }
        if (array.length == 0) {
            throw new IllegalArgumentException("privateKeyInfoData array empty");
        }
        return createKey(PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(array)));
    }
    
    public static AsymmetricKeyParameter createKey(final InputStream inputStream) throws IOException {
        return createKey(PrivateKeyInfo.getInstance(new ASN1InputStream(inputStream).readObject()));
    }
    
    public static AsymmetricKeyParameter createKey(final PrivateKeyInfo privateKeyInfo) throws IOException {
        if (privateKeyInfo == null) {
            throw new IllegalArgumentException("keyInfo argument null");
        }
        final AlgorithmIdentifier privateKeyAlgorithm = privateKeyInfo.getPrivateKeyAlgorithm();
        final ASN1ObjectIdentifier algorithm = privateKeyAlgorithm.getAlgorithm();
        if (algorithm.equals(PKCSObjectIdentifiers.rsaEncryption) || algorithm.equals(PKCSObjectIdentifiers.id_RSASSA_PSS) || algorithm.equals(X509ObjectIdentifiers.id_ea_rsa)) {
            final RSAPrivateKey instance = RSAPrivateKey.getInstance(privateKeyInfo.parsePrivateKey());
            return new RSAPrivateCrtKeyParameters(instance.getModulus(), instance.getPublicExponent(), instance.getPrivateExponent(), instance.getPrime1(), instance.getPrime2(), instance.getExponent1(), instance.getExponent2(), instance.getCoefficient());
        }
        if (algorithm.equals(PKCSObjectIdentifiers.dhKeyAgreement)) {
            final DHParameter instance2 = DHParameter.getInstance(privateKeyAlgorithm.getParameters());
            final ASN1Integer asn1Integer = (ASN1Integer)privateKeyInfo.parsePrivateKey();
            final BigInteger l = instance2.getL();
            return new DHPrivateKeyParameters(asn1Integer.getValue(), new DHParameters(instance2.getP(), instance2.getG(), null, (l == null) ? 0 : l.intValue()));
        }
        if (algorithm.equals(OIWObjectIdentifiers.elGamalAlgorithm)) {
            final ElGamalParameter instance3 = ElGamalParameter.getInstance(privateKeyAlgorithm.getParameters());
            return new ElGamalPrivateKeyParameters(((ASN1Integer)privateKeyInfo.parsePrivateKey()).getValue(), new ElGamalParameters(instance3.getP(), instance3.getG()));
        }
        if (algorithm.equals(X9ObjectIdentifiers.id_dsa)) {
            final ASN1Integer asn1Integer2 = (ASN1Integer)privateKeyInfo.parsePrivateKey();
            final ASN1Encodable parameters = privateKeyAlgorithm.getParameters();
            DSAParameters dsaParameters = null;
            if (parameters != null) {
                final DSAParameter instance4 = DSAParameter.getInstance(parameters.toASN1Primitive());
                dsaParameters = new DSAParameters(instance4.getP(), instance4.getQ(), instance4.getG());
            }
            return new DSAPrivateKeyParameters(asn1Integer2.getValue(), dsaParameters);
        }
        if (algorithm.equals(X9ObjectIdentifiers.id_ecPublicKey)) {
            final ECPrivateKey instance5 = ECPrivateKey.getInstance(privateKeyInfo.parsePrivateKey());
            final X962Parameters instance6 = X962Parameters.getInstance(privateKeyAlgorithm.getParameters().toASN1Primitive());
            ECDomainParameters lookup;
            if (instance6.isNamedCurve()) {
                lookup = ECNamedDomainParameters.lookup(ASN1ObjectIdentifier.getInstance(instance6.getParameters()));
            }
            else {
                lookup = new ECDomainParameters(X9ECParameters.getInstance(instance6.getParameters()));
            }
            return new ECPrivateKeyParameters(instance5.getKey(), lookup);
        }
        if (algorithm.equals(EdECObjectIdentifiers.id_X25519)) {
            if (32 == privateKeyInfo.getPrivateKeyLength()) {
                return new X25519PrivateKeyParameters(privateKeyInfo.getPrivateKey().getOctets());
            }
            return new X25519PrivateKeyParameters(getRawKey(privateKeyInfo));
        }
        else if (algorithm.equals(EdECObjectIdentifiers.id_X448)) {
            if (56 == privateKeyInfo.getPrivateKeyLength()) {
                return new X448PrivateKeyParameters(privateKeyInfo.getPrivateKey().getOctets());
            }
            return new X448PrivateKeyParameters(getRawKey(privateKeyInfo));
        }
        else {
            if (algorithm.equals(EdECObjectIdentifiers.id_Ed25519)) {
                return new Ed25519PrivateKeyParameters(getRawKey(privateKeyInfo));
            }
            if (algorithm.equals(EdECObjectIdentifiers.id_Ed448)) {
                return new Ed448PrivateKeyParameters(getRawKey(privateKeyInfo));
            }
            if (algorithm.equals(CryptoProObjectIdentifiers.gostR3410_2001) || algorithm.equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512) || algorithm.equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256)) {
                final ASN1Encodable parameters2 = privateKeyAlgorithm.getParameters();
                final GOST3410PublicKeyAlgParameters instance7 = GOST3410PublicKeyAlgParameters.getInstance(parameters2);
                final ASN1Primitive asn1Primitive = parameters2.toASN1Primitive();
                ECGOST3410Parameters ecgost3410Parameters;
                BigInteger bigInteger;
                if (asn1Primitive instanceof ASN1Sequence && (ASN1Sequence.getInstance(asn1Primitive).size() == 2 || ASN1Sequence.getInstance(asn1Primitive).size() == 3)) {
                    ecgost3410Parameters = new ECGOST3410Parameters(new ECNamedDomainParameters(instance7.getPublicKeyParamSet(), ECGOST3410NamedCurves.getByOIDX9(instance7.getPublicKeyParamSet())), instance7.getPublicKeyParamSet(), instance7.getDigestParamSet(), instance7.getEncryptionParamSet());
                    final int privateKeyLength = privateKeyInfo.getPrivateKeyLength();
                    if (privateKeyLength == 32 || privateKeyLength == 64) {
                        bigInteger = new BigInteger(1, Arrays.reverse(privateKeyInfo.getPrivateKey().getOctets()));
                    }
                    else {
                        final ASN1Encodable privateKey = privateKeyInfo.parsePrivateKey();
                        if (privateKey instanceof ASN1Integer) {
                            bigInteger = ASN1Integer.getInstance(privateKey).getPositiveValue();
                        }
                        else {
                            bigInteger = new BigInteger(1, Arrays.reverse(ASN1OctetString.getInstance(privateKey).getOctets()));
                        }
                    }
                }
                else {
                    final X962Parameters instance8 = X962Parameters.getInstance(privateKeyAlgorithm.getParameters());
                    if (instance8.isNamedCurve()) {
                        final ASN1ObjectIdentifier instance9 = ASN1ObjectIdentifier.getInstance(instance8.getParameters());
                        ecgost3410Parameters = new ECGOST3410Parameters(new ECNamedDomainParameters(instance9, ECNamedCurveTable.getByOID(instance9)), instance7.getPublicKeyParamSet(), instance7.getDigestParamSet(), instance7.getEncryptionParamSet());
                    }
                    else if (instance8.isImplicitlyCA()) {
                        ecgost3410Parameters = null;
                    }
                    else {
                        ecgost3410Parameters = new ECGOST3410Parameters(new ECNamedDomainParameters(algorithm, X9ECParameters.getInstance(instance8.getParameters())), instance7.getPublicKeyParamSet(), instance7.getDigestParamSet(), instance7.getEncryptionParamSet());
                    }
                    final ASN1Encodable privateKey2 = privateKeyInfo.parsePrivateKey();
                    if (privateKey2 instanceof ASN1Integer) {
                        bigInteger = ASN1Integer.getInstance(privateKey2).getValue();
                    }
                    else {
                        bigInteger = ECPrivateKey.getInstance(privateKey2).getKey();
                    }
                }
                return new ECPrivateKeyParameters(bigInteger, new ECGOST3410Parameters(ecgost3410Parameters, instance7.getPublicKeyParamSet(), instance7.getDigestParamSet(), instance7.getEncryptionParamSet()));
            }
            throw new RuntimeException("algorithm identifier in private key not recognised");
        }
    }
    
    private static byte[] getRawKey(final PrivateKeyInfo privateKeyInfo) throws IOException {
        return ASN1OctetString.getInstance(privateKeyInfo.parsePrivateKey()).getOctets();
    }
}
