• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ItemAdd(CFDictionaryRef attributes, CFTypeRef* result) override;
51   OSStatus ItemCopyMatching(CFDictionaryRef query, CFTypeRef* result) override;
52   OSStatus ItemDelete(CFDictionaryRef query) override;
53   OSStatus ItemUpdate(CFDictionaryRef query,
54                       CFDictionaryRef keychain_data) override;
55 #if !BUILDFLAG(IS_IOS)
56   base::apple::ScopedCFTypeRef<CFTypeRef> TaskCopyValueForEntitlement(
57       SecTaskRef task,
58       CFStringRef entitlement,
59       CFErrorRef* error) override;
60 #endif  // !BUILDFLAG(IS_IOS)
61   BOOL LAContextCanEvaluatePolicy(LAPolicy policy,
62                                   NSError* __autoreleasing* error) override;
63 
64  private:
65   bool is_secure_enclave_available_ = true;
66 
67   UVMethod uv_method_ = UVMethod::kBiometrics;
68 
69   // items_ contains the keychain items created by `KeyCreateRandomKey`.
70   std::vector<base::apple::ScopedCFTypeRef<CFDictionaryRef>> items_;
71   // keychain_access_group_ is the value of `kSecAttrAccessGroup` that this
72   // keychain expects to operate on.
73   base::apple::ScopedCFTypeRef<CFStringRef> keychain_access_group_;
74 };
75 
76 }  // namespace crypto
77 
78 #endif  // CRYPTO_FAKE_APPLE_KEYCHAIN_V2_H_
79