alexandroid/decompiled/jadx/sources/org/java_websocket/SSLSocketChannel.java
KaKi87 a47fad5135 📦 Add decompiled Alexa 2.2.730155.0 (jadx, apktool, Hermes strings)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-07 00:04:16 +01:00

379 lines
17 KiB
Java

package org.java_websocket;
import ai.onnxruntime.OnnxMap$$ExternalSyntheticBUOutline0;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ByteChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.util.concurrent.ExecutorService;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLEngineResult;
import javax.net.ssl.SSLException;
import org.java_websocket.interfaces.ISSLChannel;
import org.java_websocket.util.ByteBufferUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rx.Single$$ExternalSyntheticBUOutline0;
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
/* loaded from: classes11.dex */
public class SSLSocketChannel implements WrappedByteChannel, ByteChannel, ISSLChannel {
private final SSLEngine engine;
private ExecutorService executor;
private final Logger log = LoggerFactory.getLogger((Class<?>) SSLSocketChannel.class);
private ByteBuffer myAppData;
private ByteBuffer myNetData;
private ByteBuffer peerAppData;
private ByteBuffer peerNetData;
private final SocketChannel socketChannel;
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
/* renamed from: org.java_websocket.SSLSocketChannel$1, reason: invalid class name */
public static /* synthetic */ class AnonymousClass1 {
static final /* synthetic */ int[] $SwitchMap$javax$net$ssl$SSLEngineResult$HandshakeStatus;
static final /* synthetic */ int[] $SwitchMap$javax$net$ssl$SSLEngineResult$Status;
static {
int[] iArr = new int[SSLEngineResult.HandshakeStatus.values().length];
$SwitchMap$javax$net$ssl$SSLEngineResult$HandshakeStatus = iArr;
try {
iArr[SSLEngineResult.HandshakeStatus.FINISHED.ordinal()] = 1;
} catch (NoSuchFieldError unused) {
}
try {
$SwitchMap$javax$net$ssl$SSLEngineResult$HandshakeStatus[SSLEngineResult.HandshakeStatus.NEED_UNWRAP.ordinal()] = 2;
} catch (NoSuchFieldError unused2) {
}
try {
$SwitchMap$javax$net$ssl$SSLEngineResult$HandshakeStatus[SSLEngineResult.HandshakeStatus.NEED_WRAP.ordinal()] = 3;
} catch (NoSuchFieldError unused3) {
}
try {
$SwitchMap$javax$net$ssl$SSLEngineResult$HandshakeStatus[SSLEngineResult.HandshakeStatus.NEED_TASK.ordinal()] = 4;
} catch (NoSuchFieldError unused4) {
}
try {
$SwitchMap$javax$net$ssl$SSLEngineResult$HandshakeStatus[SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING.ordinal()] = 5;
} catch (NoSuchFieldError unused5) {
}
int[] iArr2 = new int[SSLEngineResult.Status.values().length];
$SwitchMap$javax$net$ssl$SSLEngineResult$Status = iArr2;
try {
iArr2[SSLEngineResult.Status.OK.ordinal()] = 1;
} catch (NoSuchFieldError unused6) {
}
try {
$SwitchMap$javax$net$ssl$SSLEngineResult$Status[SSLEngineResult.Status.BUFFER_UNDERFLOW.ordinal()] = 2;
} catch (NoSuchFieldError unused7) {
}
try {
$SwitchMap$javax$net$ssl$SSLEngineResult$Status[SSLEngineResult.Status.BUFFER_OVERFLOW.ordinal()] = 3;
} catch (NoSuchFieldError unused8) {
}
try {
$SwitchMap$javax$net$ssl$SSLEngineResult$Status[SSLEngineResult.Status.CLOSED.ordinal()] = 4;
} catch (NoSuchFieldError unused9) {
}
}
}
public SSLSocketChannel(SocketChannel socketChannel, SSLEngine sSLEngine, ExecutorService executorService, SelectionKey selectionKey) throws IOException {
if (socketChannel == null || sSLEngine == null || this.executor == executorService) {
Single$$ExternalSyntheticBUOutline0.m$3("parameter must not be null");
throw null;
}
this.socketChannel = socketChannel;
this.engine = sSLEngine;
this.executor = executorService;
this.myNetData = ByteBuffer.allocate(sSLEngine.getSession().getPacketBufferSize());
this.peerNetData = ByteBuffer.allocate(sSLEngine.getSession().getPacketBufferSize());
sSLEngine.beginHandshake();
if (doHandshake()) {
if (selectionKey != null) {
selectionKey.interestOps(selectionKey.interestOps() | 4);
}
} else {
try {
socketChannel.close();
} catch (IOException e) {
this.log.error("Exception during the closing of the channel", (Throwable) e);
}
}
}
private void closeConnection() throws IOException {
this.engine.closeOutbound();
try {
doHandshake();
} catch (IOException unused) {
}
this.socketChannel.close();
}
private boolean doHandshake() throws IOException {
SSLEngineResult.HandshakeStatus handshakeStatus;
int applicationBufferSize = this.engine.getSession().getApplicationBufferSize();
this.myAppData = ByteBuffer.allocate(applicationBufferSize);
this.peerAppData = ByteBuffer.allocate(applicationBufferSize);
this.myNetData.clear();
this.peerNetData.clear();
SSLEngineResult.HandshakeStatus handshakeStatus2 = this.engine.getHandshakeStatus();
boolean z = false;
while (!z) {
int i = AnonymousClass1.$SwitchMap$javax$net$ssl$SSLEngineResult$HandshakeStatus[handshakeStatus2.ordinal()];
if (i != 1) {
if (i != 2) {
if (i == 3) {
this.myNetData.clear();
try {
SSLEngineResult wrap = this.engine.wrap(this.myAppData, this.myNetData);
handshakeStatus = wrap.getHandshakeStatus();
int i2 = AnonymousClass1.$SwitchMap$javax$net$ssl$SSLEngineResult$Status[wrap.getStatus().ordinal()];
if (i2 == 1) {
this.myNetData.flip();
while (this.myNetData.hasRemaining()) {
this.socketChannel.write(this.myNetData);
}
} else {
if (i2 == 2) {
throw new SSLException("Buffer underflow occurred after a wrap. I don't think we should ever get here.");
}
if (i2 == 3) {
this.myNetData = enlargePacketBuffer(this.myNetData);
} else {
if (i2 != 4) {
Single$$ExternalSyntheticBUOutline0.m(wrap.getStatus(), "Invalid SSL status: ");
return false;
}
try {
this.myNetData.flip();
while (this.myNetData.hasRemaining()) {
this.socketChannel.write(this.myNetData);
}
this.peerNetData.clear();
} catch (Exception unused) {
handshakeStatus2 = this.engine.getHandshakeStatus();
}
}
}
} catch (SSLException unused2) {
this.engine.closeOutbound();
handshakeStatus2 = this.engine.getHandshakeStatus();
}
} else if (i == 4) {
while (true) {
Runnable delegatedTask = this.engine.getDelegatedTask();
if (delegatedTask == null) {
break;
}
this.executor.execute(delegatedTask);
}
handshakeStatus2 = this.engine.getHandshakeStatus();
} else if (i != 5) {
OnnxMap$$ExternalSyntheticBUOutline0.m(handshakeStatus2, "Invalid SSL status: ");
return false;
}
} else if (this.socketChannel.read(this.peerNetData) >= 0) {
this.peerNetData.flip();
try {
SSLEngineResult unwrap = this.engine.unwrap(this.peerNetData, this.peerAppData);
this.peerNetData.compact();
handshakeStatus = unwrap.getHandshakeStatus();
int i3 = AnonymousClass1.$SwitchMap$javax$net$ssl$SSLEngineResult$Status[unwrap.getStatus().ordinal()];
if (i3 != 1) {
if (i3 == 2) {
this.peerNetData = handleBufferUnderflow(this.peerNetData);
} else if (i3 == 3) {
this.peerAppData = enlargeApplicationBuffer(this.peerAppData);
} else {
if (i3 != 4) {
Single$$ExternalSyntheticBUOutline0.m(unwrap.getStatus(), "Invalid SSL status: ");
return false;
}
if (this.engine.isOutboundDone()) {
return false;
}
this.engine.closeOutbound();
handshakeStatus2 = this.engine.getHandshakeStatus();
}
}
} catch (SSLException unused3) {
this.engine.closeOutbound();
handshakeStatus2 = this.engine.getHandshakeStatus();
}
} else {
if (this.engine.isInboundDone() && this.engine.isOutboundDone()) {
return false;
}
try {
this.engine.closeInbound();
} catch (SSLException unused4) {
}
this.engine.closeOutbound();
handshakeStatus2 = this.engine.getHandshakeStatus();
}
handshakeStatus2 = handshakeStatus;
} else {
boolean hasRemaining = this.peerNetData.hasRemaining();
boolean z2 = !hasRemaining;
if (!hasRemaining) {
return true;
}
this.socketChannel.write(this.peerNetData);
z = z2;
}
}
return true;
}
private ByteBuffer enlargeApplicationBuffer(ByteBuffer byteBuffer) {
return enlargeBuffer(byteBuffer, this.engine.getSession().getApplicationBufferSize());
}
private ByteBuffer enlargeBuffer(ByteBuffer byteBuffer, int i) {
return i > byteBuffer.capacity() ? ByteBuffer.allocate(i) : ByteBuffer.allocate(byteBuffer.capacity() * 2);
}
private ByteBuffer enlargePacketBuffer(ByteBuffer byteBuffer) {
return enlargeBuffer(byteBuffer, this.engine.getSession().getPacketBufferSize());
}
private ByteBuffer handleBufferUnderflow(ByteBuffer byteBuffer) {
if (this.engine.getSession().getPacketBufferSize() < byteBuffer.limit()) {
return byteBuffer;
}
ByteBuffer enlargePacketBuffer = enlargePacketBuffer(byteBuffer);
byteBuffer.flip();
enlargePacketBuffer.put(byteBuffer);
return enlargePacketBuffer;
}
private void handleEndOfStream() throws IOException {
try {
this.engine.closeInbound();
} catch (Exception unused) {
this.log.error("This engine was forced to close inbound, without having received the proper SSL/TLS close notification message from the peer, due to end of stream.");
}
closeConnection();
}
@Override // java.nio.channels.Channel, java.io.Closeable, java.lang.AutoCloseable
public void close() throws IOException {
closeConnection();
}
@Override // org.java_websocket.interfaces.ISSLChannel
public SSLEngine getSSLEngine() {
return this.engine;
}
@Override // org.java_websocket.WrappedByteChannel
public boolean isBlocking() {
return this.socketChannel.isBlocking();
}
@Override // org.java_websocket.WrappedByteChannel
public boolean isNeedRead() {
return this.peerNetData.hasRemaining() || this.peerAppData.hasRemaining();
}
@Override // org.java_websocket.WrappedByteChannel
public boolean isNeedWrite() {
return false;
}
@Override // java.nio.channels.Channel
public boolean isOpen() {
return this.socketChannel.isOpen();
}
@Override // java.nio.channels.ReadableByteChannel
public synchronized int read(ByteBuffer byteBuffer) throws IOException {
if (!byteBuffer.hasRemaining()) {
return 0;
}
if (this.peerAppData.hasRemaining()) {
this.peerAppData.flip();
return ByteBufferUtils.transferByteBuffer(this.peerAppData, byteBuffer);
}
this.peerNetData.compact();
int read = this.socketChannel.read(this.peerNetData);
if (read <= 0 && !this.peerNetData.hasRemaining()) {
if (read < 0) {
handleEndOfStream();
}
ByteBufferUtils.transferByteBuffer(this.peerAppData, byteBuffer);
return read;
}
this.peerNetData.flip();
if (this.peerNetData.hasRemaining()) {
this.peerAppData.compact();
try {
SSLEngineResult unwrap = this.engine.unwrap(this.peerNetData, this.peerAppData);
int i = AnonymousClass1.$SwitchMap$javax$net$ssl$SSLEngineResult$Status[unwrap.getStatus().ordinal()];
if (i == 1) {
this.peerAppData.flip();
return ByteBufferUtils.transferByteBuffer(this.peerAppData, byteBuffer);
}
if (i == 2) {
this.peerAppData.flip();
return ByteBufferUtils.transferByteBuffer(this.peerAppData, byteBuffer);
}
if (i == 3) {
this.peerAppData = enlargeApplicationBuffer(this.peerAppData);
return read(byteBuffer);
}
if (i == 4) {
closeConnection();
byteBuffer.clear();
return -1;
}
throw new IllegalStateException("Invalid SSL status: " + unwrap.getStatus());
} catch (SSLException e) {
this.log.error("SSLException during unwrap", (Throwable) e);
throw e;
}
}
ByteBufferUtils.transferByteBuffer(this.peerAppData, byteBuffer);
return read;
}
@Override // org.java_websocket.WrappedByteChannel
public int readMore(ByteBuffer byteBuffer) throws IOException {
return read(byteBuffer);
}
@Override // java.nio.channels.WritableByteChannel
public synchronized int write(ByteBuffer byteBuffer) throws IOException {
int i = 0;
while (byteBuffer.hasRemaining()) {
this.myNetData.clear();
SSLEngineResult wrap = this.engine.wrap(byteBuffer, this.myNetData);
int i2 = AnonymousClass1.$SwitchMap$javax$net$ssl$SSLEngineResult$Status[wrap.getStatus().ordinal()];
if (i2 == 1) {
this.myNetData.flip();
while (this.myNetData.hasRemaining()) {
i += this.socketChannel.write(this.myNetData);
}
} else {
if (i2 == 2) {
throw new SSLException("Buffer underflow occurred after a wrap. I don't think we should ever get here.");
}
if (i2 != 3) {
if (i2 == 4) {
closeConnection();
return 0;
}
throw new IllegalStateException("Invalid SSL status: " + wrap.getStatus());
}
this.myNetData = enlargePacketBuffer(this.myNetData);
}
}
return i;
}
@Override // org.java_websocket.WrappedByteChannel
public void writeMore() throws IOException {
}
}