package org.bouncycastle.openpgp.api;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.bouncycastle.bcpg.BCPGInputStream;
import org.bouncycastle.bcpg.KeyIdentifier;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPObjectFactory;
import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.openpgp.PGPSignatureException;
import org.bouncycastle.openpgp.PGPSignatureList;
import org.bouncycastle.openpgp.PGPUtil;

/* JADX INFO: loaded from: classes2.dex */
public class OpenPGPDetachedSignatureProcessor {
    private final OpenPGPKeyMaterialPool.OpenPGPCertificatePool certificatePool;
    private OpenPGPMessageProcessor.PGPExceptionCallback exceptionCallback;
    private final OpenPGPImplementation implementation;
    private final List<PGPSignature> pgpSignatures;
    private final OpenPGPPolicy policy;
    private Date verifyNotAfter;
    private Date verifyNotBefore;

    public OpenPGPDetachedSignatureProcessor() {
        this(OpenPGPImplementation.getInstance());
    }

    public OpenPGPDetachedSignatureProcessor(OpenPGPImplementation openPGPImplementation) {
        this(openPGPImplementation, openPGPImplementation.policy());
    }

    public OpenPGPDetachedSignatureProcessor(OpenPGPImplementation openPGPImplementation, OpenPGPPolicy openPGPPolicy) {
        this.certificatePool = new OpenPGPKeyMaterialPool.OpenPGPCertificatePool();
        this.pgpSignatures = new ArrayList();
        this.verifyNotAfter = new Date();
        this.verifyNotBefore = new Date(0L);
        this.exceptionCallback = null;
        this.implementation = openPGPImplementation;
        this.policy = openPGPPolicy;
    }

    public OpenPGPDetachedSignatureProcessor addSignature(PGPSignature pGPSignature) {
        this.pgpSignatures.add(pGPSignature);
        return this;
    }

    public OpenPGPDetachedSignatureProcessor addSignatures(InputStream inputStream) throws IOException {
        PGPObjectFactory pgpObjectFactory = this.implementation.pgpObjectFactory(BCPGInputStream.wrap(PGPUtil.getDecoderStream(inputStream)));
        while (true) {
            Object objNextObject = pgpObjectFactory.nextObject();
            if (objNextObject == null) {
                return this;
            }
            if (objNextObject instanceof PGPSignatureList) {
                addSignatures((PGPSignatureList) objNextObject);
            } else if (objNextObject instanceof PGPSignature) {
                addSignature((PGPSignature) objNextObject);
            }
        }
    }

    public OpenPGPDetachedSignatureProcessor addSignatures(PGPSignatureList pGPSignatureList) {
        Iterator<PGPSignature> it = pGPSignatureList.iterator();
        while (it.hasNext()) {
            addSignature(it.next());
        }
        return this;
    }

    public OpenPGPDetachedSignatureProcessor addVerificationCertificate(OpenPGPCertificate openPGPCertificate) {
        this.certificatePool.addItem(openPGPCertificate);
        return this;
    }

    public List<OpenPGPSignature.OpenPGPDocumentSignature> process(InputStream inputStream) throws IOException {
        OpenPGPCertificate openPGPCertificateProvide;
        OpenPGPCertificate.OpenPGPComponentKey key;
        ArrayList arrayList = new ArrayList();
        for (PGPSignature pGPSignature : this.pgpSignatures) {
            KeyIdentifier mostExpressiveIdentifier = OpenPGPSignature.getMostExpressiveIdentifier(pGPSignature.getKeyIdentifiers());
            if (mostExpressiveIdentifier != null && (openPGPCertificateProvide = this.certificatePool.provide(mostExpressiveIdentifier)) != null && (key = openPGPCertificateProvide.getKey(mostExpressiveIdentifier)) != null) {
                try {
                    pGPSignature.init(this.implementation.pgpContentVerifierBuilderProvider(), key.getPGPPublicKey());
                    OpenPGPSignature.OpenPGPDocumentSignature openPGPDocumentSignature = new OpenPGPSignature.OpenPGPDocumentSignature(pGPSignature, key);
                    try {
                        openPGPDocumentSignature.sanitize(key, this.policy);
                        if (openPGPDocumentSignature.createdInBounds(this.verifyNotBefore, this.verifyNotAfter)) {
                            arrayList.add(openPGPDocumentSignature);
                        }
                    } catch (PGPSignatureException e) {
                        e = e;
                        if (this.exceptionCallback != null) {
                            this.exceptionCallback.onException(e);
                        }
                    }
                } catch (PGPException e2) {
                    e = e2;
                    if (this.exceptionCallback != null) {
                        this.exceptionCallback.onException(e);
                    }
                }
            }
        }
        byte[] bArr = new byte[2048];
        while (true) {
            int i = inputStream.read(bArr);
            if (i == -1) {
                break;
            }
            Iterator it = arrayList.iterator();
            while (it.hasNext()) {
                ((OpenPGPSignature.OpenPGPDocumentSignature) it.next()).getSignature().update(bArr, 0, i);
            }
        }
        Iterator it2 = arrayList.iterator();
        while (it2.hasNext()) {
            try {
                ((OpenPGPSignature.OpenPGPDocumentSignature) it2.next()).verify();
            } catch (PGPException e3) {
                if (this.exceptionCallback != null) {
                    this.exceptionCallback.onException(e3);
                }
            }
        }
        return arrayList;
    }

    public OpenPGPDetachedSignatureProcessor setExceptionCallback(OpenPGPMessageProcessor.PGPExceptionCallback pGPExceptionCallback) {
        this.exceptionCallback = pGPExceptionCallback;
        return this;
    }

    public OpenPGPDetachedSignatureProcessor verifyNotAfter(Date date) {
        this.verifyNotAfter = date;
        return this;
    }

    public OpenPGPDetachedSignatureProcessor verifyNotBefore(Date date) {
        this.verifyNotBefore = date;
        return this;
    }
}
