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

Add group key mgmt cluster #270

Merged
merged 5 commits into from
Mar 11, 2023
Merged
Changes from 2 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
135 changes: 135 additions & 0 deletions src/matter/cluster/GroupKeyManagementCluster.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/**
* @license
* Copyright 2022 The node-matter Authors
* SPDX-License-Identifier: Apache-2.0
*/

import { WritableAttribute, Attribute, Cluster, Command, TlvNoArguments, TlvNoResponse } from "./Cluster";
import { StatusCode } from "../interaction/InteractionMessages";
import { TlvGroupId } from "../common/GroupId";
import { BitFlag, MatterApplicationClusterSpecificationV1_0, TlvArray, TlvEnum, TlvField, TlvNullable, TlvObject, TlvString, TlvUInt16, TlvInt64 } from "@project-chip/matter.js";
import { TlvEndpointNumber } from "../common/EndpointNumber";


/** @see {@link MatterApplicationClusterSpecificationV1_0} § 11.2.6.1 */
JimBuzbee marked this conversation as resolved.
Show resolved Hide resolved
const TlvGroupKeyMapStruct = TlvObject({
JimBuzbee marked this conversation as resolved.
Show resolved Hide resolved
groupId: TlvField(0, TlvGroupId), /* min: 1 */
JimBuzbee marked this conversation as resolved.
Show resolved Hide resolved
groupKeySetId: TlvField(1, TlvUInt16),
});

/** @see {@link MatterApplicationClusterSpecificationV1_0} § 11.2.6.2 */
const TlvGroupKeySetStruct = TlvObject({
groupKeySetID: TlvField(0, TlvUInt16),
groupKeySecurityPolicy: TlvField(1, TlvUInt16),
JimBuzbee marked this conversation as resolved.
Show resolved Hide resolved

epochKey0: TlvField(2, TlvNullable(TlvString.bound({ maxLength: 16 }))),
epochStartTime0: TlvField(3, TlvNullable(TlvInt64)), // epoch_us
JimBuzbee marked this conversation as resolved.
Show resolved Hide resolved

epochKey1: TlvField(4, TlvNullable(TlvString.bound({ maxLength: 16 }))),
epochStartTime1: TlvField(5, TlvNullable(TlvInt64)), // epoch_us

epochKey2: TlvField(6, TlvNullable(TlvString.bound({ maxLength: 16 }))),
epochStartTime2: TlvField(7, TlvNullable(TlvInt64)), // epoch_us
});
JimBuzbee marked this conversation as resolved.
Show resolved Hide resolved

/** @see {@link MatterApplicationClusterSpecificationV1_0} § 11.2.6.3 */
const TlvGroupInfoMapStruct = TlvObject({
groupId: TlvField(1, TlvGroupId), /* min: 1 */
endPoints: TlvField(2, TlvArray(TlvEndpointNumber)),
JimBuzbee marked this conversation as resolved.
Show resolved Hide resolved
groupName: TlvField(3, TlvString.bound( { maxLength: 16 })),
JimBuzbee marked this conversation as resolved.
Show resolved Hide resolved
});

/** @see {@link MatterApplicationClusterSpecificationV1_0} § 11.2.6.2 table 87 */
export const enum GroupKeySecurityPolicyEnum {
/** Message counter synchronization using trust-first */
TrustFirst = 0x00,

/** Message counter synchronization using cache-and-sync */
CacheAndSync = 0x01,
}

/** @see {@link MatterApplicationClusterSpecificationV1_0} § 11.2.9.1 */
const TlvKeySetWriteRequest= TlvObject({
groupKeySet: TlvField(0, TlvGroupKeySetStruct, )
JimBuzbee marked this conversation as resolved.
Show resolved Hide resolved
});

/** @see {@link MatterApplicationClusterSpecificationV1_0} § 11.2.9.1 */
const TlvKeySetWriteResponse= TlvObject({
JimBuzbee marked this conversation as resolved.
Show resolved Hide resolved
status: TlvField(0, TlvEnum<StatusCode>()),
});

/** @see {@link MatterApplicationClusterSpecificationV1_0} § 11.2.9.2 */
const TlvKeySetReadRequest= TlvObject({
groupKeySetId: TlvField(0, TlvUInt16, )
});

