Skip to content

Commit

Permalink
Resolve logic in generating the root certificate (#16788)
Browse files Browse the repository at this point in the history
  • Loading branch information
krypton36 authored Mar 30, 2022
1 parent 11ab5a3 commit 645e98d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 11 additions & 7 deletions src/darwin/Framework/CHIP/CHIPOperationalCredentialsDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ static BOOL isRunningTests(void)
}

NSLog(@"Stored the keys");
mGenerateRootCert = true;
mForceRootCertRegeneration = true;
return CHIP_NO_ERROR;
}

Expand Down Expand Up @@ -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");
Expand All @@ -204,13 +205,16 @@ static BOOL isRunningTests(void)
}

ChipDN rcac_dn;
if (!mGenerateRootCert) {
if (!mForceRootCertRegeneration) {
uint16_t rcacBufLen = static_cast<uint16_t>(std::min(rcac.size(), static_cast<size_t>(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));

Expand All @@ -222,7 +226,7 @@ static BOOL isRunningTests(void)
PERSISTENT_KEY_OP(fabricId, kOperationalCredentialsRootCertificateStorage, key,
ReturnErrorOnFailure(mStorage->SyncSetKeyValue(key, rcac.data(), static_cast<uint16_t>(rcac.size()))));

mGenerateRootCert = false;
mForceRootCertRegeneration = false;
}

icac.reduce_size(0);
Expand Down

0 comments on commit 645e98d

Please sign in to comment.