package org.bouncycastle.openpgp.api;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.bouncycastle.bcpg.BCPGOutputStream;
import org.bouncycastle.bcpg.KeyIdentifier;
import org.bouncycastle.bcpg.PacketFormat;
import org.bouncycastle.bcpg.S2K;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPKeyPair;
import org.bouncycastle.openpgp.PGPKeyValidationException;
import org.bouncycastle.openpgp.PGPPrivateKey;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.api.exception.KeyPassphraseException;
import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor;
import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptorBuilderProvider;
import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor;
import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptorFactory;

/* JADX INFO: loaded from: classes.dex */
public class OpenPGPKey extends OpenPGPCertificate {
    private final Map<KeyIdentifier, OpenPGPSecretKey> secretKeys;

    /* JADX INFO: loaded from: classes3.dex */
    public static class OpenPGPPrivateKey {
        private final OpenPGPSecretKey secretKey;
        private final PGPKeyPair unlockedKey;

        public OpenPGPPrivateKey(OpenPGPSecretKey openPGPSecretKey, PGPKeyPair pGPKeyPair) {
            this.secretKey = openPGPSecretKey;
            this.unlockedKey = pGPKeyPair;
        }

        private OpenPGPImplementation getImplementation() {
            return getSecretKey().getOpenPGPKey().implementation;
        }

        public OpenPGPSecretKey changePassphrase(PBESecretKeyEncryptor pBESecretKeyEncryptor) throws PGPException {
            OpenPGPSecretKey openPGPSecretKey = new OpenPGPSecretKey(getSecretKey().getPublicKey(), new PGPSecretKey(getKeyPair().getPrivateKey(), getKeyPair().getPublicKey(), getImplementation().pgpDigestCalculatorProvider().get(2), getSecretKey().isPrimaryKey(), pBESecretKeyEncryptor), getImplementation().pbeSecretKeyDecryptorBuilderProvider());
            openPGPSecretKey.sanitizeProtectionMode();
            return openPGPSecretKey;
        }

        public OpenPGPSecretKey changePassphrase(char[] cArr) throws PGPException {
            return changePassphrase(cArr, getImplementation(), !this.secretKey.isLocked() || this.secretKey.getPGPSecretKey().getS2KUsage() == 253);
        }

        public OpenPGPSecretKey changePassphrase(char[] cArr, OpenPGPImplementation openPGPImplementation, boolean z) throws PGPException {
            return changePassphrase(cArr, openPGPImplementation.pbeSecretKeyEncryptorFactory(z));
        }

        public OpenPGPSecretKey changePassphrase(char[] cArr, PBESecretKeyEncryptorFactory pBESecretKeyEncryptorFactory) throws PGPException {
            return changePassphrase((cArr == null || cArr.length == 0) ? null : pBESecretKeyEncryptorFactory.build(cArr, getKeyPair().getPublicKey().getPublicKeyPacket()));
        }

        public PGPKeyPair getKeyPair() {
            return this.unlockedKey;
        }

        public OpenPGPCertificate.OpenPGPComponentKey getPublicKey() {
            return this.secretKey.getPublicKey();
        }

        public OpenPGPSecretKey getSecretKey() {
            return this.secretKey;
        }

        public OpenPGPSecretKey removePassphrase() throws PGPException {
            return changePassphrase((PBESecretKeyEncryptor) null);
        }
    }

    public static class OpenPGPSecretKey extends OpenPGPCertificate.OpenPGPComponentKey {
        private final PBESecretKeyDecryptorBuilderProvider decryptorBuilderProvider;
        private final OpenPGPCertificate.OpenPGPComponentKey pubKey;
        private final PGPSecretKey rawSecKey;

        public OpenPGPSecretKey(OpenPGPCertificate.OpenPGPComponentKey openPGPComponentKey, PGPSecretKey pGPSecretKey, PBESecretKeyDecryptorBuilderProvider pBESecretKeyDecryptorBuilderProvider) {
            super(openPGPComponentKey.getPGPPublicKey(), openPGPComponentKey.getCertificate());
            this.decryptorBuilderProvider = pBESecretKeyDecryptorBuilderProvider;
            this.rawSecKey = pGPSecretKey;
            this.pubKey = openPGPComponentKey;
        }

