Skip to content

Commit b38124d

Browse files
committed
salty
1 parent 6bc0e80 commit b38124d

File tree

6 files changed

+66
-69
lines changed

6 files changed

+66
-69
lines changed

bukkit/dependency-reduced-pom.xml

-63
This file was deleted.

bukkit/src/main/java/net/outfluencer/convey/bukkit/ConveyBukkit.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import net.outfluencer.convey.common.protocol.pipe.PacketHandler;
3636
import net.outfluencer.convey.common.protocol.pipe.Varint21FrameDecoder;
3737
import net.outfluencer.convey.common.protocol.pipe.Varint21LengthFieldPrepender;
38+
import net.outfluencer.convey.common.utils.AESGCMUtils;
3839
import net.outfluencer.convey.common.utils.AESUtils;
3940
import org.bukkit.Bukkit;
4041
import org.bukkit.entity.Player;
@@ -80,7 +81,7 @@ public class ConveyBukkit extends Convey {
8081
@Getter
8182
private SecretKey secretKey;
8283
@Getter
83-
private AESUtils aesUtils;
84+
private AESGCMUtils aesUtils;
8485
private EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
8586
@Getter
8687
@Setter
@@ -136,7 +137,7 @@ public void onEnable() {
136137
return;
137138
}
138139

139-
this.aesUtils = new AESUtils(secretKey);
140+
this.aesUtils = new AESGCMUtils(secretKey);
140141
this.reloadMessages();
141142

142143
Bukkit.getPluginManager().registerEvents(new PlayerLoginListener(this), this.plugin);

bukkit/src/main/java/net/outfluencer/convey/bukkit/impl/BukkitConveyPlayer.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.ByteArrayOutputStream;
2020
import java.io.DataOutputStream;
2121
import java.util.ArrayList;
22+
import java.util.Arrays;
2223
import java.util.List;
2324
import java.util.UUID;
2425

@@ -123,7 +124,7 @@ public void sendCookie(AbstractCookie cookie) {
123124
DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
124125
cookie.write(dataOutputStream);
125126
byte[] encrypted = this.convey.getAesUtils().encrypt(byteArrayOutputStream.toByteArray());
126-
this.player.storeCookie(NamespacedKey.fromString(cookie.getCookieName()), encrypted);
127+
this.player.storeCookie(NamespacedKey.fromString(cookie.getCookieName()), Arrays.copyOf(encrypted, encrypted.length));
127128
}
128129

129130
@Override

bukkit/src/main/java/net/outfluencer/convey/bukkit/listeners/PlayerLoginListener.java

+2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ public void onLogin(PlayerLoginEvent event) {
105105
AtomicInteger amt = new AtomicInteger(verifyCookie.getClientCookies().size());
106106
List<FriendlyCookie> cookies = new CopyOnWriteArrayList<>();
107107
for (String clientCookie : verifyCookie.getClientCookies()) {
108+
System.out.println(clientCookie);
108109
player.retrieveCookie(NamespacedKey.fromString(clientCookie)).thenAccept(cookieData -> {
110+
System.out.println("GOT " + clientCookie);
109111
try {
110112
cookieData = this.convey.getAesUtils().decrypt(cookieData);
111113
InternalCookie internalCookie = new InternalCookie();

bukkit/src/main/java/net/outfluencer/convey/bukkit/utils/KickCatcher.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import net.outfluencer.convey.bukkit.ConveyBukkit;
1616
import net.outfluencer.convey.bukkit.impl.BukkitConveyPlayer;
1717
import net.outfluencer.convey.bukkit.impl.ServerImplBukkit;
18-
import net.outfluencer.convey.common.utils.AESUtils;
18+
import net.outfluencer.convey.common.utils.AESGCMUtils;
1919
import org.bukkit.entity.Player;
2020

2121
import java.io.ByteArrayOutputStream;
@@ -85,7 +85,7 @@ public class KickCatcher {
8585
@SneakyThrows
8686
public static void applyKickCatcher(BukkitConveyPlayer player) {
8787
ConveyBukkit convey = player.getConvey();
88-
AESUtils aesUtils = convey.getAesUtils();
88+
AESGCMUtils aesUtils = convey.getAesUtils();
8989
Player bukkitPlayer = player.getPlayer();
9090
UUID uuid = bukkitPlayer.getUniqueId();
9191
Object entityPlayer = getHandleMethod.invoke(bukkitPlayer);
@@ -155,7 +155,7 @@ public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exce
155155
}
156156

157157
@SneakyThrows
158-
public static byte[] parseCookie(AESUtils utils, AbstractCookie cookie) {
158+
public static byte[] parseCookie(AESGCMUtils utils, AbstractCookie cookie) {
159159
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
160160
DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
161161
cookie.write(dataOutputStream);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package net.outfluencer.convey.common.utils;
2+
3+
import lombok.SneakyThrows;
4+
5+
import javax.crypto.Cipher;
6+
import javax.crypto.KeyGenerator;
7+
import javax.crypto.SecretKey;
8+
import javax.crypto.spec.GCMParameterSpec;
9+
import java.nio.ByteBuffer;
10+
import java.nio.charset.StandardCharsets;
11+
import java.security.SecureRandom;
12+
import java.util.Arrays;
13+
14+
public class AESGCMUtils {
15+
16+
private final static int GCM_IV_LENGTH = 12;
17+
18+
@SneakyThrows
19+
public byte[] decrypt(byte[] data) {
20+
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
21+
cipher.init(Cipher.DECRYPT_MODE, this.key, new GCMParameterSpec(128, data, 0, AESGCMUtils.GCM_IV_LENGTH));
22+
return cipher.doFinal(data, AESGCMUtils.GCM_IV_LENGTH, data.length - AESGCMUtils.GCM_IV_LENGTH);
23+
}
24+
25+
public byte[] encrypt(byte[] data) {
26+
return this.encrypt(this.generateIv(), data);
27+
}
28+
29+
@SneakyThrows
30+
public byte[] encrypt(byte[] iv, byte[] data) {
31+
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
32+
cipher.init(Cipher.ENCRYPT_MODE, this.key, new GCMParameterSpec(128, iv));
33+
byte[] encrypted = cipher.doFinal(data);
34+
ByteBuffer buffer = ByteBuffer.allocate(iv.length + encrypted.length);
35+
buffer.put(iv);
36+
buffer.put(encrypted);
37+
38+
return buffer.array();
39+
}
40+
41+
private final SecretKey key;
42+
private final SecureRandom random;
43+
44+
@SneakyThrows
45+
public AESGCMUtils(SecretKey key) {
46+
this.key = key;
47+
this.random = new SecureRandom();
48+
}
49+
50+
public byte[] generateIv() {
51+
final byte[] iv = new byte[AESGCMUtils.GCM_IV_LENGTH];
52+
this.random.nextBytes(iv);
53+
return iv;
54+
}
55+
56+
}

0 commit comments

Comments
 (0)