Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Darwin] add API to get public key without leaks (with fixes) #36340

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
57423cd
Initial checkin
woody-apple Oct 30, 2024
cb67d1e
Fixing review feedback
woody-apple Oct 30, 2024
cbefb5a
Adding braces
woody-apple Oct 30, 2024
456ac90
Restyled by clang-format
restyled-commits Oct 30, 2024
bc33c7b
Fixing build
woody-apple Oct 30, 2024
84ffc4e
Restyled by clang-format
restyled-commits Oct 30, 2024
6a50d9d
Fixing annotations
woody-apple Oct 30, 2024
a23b86e
Update src/darwin/Framework/CHIP/MTRKeypair.h
woody-apple Oct 30, 2024
f66949f
separate statements involving _mPublicKey to make compiler happy
kiel-apple Oct 31, 2024
de11a1e
use `CFAutorelease` on CoreFoundation typed public key copies
kiel-apple Oct 31, 2024
79aaaca
fix indent
kiel-apple Oct 31, 2024
2bf82f0
implement `copyPublicKey` for `MTRTestKeys`; add TODO note about opti…
kiel-apple Oct 31, 2024
c134be6
remove comment
kiel-apple Nov 1, 2024
8a17443
consistent formatting for `copyPublicKey` calls
kiel-apple Nov 1, 2024
8691282
reformat `copyPublicKey` in `MTRTestKeys`
kiel-apple Nov 1, 2024
47e264a
Update src/darwin/Framework/CHIP/MTRKeypair.h
woody-apple Nov 1, 2024
8dbbcaa
re-add `publicKey` in `CHIPToolKeypair`
kiel-apple Nov 2, 2024
d328eaf
use `copyPublicKey` in more places
kiel-apple Nov 3, 2024
82379eb
dedupe public key cache population for CHIPToolKeypair
kiel-apple Nov 4, 2024
fbbd06a
Restyled by clang-format
restyled-commits Nov 4, 2024
fdbd53c
try manually releasing pubkey in test
kiel-apple Nov 5, 2024
33538e4
leakfix: MTRP256KeypairBridge
kiel-apple Nov 5, 2024
1cdc2f4
leakfix: `MTRCertificateTests.testGenerateCSR`
kiel-apple Nov 5, 2024
a761b1c
remove unnecessary plain `publicKey` implementation
kiel-apple Nov 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
@interface CHIPToolKeypair : NSObject <MTRKeypair>
- (BOOL)initialize;
- (NSData *)signMessageECDSA_RAW:(NSData *)message;
- (SecKeyRef)publicKey;
- (SecKeyRef)publicKey MTR_DEPRECATED("Please implement copyPublicKey, this will leak otherwise", ios(16.1, 18.3), macos(13.0, 15.3), watchos(9.1, 11.3), tvos(16.1, 18.3));
- (SecKeyRef)copyPublicKey;
- (CHIP_ERROR)Serialize:(chip::Crypto::P256SerializedKeypair &)output;
- (CHIP_ERROR)Deserialize:(chip::Crypto::P256SerializedKeypair &)input;
- (CHIP_ERROR)createOrLoadKeys:(id)storage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ - (NSData *)signMessageECDSA_RAW:(NSData *)message
return out_signature;
}

- (SecKeyRef)publicKey
- (void)_populatePublicKeyIfNecessary
{
if (_mPublicKey == nil) {
chip::Crypto::P256PublicKey publicKey = _mKeyPair.Pubkey();
Expand All @@ -79,9 +79,27 @@ - (SecKeyRef)publicKey
};
_mPublicKey = SecKeyCreateWithData((__bridge CFDataRef) publicKeyNSData, (__bridge CFDictionaryRef) attributes, nullptr);
}
}

- (SecKeyRef)publicKey
woody-apple marked this conversation as resolved.
Show resolved Hide resolved
{
[self _populatePublicKeyIfNecessary];

return _mPublicKey;
}

- (SecKeyRef)copyPublicKey
{
[self _populatePublicKeyIfNecessary];

if (_mPublicKey) {
CFRetain(_mPublicKey);
return _mPublicKey;
}

return NULL;
}

