package org.bouncycastle.openpgp.api;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.bouncycastle.bcpg.BCPGInputStream;
import org.bouncycastle.openpgp.PGPMarker;
import org.bouncycastle.openpgp.PGPObjectFactory;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPUtil;
import org.bouncycastle.util.Strings;
import org.bouncycastle.util.io.Streams;

/* JADX INFO: loaded from: classes2.dex */
public class OpenPGPKeyReader {
    private final OpenPGPImplementation implementation;
    private final OpenPGPPolicy policy;

    public OpenPGPKeyReader() {
        this(OpenPGPImplementation.getInstance());
    }

    public OpenPGPKeyReader(OpenPGPImplementation openPGPImplementation) {
        this(openPGPImplementation, openPGPImplementation.policy());
    }

    public OpenPGPKeyReader(OpenPGPImplementation openPGPImplementation, OpenPGPPolicy openPGPPolicy) {
        this.implementation = openPGPImplementation;
        this.policy = openPGPPolicy;
    }

    public OpenPGPCertificate parseCertificate(InputStream inputStream) throws IOException {
        OpenPGPCertificate keyOrCertificate = parseKeyOrCertificate(inputStream);
        if (keyOrCertificate instanceof OpenPGPKey) {
            throw new IOException("Could not parse OpenPGPCertificate: Is OpenPGPKey.");
        }
        return keyOrCertificate;
    }

    public OpenPGPCertificate parseCertificate(String str) throws IOException {
        OpenPGPCertificate keyOrCertificate = parseKeyOrCertificate(str);
        if (keyOrCertificate instanceof OpenPGPKey) {
            throw new IOException("Could not parse OpenPGPCertificate: Is OpenPGPKey.");
        }
        return keyOrCertificate;
    }

    public OpenPGPCertificate parseCertificate(byte[] bArr) throws IOException {
        OpenPGPCertificate keyOrCertificate = parseKeyOrCertificate(bArr);
        if (keyOrCertificate instanceof OpenPGPKey) {
            throw new IOException("Could not parse OpenPGPCertificate: Is OpenPGPKey.");
        }
        return keyOrCertificate;
    }

    @Deprecated
    public OpenPGPCertificate parseCertificateOrKey(InputStream inputStream) throws IOException {
        return parseKeyOrCertificate(inputStream);
    }

    @Deprecated
    public OpenPGPCertificate parseCertificateOrKey(String str) throws IOException {
        return parseKeyOrCertificate(str);
    }

    @Deprecated
    public OpenPGPCertificate parseCertificateOrKey(byte[] bArr) throws IOException {
        return parseKeyOrCertificate(bArr);
    }

    public List<OpenPGPCertificate> parseCertificates(InputStream inputStream) throws IOException {
        return parseCertificates(Streams.readAll(inputStream));
    }

    public List<OpenPGPCertificate> parseCertificates(String str) throws IOException {
        return parseCertificates(Strings.toUTF8ByteArray(str));
    }

    public List<OpenPGPCertificate> parseCertificates(byte[] bArr) throws IOException {
        ArrayList arrayList = new ArrayList();
        PGPObjectFactory pgpObjectFactory = this.implementation.pgpObjectFactory(PGPUtil.getDecoderStream(PGPUtil.getDecoderStream(new ByteArrayInputStream(bArr))));
        while (true) {
            Object objNextObject = pgpObjectFactory.nextObject();
            if (objNextObject == null) {
                return arrayList;
            }
            if (!(objNextObject instanceof PGPMarker)) {
                if (!(objNextObject instanceof PGPPublicKeyRing)) {
                    throw new IOException("Encountered unexpected packet: " + objNextObject.getClass().getName());
                }
                arrayList.add(new OpenPGPCertificate((PGPPublicKeyRing) objNextObject, this.implementation, this.policy));
            }
        }
    }

    public OpenPGPKey parseKey(InputStream inputStream) throws IOException {
        return parseKey(Streams.readAll(inputStream));
    }

    public OpenPGPKey parseKey(String str) throws IOException {
        return parseKey(Strings.toUTF8ByteArray(str));
    }

