Skip to content

Commit

Permalink
[Matter.framework] Update some test/helper code to use bridge_transfe… (
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple authored and yyzhong-g committed Dec 11, 2024
1 parent 041a6e8 commit e900b11
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 27 deletions.
45 changes: 28 additions & 17 deletions src/darwin/CHIPTool/CHIPTool/Framework Helpers/FabricKeys.m
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,14 @@ + (SecKeyRef)loadCAPrivateKey
return NULL;
}

CFErrorRef error = NULL;
CFErrorRef cfError = NULL;
SecKeyRef key = SecKeyCreateWithData(
(__bridge CFDataRef) keyData, (__bridge CFDictionaryRef)[FabricKeys privateKeyCreationParams], &error);
if (error) {
NSLog(@"Could not reconstruct private key %@", (__bridge NSError *) error);
(__bridge CFDataRef) keyData,
(__bridge CFDictionaryRef)[FabricKeys privateKeyCreationParams],
&cfError);

if (!key) {
NSLog(@"Could not reconstruct private key %@", (__bridge_transfer NSError *) cfError);
return NULL;
}

Expand All @@ -159,16 +162,19 @@ + (SecKeyRef)generateCAPrivateKey
// item at all.
SecItemDelete((__bridge CFDictionaryRef) query);

CFErrorRef error = NULL;
SecKeyRef key = SecKeyCreateRandomKey((__bridge CFDictionaryRef)[FabricKeys privateKeyCreationParams], &error);
if (error) {
NSLog(@"Could not generate private key: %@", (__bridge NSError *) error);
CFErrorRef cfError = NULL;
SecKeyRef key = SecKeyCreateRandomKey(
(__bridge CFDictionaryRef)[FabricKeys privateKeyCreationParams],
&cfError);

if (!key) {
NSLog(@"Could not generate private key: %@", (__bridge_transfer NSError *) cfError);
return NULL;
}

NSData * keyData = (__bridge_transfer NSData *) SecKeyCopyExternalRepresentation(key, &error);
if (error) {
NSLog(@"Could not get key external representation: %@", (__bridge NSError *) error);
NSData * keyData = (__bridge_transfer NSData *) SecKeyCopyExternalRepresentation(key, &cfError);
if (!keyData) {
NSLog(@"Could not get key external representation: %@", (__bridge_transfer NSError *) cfError);
CFRelease(key);
return NULL;
}
Expand Down Expand Up @@ -209,13 +215,18 @@ - (instancetype)init

- (NSData *)signMessageECDSA_DER:(NSData *)message
{
CFErrorRef error = NULL;
CFDataRef outData
= SecKeyCreateSignature(_privateKey, kSecKeyAlgorithmECDSASignatureMessageX962SHA256, (__bridge CFDataRef) message, &error);

if (error != noErr) {
NSLog(@"Failed to sign cert: %@", (__bridge NSError *) error);
CFErrorRef cfError = NULL;
CFDataRef cfData = SecKeyCreateSignature(
_privateKey,
kSecKeyAlgorithmECDSASignatureMessageX962SHA256,
(__bridge CFDataRef) message,
&cfError);

if (!cfData) {
NSLog(@"Failed to sign cert: %@", (__bridge_transfer NSError *) cfError);
return nil;
}

return (__bridge_transfer NSData *) outData;
}

Expand Down
29 changes: 19 additions & 10 deletions src/darwin/Framework/CHIPTests/TestHelpers/MTRTestKeys.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,23 @@ - (instancetype)init

// Generate a keypair. For now harcoded to 256 bits until the framework exposes this constant.
const size_t keySizeInBits = 256;
CFErrorRef error = NULL;
const NSDictionary * keygenParams = @{
(__bridge NSString *) kSecAttrKeyClass : (__bridge NSString *) kSecAttrKeyClassPrivate,
(__bridge NSString *) kSecAttrKeyType : (__bridge NSString *) kSecAttrKeyTypeECSECPrimeRandom,
(__bridge NSString *) kSecAttrKeySizeInBits : @(keySizeInBits),
(__bridge NSString *) kSecAttrIsPermanent : @(NO)
};

_privateKey = SecKeyCreateRandomKey((__bridge CFDictionaryRef) keygenParams, &error);
if (error) {
NSLog(@"Failed to generate private key");
CFErrorRef cfError = NULL;
_privateKey = SecKeyCreateRandomKey(
(__bridge CFDictionaryRef) keygenParams,
&cfError);

if (!_privateKey) {
NSLog(@"Failed to generate private key: %@", (__bridge_transfer NSError *) cfError);
return nil;
}

_publicKey = SecKeyCopyPublicKey(_privateKey);

_signatureCount = 0;
Expand All @@ -84,14 +88,19 @@ - (NSData *)signMessageECDSA_DER:(NSData *)message
{
++_signatureCount;

CFErrorRef error = NULL;
CFDataRef outData
= SecKeyCreateSignature(_privateKey, kSecKeyAlgorithmECDSASignatureMessageX962SHA256, (__bridge CFDataRef) message, &error);
CFErrorRef cfError = NULL;
CFDataRef cfData = SecKeyCreateSignature(
_privateKey,
kSecKeyAlgorithmECDSASignatureMessageX962SHA256,
(__bridge CFDataRef) message,
&cfError);

if (error != noErr) {
NSLog(@"Failed to sign cert: %@", (__bridge NSError *) error);
if (!cfData) {
NSLog(@"Failed to sign cert: %@", (__bridge_transfer NSError *) cfError);
return nil;
}
return (__bridge_transfer NSData *) outData;

return (__bridge_transfer NSData *) cfData;
}

- (void)dealloc
Expand Down

0 comments on commit e900b11

Please sign in to comment.