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

package org.bouncycastle.its.bc;

import org.bouncycastle.crypto.ExtendedDigest;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.DSA;
import org.bouncycastle.crypto.signers.ECDSASigner;
import org.bouncycastle.crypto.signers.DSADigestSigner;
import org.bouncycastle.crypto.Digest;
import java.io.OutputStream;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.oer.OEREncoder;
import org.bouncycastle.oer.its.template.ieee1609dot2.IEEE1609dot2;
import org.bouncycastle.operator.bc.BcDefaultDigestProvider;
import org.bouncycastle.operator.OperatorCreationException;
import org.bouncycastle.operator.ContentVerifier;
import java.io.IOException;
import org.bouncycastle.oer.its.ieee1609dot2.VerificationKeyIndicator;
import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
import org.bouncycastle.oer.its.ieee1609dot2.basetypes.PublicVerificationKey;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.its.ITSCertificate;
import org.bouncycastle.its.operator.ITSContentVerifierProvider;

public class BcITSContentVerifierProvider implements ITSContentVerifierProvider
{
    private final ITSCertificate issuer;
    private final byte[] parentData;
    private final AlgorithmIdentifier digestAlgo;
    private final ECPublicKeyParameters pubParams;
    private final int sigChoice;
    
    public BcITSContentVerifierProvider(final ITSCertificate issuer) throws IOException {
        this.issuer = issuer;
        this.parentData = issuer.getEncoded();
        final VerificationKeyIndicator verifyKeyIndicator = issuer.toASN1Structure().getToBeSigned().getVerifyKeyIndicator();
        if (verifyKeyIndicator.getVerificationKeyIndicator() instanceof PublicVerificationKey) {
            final PublicVerificationKey instance = PublicVerificationKey.getInstance(verifyKeyIndicator.getVerificationKeyIndicator());
            this.sigChoice = instance.getChoice();
            switch (instance.getChoice()) {
                case 0: {
                    this.digestAlgo = new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha256);
                    break;
                }
                case 1: {
                    this.digestAlgo = new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha256);
                    break;
                }
                case 2: {
                    this.digestAlgo = new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha384);
                    break;
                }
                default: {
                    throw new IllegalStateException("unknown key type");
                }
            }
            this.pubParams = (ECPublicKeyParameters)new BcITSPublicVerificationKey(instance).getKey();
            return;
        }
        throw new IllegalStateException("not public verification key");
    }
    
    @Override
    public ITSCertificate getAssociatedCertificate() {
        return this.issuer;
    }
    
    @Override
    public boolean hasAssociatedCertificate() {
        return this.issuer != null;
    }
    
    @Override
    public ContentVerifier get(final int i) throws OperatorCreationException {
        if (this.sigChoice != i) {
            throw new OperatorCreationException("wrong verifier for algorithm: " + i);
        }
        final ExtendedDigest value = BcDefaultDigestProvider.INSTANCE.get(this.digestAlgo);
        final byte[] array = new byte[value.getDigestSize()];
        value.update(this.parentData, 0, this.parentData.length);
        value.doFinal(array, 0);
        final byte[] array2 = (byte[])(this.issuer.getIssuer().isSelf() ? new byte[value.getDigestSize()] : null);
        if (array2 != null) {
            final byte[] byteArray = OEREncoder.toByteArray(this.issuer.toASN1Structure().getToBeSigned(), IEEE1609dot2.ToBeSignedCertificate.build());
            value.update(byteArray, 0, byteArray.length);
            value.doFinal(array2, 0);
        }
        return new ContentVerifier() {
            final DSADigestSigner signer = new DSADigestSigner(new ECDSASigner(), BcDefaultDigestProvider.INSTANCE.get(BcITSContentVerifierProvider.this.digestAlgo));
            final /* synthetic */ OutputStream val$os = new OutputStream(this, value) {
                final /* synthetic */ Digest val$digest;
                
                @Override
                public void write(final int n) throws IOException {
                    this.val$digest.update((byte)n);
                }
                
                @Override
                public void write(final byte[] array) throws IOException {
                    this.val$digest.update(array, 0, array.length);
                }
                
                @Override
                public void write(final byte[] array, final int n, final int n2) throws IOException {
                    this.val$digest.update(array, n, n2);
                }
            };
            
            @Override
            public AlgorithmIdentifier getAlgorithmIdentifier() {
                return null;
            }
            
            @Override
            public OutputStream getOutputStream() {
                return this.val$os;
            }
            
            @Override
            public boolean verify(final byte[] array) {
                final byte[] array2 = new byte[value.getDigestSize()];
                value.doFinal(array2, 0);
                this.signer.init(false, BcITSContentVerifierProvider.this.pubParams);
                this.signer.update(array2, 0, array2.length);
                if (array2 != null && Arrays.areEqual(array2, array2)) {
                    final byte[] array3 = new byte[value.getDigestSize()];
                    value.doFinal(array3, 0);
                    this.signer.update(array3, 0, array3.length);
                }
                else {
                    this.signer.update(array, 0, array.length);
                }
                return this.signer.verifySignature(array);
            }
        };
    }
}
