package org.bouncycastle.openpgp.api;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.bouncycastle.bcpg.AEADEncDataPacket;
import org.bouncycastle.bcpg.BCPGInputStream;
import org.bouncycastle.bcpg.KeyIdentifier;
import org.bouncycastle.bcpg.SymmetricEncIntegrityPacket;
import org.bouncycastle.openpgp.PGPCompressedData;
import org.bouncycastle.openpgp.PGPEncryptedDataList;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPLiteralData;
import org.bouncycastle.openpgp.PGPMarker;
import org.bouncycastle.openpgp.PGPObjectFactory;
import org.bouncycastle.openpgp.PGPOnePassSignature;
import org.bouncycastle.openpgp.PGPOnePassSignatureList;
import org.bouncycastle.openpgp.PGPPadding;
import org.bouncycastle.openpgp.PGPSessionKey;
import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.openpgp.PGPSignatureException;
import org.bouncycastle.openpgp.PGPSignatureList;

/* JADX INFO: loaded from: classes3.dex */
public class OpenPGPMessageInputStream extends InputStream {
    public static int MAX_RECURSION = 16;
    private final List<PacketHandler> closeHandlers;
    private final OpenPGPImplementation implementation;
    private InputStream in;
    private final Layer layer;
    private final PGPObjectFactory objectFactory;
    private final List<PacketHandler> packetHandlers;
    private final OpenPGPMessageProcessor processor;
    private final Result.Builder resultBuilder;

    /* JADX INFO: loaded from: classes2.dex */
    static class CompressedData extends Nested {
        private final int compressionAlgorithm;

        public CompressedData(int i) {
            this.compressionAlgorithm = i;
        }
    }

    /* JADX INFO: loaded from: classes.dex */
    private class CompressedDataHandler extends PacketHandler {
        private CompressedDataHandler() {
            super();
        }

