amtprovisioningserver.js 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /**
  2. * @description MeshCentral Intel AMT Hello server
  3. * @author Ylian Saint-Hilaire
  4. * @copyright Intel Corporation 2018-2022
  5. * @license Apache-2.0
  6. * @version v0.0.1
  7. */
  8. /*xjslint node: true */
  9. /*xjslint plusplus: true */
  10. /*xjslint maxlen: 256 */
  11. /*jshint node: true */
  12. /*jshint strict: false */
  13. /*jshint esversion: 6 */
  14. "use strict";
  15. // Construct the Intel AMT hello server. This is used for Intel AMT bare-metal activation on the local LAN.
  16. // This server can receive a notification from Intel AMT and attempt activation.
  17. // In Intel documentation, this is called the Setup and Configuration Application (SCA)
  18. module.exports.CreateAmtProvisioningServer = function (parent, config) {
  19. var obj = {};
  20. obj.meshid = config.devicegroup; // This is the device group identifier that all activated devices will be added to.
  21. // WSMAN stack
  22. const CreateWsmanComm = require('./amt/amt-wsman-comm');
  23. const WsmanStackCreateService = require('./amt/amt-wsman');
  24. const AmtStackCreateService = require('./amt/amt');
  25. // Check configuration
  26. if (checkAmtPassword(config.newmebxpassword) == false) { console.log('Invalid MEBx password, must have 1 lower, 1 upper, 1 numeric, 1 non-alpha and be 8 or more in length.'); return null; }
  27. // Start the Intel AMT hello server
  28. var port = 9971;
  29. if (typeof config.port == 'number') { port = config.port; }
  30. const net = require('net');
  31. obj.server = net.createServer(function (socket) {
  32. socket.ra = socket.remoteAddress;
  33. socket.data = null;
  34. socket.on('error', function (err) { })
  35. socket.on('close', function () { if (this.data != null) { processHelloData(this.data, this.ra); } delete this.ra; this.removeAllListeners(); })
  36. socket.on('data', function (data) {
  37. if (this.data == null) { this.data = data; } else { Buffer.concat([this.data, data]); }
  38. var str = this.data.toString();
  39. if (str.startsWith('GET ') && (str.indexOf('\r\n\r\n') >= 0)) {
  40. this.data = null;
  41. var content = "<!DOCTYPE html><html><head><meta charset=\"UTF-8\"><title>Intel&reg; AMT Hello Server</title></head><body>Intel AMT hello server.<br />Intel&reg; AMT devices should send notification to this port for activation.</body></html>";
  42. try { socket.end('HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: ' + content.length + '\r\nConnection: close\r\n\r\n' + content); } catch (ex) {}
  43. } else if (this.data.length > 16000) {
  44. try { this.end(); } catch (ex) { };
  45. }
  46. })
  47. });
  48. obj.server.listen(port);
  49. console.log('MeshCentral Intel(R) AMT provisioning server running on port ' + port + '.');
  50. obj.parent = parent;
  51. obj.rootCertCN = obj.parent.certificateOperations.forge.pki.certificateFromPem(obj.parent.certificates.root.cert).subject.getField('CN').value;
  52. // Devices activaly being configured
  53. obj.devices = {} // Address -> Device
  54. // Example hello data for testing
  55. //setTimeout(function () { processHelloData(Buffer.from('01000300000000004b529b93d413181de4871c697a6b7a2b170220c3846bf24b9e93ca64274c0ec67c1ecc5e024ffcacd2d74019350e81fe546ae4022045140b3247eb9cc8c5b4f0d7b53091f73292089e6e5a63e2749dd3aca9198eda0220d7a7a0fb5d7e2731d771e9484ebcdef71d5f0c3e0a2948782bc83ee0ea699ef402201465fa205397b876faa6f0a9958e5590e40fcc7faa4fb7c2c8677521fb5fb65802202ce1cb0bf9d2f9e102993fbe215152c3b2dd0cabde1c68e5319b839154dbb7f502209acfab7e43c8d880d06b262a94deeee4b4659989c3d0caf19baf6405e41ab7df022016af57a9f676b0ab126095aa5ebadef22ab31119d644ac95cd4b93dbf3f26aeb0220960adf0063e96356750c2965dd0a0867da0b9cbd6e77714aeafb2349ab393da3022068ad50909b04363c605ef13581a939ff2c96372e3f12325b0a6861e1d59f660302206dc47172e01cbcb0bf62580d895fe2b8ac9ad4f873801e0c10b9c837d21eb177022073c176434f1bc6d5adf45b0e76e727287c8de57616c1e6e6141a2b2cbc7d8e4c022043df5774b03e7fef5fe40d931a7bedf1bb2e6b42738c4e6d3841103d3aa7f33902202399561127a57125de8cefea610ddf2fa078b5c8067f4e828290bfb860e84b3c022070a73f7f376b60074248904534b11482d5bf0e698ecc498df52577ebf2e93b9a02204348a0e9444c78cb265e058d5e8944b4d84f9662bd26db257f8934a443c701610220cb3ccbb76031e5e0138f8dd39a23f9de47ffc35e43c1144cea27d46a5ab1cb5f022031ad6648f8104138c738f39ea4320133393e3a18cc02296ef97c2ac9ef6731d00220552f7bdcf1a7af9e6ce672017f4f12abf77240c78e761ac203d1d9d20ac89988022067540a47aa5b9f34570a99723cfefa96a96ee3f0d9b8bf4def9440b8065d665d02207224395222cd588c4f2683716922addb41e39b581ac34fa87b39efa896fbb39e0220cbb522d7b7f127ad6a0113865bdf1cd4102e7d0759af635a7cf4720dc963c53b0220179fbc148a3dd00fd24ea13458cc43bfa7f59c8182d783a513f6ebec100c892402202cabeafe37d06ca22aba7391c0033d25982952c453647349763a3ab5ad6ccf69', 'hex'), '192.168.2.148'); }, 500);
  56. //setTimeout(function () { processHelloData(Buffer.from('01000300000000004b529b93d413181de4871c697a6b7a2b180220c3846bf24b9e93ca64274c0ec67c1ecc5e024ffcacd2d74019350e81fe546ae4022045140b3247eb9cc8c5b4f0d7b53091f73292089e6e5a63e2749dd3aca9198eda0220d7a7a0fb5d7e2731d771e9484ebcdef71d5f0c3e0a2948782bc83ee0ea699ef402201465fa205397b876faa6f0a9958e5590e40fcc7faa4fb7c2c8677521fb5fb65802202ce1cb0bf9d2f9e102993fbe215152c3b2dd0cabde1c68e5319b839154dbb7f502209acfab7e43c8d880d06b262a94deeee4b4659989c3d0caf19baf6405e41ab7df022016af57a9f676b0ab126095aa5ebadef22ab31119d644ac95cd4b93dbf3f26aeb0220960adf0063e96356750c2965dd0a0867da0b9cbd6e77714aeafb2349ab393da3022068ad50909b04363c605ef13581a939ff2c96372e3f12325b0a6861e1d59f660302206dc47172e01cbcb0bf62580d895fe2b8ac9ad4f873801e0c10b9c837d21eb177022073c176434f1bc6d5adf45b0e76e727287c8de57616c1e6e6141a2b2cbc7d8e4c022043df5774b03e7fef5fe40d931a7bedf1bb2e6b42738c4e6d3841103d3aa7f33902202399561127a57125de8cefea610ddf2fa078b5c8067f4e828290bfb860e84b3c022070a73f7f376b60074248904534b11482d5bf0e698ecc498df52577ebf2e93b9a02204348a0e9444c78cb265e058d5e8944b4d84f9662bd26db257f8934a443c701610220cb3ccbb76031e5e0138f8dd39a23f9de47ffc35e43c1144cea27d46a5ab1cb5f022031ad6648f8104138c738f39ea4320133393e3a18cc02296ef97c2ac9ef6731d00220552f7bdcf1a7af9e6ce672017f4f12abf77240c78e761ac203d1d9d20ac89988022067540a47aa5b9f34570a99723cfefa96a96ee3f0d9b8bf4def9440b8065d665d0220a267c480b0b29056eb5e8aa7c93add804f5a7df516e969e77bcacafe8d45607902207224395222cd588c4f2683716922addb41e39b581ac34fa87b39efa896fbb39e0220cbb522d7b7f127ad6a0113865bdf1cd4102e7d0759af635a7cf4720dc963c53b0220179fbc148a3dd00fd24ea13458cc43bfa7f59c8182d783a513f6ebec100c892402202cabeafe37d06ca22aba7391c0033d25982952c453647349763a3ab5ad6ccf69', 'hex'), '192.168.2.148'); }, 5000);
  57. //setTimeout(function () { processHelloData(Buffer.from('01000300010000003ec2ffd2d19d2d41860a54b2039b72ff180220c3846bf24b9e93ca64274c0ec67c1ecc5e024ffcacd2d74019350e81fe546ae4022045140b3247eb9cc8c5b4f0d7b53091f73292089e6e5a63e2749dd3aca9198eda0220d7a7a0fb5d7e2731d771e9484ebcdef71d5f0c3e0a2948782bc83ee0ea699ef402201465fa205397b876faa6f0a9958e5590e40fcc7faa4fb7c2c8677521fb5fb65802202ce1cb0bf9d2f9e102993fbe215152c3b2dd0cabde1c68e5319b839154dbb7f502209acfab7e43c8d880d06b262a94deeee4b4659989c3d0caf19baf6405e41ab7df022016af57a9f676b0ab126095aa5ebadef22ab31119d644ac95cd4b93dbf3f26aeb0220960adf0063e96356750c2965dd0a0867da0b9cbd6e77714aeafb2349ab393da3022068ad50909b04363c605ef13581a939ff2c96372e3f12325b0a6861e1d59f660302206dc47172e01cbcb0bf62580d895fe2b8ac9ad4f873801e0c10b9c837d21eb177022073c176434f1bc6d5adf45b0e76e727287c8de57616c1e6e6141a2b2cbc7d8e4c022043df5774b03e7fef5fe40d931a7bedf1bb2e6b42738c4e6d3841103d3aa7f33902202399561127a57125de8cefea610ddf2fa078b5c8067f4e828290bfb860e84b3c022070a73f7f376b60074248904534b11482d5bf0e698ecc498df52577ebf2e93b9a02204348a0e9444c78cb265e058d5e8944b4d84f9662bd26db257f8934a443c701610220cb3ccbb76031e5e0138f8dd39a23f9de47ffc35e43c1144cea27d46a5ab1cb5f022031ad6648f8104138c738f39ea4320133393e3a18cc02296ef97c2ac9ef6731d00220552f7bdcf1a7af9e6ce672017f4f12abf77240c78e761ac203d1d9d20ac89988022067540a47aa5b9f34570a99723cfefa96a96ee3f0d9b8bf4def9440b8065d665d0220a267c480b0b29056eb5e8aa7c93add804f5a7df516e969e77bcacafe8d45607902207224395222cd588c4f2683716922addb41e39b581ac34fa87b39efa896fbb39e0220cbb522d7b7f127ad6a0113865bdf1cd4102e7d0759af635a7cf4720dc963c53b0220179fbc148a3dd00fd24ea13458cc43bfa7f59c8182d783a513f6ebec100c892402202cabeafe37d06ca22aba7391c0033d25982952c453647349763a3ab5ad6ccf69', 'hex'), '192.168.2.134'); }, 5000);
  58. // Parse Intel AMT hello data
  59. function parseHelloData(data, addr) {
  60. try {
  61. var amtHello = { time: Date.now(), addr: addr };
  62. // Decode header
  63. if (data.length < 25) return; // Invalid data
  64. const firstBytes = data.readInt16LE(0);
  65. if (firstBytes > 1) return; // Invalid data
  66. amtHello.adminCredentialsSet = (firstBytes != 0);
  67. amtHello.helloversion = data.readInt16LE(2);
  68. if (amtHello.helloversion != 3) return null; // One touch PID not supported, only version 3 supported.
  69. amtHello.retryCount = data.readInt32LE(4);
  70. amtHello.guidhex = data.slice(8, 24).toString('hex');
  71. amtHello.guid = guidToStr(amtHello.guidhex);
  72. // Get the list of hashes
  73. const hashCount = data[24];
  74. amtHello.hashes = [];
  75. var ptr = 25;
  76. for (var i = 0; i < hashCount; i++)
  77. {
  78. const hashType = data[ptr]; // 1=SHA1 (20 byte hash); 2 = SHA256 (32 byte hash); 3 = SHA384 (48 byte hash)
  79. const hashSize = data[ptr + 1];
  80. if ((hashType < 1) || (hashType > 3)) return null; // Unexpected hash type
  81. if ((hashType == 1) && (hashSize != 20)) return null; // Unexpected SHA1 hash size
  82. if ((hashType == 2) && (hashSize != 32)) return null; // Unexpected SHA256 hash size
  83. if ((hashType == 3) && (hashSize != 48)) return null; // Unexpected SHA384 hash size
  84. const hash = data.slice(ptr + 2, ptr + 2 + hashSize);
  85. amtHello.hashes.push(hash.toString('hex'));
  86. ptr += (hashSize + 2);
  87. }
  88. if (amtHello.hashes.length != hashCount) return null; // Unexpected number of hashes
  89. return amtHello; // Everything looks good.
  90. } catch (ex) { return null; }
  91. }
  92. function guidToStr(g) { return g.substring(6, 8) + g.substring(4, 6) + g.substring(2, 4) + g.substring(0, 2) + "-" + g.substring(10, 12) + g.substring(8, 10) + "-" + g.substring(14, 16) + g.substring(12, 14) + "-" + g.substring(16, 20) + "-" + g.substring(20); }
  93. function strToGuid(s) { s = s.replace(/-/g, ''); var ret = s.substring(6, 8) + s.substring(4, 6) + s.substring(2, 4) + s.substring(0, 2) + s.substring(10, 12) + s.substring(8, 10) + s.substring(14, 16) + s.substring(12, 14) + s.substring(16, 20) + s.substring(20); return ret; }
  94. // Process incoming Intel AMT hello data
  95. function processHelloData(data, addr) {
  96. // Check if we can parse the incoming data
  97. if (addr.startsWith('::ffff:')) { addr = addr.substring(7); }
  98. if (obj.devices[addr] != null) return; // Device on this address already being activated.
  99. const dev = parseHelloData(data, addr);
  100. if (dev == null) { parent.debug('amtsca', addr, 'Got invalid hello from: ' + addr); return; } // Invalid Intel AMT hello
  101. parent.debug('amtsca', 'Got hello from ' + addr);
  102. obj.devices[addr] = dev;
  103. dev.aquired = {};
  104. // Set device messages
  105. dev.consoleMsg = function deviceConsoleMsg(msg) { parent.debug('amtsca', deviceConsoleMsg.dev.aquired.host ? deviceConsoleMsg.dev.aquired.host : deviceConsoleMsg.dev.addr, msg); return; }
  106. dev.consoleMsg.dev = dev;
  107. // Get assumed trusted FQDN and device group
  108. dev.trustedFqdn = config.trustedfqdn;
  109. var mesh = parent.webserver.meshes[config.devicegroup];
  110. if ((mesh == null) || (mesh.mtype !== 1) || (typeof mesh.amt !== 'object') || (typeof mesh.amt.type !== 'number')) { dev.consoleMsg('Invalid device group for Intel AMT activation.'); return; }
  111. if ((mesh.amt.type != 3) && (mesh.amt.type != 4)) { dev.consoleMsg('Device group does not have ACM activation policy.'); return; }
  112. dev.mesh = mesh;
  113. dev.meshid = mesh._id;
  114. dev.domainid = mesh.domain;
  115. // Compute the nodeid for this device using the device GUID
  116. const g = dev.guid.split('-').join('');
  117. const id = Buffer.from(g + g + g, 'hex').toString('base64').replace(/\+/g, '@').replace(/\//g, '$');
  118. dev.nodeid = 'node/' + mesh.domain + '/' + id;
  119. // Attempts reverse DNS loopup on the device IP address
  120. const func = function dnsReverseLoopup(err, hostnames) {
  121. var hostname = dnsReverseLoopup.addr;
  122. if ((err == null) && (hostnames != null) && (hostnames.length > 0)) { hostname = hostnames[0]; }
  123. dnsReverseLoopup.dev.aquired.host = hostname;
  124. processHelloDataEx1(dnsReverseLoopup.dev);
  125. }
  126. func.addr = addr;
  127. func.dev = dev;
  128. require('dns').reverse(addr, func);
  129. }
  130. // Check if this device has any way to be activated in ACM using our server certificates.
  131. function checkAcmActivation(hello) {
  132. var domain = parent.config.domains[hello.domainid];
  133. if ((domain == null) || (domain.amtacmactivation == null) || (domain.amtacmactivation.certs == null) || (domain.amtacmactivation.certs.length == 0)) return null;
  134. const activationCerts = domain.amtacmactivation.certs;
  135. // Get the trusted FQDN of the device
  136. var trustedFqdn = hello.trustedFqdn;
  137. // Find a matching certificate
  138. for (var i in activationCerts) {
  139. var cert = activationCerts[i];
  140. if ((cert.cn == '*') || (cert.cn == trustedFqdn)) {
  141. for (var j in hello.hashes) {
  142. var hash = hello.hashes[j];
  143. if (hash == cert.sha256) { return { cert: cert, fqdn: trustedFqdn, hash: cert.sha256 }; } // Found a match
  144. else if (hash == cert.sha1) { return { cert: cert, fqdn: trustedFqdn, hash: cert.sha1 }; } // Found a match
  145. }
  146. }
  147. }
  148. return null; // Did not find a match
  149. }
  150. function processHelloDataEx1(dev) {
  151. // Get an activation certificate chain
  152. const certinfo = checkAcmActivation(dev);
  153. if (certinfo == null) { dev.consoleMsg('Unable to find a matching ACM activation certificate.'); destroyDevice(dev); return; }
  154. var certchain = parent.certificateOperations.getAcmCertChain(parent.config.domains[dev.domainid], dev.trustedFqdn, certinfo.cert.sha256);
  155. if (certchain == null) { dev.consoleMsg('Unable to create TLS certificate chain.'); destroyDevice(dev); return; }
  156. dev.certchain = certchain;
  157. // Setup a connection to the Intel AMT device
  158. dev.consoleMsg('Launching TLS connection...');
  159. var comm = CreateWsmanComm(dev.aquired.host, 16993, 'admin', '', 1, { cert: dev.certchain.certs.reverse().join(''), key: dev.certchain.signkey }); // Perform TLS connection
  160. comm.xtlsFingerprint = 0; // No Intel AMT certificate checking.
  161. var wsstack = WsmanStackCreateService(comm);
  162. dev.amtstack = AmtStackCreateService(wsstack);
  163. dev.amtstack.dev = dev;
  164. dev.amtstack.BatchEnum(null, ['*AMT_GeneralSettings', 'CIM_SoftwareIdentity', '*AMT_SetupAndConfigurationService'], processHelloDataEx2);
  165. }
  166. function processHelloDataEx2(stack, name, responses, status) {
  167. const dev = stack.dev;
  168. if (isAmtDeviceValid(dev) == false) return; // Device no longer exists, ignore this request.
  169. if (status != 200) { dev.consoleMsg('Failed TLS connection, status=' + status + '.'); destroyDevice(dev); return; }
  170. // Fetch the Intel AMT version from WSMAN
  171. if ((responses != null) && (responses['CIM_SoftwareIdentity'] != null) && (responses['CIM_SoftwareIdentity'].responses != null)) {
  172. var amtlogicalelements = [];
  173. amtlogicalelements = responses['CIM_SoftwareIdentity'].responses;
  174. if (responses['AMT_SetupAndConfigurationService'] != null && responses['AMT_SetupAndConfigurationService'].response != null) {
  175. amtlogicalelements.push(responses['AMT_SetupAndConfigurationService'].response);
  176. }
  177. if (amtlogicalelements.length > 0) {
  178. var vs = getInstance(amtlogicalelements, 'AMT')['VersionString'];
  179. if (vs != null) {
  180. dev.aquired.version = vs;
  181. const versionSplit = parseInt(dev.aquired.version.split('.'));
  182. dev.aquired.versionmajor = parseInt(versionSplit[0]);
  183. dev.aquired.versionminor = parseInt(versionSplit[1]);
  184. if (versionSplit.length >= 3) { dev.aquired.versionmaintenance = parseInt(versionSplit[2]); }
  185. }
  186. }
  187. }
  188. // Fetch the Intel AMT version from HTTP stack
  189. if ((dev.amtversionstr == null) && (stack.wsman.comm.amtVersion != null)) {
  190. var s = stack.wsman.comm.amtVersion.split('.');
  191. if (s.length >= 2) {
  192. dev.aquired.version = s[0] + '.' + s[1];
  193. dev.aquired.versionmajor = parseInt(s[0]);
  194. dev.aquired.versionminor = parseInt(s[1]);
  195. if (s.length >= 3) {
  196. dev.aquired.version = s[0] + '.' + s[1] + '.' + s[2];
  197. dev.aquired.versionmaintenance = parseInt(s[2]);
  198. }
  199. }
  200. }
  201. // If we can't get the Intel AMT version, stop here.
  202. if (dev.aquired.version == null) { dev.consoleMsg('Could not get Intel AMT version.'); destroyDevice(dev); return; } // Could not get Intel AMT version, disconnect();
  203. // Get the digest realm
  204. if (responses['AMT_GeneralSettings'] && responses['AMT_GeneralSettings'].response && (typeof responses['AMT_GeneralSettings'].response['DigestRealm'] == 'string')) {
  205. dev.aquired.realm = responses['AMT_GeneralSettings'].response['DigestRealm'];
  206. } else {
  207. dev.consoleMsg('Could not get Intel AMT digest realm.'); destroyDevice(dev); return;
  208. }
  209. // Looks like we are doing well.
  210. dev.consoleMsg('Succesful TLS connection, Intel AMT v' + dev.aquired.version);
  211. // Set the new MEBx password
  212. dev.consoleMsg('Setting MEBx password...');
  213. dev.amtstack.AMT_SetupAndConfigurationService_SetMEBxPassword(config.newmebxpassword, processHelloDataEx3);
  214. }
  215. // Response from setting MEBx password
  216. function processHelloDataEx3(stack, name, responses, status) {
  217. const dev = stack.dev;
  218. if (isAmtDeviceValid(dev) == false) return; // Device no longer exists, ignore this request.
  219. if (status != 200) { dev.consoleMsg('Failed to set MEBx password, status=' + status + '.'); destroyDevice(dev); return; }
  220. dev.consoleMsg('MEBx password set. Setting admin password...');
  221. // See what admin password to use
  222. dev.aquired.user = 'admin';
  223. dev.aquired.pass = dev.mesh.amt.password;
  224. if (dev.aquired.pass == null) { dev.aquired.pass = getRandomAmtPassword(); }
  225. // Set the admin password
  226. dev.amtstack.AMT_AuthorizationService_SetAdminAclEntryEx(dev.aquired.user, hex_md5(dev.aquired.user + ':' + dev.aquired.realm + ':' + dev.aquired.pass), processHelloDataEx4);
  227. }
  228. // Response from setting admin password
  229. function processHelloDataEx4(stack, name, responses, status) {
  230. const dev = stack.dev;
  231. if (isAmtDeviceValid(dev) == false) return; // Device no longer exists, ignore this request.
  232. if (status != 200) { dev.consoleMsg('Failed to set admin password, status=' + status + '.'); destroyDevice(dev); return; }
  233. dev.consoleMsg('Admin password set.');
  234. // Perform Intel AMT clock sync
  235. attemptSyncClock(dev, function (dev) {
  236. // Setup TLS and commit.
  237. attemptTlsSync(dev, function (dev) {
  238. dev.consoleMsg('Intel AMT ACM activation completed.');
  239. parent.SetConnectivityState(dev.meshid, dev.nodeid, Date.now(), 4, 7, null, { name: dev.name }); // Report power state as "present" (7).
  240. if (obj.parent.amtManager != null) { obj.parent.amtManager.startAmtManagement(dev.nodeid, 3, dev.aquired.host); } // Request that Intel AMT manager take a look at this device.
  241. destroyDevice(dev); // We are done, clean up.
  242. });
  243. });
  244. }
  245. //
  246. // Intel AMT TLS setup
  247. //
  248. // Check if Intel AMT TLS state is correct
  249. function attemptTlsSync(dev, func) {
  250. dev.taskCount = 1;
  251. dev.taskCompleted = func;
  252. // TODO: We only deal with certificates starting with Intel AMT 6 and beyond
  253. dev.amtstack.BatchEnum(null, ['AMT_PublicKeyCertificate', 'AMT_PublicPrivateKeyPair', 'AMT_TLSSettingData', 'AMT_TLSCredentialContext'], attemptTlsSyncEx);
  254. }
  255. // Intel AMT is not always in a good spot to generate a key pair. This will retry at 10 second interval.
  256. function generateKeyPairWithRetry(dev, func) {
  257. if (isAmtDeviceValid(dev) == false) return;
  258. if (dev.keyPairAttempts == null) { dev.keyPairAttempts = 1; } else { dev.keyPairAttempts++; }
  259. dev.amtstack.AMT_PublicKeyManagementService_GenerateKeyPair(0, 2048, function (stack, name, responses, status) {
  260. if (isAmtDeviceValid(dev) == false) { delete dev.keyPairAttempts; return; }
  261. if ((status == 200) || (dev.keyPairAttempts > 19)) {
  262. delete dev.keyPairAttempts;
  263. func(stack, name, responses, status);
  264. } else {
  265. if ((responses.Body != null) && (responses.Body.ReturnValue != null) && (responses.Body.ReturnValueStr != null)) {
  266. dev.consoleMsg("Failed to generate a key pair (" + status + ", " + responses.Body.ReturnValue + ", \"" + responses.Body.ReturnValueStr + "\"), attempt " + dev.keyPairAttempts + ", trying again in 10 seconds...");
  267. } else {
  268. dev.consoleMsg("Failed to generate a key pair (" + status + "), attempt " + dev.keyPairAttempts + ", trying again in 10 seconds...");
  269. }
  270. // Wait 10 seconds before attempting again
  271. var f = function doManage() { generateKeyPairWithRetry(doManage.dev, doManage.func); }
  272. f.dev = dev;
  273. f.func = func;
  274. setTimeout(f, 10000);
  275. }
  276. });
  277. }
  278. function attemptTlsSyncEx(stack, name, responses, status) {
  279. const dev = stack.dev;
  280. if (isAmtDeviceValid(dev) == false) return; // Device no longer exists, ignore this request.
  281. if (status != 200) { dev.consoleMsg("Failed to get security information (" + status + ")."); destroyDevice(dev); return; }
  282. // Setup the certificates
  283. dev.policy = {};
  284. dev.policy.certPrivateKeys = responses['AMT_PublicPrivateKeyPair'].responses;
  285. dev.policy.tlsSettings = responses['AMT_TLSSettingData'].responses;
  286. dev.policy.tlsCredentialContext = responses['AMT_TLSCredentialContext'].responses;
  287. var xxCertificates = responses['AMT_PublicKeyCertificate'].responses;
  288. for (var i in xxCertificates) {
  289. xxCertificates[i].TrustedRootCertficate = (xxCertificates[i]['TrustedRootCertficate'] == true);
  290. xxCertificates[i].X509CertificateBin = Buffer.from(xxCertificates[i]['X509Certificate'], 'base64').toString('binary');
  291. xxCertificates[i].XIssuer = parseCertName(xxCertificates[i]['Issuer']);
  292. xxCertificates[i].XSubject = parseCertName(xxCertificates[i]['Subject']);
  293. }
  294. amtcert_linkCertPrivateKey(xxCertificates, dev.policy.certPrivateKeys);
  295. dev.policy.certificates = xxCertificates;
  296. dev.consoleMsg("Intel AMT has " + xxCertificates.length + " certificate(s) and " + dev.policy.certPrivateKeys.length + " private keys(s).");
  297. // Find the current TLS certificate & MeshCentral root certificate
  298. var xxTlsCurrentCert = null;
  299. if (dev.policy.tlsCredentialContext.length > 0) {
  300. var certInstanceId = dev.policy.tlsCredentialContext[0]['ElementInContext']['ReferenceParameters']['SelectorSet']['Selector']['Value'];
  301. for (var i in dev.policy.certificates) { if (dev.policy.certificates[i]['InstanceID'] == certInstanceId) { xxTlsCurrentCert = i; } }
  302. }
  303. // This is a managed device and TLS is not enabled, turn it on.
  304. if (xxTlsCurrentCert === null) {
  305. // Start by generating a key pair
  306. dev.consoleMsg("No TLS certificate. Generating key pair...");
  307. generateKeyPairWithRetry(dev, function (stack, name, responses, status) {
  308. const dev = stack.dev;
  309. if (isAmtDeviceValid(dev) == false) return; // Device no longer exists, ignore this request.
  310. if (status != 200) { dev.consoleMsg("Failed to generate a key pair (" + status + ")."); removeAmtDevice(dev, 20); return; }
  311. // Check that we get a key pair reference
  312. var x = null;
  313. try { x = responses.Body['KeyPair']['ReferenceParameters']['SelectorSet']['Selector']['Value']; } catch (ex) { }
  314. if (x == null) { dev.consoleMsg("Unable to get key pair reference."); removeAmtDevice(dev, 21); return; }
  315. // Get the new key pair
  316. dev.consoleMsg("Fetching key pair...");
  317. dev.amtstack.Enum('AMT_PublicPrivateKeyPair', function (stack, name, responses, status, tag) {
  318. const dev = stack.dev;
  319. if (isAmtDeviceValid(dev) == false) return; // Device no longer exists, ignore this request.
  320. if (status != 200) { dev.consoleMsg("Failed to get a key pair list (" + status + ")."); removeAmtDevice(dev, 22); return; }
  321. // Get the new DER key
  322. var DERKey = null;
  323. for (var i in responses) { if (responses[i]['InstanceID'] == tag) { DERKey = responses[i]['DERKey']; } }
  324. // Get certificate values
  325. const commonName = 'IntelAMT-' + Buffer.from(parent.crypto.randomBytes(6), 'binary').toString('hex');
  326. const domain = parent.config.domains[dev.domainid];
  327. var serverName = 'MeshCentral';
  328. if ((domain != null) && (domain.title != null)) { serverName = domain.title; }
  329. const certattributes = { 'CN': commonName, 'O': serverName, 'ST': 'MC', 'C': 'MC' };
  330. // See what root certificate to use to sign the TLS cert
  331. var xxCaPrivateKey = parent.webserver.certificates.root.key; // Use our own root by default
  332. var issuerattributes = { 'CN': obj.rootCertCN };
  333. if (domain.amtmanager.tlsrootcert2 != null) {
  334. xxCaPrivateKey = domain.amtmanager.tlsrootcert2.key;
  335. issuerattributes = domain.amtmanager.tlsrootcert2.attributes;
  336. // TODO: We should change the start and end dates of our issued certificate to at least match the root.
  337. // TODO: We could do one better and auto-renew TLS certificates as needed.
  338. }
  339. // Set the extended key usages
  340. var extKeyUsage = { name: 'extKeyUsage', serverAuth: true, clientAuth: true }
  341. // Sign the key pair using the CA certifiate
  342. dev.consoleMsg("Signing certificate...");
  343. const cert = parent.amtManager.amtcert_createCertificate(certattributes, xxCaPrivateKey, DERKey, issuerattributes, extKeyUsage);
  344. if (cert == null) { dev.consoleMsg("Failed to sign the TLS certificate."); removeAmtDevice(dev, 23); return; }
  345. // Place the resulting signed certificate back into AMT
  346. var pem = obj.parent.certificateOperations.forge.pki.certificateToPem(cert).replace(/(\r\n|\n|\r)/gm, '');
  347. // Set the certificate finderprint (SHA1)
  348. var md = obj.parent.certificateOperations.forge.md.sha1.create();
  349. md.update(obj.parent.certificateOperations.forge.asn1.toDer(obj.parent.certificateOperations.forge.pki.certificateToAsn1(cert)).getBytes());
  350. dev.aquired.hash = md.digest().toHex();
  351. dev.consoleMsg("Adding certificate, hash: " + dev.aquired.hash);
  352. dev.amtstack.AMT_PublicKeyManagementService_AddCertificate(pem.substring(27, pem.length - 25), function (stack, name, responses, status) {
  353. const dev = stack.dev;
  354. if (isAmtDeviceValid(dev) == false) return; // Device no longer exists, ignore this request.
  355. if (status != 200) { dev.consoleMsg("Failed to add TLS certificate (" + status + ")."); removeAmtDevice(dev, 24); return; }
  356. var certInstanceId = null;
  357. try { certInstanceId = responses.Body['CreatedCertificate']['ReferenceParameters']['SelectorSet']['Selector']['Value']; } catch (ex) { }
  358. if (certInstanceId == null) { dev.consoleMsg("Failed to get TLS certificate identifier."); removeAmtDevice(dev, 25); return; }
  359. // Set the TLS certificate
  360. dev.setTlsSecurityPendingCalls = 2;
  361. if (dev.policy.tlsCredentialContext.length > 0) {
  362. // Modify the current context
  363. var newTLSCredentialContext = Clone(dev.policy.tlsCredentialContext[0]);
  364. newTLSCredentialContext['ElementInContext']['ReferenceParameters']['SelectorSet']['Selector']['Value'] = certInstanceId;
  365. dev.amtstack.Put('AMT_TLSCredentialContext', newTLSCredentialContext, amtSwitchToTls, 0, 1);
  366. } else {
  367. // Add a new security context
  368. dev.amtstack.Create('AMT_TLSCredentialContext', {
  369. 'ElementInContext': '<a:Address>/wsman</a:Address><a:ReferenceParameters><w:ResourceURI>' + dev.amtstack.CompleteName('AMT_PublicKeyCertificate') + '</w:ResourceURI><w:SelectorSet><w:Selector Name="InstanceID">' + certInstanceId + '</w:Selector></w:SelectorSet></a:ReferenceParameters>',
  370. 'ElementProvidingContext': '<a:Address>/wsman</a:Address><a:ReferenceParameters><w:ResourceURI>' + dev.amtstack.CompleteName('AMT_TLSProtocolEndpointCollection') + '</w:ResourceURI><w:SelectorSet><w:Selector Name="ElementName">TLSProtocolEndpointInstances Collection</w:Selector></w:SelectorSet></a:ReferenceParameters>'
  371. }, amtSwitchToTls);
  372. }
  373. // Figure out what index is local & remote
  374. var localNdx = ((dev.policy.tlsSettings[0]['InstanceID'] == 'Intel(r) AMT LMS TLS Settings')) ? 0 : 1, remoteNdx = (1 - localNdx);
  375. // Remote TLS settings
  376. var xxTlsSettings2 = Clone(dev.policy.tlsSettings);
  377. xxTlsSettings2[remoteNdx]['Enabled'] = true;
  378. xxTlsSettings2[remoteNdx]['MutualAuthentication'] = false;
  379. xxTlsSettings2[remoteNdx]['AcceptNonSecureConnections'] = true;
  380. delete xxTlsSettings2[remoteNdx]['TrustedCN'];
  381. // Update TLS settings. Enable on remote port only. If you enable on local port, the commit() will succeed but be ignored.
  382. dev.consoleMsg("Enabling TLS on remote port...");
  383. if (remoteNdx == 0) { dev.amtstack.Put('AMT_TLSSettingData', xxTlsSettings2[0], amtSwitchToTls, 0, 1, xxTlsSettings2[0]); }
  384. else { dev.amtstack.Put('AMT_TLSSettingData', xxTlsSettings2[1], amtSwitchToTls, 0, 1, xxTlsSettings2[1]); }
  385. });
  386. }, responses.Body['KeyPair']['ReferenceParameters']['SelectorSet']['Selector']['Value']);
  387. });
  388. } else {
  389. // TLS already enabled, update device in the database
  390. dev.consoleMsg("Intel AMT has TLS already enabled.");
  391. // Perform commit
  392. dev.taskCount = 1;
  393. amtPerformCommit(dev);
  394. }
  395. }
  396. function amtSwitchToTls(stack, name, responses, status) {
  397. const dev = stack.dev;
  398. if (isAmtDeviceValid(dev) == false) return; // Device no longer exists, ignore this request.
  399. if (status != 200) { dev.consoleMsg("Failed setup TLS (" + status + ")."); removeAmtDevice(dev, 26); return; }
  400. // Check if all the calls are done & perform a commit
  401. if ((--dev.setTlsSecurityPendingCalls) == 0) {
  402. dev.consoleMsg("Calling Commit...");
  403. amtPerformCommit(dev);
  404. }
  405. }
  406. function amtPerformCommit(dev) {
  407. dev.consoleMsg("Performing commit...");
  408. dev.amtstack.AMT_SetupAndConfigurationService_CommitChanges(null, function (stack, name, responses, status) {
  409. const dev = stack.dev;
  410. if (isAmtDeviceValid(dev) == false) return; // Device no longer exists, ignore this request.
  411. if (status != 200) { dev.consoleMsg("Failed perform commit (" + status + ")."); removeAmtDevice(dev, 27); return; }
  412. dev.consoleMsg("Commited, holding 5 seconds...");
  413. // Update the device state
  414. dev.aquired.tls = 1;
  415. dev.aquired.state = 2; // Activated
  416. dev.aquired.controlMode = 2; // Activated in ACM
  417. // Save activation data to amtactivation.log
  418. var domain = parent.config.domains[dev.domainid];
  419. obj.logAmtActivation(domain, { time: new Date(), action: 'acmactivate-bare-metal', domain: dev.domainid, amtUuid: dev.guid, newmebx: config.newmebxpassword, mesh: dev.meshid, amtRealm: dev.aquired.realm, amtver: dev.aquired.version, host: dev.aquired.host, ip: dev.addr, user: dev.aquired.user, pass: dev.aquired.pass, tls: dev.aquired.tls, tlshash: dev.aquired.hash });
  420. // Update device in the database
  421. if (UpdateDevice(dev) == false) return;
  422. // Switch our communications to TLS (Restart our management of this node)
  423. dev.switchToTls = 1;
  424. delete dev.tlsfail;
  425. // Wait 5 seconds before attempting to manage this device some more
  426. var f = function doManage() { if (isAmtDeviceValid(dev)) { devTaskCompleted(doManage.dev); } }
  427. f.dev = dev;
  428. setTimeout(f, 5000);
  429. });
  430. }
  431. //
  432. // Intel AMT Clock Syncronization
  433. //
  434. // Attempt to sync the Intel AMT clock if needed, call func back when done.
  435. // Care should be take not to have many pending WSMAN called when performing clock sync.
  436. function attemptSyncClock(dev, func) {
  437. if (isAmtDeviceValid(dev) == false) return; // Device no longer exists, ignore this request.
  438. dev.taskCount = 1;
  439. dev.taskCompleted = func;
  440. dev.amtstack.AMT_TimeSynchronizationService_GetLowAccuracyTimeSynch(attemptSyncClockEx);
  441. }
  442. // Intel AMT clock query response
  443. function attemptSyncClockEx(stack, name, response, status) {
  444. const dev = stack.dev;
  445. if (isAmtDeviceValid(dev) == false) return; // Device no longer exists, ignore this request.
  446. if (status != 200) { dev.consoleMsg("Failed to get clock (" + status + ")."); removeAmtDevice(dev, 17); return; }
  447. // Compute how much drift between Intel AMT and our clock.
  448. var t = new Date(), now = new Date();
  449. t.setTime(response.Body['Ta0'] * 1000);
  450. if (Math.abs(t - now) > 10000) { // If the Intel AMT clock is more than 10 seconds off, set it.
  451. dev.consoleMsg("Performing clock sync.");
  452. var Tm1 = Math.round(now.getTime() / 1000);
  453. dev.amtstack.AMT_TimeSynchronizationService_SetHighAccuracyTimeSynch(response.Body['Ta0'], Tm1, Tm1, attemptSyncClockSet);
  454. } else {
  455. // Clock is fine, we are done.
  456. devTaskCompleted(dev)
  457. }
  458. }
  459. // Intel AMT clock set response
  460. function attemptSyncClockSet(stack, name, responses, status) {
  461. const dev = stack.dev;
  462. if (isAmtDeviceValid(dev) == false) return; // Device no longer exists, ignore this request.
  463. if (status != 200) { dev.consoleMsg("Failed to sync clock (" + status + ")."); removeAmtDevice(dev, 18); }
  464. devTaskCompleted(dev)
  465. }
  466. //
  467. // Device Management Methods
  468. //
  469. // Do aggressive cleanup on the device
  470. function destroyDevice(dev) {
  471. delete obj.devices[dev.addr]; // Remove the device from the list of currently active devices.
  472. if (dev.amtstack != null) { delete dev.amtstack.dev; delete dev.amtstack; } // Clean up the AMT stack.
  473. for (var i in dev) { delete dev[i]; } // Aggressive cleanup or everything else.
  474. }
  475. // Update the device in the database and event any changes
  476. function UpdateDevice(dev) {
  477. // Check that the mesh exists
  478. const mesh = parent.webserver.meshes[dev.meshid];
  479. if (mesh == null) { destroyDevice(dev); return false; }
  480. // Get the node and change it if needed
  481. parent.db.Get(dev.nodeid, function (err, nodes) {
  482. if ((nodes == null) || (nodes.length == 0)) {
  483. // Add a new device
  484. var devicename = dev.guid;
  485. if (dev.addr != dev.aquired.host) { devicename = dev.aquired.host.split('.')[0]; }
  486. var device = { type: 'node', _id: dev.nodeid, meshid: dev.meshid, name: devicename, host: dev.aquired.host, domain: dev.domainid, intelamt: { ver: dev.aquired.version, user: dev.aquired.user, pass: dev.aquired.pass, tls: dev.aquired.tls, state: 2, realm: dev.aquired.realm } };
  487. if (dev.aquired.hash != null) { device.intelamt.hash = dev.aquired.hash; }
  488. // Set Intel AMT flags
  489. // dev.aquired.controlMode // 1 = CCM, 2 = ACM
  490. // (node.intelamt.flags & 2) == CCM, (node.intelamt.flags & 4) == ACM
  491. if (dev.aquired.controlMode == 1) { device.intelamt.flags = 2; } // CCM
  492. if (dev.aquired.controlMode == 2) { device.intelamt.flags = 4; } // ACM
  493. parent.db.Set(device);
  494. // Event the new node
  495. parent.DispatchEvent(parent.webserver.CreateMeshDispatchTargets(dev.meshid, [dev.nodeid]), obj, { etype: 'node', action: 'addnode', node: parent.webserver.CloneSafeNode(device), msgid: 84, msgArgs: [devicename, mesh.name], msg: 'Added device ' + devicename + ' to device group ' + mesh.name, domain: dev.domainid });
  496. } else {
  497. // Update an existing device
  498. const device = nodes[0];
  499. var changes = [], change = 0, log = 0;
  500. var domain = parent.config.domains[device.domain];
  501. if (domain == null) return false;
  502. // Check if anything changes
  503. if (device.intelamt == null) { device.intelamt = {}; }
  504. if ((typeof dev.aquired.version == 'string') && (dev.aquired.version != device.intelamt.ver)) { change = 1; log = 1; device.intelamt.ver = dev.aquired.version; changes.push('AMT version'); }
  505. if ((typeof dev.aquired.user == 'string') && (dev.aquired.user != device.intelamt.user)) { change = 1; log = 1; device.intelamt.user = dev.aquired.user; changes.push('AMT user'); }
  506. if ((typeof dev.aquired.pass == 'string') && (dev.aquired.pass != device.intelamt.pass)) { change = 1; log = 1; device.intelamt.pass = dev.aquired.pass; changes.push('AMT pass'); }
  507. if ((typeof dev.aquired.mpspass == 'string') && (dev.aquired.mpspass != device.intelamt.mpspass)) { change = 1; log = 1; device.intelamt.mpspass = dev.aquired.mpspass; changes.push('AMT MPS pass'); }
  508. if ((typeof dev.aquired.host == 'string') && (dev.aquired.host != device.intelamt.host)) { change = 1; log = 1; device.intelamt.host = dev.aquired.host; changes.push('AMT host'); }
  509. if ((typeof dev.aquired.realm == 'string') && (dev.aquired.realm != device.intelamt.realm)) { change = 1; log = 1; device.intelamt.realm = dev.aquired.realm; changes.push('AMT realm'); }
  510. if ((typeof dev.aquired.hash == 'string') && (dev.aquired.hash != device.intelamt.hash)) { change = 1; log = 1; device.intelamt.hash = dev.aquired.hash; changes.push('AMT hash'); }
  511. if ((typeof dev.aquired.tls == 'number') && (dev.aquired.tls != device.intelamt.tls)) { change = 1; log = 1; device.intelamt.tls = dev.aquired.tls; /*changes.push('AMT TLS');*/ }
  512. if ((typeof dev.aquired.state == 'number') && (dev.aquired.state != device.intelamt.state)) { change = 1; log = 1; device.intelamt.state = dev.aquired.state; changes.push('AMT state'); }
  513. // Intel AMT Warning Flags: 1 = Unknown credentials, 2 = Realm Mismatch, 4 = TLS Cert Mismatch, 8 = Trying credentials
  514. if ((typeof dev.aquired.warn == 'number')) { if ((dev.aquired.warn == 0) && (device.intelamt.warn != null)) { delete device.intelamt.warn; change = 1; } else if (dev.aquired.warn != device.intelamt.warn) { device.intelamt.warn = dev.aquired.warn; change = 1; } }
  515. // Update Intel AMT flags if needed
  516. // dev.aquired.controlMode // 1 = CCM, 2 = ACM
  517. // (node.intelamt.flags & 2) == CCM, (node.intelamt.flags & 4) == ACM
  518. var flags = 0;
  519. if (typeof device.intelamt.flags == 'number') { flags = device.intelamt.flags; }
  520. if (dev.aquired.controlMode == 1) { if ((flags & 4) != 0) { flags -= 4; } if ((flags & 2) == 0) { flags += 2; } } // CCM
  521. if (dev.aquired.controlMode == 2) { if ((flags & 4) == 0) { flags += 4; } if ((flags & 2) != 0) { flags -= 2; } } // ACM
  522. if (device.intelamt.flags != flags) { change = 1; log = 1; device.intelamt.flags = flags; changes.push('AMT flags'); }
  523. // If there are changes, event the new device
  524. if (change == 1) {
  525. // Save to the database
  526. parent.db.Set(device);
  527. // Event the node change
  528. var event = { etype: 'node', action: 'changenode', nodeid: device._id, domain: domain.id, node: parent.webserver.CloneSafeNode(device) };
  529. if (changes.length > 0) { event.msg = 'Changed device ' + device.name + ' from group ' + mesh.name + ': ' + changes.join(', '); }
  530. if ((log == 0) || ((obj.agentInfo) && (obj.agentInfo.capabilities) && (obj.agentInfo.capabilities & 0x20)) || (changes.length == 0)) { event.nolog = 1; } // If this is a temporary device, don't log changes
  531. if (parent.db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the node. Another event will come.
  532. parent.DispatchEvent(parent.webserver.CreateMeshDispatchTargets(device.meshid, [device._id]), obj, event);
  533. }
  534. }
  535. });
  536. return true;
  537. }
  538. //
  539. // General Methods
  540. //
  541. // Log the Intel AMT activation operation in the domain log
  542. obj.logAmtActivation = function (domain, x) {
  543. if (x == null) return true;
  544. // Add the password to the Intel AMT list of UUID to passwords
  545. if ((typeof x.amtUuid == 'string') && (typeof x.password == 'string')) {
  546. if (parent.amtPasswords == null) { parent.amtPasswords = {}; }
  547. if (parent.amtPasswords[x.amtUuid] == null) {
  548. parent.amtPasswords[x.amtUuid] = [x.password]; // Add password to array
  549. parent.amtPasswords = parent.common.sortObj(parent.amtPasswords);
  550. } else {
  551. if (parent.amtPasswords[x.amtUuid].indexOf(x.password) == -1) {
  552. parent.amtPasswords[x.amtUuid].unshift(x.password); // Add password at the start of the array
  553. while (parent.amtPasswords[x.amtUuid].length > 3) { parent.amtPasswords[x.amtUuid].pop(); } // Only keep the 3 last passwords for any given device
  554. }
  555. }
  556. }
  557. // Append to the log file
  558. var logpath = null;
  559. if ((domain.amtacmactivation == null) || (domain.amtacmactivation.log == null) || (typeof domain.amtacmactivation.log != 'string')) {
  560. if (domain.id == '') { logpath = parent.path.join(obj.parent.datapath, 'amtactivation.log'); } else { logpath = parent.path.join(obj.parent.datapath, 'amtactivation-' + domain.id + '.log'); }
  561. } else {
  562. logpath = parent.common.joinPath(obj.parent.datapath, domain.amtacmactivation.log);
  563. }
  564. try { parent.fs.appendFileSync(logpath, JSON.stringify(x) + '\r\n'); } catch (ex) { console.log(ex); return false; }
  565. return true;
  566. }
  567. // Called this when a task is completed, when all tasks are completed the call back function will be called.
  568. function devTaskCompleted(dev) {
  569. dev.taskCount--;
  570. if (dev.taskCount == 0) { var f = dev.taskCompleted; delete dev.taskCount; delete dev.taskCompleted; if (f != null) { f(dev); } }
  571. }
  572. // Check which key pair matches the public key in the certificate
  573. function amtcert_linkCertPrivateKey(certs, keys) {
  574. for (var i in certs) {
  575. var cert = certs[i];
  576. try {
  577. if (keys.length == 0) return;
  578. var b = obj.parent.certificateOperations.forge.asn1.fromDer(cert.X509CertificateBin);
  579. var a = obj.parent.certificateOperations.forge.pki.certificateFromAsn1(b).publicKey;
  580. var publicKeyPEM = obj.parent.certificateOperations.forge.pki.publicKeyToPem(a).substring(28 + 32).replace(/(\r\n|\n|\r)/gm, "");
  581. for (var j = 0; j < keys.length; j++) {
  582. if (publicKeyPEM === (keys[j]['DERKey'] + '-----END PUBLIC KEY-----')) {
  583. keys[j].XCert = cert; // Link the key pair to the certificate
  584. cert.XPrivateKey = keys[j]; // Link the certificate to the key pair
  585. }
  586. }
  587. } catch (e) { console.log(e); }
  588. }
  589. }
  590. function isAmtDeviceValid(dev) { return (obj.devices[dev.addr] != null); }
  591. function getInstance(x, y) { for (var i in x) { if (x[i]['InstanceID'] == y) return x[i]; } return null; }
  592. function checkAmtPassword(p) { return (p.length > 7) && (/\d/.test(p)) && (/[a-z]/.test(p)) && (/[A-Z]/.test(p)) && (/\W/.test(p)); }
  593. function getRandomAmtPassword() { var p; do { p = Buffer.from(obj.crypto.randomBytes(9), 'binary').toString('base64').split('/').join('@'); } while (checkAmtPassword(p) == false); return p; }
  594. function hex_md5(str) { return parent.crypto.createHash('md5').update(str).digest('hex'); }
  595. function Clone(v) { return JSON.parse(JSON.stringify(v)); }
  596. function parseCertName(x) {
  597. var j, r = {}, xx = x.split(',');
  598. for (var i in xx) { j = xx[i].indexOf('='); r[xx[i].substring(0, j)] = xx[i].substring(j + 1); }
  599. return r;
  600. }
  601. return obj;
  602. };