package io.netty.handler.ssl;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.base64.Base64;
import io.netty.handler.codec.base64.Base64Dialect;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import javax.net.ssl.SSLHandshakeException;

/* JADX INFO: loaded from: classes.dex */
public final class SslUtils {
    public static final String[] DEFAULT_CIPHER_SUITES = {"TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", "TLS_RSA_WITH_AES_128_GCM_SHA256", "TLS_RSA_WITH_AES_128_CBC_SHA", "TLS_RSA_WITH_AES_256_CBC_SHA"};
    public static final int NOT_ENCRYPTED = -2;
    public static final int NOT_ENOUGH_DATA = -1;
    public static final String PROTOCOL_SSL_V2 = "SSLv2";
    public static final String PROTOCOL_SSL_V2_HELLO = "SSLv2Hello";
    public static final String PROTOCOL_SSL_V3 = "SSLv3";
    public static final String PROTOCOL_TLS_V1 = "TLSv1";
    public static final String PROTOCOL_TLS_V1_1 = "TLSv1.1";
    public static final String PROTOCOL_TLS_V1_2 = "TLSv1.2";
    public static final int SSL_CONTENT_TYPE_ALERT = 21;
    public static final int SSL_CONTENT_TYPE_APPLICATION_DATA = 23;
    public static final int SSL_CONTENT_TYPE_CHANGE_CIPHER_SPEC = 20;
    public static final int SSL_CONTENT_TYPE_EXTENSION_HEARTBEAT = 24;
    public static final int SSL_CONTENT_TYPE_HANDSHAKE = 22;
    public static final int SSL_RECORD_HEADER_LENGTH = 5;

    private SslUtils() {
    }

