Skip to content

Commit

Permalink
Adding darwin callbacks for NOC Generation (#21705)
Browse files Browse the repository at this point in the history
* Adding darwin callbacks for NOC Generation

* Fixing compiler errors in new dependencies in src/credentials

* Addressing feedback from woody-apple@ and bzbarsky-apple@

* Addressing second round of feedback

* Address feedback around returning errors and adding comments

* Adding comments

Co-authored-by: Justin Wood <woody@apple.com>
  • Loading branch information
2 people authored and pull[bot] committed Jul 26, 2023
1 parent 8661864 commit 2732633
Show file tree
Hide file tree
Showing 15 changed files with 513 additions and 9 deletions.
5 changes: 5 additions & 0 deletions src/controller/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,11 @@ class DLL_EXPORT DeviceCommissioner : public DeviceController,
mDeviceAttestationVerifier = deviceAttestationVerifier;
}

Optional<CommissioningParameters> GetCommissioningParameters()
{
return mDefaultCommissioner == nullptr ? NullOptional : MakeOptional(mDefaultCommissioner->GetCommissioningParameters());
}

private:
DevicePairingDelegate * mPairingDelegate;

Expand Down
4 changes: 2 additions & 2 deletions src/controller/java/AndroidOperationalCredentialsIssuer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ CHIP_ERROR AndroidOperationalCredentialsIssuer::LocalGenerateNOCChain(const Byte
return CHIP_NO_ERROR;
}

