Cipher#update is copy-safe

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-09-15 03:47:48 +02:00
parent 30a668aa6a
commit 2ecd10a4ec

View File

@ -92,17 +92,13 @@ public class PlayerSocketConnection extends PlayerConnection {
// Decrypt data // Decrypt data
if (encrypted) { if (encrypted) {
final Cipher cipher = decryptCipher; final Cipher cipher = decryptCipher;
final int remainingBytes = readBuffer.readableBytes(); ByteBuffer input = readBuffer.asByteBuffer(0, readBuffer.writerOffset());
final byte[] bytes = readBuffer.readRemainingBytes();
byte[] output = new byte[cipher.getOutputSize(remainingBytes)];
try { try {
cipher.update(bytes, 0, remainingBytes, output, 0); cipher.update(input, input.duplicate());
} catch (ShortBufferException e) { } catch (ShortBufferException e) {
MinecraftServer.getExceptionManager().handleException(e); MinecraftServer.getExceptionManager().handleException(e);
return; return;
} }
readBuffer.clear();
readBuffer.writeBytes(output);
} }
// Read all packets // Read all packets
while (readBuffer.readableBytes() > 0) { while (readBuffer.readableBytes() > 0) {
@ -278,16 +274,11 @@ public class PlayerSocketConnection extends PlayerConnection {
final Cipher cipher = encryptCipher; final Cipher cipher = encryptCipher;
// Encrypt data first // Encrypt data first
ByteBuffer cipherInput = localBuffer.asByteBuffer(0, localBuffer.writerOffset()); ByteBuffer cipherInput = localBuffer.asByteBuffer(0, localBuffer.writerOffset());
BinaryBuffer pooled = PooledBuffers.get();
ByteBuffer cipherOutput = pooled.asByteBuffer(0, pooled.capacity());
try { try {
cipher.update(cipherInput, cipherOutput); cipher.update(cipherInput, cipherInput.duplicate());
} catch (ShortBufferException e) { } catch (ShortBufferException e) {
MinecraftServer.getExceptionManager().handleException(e); MinecraftServer.getExceptionManager().handleException(e);
} }
localBuffer.clear();
localBuffer.write(cipherOutput.flip());
PooledBuffers.add(pooled);
} }
this.waitingBuffers.add(localBuffer); this.waitingBuffers.add(localBuffer);