package org.bouncycastle.openpgp;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bouncycastle.bcpg.ArmoredInputException;
import org.bouncycastle.bcpg.BCPGInputStream;
import org.bouncycastle.bcpg.BCPGOutputStream;
import org.bouncycastle.bcpg.KeyIdentifier;
import org.bouncycastle.bcpg.Packet;
import org.bouncycastle.bcpg.PacketFormat;
import org.bouncycastle.bcpg.PublicKeyPacket;
import org.bouncycastle.bcpg.TrustPacket;
import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator;
import org.bouncycastle.util.Iterable;
import org.bouncycastle.util.Longs;

/* JADX INFO: loaded from: classes2.dex */
public class PGPPublicKeyRing extends PGPKeyRing implements Iterable<PGPPublicKey> {
    private static final Logger LOG = Logger.getLogger(PGPPublicKeyRing.class.getName());
    List<PGPPublicKey> keys;

    public PGPPublicKeyRing(InputStream inputStream, KeyFingerPrintCalculator keyFingerPrintCalculator) throws IOException {
        this.keys = new ArrayList();
        BCPGInputStream bCPGInputStreamWrap = BCPGInputStream.wrap(inputStream);
        int iSkipMarkerAndPaddingPackets = bCPGInputStreamWrap.skipMarkerAndPaddingPackets();
        if (iSkipMarkerAndPaddingPackets != 6 && iSkipMarkerAndPaddingPackets != 14) {
            throw new IOException("public key ring doesn't start with public key tag: tag 0x" + Integer.toHexString(iSkipMarkerAndPaddingPackets));
        }
        PublicKeyPacket publicKeyPacket = readPublicKeyPacket(bCPGInputStreamWrap);
        TrustPacket optionalTrustPacket = readOptionalTrustPacket(bCPGInputStreamWrap);
        List<PGPSignature> signaturesAndTrust = readSignaturesAndTrust(bCPGInputStreamWrap);
        ArrayList arrayList = new ArrayList();
        ArrayList arrayList2 = new ArrayList();
        ArrayList arrayList3 = new ArrayList();
        readUserIDs(bCPGInputStreamWrap, arrayList, arrayList2, arrayList3);
        try {
            this.keys.add(new PGPPublicKey(publicKeyPacket, optionalTrustPacket, signaturesAndTrust, arrayList, arrayList2, arrayList3, keyFingerPrintCalculator));
            while (bCPGInputStreamWrap.nextPacketTag() == 14) {
                PGPPublicKey subkey = readSubkey(bCPGInputStreamWrap, keyFingerPrintCalculator);
                if (subkey != null) {
                    this.keys.add(subkey);
                }
            }
        } catch (PGPException e) {
            throw new IOException("processing exception: " + e.toString());
        }
    }

    public PGPPublicKeyRing(List<PGPPublicKey> list) {
        this.keys = checkKeys(list);
    }

    public PGPPublicKeyRing(byte[] bArr, KeyFingerPrintCalculator keyFingerPrintCalculator) throws IOException {
        this(new ByteArrayInputStream(bArr), keyFingerPrintCalculator);
    }

    private static List<PGPPublicKey> checkKeys(List<PGPPublicKey> list) {
        ArrayList arrayList = new ArrayList(list.size());
        for (int i = 0; i != list.size(); i++) {
            PGPPublicKey pGPPublicKey = list.get(i);
            boolean zIsMasterKey = pGPPublicKey.isMasterKey();
            if (i == 0) {
                if (!zIsMasterKey) {
                    throw new IllegalArgumentException("key 0 must be a master key");
                }
            } else if (zIsMasterKey) {
                throw new IllegalArgumentException("key 0 can be only master key");
            }
            arrayList.add(pGPPublicKey);
        }
        return arrayList;
    }

