package org.bouncycastle.openpgp.api;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.bcpg.BCPGInputStream;
import org.bouncycastle.bcpg.BCPGOutputStream;
import org.bouncycastle.bcpg.FingerprintUtil;
import org.bouncycastle.bcpg.KeyIdentifier;
import org.bouncycastle.bcpg.PacketFormat;
import org.bouncycastle.bcpg.PublicKeyUtils;
import org.bouncycastle.bcpg.SignatureSubpacket;
import org.bouncycastle.bcpg.sig.Features;
import org.bouncycastle.bcpg.sig.KeyExpirationTime;
import org.bouncycastle.bcpg.sig.KeyFlags;
import org.bouncycastle.bcpg.sig.PreferredAEADCiphersuites;
import org.bouncycastle.bcpg.sig.PreferredAlgorithms;
import org.bouncycastle.bcpg.sig.PrimaryUserID;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPKeyRing;
import org.bouncycastle.openpgp.PGPObjectFactory;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.openpgp.PGPSignatureException;
import org.bouncycastle.openpgp.PGPSignatureList;
import org.bouncycastle.openpgp.PGPSignatureSubpacketVector;
import org.bouncycastle.openpgp.PGPUserAttributeSubpacketVector;
import org.bouncycastle.openpgp.PGPUtil;
import org.bouncycastle.openpgp.api.exception.IncorrectOpenPGPSignatureException;
import org.bouncycastle.openpgp.api.exception.MalformedOpenPGPSignatureException;
import org.bouncycastle.openpgp.api.exception.MissingIssuerCertException;
import org.bouncycastle.openpgp.api.util.UTCUtil;
import org.bouncycastle.openpgp.operator.PGPContentVerifierBuilderProvider;

/* JADX INFO: loaded from: classes2.dex */
public class OpenPGPCertificate {
    private final Map<OpenPGPCertificateComponent, OpenPGPSignatureChains> componentSignatureChains;
    final OpenPGPImplementation implementation;
    protected PGPKeyRing keyRing;
    final OpenPGPPolicy policy;
    private final OpenPGPPrimaryKey primaryKey;
    private final Map<KeyIdentifier, OpenPGPSubkey> subkeys;

    private interface KeyFilter {
        boolean test(OpenPGPComponentKey openPGPComponentKey, Date date);
    }

    public static abstract class OpenPGPCertificateComponent {
        private final OpenPGPCertificate certificate;

        public OpenPGPCertificateComponent(OpenPGPCertificate openPGPCertificate) {
            this.certificate = openPGPCertificate;
        }

        public PreferredAEADCiphersuites getAEADCipherSuitePreferences() {
            return getAEADCipherSuitePreferences(new Date());
        }

        public PreferredAEADCiphersuites getAEADCipherSuitePreferences(Date date) {
            OpenPGPSignature.OpenPGPSignatureSubpacket applyingSubpacket = getApplyingSubpacket(date, 39);
            if (applyingSubpacket != null) {
                return (PreferredAEADCiphersuites) applyingSubpacket.getSubpacket();
            }
            return null;
        }

        protected OpenPGPSignature.OpenPGPSignatureSubpacket getApplyingSubpacket(Date date, int i) {
            OpenPGPSignatureChain certificationAt = getSignatureChains().getCertificationAt(date);
            if (certificationAt == null) {
                return null;
            }
            try {
                if (!certificationAt.isValid()) {
                    return null;
                }
                OpenPGPComponentSignature signature = certificationAt.getSignature();
                PGPSignatureSubpacketVector hashedSubPackets = signature.getSignature().getHashedSubPackets();
                if (hashedSubPackets == null || !hashedSubPackets.hasSubpacket(i)) {
                    OpenPGPSignatureChain preferenceSignature = getCertificate().getPreferenceSignature(date);
                    if (preferenceSignature == null) {
                        return null;
                    }
                    signature = preferenceSignature.getSignature();
                    hashedSubPackets = signature.getSignature().getHashedSubPackets();
                }
                SignatureSubpacket subpacket = hashedSubPackets.getSubpacket(i);
                if (subpacket == null) {
                    return null;
                }
                return OpenPGPSignature.OpenPGPSignatureSubpacket.hashed(subpacket, signature);
            } catch (PGPSignatureException e) {
                return null;
            }
        }

        public OpenPGPCertificate getCertificate() {
            return this.certificate;
        }

        public OpenPGPComponentSignature getCertification(Date date) {
            OpenPGPSignatureChain certificationAt = getSignatureChains().getCertificationAt(date);
            if (certificationAt != null) {
                return certificationAt.getSignature();
            }
            return null;
        }

        public PreferredAlgorithms getCompressionAlgorithmPreferences() {
            return getCompressionAlgorithmPreferences(new Date());
        }

        public PreferredAlgorithms getCompressionAlgorithmPreferences(Date date) {
            OpenPGPSignature.OpenPGPSignatureSubpacket applyingSubpacket = getApplyingSubpacket(date, 22);
            if (applyingSubpacket != null) {
                return (PreferredAlgorithms) applyingSubpacket.getSubpacket();
            }
            return null;
        }

        public Features getFeatures() {
            return getFeatures(new Date());
        }

        public Features getFeatures(Date date) {
            OpenPGPSignature.OpenPGPSignatureSubpacket applyingSubpacket = getApplyingSubpacket(date, 30);
            if (applyingSubpacket != null) {
                return (Features) applyingSubpacket.getSubpacket();
            }
            return null;
        }

        public PreferredAlgorithms getHashAlgorithmPreferences() {
            return getHashAlgorithmPreferences(new Date());
        }

        public PreferredAlgorithms getHashAlgorithmPreferences(Date date) {
            OpenPGPSignature.OpenPGPSignatureSubpacket applyingSubpacket = getApplyingSubpacket(date, 21);
            if (applyingSubpacket != null) {
                return (PreferredAlgorithms) applyingSubpacket.getSubpacket();
            }
            return null;
        }

        protected abstract OpenPGPComponentKey getKeyComponent();

        public Date getKeyExpirationDate() {
            return getKeyExpirationDateAt(new Date());
        }

        public Date getKeyExpirationDateAt(Date date) {
            return getLatestSelfSignature(date).getKeyExpirationTime();
        }

        public KeyFlags getKeyFlags() {
            return getKeyFlags(new Date());
        }

        public KeyFlags getKeyFlags(Date date) {
            OpenPGPSignature.OpenPGPSignatureSubpacket applyingSubpacket = getApplyingSubpacket(date, 27);
            if (applyingSubpacket != null) {
                return (KeyFlags) applyingSubpacket.getSubpacket();
            }
            return null;
        }

        public OpenPGPComponentSignature getLatestSelfSignature() {
            return getLatestSelfSignature(new Date());
        }

        public abstract OpenPGPComponentSignature getLatestSelfSignature(Date date);

        protected OpenPGPSignatureChains getMergedDanglingExternalSignatureChainEndsFrom(OpenPGPCertificate openPGPCertificate, Date date) {
            OpenPGPSignatureChains openPGPSignatureChains = new OpenPGPSignatureChains(this);
            Iterator<OpenPGPSignatureChain> it = getCertificate().getAllSignatureChainsFor(this).getChainsAt(date).iterator();
            while (it.hasNext()) {
                OpenPGPSignatureChain.Link rootLink = it.next().getRootLink();
                for (OpenPGPComponentKey openPGPComponentKey : openPGPCertificate.getKeys()) {
                    if (KeyIdentifier.matches(rootLink.getSignature().getKeyIdentifiers(), openPGPComponentKey.getKeyIdentifier(), true)) {
                        openPGPSignatureChains.add(openPGPComponentKey.getSignatureChains().getChainAt(date).plus(new OpenPGPComponentSignature(rootLink.signature.getSignature(), openPGPComponentKey, this)));
                    }
                }
            }
            return openPGPSignatureChains;
        }

        protected OpenPGPCertificateComponent getPublicComponent() {
            return this;
        }

        public OpenPGPComponentSignature getRevocation(Date date) {
            OpenPGPSignatureChain revocationAt = getSignatureChains().getRevocationAt(date);
            if (revocationAt != null) {
                return revocationAt.getSignature();
            }
            return null;
        }

        public OpenPGPSignatureChains getSignatureChains() {
            OpenPGPSignatureChains allSignatureChainsFor = getCertificate().getAllSignatureChainsFor(this);
            if (getPublicComponent() instanceof OpenPGPPrimaryKey) {
                OpenPGPPrimaryKey openPGPPrimaryKey = (OpenPGPPrimaryKey) getPublicComponent();
                if (!openPGPPrimaryKey.getUserIDs().isEmpty()) {
                    allSignatureChainsFor.addAll(getCertificate().getAllSignatureChainsFor(openPGPPrimaryKey.getUserIDs().get(0)));
                }
            }
            return allSignatureChainsFor;
        }