- (CHIP_ERROR)Deserialize:(chip::Crypto::P256SerializedKeypair &)input
{
return _mKeyPair.Deserialize(input);
Expand Down
19 changes: 18 additions & 1 deletion src/darwin/Framework/CHIP/MTRCertificates.mm
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,24 @@ + (MTRCertificateDERBytes _Nullable)createOperationalCertificate:(id<MTRKeypair>
+ (BOOL)keypair:(id<MTRKeypair>)keypair matchesCertificate:(NSData *)certificate
{
P256PublicKey keypairPubKey;
CHIP_ERROR err = MTRP256KeypairBridge::MatterPubKeyFromSecKeyRef(keypair.publicKey, &keypairPubKey);
SecKeyRef publicKey = NULL;

if ([keypair respondsToSelector:@selector(copyPublicKey)]) {
publicKey = [keypair copyPublicKey];
} else {
publicKey = [keypair publicKey];
if (publicKey) {
CFRetain(publicKey);
}
}

CHIP_ERROR err = MTRP256KeypairBridge::MatterPubKeyFromSecKeyRef(publicKey, &keypairPubKey);

if (publicKey != NULL) {
CFRelease(publicKey);
publicKey = NULL;
}

if (err != CHIP_NO_ERROR) {
MTR_LOG_ERROR("Can't extract public key from keypair: %s", ErrorStr(err));
return NO;
Expand Down
19 changes: 18 additions & 1 deletion src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,24 @@ - (BOOL)findMatchingFabric:(FabricTable &)fabricTable
} else {
// No root certificate means the nocSigner is using the root keys, because
// consumers must provide a root certificate whenever an ICA is used.
CHIP_ERROR err = MTRP256KeypairBridge::MatterPubKeyFromSecKeyRef(params.nocSigner.publicKey, &pubKey);
SecKeyRef publicKey = NULL;

if ([params.nocSigner respondsToSelector:@selector(copyPublicKey)]) {
publicKey = [params.nocSigner copyPublicKey];
} else {
publicKey = [params.nocSigner publicKey];
if (publicKey) {
CFRetain(publicKey);
}
}

CHIP_ERROR err = MTRP256KeypairBridge::MatterPubKeyFromSecKeyRef(publicKey, &pubKey);

if (publicKey != NULL) {
CFRelease(publicKey);
publicKey = NULL;
}

if (err != CHIP_NO_ERROR) {
MTR_LOG_ERROR("Can't extract public key from MTRKeypair: %s", ErrorStr(err));
return NO;
Expand Down
15 changes: 11 additions & 4 deletions src/darwin/Framework/CHIP/MTRKeypair.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

#import <Foundation/Foundation.h>
#import <Matter/Matter.h>
#import <Security/Security.h>

NS_ASSUME_NONNULL_BEGIN
Expand All @@ -31,13 +32,19 @@ NS_ASSUME_NONNULL_BEGIN
* framework APIs.
*/
@protocol MTRKeypair <NSObject>
@required

@optional
/**
* @brief Return public key for the keypair.
* @brief Returns a copy of the public key for the keypair.
*/
- (SecKeyRef)publicKey;
- (SecKeyRef)copyPublicKey MTR_NEWLY_AVAILABLE;

/**
* @brief Returns public key for the keypair without adding a reference. DEPRECATED - please use copyPublicKey, otherwise this will leak.
*/

- (SecKeyRef)publicKey MTR_DEPRECATED("Please implement copyPublicKey, this will leak otherwise", ios(16.1, 18.3), macos(13.0, 15.3), watchos(9.1, 11.3), tvos(16.1, 18.3));

@optional
/**
* @brief A function to sign a message using ECDSA
*
Expand Down
9 changes: 8 additions & 1 deletion src/darwin/Framework/CHIP/MTRP256KeypairBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,14 @@
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
}

CHIP_ERROR MTRP256KeypairBridge::setPubkey() { return MatterPubKeyFromSecKeyRef([mKeypair publicKey], &mPubkey); }
CHIP_ERROR MTRP256KeypairBridge::setPubkey()
{
if ([mKeypair respondsToSelector:@selector(copyPublicKey)]) {
return MatterPubKeyFromSecKeyRef([mKeypair copyPublicKey], &mPubkey);
woody-apple marked this conversation as resolved.
Show resolved Hide resolved
} else {
return MatterPubKeyFromSecKeyRef([mKeypair publicKey], &mPubkey);
}
}

CHIP_ERROR MTRP256KeypairBridge::MatterPubKeyFromSecKeyRef(SecKeyRef pubkeyRef, P256PublicKey * matterPubKey)
{
Expand Down
Loading
Loading