Improved certificate checking.

Ylian Saint-Hilaire committed Nov 8, 2020 at 13:29 UTC 8b9a437ed7bbf491ebda6c1435bdd529ef12d858
3 files changed +18 -2
agents/meshcore.js
+11 -1
@@ -776,7 +776,12 @@ function createMeshCore(agent) {
776 var woptions = http.parseUri(xurl);
777 woptions.perMessageDeflate = false;
778 if (typeof data.perMessageDeflate == 'boolean') { woptions.perMessageDeflate = data.perMessageDeflate; }
779 +
780 + // Perform manual server TLS certificate checking based on the certificate hash given by the server.
781 woptions.rejectUnauthorized = 0;
782 + woptions.checkServerIdentity = function checkServerIdentity(certs) { if ((checkServerIdentity.servertlshash != null) && (checkServerIdentity.servertlshash != certs[0].fingerprint.split(':').join('').toLowerCase())) { throw new Error('BadCert') } }
783 + woptions.checkServerIdentity.servertlshash = data.servertlshash;
784 +
785 //sendConsoleText(JSON.stringify(woptions));
786 //sendConsoleText('TUNNEL: ' + JSON.stringify(data));
787 var tunnel = http.request(woptions);
@@ -1147,7 +1152,12 @@ function createMeshCore(agent) {
1152 data.url = 'http' + getServerTargetUrlEx('*/').substring(2);
1153 var agentFileHttpOptions = http.parseUri(data.url);
1154 agentFileHttpOptions.path = data.urlpath;
1150 - agentFileHttpOptions.rejectUnauthorized = 0; // TODO: Check TLS cert
1155 +
1156 + // Perform manual server TLS certificate checking based on the certificate hash given by the server.
1157 + agentFileHttpOptions.rejectUnauthorized = 0;
1158 + agentFileHttpOptions.checkServerIdentity = function checkServerIdentity(certs) { if ((checkServerIdentity.servertlshash != null) && (checkServerIdentity.servertlshash != certs[0].fingerprint.split(':').join('').toLowerCase())) { throw new Error('BadCert') } }
1159 + agentFileHttpOptions.checkServerIdentity.servertlshash = data.servertlshash;
1160 +
1161 if (agentFileHttpOptions == null) break;
1162 var agentFileHttpRequest = http.request(agentFileHttpOptions,
1163 function (response) {
meshuser.js
+4
@@ -1328,6 +1328,10 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
1328 if (url.query.p == '1') { requiredNonRights = MESHRIGHT_NOTERMINAL; }
1329 else if ((url.query.p == '4') || (url.query.p == '5')) { requiredNonRights = MESHRIGHT_NOFILES; }
1330
1331 + // Add server TLS cert hash
1332 + const tlsCertHash = parent.webCertificateHashs[domain.id];
1333 + if (tlsCertHash != null) { command.servertlshash = Buffer.from(tlsCertHash, 'binary').toString('hex'); }
1334 +
1335 // Add user consent messages
1336 command.soptions = {};
1337 if (typeof domain.consentmessages == 'object') {
webserver.js
+3 -1
@@ -3223,12 +3223,14 @@ module.exports.CreateWebServer = function (parent, db, args, certificates) {
3223
3224 // Instruct one of more agents to download a URL to a given local drive location.
3225 function handleUploadFileBatchEx(cmd) {
3226 + var tlsCertHash = obj.webCertificateHashs[cmd.domain.id];
3227 + if (tlsCertHash != null) { tlsCertHash = Buffer.from(tlsCertHash, 'binary').toString('hex'); }
3228 for (var i in cmd.nodeids) {
3229 obj.GetNodeWithRights(cmd.domain, cmd.user, cmd.nodeids[i], function (node, rights, visible) {
3230 if ((node == null) || ((rights & 8) == 0) || (visible == false)) return; // We don't have remote control rights to this device
3231 var agentPath = ((node.agent.id > 0) && (node.agent.id < 5)) ? cmd.windowsPath : cmd.linuxPath;
3232 for (var f in cmd.files) {
3231 - const acmd = { action: 'wget', overwrite: cmd.overwrite, urlpath: '/agentdownload.ashx?c=' + obj.parent.encodeCookie({ a: 'tmpdl', d: cmd.domain.id, nid: node._id, f: cmd.files[f].target }, obj.parent.loginCookieEncryptionKey), path: obj.path.join(agentPath, cmd.files[f].name) };
3233 + const acmd = { action: 'wget', overwrite: cmd.overwrite, urlpath: '/agentdownload.ashx?c=' + obj.parent.encodeCookie({ a: 'tmpdl', d: cmd.domain.id, nid: node._id, f: cmd.files[f].target }, obj.parent.loginCookieEncryptionKey), path: obj.path.join(agentPath, cmd.files[f].name), servertlshash: tlsCertHash };
3234 var agent = obj.wsagents[node._id];
3235 if (agent != null) { try { agent.send(JSON.stringify(acmd)); } catch (ex) { } }
3236 // TODO: Add support for peer servers.