package com.google.android.gms.internal.pal;

import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;

/* JADX INFO: loaded from: classes2.dex */
public abstract class u7 {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final Logger f21239a = Logger.getLogger(u7.class.getName());

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final AtomicReference f21240b = new AtomicReference(new j7());

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final ConcurrentHashMap f21241c = new ConcurrentHashMap();

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final ConcurrentHashMap f21242d = new ConcurrentHashMap();

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final ConcurrentHashMap f21243e = new ConcurrentHashMap();

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final ConcurrentHashMap f21244f = new ConcurrentHashMap();

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final ConcurrentHashMap f21245g = new ConcurrentHashMap();

    public static void a(String str) {
        if (str == null) {
            throw new IllegalArgumentException("catalogueName must be non-null.");
        }
        ConcurrentHashMap concurrentHashMap = f21243e;
        Locale locale = Locale.US;
        com.google.android.gms.measurement.internal.u1.x(concurrentHashMap.get(str.toLowerCase(locale)));
        String strConcat = String.format("no catalogue found for %s. ", str);
        if (str.toLowerCase(locale).startsWith("tinkaead")) {
            strConcat = String.valueOf(strConcat).concat("Maybe call AeadConfig.register().");
        }
        if (str.toLowerCase(locale).startsWith("tinkdeterministicaead")) {
            strConcat = String.valueOf(strConcat).concat("Maybe call DeterministicAeadConfig.register().");
        } else if (str.toLowerCase(locale).startsWith("tinkstreamingaead")) {
            strConcat = String.valueOf(strConcat).concat("Maybe call StreamingAeadConfig.register().");
        } else if (str.toLowerCase(locale).startsWith("tinkhybriddecrypt") || str.toLowerCase(locale).startsWith("tinkhybridencrypt")) {
            strConcat = String.valueOf(strConcat).concat("Maybe call HybridConfig.register().");
        } else if (str.toLowerCase(locale).startsWith("tinkmac")) {
            strConcat = String.valueOf(strConcat).concat("Maybe call MacConfig.register().");
        } else if (str.toLowerCase(locale).startsWith("tinkpublickeysign") || str.toLowerCase(locale).startsWith("tinkpublickeyverify")) {
            strConcat = String.valueOf(strConcat).concat("Maybe call SignatureConfig.register().");
        } else if (str.toLowerCase(locale).startsWith("tink")) {
            strConcat = String.valueOf(strConcat).concat("Maybe call TinkConfig.register().");
        }
        throw new GeneralSecurityException(strConcat);
    }

    public static synchronized z0 b(ad adVar) {
        m5 m5VarZzb;
        zzaby zzabyVarP;
        try {
            m5VarZzb = ((j7) f21240b.get()).d(adVar.q()).zzb();
            if (!((Boolean) f21242d.get(adVar.q())).booleanValue()) {
                throw new GeneralSecurityException("newKey-operation not permitted for key type ".concat(String.valueOf(adVar.q())));
            }
            zzabyVarP = adVar.p();
            m5VarZzb.getClass();
            try {
            } catch (zzadi e10) {
                throw new GeneralSecurityException("Failures parsing proto of type ".concat(((k.e) m5VarZzb.f21081b).b().f20778a.getName()), e10);
            }
        } catch (Throwable th) {
            throw th;
        }
        return new o3(((k.e) m5VarZzb.f21081b).b(), 4).e(zzabyVarP);
    }

    public static Object c(String str, d0 d0Var, Class cls) throws GeneralSecurityException {
        m5 m5VarA = ((j7) f21240b.get()).a(cls, str);
        k.e eVar = (k.e) m5VarA.f21081b;
        String strConcat = "Expected proto of type ".concat(((Class) eVar.f28819a).getName());
        if (((Class) eVar.f28819a).isInstance(d0Var)) {
            return m5VarA.e(d0Var);
        }
        throw new GeneralSecurityException(strConcat);
    }

    public static synchronized void d(o9 o9Var, b8 b8Var) {
        try {
            AtomicReference atomicReference = f21240b;
            j7 j7Var = new j7((j7) atomicReference.get());
            j7Var.b(o9Var, b8Var);
            String strE = o9Var.e();
            String strE2 = b8Var.e();
            g(strE, o9Var.b().c(), true);
            g(strE2, Collections.emptyMap(), false);
            if (!((j7) atomicReference.get()).f20996a.containsKey(strE)) {
                f21241c.put(strE, new o3(o9Var, 5));
                h(o9Var.e(), o9Var.b().c());
            }
            ConcurrentHashMap concurrentHashMap = f21242d;
            concurrentHashMap.put(strE, Boolean.TRUE);
            concurrentHashMap.put(strE2, Boolean.FALSE);
            atomicReference.set(j7Var);
        } catch (Throwable th) {
            throw th;
        }
    }

