You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.5 KiB

  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 "FIRInstanceIDKeychain.h"
  17. #import "FIRInstanceIDLogger.h"
  18. NSString *const kFIRInstanceIDKeychainErrorDomain = @"com.google.iid";
  19. @interface FIRInstanceIDKeychain () {
  20. dispatch_queue_t _keychainOperationQueue;
  21. }
  22. @end
  23. @implementation FIRInstanceIDKeychain
  24. + (instancetype)sharedInstance {
  25. static FIRInstanceIDKeychain *sharedInstance;
  26. static dispatch_once_t onceToken;
  27. dispatch_once(&onceToken, ^{
  28. sharedInstance = [[FIRInstanceIDKeychain alloc] init];
  29. });
  30. return sharedInstance;
  31. }
  32. - (instancetype)init {
  33. self = [super init];
  34. if (self) {
  35. _keychainOperationQueue =
  36. dispatch_queue_create("com.google.FirebaseInstanceID.Keychain", DISPATCH_QUEUE_SERIAL);
  37. }
  38. return self;
  39. }
  40. - (CFTypeRef)itemWithQuery:(NSDictionary *)keychainQuery {
  41. __block SecKeyRef keyRef = NULL;
  42. dispatch_sync(_keychainOperationQueue, ^{
  43. OSStatus status =
  44. SecItemCopyMatching((__bridge CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyRef);
  45. if (status != noErr) {
  46. if (keyRef) {
  47. CFRelease(keyRef);
  48. }
  49. FIRInstanceIDLoggerDebug(kFIRInstanceIDKeychainReadItemError,
  50. @"Info is not found in Keychain. OSStatus: %d. Keychain query: %@",
  51. (int)status, keychainQuery);
  52. }
  53. });
  54. return keyRef;
  55. }
  56. - (void)removeItemWithQuery:(NSDictionary *)keychainQuery
  57. handler:(void (^)(NSError *error))handler {
  58. dispatch_async(_keychainOperationQueue, ^{
  59. OSStatus status = SecItemDelete((__bridge CFDictionaryRef)keychainQuery);
  60. if (status != noErr) {
  61. FIRInstanceIDLoggerDebug(
  62. kFIRInstanceIDKeychainDeleteItemError,
  63. @"Couldn't delete item from Keychain OSStatus: %d with the keychain query %@",
  64. (int)status, keychainQuery);
  65. }
  66. if (handler) {
  67. NSError *error;
  68. // When item is not found, it should NOT be considered as an error. The operation should
  69. // continue.
  70. if (status != noErr && status != errSecItemNotFound) {
  71. error = [NSError errorWithDomain:kFIRInstanceIDKeychainErrorDomain
  72. code:status
  73. userInfo:nil];
  74. }
  75. dispatch_async(dispatch_get_main_queue(), ^{
  76. handler(error);
  77. });
  78. }
  79. });
  80. }
  81. - (void)addItemWithQuery:(NSDictionary *)keychainQuery handler:(void (^)(NSError *))handler {
  82. dispatch_async(_keychainOperationQueue, ^{
  83. OSStatus status = SecItemAdd((__bridge CFDictionaryRef)keychainQuery, NULL);
  84. if (handler) {
  85. NSError *error;
  86. if (status != noErr) {
  87. FIRInstanceIDLoggerWarning(kFIRInstanceIDKeychainAddItemError,
  88. @"Couldn't add item to Keychain OSStatus: %d", (int)status);
  89. error = [NSError errorWithDomain:kFIRInstanceIDKeychainErrorDomain
  90. code:status
  91. userInfo:nil];
  92. }
  93. dispatch_async(dispatch_get_main_queue(), ^{
  94. handler(error);
  95. });
  96. }
  97. });
  98. }
  99. @end