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

package org.bouncycastle.jcajce.provider.asymmetric.compositesignatures;

import org.bouncycastle.internal.asn1.iana.IANAObjectIdentifiers;
import java.util.HashMap;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.asn1.teletrust.TeleTrusTObjectIdentifiers;
import org.bouncycastle.asn1.x9.X962Parameters;
import org.bouncycastle.asn1.sec.SECObjectIdentifiers;
import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
import org.bouncycastle.internal.asn1.edec.EdECObjectIdentifiers;
import org.bouncycastle.asn1.bc.BCObjectIdentifiers;
import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
import java.security.NoSuchProviderException;
import java.util.Collections;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import org.bouncycastle.asn1.DERBitString;
import org.bouncycastle.asn1.ASN1BitString;
import org.bouncycastle.jcajce.CompositePublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.util.List;
import java.security.GeneralSecurityException;
import org.bouncycastle.util.Exceptions;
import org.bouncycastle.asn1.DERSequence;
import java.security.KeyFactory;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.asn1.ASN1OctetString;
import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.jcajce.CompositePrivateKey;
import java.security.spec.KeySpec;
import java.security.spec.PKCS8EncodedKeySpec;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.internal.asn1.misc.MiscObjectIdentifiers;
import java.io.IOException;
import java.security.InvalidKeyException;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import java.security.PublicKey;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import java.security.PrivateKey;
import org.bouncycastle.jcajce.util.BCJcaJceHelper;
import java.security.Key;
import org.bouncycastle.jcajce.util.JcaJceHelper;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import java.util.Map;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.jcajce.provider.util.AsymmetricKeyInfoConverter;
import org.bouncycastle.jcajce.provider.asymmetric.util.BaseKeyFactorySpi;

public class KeyFactorySpi extends BaseKeyFactorySpi implements AsymmetricKeyInfoConverter
{
    private static final AlgorithmIdentifier mlDsa44;
    private static final AlgorithmIdentifier mlDsa65;
    private static final AlgorithmIdentifier mlDsa87;
    private static final AlgorithmIdentifier falcon512Identifier;
    private static final AlgorithmIdentifier ed25519;
    private static final AlgorithmIdentifier ecDsaP256;
    private static final AlgorithmIdentifier ecDsaBrainpoolP256r1;
    private static final AlgorithmIdentifier rsa;
    private static final AlgorithmIdentifier ed448;
    private static final AlgorithmIdentifier ecDsaP384;
    private static final AlgorithmIdentifier ecDsaP521;
    private static final AlgorithmIdentifier ecDsaBrainpoolP384r1;
    private static Map<ASN1ObjectIdentifier, AlgorithmIdentifier[]> pairings;
    private static Map<ASN1ObjectIdentifier, int[]> componentKeySizes;
    private JcaJceHelper helper;
    
    public KeyFactorySpi() {
        this(null);
    }
    
    public KeyFactorySpi(final JcaJceHelper helper) {
        this.helper = helper;
    }
    
    @Override
    protected Key engineTranslateKey(final Key key) throws InvalidKeyException {
        if (this.helper == null) {
            this.helper = new BCJcaJceHelper();
        }
        try {
            if (key instanceof PrivateKey) {
                return this.generatePrivate(PrivateKeyInfo.getInstance(key.getEncoded()));
            }
            if (key instanceof PublicKey) {
                return this.generatePublic(SubjectPublicKeyInfo.getInstance(key.getEncoded()));
            }
        }
        catch (final IOException ex) {
            throw new InvalidKeyException("Key could not be parsed: " + ex.getMessage());
        }
        throw new InvalidKeyException("Key not recognized");
    }
    
