package org.bouncycastle.openpgp;

import java.io.InputStream;
import org.bouncycastle.bcpg.InputStreamPacket;
import org.bouncycastle.bcpg.SymmetricEncDataPacket;
import org.bouncycastle.bcpg.SymmetricEncIntegrityPacket;
import org.bouncycastle.bcpg.SymmetricKeyEncSessionPacket;
import org.bouncycastle.bcpg.UnsupportedPacketVersionException;
import org.bouncycastle.openpgp.operator.PBEDataDecryptorFactory;
import org.bouncycastle.openpgp.operator.SessionKeyDataDecryptorFactory;
import org.bouncycastle.util.Arrays;

/* JADX INFO: loaded from: classes.dex */
public class PGPPBEEncryptedData extends PGPSymmetricKeyEncryptedData {
    SymmetricKeyEncSessionPacket keyData;

    PGPPBEEncryptedData(SymmetricKeyEncSessionPacket symmetricKeyEncSessionPacket, InputStreamPacket inputStreamPacket) {
        super(inputStreamPacket);
        this.keyData = symmetricKeyEncSessionPacket;
        enforceConstraints(symmetricKeyEncSessionPacket, inputStreamPacket);
    }

    private static void enforceConstraints(SymmetricKeyEncSessionPacket symmetricKeyEncSessionPacket, InputStreamPacket inputStreamPacket) {
        switch (symmetricKeyEncSessionPacket.getVersion()) {
            case 4:
                if (inputStreamPacket instanceof SymmetricEncDataPacket) {
                    return;
                }
                if (inputStreamPacket instanceof SymmetricEncIntegrityPacket) {
                    SymmetricEncIntegrityPacket symmetricEncIntegrityPacket = (SymmetricEncIntegrityPacket) inputStreamPacket;
                    if (symmetricEncIntegrityPacket.getVersion() != 1) {
                        throw new IllegalArgumentException("Version 4 SKESK cannot precede SEIPD of version " + symmetricEncIntegrityPacket.getVersion());
                    }
                    return;
                }
                break;
            case 6:
                if (!(inputStreamPacket instanceof SymmetricEncIntegrityPacket)) {
                    throw new IllegalArgumentException("Version 6 SKESK MUST be followed only by SEIPD version 2");
                }
                if (((SymmetricEncIntegrityPacket) inputStreamPacket).getVersion() != 2) {
                    throw new IllegalArgumentException("Version 6 SKESK MUST be followed only by SEIPD version 2");
                }
                break;
        }
        if ((inputStreamPacket instanceof SymmetricEncDataPacket) && symmetricKeyEncSessionPacket.getVersion() != 4) {
            throw new IllegalArgumentException("Version of SKESK packet preceding a SED packet can only be 4.");
        }
        if (inputStreamPacket instanceof SymmetricEncIntegrityPacket) {
            SymmetricEncIntegrityPacket symmetricEncIntegrityPacket2 = (SymmetricEncIntegrityPacket) inputStreamPacket;
            if (symmetricKeyEncSessionPacket.getVersion() == 4 && symmetricEncIntegrityPacket2.getVersion() != 1) {
                throw new IllegalArgumentException("Version 4 SKESK can only precede version 1 SEIPD.");
            }
            if (symmetricKeyEncSessionPacket.getVersion() == 6 && symmetricEncIntegrityPacket2.getVersion() == 1) {
                throw new IllegalArgumentException("Version 6 SKESK packet MUST NOT precede a V1 SEIPD packet.");
            }
        }
    }

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

    public InputStream getDataStream(PBEDataDecryptorFactory pBEDataDecryptorFactory) throws PGPException {
        try {
            this.encStream = createDecryptionStream(pBEDataDecryptorFactory, getSessionKey(pBEDataDecryptorFactory));
            return this.encStream;
        } catch (PGPException e) {
            throw e;
        } catch (Exception e2) {
            throw new PGPException("Exception creating cipher", e2);
        }
    }

    public InputStream getDataStream(SessionKeyDataDecryptorFactory sessionKeyDataDecryptorFactory) throws PGPException {
        try {
            this.encStream = createDecryptionStream(sessionKeyDataDecryptorFactory, sessionKeyDataDecryptorFactory.getSessionKey());
            return this.encStream;
        } catch (PGPException e) {
            throw e;
        } catch (Exception e2) {
            throw new PGPException("Exception creating cipher", e2);
        }
    }

    public PGPSessionKey getSessionKey(PBEDataDecryptorFactory pBEDataDecryptorFactory) throws PGPException {
        byte[] bArrMakeKeyFromPassPhrase = pBEDataDecryptorFactory.makeKeyFromPassPhrase(this.keyData.getEncAlgorithm(), this.keyData.getS2K());
        int version = getVersion();
        if (version == 4) {
            byte[] bArrRecoverSessionData = pBEDataDecryptorFactory.recoverSessionData(this.keyData.getEncAlgorithm(), bArrMakeKeyFromPassPhrase, this.keyData.getSecKeyData());
            return new PGPSessionKey(bArrRecoverSessionData[0] & 255, Arrays.copyOfRange(bArrRecoverSessionData, 1, bArrRecoverSessionData.length));
        }
        if (version == 5 || version == 6) {
            return new PGPSessionKey(getSymmetricAlgorithm(pBEDataDecryptorFactory), pBEDataDecryptorFactory.recoverAEADEncryptedSessionData(this.keyData, bArrMakeKeyFromPassPhrase));
        }
        throw new UnsupportedPacketVersionException("Unsupported packet version: " + version);
    }

    public int getSymmetricAlgorithm(PBEDataDecryptorFactory pBEDataDecryptorFactory) throws PGPException {
        int version = this.keyData.getVersion();
        SymmetricKeyEncSessionPacket symmetricKeyEncSessionPacket = this.keyData;
        if (version == 4) {
            return pBEDataDecryptorFactory.recoverSessionData(this.keyData.getEncAlgorithm(), pBEDataDecryptorFactory.makeKeyFromPassPhrase(symmetricKeyEncSessionPacket.getEncAlgorithm(), this.keyData.getS2K()), this.keyData.getSecKeyData())[0];
        }
        return symmetricKeyEncSessionPacket.getVersion() == 5 ? this.keyData.getEncAlgorithm() : ((SymmetricEncIntegrityPacket) this.encData).getCipherAlgorithm();
    }

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