package p138.p176.p278.p279.p284.p285;

import android.content.Context;
import android.os.Build;
import android.security.keystore.KeyGenParameterSpec;
import android.util.Log;
import com.google.protobuf.InvalidProtocolBufferException;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import javax.annotation.concurrent.GuardedBy;
import javax.crypto.KeyGenerator;
import p138.p139.p143.p144.C2803;
import p138.p176.p177.p242.p246.p251.C4303;
import p138.p176.p278.p279.C5184;
import p138.p176.p278.p279.C5185;
import p138.p176.p278.p279.InterfaceC5176;
import p138.p176.p278.p279.InterfaceC5186;
import p138.p176.p278.p279.p287.C5441;
import p138.p176.p278.p279.p287.C5449;
import p138.p176.p278.p279.p290.C5554;

/* JADX INFO: renamed from: ދ.ހ.ހ.֏.ޙ.֏.֏, reason: contains not printable characters */
/* JADX INFO: loaded from: classes.dex */
public final class C5233 {

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final C5237 f15452;

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public final InterfaceC5186 f15453;

    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    public final boolean f15454;

    /* JADX INFO: renamed from: ށ, reason: contains not printable characters */
    public final InterfaceC5176 f15455;

    /* JADX INFO: renamed from: ނ, reason: contains not printable characters */
    public final C5441 f15456;

    /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
    @GuardedBy("this")
    public C5185 f15457;

    /* JADX INFO: renamed from: ދ.ހ.ހ.֏.ޙ.֏.֏$ؠ, reason: contains not printable characters */
    public static final class C5235 {

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public C5237 f15458 = null;

        /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
        public InterfaceC5186 f15459 = null;

        /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
        public String f15460 = null;

        /* JADX INFO: renamed from: ށ, reason: contains not printable characters */
        public boolean f15461 = true;

        /* JADX INFO: renamed from: ނ, reason: contains not printable characters */
        public C5441 f15462 = null;

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public C5235 m8716(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.f15458 = new C5237(context, str, str2);
            this.f15459 = new C5238(context, str, str2);
            return this;
        }
    }

    public /* synthetic */ C5233(C5235 c5235, C5234 c5234) throws GeneralSecurityException, IOException {
        C5185 c5185;
        C5237 c5237 = c5235.f15458;
        this.f15452 = c5237;
        if (c5237 == null) {
            throw new IllegalArgumentException("need to specify where to read the keyset from with Builder#withSharedPref");
        }
        InterfaceC5186 interfaceC5186 = c5235.f15459;
        this.f15453 = interfaceC5186;
        if (interfaceC5186 == null) {
            throw new IllegalArgumentException("need to specify where to write the keyset to with Builder#withSharedPref");
        }
        boolean z = c5235.f15461;
        this.f15454 = z;
        if (z && c5235.f15460 == null) {
            throw new IllegalArgumentException("need a master key URI, please set it with Builder#masterKeyUri");
        }
        if (m8715()) {
            String str = c5235.f15460;
            String strM8919 = C5554.m8919("android-keystore://", str);
            KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
            keyStore.load(null);
            if (!keyStore.containsAlias(strM8919)) {
                String strM89192 = C5554.m8919("android-keystore://", str);
                KeyGenerator keyGenerator = KeyGenerator.getInstance("AES", "AndroidKeyStore");
                keyGenerator.init(new KeyGenParameterSpec.Builder(strM89192, 3).setKeySize(256).setBlockModes("GCM").setEncryptionPaddings("NoPadding").build());
                keyGenerator.generateKey();
            }
            if (!(Build.VERSION.SDK_INT >= 23)) {
                throw new GeneralSecurityException("needs Android Keystore on Android M or newer");
            }
            try {
                this.f15455 = new C5236(C5554.m8919("android-keystore://", str));
            } catch (IOException e) {
                throw new GeneralSecurityException(e);
            }
        } else {
            this.f15455 = null;
        }
        this.f15456 = c5235.f15462;
        try {
            c5185 = m8714();
        } catch (IOException e2) {
            StringBuilder sbM4889 = C2803.m4889("cannot read keyset: ");
            sbM4889.append(e2.toString());
            Log.i("ދ.ހ.ހ.֏.ޙ.֏.֏", sbM4889.toString());
            if (this.f15456 == null) {
                throw new GeneralSecurityException("cannot obtain keyset handle");
            }
            c5185 = new C5185(C5449.f15803.mo9401());
            c5185.m8675(this.f15456);
            try {
                if (m8715()) {
                    c5185.m8672().m8669(this.f15453, this.f15455);
                } else {
                    C5184 c5184M8672 = c5185.m8672();
                    C5238 c5238 = (C5238) this.f15453;
                    c5238.f15466.putString(c5238.f15467, C4303.m7183(c5184M8672.f15417.mo9348())).apply();
                }
            } catch (IOException e3) {
                throw new GeneralSecurityException(e3);
            }
        }
        this.f15457 = c5185;
    }

    @GuardedBy("this")
    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public synchronized C5184 m8713() {
        return this.f15457.m8672();
    }

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public final C5185 m8714() throws GeneralSecurityException {
        if (m8715()) {
            try {
                return new C5185(C5184.m8666(this.f15452, this.f15455).f15417.mo9401());
            } catch (InvalidProtocolBufferException | GeneralSecurityException e) {
                StringBuilder sbM4889 = C2803.m4889("cannot decrypt keyset: ");
                sbM4889.append(e.toString());
                Log.i("ދ.ހ.ހ.֏.ޙ.֏.֏", sbM4889.toString());
            }
        }
        C5184 c5184M8667 = C5184.m8667(C5449.m8844(this.f15452.m8717()));
        if (m8715()) {
            c5184M8667.m8669(this.f15453, this.f15455);
        }
        return new C5185(c5184M8667.f15417.mo9401());
    }

    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    public final boolean m8715() {
        return this.f15454 && Build.VERSION.SDK_INT >= 23;
    }
}
