diff --git a/src/main/java/de/bixilon/minosoft/commands/stack/CommandStack.kt b/src/main/java/de/bixilon/minosoft/commands/stack/CommandStack.kt index 5b336a68f..5f6d600fd 100644 --- a/src/main/java/de/bixilon/minosoft/commands/stack/CommandStack.kt +++ b/src/main/java/de/bixilon/minosoft/commands/stack/CommandStack.kt @@ -16,14 +16,17 @@ package de.bixilon.minosoft.commands.stack import de.bixilon.kutil.cast.CastUtil.nullCast import de.bixilon.minosoft.commands.stack.print.PrintTarget import de.bixilon.minosoft.commands.stack.print.SystemPrintTarget +import de.bixilon.minosoft.data.chat.signature.MessageChain import de.bixilon.minosoft.data.entities.entities.Entity import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection +import java.security.PrivateKey +import java.time.Instant class CommandStack( connection: PlayConnection? = null, val print: PrintTarget = SystemPrintTarget, ) { - private val stack: MutableList = mutableListOf() + private val stack: MutableList = mutableListOf() val size: Int get() = stack.size var executor: Entity? = null @@ -49,11 +52,14 @@ class CommandStack( } fun push(name: String, data: Any?) { - stack.add(StackEntry(name, data)) + stack.add(CommandStackEntry(name, data)) } - private data class StackEntry( - val name: String, - val data: Any?, - ) + fun sign(chain: MessageChain, key: PrivateKey, salt: Long, time: Instant): Map { + val output: MutableMap = mutableMapOf() + for (entry in stack) { + output[entry.name] = entry.sign(connection, chain, key, salt, time) + } + return output + } } diff --git a/src/main/java/de/bixilon/minosoft/commands/stack/CommandStackEntry.kt b/src/main/java/de/bixilon/minosoft/commands/stack/CommandStackEntry.kt new file mode 100644 index 000000000..111f42e14 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/commands/stack/CommandStackEntry.kt @@ -0,0 +1,30 @@ +/* + * Minosoft + * Copyright (C) 2020-2022 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.commands.stack + +import de.bixilon.minosoft.data.chat.signature.LastSeenMessageList +import de.bixilon.minosoft.data.chat.signature.MessageChain +import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection +import java.security.PrivateKey +import java.time.Instant + +data class CommandStackEntry( + val name: String, + val data: Any?, +) { + + fun sign(connection: PlayConnection, chain: MessageChain, key: PrivateKey, salt: Long, time: Instant): ByteArray { + return chain.signMessage(connection.version, key, data.toString(), null, salt, connection.player.uuid, time, LastSeenMessageList(emptyArray())) + } +} diff --git a/src/main/java/de/bixilon/minosoft/protocol/network/connection/play/ConnectionUtil.kt b/src/main/java/de/bixilon/minosoft/protocol/network/connection/play/ConnectionUtil.kt index 945d01a20..c64fa2cdd 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/network/connection/play/ConnectionUtil.kt +++ b/src/main/java/de/bixilon/minosoft/protocol/network/connection/play/ConnectionUtil.kt @@ -103,17 +103,18 @@ class ConnectionUtil( if (!connection.version.requiresSignedChat || connection.profiles.connection.signature.sendCommandAsMessage) { return sendChatMessage(command) } - val seed = SecureRandom().nextLong() + val salt = SecureRandom().nextLong() val time = Instant.now() val acknowledgement = Acknowledgement.EMPTY - val signature: MutableMap = mutableMapOf() + var signature: Map = emptyMap() - if (connection.profiles.connection.signature.signCommands) { - Log.log(LogMessageType.CHAT_OUT, LogLevels.WARN) { "Can not sign commands yet!" } + val key = connection.player.privateKey + if (key != null && connection.profiles.connection.signature.signCommands) { + signature = stack.sign(chain, key.private, salt, time) } - connection.sendPacket(CommandC2SP(command.trimWhitespaces().removePrefix("/"), time, seed, signature, false, acknowledgement)) + connection.sendPacket(CommandC2SP(command.trimWhitespaces().removePrefix("/"), time, salt, signature, false, acknowledgement)) } fun prepareSpawn() {