package org.bouncycastle.openpgp.api;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.bouncycastle.bcpg.PublicKeyUtils;
import org.bouncycastle.bcpg.PublicSubkeyPacket;
import org.bouncycastle.bcpg.S2K;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPKeyPair;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.openpgp.PGPSignatureSubpacketGenerator;
import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator;
import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor;
import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptorFactory;
import org.bouncycastle.openpgp.operator.PGPDigestCalculatorProvider;
import org.bouncycastle.openpgp.operator.PGPKeyPairGenerator;
import org.bouncycastle.openpgp.operator.PGPKeyPairGeneratorProvider;
import org.bouncycastle.util.Arrays;

/* JADX INFO: loaded from: classes3.dex */
public class OpenPGPKeyGenerator extends AbstractOpenPGPKeySignatureGenerator {
    private static final long SECONDS_PER_DAY = 86400;
    private static final long SECONDS_PER_HOUR = 3600;
    private static final long SECONDS_PER_MINUTE = 60;
    private static final long SECONDS_PER_YEAR = 31536000;
    private final Configuration configuration;
    private final OpenPGPImplementation implementationProvider;
    private final int keyVersion;

    /* JADX INFO: loaded from: classes2.dex */
    private static class Configuration {
        final PGPDigestCalculatorProvider digestCalculatorProvider;
        final Date keyCreationTime;
        final PBESecretKeyEncryptorFactory keyEncryptorBuilderProvider;
        final KeyFingerPrintCalculator keyFingerprintCalculator;
        final PGPKeyPairGeneratorProvider kpGenProvider;

        public Configuration(Date date, PGPKeyPairGeneratorProvider pGPKeyPairGeneratorProvider, PGPDigestCalculatorProvider pGPDigestCalculatorProvider, PBESecretKeyEncryptorFactory pBESecretKeyEncryptorFactory, KeyFingerPrintCalculator keyFingerPrintCalculator) {
            this.keyCreationTime = new Date((date.getTime() / 1000) * 1000);
            this.kpGenProvider = pGPKeyPairGeneratorProvider;
            this.digestCalculatorProvider = pGPDigestCalculatorProvider;
            this.keyEncryptorBuilderProvider = pBESecretKeyEncryptorFactory;
            this.keyFingerprintCalculator = keyFingerPrintCalculator;
        }
    }

    public class WithPrimaryKey {
        private final Configuration configuration;
        private final OpenPGPImplementation implementation;
        private PGPKeyPair primaryKey;
        private final List<PGPKeyPair> subkeys;

        private WithPrimaryKey(OpenPGPImplementation openPGPImplementation, Configuration configuration, PGPKeyPair pGPKeyPair) {
            this.subkeys = new ArrayList();
            this.implementation = openPGPImplementation;
            this.configuration = configuration;
            this.primaryKey = pGPKeyPair;
        }

        private PGPKeyPair updateSubkey(PGPKeyPair pGPKeyPair, SignatureParameters.Callback callback, Utils.HashedSubpacketsOperation hashedSubpacketsOperation) throws PGPException {
            SignatureParameters signatureParametersApplySignatureParameters = Utils.applySignatureParameters(callback, SignatureParameters.subkeyBinding(this.implementation.policy()).setSignatureCreationTime(this.configuration.keyCreationTime));
            if (signatureParametersApplySignatureParameters == null) {
                return pGPKeyPair;
            }
            return new PGPKeyPair(Utils.injectCertification(pGPKeyPair.getPublicKey(), Utils.getPgpSignatureGenerator(this.implementation, this.primaryKey.getPublicKey(), this.primaryKey.getPrivateKey(), signatureParametersApplySignatureParameters, signatureParametersApplySignatureParameters.getSignatureCreationTime(), hashedSubpacketsOperation), this.primaryKey.getPublicKey()), pGPKeyPair.getPrivateKey());
        }

        public WithPrimaryKey addEncryptionSubkey() throws PGPException {
            return addEncryptionSubkey(KeyPairGeneratorCallback.Util.encryptionKey());
        }