        /* JADX INFO: Access modifiers changed from: private */
        public void sanitizeProtectionMode() throws PGPException {
            if (isLocked()) {
                PGPSecretKey pGPSecretKey = getPGPSecretKey();
                S2K s2k = pGPSecretKey.getS2K();
                if (s2k == null) {
                    throw new PGPKeyValidationException("Legacy CFB using MD5 is not allowed.");
                }
                if (s2k.getType() == 4 && pGPSecretKey.getS2KUsage() != 253) {
                    throw new PGPKeyValidationException("Argon2 without AEAD is not allowed.");
                }
                if (getVersion() == 6) {
                    if (pGPSecretKey.getS2KUsage() == 255) {
                        throw new PGPKeyValidationException("Version 6 keys MUST NOT use malleable CFB.");
                    }
                    if (s2k.getType() == 0) {
                        throw new PGPKeyValidationException("Version 6 keys MUST NOT use SIMPLE S2K.");
                    }
                }
            }
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPCertificate.OpenPGPComponentKey, org.bouncycastle.openpgp.api.OpenPGPCertificate.OpenPGPCertificateComponent
        public OpenPGPCertificate.OpenPGPComponentSignature getLatestSelfSignature(Date date) {
            return getPublicKey().getLatestSelfSignature(date);
        }

        public OpenPGPKey getOpenPGPKey() {
            return (OpenPGPKey) getCertificate();
        }

        public PGPSecretKey getPGPSecretKey() {
            return this.rawSecKey;
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPCertificate.OpenPGPCertificateComponent
        protected OpenPGPCertificate.OpenPGPCertificateComponent getPublicComponent() {
            return this.pubKey;
        }

        public OpenPGPCertificate.OpenPGPComponentKey getPublicKey() {
            return this.pubKey;
        }

        public boolean isLocked() {
            return getPGPSecretKey().getS2KUsage() != 0;
        }

        public boolean isPassphraseCorrect(char[] cArr) {
            if (cArr != null && !isLocked()) {
                return false;
            }
            try {
                OpenPGPPrivateKey openPGPPrivateKeyUnlock = unlock(cArr);
                return (openPGPPrivateKeyUnlock == null || openPGPPrivateKeyUnlock.unlockedKey == null) ? false : true;
            } catch (PGPException e) {
                return false;
            }
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPCertificate.OpenPGPComponentKey
        public boolean isPrimaryKey() {
            return getPublicKey().isPrimaryKey();
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPCertificate.OpenPGPCertificateComponent
        public String toDetailString() {
            return "Private" + this.pubKey.toDetailString();
        }

        public OpenPGPPrivateKey unlock() throws PGPException {
            return unlock((char[]) null);
        }

        public OpenPGPPrivateKey unlock(KeyPassphraseProvider keyPassphraseProvider) throws PGPException {
            char[] keyPassword;
            if (isLocked()) {
                keyPassword = keyPassphraseProvider.getKeyPassword(this);
            } else {
                keyPassword = null;
            }
            return unlock(keyPassword);
        }

        public OpenPGPPrivateKey unlock(char[] cArr) throws PGPException {
            PBESecretKeyDecryptor pBESecretKeyDecryptorBuild;
            sanitizeProtectionMode();
            if (cArr != null) {
                try {
                    pBESecretKeyDecryptorBuild = this.decryptorBuilderProvider.provide().build(cArr);
                } catch (PGPException e) {
                    throw new KeyPassphraseException(this, e);
                }
            } else {
                pBESecretKeyDecryptorBuild = null;
            }
            PGPPrivateKey pGPPrivateKeyExtractPrivateKey = getPGPSecretKey().extractPrivateKey(pBESecretKeyDecryptorBuild);
            if (pGPPrivateKeyExtractPrivateKey == null) {
                return null;
            }
            return new OpenPGPPrivateKey(this, new PGPKeyPair(getPGPSecretKey().getPublicKey(), pGPPrivateKeyExtractPrivateKey));
        }
    }

    public OpenPGPKey(PGPSecretKeyRing pGPSecretKeyRing) {
        this(pGPSecretKeyRing, OpenPGPImplementation.getInstance());
    }

    public OpenPGPKey(PGPSecretKeyRing pGPSecretKeyRing, OpenPGPImplementation openPGPImplementation) {
        this(pGPSecretKeyRing, openPGPImplementation, openPGPImplementation.policy());
    }

    public OpenPGPKey(PGPSecretKeyRing pGPSecretKeyRing, OpenPGPImplementation openPGPImplementation, OpenPGPPolicy openPGPPolicy) {
        super(pGPSecretKeyRing, openPGPImplementation, openPGPPolicy);
        this.secretKeys = new LinkedHashMap();
        for (OpenPGPCertificate.OpenPGPComponentKey openPGPComponentKey : getKeys()) {
            KeyIdentifier keyIdentifier = openPGPComponentKey.getKeyIdentifier();
            PGPSecretKey secretKey = pGPSecretKeyRing.getSecretKey(keyIdentifier);
            if (secretKey != null) {
                this.secretKeys.put(keyIdentifier, new OpenPGPSecretKey(openPGPComponentKey, secretKey, openPGPImplementation.pbeSecretKeyDecryptorBuilderProvider()));
            }
        }
    }

    @Override // org.bouncycastle.openpgp.api.OpenPGPCertificate
    public List<OpenPGPCertificate.OpenPGPCertificateComponent> getComponents() {
        OpenPGPSecretKey secretKey;
        List<OpenPGPCertificate.OpenPGPCertificateComponent> components = super.getComponents();
        for (int size = components.size() - 1; size >= 0; size--) {
            OpenPGPCertificate.OpenPGPCertificateComponent openPGPCertificateComponent = components.get(size);
            if ((openPGPCertificateComponent instanceof OpenPGPCertificate.OpenPGPComponentKey) && (secretKey = getSecretKey((OpenPGPCertificate.OpenPGPComponentKey) openPGPCertificateComponent)) != null) {
                components.remove(size);
                components.add(size, secretKey);
            }
        }
        return components;
    }

    @Override // org.bouncycastle.openpgp.api.OpenPGPCertificate
    public byte[] getEncoded(PacketFormat packetFormat) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        BCPGOutputStream bCPGOutputStream = new BCPGOutputStream(byteArrayOutputStream, packetFormat);
        getPGPSecretKeyRing().encode(bCPGOutputStream);
        bCPGOutputStream.close();
        return byteArrayOutputStream.toByteArray();
    }

    @Override // org.bouncycastle.openpgp.api.OpenPGPCertificate
    public PGPSecretKeyRing getPGPKeyRing() {
        return getPGPSecretKeyRing();
    }

    public PGPSecretKeyRing getPGPSecretKeyRing() {
        return (PGPSecretKeyRing) super.getPGPKeyRing();
    }

    public OpenPGPSecretKey getPrimarySecretKey() {
        return getSecretKey(getPrimaryKey());
    }

    public OpenPGPSecretKey getSecretKey(KeyIdentifier keyIdentifier) {
        return this.secretKeys.get(keyIdentifier);
    }

    public OpenPGPSecretKey getSecretKey(OpenPGPCertificate.OpenPGPComponentKey openPGPComponentKey) {
        return getSecretKey(openPGPComponentKey.getKeyIdentifier());
    }

    public Map<KeyIdentifier, OpenPGPSecretKey> getSecretKeys() {
        return new LinkedHashMap(this.secretKeys);
    }

    @Override // org.bouncycastle.openpgp.api.OpenPGPCertificate
    public boolean isSecretKey() {
        return true;
    }

    void replaceSecretKey(OpenPGPSecretKey openPGPSecretKey) {
        this.keyRing = PGPSecretKeyRing.insertSecretKey((PGPSecretKeyRing) this.keyRing, openPGPSecretKey.rawSecKey);
        this.secretKeys.put(openPGPSecretKey.getKeyIdentifier(), openPGPSecretKey);
    }

    public OpenPGPCertificate toCertificate() {
        return new OpenPGPCertificate(getPGPPublicKeyRing(), this.implementation, this.policy);
    }
}
