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