forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGroupDataProviderImpl.h
243 lines (209 loc) · 9.5 KB
/
GroupDataProviderImpl.h
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
/*
*
* Copyright (c) 2021 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
*
* http://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.
*/
#pragma once
#include <credentials/GroupDataProvider.h>
#include <lib/core/CHIPPersistentStorageDelegate.h>
#include <lib/support/Pool.h>
namespace chip {
namespace Credentials {
class GroupDataProviderImpl : public GroupDataProvider
{
public:
static constexpr size_t kIteratorsMax = CHIP_CONFIG_MAX_GROUP_CONCURRENT_ITERATORS;
GroupDataProviderImpl() = default;
GroupDataProviderImpl(uint16_t maxGroupsPerFabric, uint16_t maxGroupKeysPerFabric) :
GroupDataProvider(maxGroupsPerFabric, maxGroupKeysPerFabric)
{}
~GroupDataProviderImpl() override {}
/**
* @brief Set the storage implementation used for non-volatile storage of configuration data.
* This method MUST be called before Init().
*
* @param storage Pointer to storage instance to set. Cannot be nullptr, will assert.
*/
void SetStorageDelegate(PersistentStorageDelegate * storage);
CHIP_ERROR Init() override;
void Finish() override;
//
// Group Info
//
// By id
CHIP_ERROR SetGroupInfo(FabricIndex fabric_index, const GroupInfo & info) override;
CHIP_ERROR GetGroupInfo(FabricIndex fabric_index, GroupId group_id, GroupInfo & info) override;
CHIP_ERROR RemoveGroupInfo(FabricIndex fabric_index, GroupId group_id) override;
// By index
CHIP_ERROR SetGroupInfoAt(FabricIndex fabric_index, size_t index, const GroupInfo & info) override;
CHIP_ERROR GetGroupInfoAt(FabricIndex fabric_index, size_t index, GroupInfo & info) override;
CHIP_ERROR RemoveGroupInfoAt(FabricIndex fabric_index, size_t index) override;
// Endpoints
bool HasEndpoint(FabricIndex fabric_index, GroupId group_id, EndpointId endpoint_id) override;
CHIP_ERROR AddEndpoint(FabricIndex fabric_index, GroupId group_id, EndpointId endpoint_id) override;
CHIP_ERROR RemoveEndpoint(FabricIndex fabric_index, GroupId group_id, EndpointId endpoint_id) override;
CHIP_ERROR RemoveEndpoint(FabricIndex fabric_index, EndpointId endpoint_id) override;
// Iterators
GroupInfoIterator * IterateGroupInfo(FabricIndex fabric_index) override;
EndpointIterator * IterateEndpoints(FabricIndex fabric_index) override;
//
// Group-Key map
//
CHIP_ERROR SetGroupKeyAt(FabricIndex fabric_index, size_t index, const GroupKey & info) override;
CHIP_ERROR GetGroupKeyAt(FabricIndex fabric_index, size_t index, GroupKey & info) override;
CHIP_ERROR RemoveGroupKeyAt(FabricIndex fabric_index, size_t index) override;
CHIP_ERROR RemoveGroupKeys(FabricIndex fabric_index) override;
GroupKeyIterator * IterateGroupKeys(FabricIndex fabric_index) override;
//
// Key Sets
//
CHIP_ERROR SetKeySet(FabricIndex fabric_index, const ByteSpan & compressed_fabric_id, const KeySet & keys) override;
CHIP_ERROR GetKeySet(FabricIndex fabric_index, chip::KeysetId keyset_id, KeySet & keys) override;
CHIP_ERROR RemoveKeySet(FabricIndex fabric_index, chip::KeysetId keyset_id) override;
CHIP_ERROR GetIpkKeySet(FabricIndex fabric_index, KeySet & out_keyset) override;
KeySetIterator * IterateKeySets(FabricIndex fabric_index) override;
// Fabrics
CHIP_ERROR RemoveFabric(FabricIndex fabric_index) override;
// Decryption
Crypto::SymmetricKeyContext * GetKeyContext(FabricIndex fabric_index, GroupId group_id) override;
GroupSessionIterator * IterateGroupSessions(uint16_t session_id) override;
protected:
class GroupInfoIteratorImpl : public GroupInfoIterator
{
public:
GroupInfoIteratorImpl(GroupDataProviderImpl & provider, FabricIndex fabric_index);
size_t Count() override;
bool Next(GroupInfo & output) override;
void Release() override;
protected:
GroupDataProviderImpl & mProvider;
FabricIndex mFabric = kUndefinedFabricIndex;
uint16_t mNextId = 0;
size_t mCount = 0;
size_t mTotal = 0;
};
class GroupKeyIteratorImpl : public GroupKeyIterator
{
public:
GroupKeyIteratorImpl(GroupDataProviderImpl & provider, FabricIndex fabric_index);
size_t Count() override;
bool Next(GroupKey & output) override;
void Release() override;
protected:
GroupDataProviderImpl & mProvider;
FabricIndex mFabric = kUndefinedFabricIndex;
uint16_t mNextId = 0;
size_t mCount = 0;
size_t mTotal = 0;
};
class EndpointIteratorImpl : public EndpointIterator
{
public:
EndpointIteratorImpl(GroupDataProviderImpl & provider, FabricIndex fabric_index);
size_t Count() override;
bool Next(GroupEndpoint & output) override;
void Release() override;
protected:
GroupDataProviderImpl & mProvider;
FabricIndex mFabric = kUndefinedFabricIndex;
GroupId mFirstGroup = kUndefinedGroupId;
uint16_t mGroup = 0;
size_t mGroupIndex = 0;
size_t mGroupCount = 0;
uint16_t mEndpoint = 0;
size_t mEndpointIndex = 0;
size_t mEndpointCount = 0;
bool mFirstEndpoint = true;
};
class GroupKeyContext : public Crypto::SymmetricKeyContext
{
public:
GroupKeyContext(GroupDataProviderImpl & provider) : mProvider(provider) {}
GroupKeyContext(GroupDataProviderImpl & provider, const ByteSpan & encryptionKey, uint16_t hash,
const ByteSpan & privacyKey) :
mProvider(provider)
{
SetKey(encryptionKey, hash);
SetPrivacyKey(privacyKey);
}
void SetKey(const ByteSpan & encryptionKey, uint16_t hash)
{
mKeyHash = hash;
memcpy(mEncryptionKey, encryptionKey.data(), std::min(encryptionKey.size(), sizeof(mEncryptionKey)));
}
void SetPrivacyKey(const ByteSpan & privacyKey)
{
memcpy(mPrivacyKey, privacyKey.data(), std::min(privacyKey.size(), sizeof(mPrivacyKey)));
}
uint16_t GetKeyHash() override { return mKeyHash; }
CHIP_ERROR MessageEncrypt(const ByteSpan & plaintext, const ByteSpan & aad, const ByteSpan & nonce, MutableByteSpan & mic,
MutableByteSpan & ciphertext) const override;
CHIP_ERROR MessageDecrypt(const ByteSpan & ciphertext, const ByteSpan & aad, const ByteSpan & nonce, const ByteSpan & mic,
MutableByteSpan & plaintext) const override;
CHIP_ERROR PrivacyEncrypt(const ByteSpan & input, const ByteSpan & nonce, MutableByteSpan & output) const override;
CHIP_ERROR PrivacyDecrypt(const ByteSpan & input, const ByteSpan & nonce, MutableByteSpan & output) const override;
void Release() override;
protected:
GroupDataProviderImpl & mProvider;
uint16_t mKeyHash = 0;
uint8_t mEncryptionKey[Crypto::CHIP_CRYPTO_SYMMETRIC_KEY_LENGTH_BYTES] = { 0 };
uint8_t mPrivacyKey[Crypto::CHIP_CRYPTO_SYMMETRIC_KEY_LENGTH_BYTES] = { 0 };
};
class KeySetIteratorImpl : public KeySetIterator
{
public:
KeySetIteratorImpl(GroupDataProviderImpl & provider, FabricIndex fabric_index);
size_t Count() override;
bool Next(KeySet & output) override;
void Release() override;
protected:
GroupDataProviderImpl & mProvider;
FabricIndex mFabric = kUndefinedFabricIndex;
uint16_t mNextId = 0;
size_t mCount = 0;
size_t mTotal = 0;
};
class GroupSessionIteratorImpl : public GroupSessionIterator
{
public:
GroupSessionIteratorImpl(GroupDataProviderImpl & provider, uint16_t session_id);
size_t Count() override;
bool Next(GroupSession & output) override;
void Release() override;
protected:
GroupDataProviderImpl & mProvider;
uint16_t mSessionId = 0;
FabricIndex mFirstFabric = kUndefinedFabricIndex;
FabricIndex mFabric = kUndefinedFabricIndex;
uint16_t mFabricCount = 0;
uint16_t mFabricTotal = 0;
uint16_t mMapping = 0;
uint16_t mMapCount = 0;
uint16_t mKeyIndex = 0;
uint16_t mKeyCount = 0;
bool mFirstMap = true;
GroupKeyContext mGroupKeyContext;
};
bool IsInitialized() { return (mStorage != nullptr); }
CHIP_ERROR RemoveEndpoints(FabricIndex fabric_index, GroupId group_id);
chip::PersistentStorageDelegate * mStorage = nullptr;
ObjectPool<GroupInfoIteratorImpl, kIteratorsMax> mGroupInfoIterators;
ObjectPool<GroupKeyIteratorImpl, kIteratorsMax> mGroupKeyIterators;
ObjectPool<EndpointIteratorImpl, kIteratorsMax> mEndpointIterators;
ObjectPool<KeySetIteratorImpl, kIteratorsMax> mKeySetIterators;
ObjectPool<GroupSessionIteratorImpl, kIteratorsMax> mGroupSessionsIterator;
ObjectPool<GroupKeyContext, kIteratorsMax> mGroupKeyContexPool;
};
} // namespace Credentials
} // namespace chip