        private void processNestedStream(InputStream inputStream) throws IOException, PGPException {
            OpenPGPMessageInputStream openPGPMessageInputStream = new OpenPGPMessageInputStream(OpenPGPMessageInputStream.this.implementation.pgpObjectFactory(BCPGInputStream.wrap(inputStream)), OpenPGPMessageInputStream.this.processor, OpenPGPMessageInputStream.this.resultBuilder);
            OpenPGPMessageInputStream.this.in = openPGPMessageInputStream;
            openPGPMessageInputStream.process();
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPMessageInputStream.PacketHandler
        public boolean canHandle(Object obj) {
            return obj instanceof PGPCompressedData;
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPMessageInputStream.PacketHandler
        public void handle(Object obj) throws IOException, PGPException {
            PGPCompressedData pGPCompressedData = (PGPCompressedData) obj;
            OpenPGPMessageInputStream.this.resultBuilder.compressed(pGPCompressedData.getAlgorithm());
            processNestedStream(pGPCompressedData.getDataStream());
        }
    }

    private class DefaultPacketHandler extends PacketHandler {
        private DefaultPacketHandler() {
            super();
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPMessageInputStream.PacketHandler
        public boolean canHandle(Object obj) {
            return true;
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPMessageInputStream.PacketHandler
        public void handle(Object obj) throws PGPException {
            OpenPGPMessageInputStream.this.processor.onException(new PGPException("Unexpected packet: " + obj.getClass().getName()));
        }
    }

    /* JADX INFO: loaded from: classes.dex */
    static class EncryptedData extends Nested {
        private final OpenPGPCertificate.OpenPGPComponentKey decryptionKey;
        private final char[] decryptionPassphrase;
        private final MessageEncryptionMechanism encryption;
        private final PGPSessionKey sessionKey;

        EncryptedData(OpenPGPMessageProcessor.Decrypted decrypted) {
            MessageEncryptionMechanism messageEncryptionMechanismLibrePgp;
            this.decryptionKey = decrypted.decryptionKey;
            this.decryptionPassphrase = decrypted.decryptionPassphrase;
            this.sessionKey = decrypted.sessionKey;
            if (decrypted.esk.getEncData() instanceof SymmetricEncIntegrityPacket) {
                SymmetricEncIntegrityPacket symmetricEncIntegrityPacket = (SymmetricEncIntegrityPacket) decrypted.esk.getEncData();
                messageEncryptionMechanismLibrePgp = symmetricEncIntegrityPacket.getVersion() == 2 ? MessageEncryptionMechanism.aead(symmetricEncIntegrityPacket.getCipherAlgorithm(), symmetricEncIntegrityPacket.getAeadAlgorithm()) : MessageEncryptionMechanism.integrityProtected(this.sessionKey.getAlgorithm());
            } else {
                if (!(decrypted.esk.getEncData() instanceof AEADEncDataPacket)) {
                    throw new RuntimeException("Unexpected encrypted data packet type: " + decrypted.esk.getClass().getName());
                }
                messageEncryptionMechanismLibrePgp = MessageEncryptionMechanism.librePgp(this.sessionKey.getAlgorithm());
            }
            this.encryption = messageEncryptionMechanismLibrePgp;
        }
    }

    private class EncryptedDataHandler extends PacketHandler {
        private EncryptedDataHandler() {
            super();
        }

        private void processNestedStream(InputStream inputStream) throws IOException, PGPException {
            OpenPGPMessageInputStream openPGPMessageInputStream = new OpenPGPMessageInputStream(OpenPGPMessageInputStream.this.implementation.pgpObjectFactory(BCPGInputStream.wrap(inputStream)), OpenPGPMessageInputStream.this.processor, OpenPGPMessageInputStream.this.resultBuilder);
            OpenPGPMessageInputStream.this.in = openPGPMessageInputStream;
            openPGPMessageInputStream.process();
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPMessageInputStream.PacketHandler
        public boolean canHandle(Object obj) {
            return obj instanceof PGPEncryptedDataList;
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPMessageInputStream.PacketHandler
        public void handle(Object obj) throws IOException, PGPException {
            OpenPGPMessageProcessor.Decrypted decryptedDecrypt = OpenPGPMessageInputStream.this.processor.decrypt((PGPEncryptedDataList) obj);
            OpenPGPMessageInputStream.this.resultBuilder.encrypted(decryptedDecrypt);
            processNestedStream(decryptedDecrypt.inputStream);
        }
    }

    /* JADX INFO: loaded from: classes.dex */
    static class Layer {
        private Nested nested;
        private final OnePassSignatures onePassSignatures = new OnePassSignatures();
        private final PrefixedSignatures prefixedSignatures = new PrefixedSignatures();
        private List<OpenPGPSignature.OpenPGPDocumentSignature> signatures = null;
        private boolean open = true;

        Layer() {
        }

        void close() {
            this.open = false;
        }

        boolean isOpen() {
            return this.open;
        }

        void setNested(Nested nested) {
            this.nested = nested;
        }
    }

    /* JADX INFO: loaded from: classes.dex */
    static class LiteralData extends Nested {
        private final char encoding;
        private final String filename;
        private final Date modificationTime;

        LiteralData(String str, char c, Date date) {
            this.filename = str;
            this.encoding = c;
            this.modificationTime = date;
        }
    }

    /* JADX INFO: loaded from: classes.dex */
    private class LiteralDataHandler extends PacketHandler {
        private LiteralDataHandler() {
            super();
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPMessageInputStream.PacketHandler
        public boolean canHandle(Object obj) {
            return obj instanceof PGPLiteralData;
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPMessageInputStream.PacketHandler
        public void handle(Object obj) throws IOException, PGPException {
            PGPLiteralData pGPLiteralData = (PGPLiteralData) obj;
            OpenPGPMessageInputStream.this.resultBuilder.literalData(pGPLiteralData.getFileName(), (char) pGPLiteralData.getFormat(), pGPLiteralData.getModificationTime());
            OpenPGPMessageInputStream.this.in = pGPLiteralData.getDataStream();
            OpenPGPMessageInputStream.this.resultBuilder.initSignatures(OpenPGPMessageInputStream.this.processor);
        }
    }

    /* JADX INFO: loaded from: classes2.dex */
    private static class MarkerHandler extends PacketHandler {
        private MarkerHandler() {
            super();
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPMessageInputStream.PacketHandler
        public boolean canHandle(Object obj) {
            return obj instanceof PGPMarker;
        }
    }

    /* JADX INFO: loaded from: classes.dex */
    static class Nested {
        Nested() {
        }
    }

    private class OnePassSignatureHandler extends PacketHandler {
        private OnePassSignatureHandler() {
            super();
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPMessageInputStream.PacketHandler
        public boolean canHandle(Object obj) {
            return obj instanceof PGPOnePassSignatureList;
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPMessageInputStream.PacketHandler
        public void handle(Object obj) throws IOException, PGPException {
            OpenPGPMessageInputStream.this.resultBuilder.onePassSignatures((PGPOnePassSignatureList) obj);
        }
    }

    static class OnePassSignatures {
        private final List<PGPOnePassSignature> onePassSignatures = new ArrayList();
        private final List<PGPSignature> signatures = new ArrayList();
        private final Map<PGPOnePassSignature, OpenPGPCertificate.OpenPGPComponentKey> issuers = new HashMap();

        OnePassSignatures() {
        }

        void addOnePassSignatures(PGPOnePassSignatureList pGPOnePassSignatureList) {
            Iterator<PGPOnePassSignature> it = pGPOnePassSignatureList.iterator();
            while (it.hasNext()) {
                this.onePassSignatures.add(it.next());
            }
        }

        void addSignatures(PGPSignatureList pGPSignatureList) {
            Iterator<PGPSignature> it = pGPSignatureList.iterator();
            while (it.hasNext()) {
                this.signatures.add(it.next());
            }
        }

        void init(OpenPGPMessageProcessor openPGPMessageProcessor) {
            for (PGPOnePassSignature pGPOnePassSignature : this.onePassSignatures) {
                KeyIdentifier keyIdentifier = pGPOnePassSignature.getKeyIdentifier();
                OpenPGPCertificate openPGPCertificateProvideCertificate = openPGPMessageProcessor.provideCertificate(keyIdentifier);
                if (openPGPCertificateProvideCertificate != null) {
                    try {
                        OpenPGPCertificate.OpenPGPComponentKey key = openPGPCertificateProvideCertificate.getKey(keyIdentifier);
                        this.issuers.put(pGPOnePassSignature, key);
                        pGPOnePassSignature.init(openPGPMessageProcessor.getImplementation().pgpContentVerifierBuilderProvider(), key.getPGPPublicKey());
                    } catch (PGPException e) {
                        openPGPMessageProcessor.onException(e);
                    }
                }
            }
        }

        void update(int i) {
            for (PGPOnePassSignature pGPOnePassSignature : this.onePassSignatures) {
                if (this.issuers.containsKey(pGPOnePassSignature)) {
                    pGPOnePassSignature.update((byte) i);
                }
            }
        }

        void update(byte[] bArr, int i, int i2) {
            for (PGPOnePassSignature pGPOnePassSignature : this.onePassSignatures) {
                if (this.issuers.containsKey(pGPOnePassSignature)) {
                    pGPOnePassSignature.update(bArr, i, i2);
                }
            }
        }

        List<OpenPGPSignature.OpenPGPDocumentSignature> verify(OpenPGPMessageProcessor openPGPMessageProcessor) {
            OpenPGPPolicy openPGPPolicyPolicy = openPGPMessageProcessor.getImplementation().policy();
            ArrayList arrayList = new ArrayList();
            int size = this.onePassSignatures.size();
            for (int i = 0; i < this.signatures.size(); i++) {
                PGPSignature pGPSignature = this.signatures.get(i);
                PGPOnePassSignature pGPOnePassSignature = this.onePassSignatures.get((size - i) - 1);
                OpenPGPCertificate.OpenPGPComponentKey openPGPComponentKey = this.issuers.get(pGPOnePassSignature);
                if (openPGPComponentKey != null) {
                    OpenPGPSignature.OpenPGPDocumentSignature openPGPDocumentSignature = new OpenPGPSignature.OpenPGPDocumentSignature(pGPSignature, openPGPComponentKey);
                    try {
                        openPGPDocumentSignature.sanitize(openPGPComponentKey, openPGPPolicyPolicy);
                    } catch (PGPSignatureException e) {
                    }
                    if (openPGPDocumentSignature.createdInBounds(openPGPMessageProcessor.getVerifyNotBefore(), openPGPMessageProcessor.getVerifyNotAfter())) {
                        try {
                            openPGPDocumentSignature.verify(pGPOnePassSignature);
                        } catch (PGPException e2) {
                            openPGPMessageProcessor.onException(e2);
                        }
                        arrayList.add(openPGPDocumentSignature);
                    }
                }
            }
            return arrayList;
        }
    }

    private static class PacketHandler {
        private PacketHandler() {
        }

        public boolean canHandle(Object obj) {
            return false;
        }

        public void close(Object obj) throws IOException {
        }

        public void handle(Object obj) throws IOException, PGPException {
        }
    }

    /* JADX INFO: loaded from: classes.dex */
    private static class PaddingHandler extends PacketHandler {
        private PaddingHandler() {
            super();
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPMessageInputStream.PacketHandler
        public boolean canHandle(Object obj) {
            return obj instanceof PGPPadding;
        }
    }

    static class PrefixedSignatures {
        private final List<PGPSignature> prefixedSignatures = new ArrayList();
        private final List<OpenPGPSignature.OpenPGPDocumentSignature> dataSignatures = new ArrayList();

        PrefixedSignatures() {
        }

        void addAll(PGPSignatureList pGPSignatureList) {
            Iterator<PGPSignature> it = pGPSignatureList.iterator();
            while (it.hasNext()) {
                this.prefixedSignatures.add(it.next());
            }
        }

        void init(OpenPGPMessageProcessor openPGPMessageProcessor) {
            List<OpenPGPSignature.OpenPGPDocumentSignature> list;
            OpenPGPSignature.OpenPGPDocumentSignature openPGPDocumentSignature;
            for (PGPSignature pGPSignature : this.prefixedSignatures) {
                KeyIdentifier mostExpressiveIdentifier = OpenPGPSignature.getMostExpressiveIdentifier(pGPSignature.getKeyIdentifiers());
                if (mostExpressiveIdentifier == null) {
                    list = this.dataSignatures;
                    openPGPDocumentSignature = new OpenPGPSignature.OpenPGPDocumentSignature(pGPSignature, null);
                } else {
                    OpenPGPCertificate openPGPCertificateProvideCertificate = openPGPMessageProcessor.provideCertificate(mostExpressiveIdentifier);
                    if (openPGPCertificateProvideCertificate == null) {
                        list = this.dataSignatures;
                        openPGPDocumentSignature = new OpenPGPSignature.OpenPGPDocumentSignature(pGPSignature, null);
                    } else {
                        OpenPGPSignature.OpenPGPDocumentSignature openPGPDocumentSignature2 = new OpenPGPSignature.OpenPGPDocumentSignature(pGPSignature, openPGPCertificateProvideCertificate.getKey(mostExpressiveIdentifier));
                        this.dataSignatures.add(openPGPDocumentSignature2);
                        try {
                            openPGPDocumentSignature2.signature.init(openPGPMessageProcessor.getImplementation().pgpContentVerifierBuilderProvider(), openPGPCertificateProvideCertificate.getKey(mostExpressiveIdentifier).getPGPPublicKey());
                        } catch (PGPException e) {
                            openPGPMessageProcessor.onException(e);
                        }
                    }
                }
                list.add(openPGPDocumentSignature);
            }
        }

        void update(int i) {
            Iterator<PGPSignature> it = this.prefixedSignatures.iterator();
            while (it.hasNext()) {
                it.next().update((byte) i);
            }
        }

        void update(byte[] bArr, int i, int i2) {
            Iterator<PGPSignature> it = this.prefixedSignatures.iterator();
            while (it.hasNext()) {
                it.next().update(bArr, i, i2);
            }
        }

        List<OpenPGPSignature.OpenPGPDocumentSignature> verify(OpenPGPMessageProcessor openPGPMessageProcessor) {
            ArrayList arrayList = new ArrayList();
            OpenPGPPolicy openPGPPolicyPolicy = openPGPMessageProcessor.getImplementation().policy();
            for (OpenPGPSignature.OpenPGPDocumentSignature openPGPDocumentSignature : this.dataSignatures) {
                try {
                    openPGPDocumentSignature.sanitize(openPGPDocumentSignature.issuer, openPGPPolicyPolicy);
                    try {
                        openPGPDocumentSignature.verify();
                    } catch (PGPException e) {
                        openPGPMessageProcessor.onException(e);
                    }
                    arrayList.add(openPGPDocumentSignature);
                } catch (PGPSignatureException e2) {
                    openPGPMessageProcessor.onException(e2);
                }
            }
            return arrayList;
        }
    }

    public static class Result {
        private int compressionAlgorithm;
        private OpenPGPCertificate.OpenPGPComponentKey decryptionKey;
        private char[] decryptionPassphrase;
        private final List<OpenPGPSignature.OpenPGPDocumentSignature> documentSignatures;
        private MessageEncryptionMechanism encryptionMethod;
        private char fileFormat;
        private Date fileModificationTime;
        private String filename;
        private PGPSessionKey sessionKey;

        static class Builder {
            private final List<Layer> layers;

            private Builder() {
                this.layers = new ArrayList();
            }

            Result build() {
                return new Result(this.layers);
            }

            void closeLayer() {
                for (int size = this.layers.size() - 1; size >= 0; size--) {
                    Layer layer = this.layers.get(size);
                    if (layer.isOpen()) {
                        layer.close();
                        return;
                    }
                }
            }

            void compressed(int i) {
                last().setNested(new CompressedData(i));
            }

            void encrypted(OpenPGPMessageProcessor.Decrypted decrypted) {
                last().setNested(new EncryptedData(decrypted));
            }

            void initSignatures(OpenPGPMessageProcessor openPGPMessageProcessor) {
                last().onePassSignatures.init(openPGPMessageProcessor);
                last().prefixedSignatures.init(openPGPMessageProcessor);
            }

            Layer last() {
                return this.layers.get(this.layers.size() - 1);
            }

            void literalData(String str, char c, Date date) {
                last().setNested(new LiteralData(str, c, date));
            }

            void onePassSignatures(PGPOnePassSignatureList pGPOnePassSignatureList) {
                last().onePassSignatures.addOnePassSignatures(pGPOnePassSignatureList);
            }

            Layer openLayer() throws PGPException {
                if (this.layers.size() >= OpenPGPMessageInputStream.MAX_RECURSION) {
                    throw new PGPException("Exceeded maximum packet nesting depth.");
                }
                Layer layer = new Layer();
                this.layers.add(layer);
                return layer;
            }

            void prefixedSignatures(PGPSignatureList pGPSignatureList) {
                last().prefixedSignatures.addAll(pGPSignatureList);
            }

            void verifySignatures(OpenPGPMessageProcessor openPGPMessageProcessor) {
                Layer layerLast = last();
                if (layerLast.signatures != null) {
                    return;
                }
                layerLast.signatures = new ArrayList();
                layerLast.signatures.addAll(layerLast.onePassSignatures.verify(openPGPMessageProcessor));
                layerLast.signatures.addAll(layerLast.prefixedSignatures.verify(openPGPMessageProcessor));
            }
        }

        private Result(List<Layer> list) {
            this.documentSignatures = new ArrayList();
            this.encryptionMethod = MessageEncryptionMechanism.unencrypted();
            this.compressionAlgorithm = 0;
            for (Layer layer : list) {
                if (layer.signatures != null) {
                    this.documentSignatures.addAll(layer.signatures);
                }
                if (layer.nested instanceof EncryptedData) {
                    EncryptedData encryptedData = (EncryptedData) layer.nested;
                    this.encryptionMethod = encryptedData.encryption;
                    this.sessionKey = encryptedData.sessionKey;
                    this.decryptionKey = encryptedData.decryptionKey;
                    this.decryptionPassphrase = encryptedData.decryptionPassphrase;
                } else if (layer.nested instanceof CompressedData) {
                    this.compressionAlgorithm = ((CompressedData) layer.nested).compressionAlgorithm;
                } else if (layer.nested instanceof LiteralData) {
                    LiteralData literalData = (LiteralData) layer.nested;
                    this.filename = literalData.filename;
                    this.fileFormat = literalData.encoding;
                    this.fileModificationTime = literalData.modificationTime;
                }
            }
        }

        static Builder builder() {
            return new Builder();
        }

        public int getCompressionAlgorithm() {
            return this.compressionAlgorithm;
        }

        public OpenPGPCertificate.OpenPGPComponentKey getDecryptionKey() {
            return this.decryptionKey;
        }

        public char[] getDecryptionPassphrase() {
            return this.decryptionPassphrase;
        }

        public MessageEncryptionMechanism getEncryptionMethod() {
            return this.encryptionMethod;
        }

        public char getFileFormat() {
            return this.fileFormat;
        }

        public Date getFileModificationTime() {
            return this.fileModificationTime;
        }

        public String getFilename() {
            return this.filename;
        }

        public PGPSessionKey getSessionKey() {
            return this.sessionKey;
        }

        public List<OpenPGPSignature.OpenPGPDocumentSignature> getSignatures() {
            return new ArrayList(this.documentSignatures);
        }
    }

    private class SignatureListHandler extends PacketHandler {
        private SignatureListHandler() {
            super();
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPMessageInputStream.PacketHandler
        public boolean canHandle(Object obj) {
            return obj instanceof PGPSignatureList;
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPMessageInputStream.PacketHandler
        public void close(Object obj) {
            OpenPGPMessageInputStream.this.resultBuilder.last().onePassSignatures.addSignatures((PGPSignatureList) obj);
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPMessageInputStream.PacketHandler
        public void handle(Object obj) {
            OpenPGPMessageInputStream.this.resultBuilder.prefixedSignatures((PGPSignatureList) obj);
        }
    }

    OpenPGPMessageInputStream(PGPObjectFactory pGPObjectFactory, OpenPGPMessageProcessor openPGPMessageProcessor) {
        this(pGPObjectFactory, openPGPMessageProcessor, Result.builder());
    }

    private OpenPGPMessageInputStream(PGPObjectFactory pGPObjectFactory, OpenPGPMessageProcessor openPGPMessageProcessor, Result.Builder builder) {
        this.packetHandlers = new ArrayList<PacketHandler>() { // from class: org.bouncycastle.openpgp.api.OpenPGPMessageInputStream.1
            {
                add(new SignatureListHandler());
                add(new OnePassSignatureHandler());
                add(new MarkerHandler());
                add(new LiteralDataHandler());
                add(new CompressedDataHandler());
                add(new EncryptedDataHandler());
                add(new DefaultPacketHandler());
            }
        };
        this.closeHandlers = new ArrayList<PacketHandler>() { // from class: org.bouncycastle.openpgp.api.OpenPGPMessageInputStream.2
            {
                add(new SignatureListHandler());
                add(new PaddingHandler());
                add(new MarkerHandler());
                add(new DefaultPacketHandler());
            }
        };
        this.objectFactory = pGPObjectFactory;
        this.processor = openPGPMessageProcessor;
        this.implementation = openPGPMessageProcessor.getImplementation();
        this.resultBuilder = builder;
        try {
            this.layer = builder.openLayer();
        } catch (PGPException e) {
            throw new AssertionError(e);
        }
    }

    @Override // java.io.InputStream, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        this.in.close();
        while (true) {
            Object objNextObject = this.objectFactory.nextObject();
            if (objNextObject == null) {
                this.resultBuilder.verifySignatures(this.processor);
                this.resultBuilder.closeLayer();
                return;
            }
            Iterator<PacketHandler> it = this.closeHandlers.iterator();
            while (true) {
                if (!it.hasNext()) {
                    this.processor.onException(new PGPException("Unexpected trailing packet encountered: " + objNextObject.getClass().getName()));
                    break;
                }
                PacketHandler next = it.next();
                if (next.canHandle(objNextObject)) {
                    next.close(objNextObject);
                    break;
                }
            }
        }
    }

    public Result getResult() {
        return this.resultBuilder.build();
    }

    void process() throws IOException, PGPException {
        do {
            Object objNextObject = this.objectFactory.nextObject();
            if (objNextObject == null) {
                return;
            }
            for (PacketHandler packetHandler : this.packetHandlers) {
                if (packetHandler.canHandle(objNextObject)) {
                    packetHandler.handle(objNextObject);
                    break;
                }
            }
        } while (this.in == null);
    }

    @Override // java.io.InputStream
    public int read() throws IOException {
        int i = this.in.read();
        if (i >= 0) {
            this.layer.onePassSignatures.update(i);
            this.layer.prefixedSignatures.update(i);
        }
        return i;
    }

    @Override // java.io.InputStream
    public int read(byte[] bArr) throws IOException {
        int i = this.in.read(bArr);
        if (i >= 0) {
            this.layer.onePassSignatures.update(bArr, 0, i);
            this.layer.prefixedSignatures.update(bArr, 0, i);
        }
        return i;
    }

    @Override // java.io.InputStream
    public int read(byte[] bArr, int i, int i2) throws IOException {
        int i3 = this.in.read(bArr, i, i2);
        if (i3 > 0) {
            this.layer.onePassSignatures.update(bArr, i, i3);
            this.layer.prefixedSignatures.update(bArr, i, i3);
        }
        return i3;
    }
}
