Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use node-rsa for decryption for higher node compatibility #1319

Merged
merged 3 commits into from
Jan 19, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')

/**
* @param {import('../index').Client} client
Expand Down Expand Up @@ -111,6 +112,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 @@ -122,10 +126,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 @@ -136,13 +137,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
Loading