From 0879c17d63a0d55e1b755c32dd4bfaa6ab97cce6 Mon Sep 17 00:00:00 2001 From: fiereu Date: Mon, 25 Aug 2025 13:09:37 +0200 Subject: [PATCH] Fix HMAC checksum using client seed for outgoing packets --- .../server/netty/DefaultChannelHandlerProvider.kt | 14 +++++++++----- .../openmmo/server/protocol/tls/TlsContext.kt | 1 + .../server/protocol/tls/TlsProtocolHandler.kt | 5 +++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/server/src/main/kotlin/de/fiereu/openmmo/server/netty/DefaultChannelHandlerProvider.kt b/server/src/main/kotlin/de/fiereu/openmmo/server/netty/DefaultChannelHandlerProvider.kt index d8b90f9..4fecaa8 100644 --- a/server/src/main/kotlin/de/fiereu/openmmo/server/netty/DefaultChannelHandlerProvider.kt +++ b/server/src/main/kotlin/de/fiereu/openmmo/server/netty/DefaultChannelHandlerProvider.kt @@ -17,6 +17,8 @@ import de.fiereu.openmmo.server.protocol.tls.checksum.Checksum import de.fiereu.openmmo.server.protocol.tls.checksum.NoOpChecksum import io.netty.channel.ChannelHandlerContext import io.netty.channel.ChannelPipeline +import io.netty.handler.logging.LogLevel +import io.netty.handler.logging.LoggingHandler import io.netty.handler.timeout.WriteTimeoutHandler import java.util.concurrent.TimeUnit @@ -38,6 +40,7 @@ class DefaultChannelHandlerProvider( override fun configurePipeline(pipeline: ChannelPipeline) { pipeline.addLast("timeout", WriteTimeoutHandler(serverConfig.writeTimeoutSeconds, TimeUnit.SECONDS)) + pipeline.addLast("frame-logger", LoggingHandler("frame-logger", LogLevel.TRACE)) pipeline.addLast("frame-decoder", PacketFrameDecoder()) pipeline.addLast("frame-encoder", PacketFrameEncoder()) @@ -48,7 +51,7 @@ class DefaultChannelHandlerProvider( pipeline.addLast("tls-decryption", TlsDecryptionHandler(tlsContext)) pipeline.addLast("tls-encryption", TlsEncryptionHandler(tlsContext)) - + pipeline.addLast("protocol-logger", LoggingHandler("protocol-logger", LogLevel.TRACE)) pipeline.addLast("protocol", TlsProtocolHandler(tlsProtocol, tlsConfig, this)) } @@ -58,17 +61,18 @@ class DefaultChannelHandlerProvider( * Will be called from the [TlsProtocolHandler] once the handshake is complete and a checksum algorithm is negotiated. * * @param ctx The channel handler context. - * @param newChecksum The new checksum algorithm to use. + * @param newChecksumIncoming The new checksum algorithm for incoming packets. + * @param newChecksumOutgoing The new checksum algorithm for outgoing packets. */ - fun switchChecksum(ctx: ChannelHandlerContext, newChecksum: Checksum) { + fun switchChecksum(ctx: ChannelHandlerContext, newChecksumIncoming: Checksum, newChecksumOutgoing: Checksum) { val pipeline = ctx.pipeline() if (pipeline.get("checksum-decoder") != null) { - pipeline.replace("checksum-decoder", "checksum-decoder", ChecksumFrameDecoder(newChecksum)) + pipeline.replace("checksum-decoder", "checksum-decoder", ChecksumFrameDecoder(newChecksumIncoming)) } if (pipeline.get("checksum-encoder") != null) { - pipeline.replace("checksum-encoder", "checksum-encoder", ChecksumFrameEncoder(newChecksum)) + pipeline.replace("checksum-encoder", "checksum-encoder", ChecksumFrameEncoder(newChecksumOutgoing)) } } diff --git a/server/src/main/kotlin/de/fiereu/openmmo/server/protocol/tls/TlsContext.kt b/server/src/main/kotlin/de/fiereu/openmmo/server/protocol/tls/TlsContext.kt index b046dc4..e2820e2 100644 --- a/server/src/main/kotlin/de/fiereu/openmmo/server/protocol/tls/TlsContext.kt +++ b/server/src/main/kotlin/de/fiereu/openmmo/server/protocol/tls/TlsContext.kt @@ -35,6 +35,7 @@ class DefaultTlsContext( private val decryptionCipher: Cipher // Client to server fun getClientSeed(): ByteArray = clientSeed.copyOf() + fun getServerSeed(): ByteArray = serverSeed.copyOf() init { val sharedSecret = performKeyAgreement(privateKey, publicKey) diff --git a/server/src/main/kotlin/de/fiereu/openmmo/server/protocol/tls/TlsProtocolHandler.kt b/server/src/main/kotlin/de/fiereu/openmmo/server/protocol/tls/TlsProtocolHandler.kt index 6ef725f..d25913c 100644 --- a/server/src/main/kotlin/de/fiereu/openmmo/server/protocol/tls/TlsProtocolHandler.kt +++ b/server/src/main/kotlin/de/fiereu/openmmo/server/protocol/tls/TlsProtocolHandler.kt @@ -65,9 +65,10 @@ class TlsProtocolHandler( connectionKeyPair.second, event.packet.clientPublicKey ) - val checksum = ChecksumFactory.create(tlsConfig.checksumSize, tlsContext.getClientSeed()) + val checksumIncoming = ChecksumFactory.create(tlsConfig.checksumSize, tlsContext.getClientSeed()) + val checksumOutgoing = ChecksumFactory.create(tlsConfig.checksumSize, tlsContext.getServerSeed()) - channelHandlerProvider.switchChecksum(event.ctx, checksum) + channelHandlerProvider.switchChecksum(event.ctx, checksumIncoming, checksumOutgoing) channelHandlerProvider.switchTlsContext(event.ctx, tlsContext) channelHandlerProvider.enableProtocolHandler(event.ctx) log.info { "Client ${event.ctx.channel().remoteAddress()} completed TLS handshake. Switching to next protocol." }