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

package org.bouncycastle.pqc.jcajce.provider.util;

import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import java.security.spec.X509EncodedKeySpec;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.PrivateKey;
import java.security.spec.KeySpec;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import java.util.Set;
import org.bouncycastle.jcajce.provider.util.AsymmetricKeyInfoConverter;
import java.security.KeyFactorySpi;

public abstract class BaseKeyFactorySpi extends KeyFactorySpi implements AsymmetricKeyInfoConverter
{
    private final Set<ASN1ObjectIdentifier> keyOids;
    private final ASN1ObjectIdentifier keyOid;
    
    protected BaseKeyFactorySpi(final Set<ASN1ObjectIdentifier> keyOids) {
        this.keyOid = null;
        this.keyOids = keyOids;
    }
    
    protected BaseKeyFactorySpi(final ASN1ObjectIdentifier keyOid) {
        this.keyOid = keyOid;
        this.keyOids = null;
    }
    
    public PrivateKey engineGeneratePrivate(final KeySpec keySpec) throws InvalidKeySpecException {
        if (keySpec instanceof PKCS8EncodedKeySpec) {
            final byte[] encoded = ((PKCS8EncodedKeySpec)keySpec).getEncoded();
            try {
                final PrivateKeyInfo instance = PrivateKeyInfo.getInstance(encoded);
                this.checkAlgorithm(instance.getPrivateKeyAlgorithm().getAlgorithm());
                return this.generatePrivate(instance);
            }
            catch (final InvalidKeySpecException ex) {
                throw ex;
            }
            catch (final IllegalStateException ex2) {
                throw new InvalidKeySpecException(ex2.getMessage());
            }
            catch (final Exception ex3) {
                throw new InvalidKeySpecException(ex3.toString());
            }
        }
        throw new InvalidKeySpecException("Unsupported key specification: " + keySpec.getClass() + ".");
    }
    
    public PublicKey engineGeneratePublic(final KeySpec obj) throws InvalidKeySpecException {
        if (obj instanceof X509EncodedKeySpec) {
            final byte[] encoded = ((X509EncodedKeySpec)obj).getEncoded();
            try {
                final SubjectPublicKeyInfo instance = SubjectPublicKeyInfo.getInstance(encoded);
                this.checkAlgorithm(instance.getAlgorithm().getAlgorithm());
                return this.generatePublic(instance);
            }
            catch (final InvalidKeySpecException ex) {
                throw ex;
            }
            catch (final Exception ex2) {
                throw new InvalidKeySpecException(ex2.toString());
            }
        }
        throw new InvalidKeySpecException("Unknown key specification: " + obj + ".");
    }
    
    private void checkAlgorithm(final ASN1ObjectIdentifier asn1ObjectIdentifier) throws InvalidKeySpecException {
        if (this.keyOid != null) {
            if (!this.keyOid.equals(asn1ObjectIdentifier)) {
                throw new InvalidKeySpecException("incorrect algorithm OID for key: " + asn1ObjectIdentifier);
            }
        }
        else if (!this.keyOids.contains(asn1ObjectIdentifier)) {
            throw new InvalidKeySpecException("incorrect algorithm OID for key: " + asn1ObjectIdentifier);
        }
    }
}
