package org.bouncycastle.openpgp;

import java.io.InputStream;
import org.bouncycastle.bcpg.AEADEncDataPacket;
import org.bouncycastle.bcpg.InputStreamPacket;
import org.bouncycastle.bcpg.KeyIdentifier;
import org.bouncycastle.bcpg.PublicKeyEncSessionPacket;
import org.bouncycastle.bcpg.SymmetricEncIntegrityPacket;
import org.bouncycastle.bcpg.UnsupportedPacketVersionException;
import org.bouncycastle.openpgp.operator.PGPDataDecryptor;
import org.bouncycastle.openpgp.operator.PGPDataDecryptorFactory;
import org.bouncycastle.openpgp.operator.PublicKeyDataDecryptorFactory;
import org.bouncycastle.openpgp.operator.SessionKeyDataDecryptorFactory;
import org.bouncycastle.util.Arrays;

/* JADX INFO: loaded from: classes2.dex */
public class PGPPublicKeyEncryptedData extends PGPEncryptedData {
    PublicKeyEncSessionPacket keyData;

    PGPPublicKeyEncryptedData(PublicKeyEncSessionPacket publicKeyEncSessionPacket, InputStreamPacket inputStreamPacket) {
        super(inputStreamPacket);
        this.keyData = publicKeyEncSessionPacket;
    }

    private boolean confirmCheckSum(byte[] bArr) {
        int i = 0;
        for (int i2 = 1; i2 != bArr.length - 2; i2++) {
            i += bArr[i2] & 255;
        }
        return bArr[bArr.length + (-2)] == ((byte) (i >> 8)) && bArr[bArr.length - 1] == ((byte) i);
    }

    private boolean containsChecksum(int i) {
        return (i == 25 || i == 26) ? false : true;
    }

    private InputStream getDataStream(PGPDataDecryptorFactory pGPDataDecryptorFactory, PGPSessionKey pGPSessionKey) throws PGPException {
        PGPDataDecryptor pGPDataDecryptorCreateDataDecryptor;
        if (pGPSessionKey.getAlgorithm() == 0) {
            return getInputStream();
        }
        try {
            InputStream inputStream = getInputStream();
            if (!(this.encData instanceof AEADEncDataPacket)) {
                if (this.encData instanceof SymmetricEncIntegrityPacket) {
                    SymmetricEncIntegrityPacket symmetricEncIntegrityPacket = (SymmetricEncIntegrityPacket) this.encData;
                    if (symmetricEncIntegrityPacket.getVersion() == 1) {
                        processSymmetricEncIntegrityPacketDataStream(true, pGPDataDecryptorFactory.createDataDecryptor(true, pGPSessionKey.getAlgorithm(), pGPSessionKey.getKey()), inputStream);
                    } else {
                        pGPDataDecryptorCreateDataDecryptor = pGPDataDecryptorFactory.createDataDecryptor(symmetricEncIntegrityPacket, pGPSessionKey);
                    }
                } else {
                    processSymmetricEncIntegrityPacketDataStream(false, pGPDataDecryptorFactory.createDataDecryptor(false, pGPSessionKey.getAlgorithm(), pGPSessionKey.getKey()), inputStream);
                }
                return this.encStream;
            }
            AEADEncDataPacket aEADEncDataPacket = (AEADEncDataPacket) this.encData;
            if (aEADEncDataPacket.getAlgorithm() != pGPSessionKey.getAlgorithm()) {
                throw new PGPException("session key and AEAD algorithm mismatch");
            }
            pGPDataDecryptorCreateDataDecryptor = pGPDataDecryptorFactory.createDataDecryptor(aEADEncDataPacket, pGPSessionKey);
            this.encStream = pGPDataDecryptorCreateDataDecryptor.getInputStream(inputStream);
            return this.encStream;
        } catch (PGPException e) {
            throw e;
        } catch (Exception e2) {
            throw new PGPException("Exception starting decryption", e2);
        }
    }

    @Override // org.bouncycastle.openpgp.PGPEncryptedData
    public int getAlgorithm() {
        return this.keyData.getAlgorithm();
    }

    public InputStream getDataStream(PublicKeyDataDecryptorFactory publicKeyDataDecryptorFactory) throws PGPException {
        return getDataStream(publicKeyDataDecryptorFactory, getSessionKey(publicKeyDataDecryptorFactory));
    }

    public InputStream getDataStream(SessionKeyDataDecryptorFactory sessionKeyDataDecryptorFactory) throws PGPException {
        return getDataStream(sessionKeyDataDecryptorFactory, sessionKeyDataDecryptorFactory.getSessionKey());
    }

    @Deprecated
    public long getKeyID() {
        return this.keyData.getKeyID();
    }

    public KeyIdentifier getKeyIdentifier() {
        return new KeyIdentifier(this.keyData.getKeyFingerprint(), this.keyData.getKeyID());
    }

    public PGPSessionKey getSessionKey(PublicKeyDataDecryptorFactory publicKeyDataDecryptorFactory) throws PGPException {
        int cipherAlgorithm;
        byte[] bArrRecoverSessionData = publicKeyDataDecryptorFactory.recoverSessionData(this.keyData, this.encData);
        if (containsChecksum(this.keyData.getAlgorithm())) {
            if (!confirmCheckSum(bArrRecoverSessionData)) {
                throw new PGPException("Key checksum failed.");
            }
            bArrRecoverSessionData = Arrays.copyOf(bArrRecoverSessionData, bArrRecoverSessionData.length - 2);
        }
        byte[] bArrCopyOfRange = Arrays.copyOfRange(bArrRecoverSessionData, 1, bArrRecoverSessionData.length);
        if (this.encData instanceof AEADEncDataPacket) {
            cipherAlgorithm = ((AEADEncDataPacket) this.encData).getAlgorithm();
        } else if (this.encData instanceof SymmetricEncIntegrityPacket) {
            SymmetricEncIntegrityPacket symmetricEncIntegrityPacket = (SymmetricEncIntegrityPacket) this.encData;
            if (symmetricEncIntegrityPacket.getVersion() == 1) {
                cipherAlgorithm = bArrRecoverSessionData[0];
            } else {
                if (symmetricEncIntegrityPacket.getVersion() != 2) {
                    throw new UnsupportedPacketVersionException("Unsupported SEIPD packet version: " + symmetricEncIntegrityPacket.getVersion());
                }
                cipherAlgorithm = symmetricEncIntegrityPacket.getCipherAlgorithm();
            }
        } else {
            cipherAlgorithm = bArrRecoverSessionData[0];
        }
        return new PGPSessionKey(cipherAlgorithm & 255, bArrCopyOfRange);
    }

    public int getSymmetricAlgorithm(PublicKeyDataDecryptorFactory publicKeyDataDecryptorFactory) throws PGPException {
        int version = this.keyData.getVersion();
        PublicKeyEncSessionPacket publicKeyEncSessionPacket = this.keyData;
        if (version == 3) {
            return publicKeyDataDecryptorFactory.recoverSessionData(publicKeyEncSessionPacket, this.encData)[0];
        }
        if (publicKeyEncSessionPacket.getVersion() == 6) {
            return ((SymmetricEncIntegrityPacket) this.encData).getCipherAlgorithm();
        }
        throw new UnsupportedPacketVersionException("Unsupported packet version: " + this.keyData.getVersion());
    }

    @Override // org.bouncycastle.openpgp.PGPEncryptedData
    public int getVersion() {
        return this.keyData.getVersion();
    }
}
