package t2;

import F2.Y;
import F2.a0;
import F2.b0;
import F2.r0;
import com.google.crypto.tink.shaded.protobuf.AbstractC0472a;
import com.google.crypto.tink.shaded.protobuf.AbstractC0479h;
import com.google.crypto.tink.shaded.protobuf.B;
import com.google.crypto.tink.shaded.protobuf.C0478g;
import io.sentry.C0732k1;
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 m.C0863j;

/* 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 f9997a;

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

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

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

    static {
        Logger.getLogger(n.class.getName());
        f9997a = new AtomicReference(new e());
        f9998b = new ConcurrentHashMap();
        f9999c = new ConcurrentHashMap();
        new ConcurrentHashMap();
        f10000d = new ConcurrentHashMap();
    }

    public static synchronized void a(String str, Map map, boolean z5) {
        if (z5) {
            try {
                ConcurrentHashMap concurrentHashMap = f9999c;
                if (concurrentHashMap.containsKey(str) && !((Boolean) concurrentHashMap.get(str)).booleanValue()) {
                    throw new GeneralSecurityException("New keys are already disallowed for key type " + str);
                }
            } finally {
            }
        }
        if (z5) {
            if (((e) f9997a.get()).f9979a.containsKey(str)) {
                for (Map.Entry entry : map.entrySet()) {
                    if (!f10000d.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 (f10000d.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 {
        C2.a aVar;
        A2.o oVar = (A2.o) A2.i.f36b.f37a.get();
        oVar.getClass();
        A2.n nVar = new A2.n(bVar.getClass(), cls);
        HashMap map = oVar.f46a;
        if (!map.containsKey(nVar)) {
            throw new GeneralSecurityException("No PrimitiveConstructor for " + nVar + " available");
        }
        switch (((A2.m) map.get(nVar)).f43b.f139k) {
            case 0:
                aVar = new C2.a();
                if (!s0.c.a(1)) {
                    throw new GeneralSecurityException("Can not use AES-CMAC in FIPS-mode.");
                }
                return aVar;
            default:
                aVar = new C2.a();
                if (!s0.c.b(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, AbstractC0479h abstractC0479h, Class cls) throws GeneralSecurityException {
        e eVar = (e) f9997a.get();
        eVar.getClass();
        d dVarA = eVar.a(str);
        boolean zContains = dVarA.f9977a.f31b.keySet().contains(cls);
        A2.f fVar = dVarA.f9977a;
        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 = fVar.f31b.keySet();
            StringBuilder sb2 = new StringBuilder();
            boolean z5 = true;
            for (Class cls2 : setKeySet) {
                if (!z5) {
                    sb2.append(", ");
                }
                sb2.append(cls2.getCanonicalName());
                z5 = false;
            }
            sb.append(sb2.toString());
            throw new GeneralSecurityException(sb.toString());
        }
        try {
            if (!fVar.f31b.keySet().contains(cls) && !Void.class.equals(cls)) {
                throw new IllegalArgumentException("Given internalKeyMananger " + fVar.toString() + " does not support primitive class " + cls.getName());
            }
            try {
                AbstractC0472a abstractC0472aF = fVar.f(abstractC0479h);
                if (Void.class.equals(cls)) {
                    throw new GeneralSecurityException("Cannot create a primitive for Void");
                }
                fVar.g(abstractC0472aF);
                return fVar.c(abstractC0472aF, cls);
            } catch (B e2) {
                throw new GeneralSecurityException("Failures parsing proto of type ".concat(fVar.f30a.getName()), e2);
            }
        } catch (IllegalArgumentException e6) {
            throw new GeneralSecurityException("Primitive type not supported", e6);
        }
    }

    public static Object d(String str, byte[] bArr) {
        C0478g c0478g = AbstractC0479h.f5758l;
        return c(str, AbstractC0479h.c(bArr, 0, bArr.length), InterfaceC1024a.class);
    }

    public static synchronized Y e(b0 b0Var) {
        f fVar;
        A2.f fVar2 = ((e) f9997a.get()).a(b0Var.B()).f9977a;
        fVar = new f(fVar2, fVar2.f32c);
        if (!((Boolean) f9999c.get(b0Var.B())).booleanValue()) {
            throw new GeneralSecurityException("newKey-operation not permitted for key type " + b0Var.B());
        }
        return fVar.e(b0Var.C());
    }

    public static synchronized void f(A2.f fVar, boolean z5) {
        try {
            AtomicReference atomicReference = f9997a;
            e eVar = new e((e) atomicReference.get());
            eVar.b(fVar);
            String strB = fVar.b();
            a(strB, z5 ? fVar.d().h() : Collections.EMPTY_MAP, z5);
            if (!((e) atomicReference.get()).f9979a.containsKey(strB)) {
                f9998b.put(strB, new C0863j());
                if (z5) {
                    g(strB, fVar.d().h());
                }
            }
            f9999c.put(strB, Boolean.valueOf(z5));
            atomicReference.set(eVar);
        } catch (Throwable th) {
            throw th;
        }
    }

    public static void g(String str, Map map) {
        r0 r0Var;
        for (Map.Entry entry : map.entrySet()) {
            ConcurrentHashMap concurrentHashMap = f10000d;
            String str2 = (String) entry.getKey();
            byte[] bArrE = ((A2.d) entry.getValue()).f27a.e();
            int i = ((A2.d) entry.getValue()).f28b;
            a0 a0VarD = b0.D();
            a0VarD.e();
            b0.w((b0) a0VarD.f5805l, str);
            C0478g c0478gC = AbstractC0479h.c(bArrE, 0, bArrE.length);
            a0VarD.e();
            b0.x((b0) a0VarD.f5805l, c0478gC);
            int iC = u.e.c(i);
            if (iC == 0) {
                r0Var = r0.TINK;
            } else if (iC == 1) {
                r0Var = r0.LEGACY;
            } else if (iC == 2) {
                r0Var = r0.RAW;
            } else {
                if (iC != 3) {
                    throw new IllegalArgumentException("Unknown output prefix type");
                }
                r0Var = r0.CRUNCHY;
            }
            a0VarD.e();
            b0.y((b0) a0VarD.f5805l, r0Var);
            concurrentHashMap.put(str2, new g((b0) a0VarD.b()));
        }
    }

    public static synchronized void h(m mVar) {
        A2.i iVar = A2.i.f36b;
        synchronized (iVar) {
            C0732k1 c0732k1 = new C0732k1((A2.o) iVar.f37a.get());
            c0732k1.a0(mVar);
            iVar.f37a.set(new A2.o(c0732k1));
        }
    }
}
