diff --git a/src/darwin/Framework/CHIP/MTRBaseDevice.h b/src/darwin/Framework/CHIP/MTRBaseDevice.h index c6acd9046d2c67..725f4759a10da5 100644 --- a/src/darwin/Framework/CHIP/MTRBaseDevice.h +++ b/src/darwin/Framework/CHIP/MTRBaseDevice.h @@ -79,7 +79,12 @@ NS_ASSUME_NONNULL_BEGIN * A structure-value is an NSArray object with NSDictionary objects as its elements. Each dictionary element will * contain the following key values. * - * MTRContextTagKey : NSNumber object as context tag. + * MTRContextTagKey : NSNumber object as context tag. This can + * actually be a fully-qualified profile tag, + * but for compatibility it's using the same + * key name. The two types of tags can be + * told apart by checking whether the value is + * in the context tag range (0 <= tag <= 0xFF). * MTRDataKey : Data-value NSDictionary object. * * An array-value is an NSArray object with NSDictionary objects as its elements. Each dictionary element will diff --git a/src/darwin/Framework/CHIP/MTRBaseDevice.mm b/src/darwin/Framework/CHIP/MTRBaseDevice.mm index 1d76e22d9fc53a..1c9b1a9d851f4e 100644 --- a/src/darwin/Framework/CHIP/MTRBaseDevice.mm +++ b/src/darwin/Framework/CHIP/MTRBaseDevice.mm @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#import #import #import "MTRAttributeTLVValueDecoder_Internal.h" @@ -583,7 +584,17 @@ - (void)subscribeWithQueue:(dispatch_queue_t)queue NSMutableDictionary * arrayElement = [NSMutableDictionary dictionary]; [arrayElement setObject:value forKey:MTRDataKey]; if (dataTLVType == chip::TLV::kTLVType_Structure) { - [arrayElement setObject:[NSNumber numberWithUnsignedLong:TagNumFromTag(tag)] forKey:MTRContextTagKey]; + uint64_t tagNum; + if (IsContextTag(tag)) { + tagNum = TagNumFromTag(tag); + } else if (IsProfileTag(tag)) { + uint64_t profile = ProfileIdFromTag(tag); + tagNum = (profile << kProfileIdShift) | TagNumFromTag(tag); + } else { + MTR_LOG_ERROR("Skipping unknown tag type when decoding TLV structure."); + continue; + } + [arrayElement setObject:[NSNumber numberWithUnsignedLongLong:tagNum] forKey:MTRContextTagKey]; } [array addObject:arrayElement]; } @@ -680,14 +691,28 @@ static CHIP_ERROR MTREncodeTLVFromDataValueDictionary(id object, chip::TLV::TLVW MTR_LOG_ERROR("Error: Structure element to encode has corrupt type: %@", [element class]); return CHIP_ERROR_INVALID_ARGUMENT; } - NSNumber * elementTag = element[MTRContextTagKey]; + id elementTag = element[MTRContextTagKey]; id elementValue = element[MTRDataKey]; if (!elementTag || !elementValue) { MTR_LOG_ERROR("Error: Structure element to encode has corrupt value: %@", element); return CHIP_ERROR_INVALID_ARGUMENT; } + if (![elementTag isKindOfClass:NSNumber.class]) { + MTR_LOG_ERROR("Error: Structure element to encode has corrupt tag type: %@", [elementTag class]); + return CHIP_ERROR_INVALID_ARGUMENT; + } + + // Our tag might actually be a profile tag. + uint64_t tagValue = [elementTag unsignedLongLongValue]; + TLV::Tag tag; + if (tagValue > UINT8_MAX) { + tag = TLV::ProfileTag(tagValue >> kProfileIdShift, + (tagValue & ((1ull << kProfileIdShift) - 1))); + } else { + tag = TLV::ContextTag(static_cast(tagValue)); + } ReturnErrorOnFailure( - MTREncodeTLVFromDataValueDictionary(elementValue, writer, chip::TLV::ContextTag([elementTag unsignedCharValue]))); + MTREncodeTLVFromDataValueDictionary(elementValue, writer, tag)); } ReturnErrorOnFailure(writer.EndContainer(outer)); return CHIP_NO_ERROR; @@ -751,10 +776,10 @@ CHIP_ERROR Encode(chip::TLV::TLVWriter & writer, chip::TLV::Tag tag) const static bool MustUseTimedInvoke() { return false; } - id _Nullable GetDecodedObject() const { return decodedObj; } + NSDictionary * _Nullable GetDecodedObject() const { return decodedObj; } private: - id _Nullable decodedObj; + NSDictionary * _Nullable decodedObj; }; // Callback bridge for MTRDataValueDictionaryCallback @@ -1252,15 +1277,41 @@ - (void)_invokeCommandWithEndpointID:(NSNumber *)endpointID auto * bridge = new MTRDataValueDictionaryCallbackBridge(queue, completion, ^(ExchangeManager & exchangeManager, const SessionHandle & session, MTRDataValueDictionaryCallback successCb, MTRErrorCallback failureCb, MTRCallbackBridgeBase * bridge) { + NSData * attestationChallenge; + if ([clusterID isEqualToNumber:@(MTRClusterIDTypeOperationalCredentialsID)] && + [commandID isEqualToNumber:@(MTRCommandIDTypeClusterOperationalCredentialsCommandAttestationRequestID)] && session->IsSecureSession()) { + // An AttestationResponse command needs to have an attestationChallenge + // to make sense of the results. If we are doing an + // AttestationRequest, store the challenge now. + attestationChallenge = AsData(session->AsSecureSession()->GetCryptoContext().GetAttestationChallenge()); + } // NSObjectCommandCallback guarantees that there will be exactly one call to either the success callback or the failure // callback. - auto onSuccessCb = [successCb, bridge](const app::ConcreteCommandPath & commandPath, const app::StatusIB & status, + auto onSuccessCb = [successCb, bridge, attestationChallenge](const app::ConcreteCommandPath & commandPath, const app::StatusIB & status, const MTRDataValueDictionaryDecodableType & responseData) { auto resultArray = [[NSMutableArray alloc] init]; if (responseData.GetDecodedObject()) { + auto response = responseData.GetDecodedObject(); + if (attestationChallenge != nil) { + // Add the attestationChallenge to our data. + NSArray *> * value = response[MTRValueKey]; + NSMutableArray *> * newValue = [[NSMutableArray alloc] initWithCapacity:(value.count + 1)]; + [newValue addObjectsFromArray:value]; + [newValue addObject:@{ + MTRContextTagKey : @(kAttestationChallengeTagValue), + MTRDataKey : @ { + MTRTypeKey : MTROctetStringValueType, + MTRValueKey : attestationChallenge, + }, + }]; + auto * newResponse = [NSMutableDictionary dictionaryWithCapacity:(response.count + 1)]; + [newResponse addEntriesFromDictionary:response]; + newResponse[MTRValueKey] = newValue; + response = newResponse; + } [resultArray addObject:@ { MTRCommandPathKey : [[MTRCommandPath alloc] initWithPath:commandPath], - MTRDataKey : responseData.GetDecodedObject() + MTRDataKey : response, }]; } else { [resultArray addObject:@ { MTRCommandPathKey : [[MTRCommandPath alloc] initWithPath:commandPath] }]; diff --git a/src/darwin/Framework/CHIP/MTRBaseDevice_Internal.h b/src/darwin/Framework/CHIP/MTRBaseDevice_Internal.h index 671ef1a25b2660..a5732ed0120c77 100644 --- a/src/darwin/Framework/CHIP/MTRBaseDevice_Internal.h +++ b/src/darwin/Framework/CHIP/MTRBaseDevice_Internal.h @@ -26,10 +26,24 @@ #include #include #include +#include +#include #include @class MTRDeviceController; +// An AttestationResponse command needs to have an attestationChallenge +// to make sense of the results. Encode that with a profile-specific tag under +// the Apple vendor id. Let's select profile 0xFFFF just because. +inline constexpr chip::TLV::Tag kAttestationChallengeTag = chip::TLV::ProfileTag(chip::VendorId::Apple, 0xFFFF, 0); + +// We have no way to extract the tag value as a single thing, so just do it +// manually. +inline constexpr unsigned kProfileIdShift = 32; +inline constexpr uint64_t kAttestationChallengeTagProfile = chip::TLV::ProfileIdFromTag(kAttestationChallengeTag); +inline constexpr uint64_t kAttestationChallengeTagNumber = chip::TLV::TagNumFromTag(kAttestationChallengeTag); +inline constexpr uint64_t kAttestationChallengeTagValue = (kAttestationChallengeTagProfile << kProfileIdShift) | kAttestationChallengeTagNumber; + NS_ASSUME_NONNULL_BEGIN static inline MTRTransportType MTRMakeTransportType(chip::Transport::Type type) diff --git a/src/darwin/Framework/CHIP/MTRCallbackBridgeBase.h b/src/darwin/Framework/CHIP/MTRCallbackBridgeBase.h index e1f81b101bc88e..e99030749a5baa 100644 --- a/src/darwin/Framework/CHIP/MTRCallbackBridgeBase.h +++ b/src/darwin/Framework/CHIP/MTRCallbackBridgeBase.h @@ -20,18 +20,13 @@ #import "MTRBaseDevice_Internal.h" #import "MTRDeviceController_Internal.h" #import "MTRError_Internal.h" -#import "NSDataSpanConversion.h" #import "zap-generated/MTRBaseClusters.h" -#import "zap-generated/MTRCommandPayloads_Internal.h" -#include #include #include #include #include -#include - NS_ASSUME_NONNULL_BEGIN /** @@ -152,23 +147,8 @@ using MTRActionBlockT = CHIP_ERROR (^)(chip::Messaging::ExchangeManager & exchan template using MTRLocalActionBlockT = CHIP_ERROR (^)(SuccessCallback successCb, MTRErrorCallback failureCb); -class NoAttestationChallenge { -}; - -class HaveAttestationChallenge { -protected: - NSData * mAttestationChallenge; -}; - -namespace detail { -using AttestationResponseCallback - = void (*)(void *, const chip::app::Clusters::OperationalCredentials::Commands::AttestationResponse::DecodableType &); -} // namespace detail - template -class MTRCallbackBridge : public MTRCallbackBridgeBase, - protected std::conditional, - HaveAttestationChallenge, NoAttestationChallenge>::type { +class MTRCallbackBridge : public MTRCallbackBridgeBase { public: using MTRActionBlock = MTRActionBlockT; using MTRLocalActionBlock = MTRLocalActionBlockT; @@ -253,10 +233,6 @@ class MTRCallbackBridge : public MTRCallbackBridgeBase, return; } - if constexpr (HaveAttestationChallenge()) { - this->mAttestationChallenge = AsData(session.Value()->AsSecureSession()->GetCryptoContext().GetAttestationChallenge()); - } - CHIP_ERROR err = action(*exchangeManager, session.Value(), mSuccess, mFailure, this); if (err != CHIP_NO_ERROR) { ChipLogError(Controller, "Failure performing action. C++-mangled success callback type: '%s', error: %s", @@ -277,18 +253,7 @@ class MTRCallbackBridge : public MTRCallbackBridgeBase, static void DispatchFailure(void * context, NSError * error) { DispatchCallbackResult(context, error, nil); } - template - static void SetAttestationChallengeIfNeeded(void * context, ResponseType * _Nonnull response) - { - if constexpr (HaveAttestationChallenge()) { - auto * self = static_cast(context); - response.attestationChallenge = self->mAttestationChallenge; - } - } - private: - static constexpr bool HaveAttestationChallenge() { return std::is_same_v; } - static void DispatchCallbackResult(void * context, NSError * _Nullable error, id _Nullable value) { MTRCallbackBridge * callbackBridge = static_cast(context); diff --git a/src/darwin/Framework/CHIP/templates/MTRCommandPayloadsObjc-src.zapt b/src/darwin/Framework/CHIP/templates/MTRCommandPayloadsObjc-src.zapt index 960e15a392407a..ca272fb7017cf7 100644 --- a/src/darwin/Framework/CHIP/templates/MTRCommandPayloadsObjc-src.zapt +++ b/src/darwin/Framework/CHIP/templates/MTRCommandPayloadsObjc-src.zapt @@ -97,6 +97,84 @@ NS_ASSUME_NONNULL_BEGIN err = chip::app::DataModel::Decode(reader, decodedStruct); if (err == CHIP_NO_ERROR) { err = [self _setFieldsFromDecodableStruct:decodedStruct]; + {{#if (and (isStrEqual (asUpperCamelCase parent.name preserveAcronyms=true) "OperationalCredentials") + (isStrEqual (asUpperCamelCase name preserveAcronyms=true) "AttestationResponse"))}} + if (err == CHIP_NO_ERROR) { + do { + // AttestationResponse has an extra attestationChallenge field. Once we + // have some sort of more direct decoding from the responseValue, we can + // probably make this less hardcoded. + // + // It might be simpler to look for the right profile tag in the TLV, but let's stick to examining + // the responseValue we were handed. + id data = responseValue[MTRDataKey]; + if (![data isKindOfClass:NSDictionary.class]) { + err = CHIP_ERROR_INVALID_ARGUMENT; + break; + } + + NSDictionary * dataDictionary = data; + if (dataDictionary[MTRTypeKey] == nil || ![dataDictionary[MTRTypeKey] isEqualTo:MTRStructureValueType]) { + err = CHIP_ERROR_INVALID_ARGUMENT; + break; + } + + id value = dataDictionary[MTRValueKey]; + if (value == nil || ![value isKindOfClass:NSArray.class]) { + err = CHIP_ERROR_INVALID_ARGUMENT; + break; + } + + NSArray * valueArray = value; + for (id item in valueArray) { + if (![item isKindOfClass:NSDictionary.class]) { + err = CHIP_ERROR_INVALID_ARGUMENT; + break; + } + + NSDictionary * itemDictionary = item; + id contextTag = itemDictionary[MTRContextTagKey]; + if (contextTag == nil || ![contextTag isKindOfClass:NSNumber.class]) { + err = CHIP_ERROR_INVALID_ARGUMENT; + break; + } + + NSNumber * contextTagNumber = contextTag; + if (![contextTagNumber isEqualToNumber:@(kAttestationChallengeTagValue)]) { + // Not the right field; keep going. + continue; + } + + id data = itemDictionary[MTRDataKey]; + if (data == nil || ![data isKindOfClass:NSDictionary.class]) { + err = CHIP_ERROR_INVALID_ARGUMENT; + break; + } + + NSDictionary * dataDictionary = data; + id dataType = dataDictionary[MTRTypeKey]; + id dataValue = dataDictionary[MTRValueKey]; + if (dataType == nil || dataValue == nil || + ![dataType isKindOfClass:NSString.class] || + ![dataValue isKindOfClass:NSData.class]) { + err = CHIP_ERROR_INVALID_ARGUMENT; + break; + } + + NSString * dataTypeString = dataType; + if (![dataTypeString isEqualToString:MTROctetStringValueType]) { + err = CHIP_ERROR_INVALID_ARGUMENT; + break; + } + + self.attestationChallenge = dataValue; + break; + } + + // Do not add code here without first checking whether err is success. + } while (0); + } + {{/if}} if (err == CHIP_NO_ERROR) { return self; } diff --git a/src/darwin/Framework/CHIP/templates/MTRCommandPayloads_Internal.zapt b/src/darwin/Framework/CHIP/templates/MTRCommandPayloads_Internal.zapt index 83a6d1c44b5bc1..0919cbf306f4fc 100644 --- a/src/darwin/Framework/CHIP/templates/MTRCommandPayloads_Internal.zapt +++ b/src/darwin/Framework/CHIP/templates/MTRCommandPayloads_Internal.zapt @@ -7,7 +7,7 @@ NS_ASSUME_NONNULL_BEGIN @interface MTROperationalCredentialsClusterAttestationResponseParams () -@property (nonatomic, strong) NSData * attestationChallenge; +@property (nonatomic, copy) NSData * attestationChallenge; @end {{#zcl_clusters}} diff --git a/src/darwin/Framework/CHIP/zap-generated/MTRCommandPayloadsObjc.mm b/src/darwin/Framework/CHIP/zap-generated/MTRCommandPayloadsObjc.mm index c9e838300eff1a..c9b825fe7ab8e3 100644 --- a/src/darwin/Framework/CHIP/zap-generated/MTRCommandPayloadsObjc.mm +++ b/src/darwin/Framework/CHIP/zap-generated/MTRCommandPayloadsObjc.mm @@ -9783,6 +9783,79 @@ - (nullable instancetype)initWithResponseValue:(NSDictionary *)r err = chip::app::DataModel::Decode(reader, decodedStruct); if (err == CHIP_NO_ERROR) { err = [self _setFieldsFromDecodableStruct:decodedStruct]; + if (err == CHIP_NO_ERROR) { + do { + // AttestationResponse has an extra attestationChallenge field. Once we + // have some sort of more direct decoding from the responseValue, we can + // probably make this less hardcoded. + // + // It might be simpler to look for the right profile tag in the TLV, but let's stick to examining + // the responseValue we were handed. + id data = responseValue[MTRDataKey]; + if (![data isKindOfClass:NSDictionary.class]) { + err = CHIP_ERROR_INVALID_ARGUMENT; + break; + } + + NSDictionary * dataDictionary = data; + if (dataDictionary[MTRTypeKey] == nil || ![dataDictionary[MTRTypeKey] isEqualTo:MTRStructureValueType]) { + err = CHIP_ERROR_INVALID_ARGUMENT; + break; + } + + id value = dataDictionary[MTRValueKey]; + if (value == nil || ![value isKindOfClass:NSArray.class]) { + err = CHIP_ERROR_INVALID_ARGUMENT; + break; + } + + NSArray * valueArray = value; + for (id item in valueArray) { + if (![item isKindOfClass:NSDictionary.class]) { + err = CHIP_ERROR_INVALID_ARGUMENT; + break; + } + + NSDictionary * itemDictionary = item; + id contextTag = itemDictionary[MTRContextTagKey]; + if (contextTag == nil || ![contextTag isKindOfClass:NSNumber.class]) { + err = CHIP_ERROR_INVALID_ARGUMENT; + break; + } + + NSNumber * contextTagNumber = contextTag; + if (![contextTagNumber isEqualToNumber:@(kAttestationChallengeTagValue)]) { + // Not the right field; keep going. + continue; + } + + id data = itemDictionary[MTRDataKey]; + if (data == nil || ![data isKindOfClass:NSDictionary.class]) { + err = CHIP_ERROR_INVALID_ARGUMENT; + break; + } + + NSDictionary * dataDictionary = data; + id dataType = dataDictionary[MTRTypeKey]; + id dataValue = dataDictionary[MTRValueKey]; + if (dataType == nil || dataValue == nil || ![dataType isKindOfClass:NSString.class] || ![dataValue isKindOfClass:NSData.class]) { + err = CHIP_ERROR_INVALID_ARGUMENT; + break; + } + + NSString * dataTypeString = dataType; + if (![dataTypeString isEqualToString:MTROctetStringValueType]) { + err = CHIP_ERROR_INVALID_ARGUMENT; + break; + } + + self.attestationChallenge = dataValue; + break; + } + + // Do not add code here without first checking whether err is success. + } while (0); + } if (err == CHIP_NO_ERROR) { return self; } diff --git a/src/darwin/Framework/CHIP/zap-generated/MTRCommandPayloads_Internal.h b/src/darwin/Framework/CHIP/zap-generated/MTRCommandPayloads_Internal.h index fdecc5a05fdd24..482959a1683c9f 100644 --- a/src/darwin/Framework/CHIP/zap-generated/MTRCommandPayloads_Internal.h +++ b/src/darwin/Framework/CHIP/zap-generated/MTRCommandPayloads_Internal.h @@ -22,7 +22,7 @@ NS_ASSUME_NONNULL_BEGIN @interface MTROperationalCredentialsClusterAttestationResponseParams () -@property (nonatomic, strong) NSData * attestationChallenge; +@property (nonatomic, copy) NSData * attestationChallenge; @end @interface MTRIdentifyClusterIdentifyParams (InternalMethods) diff --git a/src/darwin/Framework/CHIPTests/MTRDeviceTests.m b/src/darwin/Framework/CHIPTests/MTRDeviceTests.m index 661bde70075fee..b9f90524289cb0 100644 --- a/src/darwin/Framework/CHIPTests/MTRDeviceTests.m +++ b/src/darwin/Framework/CHIPTests/MTRDeviceTests.m @@ -25,6 +25,7 @@ #import #import "MTRErrorTestUtils.h" +#import "MTRTestCppAdapter.h" #import "MTRTestKeys.h" #import "MTRTestResetCommissioneeHelper.h" #import "MTRTestStorage.h" @@ -2446,6 +2447,83 @@ - (void)test026_LocationAttribute [self waitForExpectations:@[ expectation ] timeout:kTimeoutInSeconds]; } +- (void)test027_AttestationChallenge +{ + // Check that we have an attestation challenge result in + // MTROperationalCredentialsClusterAttestationResponseParams. + dispatch_queue_t queue = dispatch_get_main_queue(); + + // We don't care about our nonce being random here, so just all-0 is fine. + __auto_type * nonce = [[NSMutableData alloc] initWithLength:32]; + __auto_type * params = [[MTROperationalCredentialsClusterAttestationRequestParams alloc] init]; + params.attestationNonce = nonce; + + __auto_type * baseDevice = GetConnectedDevice(); + __auto_type * baseCluster = [[MTRBaseClusterOperationalCredentials alloc] initWithDevice:baseDevice endpointID:@(0) queue:queue]; + XCTestExpectation * attestationRequestedViaBaseCluster = [self expectationWithDescription:@"Invoked AttestationRequest via base cluster"]; + [baseCluster attestationRequestWithParams:params completion:^(MTROperationalCredentialsClusterAttestationResponseParams * _Nullable data, NSError * _Nullable error) { + XCTAssertNil(error); + XCTAssertNotNil(data); + XCTAssertNotNil(GetAttestationChallenge(data)); + [attestationRequestedViaBaseCluster fulfill]; + }]; + + [self waitForExpectations:@[ attestationRequestedViaBaseCluster ] timeout:kTimeoutInSeconds]; + + __auto_type * requestFields = @{ + MTRTypeKey : MTRStructureValueType, + MTRValueKey : @[ + @{ + MTRContextTagKey : @(0), // AttestationNonce + MTRDataKey : @ { + MTRTypeKey : MTROctetStringValueType, + MTRValueKey : nonce, + }, + }, + ], + }; + + XCTestExpectation * attestationRequestedViaBaseDevice = [self expectationWithDescription:@"Invoked AttestationRequest via base device"]; + [baseDevice invokeCommandWithEndpointID:@(0) clusterID:@(MTRClusterIDTypeOperationalCredentialsID) commandID:@(MTRCommandIDTypeClusterOperationalCredentialsCommandAttestationRequestID) commandFields:requestFields timedInvokeTimeout:nil queue:queue completion:^(NSArray *> * _Nullable values, NSError * _Nullable error) { + XCTAssertNil(error); + XCTAssertNotNil(values); + XCTAssertEqual(values.count, 1); + __auto_type * response = [[MTROperationalCredentialsClusterAttestationResponseParams alloc] initWithResponseValue:values[0] error:&error]; + XCTAssertNil(error); + XCTAssertNotNil(response); + XCTAssertNotNil(GetAttestationChallenge(response)); + [attestationRequestedViaBaseDevice fulfill]; + }]; + + [self waitForExpectations:@[ attestationRequestedViaBaseDevice ] timeout:kTimeoutInSeconds]; + + __auto_type * device = [MTRDevice deviceWithNodeID:kDeviceId deviceController:sController]; + __auto_type * cluster = [[MTRClusterOperationalCredentials alloc] initWithDevice:device endpointID:@(0) queue:queue]; + XCTestExpectation * attestationRequestedViaCluster = [self expectationWithDescription:@"Invoked AttestationRequest via cluster"]; + [cluster attestationRequestWithParams:params expectedValues:nil expectedValueInterval:nil completion:^(MTROperationalCredentialsClusterAttestationResponseParams * _Nullable data, NSError * _Nullable error) { + XCTAssertNil(error); + XCTAssertNotNil(data); + XCTAssertNotNil(GetAttestationChallenge(data)); + [attestationRequestedViaCluster fulfill]; + }]; + + [self waitForExpectations:@[ attestationRequestedViaCluster ] timeout:kTimeoutInSeconds]; + + XCTestExpectation * attestationRequestedViaDevice = [self expectationWithDescription:@"Invoked AttestationRequest via device"]; + [device invokeCommandWithEndpointID:@(0) clusterID:@(MTRClusterIDTypeOperationalCredentialsID) commandID:@(MTRCommandIDTypeClusterOperationalCredentialsCommandAttestationRequestID) commandFields:requestFields expectedValues:nil expectedValueInterval:nil timedInvokeTimeout:nil queue:queue completion:^(NSArray *> * _Nullable values, NSError * _Nullable error) { + XCTAssertNil(error); + XCTAssertNotNil(values); + XCTAssertEqual(values.count, 1); + __auto_type * response = [[MTROperationalCredentialsClusterAttestationResponseParams alloc] initWithResponseValue:values[0] error:&error]; + XCTAssertNil(error); + XCTAssertNotNil(response); + XCTAssertNotNil(GetAttestationChallenge(response)); + [attestationRequestedViaDevice fulfill]; + }]; + + [self waitForExpectations:@[ attestationRequestedViaDevice ] timeout:kTimeoutInSeconds]; +} + - (void)test900_SubscribeAllAttributes { MTRBaseDevice * device = GetConnectedDevice(); diff --git a/src/darwin/Framework/CHIPTests/MTRSwiftDeviceTests.swift b/src/darwin/Framework/CHIPTests/MTRSwiftDeviceTests.swift index e40c855763d60b..badcbabbca2fd2 100644 --- a/src/darwin/Framework/CHIPTests/MTRSwiftDeviceTests.swift +++ b/src/darwin/Framework/CHIPTests/MTRSwiftDeviceTests.swift @@ -389,6 +389,9 @@ class MTRSwiftDeviceTests : XCTestCase { XCTAssertEqual(eventReportsReceived, 0); } + // Note: test027_AttestationChallenge is not implementable in Swift so far, + // because the attestationChallenge property is internal-only + func test999_TearDown() { ResetCommissionee(sConnectedDevice, DispatchQueue.main, self, DeviceConstants.timeoutInSeconds) diff --git a/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestCppAdapter.h b/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestCppAdapter.h new file mode 100644 index 00000000000000..8017d9031ce190 --- /dev/null +++ b/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestCppAdapter.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Project CHIP Authors + * + * 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. + */ + +/** + * Utilities for doing things whose implementation requires being in Objective + * C++ from tests that are Objective C (without just creating a whole separate + * Objective C++ test). + */ + +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +#ifdef __cplusplus +extern "C" +#endif + NSData * _Nullable GetAttestationChallenge(MTROperationalCredentialsClusterAttestationResponseParams * params); + +NS_ASSUME_NONNULL_END diff --git a/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestCppAdapter.mm b/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestCppAdapter.mm new file mode 100644 index 00000000000000..05f69a1ca7c477 --- /dev/null +++ b/src/darwin/Framework/CHIPTests/TestHelpers/MTRTestCppAdapter.mm @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2023 Project CHIP Authors + * + * 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. + */ + +#import "MTRTestCppAdapter.h" + +#import "MTRCommandPayloads_Internal.h" + +NSData * _Nullable GetAttestationChallenge(MTROperationalCredentialsClusterAttestationResponseParams * params) +{ + return params.attestationChallenge; +} diff --git a/src/darwin/Framework/Matter.xcodeproj/project.pbxproj b/src/darwin/Framework/Matter.xcodeproj/project.pbxproj index 87196b6063c07d..5788fe09931680 100644 --- a/src/darwin/Framework/Matter.xcodeproj/project.pbxproj +++ b/src/darwin/Framework/Matter.xcodeproj/project.pbxproj @@ -192,6 +192,9 @@ 51E95DFB2A78443C00A434F0 /* MTRSessionResumptionStorageBridge.h in Headers */ = {isa = PBXBuildFile; fileRef = 51E95DF92A78443C00A434F0 /* MTRSessionResumptionStorageBridge.h */; }; 51E95DFC2A78443C00A434F0 /* MTRSessionResumptionStorageBridge.mm in Sources */ = {isa = PBXBuildFile; fileRef = 51E95DFA2A78443C00A434F0 /* MTRSessionResumptionStorageBridge.mm */; }; 51EF279F2A2A3EB100E33F75 /* MTRBackwardsCompatShims.h in Headers */ = {isa = PBXBuildFile; fileRef = 51EF279E2A2A3EB100E33F75 /* MTRBackwardsCompatShims.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 51FE72352ACDB40000437032 /* MTRCommandPayloads_Internal.h in Headers */ = {isa = PBXBuildFile; fileRef = 51FE72342ACDB40000437032 /* MTRCommandPayloads_Internal.h */; }; + 51FE72382ACDB64A00437032 /* MTRTestCppAdapter.mm in Sources */ = {isa = PBXBuildFile; fileRef = 51FE72362ACDB64A00437032 /* MTRTestCppAdapter.mm */; }; + 51FE72392ACDB64A00437032 /* MTRTestCppAdapter.h in Headers */ = {isa = PBXBuildFile; fileRef = 51FE72372ACDB64A00437032 /* MTRTestCppAdapter.h */; }; 5A60370827EA1FF60020DB79 /* MTRClusterStateCacheContainer+XPC.h in Headers */ = {isa = PBXBuildFile; fileRef = 5A60370727EA1FF60020DB79 /* MTRClusterStateCacheContainer+XPC.h */; }; 5A6FEC9027B563D900F25F42 /* MTRDeviceControllerOverXPC.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5A6FEC8F27B563D900F25F42 /* MTRDeviceControllerOverXPC.mm */; }; 5A6FEC9227B5669C00F25F42 /* MTRDeviceControllerOverXPC.h in Headers */ = {isa = PBXBuildFile; fileRef = 5A6FEC8D27B5624E00F25F42 /* MTRDeviceControllerOverXPC.h */; }; @@ -552,6 +555,9 @@ 51E95DF92A78443C00A434F0 /* MTRSessionResumptionStorageBridge.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRSessionResumptionStorageBridge.h; sourceTree = ""; }; 51E95DFA2A78443C00A434F0 /* MTRSessionResumptionStorageBridge.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRSessionResumptionStorageBridge.mm; sourceTree = ""; }; 51EF279E2A2A3EB100E33F75 /* MTRBackwardsCompatShims.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRBackwardsCompatShims.h; sourceTree = ""; }; + 51FE72342ACDB40000437032 /* MTRCommandPayloads_Internal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRCommandPayloads_Internal.h; sourceTree = ""; }; + 51FE72362ACDB64A00437032 /* MTRTestCppAdapter.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRTestCppAdapter.mm; sourceTree = ""; }; + 51FE72372ACDB64A00437032 /* MTRTestCppAdapter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTRTestCppAdapter.h; sourceTree = ""; }; 5A60370727EA1FF60020DB79 /* MTRClusterStateCacheContainer+XPC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "MTRClusterStateCacheContainer+XPC.h"; sourceTree = ""; }; 5A6FEC8B27B5609C00F25F42 /* MTRDeviceOverXPC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MTRDeviceOverXPC.h; sourceTree = ""; }; 5A6FEC8D27B5624E00F25F42 /* MTRDeviceControllerOverXPC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MTRDeviceControllerOverXPC.h; sourceTree = ""; }; @@ -984,6 +990,7 @@ 7596A85228788557004DAE0E /* MTRClusters.mm */, 51B22C212740CB1D008D5055 /* MTRCommandPayloadsObjc.h */, 51B22C292740CB47008D5055 /* MTRCommandPayloadsObjc.mm */, + 51FE72342ACDB40000437032 /* MTRCommandPayloads_Internal.h */, 7560FD1B27FBBD3F005E85B3 /* MTREventTLVValueDecoder.mm */, 51B22C1D2740CB0A008D5055 /* MTRStructsObjc.h */, 51B22C252740CB32008D5055 /* MTRStructsObjc.mm */, @@ -1062,6 +1069,8 @@ 518D3F822AA132DC008E0007 /* MTRTestPerControllerStorage.h */, 518D3F812AA132DC008E0007 /* MTRTestPerControllerStorage.m */, 51C984602A61CE2A00B0AD9A /* MTRFabricInfoChecker.m */, + 51FE72372ACDB64A00437032 /* MTRTestCppAdapter.h */, + 51FE72362ACDB64A00437032 /* MTRTestCppAdapter.mm */, ); path = TestHelpers; sourceTree = ""; @@ -1424,6 +1433,7 @@ 3CF134AF289D90FF0017A19E /* MTROperationalCertificateIssuer.h in Headers */, 3CF134AB289D8DF70017A19E /* MTRDeviceAttestationInfo.h in Headers */, B2E0D7B2245B0B5C003C5B48 /* MTRManualSetupPayloadParser.h in Headers */, + 51FE72392ACDB64A00437032 /* MTRTestCppAdapter.h in Headers */, 3CF134A7289D8ADA0017A19E /* MTRCSRInfo.h in Headers */, B2E0D7B1245B0B5C003C5B48 /* Matter.h in Headers */, 7596A84428762729004DAE0E /* MTRDevice.h in Headers */, @@ -1454,6 +1464,7 @@ 2C5EEEF6268A85C400CAE3D3 /* MTRDeviceConnectionBridge.h in Headers */, 2C8C8FC0253E0C2100797F05 /* MTRPersistentStorageDelegateBridge.h in Headers */, 51E4D121291D0EB400C8C535 /* MTRBaseClusterUtils.h in Headers */, + 51FE72352ACDB40000437032 /* MTRCommandPayloads_Internal.h in Headers */, 51E51FC0282AD37A00FC978D /* MTRDeviceControllerStartupParams_Internal.h in Headers */, 3DECCB702934AECD00585AEC /* MTRLogging.h in Headers */, 1E4D654E29C208DD00BC3478 /* MTRCommissionableBrowserResult.h in Headers */, @@ -1703,6 +1714,7 @@ buildActionMask = 2147483647; files = ( 2C8C8FC2253E0C2100797F05 /* MTRPersistentStorageDelegateBridge.mm in Sources */, + 51FE72382ACDB64A00437032 /* MTRTestCppAdapter.mm in Sources */, 99AECC802798A57F00B6355B /* MTRCommissioningParameters.mm in Sources */, 2CB7163C252E8A7C0026E2BB /* MTRDeviceControllerDelegateBridge.mm in Sources */, 997DED162695343400975E97 /* MTRThreadOperationalDataset.mm in Sources */,