/** @see {@link MatterApplicationClusterSpecificationV1_0} § 11.2.9.3 */
const TlvKeySetReadResponse= TlvObject({
groupKeySet: TlvField(0, TlvGroupKeySetStruct),
});

/** @see {@link MatterApplicationClusterSpecificationV1_0} § 11.2.9.4 */
const TlvKeySetRemoveRequest= TlvObject({
groupKeySetId: TlvField(0, TlvUInt16, )
});

/** @see {@link MatterApplicationClusterSpecificationV1_0} § 11.2.9.4 */
const TlvKeySetRemoveResponse= TlvObject({
JimBuzbee marked this conversation as resolved.
Show resolved Hide resolved
status: TlvField(0, TlvEnum<StatusCode>()), //TODO
});

/** @see {@link MatterApplicationClusterSpecificationV1_0} § 11.2.9.5 */
const TlvKeySetReadAllIndicesRequest= TlvObject({
JimBuzbee marked this conversation as resolved.
Show resolved Hide resolved
// Spec says no payload which seems correct but chip XML shows the following
// groupKeySetIds: TlvField(0, TlvArray(TlvUInt16), )
});

/** @see {@link MatterApplicationClusterSpecificationV1_0} §1 1.2.9.6 */
const TlvKeySetReadAllIndicesResponse= TlvObject({
groupKeySetIds: TlvField(0, TlvArray(TlvUInt16), )
});

/**
* The Group Key Management Cluster is the mechanism by which group keys are managed.
*
* @see {@link MatterApplicationClusterSpecificationV1_0} § 11.2
*/
export const GroupKeyManagementCluster = Cluster({
id: 0x3f,
name: "GroupKeyManagement",
revision: 1,
features: {
/** The ability to support CacheAndSync security policy and MCSP. */
cacheAndSync: BitFlag(0),
},

/** @see {@link MatterApplicationClusterSpecificationV1_0} § 11.2.7.1 */
attributes: {
/** Each entry associates a logical Group Id with a particular group key set. */
groupKeyMap: WritableAttribute(0, TlvArray(TlvGroupKeyMapStruct, { maxLength: 254 })),
JimBuzbee marked this conversation as resolved.
Show resolved Hide resolved

/** Each entry provides read-only information about how a given logical Group ID maps to a particular set of endpoints */
groupTable: Attribute(1, TlvArray(TlvGroupInfoMapStruct, { maxLength: 254 })),
JimBuzbee marked this conversation as resolved.
Show resolved Hide resolved

/** Maximum number of groups that this node supports per fabric */
maxGroupsPerFabric: Attribute(2, TlvUInt16),
JimBuzbee marked this conversation as resolved.
Show resolved Hide resolved

/** Maximum number of group key sets this node supports per fabric */
maxGroupKeysPerFabric: Attribute(3, TlvUInt16),
JimBuzbee marked this conversation as resolved.
Show resolved Hide resolved
},

/** @see {@link MatterApplicationClusterSpecificationV1_0} § 11.2.9 */
commands: {
/** Set the state of a given Group Key Set,including atomically updating the state of all epoch keys */
keySetWrite: Command(0, TlvKeySetWriteRequest, 0, TlvKeySetWriteResponse),
JimBuzbee marked this conversation as resolved.
Show resolved Hide resolved

/** Read the state of a given Group Key Set */
keySetRead: Command(1, TlvKeySetReadRequest, 1, TlvKeySetReadResponse),
JimBuzbee marked this conversation as resolved.
Show resolved Hide resolved

/** Remove all state of a given Group Key Set */
keySetRemove: Command(3, TlvKeySetRemoveRequest, 3, TlvKeySetRemoveResponse),
JimBuzbee marked this conversation as resolved.
Show resolved Hide resolved

/** Query a list of all Group Key Sets associated with the accessing fabric */
keySetReadAllIndices: Command(5, TlvKeySetReadAllIndicesRequest, 5, TlvKeySetReadAllIndicesResponse),
JimBuzbee marked this conversation as resolved.
Show resolved Hide resolved
}
});