package L3;

import T.p;
import X3.Y;
import X3.a0;
import X3.b0;
import X3.r0;
import com.google.crypto.tink.shaded.protobuf.AbstractC1253a;
import com.google.crypto.tink.shaded.protobuf.AbstractC1261i;
import com.google.crypto.tink.shaded.protobuf.C;
import com.google.crypto.tink.shaded.protobuf.C1260h;
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 m1.AbstractC2464a;

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

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

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

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

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

    static {
        Logger.getLogger(n.class.getName());
        f4186a = new AtomicReference(new e());
        f4187b = new ConcurrentHashMap();
        f4188c = new ConcurrentHashMap();
        new ConcurrentHashMap();
        f4189d = new ConcurrentHashMap();
    }

    public static synchronized void a(String str, Map map, boolean z6) {
        if (z6) {
            try {
                ConcurrentHashMap concurrentHashMap = f4188c;
                if (concurrentHashMap.containsKey(str) && !((Boolean) concurrentHashMap.get(str)).booleanValue()) {
                    throw new GeneralSecurityException("New keys are already disallowed for key type " + str);
                }
            } finally {
            }
        }
        if (z6) {
            if (((e) f4186a.get()).f4168a.containsKey(str)) {
                for (Map.Entry entry : map.entrySet()) {
                    if (!f4189d.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 (f4189d.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 {
        U3.a aVar;
        S3.n nVar = (S3.n) S3.h.f6229b.f6230a.get();
        nVar.getClass();
        S3.m mVar = new S3.m(bVar.getClass(), cls);
        HashMap map = nVar.f6239a;
        if (!map.containsKey(mVar)) {
            throw new GeneralSecurityException("No PrimitiveConstructor for " + mVar + " available");
        }
        switch (((S3.l) map.get(mVar)).f6236b.f1886a) {
            case 8:
                aVar = new U3.a();
                if (!A0.a.a(1)) {
                    throw new GeneralSecurityException("Can not use AES-CMAC in FIPS-mode.");
                }
                return aVar;
            default:
                aVar = new U3.a();
                if (!A0.a.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, AbstractC1261i abstractC1261i, Class cls) throws GeneralSecurityException {
        e eVar = (e) f4186a.get();
        eVar.getClass();
        d dVarA = eVar.a(str);
        boolean zContains = dVarA.f4166a.f6224b.keySet().contains(cls);
        S3.d dVar = dVarA.f4166a;
        if (!zContains) {
            StringBuilder sb = new StringBuilder("Primitive type ");
            sb.append(cls.getName());
            sb.append(" not supported by key manager of type ");
            sb.append(dVar.getClass());
            sb.append(", supported primitives: ");
            Set<Class> setKeySet = dVar.f6224b.keySet();
            StringBuilder sb2 = new StringBuilder();
            boolean z6 = true;
            for (Class cls2 : setKeySet) {
                if (!z6) {
                    sb2.append(", ");
                }
                sb2.append(cls2.getCanonicalName());
                z6 = false;
            }
            sb.append(sb2.toString());
            throw new GeneralSecurityException(sb.toString());
        }
        try {
            if (!dVar.f6224b.keySet().contains(cls) && !Void.class.equals(cls)) {
                throw new IllegalArgumentException("Given internalKeyMananger " + dVar.toString() + " does not support primitive class " + cls.getName());
            }
            try {
                AbstractC1253a abstractC1253aF = dVar.f(abstractC1261i);
                if (Void.class.equals(cls)) {
                    throw new GeneralSecurityException("Cannot create a primitive for Void");
                }
                dVar.g(abstractC1253aF);
                return dVar.c(abstractC1253aF, cls);
            } catch (C e6) {
                throw new GeneralSecurityException("Failures parsing proto of type ".concat(dVar.f6223a.getName()), e6);
            }
        } catch (IllegalArgumentException e7) {
            throw new GeneralSecurityException("Primitive type not supported", e7);
        }
    }

    public static Object d(String str, byte[] bArr) {
        C1260h c1260h = AbstractC1261i.f13790b;
        return c(str, AbstractC1261i.h(bArr, 0, bArr.length), a.class);
    }

    public static synchronized Y e(b0 b0Var) {
        f fVar;
        S3.d dVar = ((e) f4186a.get()).a(b0Var.A()).f4166a;
        fVar = new f(dVar, dVar.f6225c);
        if (!((Boolean) f4188c.get(b0Var.A())).booleanValue()) {
            throw new GeneralSecurityException("newKey-operation not permitted for key type " + b0Var.A());
        }
        return fVar.e(b0Var.B());
    }

    public static synchronized void f(S3.d dVar, boolean z6) {
        try {
            AtomicReference atomicReference = f4186a;
            e eVar = new e((e) atomicReference.get());
            eVar.b(dVar);
            String strB = dVar.b();
            a(strB, z6 ? dVar.d().g() : Collections.EMPTY_MAP, z6);
            if (!((e) atomicReference.get()).f4168a.containsKey(strB)) {
                f4187b.put(strB, new u4.c(7));
                if (z6) {
                    g(strB, dVar.d().g());
                }
            }
            f4188c.put(strB, Boolean.valueOf(z6));
            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 = f4189d;
            String str2 = (String) entry.getKey();
            byte[] bArrD = ((S3.c) entry.getValue()).f6221a.d();
            int i6 = ((S3.c) entry.getValue()).f6222b;
            a0 a0VarC = b0.C();
            a0VarC.d();
            b0.v((b0) a0VarC.f13838b, str);
            C1260h c1260hH = AbstractC1261i.h(bArrD, 0, bArrD.length);
            a0VarC.d();
            b0.w((b0) a0VarC.f13838b, c1260hH);
            int iC = AbstractC2464a.c(i6);
            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;
            }
            a0VarC.d();
            b0.x((b0) a0VarC.f13838b, r0Var);
            concurrentHashMap.put(str2, new g((b0) a0VarC.a()));
        }
    }

    public static synchronized void h(m mVar) {
        S3.h hVar = S3.h.f6229b;
        synchronized (hVar) {
            p pVar = new p((S3.n) hVar.f6230a.get());
            pVar.x(mVar);
            hVar.f6230a.set(new S3.n(pVar));
        }
    }
}
