package io.netty.handler.ssl;

import io.netty.buffer.ByteBuf;
import io.netty.internal.tcnative.Buffer;
import io.netty.internal.tcnative.Library;
import io.netty.internal.tcnative.SSL;
import io.netty.internal.tcnative.SSLContext;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.ReferenceCounted;
import io.netty.util.internal.NativeLibraryLoader;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/* JADX INFO: loaded from: classes.dex */
public final class OpenSsl {
    public static final /* synthetic */ boolean $assertionsDisabled = false;
    public static final Set<String> AVAILABLE_CIPHER_SUITES;
    private static final Set<String> AVAILABLE_JAVA_CIPHER_SUITES;
    private static final Set<String> AVAILABLE_OPENSSL_CIPHER_SUITES;
    public static final List<String> DEFAULT_CIPHERS;
    public static final Set<String> SUPPORTED_PROTOCOLS_SET;
    private static final boolean SUPPORTS_HOSTNAME_VALIDATION;
    private static final boolean SUPPORTS_KEYMANAGER_FACTORY;
    private static final boolean SUPPORTS_OCSP;
    private static final Throwable UNAVAILABILITY_CAUSE;
    private static final boolean USE_KEYMANAGER_FACTORY;
    private static final InternalLogger logger = InternalLoggerFactory.getInstance((Class<?>) OpenSsl.class);

    /* JADX WARN: Removed duplicated region for block: B:100:0x0202  */
    /* JADX WARN: Removed duplicated region for block: B:103:0x020f  */
    /* JADX WARN: Removed duplicated region for block: B:106:0x021c  */
    /* JADX WARN: Removed duplicated region for block: B:109:0x0235  */
    /* JADX WARN: Removed duplicated region for block: B:149:? A[RETURN, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:59:0x012a A[Catch: all -> 0x0136, TryCatch #7 {all -> 0x0136, blocks: (B:57:0x0123, B:59:0x012a, B:61:0x012f), top: B:126:0x0123 }] */
    /* JADX WARN: Removed duplicated region for block: B:61:0x012f A[Catch: all -> 0x0136, TRY_LEAVE, TryCatch #7 {all -> 0x0136, blocks: (B:57:0x0123, B:59:0x012a, B:61:0x012f), top: B:126:0x0123 }] */
    /* JADX WARN: Removed duplicated region for block: B:73:0x014a A[Catch: all -> 0x0153, TryCatch #4 {all -> 0x0153, blocks: (B:71:0x0143, B:73:0x014a, B:75:0x014f, B:76:0x0152), top: B:120:0x0143 }] */
    /* JADX WARN: Removed duplicated region for block: B:75:0x014f A[Catch: all -> 0x0153, TryCatch #4 {all -> 0x0153, blocks: (B:71:0x0143, B:73:0x014a, B:75:0x014f, B:76:0x0152), top: B:120:0x0143 }] */
    /* JADX WARN: Removed duplicated region for block: B:91:0x018f A[LOOP:1: B:89:0x0189->B:91:0x018f, LOOP_END] */
    /* JADX WARN: Removed duplicated region for block: B:94:0x01eb  */
    /* JADX WARN: Removed duplicated region for block: B:97:0x01f6  */
    static {
        /*
            Method dump skipped, instruction units count: 625
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: io.netty.handler.ssl.OpenSsl.<clinit>():void");
    }

    private OpenSsl() {
    }

    @Deprecated
    public static Set<String> availableCipherSuites() {
        return availableOpenSslCipherSuites();
    }

    public static Set<String> availableJavaCipherSuites() {
        return AVAILABLE_JAVA_CIPHER_SUITES;
    }

    public static Set<String> availableOpenSslCipherSuites() {
        return AVAILABLE_OPENSSL_CIPHER_SUITES;
    }

    private static boolean doesSupportOcsp() throws Throwable {
        long jMake;
        if (version() >= 268443648) {
            try {
                jMake = SSLContext.make(16, 1);
                try {
                    SSLContext.enableOcsp(jMake, false);
                    if (jMake == -1) {
                        return true;
                    }
                    SSLContext.free(jMake);
                    return true;
                } catch (Exception unused) {
                    if (jMake != -1) {
                        SSLContext.free(jMake);
                    }
                    return false;
                } catch (Throwable th) {
                    th = th;
                    if (jMake != -1) {
                        SSLContext.free(jMake);
                    }
                    throw th;
                }
            } catch (Exception unused2) {
                jMake = -1;
            } catch (Throwable th2) {
                th = th2;
                jMake = -1;
            }
        }
        return false;
    }

    private static boolean doesSupportProtocol(int i2) {
        try {
            long jMake = SSLContext.make(i2, 2);
            if (jMake != -1) {
                SSLContext.free(jMake);
            }
            return true;
        } catch (Exception unused) {
            return false;
        }
    }

    public static void ensureAvailability() {
        Throwable th = UNAVAILABILITY_CAUSE;
        if (th != null) {
            throw ((Error) new UnsatisfiedLinkError("failed to load the required native library").initCause(th));
        }
    }

    private static boolean initializeTcNative() {
        return Library.initialize();
    }

    public static boolean isAlpnSupported() {
        return ((long) version()) >= 268443648;
    }

    public static boolean isAvailable() {
        return UNAVAILABILITY_CAUSE == null;
    }

    public static boolean isCipherSuiteAvailable(String str) {
        String openSsl = CipherSuiteConverter.toOpenSsl(str);
        if (openSsl != null) {
            str = openSsl;
        }
        return AVAILABLE_OPENSSL_CIPHER_SUITES.contains(str);
    }

    public static boolean isOcspSupported() {
        return SUPPORTS_OCSP;
    }

    private static void loadTcNative() {
        String strNormalizedOs = PlatformDependent.normalizedOs();
        String strNormalizedArch = PlatformDependent.normalizedArch();
        LinkedHashSet linkedHashSet = new LinkedHashSet(4);
        linkedHashSet.add("netty_tcnative_" + strNormalizedOs + '_' + strNormalizedArch);
        if ("linux".equalsIgnoreCase(strNormalizedOs)) {
            linkedHashSet.add("netty_tcnative_" + strNormalizedOs + '_' + strNormalizedArch + "_fedora");
        }
        linkedHashSet.add("netty_tcnative_" + strNormalizedArch);
        linkedHashSet.add("netty_tcnative");
        NativeLibraryLoader.loadFirstAvailable(SSL.class.getClassLoader(), (String[]) linkedHashSet.toArray(new String[linkedHashSet.size()]));
    }

    public static long memoryAddress(ByteBuf byteBuf) {
        return byteBuf.hasMemoryAddress() ? byteBuf.memoryAddress() : Buffer.address(byteBuf.nioBuffer());
    }

    public static void releaseIfNeeded(ReferenceCounted referenceCounted) {
        if (referenceCounted.refCnt() > 0) {
            ReferenceCountUtil.safeRelease(referenceCounted);
        }
    }

    public static boolean supportsHostnameValidation() {
        return SUPPORTS_HOSTNAME_VALIDATION;
    }

    public static boolean supportsKeyManagerFactory() {
        return SUPPORTS_KEYMANAGER_FACTORY;
    }

    public static Throwable unavailabilityCause() {
        return UNAVAILABILITY_CAUSE;
    }

    public static boolean useKeyManagerFactory() {
        return USE_KEYMANAGER_FACTORY;
    }

    public static int version() {
        if (isAvailable()) {
            return SSL.version();
        }
        return -1;
    }

    public static String versionString() {
        if (isAvailable()) {
            return SSL.versionString();
        }
        return null;
    }
}