    public static synchronized void e(b8 b8Var) {
        try {
            AtomicReference atomicReference = f21240b;
            j7 j7Var = new j7((j7) atomicReference.get());
            j7Var.c(b8Var);
            String strE = b8Var.e();
            g(strE, b8Var.b().c(), true);
            if (!((j7) atomicReference.get()).f20996a.containsKey(strE)) {
                f21241c.put(strE, new o3(b8Var, 5));
                h(strE, b8Var.b().c());
            }
            f21242d.put(strE, Boolean.TRUE);
            atomicReference.set(j7Var);
        } catch (Throwable th) {
            throw th;
        }
    }

    public static synchronized void f(s7 s7Var) {
        try {
            Class clsZzb = s7Var.zzb();
            ConcurrentHashMap concurrentHashMap = f21244f;
            if (concurrentHashMap.containsKey(clsZzb)) {
                s7 s7Var2 = (s7) concurrentHashMap.get(clsZzb);
                if (!s7Var.getClass().getName().equals(s7Var2.getClass().getName())) {
                    f21239a.logp(Level.WARNING, "com.google.crypto.tink.Registry", "registerPrimitiveWrapper", "Attempted overwrite of a registered PrimitiveWrapper for type ".concat(clsZzb.toString()));
                    throw new GeneralSecurityException(String.format("PrimitiveWrapper for primitive (%s) is already registered to be %s, cannot be re-registered with %s", clsZzb.getName(), s7Var2.getClass().getName(), s7Var.getClass().getName()));
                }
            }
            concurrentHashMap.put(clsZzb, s7Var);
        } catch (Throwable th) {
            throw th;
        }
    }

    public static synchronized void g(String str, Map map, boolean z10) {
        if (z10) {
            try {
                ConcurrentHashMap concurrentHashMap = f21242d;
                if (concurrentHashMap.containsKey(str) && !((Boolean) concurrentHashMap.get(str)).booleanValue()) {
                    throw new GeneralSecurityException("New keys are already disallowed for key type ".concat(str));
                }
                if (((j7) f21240b.get()).f20996a.containsKey(str)) {
                    for (Map.Entry entry : map.entrySet()) {
                        if (!f21245g.containsKey(entry.getKey())) {
                            throw new GeneralSecurityException("Attempted to register a new key template " + ((String) entry.getKey()) + " from an existing key manager of type " + str);
                        }
                    }
                } else {
                    for (Map.Entry entry2 : map.entrySet()) {
                        if (f21245g.containsKey(entry2.getKey())) {
                            throw new GeneralSecurityException("Attempted overwrite of a registered key template ".concat(String.valueOf((String) entry2.getKey())));
                        }
                    }
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    /* JADX WARN: Type inference failed for: r3v2, types: [com.google.android.gms.internal.pal.z0, java.lang.Object] */
    public static void h(String str, Map map) {
        for (Map.Entry entry : map.entrySet()) {
            ConcurrentHashMap concurrentHashMap = f21245g;
            String str2 = (String) entry.getKey();
            byte[] bArrC = ((d9) entry.getValue()).f20757a.c();
            int i10 = ((d9) entry.getValue()).f20758b;
            zc zcVarN = ad.n();
            if (zcVarN.f20700c) {
                zcVarN.i();
                zcVarN.f20700c = false;
            }
            ad.r((ad) zcVarN.f20699b, str);
            zzaby zzabyVarK = zzaby.k(0, bArrC.length, bArrC);
            if (zcVarN.f20700c) {
                zcVarN.i();
                zcVarN.f20700c = false;
            }
            ((ad) zcVarN.f20699b).zzf = zzabyVarK;
            int i11 = i10 - 1;
            int i12 = i11 != 0 ? i11 != 1 ? 5 : 4 : 3;
            if (zcVarN.f20700c) {
                zcVarN.i();
                zcVarN.f20700c = false;
            }
            ((ad) zcVarN.f20699b).zzg = q9.A(i12);
            concurrentHashMap.put(str2, new l7((ad) zcVarN.g()));
        }
    }
}
