Skip to content

Commit 4748606

Browse files
cecillerestyled-commitsbzbarsky-apple
authored andcommitted
Remove GroupKeyMap entries for removed KeySet (#23864)
* Remove GroupKeyMap entries for removed KeySet Per Core spec 11.2.9.4, If there exist any entries for the accessing fabric within the GroupKeyMap attribute that refer to the GroupKeySetID just removed, then these entries SHALL be removed from that list. Fixes #23862 * Restyled by clang-format * Restyled by prettier-yaml * Update src/credentials/GroupDataProviderImpl.cpp Co-authored-by: Boris Zbarsky <[email protected]> Co-authored-by: Restyled.io <[email protected]> Co-authored-by: Boris Zbarsky <[email protected]>
1 parent aff6146 commit 4748606

File tree

4 files changed

+582
-3
lines changed

4 files changed

+582
-3
lines changed

src/app/tests/suites/TestGroupKeyManagementCluster.yaml

+74
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,77 @@ tests:
295295
value: 0x01a2
296296
response:
297297
error: NOT_FOUND
298+
299+
- label: "KeySet Write 1"
300+
command: "KeySetWrite"
301+
arguments:
302+
values:
303+
- name: "GroupKeySet"
304+
value:
305+
{
306+
GroupKeySetID: 0x01a1,
307+
GroupKeySecurityPolicy: 0,
308+
EpochKey0: "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf",
309+
EpochStartTime0: 1110000,
310+
EpochKey1: "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf",
311+
EpochStartTime1: 1110001,
312+
EpochKey2: "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf",
313+
EpochStartTime2: 1110002,
314+
}
315+
316+
- label: "KeySet Write 2"
317+
command: "KeySetWrite"
318+
arguments:
319+
values:
320+
- name: "GroupKeySet"
321+
value:
322+
{
323+
GroupKeySetID: 0x01a2,
324+
GroupKeySecurityPolicy: 1,
325+
EpochKey0: "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf",
326+
EpochStartTime0: 2110000,
327+
EpochKey1: "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef",
328+
EpochStartTime1: 2110001,
329+
EpochKey2: "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff",
330+
EpochStartTime2: 2110002,
331+
}
332+
333+
- label: "Map Group 1 and Group 2 to KeySet 1 and group 2 to KeySet 2"
334+
command: "writeAttribute"
335+
attribute: "GroupKeyMap"
336+
arguments:
337+
value:
338+
[
339+
{ FabricIndex: 1, GroupId: 0x0101, GroupKeySetID: 0x01a1 },
340+
{ FabricIndex: 1, GroupId: 0x0102, GroupKeySetID: 0x01a2 },
341+
{ FabricIndex: 1, GroupId: 0x0102, GroupKeySetID: 0x01a1 },
342+
]
343+
344+
- label: "Remove keyset 1"
345+
command: "KeySetRemove"
346+
arguments:
347+
values:
348+
- name: "GroupKeySetID"
349+
value: 0x01a1
350+
351+
- label: "TH verifies GroupKeyMap entries for KeySet 1 have been removed"
352+
cluster: "Group Key Management"
353+
endpoint: 0
354+
command: "readAttribute"
355+
attribute: "GroupKeyMap"
356+
response:
357+
value: [{ FabricIndex: 1, GroupId: 0x0102, GroupKeySetID: 0x01a2 }]
358+
- label: "Remove keyset 2"
359+
command: "KeySetRemove"
360+
arguments:
361+
values:
362+
- name: "GroupKeySetID"
363+
value: 0x01a2
364+
365+
- label: "TH verifies GroupKeyMap entries for KeySet 2 have been removed"
366+
cluster: "Group Key Management"
367+
endpoint: 0
368+
command: "readAttribute"
369+
attribute: "GroupKeyMap"
370+
response:
371+
value: []

src/credentials/GroupDataProviderImpl.cpp

+50-1
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,37 @@ struct KeyMapData : public GroupDataProvider::GroupKey, LinkedData
503503
id = static_cast<uint16_t>(max_id + 1);
504504
return false;
505505
}
506+
507+
// returns index if the find_id is found, otherwise std::numeric_limits<size_t>::max
508+
size_t Find(PersistentStorageDelegate * storage, const FabricData & fabric, const KeysetId find_id)
509+
{
510+
fabric_index = fabric.fabric_index;
511+
id = fabric.first_map;
512+
max_id = 0;
513+
index = 0;
514+
first = true;
515+
516+
while (index < fabric.map_count)
517+
{
518+
if (CHIP_NO_ERROR != Load(storage))
519+
{
520+
break;
521+
}
522+
if (keyset_id == find_id)
523+
{
524+
// Match found
525+
return index;
526+
}
527+
max_id = std::max(id, max_id);
528+
first = false;
529+
prev = id;
530+
id = next;
531+
index++;
532+
}
533+
534+
id = static_cast<uint16_t>(max_id + 1);
535+
return std::numeric_limits<size_t>::max();
536+
}
506537
};
507538

508539
struct EndpointData : GroupDataProvider::GroupEndpoint, PersistentData<kPersistentBufferMax>
@@ -1574,7 +1605,25 @@ CHIP_ERROR GroupDataProviderImpl::RemoveKeySet(chip::FabricIndex fabric_index, u
15741605
fabric.keyset_count--;
15751606
}
15761607
// Update fabric info
1577-
return fabric.Save(mStorage);
1608+
ReturnErrorOnFailure(fabric.Save(mStorage));
1609+
1610+
// Removing a key set also removes the associated group mappings
1611+
KeyMapData map;
1612+
uint16_t original_count = fabric.map_count;
1613+
for (uint16_t i = 0; i < original_count; ++i)
1614+
{
1615+
fabric.Load(mStorage);
1616+
size_t idx = map.Find(mStorage, fabric, target_id);
1617+
if (idx == std::numeric_limits<size_t>::max())
1618+
{
1619+
break;
1620+
}
1621+
// NOTE: It's unclear what should happen here if we have removed the key set
1622+
// and possibly some mappings before failing. For now, ignoring errors, but
1623+
// open to suggestsions for the correct behavior.
1624+
RemoveGroupKeyAt(fabric_index, idx);
1625+
}
1626+
return CHIP_NO_ERROR;
15781627
}
15791628

15801629
GroupDataProvider::KeySetIterator * GroupDataProviderImpl::IterateKeySets(chip::FabricIndex fabric_index)

zzz_generated/chip-tool/zap-generated/test/Commands.h

+173-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)