package fk;

import com.google.crypto.tink.shaded.protobuf.o0;
import fk.p;
import java.security.GeneralSecurityException;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.logging.Logger;
import mk.a0;
import mk.c0;
import mk.y;
import mk.z;

/* loaded from: classes3.dex */
public abstract class r {

    /* renamed from: a, reason: collision with root package name */
    private static final Logger f18539a = Logger.getLogger(r.class.getName());

    /* renamed from: b, reason: collision with root package name */
    private static final ConcurrentMap f18540b = new ConcurrentHashMap();

    /* renamed from: c, reason: collision with root package name */
    private static final ConcurrentMap f18541c = new ConcurrentHashMap();

    /* renamed from: d, reason: collision with root package name */
    private static final ConcurrentMap f18542d = new ConcurrentHashMap();

    /* renamed from: e, reason: collision with root package name */
    private static final ConcurrentMap f18543e = new ConcurrentHashMap();

    /* renamed from: f, reason: collision with root package name */
    private static final ConcurrentMap f18544f = new ConcurrentHashMap();

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes3.dex */
    public class a implements d {

        /* renamed from: a, reason: collision with root package name */
        final /* synthetic */ h f18545a;

        a(h hVar) {
            this.f18545a = hVar;
        }

        @Override // fk.r.d
        public e a(Class cls) {
            try {
                return new f(this.f18545a, cls);
            } catch (IllegalArgumentException e10) {
                throw new GeneralSecurityException("Primitive type not supported", e10);
            }
        }

        @Override // fk.r.d
        public e b() {
            h hVar = this.f18545a;
            return new f(hVar, hVar.a());
        }

        @Override // fk.r.d
        public Class c() {
            return this.f18545a.getClass();
        }

