package da;

import com.google.crypto.tink.internal.TinkBugException;
import com.google.crypto.tink.proto.KeyData$KeyMaterialType;
import com.google.crypto.tink.proto.KeyStatusType;
import com.google.crypto.tink.proto.OutputPrefixType;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import ma.f0;
import ma.g0;
import ma.h0;
import ma.l0;
import ma.n0;
import ma.s;
import ma.t;
import ra.a1;
import ra.b1;
import ra.c1;
import ra.d1;
import ra.v0;
import ra.w0;

/* JADX INFO: loaded from: classes2.dex */
public final class h {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final List f25082a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final ma.l f25083b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final h f25084c;

    public h(List list, ma.l lVar) throws GeneralSecurityException {
        this.f25082a = list;
        this.f25083b = lVar;
        if (((AtomicBoolean) ha.a.f26450a.f25365a).get()) {
            HashSet hashSet = new HashSet();
            Iterator it = list.iterator();
            boolean z10 = false;
            while (it.hasNext()) {
                g gVar = (g) it.next();
                boolean zContains = hashSet.contains(Integer.valueOf(gVar.f25078d));
                int i10 = gVar.f25078d;
                if (zContains) {
                    throw new GeneralSecurityException(defpackage.e.e("KeyID ", i10, " is duplicated in the keyset, and Tink is configured to reject such keysets with the flag validateKeysetsOnParsing."));
                }
                hashSet.add(Integer.valueOf(i10));
                if (gVar.f25079e) {
                    z10 = true;
                }
            }
            if (!z10) {
                throw new GeneralSecurityException("Primary key id not found in keyset, and Tink is configured to reject such keysets with the flag validateKeysetsOnParsing.");
            }
        }
        this.f25084c = null;
    }

    public static c1 a(com.bumptech.glide.g gVar, KeyStatusType keyStatusType, int i10) throws GeneralSecurityException {
        h0 h0Var = (h0) t.f30555b.g(gVar);
        Integer numI = gVar.i();
        if (numI != null && numI.intValue() != i10) {
            throw new GeneralSecurityException("Wrong ID set for key with ID requirement");
        }
        b1 b1VarM = c1.M();
        v0 v0VarK = w0.K();
        String str = h0Var.f30520a;
        v0VarK.f();
        w0.D((w0) v0VarK.f23982b, str);
        v0VarK.f();
        w0.E((w0) v0VarK.f23982b, h0Var.f30522c);
        v0VarK.f();
        w0.F((w0) v0VarK.f23982b, h0Var.f30523d);
        b1VarM.f();
        c1.D((c1) b1VarM.f23982b, (w0) v0VarK.c());
        b1VarM.f();
        c1.F((c1) b1VarM.f23982b, keyStatusType);
        b1VarM.f();
        c1.G((c1) b1VarM.f23982b, i10);
        b1VarM.f();
        c1.E((c1) b1VarM.f23982b, h0Var.f30524e);
        return (c1) b1VarM.c();
    }

    public static final h b(d1 d1Var) throws GeneralSecurityException {
        com.bumptech.glide.g jVar;
        boolean z10;
        if (d1Var.G() <= 0) {
            throw new GeneralSecurityException("empty keyset");
        }
        ArrayList arrayList = new ArrayList(d1Var.G());
        for (c1 c1Var : d1Var.H()) {
            int I = c1Var.I();
            try {
                jVar = h(c1Var);
                z10 = false;
            } catch (GeneralSecurityException e10) {
                if (((AtomicBoolean) ha.a.f26450a.f25365a).get()) {
                    throw e10;
                }
                jVar = new ma.j(i(c1Var));
                z10 = true;
            }
            if (((AtomicBoolean) ha.a.f26450a.f25365a).get() && !g(c1Var.K())) {
                throw new GeneralSecurityException("Parsing of a single key failed (wrong status) and Tink is configured via validateKeysetsOnParsing to reject such keysets.");
            }
            arrayList.add(new g(jVar, c1Var.K(), I, I == d1Var.I(), z10, g.f25074h));
        }
        return new h(Collections.unmodifiableList(arrayList), ma.l.f30533b);
    }

    public static boolean g(KeyStatusType keyStatusType) {
        int iOrdinal = keyStatusType.ordinal();
        return iOrdinal == 1 || iOrdinal == 2 || iOrdinal == 3;
    }

    public static com.bumptech.glide.g h(c1 c1Var) {
        h0 h0VarI = i(c1Var);
        t tVar = t.f30555b;
        n0 n0Var = (n0) tVar.f30556a.get();
        n0Var.getClass();
        return !n0Var.f30542b.containsKey(new l0(h0.class, h0VarI.f30521b)) ? new ma.j(h0VarI) : tVar.a(h0VarI);
    }

