Skip to content

Commit

Permalink
Create cipher object
Browse files Browse the repository at this point in the history
  • Loading branch information
MacaylaMarvelous81 committed Aug 18, 2024
1 parent 511ab2c commit d598f45
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 28 deletions.
22 changes: 5 additions & 17 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,9 @@ const wss = new WebSocketServer({
return protocols.has('com.microsoft.minecraft.wsencrypt') ? 'com.microsoft.minecraft.wsencrypt' : false;
}
});
const key = await new Promise((resolve, reject) => {
crypto.generateKeyPair('ec', {
namedCurve: 'P-384',
publicKeyEncoding: {
type: 'spki',
format: 'der'
},
}, (err, publicKey, privateKey) => {
if (err) {
reject(err);
} else {
resolve({ publicKey, privateKey });
}
})
});
const ecdh = crypto.createECDH('secp384r1');
ecdh.generateKeys();

const salt = crypto.randomBytes(16);

console.log(`Server started on port ${ port }`);
Expand All @@ -42,9 +30,9 @@ wss.on('connection', async (ws) => {

sendAllRenderers('connection');

const client = new Client(ws);
const client = new Client(ws, ecdh);

await client.enableEncryption(key.publicKey, salt);
await client.enableEncryption(salt);
client.subscribeEvent('PlayerDied');
client.subscribeEvent('ItemUsed');
client.subscribeEvent('PlayerMessage');
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"main": "app.js",
"dependencies": {
"blockly": "^11.1.1",
"eckey-utils": "^0.7.14",
"get-port": "^7.1.0",
"sval": "^0.5.2",
"vex-dialog": "^1.1.0",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 19 additions & 11 deletions websocket/client.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import crypto from 'node:crypto';
import { buildCommandRequest, buildSubscription } from './requests.js';
import ecKeyUtils from 'eckey-utils';

export class Client {
static clients = [];

#ws;
#serverPrivateKey;
#ecdh;
#commandRequests = {};
#gameEventHandlers = [];
#playerKey;
#sharedSecret;
#secretKey;
#cipher;

constructor(ws, privateKey) {
constructor(ws, ecdh) {
this.#ws = ws;
this.#serverPrivateKey = privateKey;
this.#ecdh = ecdh;

ws.on('message', this.#handleMessage.bind(this));

Expand All @@ -28,19 +31,24 @@ export class Client {
this.#ws.send(JSON.stringify(buildSubscription(eventName, crypto.randomUUID())));
}

async enableEncryption(pubkey, salt) {
async enableEncryption(salt) {
const encodedKey = this.#ecdh.getPublicKey('base64');
const encodedSalt = salt.toString('base64');
const body = await this.execute(`enableencryption "${ encodedKey }" "${ encodedSalt }"`);

this.#playerKey = crypto.createPublicKey({
const pemKey = crypto.createPublicKey({
key: Buffer.from(body.publicKey, 'base64'),
format: 'der',
type: 'spki'
type: 'spki',
format: 'der'
}).export({
type: 'spki',
format: 'pem'
});
this.#playerKey = ecKeyUtils.parsePem(pemKey).publicKey;

const ecdh = crypto.createECDH('P-384');
ecdh.setPrivateKey(this.#serverPrivateKey);

this.#sharedSecret = ecdh.computeSecret(this.#playerKey);
this.#sharedSecret = this.#ecdh.computeSecret(this.#playerKey);
this.#secretKey = crypto.hash('sha256', Buffer.concat([ salt, this.#sharedSecret ]));
this.#cipher = crypto.createCipheriv('aes-256-cbc', this.#secretKey, this.#secretKey.slice(0, 16));
}

execute(command) {
Expand Down

0 comments on commit d598f45

Please sign in to comment.