package jk;

import android.content.Context;
import android.util.Log;
import com.google.crypto.tink.shaded.protobuf.a0;
import fk.g;
import fk.i;
import fk.j;
import fk.k;
import fk.l;
import java.io.FileNotFoundException;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.ProviderException;
import jk.c;

/* loaded from: classes3.dex */
public final class a {

    /* renamed from: d, reason: collision with root package name */
    private static final String f25019d = "a";

    /* renamed from: a, reason: collision with root package name */
    private final l f25020a;

    /* renamed from: b, reason: collision with root package name */
    private final fk.a f25021b;

    /* renamed from: c, reason: collision with root package name */
    private j f25022c;

    /* loaded from: classes3.dex */
    public static final class b {

        /* renamed from: a, reason: collision with root package name */
        private k f25023a = null;

        /* renamed from: b, reason: collision with root package name */
        private l f25024b = null;

        /* renamed from: c, reason: collision with root package name */
        private String f25025c = null;

        /* renamed from: d, reason: collision with root package name */
        private fk.a f25026d = null;

        /* renamed from: e, reason: collision with root package name */
        private boolean f25027e = true;

        /* renamed from: f, reason: collision with root package name */
        private g f25028f = null;

        /* renamed from: g, reason: collision with root package name */
        private KeyStore f25029g = null;

        /* renamed from: h, reason: collision with root package name */
        private j f25030h;

        private j e() {
            fk.a aVar = this.f25026d;
            if (aVar != null) {
                try {
                    return j.j(i.j(this.f25023a, aVar));
                } catch (a0 | GeneralSecurityException e10) {
                    Log.w(a.f25019d, "cannot decrypt keyset: ", e10);
                }
            }
            return j.j(fk.b.a(this.f25023a));
        }

        private j f() {
            try {
                return e();
            } catch (FileNotFoundException e10) {
                Log.w(a.f25019d, "keyset not found, will generate a new one", e10);
                if (this.f25028f == null) {
                    throw new GeneralSecurityException("cannot read or generate keyset");
                }
                j a10 = j.i().a(this.f25028f);
                j h10 = a10.h(a10.c().g().K(0).K());
                if (this.f25026d != null) {
                    h10.c().k(this.f25024b, this.f25026d);
                } else {
                    fk.b.b(h10.c(), this.f25024b);
                }
                return h10;
            }
        }

        private fk.a g() {
            if (!a.a()) {
                Log.w(a.f25019d, "Android Keystore requires at least Android M");
                return null;
            }
            c a10 = this.f25029g != null ? new c.b().b(this.f25029g).a() : new c();
            boolean e10 = a10.e(this.f25025c);
            if (!e10) {
                try {
                    c.d(this.f25025c);
                } catch (GeneralSecurityException | ProviderException e11) {
                    Log.w(a.f25019d, "cannot use Android Keystore, it'll be disabled", e11);
                    return null;
                }
            }
            try {
                return a10.b(this.f25025c);
            } catch (GeneralSecurityException | ProviderException e12) {
                if (e10) {
                    throw new KeyStoreException(String.format("the master key %s exists but is unusable", this.f25025c), e12);
                }
                Log.w(a.f25019d, "cannot use Android Keystore, it'll be disabled", e12);
                return null;
            }
        }

        public synchronized a d() {
            if (this.f25025c != null) {
                this.f25026d = g();
            }
            this.f25030h = f();
            return new a(this);
        }

        public b h(g gVar) {
            this.f25028f = gVar;
            return this;
        }

        public b i(String str) {
            if (!str.startsWith("android-keystore://")) {
                throw new IllegalArgumentException("key URI must start with android-keystore://");
            }
            if (!this.f25027e) {
                throw new IllegalArgumentException("cannot call withMasterKeyUri() after calling doNotUseKeystore()");
            }
            this.f25025c = str;
            return this;
        }

        public b j(Context context, String str, String str2) {
            if (context == null) {
                throw new IllegalArgumentException("need an Android context");
            }
            if (str == null) {
                throw new IllegalArgumentException("need a keyset name");
            }
            this.f25023a = new d(context, str, str2);
            this.f25024b = new e(context, str, str2);
            return this;
        }
    }

    static /* synthetic */ boolean a() {
        return d();
    }

    private static boolean d() {
        return true;
    }

    public synchronized i c() {
        return this.f25022c.c();
    }

    private a(b bVar) {
        this.f25020a = bVar.f25024b;
        this.f25021b = bVar.f25026d;
        this.f25022c = bVar.f25030h;
    }
}
