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.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.PacketFormat;
import org.bouncycastle.bcpg.PublicSubkeyPacket;
import org.bouncycastle.bcpg.SecretKeyPacket;
import org.bouncycastle.bcpg.SecretSubkeyPacket;
import org.bouncycastle.bcpg.TrustPacket;
import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator;
import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor;
import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor;
import org.bouncycastle.util.Iterable;

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

    public PGPSecretKeyRing(InputStream inputStream, KeyFingerPrintCalculator keyFingerPrintCalculator) throws IOException, PGPException {
        List list;
        Object pGPPublicKey;
        this.keys = new ArrayList();
        this.extraPubKeys = new ArrayList();
        BCPGInputStream bCPGInputStreamWrap = BCPGInputStream.wrap(inputStream);
        int iSkipMarkerAndPaddingPackets = bCPGInputStreamWrap.skipMarkerAndPaddingPackets();
        if (iSkipMarkerAndPaddingPackets != 5 && iSkipMarkerAndPaddingPackets != 7) {
            throw new IOException("secret key ring doesn't start with secret key tag: tag 0x" + Integer.toHexString(iSkipMarkerAndPaddingPackets));
        }
        SecretKeyPacket secretKeyPacket = (SecretKeyPacket) bCPGInputStreamWrap.readPacket();
        while (bCPGInputStreamWrap.nextPacketTag() == 61) {
            bCPGInputStreamWrap.readPacket();
        }
        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);
        this.keys.add(new PGPSecretKey(secretKeyPacket, new PGPPublicKey(secretKeyPacket.getPublicKeyPacket(), optionalTrustPacket, signaturesAndTrust, arrayList, arrayList2, arrayList3, keyFingerPrintCalculator)));
        while (true) {
            if (bCPGInputStreamWrap.nextPacketTag() != 7 && bCPGInputStreamWrap.nextPacketTag() != 14) {
                return;
            }
            try {
                if (bCPGInputStreamWrap.nextPacketTag() == 7) {
                    SecretSubkeyPacket secretSubkeyPacket = (SecretSubkeyPacket) bCPGInputStreamWrap.readPacket();
                    while (bCPGInputStreamWrap.nextPacketTag() == 61) {
                        bCPGInputStreamWrap.readPacket();
                    }
                    TrustPacket optionalTrustPacket2 = readOptionalTrustPacket(bCPGInputStreamWrap);
                    List<PGPSignature> signaturesAndTrust2 = readSignaturesAndTrust(bCPGInputStreamWrap);
                    list = this.keys;
                    pGPPublicKey = new PGPSecretKey(secretSubkeyPacket, new PGPPublicKey(secretSubkeyPacket.getPublicKeyPacket(), optionalTrustPacket2, signaturesAndTrust2, keyFingerPrintCalculator));
                } else {
                    PublicSubkeyPacket publicSubkeyPacket = (PublicSubkeyPacket) bCPGInputStreamWrap.readPacket();
                    TrustPacket optionalTrustPacket3 = readOptionalTrustPacket(bCPGInputStreamWrap);
                    List<PGPSignature> signaturesAndTrust3 = readSignaturesAndTrust(bCPGInputStreamWrap);
                    list = this.extraPubKeys;
                    pGPPublicKey = new PGPPublicKey(publicSubkeyPacket, optionalTrustPacket3, signaturesAndTrust3, keyFingerPrintCalculator);
                }
                list.add(pGPPublicKey);
            } catch (EOFException e) {
                throw e;
            } catch (ArmoredInputException e2) {
                throw e2;
            } catch (IOException e3) {
                if (LOG.isLoggable(Level.FINE)) {
                    LOG.fine("skipping unknown subkey: " + e3.getMessage());
                }
            }
        }
    }

    public PGPSecretKeyRing(List<PGPSecretKey> list) {
        this(checkKeys(list), new ArrayList());
    }

    private PGPSecretKeyRing(List<PGPSecretKey> list, List<PGPPublicKey> list2) {
        this.keys = list;
        this.extraPubKeys = list2;
    }

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

    private static List<PGPSecretKey> checkKeys(List<PGPSecretKey> list) {
        ArrayList arrayList = new ArrayList(list.size());
        for (int i = 0; i != list.size(); i++) {
            PGPSecretKey pGPSecretKey = list.get(i);
            boolean zIsMasterKey = pGPSecretKey.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(pGPSecretKey);
        }
        return arrayList;
    }

    public static PGPSecretKeyRing copyWithNewPassword(PGPSecretKeyRing pGPSecretKeyRing, PBESecretKeyDecryptor pBESecretKeyDecryptor, PBESecretKeyEncryptor pBESecretKeyEncryptor) throws PGPException {
        ArrayList arrayList = new ArrayList(pGPSecretKeyRing.keys.size());
        Iterator<PGPSecretKey> secretKeys = pGPSecretKeyRing.getSecretKeys();
        while (secretKeys.hasNext()) {
            PGPSecretKey next = secretKeys.next();
            if (!next.isPrivateKeyEmpty()) {
                next = PGPSecretKey.copyWithNewPassword(next, pBESecretKeyDecryptor, pBESecretKeyEncryptor);
            }
            arrayList.add(next);
        }
        return new PGPSecretKeyRing(arrayList, pGPSecretKeyRing.extraPubKeys);
    }

    public static PGPSecretKeyRing insertOrReplacePublicKey(PGPSecretKeyRing pGPSecretKeyRing, PGPPublicKey pGPPublicKey) {
        PGPSecretKey secretKey = pGPSecretKeyRing.getSecretKey(pGPPublicKey.getKeyID());
        if (secretKey != null) {
            ArrayList arrayList = new ArrayList(pGPSecretKeyRing.keys.size());
            Iterator<PGPSecretKey> secretKeys = pGPSecretKeyRing.getSecretKeys();
            while (secretKeys.hasNext()) {
                if (secretKeys.next().getKeyID() == pGPPublicKey.getKeyID()) {
                    arrayList.add(PGPSecretKey.replacePublicKey(secretKey, pGPPublicKey));
                }
            }
            return new PGPSecretKeyRing(arrayList);
        }
        ArrayList arrayList2 = new ArrayList(pGPSecretKeyRing.extraPubKeys.size());
        Iterator<PGPPublicKey> extraPublicKeys = pGPSecretKeyRing.getExtraPublicKeys();
        boolean z = false;
        while (extraPublicKeys.hasNext()) {
            PGPPublicKey next = extraPublicKeys.next();
            if (next.getKeyID() == pGPPublicKey.getKeyID()) {
                arrayList2.add(pGPPublicKey);
                z = true;
            } else {
                arrayList2.add(next);
            }
        }
        if (!z) {
            arrayList2.add(pGPPublicKey);
        }
        return new PGPSecretKeyRing(new ArrayList(pGPSecretKeyRing.keys), arrayList2);
    }

    public static PGPSecretKeyRing insertSecretKey(PGPSecretKeyRing pGPSecretKeyRing, PGPSecretKey pGPSecretKey) {
        ArrayList arrayList = new ArrayList(pGPSecretKeyRing.keys);
        boolean z = false;
        boolean z2 = false;
        for (int i = 0; i != arrayList.size(); i++) {
            PGPSecretKey pGPSecretKey2 = (PGPSecretKey) arrayList.get(i);
            if (pGPSecretKey2.getKeyID() == pGPSecretKey.getKeyID()) {
                arrayList.set(i, pGPSecretKey);
                z = true;
            }
            if (pGPSecretKey2.isMasterKey()) {
                z2 = true;
            }
        }
        if (!z) {
            if (!pGPSecretKey.isMasterKey()) {
                arrayList.add(pGPSecretKey);
            } else {
                if (z2) {
                    throw new IllegalArgumentException("cannot add a master key to a ring that already has one");
                }
                arrayList.add(0, pGPSecretKey);
            }
        }
        return new PGPSecretKeyRing(arrayList, pGPSecretKeyRing.extraPubKeys);
    }

    public static PGPSecretKeyRing removeSecretKey(PGPSecretKeyRing pGPSecretKeyRing, PGPSecretKey pGPSecretKey) {
        int size = pGPSecretKeyRing.keys.size();
        long keyID = pGPSecretKey.getKeyID();
        ArrayList arrayList = new ArrayList(size);
        boolean z = false;
        for (int i = 0; i < size; i++) {
            PGPSecretKey pGPSecretKey2 = pGPSecretKeyRing.keys.get(i);
            if (pGPSecretKey2.getKeyID() == keyID) {
                z = true;
            } else {
                arrayList.add(pGPSecretKey2);
            }
        }
        if (z) {
            return new PGPSecretKeyRing(arrayList, pGPSecretKeyRing.extraPubKeys);
        }
        return null;
    }

    public static PGPSecretKeyRing replacePublicKeys(PGPSecretKeyRing pGPSecretKeyRing, PGPPublicKeyRing pGPPublicKeyRing) {
        ArrayList arrayList = new ArrayList(pGPSecretKeyRing.keys.size());
        for (PGPSecretKey pGPSecretKey : pGPSecretKeyRing.keys) {
            arrayList.add(PGPSecretKey.replacePublicKey(pGPSecretKey, pGPPublicKeyRing.getPublicKey(pGPSecretKey.getKeyID())));
        }
        return new PGPSecretKeyRing(arrayList);
    }

    @Override // org.bouncycastle.openpgp.PGPKeyRing
    public void encode(OutputStream outputStream) throws IOException {
        for (int i = 0; i != this.keys.size(); i++) {
            this.keys.get(i).encode(outputStream);
        }
        for (int i2 = 0; i2 != this.extraPubKeys.size(); i2++) {
            this.extraPubKeys.get(i2).encode(outputStream);
        }
    }

    @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 Iterator<PGPPublicKey> getExtraPublicKeys() {
        return this.extraPubKeys.iterator();
    }

    @Override // org.bouncycastle.openpgp.PGPKeyRing
    public Iterator<PGPPublicKey> getKeysWithSignaturesBy(long j) {
        ArrayList arrayList = new ArrayList();
        Iterator<PGPPublicKey> publicKeys = getPublicKeys();
        while (publicKeys.hasNext()) {
            PGPPublicKey next = publicKeys.next();
            if (next.getSignaturesForKeyID(j).hasNext()) {
                arrayList.add(next);
            }
        }
        return arrayList.iterator();
    }

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

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

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

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

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

    @Override // org.bouncycastle.openpgp.PGPKeyRing
    public Iterator<PGPPublicKey> getPublicKeys() {
        ArrayList arrayList = new ArrayList();
        Iterator<PGPSecretKey> secretKeys = getSecretKeys();
        while (secretKeys.hasNext()) {
            arrayList.add(secretKeys.next().getPublicKey());
        }
        arrayList.addAll(this.extraPubKeys);
        return Collections.unmodifiableList(arrayList).iterator();
    }

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

    public PGPSecretKey getSecretKey() {
        return this.keys.get(0);
    }

    public PGPSecretKey getSecretKey(long j) {
        for (int i = 0; i != this.keys.size(); i++) {
            PGPSecretKey pGPSecretKey = this.keys.get(i);
            if (j == pGPSecretKey.getKeyID()) {
                return pGPSecretKey;
            }
        }
        return null;
    }

    public PGPSecretKey getSecretKey(KeyIdentifier keyIdentifier) {
        for (PGPSecretKey pGPSecretKey : this.keys) {
            if (keyIdentifier.matchesExplicit(pGPSecretKey.getKeyIdentifier())) {
                return pGPSecretKey;
            }
        }
        return null;
    }

    public PGPSecretKey getSecretKey(byte[] bArr) {
        for (int i = 0; i != this.keys.size(); i++) {
            PGPSecretKey pGPSecretKey = this.keys.get(i);
            if (pGPSecretKey.getPublicKey().hasFingerprint(bArr)) {
                return pGPSecretKey;
            }
        }
        return null;
    }

    public Iterator<PGPSecretKey> getSecretKeys() {
        return Collections.unmodifiableList(this.keys).iterator();
    }

    public Iterator<PGPSecretKey> getSecretKeys(KeyIdentifier keyIdentifier) {
        ArrayList arrayList = new ArrayList();
        for (PGPSecretKey pGPSecretKey : this.keys) {
            if (keyIdentifier.matchesExplicit(pGPSecretKey.getKeyIdentifier())) {
                arrayList.add(pGPSecretKey);
            }
        }
        return arrayList.iterator();
    }

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

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

    public PGPPublicKeyRing toCertificate() {
        ArrayList arrayList = new ArrayList();
        Iterator<PGPPublicKey> publicKeys = getPublicKeys();
        while (publicKeys.hasNext()) {
            arrayList.add(publicKeys.next());
        }
        return new PGPPublicKeyRing(arrayList);
    }
}
