Skip to content

Commit

Permalink
Fixup client encryption handling
Browse files Browse the repository at this point in the history
This does away with EncryptionTranslator and its cached buffers, instead allocating what it needs and leaning on the cipher itself to produce the resulting buffer.
  • Loading branch information
Protonull committed Nov 1, 2023
1 parent 801badb commit 952d1be
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
package gjum.minecraft.mapsync.common.net.encryption;

import gjum.minecraft.mapsync.common.net.Packet;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;

import javax.crypto.Cipher;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.IvParameterSpec;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.util.List;
import javax.crypto.Cipher;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.IvParameterSpec;
import org.apache.commons.lang3.ArrayUtils;
import org.jetbrains.annotations.NotNull;

public class EncryptionDecoder extends MessageToMessageDecoder<ByteBuf> {
private final EncryptionTranslator decryptionCodec;
private final Cipher cipher;

public EncryptionDecoder(Key key) {
public EncryptionDecoder(
final @NotNull Key key
) {
try {
Cipher cipher = Cipher.getInstance("AES/CFB8/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(key.getEncoded()));
decryptionCodec = new EncryptionTranslator(cipher);
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
this.cipher = Cipher.getInstance("AES/CFB8/NoPadding");
this.cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(key.getEncoded()));
}
catch (final GeneralSecurityException thrown) {
throw new IllegalStateException(thrown);
}
}

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws ShortBufferException {
out.add(decryptionCodec.decipher(ctx, in));
protected void decode(
final ChannelHandlerContext ctx,
final ByteBuf in,
final List<Object> out
) throws ShortBufferException {
final byte[] receivedBytes = Packet.readByteArrayOfSize(in, in.readableBytes());
final byte[] decryptedBytes = this.cipher.update(receivedBytes);
if (ArrayUtils.getLength(decryptedBytes) > 0) {
out.add(Unpooled.wrappedBuffer(decryptedBytes));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
package gjum.minecraft.mapsync.common.net.encryption;

import gjum.minecraft.mapsync.common.net.Packet;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

import java.security.GeneralSecurityException;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.IvParameterSpec;
import java.security.GeneralSecurityException;
import java.security.Key;
import org.apache.commons.lang3.ArrayUtils;
import org.jetbrains.annotations.NotNull;

public class EncryptionEncoder extends MessageToByteEncoder<ByteBuf> {
private final EncryptionTranslator encryptionCodec;
private final Cipher cipher;

public EncryptionEncoder(Key key) {
public EncryptionEncoder(
final @NotNull Key key
) {
try {
Cipher cipher = Cipher.getInstance("AES/CFB8/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(key.getEncoded()));
encryptionCodec = new EncryptionTranslator(cipher);
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
this.cipher = Cipher.getInstance("AES/CFB8/NoPadding");
this.cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(key.getEncoded()));
}
}
catch (final GeneralSecurityException thrown) {
throw new IllegalStateException(thrown);
}
}

@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws ShortBufferException {
encryptionCodec.encipher(in, out);
protected void encode(
final ChannelHandlerContext ctx,
final ByteBuf in,
final ByteBuf out
) throws ShortBufferException {
final byte[] sendingBytes = Packet.readByteArrayOfSize(in, in.readableBytes());
final byte[] encryptedBytes = this.cipher.update(sendingBytes);
if (ArrayUtils.getLength(encryptedBytes) > 0) {
out.writeBytes(encryptedBytes);
}
}
}

This file was deleted.

0 comments on commit 952d1be

Please sign in to comment.