Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use an NSData class extension.
Browse files Browse the repository at this point in the history
bzbarsky-apple committed May 12, 2022

Verified

This commit was signed with the committer’s verified signature.
bdice Bradley Dice
1 parent c10b4c4 commit ea8527a
Showing 7 changed files with 55 additions and 10 deletions.
8 changes: 8 additions & 0 deletions src/darwin/Framework/CHIP.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
@@ -44,6 +44,8 @@
517BF3F0282B62B800A8B7DB /* MatterCertificates.h in Headers */ = {isa = PBXBuildFile; fileRef = 517BF3EE282B62B800A8B7DB /* MatterCertificates.h */; settings = {ATTRIBUTES = (Public, ); }; };
517BF3F1282B62B800A8B7DB /* MatterCertificates.mm in Sources */ = {isa = PBXBuildFile; fileRef = 517BF3EF282B62B800A8B7DB /* MatterCertificates.mm */; };
517BF3F3282B62CB00A8B7DB /* MatterCertificateTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 517BF3F2282B62CB00A8B7DB /* MatterCertificateTests.m */; };
51874556282CAEF4008CD968 /* NSData+Span.mm in Sources */ = {isa = PBXBuildFile; fileRef = 51874554282CAEF4008CD968 /* NSData+Span.mm */; };
51874557282CAEF4008CD968 /* NSData+Span.h in Headers */ = {isa = PBXBuildFile; fileRef = 51874555282CAEF4008CD968 /* NSData+Span.h */; };
51B22C1E2740CB0A008D5055 /* CHIPStructsObjc.h in Headers */ = {isa = PBXBuildFile; fileRef = 51B22C1D2740CB0A008D5055 /* CHIPStructsObjc.h */; settings = {ATTRIBUTES = (Public, ); }; };
51B22C222740CB1D008D5055 /* CHIPCommandPayloadsObjc.h in Headers */ = {isa = PBXBuildFile; fileRef = 51B22C212740CB1D008D5055 /* CHIPCommandPayloadsObjc.h */; settings = {ATTRIBUTES = (Public, ); }; };
51B22C262740CB32008D5055 /* CHIPStructsObjc.mm in Sources */ = {isa = PBXBuildFile; fileRef = 51B22C252740CB32008D5055 /* CHIPStructsObjc.mm */; };
@@ -152,6 +154,8 @@
517BF3EE282B62B800A8B7DB /* MatterCertificates.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MatterCertificates.h; sourceTree = "<group>"; };
517BF3EF282B62B800A8B7DB /* MatterCertificates.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MatterCertificates.mm; sourceTree = "<group>"; };
517BF3F2282B62CB00A8B7DB /* MatterCertificateTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MatterCertificateTests.m; sourceTree = "<group>"; };
51874554282CAEF4008CD968 /* NSData+Span.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = "NSData+Span.mm"; sourceTree = "<group>"; };
51874555282CAEF4008CD968 /* NSData+Span.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSData+Span.h"; sourceTree = "<group>"; };
51B22C1D2740CB0A008D5055 /* CHIPStructsObjc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CHIPStructsObjc.h; path = "zap-generated/CHIPStructsObjc.h"; sourceTree = "<group>"; };
51B22C212740CB1D008D5055 /* CHIPCommandPayloadsObjc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CHIPCommandPayloadsObjc.h; path = "zap-generated/CHIPCommandPayloadsObjc.h"; sourceTree = "<group>"; };
51B22C252740CB32008D5055 /* CHIPStructsObjc.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = CHIPStructsObjc.mm; path = "zap-generated/CHIPStructsObjc.mm"; sourceTree = "<group>"; };
@@ -358,6 +362,8 @@
5A6FEC8F27B563D900F25F42 /* CHIPDeviceControllerOverXPC.m */,
5A6FEC9427B5976200F25F42 /* CHIPDeviceControllerXPCConnection.h */,
5A6FEC9527B5983000F25F42 /* CHIPDeviceControllerXPCConnection.m */,
51874555282CAEF4008CD968 /* NSData+Span.h */,
51874554282CAEF4008CD968 /* NSData+Span.mm */,
);
path = CHIP;
sourceTree = "<group>";
@@ -401,6 +407,7 @@
5136661628067D550025EDAE /* MatterControllerFactory.h in Headers */,
5A6FEC9927B5C88900F25F42 /* CHIPDeviceOverXPC.h in Headers */,
51B22C222740CB1D008D5055 /* CHIPCommandPayloadsObjc.h in Headers */,
51874557282CAEF4008CD968 /* NSData+Span.h in Headers */,
51B22C1E2740CB0A008D5055 /* CHIPStructsObjc.h in Headers */,
2CB7163B252E8A7B0026E2BB /* CHIPDevicePairingDelegateBridge.h in Headers */,
5ACDDD7A27CD129700EFD68A /* CHIPAttributeCacheContainer.h in Headers */,
@@ -575,6 +582,7 @@
51B22C2A2740CB47008D5055 /* CHIPCommandPayloadsObjc.mm in Sources */,
2C5EEEF7268A85C400CAE3D3 /* CHIPDeviceConnectionBridge.mm in Sources */,
51B22C262740CB32008D5055 /* CHIPStructsObjc.mm in Sources */,
51874556282CAEF4008CD968 /* NSData+Span.mm in Sources */,
2C222AD1255C620600E446B9 /* CHIPDevice.mm in Sources */,
1EC3238D271999E2002A8BF0 /* cluster-objects.cpp in Sources */,
991DC0892475F47D00C13860 /* CHIPDeviceController.mm in Sources */,
2 changes: 2 additions & 0 deletions src/darwin/Framework/CHIP/BUILD.gn
Original file line number Diff line number Diff line change
@@ -68,6 +68,8 @@ static_library("framework") {
"MatterControllerFactory.h",
"MatterControllerFactory.mm",
"MatterControllerFactory_Internal.h",
"NSData+Span.h",
"NSData+Span.mm",
"zap-generated/CHIPAttributeTLVValueDecoder.mm",
"zap-generated/CHIPCallbackBridge.mm",
"zap-generated/CHIPClustersObjc.h",
4 changes: 2 additions & 2 deletions src/darwin/Framework/CHIP/CHIPCluster.mm
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@

#import "CHIPCluster_internal.h"
#import "CHIPDevice.h"
#import "SpanUtils.h"
#import "NSData+Span.h"

using namespace ::chip;

@@ -51,7 +51,7 @@ - (instancetype)initWithDevice:(CHIPDevice *)device endpoint:(EndpointId)endpoin

- (chip::ByteSpan)asByteSpan:(NSData *)value
{
return AsByteSpan(value);
return [value asByteSpan];
}

- (chip::CharSpan)asCharSpan:(NSString *)value
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@

#import "CHIPLogging.h"
#import "MatterCertificates.h"
#import "SpanUtils.h"
#import "NSData+Span.h"

#include <credentials/CHIPCert.h>
#include <crypto/CHIPCryptoPAL.h>
@@ -222,7 +222,7 @@ uint64_t GetIssuerId(NSNumber * _Nullable providedIssuerId)
MutableByteSpan rcac(rcacBuffer);
X509CertRequestParams rcac_request = { 0, validityStart, validityEnd, rcac_dn, rcac_dn };
ReturnErrorOnFailure(NewRootX509Cert(rcac_request, nativeKeypair, rcac));
*rootCert = AsData(rcac);
*rootCert = [NSData fromByteSpan:rcac];
return CHIP_NO_ERROR;
}

