暂无描述

FIRInstanceIDKeyPairStore.m 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FIRInstanceIDKeyPairStore.h"
  17. #import "FIRInstanceIDBackupExcludedPlist.h"
  18. #import "FIRInstanceIDConstants.h"
  19. #import "FIRInstanceIDDefines.h"
  20. #import "FIRInstanceIDKeyPair.h"
  21. #import "FIRInstanceIDKeyPairUtilities.h"
  22. #import "FIRInstanceIDKeychain.h"
  23. #import "FIRInstanceIDLogger.h"
  24. #import "FIRInstanceIDUtilities.h"
  25. #import "NSError+FIRInstanceID.h"
  26. // NOTE: These values should be in sync with what InstanceID saves in as.
  27. static NSString *const kFIRInstanceIDKeyPairStoreFileName = @"com.google.iid-keypair";
  28. static NSString *const kFIRInstanceIDStoreKeyGenerationTime = @"cre";
  29. static NSString *const kFIRInstanceIDStoreKeyPrefix = @"com.google.iid-";
  30. static NSString *const kFIRInstanceIDStoreKeyPublic = @"|P|";
  31. static NSString *const kFIRInstanceIDStoreKeyPrivate = @"|K|";
  32. static NSString *const kFIRInstanceIDStoreKeySubtype = @"|S|";
  33. static NSString *const kFIRInstanceIDKeyPairPublicTagPrefix = @"com.google.iid.keypair.public-";
  34. static NSString *const kFIRInstanceIDKeyPairPrivateTagPrefix = @"com.google.iid.keypair.private-";
  35. static const int kMaxMissingEntitlementErrorCount = 3;
  36. NSString *const kFIRInstanceIDKeyPairSubType = @"";
  37. // Query the key with NSData format
  38. NSData *FIRInstanceIDKeyDataWithTag(NSString *tag) {
  39. _FIRInstanceIDDevAssert([tag length], @"Invalid tag for keychain specified");
  40. if (![tag length]) {
  41. return NULL;
  42. }
  43. NSDictionary *queryKey = FIRInstanceIDKeyPairQuery(tag, YES, YES);
  44. CFTypeRef result = [[FIRInstanceIDKeychain sharedInstance] itemWithQuery:queryKey];
  45. if (!result) {
  46. return NULL;
  47. }
  48. return (__bridge NSData *)result;
  49. }
  50. // Query the key given a tag
  51. SecKeyRef FIRInstanceIDCachedKeyRefWithTag(NSString *tag) {
  52. _FIRInstanceIDDevAssert([tag length], @"Invalid tag for keychain specified");
  53. if (![tag length]) {
  54. return NULL;
  55. }
  56. NSDictionary *queryKey = FIRInstanceIDKeyPairQuery(tag, YES, NO);
  57. CFTypeRef result = [[FIRInstanceIDKeychain sharedInstance] itemWithQuery:queryKey];
  58. return (SecKeyRef)result;
  59. }
  60. // Check if keypair has been migrated from the legacy to the new version
  61. BOOL FIRInstanceIDHasMigratedKeyPair(NSString *legacyPublicKeyTag, NSString *newPublicKeyTag) {
  62. NSData *oldPublicKeyData = FIRInstanceIDKeyDataWithTag(legacyPublicKeyTag);
  63. NSData *newPublicKeyData = FIRInstanceIDKeyDataWithTag(newPublicKeyTag);
  64. return [oldPublicKeyData isEqualToData:newPublicKeyData];
  65. }
  66. // The legacy value is hardcoded to be the same key. This is a potential problem in shared keychain
  67. // environments.
  68. NSString *FIRInstanceIDLegacyPublicTagWithSubtype(NSString *subtype) {
  69. NSString *prefix = kFIRInstanceIDStoreKeyPrefix;
  70. return [NSString stringWithFormat:@"%@%@%@", prefix, subtype, kFIRInstanceIDStoreKeyPublic];
  71. }
  72. // The legacy value is hardcoded to be the same key. This is a potential problem in shared keychain
  73. // environments.
  74. NSString *FIRInstanceIDLegacyPrivateTagWithSubtype(NSString *subtype) {
  75. NSString *prefix = kFIRInstanceIDStoreKeyPrefix;
  76. return [NSString stringWithFormat:@"%@%@%@", prefix, subtype, kFIRInstanceIDStoreKeyPrivate];
  77. }
  78. NSString *FIRInstanceIDPublicTagWithSubtype(NSString *subtype) {
  79. static NSString *publicTag;
  80. static dispatch_once_t onceToken;
  81. dispatch_once(&onceToken, ^{
  82. NSString *mainAppBundleID = FIRInstanceIDAppIdentifier();
  83. publicTag =
  84. [NSString stringWithFormat:@"%@%@", kFIRInstanceIDKeyPairPublicTagPrefix, mainAppBundleID];
  85. });
  86. return publicTag;
  87. }
  88. NSString *FIRInstanceIDPrivateTagWithSubtype(NSString *subtype) {
  89. static NSString *privateTag;
  90. static dispatch_once_t onceToken;
  91. dispatch_once(&onceToken, ^{
  92. NSString *mainAppBundleID = FIRInstanceIDAppIdentifier();
  93. privateTag =
  94. [NSString stringWithFormat:@"%@%@", kFIRInstanceIDKeyPairPrivateTagPrefix, mainAppBundleID];
  95. });
  96. return privateTag;
  97. }
  98. NSString *FIRInstanceIDCreationTimeKeyWithSubtype(NSString *subtype) {
  99. return [NSString stringWithFormat:@"%@%@%@", subtype, kFIRInstanceIDStoreKeySubtype,
  100. kFIRInstanceIDStoreKeyGenerationTime];
  101. }
  102. @interface FIRInstanceIDKeyPairStore ()
  103. @property(nonatomic, readwrite, strong) FIRInstanceIDBackupExcludedPlist *plist;
  104. @property(nonatomic, readwrite, strong) FIRInstanceIDKeyPair *keyPair;
  105. @property(nonatomic, readwrite, assign) NSInteger keychainEntitlementsErrorCount;
  106. @end
  107. @implementation FIRInstanceIDKeyPairStore
  108. - (instancetype)init {
  109. self = [super init];
  110. if (self) {
  111. NSString *fileName = [[self class] keyStoreFileName];
  112. _plist =
  113. [[FIRInstanceIDBackupExcludedPlist alloc] initWithFileName:fileName
  114. subDirectory:kFIRInstanceIDSubDirectoryName];
  115. }
  116. return self;
  117. }
  118. - (BOOL)invalidateKeyPairsIfNeeded {
  119. // Currently keypairs are always invalidated if self.plist is missing. This normally indicates
  120. // a fresh install (or an uninstall/reinstall). In those situations the key pairs should be
  121. // deleted.
  122. // NOTE: Although this class refers to multiple key pairs, with different subtypes, in practice
  123. // only a single subtype is currently supported. (b/64906549)
  124. if (![self.plist doesFileExist]) {
  125. // A fresh install, clear all the key pairs in the key chain. Do not perform migration as all
  126. // key pairs are gone.
  127. [self deleteSavedKeyPairWithSubtype:kFIRInstanceIDKeyPairSubType handler:nil];
  128. return YES;
  129. }
  130. // Not a fresh install, perform migration at early state.
  131. [self migrateKeyPairCacheIfNeededWithHandler:nil];
  132. return NO;
  133. }
  134. - (BOOL)hasCachedKeyPairs {
  135. NSError *error;
  136. if ([self cachedKeyPairWithSubtype:kFIRInstanceIDKeyPairSubType error:&error] == nil) {
  137. if (error) {
  138. FIRInstanceIDLoggerDebug(kFIRInstanceIDMessageCodeKeyPairStore000,
  139. @"Failed to get cached keyPair %@", error);
  140. }
  141. error = nil;
  142. [self removeKeyPairCreationTimePlistWithError:&error];
  143. if (error) {
  144. FIRInstanceIDLoggerDebug(kFIRInstanceIDMessageCodeKeyPairStore001,
  145. @"Failed to remove keyPair creationTime plist %@", error);
  146. }
  147. return NO;
  148. }
  149. return YES;
  150. }
  151. - (NSString *)appIdentityWithError:(NSError *__autoreleasing *)error {
  152. // Load the keyPair from Keychain (or generate a key pair, if this is the first run of the app).
  153. FIRInstanceIDKeyPair *keyPair = [self loadKeyPairWithError:error];
  154. if (!keyPair) {
  155. FIRInstanceIDLoggerError(kFIRInstanceIDMessageCodeKeyPairStoreCouldNotLoadKeyPair,
  156. @"Keypair could not be loaded from Keychain. Error: %@", (*error));
  157. return nil;
  158. }
  159. if (error) {
  160. *error = nil;
  161. }
  162. NSString *appIdentity = FIRInstanceIDAppIdentity(keyPair);
  163. if (!appIdentity.length) {
  164. if (error) {
  165. *error = [NSError errorWithFIRInstanceIDErrorCode:kFIRInstanceIDErrorCodeUnknown];
  166. }
  167. }
  168. return appIdentity;
  169. }
  170. - (FIRInstanceIDKeyPair *)loadKeyPairWithError:(NSError **)error {
  171. // In case we call this from different threads we don't want to generate or fetch the
  172. // keyPair multiple times. Once we have a keyPair in the cache it would mostly be used
  173. // from there.
  174. @synchronized(self) {
  175. if ([self.keyPair isValid]) {
  176. return self.keyPair;
  177. }
  178. if (self.keychainEntitlementsErrorCount >= kMaxMissingEntitlementErrorCount) {
  179. FIRInstanceIDLoggerDebug(kFIRInstanceIDMessageCodeKeyPairStore002,
  180. @"Keychain not accessible, Entitlements missing error (-34018). "
  181. @"Will not check token in cache.");
  182. return nil;
  183. }
  184. if (!self.keyPair) {
  185. self.keyPair = [self validCachedKeyPairWithSubtype:kFIRInstanceIDKeyPairSubType error:error];
  186. }
  187. if ((*error).code == kFIRInstanceIDSecMissingEntitlementErrorCode) {
  188. self.keychainEntitlementsErrorCount++;
  189. }
  190. if (!self.keyPair) {
  191. self.keyPair = [self generateAndSaveKeyWithSubtype:kFIRInstanceIDKeyPairSubType
  192. creationTime:FIRInstanceIDCurrentTimestampInSeconds()
  193. error:error];
  194. }
  195. }
  196. return self.keyPair;
  197. }
  198. // TODO(chliangGoogle: Remove subtype support, as it's not being used.
  199. - (FIRInstanceIDKeyPair *)generateAndSaveKeyWithSubtype:(NSString *)subtype
  200. creationTime:(int64_t)creationTime
  201. error:(NSError **)error {
  202. NSString *publicKeyTag = FIRInstanceIDPublicTagWithSubtype(subtype);
  203. NSString *privateKeyTag = FIRInstanceIDPrivateTagWithSubtype(subtype);
  204. FIRInstanceIDKeyPair *keyPair =
  205. [[FIRInstanceIDKeychain sharedInstance] generateKeyPairWithPrivateTag:privateKeyTag
  206. publicTag:publicKeyTag];
  207. if (![keyPair isValid]) {
  208. FIRInstanceIDLoggerError(kFIRInstanceIDMessageCodeKeyPairStore003,
  209. @"Unable to generate keypair.");
  210. return nil;
  211. }
  212. NSString *creationTimeKey = FIRInstanceIDCreationTimeKeyWithSubtype(subtype);
  213. NSDictionary *keyPairData = @{creationTimeKey : @(creationTime)};
  214. if (error) {
  215. *error = nil;
  216. }
  217. NSMutableDictionary *allKeyPairs = [[self.plist contentAsDictionary] mutableCopy];
  218. if (allKeyPairs.count) {
  219. [allKeyPairs addEntriesFromDictionary:keyPairData];
  220. } else {
  221. allKeyPairs = [keyPairData mutableCopy];
  222. }
  223. if (![self.plist writeDictionary:allKeyPairs error:error]) {
  224. [FIRInstanceIDKeyPairStore deleteKeyPairWithPrivateTag:privateKeyTag
  225. publicTag:publicKeyTag
  226. handler:nil];
  227. FIRInstanceIDLoggerError(kFIRInstanceIDMessageCodeKeyPairStore004,
  228. @"Failed to save keypair data to plist %@", error ? *error : @"");
  229. return nil;
  230. }
  231. return keyPair;
  232. }
  233. - (FIRInstanceIDKeyPair *)validCachedKeyPairWithSubtype:(NSString *)subtype
  234. error:(NSError **)error {
  235. // On a new install (or if the ID was deleted), the plist will be missing, which should trigger
  236. // a reset of the key pairs in Keychain (if they exist).
  237. NSDictionary *allKeyPairs = [self.plist contentAsDictionary];
  238. NSString *creationTimeKey = FIRInstanceIDCreationTimeKeyWithSubtype(subtype);
  239. if (allKeyPairs[creationTimeKey] > 0) {
  240. return [self cachedKeyPairWithSubtype:subtype error:error];
  241. } else {
  242. // There is no need to reset keypair again here as FIRInstanceID init call is always
  243. // going to be ahead of this call, which already trigger keypair reset if it's new install
  244. FIRInstanceIDErrorCode code = kFIRInstanceIDErrorCodeInvalidKeyPairCreationTime;
  245. *error = [NSError errorWithFIRInstanceIDErrorCode:code];
  246. return nil;
  247. }
  248. }
  249. - (FIRInstanceIDKeyPair *)cachedKeyPairWithSubtype:(NSString *)subtype
  250. error:(NSError *__autoreleasing *)error {
  251. // base64 encoded keys
  252. NSString *publicKeyTag = FIRInstanceIDPublicTagWithSubtype(subtype);
  253. NSString *privateKeyTag = FIRInstanceIDPrivateTagWithSubtype(subtype);
  254. return [FIRInstanceIDKeyPairStore keyPairForPrivateKeyTag:privateKeyTag
  255. publicKeyTag:publicKeyTag
  256. error:error];
  257. }
  258. + (FIRInstanceIDKeyPair *)keyPairForPrivateKeyTag:(NSString *)privateKeyTag
  259. publicKeyTag:(NSString *)publicKeyTag
  260. error:(NSError *__autoreleasing *)error {
  261. _FIRInstanceIDDevAssert([privateKeyTag length] && [publicKeyTag length],
  262. @"Invalid tags for keypair");
  263. if (![privateKeyTag length] || ![publicKeyTag length]) {
  264. if (error) {
  265. *error = [NSError errorWithFIRInstanceIDErrorCode:kFIRInstanceIDErrorCodeInvalidKeyPairTags];
  266. }
  267. return nil;
  268. }
  269. SecKeyRef privateKeyRef = FIRInstanceIDCachedKeyRefWithTag(privateKeyTag);
  270. SecKeyRef publicKeyRef = FIRInstanceIDCachedKeyRefWithTag(publicKeyTag);
  271. if (!privateKeyRef || !publicKeyRef) {
  272. if (error) {
  273. *error = [NSError errorWithFIRInstanceIDErrorCode:kFIRInstanceIDErrorCodeMissingKeyPair];
  274. }
  275. FIRInstanceIDLoggerDebug(kFIRInstanceIDMessageCodeKeyPair000,
  276. @"No keypair info is retrieved with tag %@", privateKeyTag);
  277. return nil;
  278. }
  279. NSData *publicKeyData = FIRInstanceIDKeyDataWithTag(publicKeyTag);
  280. NSData *privateKeyData = FIRInstanceIDKeyDataWithTag(privateKeyTag);
  281. FIRInstanceIDKeyPair *keyPair = [[FIRInstanceIDKeyPair alloc] initWithPrivateKey:privateKeyRef
  282. publicKey:publicKeyRef
  283. publicKeyData:publicKeyData
  284. privateKeyData:privateKeyData];
  285. return keyPair;
  286. }
  287. // Migrates from keypair saved under legacy keys (hardcoded value) to dynamic keys (stable, but
  288. // unique for the app's bundle id
  289. - (void)migrateKeyPairCacheIfNeededWithHandler:(void (^)(NSError *error))handler {
  290. // Attempt to load keypair using legacy keys
  291. NSString *legacyPublicKeyTag =
  292. FIRInstanceIDLegacyPublicTagWithSubtype(kFIRInstanceIDKeyPairSubType);
  293. NSString *legacyPrivateKeyTag =
  294. FIRInstanceIDLegacyPrivateTagWithSubtype(kFIRInstanceIDKeyPairSubType);
  295. NSError *error;
  296. FIRInstanceIDKeyPair *keyPair =
  297. [FIRInstanceIDKeyPairStore keyPairForPrivateKeyTag:legacyPrivateKeyTag
  298. publicKeyTag:legacyPublicKeyTag
  299. error:&error];
  300. if (![keyPair isValid]) {
  301. if (handler) {
  302. handler(nil);
  303. }
  304. return;
  305. }
  306. // Check whether migration already done.
  307. NSString *publicKeyTag = FIRInstanceIDPublicTagWithSubtype(kFIRInstanceIDKeyPairSubType);
  308. if (FIRInstanceIDHasMigratedKeyPair(legacyPublicKeyTag, publicKeyTag)) {
  309. if (handler) {
  310. handler(nil);
  311. }
  312. return;
  313. }
  314. // Also cache locally since we are sure to use the migrated key pair.
  315. self.keyPair = keyPair;
  316. // Either new key pair doesn't exist or it's different than legacy key pair, start the migration.
  317. NSString *privateKeyTag = FIRInstanceIDPrivateTagWithSubtype(kFIRInstanceIDKeyPairSubType);
  318. [self updateKeyRef:keyPair.publicKey
  319. withTag:publicKeyTag
  320. handler:^(NSError *error) {
  321. if (error) {
  322. FIRInstanceIDLoggerError(kFIRInstanceIDMessageCodeKeyPairMigrationError,
  323. @"Unable to migrate key pair from legacy ones.");
  324. }
  325. [self updateKeyRef:keyPair.privateKey
  326. withTag:privateKeyTag
  327. handler:^(NSError *error) {
  328. if (error) {
  329. FIRInstanceIDLoggerError(
  330. kFIRInstanceIDMessageCodeKeyPairMigrationError,
  331. @"Unable to migrate key pair from legacy ones.");
  332. return;
  333. }
  334. FIRInstanceIDLoggerDebug(
  335. kFIRInstanceIDMessageCodeKeyPairMigrationSuccess,
  336. @"Successfully migrated the key pair from legacy ones.");
  337. if (handler) {
  338. handler(error);
  339. }
  340. }];
  341. }];
  342. }
  343. // Used for migrating from legacy tags to updated tags. The legacy keychain is not deleted for
  344. // backward compatibility.
  345. // TODO(chliangGoogle) Delete the legacy keychain when GCM is fully deprecated.
  346. - (void)updateKeyRef:(SecKeyRef)keyRef
  347. withTag:(NSString *)tag
  348. handler:(void (^)(NSError *error))handler {
  349. NSData *updatedTagData = [tag dataUsingEncoding:NSUTF8StringEncoding];
  350. // Always delete the old keychain before adding a new one to avoid conflicts.
  351. NSDictionary *deleteQuery = @{
  352. (__bridge id)kSecAttrApplicationTag : updatedTagData,
  353. (__bridge id)kSecClass : (__bridge id)kSecClassKey,
  354. (__bridge id)kSecAttrKeyType : (__bridge id)kSecAttrKeyTypeRSA,
  355. (__bridge id)kSecReturnRef : @(YES),
  356. };
  357. [[FIRInstanceIDKeychain sharedInstance]
  358. removeItemWithQuery:deleteQuery
  359. handler:^(NSError *error) {
  360. if (error) {
  361. if (handler) {
  362. handler(error);
  363. }
  364. return;
  365. }
  366. NSDictionary *addQuery = @{
  367. (__bridge id)kSecAttrApplicationTag : updatedTagData,
  368. (__bridge id)kSecClass : (__bridge id)kSecClassKey,
  369. (__bridge id)kSecValueRef : (__bridge id)keyRef,
  370. (__bridge id)
  371. kSecAttrAccessible : (__bridge id)kSecAttrAccessibleAlwaysThisDeviceOnly,
  372. };
  373. [[FIRInstanceIDKeychain sharedInstance] addItemWithQuery:addQuery
  374. handler:^(NSError *addError) {
  375. if (handler) {
  376. handler(addError);
  377. }
  378. }];
  379. }];
  380. }
  381. - (void)deleteSavedKeyPairWithSubtype:(NSString *)subtype
  382. handler:(void (^)(NSError *error))handler {
  383. NSDictionary *allKeyPairs = [self.plist contentAsDictionary];
  384. NSString *publicKeyTag = FIRInstanceIDPublicTagWithSubtype(subtype);
  385. NSString *privateKeyTag = FIRInstanceIDPrivateTagWithSubtype(subtype);
  386. NSString *creationTimeKey = FIRInstanceIDCreationTimeKeyWithSubtype(subtype);
  387. // remove the creation time
  388. if (allKeyPairs[creationTimeKey] > 0) {
  389. NSMutableDictionary *newKeyPairs = [NSMutableDictionary dictionaryWithDictionary:allKeyPairs];
  390. [newKeyPairs removeObjectForKey:creationTimeKey];
  391. NSError *plistError;
  392. if (![self.plist writeDictionary:newKeyPairs error:&plistError]) {
  393. FIRInstanceIDLoggerError(kFIRInstanceIDMessageCodeKeyPairStore006,
  394. @"Unable to remove keypair creation time from plist %@", plistError);
  395. }
  396. }
  397. [FIRInstanceIDKeyPairStore
  398. deleteKeyPairWithPrivateTag:privateKeyTag
  399. publicTag:publicKeyTag
  400. handler:^(NSError *error) {
  401. // Delete legacy key pairs from GCM/FCM If they exist. All key pairs
  402. // should be deleted when app is newly installed.
  403. NSString *legacyPublicKeyTag =
  404. FIRInstanceIDLegacyPublicTagWithSubtype(subtype);
  405. NSString *legacyPrivateKeyTag =
  406. FIRInstanceIDLegacyPrivateTagWithSubtype(subtype);
  407. [FIRInstanceIDKeyPairStore
  408. deleteKeyPairWithPrivateTag:legacyPrivateKeyTag
  409. publicTag:legacyPublicKeyTag
  410. handler:nil];
  411. if (error) {
  412. FIRInstanceIDLoggerError(kFIRInstanceIDMessageCodeKeyPairStore007,
  413. @"Unable to remove RSA keypair, error: %@",
  414. error);
  415. if (handler) {
  416. handler(error);
  417. }
  418. } else {
  419. self.keyPair = nil;
  420. if (handler) {
  421. handler(nil);
  422. }
  423. }
  424. }];
  425. }
  426. + (void)deleteKeyPairWithPrivateTag:(NSString *)privateTag
  427. publicTag:(NSString *)publicTag
  428. handler:(void (^)(NSError *))handler {
  429. NSDictionary *queryPublicKey = FIRInstanceIDKeyPairQuery(publicTag, NO, NO);
  430. NSDictionary *queryPrivateKey = FIRInstanceIDKeyPairQuery(privateTag, NO, NO);
  431. // Always remove public key first because it is the key we generate IID.
  432. [[FIRInstanceIDKeychain sharedInstance] removeItemWithQuery:queryPublicKey
  433. handler:^(NSError *error) {
  434. if (error) {
  435. if (handler) {
  436. handler(error);
  437. }
  438. return;
  439. }
  440. [[FIRInstanceIDKeychain sharedInstance]
  441. removeItemWithQuery:queryPrivateKey
  442. handler:^(NSError *error) {
  443. if (error) {
  444. if (handler) {
  445. handler(error);
  446. }
  447. return;
  448. }
  449. if (handler) {
  450. handler(nil);
  451. }
  452. }];
  453. }];
  454. }
  455. - (BOOL)removeKeyPairCreationTimePlistWithError:(NSError *__autoreleasing *)error {
  456. if (![self.plist deleteFile:error]) {
  457. FIRInstanceIDLoggerError(kFIRInstanceIDMessageCodeKeyPairStore008,
  458. @"Unable to delete keypair creation times plist");
  459. return NO;
  460. }
  461. return YES;
  462. }
  463. + (NSString *)keyStoreFileName {
  464. return kFIRInstanceIDKeyPairStoreFileName;
  465. }
  466. @end