    public static PGPPublicKeyRing insertPublicKey(PGPPublicKeyRing pGPPublicKeyRing, PGPPublicKey pGPPublicKey) {
        ArrayList arrayList = new ArrayList(pGPPublicKeyRing.keys);
        boolean z = false;
        boolean z2 = false;
        for (int i = 0; i != arrayList.size(); i++) {
            PGPPublicKey pGPPublicKey2 = (PGPPublicKey) arrayList.get(i);
            if (pGPPublicKey2.getKeyID() == pGPPublicKey.getKeyID()) {
                arrayList.set(i, pGPPublicKey);
                z = true;
            }
            if (pGPPublicKey2.isMasterKey()) {
                z2 = true;
            }
        }
        if (!z) {
            if (!pGPPublicKey.isMasterKey()) {
                arrayList.add(pGPPublicKey);
            } else {
                if (z2) {
                    throw new IllegalArgumentException("cannot add a master key to a ring that already has one");
                }
                arrayList.add(0, pGPPublicKey);
            }
        }
        return new PGPPublicKeyRing(arrayList);
    }

    public static PGPPublicKeyRing join(PGPPublicKeyRing pGPPublicKeyRing, PGPPublicKeyRing pGPPublicKeyRing2) throws PGPException {
        return join(pGPPublicKeyRing, pGPPublicKeyRing2, false, false);
    }

    public static PGPPublicKeyRing join(PGPPublicKeyRing pGPPublicKeyRing, PGPPublicKeyRing pGPPublicKeyRing2, boolean z, boolean z2) throws PGPException {
        if (!pGPPublicKeyRing2.getPublicKey().hasFingerprint(pGPPublicKeyRing.getPublicKey().getFingerprint())) {
            throw new IllegalArgumentException("Cannot merge certificates with differing primary keys.");
        }
        HashSet hashSet = new HashSet();
        Iterator<PGPPublicKey> it = pGPPublicKeyRing2.iterator();
        while (it.hasNext()) {
            hashSet.add(Longs.valueOf(it.next().getKeyID()));
        }
        ArrayList arrayList = new ArrayList();
        for (PGPPublicKey pGPPublicKey : pGPPublicKeyRing) {
            PGPPublicKey publicKey = pGPPublicKeyRing2.getPublicKey(pGPPublicKey.getKeyID());
            if (publicKey != null) {
                arrayList.add(PGPPublicKey.join(pGPPublicKey, publicKey, z, z2));
                hashSet.remove(Longs.valueOf(pGPPublicKey.getKeyID()));
            } else {
                arrayList.add(pGPPublicKey);
            }
        }
        Iterator it2 = hashSet.iterator();
        while (it2.hasNext()) {
            arrayList.add(pGPPublicKeyRing2.getPublicKey(((Long) it2.next()).longValue()));
        }
        return new PGPPublicKeyRing(arrayList);
    }

    static PublicKeyPacket readPublicKeyPacket(BCPGInputStream bCPGInputStream) throws IOException {
        Packet packet = bCPGInputStream.readPacket();
        if (packet instanceof PublicKeyPacket) {
            return (PublicKeyPacket) packet;
        }
        throw new IOException("unexpected packet in stream: " + packet);
    }

    static PGPPublicKey readSubkey(BCPGInputStream bCPGInputStream, KeyFingerPrintCalculator keyFingerPrintCalculator) throws IOException, PGPException {
        try {
            return new PGPPublicKey(readPublicKeyPacket(bCPGInputStream), readOptionalTrustPacket(bCPGInputStream), readSignaturesAndTrust(bCPGInputStream), keyFingerPrintCalculator);
        } catch (EOFException e) {
            throw e;
        } catch (ArmoredInputException e2) {
            throw e2;
        } catch (IOException e3) {
            if (!LOG.isLoggable(Level.FINE)) {
                return null;
            }
            LOG.fine("skipping unknown subkey: " + e3.getMessage());
            return null;
        }
    }

    public static PGPPublicKeyRing removePublicKey(PGPPublicKeyRing pGPPublicKeyRing, PGPPublicKey pGPPublicKey) {
        int size = pGPPublicKeyRing.keys.size();
        long keyID = pGPPublicKey.getKeyID();
        ArrayList arrayList = new ArrayList(size);
        boolean z = false;
        for (int i = 0; i < size; i++) {
            PGPPublicKey pGPPublicKey2 = pGPPublicKeyRing.keys.get(i);
            if (pGPPublicKey2.getKeyID() == keyID) {
                z = true;
            } else {
                arrayList.add(pGPPublicKey2);
            }
        }
        if (z) {
            return new PGPPublicKeyRing(arrayList);
        }
        return null;
    }

