remove ursa, uses crypto rsa functionalities instead, and keypair for keys generation

This commit is contained in:
Romain Beaumont 2017-07-20 17:22:19 +02:00
parent 6246e0e644
commit 01c3c95cb4
No known key found for this signature in database
GPG key ID: DB60E388B3BCF286
4 changed files with 11 additions and 26 deletions

View file

@ -39,17 +39,14 @@
"buffer-equal": "^1.0.0",
"debug": "^2.2.0",
"endian-toggle": "^0.0.0",
"keypair": "^1.0.1",
"lodash.get": "^4.1.2",
"lodash.merge": "^4.3.0",
"minecraft-data": "^2.17.0",
"prismarine-nbt": "^1.0.0",
"protodef": "^1.5.1",
"readable-stream": "^2.0.5",
"ursa-purejs": "^0.0.3",
"uuid-1345": "^0.99.6",
"yggdrasil": "^1.0.0"
},
"optionalDependencies": {
"ursa": "^0.9.1"
}
}

View file

@ -2,7 +2,6 @@
const crypto = require('crypto');
const yggserver = require('yggdrasil').server({});
const ursa=require("../ursa");
const debug = require('debug')('minecraft-protocol');
module.exports = function(client, options) {
@ -42,9 +41,9 @@ module.exports = function(client, options) {
}
function sendEncryptionKeyResponse() {
const pubKey = mcPubKeyToURsa(packet.publicKey);
const encryptedSharedSecretBuffer = pubKey.encrypt(sharedSecret, undefined, undefined, ursa.RSA_PKCS1_PADDING);
const encryptedVerifyTokenBuffer = pubKey.encrypt(packet.verifyToken, undefined, undefined, ursa.RSA_PKCS1_PADDING);
const pubKey = mcPubKeyToPem(packet.publicKey);
const encryptedSharedSecretBuffer = crypto.publicEncrypt({key:pubKey,padding:crypto.constants.RSA_PKCS1_PADDING},sharedSecret);
const encryptedVerifyTokenBuffer = crypto.publicEncrypt({key:pubKey,padding:crypto.constants.RSA_PKCS1_PADDING},packet.verifyToken);
client.write('encryption_begin', {
sharedSecret: encryptedSharedSecretBuffer,
verifyToken: encryptedVerifyTokenBuffer
@ -55,7 +54,7 @@ module.exports = function(client, options) {
}
};
function mcPubKeyToURsa(mcPubKeyBuffer) {
function mcPubKeyToPem(mcPubKeyBuffer) {
let pem = "-----BEGIN PUBLIC KEY-----\n";
let base64PubKey = mcPubKeyBuffer.toString('base64');
const maxLineLength = 65;
@ -64,5 +63,5 @@ function mcPubKeyToURsa(mcPubKeyBuffer) {
base64PubKey = base64PubKey.substring(maxLineLength);
}
pem += "-----END PUBLIC KEY-----\n";
return ursa.createPublicKey(pem, 'utf8');
return pem;
}

View file

@ -1,6 +1,5 @@
'use strict';
const ursa=require("./ursa");
const crypto = require('crypto');
const yggserver = require('yggdrasil').server({});
const states = require("./states");
@ -9,6 +8,7 @@ const Server = require('./server');
const UUID = require('uuid-1345');
const endianToggle = require('endian-toggle');
const pluginChannels = require('./client/pluginChannels');
const keypair = require('keypair');
module.exports=createServer;
@ -36,7 +36,7 @@ function createServer(options) {
const mcData=require("minecraft-data")(optVersion);
const version = mcData.version;
const serverKey = ursa.generatePrivateKey(1024);
const serverKey = keypair({bits:1024});
const server = new Server(version.minecraftVersion,options.customPackets);
server.motd = options.motd || "A Minecraft server";
@ -175,7 +175,7 @@ function createServer(options) {
if(needToVerify) {
serverId = crypto.randomBytes(4).toString('hex');
client.verifyToken = crypto.randomBytes(4);
const publicKeyStrArr = serverKey.toPublicPem("utf8").split("\n");
const publicKeyStrArr = serverKey.public.split("\n");
let publicKeyStr = "";
for(let i = 1; i < publicKeyStrArr.length - 2; i++) {
publicKeyStr += publicKeyStrArr[i]
@ -210,12 +210,12 @@ function createServer(options) {
function onEncryptionKeyResponse(packet) {
let sharedSecret;
try {
const verifyToken = serverKey.decrypt(packet.verifyToken, undefined, undefined, ursa.RSA_PKCS1_PADDING);
const verifyToken = crypto.privateDecrypt({key:serverKey.private,padding:crypto.constants.RSA_PKCS1_PADDING},packet.verifyToken);
if(!bufferEqual(client.verifyToken, verifyToken)) {
client.end('DidNotEncryptVerifyTokenProperly');
return;
}
sharedSecret = serverKey.decrypt(packet.sharedSecret, undefined, undefined, ursa.RSA_PKCS1_PADDING);
sharedSecret = crypto.privateDecrypt({key:serverKey.private,padding:crypto.constants.RSA_PKCS1_PADDING},packet.sharedSecret);
} catch(e) {
client.end('DidNotEncryptVerifyTokenProperly');
return;

View file

@ -1,11 +0,0 @@
'use strict';
let ursa;
try {
ursa = require("ursa");
} catch(e) {
console.log("You are using a pure-javascript implementation of RSA.");
console.log("Your performance might be subpar. Please consider installing URSA");
ursa = require("ursa-purejs");
}
module.exports=ursa;