Skip to content

Commit

Permalink
fix: use node-rsa for decryption for higher node compatibility (#1319)
Browse files Browse the repository at this point in the history
Co-authored-by: extremeheat <[email protected]>
  • Loading branch information
jacobk999 and extremeheat authored Jan 19, 2025
1 parent 556b9dd commit c879d0e
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/server/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const chatPlugin = require('./chat')
const { concat } = require('../transforms/binaryStream')
const { mojangPublicKeyPem } = require('./constants')
const debug = require('debug')('minecraft-protocol')
const NodeRSA = require('node-rsa')
const nbt = require('prismarine-nbt')

/**
Expand Down Expand Up @@ -112,6 +113,9 @@ module.exports = function (client, server, options) {
}
}

const keyRsa = new NodeRSA(server.serverKey.exportKey('pkcs1'), 'private', { encryptionScheme: 'pkcs1' })
keyRsa.setOptions({ environment: 'browser' })

if (packet.hasVerifyToken === false) {
// 1.19, hasVerifyToken is set and equal to false IF chat signing is enabled
// This is the default action starting in 1.19.1.
Expand All @@ -123,10 +127,7 @@ module.exports = function (client, server, options) {
} else {
const encryptedToken = packet.hasVerifyToken ? packet.crypto.verifyToken : packet.verifyToken
try {
const decryptedToken = crypto.privateDecrypt({
key: server.serverKey.exportKey(),
padding: crypto.constants.RSA_PKCS1_PADDING
}, encryptedToken)
const decryptedToken = keyRsa.decrypt(encryptedToken)

if (!client.verifyToken.equals(decryptedToken)) {
client.end('DidNotEncryptVerifyTokenProperly')
Expand All @@ -137,13 +138,9 @@ module.exports = function (client, server, options) {
return
}
}

let sharedSecret
try {
sharedSecret = crypto.privateDecrypt({
key: server.serverKey.exportKey(),
padding: crypto.constants.RSA_PKCS1_PADDING
}, packet.sharedSecret)
sharedSecret = keyRsa.decrypt(packet.sharedSecret)
} catch (e) {
client.end('DidNotEncryptVerifyTokenProperly')
return
Expand Down

0 comments on commit c879d0e

Please sign in to comment.