1. Updated merge with previous commit 2. Fixed bug in getCommand()

1. Updated merge with previous commit 2. Fixed bug in getCommand()

Bryan Roe committed Jul 14, 2021 at 10:49 UTC 48587408943668f06c4371fd71d6e9537e16788a
1 file changed +89 -43
agents/modules_meshcmd/amt-mei.js
+89 -43
@@ -1,5 +1,5 @@
1 /*
2 -Copyright 2018-2020 Intel Corporation
2 +Copyright 2018-2021 Intel Corporation
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
@@ -108,7 +108,7 @@ function amt_heci()
108
109 function trim(x) { var y = x.indexOf('\0'); if (y >= 0) { return x.substring(0, y); } else { return x; } }
110 this.getCommand = function getCommand(chunk) {
111 - var command = chunk.length == 0 ? (this._rq.peekQueue().cmd | 0x800000) : chunk.readUInt32LE(4);
111 + var command = chunk.length == 0 ? (g_internal._rq.peekQueue().cmd | 0x800000) : chunk.readUInt32LE(4);
112 var ret = { IsResponse: (command & 0x800000) == 0x800000 ? true : false, Command: (command & 0x7FFFFF), Status: chunk.length != 0 ? chunk.readUInt32LE(12) : -1, Data: chunk.length != 0 ? chunk.slice(16) : null };
113 return (ret);
114 };
@@ -332,34 +332,27 @@ function amt_heci()
332 this.getLocalSystemAccount = function getLocalSystemAccount(callback) {
333 var optional = [];
334 for (var i = 1; i < arguments.length; ++i) { optional.push(arguments[i]); }
335 - this.sendCommand(103, Buffer.alloc(40), function (header, fn, opt)
336 - {
337 - if (header.Status == 0 && header.Data.length == 68)
338 - {
335 + this.sendCommand(103, Buffer.alloc(40), function (header, fn, opt) {
336 + if (header.Status == 0 && header.Data.length == 68) {
337 opt.unshift({ user: trim(header.Data.slice(0, 33).toString()), pass: trim(header.Data.slice(33, 67).toString()), raw: header.Data });
338 }
341 - else
342 - {
339 + else {
340 opt.unshift(null);
341 }
342 fn.apply(this, opt);
343 }, callback, optional);
344 }
348 - this.getLanInterfaceSettings = function getLanInterfaceSettings(index, callback)
349 - {
345 + this.getLanInterfaceSettings = function getLanInterfaceSettings(index, callback) {
346 var optional = [];
347 for (var i = 2; i < arguments.length; ++i) { optional.push(arguments[i]); }
348 var ifx = Buffer.alloc(4);
349 ifx.writeUInt32LE(index);
354 - this.sendCommand(0x48, ifx, function onGetLanInterfaceSettings(header, fn, opt)
355 - {
356 - if(header.Status == 0)
357 - {
350 + this.sendCommand(0x48, ifx, function onGetLanInterfaceSettings(header, fn, opt) {
351 + if (header.Status == 0) {
352 var info = {};
353 info.enabled = header.Data.readUInt32LE(0);
354 info.dhcpEnabled = header.Data.readUInt32LE(8);
361 - switch(header.Data[12])
362 - {
355 + switch (header.Data[12]) {
356 case 1:
357 info.dhcpMode = 'ACTIVE'
358 break;
@@ -371,14 +364,13 @@ function amt_heci()
364 break;
365 }
366 info.mac = header.Data.slice(14).toString('hex:');
374 -
367 +
368 var addr = header.Data.readUInt32LE(4);
369 info.address = ((addr >> 24) & 255) + '.' + ((addr >> 16) & 255) + '.' + ((addr >> 8) & 255) + '.' + (addr & 255);
370 opt.unshift(info);
371 fn.apply(this, opt);
372 }
380 - else
381 - {
373 + else {
374 opt.unshift(null);
375 fn.apply(this, opt);
376 }
@@ -395,32 +387,32 @@ function amt_heci()
387 fn.apply(this, opt);
388 }, callback, optional);
389 }
398 - this.startConfiguration = function startConfiguration() {
390 + this.startConfiguration = function startConfiguration(callback) {
391 var optional = [];
400 - for (var i = 2; i < arguments.length; ++i) { optional.push(arguments[i]); }
401 - this.sendCommand(0x29, data, function (header, fn, opt) { opt.unshift(header.Status); fn.apply(this, opt); }, callback, optional);
392 + for (var i = 1; i < arguments.length; ++i) { optional.push(arguments[i]); }
393 + this.sendCommand(0x29, null, function (header, fn, opt) { opt.unshift(header.Status); fn.apply(this, opt); }, callback, optional);
394 }
403 - this.stopConfiguration = function stopConfiguration() {
395 + this.stopConfiguration = function stopConfiguration(callback) {
396 var optional = [];
405 - for (var i = 2; i < arguments.length; ++i) { optional.push(arguments[i]); }
406 - this.sendCommand(0x5E, data, function (header, fn, opt) { opt.unshift(header.Status); fn.apply(this, opt); }, callback, optional);
397 + for (var i = 1; i < arguments.length; ++i) { optional.push(arguments[i]); }
398 + this.sendCommand(0x5E, null, function (header, fn, opt) { opt.unshift(header.Status); fn.apply(this, opt); }, callback, optional);
399 }
408 - this.openUserInitiatedConnection = function openUserInitiatedConnection() {
400 + this.openUserInitiatedConnection = function openUserInitiatedConnection(callback) {
401 var optional = [];
410 - for (var i = 2; i < arguments.length; ++i) { optional.push(arguments[i]); }
411 - this.sendCommand(0x44, data, function (header, fn, opt) { opt.unshift(header.Status); fn.apply(this, opt); }, callback, optional);
402 + for (var i = 1; i < arguments.length; ++i) { optional.push(arguments[i]); }
403 + this.sendCommand(0x44, null, function (header, fn, opt) { opt.unshift(header.Status); fn.apply(this, opt); }, callback, optional);
404 }
413 - this.closeUserInitiatedConnection = function closeUnserInitiatedConnected() {
405 + this.closeUserInitiatedConnection = function closeUnserInitiatedConnected(callback) {
406 var optional = [];
415 - for (var i = 2; i < arguments.length; ++i) { optional.push(arguments[i]); }
416 - this.sendCommand(0x45, data, function (header, fn, opt) { opt.unshift(header.Status); fn.apply(this, opt); }, callback, optional);
407 + for (var i = 1; i < arguments.length; ++i) { optional.push(arguments[i]); }
408 + this.sendCommand(0x45, null, function (header, fn, opt) { opt.unshift(header.Status); fn.apply(this, opt); }, callback, optional);
409 }
418 - this.getRemoteAccessConnectionStatus = function getRemoteAccessConnectionStatus() {
410 + this.getRemoteAccessConnectionStatus = function getRemoteAccessConnectionStatus(callback) {
411 var optional = [];
420 - for (var i = 2; i < arguments.length; ++i) { optional.push(arguments[i]); }
421 - this.sendCommand(0x46, data, function (header, fn, opt) {
412 + for (var i = 1; i < arguments.length; ++i) { optional.push(arguments[i]); }
413 + this.sendCommand(0x46, null, function (header, fn, opt) {
414 if (header.Status == 0) {
423 - var hostname = v.slice(14, header.Data.readUInt16LE(12) + 14).toString()
415 + var hostname = header.Data.slice(14, header.Data.readUInt16LE(12) + 14).toString()
416 opt.unshift({ status: header.Status, networkStatus: header.Data.readUInt32LE(0), remoteAccessStatus: header.Data.readUInt32LE(4), remoteAccessTrigger: header.Data.readUInt32LE(8), mpsHostname: hostname, raw: header.Data });
417 } else {
418 opt.unshift({ status: header.Status });
@@ -428,27 +420,81 @@ function amt_heci()
420 fn.apply(this, opt);
421 }, callback, optional);
422 }
431 - this.getProtocolVersion = function getProtocolVersion(callback)
432 - {
423 + this.getProtocolVersion = function getProtocolVersion(callback) {
424 var optional = [];
425 for (var i = 1; i < arguments.length; ++i) { opt.push(arguments[i]); }
426
436 - if (!this._tmpSession) { this._tmpSession = heci.create(); this._tmpSession.parent = this;}
437 - this._tmpSession.doIoctl(heci.IOCTL.HECI_VERSION, Buffer.alloc(5), Buffer.alloc(5), function (status, buffer, self, fn, opt)
438 - {
427 + if (!this._tmpSession) { this._tmpSession = heci.create(); this._tmpSession.parent = this; }
428 + this._tmpSession.doIoctl(heci.IOCTL.HECI_VERSION, Buffer.alloc(5), Buffer.alloc(5), function (status, buffer, self, fn, opt) {
429 if (status == 0) {
430 var result = buffer.readUInt8(0).toString() + '.' + buffer.readUInt8(1).toString() + '.' + buffer.readUInt8(2).toString() + '.' + buffer.readUInt16BE(3).toString();
431 opt.unshift(result);
432 fn.apply(self, opt);
433 }
444 - else
445 - {
434 + else {
435 opt.unshift(null);
436 fn.apply(self, opt);
437 }
438
439 }, this, callback, optional);
440 }
441 + this.startConfigurationHBased = function startConfigurationHBased(certHash, hostVpn, dnsSuffixList, func) {
442 + if ((certHash == null) || ((certHash.length != 32) && (certHash.length != 48))) { func({ status: -101 }); }
443 + this.stopConfiguration(function (status) {
444 + if (status == 0) {
445 + // We stopped the configuration, wait 20 seconds before starting up again.
446 + var f = function tf() { delete tf.parent.xtimeout; tf.parent.startConfigurationHBasedEx(certHash, hostVpn, dnsSuffixList, func); }
447 + f.parent = this;
448 + this.xtimeout = setTimeout(f, 20000);
449 + } else {
450 + // We are not in the connect mode, this is good, start configuration right away.
451 + this.startConfigurationHBasedEx(certHash, hostVpn, dnsSuffixList, func);
452 + }
453 + })
454 + }
455 + this.startConfigurationHBasedEx = function startConfigurationHBased(certHash, hostVpn, dnsSuffixList, func) {
456 + var optional = [];
457 + for (var i = 4; i < arguments.length; ++i) { optional.push(arguments[i]); }
458 +
459 + // Format the command
460 + var data = Buffer.alloc(4 + 64 + 4 + 4 + 320);
461 + data.writeUInt32LE((certHash.length == 48) ? 3 : 2, 0); // Write certificate hash type: SHA256 = 2, SHA384 = 3
462 + certHash.copy(data, 4); // Write the hash
463 + data.writeUInt32LE(hostVpn ? 1 : 0, 68); // Write is HostVPN is enabled
464 + if (dnsSuffixList != null) {
465 + data.writeUInt32LE(dnsSuffixList.length, 72); // Write the number of DNS Suffix, from 0 to 4
466 + var ptr = 76;
467 + for (var i = 0; i < dnsSuffixList.length; i++) { ptr += data.write(dnsSuffixList[i], ptr) + 1; } // Write up to 4 DNS Suffix with null seperation.
468 + }
469 +
470 + // Send the command
471 + this.sendCommand(139, data, function (header, fn, opt) {
472 + if (header.Status == 0) {
473 + var amtHash = null;
474 + if (header.Data[0] == 2) { amtHash = header.Data.slice(1, 33); } // SHA256
475 + if (header.Data[0] == 3) { amtHash = header.Data.slice(1, 49); } // SHA384
476 + opt.unshift({ status: header.Status, hash: amtHash.toString('hex') });
477 + } else {
478 + opt.unshift({ status: header.Status });
479 + }
480 + fn.apply(this, opt);
481 + }, func, optional);
482 + }
483 }
484
454 -module.exports = amt_heci;
\ No newline at end of file
485 +module.exports = amt_heci;
486 +
487 +
488 +/*
489 +AMT_STATUS_SUCCESS = 0,
490 +AMT_STATUS_INTERNAL_ERROR = 1,
491 +AMT_STATUS_INVALID_AMT_MODE = 3,
492 +AMT_STATUS_INVALID_MESSAGE_LENGTH = 4,
493 +AMT_STATUS_MAX_LIMIT_REACHED = 23,
494 +AMT_STATUS_INVALID_PARAMETER = 36,
495 +AMT_STATUS_RNG_GENERATION_IN_PROGRESS = 47,
496 +AMT_STATUS_RNG_NOT_READY = 48,
497 +AMT_STATUS_CERTIFICATE_NOT_READY = 49,
498 +AMT_STATUS_INVALID_HANDLE = 2053
499 +AMT_STATUS_NOT_FOUND = 2068,
500 +*/
\ No newline at end of file