package com.android.org.conscrypt;

import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/* JADX INFO: loaded from: classes.dex */
public final class CertBlocklistImpl implements CertBlocklist {
    private final Set<ByteString> pubkeyBlocklist;
    private final Set<BigInteger> serialBlocklist;
    private static final Logger logger = Logger.getLogger(CertBlocklistImpl.class.getName());
    private static final byte[] HEX_TABLE = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102};

    private static class ByteString {
        final byte[] bytes;

        public ByteString(byte[] bArr) {
            this.bytes = bArr;
        }

        public boolean equals(Object obj) {
            if (obj == this) {
                return true;
            }
            if (obj instanceof ByteString) {
                return Arrays.equals(this.bytes, ((ByteString) obj).bytes);
            }
            return false;
        }

        public int hashCode() {
            return Arrays.hashCode(this.bytes);
        }
    }

    public CertBlocklistImpl(Set<BigInteger> set, Set<ByteString> set2) {
        this.serialBlocklist = set;
        this.pubkeyBlocklist = set2;
    }

    private static void closeQuietly(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (RuntimeException e3) {
                throw e3;
            } catch (Exception unused) {
            }
        }
    }

    public static CertBlocklist getDefault() {
        String str = System.getenv("ANDROID_DATA") + "/misc/keychain/";
        String str2 = str + "pubkey_blacklist.txt";
        return new CertBlocklistImpl(readSerialBlockList(str + "serial_blacklist.txt"), readPublicKeyBlockList(str2));
    }

    private static boolean isHex(String str) {
        try {
            new BigInteger(str, 16);
            return true;
        } catch (NumberFormatException e3) {
            logger.log(Level.WARNING, "Could not parse hex value " + str, (Throwable) e3);
            return false;
        }
    }

    private static boolean isPubkeyHash(String str) {
        if (str.length() == 40) {
            return isHex(str);
        }
        logger.log(Level.WARNING, "Invalid pubkey hash length: " + str.length());
        return false;
    }

    private static String readBlocklist(String str) {
        try {
            return readFileAsString(str);
        } catch (FileNotFoundException unused) {
            return "";
        } catch (IOException e3) {
            logger.log(Level.WARNING, "Could not read blocklist", (Throwable) e3);
            return "";
        }
    }

    private static ByteArrayOutputStream readFileAsBytes(String str) throws Throwable {
        RandomAccessFile randomAccessFile = null;
        try {
            RandomAccessFile randomAccessFile2 = new RandomAccessFile(str, "r");
            try {
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream((int) randomAccessFile2.length());
                byte[] bArr = new byte[8192];
                while (true) {
                    int i3 = randomAccessFile2.read(bArr);
                    if (i3 == -1) {
                        closeQuietly(randomAccessFile2);
                        return byteArrayOutputStream;
                    }
                    byteArrayOutputStream.write(bArr, 0, i3);
                }
            } catch (Throwable th) {
                th = th;
                randomAccessFile = randomAccessFile2;
                closeQuietly(randomAccessFile);
                throw th;
            }
        } catch (Throwable th2) {
            th = th2;
        }
    }

    private static String readFileAsString(String str) throws IOException {
        return readFileAsBytes(str).toString("UTF-8");
    }

    private static Set<ByteString> readPublicKeyBlockList(String str) {
        HashSet hashSet = new HashSet(toByteStrings("bae78e6bed65a2bf60ddedde7fd91e825865e93d".getBytes(StandardCharsets.UTF_8), "410f36363258f30b347d12ce4863e433437806a8".getBytes(StandardCharsets.UTF_8), "ba3e7bd38cd7e1e6b9cd4c219962e59d7a2f4e37".getBytes(StandardCharsets.UTF_8), "e23b8d105f87710a68d9248050ebefc627be4ca6".getBytes(StandardCharsets.UTF_8), "7b2e16bc39bcd72b456e9f055d1de615b74945db".getBytes(StandardCharsets.UTF_8), "e8f91200c65cee16e039b9f883841661635f81c5".getBytes(StandardCharsets.UTF_8), "0129bcd5b448ae8d2496d1c3e19723919088e152".getBytes(StandardCharsets.UTF_8), "5f3ab33d55007054bc5e3e5553cd8d8465d77c61".getBytes(StandardCharsets.UTF_8), "783333c9687df63377efceddd82efa9101913e8e".getBytes(StandardCharsets.UTF_8), "3ecf4bbbe46096d514bb539bb913d77aa4ef31bf".getBytes(StandardCharsets.UTF_8)));
        String blocklist = readBlocklist(str);
        if (!blocklist.equals("")) {
            for (String str2 : blocklist.split(",", -1)) {
                String strTrim = str2.trim();
                if (isPubkeyHash(strTrim)) {
                    hashSet.add(new ByteString(strTrim.getBytes(StandardCharsets.UTF_8)));
                } else {
                    logger.log(Level.WARNING, "Tried to blocklist invalid pubkey " + strTrim);
                }
            }
        }
        return hashSet;
    }

    private static Set<BigInteger> readSerialBlockList(String str) {
        HashSet hashSet = new HashSet(Arrays.asList(new BigInteger("077a59bcd53459601ca6907267a6dd1c", 16), new BigInteger("047ecbe9fca55f7bd09eae36e10cae1e", 16), new BigInteger("d8f35f4eb7872b2dab0692e315382fb0", 16), new BigInteger("b0b7133ed096f9b56fae91c874bd3ac0", 16), new BigInteger("9239d5348f40d1695a745470e1f23f43", 16), new BigInteger("e9028b9578e415dc1a710a2b88154447", 16), new BigInteger("d7558fdaf5f1105bb213282b707729a3", 16), new BigInteger("f5c86af36162f13a64f54f6dc9587c06", 16), new BigInteger("392a434f0e07df1f8aa305de34e0c229", 16), new BigInteger("3e75ced46b693021218830ae86a82a71", 16)));
        String blocklist = readBlocklist(str);
        if (!blocklist.equals("")) {
            for (String str2 : blocklist.split(",", -1)) {
                try {
                    hashSet.add(new BigInteger(str2, 16));
                } catch (NumberFormatException e3) {
                    logger.log(Level.WARNING, "Tried to blacklist invalid serial number " + str2, (Throwable) e3);
                }
            }
        }
        return Collections.unmodifiableSet(hashSet);
    }

    private static List<ByteString> toByteStrings(byte[]... bArr) {
        ArrayList arrayList = new ArrayList(bArr.length + 1);
        for (byte[] bArr2 : bArr) {
            arrayList.add(new ByteString(bArr2));
        }
        return arrayList;
    }

    private static byte[] toHex(byte[] bArr) {
        byte[] bArr2 = new byte[bArr.length * 2];
        int i3 = 0;
        for (byte b3 : bArr) {
            int i4 = b3 & 255;
            int i5 = i3 + 1;
            byte[] bArr3 = HEX_TABLE;
            bArr2[i3] = bArr3[i4 >> 4];
            i3 = i5 + 1;
            bArr2[i5] = bArr3[i4 & 15];
        }
        return bArr2;
    }

    @Override // com.android.org.conscrypt.CertBlocklist
    public boolean isPublicKeyBlockListed(PublicKey publicKey) {
        try {
            byte[] hex = toHex(MessageDigest.getInstance("SHA1").digest(publicKey.getEncoded()));
            Iterator<ByteString> it = this.pubkeyBlocklist.iterator();
            while (it.hasNext()) {
                if (Arrays.equals(it.next().bytes, hex)) {
                    return true;
                }
            }
            return false;
        } catch (GeneralSecurityException e3) {
            logger.log(Level.SEVERE, "Unable to get SHA1 MessageDigest", (Throwable) e3);
            return false;
        }
    }

    @Override // com.android.org.conscrypt.CertBlocklist
    public boolean isSerialNumberBlockListed(BigInteger bigInteger) {
        return this.serialBlocklist.contains(bigInteger);
    }
}
