@@ -112,14 +112,24 @@ CHIP_ERROR DeviceController::Init(ControllerInitParams params)
112
112
mDNSResolver .SetCommissioningDelegate (this );
113
113
RegisterDeviceDiscoveryDelegate (params.deviceDiscoveryDelegate );
114
114
115
- VerifyOrReturnError (params.operationalCredentialsDelegate != nullptr , CHIP_ERROR_INVALID_ARGUMENT);
116
- mOperationalCredentialsDelegate = params.operationalCredentialsDelegate ;
117
-
118
115
mVendorId = params.controllerVendorId ;
119
116
if (params.operationalKeypair != nullptr || !params.controllerNOC .empty () || !params.controllerRCAC .empty ())
120
117
{
121
118
ReturnErrorOnFailure (InitControllerNOCChain (params));
122
119
}
120
+ else if (params.fabricIndex .HasValue ())
121
+ {
122
+ VerifyOrReturnError (params.systemState ->Fabrics ()->FabricCount () > 0 , CHIP_ERROR_INVALID_ARGUMENT);
123
+ if (params.systemState ->Fabrics ()->FindFabricWithIndex (params.fabricIndex .Value ()) != nullptr )
124
+ {
125
+ mFabricIndex = params.fabricIndex .Value ();
126
+ }
127
+ else
128
+ {
129
+ ChipLogError (Controller, " There is no fabric corresponding to the given fabricIndex" );
130
+ return CHIP_ERROR_INVALID_ARGUMENT;
131
+ }
132
+ }
123
133
124
134
mSystemState = params.systemState ->Retain ();
125
135
mState = State::Initialized;
@@ -306,8 +316,62 @@ CHIP_ERROR DeviceController::InitControllerNOCChain(const ControllerInitParams &
306
316
ReturnErrorOnFailure (err);
307
317
VerifyOrReturnError (fabricIndex != kUndefinedFabricIndex , CHIP_ERROR_INTERNAL);
308
318
309
- mFabricIndex = fabricIndex;
319
+ mFabricIndex = fabricIndex;
320
+ mAdvertiseIdentity = advertiseOperational;
321
+ return CHIP_NO_ERROR;
322
+ }
323
+
324
+ CHIP_ERROR DeviceController::UpdateControllerNOCChain (const ByteSpan & noc, const ByteSpan & icac,
325
+ Crypto::P256Keypair * operationalKeypair,
326
+ bool operationalKeypairExternalOwned)
327
+ {
328
+ VerifyOrReturnError (mFabricIndex != kUndefinedFabricIndex , CHIP_ERROR_INTERNAL);
329
+ VerifyOrReturnError (mSystemState != nullptr , CHIP_ERROR_INTERNAL);
330
+ FabricTable * fabricTable = mSystemState ->Fabrics ();
331
+ CHIP_ERROR err = CHIP_NO_ERROR;
332
+ FabricId fabricId;
333
+ NodeId nodeId;
334
+ CATValues oldCats;
335
+ CATValues newCats;
336
+ ReturnErrorOnFailure (ExtractNodeIdFabricIdFromOpCert (noc, &nodeId, &fabricId));
337
+ ReturnErrorOnFailure (fabricTable->FetchCATs (mFabricIndex , oldCats));
338
+ ReturnErrorOnFailure (ExtractCATsFromOpCert (noc, newCats));
310
339
340
+ bool needCloseSession = true ;
341
+ if (GetFabricInfo ()->GetNodeId () == nodeId && oldCats == newCats)
342
+ {
343
+ needCloseSession = false ;
344
+ }
345
+
346
+ if (operationalKeypair != nullptr )
347
+ {
348
+ err = fabricTable->UpdatePendingFabricWithProvidedOpKey (mFabricIndex , noc, icac, operationalKeypair,
349
+ operationalKeypairExternalOwned, mAdvertiseIdentity );
350
+ }
351
+ else
352
+ {
353
+ VerifyOrReturnError (fabricTable->HasOperationalKeyForFabric (mFabricIndex ), CHIP_ERROR_KEY_NOT_FOUND);
354
+ err = fabricTable->UpdatePendingFabricWithOperationalKeystore (mFabricIndex , noc, icac, mAdvertiseIdentity );
355
+ }
356
+
357
+ if (err == CHIP_NO_ERROR)
358
+ {
359
+ err = fabricTable->CommitPendingFabricData ();
360
+ }
361
+ else
362
+ {
363
+ fabricTable->RevertPendingFabricData ();
364
+ }
365
+
366
+ ReturnErrorOnFailure (err);
367
+ if (needCloseSession)
368
+ {
369
+ // If the node id or CATs have changed, our existing CASE sessions are no longer valid,
370
+ // because the other side will think anything coming over those sessions comes from our
371
+ // old node ID, and the new CATs might not satisfy the ACL requirements of the other side.
372
+ mSystemState ->SessionMgr ()->ExpireAllSessionsForFabric (mFabricIndex );
373
+ }
374
+ ChipLogProgress (Controller, " Controller NOC chain has updated" );
311
375
return CHIP_NO_ERROR;
312
376
}
313
377
@@ -398,6 +462,8 @@ DeviceCommissioner::DeviceCommissioner() :
398
462
399
463
CHIP_ERROR DeviceCommissioner::Init (CommissionerInitParams params)
400
464
{
465
+ VerifyOrReturnError (params.operationalCredentialsDelegate != nullptr , CHIP_ERROR_INVALID_ARGUMENT);
466
+ mOperationalCredentialsDelegate = params.operationalCredentialsDelegate ;
401
467
ReturnErrorOnFailure (DeviceController::Init (params));
402
468
403
469
mPairingDelegate = params.pairingDelegate ;
0 commit comments