FIRAppAttestArtifactStorage.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 2021 Google LLC
  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 "FirebaseAppCheck/Sources/AppAttestProvider/Storage/FIRAppAttestArtifactStorage.h"
  17. #if __has_include(<FBLPromises/FBLPromises.h>)
  18. #import <FBLPromises/FBLPromises.h>
  19. #else
  20. #import "FBLPromises.h"
  21. #endif
  22. #import <GoogleUtilities/GULKeychainStorage.h>
  23. #import "FirebaseAppCheck/Sources/AppAttestProvider/Storage/FIRAppAttestStoredArtifact.h"
  24. #import "FirebaseAppCheck/Sources/Core/Errors/FIRAppCheckErrorUtil.h"
  25. NS_ASSUME_NONNULL_BEGIN
  26. static NSString *const kKeychainService = @"com.firebase.app_check.app_attest_artifact_storage";
  27. @interface FIRAppAttestArtifactStorage ()
  28. @property(nonatomic, readonly) NSString *appName;
  29. @property(nonatomic, readonly) NSString *appID;
  30. @property(nonatomic, readonly) GULKeychainStorage *keychainStorage;
  31. @property(nonatomic, readonly, nullable) NSString *accessGroup;
  32. @end
  33. @implementation FIRAppAttestArtifactStorage
  34. - (instancetype)initWithAppName:(NSString *)appName
  35. appID:(NSString *)appID
  36. keychainStorage:(GULKeychainStorage *)keychainStorage
  37. accessGroup:(nullable NSString *)accessGroup {
  38. self = [super init];
  39. if (self) {
  40. _appName = [appName copy];
  41. _appID = [appID copy];
  42. _keychainStorage = keychainStorage;
  43. _accessGroup = [accessGroup copy];
  44. }
  45. return self;
  46. }
  47. - (instancetype)initWithAppName:(NSString *)appName
  48. appID:(NSString *)appID
  49. accessGroup:(nullable NSString *)accessGroup {
  50. GULKeychainStorage *keychainStorage =
  51. [[GULKeychainStorage alloc] initWithService:kKeychainService];
  52. return [self initWithAppName:appName
  53. appID:appID
  54. keychainStorage:keychainStorage
  55. accessGroup:accessGroup];
  56. }
  57. - (FBLPromise<NSData *> *)getArtifactForKey:(NSString *)keyID {
  58. return [self.keychainStorage getObjectForKey:[self artifactKey]
  59. objectClass:[FIRAppAttestStoredArtifact class]
  60. accessGroup:self.accessGroup]
  61. .then(^NSData *(id<NSSecureCoding> storedArtifact) {
  62. FIRAppAttestStoredArtifact *artifact = (FIRAppAttestStoredArtifact *)storedArtifact;
  63. if ([artifact isKindOfClass:[FIRAppAttestStoredArtifact class]] &&
  64. [artifact.keyID isEqualToString:keyID]) {
  65. return artifact.artifact;
  66. } else {
  67. return nil;
  68. }
  69. })
  70. .recover(^NSError *(NSError *error) {
  71. return [FIRAppCheckErrorUtil keychainErrorWithError:error];
  72. });
  73. }
  74. - (FBLPromise<NSData *> *)setArtifact:(nullable NSData *)artifact forKey:(nonnull NSString *)keyID {
  75. if (artifact) {
  76. return [self storeArtifact:artifact forKey:keyID].recover(^NSError *(NSError *error) {
  77. return [FIRAppCheckErrorUtil keychainErrorWithError:error];
  78. });
  79. } else {
  80. return [self.keychainStorage removeObjectForKey:[self artifactKey] accessGroup:self.accessGroup]
  81. .then(^id _Nullable(NSNull *_Nullable value) {
  82. return nil;
  83. })
  84. .recover(^NSError *(NSError *error) {
  85. return [FIRAppCheckErrorUtil keychainErrorWithError:error];
  86. });
  87. }
  88. }
  89. #pragma mark - Helpers
  90. - (FBLPromise<NSData *> *)storeArtifact:(nullable NSData *)artifact
  91. forKey:(nonnull NSString *)keyID {
  92. FIRAppAttestStoredArtifact *storedArtifact =
  93. [[FIRAppAttestStoredArtifact alloc] initWithKeyID:keyID artifact:artifact];
  94. return [self.keychainStorage setObject:storedArtifact
  95. forKey:[self artifactKey]
  96. accessGroup:self.accessGroup]
  97. .then(^id _Nullable(NSNull *_Nullable value) {
  98. return artifact;
  99. });
  100. }
  101. - (NSString *)artifactKey {
  102. return
  103. [NSString stringWithFormat:@"app_check_app_attest_artifact.%@.%@", self.appName, self.appID];
  104. }
  105. @end
  106. NS_ASSUME_NONNULL_END