        public WithPrimaryKey addEncryptionSubkey(PGPKeyPair pGPKeyPair, SignatureParameters.Callback callback) throws PGPException {
            if (!(pGPKeyPair.getPublicKey().getPublicKeyPacket() instanceof PublicSubkeyPacket)) {
                throw new IllegalArgumentException("Encryption subkey MUST NOT consist of a primary key packet.");
            }
            if (!pGPKeyPair.getPublicKey().isEncryptionKey()) {
                throw new PGPException("Encryption key MUST use encryption-capable algorithm.");
            }
            this.subkeys.add(updateSubkey(pGPKeyPair, callback, new Utils.HashedSubpacketsOperation() { // from class: org.bouncycastle.openpgp.api.OpenPGPKeyGenerator.WithPrimaryKey.1
                @Override // org.bouncycastle.openpgp.api.Utils.HashedSubpacketsOperation
                public void operate(PGPSignatureSubpacketGenerator pGPSignatureSubpacketGenerator) throws PGPException {
                    OpenPGPKeyGenerator.this.encryptionSubkeySubpackets.apply(pGPSignatureSubpacketGenerator);
                }
            }));
            return this;
        }

        public WithPrimaryKey addEncryptionSubkey(KeyPairGeneratorCallback keyPairGeneratorCallback) throws PGPException {
            return addEncryptionSubkey(keyPairGeneratorCallback, (SignatureParameters.Callback) null);
        }

        public WithPrimaryKey addEncryptionSubkey(KeyPairGeneratorCallback keyPairGeneratorCallback, SignatureParameters.Callback callback) throws PGPException {
            return addEncryptionSubkey(keyPairGeneratorCallback.generateFrom(this.configuration.kpGenProvider.get(OpenPGPKeyGenerator.this.keyVersion, this.configuration.keyCreationTime)).asSubkey(this.implementation.keyFingerPrintCalculator()), callback);
        }

        public WithPrimaryKey addSigningSubkey() throws PGPException {
            return addSigningSubkey(KeyPairGeneratorCallback.Util.signingKey());
        }

        public WithPrimaryKey addSigningSubkey(PGPKeyPair pGPKeyPair, SignatureParameters.Callback callback, SignatureParameters.Callback callback2) throws PGPException {
            if (!(pGPKeyPair.getPublicKey().getPublicKeyPacket() instanceof PublicSubkeyPacket)) {
                throw new IllegalArgumentException("Signing subkey MUST NOT consist of primary key packet.");
            }
            if (!PublicKeyUtils.isSigningAlgorithm(pGPKeyPair.getPublicKey().getAlgorithm())) {
                throw new PGPException("Signing key MUST use signing-capable algorithm.");
            }
            final PGPSignature backSignature = Utils.getBackSignature(pGPKeyPair, Utils.applySignatureParameters(callback2, SignatureParameters.primaryKeyBinding(this.implementation.policy())), this.primaryKey.getPublicKey(), this.implementation, this.configuration.keyCreationTime);
            this.subkeys.add(updateSubkey(pGPKeyPair, callback, new Utils.HashedSubpacketsOperation(this) { // from class: org.bouncycastle.openpgp.api.OpenPGPKeyGenerator.WithPrimaryKey.2
                final /* synthetic */ WithPrimaryKey this$1;

                {
                    this.this$1 = this;
                }

                @Override // org.bouncycastle.openpgp.api.Utils.HashedSubpacketsOperation
                public void operate(PGPSignatureSubpacketGenerator pGPSignatureSubpacketGenerator) throws PGPException {
                    Utils.addEmbeddedSiganture(backSignature, OpenPGPKeyGenerator.this.signingSubkeySubpackets.apply(pGPSignatureSubpacketGenerator));
                }
            }));
            return this;
        }

        public WithPrimaryKey addSigningSubkey(KeyPairGeneratorCallback keyPairGeneratorCallback) throws PGPException {
            return addSigningSubkey(keyPairGeneratorCallback, (SignatureParameters.Callback) null, (SignatureParameters.Callback) null);
        }

        public WithPrimaryKey addSigningSubkey(KeyPairGeneratorCallback keyPairGeneratorCallback, SignatureParameters.Callback callback, SignatureParameters.Callback callback2) throws PGPException {
            return addSigningSubkey(keyPairGeneratorCallback.generateFrom(this.configuration.kpGenProvider.get(OpenPGPKeyGenerator.this.keyVersion, this.configuration.keyCreationTime)).asSubkey(this.configuration.keyFingerprintCalculator), callback, callback2);
        }

