1 // Copyright 2024 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CRYPTO_FAKE_APPLE_KEYCHAIN_V2_H_ 6 #define CRYPTO_FAKE_APPLE_KEYCHAIN_V2_H_ 7 8 #import <Foundation/Foundation.h> 9 10 #include <string> 11 #include <vector> 12 13 #include "base/apple/scoped_cftyperef.h" 14 #include "crypto/apple_keychain_v2.h" 15 #include "crypto/crypto_export.h" 16 #include "crypto/scoped_fake_apple_keychain_v2.h" 17 18 namespace crypto { 19 20 // FakeAppleKeychainV2 is an implementation of AppleKeychainV2 for testing. It 21 // works around behavior that can't be relied on in tests, such as writing to 22 // the actual Keychain or using functionality that requires code-signed, 23 // entitled builds. 24 class CRYPTO_EXPORT FakeAppleKeychainV2 : public AppleKeychainV2 { 25 public: 26 using UVMethod = ScopedFakeAppleKeychainV2::UVMethod; 27 28 explicit FakeAppleKeychainV2(const std::string& keychain_access_group); 29 FakeAppleKeychainV2(const FakeAppleKeychainV2&) = delete; 30 FakeAppleKeychainV2& operator=(const FakeAppleKeychainV2&) = delete; 31 ~FakeAppleKeychainV2() override; 32 items()33 const std::vector<base::apple::ScopedCFTypeRef<CFDictionaryRef>>& items() { 34 return items_; 35 } 36 set_secure_enclave_available(bool is_secure_enclave_available)37 void set_secure_enclave_available(bool is_secure_enclave_available) { 38 is_secure_enclave_available_ = is_secure_enclave_available; 39 } 40 set_uv_method(UVMethod uv_method)41 void set_uv_method(UVMethod uv_method) { uv_method_ = uv_method; } 42 43 // AppleKeychainV2: 44 NSArray* GetTokenIDs() override; 45 base::apple::ScopedCFTypeRef<SecKeyRef> KeyCreateRandomKey( 46 CFDictionaryRef params, 47 CFErrorRef* error) override; 48 base::apple::ScopedCFTypeRef<CFDictionaryRef> KeyCopyAttributes( 49 SecKeyRef key) override; 50 OSStatus ItemCopyMatching(CFDictionaryRef query, CFTypeRef* result) override; 51 OSStatus ItemDelete(CFDictionaryRef query) override; 52 OSStatus ItemUpdate(CFDictionaryRef query, 53 CFDictionaryRef keychain_data) override; 54 #if !BUILDFLAG(IS_IOS) 55 base::apple::ScopedCFTypeRef<CFTypeRef> TaskCopyValueForEntitlement( 56 SecTaskRef task, 57 CFStringRef entitlement, 58 CFErrorRef* error) override; 59 #endif // !BUILDFLAG(IS_IOS) 60 BOOL LAContextCanEvaluatePolicy(LAPolicy policy, 61 NSError* __autoreleasing* error) override; 62 63 private: 64 bool is_secure_enclave_available_ = true; 65 66 UVMethod uv_method_ = UVMethod::kBiometrics; 67 68 // items_ contains the keychain items created by `KeyCreateRandomKey`. 69 std::vector<base::apple::ScopedCFTypeRef<CFDictionaryRef>> items_; 70 // keychain_access_group_ is the value of `kSecAttrAccessGroup` that this 71 // keychain expects to operate on. 72 base::apple::ScopedCFTypeRef<CFStringRef> keychain_access_group_; 73 }; 74 75 } // namespace crypto 76 77 #endif // CRYPTO_FAKE_APPLE_KEYCHAIN_V2_H_ 78