package org.bouncycastle.openpgp.api;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.bouncycastle.bcpg.sig.PreferredAlgorithms;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPKeyPair;
import org.bouncycastle.openpgp.PGPSignatureGenerator;
import org.bouncycastle.openpgp.api.AbstractOpenPGPDocumentSignatureGenerator;
import org.bouncycastle.openpgp.api.exception.InvalidSigningKeyException;
import org.bouncycastle.openpgp.api.exception.KeyPassphraseException;

/* JADX INFO: loaded from: classes3.dex */
public class AbstractOpenPGPDocumentSignatureGenerator<T extends AbstractOpenPGPDocumentSignatureGenerator<T>> {
    protected final OpenPGPImplementation implementation;
    protected final OpenPGPPolicy policy;
    protected final List<PGPSignatureGenerator> signatureGenerators = new ArrayList();
    protected final List<OpenPGPKey.OpenPGPSecretKey> signingKeys = new ArrayList();
    protected final List<SignatureParameters.Callback> signatureCallbacks = new ArrayList();
    protected final List<KeyPassphraseProvider> signingKeyPassphraseProviders = new ArrayList();
    protected final KeyPassphraseProvider.DefaultKeyPassphraseProvider defaultKeyPassphraseProvider = new KeyPassphraseProvider.DefaultKeyPassphraseProvider();
    protected SubkeySelector signingKeySelector = new SubkeySelector() { // from class: org.bouncycastle.openpgp.api.AbstractOpenPGPDocumentSignatureGenerator.1
        @Override // org.bouncycastle.openpgp.api.SubkeySelector
        public List<OpenPGPCertificate.OpenPGPComponentKey> select(OpenPGPCertificate openPGPCertificate, OpenPGPPolicy openPGPPolicy) {
            ArrayList arrayList = new ArrayList();
            for (OpenPGPCertificate.OpenPGPComponentKey openPGPComponentKey : openPGPCertificate.getSigningKeys()) {
                if (openPGPPolicy.isAcceptablePublicKey(openPGPComponentKey.getPGPPublicKey())) {
                    arrayList.add(openPGPComponentKey);
                }
            }
            return arrayList;
        }
    };

    public AbstractOpenPGPDocumentSignatureGenerator(OpenPGPImplementation openPGPImplementation, OpenPGPPolicy openPGPPolicy) {
        this.implementation = openPGPImplementation;
        this.policy = openPGPPolicy;
    }

    private int getPreferredHashAlgorithm(OpenPGPCertificate.OpenPGPComponentKey openPGPComponentKey) {
        PreferredAlgorithms hashAlgorithmPreferences = openPGPComponentKey.getHashAlgorithmPreferences();
        if (hashAlgorithmPreferences != null) {
            int[] preferences = hashAlgorithmPreferences.getPreferences();
            ArrayList arrayList = new ArrayList();
            for (int i : preferences) {
                if (this.policy.isAcceptableDocumentSignatureHashAlgorithm(i, new Date())) {
                    arrayList.add(Integer.valueOf(i));
                }
            }
            if (!arrayList.isEmpty()) {
                return ((Integer) arrayList.get(0)).intValue();
            }
        }
        return this.policy.getDefaultDocumentSignatureHashAlgorithm();
    }

    public T addKeyPassphrase(char[] cArr) {
        this.defaultKeyPassphraseProvider.addPassphrase(cArr);
        return this;
    }

    protected void addSignToGenerator() throws PGPException {
        for (int i = 0; i < this.signingKeys.size(); i++) {
            this.signatureGenerators.add(initSignatureGenerator(this.signingKeys.get(i), this.signingKeyPassphraseProviders.get(i), this.signatureCallbacks.get(i)));
        }
    }

