Skip to content

Commit

Permalink
Expose Credentials::ConvertX509CertToChipCert to Obj-C. (#22113)
Browse files Browse the repository at this point in the history
* Expose `Credentials::ConvertX509CertToChipCert` to Obj-C.

* Restyled by clang-format

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
mburshteyn1 and restyled-commits authored Aug 25, 2022
1 parent 85a2f68 commit 80d134e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/darwin/Framework/CHIP/MTRCertificates.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ NS_ASSUME_NONNULL_BEGIN
+ (nullable NSData *)generateCertificateSigningRequest:(id<MTRKeypair>)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
18 changes: 18 additions & 0 deletions src/darwin/Framework/CHIP/MTRCertificates.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#import "MTRCertificates.h"
#import "MTRError_Internal.h"
#import "MTRLogging.h"
#import "MTRMemory.h"
#import "MTROperationalCredentialsDelegate.h"
#import "MTRP256KeypairBridge.h"
Expand Down Expand Up @@ -196,4 +197,21 @@ + (nullable NSData *)generateCertificateSigningRequest:(id<MTRKeypair>)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

0 comments on commit 80d134e

Please sign in to comment.