Skip to content

Commit db02354

Browse files
Darwin: mark copyPublicKey as CF_RETURNS_RETAINED (project-chip#37022)
for correct memory management when called from Swift. added a test that shows how the annotation affects typical callees. Co-authored-by: Andrei Litvin <andy314@gmail.com>
1 parent 44bb573 commit db02354

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

src/darwin/Framework/CHIP/MTRKeypair.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ MTR_AVAILABLE(ios(16.1), macos(13.0), watchos(9.1), tvos(16.1))
3838
/**
3939
* @brief Returns a copy of the public key for the keypair.
4040
*/
41-
- (SecKeyRef)copyPublicKey MTR_AVAILABLE(ios(18.4), macos(15.4), watchos(11.4), tvos(18.4));
41+
- (SecKeyRef)copyPublicKey CF_RETURNS_RETAINED MTR_AVAILABLE(ios(18.4), macos(15.4), watchos(11.4), tvos(18.4));
4242

4343
/**
4444
* @brief Returns public key for the keypair without adding a reference. DEPRECATED - please use copyPublicKey, otherwise this will leak.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Copyright (c) 2024 Project CHIP Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import Foundation
18+
19+
class MTRSwiftCertificateTests : XCTestCase {
20+
func testGenerateIntermediateCert() {
21+
let rootKeys = MTRTestKeys()
22+
do {
23+
let rootCert = try MTRCertificates.createRootCertificate(rootKeys, issuerID: nil, fabricID: nil)
24+
XCTAssertNotNil(rootCert)
25+
26+
let intermediateKeys = MTRTestKeys()
27+
XCTAssertNotNil(intermediateKeys)
28+
29+
// NB: `copyPublicKey` returns Unmanaged<SecKey> if `copyPublicKey` lacks CF_RETURNS_RETAINED annotation.
30+
let intermediatePublicKey = intermediateKeys.copyPublicKey()
31+
XCTAssertNotNil(intermediatePublicKey)
32+
33+
let intermediateCert = try MTRCertificates.createIntermediateCertificate(rootKeys, rootCertificate: rootCert, intermediatePublicKey: intermediatePublicKey, issuerID: nil as NSNumber?, fabricID: nil)
34+
XCTAssertNotNil(intermediateCert)
35+
36+
guard let tlvCert = MTRCertificates.convertX509Certificate(intermediateCert) else {
37+
XCTFail()
38+
return
39+
}
40+
XCTAssertNotNil(tlvCert)
41+
42+
guard let derCert = MTRCertificates.convertMatterCertificate(tlvCert) else {
43+
XCTFail()
44+
return
45+
}
46+
XCTAssertNotNil(derCert)
47+
48+
XCTAssertEqual(intermediateCert, derCert)
49+
50+
} catch {
51+
XCTFail()
52+
}
53+
}
54+
}

src/darwin/Framework/Matter.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@
348348
9B5CCB5D2C6EC890009DD99B /* MTRDevice_XPC.h in Headers */ = {isa = PBXBuildFile; fileRef = 9B5CCB5A2C6EC890009DD99B /* MTRDevice_XPC.h */; };
349349
9B5CCB602C6EE29E009DD99B /* MTRDeviceControllerXPCParameters.mm in Sources */ = {isa = PBXBuildFile; fileRef = 9B5CCB5F2C6EE29E009DD99B /* MTRDeviceControllerXPCParameters.mm */; };
350350
9B5CCB612C6EE29E009DD99B /* MTRDeviceControllerXPCParameters.h in Headers */ = {isa = PBXBuildFile; fileRef = 9B5CCB5E2C6EE29E009DD99B /* MTRDeviceControllerXPCParameters.h */; };
351+
9BC9E5872D3099FF00784A21 /* MTRSwiftCertificateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9BC9E5862D3099FF00784A21 /* MTRSwiftCertificateTests.swift */; };
351352
9BDA2A062C5D9AF800A32BDD /* MTRDevice_Concrete.mm in Sources */ = {isa = PBXBuildFile; fileRef = 9BDA2A052C5D9AF800A32BDD /* MTRDevice_Concrete.mm */; };
352353
9BDA2A082C5D9AFB00A32BDD /* MTRDevice_Concrete.h in Headers */ = {isa = PBXBuildFile; fileRef = 9BDA2A072C5D9AFB00A32BDD /* MTRDevice_Concrete.h */; };
353354
9BFE5D502C6D3075007D4319 /* MTRDeviceController_XPC.h in Headers */ = {isa = PBXBuildFile; fileRef = 9BFE5D4E2C6D3075007D4319 /* MTRDeviceController_XPC.h */; };
@@ -835,6 +836,7 @@
835836
9B5CCB5B2C6EC890009DD99B /* MTRDevice_XPC.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRDevice_XPC.mm; sourceTree = "<group>"; };
836837
9B5CCB5E2C6EE29E009DD99B /* MTRDeviceControllerXPCParameters.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MTRDeviceControllerXPCParameters.h; sourceTree = "<group>"; };
837838
9B5CCB5F2C6EE29E009DD99B /* MTRDeviceControllerXPCParameters.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRDeviceControllerXPCParameters.mm; sourceTree = "<group>"; };
839+
9BC9E5862D3099FF00784A21 /* MTRSwiftCertificateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MTRSwiftCertificateTests.swift; sourceTree = "<group>"; };
838840
9BDA2A052C5D9AF800A32BDD /* MTRDevice_Concrete.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRDevice_Concrete.mm; sourceTree = "<group>"; };
839841
9BDA2A072C5D9AFB00A32BDD /* MTRDevice_Concrete.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MTRDevice_Concrete.h; sourceTree = "<group>"; };
840842
9BFE5D4E2C6D3075007D4319 /* MTRDeviceController_XPC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MTRDeviceController_XPC.h; sourceTree = "<group>"; };
@@ -1634,6 +1636,7 @@
16341636
3D4733AE2BDF1B80003DC19B /* MTRSetupPayloadTests.m */,
16351637
519498312A25581C00B3BABE /* MTRSetupPayloadInitializationTests.m */,
16361638
51E0FC0F2ACBBF230001E197 /* MTRSwiftDeviceTests.swift */,
1639+
9BC9E5862D3099FF00784A21 /* MTRSwiftCertificateTests.swift */,
16371640
5143851D2A65885500EDC8E6 /* MTRSwiftPairingTests.swift */,
16381641
997DED1926955D0200975E97 /* MTRThreadOperationalDatasetTests.mm */,
16391642
5A7947DD27BEC3F500434CF2 /* MTRXPCListenerSampleTests.m */,
@@ -2344,6 +2347,7 @@
23442347
isa = PBXSourcesBuildPhase;
23452348
buildActionMask = 2147483647;
23462349
files = (
2350+
9BC9E5872D3099FF00784A21 /* MTRSwiftCertificateTests.swift in Sources */,
23472351
51742B4E29CB6B88009974FE /* MTRPairingTests.m in Sources */,
23482352
5131BF662BE2E1B000D5D6BC /* MTRTestCase.mm in Sources */,
23492353
51669AF02913204400F4AA36 /* MTRBackwardsCompatTests.m in Sources */,

0 commit comments

Comments
 (0)