package R3;

import L3.f;
import L3.g;
import X3.M;
import X3.N;
import X3.Z;
import X3.d0;
import X3.f0;
import X3.g0;
import X3.k0;
import Y3.j;
import Z0.o;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
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 com.google.crypto.tink.shaded.protobuf.C1267o;
import java.io.ByteArrayInputStream;
import java.io.CharConversionException;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyStoreException;
import java.security.ProviderException;
import java.util.concurrent.Executor;
import s3.C2914f;
import t3.InterfaceC2949b;
import v4.InterfaceC3128a;
import x3.C3218c;
import x3.C3222g;
import y3.InterfaceC3266c;
import y3.InterfaceC3267d;

/* JADX INFO: loaded from: classes3.dex */
public final class a implements InterfaceC2949b {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public Object f6072a = null;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public Object f6073b = null;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public Object f6074c = null;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public Object f6075d = null;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public Object f6076e = null;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public Object f6077f = null;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public Object f6078j;

    public static byte[] c(Context context, String str, String str2) throws CharConversionException {
        if (str == null) {
            throw new IllegalArgumentException("keysetName cannot be null");
        }
        Context applicationContext = context.getApplicationContext();
        try {
            String string = (str2 == null ? PreferenceManager.getDefaultSharedPreferences(applicationContext) : applicationContext.getSharedPreferences(str2, 0)).getString(str, null);
            if (string == null) {
                return null;
            }
            return j.e(string);
        } catch (ClassCastException | IllegalArgumentException unused) {
            throw new CharConversionException(o.o("can't read keyset; the pref value ", str, " is not a valid hex string"));
        }
    }

    public static f d(byte[] bArr) throws IOException {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bArr);
        try {
            g0 g0VarC = g0.C(byteArrayInputStream, C1267o.a());
            byteArrayInputStream.close();
            return new f((d0) ((g0) C4.f.s(g0VarC).f741b).u(), 3);
        } catch (Throwable th) {
            byteArrayInputStream.close();
            throw th;
        }
    }

    public synchronized b a() {
        b bVar;
        try {
            if (((String) this.f6073b) == null) {
                throw new IllegalArgumentException("keysetName cannot be null");
            }
            synchronized (b.f6079b) {
                try {
                    byte[] bArrC = c((Context) this.f6072a, (String) this.f6073b, (String) this.f6074c);
                    if (bArrC == null) {
                        if (((String) this.f6075d) != null) {
                            this.f6076e = f();
                        }
                        this.f6078j = b();
                    } else if (((String) this.f6075d) != null) {
                        this.f6078j = e(bArrC);
                    } else {
                        this.f6078j = d(bArrC);
                    }
                    bVar = new b(this);
                } finally {
                }
            }
        } catch (Throwable th) {
            throw th;
        }
        return bVar;
    }

    public f b() throws GeneralSecurityException, IOException {
        if (((g) this.f6077f) == null) {
            throw new GeneralSecurityException("cannot read or generate keyset");
        }
        f fVar = new f(g0.B(), 3);
        g gVar = (g) this.f6077f;
        synchronized (fVar) {
            fVar.a(gVar.f4174a);
        }
        int iZ = L3.o.a((g0) fVar.c().f741b).x().z();
        synchronized (fVar) {
            for (int i6 = 0; i6 < ((g0) ((d0) fVar.f4173b).f13838b).y(); i6++) {
                try {
                    f0 f0VarX = ((g0) ((d0) fVar.f4173b).f13838b).x(i6);
                    if (f0VarX.A() == iZ) {
                        if (!f0VarX.C().equals(Z.ENABLED)) {
                            throw new GeneralSecurityException("cannot set key as primary because it's not enabled: " + iZ);
                        }
                        d0 d0Var = (d0) fVar.f4173b;
                        d0Var.d();
                        g0.v((g0) d0Var.f13838b, iZ);
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
            throw new GeneralSecurityException("key not found: " + iZ);
        }
        Context context = (Context) this.f6072a;
        String str = (String) this.f6073b;
        String str2 = (String) this.f6074c;
        if (str == null) {
            throw new IllegalArgumentException("keysetName cannot be null");
        }
        Context applicationContext = context.getApplicationContext();
        SharedPreferences.Editor editorEdit = str2 == null ? PreferenceManager.getDefaultSharedPreferences(applicationContext).edit() : applicationContext.getSharedPreferences(str2, 0).edit();
        if (((c) this.f6076e) != null) {
            C4.f fVarC = fVar.c();
            c cVar = (c) this.f6076e;
            byte[] bArr = new byte[0];
            g0 g0Var = (g0) fVarC.f741b;
            byte[] bArrA = cVar.a(g0Var.d(), bArr);
            try {
                if (!g0.D(cVar.b(bArrA, bArr), C1267o.a()).equals(g0Var)) {
                    throw new GeneralSecurityException("cannot encrypt keyset");
                }
                M mY = N.y();
                C1260h c1260hH = AbstractC1261i.h(bArrA, 0, bArrA.length);
                mY.d();
                N.v((N) mY.f13838b, c1260hH);
                k0 k0VarA = L3.o.a(g0Var);
                mY.d();
                N.w((N) mY.f13838b, k0VarA);
                if (!editorEdit.putString(str, j.f(((N) mY.a()).d())).commit()) {
                    throw new IOException("Failed to write to SharedPreferences");
                }
            } catch (C unused) {
                throw new GeneralSecurityException("invalid keyset, corrupted key material");
            }
        } else if (!editorEdit.putString(str, j.f(((g0) fVar.c().f741b).d())).commit()) {
            throw new IOException("Failed to write to SharedPreferences");
        }
        return fVar;
    }

    public f e(byte[] bArr) {
        try {
            this.f6076e = new d().c((String) this.f6075d);
            try {
                return new f((d0) ((g0) C4.f.M(new f(new ByteArrayInputStream(bArr), 1), (c) this.f6076e).f741b).u(), 3);
            } catch (IOException | GeneralSecurityException e6) {
                try {
                    return d(bArr);
                } catch (IOException unused) {
                    throw e6;
                }
            }
        } catch (GeneralSecurityException | ProviderException e7) {
            try {
                f fVarD = d(bArr);
                Log.w("b", "cannot use Android Keystore, it'll be disabled", e7);
                return fVarD;
            } catch (IOException unused2) {
                throw e7;
            }
        }
    }

    public c f() throws KeyStoreException {
        d dVar = new d();
        try {
            boolean zA = d.a((String) this.f6075d);
            try {
                return dVar.c((String) this.f6075d);
            } catch (GeneralSecurityException | ProviderException e6) {
                if (!zA) {
                    throw new KeyStoreException(o.o("the master key ", (String) this.f6075d, " exists but is unusable"), e6);
                }
                Log.w("b", "cannot use Android Keystore, it'll be disabled", e6);
                return null;
            }
        } catch (GeneralSecurityException | ProviderException e7) {
            Log.w("b", "cannot use Android Keystore, it'll be disabled", e7);
            return null;
        }
    }

    @Override // v4.InterfaceC3128a
    public Object get() {
        return new C3222g((Context) ((M0.a) this.f6072a).f4197a, (C2914f) ((InterfaceC3128a) this.f6073b).get(), (InterfaceC3267d) ((InterfaceC3128a) this.f6074c).get(), (C3218c) ((o4.f) this.f6075d).get(), (Executor) ((InterfaceC3128a) this.f6076e).get(), (z3.c) ((InterfaceC3128a) this.f6077f).get(), new u4.c(3), new u4.c(2), (InterfaceC3266c) ((InterfaceC3128a) this.f6078j).get());
    }
}
