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.KeyIdentifier;
import org.bouncycastle.openpgp.IntegrityProtectedInputStream;
import org.bouncycastle.openpgp.PGPEncryptedData;
import org.bouncycastle.openpgp.PGPEncryptedDataList;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPKeyPair;
import org.bouncycastle.openpgp.PGPPBEEncryptedData;
import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData;
import org.bouncycastle.openpgp.PGPSessionKey;
import org.bouncycastle.openpgp.PGPSessionKeyEncryptedData;
import org.bouncycastle.openpgp.PGPUtil;
import org.bouncycastle.openpgp.api.exception.KeyPassphraseException;
import org.bouncycastle.openpgp.operator.SessionKeyDataDecryptorFactory;
import org.bouncycastle.util.Arrays;

/* JADX INFO: loaded from: classes2.dex */
public class OpenPGPMessageProcessor {
    private final Configuration configuration;
    private final OpenPGPImplementation implementation;

    /* JADX INFO: loaded from: classes3.dex */
    public static class Configuration {
        private MissingMessagePassphraseCallback missingMessagePassphraseCallback;
        private final OpenPGPPolicy policy;
        private PGPSessionKey sessionKey;
        public final List<char[]> messagePassphrases = new ArrayList();
        private PGPExceptionCallback exceptionCallback = null;
        private Date verifyNotAfter = new Date();
        private Date verifyNotBefore = new Date(0);
        private final OpenPGPKeyMaterialPool.OpenPGPCertificatePool certificatePool = new OpenPGPKeyMaterialPool.OpenPGPCertificatePool();
        private final OpenPGPKeyMaterialPool.OpenPGPKeyPool keyPool = new OpenPGPKeyMaterialPool.OpenPGPKeyPool();
        private final KeyPassphraseProvider.DefaultKeyPassphraseProvider keyPassphraseProvider = new KeyPassphraseProvider.DefaultKeyPassphraseProvider();

        public Configuration(OpenPGPPolicy openPGPPolicy) {
            this.policy = openPGPPolicy;
        }

        public Configuration addMessagePassphrase(char[] cArr) {
            Iterator<char[]> it = this.messagePassphrases.iterator();
            boolean zAreEqual = false;
            while (it.hasNext()) {
                zAreEqual |= Arrays.areEqual(it.next(), cArr);
            }
            if (!zAreEqual) {
                this.messagePassphrases.add(cArr);
            }
            return this;
        }
    }

    static class Decrypted {
        OpenPGPCertificate.OpenPGPComponentKey decryptionKey;
        char[] decryptionPassphrase;
        final PGPEncryptedData esk;
        final InputStream inputStream;
        final PGPSessionKey sessionKey;

        public Decrypted(PGPEncryptedData pGPEncryptedData, PGPSessionKey pGPSessionKey, InputStream inputStream) {
            this.esk = pGPEncryptedData;
            this.sessionKey = pGPSessionKey;
            this.inputStream = inputStream;
        }
    }

    /* JADX INFO: loaded from: classes.dex */
    public interface PGPExceptionCallback {
        void onException(PGPException pGPException);
    }

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

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

    public OpenPGPMessageProcessor(OpenPGPImplementation openPGPImplementation, OpenPGPPolicy openPGPPolicy) {
        this.implementation = openPGPImplementation;
        this.configuration = new Configuration(openPGPPolicy);
    }

    private List<PGPPublicKeyEncryptedData> pkesks(PGPEncryptedDataList pGPEncryptedDataList) {
        ArrayList arrayList = new ArrayList();
        for (PGPEncryptedData pGPEncryptedData : pGPEncryptedDataList) {
            if (pGPEncryptedData instanceof PGPPublicKeyEncryptedData) {
                arrayList.add((PGPPublicKeyEncryptedData) pGPEncryptedData);
            }
        }
        return arrayList;
    }

    private List<PGPPBEEncryptedData> skesks(PGPEncryptedDataList pGPEncryptedDataList) {
        ArrayList arrayList = new ArrayList();
        for (PGPEncryptedData pGPEncryptedData : pGPEncryptedDataList) {
            if (pGPEncryptedData instanceof PGPPBEEncryptedData) {
                arrayList.add((PGPPBEEncryptedData) pGPEncryptedData);
            }
        }
        return arrayList;
    }

    public OpenPGPMessageProcessor addDecryptionKey(OpenPGPKey openPGPKey) {
        this.configuration.keyPool.addItem(openPGPKey);
        return this;
    }

    public OpenPGPMessageProcessor addDecryptionKey(OpenPGPKey openPGPKey, char[] cArr) {
        this.configuration.keyPool.addItem(openPGPKey);
        this.configuration.keyPassphraseProvider.addPassphrase(openPGPKey, cArr);
        return this;
    }