    @Override // org.bouncycastle.openpgp.PGPKeyRing
    public void encode(OutputStream outputStream) throws IOException {
        encode(outputStream, false);
    }

    public void encode(OutputStream outputStream, boolean z) throws IOException {
        for (int i = 0; i != this.keys.size(); i++) {
            this.keys.get(i).encode(outputStream, z);
        }
    }

    @Override // org.bouncycastle.openpgp.PGPKeyRing
    public byte[] getEncoded() throws IOException {
        return getEncoded(PacketFormat.ROUNDTRIP);
    }

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

    public byte[] getEncoded(boolean z) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        encode(byteArrayOutputStream, z);
        return byteArrayOutputStream.toByteArray();
    }

    @Override // org.bouncycastle.openpgp.PGPKeyRing
    public Iterator<PGPPublicKey> getKeysWithSignaturesBy(long j) {
        ArrayList arrayList = new ArrayList();
        for (int i = 0; i != this.keys.size(); i++) {
            PGPPublicKey pGPPublicKey = this.keys.get(i);
            if (pGPPublicKey.getSignaturesForKeyID(j).hasNext()) {
                arrayList.add(pGPPublicKey);
            }
        }
        return arrayList.iterator();
    }

    @Override // org.bouncycastle.openpgp.PGPKeyRing
    public Iterator<PGPPublicKey> getKeysWithSignaturesBy(KeyIdentifier keyIdentifier) {
        ArrayList arrayList = new ArrayList();
        for (PGPPublicKey pGPPublicKey : this.keys) {
            if (pGPPublicKey.getSignaturesForKey(keyIdentifier).hasNext()) {
                arrayList.add(pGPPublicKey);
            }
        }
        return arrayList.iterator();
    }

    @Override // org.bouncycastle.openpgp.PGPKeyRing
    public PGPPublicKey getPublicKey() {
        return this.keys.get(0);
    }

    @Override // org.bouncycastle.openpgp.PGPKeyRing
    public PGPPublicKey getPublicKey(long j) {
        for (int i = 0; i != this.keys.size(); i++) {
            PGPPublicKey pGPPublicKey = this.keys.get(i);
            if (j == pGPPublicKey.getKeyID()) {
                return pGPPublicKey;
            }
        }
        return null;
    }

    @Override // org.bouncycastle.openpgp.PGPKeyRing
    public PGPPublicKey getPublicKey(KeyIdentifier keyIdentifier) {
        for (PGPPublicKey pGPPublicKey : this.keys) {
            if (keyIdentifier.matchesExplicit(pGPPublicKey.getKeyIdentifier())) {
                return pGPPublicKey;
            }
        }
        return null;
    }

    @Override // org.bouncycastle.openpgp.PGPKeyRing
    public PGPPublicKey getPublicKey(byte[] bArr) {
        for (int i = 0; i != this.keys.size(); i++) {
            PGPPublicKey pGPPublicKey = this.keys.get(i);
            if (pGPPublicKey.hasFingerprint(bArr)) {
                return pGPPublicKey;
            }
        }
        return null;
    }

    @Override // org.bouncycastle.openpgp.PGPKeyRing
    public Iterator<PGPPublicKey> getPublicKeys() {
        return Collections.unmodifiableList(this.keys).iterator();
    }

    @Override // org.bouncycastle.openpgp.PGPKeyRing
    public Iterator<PGPPublicKey> getPublicKeys(KeyIdentifier keyIdentifier) {
        ArrayList arrayList = new ArrayList();
        for (PGPPublicKey pGPPublicKey : this.keys) {
            if (keyIdentifier.matchesExplicit(pGPPublicKey.getKeyIdentifier())) {
                arrayList.add(pGPPublicKey);
            }
        }
        return arrayList.iterator();
    }

    @Override // org.bouncycastle.util.Iterable, java.lang.Iterable
    public Iterator<PGPPublicKey> iterator() {
        return getPublicKeys();
    }

    @Override // org.bouncycastle.openpgp.PGPKeyRing
    public int size() {
        return this.keys.size();
    }
}
