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

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

import java.util.Enumeration;
import java.math.BigInteger;
import org.bouncycastle.asn1.ASN1OctetString;
import org.bouncycastle.asn1.ASN1TaggedObject;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.ASN1Sequence;
import java.io.IOException;
import org.bouncycastle.asn1.ASN1Boolean;
import org.bouncycastle.asn1.DERSequence;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.DERTaggedObject;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.ASN1EncodableVector;
import java.security.spec.InvalidParameterSpecException;
import java.security.spec.AlgorithmParameterSpec;
import org.bouncycastle.jce.spec.IESParameterSpec;

public class AlgorithmParametersSpi extends java.security.AlgorithmParametersSpi
{
    IESParameterSpec currentSpec;
    
    protected boolean isASN1FormatString(final String s) {
        return s == null || s.equals("ASN.1");
    }
    
    @Override
    protected AlgorithmParameterSpec engineGetParameterSpec(final Class clazz) throws InvalidParameterSpecException {
        if (clazz == null) {
            throw new NullPointerException("argument to getParameterSpec must not be null");
        }
        return this.localEngineGetParameterSpec(clazz);
    }
    
    @Override
    protected byte[] engineGetEncoded() {
        try {
            final ASN1EncodableVector asn1EncodableVector = new ASN1EncodableVector();
            if (this.currentSpec.getDerivationV() != null) {
                asn1EncodableVector.add(new DERTaggedObject(false, 0, new DEROctetString(this.currentSpec.getDerivationV())));
            }
            if (this.currentSpec.getEncodingV() != null) {
                asn1EncodableVector.add(new DERTaggedObject(false, 1, new DEROctetString(this.currentSpec.getEncodingV())));
            }
            asn1EncodableVector.add(new ASN1Integer(this.currentSpec.getMacKeySize()));
            final byte[] nonce = this.currentSpec.getNonce();
            if (nonce != null) {
                final ASN1EncodableVector asn1EncodableVector2 = new ASN1EncodableVector();
                asn1EncodableVector2.add(new ASN1Integer(this.currentSpec.getCipherKeySize()));
                asn1EncodableVector2.add(new DEROctetString(nonce));
                asn1EncodableVector.add(new DERSequence(asn1EncodableVector2));
            }
            asn1EncodableVector.add(this.currentSpec.getPointCompression() ? ASN1Boolean.TRUE : ASN1Boolean.FALSE);
            return new DERSequence(asn1EncodableVector).getEncoded("DER");
        }
        catch (final IOException ex) {
            throw new RuntimeException("Error encoding IESParameters");
        }
    }
    
    @Override
    protected byte[] engineGetEncoded(final String s) {
        if (this.isASN1FormatString(s) || s.equalsIgnoreCase("X.509")) {
            return this.engineGetEncoded();
        }
        return null;
    }
    
    protected AlgorithmParameterSpec localEngineGetParameterSpec(final Class clazz) throws InvalidParameterSpecException {
        if (clazz == IESParameterSpec.class || clazz == AlgorithmParameterSpec.class) {
            return this.currentSpec;
        }
        throw new InvalidParameterSpecException("unknown parameter spec passed to ElGamal parameters object.");
    }
    
    @Override
    protected void engineInit(final AlgorithmParameterSpec algorithmParameterSpec) throws InvalidParameterSpecException {
        if (!(algorithmParameterSpec instanceof IESParameterSpec)) {
            throw new InvalidParameterSpecException("IESParameterSpec required to initialise a IES algorithm parameters object");
        }
        this.currentSpec = (IESParameterSpec)algorithmParameterSpec;
    }
    
    @Override
    protected void engineInit(final byte[] array) throws IOException {
        try {
            final ASN1Sequence asn1Sequence = (ASN1Sequence)ASN1Primitive.fromByteArray(array);
            if (asn1Sequence.size() > 5) {
                throw new IOException("sequence too big");
            }
            byte[] octets = null;
            byte[] octets2 = null;
            BigInteger value = null;
            BigInteger value2 = null;
            byte[] octets3 = null;
            boolean true = false;
            final Enumeration objects = asn1Sequence.getObjects();
            while (objects.hasMoreElements()) {
                final Object nextElement = objects.nextElement();
                if (nextElement instanceof ASN1TaggedObject) {
                    final ASN1TaggedObject instance = ASN1TaggedObject.getInstance(nextElement);
                    if (instance.getTagNo() == 0) {
                        octets = ASN1OctetString.getInstance(instance, false).getOctets();
                    }
                    else {
                        if (instance.getTagNo() != 1) {
                            continue;
                        }
                        octets2 = ASN1OctetString.getInstance(instance, false).getOctets();
                    }
                }
                else if (nextElement instanceof ASN1Integer) {
                    value = ASN1Integer.getInstance(nextElement).getValue();
                }
                else if (nextElement instanceof ASN1Sequence) {
                    final ASN1Sequence instance2 = ASN1Sequence.getInstance(nextElement);
                    value2 = ASN1Integer.getInstance(instance2.getObjectAt(0)).getValue();
                    octets3 = ASN1OctetString.getInstance(instance2.getObjectAt(1)).getOctets();
                }
                else {
                    if (!(nextElement instanceof ASN1Boolean)) {
                        continue;
                    }
                    true = ASN1Boolean.getInstance(nextElement).isTrue();
                }
            }
            if (value2 != null) {
                this.currentSpec = new IESParameterSpec(octets, octets2, value.intValue(), value2.intValue(), octets3, true);
            }
            else {
                this.currentSpec = new IESParameterSpec(octets, octets2, value.intValue(), -1, null, true);
            }
        }
        catch (final ClassCastException ex) {
            throw new IOException("Not a valid IES Parameter encoding.");
        }
        catch (final ArrayIndexOutOfBoundsException ex2) {
            throw new IOException("Not a valid IES Parameter encoding.");
        }
    }
    
    @Override
    protected void engineInit(final byte[] array, final String str) throws IOException {
        if (this.isASN1FormatString(str) || str.equalsIgnoreCase("X.509")) {
            this.engineInit(array);
            return;
        }
        throw new IOException("Unknown parameter format " + str);
    }
    
    @Override
    protected String engineToString() {
        return "IES Parameters";
    }
}
