Skip to content

Commit

Permalink
Create CHIPAttestationTrustStoreBridge when we know we have PAA certs. (
Browse files Browse the repository at this point in the history
#17783)

Fixes #17746
  • Loading branch information
bzbarsky-apple authored Apr 27, 2022
1 parent afe0fef commit a690115
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
8 changes: 5 additions & 3 deletions src/darwin/Framework/CHIP/CHIPAttestationTrustStoreBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ NS_ASSUME_NONNULL_BEGIN

class CHIPAttestationTrustStoreBridge : public chip::Credentials::AttestationTrustStore {
public:
CHIPAttestationTrustStoreBridge(NSArray<NSData *> * paaCerts)
: mPaaCerts(paaCerts)
{
}
~CHIPAttestationTrustStoreBridge() {};

void Init(NSArray<NSData *> * paaCerts);

CHIP_ERROR GetProductAttestationAuthorityCert(
const chip::ByteSpan & skid, chip::MutableByteSpan & outPaaDerBuffer) const override;

private:
NSArray<NSData *> * _Nullable mPaaCerts;
NSArray<NSData *> * mPaaCerts;
};

NS_ASSUME_NONNULL_END
2 changes: 0 additions & 2 deletions src/darwin/Framework/CHIP/CHIPAttestationTrustStoreBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

static chip::ByteSpan asByteSpan(NSData * value) { return chip::ByteSpan(static_cast<const uint8_t *>(value.bytes), value.length); }

void CHIPAttestationTrustStoreBridge::Init(NSArray<NSData *> * paaCerts) { mPaaCerts = paaCerts; }

CHIP_ERROR CHIPAttestationTrustStoreBridge::GetProductAttestationAuthorityCert(
const chip::ByteSpan & skid, chip::MutableByteSpan & outPaaDerBuffer) const
{
Expand Down
38 changes: 21 additions & 17 deletions src/darwin/Framework/CHIP/MatterControllerFactory.mm
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ - (instancetype)init
return nil;
}

_attestationTrustStoreBridge = new CHIPAttestationTrustStoreBridge();
if ([self checkForInitError:(_attestationTrustStoreBridge != nullptr) logMsg:kErrorAttestationTrustStoreInit]) {
return nil;
}

_groupStorageDelegate = new chip::TestPersistentStorageDelegate();
if ([self checkForInitError:(_groupStorageDelegate != nullptr) logMsg:kErrorGroupProviderInit]) {
return nil;
Expand Down Expand Up @@ -118,7 +113,8 @@ - (instancetype)init

- (void)dealloc
{
[self cleanupOwnedObjects];
[self shutdown];
[self cleanupInitObjects];
}

- (BOOL)checkForInitError:(BOOL)condition logMsg:(NSString *)logMsg
Expand All @@ -129,12 +125,12 @@ - (BOOL)checkForInitError:(BOOL)condition logMsg:(NSString *)logMsg

CHIP_LOG_ERROR("Error: %@", logMsg);

[self cleanupOwnedObjects];
[self cleanupInitObjects];

return YES;
}

- (void)cleanupOwnedObjects
- (void)cleanupInitObjects
{
_controllers = nil;

Expand All @@ -149,6 +145,11 @@ - (void)cleanupOwnedObjects
_groupStorageDelegate = nullptr;
}

Platform::MemoryShutdown();
}

- (void)cleanupStartupObjects
{
if (_attestationTrustStoreBridge) {
delete _attestationTrustStoreBridge;
_attestationTrustStoreBridge = nullptr;
Expand All @@ -158,8 +159,6 @@ - (void)cleanupOwnedObjects
delete _persistentStorageDelegateBridge;
_persistentStorageDelegateBridge = nullptr;
}

Platform::MemoryShutdown();
}

- (BOOL)startup:(MatterControllerFactoryParams *)startupParams
Expand All @@ -186,7 +185,11 @@ - (BOOL)startup:(MatterControllerFactoryParams *)startupParams

// Initialize device attestation verifier
if (startupParams.paaCerts) {
_attestationTrustStoreBridge->Init(startupParams.paaCerts);
_attestationTrustStoreBridge = new CHIPAttestationTrustStoreBridge(startupParams.paaCerts);
if (_attestationTrustStoreBridge == nullptr) {
CHIP_LOG_ERROR("Error: %@", kErrorAttestationTrustStoreInit);
return;
}
chip::Credentials::SetDeviceAttestationVerifier(chip::Credentials::GetDefaultDACVerifier(_attestationTrustStoreBridge));
} else {
// TODO: Replace testingRootStore with a AttestationTrustStore that has the necessary official PAA roots available
Expand Down Expand Up @@ -216,6 +219,10 @@ - (BOOL)startup:(MatterControllerFactoryParams *)startupParams
// Make sure to stop the event loop again before returning, so we are not running it while we don't have any controllers.
DeviceLayer::PlatformMgrImpl().StopEventLoopTask();

if (![self isRunning]) {
[self cleanupStartupObjects];
}

return [self isRunning];
}

Expand All @@ -232,13 +239,10 @@ - (void)shutdown
CHIP_LOG_DEBUG("%@", kInfoFactoryShutdown);
_controllerFactory->Shutdown();

if (_persistentStorageDelegateBridge) {
delete _persistentStorageDelegateBridge;
_persistentStorageDelegateBridge = nullptr;
}
[self cleanupStartupObjects];

// NOTE: we do not call cleanupOwnedObjects because we can be restarted, and
// that does not re-create the owned objects that we create inside init.
// NOTE: we do not call cleanupInitObjects because we can be restarted, and
// that does not re-create the objects that we create inside init.
// Maybe we should be creating them in startup?

_isRunning = NO;
Expand Down

0 comments on commit a690115

Please sign in to comment.