diff --git a/src/darwin/Framework/CHIP/CHIPOperationalCredentialsDelegate.h b/src/darwin/Framework/CHIP/CHIPOperationalCredentialsDelegate.h index 0d038673093835..37b8b0e580a07a 100644 --- a/src/darwin/Framework/CHIP/CHIPOperationalCredentialsDelegate.h +++ b/src/darwin/Framework/CHIP/CHIPOperationalCredentialsDelegate.h @@ -79,7 +79,7 @@ class CHIPOperationalCredentialsDelegate : public chip::Controller::OperationalC chip::NodeId mNextRequestedNodeId = 1; chip::FabricId mNextFabricId = 1; bool mNodeIdRequested = false; - bool mGenerateRootCert = false; + bool mForceRootCertRegeneration = false; }; NS_ASSUME_NONNULL_END diff --git a/src/darwin/Framework/CHIP/CHIPOperationalCredentialsDelegate.mm b/src/darwin/Framework/CHIP/CHIPOperationalCredentialsDelegate.mm index 988dea237afc82..58edf9a095b511 100644 --- a/src/darwin/Framework/CHIP/CHIPOperationalCredentialsDelegate.mm +++ b/src/darwin/Framework/CHIP/CHIPOperationalCredentialsDelegate.mm @@ -162,7 +162,7 @@ static BOOL isRunningTests(void) } NSLog(@"Stored the keys"); - mGenerateRootCert = true; + mForceRootCertRegeneration = true; return CHIP_NO_ERROR; } @@ -192,6 +192,7 @@ static BOOL isRunningTests(void) MutableByteSpan & noc) { uint32_t validityStart, validityEnd; + bool haveRootCert = false; if (!ToChipEpochTime(0, validityStart)) { NSLog(@"Failed in computing certificate validity start date"); @@ -204,13 +205,16 @@ static BOOL isRunningTests(void) } ChipDN rcac_dn; - if (!mGenerateRootCert) { + if (!mForceRootCertRegeneration) { uint16_t rcacBufLen = static_cast(std::min(rcac.size(), static_cast(UINT16_MAX))); PERSISTENT_KEY_OP(fabricId, kOperationalCredentialsRootCertificateStorage, key, - ReturnErrorOnFailure(mStorage->SyncGetKeyValue(key, rcac.data(), rcacBufLen))); - rcac.reduce_size(rcacBufLen); - ReturnErrorOnFailure(ExtractSubjectDNFromX509Cert(rcac, rcac_dn)); - } else { + haveRootCert = (mStorage->SyncGetKeyValue(key, rcac.data(), rcacBufLen) == CHIP_NO_ERROR)); + if (haveRootCert) { + rcac.reduce_size(rcacBufLen); + ReturnErrorOnFailure(ExtractSubjectDNFromX509Cert(rcac, rcac_dn)); + } + } + if (!haveRootCert) { ReturnErrorOnFailure(rcac_dn.AddAttribute(chip::ASN1::kOID_AttributeType_ChipRootId, mIssuerId)); ReturnErrorOnFailure(rcac_dn.AddAttribute(chip::ASN1::kOID_AttributeType_ChipFabricId, fabricId)); @@ -222,7 +226,7 @@ static BOOL isRunningTests(void) PERSISTENT_KEY_OP(fabricId, kOperationalCredentialsRootCertificateStorage, key, ReturnErrorOnFailure(mStorage->SyncSetKeyValue(key, rcac.data(), static_cast(rcac.size())))); - mGenerateRootCert = false; + mForceRootCertRegeneration = false; } icac.reduce_size(0);