        public PreferredAlgorithms getSymmetricCipherPreferences() {
            return getSymmetricCipherPreferences(new Date());
        }

        public PreferredAlgorithms getSymmetricCipherPreferences(Date date) {
            OpenPGPSignature.OpenPGPSignatureSubpacket applyingSubpacket = getApplyingSubpacket(date, 11);
            if (applyingSubpacket != null) {
                return (PreferredAlgorithms) applyingSubpacket.getSubpacket();
            }
            return null;
        }

        public boolean hasKeyFlags(Date date, int... iArr) {
            KeyFlags keyFlags = getKeyFlags(date);
            if (keyFlags == null) {
                return false;
            }
            for (int i = 0; i < iArr.length; i++) {
                if ((keyFlags.getFlags() & iArr[i]) == iArr[i]) {
                    return true;
                }
            }
            return false;
        }

        public boolean isBound() {
            return isBoundAt(new Date());
        }

        public boolean isBoundAt(Date date) {
            return getCertificate().isBound(this, date);
        }

        public abstract String toDetailString();
    }

    /* JADX INFO: loaded from: classes.dex */
    public static abstract class OpenPGPComponentKey extends OpenPGPCertificateComponent {
        protected final PGPPublicKey rawPubkey;

        public OpenPGPComponentKey(PGPPublicKey pGPPublicKey, OpenPGPCertificate openPGPCertificate) {
            super(openPGPCertificate);
            this.rawPubkey = pGPPublicKey;
        }

        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (this == obj) {
                return true;
            }
            if (obj instanceof OpenPGPComponentKey) {
                return getPGPPublicKey().equals(((OpenPGPComponentKey) obj).getPGPPublicKey());
            }
            return false;
        }

        public int getAlgorithm() {
            return getPGPPublicKey().getAlgorithm();
        }

