Added option to hex encode the cookies for Azure Application Proxy.

Ylian Saint-Hilaire committed Feb 3, 2020 at 10:45 UTC b07f4114e4651083d86a539bb77ce351a9136423
2 files changed +8 -4
meshcentral.js
+7 -3
@@ -1993,7 +1993,7 @@ function CreateMeshCentralServer(config, args) {
1993 o.time = Math.floor(Date.now() / 1000); // Add the cookie creation time
1994 const iv = Buffer.from(obj.crypto.randomBytes(12), 'binary'), cipher = obj.crypto.createCipheriv('aes-256-gcm', key.slice(0, 32), iv);
1995 const crypted = Buffer.concat([cipher.update(JSON.stringify(o), 'utf8'), cipher.final()]);
1996 - var r = Buffer.concat([iv, cipher.getAuthTag(), crypted]).toString('base64').replace(/\+/g, '@').replace(/\//g, '$');
1996 + var r = Buffer.concat([iv, cipher.getAuthTag(), crypted]).toString(obj.args.cookieencoding ? obj.args.cookieencoding : 'base64').replace(/\+/g, '@').replace(/\//g, '$');
1997 obj.debug('cookie', 'Encoded AESGCM cookie: ' + JSON.stringify(o));
1998 return r;
1999 } catch (ex) { obj.debug('cookie', 'ERR: Failed to encode AESGCM cookie due to exception: ' + ex); return null; }
@@ -2003,6 +2003,10 @@ function CreateMeshCentralServer(config, args) {
2003 obj.decodeCookie = function (cookie, key, timeout) {
2004 var r = obj.decodeCookieAESGCM(cookie, key, timeout);
2005 if (r == null) { r = obj.decodeCookieAESSHA(cookie, key, timeout); }
2006 + if ((r == null) && (obj.args.cookieencoding == null) && ((cookie == cookie.toLowerCase()) || (cookie == cookie.toUpperCase()))) {
2007 + obj.debug('cookie', 'Upper/Lowercase cookie, try "CookieEncoding":"hex" in settings section of config.json.');
2008 + console.log('Upper/Lowercase cookie, try "CookieEncoding":"hex" in settings section of config.json.');
2009 + }
2010 if ((r != null) && (typeof r.once == 'string') && (r.once.length > 0)) {
2011 // This cookie must only be used once.
2012 if (timeout == null) { timeout = 2; }
@@ -2033,7 +2037,7 @@ function CreateMeshCentralServer(config, args) {
2037 obj.decodeCookieAESGCM = function (cookie, key, timeout) {
2038 try {
2039 if (key == null) { key = obj.serverKey; }
2036 - cookie = Buffer.from(cookie.replace(/\@/g, '+').replace(/\$/g, '/'), 'base64');
2040 + cookie = Buffer.from(cookie.replace(/\@/g, '+').replace(/\$/g, '/'), obj.args.cookieencoding ? obj.args.cookieencoding : 'base64');
2041 const decipher = obj.crypto.createDecipheriv('aes-256-gcm', key.slice(0, 32), cookie.slice(0, 12));
2042 decipher.setAuthTag(cookie.slice(12, 16));
2043 const o = JSON.parse(decipher.update(cookie.slice(28), 'binary', 'utf8') + decipher.final('utf8'));
@@ -2059,7 +2063,7 @@ function CreateMeshCentralServer(config, args) {
2063 try {
2064 if (key == null) { key = obj.serverKey; }
2065 if (key.length < 80) { return null; }
2062 - cookie = Buffer.from(cookie.replace(/\@/g, '+').replace(/\$/g, '/'), 'base64');
2066 + cookie = Buffer.from(cookie.replace(/\@/g, '+').replace(/\$/g, '/'), obj.args.cookieencoding ? obj.args.cookieencoding : 'base64');
2067 const decipher = obj.crypto.createDecipheriv('aes-256-cbc', key.slice(48, 80), cookie.slice(0, 16));
2068 const rawmsg = decipher.update(cookie.slice(16), 'binary', 'binary') + decipher.final('binary');
2069 const hmac = obj.crypto.createHmac('sha384', key.slice(0, 48));
package.json
+1 -1
@@ -1,6 +1,6 @@
1 {
2 "name": "meshcentral",
3 - "version": "0.4.8-i",
3 + "version": "0.4.8-k",
4 "keywords": [
5 "Remote Management",
6 "Intel AMT",