Skip to content

Commit

Permalink
Minor
Browse files Browse the repository at this point in the history
  • Loading branch information
keeramis committed Jun 17, 2024
1 parent b1e69da commit ebf1323
Showing 1 changed file with 33 additions and 45 deletions.
78 changes: 33 additions & 45 deletions src/cmd/device-protection.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,7 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
// then the device is not pretected. For now though, let's assume the device is in normal mode and not in dfu mode.

return this._withDevice(async () => {
let s;
try {
s = await this.device.getProtectionState();
} catch (error) {
if (error.message === 'Not supported') {
throw new Error(`Device protection feature is not supported on this device`);
}
}
const s = await this._getDeviceProtection();

let res;
if (!s.protected && !s.overridden) {
Expand All @@ -57,16 +50,10 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {

async disableProtection({ open } = {}) {
return this._withDevice(async () => {
let s;
try {
s = await this.device.getProtectionState();
} catch (error) {
if (error.message === 'Not supported') {
throw new Error(`Device protection feature is not supported on this device${os.EOL}`);
}
}
const s = await this._getDeviceProtection();

if (!s.protected && !s.overridden) {
this.ui.stdout.write(`Device is not protected${os.EOL}`);
this.ui.stdout.write(`Device is not protected`);
return;
}

Expand All @@ -79,22 +66,22 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
// console.log(`CLI -> Device:\n\tserver_nonce=${serverNonce.toString('base64')}`);
r = await this.device.unprotectDevice({ action: 'prepare', serverNonce });
if (!r.protected) {
console.log('Device is not protected');
return;
this.ui.stdout.write('Device is not protected');
return;
}
const { deviceNonce, deviceSignature, devicePublicKeyFingerprint } = r;
// console.log(`Device -> CLI:\n\tdevice_signature=${deviceSignature.toString('base64')}`);

// Verify the device signature and get a server signature
// console.log(`CLI -> Server:\n\tdevice_signature=${deviceSignature.toString('base64')}`);
r = await this.api.unprotectDevice({
deviceId: this.deviceId,
action: 'confirm',
serverNonce: serverNonce.toString('base64'),
deviceNonce: deviceNonce.toString('base64'),
deviceSignature: deviceSignature.toString('base64'),
devicePublicKeyFingerprint: devicePublicKeyFingerprint.toString('base64'),
auth: settings.access_token
deviceId: this.deviceId,
action: 'confirm',
serverNonce: serverNonce.toString('base64'),
deviceNonce: deviceNonce.toString('base64'),
deviceSignature: deviceSignature.toString('base64'),
devicePublicKeyFingerprint: devicePublicKeyFingerprint.toString('base64'),
auth: settings.access_token
});

const serverSignature = Buffer.from(r.server_signature, 'base64');
Expand All @@ -104,25 +91,22 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
// Unprotect the device
await this.device.unprotectDevice({ action: 'confirm', serverSignature, serverPublicKeyFingerprint });

s = await this.device.getProtectionState();
s = await this._getDeviceProtection();
if (!open) {
this.ui.stdout.write(`Device protection temporarily disabled.${os.EOL}Device is put into service mode for 20 reboots or 24 hours.${os.EOL}`);
return;
}

if (open) {

const localBootloaderPath = await this._downloadBootloader();
const localBootloaderPath = await this._downloadBootloader();

await this._flashBootloader(localBootloaderPath, 'disable');
await this._flashBootloader(localBootloaderPath, 'disable');

this.ui.stdout.write(`Device protection disabled.${os.EOL}Device is open${os.EOL}`);
this.ui.stdout.write(`Device protection disabled.${os.EOL}Device is open${os.EOL}`);

const success = await this._markAsDevelopmentDevice(true);
const success = await this._markAsDevelopmentDevice(true);

if (!success) {
this.ui.stdout.write(`Failed to mark device as development device. Protection will be automatically enabled after a power cycle${os.EOL}`);
}
if (!success) {
this.ui.stdout.write(`Failed to mark device as development device. Protection will be automatically enabled after a power cycle${os.EOL}`);
}
});
}
Expand Down Expand Up @@ -155,15 +139,8 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
let protectedBinary = file;

return this._withDevice(async () => {
let s;
try {
s = await this.device.getProtectionState();
} catch (error) {
if (error.message === 'Not supported') {
throw new Error(`Device protection feature is not supported on this device`);
}
}

const s = await this._getDeviceProtection();

const attrs = await this.api.getDeviceAttributes(this.deviceId);
let deviceProtectionActiveInProduct = false;
if (attrs.platform_id !== attrs.product_id) {
Expand Down Expand Up @@ -209,6 +186,17 @@ module.exports = class DeviceProtectionCommands extends CLICommandBase {
});
}

async _getDeviceProtection() {
try {
const s = await this.device.getProtectionState();
return s;
} catch (error) {
if (error.message === 'Not supported') {
throw new Error(`Device protection feature is not supported on this device${os.EOL}`);
}
}
}

async _flashBootloader(path, action) {
let msg;
switch (action) {
Expand Down

0 comments on commit ebf1323

Please sign in to comment.