forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GroupDataProviderImpl.cpp
1976 lines (1688 loc) · 65.3 KB
/
GroupDataProviderImpl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* Copyright (c) 2021-2022 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <credentials/GroupDataProviderImpl.h>
#include <crypto/CHIPCryptoPAL.h>
#include <lib/core/TLV.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/CommonPersistentData.h>
#include <lib/support/DefaultStorageKeyAllocator.h>
#include <lib/support/PersistentData.h>
#include <lib/support/Pool.h>
#include <stdlib.h>
namespace chip {
namespace Credentials {
using GroupInfo = GroupDataProvider::GroupInfo;
using GroupKey = GroupDataProvider::GroupKey;
using GroupEndpoint = GroupDataProvider::GroupEndpoint;
using EpochKey = GroupDataProvider::EpochKey;
using KeySet = GroupDataProvider::KeySet;
using GroupSession = GroupDataProvider::GroupSession;
struct FabricList : public CommonPersistentData::FabricList
{
CHIP_ERROR UpdateKey(StorageKeyName & key) override
{
key = DefaultStorageKeyAllocator::GroupFabricList();
return CHIP_NO_ERROR;
}
};
constexpr size_t kPersistentBufferMax = 128;
struct LinkedData : public PersistentData<kPersistentBufferMax>
{
static constexpr uint16_t kMinLinkId = 1;
LinkedData() = default;
LinkedData(uint16_t linked_id) : id(linked_id) {}
uint16_t id = kMinLinkId;
uint16_t index = 0;
uint16_t next = 0;
uint16_t prev = 0;
uint16_t max_id = 0;
bool first = true;
};
struct FabricData : public PersistentData<kPersistentBufferMax>
{
static constexpr TLV::Tag TagFirstGroup() { return TLV::ContextTag(1); }
static constexpr TLV::Tag TagGroupCount() { return TLV::ContextTag(2); }
static constexpr TLV::Tag TagFirstMap() { return TLV::ContextTag(3); }
static constexpr TLV::Tag TagMapCount() { return TLV::ContextTag(4); }
static constexpr TLV::Tag TagFirstKeyset() { return TLV::ContextTag(5); }
static constexpr TLV::Tag TagKeysetCount() { return TLV::ContextTag(6); }
static constexpr TLV::Tag TagNext() { return TLV::ContextTag(7); }
chip::FabricIndex fabric_index = kUndefinedFabricIndex;
chip::GroupId first_group = kUndefinedGroupId;
uint16_t group_count = 0;
uint16_t first_map = 0;
uint16_t map_count = 0;
chip::KeysetId first_keyset = kInvalidKeysetId;
uint16_t keyset_count = 0;
chip::FabricIndex next = kUndefinedFabricIndex;
FabricData() = default;
FabricData(chip::FabricIndex fabric) : fabric_index(fabric) {}
CHIP_ERROR UpdateKey(StorageKeyName & key) override
{
VerifyOrReturnError(kUndefinedFabricIndex != fabric_index, CHIP_ERROR_INVALID_FABRIC_INDEX);
key = DefaultStorageKeyAllocator::FabricGroups(fabric_index);
return CHIP_NO_ERROR;
}
void Clear() override
{
first_group = kUndefinedGroupId;
group_count = 0;
first_keyset = kInvalidKeysetId;
keyset_count = 0;
next = kUndefinedFabricIndex;
}
CHIP_ERROR Serialize(TLV::TLVWriter & writer) const override
{
TLV::TLVType container;
ReturnErrorOnFailure(writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, container));
ReturnErrorOnFailure(writer.Put(TagFirstGroup(), static_cast<uint16_t>(first_group)));
ReturnErrorOnFailure(writer.Put(TagGroupCount(), static_cast<uint16_t>(group_count)));
ReturnErrorOnFailure(writer.Put(TagFirstMap(), static_cast<uint16_t>(first_map)));
ReturnErrorOnFailure(writer.Put(TagMapCount(), static_cast<uint16_t>(map_count)));
ReturnErrorOnFailure(writer.Put(TagFirstKeyset(), static_cast<uint16_t>(first_keyset)));
ReturnErrorOnFailure(writer.Put(TagKeysetCount(), static_cast<uint16_t>(keyset_count)));
ReturnErrorOnFailure(writer.Put(TagNext(), static_cast<uint16_t>(next)));
return writer.EndContainer(container);
}
CHIP_ERROR Deserialize(TLV::TLVReader & reader) override
{
ReturnErrorOnFailure(reader.Next(TLV::AnonymousTag()));
VerifyOrReturnError(TLV::kTLVType_Structure == reader.GetType(), CHIP_ERROR_INTERNAL);
TLV::TLVType container;
ReturnErrorOnFailure(reader.EnterContainer(container));
// first_group
ReturnErrorOnFailure(reader.Next(TagFirstGroup()));
ReturnErrorOnFailure(reader.Get(first_group));
// group_count
ReturnErrorOnFailure(reader.Next(TagGroupCount()));
ReturnErrorOnFailure(reader.Get(group_count));
// first_map
ReturnErrorOnFailure(reader.Next(TagFirstMap()));
ReturnErrorOnFailure(reader.Get(first_map));
// map_count
ReturnErrorOnFailure(reader.Next(TagMapCount()));
ReturnErrorOnFailure(reader.Get(map_count));
// first_keyset
ReturnErrorOnFailure(reader.Next(TagFirstKeyset()));
ReturnErrorOnFailure(reader.Get(first_keyset));
// keyset_count
ReturnErrorOnFailure(reader.Next(TagKeysetCount()));
ReturnErrorOnFailure(reader.Get(keyset_count));
// next
ReturnErrorOnFailure(reader.Next(TagNext()));
ReturnErrorOnFailure(reader.Get(next));
return reader.ExitContainer(container);
}
// Register the fabric in the fabrics' linked-list
CHIP_ERROR Register(PersistentStorageDelegate * storage)
{
FabricList fabric_list;
CHIP_ERROR err = fabric_list.Load(storage);
if (CHIP_ERROR_NOT_FOUND == err)
{
// New fabric list
fabric_list.first_entry = fabric_index;
fabric_list.entry_count = 1;
return fabric_list.Save(storage);
}
ReturnErrorOnFailure(err);
// Existing fabric list, search for existing entry
FabricData fabric(fabric_list.first_entry);
for (size_t i = 0; i < fabric_list.entry_count; i++)
{
err = fabric.Load(storage);
if (CHIP_NO_ERROR != err)
{
break;
}
if (fabric.fabric_index == this->fabric_index)
{
// Fabric already registered
return CHIP_NO_ERROR;
}
fabric.fabric_index = fabric.next;
}
// Add this fabric to the fabric list
this->next = fabric_list.first_entry;
fabric_list.first_entry = this->fabric_index;
fabric_list.entry_count++;
return fabric_list.Save(storage);
}
// Remove the fabric from the fabrics' linked list
CHIP_ERROR Unregister(PersistentStorageDelegate * storage) const
{
FabricList fabric_list;
CHIP_ERROR err = fabric_list.Load(storage);
VerifyOrReturnError(CHIP_NO_ERROR == err || CHIP_ERROR_NOT_FOUND == err, err);
// Existing fabric list, search for existing entry
FabricData fabric(fabric_list.first_entry);
FabricData prev;
for (size_t i = 0; i < fabric_list.entry_count; i++)
{
err = fabric.Load(storage);
if (CHIP_NO_ERROR != err)
{
break;
}
if (fabric.fabric_index == this->fabric_index)
{
// Fabric found
if (i == 0)
{
// Remove first fabric
fabric_list.first_entry = this->next;
}
else
{
// Remove intermediate fabric
prev.next = this->next;
ReturnErrorOnFailure(prev.Save(storage));
}
VerifyOrReturnError(fabric_list.entry_count > 0, CHIP_ERROR_INTERNAL);
fabric_list.entry_count--;
return fabric_list.Save(storage);
}
prev = fabric;
fabric.fabric_index = fabric.next;
}
// Fabric not in the list
return CHIP_ERROR_NOT_FOUND;
}
// Check the fabric is registered in the fabrics' linked list
CHIP_ERROR Validate(PersistentStorageDelegate * storage) const
{
FabricList fabric_list;
ReturnErrorOnFailure(fabric_list.Load(storage));
// Existing fabric list, search for existing entry
FabricData fabric(fabric_list.first_entry);
for (size_t i = 0; i < fabric_list.entry_count; i++)
{
ReturnErrorOnFailure(fabric.Load(storage));
if (fabric.fabric_index == this->fabric_index)
{
return CHIP_NO_ERROR;
}
fabric.fabric_index = fabric.next;
}
// Fabric not in the list
return CHIP_ERROR_NOT_FOUND;
}
CHIP_ERROR Save(PersistentStorageDelegate * storage) override
{
ReturnErrorOnFailure(Register(storage));
return PersistentData::Save(storage);
}
CHIP_ERROR Delete(PersistentStorageDelegate * storage) override
{
ReturnErrorOnFailure(Unregister(storage));
return PersistentData::Delete(storage);
}
};
struct GroupData : public GroupDataProvider::GroupInfo, PersistentData<kPersistentBufferMax>
{
static constexpr TLV::Tag TagName() { return TLV::ContextTag(1); }
static constexpr TLV::Tag TagFirstEndpoint() { return TLV::ContextTag(2); }
static constexpr TLV::Tag TagEndpointCount() { return TLV::ContextTag(3); }
static constexpr TLV::Tag TagNext() { return TLV::ContextTag(4); }
chip::FabricIndex fabric_index = kUndefinedFabricIndex;
chip::EndpointId first_endpoint = kInvalidEndpointId;
uint16_t endpoint_count = 0;
uint16_t index = 0;
chip::GroupId next = 0;
chip::GroupId prev = 0;
bool first = true;
GroupData() : GroupInfo(nullptr){};
GroupData(chip::FabricIndex fabric) : fabric_index(fabric) {}
GroupData(chip::FabricIndex fabric, chip::GroupId group) : GroupInfo(group, nullptr), fabric_index(fabric) {}
CHIP_ERROR UpdateKey(StorageKeyName & key) override
{
VerifyOrReturnError(kUndefinedFabricIndex != fabric_index, CHIP_ERROR_INVALID_FABRIC_INDEX);
key = DefaultStorageKeyAllocator::FabricGroup(fabric_index, group_id);
return CHIP_NO_ERROR;
}
void Clear() override
{
SetName(CharSpan());
first_endpoint = kInvalidEndpointId;
endpoint_count = 0;
next = 0;
}
CHIP_ERROR Serialize(TLV::TLVWriter & writer) const override
{
TLV::TLVType container;
ReturnErrorOnFailure(writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, container));
size_t name_size = strnlen(name, GroupDataProvider::GroupInfo::kGroupNameMax);
ReturnErrorOnFailure(writer.PutString(TagName(), name, static_cast<uint32_t>(name_size)));
ReturnErrorOnFailure(writer.Put(TagFirstEndpoint(), static_cast<uint16_t>(first_endpoint)));
ReturnErrorOnFailure(writer.Put(TagEndpointCount(), static_cast<uint16_t>(endpoint_count)));
ReturnErrorOnFailure(writer.Put(TagNext(), static_cast<uint16_t>(next)));
return writer.EndContainer(container);
}
CHIP_ERROR Deserialize(TLV::TLVReader & reader) override
{
ReturnErrorOnFailure(reader.Next(TLV::AnonymousTag()));
VerifyOrReturnError(TLV::kTLVType_Structure == reader.GetType(), CHIP_ERROR_INTERNAL);
TLV::TLVType container;
ReturnErrorOnFailure(reader.EnterContainer(container));
// name
ReturnErrorOnFailure(reader.Next(TagName()));
ReturnErrorOnFailure(reader.GetString(name, sizeof(name)));
size_t size = strnlen(name, kGroupNameMax);
name[size] = 0;
// first_endpoint
ReturnErrorOnFailure(reader.Next(TagFirstEndpoint()));
ReturnErrorOnFailure(reader.Get(first_endpoint));
// endpoint_count
ReturnErrorOnFailure(reader.Next(TagEndpointCount()));
ReturnErrorOnFailure(reader.Get(endpoint_count));
// next
ReturnErrorOnFailure(reader.Next(TagNext()));
ReturnErrorOnFailure(reader.Get(next));
return reader.ExitContainer(container);
}
bool Get(PersistentStorageDelegate * storage, const FabricData & fabric, size_t target_index)
{
fabric_index = fabric.fabric_index;
group_id = fabric.first_group;
index = 0;
first = true;
while (index < fabric.group_count)
{
if (CHIP_NO_ERROR != Load(storage))
{
break;
}
if (index == target_index)
{
// Target index found
return true;
}
first = false;
prev = group_id;
group_id = next;
index++;
}
return false;
}
bool Find(PersistentStorageDelegate * storage, const FabricData & fabric, chip::GroupId target_group)
{
fabric_index = fabric.fabric_index;
group_id = fabric.first_group;
index = 0;
first = true;
while (index < fabric.group_count)
{
if (CHIP_NO_ERROR != Load(storage))
{
break;
}
if (group_id == target_group)
{
// Target index found
return true;
}
first = false;
prev = group_id;
group_id = next;
index++;
}
return false;
}
};
struct KeyMapData : public GroupDataProvider::GroupKey, LinkedData
{
static constexpr TLV::Tag TagGroupId() { return TLV::ContextTag(1); }
static constexpr TLV::Tag TagKeysetId() { return TLV::ContextTag(2); }
static constexpr TLV::Tag TagNext() { return TLV::ContextTag(3); }
chip::FabricIndex fabric_index = kUndefinedFabricIndex;
chip::GroupId group_id = kUndefinedGroupId;
chip::KeysetId keyset_id = 0;
KeyMapData(){};
KeyMapData(chip::FabricIndex fabric, uint16_t link_id = 0, chip::GroupId group = kUndefinedGroupId, chip::KeysetId keyset = 0) :
GroupKey(group, keyset), LinkedData(link_id), fabric_index(fabric)
{}
CHIP_ERROR UpdateKey(StorageKeyName & key) override
{
VerifyOrReturnError(kUndefinedFabricIndex != fabric_index, CHIP_ERROR_INVALID_FABRIC_INDEX);
key = DefaultStorageKeyAllocator::FabricGroupKey(fabric_index, id);
return CHIP_NO_ERROR;
}
void Clear() override {}
CHIP_ERROR Serialize(TLV::TLVWriter & writer) const override
{
TLV::TLVType container;
ReturnErrorOnFailure(writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, container));
ReturnErrorOnFailure(writer.Put(TagGroupId(), static_cast<uint16_t>(group_id)));
ReturnErrorOnFailure(writer.Put(TagKeysetId(), static_cast<uint16_t>(keyset_id)));
ReturnErrorOnFailure(writer.Put(TagNext(), static_cast<uint16_t>(next)));
return writer.EndContainer(container);
}
CHIP_ERROR Deserialize(TLV::TLVReader & reader) override
{
ReturnErrorOnFailure(reader.Next(TLV::AnonymousTag()));
VerifyOrReturnError(TLV::kTLVType_Structure == reader.GetType(), CHIP_ERROR_INTERNAL);
TLV::TLVType container;
ReturnErrorOnFailure(reader.EnterContainer(container));
// first_endpoint
ReturnErrorOnFailure(reader.Next(TagGroupId()));
ReturnErrorOnFailure(reader.Get(group_id));
// endpoint_count
ReturnErrorOnFailure(reader.Next(TagKeysetId()));
ReturnErrorOnFailure(reader.Get(keyset_id));
// next
ReturnErrorOnFailure(reader.Next(TagNext()));
ReturnErrorOnFailure(reader.Get(next));
return reader.ExitContainer(container);
}
bool Get(PersistentStorageDelegate * storage, const FabricData & fabric, size_t target_index)
{
fabric_index = fabric.fabric_index;
id = fabric.first_map;
max_id = 0;
index = 0;
first = true;
while (index < fabric.map_count)
{
if (CHIP_NO_ERROR != Load(storage))
{
break;
}
if (index == target_index)
{
// Target index found
return true;
}
max_id = std::max(id, max_id);
first = false;
prev = id;
id = next;
index++;
}
id = static_cast<uint16_t>(max_id + 1);
return false;
}
bool Find(PersistentStorageDelegate * storage, const FabricData & fabric, const GroupKey & map)
{
fabric_index = fabric.fabric_index;
id = fabric.first_map;
max_id = 0;
index = 0;
first = true;
while (index < fabric.map_count)
{
if (CHIP_NO_ERROR != Load(storage))
{
break;
}
if ((group_id == map.group_id) && (keyset_id == map.keyset_id))
{
// Match found
return true;
}
max_id = std::max(id, max_id);
first = false;
prev = id;
id = next;
index++;
}
id = static_cast<uint16_t>(max_id + 1);
return false;
}
// returns index if the find_id is found, otherwise std::numeric_limits<size_t>::max
size_t Find(PersistentStorageDelegate * storage, const FabricData & fabric, const KeysetId find_id)
{
fabric_index = fabric.fabric_index;
id = fabric.first_map;
max_id = 0;
index = 0;
first = true;
while (index < fabric.map_count)
{
if (CHIP_NO_ERROR != Load(storage))
{
break;
}
if (keyset_id == find_id)
{
// Match found
return index;
}
max_id = std::max(id, max_id);
first = false;
prev = id;
id = next;
index++;
}
id = static_cast<uint16_t>(max_id + 1);
return std::numeric_limits<size_t>::max();
}
};
struct EndpointData : GroupDataProvider::GroupEndpoint, PersistentData<kPersistentBufferMax>
{
static constexpr TLV::Tag TagEndpoint() { return TLV::ContextTag(1); }
static constexpr TLV::Tag TagNext() { return TLV::ContextTag(2); }
chip::FabricIndex fabric_index = kUndefinedFabricIndex;
uint16_t index = 0;
chip::EndpointId next = 0;
chip::EndpointId prev = 0;
bool first = true;
EndpointData() = default;
EndpointData(chip::FabricIndex fabric, chip::GroupId group = kUndefinedGroupId,
chip::EndpointId endpoint = kInvalidEndpointId) :
GroupEndpoint(group, endpoint),
fabric_index(fabric)
{}
CHIP_ERROR UpdateKey(StorageKeyName & key) override
{
VerifyOrReturnError(kUndefinedFabricIndex != fabric_index, CHIP_ERROR_INVALID_FABRIC_INDEX);
key = DefaultStorageKeyAllocator::FabricGroupEndpoint(fabric_index, group_id, endpoint_id);
return CHIP_NO_ERROR;
}
void Clear() override { next = kInvalidEndpointId; }
CHIP_ERROR Serialize(TLV::TLVWriter & writer) const override
{
TLV::TLVType container;
ReturnErrorOnFailure(writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, container));
ReturnErrorOnFailure(writer.Put(TagEndpoint(), static_cast<uint16_t>(endpoint_id)));
ReturnErrorOnFailure(writer.Put(TagNext(), static_cast<uint16_t>(next)));
return writer.EndContainer(container);
}
CHIP_ERROR Deserialize(TLV::TLVReader & reader) override
{
ReturnErrorOnFailure(reader.Next(TLV::AnonymousTag()));
VerifyOrReturnError(TLV::kTLVType_Structure == reader.GetType(), CHIP_ERROR_INTERNAL);
TLV::TLVType container;
ReturnErrorOnFailure(reader.EnterContainer(container));
// endpoint_id
ReturnErrorOnFailure(reader.Next(TagEndpoint()));
ReturnErrorOnFailure(reader.Get(endpoint_id));
// next
ReturnErrorOnFailure(reader.Next(TagNext()));
ReturnErrorOnFailure(reader.Get(next));
return reader.ExitContainer(container);
}
bool Find(PersistentStorageDelegate * storage, const FabricData & fabric, const GroupData & group, chip::EndpointId target_id)
{
fabric_index = fabric.fabric_index;
group_id = group.group_id;
endpoint_id = group.first_endpoint;
index = 0;
first = true;
while (index < group.endpoint_count)
{
if (CHIP_NO_ERROR != Load(storage))
{
break;
}
if (this->endpoint_id == target_id)
{
// Match found
return true;
}
first = false;
prev = endpoint_id;
endpoint_id = next;
index++;
}
return false;
}
};
struct KeySetData : PersistentData<kPersistentBufferMax>
{
static constexpr TLV::Tag TagPolicy() { return TLV::ContextTag(1); }
static constexpr TLV::Tag TagNumKeys() { return TLV::ContextTag(2); }
static constexpr TLV::Tag TagGroupCredentials() { return TLV::ContextTag(3); }
static constexpr TLV::Tag TagStartTime() { return TLV::ContextTag(4); }
static constexpr TLV::Tag TagKeyHash() { return TLV::ContextTag(5); }
static constexpr TLV::Tag TagKeyValue() { return TLV::ContextTag(6); }
static constexpr TLV::Tag TagNext() { return TLV::ContextTag(7); }
chip::FabricIndex fabric_index = kUndefinedFabricIndex;
chip::KeysetId next = kInvalidKeysetId;
chip::KeysetId prev = kInvalidKeysetId;
bool first = true;
uint16_t keyset_id = 0;
GroupDataProvider::SecurityPolicy policy = GroupDataProvider::SecurityPolicy::kCacheAndSync;
uint8_t keys_count = 0;
Crypto::GroupOperationalCredentials operational_keys[KeySet::kEpochKeysMax];
KeySetData() = default;
KeySetData(chip::FabricIndex fabric, chip::KeysetId id) : fabric_index(fabric) { keyset_id = id; }
KeySetData(chip::FabricIndex fabric, chip::KeysetId id, GroupDataProvider::SecurityPolicy policy_id, uint8_t num_keys) :
fabric_index(fabric), keyset_id(id), policy(policy_id), keys_count(num_keys)
{}
CHIP_ERROR UpdateKey(StorageKeyName & key) override
{
VerifyOrReturnError(kUndefinedFabricIndex != fabric_index, CHIP_ERROR_INVALID_FABRIC_INDEX);
VerifyOrReturnError(kInvalidKeysetId != keyset_id, CHIP_ERROR_INVALID_KEY_ID);
key = DefaultStorageKeyAllocator::FabricKeyset(fabric_index, keyset_id);
return CHIP_NO_ERROR;
}
void Clear() override
{
policy = GroupDataProvider::SecurityPolicy::kCacheAndSync;
keys_count = 0;
memset(operational_keys, 0x00, sizeof(operational_keys));
next = kInvalidKeysetId;
}
Crypto::GroupOperationalCredentials * GetCurrentGroupCredentials()
{
// An epoch key update SHALL order the keys from oldest to newest,
// the current epoch key having the second newest time if time
// synchronization is not achieved or guaranteed.
switch (this->keys_count)
{
case 1:
case 2:
return &operational_keys[0];
case 3:
return &operational_keys[1];
default:
return nullptr;
}
}
CHIP_ERROR Serialize(TLV::TLVWriter & writer) const override
{
TLV::TLVType container;
ReturnErrorOnFailure(writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, container));
// policy
ReturnErrorOnFailure(writer.Put(TagPolicy(), static_cast<uint16_t>(policy)));
// keys_count
ReturnErrorOnFailure(writer.Put(TagNumKeys(), static_cast<uint16_t>(keys_count)));
// operational_keys
{
TLV::TLVType array, item;
ReturnErrorOnFailure(writer.StartContainer(TagGroupCredentials(), TLV::kTLVType_Array, array));
uint8_t keyCount = 0;
uint64_t startTime = 0;
uint16_t hash = 0;
uint8_t encryptionKey[Crypto::CHIP_CRYPTO_SYMMETRIC_KEY_LENGTH_BYTES];
for (auto & key : operational_keys)
{
startTime = 0;
hash = 0;
memset(encryptionKey, 0, sizeof(encryptionKey));
ReturnErrorOnFailure(writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, item));
if (keyCount++ < keys_count)
{
startTime = key.start_time;
hash = key.hash;
memcpy(encryptionKey, key.encryption_key, sizeof(encryptionKey));
}
ReturnErrorOnFailure(writer.Put(TagStartTime(), static_cast<uint64_t>(startTime)));
ReturnErrorOnFailure(writer.Put(TagKeyHash(), hash));
ReturnErrorOnFailure(writer.Put(TagKeyValue(), ByteSpan(encryptionKey)));
ReturnErrorOnFailure(writer.EndContainer(item));
}
ReturnErrorOnFailure(writer.EndContainer(array));
}
// next keyset
ReturnErrorOnFailure(writer.Put(TagNext(), static_cast<uint16_t>(next)));
return writer.EndContainer(container);
}
CHIP_ERROR Deserialize(TLV::TLVReader & reader) override
{
ReturnErrorOnFailure(reader.Next(TLV::AnonymousTag()));
VerifyOrReturnError(TLV::kTLVType_Structure == reader.GetType(), CHIP_ERROR_INTERNAL);
TLV::TLVType container;
ReturnErrorOnFailure(reader.EnterContainer(container));
// policy
ReturnErrorOnFailure(reader.Next(TagPolicy()));
ReturnErrorOnFailure(reader.Get(policy));
// keys_count
ReturnErrorOnFailure(reader.Next(TagNumKeys()));
ReturnErrorOnFailure(reader.Get(keys_count));
// TODO(#21614): Enforce maximum number of 3 keys in a keyset
{
// operational_keys
ReturnErrorOnFailure(reader.Next(TagGroupCredentials()));
VerifyOrReturnError(TLV::kTLVType_Array == reader.GetType(), CHIP_ERROR_INTERNAL);
TLV::TLVType array, item;
ReturnErrorOnFailure(reader.EnterContainer(array));
for (auto & key : operational_keys)
{
ReturnErrorOnFailure(reader.Next(TLV::AnonymousTag()));
VerifyOrReturnError(TLV::kTLVType_Structure == reader.GetType(), CHIP_ERROR_INTERNAL);
ReturnErrorOnFailure(reader.EnterContainer(item));
// start_time
ReturnErrorOnFailure(reader.Next(TagStartTime()));
ReturnErrorOnFailure(reader.Get(key.start_time));
// key hash
ReturnErrorOnFailure(reader.Next(TagKeyHash()));
ReturnErrorOnFailure(reader.Get(key.hash));
// key value
ByteSpan encryption_key;
ReturnErrorOnFailure(reader.Next(TagKeyValue()));
ReturnErrorOnFailure(reader.Get(encryption_key));
VerifyOrReturnError(Crypto::CHIP_CRYPTO_SYMMETRIC_KEY_LENGTH_BYTES == encryption_key.size(), CHIP_ERROR_INTERNAL);
memcpy(key.encryption_key, encryption_key.data(), encryption_key.size());
// Re-derive privacy key from encryption key when loading from storage to save on storage size.
MutableByteSpan privacy_key(key.privacy_key);
ReturnErrorOnFailure(Crypto::DeriveGroupPrivacyKey(encryption_key, privacy_key));
ReturnErrorOnFailure(reader.ExitContainer(item));
}
ReturnErrorOnFailure(reader.ExitContainer(array));
}
// next keyset
ReturnErrorOnFailure(reader.Next(TagNext()));
ReturnErrorOnFailure(reader.Get(next));
return reader.ExitContainer(container);
}
bool Find(PersistentStorageDelegate * storage, const FabricData & fabric, size_t target_id)
{
uint16_t count = 0;
fabric_index = fabric.fabric_index;
keyset_id = fabric.first_keyset;
first = true;
while (count++ < fabric.keyset_count)
{
if (CHIP_NO_ERROR != Load(storage))
{
break;
}
if (keyset_id == target_id)
{
// Target id found
return true;
}
first = false;
prev = keyset_id;
keyset_id = next;
}
return false;
}
};
//
// General
//
constexpr size_t GroupDataProvider::GroupInfo::kGroupNameMax;
constexpr size_t GroupDataProviderImpl::kIteratorsMax;
CHIP_ERROR GroupDataProviderImpl::Init()
{
if (mStorage == nullptr || mSessionKeystore == nullptr)
{
return CHIP_ERROR_INCORRECT_STATE;
}
return CHIP_NO_ERROR;
}
void GroupDataProviderImpl::Finish()
{
mGroupInfoIterators.ReleaseAll();
mGroupKeyIterators.ReleaseAll();
mEndpointIterators.ReleaseAll();
mKeySetIterators.ReleaseAll();
mGroupSessionsIterator.ReleaseAll();
mGroupKeyContexPool.ReleaseAll();
}
void GroupDataProviderImpl::SetStorageDelegate(PersistentStorageDelegate * storage)
{
VerifyOrDie(storage != nullptr);
mStorage = storage;
}
//
// Group Info
//
CHIP_ERROR GroupDataProviderImpl::SetGroupInfo(chip::FabricIndex fabric_index, const GroupInfo & info)
{
VerifyOrReturnError(IsInitialized(), CHIP_ERROR_INTERNAL);
FabricData fabric(fabric_index);
GroupData group;
// Load fabric data (defaults to zero)
CHIP_ERROR err = fabric.Load(mStorage);
VerifyOrReturnError(CHIP_NO_ERROR == err || CHIP_ERROR_NOT_FOUND == err, err);
if (group.Find(mStorage, fabric, info.group_id))
{
// Existing group_id
group.SetName(info.name);
return group.Save(mStorage);
}
// New group_id
group.group_id = info.group_id;
group.SetName(info.name);
return SetGroupInfoAt(fabric_index, fabric.group_count, group);
}
CHIP_ERROR GroupDataProviderImpl::GetGroupInfo(chip::FabricIndex fabric_index, chip::GroupId group_id, GroupInfo & info)
{
FabricData fabric(fabric_index);
GroupData group;
ReturnErrorOnFailure(fabric.Load(mStorage));
VerifyOrReturnError(group.Find(mStorage, fabric, group_id), CHIP_ERROR_NOT_FOUND);
info.group_id = group_id;
info.SetName(group.name);
return CHIP_NO_ERROR;
}
CHIP_ERROR GroupDataProviderImpl::RemoveGroupInfo(chip::FabricIndex fabric_index, chip::GroupId group_id)
{
FabricData fabric(fabric_index);
GroupData group;
ReturnErrorOnFailure(fabric.Load(mStorage));
VerifyOrReturnError(group.Find(mStorage, fabric, group_id), CHIP_ERROR_NOT_FOUND);
return RemoveGroupInfoAt(fabric_index, group.index);
}
CHIP_ERROR GroupDataProviderImpl::SetGroupInfoAt(chip::FabricIndex fabric_index, size_t index, const GroupInfo & info)
{
VerifyOrReturnError(IsInitialized(), CHIP_ERROR_INTERNAL);
FabricData fabric(fabric_index);
GroupData group;
// Load fabric, defaults to zero
CHIP_ERROR err = fabric.Load(mStorage);
VerifyOrReturnError(CHIP_NO_ERROR == err || CHIP_ERROR_NOT_FOUND == err, err);
// If the group exists, the index must match
bool found = group.Find(mStorage, fabric, info.group_id);
VerifyOrReturnError(!found || (group.index == index), CHIP_ERROR_DUPLICATE_KEY_ID);
group.group_id = info.group_id;
group.endpoint_count = 0;
group.SetName(info.name);
if (found)
{
// Update existing entry
return group.Save(mStorage);
}
if (index < fabric.group_count)
{
// Replace existing entry with a new group
GroupData old_group;
old_group.Get(mStorage, fabric, index);
group.first = old_group.first;
group.prev = old_group.prev;
group.next = old_group.next;
ReturnErrorOnFailure(RemoveEndpoints(fabric_index, old_group.group_id));
ReturnErrorOnFailure(old_group.Delete(mStorage));
GroupRemoved(fabric_index, old_group);
}
else
{
// Insert last
VerifyOrReturnError(fabric.group_count == index, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(fabric.group_count < mMaxGroupsPerFabric, CHIP_ERROR_INVALID_LIST_LENGTH);
fabric.group_count++;
}
ReturnErrorOnFailure(group.Save(mStorage));
if (group.first)
{
// First group, update fabric
fabric.first_group = group.group_id;
}
else
{
// Second to last group, update previous
GroupData prev(fabric_index, group.prev);
ReturnErrorOnFailure(prev.Load(mStorage));
prev.next = group.group_id;
ReturnErrorOnFailure(prev.Save(mStorage));
}
// Update fabric
ReturnErrorOnFailure(fabric.Save(mStorage));
GroupAdded(fabric_index, group);
return CHIP_NO_ERROR;
}
CHIP_ERROR GroupDataProviderImpl::GetGroupInfoAt(chip::FabricIndex fabric_index, size_t index, GroupInfo & info)
{
VerifyOrReturnError(IsInitialized(), CHIP_ERROR_INTERNAL);
FabricData fabric(fabric_index);
GroupData group;
ReturnErrorOnFailure(fabric.Load(mStorage));
VerifyOrReturnError(group.Get(mStorage, fabric, index), CHIP_ERROR_NOT_FOUND);
// Target group found
info.group_id = group.group_id;
info.SetName(group.name);
return CHIP_NO_ERROR;
}
CHIP_ERROR GroupDataProviderImpl::RemoveGroupInfoAt(chip::FabricIndex fabric_index, size_t index)
{
VerifyOrReturnError(IsInitialized(), CHIP_ERROR_INTERNAL);
FabricData fabric(fabric_index);
GroupData group;
ReturnErrorOnFailure(fabric.Load(mStorage));
VerifyOrReturnError(group.Get(mStorage, fabric, index), CHIP_ERROR_NOT_FOUND);
// Remove endpoints
EndpointData endpoint(fabric_index, group.group_id, group.first_endpoint);
size_t count = 0;
while (count++ < group.endpoint_count)
{
if (CHIP_NO_ERROR != endpoint.Load(mStorage))
{
break;
}
endpoint.Delete(mStorage);
endpoint.endpoint_id = endpoint.next;
}
ReturnErrorOnFailure(group.Delete(mStorage));