forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProvisionStorage.h
298 lines (271 loc) · 12 KB
/
ProvisionStorage.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
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
/*
* Copyright (c) 2024 Project CHIP Authors
* All rights reserved.
*
* 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 "ProvisionStorageGeneric.h"
#include <credentials/DeviceAttestationCredsProvider.h>
#include <platform/CommissionableDataProvider.h>
#include <platform/DeviceInstanceInfoProvider.h>
#include <app/data-model/Nullable.h>
#include <crypto/CHIPCryptoPAL.h>
#include <lib/core/CHIPError.h>
#include <lib/support/Base64.h>
#include <lib/support/Span.h>
#include <stddef.h>
#include <stdint.h>
namespace chip {
namespace DeviceLayer {
namespace Silabs {
namespace Provision {
namespace Parameters {
enum ID : uint16_t
{
// Internal,
kFlashAddress = 0x0101,
kFlashSize = 0x0102,
kFlashPageSize = 0x0103,
kBaseAddress = 0x0104,
kCsrFile = 0x0105,
// Options,
kVersion = 0x0111,
kAction = 0x0112,
kExtra = 0x0113,
kStop = 0x0114,
kParamsPath = 0x0121,
kInputsPath = 0x0122,
kOutputPath = 0x0123,
kTemporaryDir = 0x0124,
kTargetDevice = 0x0131,
kChannel = 0x0132,
kGenerateCreds = 0x0133,
kCsrMode = 0x0134,
kGeneratorFW = 0x0135,
kProductionFW = 0x0136,
kCertToolPath = 0x0137,
kPylinkLib = 0x0138,
// Instance Info,
kSerialNumber = 0x0141,
kVendorId = 0x0142,
kVendorName = 0x0143,
kProductId = 0x0144,
kProductName = 0x0145,
kProductLabel = 0x0146,
kProductUrl = 0x0147,
kPartNumber = 0x0148,
kHwVersion = 0x0151,
kHwVersionStr = 0x0152,
kManufacturingDate = 0x0153,
kUniqueId = 0x0154,
// Commissionable Data,
kDiscriminator = 0x0161,
kSpake2pPasscode = 0x0162,
kSpake2pIterations = 0x0163,
kSpake2pSalt = 0x0164,
kSpake2pVerifier = 0x0165,
kSetupPayload = 0x0166,
kCommissioningFlow = 0x0167,
kRendezvousFlags = 0x0168,
// Attestation Credentials,
kFirmwareInfo = 0x0181,
kCertification = 0x0182,
kCdCert = 0x0183,
kCdKey = 0x0184,
kPaaCert = 0x0191,
kPaaKey = 0x0192,
kPaiCert = 0x0193,
kPaiKey = 0x0194,
kDacCert = 0x0195,
kDacKey = 0x0196,
kKeyId = 0x0197,
kKeyPass = 0x0198,
kPKCS12 = 0x0199,
kCommonName = 0x01a1,
kOtaTlvEncryptionKey = 0x01a2,
};
} // namespace Parameters
struct CustomStorage : public GenericStorage
{
CHIP_ERROR Set(uint16_t id, const uint8_t * value) override;
CHIP_ERROR Get(uint16_t id, uint8_t & value) override;
CHIP_ERROR Set(uint16_t id, const uint16_t * value) override;
CHIP_ERROR Get(uint16_t id, uint16_t & value) override;
CHIP_ERROR Set(uint16_t id, const uint32_t * value) override;
CHIP_ERROR Get(uint16_t id, uint32_t & value) override;
CHIP_ERROR Set(uint16_t id, const uint64_t * value) override;
CHIP_ERROR Get(uint16_t id, uint64_t & value) override;
CHIP_ERROR Get(uint16_t id, uint8_t * value, size_t max_size, size_t & size) override;
CHIP_ERROR Set(uint16_t id, const uint8_t * value, size_t size) override;
};
namespace {
constexpr size_t kVersionFieldLengthInBits = 3;
constexpr size_t kVendorIDFieldLengthInBits = 16;
constexpr size_t kProductIDFieldLengthInBits = 16;
constexpr size_t kCommissioningFlowFieldLengthInBits = 2;
constexpr size_t kRendezvousInfoFieldLengthInBits = 8;
constexpr size_t kPayloadDiscriminatorFieldLengthInBits = 12;
constexpr size_t kSetupPINCodeFieldLengthInBits = 27;
constexpr size_t kPaddingFieldLengthInBits = 4;
} // namespace
struct Storage : public GenericStorage,
public chip::DeviceLayer::DeviceInstanceInfoProvider,
public chip::DeviceLayer::CommissionableDataProvider,
public chip::Credentials::DeviceAttestationCredentialsProvider
{
static constexpr size_t kArgumentSizeMax = 512;
static constexpr size_t kVersionLengthMax = 16;
static constexpr size_t kSerialNumberLengthMax = 32;
static constexpr size_t kVendorNameLengthMax = 32;
static constexpr size_t kProductNameLengthMax = 32;
static constexpr size_t kProductLabelLengthMax = 32;
static constexpr size_t kProductUrlLengthMax = 32;
static constexpr size_t kPartNumberLengthMax = 32;
static constexpr size_t kHardwareVersionStrLengthMax = 32;
static constexpr size_t kManufacturingDateLengthMax = 11; // yyyy-mm-dd + \0
static constexpr size_t kUniqueIdLengthMax = 16;
static constexpr size_t kSpake2pVerifierB64LengthMax = BASE64_ENCODED_LEN(chip::Crypto::kSpake2p_VerifierSerialized_Length) + 1;
static constexpr size_t kSpake2pSaltB64LengthMax = BASE64_ENCODED_LEN(chip::Crypto::kSpake2p_Max_PBKDF_Salt_Length) + 1;
static constexpr size_t kFirmwareInfoSizeMax = 32;
static constexpr size_t kCertificationSizeMax = 350;
static constexpr size_t kCertificateSizeMax = kArgumentSizeMax;
static constexpr size_t kDeviceAttestationKeySizeMax = 128;
static constexpr size_t kSetupPayloadSizeMax = 32;
static constexpr size_t kCsrLengthMax = 512;
static constexpr size_t kCommonNameMax = 128;
static constexpr size_t kTotalPayloadDataSizeInBits =
(kVersionFieldLengthInBits + kVendorIDFieldLengthInBits + kProductIDFieldLengthInBits +
kCommissioningFlowFieldLengthInBits + kRendezvousInfoFieldLengthInBits + kPayloadDiscriminatorFieldLengthInBits +
kSetupPINCodeFieldLengthInBits + kPaddingFieldLengthInBits);
static constexpr size_t kTotalPayloadDataSize = kTotalPayloadDataSizeInBits / 8;
public:
static uint8_t aux_buffer[Storage::kArgumentSizeMax];
friend class Manager;
friend class Protocol1;
friend class Command;
friend class CsrCommand;
friend class ReadCommand;
friend class WriteCommand;
//
// Initialization
//
CHIP_ERROR Initialize(uint32_t flash_addr = 0, uint32_t flash_size = 0);
CHIP_ERROR Commit();
CHIP_ERROR GetBaseAddress(uint32_t & value);
//
// Generic Interface
//
CHIP_ERROR Get(uint16_t id, uint8_t & value) override;
CHIP_ERROR Get(uint16_t id, uint16_t & value) override;
CHIP_ERROR Get(uint16_t id, uint32_t & value) override;
CHIP_ERROR Get(uint16_t id, uint64_t & value) override;
CHIP_ERROR Get(uint16_t id, uint8_t * value, size_t max_size, size_t & size) override;
//
// DeviceInstanceInfoProvider
//
CHIP_ERROR GetSerialNumber(char * value, size_t max);
CHIP_ERROR GetVendorId(uint16_t & value);
CHIP_ERROR GetVendorName(char * value, size_t max);
CHIP_ERROR GetProductId(uint16_t & productId);
CHIP_ERROR GetProductName(char * value, size_t max);
CHIP_ERROR GetProductLabel(char * value, size_t max);
CHIP_ERROR GetProductURL(char * value, size_t max);
CHIP_ERROR GetPartNumber(char * value, size_t max);
CHIP_ERROR GetHardwareVersion(uint16_t & value);
CHIP_ERROR GetHardwareVersionString(char * value, size_t max);
CHIP_ERROR GetManufacturingDate(uint16_t & year, uint8_t & month, uint8_t & day);
CHIP_ERROR GetRotatingDeviceIdUniqueId(MutableByteSpan & value);
//
// CommissionableDataProvider
//
CHIP_ERROR GetSetupDiscriminator(uint16_t & value);
CHIP_ERROR GetSpake2pIterationCount(uint32_t & value);
CHIP_ERROR GetSetupPasscode(uint32_t & value);
CHIP_ERROR GetSpake2pSalt(MutableByteSpan & value);
CHIP_ERROR GetSpake2pVerifier(MutableByteSpan & value, size_t & size);
//
// DeviceAttestationCredentialsProvider
//
CHIP_ERROR GetFirmwareInformation(MutableByteSpan & value);
CHIP_ERROR GetCertificationDeclaration(MutableByteSpan & value);
CHIP_ERROR GetProductAttestationIntermediateCert(MutableByteSpan & value);
CHIP_ERROR GetDeviceAttestationCert(MutableByteSpan & value);
CHIP_ERROR GetDeviceAttestationCSR(uint16_t vid, uint16_t pid, const CharSpan & cn, MutableCharSpan & csr);
CHIP_ERROR SignWithDeviceAttestationKey(const ByteSpan & message, MutableByteSpan & signature);
CHIP_ERROR SetCertificationDeclaration(const ByteSpan & value);
CHIP_ERROR SetProductAttestationIntermediateCert(const ByteSpan & value);
CHIP_ERROR SetDeviceAttestationCert(const ByteSpan & value);
CHIP_ERROR SetDeviceAttestationKey(const ByteSpan & value);
//
// Other
//
CHIP_ERROR GetSetupPayload(chip::MutableCharSpan & value);
CHIP_ERROR SetProvisionRequest(bool value);
CHIP_ERROR GetProvisionRequest(bool & value);
CHIP_ERROR GetTestEventTriggerKey(MutableByteSpan & keySpan);
private:
// Generic Interface
CHIP_ERROR Set(uint16_t id, const uint8_t * value) override;
CHIP_ERROR Set(uint16_t id, const uint16_t * value) override;
CHIP_ERROR Set(uint16_t id, const uint32_t * value) override;
CHIP_ERROR Set(uint16_t id, const uint64_t * value) override;
CHIP_ERROR Set(uint16_t id, const uint8_t * value, size_t size) override;
// DeviceInstanceInfoProvider
CHIP_ERROR SetSerialNumber(const char * value, size_t len);
CHIP_ERROR SetVendorId(uint16_t value);
CHIP_ERROR SetVendorName(const char * value, size_t len);
CHIP_ERROR SetProductId(uint16_t productId);
CHIP_ERROR SetProductName(const char * value, size_t len);
CHIP_ERROR SetProductLabel(const char * value, size_t len);
CHIP_ERROR SetProductURL(const char * value, size_t len);
CHIP_ERROR SetPartNumber(const char * value, size_t len);
CHIP_ERROR SetHardwareVersion(uint16_t value);
CHIP_ERROR SetHardwareVersionString(const char * value, size_t len);
CHIP_ERROR SetManufacturingDate(const char * value, size_t len);
CHIP_ERROR GetManufacturingDate(uint8_t * value, size_t max, size_t & size);
CHIP_ERROR SetUniqueId(const uint8_t * value, size_t size);
CHIP_ERROR GetUniqueId(uint8_t * value, size_t max, size_t & size);
// CommissionableDataProvider
CHIP_ERROR SetSetupDiscriminator(uint16_t value);
CHIP_ERROR SetSpake2pIterationCount(uint32_t value);
CHIP_ERROR SetSetupPasscode(uint32_t value);
CHIP_ERROR SetSpake2pSalt(const char * value, size_t size);
CHIP_ERROR GetSpake2pSalt(char * value, size_t max, size_t & size);
CHIP_ERROR SetSpake2pVerifier(const char * value, size_t size);
CHIP_ERROR GetSpake2pVerifier(char * value, size_t max, size_t & size);
// DeviceAttestationCredentialsProvider
CHIP_ERROR SetFirmwareInformation(const ByteSpan & value);
// Other
CHIP_ERROR SetProvisionVersion(const char * value, size_t len);
CHIP_ERROR GetProvisionVersion(char * value, size_t max, size_t & size);
CHIP_ERROR SetSetupPayload(const uint8_t * value, size_t size);
CHIP_ERROR GetSetupPayload(uint8_t * value, size_t max, size_t & size);
#if OTA_ENCRYPTION_ENABLE
CHIP_ERROR SetOtaTlvEncryptionKey(const ByteSpan & value);
#endif
uint16_t mVendorId = 0;
uint16_t mProductId = 0;
uint16_t mHwVersion = 0;
uint16_t mDiscriminator = 0; // 12-bit
uint32_t mCommissioningFlow = 0;
uint32_t mRendezvousFlags = 0;
uint32_t mPasscode = 0;
uint32_t mKeyId = 0;
char mCommonName[kCommonNameMax] = { 0 };
CustomStorage mCustom;
};
} // namespace Provision
} // namespace Silabs
} // namespace DeviceLayer
} // namespace chip