Skip to content

Commit

Permalink
Address review comments on Darwin certificate conversion function. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bzbarsky-apple committed Sep 6, 2022
1 parent 001e7bf commit df11d01
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
11 changes: 9 additions & 2 deletions src/darwin/Framework/CHIP/MTRCertificates.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,15 @@ 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;
/**
* Convert the given X.509v3 DER encoded certificate to the Matter certificate
* format.
*
* Returns nil if the conversion fails (e.g. if the input data cannot be parsed
* as a DER encoded X.509 certificate, or if the certificate cannot be
* represented in the Matter certificate format).
*/
+ (nullable NSData *)convertX509Certificate:(NSData *)x509Certificate;

@end

Expand Down
10 changes: 5 additions & 5 deletions src/darwin/Framework/CHIP/MTRCertificates.mm
Original file line number Diff line number Diff line change
Expand Up @@ -197,21 +197,21 @@ + (nullable NSData *)generateCertificateSigningRequest:(id<MTRKeypair>)keypair
return nil;
}

+ (nullable NSData *)convertToCHIPCertFromX509Cert:(NSData *)x509Certificate
+ (nullable NSData *)convertX509Certificate:(NSData *)x509Certificate
{

chip::ByteSpan x509CertBytes = chip::ByteSpan((uint8_t *) x509Certificate.bytes, x509Certificate.length);
chip::ByteSpan x509CertBytes = AsByteSpan(x509Certificate);

NSMutableData * chipCertBuffer = [[NSMutableData alloc] initWithLength:chip::Credentials::kMaxCHIPCertLength];
chip::MutableByteSpan chipCertBytes((uint8_t *) chipCertBuffer.mutableBytes, chip::Credentials::kMaxCHIPCertLength);
uint8_t chipCertBuffer[chip::Credentials::kMaxCHIPCertLength];
chip::MutableByteSpan chipCertBytes(chipCertBuffer);

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()];
return AsData(chipCertBytes);
}

@end

0 comments on commit df11d01

Please sign in to comment.