From 80d134ed829af3c922e8973fa86a80fe3f10b9f1 Mon Sep 17 00:00:00 2001 From: Mikhail Burshteyn Date: Thu, 25 Aug 2022 11:52:05 -0600 Subject: [PATCH] Expose `Credentials::ConvertX509CertToChipCert` to Obj-C. (#22113) * Expose `Credentials::ConvertX509CertToChipCert` to Obj-C. * Restyled by clang-format Co-authored-by: Restyled.io --- src/darwin/Framework/CHIP/MTRCertificates.h | 3 +++ src/darwin/Framework/CHIP/MTRCertificates.mm | 18 ++++++++++++++++++ 2 files changed, 21 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..4caf69bf040ab8 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,21 @@ + (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