init
This commit is contained in:
commit
c5ee4e27e6
131 files changed
+9926
No files matched your search
@@ -0,0 +1 @@
|
||||
build/*.pem
|
||||
@@ -0,0 +1,28 @@
|
||||
# Keys (OpenMMO)
|
||||
|
||||
## ToC
|
||||
- [Description](#description)
|
||||
- [How to Use](#how-to-use)
|
||||
- [How It Works](#how-it-works)
|
||||
|
||||
## Description
|
||||
The keys module is responsible for generating the keys used by the servers for signing packets and by the client for verifying them.
|
||||
It generates the public keys that are later used by the patcher module to replace the client's public keys.
|
||||
|
||||
> Generated keys are specifically excluded from git commits using `.gitignore` and should not be committed.
|
||||
|
||||
## How to Use
|
||||
Normally, you don't need to worry about this module, as it is automatically called by all depending modules.
|
||||
However, if you want to generate the keys manually, you can do so by running the `generateKeys` task in Gradle.
|
||||
This will generate the keys and place them in the `build/` directory of the module.
|
||||
|
||||
Currently, there are only two key pairs generated:
|
||||
- `game`: The key pair used for signing game packets by the login and game servers.
|
||||
- `chat`: The key pair used for signing chat packets by the chat server.
|
||||
|
||||
## How It Works
|
||||
The build script used [cryptography-kotlin](https://github.com/whyoleg/cryptography-kotlin) to generate the keypairs.
|
||||
`cryptography-kotlin` is a Kotlin wrapper around libraries like OpenSSL and just provides us a simple API to generate the keys.
|
||||
|
||||
### Key Generation
|
||||
Keys are generated using the `ECDSA` algorithm with the `secp256r1` curve. Generated keys are saved in PEM format.
|
||||
@@ -0,0 +1,90 @@
|
||||
import org.bouncycastle.util.io.pem.PemObject
|
||||
import org.bouncycastle.util.io.pem.PemWriter
|
||||
import java.security.KeyPairGenerator
|
||||
import java.security.spec.ECGenParameterSpec
|
||||
import java.io.StringWriter
|
||||
|
||||
buildscript {
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
classpath(libs.bundles.crypto)
|
||||
}
|
||||
}
|
||||
|
||||
plugins {
|
||||
id("buildsrc.convention.spotless")
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract task for generating a certificate.
|
||||
*/
|
||||
abstract class GenerateCertificateTask : DefaultTask() {
|
||||
|
||||
@get:OutputFile
|
||||
val privateKeyOutput: RegularFileProperty = project.objects.fileProperty()
|
||||
|
||||
@get:OutputFile
|
||||
val publicKeyOutput: RegularFileProperty = project.objects.fileProperty()
|
||||
|
||||
@get:Input
|
||||
val overwriteCertificates: Property<Boolean> = project.objects.property(Boolean::class.java)
|
||||
.convention(false)
|
||||
|
||||
init {
|
||||
group = "openmmo"
|
||||
outputs.upToDateWhen { !overwriteCertificates.get() }
|
||||
}
|
||||
|
||||
@TaskAction
|
||||
open fun generate() {
|
||||
val publicKeyFile = publicKeyOutput.get().asFile
|
||||
val privateKeyFile = privateKeyOutput.get().asFile
|
||||
if (!overwriteCertificates.get() && (publicKeyFile.exists() || privateKeyFile.exists())) {
|
||||
logger.warn("Keys already exist. Use 'overwriteCertificates' to overwrite them.")
|
||||
return
|
||||
}
|
||||
|
||||
// Generate ECDSA key pair using secp256r1 curve
|
||||
val keyPairGenerator = KeyPairGenerator.getInstance("EC")
|
||||
keyPairGenerator.initialize(ECGenParameterSpec("secp256r1"))
|
||||
val keyPair = keyPairGenerator.generateKeyPair()
|
||||
|
||||
// Write private key as PEM
|
||||
val privateKeyPem = StringWriter().use { writer ->
|
||||
PemWriter(writer).use { pemWriter ->
|
||||
pemWriter.writeObject(PemObject("EC PRIVATE KEY", keyPair.private.encoded))
|
||||
}
|
||||
writer.toString()
|
||||
}
|
||||
privateKeyFile.writeText(privateKeyPem)
|
||||
|
||||
// Write public key as PEM
|
||||
val publicKeyPem = StringWriter().use { writer ->
|
||||
PemWriter(writer).use { pemWriter ->
|
||||
pemWriter.writeObject(PemObject("PUBLIC KEY", keyPair.public.encoded))
|
||||
}
|
||||
writer.toString()
|
||||
}
|
||||
publicKeyFile.writeText(publicKeyPem)
|
||||
}
|
||||
}
|
||||
|
||||
tasks.register<GenerateCertificateTask>("generateGame") {
|
||||
description = "Generates a key pair for the login and game servers."
|
||||
privateKeyOutput.set(layout.buildDirectory.file("game.private.pem"))
|
||||
publicKeyOutput.set(layout.buildDirectory.file("game.public.pem"))
|
||||
}
|
||||
|
||||
tasks.register<GenerateCertificateTask>("generateChat") {
|
||||
description = "Generates a key pair for the chat server."
|
||||
privateKeyOutput.set(layout.buildDirectory.file("chat.private.pem"))
|
||||
publicKeyOutput.set(layout.buildDirectory.file("chat.public.pem"))
|
||||
}
|
||||
|
||||
tasks.register("generate") {
|
||||
group = "openmmo"
|
||||
dependsOn("generateGame", "generateChat")
|
||||
}
|
||||
Reference in new issue
Block a user