    public static h0 i(c1 c1Var) {
        return h0.a(c1Var.H().I(), c1Var.H().J(), c1Var.H().H(), c1Var.J(), c1Var.J() == OutputPrefixType.RAW ? null : Integer.valueOf(c1Var.I()));
    }

    public final g c(int i10) {
        List list = this.f25082a;
        if (i10 < 0 || i10 >= list.size()) {
            StringBuilder sbQ = defpackage.e.q("Invalid index ", i10, " for keyset of size ");
            sbQ.append(list.size());
            throw new IndexOutOfBoundsException(sbQ.toString());
        }
        g gVar = (g) list.get(i10);
        if (!g(gVar.f25076b)) {
            throw new IllegalStateException(defpackage.e.e("Keyset-Entry at position ", i10, " has wrong status"));
        }
        if (gVar.f25080f) {
            throw new IllegalStateException(defpackage.e.e("Keyset-Entry at position ", i10, " didn't parse correctly"));
        }
        return (g) list.get(i10);
    }

    public final d1 d() {
        try {
            a1 a1VarJ = d1.J();
            for (g gVar : this.f25082a) {
                com.bumptech.glide.g gVarA = gVar.a();
                int i10 = gVar.f25078d;
                c1 c1VarA = a(gVarA, gVar.f25076b, i10);
                a1VarJ.f();
                d1.E((d1) a1VarJ.f23982b, c1VarA);
                if (gVar.f25079e) {
                    a1VarJ.f();
                    d1.D((d1) a1VarJ.f23982b, i10);
                }
            }
            return (d1) a1VarJ.c();
        } catch (GeneralSecurityException e10) {
            throw new TinkBugException(e10);
        }
    }

    public final g e() {
        for (g gVar : this.f25082a) {
            if (gVar != null && gVar.f25079e) {
                if (gVar.f25077c == c.f25064b) {
                    return gVar;
                }
                throw new IllegalStateException("Keyset has primary which isn't enabled");
            }
        }
        throw new IllegalStateException("Keyset has no valid primary");
    }

    public final Object f(Class cls) throws GeneralSecurityException {
        h hVar = this.f25084c;
        d1 d1VarD = (hVar == null ? this : hVar).d();
        int i10 = m.f25086a;
        int I = d1VarD.I();
        int i11 = 0;
        boolean z10 = true;
        int i12 = 0;
        boolean z11 = false;
        for (c1 c1Var : d1VarD.H()) {
            if (c1Var.K() == KeyStatusType.ENABLED) {
                if (!c1Var.L()) {
                    throw new GeneralSecurityException(String.format("key %d has no key data", Integer.valueOf(c1Var.I())));
                }
                if (c1Var.J() == OutputPrefixType.UNKNOWN_PREFIX) {
                    throw new GeneralSecurityException(String.format("key %d has unknown prefix", Integer.valueOf(c1Var.I())));
                }
                if (c1Var.K() == KeyStatusType.UNKNOWN_STATUS) {
                    throw new GeneralSecurityException(String.format("key %d has unknown status", Integer.valueOf(c1Var.I())));
                }
                if (c1Var.I() == I) {
                    if (z11) {
                        throw new GeneralSecurityException("keyset contains multiple primary keys");
                    }
                    z11 = true;
                }
                if (c1Var.H().H() != KeyData$KeyMaterialType.ASYMMETRIC_PUBLIC) {
                    z10 = false;
                }
                i12++;
            }
        }
        if (i12 == 0) {
            throw new GeneralSecurityException("keyset must contain at least one ENABLED key");
        }
        if (!z11 && !z10) {
            throw new GeneralSecurityException("keyset doesn't contain a valid primary key");
        }
        while (true) {
            List list = this.f25082a;
            if (i11 >= list.size()) {
                if (hVar == null) {
                    hVar = this;
                }
                f0 f0Var = (f0) s.f30553b.f30554a.get();
                HashMap map = f0Var.f30515b;
                if (map.containsKey(cls)) {
                    g0 g0Var = (g0) map.get(cls);
                    return g0Var.c(hVar, this.f25083b, new androidx.media3.exoplayer.analytics.g(f0Var, g0Var, 15));
                }
                throw new GeneralSecurityException("No wrapper found for " + cls);
            }
            if (((g) list.get(i11)).f25080f || !g(((g) list.get(i11)).f25076b)) {
                break;
            }
            i11++;
        }
        c1 c1VarF = d1VarD.F(i11);
        StringBuilder sbQ = defpackage.e.q("Key parsing of key with index ", i11, " and type_url ");
        sbQ.append(c1VarF.H().I());
        sbQ.append(" failed, unable to get primitive");
        throw new GeneralSecurityException(sbQ.toString());
    }

    public final String toString() {
        return m.a(d()).toString();
    }

    public h(ArrayList arrayList, ma.l lVar, h hVar) {
        this.f25082a = arrayList;
        this.f25083b = lVar;
        this.f25084c = hVar;
    }
}
