This commit is contained in:
Fiereu committed 2025-08-22 16:11:41 +02:00
commit c5ee4e27e6
131 files changed
+9926

No files matched your search

+35
View File
@@ -0,0 +1,35 @@
# Protocol.TLS Module
The protocol.tls module implements a custom TLS-like protocol for secure client-server communication in OpenMMO.
## Contents
### Core Classes
- **TlsProtocol**: Abstract base protocol for TLS handshake implementation
- **TlsServerProtocol**: Server-side TLS protocol implementation
- **TlsClientProtocol**: Client-side TLS protocol implementation
### Packet Types
- **ClientHelloPacket**: Initial handshake packet sent by client
- **ServerHelloPacket**: Server response containing server's public key
- **ClientReadyPacket**: Client confirmation with client's public key
### Cryptographic Features
- **ECKeyExtensions**: Extensions for elliptic curve key operations
- ECDH key exchange for shared secret derivation
- Secure communication setup using ECDSA key pairs
## Protocol Flow
1. Client sends `ClientHello` to initiate handshake
2. Server responds with `ServerHello` containing its public key
3. Client derives shared secret using ECDH and switches to secure mode
4. Client sends `ClientReady` with its public key
5. Server derives shared secret and switches to next protocol (e.g., LoginProtocol)
## Dependencies
- Protocol module for base framework
- BouncyCastle crypto libraries for cryptographic operations
- Kotest for testing
## Security
This module implements a custom TLS-like protocol specifically designed for PokeMMO server compatibility, using ECDSA with secp256r1 curve for key exchange and authentication.
+11
View File
@@ -0,0 +1,11 @@
plugins {
id("buildsrc.convention.kotlin-jvm")
id("buildsrc.convention.spotless")
}
dependencies {
api(project(":protocol"))
api(libs.bundles.crypto)
testImplementation(libs.bundles.kotest)
}
@@ -0,0 +1,46 @@
package de.fiereu.openmmo.protocols.tls
import de.fiereu.openmmo.protocols.Protocol
import de.fiereu.openmmo.protocols.incomingPacket
import de.fiereu.openmmo.protocols.outgoingPacket
import de.fiereu.openmmo.protocols.tls.packets.*
import java.security.interfaces.ECPrivateKey
import java.security.interfaces.ECPublicKey
/**
* TLS Protocol implementation.
* This protocol handles the initial TLS handshake before any secure communication can take place.
*
* The flow is as follows:
* 1. Client sends a ClientHello packet to initiate the handshake.
* 2. Server responds with a ServerHello packet.
* -> The client knows the servers public key and its own private key. And it can derive a shared secret using ECDH.
* -> Every packet the client now would receive is expected to be secured.
* -> Client switches to next protocol (e.g. LoginProtocol) after the handshake is complete.
* 3. Client sends a ClientReady packet with its public key.
* -> The server can now secure its communication with the client using the shared secret.
* -> Server switches to next protocol (e.g. LoginProtocol) after the handshake is complete.
*/
abstract class TlsProtocol() : Protocol() {
override val async: Boolean
get() = false
}
class TlsServerProtocol(
serverKeyPair: Pair<ECPrivateKey, ECPublicKey>
) : TlsProtocol() {
init {
incomingPacket(0x00u, ClientHelloPacketDeserializer())
outgoingPacket(0x01u, ServerHelloPacket::class, ServerHelloPacketSerializer(serverKeyPair.first))
incomingPacket(0x02u, ClientReadyPacketDeserializer())
}
}
class TlsClientProtocol() : TlsProtocol() {
init {
outgoingPacket(0x00u, ClientHelloPacket::class, ClientHelloPacketSerializer())
incomingPacket(0x01u, ServerHelloPacketDeserializer())
outgoingPacket(0x02u, ClientReadyPacket::class, ClientReadyPacketSerializer())
}
}
@@ -0,0 +1,37 @@
package de.fiereu.openmmo.protocols.tls.packets
import de.fiereu.openmmo.protocols.PacketDeserializer
import de.fiereu.openmmo.protocols.PacketSerializer
import io.netty.buffer.ByteBuf
import kotlin.random.Random
private const val key1 = 3214621489648854472L
private const val key2 = -4214651440992349575L
data class ClientHelloPacket(val timestamp: Long)
class ClientHelloPacketSerializer : PacketSerializer<ClientHelloPacket> {
override fun serialize(packet: ClientHelloPacket, buffer: ByteBuf) {
val randomKey = Random.nextLong()
val xoredRandomKey = randomKey xor key1
val xoredTimestamp = packet.timestamp xor key2 xor randomKey
buffer.writeLongLE(xoredRandomKey)
buffer.writeLongLE(xoredTimestamp)
}
}
class ClientHelloPacketDeserializer : PacketDeserializer<ClientHelloPacket> {
override fun deserialize(buffer: ByteBuf): ClientHelloPacket {
val xoredRandomKey = buffer.readLongLE()
val xoredTimestamp = buffer.readLongLE()
val randomKey = xoredRandomKey xor key1
val timestamp = xoredTimestamp xor key2 xor randomKey
return ClientHelloPacket(timestamp)
}
}
@@ -0,0 +1,34 @@
package de.fiereu.openmmo.protocols.tls.packets
import de.fiereu.openmmo.protocols.PacketDeserializer
import de.fiereu.openmmo.protocols.PacketSerializer
import de.fiereu.openmmo.protocols.tls.util.toECPublicKey
import de.fiereu.openmmo.protocols.tls.util.toUncompressedPoint
import io.netty.buffer.ByteBuf
import java.security.interfaces.ECPublicKey
data class ClientReadyPacket(
val clientPublicKey: ECPublicKey
)
class ClientReadyPacketSerializer : PacketSerializer<ClientReadyPacket> {
override fun serialize(packet: ClientReadyPacket, buffer: ByteBuf) {
val publicKeyBytes = packet.clientPublicKey.toUncompressedPoint()
buffer.writeShortLE(publicKeyBytes.size)
buffer.writeBytes(publicKeyBytes)
}
}
class ClientReadyPacketDeserializer : PacketDeserializer<ClientReadyPacket> {
override fun deserialize(buffer: ByteBuf): ClientReadyPacket {
val keyBytes = ByteArray(buffer.readShortLE().toInt())
buffer.readBytes(keyBytes)
val publicKey = keyBytes.toECPublicKey()
return ClientReadyPacket(publicKey)
}
}
@@ -0,0 +1,65 @@
package de.fiereu.openmmo.protocols.tls.packets
import de.fiereu.openmmo.protocols.PacketDeserializer
import de.fiereu.openmmo.protocols.PacketSerializer
import de.fiereu.openmmo.protocols.tls.util.sign
import de.fiereu.openmmo.protocols.tls.util.toECPublicKey
import de.fiereu.openmmo.protocols.tls.util.toUncompressedPoint
import io.netty.buffer.ByteBuf
import java.security.PublicKey
import java.security.interfaces.ECPrivateKey
import java.security.interfaces.ECPublicKey
/**
* Represents a server hello packet in the TLS handshake.
*
* @property publicKey The public key generated for this connection.
* @property checksumSize The size of the checksum used for this connection.
*/
data class ServerHelloPacket(
val publicKey: ECPublicKey,
val checksumSize: Int
)
class ServerHelloPacketSerializer(
private val serverRootPrivateKey: ECPrivateKey
) : PacketSerializer<ServerHelloPacket> {
override fun serialize(packet: ServerHelloPacket, buffer: ByteBuf) {
val publicKeyBytes = packet.publicKey.toUncompressedPoint()
val signature = serverRootPrivateKey.sign(publicKeyBytes)
buffer.writeShortLE(publicKeyBytes.size)
buffer.writeBytes(publicKeyBytes)
buffer.writeShortLE(signature.size)
buffer.writeBytes(signature)
buffer.writeByte(packet.checksumSize)
}
}
class ServerHelloPacketDeserializer : PacketDeserializer<ServerHelloPacketDeserializer.ServerHelloPacket> {
data class ServerHelloPacket(
val clientPublicKey: PublicKey,
val signature: ByteArray,
val checksumSize: Int
)
override fun deserialize(buffer: ByteBuf): ServerHelloPacket {
val publicKeySize = buffer.readShortLE().toInt()
val publicKeyBytes = ByteArray(publicKeySize)
buffer.readBytes(publicKeyBytes)
val clientPublicKey = publicKeyBytes.toECPublicKey()
val signatureSize = buffer.readShortLE().toInt()
val signature = ByteArray(signatureSize)
buffer.readBytes(signature)
val checksumSize = buffer.readByte().toInt()
return ServerHelloPacket(clientPublicKey, signature, checksumSize)
}
}
@@ -0,0 +1,96 @@
package de.fiereu.openmmo.protocols.tls.util
import java.math.BigInteger
import java.security.AlgorithmParameters
import java.security.KeyFactory
import java.security.Security
import java.security.interfaces.ECPublicKey
import java.security.spec.ECGenParameterSpec
import java.security.spec.ECParameterSpec
import java.security.spec.ECPoint
import java.security.spec.ECPublicKeySpec
import java.security.Signature
import java.security.interfaces.ECPrivateKey
private const val uncompressedPointIndicator: Byte = 0x04
private val keyFactory: KeyFactory = KeyFactory.getInstance("EC")
private val ecParameterSpec: ECParameterSpec by lazy {
val algorithmParameters = AlgorithmParameters.getInstance("EC")
algorithmParameters.init(ECGenParameterSpec("secp256r1"))
algorithmParameters.getParameterSpec(ECParameterSpec::class.java)
}
fun ECPublicKey.toUncompressedPoint(): ByteArray {
val key = this
val point = key.w
val keyLength = (ecParameterSpec.order.bitLength() + Byte.SIZE_BITS - 1) / Byte.SIZE_BITS
var offset = 0
val data = ByteArray(1 + 2 * keyLength)
data[offset++] = uncompressedPointIndicator
val x = point.affineX.toByteArray()
val y = point.affineY.toByteArray()
when {
x.size <= keyLength -> {
System.arraycopy(x, 0, data, offset + keyLength - x.size, x.size)
}
x.size == keyLength + 1 && x[0] == 0.toByte() -> {
System.arraycopy(x, 1, data, offset, keyLength)
}
else -> {
throw IllegalStateException("x value is too large")
}
}
offset += keyLength
when {
y.size <= keyLength -> {
System.arraycopy(y, 0, data, offset + keyLength - y.size, y.size)
}
y.size == keyLength + 1 && y[0] == 0.toByte() -> {
System.arraycopy(y, 1, data, offset, keyLength)
}
else -> {
throw IllegalStateException("y value is too large")
}
}
return data
}
fun ByteArray.toECPublicKey(): ECPublicKey {
if (this[0] != uncompressedPointIndicator) {
throw IllegalArgumentException("Invalid uncompressed point indicator")
}
val keyLength = (ecParameterSpec.order.bitLength() + Byte.SIZE_BITS - 1) / Byte.SIZE_BITS
if (this.size != 1 + 2 * keyLength) {
throw IllegalArgumentException("Invalid data length")
}
var offset = 1
val x = BigInteger(1, this.copyOfRange(offset, offset + keyLength))
offset += keyLength
val y = BigInteger(1, this.copyOfRange(offset, offset + keyLength))
val point = ECPoint(x, y)
return keyFactory.generatePublic(ECPublicKeySpec(point, ecParameterSpec)) as ECPublicKey
}
fun ECPrivateKey.sign(data: ByteArray): ByteArray {
val signature = Signature.getInstance("SHA256withECDSA")
signature.initSign(this)
signature.update(data)
return signature.sign()
}
fun ECPublicKey.verify(data: ByteArray, signatureBytes: ByteArray): Boolean {
val signature = Signature.getInstance("SHA256withECDSA")
signature.initVerify(this)
signature.update(data)
return signature.verify(signatureBytes)
}