    @Override
    public PrivateKey generatePrivate(final PrivateKeyInfo privateKeyInfo) throws IOException {
        if (this.helper == null) {
            this.helper = new BCJcaJceHelper();
        }
        final ASN1ObjectIdentifier algorithm = privateKeyInfo.getPrivateKeyAlgorithm().getAlgorithm();
        if (MiscObjectIdentifiers.id_alg_composite.equals(algorithm) || MiscObjectIdentifiers.id_composite_key.equals(algorithm)) {
            final ASN1Sequence instance = ASN1Sequence.getInstance(privateKeyInfo.parsePrivateKey());
            final PrivateKey[] array = new PrivateKey[instance.size()];
            for (int i = 0; i != instance.size(); ++i) {
                final PrivateKeyInfo instance2 = PrivateKeyInfo.getInstance(ASN1Sequence.getInstance(instance.getObjectAt(i)));
                try {
                    array[i] = this.helper.createKeyFactory(instance2.getPrivateKeyAlgorithm().getAlgorithm().getId()).generatePrivate(new PKCS8EncodedKeySpec(instance2.getEncoded()));
                }
                catch (final Exception cause) {
                    throw new IOException("cannot decode generic composite: " + cause.getMessage(), cause);
                }
            }
            return new CompositePrivateKey(array);
        }
        try {
            final List<KeyFactory> keyFactoriesFromIdentifier = this.getKeyFactoriesFromIdentifier(algorithm);
            final ASN1EncodableVector asn1EncodableVector = new ASN1EncodableVector();
            byte[] array2;
            try {
                array2 = ASN1OctetString.getInstance(privateKeyInfo.parsePrivateKey()).getOctets();
            }
            catch (final Exception ex) {
                array2 = privateKeyInfo.getPrivateKey().getOctets();
            }
            asn1EncodableVector.add(new DEROctetString(Arrays.copyOfRange(array2, 0, 32)));
            final String algorithm2 = keyFactoriesFromIdentifier.get(1).getAlgorithm();
            if (algorithm2.equals("Ed25519")) {
                asn1EncodableVector.add(new DEROctetString(Arrays.concatenate(new byte[] { 4, 32 }, Arrays.copyOfRange(array2, 32, array2.length))));
            }
            else if (algorithm2.equals("Ed448")) {
                asn1EncodableVector.add(new DEROctetString(Arrays.concatenate(new byte[] { 4, 57 }, Arrays.copyOfRange(array2, 32, array2.length))));
            }
            else {
                asn1EncodableVector.add(new DEROctetString(Arrays.copyOfRange(array2, 32, array2.length)));
            }
            final DERSequence derSequence = new DERSequence(asn1EncodableVector);
            final PrivateKey[] array3 = new PrivateKey[derSequence.size()];
            final AlgorithmIdentifier[] array4 = KeyFactorySpi.pairings.get(algorithm);
            for (int j = 0; j < derSequence.size(); ++j) {
                if (derSequence.getObjectAt(j) instanceof ASN1OctetString) {
                    final ASN1EncodableVector asn1EncodableVector2 = new ASN1EncodableVector(3);
                    asn1EncodableVector2.add(privateKeyInfo.getVersion());
                    asn1EncodableVector2.add(array4[j]);
                    asn1EncodableVector2.add(derSequence.getObjectAt(j));
                    array3[j] = keyFactoriesFromIdentifier.get(j).generatePrivate(new PKCS8EncodedKeySpec(PrivateKeyInfo.getInstance(new DERSequence(asn1EncodableVector2)).getEncoded()));
                }
                else {
                    array3[j] = keyFactoriesFromIdentifier.get(j).generatePrivate(new PKCS8EncodedKeySpec(PrivateKeyInfo.getInstance(ASN1Sequence.getInstance(derSequence.getObjectAt(j))).getEncoded()));
                }
            }
            return new CompositePrivateKey(algorithm, array3);
        }
        catch (final GeneralSecurityException ex2) {
            throw Exceptions.ioException(ex2.getMessage(), ex2);
        }
    }
    