        public Date getCreationTime() {
            return this.rawPubkey.getCreationTime();
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPCertificate.OpenPGPCertificateComponent
        protected OpenPGPComponentKey getKeyComponent() {
            return this;
        }

        public KeyIdentifier getKeyIdentifier() {
            return this.rawPubkey.getKeyIdentifier();
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPCertificate.OpenPGPCertificateComponent
        public OpenPGPComponentSignature getLatestSelfSignature(Date date) {
            OpenPGPSignatureChain chainAt = getSignatureChains().getChainAt(date);
            if (chainAt == null || chainAt.chainLinks.isEmpty()) {
                return null;
            }
            return chainAt.getSignature();
        }

        public PGPPublicKey getPGPPublicKey() {
            return this.rawPubkey;
        }

        public int getVersion() {
            return getPGPPublicKey().getVersion();
        }

        public int hashCode() {
            return getPGPPublicKey().hashCode();
        }

        public boolean isCertificationKey() {
            return isCertificationKey(new Date());
        }

        public boolean isCertificationKey(Date date) {
            if (PublicKeyUtils.isSigningAlgorithm(this.rawPubkey.getAlgorithm())) {
                return hasKeyFlags(date, 1);
            }
            return false;
        }

        public boolean isEncryptionKey() {
            return isEncryptionKey(new Date());
        }

        public boolean isEncryptionKey(Date date) {
            if (this.rawPubkey.isEncryptionKey()) {
                return hasKeyFlags(date, 8) || hasKeyFlags(date, 4);
            }
            return false;
        }

        public abstract boolean isPrimaryKey();

        public boolean isSigningKey() {
            return isSigningKey(new Date());
        }

        public boolean isSigningKey(Date date) {
            if (PublicKeyUtils.isSigningAlgorithm(this.rawPubkey.getAlgorithm())) {
                return hasKeyFlags(date, 2);
            }
            return false;
        }
    }

    public static class OpenPGPComponentSignature extends OpenPGPSignature {
        private final OpenPGPCertificateComponent target;

        public OpenPGPComponentSignature(PGPSignature pGPSignature, OpenPGPComponentKey openPGPComponentKey, OpenPGPCertificateComponent openPGPCertificateComponent) {
            super(pGPSignature, openPGPComponentKey);
            this.target = openPGPCertificateComponent;
        }

        private static void addSignatures(List<PGPSignature> list, PGPSignatureList pGPSignatureList) {
            Iterator<PGPSignature> it = pGPSignatureList.iterator();
            while (it.hasNext()) {
                list.add(it.next());
            }
        }

        private void verifyEmbeddedPrimaryKeyBinding(PGPContentVerifierBuilderProvider pGPContentVerifierBuilderProvider, OpenPGPPolicy openPGPPolicy, Date date) throws PGPSignatureException {
            if ((this.signature.getHashedSubPackets().getKeyFlags() & 2) != 2) {
                return;
            }
            OpenPGPComponentKey targetKeyComponent = getTargetKeyComponent();
            ArrayList<PGPSignature> arrayList = new ArrayList();
            try {
                addSignatures(arrayList, this.signature.getHashedSubPackets().getEmbeddedSignatures());
                addSignatures(arrayList, this.signature.getUnhashedSubPackets().getEmbeddedSignatures());
                if (arrayList.isEmpty()) {
                    throw new MalformedOpenPGPSignatureException(this, "Signing key SubkeyBindingSignature MUST contain embedded PrimaryKeyBindingSignature.");
                }
                PGPSignatureException e = null;
                for (PGPSignature pGPSignature : arrayList) {
                    OpenPGPComponentSignature openPGPComponentSignature = new OpenPGPComponentSignature(pGPSignature, targetKeyComponent, this.issuer);
                    if (pGPSignature.getSignatureType() != 25) {
                        e = new PGPSignatureException("Unexpected embedded signature type: " + pGPSignature.getSignatureType());
                    } else if (openPGPComponentSignature.isEffectiveAt(date)) {
                        try {
                            openPGPComponentSignature.sanitize(targetKeyComponent, openPGPPolicy);
                            openPGPComponentSignature.verifyKeySignature(targetKeyComponent, this.issuer, pGPContentVerifierBuilderProvider);
                            return;
                        } catch (PGPSignatureException e2) {
                            e = e2;
                        }
                    } else {
                        e = new PGPSignatureException("Embedded PrimaryKeyBinding signature is expired or not yet effective.");
                    }
                }
                throw e;
            } catch (PGPException e3) {
                throw new PGPSignatureException("Cannot extract embedded signature.", e3);
            }
        }

        public OpenPGPComponentKey getIssuerComponent() {
            return getIssuer();
        }

        public Date getKeyExpirationTime() {
            PGPSignatureSubpacketVector hashedSubPackets = this.signature.getHashedSubPackets();
            if (hashedSubPackets == null) {
                return null;
            }
            long keyExpirationTime = hashedSubPackets.getKeyExpirationTime();
            if (keyExpirationTime < 0) {
                throw new RuntimeException("Negative key expiration time");
            }
            if (keyExpirationTime == 0) {
                return null;
            }
            return new Date(getTargetKeyComponent().getCreationTime().getTime() + (keyExpirationTime * 1000));
        }

        public OpenPGPCertificateComponent getTargetComponent() {
            return this.target;
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPSignature
        protected String getTargetDisplay() {
            return this.target.toString();
        }

        public OpenPGPComponentKey getTargetKeyComponent() {
            if (getTargetComponent() instanceof OpenPGPIdentityComponent) {
                return ((OpenPGPIdentityComponent) getTargetComponent()).getPrimaryKey();
            }
            if (getTargetComponent() instanceof OpenPGPComponentKey) {
                return (OpenPGPComponentKey) getTargetComponent();
            }
            throw new IllegalArgumentException("Unknown target type.");
        }

        public void verify(OpenPGPImplementation openPGPImplementation) throws PGPSignatureException {
            verify(openPGPImplementation.pgpContentVerifierBuilderProvider(), openPGPImplementation.policy());
        }

        public void verify(PGPContentVerifierBuilderProvider pGPContentVerifierBuilderProvider, OpenPGPPolicy openPGPPolicy) throws PGPSignatureException {
            OpenPGPComponentKey openPGPComponentKey;
            OpenPGPComponentKey keyComponent;
            if (this.issuer == null) {
                throw new MissingIssuerCertException(this, "Issuer certificate unavailable.");
            }
            sanitize(this.issuer, openPGPPolicy);
            if (this.signature.getSignatureType() == 31 || this.signature.getSignatureType() == 32) {
                openPGPComponentKey = this.issuer;
                keyComponent = this.target.getKeyComponent();
            } else {
                if (this.signature.getSignatureType() == 24) {
                    verifyEmbeddedPrimaryKeyBinding(pGPContentVerifierBuilderProvider, openPGPPolicy, getCreationTime());
                    if (((OpenPGPSubkey) this.target).getCreationTime().after(this.signature.getCreationTime())) {
                        this.isCorrect = false;
                        throw new MalformedOpenPGPSignatureException(this, "Subkey binding signature predates subkey creation time.");
                    }
                } else {
                    int signatureType = this.signature.getSignatureType();
                    OpenPGPCertificateComponent openPGPCertificateComponent = this.target;
                    if (signatureType != 40) {
                        if (openPGPCertificateComponent instanceof OpenPGPUserId) {
                            verifyUserIdSignature(this.issuer, (OpenPGPUserId) this.target, pGPContentVerifierBuilderProvider);
                            return;
                        } else {
                            if (!(this.target instanceof OpenPGPUserAttribute)) {
                                throw new PGPSignatureException("Unexpected signature type: " + getType());
                            }
                            verifyUserAttributeSignature(this.issuer, (OpenPGPUserAttribute) this.target, pGPContentVerifierBuilderProvider);
                            return;
                        }
                    }
                    if (((OpenPGPSubkey) openPGPCertificateComponent).getCreationTime().after(this.signature.getCreationTime())) {
                        this.isCorrect = false;
                        throw new MalformedOpenPGPSignatureException(this, "Subkey revocation signature predates subkey creation time.");
                    }
                }
                openPGPComponentKey = this.issuer;
                keyComponent = (OpenPGPSubkey) this.target;
            }
            verifyKeySignature(openPGPComponentKey, keyComponent, pGPContentVerifierBuilderProvider);
        }

        protected void verifyKeySignature(OpenPGPComponentKey openPGPComponentKey, OpenPGPComponentKey openPGPComponentKey2, PGPContentVerifierBuilderProvider pGPContentVerifierBuilderProvider) throws PGPSignatureException {
            boolean zVerifyCertification;
            this.isTested = true;
            try {
                this.signature.init(pGPContentVerifierBuilderProvider, openPGPComponentKey.getPGPPublicKey());
                if (this.signature.getSignatureType() == 31 || this.signature.getSignatureType() == 32) {
                    zVerifyCertification = this.signature.verifyCertification(openPGPComponentKey2.getPGPPublicKey());
                } else {
                    zVerifyCertification = this.signature.getSignatureType() == 25 ? this.signature.verifyCertification(openPGPComponentKey2.getPGPPublicKey(), openPGPComponentKey.getPGPPublicKey()) : this.signature.verifyCertification(openPGPComponentKey.getPGPPublicKey(), openPGPComponentKey2.getPGPPublicKey());
                }
                this.isCorrect = zVerifyCertification;
                if (this.isCorrect) {
                } else {
                    throw new IncorrectOpenPGPSignatureException(this, "Key Signature is not correct.");
                }
            } catch (ClassCastException e) {
                this.isCorrect = false;
                throw new PGPSignatureException("Key Signature could not be verified.", e);
            } catch (PGPException e2) {
                this.isCorrect = false;
                throw new PGPSignatureException("Key Signature could not be verified.", e2);
            }
        }

        protected void verifyUserAttributeSignature(OpenPGPComponentKey openPGPComponentKey, OpenPGPUserAttribute openPGPUserAttribute, PGPContentVerifierBuilderProvider pGPContentVerifierBuilderProvider) throws PGPSignatureException {
            this.isTested = true;
            try {
                this.signature.init(pGPContentVerifierBuilderProvider, openPGPComponentKey.getPGPPublicKey());
                this.isCorrect = this.signature.verifyCertification(openPGPUserAttribute.getUserAttribute(), openPGPUserAttribute.getPrimaryKey().getPGPPublicKey());
                if (this.isCorrect) {
                } else {
                    throw new IncorrectOpenPGPSignatureException(this, "UserAttribute Signature is not correct.");
                }
            } catch (ClassCastException e) {
                this.isCorrect = false;
                throw new PGPSignatureException("Could not verify UserAttribute Signature.", e);
            } catch (PGPException e2) {
                this.isCorrect = false;
                throw new PGPSignatureException("Could not verify UserAttribute Signature.", e2);
            }
        }

        protected void verifyUserIdSignature(OpenPGPComponentKey openPGPComponentKey, OpenPGPUserId openPGPUserId, PGPContentVerifierBuilderProvider pGPContentVerifierBuilderProvider) throws PGPSignatureException {
            this.isTested = true;
            try {
                this.signature.init(pGPContentVerifierBuilderProvider, openPGPComponentKey.getPGPPublicKey());
                this.isCorrect = this.signature.verifyCertification(openPGPUserId.getUserId(), openPGPUserId.getPrimaryKey().getPGPPublicKey());
                if (this.isCorrect) {
                } else {
                    throw new IncorrectOpenPGPSignatureException(this, "UserID Signature is not correct.");
                }
            } catch (ClassCastException e) {
                this.isCorrect = false;
                throw new PGPSignatureException("UserID Signature could not be verified.", e);
            } catch (PGPException e2) {
                this.isCorrect = false;
                throw new PGPSignatureException("UserID Signature could not be verified.", e2);
            }
        }
    }

    public static abstract class OpenPGPIdentityComponent extends OpenPGPCertificateComponent {
        private final OpenPGPPrimaryKey primaryKey;

        public OpenPGPIdentityComponent(OpenPGPPrimaryKey openPGPPrimaryKey) {
            super(openPGPPrimaryKey.getCertificate());
            this.primaryKey = openPGPPrimaryKey;
        }

        public OpenPGPSignatureChain getCertificationBy(OpenPGPCertificate openPGPCertificate) {
            return getCertificationBy(openPGPCertificate, new Date());
        }

        public OpenPGPSignatureChain getCertificationBy(OpenPGPCertificate openPGPCertificate, Date date) {
            return getMergedDanglingExternalSignatureChainEndsFrom(openPGPCertificate, date).getCertificationAt(date);
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPCertificate.OpenPGPCertificateComponent
        protected OpenPGPComponentKey getKeyComponent() {
            return this.primaryKey;
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPCertificate.OpenPGPCertificateComponent
        public OpenPGPComponentSignature getLatestSelfSignature(Date date) {
            OpenPGPSignatureChain chainAt = getSignatureChains().getChainAt(date);
            if (chainAt == null || chainAt.chainLinks.isEmpty()) {
                return null;
            }
            return chainAt.getSignature();
        }

        public OpenPGPPrimaryKey getPrimaryKey() {
            return this.primaryKey;
        }

        public OpenPGPSignatureChain getRevocationBy(OpenPGPCertificate openPGPCertificate) {
            return getRevocationBy(openPGPCertificate, new Date());
        }

        public OpenPGPSignatureChain getRevocationBy(OpenPGPCertificate openPGPCertificate, Date date) {
            return getMergedDanglingExternalSignatureChainEndsFrom(openPGPCertificate, date).getRevocationAt(date);
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPCertificate.OpenPGPCertificateComponent
        public String toDetailString() {
            return toString();
        }
    }

    /* JADX INFO: loaded from: classes.dex */
    public static class OpenPGPPrimaryKey extends OpenPGPComponentKey {
        protected final List<OpenPGPIdentityComponent> identityComponents;

        public OpenPGPPrimaryKey(PGPPublicKey pGPPublicKey, OpenPGPCertificate openPGPCertificate) {
            super(pGPPublicKey, openPGPCertificate);
            this.identityComponents = new ArrayList();
            Iterator<String> userIDs = pGPPublicKey.getUserIDs();
            while (userIDs.hasNext()) {
                this.identityComponents.add(new OpenPGPUserId(userIDs.next(), this));
            }
            Iterator<PGPUserAttributeSubpacketVector> userAttributes = pGPPublicKey.getUserAttributes();
            while (userAttributes.hasNext()) {
                this.identityComponents.add(new OpenPGPUserAttribute(userAttributes.next(), this));
            }
        }

        private List<OpenPGPComponentSignature> signIterToList(OpenPGPIdentityComponent openPGPIdentityComponent, Iterator<PGPSignature> it) {
            ArrayList arrayList = new ArrayList();
            while (it.hasNext()) {
                PGPSignature next = it.next();
                arrayList.add(new OpenPGPComponentSignature(next, getCertificate().getSigningKeyFor(next), openPGPIdentityComponent));
            }
            return arrayList;
        }

        public OpenPGPUserId getExplicitOrImplicitPrimaryUserId(Date date) {
            OpenPGPUserId explicitPrimaryUserId = getExplicitPrimaryUserId(date);
            if (explicitPrimaryUserId != null) {
                return explicitPrimaryUserId;
            }
            OpenPGPUserId openPGPUserId = null;
            OpenPGPComponentSignature openPGPComponentSignature = null;
            for (OpenPGPUserId openPGPUserId2 : getUserIDs()) {
                OpenPGPSignatureChain certificationAt = openPGPUserId2.getSignatureChains().getCertificationAt(date);
                if (certificationAt != null) {
                    OpenPGPComponentSignature signature = certificationAt.getSignature();
                    if (openPGPComponentSignature == null || signature.getCreationTime().before(openPGPComponentSignature.getCreationTime())) {
                        openPGPUserId = openPGPUserId2;
                        openPGPComponentSignature = signature;
                    }
                }
            }
            return openPGPUserId;
        }

        public OpenPGPUserId getExplicitPrimaryUserId(Date date) {
            OpenPGPUserId openPGPUserId = null;
            OpenPGPSignature signature = null;
            for (OpenPGPUserId openPGPUserId2 : getUserIDs()) {
                OpenPGPSignature.OpenPGPSignatureSubpacket applyingSubpacket = openPGPUserId2.getApplyingSubpacket(date, 25);
                if (applyingSubpacket != null && ((PrimaryUserID) applyingSubpacket.getSubpacket()).isPrimaryUserID() && (signature == null || applyingSubpacket.getSignature().getCreationTime().after(signature.getCreationTime()))) {
                    signature = applyingSubpacket.getSignature();
                    openPGPUserId = openPGPUserId2;
                }
            }
            return openPGPUserId;
        }

        protected List<OpenPGPComponentSignature> getKeySignatures() {
            Iterator<PGPSignature> signatures = this.rawPubkey.getSignatures();
            ArrayList arrayList = new ArrayList();
            while (signatures.hasNext()) {
                PGPSignature next = signatures.next();
                int signatureType = next.getSignatureType();
                if (signatureType == 31 || signatureType == 32) {
                    arrayList.add(new OpenPGPComponentSignature(next, getCertificate().getSigningKeyFor(next), this));
                }
            }
            return arrayList;
        }

        public OpenPGPComponentSignature getLatestDirectKeySelfSignature() {
            return getLatestDirectKeySelfSignature(new Date());
        }

        public OpenPGPComponentSignature getLatestDirectKeySelfSignature(Date date) {
            OpenPGPSignatureChain certificationAt = getCertificate().getAllSignatureChainsFor(this).getCertificationAt(date);
            if (certificationAt == null || certificationAt.chainLinks.isEmpty()) {
                return null;
            }
            return certificationAt.getSignature();
        }

        public OpenPGPComponentSignature getLatestKeyRevocationSelfSignature() {
            return getLatestKeyRevocationSelfSignature(new Date());
        }

        public OpenPGPComponentSignature getLatestKeyRevocationSelfSignature(Date date) {
            OpenPGPSignatureChain revocationAt = getCertificate().getAllSignatureChainsFor(this).getRevocationAt(date);
            if (revocationAt == null || revocationAt.chainLinks.isEmpty()) {
                return null;
            }
            return revocationAt.getSignature();
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPCertificate.OpenPGPComponentKey, org.bouncycastle.openpgp.api.OpenPGPCertificate.OpenPGPCertificateComponent
        public OpenPGPComponentSignature getLatestSelfSignature(Date date) {
            ArrayList<OpenPGPComponentSignature> arrayList = new ArrayList();
            OpenPGPComponentSignature latestDirectKeySelfSignature = getLatestDirectKeySelfSignature(date);
            if (latestDirectKeySelfSignature != null) {
                arrayList.add(latestDirectKeySelfSignature);
            }
            OpenPGPComponentSignature latestKeyRevocationSelfSignature = getLatestKeyRevocationSelfSignature(date);
            if (latestKeyRevocationSelfSignature != null) {
                arrayList.add(latestKeyRevocationSelfSignature);
            }
            Iterator<OpenPGPIdentityComponent> it = getCertificate().getIdentities().iterator();
            while (it.hasNext()) {
                OpenPGPComponentSignature latestSelfSignature = it.next().getLatestSelfSignature(date);
                if (latestSelfSignature != null) {
                    arrayList.add(latestSelfSignature);
                }
            }
            OpenPGPComponentSignature openPGPComponentSignature = null;
            for (OpenPGPComponentSignature openPGPComponentSignature2 : arrayList) {
                if (openPGPComponentSignature == null || openPGPComponentSignature2.getCreationTime().after(openPGPComponentSignature.getCreationTime())) {
                    openPGPComponentSignature = openPGPComponentSignature2;
                }
            }
            return openPGPComponentSignature;
        }

        protected List<OpenPGPComponentSignature> getUserAttributeSignatures(OpenPGPUserAttribute openPGPUserAttribute) {
            return signIterToList(openPGPUserAttribute, this.rawPubkey.getSignaturesForUserAttribute(openPGPUserAttribute.getUserAttribute()));
        }

        public List<OpenPGPUserAttribute> getUserAttributes() {
            ArrayList arrayList = new ArrayList();
            for (OpenPGPIdentityComponent openPGPIdentityComponent : this.identityComponents) {
                if (openPGPIdentityComponent instanceof OpenPGPUserAttribute) {
                    arrayList.add((OpenPGPUserAttribute) openPGPIdentityComponent);
                }
            }
            return arrayList;
        }

        public List<OpenPGPUserId> getUserIDs() {
            ArrayList arrayList = new ArrayList();
            for (OpenPGPIdentityComponent openPGPIdentityComponent : this.identityComponents) {
                if (openPGPIdentityComponent instanceof OpenPGPUserId) {
                    arrayList.add((OpenPGPUserId) openPGPIdentityComponent);
                }
            }
            return arrayList;
        }

        protected List<OpenPGPComponentSignature> getUserIdSignatures(OpenPGPUserId openPGPUserId) {
            return signIterToList(openPGPUserId, this.rawPubkey.getSignaturesForID(openPGPUserId.getUserId()));
        }

        public List<OpenPGPUserId> getValidUserIDs(Date date) {
            ArrayList arrayList = new ArrayList();
            for (OpenPGPIdentityComponent openPGPIdentityComponent : this.identityComponents) {
                if ((openPGPIdentityComponent instanceof OpenPGPUserId) && openPGPIdentityComponent.isBoundAt(date)) {
                    arrayList.add((OpenPGPUserId) openPGPIdentityComponent);
                }
            }
            return arrayList;
        }

        public List<OpenPGPUserId> getValidUserIds() {
            return getValidUserIDs(new Date());
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPCertificate.OpenPGPComponentKey
        public boolean isPrimaryKey() {
            return true;
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPCertificate.OpenPGPCertificateComponent
        public String toDetailString() {
            return "PrimaryKey[" + getKeyIdentifier() + "] (" + UTCUtil.format(getCreationTime()) + ")";
        }

        public String toString() {
            return "PrimaryKey[" + Long.toHexString(getKeyIdentifier().getKeyId()).toUpperCase(Locale.getDefault()) + "]";
        }
    }

    /* JADX INFO: loaded from: classes3.dex */
    public static class OpenPGPSignatureChain implements Comparable<OpenPGPSignatureChain>, Iterable<Link> {
        private final List<Link> chainLinks;

        /* JADX INFO: loaded from: classes.dex */
        public static class Certification extends Link {
            public Certification(OpenPGPComponentSignature openPGPComponentSignature) {
                super(openPGPComponentSignature);
            }
        }

        public static abstract class Link {
            protected final OpenPGPComponentSignature signature;

            public Link(OpenPGPComponentSignature openPGPComponentSignature) {
                this.signature = openPGPComponentSignature;
            }

            public static Link create(OpenPGPComponentSignature openPGPComponentSignature) {
                return openPGPComponentSignature.isRevocation() ? new Revocation(openPGPComponentSignature) : new Certification(openPGPComponentSignature);
            }

            private Date getBackSigExpirationTime() {
                PGPSignatureSubpacketVector hashedSubPackets;
                if (this.signature.getSignature().getSignatureType() != 24 || (hashedSubPackets = this.signature.getSignature().getHashedSubPackets()) == null || (this.signature.getSignature().getHashedSubPackets().getKeyFlags() & 2) != 2) {
                    return null;
                }
                try {
                    PGPSignatureList embeddedSignatures = hashedSubPackets.getEmbeddedSignatures();
                    if (embeddedSignatures.isEmpty()) {
                        return null;
                    }
                    return new OpenPGPComponentSignature(embeddedSignatures.get(0), getSignature().getTargetKeyComponent(), getSignature().getIssuer()).getExpirationTime();
                } catch (PGPException e) {
                    return null;
                }
            }

            public OpenPGPComponentSignature getSignature() {
                return this.signature;
            }

            public Date since() {
                return this.signature.getCreationTime();
            }

            public String toString() {
                return this.signature.toString();
            }

            public Date until() {
                Date backSigExpirationTime = getBackSigExpirationTime();
                Date expirationTime = this.signature.getExpirationTime();
                if (expirationTime == null) {
                    return backSigExpirationTime;
                }
                return (backSigExpirationTime == null || expirationTime.before(backSigExpirationTime)) ? expirationTime : backSigExpirationTime;
            }

            public boolean verify(PGPContentVerifierBuilderProvider pGPContentVerifierBuilderProvider, OpenPGPPolicy openPGPPolicy) throws PGPSignatureException {
                this.signature.verify(pGPContentVerifierBuilderProvider, openPGPPolicy);
                return true;
            }
        }

        /* JADX INFO: loaded from: classes2.dex */
        public static class Revocation extends Link {
            public Revocation(OpenPGPComponentSignature openPGPComponentSignature) {
                super(openPGPComponentSignature);
            }

            @Override // org.bouncycastle.openpgp.api.OpenPGPCertificate.OpenPGPSignatureChain.Link
            public Date since() {
                return this.signature.signature.isHardRevocation() ? new Date(0L) : super.since();
            }

            @Override // org.bouncycastle.openpgp.api.OpenPGPCertificate.OpenPGPSignatureChain.Link
            public Date until() {
                return this.signature.signature.isHardRevocation() ? new Date(Long.MAX_VALUE) : super.until();
            }
        }

        private OpenPGPSignatureChain(List<Link> list) {
            this.chainLinks = new ArrayList();
            this.chainLinks.addAll(list);
        }

        private OpenPGPSignatureChain(Link link) {
            this.chainLinks = new ArrayList();
            this.chainLinks.add(link);
        }

        private OpenPGPSignatureChain(OpenPGPSignatureChain openPGPSignatureChain) {
            this(openPGPSignatureChain.chainLinks);
        }

        public static OpenPGPSignatureChain direct(OpenPGPComponentSignature openPGPComponentSignature) {
            return new OpenPGPSignatureChain(Link.create(openPGPComponentSignature));
        }

        @Override // java.lang.Comparable
        public int compareTo(OpenPGPSignatureChain openPGPSignatureChain) {
            if (this == openPGPSignatureChain) {
                return 0;
            }
            if (isHardRevocation()) {
                return -1;
            }
            if (openPGPSignatureChain.isHardRevocation()) {
                return 1;
            }
            int i = -getRootLink().since().compareTo(openPGPSignatureChain.getRootLink().since());
            if (i != 0) {
                return i;
            }
            int i2 = -getLeafLink().since().compareTo(openPGPSignatureChain.getLeafLink().since());
            if (i2 != 0) {
                return i2;
            }
            return isRevocation() ? 1 : -1;
        }

        public Link getLeafLink() {
            return this.chainLinks.get(this.chainLinks.size() - 1);
        }

        public OpenPGPComponentKey getLeafLinkTargetKey() {
            return getSignature().getTargetKeyComponent();
        }

        public OpenPGPComponentSignature getRevocation() {
            for (OpenPGPComponentSignature openPGPComponentSignature : getSignatures()) {
                if (openPGPComponentSignature.isRevocation()) {
                    return openPGPComponentSignature;
                }
            }
            return null;
        }

        public Link getRootLink() {
            return this.chainLinks.get(0);
        }

        public OpenPGPComponentKey getRootLinkIssuer() {
            return getRootLink().getSignature().getIssuer();
        }

        public OpenPGPComponentSignature getSignature() {
            return getLeafLink().getSignature();
        }

        public List<OpenPGPComponentSignature> getSignatures() {
            ArrayList arrayList = new ArrayList();
            Iterator<Link> it = this.chainLinks.iterator();
            while (it.hasNext()) {
                arrayList.add(it.next().getSignature());
            }
            return arrayList;
        }

        public Date getSince() {
            Iterator<Link> it = this.chainLinks.iterator();
            Date date = null;
            while (it.hasNext()) {
                Date creationTime = it.next().getSignature().getCreationTime();
                if (date == null || creationTime.after(date)) {
                    date = creationTime;
                }
            }
            return date;
        }

        public Date getUntil() {
            Iterator<Link> it = this.chainLinks.iterator();
            Date date = null;
            while (it.hasNext()) {
                Date dateUntil = it.next().until();
                if (dateUntil != null && (date == null || dateUntil.before(date))) {
                    date = dateUntil;
                }
            }
            return date;
        }

        public boolean isCertification() {
            Iterator<Link> it = this.chainLinks.iterator();
            while (it.hasNext()) {
                if (it.next() instanceof Revocation) {
                    return false;
                }
            }
            return true;
        }

        public boolean isEffectiveAt(Date date) {
            if (isHardRevocation()) {
                return true;
            }
            Date since = getSince();
            Date until = getUntil();
            return !date.before(since) && (until == null || !date.after(until));
        }

        public boolean isHardRevocation() {
            Iterator<Link> it = this.chainLinks.iterator();
            while (it.hasNext()) {
                if (it.next().signature.signature.isHardRevocation()) {
                    return true;
                }
            }
            return false;
        }

        public boolean isRevocation() {
            Iterator<Link> it = this.chainLinks.iterator();
            while (it.hasNext()) {
                if (it.next() instanceof Revocation) {
                    return true;
                }
            }
            return false;
        }

        public boolean isValid() throws PGPSignatureException {
            OpenPGPComponentKey rootLinkIssuer = getRootLinkIssuer();
            if (rootLinkIssuer == null) {
                throw new MissingIssuerCertException(getRootLink().signature, "Missing issuer certificate.");
            }
            OpenPGPCertificate certificate = rootLinkIssuer.getCertificate();
            return isValid(certificate.implementation.pgpContentVerifierBuilderProvider(), certificate.policy);
        }

        public boolean isValid(PGPContentVerifierBuilderProvider pGPContentVerifierBuilderProvider, OpenPGPPolicy openPGPPolicy) throws PGPSignatureException {
            boolean z = true;
            for (Link link : this.chainLinks) {
                if (!link.signature.isTested) {
                    link.verify(pGPContentVerifierBuilderProvider, openPGPPolicy);
                }
                if (!link.signature.isCorrect) {
                    z = false;
                }
            }
            return z;
        }

        @Override // java.lang.Iterable
        public Iterator<Link> iterator() {
            return this.chainLinks.iterator();
        }

        public OpenPGPSignatureChain plus(OpenPGPComponentSignature openPGPComponentSignature) {
            if (getLeafLinkTargetKey() != openPGPComponentSignature.getIssuerComponent()) {
                throw new IllegalArgumentException("Chain head is not equal to link issuer.");
            }
            OpenPGPSignatureChain openPGPSignatureChain = new OpenPGPSignatureChain(this);
            openPGPSignatureChain.chainLinks.add(Link.create(openPGPComponentSignature));
            return openPGPSignatureChain;
        }

        public String toString() {
            StringBuilder sb = new StringBuilder();
            sb.append("From ").append(UTCUtil.format(getSince())).append(" until ").append(getUntil() == null ? "EndOfTime" : UTCUtil.format(getUntil())).append("\n");
            Iterator<Link> it = this.chainLinks.iterator();
            while (it.hasNext()) {
                sb.append("  ").append(it.next().toString()).append("\n");
            }
            return sb.toString();
        }
    }

    /* JADX INFO: loaded from: classes.dex */
    public static class OpenPGPSignatureChains implements Iterable<OpenPGPSignatureChain> {
        private final Set<OpenPGPSignatureChain> chains = new TreeSet();
        private final OpenPGPCertificateComponent targetComponent;

        public OpenPGPSignatureChains(OpenPGPCertificateComponent openPGPCertificateComponent) {
            this.targetComponent = openPGPCertificateComponent;
        }

        public void add(OpenPGPSignatureChain openPGPSignatureChain) {
            this.chains.add(openPGPSignatureChain);
        }

        public void addAll(OpenPGPSignatureChains openPGPSignatureChains) {
            this.chains.addAll(openPGPSignatureChains.chains);
        }

        public OpenPGPSignatureChains fromOrigin(OpenPGPComponentKey openPGPComponentKey) {
            OpenPGPSignatureChains openPGPSignatureChains = new OpenPGPSignatureChains(openPGPComponentKey);
            for (OpenPGPSignatureChain openPGPSignatureChain : this.chains) {
                if (openPGPSignatureChain.getRootLinkIssuer() == openPGPComponentKey) {
                    openPGPSignatureChains.add(openPGPSignatureChain);
                }
            }
            return openPGPSignatureChains;
        }

        public OpenPGPSignatureChain getCertificationAt(Date date) {
            for (OpenPGPSignatureChain openPGPSignatureChain : this.chains) {
                boolean zIsEffectiveAt = openPGPSignatureChain.isEffectiveAt(date);
                boolean zIsCertification = openPGPSignatureChain.isCertification();
                if (zIsEffectiveAt && zIsCertification) {
                    return openPGPSignatureChain;
                }
            }
            return null;
        }

        public OpenPGPSignatureChain getChainAt(Date date) {
            Iterator<OpenPGPSignatureChain> it = getChainsAt(date).chains.iterator();
            if (it.hasNext()) {
                return it.next();
            }
            return null;
        }

        public OpenPGPSignatureChains getChainsAt(Date date) {
            OpenPGPSignatureChains openPGPSignatureChains = new OpenPGPSignatureChains(this.targetComponent);
            for (OpenPGPSignatureChain openPGPSignatureChain : this.chains) {
                if (openPGPSignatureChain.isEffectiveAt(date)) {
                    openPGPSignatureChains.add(openPGPSignatureChain);
                }
            }
            return openPGPSignatureChains;
        }

        public OpenPGPSignatureChain getRevocationAt(Date date) {
            for (OpenPGPSignatureChain openPGPSignatureChain : this.chains) {
                if (openPGPSignatureChain.isRevocation() && openPGPSignatureChain.isEffectiveAt(date)) {
                    return openPGPSignatureChain;
                }
            }
            return null;
        }

        public boolean isEmpty() {
            return this.chains.isEmpty();
        }

        @Override // java.lang.Iterable
        public Iterator<OpenPGPSignatureChain> iterator() {
            return this.chains.iterator();
        }

        public String toString() {
            StringBuilder sbAppend = new StringBuilder(this.targetComponent.toDetailString()).append(" is bound with ").append(this.chains.size()).append(" chains:").append("\n");
            Iterator<OpenPGPSignatureChain> it = this.chains.iterator();
            while (it.hasNext()) {
                sbAppend.append(it.next().toString());
            }
            return sbAppend.toString();
        }
    }

    /* JADX INFO: loaded from: classes3.dex */
    public static class OpenPGPSubkey extends OpenPGPComponentKey {
        public OpenPGPSubkey(PGPPublicKey pGPPublicKey, OpenPGPCertificate openPGPCertificate) {
            super(pGPPublicKey, openPGPCertificate);
        }

        protected List<OpenPGPComponentSignature> getKeySignatures() {
            Iterator<PGPSignature> signatures = this.rawPubkey.getSignatures();
            ArrayList arrayList = new ArrayList();
            while (signatures.hasNext()) {
                PGPSignature next = signatures.next();
                int signatureType = next.getSignatureType();
                if (signatureType == 24 || signatureType == 40) {
                    arrayList.add(new OpenPGPComponentSignature(next, getCertificate().getSigningKeyFor(next), this));
                }
            }
            return arrayList;
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPCertificate.OpenPGPComponentKey
        public boolean isPrimaryKey() {
            return false;
        }

        @Override // org.bouncycastle.openpgp.api.OpenPGPCertificate.OpenPGPCertificateComponent
        public String toDetailString() {
            return "Subkey[" + getKeyIdentifier() + "] (" + UTCUtil.format(getCreationTime()) + ")";
        }

        public String toString() {
            return "Subkey[" + Long.toHexString(getKeyIdentifier().getKeyId()).toUpperCase(Locale.getDefault()) + "]";
        }
    }

    public static class OpenPGPUserAttribute extends OpenPGPIdentityComponent {
        private final PGPUserAttributeSubpacketVector userAttribute;

        public OpenPGPUserAttribute(PGPUserAttributeSubpacketVector pGPUserAttributeSubpacketVector, OpenPGPPrimaryKey openPGPPrimaryKey) {
            super(openPGPPrimaryKey);
            this.userAttribute = pGPUserAttributeSubpacketVector;
        }

        public PGPUserAttributeSubpacketVector getUserAttribute() {
            return this.userAttribute;
        }

        public String toString() {
            return "UserAttribute" + this.userAttribute.toString();
        }
    }

    /* JADX INFO: loaded from: classes.dex */
    public static class OpenPGPUserId extends OpenPGPIdentityComponent {
        private final String userId;

        public OpenPGPUserId(String str, OpenPGPPrimaryKey openPGPPrimaryKey) {
            super(openPGPPrimaryKey);
            this.userId = str;
        }

        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (this == obj) {
                return true;
            }
            if (obj instanceof OpenPGPUserId) {
                return getUserId().equals(((OpenPGPUserId) obj).getUserId());
            }
            return false;
        }

        public String getUserId() {
            return this.userId;
        }

        public int hashCode() {
            return this.userId.hashCode();
        }

        public String toString() {
            return "UserID[" + this.userId + "]";
        }
    }

    public OpenPGPCertificate(PGPKeyRing pGPKeyRing) {
        this(pGPKeyRing, OpenPGPImplementation.getInstance());
    }

    public OpenPGPCertificate(PGPKeyRing pGPKeyRing, OpenPGPImplementation openPGPImplementation) {
        this(pGPKeyRing, openPGPImplementation, openPGPImplementation.policy());
    }

    public OpenPGPCertificate(PGPKeyRing pGPKeyRing, OpenPGPImplementation openPGPImplementation, OpenPGPPolicy openPGPPolicy) {
        this.implementation = openPGPImplementation;
        this.policy = openPGPPolicy;
        this.keyRing = pGPKeyRing;
        this.subkeys = new LinkedHashMap();
        this.componentSignatureChains = new LinkedHashMap();
        Iterator<PGPPublicKey> publicKeys = pGPKeyRing.getPublicKeys();
        this.primaryKey = new OpenPGPPrimaryKey(publicKeys.next(), this);
        processPrimaryKey(this.primaryKey);
        while (publicKeys.hasNext()) {
            PGPPublicKey next = publicKeys.next();
            OpenPGPSubkey openPGPSubkey = new OpenPGPSubkey(next, this);
            this.subkeys.put(next.getKeyIdentifier(), openPGPSubkey);
            processSubkey(openPGPSubkey);
        }
    }

    private void addSignaturesToChains(List<OpenPGPComponentSignature> list, OpenPGPSignatureChains openPGPSignatureChains) {
        Iterator<OpenPGPComponentSignature> it = list.iterator();
        while (it.hasNext()) {
            openPGPSignatureChains.add(OpenPGPSignatureChain.direct(it.next()));
        }
    }

    private List<OpenPGPComponentKey> filterKeys(Date date, KeyFilter keyFilter) {
        ArrayList arrayList = new ArrayList();
        for (OpenPGPComponentKey openPGPComponentKey : getKeys()) {
            if (isBound(openPGPComponentKey, date) && keyFilter.test(openPGPComponentKey, date)) {
                arrayList.add(openPGPComponentKey);
            }
        }
        return arrayList;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public OpenPGPSignatureChains getAllSignatureChainsFor(OpenPGPCertificateComponent openPGPCertificateComponent) {
        OpenPGPSignatureChains openPGPSignatureChains = new OpenPGPSignatureChains(openPGPCertificateComponent.getPublicComponent());
        openPGPSignatureChains.addAll(this.componentSignatureChains.get(openPGPCertificateComponent.getPublicComponent()));
        return openPGPSignatureChains;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public OpenPGPSignatureChain getPreferenceSignature(Date date) {
        OpenPGPSignatureChain certificationAt = getPrimaryKey().getSignatureChains().fromOrigin(getPrimaryKey()).getCertificationAt(date);
        if (certificationAt != null) {
            return certificationAt;
        }
        ArrayList<OpenPGPSignatureChain> arrayList = new ArrayList();
        Iterator<OpenPGPUserId> it = getPrimaryKey().getUserIDs().iterator();
        while (it.hasNext()) {
            OpenPGPSignatureChain certificationAt2 = getAllSignatureChainsFor(it.next()).fromOrigin(getPrimaryKey()).getCertificationAt(date);
            if (certificationAt2 != null) {
                arrayList.add(certificationAt2);
            }
        }
        arrayList.sort(new Comparator<OpenPGPSignatureChain>() { // from class: org.bouncycastle.openpgp.api.OpenPGPCertificate.6
            @Override // java.util.Comparator
            public int compare(OpenPGPSignatureChain openPGPSignatureChain, OpenPGPSignatureChain openPGPSignatureChain2) {
                return openPGPSignatureChain2.getSince().compareTo(openPGPSignatureChain.getSince());
            }
        });
        for (OpenPGPSignatureChain openPGPSignatureChain : arrayList) {
            if (openPGPSignatureChain.getSignature().getSignature().getHashedSubPackets().isPrimaryUserID()) {
                return openPGPSignatureChain;
            }
        }
        if (arrayList.isEmpty()) {
            return null;
        }
        return (OpenPGPSignatureChain) arrayList.get(0);
    }

    private OpenPGPSignatureChain getSignatureChainFor(OpenPGPCertificateComponent openPGPCertificateComponent, OpenPGPComponentKey openPGPComponentKey, Date date) {
        OpenPGPUserId primaryUserId;
        OpenPGPSignatureChains allSignatureChainsFor = getAllSignatureChainsFor(openPGPCertificateComponent);
        if (openPGPCertificateComponent == getPrimaryKey() && allSignatureChainsFor.getCertificationAt(date) == null && (primaryUserId = getPrimaryUserId(date)) != null) {
            allSignatureChainsFor.addAll(getAllSignatureChainsFor(primaryUserId));
        }
        OpenPGPSignatureChains openPGPSignatureChainsFromOrigin = allSignatureChainsFor.fromOrigin(openPGPComponentKey);
        if (openPGPSignatureChainsFromOrigin == null) {
            return null;
        }
        return openPGPSignatureChainsFromOrigin.getChainAt(date);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public boolean isBound(OpenPGPCertificateComponent openPGPCertificateComponent, Date date) {
        return isBoundBy(openPGPCertificateComponent, getPrimaryKey(), date);
    }

    private boolean isBoundBy(OpenPGPCertificateComponent openPGPCertificateComponent, OpenPGPComponentKey openPGPComponentKey, Date date) {
        OpenPGPSignature.OpenPGPSignatureSubpacket applyingSubpacket = openPGPCertificateComponent.getApplyingSubpacket(date, 9);
        if (applyingSubpacket != null) {
            KeyExpirationTime keyExpirationTime = (KeyExpirationTime) applyingSubpacket.getSubpacket();
            if (keyExpirationTime.getTime() != 0) {
                if (new Date((keyExpirationTime.getTime() * 1000) + openPGPCertificateComponent.getKeyComponent().getCreationTime().getTime()).before(date)) {
                    return false;
                }
            }
        }
        try {
            OpenPGPSignatureChain signatureChainFor = getSignatureChainFor(openPGPCertificateComponent, openPGPComponentKey, date);
            if (signatureChainFor != null && signatureChainFor.isValid(this.implementation.pgpContentVerifierBuilderProvider(), this.policy)) {
                return !signatureChainFor.isRevocation();
            }
            return false;
        } catch (PGPException e) {
            return false;
        }
    }

    public static OpenPGPCertificate join(OpenPGPCertificate openPGPCertificate, String str) throws IOException, PGPException {
        PGPObjectFactory pgpObjectFactory = openPGPCertificate.implementation.pgpObjectFactory(BCPGInputStream.wrap(PGPUtil.getDecoderStream(new ByteArrayInputStream(str.getBytes()))));
        while (true) {
            Object objNextObject = pgpObjectFactory.nextObject();
            if (objNextObject == null) {
                return null;
            }
            if (objNextObject instanceof PGPPublicKeyRing) {
                try {
                    return join(openPGPCertificate, new OpenPGPCertificate((PGPPublicKeyRing) objNextObject, openPGPCertificate.implementation));
                } catch (IllegalArgumentException e) {
                }
            } else {
                if (objNextObject instanceof PGPSecretKeyRing) {
                    throw new IllegalArgumentException("Joining with a secret key is not supported.");
                }
                if (objNextObject instanceof PGPSignatureList) {
                    PGPPublicKeyRing pGPPublicKeyRing = openPGPCertificate.getPGPPublicKeyRing();
                    PGPPublicKey publicKey = pGPPublicKeyRing.getPublicKey();
                    Iterator<PGPSignature> it = ((PGPSignatureList) objNextObject).iterator();
                    while (it.hasNext()) {
                        publicKey = PGPPublicKey.addCertification(publicKey, it.next());
                    }
                    return new OpenPGPCertificate(PGPPublicKeyRing.insertPublicKey(pGPPublicKeyRing, publicKey), openPGPCertificate.implementation);
                }
            }
        }
    }

    public static OpenPGPCertificate join(OpenPGPCertificate openPGPCertificate, OpenPGPCertificate openPGPCertificate2) throws PGPException {
        return new OpenPGPCertificate(PGPPublicKeyRing.join(openPGPCertificate.getPGPPublicKeyRing(), openPGPCertificate2.getPGPPublicKeyRing()), openPGPCertificate.implementation);
    }

    private void processPrimaryKey(OpenPGPPrimaryKey openPGPPrimaryKey) {
        OpenPGPSignatureChains openPGPSignatureChains = new OpenPGPSignatureChains(openPGPPrimaryKey);
        addSignaturesToChains(openPGPPrimaryKey.getKeySignatures(), openPGPSignatureChains);
        this.componentSignatureChains.put(openPGPPrimaryKey, openPGPSignatureChains);
        for (OpenPGPIdentityComponent openPGPIdentityComponent : openPGPPrimaryKey.identityComponents) {
            OpenPGPSignatureChains openPGPSignatureChains2 = new OpenPGPSignatureChains(openPGPIdentityComponent);
            addSignaturesToChains(openPGPIdentityComponent instanceof OpenPGPUserId ? openPGPPrimaryKey.getUserIdSignatures((OpenPGPUserId) openPGPIdentityComponent) : openPGPPrimaryKey.getUserAttributeSignatures((OpenPGPUserAttribute) openPGPIdentityComponent), openPGPSignatureChains2);
            this.componentSignatureChains.put(openPGPIdentityComponent, openPGPSignatureChains2);
        }
    }

    private void processSubkey(OpenPGPSubkey openPGPSubkey) {
        List<OpenPGPComponentSignature> keySignatures = openPGPSubkey.getKeySignatures();
        OpenPGPSignatureChains openPGPSignatureChains = new OpenPGPSignatureChains(openPGPSubkey);
        for (OpenPGPComponentSignature openPGPComponentSignature : keySignatures) {
            OpenPGPComponentKey signingKeyFor = openPGPSubkey.getCertificate().getSigningKeyFor(openPGPComponentSignature.getSignature());
            if (signingKeyFor != null) {
                OpenPGPSignatureChains allSignatureChainsFor = getAllSignatureChainsFor(signingKeyFor);
                if (allSignatureChainsFor.chains.isEmpty()) {
                    openPGPSignatureChains.add(new OpenPGPSignatureChain(OpenPGPSignatureChain.Link.create(openPGPComponentSignature)));
                } else {
                    Iterator it = allSignatureChainsFor.chains.iterator();
                    while (it.hasNext()) {
                        openPGPSignatureChains.add(((OpenPGPSignatureChain) it.next()).plus(openPGPComponentSignature));
                    }
                }
            }
        }
        this.componentSignatureChains.put(openPGPSubkey, openPGPSignatureChains);
    }

    public List<KeyIdentifier> getAllKeyIdentifiers() {
        ArrayList arrayList = new ArrayList();
        Iterator<PGPPublicKey> publicKeys = this.keyRing.getPublicKeys();
        while (publicKeys.hasNext()) {
            arrayList.add(publicKeys.next().getKeyIdentifier());
        }
        return arrayList;
    }

    public List<OpenPGPUserId> getAllUserIds() {
        return getPrimaryKey().getUserIDs();
    }

    public OpenPGPComponentSignature getCertification() {
        return getCertification(new Date());
    }

    public OpenPGPComponentSignature getCertification(Date date) {
        return this.primaryKey.getCertification(date);
    }

    public List<OpenPGPComponentKey> getCertificationKeys() {
        return getCertificationKeys(new Date());
    }

    public List<OpenPGPComponentKey> getCertificationKeys(Date date) {
        return filterKeys(date, new KeyFilter() { // from class: org.bouncycastle.openpgp.api.OpenPGPCertificate.5
            @Override // org.bouncycastle.openpgp.api.OpenPGPCertificate.KeyFilter
            public boolean test(OpenPGPComponentKey openPGPComponentKey, Date date2) {
                return openPGPComponentKey.isCertificationKey(date2);
            }
        });
    }

    public List<OpenPGPComponentKey> getComponentKeysWithFlag(Date date, final int... iArr) {
        return filterKeys(date, new KeyFilter(this) { // from class: org.bouncycastle.openpgp.api.OpenPGPCertificate.1
            final /* synthetic */ OpenPGPCertificate this$0;

            {
                this.this$0 = this;
            }

            @Override // org.bouncycastle.openpgp.api.OpenPGPCertificate.KeyFilter
            public boolean test(OpenPGPComponentKey openPGPComponentKey, Date date2) {
                return openPGPComponentKey.hasKeyFlags(date2, iArr);
            }
        });
    }

    public List<OpenPGPCertificateComponent> getComponents() {
        return new ArrayList(this.componentSignatureChains.keySet());
    }

    public OpenPGPSignatureChain getDelegationBy(OpenPGPCertificate openPGPCertificate) {
        return getDelegationBy(openPGPCertificate, new Date());
    }

    public OpenPGPSignatureChain getDelegationBy(OpenPGPCertificate openPGPCertificate, Date date) {
        return getPrimaryKey().getMergedDanglingExternalSignatureChainEndsFrom(openPGPCertificate, date).getCertificationAt(date);
    }

    public byte[] getEncoded() throws IOException {
        return getEncoded(PacketFormat.ROUNDTRIP);
    }

    public byte[] getEncoded(PacketFormat packetFormat) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        BCPGOutputStream bCPGOutputStream = new BCPGOutputStream(byteArrayOutputStream, packetFormat);
        ArrayList arrayList = new ArrayList();
        Iterator<PGPPublicKey> publicKeys = getPGPKeyRing().getPublicKeys();
        while (publicKeys.hasNext()) {
            arrayList.add(publicKeys.next());
        }
        new PGPPublicKeyRing(arrayList).encode(bCPGOutputStream, true);
        bCPGOutputStream.close();
        return byteArrayOutputStream.toByteArray();
    }

    public List<OpenPGPComponentKey> getEncryptionKeys() {
        return getEncryptionKeys(new Date());
    }

    public List<OpenPGPComponentKey> getEncryptionKeys(Date date) {
        return filterKeys(date, new KeyFilter() { // from class: org.bouncycastle.openpgp.api.OpenPGPCertificate.3
            @Override // org.bouncycastle.openpgp.api.OpenPGPCertificate.KeyFilter
            public boolean test(OpenPGPComponentKey openPGPComponentKey, Date date2) {
                return openPGPComponentKey.isEncryptionKey(date2);
            }
        });
    }

    public Date getExpirationTime() {
        return getExpirationTime(new Date());
    }

    public Date getExpirationTime(Date date) {
        return getPrimaryKey().getKeyExpirationDateAt(date);
    }

    public byte[] getFingerprint() {
        return this.primaryKey.getPGPPublicKey().getFingerprint();
    }

    public List<OpenPGPIdentityComponent> getIdentities() {
        return new ArrayList(this.primaryKey.identityComponents);
    }

    public OpenPGPComponentKey getKey(KeyIdentifier keyIdentifier) {
        return keyIdentifier.matchesExplicit(getPrimaryKey().getPGPPublicKey().getKeyIdentifier()) ? this.primaryKey : this.subkeys.get(keyIdentifier);
    }

    public KeyIdentifier getKeyIdentifier() {
        return this.primaryKey.getKeyIdentifier();
    }

    public List<OpenPGPComponentKey> getKeys() {
        ArrayList arrayList = new ArrayList();
        arrayList.add(this.primaryKey);
        arrayList.addAll(this.subkeys.values());
        return arrayList;
    }

    public Date getLastModificationDate() {
        return getLastModificationDateAt(new Date());
    }

    public Date getLastModificationDateAt(Date date) {
        Iterator<OpenPGPCertificateComponent> it = getComponents().iterator();
        Date creationTime = null;
        while (it.hasNext()) {
            Iterator<OpenPGPSignatureChain> it2 = getAllSignatureChainsFor(it.next()).getChainsAt(date).iterator();
            while (it2.hasNext()) {
                for (OpenPGPSignatureChain.Link link : it2.next()) {
                    if (creationTime == null || link.since().after(creationTime)) {
                        creationTime = link.since();
                    }
                }
            }
        }
        if (creationTime != null) {
            return creationTime;
        }
        for (OpenPGPComponentKey openPGPComponentKey : getKeys()) {
            if (!openPGPComponentKey.getCreationTime().after(date) && (creationTime == null || openPGPComponentKey.getCreationTime().after(creationTime))) {
                creationTime = openPGPComponentKey.getCreationTime();
            }
        }
        return creationTime;
    }

    public PGPKeyRing getPGPKeyRing() {
        return this.keyRing;
    }

    public PGPPublicKeyRing getPGPPublicKeyRing() {
        if (this.keyRing instanceof PGPPublicKeyRing) {
            return (PGPPublicKeyRing) this.keyRing;
        }
        ArrayList arrayList = new ArrayList();
        Iterator<PGPPublicKey> publicKeys = this.keyRing.getPublicKeys();
        while (publicKeys.hasNext()) {
            arrayList.add(publicKeys.next());
        }
        return new PGPPublicKeyRing(arrayList);
    }

    public String getPrettyFingerprint() {
        return FingerprintUtil.prettifyFingerprint(getFingerprint());
    }

    public OpenPGPPrimaryKey getPrimaryKey() {
        return this.primaryKey;
    }

    public OpenPGPUserId getPrimaryUserId() {
        return getPrimaryUserId(new Date());
    }

    public OpenPGPUserId getPrimaryUserId(Date date) {
        return this.primaryKey.getExplicitOrImplicitPrimaryUserId(date);
    }

    public Map<KeyIdentifier, OpenPGPComponentKey> getPublicKeys() {
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        linkedHashMap.put(this.primaryKey.getKeyIdentifier(), this.primaryKey);
        linkedHashMap.putAll(this.subkeys);
        return linkedHashMap;
    }

    public OpenPGPComponentSignature getRevocation() {
        return getRevocation(new Date());
    }

    public OpenPGPComponentSignature getRevocation(Date date) {
        return this.primaryKey.getRevocation(date);
    }

    public OpenPGPSignatureChain getRevocationBy(OpenPGPCertificate openPGPCertificate) {
        return getRevocationBy(openPGPCertificate, new Date());
    }

    public OpenPGPSignatureChain getRevocationBy(OpenPGPCertificate openPGPCertificate, Date date) {
        return getPrimaryKey().getMergedDanglingExternalSignatureChainEndsFrom(openPGPCertificate, date).getRevocationAt(date);
    }

    public OpenPGPComponentKey getSigningKeyFor(PGPSignature pGPSignature) {
        List<KeyIdentifier> keyIdentifiers = pGPSignature.getKeyIdentifiers();
        int signatureType = pGPSignature.getSignatureType();
        if (signatureType == 24 || signatureType == 40) {
            return this.primaryKey;
        }
        if (KeyIdentifier.matches(keyIdentifiers, getPrimaryKey().getKeyIdentifier(), true)) {
            return this.primaryKey;
        }
        for (KeyIdentifier keyIdentifier : this.subkeys.keySet()) {
            if (KeyIdentifier.matches(keyIdentifiers, keyIdentifier, true)) {
                return this.subkeys.get(keyIdentifier);
            }
        }
        return null;
    }

    public List<OpenPGPComponentKey> getSigningKeys() {
        return getSigningKeys(new Date());
    }

    public List<OpenPGPComponentKey> getSigningKeys(Date date) {
        return filterKeys(date, new KeyFilter() { // from class: org.bouncycastle.openpgp.api.OpenPGPCertificate.4
            @Override // org.bouncycastle.openpgp.api.OpenPGPCertificate.KeyFilter
            public boolean test(OpenPGPComponentKey openPGPComponentKey, Date date2) {
                return openPGPComponentKey.isSigningKey(date2);
            }
        });
    }

    public Map<KeyIdentifier, OpenPGPSubkey> getSubkeys() {
        return new LinkedHashMap(this.subkeys);
    }

    public OpenPGPUserId getUserId(String str) {
        for (OpenPGPUserId openPGPUserId : this.primaryKey.getUserIDs()) {
            if (openPGPUserId.getUserId().equals(str)) {
                return openPGPUserId;
            }
        }
        return null;
    }

    public List<OpenPGPComponentKey> getValidKeys() {
        return getValidKeys(new Date());
    }

    public List<OpenPGPComponentKey> getValidKeys(Date date) {
        return filterKeys(date, new KeyFilter() { // from class: org.bouncycastle.openpgp.api.OpenPGPCertificate.2
            @Override // org.bouncycastle.openpgp.api.OpenPGPCertificate.KeyFilter
            public boolean test(OpenPGPComponentKey openPGPComponentKey, Date date2) {
                return true;
            }
        });
    }

    public List<OpenPGPUserId> getValidUserIds() {
        return getValidUserIds(new Date());
    }

    public List<OpenPGPUserId> getValidUserIds(Date date) {
        return getPrimaryKey().getValidUserIDs(date);
    }

    public boolean isSecretKey() {
        return false;
    }

    public String toAsciiArmoredString() throws IOException {
        return toAsciiArmoredString(PacketFormat.ROUNDTRIP);
    }

    public String toAsciiArmoredString(PacketFormat packetFormat) throws IOException {
        ArmoredOutputStream.Builder builderClearHeaders = ArmoredOutputStream.builder().clearHeaders();
        builderClearHeaders.addSplitMultilineComment(getPrettyFingerprint());
        Iterator<OpenPGPUserId> it = getPrimaryKey().getUserIDs().iterator();
        while (it.hasNext()) {
            builderClearHeaders.addEllipsizedComment(it.next().getUserId());
        }
        return toAsciiArmoredString(packetFormat, builderClearHeaders);
    }

    public String toAsciiArmoredString(PacketFormat packetFormat, ArmoredOutputStream.Builder builder) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ArmoredOutputStream armoredOutputStreamBuild = builder.build(byteArrayOutputStream);
        armoredOutputStreamBuild.write(getEncoded(packetFormat));
        armoredOutputStreamBuild.close();
        return byteArrayOutputStream.toString();
    }
}