CHIP_ERROR N2J_CSRInfo(JNIEnv * env, jbyteArray nonce, jbyteArray elements, jbyteArray elementsSignature, jbyteArray csr,
CHIP_ERROR N2J_CSRInfo(JNIEnv * env, jbyteArray nonce, jbyteArray elements, jbyteArray csrElementsSignature, jbyteArray csr,
jobject & outCSRInfo)
{
CHIP_ERROR err = CHIP_NO_ERROR;
Expand All @@ -393,7 +393,7 @@ CHIP_ERROR N2J_CSRInfo(JNIEnv * env, jbyteArray nonce, jbyteArray elements, jbyt
constructor = env->GetMethodID(infoClass, "<init>", "([B[B[B[B)V");
VerifyOrExit(constructor != nullptr, err = CHIP_JNI_ERROR_METHOD_NOT_FOUND);

outCSRInfo = (jobject) env->NewObject(infoClass, constructor, nonce, elements, elementsSignature, csr);
outCSRInfo = (jobject) env->NewObject(infoClass, constructor, nonce, elements, csrElementsSignature, csr);

VerifyOrExit(!env->ExceptionCheck(), err = CHIP_JNI_ERROR_EXCEPTION_THROWN);
exit:
Expand Down
12 changes: 6 additions & 6 deletions src/credentials/DeviceAttestationConstructor.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ constexpr size_t kExpectedAttestationNonceSize = 32;
* All output data stays valid while attestationElements buffer is valid.
*
* @param[in] attestationElements ByteSpan containing source of Attestation Elements data.
* @param[out] certificationDeclaration
* @param[out] attestationNonce
* @param[out] timestamp
* @param[out] certificationDeclaration Valid Certification Declaration data.
* @param[out] attestationNonce Attestation Nonce - 32 octets required.
* @param[out] timestamp Timestamp data in epoch time format.
* @param[out] firmwareInfo ByteSpan containing Firmware Information data if present within attestationElements.
* Empty ByteSpan if not present in attestationElements.
* @param[out] VendorReserved Placeholder to for client to examine VendorReserved elements later
* @param[out] vendorReserved Placeholder to for client to examine vendorReserved elements later
*/
CHIP_ERROR DeconstructAttestationElements(const ByteSpan & attestationElements, ByteSpan & certificationDeclaration,
ByteSpan & attestationNonce, uint32_t & timestamp, ByteSpan & firmwareInfo,
Expand All @@ -49,7 +49,7 @@ CHIP_ERROR DeconstructAttestationElements(const ByteSpan & attestationElements,
* @param[in] attestationNonce Attestation Nonce - 32 octets required.
* @param[in] timestamp Timestamp data in epoch time format.
* @param[in] firmwareInfo Optional Firmware Information data - Can be empty.
* @param[in] VendorReserved Prefilled-in vendor reserved elements to be put into DA elements.
* @param[in] vendorReserved Prefilled-in vendor reserved elements to be put into DA elements.
* @param[out] attestationElements Buffer used to write all AttestationElements data, formed with all the data fields above.
* Provided buffer needs to be capable to handle all data fields + tags.
*/
Expand All @@ -62,7 +62,7 @@ CHIP_ERROR ConstructAttestationElements(const ByteSpan & certificationDeclaratio
* @brief Count the number of VendorReservedElements in a DeviceAttestation blob
*
* @param[in] attestationElements ByeSpan conitaining source of Attestation Elements data
* @param[out]
* @param[out] numElements Count of vendor reserved elements in the DeviceAttestation
* @returns CHIP_NO_ERROR on success
*/
CHIP_ERROR CountVendorReservedElementsInDA(const ByteSpan & attestationElements, size_t & numElements);
Expand Down
2 changes: 1 addition & 1 deletion src/credentials/DeviceAttestationVendorReserved.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ class DeviceAttestationVendorReservedConstructor

// first lowest tagNum for this vendorId/profileNum
uint64_t minTagNum = UINT64_MAX;
size_t lowestIndex;
size_t lowestIndex = SIZE_MAX;
for (i = starting; i < mNumEntriesUsed; i++)
{
if (mElements[i].vendorId == minVendor && mElements[i].profileNum == minProfile)
Expand Down
55 changes: 55 additions & 0 deletions src/darwin/Framework/CHIP/MTRAttestationInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
*
* Copyright (c) 2022 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 <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

/**
* Represents information relating to product attestation.
*
*/
@interface AttestationInfo : NSObject

@property (nonatomic, copy) NSData * challenge;

@property (nonatomic, copy) NSData * nonce;

@property (nonatomic, copy) NSData * elements;

@property (nonatomic, copy) NSData * elementsSignature;

@property (nonatomic, copy) NSData * dac;

@property (nonatomic, copy) NSData * pai;

@property (nonatomic, copy) NSData * certificationDeclaration;

@property (nonatomic, copy) NSData * firmwareInfo;

- (instancetype)initWithChallenge:(NSData *)challenge
nonce:(NSData *)nonce
elements:(NSData *)elements
elementsSignature:(NSData *)elementsSignature
dac:(NSData *)dac
pai:(NSData *)pai
certificationDeclaration:(NSData *)certificationDeclaration
firmwareInfo:(NSData *)firmwareInfo;

@end

NS_ASSUME_NONNULL_END
48 changes: 48 additions & 0 deletions src/darwin/Framework/CHIP/MTRAttestationInfo.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
*
* Copyright (c) 2022 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 "MTRAttestationInfo.h"

NS_ASSUME_NONNULL_BEGIN

@implementation AttestationInfo : NSObject

- (instancetype)initWithChallenge:(NSData *)challenge
nonce:(NSData *)nonce
elements:(NSData *)elements
elementsSignature:(NSData *)elementsSignature
dac:(NSData *)dac
pai:(NSData *)pai
certificationDeclaration:(NSData *)certificationDeclaration
firmwareInfo:(NSData *)firmwareInfo
{
if (self = [super init]) {
_challenge = challenge;
_nonce = nonce;
_elements = elements;
_elementsSignature = elementsSignature;
_dac = dac;
_pai = pai;
_certificationDeclaration = certificationDeclaration;
_firmwareInfo = firmwareInfo;
}
return self;
}

@end

NS_ASSUME_NONNULL_END
43 changes: 43 additions & 0 deletions src/darwin/Framework/CHIP/MTRCSRInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
*
* Copyright (c) 2022 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 <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

/**
* Represents information relating to NOC CSR.
*
*/
@interface CSRInfo : NSObject

@property (nonatomic, copy) NSData * nonce;

@property (nonatomic, copy) NSData * elements;

@property (nonatomic, copy) NSData * elementsSignature;

@property (nonatomic, copy) NSData * csr;

- (instancetype)initWithNonce:(NSData *)nonce
elements:(NSData *)elements
elementsSignature:(NSData *)elementsSignature
csr:(NSData *)csr;

@end

NS_ASSUME_NONNULL_END
39 changes: 39 additions & 0 deletions src/darwin/Framework/CHIP/MTRCSRInfo.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
*
* Copyright (c) 2022 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 "MTRCSRInfo.h"

NS_ASSUME_NONNULL_BEGIN

@implementation CSRInfo : NSObject

- (instancetype)initWithNonce:(NSData *)nonce
elements:(NSData *)elements
elementsSignature:(NSData *)elementsSignature
csr:(NSData *)csr
{
if (self = [super init]) {
_nonce = nonce;
_elements = elements;
_elementsSignature = elementsSignature;
_csr = csr;
}
return self;
}
@end

NS_ASSUME_NONNULL_END
11 changes: 11 additions & 0 deletions src/darwin/Framework/CHIP/MTRDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#import <Foundation/Foundation.h>

#import <Matter/MTRNOCChainIssuer.h>
#import <Matter/MTROnboardingPayloadParser.h>

@class MTRBaseDevice;
Expand Down Expand Up @@ -119,6 +120,16 @@ typedef void (^MTRDeviceConnectionCallback)(MTRBaseDevice * _Nullable device, NS
*/
- (void)setPairingDelegate:(id<MTRDevicePairingDelegate>)delegate queue:(dispatch_queue_t)queue;

/**
* Sets this MTRDeviceController to use the given issuer for issuing operational certs. By default, the MTRDeviceController uses an
* internal issuer.
*
* @param[in] nocChainIssuer the NOC Chain issuer to use for issuer operational certs
*
* @param[in] queue The queue on which the callbacks will be delivered
*/
- (void)setNocChainIssuer:(id<MTRNOCChainIssuer>)nocChainIssuer queue:(dispatch_queue_t)queue;

/**
* Shutdown the controller. Calls to shutdown after the first one are NO-OPs.
*/
Expand Down
14 changes: 14 additions & 0 deletions src/darwin/Framework/CHIP/MTRDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ - (instancetype)initWithFactory:(MTRControllerFactory *)factory queue:(dispatch_
if ([self checkForInitError:(_operationalCredentialsDelegate != nullptr) logMsg:kErrorOperationalCredentialsInit]) {
return nil;
}
_operationalCredentialsDelegate->setChipWorkQueue(_chipWorkQueue);
}
return self;
}
Expand Down Expand Up @@ -229,6 +230,8 @@ - (BOOL)startup:(MTRDeviceControllerStartupParamsInternal *)startupParams

commissionerParams.pairingDelegate = _pairingDelegateBridge;

_operationalCredentialsDelegate->SetDeviceCommissioner(_cppCommissioner);

commissionerParams.operationalCredentialsDelegate = _operationalCredentialsDelegate;

commissionerParams.controllerRCAC = _operationalCredentialsDelegate->RootCertSpan();
Expand Down Expand Up @@ -654,6 +657,17 @@ - (void)setPairingDelegate:(id<MTRDevicePairingDelegate>)delegate queue:(dispatc
});
}

- (void)setNocChainIssuer:(id<MTRNOCChainIssuer>)nocChainIssuer queue:(dispatch_queue_t)queue
{
VerifyOrReturn([self checkIsRunning]);

dispatch_sync(_chipWorkQueue, ^{
VerifyOrReturn([self checkIsRunning]);

self->_operationalCredentialsDelegate->SetNocChainIssuer(nocChainIssuer, queue);
});
}

- (BOOL)checkForInitError:(BOOL)condition logMsg:(NSString *)logMsg
{
if (condition) {
Expand Down
52 changes: 52 additions & 0 deletions src/darwin/Framework/CHIP/MTRNOCChainIssuer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
*
* Copyright (c) 2022 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 <Foundation/Foundation.h>

#import <Matter/MTRAttestationInfo.h>
#import <Matter/MTRCSRInfo.h>

NS_ASSUME_NONNULL_BEGIN

@protocol MTRNOCChainIssuer <NSObject>
@required

/**
* @brief When a MTRNOCChainIssuer is set for the MTRDeviceController, then onNOCChainGenerationNeeded will be
* called when the NOC CSR needs to be signed. This allows for custom credentials issuer
* implementations, for example, when a proprietary cloud API will perform the CSR signing.
* The commissioning workflow will stop upon the onNOCChainGenerationNeeded callback and
* resume once onNOCChainGenerationComplete is called
* The following fields MUST be passed to onNOCChainGenerationComplete with non-nil values:
* rootCertificate, intermediateCertificate, operationalCertificate.
* If ipk and adminSubject are passed, then they will be used in
* the AddNOC command sent to the commissionee. If they are not passed, then the values
* provided in the MTRDeviceController initialization will be used.
*
* All csr and attestation fields are provided to allow for custom attestestation checks.
*/
- (void)onNOCChainGenerationNeeded:(CSRInfo *)csrInfo
attestationInfo:(AttestationInfo *)attestationInfo
onNOCChainGenerationComplete:(void (^)(NSData * operationalCertificate, NSData * intermediateCertificate,
NSData * rootCertificate, NSData * ipk, NSNumber * adminSubject,
NSError * __autoreleasing * error))onNOCChainGenerationComplete;

@end

NS_ASSUME_NONNULL_END
Loading

0 comments on commit 2732633

Please sign in to comment.