@@ -241,7 +241,7 @@ uint64_t GetIssuerId(NSNumber * _Nullable providedIssuerId)
ReturnErrorOnFailure(keypairBridge.Init(rootKeypair));
CHIPP256KeypairNativeBridge nativeRootKeypair(keypairBridge);

ByteSpan rcac = AsByteSpan(rootCertificate);
ByteSpan rcac = [rootCertificate asByteSpan];

P256PublicKey pubKey;
ReturnErrorOnFailure(CHIPP256KeypairBridge::MatterPubKeyFromSecKeyRef(intermediatePublicKey, &pubKey));
@@ -271,6 +271,6 @@ uint64_t GetIssuerId(NSNumber * _Nullable providedIssuerId)
MutableByteSpan icac(icacBuffer);
X509CertRequestParams icac_request = { 0, validityStart, validityEnd, icac_dn, rcac_dn };
ReturnErrorOnFailure(NewICAX509Cert(icac_request, pubKey, nativeRootKeypair, icac));
*intermediateCert = AsData(icac);
*intermediateCert = [NSData fromByteSpan:icac];
return CHIP_NO_ERROR;
}
4 changes: 2 additions & 2 deletions src/darwin/Framework/CHIP/MatterCertificates.mm
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@
#import "CHIPError_Internal.h"
#import "CHIPOperationalCredentialsDelegate.h"
#import "CHIPP256KeypairBridge.h"
#import "SpanUtils.h"
#import "NSData+Span.h"

#include <credentials/CHIPCert.h>
#include <crypto/CHIPCryptoPAL.h>
@@ -97,7 +97,7 @@ + (BOOL)keypairMatchesCertificate:(NSData *)certificate keypair:(id<CHIPKeypair>
P256PublicKeySpan keypairKeySpan(keypairPubKey.ConstBytes());

P256PublicKey certPubKey;
err = ExtractPubkeyFromX509Cert(AsByteSpan(certificate), certPubKey);
err = ExtractPubkeyFromX509Cert([certificate asByteSpan], certPubKey);
if (err != CHIP_NO_ERROR) {
NSLog(@"Can't extract public key from certificate: %s", ErrorStr(err));
return NO;
Original file line number Diff line number Diff line change
@@ -26,8 +26,12 @@ NS_ASSUME_NONNULL_BEGIN
* Utilities for converting between NSData and chip::Span.
*/

inline chip::ByteSpan AsByteSpan(NSData * data) { return chip::ByteSpan(static_cast<const uint8_t *>(data.bytes), data.length); }
@interface NSData (Span)

inline NSData * AsData(chip::ByteSpan span) { return [NSData dataWithBytes:span.data() length:span.size()]; }
-(chip::ByteSpan)asByteSpan;

+(instancetype)fromByteSpan:(const chip::ByteSpan &)span;

@end

NS_ASSUME_NONNULL_END
31 changes: 31 additions & 0 deletions src/darwin/Framework/CHIP/NSData+Span.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* 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 "NSData+Span.h"

@implementation NSData (Span)

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

+ (instancetype)fromByteSpan:(const chip::ByteSpan &)span
{
return [NSData dataWithBytes:span.data() length:span.size()];
}

@end

0 comments on commit ea8527a

Please sign in to comment.