Skip to content

Commit c058a7a

Browse files
committed
Switch to OAEP padding
This is because nodejs has disabled RSA_PKCS1_PADDING due to CVE-2023-46809.
1 parent 4104b64 commit c058a7a

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

mapsync-mod/src/main/java/gjum/minecraft/mapsync/mod/net/SyncClient.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
import io.netty.channel.socket.nio.NioSocketChannel;
2424
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
2525
import io.netty.handler.codec.LengthFieldPrepender;
26+
import java.security.InvalidAlgorithmParameterException;
2627
import java.security.InvalidKeyException;
2728
import java.security.MessageDigest;
2829
import java.security.NoSuchAlgorithmException;
2930
import java.security.PublicKey;
31+
import java.security.spec.MGF1ParameterSpec;
3032
import java.util.ArrayList;
3133
import java.util.Arrays;
3234
import java.util.HashMap;
@@ -39,6 +41,8 @@
3941
import javax.crypto.IllegalBlockSizeException;
4042
import javax.crypto.NoSuchPaddingException;
4143
import javax.crypto.SecretKey;
44+
import javax.crypto.spec.OAEPParameterSpec;
45+
import javax.crypto.spec.PSource;
4246
import javax.crypto.spec.SecretKeySpec;
4347
import net.minecraft.client.Minecraft;
4448
import net.minecraft.client.User;
@@ -286,7 +290,7 @@ void setUpEncryption(ChannelHandlerContext ctx, ClientboundEncryptionRequestPack
286290
encrypt(packet.publicKey(), sharedSecret),
287291
encrypt(packet.publicKey(), packet.verifyToken())));
288292
} catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException | BadPaddingException |
289-
IllegalBlockSizeException e) {
293+
IllegalBlockSizeException | InvalidAlgorithmParameterException e) {
290294
shutDown();
291295
throw new RuntimeException(e);
292296
}
@@ -299,9 +303,15 @@ void setUpEncryption(ChannelHandlerContext ctx, ClientboundEncryptionRequestPack
299303
handleEncryptionSuccess();
300304
}
301305

302-
private static byte[] encrypt(PublicKey key, byte[] data) throws NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException {
303-
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
304-
cipher.init(Cipher.ENCRYPT_MODE, key);
306+
private static byte[] encrypt(PublicKey key, byte[] data) throws NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException, InvalidAlgorithmParameterException {
307+
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
308+
// https://docs.openssl.org/master/man3/RSA_public_encrypt/#description
309+
cipher.init(Cipher.ENCRYPT_MODE, key, new OAEPParameterSpec(
310+
"SHA-256",
311+
"MGF1",
312+
new MGF1ParameterSpec("SHA-256"),
313+
PSource.PSpecified.DEFAULT
314+
));
305315
return cipher.doFinal(data);
306316
}
307317
}

mapsync-server/src/server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ export class TcpServer {
5151
return crypto.privateDecrypt(
5252
{
5353
key: this.keyPair.privateKey,
54-
padding: crypto.constants.RSA_PKCS1_PADDING,
54+
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
55+
oaepHash: "sha256"
5556
},
5657
buf,
5758
);

0 commit comments

Comments
 (0)