Base64 encoding of meshid/nodeid in the database.

Ylian Saint-Hilaire committed Oct 15, 2017 at 17:36 UTC ace40464154046d6942da5cdd2036d2ebbb74968
7 files changed +47 -68
meshagent.js
+3 -3
@@ -179,7 +179,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
179 obj.unauth = {};
180 obj.unauth.nodeCert = null;
181 try { obj.unauth.nodeCert = obj.forge.pki.certificateFromAsn1(obj.forge.asn1.fromDer(msg.substring(4, 4 + certlen))); } catch (e) { return; }
182 - obj.unauth.nodeid = obj.forge.pki.getPublicKeyFingerprint(obj.unauth.nodeCert.publicKey, { encoding: 'hex', md: obj.forge.md.sha384.create() });
182 + obj.unauth.nodeid = new Buffer(obj.forge.pki.getPublicKeyFingerprint(obj.unauth.nodeCert.publicKey, { md: obj.forge.md.sha384.create() }).data, 'binary').toString('base64').replace(/\+/g, '@').replace(/\//g, '$');
183
184 // Check the agent signature if we can
185 if (obj.agentnonce == null) { obj.unauthsign = msg.substring(4 + certlen); } else { if (processAgentSignature(msg.substring(4 + certlen)) == false) { console.log('Bad Agent Signature'); obj.close(); return; } }
@@ -196,7 +196,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
196 obj.agentInfo.agentId = obj.common.ReadInt(msg, 6);
197 obj.agentInfo.agentVersion = obj.common.ReadInt(msg, 10);
198 obj.agentInfo.platformType = obj.common.ReadInt(msg, 14);
199 - obj.meshid = obj.common.rstr2hex(msg.substring(18, 66)).toUpperCase();
199 + obj.meshid = new Buffer(msg.substring(18, 66), 'binary').toString('base64').replace(/\+/g, '@').replace(/\//g, '$');;
200 obj.agentInfo.capabilities = obj.common.ReadInt(msg, 66);
201 var computerNameLen = obj.common.ReadShort(msg, 70);
202 obj.agentInfo.computerName = msg.substring(72, 72 + computerNameLen);
@@ -339,7 +339,7 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
339 if (obj.unauth.nodeCert.publicKey.verify(md.digest().bytes(), msg) == false) { return false; }
340
341 // Connection is a success, clean up
342 - obj.nodeid = obj.unauth.nodeid.toUpperCase();
342 + obj.nodeid = obj.unauth.nodeid;
343 obj.dbNodeKey = 'node/' + domain.id + '/' + obj.nodeid;
344 delete obj.nonce;
345 delete obj.agentnonce;
meshcentral.js
+2 -2
@@ -270,11 +270,11 @@ function CreateMeshCentralServer() {
270 while (obj.dbconfig.amtWsEventSecret == null) { process.nextTick(); }
271 var username = buf.toString('hex');
272 var nodeid = obj.args.getwspass;
273 - var pass = require('crypto').createHash('sha384').update(username.toLowerCase() + ":" + nodeid.toUpperCase() + ":" + obj.dbconfig.amtWsEventSecret).digest("base64").substring(0, 12).split("/").join("x").split("\\").join("x");
273 + var pass = require('crypto').createHash('sha384').update(username.toLowerCase() + ":" + nodeid + ":" + obj.dbconfig.amtWsEventSecret).digest("base64").substring(0, 12).split("/").join("x").split("\\").join("x");
274 console.log('--- Intel(r) AMT WSMAN eventing credentials ---');
275 console.log('Username: ' + username);
276 console.log('Password: ' + pass);
277 - console.log('Argument: ' + nodeid.toLowerCase());
277 + console.log('Argument: ' + nodeid);
278 process.exit();
279 });
280 } else {
meshrelay.js
-9
@@ -4,15 +4,6 @@
4 * @version v0.0.1
5 */
6
7 -// Construct a MeshRelay object, called upon connection
8 -module.exports.CreateMeshRelayKey = function (parent, func) {
9 - parent.crypto.randomBytes(48, function (err, buf) {
10 - var key = buf.toString('hex').toUpperCase() + ':' + Date.now();
11 - key += ':' + parent.crypto.createHmac('SHA384', parent.relayRandom).update(key).digest('hex');
12 - func(key);
13 - });
14 -}
15 -
7 module.exports.CreateMeshRelay = function (parent, ws, req) {
8 var obj = {};
9 obj.ws = ws;
meshscanner.js
+4 -4
@@ -17,7 +17,7 @@ module.exports.CreateMeshScanner = function (parent) {
17 var periodicScanTime = (60000 * 20); // Interval between scans, 20 minutes.
18 var membershipIPv4 = '239.255.255.235';
19 var membershipIPv6 = 'FF02:0:0:0:0:0:0:FE';
20 - obj.agentCertificatHashHex = parent.certificateOperations.forge.pki.getPublicKeyFingerprint(parent.certificateOperations.forge.pki.certificateFromPem(parent.certificates.agent.cert).publicKey, { md: parent.certificateOperations.forge.md.sha384.create(), encoding: 'hex' });
20 + obj.agentCertificatHashHex = parent.certificateOperations.forge.pki.getPublicKeyFingerprint(parent.certificateOperations.forge.pki.certificateFromPem(parent.certificates.agent.cert).publicKey, { md: parent.certificateOperations.forge.md.sha384.create(), encoding: 'hex' }).toUpperCase();
21 obj.error = 0;
22
23 // Get a list of IPv4 and IPv6 interface addresses
@@ -119,9 +119,9 @@ module.exports.CreateMeshScanner = function (parent) {
119 obj.start = function () {
120 if (obj.server4 != null) return;
121 var url = (parent.args.notls ? 'ws' : 'wss') + '://%s:' + parent.args.port + '/agent.ashx';
122 - obj.multicastPacket4 = Buffer.from("MeshCentral2|" + obj.agentCertificatHashHex.toUpperCase() + '|' + url, 'ascii');
122 + obj.multicastPacket4 = Buffer.from("MeshCentral2|" + obj.agentCertificatHashHex + '|' + url, 'ascii');
123 url = (parent.args.notls ? 'ws' : 'wss') + '://[%s]:' + parent.args.port + '/agent.ashx';
124 - obj.multicastPacket6 = Buffer.from("MeshCentral2|" + obj.agentCertificatHashHex.toUpperCase() + '|' + url, 'ascii');
124 + obj.multicastPacket6 = Buffer.from("MeshCentral2|" + obj.agentCertificatHashHex + '|' + url, 'ascii');
125 setupServers();
126 obj.mainTimer = setInterval(obj.performScan, periodicScanTime);
127 return obj;
@@ -150,7 +150,7 @@ module.exports.CreateMeshScanner = function (parent) {
150 // Called when a UDP packet is received from an agent.
151 function onUdpPacket(msg, info, server) {
152 //console.log('Received ' + msg.length + ' bytes from ' + info.address + ':' + info.port + ', on interface: ' + server.xxlocal + '.');
153 - if ((msg.length == 64) && (msg.toString('ascii') == obj.agentCertificatHashHex.toUpperCase())) {
153 + if ((msg.length == 64) && (msg.toString('ascii') == obj.agentCertificatHashHex)) {
154 if (server.xxtype == 4) { try { server.send(obj.multicastPacket4, 0, obj.multicastPacket4.length, info.port, info.address); } catch (e) { } }
155 if (server.xxtype == 6) { try { server.send(obj.multicastPacket6, 0, obj.multicastPacket6.length, info.port, info.address); } catch (e) { } }
156 }
mpsserver.js
+8 -8
@@ -108,11 +108,11 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
108 // This is a node where the MeshID is indicated within the CIRA certificate
109 var domainid = '', meshid;
110 var xx = socket.tag.clientCert.subject.O.split('/');
111 - if (xx.length == 1) { meshid = xx[0].toUpperCase(); } else { domainid = xx[0].toLowerCase(); meshid = xx[1].toUpperCase(); }
111 + if (xx.length == 1) { meshid = xx[0]; } else { domainid = xx[0].toLowerCase(); meshid = xx[1]; }
112
113 socket.tag.domainid = domainid;
114 socket.tag.meshid = 'mesh/' + domainid + '/' + meshid;
115 - socket.tag.nodeid = 'node/' + domainid + '/' + require('crypto').createHash('sha384').update(common.hex2rstr(socket.tag.clientCert.modulus, 'binary')).digest('hex').toUpperCase();
115 + socket.tag.nodeid = 'node/' + domainid + '/' + require('crypto').createHash('sha384').update(common.hex2rstr(socket.tag.clientCert.modulus, 'binary')).digest('base64').replace(/\+/g, '@').replace(/\//g, '$');
116 socket.tag.name = socket.tag.clientCert.subject.CN;
117 socket.tag.connectTime = Date.now();
118 socket.tag.host = '';
@@ -216,19 +216,19 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
216 // Check the CIRA password
217 if ((args.mpspass != undefined) && (password != args.mpspass)) { Debug(1, 'MPS:Incorrect password', username, password); SendUserAuthFail(socket); return -1; }
218
219 - // Check the CIRA username, which should be the HEX start of the MeshID.
220 - if (usernameLen != 16) { SendUserAuthFail(socket); return -1; }
219 + // Check the CIRA username, which should be the start of the MeshID.
220 + if (usernameLen != 16) { Debug(1, 'MPS:Username length not 16', username, password); SendUserAuthFail(socket); return -1; }
221 var meshIdStart = '/' + username;
222 - meshIdStart = meshIdStart.toUpperCase();
222 obj.db.GetAllType('mesh', function (err, docs) {
223 var mesh = null;
224 for (var i in docs) { if (docs[i]._id.indexOf(meshIdStart) > 0) { mesh = docs[i]; break; } }
226 - if (mesh == null) { SendUserAuthFail(socket); return -1; }
225 + if (mesh == null) { Debug(1, 'MPS:Mesh not found', username, password);SendUserAuthFail(socket); return -1; }
226
227 // Intel AMT GUID (socket.tag.SystemId) will be used at NodeID
229 - var systemid = socket.tag.SystemId.split('-').join('').toUpperCase();
228 + var systemid = socket.tag.SystemId.split('-').join('');
229 + var nodeid = new Buffer(systemid + systemid + systemid, 'hex').toString('base64').replace(/\+/g, '@').replace(/\//g, '$');
230 socket.tag.name = '';
231 - socket.tag.nodeid = 'node/' + mesh.domain + '/' + systemid + systemid + systemid; // Turn 16bit systemid guid into 48bit nodeid
231 + socket.tag.nodeid = 'node/' + mesh.domain + '/' + nodeid; // Turn 16bit systemid guid into 48bit nodeid that is base64 encoded
232 socket.tag.meshid = mesh._id;
233
234 obj.db.Get(socket.tag.nodeid, function (err, nodes) {
multiserver.js
+18 -18
@@ -33,7 +33,7 @@ module.exports.CreateMultiServer = function (parent, args) {
33 obj.retryBackoff = 0;
34 obj.connectHandler = null;
35 obj.webCertificatHash = obj.parent.parent.webserver.webCertificatHash;
36 - obj.agentCertificatHashHex = obj.parent.parent.webserver.agentCertificatHashHex;
36 + obj.agentCertificatHashBase64 = obj.parent.parent.webserver.agentCertificatHashBase64;
37 obj.agentCertificatAsn1 = obj.parent.parent.webserver.agentCertificatAsn1;
38 obj.peerServerId = null;
39 obj.authenticated = 0;
@@ -108,8 +108,8 @@ module.exports.CreateMultiServer = function (parent, args) {
108 var certlen = obj.common.ReadShort(msg, 2), serverCert = null;
109 try { serverCert = obj.forge.pki.certificateFromAsn1(obj.forge.asn1.fromDer(msg.substring(4, 4 + certlen))); } catch (e) { }
110 if (serverCert == null) { obj.parent.parent.debug(1, 'OutPeer: Invalid server certificate.'); disconnect(); return; }
111 - var serverid = obj.forge.pki.getPublicKeyFingerprint(serverCert.publicKey, { encoding: 'hex', md: obj.forge.md.sha384.create() });
112 - if (serverid !== obj.agentCertificatHashHex) { obj.parent.parent.debug(1, 'OutPeer: Server hash mismatch.'); disconnect(); return; }
111 + var serverid = new Buffer(obj.forge.pki.getPublicKeyFingerprint(serverCert.publicKey, { encoding: 'binary', md: obj.forge.md.sha384.create() }), 'binary').toString('base64').replace(/\+/g, '@').replace(/\//g, '$');
112 + if (serverid !== obj.agentCertificatHashBase64) { obj.parent.parent.debug(1, 'OutPeer: Server hash mismatch.'); disconnect(); return; }
113
114 // Server signature, verify it
115 var md = obj.forge.md.sha384.create();
@@ -121,20 +121,20 @@ module.exports.CreateMultiServer = function (parent, args) {
121 // Connection is a success, clean up
122 delete obj.nonce;
123 delete obj.servernonce;
124 - obj.serverCertHash = obj.common.rstr2hex(obj.serverCertHash).toLowerCase(); // Change this value to hex
124 + obj.serverCertHash = new Buffer(obj.serverCertHash, 'binary').toString('base64').replace(/\+/g, '@').replace(/\//g, '$'); // Change this value to base64
125 obj.connectionState |= 4;
126 obj.retryBackoff = 0; // Set backoff connection timer back to fast.
127 obj.parent.parent.debug(1, 'OutPeer ' + obj.serverid + ': Verified peer connection to ' + obj.url);
128
129 // Send information about our server to the peer
130 - if (obj.connectionState == 15) { obj.ws.send(JSON.stringify({ action: 'info', serverid: obj.parent.serverid, dbid: obj.parent.parent.db.identifier, key: obj.parent.serverKey.toString('hex'), serverCertHash: obj.parent.parent.webserver.webCertificatHashHex })); }
130 + if (obj.connectionState == 15) { obj.ws.send(JSON.stringify({ action: 'info', serverid: obj.parent.serverid, dbid: obj.parent.parent.db.identifier, key: obj.parent.serverKey.toString('hex'), serverCertHash: obj.parent.parent.webserver.webCertificatHashBase64 })); }
131 //if ((obj.connectionState == 15) && (obj.connectHandler != null)) { obj.connectHandler(1); }
132 break;
133 }
134 case 4: {
135 // Server confirmed authentication, we are allowed to send commands to the server
136 obj.connectionState |= 8;
137 - if (obj.connectionState == 15) { obj.ws.send(JSON.stringify({ action: 'info', serverid: obj.parent.serverid, dbid: obj.parent.parent.db.identifier, key: obj.parent.serverKey.toString('hex'), serverCertHash: obj.parent.parent.webserver.webCertificatHashHex })); }
137 + if (obj.connectionState == 15) { obj.ws.send(JSON.stringify({ action: 'info', serverid: obj.parent.serverid, dbid: obj.parent.parent.db.identifier, key: obj.parent.serverKey.toString('hex'), serverCertHash: obj.parent.parent.webserver.webCertificatHashBase64 })); }
138 //if ((obj.connectionState == 15) && (obj.connectHandler != null)) { obj.connectHandler(1); }
139 break;
140 }
@@ -183,7 +183,7 @@ module.exports.CreateMultiServer = function (parent, args) {
183 if ((command.serverid != null) && (command.dbid != null)) {
184 if (command.serverid == obj.parent.serverid) { console.log('ERROR: Same server ID, trying to peer with self. (' + obj.url + ', ' + command.serverid + ').'); return; }
185 if (command.dbid != obj.parent.parent.db.identifier) { console.log('ERROR: Database ID mismatch. Trying to peer to a server with the wrong database. (' + obj.url + ', ' + command.serverid + ').'); return; }
186 - if (obj.serverCertHash != command.serverCertHash) { console.log('ERROR: Outer certificate hash mismatch. (' + obj.url + ', ' + command.serverid + ').'); return; }
186 + if (obj.serverCertHash != command.serverCertHash) { console.log('ERROR: Outer certificate hash mismatch (2). (' + obj.url + ', ' + command.serverid + ').'); return; }
187 obj.peerServerId = command.serverid;
188 obj.peerServerKey = new Buffer(command.key, 'hex');
189 obj.authenticated = 3;
@@ -213,7 +213,7 @@ module.exports.CreateMultiServer = function (parent, args) {
213 obj.remoteaddr = obj.ws._socket.remoteAddress;
214 obj.receivedCommands = 0;
215 obj.webCertificatHash = obj.parent.parent.webserver.webCertificatHash;
216 - obj.agentCertificatHashHex = obj.parent.parent.webserver.agentCertificatHashHex;
216 + obj.agentCertificatHashBase64 = obj.parent.parent.webserver.agentCertificatHashBase64;
217 obj.agentCertificatAsn1 = obj.parent.parent.webserver.agentCertificatAsn1;
218 obj.infoSent = 0;
219 obj.peerServerId = null;
@@ -283,10 +283,10 @@ module.exports.CreateMultiServer = function (parent, args) {
283 obj.unauth = {};
284 obj.unauth.nodeCert = null;
285 try { obj.unauth.nodeCert = obj.forge.pki.certificateFromAsn1(obj.forge.asn1.fromDer(msg.substring(4, 4 + certlen))); } catch (e) { return; }
286 - obj.unauth.nodeid = obj.forge.pki.getPublicKeyFingerprint(obj.unauth.nodeCert.publicKey, { encoding: 'hex', md: obj.forge.md.sha384.create() });
286 + obj.unauth.nodeid = new Buffer(obj.forge.pki.getPublicKeyFingerprint(obj.unauth.nodeCert.publicKey, { encoding: 'binary', md: obj.forge.md.sha384.create() }), 'binary').toString('base64').replace(/\+/g, '@').replace(/\//g, '$');
287
288 // Check the peer server signature if we can
289 - if (obj.peernonce == null) { obj.unauthsign = msg.substring(4 + certlen); } else { if (processPeerSignature(msg.substring(4 + certlen)) == false) { disconnect(); return; } }
289 + if (obj.peernonce == null) { obj.unauthsign = msg.substring(4 + certlen); } else { if (processPeerSignature(msg.substring(4 + certlen)) == false) { obj.close(); return; } }
290 completePeerServerConnection();
291 }
292 else if (cmd == 3) {
@@ -313,7 +313,7 @@ module.exports.CreateMultiServer = function (parent, args) {
313 function completePeerServerConnection() {
314 if (obj.authenticated != 1) return;
315 obj.send(obj.common.ShortToStr(4));
316 - obj.send(JSON.stringify({ action: 'info', serverid: obj.parent.serverid, dbid: obj.parent.parent.db.identifier, key: obj.parent.serverKey.toString('hex'), serverCertHash: obj.parent.parent.webserver.webCertificatHashHex }));
316 + obj.send(JSON.stringify({ action: 'info', serverid: obj.parent.serverid, dbid: obj.parent.parent.db.identifier, key: obj.parent.serverKey.toString('hex'), serverCertHash: obj.parent.parent.webserver.webCertificatHashBase64 }));
317 obj.authenticated = 2;
318 }
319
@@ -324,10 +324,10 @@ module.exports.CreateMultiServer = function (parent, args) {
324 md.update(obj.nonce, 'binary');
325 md.update(obj.peernonce, 'binary');
326 if (obj.unauth.nodeCert.publicKey.verify(md.digest().bytes(), msg) == false) { return false; }
327 - if (obj.unauth.nodeid !== obj.agentCertificatHashHex) { return false; }
327 + if (obj.unauth.nodeid !== obj.agentCertificatHashBase64) { return false; }
328
329 // Connection is a success, clean up
330 - obj.nodeid = obj.unauth.nodeid.toUpperCase();
330 + obj.nodeid = obj.unauth.nodeid;
331 delete obj.nonce;
332 delete obj.peernonce;
333 delete obj.unauth;
@@ -396,7 +396,7 @@ module.exports.CreateMultiServer = function (parent, args) {
396 obj.decodeCookie = function (cookie, key) {
397 try {
398 if (key == null) { key = obj.serverKey; }
399 - cookie = new Buffer(cookie.replace(/\@/g, '+').replace(/\$/g, '/'), 'base64'); // HEX: This is not an efficient split, but it's very compatible.
399 + cookie = new Buffer(cookie.replace(/\@/g, '+').replace(/\$/g, '/'), 'base64');
400 var decipher = obj.crypto.createDecipheriv('aes-256-gcm', key, cookie.slice(0, 12));
401 decipher.setAuthTag(cookie.slice(12, 16));
402 var o = JSON.parse(decipher.update(cookie.slice(28), 'binary', 'utf8') + decipher.final('utf8'));
@@ -612,7 +612,7 @@ module.exports.CreateMultiServer = function (parent, args) {
612 peerTunnel.ws2.on('error', function (error) { peerTunnel.parent.parent.debug(1, 'FTunnel ' + obj.serverid + ': Connection error'); peerTunnel.close(); });
613
614 // If the peer server web socket is closed, clean up.
615 - peerTunnel.ws2.on('close', function (req) { peerTunnel.parent.parent.debug(1, 'FTunnel disconnect ' + peerTunnel.nodeid); peerTunnel.close(); });
615 + peerTunnel.ws2.on('close', function (req) { peerTunnel.parent.parent.debug(1, 'FTunnel disconnect ' + peerTunnel.serverid); peerTunnel.close(); });
616
617 // If a message is received from the peer, Peer ---> Browser (TODO: Pipe this?)
618 peerTunnel.ws2.on('message', function (msg) { try { peerTunnel.ws2.pause(); peerTunnel.ws1.send(msg, function () { peerTunnel.ws2.resume(); }); } catch (e) { } });
@@ -623,10 +623,10 @@ module.exports.CreateMultiServer = function (parent, args) {
623
624 // Get the peer server's certificate and compute the server public key hash
625 var serverCert = obj.forge.pki.certificateFromAsn1(obj.forge.asn1.fromDer(peerTunnel.ws2._socket.getPeerCertificate().raw.toString('binary')));
626 - var serverCertHashHex = obj.forge.pki.getPublicKeyFingerprint(serverCert.publicKey, { encoding: 'hex', md: obj.forge.md.sha384.create() });
626 + var serverCertHashHex = new Buffer(obj.forge.pki.getPublicKeyFingerprint(serverCert.publicKey, { encoding: 'binary', md: obj.forge.md.sha384.create() }), 'binary').toString('base64').replace(/\+/g, '@').replace(/\//g, '$');
627
628 // Check if the peer certificate is the expected one for this serverid
629 - if (obj.peerServers[serverid] == null || obj.peerServers[serverid].serverCertHash != serverCertHashHex) { console.log('ERROR: Outer certificate hash mismatch. (' + peerTunnel.url + ', ' + peerTunnel.serverid + ').'); peerTunnel.close(); return; }
629 + if (obj.peerServers[serverid] == null || obj.peerServers[serverid].serverCertHash != serverCertHashHex) { console.log('ERROR: Outer certificate hash mismatch (1). (' + peerTunnel.url + ', ' + peerTunnel.serverid + ').'); peerTunnel.close(); return; }
630
631 // Connection accepted, resume the web socket to start the data flow
632 peerTunnel.ws1.resume();
@@ -639,7 +639,7 @@ module.exports.CreateMultiServer = function (parent, args) {
639 peerTunnel.ws1.on('error', function (err) { peerTunnel.close(); });
640
641 // If the web socket is closed, close the associated TCP connection.
642 - peerTunnel.ws1.on('close', function (req) { peerTunnel.parent.parent.debug(1, 'FTunnel disconnect ' + peerTunnel.nodeid); peerTunnel.close(); });
642 + peerTunnel.ws1.on('close', function (req) { peerTunnel.parent.parent.debug(1, 'FTunnel disconnect ' + peerTunnel.serverid); peerTunnel.close(); });
643 }
644
645 // Disconnect both sides of the tunnel
webserver.js
+12 -24
@@ -70,8 +70,8 @@ module.exports.CreateWebServer = function (parent, db, args, secret, certificate
70
71 // Perform hash on web certificate and agent certificate
72 obj.webCertificatHash = parent.certificateOperations.forge.pki.getPublicKeyFingerprint(parent.certificateOperations.forge.pki.certificateFromPem(obj.certificates.web.cert).publicKey, { md: parent.certificateOperations.forge.md.sha384.create(), encoding: 'binary' });
73 - obj.webCertificatHashHex = parent.certificateOperations.forge.pki.getPublicKeyFingerprint(parent.certificateOperations.forge.pki.certificateFromPem(obj.certificates.web.cert).publicKey, { md: parent.certificateOperations.forge.md.sha384.create(), encoding: 'hex' });
74 - obj.agentCertificatHashHex = parent.certificateOperations.forge.pki.getPublicKeyFingerprint(parent.certificateOperations.forge.pki.certificateFromPem(obj.certificates.agent.cert).publicKey, { md: parent.certificateOperations.forge.md.sha384.create(), encoding: 'hex' });
73 + obj.webCertificatHashBase64 = new Buffer(parent.certificateOperations.forge.pki.getPublicKeyFingerprint(parent.certificateOperations.forge.pki.certificateFromPem(obj.certificates.web.cert).publicKey, { md: parent.certificateOperations.forge.md.sha384.create(), encoding: 'binary' }), 'binary').toString('base64').replace(/\+/g, '@').replace(/\//g, '$');
74 + obj.agentCertificatHashBase64 = new Buffer(parent.certificateOperations.forge.pki.getPublicKeyFingerprint(parent.certificateOperations.forge.pki.certificateFromPem(obj.certificates.agent.cert).publicKey, { md: parent.certificateOperations.forge.md.sha384.create(), encoding: 'binary' }), 'binary').toString('base64').replace(/\+/g, '@').replace(/\//g, '$');
75 obj.agentCertificatAsn1 = parent.certificateOperations.forge.asn1.toDer(parent.certificateOperations.forge.pki.certificateToAsn1(parent.certificateOperations.forge.pki.certificateFromPem(parent.certificates.agent.cert))).getBytes();
76
77 // Main lists
@@ -1294,7 +1294,7 @@ module.exports.CreateWebServer = function (parent, db, args, secret, certificate
1294 if ((command.meshtype == 1) || (command.meshtype == 2)) {
1295 // Create a type 1 agent-less Intel AMT mesh.
1296 obj.crypto.randomBytes(48, function (err, buf) {
1297 - var meshid = 'mesh/' + domain.id + '/' + buf.toString('hex').toUpperCase();
1297 + var meshid = 'mesh/' + domain.id + '/' + buf.toString('base64').replace(/\+/g, '@').replace(/\//g, '$');;
1298 var links = {}
1299 links[user._id] = { name: user.name, rights: 0xFFFFFFFF };
1300 var mesh = { type: 'mesh', _id: meshid, name: command.meshname, mtype: command.meshtype, desc: command.desc, domain: domain.id, links: links };
@@ -1449,7 +1449,7 @@ module.exports.CreateWebServer = function (parent, db, args, secret, certificate
1449 // Create a new nodeid
1450 obj.crypto.randomBytes(48, function (err, buf) {
1451 // create the new node
1452 - var nodeid = 'node/' + domain.id + '/' + buf.toString('hex').toUpperCase();
1452 + var nodeid = 'node/' + domain.id + '/' + buf.toString('base64').replace(/\+/g, '@').replace(/\//g, '$');;
1453 var device = { type: 'node', mtype: 1, _id: nodeid, meshid: command.meshid, name: command.devicename, host: command.hostname, domain: domain.id, intelamt: { user: command.amtusername, pass: command.amtpassword, tls: parseInt(command.amttls) } };
1454 obj.db.Set(device);
1455
@@ -1789,7 +1789,7 @@ module.exports.CreateWebServer = function (parent, db, args, secret, certificate
1789 // Completed event read, let get the argument that must contain the nodeid
1790 var i = eventData.indexOf('<m:arg xmlns:m="http://x.com">');
1791 if (i > 0) {
1792 - var nodeid = eventData.substring(i + 30, i + 30 + 64).toUpperCase();
1792 + var nodeid = eventData.substring(i + 30, i + 30 + 64);
1793 if (nodeid.length == 64) {
1794 var nodekey = 'node/' + domain.id + '/' + nodeid;
1795
@@ -1940,14 +1940,18 @@ module.exports.CreateWebServer = function (parent, db, args, secret, certificate
1940 //var user = obj.users[req.session.userid];
1941 //if ((user == null) || (mesh.links[user._id] == null) || ((mesh.links[user._id].rights & 1) == 0)) { res.sendStatus(401); return; }
1942 //if (domain.id != mesh.domain) { res.sendStatus(401); return; }
1943 -
1943 +
1944 + var meshidhex = new Buffer(req.query.id.replace(/\@/g, '+').replace(/\$/g, '/'), 'base64').toString('hex').toUpperCase();
1945 + var serveridhex = new Buffer(obj.agentCertificatHashBase64.replace(/\@/g, '+').replace(/\$/g, '/'), 'base64').toString('hex').toUpperCase();
1946 +
1947 var xdomain = domain.id;
1948 if (xdomain != '') xdomain += "/";
1946 - var meshsettings = "MeshName=" + mesh.name + "\r\nMeshType=" + mesh.mtype + "\r\nMeshID=0x" + req.query.id.toUpperCase() + "\r\nServerID=" + obj.agentCertificatHashHex.toUpperCase() + "\r\n";
1949 + var meshsettings = "MeshName=" + mesh.name + "\r\nMeshType=" + mesh.mtype + "\r\nMeshID=0x" + meshidhex + "\r\nServerID=" + serveridhex + "\r\n";
1950 if (obj.args.lanonly != true) { meshsettings += "MeshServer=ws" + (obj.args.notls ? '' : 's') + "://" + certificates.CommonName + ":" + obj.args.port + "/" + xdomain + "agent.ashx\r\n"; } else { meshsettings += "MeshServer=local"; }
1951
1952 res.set({ 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0', 'Content-Type': 'application/octet-stream', 'Content-Disposition': 'attachment; filename=meshagent.msh' });
1953 res.send(meshsettings);
1954 + console.log(meshsettings);
1955 });
1956 }
1957
@@ -2046,24 +2050,8 @@ module.exports.CreateWebServer = function (parent, db, args, secret, certificate
2050
2051 // Receive mesh agent connections
2052 obj.app.ws(url + 'agent.ashx', function (ws, req) { try { var domain = getDomain(req); obj.meshAgentHandler.CreateMeshAgent(obj, obj.db, ws, req, obj.args, domain); } catch (e) { console.log(e); } });
2049 -
2050 - obj.app.get(url + 'stop', function (req, res) { res.send('Stopping Server, <a href="' + url + '">click here to login</a>.'); setTimeout(function () { parent.Stop(); }, 500); });
2053
2052 - /*
2053 - // DEBUG ONLY: Returns a mesh relay key
2054 - obj.app.get(url + 'getkey', function (req, res) {
2055 - res.set({ 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0', 'Content-Type': 'text/plain' });
2056 - require('./meshrelay.js').CreateMeshRelayKey(obj, function (x) { res.send(x); });
2057 - });
2058 - */
2059 -
2060 - /*
2061 - // Test
2062 - obj.app.get(url + 'test.json', function (req, res) {
2063 - res.set({ 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0', 'Content-Type': 'text/plain' });
2064 - res.send('{ "glossary": { "title": "example glossary", "bob" : 5 } }');
2065 - });
2066 - */
2054 + obj.app.get(url + 'stop', function (req, res) { res.send('Stopping Server, <a href="' + url + '">click here to login</a>.'); setTimeout(function () { parent.Stop(); }, 500); });
2055
2056 // Indicates to ExpressJS that the public folder should be used to serve static files.
2057 obj.app.use(url, obj.express.static(obj.path.join(__dirname, 'public')));