package H3;

import O3.p;
import T3.Y;
import T3.a0;
import T3.b0;
import T3.r0;
import com.google.crypto.tink.shaded.protobuf.AbstractC0735a;
import com.google.crypto.tink.shaded.protobuf.AbstractC0742h;
import com.google.crypto.tink.shaded.protobuf.B;
import com.google.crypto.tink.shaded.protobuf.C0741g;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Logger;
import u.AbstractC1563h;

/* JADX INFO: loaded from: classes.dex */
public abstract class n {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final AtomicReference f3093a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final ConcurrentHashMap f3094b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final ConcurrentHashMap f3095c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final ConcurrentHashMap f3096d;

    static {
        Logger.getLogger(n.class.getName());
        f3093a = new AtomicReference(new e());
        f3094b = new ConcurrentHashMap();
        f3095c = new ConcurrentHashMap();
        new ConcurrentHashMap();
        f3096d = new ConcurrentHashMap();
    }

    public static synchronized void a(String str, Map map, boolean z8) {
        if (z8) {
            try {
                ConcurrentHashMap concurrentHashMap = f3095c;
                if (concurrentHashMap.containsKey(str) && !((Boolean) concurrentHashMap.get(str)).booleanValue()) {
                    throw new GeneralSecurityException("New keys are already disallowed for key type " + str);
                }
            } finally {
            }
        }
        if (z8) {
            if (((e) f3093a.get()).f3075a.containsKey(str)) {
                for (Map.Entry entry : map.entrySet()) {
                    if (!f3096d.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 (f3096d.containsKey(entry2.getKey())) {
                        throw new GeneralSecurityException("Attempted overwrite of a registered key template " + ((String) entry2.getKey()));
                    }
                }
            }
        }
    }

    public static Object b(b bVar, Class cls) throws GeneralSecurityException {
        Q3.a aVar;
        p pVar = (p) O3.j.f5153b.f5154a.get();
        pVar.getClass();
        O3.o oVar = new O3.o(bVar.getClass(), cls);
        HashMap map = pVar.f5163a;
        if (!map.containsKey(oVar)) {
            throw new GeneralSecurityException("No PrimitiveConstructor for " + oVar + " available");
        }
        switch (((O3.n) map.get(oVar)).f5160b.f3370q) {
            case 12:
                aVar = new Q3.a();
                if (!I1.a.b(1)) {
                    throw new GeneralSecurityException("Can not use AES-CMAC in FIPS-mode.");
                }
                return aVar;
            default:
                aVar = new Q3.a();
                if (!I1.a.c(2)) {
                    throw new GeneralSecurityException("Can not use HMAC in FIPS-mode, as BoringCrypto module is not available.");
                }
                return aVar;
        }
    }

    public static Object c(String str, AbstractC0742h abstractC0742h, Class cls) throws GeneralSecurityException {
        e eVar = (e) f3093a.get();
        eVar.getClass();
        d dVarA = eVar.a(str);
        boolean zContains = ((Map) dVarA.f3073a.f5149c).keySet().contains(cls);
        O3.f fVar = dVarA.f3073a;
        if (!zContains) {
            StringBuilder sb = new StringBuilder("Primitive type ");
            sb.append(cls.getName());
            sb.append(" not supported by key manager of type ");
            sb.append(fVar.getClass());
            sb.append(", supported primitives: ");
            Set<Class> setKeySet = ((Map) fVar.f5149c).keySet();
            StringBuilder sb2 = new StringBuilder();
            boolean z8 = true;
            for (Class cls2 : setKeySet) {
                if (!z8) {
                    sb2.append(", ");
                }
                sb2.append(cls2.getCanonicalName());
                z8 = false;
            }
            sb.append(sb2.toString());
            throw new GeneralSecurityException(sb.toString());
        }
        try {
            if (!((Map) fVar.f5149c).keySet().contains(cls) && !Void.class.equals(cls)) {
                throw new IllegalArgumentException("Given internalKeyMananger " + fVar.toString() + " does not support primitive class " + cls.getName());
            }
            try {
                AbstractC0735a abstractC0735aH = fVar.h(abstractC0742h);
                if (Void.class.equals(cls)) {
                    throw new GeneralSecurityException("Cannot create a primitive for Void");
                }
                fVar.m(abstractC0735aH);
                return fVar.d(abstractC0735aH, cls);
            } catch (B e8) {
                throw new GeneralSecurityException("Failures parsing proto of type ".concat(((Class) fVar.f5147a).getName()), e8);
            }
        } catch (IllegalArgumentException e9) {
            throw new GeneralSecurityException("Primitive type not supported", e9);
        }
    }

    public static synchronized Y d(b0 b0Var) {
        f fVar;
        O3.f fVar2 = ((e) f3093a.get()).a(b0Var.B()).f3073a;
        fVar = new f(fVar2, (Class) fVar2.f5148b);
        if (!((Boolean) f3095c.get(b0Var.B())).booleanValue()) {
            throw new GeneralSecurityException("newKey-operation not permitted for key type " + b0Var.B());
        }
        return fVar.f(b0Var.C());
    }

    public static synchronized void e(O3.f fVar, boolean z8) {
        try {
            AtomicReference atomicReference = f3093a;
            e eVar = new e((e) atomicReference.get());
            eVar.b(fVar);
            String strC = fVar.c();
            a(strC, z8 ? fVar.f().u() : Collections.EMPTY_MAP, z8);
            if (!((e) atomicReference.get()).f3075a.containsKey(strC)) {
                f3094b.put(strC, new C4.f(5));
                if (z8) {
                    f(strC, fVar.f().u());
                }
            }
            f3095c.put(strC, Boolean.valueOf(z8));
            atomicReference.set(eVar);
        } catch (Throwable th) {
            throw th;
        }
    }

    public static void f(String str, Map map) {
        r0 r0Var;
        for (Map.Entry entry : map.entrySet()) {
            ConcurrentHashMap concurrentHashMap = f3096d;
            String str2 = (String) entry.getKey();
            byte[] bArrE = ((O3.d) entry.getValue()).f5144a.e();
            int i = ((O3.d) entry.getValue()).f5145b;
            a0 a0VarD = b0.D();
            a0VarD.h();
            b0.w((b0) a0VarD.f10543r, str);
            C0741g c0741gC = AbstractC0742h.c(bArrE, 0, bArrE.length);
            a0VarD.h();
            b0.x((b0) a0VarD.f10543r, c0741gC);
            int iC = AbstractC1563h.c(i);
            if (iC == 0) {
                r0Var = r0.f6587s;
            } else if (iC == 1) {
                r0Var = r0.f6588t;
            } else if (iC == 2) {
                r0Var = r0.f6589u;
            } else {
                if (iC != 3) {
                    throw new IllegalArgumentException("Unknown output prefix type");
                }
                r0Var = r0.f6590v;
            }
            a0VarD.h();
            b0.y((b0) a0VarD.f10543r, r0Var);
            concurrentHashMap.put(str2, new g((b0) a0VarD.d()));
        }
    }

    public static synchronized void g(m mVar) {
        O3.j.f5153b.c(mVar);
    }
}