        public WithPrimaryKey addUserId(String str) throws PGPException {
            return addUserId(str, null);
        }

        public WithPrimaryKey addUserId(String str, SignatureParameters.Callback callback) throws PGPException {
            if (str == null || str.trim().length() == 0) {
                throw new IllegalArgumentException("User-ID cannot be null or empty.");
            }
            SignatureParameters signatureParametersApplySignatureParameters = Utils.applySignatureParameters(callback, SignatureParameters.certification(this.implementation.policy()));
            if (signatureParametersApplySignatureParameters != null) {
                this.primaryKey = new PGPKeyPair(Utils.injectCertification(str, this.primaryKey.getPublicKey(), Utils.getPgpSignatureGenerator(this.implementation, this.primaryKey.getPublicKey(), this.primaryKey.getPrivateKey(), signatureParametersApplySignatureParameters, this.configuration.keyCreationTime, null)), this.primaryKey.getPrivateKey());
            }
            return this;
        }

        public OpenPGPKey build() throws PGPException {
            return build(null);
        }

        public OpenPGPKey build(char[] cArr) throws PGPException {
            PBESecretKeyEncryptor pBESecretKeyEncryptorBuild = this.configuration.keyEncryptorBuilderProvider.build(cArr, this.primaryKey.getPublicKey().getPublicKeyPacket());
            PGPSecretKey pGPSecretKey = new PGPSecretKey(this.primaryKey.getPrivateKey(), this.primaryKey.getPublicKey(), this.configuration.digestCalculatorProvider.get(2), true, pBESecretKeyEncryptorBuild);
            sanitizeKeyEncryptor(pBESecretKeyEncryptorBuild);
            ArrayList arrayList = new ArrayList();
            arrayList.add(pGPSecretKey);
            for (PGPKeyPair pGPKeyPair : this.subkeys) {
                PBESecretKeyEncryptor pBESecretKeyEncryptorBuild2 = this.configuration.keyEncryptorBuilderProvider.build(cArr, pGPKeyPair.getPublicKey().getPublicKeyPacket());
                PGPSecretKey pGPSecretKey2 = new PGPSecretKey(pGPKeyPair.getPrivateKey(), pGPKeyPair.getPublicKey(), this.configuration.digestCalculatorProvider.get(2), false, pBESecretKeyEncryptorBuild2);
                sanitizeKeyEncryptor(pBESecretKeyEncryptorBuild2);
                arrayList.add(pGPSecretKey2);
            }
            if (cArr != null) {
                Arrays.fill(cArr, (char) 0);
            }
            return new OpenPGPKey(new PGPSecretKeyRing(arrayList), this.implementation);
        }

        protected void sanitizeKeyEncryptor(PBESecretKeyEncryptor pBESecretKeyEncryptor) {
            if (pBESecretKeyEncryptor == null) {
                return;
            }
            S2K s2k = pBESecretKeyEncryptor.getS2K();
            if (s2k.getType() == 0 || s2k.getType() == 1) {
                throw new IllegalArgumentException("S2K specifiers SIMPLE and SALTED are not allowed for secret key encryption.");
            }
            if (s2k.getType() == 4 && pBESecretKeyEncryptor.getAeadAlgorithm() == 0) {
                throw new IllegalArgumentException("Argon2 MUST be used with AEAD.");
            }
        }
    }

    public OpenPGPKeyGenerator(OpenPGPImplementation openPGPImplementation, int i, PGPKeyPairGeneratorProvider pGPKeyPairGeneratorProvider, PGPDigestCalculatorProvider pGPDigestCalculatorProvider, PBESecretKeyEncryptorFactory pBESecretKeyEncryptorFactory, KeyFingerPrintCalculator keyFingerPrintCalculator, Date date) {
        if (i != 4 && i != 5 && i != 6) {
            throw new IllegalArgumentException("Generating keys of version " + i + " is not supported.");
        }
        this.implementationProvider = openPGPImplementation;
        this.keyVersion = i;
        this.configuration = new Configuration(date, pGPKeyPairGeneratorProvider, pGPDigestCalculatorProvider, pBESecretKeyEncryptorFactory, keyFingerPrintCalculator);
    }

