Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some more backup support for EZSP 13+ #888

Merged
merged 1 commit into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions src/adapter/ezsp/adapter/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
import Debug from "debug";
import {Driver} from '../driver';
import * as Models from "../../../models";
import {EmberKeyType, EmberKeyStruct, EmberNetworkParameters} from '../driver/types';
import {
EmberKeyType,
EmberKeyStruct,
EmberNetworkParameters,
EmberSecurityManagerNetworkKeyInfo,
EmberKeyData
} from '../driver/types';
import {channelsMask2list} from '../driver/utils';


Expand All @@ -20,30 +26,48 @@ export class EZSPAdapterBackup {
this.debug("creating backup");
const version: number = await this.driver.ezsp.version();
const linkResult = await this.driver.getKey(EmberKeyType.TRUST_CENTER_LINK_KEY);
const trustCenterLinkKey: EmberKeyStruct = linkResult.keyStruct;
const netParams = await this.driver.ezsp.execCommand('getNetworkParameters');
const networkParams: EmberNetworkParameters = netParams.parameters;
const netResult = await this.driver.getKey(EmberKeyType.CURRENT_NETWORK_KEY);
const networkKey: EmberKeyStruct = netResult.keyStruct;
let tclKey: Buffer = null;
let netKey: Buffer = null;
let netKeySequenceNumber: number = 0;
let netKeyFrameCounter: number = 0;

if (version < 13) {
tclKey = Buffer.from((linkResult.keyStruct as EmberKeyStruct).key.contents);
netKey = Buffer.from((netResult.keyStruct as EmberKeyStruct).key.contents);
netKeySequenceNumber = (netResult.keyStruct as EmberKeyStruct).sequenceNumber;
netKeyFrameCounter = (netResult.keyStruct as EmberKeyStruct).outgoingFrameCounter;
} else {
tclKey = Buffer.from((linkResult.keyData as EmberKeyData).contents);
netKey = Buffer.from((netResult.keyData as EmberKeyData).contents);
// get rest of info from second cmd in EZSP 13+
const netKeyInfoResult = await this.driver.getNetworkKeyInfo();
const networkKeyInfo : EmberSecurityManagerNetworkKeyInfo = netKeyInfoResult.networkKeyInfo;
netKeySequenceNumber = networkKeyInfo.networkKeySequenceNumber;
netKeyFrameCounter = networkKeyInfo.networkKeyFrameCounter;
}

const ieee = (await this.driver.ezsp.execCommand('getEui64')).eui64;
/* return backup structure */
/* istanbul ignore next */
return {
ezsp: {
version: version,
hashed_tclk: Buffer.from(trustCenterLinkKey.key.contents),
hashed_tclk: tclKey,
},
networkOptions: {
panId: networkParams.panId,
extendedPanId: Buffer.from(networkParams.extendedPanId),
channelList: channelsMask2list(networkParams.channels),
networkKey: Buffer.from(networkKey.key.contents),
networkKey: netKey,
networkKeyDistribute: true,
},
logicalChannel: networkParams.radioChannel,
networkKeyInfo: {
sequenceNumber: networkKey.sequenceNumber,
frameCounter: networkKey.outgoingFrameCounter
sequenceNumber: netKeySequenceNumber,
frameCounter: netKeyFrameCounter
},
securityLevel: 5,
networkUpdateId: networkParams.nwkUpdateId,
Expand Down
12 changes: 11 additions & 1 deletion src/adapter/ezsp/driver/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ import {/* Basic Types */
EmberNeighbors,
EmberRoutingTable,
EmberSecurityManagerContext,
EmberSecurityManagerNetworkKeyInfo,
SLStatus,
} from './types';

/* eslint-disable-next-line @typescript-eslint/no-explicit-any*/
Expand Down Expand Up @@ -1191,7 +1193,15 @@ export const FRAMES: {[key: string]: EZSPFrameDesc} = {
},
response: {
keyData: EmberKeyData,
status: EmberStatus
status: SLStatus,
},
},
getNetworkKeyInfo: {
ID: 0x0116,
request: null,
response: {
status: SLStatus,
networkKeyInfo: EmberSecurityManagerNetworkKeyInfo,
},
},
switchNetworkKeyHandler: {
Expand Down
19 changes: 17 additions & 2 deletions src/adapter/ezsp/driver/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
EmberKeyType,
EmberDerivedKeyType,
EmberStackError,
SLStatus,
} from './types/named';
import {Multicast} from './multicast';
import {Waitress, Wait} from '../../../utils';
Expand Down Expand Up @@ -860,9 +861,23 @@ export class Driver extends EventEmitter {
smc.flags = 0;
smc.psaKeyAlgPermission = 0;
const keyInfo = await this.ezsp.execCommand('exportKey', {context: smc});
console.assert(keyInfo.status === EmberStatus.SUCCESS,
console.assert(keyInfo.status === SLStatus.SL_STATUS_OK,
`exportKey(${EmberKeyType.valueToName(EmberKeyType, keyType)}) `
+ `returned unexpected status: ${keyInfo.status}`);
+ `returned unexpected SL status: ${keyInfo.status}`);
return keyInfo;
}
}

public async getNetworkKeyInfo(): Promise<EZSPFrameData> {
if (this.ezsp.ezspV < 13) {
throw new Error(`getNetKeyInfo(): Invalid call on EZSP < 13.`);
} else {
const keyInfo = await this.ezsp.execCommand('getNetworkKeyInfo');
console.assert(
keyInfo.status === SLStatus.SL_STATUS_OK,
`getNetworkKeyInfo() returned unexpected SL status: ${keyInfo.status}`
);

return keyInfo;
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/adapter/ezsp/driver/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
EzspMfgTokenId,
EzspStatus,
EmberStatus,
SLStatus,
EmberStackError,
EmberEventUnits,
EmberNodeType,
Expand Down Expand Up @@ -117,6 +118,7 @@ import {
EmberRoutingTable,
EmberRoutingTableEntry,
EmberSecurityManagerContext,
EmberSecurityManagerNetworkKeyInfo,
} from './struct';

/* eslint-disable-next-line @typescript-eslint/no-explicit-any*/
Expand Down Expand Up @@ -178,6 +180,7 @@ export {
EzspMfgTokenId,
EzspStatus,
EmberStatus,
SLStatus,
EmberStackError,
EmberEventUnits,
EmberNodeType,
Expand Down Expand Up @@ -251,4 +254,5 @@ export {
EmberRoutingTable,
EmberRoutingTableEntry,
EmberSecurityManagerContext,
EmberSecurityManagerNetworkKeyInfo,
};
Loading