package com.google.android.gms.internal.p002firebaseauthapi; import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.util.Log; import java.io.CharConversionException; import java.io.IOException; import java.security.GeneralSecurityException; import java.security.KeyStoreException; import java.security.ProviderException; /* loaded from: classes.dex */ public final class zzjr { private Context zza = null; private String zzb = null; private String zzc = null; private String zzd = null; private zzbd zze = null; private zzbv zzf = null; private zzca zzg; private final zzbd zzj() { if (!zzjt.zzd()) { Log.w(zzjt.zzc, "Android Keystore requires at least Android M"); return null; } zzjv zzjvVar = new zzjv(); try { boolean zzc = zzjv.zzc(this.zzd); try { return zzjvVar.zza(this.zzd); } catch (GeneralSecurityException | ProviderException e10) { if (!zzc) { throw new KeyStoreException(String.format("the master key %s exists but is unusable", this.zzd), e10); } Log.w(zzjt.zzc, "cannot use Android Keystore, it'll be disabled", e10); return null; } } catch (GeneralSecurityException | ProviderException e11) { Log.w(zzjt.zzc, "cannot use Android Keystore, it'll be disabled", e11); return null; } } private final zzca zzk(byte[] bArr) { try { this.zze = new zzjv().zza(this.zzd); try { return zzca.zzf(zzbz.zzh(zzbe.zzc(bArr), this.zze)); } catch (IOException | GeneralSecurityException e10) { try { return zzl(bArr); } catch (IOException unused) { throw e10; } } } catch (GeneralSecurityException | ProviderException e11) { try { zzca zzl = zzl(bArr); Log.w(zzjt.zzc, "cannot use Android Keystore, it'll be disabled", e11); return zzl; } catch (IOException unused2) { throw e11; } } } private static final zzca zzl(byte[] bArr) { return zzca.zzf(zzbg.zzb(zzbe.zzc(bArr))); } public final zzjr zzd(zzth zzthVar) { String zzg = zzthVar.zzg(); byte[] zzq = zzthVar.zzf().zzq(); zzui zze = zzthVar.zze(); int i10 = zzjt.zza; zzui zzuiVar = zzui.UNKNOWN_PREFIX; int ordinal = zze.ordinal(); int i11 = 1; if (ordinal != 1) { i11 = 2; if (ordinal != 2) { i11 = 3; if (ordinal != 3) { i11 = 4; if (ordinal != 4) { throw new IllegalArgumentException("Unknown output prefix type"); } } } } this.zzf = zzbv.zzd(zzg, zzq, i11); return this; } public final zzjr zze(String str) { if (!str.startsWith("android-keystore://")) { throw new IllegalArgumentException("key URI must start with android-keystore://"); } this.zzd = str; return this; } public final zzjr zzf(Context context, String str, String str2) { if (context == null) { throw new IllegalArgumentException("need an Android context"); } this.zza = context; this.zzb = "GenericIdpKeyset"; this.zzc = str2; return this; } public final synchronized zzjt zzg() { byte[] bArr; zzjt zzjtVar; if (this.zzb == null) { throw new IllegalArgumentException("keysetName cannot be null"); } synchronized (zzjt.zzb) { Context context = this.zza; String str = this.zzb; String str2 = this.zzc; if (str == null) { throw new IllegalArgumentException("keysetName cannot be null"); } Context applicationContext = context.getApplicationContext(); SharedPreferences defaultSharedPreferences = str2 == null ? PreferenceManager.getDefaultSharedPreferences(applicationContext) : applicationContext.getSharedPreferences(str2, 0); zzjs zzjsVar = null; try { String string = defaultSharedPreferences.getString(str, null); if (string == null) { bArr = null; } else { if (string.length() % 2 != 0) { throw new IllegalArgumentException("Expected a string of even length"); } int length = string.length() / 2; bArr = new byte[length]; for (int i10 = 0; i10 < length; i10++) { int i11 = i10 + i10; int digit = Character.digit(string.charAt(i11), 16); int digit2 = Character.digit(string.charAt(i11 + 1), 16); if (digit == -1 || digit2 == -1) { throw new IllegalArgumentException("input is not hexadecimal"); } bArr[i10] = (byte) ((digit * 16) + digit2); } } if (bArr == null) { if (this.zzd != null) { this.zze = zzj(); } if (this.zzf == null) { throw new GeneralSecurityException("cannot read or generate keyset"); } zzca zze = zzca.zze(); zze.zzc(this.zzf); zze.zzd(zze.zzb().zzd().zzb(0).zza()); zzjw zzjwVar = new zzjw(this.zza, this.zzb, this.zzc); zzbz zzb = zze.zzb(); zzbd zzbdVar = this.zze; try { if (zzbdVar != null) { zzb.zzf(zzjwVar, zzbdVar); } else { zzbg.zza(zzb, zzjwVar); } this.zzg = zze; } catch (IOException e10) { throw new GeneralSecurityException(e10); } } else { if (this.zzd != null && zzjt.zzd()) { this.zzg = zzk(bArr); } this.zzg = zzl(bArr); } zzjtVar = new zzjt(this, zzjsVar); } catch (ClassCastException | IllegalArgumentException unused) { throw new CharConversionException(String.format("can't read keyset; the pref value %s is not a valid hex string", str)); } } return zzjtVar; } }