    @Override
    public PublicKey generatePublic(final SubjectPublicKeyInfo subjectPublicKeyInfo) throws IOException {
        if (this.helper == null) {
            this.helper = new BCJcaJceHelper();
        }
        final ASN1ObjectIdentifier algorithm = subjectPublicKeyInfo.getAlgorithm().getAlgorithm();
        ASN1Sequence instance = null;
        byte[][] split = new byte[2][];
        try {
            instance = ASN1Sequence.getInstance(subjectPublicKeyInfo.getPublicKeyData().getBytes());
        }
        catch (final Exception ex) {
            split = this.split(algorithm, subjectPublicKeyInfo.getPublicKeyData());
        }
        if (MiscObjectIdentifiers.id_alg_composite.equals(algorithm) || MiscObjectIdentifiers.id_composite_key.equals(algorithm)) {
            final ASN1Sequence instance2 = ASN1Sequence.getInstance(subjectPublicKeyInfo.getPublicKeyData().getBytes());
            final PublicKey[] array = new PublicKey[instance2.size()];
            for (int i = 0; i != instance2.size(); ++i) {
                final SubjectPublicKeyInfo instance3 = SubjectPublicKeyInfo.getInstance(instance2.getObjectAt(i));
                try {
                    array[i] = this.helper.createKeyFactory(instance3.getAlgorithm().getAlgorithm().getId()).generatePublic(new X509EncodedKeySpec(instance3.getEncoded()));
                }
                catch (final Exception cause) {
                    throw new IOException("cannot decode generic composite: " + cause.getMessage(), cause);
                }
            }
            return new CompositePublicKey(array);
        }
        try {
            final int n = (instance == null) ? split.length : instance.size();
            final List<KeyFactory> keyFactoriesFromIdentifier = this.getKeyFactoriesFromIdentifier(algorithm);
            final ASN1BitString[] array2 = new ASN1BitString[n];
            for (int j = 0; j < n; ++j) {
                if (instance != null) {
                    if (instance.getObjectAt(j) instanceof DEROctetString) {
                        array2[j] = new DERBitString(((DEROctetString)instance.getObjectAt(j)).getOctets());
                    }
                    else {
                        array2[j] = (DERBitString)instance.getObjectAt(j);
                    }
                }
                else {
                    array2[j] = new DERBitString(split[j]);
                }
            }
            final X509EncodedKeySpec[] keysSpecs = this.getKeysSpecs(algorithm, array2);
            final PublicKey[] array3 = new PublicKey[n];
            for (int k = 0; k < n; ++k) {
                array3[k] = keyFactoriesFromIdentifier.get(k).generatePublic(keysSpecs[k]);
            }
            return new CompositePublicKey(algorithm, array3);
        }
        catch (final GeneralSecurityException ex2) {
            throw Exceptions.ioException(ex2.getMessage(), ex2);
        }
    }
    
    byte[][] split(final ASN1ObjectIdentifier asn1ObjectIdentifier, final ASN1BitString asn1BitString) {
        final int[] array = KeyFactorySpi.componentKeySizes.get(asn1ObjectIdentifier);
        final byte[] octets = asn1BitString.getOctets();
        final byte[][] array2 = { new byte[array[0]], new byte[octets.length - array[0]] };
        System.arraycopy(octets, 0, array2[0], 0, array[0]);
        System.arraycopy(octets, array[0], array2[1], 0, array2[1].length);
        return array2;
    }
    
    private List<KeyFactory> getKeyFactoriesFromIdentifier(final ASN1ObjectIdentifier asn1ObjectIdentifier) throws NoSuchAlgorithmException, NoSuchProviderException {
        final ArrayList list = new ArrayList();
        final ArrayList list2 = new ArrayList();
        final String[] pairing = CompositeIndex.getPairing(asn1ObjectIdentifier);
        if (pairing == null) {
            throw new NoSuchAlgorithmException("Cannot create KeyFactories. Unsupported algorithm identifier.");
        }
        list.add(this.helper.createKeyFactory(CompositeIndex.getBaseName(pairing[0])));
        list.add(this.helper.createKeyFactory(CompositeIndex.getBaseName(pairing[1])));
        return (List<KeyFactory>)Collections.unmodifiableList((List<?>)list);
    }
    
    private X509EncodedKeySpec[] getKeysSpecs(final ASN1ObjectIdentifier asn1ObjectIdentifier, final ASN1BitString[] array) throws IOException {
        final X509EncodedKeySpec[] array2 = new X509EncodedKeySpec[array.length];
        final SubjectPublicKeyInfo[] array3 = new SubjectPublicKeyInfo[array.length];
        final AlgorithmIdentifier[] array4 = KeyFactorySpi.pairings.get(asn1ObjectIdentifier);
        if (array4 == null) {
            throw new IOException("Cannot create key specs. Unsupported algorithm identifier.");
        }
        array3[0] = new SubjectPublicKeyInfo(array4[0], array[0]);
        array3[1] = new SubjectPublicKeyInfo(array4[1], array[1]);
        array2[0] = new X509EncodedKeySpec(array3[0].getEncoded());
        array2[1] = new X509EncodedKeySpec(array3[1].getEncoded());
        return array2;
    }
    
