From 855fb96a0700ae8f6a504afb711508da9907a91e Mon Sep 17 00:00:00 2001 From: Mikhail Burshteyn Date: Tue, 23 Aug 2022 15:21:20 -0600 Subject: [PATCH 1/2] Expose `Credentials::ConvertX509CertToChipCert` to Obj-C. --- src/darwin/Framework/CHIP/MTRCertificates.h | 3 +++ src/darwin/Framework/CHIP/MTRCertificates.mm | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/darwin/Framework/CHIP/MTRCertificates.h b/src/darwin/Framework/CHIP/MTRCertificates.h index 301ab76f5b5ad3..f46999c661b5bc 100644 --- a/src/darwin/Framework/CHIP/MTRCertificates.h +++ b/src/darwin/Framework/CHIP/MTRCertificates.h @@ -125,6 +125,9 @@ NS_ASSUME_NONNULL_BEGIN + (nullable NSData *)generateCertificateSigningRequest:(id)keypair error:(NSError * __autoreleasing _Nullable * _Nullable)error; +/** Converts the given X.509v3 certificate to the CHIP certificate format. */ ++ (nullable NSData *)convertToCHIPCertFromX509Cert:(NSData *)x509Certificate; + @end NS_ASSUME_NONNULL_END diff --git a/src/darwin/Framework/CHIP/MTRCertificates.mm b/src/darwin/Framework/CHIP/MTRCertificates.mm index 80f3d9e10333bb..5254f3e3411eeb 100644 --- a/src/darwin/Framework/CHIP/MTRCertificates.mm +++ b/src/darwin/Framework/CHIP/MTRCertificates.mm @@ -16,6 +16,7 @@ #import "MTRCertificates.h" #import "MTRError_Internal.h" +#import "MTRLogging.h" #import "MTRMemory.h" #import "MTROperationalCredentialsDelegate.h" #import "MTRP256KeypairBridge.h" @@ -196,4 +197,19 @@ + (nullable NSData *)generateCertificateSigningRequest:(id)keypair return nil; } ++ (nullable NSData *)convertToCHIPCertFromX509Cert:(NSData *)x509Certificate { + + chip::ByteSpan x509CertBytes = chip::ByteSpan((uint8_t *) x509Certificate.bytes, x509Certificate.length); + + NSMutableData * chipCertBuffer = [[NSMutableData alloc] initWithLength:chip::Credentials::kMaxCHIPCertLength]; + chip::MutableByteSpan chipCertBytes((uint8_t *) chipCertBuffer.mutableBytes, chip::Credentials::kMaxCHIPCertLength); + + CHIP_ERROR errorCode = chip::Credentials::ConvertX509CertToChipCert(x509CertBytes, chipCertBytes); + MTR_LOG_ERROR("ConvertX509CertToChipCert: %{public}s", chip::ErrorStr(errorCode)); + + if (errorCode != CHIP_NO_ERROR) return nil; + + return [NSData dataWithBytes:chipCertBytes.data() length:chipCertBytes.size()]; +} + @end From cc99e18e2a666d5bf1c06ab45001f79d3507b79a Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Wed, 24 Aug 2022 02:42:19 +0000 Subject: [PATCH 2/2] Restyled by clang-format --- src/darwin/Framework/CHIP/MTRCertificates.mm | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/darwin/Framework/CHIP/MTRCertificates.mm b/src/darwin/Framework/CHIP/MTRCertificates.mm index 5254f3e3411eeb..4caf69bf040ab8 100644 --- a/src/darwin/Framework/CHIP/MTRCertificates.mm +++ b/src/darwin/Framework/CHIP/MTRCertificates.mm @@ -197,19 +197,21 @@ + (nullable NSData *)generateCertificateSigningRequest:(id)keypair return nil; } -+ (nullable NSData *)convertToCHIPCertFromX509Cert:(NSData *)x509Certificate { ++ (nullable NSData *)convertToCHIPCertFromX509Cert:(NSData *)x509Certificate +{ - chip::ByteSpan x509CertBytes = chip::ByteSpan((uint8_t *) x509Certificate.bytes, x509Certificate.length); + chip::ByteSpan x509CertBytes = chip::ByteSpan((uint8_t *) x509Certificate.bytes, x509Certificate.length); - NSMutableData * chipCertBuffer = [[NSMutableData alloc] initWithLength:chip::Credentials::kMaxCHIPCertLength]; - chip::MutableByteSpan chipCertBytes((uint8_t *) chipCertBuffer.mutableBytes, chip::Credentials::kMaxCHIPCertLength); + NSMutableData * chipCertBuffer = [[NSMutableData alloc] initWithLength:chip::Credentials::kMaxCHIPCertLength]; + chip::MutableByteSpan chipCertBytes((uint8_t *) chipCertBuffer.mutableBytes, chip::Credentials::kMaxCHIPCertLength); - CHIP_ERROR errorCode = chip::Credentials::ConvertX509CertToChipCert(x509CertBytes, chipCertBytes); - MTR_LOG_ERROR("ConvertX509CertToChipCert: %{public}s", chip::ErrorStr(errorCode)); + CHIP_ERROR errorCode = chip::Credentials::ConvertX509CertToChipCert(x509CertBytes, chipCertBytes); + MTR_LOG_ERROR("ConvertX509CertToChipCert: %{public}s", chip::ErrorStr(errorCode)); - if (errorCode != CHIP_NO_ERROR) return nil; + if (errorCode != CHIP_NO_ERROR) + return nil; - return [NSData dataWithBytes:chipCertBytes.data() length:chipCertBytes.size()]; + return [NSData dataWithBytes:chipCertBytes.data() length:chipCertBytes.size()]; } @end