wip: sign commands

This commit is contained in:
Bixilon 2022-10-19 21:46:05 +02:00
parent 907368a630
commit e21d31a086
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
3 changed files with 48 additions and 11 deletions

View File

@ -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<StackEntry> = mutableListOf()
private val stack: MutableList<CommandStackEntry> = 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<String, ByteArray> {
val output: MutableMap<String, ByteArray> = mutableMapOf()
for (entry in stack) {
output[entry.name] = entry.sign(connection, chain, key, salt, time)
}
return output
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*
* 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()))
}
}

View File

@ -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<String, ByteArray> = mutableMapOf()
var signature: Map<String, ByteArray> = 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() {