        @Override // fk.r.d
        public Set d() {
            return this.f18545a.h();
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes3.dex */
    public class b implements c {

        /* renamed from: a, reason: collision with root package name */
        final /* synthetic */ h f18546a;

        b(h hVar) {
            this.f18546a = hVar;
        }
    }

    /* loaded from: classes3.dex */
    private interface c {
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes3.dex */
    public interface d {
        e a(Class cls);

        e b();

        Class c();

        Set d();
    }

    private static Object a(Object obj) {
        obj.getClass();
        return obj;
    }

    private static d b(h hVar) {
        return new a(hVar);
    }

    private static c c(h hVar) {
        return new b(hVar);
    }

    private static synchronized void d(String str, Class cls, boolean z10) {
        synchronized (r.class) {
            ConcurrentMap concurrentMap = f18540b;
            if (concurrentMap.containsKey(str)) {
                d dVar = (d) concurrentMap.get(str);
                if (!dVar.c().equals(cls)) {
                    f18539a.warning("Attempted overwrite of a registered key manager for key type " + str);
                    throw new GeneralSecurityException(String.format("typeUrl (%s) is already registered with %s, cannot be re-registered with %s", str, dVar.c().getName(), cls.getName()));
                }
                if (z10 && !((Boolean) f18542d.get(str)).booleanValue()) {
                    throw new GeneralSecurityException("New keys are already disallowed for key type " + str);
                }
            }
        }
    }

    public static Class e(Class cls) {
        q qVar = (q) f18544f.get(cls);
        if (qVar == null) {
            return null;
        }
        return qVar.a();
    }

    private static synchronized d f(String str) {
        d dVar;
        synchronized (r.class) {
            ConcurrentMap concurrentMap = f18540b;
            if (!concurrentMap.containsKey(str)) {
                throw new GeneralSecurityException("No key manager found for key type " + str);
            }
            dVar = (d) concurrentMap.get(str);
        }
        return dVar;
    }

    private static e g(String str, Class cls) {
        d f10 = f(str);
        if (cls == null) {
            return f10.b();
        }
        if (f10.d().contains(cls)) {
            return f10.a(cls);
        }
        throw new GeneralSecurityException("Primitive type " + cls.getName() + " not supported by key manager of type " + f10.c() + ", supported primitives: " + s(f10.d()));
    }

    public static Object h(String str, com.google.crypto.tink.shaded.protobuf.h hVar, Class cls) {
        return j(str, hVar, (Class) a(cls));
    }

    public static Object i(String str, byte[] bArr, Class cls) {
        return h(str, com.google.crypto.tink.shaded.protobuf.h.l(bArr), cls);
    }

    private static Object j(String str, com.google.crypto.tink.shaded.protobuf.h hVar, Class cls) {
        return g(str, cls).d(hVar);
    }

    public static p k(i iVar, e eVar, Class cls) {
        return m(iVar, eVar, (Class) a(cls));
    }

    public static p l(i iVar, Class cls) {
        return k(iVar, null, cls);
    }

    private static p m(i iVar, e eVar, Class cls) {
        s.d(iVar.f());
        p f10 = p.f(cls);
        for (c0.c cVar : iVar.f().M()) {
            if (cVar.N() == z.ENABLED) {
                p.b a10 = f10.a((eVar == null || !eVar.a(cVar.K().L())) ? j(cVar.K().L(), cVar.K().M(), cls) : eVar.d(cVar.K().M()), cVar);
                if (cVar.L() == iVar.f().N()) {
                    f10.g(a10);
                }
            }
        }
        return f10;
    }

    public static e n(String str) {
        return f(str).b();
    }

    public static synchronized o0 o(a0 a0Var) {
        o0 b10;
        synchronized (r.class) {
            e n10 = n(a0Var.L());
            if (!((Boolean) f18542d.get(a0Var.L())).booleanValue()) {
                throw new GeneralSecurityException("newKey-operation not permitted for key type " + a0Var.L());
            }
            b10 = n10.b(a0Var.M());
        }
        return b10;
    }

    public static synchronized y p(a0 a0Var) {
        y c10;
        synchronized (r.class) {
            e n10 = n(a0Var.L());
            if (!((Boolean) f18542d.get(a0Var.L())).booleanValue()) {
                throw new GeneralSecurityException("newKey-operation not permitted for key type " + a0Var.L());
            }
            c10 = n10.c(a0Var.M());
        }
        return c10;
    }

    public static synchronized void q(h hVar, boolean z10) {
        synchronized (r.class) {
            if (hVar == null) {
                throw new IllegalArgumentException("key manager must be non-null.");
            }
            String c10 = hVar.c();
            d(c10, hVar.getClass(), z10);
            ConcurrentMap concurrentMap = f18540b;
            if (!concurrentMap.containsKey(c10)) {
                concurrentMap.put(c10, b(hVar));
                f18541c.put(c10, c(hVar));
            }
            f18542d.put(c10, Boolean.valueOf(z10));
        }
    }

    public static synchronized void r(q qVar) {
        synchronized (r.class) {
            if (qVar == null) {
                throw new IllegalArgumentException("wrapper must be non-null");
            }
            Class b10 = qVar.b();
            ConcurrentMap concurrentMap = f18544f;
            if (concurrentMap.containsKey(b10)) {
                q qVar2 = (q) concurrentMap.get(b10);
                if (!qVar.getClass().equals(qVar2.getClass())) {
                    f18539a.warning("Attempted overwrite of a registered SetWrapper for type " + b10);
                    throw new GeneralSecurityException(String.format("SetWrapper for primitive (%s) is already registered to be %s, cannot be re-registered with %s", b10.getName(), qVar2.getClass().getName(), qVar.getClass().getName()));
                }
            }
            concurrentMap.put(b10, qVar);
        }
    }

    private static String s(Set set) {
        StringBuilder sb2 = new StringBuilder();
        Iterator it = set.iterator();
        boolean z10 = true;
        while (it.hasNext()) {
            Class cls = (Class) it.next();
            if (!z10) {
                sb2.append(", ");
            }
            sb2.append(cls.getCanonicalName());
            z10 = false;
        }
        return sb2.toString();
    }

    public static Object t(p pVar, Class cls) {
        q qVar = (q) f18544f.get(cls);
        if (qVar == null) {
            throw new GeneralSecurityException("No wrapper found for " + pVar.d().getName());
        }
        if (qVar.a().equals(pVar.d())) {
            return qVar.c(pVar);
        }
        throw new GeneralSecurityException("Wrong input primitive class, expected " + qVar.a() + ", got " + pVar.d());
    }
}
