@@ -17,6 +17,7 @@ @interface CHIPToolKeypair ()
17
17
@property (nonatomic ) chip::Crypto::P256Keypair mIssuer ;
18
18
@property (nonatomic ) NSData * ipk;
19
19
@property (atomic ) uint32_t mNow ;
20
+ @property (nonatomic , readonly ) SecKeyRef mPublicKey ;
20
21
@end
21
22
22
23
@implementation CHIPToolKeypair
@@ -46,16 +47,19 @@ - (NSData *)signMessageECDSA_RAW:(NSData *)message
46
47
47
48
- (SecKeyRef)publicKey
48
49
{
49
- chip::Crypto::P256PublicKey publicKey = _mKeyPair.Pubkey ();
50
- NSData * publicKeyNSData = [NSData dataWithBytes: publicKey.Bytes () length: publicKey.Length ()];
51
- NSDictionary * attributes = @{
52
- (__bridge NSString *) kSecAttrKeyClass : (__bridge NSString *) kSecAttrKeyClassPublic ,
53
- (NSString *) kSecAttrKeyType : (NSString *) kSecAttrKeyTypeECSECPrimeRandom ,
54
- (NSString *) kSecAttrKeySizeInBits : @Public_KeySize,
55
- (NSString *) kSecAttrLabel : kCHIPToolKeychainLabel ,
56
- (NSString *) kSecAttrApplicationTag : @CHIPPlugin_CAKeyTag,
57
- };
58
- return SecKeyCreateWithData ((__bridge CFDataRef ) publicKeyNSData, (__bridge CFDictionaryRef ) attributes, nullptr );
50
+ if (_mPublicKey == nil ) {
51
+ chip::Crypto::P256PublicKey publicKey = _mKeyPair.Pubkey ();
52
+ NSData * publicKeyNSData = [NSData dataWithBytes: publicKey.Bytes () length: publicKey.Length ()];
53
+ NSDictionary * attributes = @{
54
+ (__bridge NSString *) kSecAttrKeyClass : (__bridge NSString *) kSecAttrKeyClassPublic ,
55
+ (NSString *) kSecAttrKeyType : (NSString *) kSecAttrKeyTypeECSECPrimeRandom ,
56
+ (NSString *) kSecAttrKeySizeInBits : @Public_KeySize,
57
+ (NSString *) kSecAttrLabel : kCHIPToolKeychainLabel ,
58
+ (NSString *) kSecAttrApplicationTag : @CHIPPlugin_CAKeyTag,
59
+ };
60
+ _mPublicKey = SecKeyCreateWithData ((__bridge CFDataRef ) publicKeyNSData, (__bridge CFDictionaryRef ) attributes, nullptr );
61
+ }
62
+ return _mPublicKey;
59
63
}
60
64
61
65
- (CHIP_ERROR)Deserialize : (chip::Crypto::P256SerializedKeypair &)input
@@ -136,4 +140,11 @@ - (CHIP_ERROR)initSerializedKeyFromValue:(NSData *)value serializedKey:(chip::Cr
136
140
return CHIP_NO_ERROR;
137
141
}
138
142
143
+ - (void )dealloc
144
+ {
145
+ if (_mPublicKey) {
146
+ CFRelease (_mPublicKey);
147
+ }
148
+ }
149
+
139
150
@end
0 commit comments