    public OpenPGPMessageProcessor addDecryptionKeyPassphrase(char[] cArr) {
        this.configuration.keyPassphraseProvider.addPassphrase(cArr);
        return this;
    }

    public OpenPGPMessageProcessor addMessagePassphrase(char[] cArr) {
        this.configuration.addMessagePassphrase(cArr);
        return this;
    }

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

    Decrypted decrypt(PGPEncryptedDataList pGPEncryptedDataList) throws PGPException {
        OpenPGPKey.OpenPGPSecretKey openPGPSecretKey;
        if (this.configuration.sessionKey != null) {
            SessionKeyDataDecryptorFactory sessionKeyDataDecryptorFactory = this.implementation.sessionKeyDataDecryptorFactory(this.configuration.sessionKey);
            PGPSessionKeyEncryptedData pGPSessionKeyEncryptedDataExtractSessionKeyEncryptedData = pGPEncryptedDataList.extractSessionKeyEncryptedData();
            return new Decrypted(pGPSessionKeyEncryptedDataExtractSessionKeyEncryptedData, this.configuration.sessionKey, new IntegrityProtectedInputStream(pGPSessionKeyEncryptedDataExtractSessionKeyEncryptedData.getDataStream(sessionKeyDataDecryptorFactory), pGPSessionKeyEncryptedDataExtractSessionKeyEncryptedData));
        }
        List<PGPPBEEncryptedData> listSkesks = skesks(pGPEncryptedDataList);
        List<PGPPublicKeyEncryptedData> listPkesks = pkesks(pGPEncryptedDataList);
        PGPException pGPException = null;
        if (!listSkesks.isEmpty() && !this.configuration.messagePassphrases.isEmpty()) {
            for (PGPPBEEncryptedData pGPPBEEncryptedData : listSkesks) {
                for (char[] cArr : this.configuration.messagePassphrases) {
                    try {
                        PGPSessionKey sessionKey = pGPPBEEncryptedData.getSessionKey(this.implementation.pbeDataDecryptorFactory(cArr));
                        SessionKeyDataDecryptorFactory sessionKeyDataDecryptorFactory2 = this.implementation.sessionKeyDataDecryptorFactory(sessionKey);
                        PGPSessionKeyEncryptedData pGPSessionKeyEncryptedDataExtractSessionKeyEncryptedData2 = pGPEncryptedDataList.extractSessionKeyEncryptedData();
                        Decrypted decrypted = new Decrypted(pGPSessionKeyEncryptedDataExtractSessionKeyEncryptedData2, sessionKey, new IntegrityProtectedInputStream(pGPSessionKeyEncryptedDataExtractSessionKeyEncryptedData2.getDataStream(sessionKeyDataDecryptorFactory2), pGPSessionKeyEncryptedDataExtractSessionKeyEncryptedData2));
                        decrypted.decryptionPassphrase = cArr;
                        return decrypted;
                    } catch (PGPException e) {
                        onException(e);
                        if (pGPException == null) {
                            pGPException = e;
                        }
                    }
                }
            }
        }
        for (PGPPublicKeyEncryptedData pGPPublicKeyEncryptedData : listPkesks) {
            KeyIdentifier keyIdentifier = pGPPublicKeyEncryptedData.getKeyIdentifier();
            OpenPGPKey openPGPKeyProvide = this.configuration.keyPool.provide(keyIdentifier);
            if (openPGPKeyProvide != null && (openPGPSecretKey = openPGPKeyProvide.getSecretKeys().get(keyIdentifier)) != null) {
                try {
                    if (!openPGPSecretKey.isEncryptionKey()) {
                        throw new PGPException("Key is not an encryption key and can therefore not decrypt.");
                    }
                    PGPKeyPair keyPair = openPGPSecretKey.unlock(this.configuration.keyPassphraseProvider.getKeyPassword(openPGPSecretKey)).getKeyPair();
                    if (keyPair == null) {
                        throw new KeyPassphraseException(openPGPSecretKey, new PGPException("Cannot unlock secret key."));
                    }
                    PGPSessionKey sessionKey2 = pGPPublicKeyEncryptedData.getSessionKey(this.implementation.publicKeyDataDecryptorFactory(keyPair.getPrivateKey()));
                    SessionKeyDataDecryptorFactory sessionKeyDataDecryptorFactory3 = this.implementation.sessionKeyDataDecryptorFactory(sessionKey2);
                    PGPSessionKeyEncryptedData pGPSessionKeyEncryptedDataExtractSessionKeyEncryptedData3 = pGPEncryptedDataList.extractSessionKeyEncryptedData();
                    Decrypted decrypted2 = new Decrypted(pGPSessionKeyEncryptedDataExtractSessionKeyEncryptedData3, sessionKey2, new IntegrityProtectedInputStream(pGPSessionKeyEncryptedDataExtractSessionKeyEncryptedData3.getDataStream(sessionKeyDataDecryptorFactory3), pGPSessionKeyEncryptedDataExtractSessionKeyEncryptedData3));
                    decrypted2.decryptionKey = openPGPSecretKey;
                    return decrypted2;
                } catch (PGPException e2) {
                    onException(e2);
                }
            }
        }
        if (!listSkesks.isEmpty() && this.configuration.missingMessagePassphraseCallback != null) {
            while (true) {
                char[] messagePassphrase = this.configuration.missingMessagePassphraseCallback.getMessagePassphrase();
                if (messagePassphrase == null) {
                    if (pGPException == null) {
                        break;
                    }
                    throw pGPException;
                }
                Iterator<PGPPBEEncryptedData> it = listSkesks.iterator();
                while (it.hasNext()) {
                    try {
                        PGPSessionKey sessionKey3 = it.next().getSessionKey(this.implementation.pbeDataDecryptorFactory(messagePassphrase));
                        SessionKeyDataDecryptorFactory sessionKeyDataDecryptorFactory4 = this.implementation.sessionKeyDataDecryptorFactory(sessionKey3);
                        PGPSessionKeyEncryptedData pGPSessionKeyEncryptedDataExtractSessionKeyEncryptedData4 = pGPEncryptedDataList.extractSessionKeyEncryptedData();
                        Decrypted decrypted3 = new Decrypted(pGPSessionKeyEncryptedDataExtractSessionKeyEncryptedData4, sessionKey3, new IntegrityProtectedInputStream(pGPSessionKeyEncryptedDataExtractSessionKeyEncryptedData4.getDataStream(sessionKeyDataDecryptorFactory4), pGPSessionKeyEncryptedDataExtractSessionKeyEncryptedData4));
                        decrypted3.decryptionPassphrase = messagePassphrase;
                        return decrypted3;
                    } catch (PGPException e3) {
                        onException(e3);
                        if (pGPException == null) {
                            pGPException = e3;
                        }
                    }
                }
            }
        }
        throw new PGPException("No working decryption method found.");
    }