    static {
        mlDsa44 = new AlgorithmIdentifier(NISTObjectIdentifiers.id_ml_dsa_44);
        mlDsa65 = new AlgorithmIdentifier(NISTObjectIdentifiers.id_ml_dsa_65);
        mlDsa87 = new AlgorithmIdentifier(NISTObjectIdentifiers.id_ml_dsa_87);
        falcon512Identifier = new AlgorithmIdentifier(BCObjectIdentifiers.falcon_512);
        ed25519 = new AlgorithmIdentifier(EdECObjectIdentifiers.id_Ed25519);
        ecDsaP256 = new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, new X962Parameters(SECObjectIdentifiers.secp256r1));
        ecDsaBrainpoolP256r1 = new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, new X962Parameters(TeleTrusTObjectIdentifiers.brainpoolP256r1));
        rsa = new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption);
        ed448 = new AlgorithmIdentifier(EdECObjectIdentifiers.id_Ed448);
        ecDsaP384 = new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, new X962Parameters(SECObjectIdentifiers.secp384r1));
        ecDsaP521 = new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, new X962Parameters(SECObjectIdentifiers.secp521r1));
        ecDsaBrainpoolP384r1 = new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, new X962Parameters(TeleTrusTObjectIdentifiers.brainpoolP384r1));
        KeyFactorySpi.pairings = new HashMap<ASN1ObjectIdentifier, AlgorithmIdentifier[]>();
        KeyFactorySpi.componentKeySizes = new HashMap<ASN1ObjectIdentifier, int[]>();
        KeyFactorySpi.pairings.put(IANAObjectIdentifiers.id_MLDSA44_RSA2048_PSS_SHA256, new AlgorithmIdentifier[] { KeyFactorySpi.mlDsa44, KeyFactorySpi.rsa });
        KeyFactorySpi.pairings.put(IANAObjectIdentifiers.id_MLDSA44_RSA2048_PKCS15_SHA256, new AlgorithmIdentifier[] { KeyFactorySpi.mlDsa44, KeyFactorySpi.rsa });
        KeyFactorySpi.pairings.put(IANAObjectIdentifiers.id_MLDSA44_Ed25519_SHA512, new AlgorithmIdentifier[] { KeyFactorySpi.mlDsa44, KeyFactorySpi.ed25519 });
        KeyFactorySpi.pairings.put(IANAObjectIdentifiers.id_MLDSA44_ECDSA_P256_SHA256, new AlgorithmIdentifier[] { KeyFactorySpi.mlDsa44, KeyFactorySpi.ecDsaP256 });
        KeyFactorySpi.pairings.put(IANAObjectIdentifiers.id_MLDSA65_RSA3072_PSS_SHA512, new AlgorithmIdentifier[] { KeyFactorySpi.mlDsa65, KeyFactorySpi.rsa });
        KeyFactorySpi.pairings.put(IANAObjectIdentifiers.id_MLDSA65_RSA3072_PKCS15_SHA512, new AlgorithmIdentifier[] { KeyFactorySpi.mlDsa65, KeyFactorySpi.rsa });
        KeyFactorySpi.pairings.put(IANAObjectIdentifiers.id_MLDSA65_RSA4096_PSS_SHA512, new AlgorithmIdentifier[] { KeyFactorySpi.mlDsa65, KeyFactorySpi.rsa });
        KeyFactorySpi.pairings.put(IANAObjectIdentifiers.id_MLDSA65_RSA4096_PKCS15_SHA512, new AlgorithmIdentifier[] { KeyFactorySpi.mlDsa65, KeyFactorySpi.rsa });
        KeyFactorySpi.pairings.put(IANAObjectIdentifiers.id_MLDSA65_ECDSA_P256_SHA512, new AlgorithmIdentifier[] { KeyFactorySpi.mlDsa65, KeyFactorySpi.ecDsaP256 });
        KeyFactorySpi.pairings.put(IANAObjectIdentifiers.id_MLDSA65_ECDSA_P384_SHA512, new AlgorithmIdentifier[] { KeyFactorySpi.mlDsa65, KeyFactorySpi.ecDsaP384 });
        KeyFactorySpi.pairings.put(IANAObjectIdentifiers.id_MLDSA65_ECDSA_brainpoolP256r1_SHA512, new AlgorithmIdentifier[] { KeyFactorySpi.mlDsa65, KeyFactorySpi.ecDsaBrainpoolP256r1 });
        KeyFactorySpi.pairings.put(IANAObjectIdentifiers.id_MLDSA65_Ed25519_SHA512, new AlgorithmIdentifier[] { KeyFactorySpi.mlDsa65, KeyFactorySpi.ed25519 });
        KeyFactorySpi.pairings.put(IANAObjectIdentifiers.id_MLDSA87_ECDSA_P384_SHA512, new AlgorithmIdentifier[] { KeyFactorySpi.mlDsa87, KeyFactorySpi.ecDsaP384 });
        KeyFactorySpi.pairings.put(IANAObjectIdentifiers.id_MLDSA87_ECDSA_brainpoolP384r1_SHA512, new AlgorithmIdentifier[] { KeyFactorySpi.mlDsa87, KeyFactorySpi.ecDsaBrainpoolP384r1 });
        KeyFactorySpi.pairings.put(IANAObjectIdentifiers.id_MLDSA87_Ed448_SHAKE256, new AlgorithmIdentifier[] { KeyFactorySpi.mlDsa87, KeyFactorySpi.ed448 });
        KeyFactorySpi.pairings.put(IANAObjectIdentifiers.id_MLDSA87_RSA4096_PSS_SHA512, new AlgorithmIdentifier[] { KeyFactorySpi.mlDsa87, KeyFactorySpi.rsa });
        KeyFactorySpi.pairings.put(IANAObjectIdentifiers.id_MLDSA87_ECDSA_P521_SHA512, new AlgorithmIdentifier[] { KeyFactorySpi.mlDsa87, KeyFactorySpi.ecDsaP521 });
        KeyFactorySpi.pairings.put(IANAObjectIdentifiers.id_MLDSA87_RSA3072_PSS_SHA512, new AlgorithmIdentifier[] { KeyFactorySpi.mlDsa87, KeyFactorySpi.rsa });
        KeyFactorySpi.componentKeySizes.put(IANAObjectIdentifiers.id_MLDSA44_RSA2048_PSS_SHA256, new int[] { 1312, 268 });
        KeyFactorySpi.componentKeySizes.put(IANAObjectIdentifiers.id_MLDSA44_RSA2048_PKCS15_SHA256, new int[] { 1312, 284 });
        KeyFactorySpi.componentKeySizes.put(IANAObjectIdentifiers.id_MLDSA44_Ed25519_SHA512, new int[] { 1312, 32 });
        KeyFactorySpi.componentKeySizes.put(IANAObjectIdentifiers.id_MLDSA44_ECDSA_P256_SHA256, new int[] { 1312, 76 });
        KeyFactorySpi.componentKeySizes.put(IANAObjectIdentifiers.id_MLDSA65_RSA3072_PSS_SHA512, new int[] { 1952, 256 });
        KeyFactorySpi.componentKeySizes.put(IANAObjectIdentifiers.id_MLDSA65_RSA3072_PKCS15_SHA512, new int[] { 1952, 256 });
        KeyFactorySpi.componentKeySizes.put(IANAObjectIdentifiers.id_MLDSA65_RSA4096_PSS_SHA512, new int[] { 1952, 542 });
        KeyFactorySpi.componentKeySizes.put(IANAObjectIdentifiers.id_MLDSA65_RSA4096_PKCS15_SHA512, new int[] { 1952, 542 });
        KeyFactorySpi.componentKeySizes.put(IANAObjectIdentifiers.id_MLDSA65_ECDSA_P256_SHA512, new int[] { 1952, 76 });
        KeyFactorySpi.componentKeySizes.put(IANAObjectIdentifiers.id_MLDSA65_ECDSA_P384_SHA512, new int[] { 1952, 87 });
        KeyFactorySpi.componentKeySizes.put(IANAObjectIdentifiers.id_MLDSA65_ECDSA_brainpoolP256r1_SHA512, new int[] { 1952, 76 });
        KeyFactorySpi.componentKeySizes.put(IANAObjectIdentifiers.id_MLDSA65_Ed25519_SHA512, new int[] { 1952, 32 });
        KeyFactorySpi.componentKeySizes.put(IANAObjectIdentifiers.id_MLDSA87_ECDSA_P384_SHA512, new int[] { 2592, 87 });
        KeyFactorySpi.componentKeySizes.put(IANAObjectIdentifiers.id_MLDSA87_ECDSA_brainpoolP384r1_SHA512, new int[] { 2592, 87 });
        KeyFactorySpi.componentKeySizes.put(IANAObjectIdentifiers.id_MLDSA87_Ed448_SHAKE256, new int[] { 2592, 57 });
        KeyFactorySpi.componentKeySizes.put(IANAObjectIdentifiers.id_MLDSA87_RSA4096_PSS_SHA512, new int[] { 2592, 542 });
        KeyFactorySpi.componentKeySizes.put(IANAObjectIdentifiers.id_MLDSA87_RSA3072_PSS_SHA512, new int[] { 2592, 256 });
        KeyFactorySpi.componentKeySizes.put(IANAObjectIdentifiers.id_MLDSA87_ECDSA_P521_SHA512, new int[] { 2592, 93 });
    }
}
