Adding accessory for publicKeyData (#36129)
* Adding accessory for publicKeyData
* Update src/darwin/Framework/CHIP/MTRCertificateInfo.mm
* Restyled by whitespace
* Restyled by clang-format
* braces for multi-line conditional statement
---------
Co-authored-by: Restyled.io <commits@restyled.io>
Co-authored-by: Kiel Oleson <kielo@apple.com>
diff --git a/src/darwin/Framework/CHIP/MTRCertificateInfo.h b/src/darwin/Framework/CHIP/MTRCertificateInfo.h
index 20e9074..eba7aec 100644
--- a/src/darwin/Framework/CHIP/MTRCertificateInfo.h
+++ b/src/darwin/Framework/CHIP/MTRCertificateInfo.h
@@ -55,15 +55,20 @@
* Matter root certificates are self-signed, i.e. the issuer and the subject are
* the same.
*/
-@property (readonly) MTRDistinguishedNameInfo * issuer;
+@property (readonly, retain) MTRDistinguishedNameInfo * issuer;
/**
* The Distinguished Name of the entity represented by the certificate.
*/
-@property (readonly) MTRDistinguishedNameInfo * subject;
+@property (readonly, retain) MTRDistinguishedNameInfo * subject;
-@property (readonly) NSDate * notBefore;
-@property (readonly) NSDate * notAfter;
+@property (readonly, retain) NSDate * notBefore;
+@property (readonly, retain) NSDate * notAfter;
+
+/**
+ * Public key data for this certificate
+ */
+@property (nullable, readonly, retain) NSData * publicKeyData;
@end
diff --git a/src/darwin/Framework/CHIP/MTRCertificateInfo.mm b/src/darwin/Framework/CHIP/MTRCertificateInfo.mm
index f3aacb9..43c4117 100644
--- a/src/darwin/Framework/CHIP/MTRCertificateInfo.mm
+++ b/src/darwin/Framework/CHIP/MTRCertificateInfo.mm
@@ -21,9 +21,15 @@
#include <credentials/CHIPCert.h>
+#import "NSDataSpanConversion.h"
+
+#include <credentials/CHIPCert.h>
+#include <crypto/CHIPCryptoPAL.h>
+
NS_ASSUME_NONNULL_BEGIN
using namespace chip;
+using namespace chip::Crypto;
using namespace chip::Credentials;
using namespace chip::ASN1;
@@ -65,6 +71,18 @@
return MatterEpochSecondsAsDate(_data.mNotBeforeTime);
}
+- (nullable NSData *)publicKeyData
+{
+ P256PublicKeySpan publicKeySpan;
+ CHIP_ERROR err = ExtractPublicKeyFromChipCert(AsByteSpan(_bytes), publicKeySpan);
+
+ if (err != CHIP_NO_ERROR) {
+ return nil;
+ }
+
+ return AsData(publicKeySpan);
+}
+
- (NSDate *)notAfter
{
// "no expiry" is encoded as kNullCertTime (see ChipEpochToASN1Time)
diff --git a/src/darwin/Framework/CHIPTests/MTRCertificateInfoTests.m b/src/darwin/Framework/CHIPTests/MTRCertificateInfoTests.m
index bdf52e3..8a532a8 100644
--- a/src/darwin/Framework/CHIPTests/MTRCertificateInfoTests.m
+++ b/src/darwin/Framework/CHIPTests/MTRCertificateInfoTests.m
@@ -117,6 +117,7 @@
{
MTRCertificateInfo * info = [[MTRCertificateInfo alloc] initWithTLVBytes:self.exampleNOCertTLV];
XCTAssertNotNil(info);
+ XCTAssertNotNil(info.publicKeyData);
XCTAssertEqual([NSDate.now compare:info.notBefore], NSOrderedDescending);
XCTAssertEqual([NSDate.now compare:info.notAfter], NSOrderedAscending);