    public static void addIfSupported(Set<String> set, List<String> list, String... strArr) {
        for (String str : strArr) {
            if (set.contains(str)) {
                list.add(str);
            }
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:17:0x0028  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public static int getEncryptedPacketLength(io.netty.buffer.ByteBuf r6, int r7) {
        /*
            short r0 = r6.getUnsignedByte(r7)
            r1 = 1
            r2 = 0
            switch(r0) {
                case 20: goto Lb;
                case 21: goto Lb;
                case 22: goto Lb;
                case 23: goto Lb;
                case 24: goto Lb;
                default: goto L9;
            }
        L9:
            r0 = r2
            goto Lc
        Lb:
            r0 = r1
        Lc:
            r3 = 3
            if (r0 == 0) goto L24
            int r4 = r7 + 1
            short r4 = r6.getUnsignedByte(r4)
            if (r4 != r3) goto L22
            int r4 = r7 + 3
            int r4 = unsignedShortBE(r6, r4)
            r5 = 5
            int r4 = r4 + r5
            if (r4 > r5) goto L25
            goto L26
        L22:
            r4 = r2
            goto L26
        L24:
            r4 = r2
        L25:
            r2 = r0
        L26:
            if (r2 != 0) goto L54
            short r0 = r6.getUnsignedByte(r7)
            r0 = r0 & 128(0x80, float:1.8E-43)
            r2 = 2
            if (r0 == 0) goto L33
            r0 = r2
            goto L34
        L33:
            r0 = r3
        L34:
            int r4 = r7 + r0
            int r4 = r4 + r1
            short r1 = r6.getUnsignedByte(r4)
            if (r1 == r2) goto L42
            if (r1 != r3) goto L40
            goto L42
        L40:
            r6 = -2
            return r6
        L42:
            short r6 = shortBE(r6, r7)
            if (r0 != r2) goto L4c
            r6 = r6 & 32767(0x7fff, float:4.5916E-41)
            int r6 = r6 + r2
            goto L4f
        L4c:
            r6 = r6 & 16383(0x3fff, float:2.2957E-41)
            int r6 = r6 + r3
        L4f:
            r4 = r6
            if (r4 > r0) goto L54
            r6 = -1
            return r6
        L54:
            return r4
        */
        throw new UnsupportedOperationException("Method not decompiled: io.netty.handler.ssl.SslUtils.getEncryptedPacketLength(io.netty.buffer.ByteBuf, int):int");
    }

    /* JADX WARN: Removed duplicated region for block: B:17:0x0034  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    private static int getEncryptedPacketLength(java.nio.ByteBuffer r7) {
        /*
            int r0 = r7.position()
            byte r1 = r7.get(r0)
            short r1 = unsignedByte(r1)
            r2 = 1
            r3 = 0
            switch(r1) {
                case 20: goto L13;
                case 21: goto L13;
                case 22: goto L13;
                case 23: goto L13;
                case 24: goto L13;
                default: goto L11;
            }
        L11:
            r1 = r3
            goto L14
        L13:
            r1 = r2
        L14:
            r4 = 3
            if (r1 == 0) goto L30
            int r5 = r0 + 1
            byte r5 = r7.get(r5)
            short r5 = unsignedByte(r5)
            if (r5 != r4) goto L2e
            int r5 = r0 + 3
            int r5 = unsignedShortBE(r7, r5)
            r6 = 5
            int r5 = r5 + r6
            if (r5 > r6) goto L31
            goto L32
        L2e:
            r5 = r3
            goto L32
        L30:
            r5 = r3
        L31:
            r3 = r1
        L32:
            if (r3 != 0) goto L68
            byte r1 = r7.get(r0)
            short r1 = unsignedByte(r1)
            r1 = r1 & 128(0x80, float:1.8E-43)
            r3 = 2
            if (r1 == 0) goto L43
            r1 = r3
            goto L44
        L43:
            r1 = r4
        L44:
            int r5 = r0 + r1
            int r5 = r5 + r2
            byte r2 = r7.get(r5)
            short r2 = unsignedByte(r2)
            if (r2 == r3) goto L56
            if (r2 != r4) goto L54
            goto L56
        L54:
            r7 = -2
            return r7
        L56:
            short r7 = shortBE(r7, r0)
            if (r1 != r3) goto L60
            r7 = r7 & 32767(0x7fff, float:4.5916E-41)
            int r7 = r7 + r3
            goto L63
        L60:
            r7 = r7 & 16383(0x3fff, float:2.2957E-41)
            int r7 = r7 + r4
        L63:
            r5 = r7
            if (r5 > r1) goto L68
            r7 = -1
            return r7
        L68:
            return r5
        */
        throw new UnsupportedOperationException("Method not decompiled: io.netty.handler.ssl.SslUtils.getEncryptedPacketLength(java.nio.ByteBuffer):int");
    }

    public static int getEncryptedPacketLength(ByteBuffer[] byteBufferArr, int i2) {
        ByteBuffer byteBuffer = byteBufferArr[i2];
        if (byteBuffer.remaining() >= 5) {
            return getEncryptedPacketLength(byteBuffer);
        }
        ByteBuffer byteBufferAllocate = ByteBuffer.allocate(5);
        while (true) {
            int i3 = i2 + 1;
            ByteBuffer byteBufferDuplicate = byteBufferArr[i2].duplicate();
            if (byteBufferDuplicate.remaining() > byteBufferAllocate.remaining()) {
                byteBufferDuplicate.limit(byteBufferAllocate.remaining() + byteBufferDuplicate.position());
            }
            byteBufferAllocate.put(byteBufferDuplicate);
            if (!byteBufferAllocate.hasRemaining()) {
                byteBufferAllocate.flip();
                return getEncryptedPacketLength(byteBufferAllocate);
            }
            i2 = i3;
        }
    }

    public static void handleHandshakeFailure(ChannelHandlerContext channelHandlerContext, Throwable th, boolean z2) {
        channelHandlerContext.flush();
        if (z2) {
            channelHandlerContext.fireUserEventTriggered((Object) new SslHandshakeCompletionEvent(th));
        }
        channelHandlerContext.close();
    }

    private static short shortBE(ByteBuf byteBuf, int i2) {
        return byteBuf.order() == ByteOrder.BIG_ENDIAN ? byteBuf.getShort(i2) : byteBuf.getShortLE(i2);
    }

    private static short shortBE(ByteBuffer byteBuffer, int i2) {
        ByteOrder byteOrderOrder = byteBuffer.order();
        ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;
        short s2 = byteBuffer.getShort(i2);
        return byteOrderOrder == byteOrder ? s2 : ByteBufUtil.swapShort(s2);
    }

    public static ByteBuf toBase64(ByteBufAllocator byteBufAllocator, ByteBuf byteBuf) {
        ByteBuf byteBufEncode = Base64.encode(byteBuf, byteBuf.readerIndex(), byteBuf.readableBytes(), true, Base64Dialect.STANDARD, byteBufAllocator);
        byteBuf.readerIndex(byteBuf.writerIndex());
        return byteBufEncode;
    }

    public static SSLHandshakeException toSSLHandshakeException(Throwable th) {
        return th instanceof SSLHandshakeException ? (SSLHandshakeException) th : (SSLHandshakeException) new SSLHandshakeException(th.getMessage()).initCause(th);
    }

    private static short unsignedByte(byte b2) {
        return (short) (b2 & 255);
    }

    private static int unsignedShortBE(ByteBuf byteBuf, int i2) {
        return byteBuf.order() == ByteOrder.BIG_ENDIAN ? byteBuf.getUnsignedShort(i2) : byteBuf.getUnsignedShortLE(i2);
    }

    private static int unsignedShortBE(ByteBuffer byteBuffer, int i2) {
        return shortBE(byteBuffer, i2) & 65535;
    }

    public static void useFallbackCiphersIfDefaultIsEmpty(List<String> list, Iterable<String> iterable) {
        if (list.isEmpty()) {
            for (String str : iterable) {
                if (!str.startsWith("SSL_") && !str.contains("_RC4_")) {
                    list.add(str);
                }
            }
        }
    }

    public static void useFallbackCiphersIfDefaultIsEmpty(List<String> list, String... strArr) {
        useFallbackCiphersIfDefaultIsEmpty(list, Arrays.asList(strArr));
    }

    public static void zeroout(ByteBuf byteBuf) {
        if (byteBuf.isReadOnly()) {
            return;
        }
        byteBuf.setZero(0, byteBuf.capacity());
    }

    public static void zerooutAndRelease(ByteBuf byteBuf) {
        zeroout(byteBuf);
        byteBuf.release();
    }
}
