From 55d72dacbe15e7ab2860e59df7f7d6b6a5133b43 Mon Sep 17 00:00:00 2001 From: Graham Date: Wed, 26 May 2021 18:09:05 +0100 Subject: [PATCH] Pass StreamCipher to PacketCodecs A small number of packets have encrypted payloads. Signed-off-by: Graham --- .../src/main/kotlin/org/openrs2/protocol/EmptyPacketCodec.kt | 5 +++-- protocol/src/main/kotlin/org/openrs2/protocol/PacketCodec.kt | 5 +++-- protocol/src/main/kotlin/org/openrs2/protocol/Rs2Decoder.kt | 2 +- protocol/src/main/kotlin/org/openrs2/protocol/Rs2Encoder.kt | 2 +- .../openrs2/protocol/login/InitJs5RemoteConnectionCodec.kt | 5 +++-- .../org/openrs2/protocol/login/RequestWorldListCodec.kt | 5 +++-- .../src/test/kotlin/org/openrs2/protocol/FixedPacketCodec.kt | 5 +++-- .../kotlin/org/openrs2/protocol/LengthMismatchPacketCodec.kt | 5 +++-- .../org/openrs2/protocol/VariableByteOptimisedPacketCodec.kt | 5 +++-- .../kotlin/org/openrs2/protocol/VariableBytePacketCodec.kt | 5 +++-- .../openrs2/protocol/VariableShortOptimisedPacketCodec.kt | 5 +++-- .../kotlin/org/openrs2/protocol/VariableShortPacketCodec.kt | 5 +++-- 12 files changed, 32 insertions(+), 22 deletions(-) diff --git a/protocol/src/main/kotlin/org/openrs2/protocol/EmptyPacketCodec.kt b/protocol/src/main/kotlin/org/openrs2/protocol/EmptyPacketCodec.kt index e6a8f8fc..1413967b 100644 --- a/protocol/src/main/kotlin/org/openrs2/protocol/EmptyPacketCodec.kt +++ b/protocol/src/main/kotlin/org/openrs2/protocol/EmptyPacketCodec.kt @@ -1,16 +1,17 @@ package org.openrs2.protocol import io.netty.buffer.ByteBuf +import org.openrs2.crypto.StreamCipher public abstract class EmptyPacketCodec( private val packet: T, opcode: Int ) : PacketCodec(packet.javaClass, opcode, length = 0) { - override fun decode(input: ByteBuf): T { + override fun decode(input: ByteBuf, cipher: StreamCipher): T { return packet } - override fun encode(input: T, output: ByteBuf) { + override fun encode(input: T, output: ByteBuf, cipher: StreamCipher) { // empty } } diff --git a/protocol/src/main/kotlin/org/openrs2/protocol/PacketCodec.kt b/protocol/src/main/kotlin/org/openrs2/protocol/PacketCodec.kt index 5d92818b..df1e894b 100644 --- a/protocol/src/main/kotlin/org/openrs2/protocol/PacketCodec.kt +++ b/protocol/src/main/kotlin/org/openrs2/protocol/PacketCodec.kt @@ -2,6 +2,7 @@ package org.openrs2.protocol import io.netty.buffer.ByteBuf import io.netty.buffer.ByteBufAllocator +import org.openrs2.crypto.StreamCipher public abstract class PacketCodec( public val type: Class, @@ -13,8 +14,8 @@ public abstract class PacketCodec( require(length >= PacketLength.VARIABLE_SHORT) } - public abstract fun decode(input: ByteBuf): T - public abstract fun encode(input: T, output: ByteBuf) + public abstract fun decode(input: ByteBuf, cipher: StreamCipher): T + public abstract fun encode(input: T, output: ByteBuf, cipher: StreamCipher) public open fun getLength(input: T): Int { return length diff --git a/protocol/src/main/kotlin/org/openrs2/protocol/Rs2Decoder.kt b/protocol/src/main/kotlin/org/openrs2/protocol/Rs2Decoder.kt index e20a1353..77838584 100644 --- a/protocol/src/main/kotlin/org/openrs2/protocol/Rs2Decoder.kt +++ b/protocol/src/main/kotlin/org/openrs2/protocol/Rs2Decoder.kt @@ -65,7 +65,7 @@ public class Rs2Decoder(public var protocol: Protocol) : ByteToMessageDecoder() val payload = input.readSlice(length) out += try { - decoder.decode(payload) + decoder.decode(payload, cipher) } catch (ex: NotImplementedError) { // TODO(gpe): remove this catch block when every packet is implemented logger.warn { "Skipping unimplemented packet: ${decoder.javaClass}" } diff --git a/protocol/src/main/kotlin/org/openrs2/protocol/Rs2Encoder.kt b/protocol/src/main/kotlin/org/openrs2/protocol/Rs2Encoder.kt index 813cdada..806ad257 100644 --- a/protocol/src/main/kotlin/org/openrs2/protocol/Rs2Encoder.kt +++ b/protocol/src/main/kotlin/org/openrs2/protocol/Rs2Encoder.kt @@ -24,7 +24,7 @@ public class Rs2Encoder(public var protocol: Protocol) : MessageToByteEncoder( @@ -8,12 +9,12 @@ public object InitJs5RemoteConnectionCodec : PacketCodec( @@ -8,12 +9,12 @@ public object RequestWorldListCodec : PacketCodec opcode = 23, length = 4 ) { - override fun decode(input: ByteBuf): LoginRequest.RequestWorldList { + override fun decode(input: ByteBuf, cipher: StreamCipher): LoginRequest.RequestWorldList { val checksum = input.readInt() return LoginRequest.RequestWorldList(checksum) } - override fun encode(input: LoginRequest.RequestWorldList, output: ByteBuf) { + override fun encode(input: LoginRequest.RequestWorldList, output: ByteBuf, cipher: StreamCipher) { output.writeInt(input.checksum) } } diff --git a/protocol/src/test/kotlin/org/openrs2/protocol/FixedPacketCodec.kt b/protocol/src/test/kotlin/org/openrs2/protocol/FixedPacketCodec.kt index 0f428c30..3d9452fd 100644 --- a/protocol/src/test/kotlin/org/openrs2/protocol/FixedPacketCodec.kt +++ b/protocol/src/test/kotlin/org/openrs2/protocol/FixedPacketCodec.kt @@ -1,18 +1,19 @@ package org.openrs2.protocol import io.netty.buffer.ByteBuf +import org.openrs2.crypto.StreamCipher internal object FixedPacketCodec : PacketCodec( type = FixedPacket::class.java, opcode = 0, length = 4 ) { - override fun decode(input: ByteBuf): FixedPacket { + override fun decode(input: ByteBuf, cipher: StreamCipher): FixedPacket { val value = input.readInt() return FixedPacket(value) } - override fun encode(input: FixedPacket, output: ByteBuf) { + override fun encode(input: FixedPacket, output: ByteBuf, cipher: StreamCipher) { output.writeInt(input.value) } } diff --git a/protocol/src/test/kotlin/org/openrs2/protocol/LengthMismatchPacketCodec.kt b/protocol/src/test/kotlin/org/openrs2/protocol/LengthMismatchPacketCodec.kt index 8a4009eb..f89d5873 100644 --- a/protocol/src/test/kotlin/org/openrs2/protocol/LengthMismatchPacketCodec.kt +++ b/protocol/src/test/kotlin/org/openrs2/protocol/LengthMismatchPacketCodec.kt @@ -1,18 +1,19 @@ package org.openrs2.protocol import io.netty.buffer.ByteBuf +import org.openrs2.crypto.StreamCipher internal object LengthMismatchPacketCodec : PacketCodec( type = FixedPacket::class.java, opcode = 0, length = 5 ) { - override fun decode(input: ByteBuf): FixedPacket { + override fun decode(input: ByteBuf, cipher: StreamCipher): FixedPacket { val value = input.readInt() return FixedPacket(value) } - override fun encode(input: FixedPacket, output: ByteBuf) { + override fun encode(input: FixedPacket, output: ByteBuf, cipher: StreamCipher) { output.writeInt(input.value) } } diff --git a/protocol/src/test/kotlin/org/openrs2/protocol/VariableByteOptimisedPacketCodec.kt b/protocol/src/test/kotlin/org/openrs2/protocol/VariableByteOptimisedPacketCodec.kt index 0b56e87c..7ac4085b 100644 --- a/protocol/src/test/kotlin/org/openrs2/protocol/VariableByteOptimisedPacketCodec.kt +++ b/protocol/src/test/kotlin/org/openrs2/protocol/VariableByteOptimisedPacketCodec.kt @@ -1,19 +1,20 @@ package org.openrs2.protocol import io.netty.buffer.ByteBuf +import org.openrs2.crypto.StreamCipher internal object VariableByteOptimisedPacketCodec : PacketCodec( type = VariableByteOptimisedPacket::class.java, opcode = 3, length = PacketLength.VARIABLE_BYTE ) { - override fun decode(input: ByteBuf): VariableByteOptimisedPacket { + override fun decode(input: ByteBuf, cipher: StreamCipher): VariableByteOptimisedPacket { val value = ByteArray(input.readableBytes()) input.readBytes(value) return VariableByteOptimisedPacket(value) } - override fun encode(input: VariableByteOptimisedPacket, output: ByteBuf) { + override fun encode(input: VariableByteOptimisedPacket, output: ByteBuf, cipher: StreamCipher) { output.writeBytes(input.value) } diff --git a/protocol/src/test/kotlin/org/openrs2/protocol/VariableBytePacketCodec.kt b/protocol/src/test/kotlin/org/openrs2/protocol/VariableBytePacketCodec.kt index 46bc4974..89de8166 100644 --- a/protocol/src/test/kotlin/org/openrs2/protocol/VariableBytePacketCodec.kt +++ b/protocol/src/test/kotlin/org/openrs2/protocol/VariableBytePacketCodec.kt @@ -1,19 +1,20 @@ package org.openrs2.protocol import io.netty.buffer.ByteBuf +import org.openrs2.crypto.StreamCipher internal object VariableBytePacketCodec : PacketCodec( type = VariableBytePacket::class.java, opcode = 1, length = PacketLength.VARIABLE_BYTE ) { - override fun decode(input: ByteBuf): VariableBytePacket { + override fun decode(input: ByteBuf, cipher: StreamCipher): VariableBytePacket { val value = ByteArray(input.readableBytes()) input.readBytes(value) return VariableBytePacket(value) } - override fun encode(input: VariableBytePacket, output: ByteBuf) { + override fun encode(input: VariableBytePacket, output: ByteBuf, cipher: StreamCipher) { output.writeBytes(input.value) } } diff --git a/protocol/src/test/kotlin/org/openrs2/protocol/VariableShortOptimisedPacketCodec.kt b/protocol/src/test/kotlin/org/openrs2/protocol/VariableShortOptimisedPacketCodec.kt index 1f719a2c..acb3d5b2 100644 --- a/protocol/src/test/kotlin/org/openrs2/protocol/VariableShortOptimisedPacketCodec.kt +++ b/protocol/src/test/kotlin/org/openrs2/protocol/VariableShortOptimisedPacketCodec.kt @@ -1,19 +1,20 @@ package org.openrs2.protocol import io.netty.buffer.ByteBuf +import org.openrs2.crypto.StreamCipher internal object VariableShortOptimisedPacketCodec : PacketCodec( type = VariableShortOptimisedPacket::class.java, opcode = 4, length = PacketLength.VARIABLE_SHORT ) { - override fun decode(input: ByteBuf): VariableShortOptimisedPacket { + override fun decode(input: ByteBuf, cipher: StreamCipher): VariableShortOptimisedPacket { val value = ByteArray(input.readableBytes()) input.readBytes(value) return VariableShortOptimisedPacket(value) } - override fun encode(input: VariableShortOptimisedPacket, output: ByteBuf) { + override fun encode(input: VariableShortOptimisedPacket, output: ByteBuf, cipher: StreamCipher) { output.writeBytes(input.value) } diff --git a/protocol/src/test/kotlin/org/openrs2/protocol/VariableShortPacketCodec.kt b/protocol/src/test/kotlin/org/openrs2/protocol/VariableShortPacketCodec.kt index 76d74607..814163d7 100644 --- a/protocol/src/test/kotlin/org/openrs2/protocol/VariableShortPacketCodec.kt +++ b/protocol/src/test/kotlin/org/openrs2/protocol/VariableShortPacketCodec.kt @@ -1,19 +1,20 @@ package org.openrs2.protocol import io.netty.buffer.ByteBuf +import org.openrs2.crypto.StreamCipher internal object VariableShortPacketCodec : PacketCodec( type = VariableShortPacket::class.java, opcode = 2, length = PacketLength.VARIABLE_SHORT ) { - override fun decode(input: ByteBuf): VariableShortPacket { + override fun decode(input: ByteBuf, cipher: StreamCipher): VariableShortPacket { val value = ByteArray(input.readableBytes()) input.readBytes(value) return VariableShortPacket(value) } - override fun encode(input: VariableShortPacket, output: ByteBuf) { + override fun encode(input: VariableShortPacket, output: ByteBuf, cipher: StreamCipher) { output.writeBytes(input.value) } }