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

Add a helper function for getting P256PublicKey from MTRKeypair. #36520

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 1 addition & 18 deletions src/darwin/Framework/CHIP/MTRCertificates.mm
Original file line number Diff line number Diff line change
Expand Up @@ -152,24 +152,7 @@ + (MTRCertificateDERBytes _Nullable)createOperationalCertificate:(id<MTRKeypair>
+ (BOOL)keypair:(id<MTRKeypair>)keypair matchesCertificate:(NSData *)certificate
{
P256PublicKey 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;
}

CHIP_ERROR err = MTRP256KeypairBridge::MatterPubKeyFromMTRKeypair(keypair, &keypairPubKey);
if (err != CHIP_NO_ERROR) {
MTR_LOG_ERROR("Can't extract public key from keypair: %s", ErrorStr(err));
return NO;
Expand Down
19 changes: 1 addition & 18 deletions src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm
Original file line number Diff line number Diff line change
Expand Up @@ -819,24 +819,7 @@ - (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.
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;
}

CHIP_ERROR err = MTRP256KeypairBridge::MatterPubKeyFromMTRKeypair(params.nocSigner, &pubKey);
if (err != CHIP_NO_ERROR) {
MTR_LOG_ERROR("Can't extract public key from MTRKeypair: %s", ErrorStr(err));
return NO;
Expand Down
7 changes: 4 additions & 3 deletions src/darwin/Framework/CHIP/MTRP256KeypairBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ class MTRP256KeypairBridge : public chip::Crypto::P256Keypair

const chip::Crypto::P256PublicKey & Pubkey() const override { return mPubkey; };

// On success, writes to *pubKey.
// On success, writes to *matterPubKey.
static CHIP_ERROR MatterPubKeyFromSecKeyRef(SecKeyRef pubkeyRef, chip::Crypto::P256PublicKey * matterPubKey);

// On success, writes to *matterPubKey.
static CHIP_ERROR MatterPubKeyFromMTRKeypair(id<MTRKeypair> keyPair, chip::Crypto::P256PublicKey * matterPubKey);

private:
id<MTRKeypair> _Nullable mKeypair;
chip::Crypto::P256PublicKey mPubkey;

CHIP_ERROR setPubkey();
};

NS_ASSUME_NONNULL_END
35 changes: 22 additions & 13 deletions src/darwin/Framework/CHIP/MTRP256KeypairBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
}

mKeypair = keypair;
return setPubkey();
return MatterPubKeyFromMTRKeypair(mKeypair, &mPubkey);
}

CHIP_ERROR MTRP256KeypairBridge::Initialize(ECPKeyTarget key_target)
Expand Down Expand Up @@ -132,18 +132,6 @@
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
}

CHIP_ERROR MTRP256KeypairBridge::setPubkey()
{
if ([mKeypair respondsToSelector:@selector(copyPublicKey)]) {
SecKeyRef publicKey = [mKeypair copyPublicKey];
auto copyResult = MatterPubKeyFromSecKeyRef(publicKey, &mPubkey);
CFRelease(publicKey);
return copyResult;
} else {
return MatterPubKeyFromSecKeyRef([mKeypair publicKey], &mPubkey);
}
}

CHIP_ERROR MTRP256KeypairBridge::MatterPubKeyFromSecKeyRef(SecKeyRef pubkeyRef, P256PublicKey * matterPubKey)
{
if (!pubkeyRef) {
Expand All @@ -165,3 +153,24 @@

return CHIP_NO_ERROR;
}

CHIP_ERROR MTRP256KeypairBridge::MatterPubKeyFromMTRKeypair(id<MTRKeypair> keyPair, chip::Crypto::P256PublicKey * matterPubKey)
{
SecKeyRef publicKey;
if ([keyPair respondsToSelector:@selector(copyPublicKey)]) {
publicKey = [keyPair copyPublicKey];
} else {
publicKey = [keyPair publicKey];
if (publicKey) {
CFRetain(publicKey);
}
}

CHIP_ERROR err = MatterPubKeyFromSecKeyRef(publicKey, matterPubKey);

if (publicKey) {
CFRelease(publicKey);
}

return err;
}
Loading