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

package org.bouncycastle.jcajce.provider.asymmetric;

import org.bouncycastle.asn1.bc.BCObjectIdentifiers;
import org.bouncycastle.jcajce.provider.util.AsymmetricAlgorithmProvider;
import java.security.InvalidKeyException;
import java.security.Key;
import org.bouncycastle.jcajce.provider.asymmetric.util.BaseKeyFactorySpi;
import org.bouncycastle.jcajce.ExternalPublicKey;
import org.bouncycastle.asn1.bc.ExternalValue;
import java.security.PublicKey;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import java.io.IOException;
import java.security.PrivateKey;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.jcajce.provider.config.ConfigurableProvider;
import java.util.HashMap;
import org.bouncycastle.jcajce.provider.util.AsymmetricKeyInfoConverter;
import java.util.Map;

public class EXTERNAL
{
    private static final String PREFIX = "org.bouncycastle.jcajce.provider.asymmetric.EXTERNAL";
    private static final Map<String, String> externalAttributes;
    private static AsymmetricKeyInfoConverter baseConverter;
    
    static {
        (externalAttributes = new HashMap<String, String>()).put("SupportedKeyClasses", "org.bouncycastle.jcajce.ExternalPublicKey");
        EXTERNAL.externalAttributes.put("SupportedKeyFormats", "X.509");
    }
    
    private static class ExternalKeyInfoConverter implements AsymmetricKeyInfoConverter
    {
        private final ConfigurableProvider provider;
        
        public ExternalKeyInfoConverter(final ConfigurableProvider provider) {
            this.provider = provider;
        }
        
        @Override
        public PrivateKey generatePrivate(final PrivateKeyInfo privateKeyInfo) throws IOException {
            throw new UnsupportedOperationException("no support for private key");
        }
        
        @Override
        public PublicKey generatePublic(final SubjectPublicKeyInfo subjectPublicKeyInfo) throws IOException {
            return new ExternalPublicKey(ExternalValue.getInstance(subjectPublicKeyInfo.parsePublicKey()));
        }
    }
    
    public static class KeyFactory extends BaseKeyFactorySpi
    {
        @Override
        protected Key engineTranslateKey(final Key key) throws InvalidKeyException {
            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 {
            return EXTERNAL.baseConverter.generatePrivate(privateKeyInfo);
        }
        
        @Override
        public PublicKey generatePublic(final SubjectPublicKeyInfo subjectPublicKeyInfo) throws IOException {
            return EXTERNAL.baseConverter.generatePublic(subjectPublicKeyInfo);
        }
    }
    
    public static class Mappings extends AsymmetricAlgorithmProvider
    {
        @Override
        public void configure(final ConfigurableProvider configurableProvider) {
            configurableProvider.addAlgorithm("KeyFactory.EXTERNAL", "org.bouncycastle.jcajce.provider.asymmetric.EXTERNAL$KeyFactory");
            configurableProvider.addAlgorithm("KeyFactory." + BCObjectIdentifiers.external_value, "org.bouncycastle.jcajce.provider.asymmetric.EXTERNAL$KeyFactory");
            configurableProvider.addAlgorithm("KeyFactory.OID." + BCObjectIdentifiers.external_value, "org.bouncycastle.jcajce.provider.asymmetric.EXTERNAL$KeyFactory");
            EXTERNAL.baseConverter = new ExternalKeyInfoConverter(configurableProvider);
            configurableProvider.addKeyInfoConverter(BCObjectIdentifiers.external_value, EXTERNAL.baseConverter);
        }
    }
}