    public OpenPGPKey parseKey(byte[] bArr) throws IOException {
        Object objNextObject;
        PGPObjectFactory pgpObjectFactory = this.implementation.pgpObjectFactory(BCPGInputStream.wrap(PGPUtil.getDecoderStream(new ByteArrayInputStream(bArr))));
        do {
            objNextObject = pgpObjectFactory.nextObject();
        } while (objNextObject instanceof PGPMarker);
        if (objNextObject instanceof PGPSecretKeyRing) {
            return new OpenPGPKey((PGPSecretKeyRing) objNextObject, this.implementation, this.policy);
        }
        throw new IOException("Not a secret key.");
    }

    public OpenPGPCertificate parseKeyOrCertificate(InputStream inputStream) throws IOException {
        return parseKeyOrCertificate(Streams.readAll(inputStream));
    }

    public OpenPGPCertificate parseKeyOrCertificate(String str) throws IOException {
        return parseKeyOrCertificate(Strings.toUTF8ByteArray(str));
    }

    public OpenPGPCertificate parseKeyOrCertificate(byte[] bArr) throws IOException {
        Object objNextObject;
        PGPObjectFactory pgpObjectFactory = this.implementation.pgpObjectFactory(BCPGInputStream.wrap(PGPUtil.getDecoderStream(new ByteArrayInputStream(bArr))));
        do {
            objNextObject = pgpObjectFactory.nextObject();
        } while (objNextObject instanceof PGPMarker);
        if (objNextObject instanceof PGPSecretKeyRing) {
            return new OpenPGPKey((PGPSecretKeyRing) objNextObject, this.implementation, this.policy);
        }
        if (objNextObject instanceof PGPPublicKeyRing) {
            return new OpenPGPCertificate((PGPPublicKeyRing) objNextObject, this.implementation, this.policy);
        }
        throw new IOException("Neither a certificate, nor secret key.");
    }

    public List<OpenPGPKey> parseKeys(InputStream inputStream) throws IOException {
        return parseKeys(Streams.readAll(inputStream));
    }

    public List<OpenPGPKey> parseKeys(String str) throws IOException {
        return parseKeys(Strings.toUTF8ByteArray(str));
    }

    public List<OpenPGPKey> parseKeys(byte[] bArr) throws IOException {
        ArrayList arrayList = new ArrayList();
        PGPObjectFactory pgpObjectFactory = this.implementation.pgpObjectFactory(PGPUtil.getDecoderStream(PGPUtil.getDecoderStream(new ByteArrayInputStream(bArr))));
        while (true) {
            Object objNextObject = pgpObjectFactory.nextObject();
            if (objNextObject == null) {
                return arrayList;
            }
            if (!(objNextObject instanceof PGPMarker)) {
                if (!(objNextObject instanceof PGPSecretKeyRing)) {
                    throw new IOException("Encountered unexpected packet: " + objNextObject.getClass().getName());
                }
                arrayList.add(new OpenPGPKey((PGPSecretKeyRing) objNextObject, this.implementation, this.policy));
            }
        }
    }

    public List<OpenPGPCertificate> parseKeysOrCertificates(InputStream inputStream) throws IOException {
        return parseKeysOrCertificates(Streams.readAll(inputStream));
    }

    public List<OpenPGPCertificate> parseKeysOrCertificates(String str) throws IOException {
        return parseKeysOrCertificates(Strings.toUTF8ByteArray(str));
    }

    public List<OpenPGPCertificate> parseKeysOrCertificates(byte[] bArr) throws IOException {
        Object openPGPCertificate;
        ArrayList arrayList = new ArrayList();
        PGPObjectFactory pgpObjectFactory = this.implementation.pgpObjectFactory(PGPUtil.getDecoderStream(PGPUtil.getDecoderStream(new ByteArrayInputStream(bArr))));
        while (true) {
            Object objNextObject = pgpObjectFactory.nextObject();
            if (objNextObject == null) {
                return arrayList;
            }
            if (!(objNextObject instanceof PGPMarker)) {
                if (objNextObject instanceof PGPSecretKeyRing) {
                    openPGPCertificate = new OpenPGPKey((PGPSecretKeyRing) objNextObject, this.implementation, this.policy);
                } else {
                    if (!(objNextObject instanceof PGPPublicKeyRing)) {
                        throw new IOException("Neither a certificate, nor secret key.");
                    }
                    openPGPCertificate = new OpenPGPCertificate((PGPPublicKeyRing) objNextObject, this.implementation, this.policy);
                }
                arrayList.add(openPGPCertificate);
            }
        }
    }
}
