Skip to content

Commit

Permalink
Expose Credentials::ConvertX509CertToChipCert to Obj-C.
Browse files Browse the repository at this point in the history
  • Loading branch information
mburshteyn1 committed Aug 23, 2022
1 parent f740b75 commit 38aca9f
Show file tree
Hide file tree
Showing 2 changed files with 18 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
15 changes: 15 additions & 0 deletions src/darwin/Framework/CHIP/MTRCertificates.mm
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,19 @@ + (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 38aca9f

Please sign in to comment.