    public T addSigningKey(OpenPGPKey.OpenPGPSecretKey openPGPSecretKey, KeyPassphraseProvider keyPassphraseProvider, SignatureParameters.Callback callback) throws InvalidSigningKeyException {
        if (!openPGPSecretKey.isSigningKey()) {
            throw new InvalidSigningKeyException(openPGPSecretKey);
        }
        this.signingKeys.add(openPGPSecretKey);
        this.signingKeyPassphraseProviders.add(keyPassphraseProvider);
        this.signatureCallbacks.add(callback);
        return this;
    }

    public T addSigningKey(OpenPGPKey.OpenPGPSecretKey openPGPSecretKey, char[] cArr, SignatureParameters.Callback callback) throws InvalidSigningKeyException {
        return (T) addSigningKey(openPGPSecretKey, this.defaultKeyPassphraseProvider.addPassphrase(openPGPSecretKey, cArr), callback);
    }

    public T addSigningKey(OpenPGPKey openPGPKey) throws InvalidSigningKeyException {
        return (T) addSigningKey(openPGPKey, this.defaultKeyPassphraseProvider);
    }

    public T addSigningKey(OpenPGPKey openPGPKey, KeyPassphraseProvider keyPassphraseProvider) throws InvalidSigningKeyException {
        return (T) addSigningKey(openPGPKey, keyPassphraseProvider, (SignatureParameters.Callback) null);
    }

    public T addSigningKey(OpenPGPKey openPGPKey, KeyPassphraseProvider keyPassphraseProvider, SignatureParameters.Callback callback) throws InvalidSigningKeyException {
        List<OpenPGPCertificate.OpenPGPComponentKey> listSelect = this.signingKeySelector.select(openPGPKey, this.policy);
        if (listSelect.isEmpty()) {
            throw new InvalidSigningKeyException(openPGPKey);
        }
        Iterator<OpenPGPCertificate.OpenPGPComponentKey> it = listSelect.iterator();
        while (it.hasNext()) {
            addSigningKey(openPGPKey.getSecretKey(it.next()), keyPassphraseProvider, callback);
        }
        return this;
    }

    public T addSigningKey(OpenPGPKey openPGPKey, SignatureParameters.Callback callback) throws InvalidSigningKeyException {
        return (T) addSigningKey(openPGPKey, this.defaultKeyPassphraseProvider, callback);
    }

    protected PGPSignatureGenerator initSignatureGenerator(OpenPGPKey.OpenPGPSecretKey openPGPSecretKey, KeyPassphraseProvider keyPassphraseProvider, SignatureParameters.Callback callback) throws PGPException {
        SignatureParameters signatureParametersApplySignatureParameters = Utils.applySignatureParameters(callback, SignatureParameters.dataSignature(this.policy).setSignatureHashAlgorithm(getPreferredHashAlgorithm(openPGPSecretKey)));
        if (signatureParametersApplySignatureParameters == null) {
            throw new IllegalStateException("SignatureParameters Callback MUST NOT return null.");
        }
        if (!openPGPSecretKey.isSigningKey(signatureParametersApplySignatureParameters.getSignatureCreationTime())) {
            throw new InvalidSigningKeyException(openPGPSecretKey);
        }
        PGPKeyPair keyPair = openPGPSecretKey.unlock(keyPassphraseProvider.getKeyPassword(openPGPSecretKey)).getKeyPair();
        if (keyPair != null) {
            return Utils.getPgpSignatureGenerator(this.implementation, openPGPSecretKey.getPGPPublicKey(), keyPair.getPrivateKey(), signatureParametersApplySignatureParameters, null, null);
        }
        throw new KeyPassphraseException(openPGPSecretKey, new PGPException("Cannot unlock secret key."));
    }

    public T setMissingKeyPassphraseCallback(KeyPassphraseProvider keyPassphraseProvider) {
        this.defaultKeyPassphraseProvider.setMissingPassphraseCallback(keyPassphraseProvider);
        return this;
    }

    public T setSigningKeySelector(SubkeySelector subkeySelector) {
        if (subkeySelector == null) {
            throw new NullPointerException();
        }
        this.signingKeySelector = subkeySelector;
        return this;
    }
}