    public OpenPGPKeyGenerator(OpenPGPImplementation openPGPImplementation, int i, boolean z, Date date) throws PGPException {
        this(openPGPImplementation, i, openPGPImplementation.pgpKeyPairGeneratorProvider(), openPGPImplementation.pgpDigestCalculatorProvider(), openPGPImplementation.pbeSecretKeyEncryptorFactory(z), openPGPImplementation.keyFingerPrintCalculator(), date);
    }

    public OpenPGPKeyGenerator(OpenPGPImplementation openPGPImplementation, boolean z, Date date) throws PGPException {
        this(openPGPImplementation, 6, z, date);
    }

    public WithPrimaryKey classicKey(String str) throws PGPException {
        WithPrimaryKey withPrimaryKeyAddEncryptionSubkey = withPrimaryKey().addSigningSubkey().addEncryptionSubkey();
        if (str != null) {
            withPrimaryKeyAddEncryptionSubkey.addUserId(str);
        }
        return withPrimaryKeyAddEncryptionSubkey;
    }

    public WithPrimaryKey ed25519x25519Key(String str) throws PGPException {
        WithPrimaryKey withPrimaryKeyAddEncryptionSubkey = withPrimaryKey(new KeyPairGeneratorCallback() { // from class: org.bouncycastle.openpgp.api.OpenPGPKeyGenerator.3
            @Override // org.bouncycastle.openpgp.api.KeyPairGeneratorCallback
            public PGPKeyPair generateFrom(PGPKeyPairGenerator pGPKeyPairGenerator) throws PGPException {
                return pGPKeyPairGenerator.generateEd25519KeyPair();
            }
        }).addSigningSubkey(new KeyPairGeneratorCallback() { // from class: org.bouncycastle.openpgp.api.OpenPGPKeyGenerator.2
            @Override // org.bouncycastle.openpgp.api.KeyPairGeneratorCallback
            public PGPKeyPair generateFrom(PGPKeyPairGenerator pGPKeyPairGenerator) throws PGPException {
                return pGPKeyPairGenerator.generateEd25519KeyPair();
            }
        }).addEncryptionSubkey(new KeyPairGeneratorCallback() { // from class: org.bouncycastle.openpgp.api.OpenPGPKeyGenerator.1
            @Override // org.bouncycastle.openpgp.api.KeyPairGeneratorCallback
            public PGPKeyPair generateFrom(PGPKeyPairGenerator pGPKeyPairGenerator) throws PGPException {
                return pGPKeyPairGenerator.generateX25519KeyPair();
            }
        });
        if (str != null) {
            withPrimaryKeyAddEncryptionSubkey.addUserId(str);
        }
        return withPrimaryKeyAddEncryptionSubkey;
    }

    public WithPrimaryKey ed448x448Key(String str) throws PGPException {
        WithPrimaryKey withPrimaryKeyAddEncryptionSubkey = withPrimaryKey(new KeyPairGeneratorCallback() { // from class: org.bouncycastle.openpgp.api.OpenPGPKeyGenerator.6
            @Override // org.bouncycastle.openpgp.api.KeyPairGeneratorCallback
            public PGPKeyPair generateFrom(PGPKeyPairGenerator pGPKeyPairGenerator) throws PGPException {
                return pGPKeyPairGenerator.generateEd448KeyPair();
            }
        }).addSigningSubkey(new KeyPairGeneratorCallback() { // from class: org.bouncycastle.openpgp.api.OpenPGPKeyGenerator.5
            @Override // org.bouncycastle.openpgp.api.KeyPairGeneratorCallback
            public PGPKeyPair generateFrom(PGPKeyPairGenerator pGPKeyPairGenerator) throws PGPException {
                return pGPKeyPairGenerator.generateEd448KeyPair();
            }
        }).addEncryptionSubkey(new KeyPairGeneratorCallback() { // from class: org.bouncycastle.openpgp.api.OpenPGPKeyGenerator.4
            @Override // org.bouncycastle.openpgp.api.KeyPairGeneratorCallback
            public PGPKeyPair generateFrom(PGPKeyPairGenerator pGPKeyPairGenerator) throws PGPException {
                return pGPKeyPairGenerator.generateX448KeyPair();
            }
        });
        if (str != null) {
            withPrimaryKeyAddEncryptionSubkey.addUserId(str);
        }
        return withPrimaryKeyAddEncryptionSubkey;
    }