    OpenPGPImplementation getImplementation() {
        return this.implementation;
    }

    Date getVerifyNotAfter() {
        return this.configuration.verifyNotAfter;
    }

    Date getVerifyNotBefore() {
        return this.configuration.verifyNotBefore;
    }

    void onException(PGPException pGPException) {
        if (this.configuration.exceptionCallback != null) {
            this.configuration.exceptionCallback.onException(pGPException);
        }
    }

    public OpenPGPMessageInputStream process(InputStream inputStream) throws IOException, PGPException {
        OpenPGPMessageInputStream openPGPMessageInputStream = new OpenPGPMessageInputStream(this.implementation.pgpObjectFactory(PGPUtil.getDecoderStream(inputStream)), this);
        openPGPMessageInputStream.process();
        return openPGPMessageInputStream;
    }

    OpenPGPCertificate provideCertificate(KeyIdentifier keyIdentifier) {
        return this.configuration.certificatePool.provide(keyIdentifier);
    }

    public OpenPGPMessageProcessor setMissingMessagePassphraseCallback(MissingMessagePassphraseCallback missingMessagePassphraseCallback) {
        this.configuration.missingMessagePassphraseCallback = missingMessagePassphraseCallback;
        return this;
    }

    public OpenPGPMessageProcessor setMissingOpenPGPCertificateProvider(OpenPGPKeyMaterialProvider.OpenPGPCertificateProvider openPGPCertificateProvider) {
        this.configuration.certificatePool.setMissingItemCallback((OpenPGPKeyMaterialProvider<OpenPGPCertificate>) openPGPCertificateProvider);
        return this;
    }

    public OpenPGPMessageProcessor setMissingOpenPGPKeyPassphraseProvider(KeyPassphraseProvider keyPassphraseProvider) {
        this.configuration.keyPassphraseProvider.setMissingPassphraseCallback(keyPassphraseProvider);
        return this;
    }

    public OpenPGPMessageProcessor setMissingOpenPGPKeyProvider(OpenPGPKeyMaterialProvider.OpenPGPKeyProvider openPGPKeyProvider) {
        this.configuration.keyPool.setMissingItemCallback((OpenPGPKeyMaterialProvider<OpenPGPKey>) openPGPKeyProvider);
        return this;
    }

    public OpenPGPMessageProcessor setSessionKey(PGPSessionKey pGPSessionKey) {
        this.configuration.sessionKey = pGPSessionKey;
        return this;
    }

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

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