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

Tlsfix #45

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.4.0 - 20200507
* Updates for newer NodeJS versions (>= 9.x, migrate deprecated functions to new ones)

## 0.2.1 - 20150708
* Minor bug fix

Expand Down
4 changes: 2 additions & 2 deletions lib/asn1/univ.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ Enumerate.prototype.encode = function(encoder) {
*/
function OctetString(value) {
spec.Asn1Spec.call(this, new spec.Asn1Tag(spec.TagClass.Universal, spec.TagFormat.Primitive, UniversalTag.OctetString));
this.value = value || new Buffer(0);
this.value = value || Buffer.alloc(0);
}

inherits(OctetString, spec.Asn1Spec);
Expand Down Expand Up @@ -241,7 +241,7 @@ OctetString.prototype.encode = function(encoder) {
*/
function ObjectIdentifier(value) {
spec.Asn1Spec.call(this, new spec.Asn1Tag(spec.TagClass.Universal, spec.TagFormat.Primitive, UniversalTag.ObjectIdentifier));
this.value = value || new Buffer(5);
this.value = value || Buffer.alloc(5);
}

inherits(ObjectIdentifier, spec.Asn1Spec);
Expand Down
38 changes: 12 additions & 26 deletions lib/core/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ var inherits = require('util').inherits;
var fs = require('fs');
var type = require('./type');
var log = require('./log');
var starttls = require('starttls');
var tls = require('tls');
var crypto = require('crypto');
var events = require('events');

/**
Expand All @@ -32,7 +30,7 @@ var events = require('events');
*/
function BufferLayer(socket) {
//for ssl connection
this.securePair = null;
this.secureSocket = null;
this.socket = socket;

var self = this;
Expand Down Expand Up @@ -100,8 +98,8 @@ BufferLayer.prototype.recv = function(data) {
BufferLayer.prototype.send = function(data) {
var s = new type.Stream(data.size());
data.write(s);
if(this.securePair) {
this.securePair.cleartext.write(s.buffer);
if(this.secureSocket) {
this.secureSocket.write(s.buffer);
}
else {
this.socket.write(s.buffer);
Expand All @@ -118,21 +116,13 @@ BufferLayer.prototype.expect = function(expectedSize) {

/**
* Convert connection to TLS connection
* Use nodejs starttls module
* @param callback {func} when connection is done
*/
BufferLayer.prototype.startTLS = function(callback) {
var options = {
socket : this.socket,
pair : tls.createSecurePair(tls.createSecureContext(), false, false, false)
};
var self = this;
this.securePair = starttls(options, function(err) {
log.warn(err);
callback();
})
this.secureSocket = new tls.TLSSocket(this.socket);

this.securePair.cleartext.on('data', function(data) {
this.secureSocket.on('data', function(data) {
try {
self.recv(data);
}
Expand All @@ -143,6 +133,7 @@ BufferLayer.prototype.startTLS = function(callback) {
}).on('error', function (err) {
self.emit('error', err);
});
callback();
};

/**
Expand All @@ -153,20 +144,14 @@ BufferLayer.prototype.startTLS = function(callback) {
*/
BufferLayer.prototype.listenTLS = function(keyFilePath, crtFilePath, callback) {
var options = {
socket : this.socket,
pair : tls.createSecurePair(tls.createSecureContext({
key: fs.readFileSync(keyFilePath),
cert: fs.readFileSync(crtFilePath),
}), true, false, false)
server: true,
key: fs.readFileSync(keyFilePath),
cert: fs.readFileSync(crtFilePath),
};
var self = this;
this.securePair = starttls(options, function(err) {
log.warn(err);
self.cleartext = this.cleartext;
callback();
});
this.secureSocket = new tls.TLSSocket(this.socket, options);

this.securePair.cleartext.on('data', function(data) {
this.secureSocket.on('data', function(data) {
try {
self.recv(data);
}
Expand All @@ -177,6 +162,7 @@ BufferLayer.prototype.listenTLS = function(keyFilePath, crtFilePath, callback) {
}).on('error', function (err) {
self.emit('error', err);
});
callback();
};

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/core/rle.js

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

4 changes: 2 additions & 2 deletions lib/core/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function Stream(i) {
this.buffer = i;
}
else {
this.buffer = new Buffer(i || 8192);
this.buffer = Buffer.alloc(i || 8192);
}
}

Expand Down Expand Up @@ -397,7 +397,7 @@ inherits(UInt32Be, SingleType);
*/
function BinaryString(value, opt) {
Type.call(this, opt);
this.value = value || new Buffer("");
this.value = value || Buffer.from("");
}

//inherit from type
Expand Down
2 changes: 1 addition & 1 deletion lib/protocol/cert.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function rsaPublicKey(opt) {
modulus : new type.BinaryString(null, { readLength : new type.CallableValue(function() {
return self.keylen.value - 8;
}) }),
padding : new type.BinaryString(new Buffer(Array(8 + 1).join('\x00')), { readLength : new type.CallableValue(8) })
padding : new type.BinaryString(Buffer.from(Array(8 + 1).join('\x00')), { readLength : new type.CallableValue(8) })
};

return new type.Component(self, opt);
Expand Down
4 changes: 2 additions & 2 deletions lib/protocol/pdu/caps.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ function orderCapability(orders, opt) {

var self = {
__TYPE__ : CapsType.CAPSTYPE_ORDER,
terminalDescriptor : new type.BinaryString(new Buffer(Array(16 + 1).join('\x00'), 'binary'), {readLength : new type.CallableValue(16)}),
terminalDescriptor : new type.BinaryString(Buffer.from(Array(16 + 1).join('\x00'), 'binary'), {readLength : new type.CallableValue(16)}),
pad4octetsA : new type.UInt32Le(0),
desktopSaveXGranularity : new type.UInt16Le(1),
desktopSaveYGranularity : new type.UInt16Le(20),
Expand Down Expand Up @@ -353,7 +353,7 @@ function inputCapability(opt) {
// same value as gcc.ClientCoreSettings.keyboardFnKeys
keyboardFunctionKey : new type.UInt32Le(),
// same value as gcc.ClientCoreSettingrrs.imeFileName
imeFileName : new type.BinaryString(new Buffer(Array(64 + 1).join('\x00'), 'binary'), {readLength : new type.CallableValue(64)})
imeFileName : new type.BinaryString(Buffer.from(Array(64 + 1).join('\x00'), 'binary'), {readLength : new type.CallableValue(64)})
};

return new type.Component(self, opt);
Expand Down
6 changes: 3 additions & 3 deletions lib/protocol/pdu/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ function demandActivePDU(capabilities, opt) {
lengthCombinedCapabilities : new type.UInt16Le(function() {
return self.numberCapabilities.size() + self.pad2Octets.size() + self.capabilitySets.size();
}),
sourceDescriptor : new type.BinaryString(new Buffer('node-rdpjs', 'binary'), { readLength : new type.CallableValue(function() {
sourceDescriptor : new type.BinaryString(Buffer.from('node-rdpjs', 'binary'), { readLength : new type.CallableValue(function() {
return self.lengthSourceDescriptor.value
}) }),
numberCapabilities : new type.UInt16Le(function() {
Expand Down Expand Up @@ -426,7 +426,7 @@ function confirmActivePDU(capabilities, shareId, opt) {
lengthCombinedCapabilities : new type.UInt16Le(function() {
return self.numberCapabilities.size() + self.pad2Octets.size() + self.capabilitySets.size();
}),
sourceDescriptor : new type.BinaryString(new Buffer('rdpy', 'binary'), { readLength : new type.CallableValue(function() {
sourceDescriptor : new type.BinaryString(Buffer.from('rdpy', 'binary'), { readLength : new type.CallableValue(function() {
return self.lengthSourceDescriptor.value
}) }),
numberCapabilities : new type.UInt16Le(function() {
Expand Down Expand Up @@ -456,7 +456,7 @@ function deactiveAllPDU(opt) {
lengthSourceDescriptor : new type.UInt16Le(function() {
return self.sourceDescriptor.size();
}),
sourceDescriptor : new type.BinaryString(new Buffer('rdpy', 'binary'), { readLength : new type.CallableValue(function() {
sourceDescriptor : new type.BinaryString(Buffer.from('rdpy', 'binary'), { readLength : new type.CallableValue(function() {
self.lengthSourceDescriptor
}) })
};
Expand Down
12 changes: 6 additions & 6 deletions lib/protocol/pdu/lic.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ function productInformation() {
return self.pbCompanyName.size();
}),
// may contain "Microsoft Corporation" from server microsoft
pbCompanyName : new type.BinaryString(new Buffer('Microsoft Corporation', 'ucs2'), { readLength : new type.CallableValue(function() {
pbCompanyName : new type.BinaryString(Buffer.from('Microsoft Corporation', 'ucs2'), { readLength : new type.CallableValue(function() {
return self.cbCompanyName.value;
})}),
cbProductId : new type.UInt32Le(function() {
return self.pbProductId.size();
}),
// may contain "A02" from microsoft license server
pbProductId : new type.BinaryString(new Buffer('A02', 'ucs2'), { readLength : new type.CallableValue(function() {
pbProductId : new type.BinaryString(Buffer.from('A02', 'ucs2'), { readLength : new type.CallableValue(function() {
return self.cbProductId.value;
})})
};
Expand Down Expand Up @@ -183,7 +183,7 @@ function scopeList() {
function serverLicenseRequest(opt) {
var self = {
__TYPE__ : MessageType.LICENSE_REQUEST,
serverRandom : new type.BinaryString(new Buffer(Array(32 + 1).join('\x00')), { readLength : new type.CallableValue(32) } ),
serverRandom : new type.BinaryString(Buffer.from(Array(32 + 1).join('\x00')), { readLength : new type.CallableValue(32) } ),
productInfo : productInformation(),
keyExchangeList : licenseBinaryBlob(BinaryBlobType.BB_KEY_EXCHG_ALG_BLOB),
serverCertificate : licenseBinaryBlob(BinaryBlobType.BB_CERTIFICATE_BLOB),
Expand All @@ -205,7 +205,7 @@ function clientNewLicenseRequest(opt) {
// pure microsoft client ;-)
// http://msdn.microsoft.com/en-us/library/1040af38-c733-4fb3-acd1-8db8cc979eda#id10
platformId : new type.UInt32Le(0x04000000 | 0x00010000),
clientRandom : new type.BinaryString(new Buffer(Array(32 + 1).join('\x00')), { readLength : new type.CallableValue(32) }),
clientRandom : new type.BinaryString(Buffer.from(Array(32 + 1).join('\x00')), { readLength : new type.CallableValue(32) }),
encryptedPreMasterSecret : licenseBinaryBlob(BinaryBlobType.BB_RANDOM_BLOB),
ClientUserName : licenseBinaryBlob(BinaryBlobType.BB_CLIENT_USER_NAME_BLOB),
ClientMachineName : licenseBinaryBlob(BinaryBlobType.BB_CLIENT_MACHINE_NAME_BLOB)
Expand All @@ -224,7 +224,7 @@ function serverPlatformChallenge(opt) {
__TYPE__ : MessageType.PLATFORM_CHALLENGE,
connectFlags : new type.UInt32Le(),
encryptedPlatformChallenge : licenseBinaryBlob(BinaryBlobType.BB_ANY_BLOB),
MACData : new type.BinaryString(new Buffer(Array(16 + 1).join('\x00')), { readLength : new type.CallableValue(16) })
MACData : new type.BinaryString(Buffer.from(Array(16 + 1).join('\x00')), { readLength : new type.CallableValue(16) })
};

return new type.Component(self, opt);
Expand All @@ -240,7 +240,7 @@ function clientPLatformChallengeResponse(opt) {
__TYPE__ : MessageType.PLATFORM_CHALLENGE_RESPONSE,
encryptedPlatformChallengeResponse : licenseBinaryBlob(BinaryBlobType.BB_DATA_BLOB),
encryptedHWID : licenseBinaryBlob(BinaryBlobType.BB_DATA_BLOB),
MACData : new type.BinaryString(new Buffer(Array(16 + 1).join('\x00'), 'binary'), { readLength : new type.CallableValue(16) })
MACData : new type.BinaryString(Buffer.from(Array(16 + 1).join('\x00'), 'binary'), { readLength : new type.CallableValue(16) })
};

return new type.Component(self, opt);
Expand Down
37 changes: 18 additions & 19 deletions lib/protocol/pdu/sec.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ function finalHash (key, random1, random2) {
* @returns {Buffer}
*/
function masterSecret (secret, random1, random2) {
var sh1 = saltedHash(new Buffer('A'), secret, random1, random2);
var sh2 = saltedHash(new Buffer('BB'), secret, random1, random2);
var sh3 = saltedHash(new Buffer('CCC'), secret, random1, random2);
var sh1 = saltedHash(Buffer.from('A'), secret, random1, random2);
var sh2 = saltedHash(Buffer.from('BB'), secret, random1, random2);
var sh3 = saltedHash(Buffer.from('CCC'), secret, random1, random2);

var ms = new Buffer(sh1.length + sh2.length + sh3.length);
var ms = Buffer.alloc(sh1.length + sh2.length + sh3.length);
sh1.copy(ms);
sh2.copy(ms, sh1.length);
sh3.copy(ms, sh1.length + sh2.length);
Expand All @@ -160,10 +160,10 @@ function masterSecret (secret, random1, random2) {
* @returns {Buffer}
*/
function macData(macSaltKey, data) {
var salt1 = new Buffer(40);
var salt1 = Buffer.alloc(40);
salt1.fill(0x36);

var salt2 = new Buffer(48);
var salt2 = Buffer.alloc(48);
salt2.fill(0x5c);

var dataLength = new type.UInt32Le(data.length).toStream().buffer;
Expand Down Expand Up @@ -207,19 +207,19 @@ function rdpInfos(extendedInfoConditional) {
cbWorkingDir : new type.UInt16Le(function() {
return self.workingDir.size() - 2;
}),
domain : new type.BinaryString(new Buffer('\x00', 'ucs2'),{ readLength : new type.CallableValue(function() {
domain : new type.BinaryString(Buffer.from('\x00', 'ucs2'),{ readLength : new type.CallableValue(function() {
return self.cbDomain.value + 2;
})}),
userName : new type.BinaryString(new Buffer('\x00', 'ucs2'), { readLength : new type.CallableValue(function() {
userName : new type.BinaryString(Buffer.from('\x00', 'ucs2'), { readLength : new type.CallableValue(function() {
return self.cbUserName.value + 2;
})}),
password : new type.BinaryString(new Buffer('\x00', 'ucs2'), { readLength : new type.CallableValue(function () {
password : new type.BinaryString(Buffer.from('\x00', 'ucs2'), { readLength : new type.CallableValue(function () {
return self.cbPassword.value + 2;
})}),
alternateShell : new type.BinaryString(new Buffer('\x00', 'ucs2'), { readLength : new type.CallableValue(function() {
alternateShell : new type.BinaryString(Buffer.from('\x00', 'ucs2'), { readLength : new type.CallableValue(function() {
return self.cbAlternateShell.value + 2;
})}),
workingDir : new type.BinaryString(new Buffer('\x00', 'ucs2'), { readLength : new type.CallableValue(function() {
workingDir : new type.BinaryString(Buffer.from('\x00', 'ucs2'), { readLength : new type.CallableValue(function() {
return self.cbWorkingDir.value + 2;
})}),
extendedInfo : rdpExtendedInfos({ conditional : extendedInfoConditional })
Expand All @@ -239,16 +239,16 @@ function rdpExtendedInfos(opt) {
cbClientAddress : new type.UInt16Le(function() {
return self.clientAddress.size();
}),
clientAddress : new type.BinaryString(new Buffer('\x00', 'ucs2'),{ readLength : new type.CallableValue(function() {
clientAddress : new type.BinaryString(Buffer.from('\x00', 'ucs2'),{ readLength : new type.CallableValue(function() {
return self.cbClientAddress;
}) }),
cbClientDir : new type.UInt16Le(function() {
return self.clientDir.size();
}),
clientDir : new type.BinaryString(new Buffer('\x00', 'ucs2'), { readLength : new type.CallableValue(function() {
clientDir : new type.BinaryString(Buffer.from('\x00', 'ucs2'), { readLength : new type.CallableValue(function() {
return self.cbClientDir;
}) }),
clientTimeZone : new type.BinaryString(new Buffer(Array(172 + 1).join("\x00"))),
clientTimeZone : new type.BinaryString(Buffer.from(Array(172 + 1).join("\x00"))),
clientSessionId : new type.UInt32Le(),
performanceFlags : new type.UInt32Le()
};
Expand Down Expand Up @@ -399,7 +399,7 @@ Client.prototype.sendInfoPkt = function() {
};

function reverse(buffer) {
var result = new Buffer(buffer.length);
var result = Buffer.alloc(buffer.length);
for(var i = 0; i < buffer.length; i++) {
result.writeUInt8(buffer.readUInt8(buffer.length - 1 - i), i);
}
Expand Down Expand Up @@ -431,13 +431,12 @@ Client.prototype.sendClientNewLicenseRequest = function(licenseRequest) {
request.obj.clientRandom.value = clientRandom;

var preMasterSecretEncrypted = reverse(rsa.encrypt(reverse(preMasterSecret), publicKey));
var preMasterSecretEncryptedPadded = new Buffer(preMasterSecretEncrypted.length + 8);
preMasterSecretEncryptedPadded.fill(0);
var preMasterSecretEncryptedPadded = Buffer.alloc(preMasterSecretEncrypted.length + 8);
preMasterSecretEncrypted.copy(preMasterSecretEncryptedPadded);
request.obj.encryptedPreMasterSecret.obj.blobData.value = preMasterSecretEncryptedPadded;

request.obj.ClientMachineName.obj.blobData.value = this.infos.obj.userName.value;
request.obj.ClientUserName.obj.blobData.value = new Buffer(this.machineName + '\x00');
request.obj.ClientUserName.obj.blobData.value = Buffer.from(this.machineName + '\x00');

this.sendFlagged(SecurityFlag.SEC_LICENSE_PKT, lic.licensePacket(request));
};
Expand All @@ -460,7 +459,7 @@ Client.prototype.sendClientChallengeResponse = function(platformChallenge) {
response.obj.encryptedPlatformChallengeResponse.obj.blobData.value = serverEncryptedChallenge;
response.obj.encryptedHWID.obj.blobData.value = crypto.createCipheriv('rc4', this.licenseKey, '').update(hwid);

var sig = new Buffer(serverChallenge.length + hwid.length);
var sig = Buffer.alloc(serverChallenge.length + hwid.length);
serverChallenge.copy(sig);
hwid.copy(sig, serverChallenge.length);
response.obj.MACData.value = macData(this.licenseMacSalt, sig);
Expand Down
6 changes: 3 additions & 3 deletions lib/protocol/rdp.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ function RdpClient(config) {

// credentials
if (config.domain) {
this.sec.infos.obj.domain.value = new Buffer(config.domain + '\x00', 'ucs2');
this.sec.infos.obj.domain.value = Buffer.from(config.domain + '\x00', 'ucs2');
}
if (config.userName) {
this.sec.infos.obj.userName.value = new Buffer(config.userName + '\x00', 'ucs2');
this.sec.infos.obj.userName.value = Buffer.from(config.userName + '\x00', 'ucs2');
}
if (config.password) {
this.sec.infos.obj.password.value = new Buffer(config.password + '\x00', 'ucs2');
this.sec.infos.obj.password.value = Buffer.from(config.password + '\x00', 'ucs2');
}

if (config.enablePerf) {
Expand Down
Loading