    public WithPrimaryKey signOnlyKey() throws PGPException {
        return withPrimaryKey(KeyPairGeneratorCallback.Util.primaryKey(), SignatureParameters.Callback.Util.modifyHashedSubpackets(new SignatureSubpacketsFunction() { // from class: org.bouncycastle.openpgp.api.OpenPGPKeyGenerator.7
            @Override // org.bouncycastle.openpgp.api.SignatureSubpacketsFunction
            public PGPSignatureSubpacketGenerator apply(PGPSignatureSubpacketGenerator pGPSignatureSubpacketGenerator) {
                pGPSignatureSubpacketGenerator.removePacketsOfType(27);
                pGPSignatureSubpacketGenerator.setKeyFlags(true, 3);
                return pGPSignatureSubpacketGenerator;
            }
        }));
    }

    public WithPrimaryKey withPrimaryKey() throws PGPException {
        return withPrimaryKey(KeyPairGeneratorCallback.Util.primaryKey());
    }

    public WithPrimaryKey withPrimaryKey(KeyPairGeneratorCallback keyPairGeneratorCallback) throws PGPException {
        return withPrimaryKey(keyPairGeneratorCallback, null);
    }

    public WithPrimaryKey withPrimaryKey(KeyPairGeneratorCallback keyPairGeneratorCallback, SignatureParameters.Callback callback) throws PGPException {
        PGPKeyPair pGPKeyPair;
        PGPKeyPair pGPKeyPairGenerateFrom = keyPairGeneratorCallback.generateFrom(this.configuration.kpGenProvider.get(this.keyVersion, this.configuration.keyCreationTime));
        if (pGPKeyPairGenerateFrom.getPublicKey().getPublicKeyPacket() instanceof PublicSubkeyPacket) {
            throw new IllegalArgumentException("Primary key MUST NOT consist of subkey packet.");
        }
        if (!PublicKeyUtils.isSigningAlgorithm(pGPKeyPairGenerateFrom.getPublicKey().getAlgorithm())) {
            throw new PGPException("Primary key MUST use signing-capable algorithm.");
        }
        SignatureParameters signatureParametersApplySignatureParameters = Utils.applySignatureParameters(callback, SignatureParameters.directKeySignature(this.implementationProvider.policy()));
        if (signatureParametersApplySignatureParameters != null) {
            pGPKeyPair = new PGPKeyPair(Utils.injectCertification(pGPKeyPairGenerateFrom.getPublicKey(), Utils.getPgpSignatureGenerator(this.implementationProvider, pGPKeyPairGenerateFrom.getPublicKey(), pGPKeyPairGenerateFrom.getPrivateKey(), signatureParametersApplySignatureParameters, this.configuration.keyCreationTime, new Utils.HashedSubpacketsOperation() { // from class: org.bouncycastle.openpgp.api.OpenPGPKeyGenerator.8
                @Override // org.bouncycastle.openpgp.api.Utils.HashedSubpacketsOperation
                public void operate(PGPSignatureSubpacketGenerator pGPSignatureSubpacketGenerator) throws PGPException {
                    PGPSignatureSubpacketGenerator pGPSignatureSubpacketGeneratorApply = OpenPGPKeyGenerator.this.directKeySignatureSubpackets.apply(pGPSignatureSubpacketGenerator);
                    pGPSignatureSubpacketGeneratorApply.setKeyFlags(true, 1);
                    pGPSignatureSubpacketGeneratorApply.setKeyExpirationTime(false, 157680000L);
                }
            })), pGPKeyPairGenerateFrom.getPrivateKey());
        } else {
            pGPKeyPair = pGPKeyPairGenerateFrom;
        }
        return new WithPrimaryKey(this.implementationProvider, this.configuration, pGPKeyPair);
    }
}
