From e77050936f65cec266f50a456d06f62ecfc0eb44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noel=20N=C3=A9meth?= Date: Sun, 12 Jun 2022 15:33:10 +0200 Subject: [PATCH] Move signatures to different record to be used in command context --- .../server/command/ArgumentsSignature.java | 73 +++++++++++++++++++ .../client/play/ClientCommandChatPacket.java | 62 ++-------------- 2 files changed, 78 insertions(+), 57 deletions(-) create mode 100644 src/main/java/net/minestom/server/command/ArgumentsSignature.java diff --git a/src/main/java/net/minestom/server/command/ArgumentsSignature.java b/src/main/java/net/minestom/server/command/ArgumentsSignature.java new file mode 100644 index 000000000..761732954 --- /dev/null +++ b/src/main/java/net/minestom/server/command/ArgumentsSignature.java @@ -0,0 +1,73 @@ +package net.minestom.server.command; + +import net.minestom.server.command.builder.arguments.Argument; +import net.minestom.server.command.builder.arguments.minecraft.SignableArgument; +import net.minestom.server.crypto.MessageSignature; +import net.minestom.server.utils.binary.BinaryReader; +import net.minestom.server.utils.binary.BinaryWriter; +import net.minestom.server.utils.binary.Writeable; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.time.Instant; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +public record ArgumentsSignature(long timestamp, long salt, Map signatures, boolean signedPreview) +implements Writeable { + public ArgumentsSignature(long timestamp, long salt, Map signatures, boolean signedPreview) { + this.timestamp = timestamp; + this.salt = salt; + this.signatures = Map.copyOf(signatures); + this.signedPreview = signedPreview; + } + + public ArgumentsSignature(BinaryReader reader) { + this(reader.readLong(), reader.readLong(), readSignature(reader), reader.readBoolean()); + } + + /** + * Constructs a new {@link MessageSignature} to use in validation + * + * @param signer player's uuid who sent this packet + * @return null if the client didn't sign the parameter + */ + @Contract("_, _ -> new") + public @Nullable MessageSignature signatureOf(String parameterName, UUID signer) { + final byte[] signature = signatures.get(parameterName); + return signature == null ? null : new MessageSignature(signer, Instant.ofEpochMilli(timestamp), salt, signature); + } + + /** + * @see #signatureOf(String, UUID) + */ + @SuppressWarnings("ConstantConditions") + @Contract("_, _ -> new") + public & SignableArgument> @Nullable MessageSignature signatureOf(T argument, UUID signer) { + return signatureOf(argument.getId(), signer); + } + + private static Map readSignature(BinaryReader reader) { + final int length = reader.readVarInt(); + Map signatures = new HashMap<>(); + for (int i = 0; i < length; i++) { + final String s = reader.readSizedString(256); + final byte[] bytes = reader.readByteArray(); + signatures.put(s, bytes); + } + return Map.copyOf(signatures); + } + + @Override + public void write(@NotNull BinaryWriter writer) { + writer.writeLong(timestamp); + writer.writeLong(salt); + this.signatures.forEach((s, bytes) -> { + writer.writeSizedString(s); + writer.writeByteArray(bytes); + }); + writer.writeBoolean(signedPreview); + } +} diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientCommandChatPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientCommandChatPacket.java index 15bc40c38..d8720e726 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientCommandChatPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientCommandChatPacket.java @@ -1,73 +1,21 @@ package net.minestom.server.network.packet.client.play; -import net.minestom.server.command.builder.arguments.Argument; -import net.minestom.server.command.builder.arguments.minecraft.SignableArgument; -import net.minestom.server.crypto.MessageSignature; +import net.minestom.server.command.ArgumentsSignature; import net.minestom.server.network.packet.client.ClientPacket; import net.minestom.server.utils.binary.BinaryReader; import net.minestom.server.utils.binary.BinaryWriter; -import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.time.Instant; -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; - -public record ClientCommandChatPacket(@NotNull String message, long timestamp, - long salt, Map signatures, - boolean signedPreview) implements ClientPacket { - public ClientCommandChatPacket { - signatures = Map.copyOf(signatures); - } +public record ClientCommandChatPacket(@NotNull String message, @NotNull ArgumentsSignature argumentsSignature) + implements ClientPacket { public ClientCommandChatPacket(BinaryReader reader) { - this(reader.readSizedString(256), reader.readLong(), - reader.readLong(), readSignature(reader), reader.readBoolean()); - } - - /** - * Constructs a new {@link MessageSignature} to use in validation - * - * @param signer player's uuid who sent this packet - * @return null if the client didn't sign the parameter - */ - @Contract("_, _ -> new") - public @Nullable MessageSignature signatureOf(String parameterName, UUID signer) { - final byte[] signature = signatures.get(parameterName); - return signature == null ? null : new MessageSignature(signer, Instant.ofEpochMilli(timestamp), salt, signature); - } - - /** - * @see #signatureOf(String, UUID) - */ - @SuppressWarnings("ConstantConditions") - @Contract("_, _ -> new") - public & SignableArgument> @Nullable MessageSignature signatureOf(T argument, UUID signer) { - return signatureOf(argument.getId(), signer); + this(reader.readSizedString(256), new ArgumentsSignature(reader)); } @Override public void write(@NotNull BinaryWriter writer) { writer.writeSizedString(message); - writer.writeLong(timestamp); - writer.writeLong(salt); - this.signatures.forEach((s, bytes) -> { - writer.writeSizedString(s); - writer.writeByteArray(bytes); - }); - writer.writeBoolean(signedPreview); + writer.write(argumentsSignature); } - private static Map readSignature(BinaryReader reader) { - final int length = reader.readVarInt(); - Map signatures = new HashMap<>(); - for (int i = 0; i < length; i++) { - final String s = reader.readSizedString(256); - final byte[] bytes = reader.readByteArray(); - signatures.put(s, bytes); - } - return Map.copyOf(signatures); - } }