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#import <CryptoTokenKit/CryptoTokenKit.h> 6#import <Foundation/Foundation.h> 7 8#include "crypto/apple_keychain_v2.h" 9#include "base/apple/foundation_util.h" 10#include "base/apple/scoped_cftyperef.h" 11#include "base/no_destructor.h" 12 13namespace crypto { 14 15static AppleKeychainV2* g_keychain_instance_override = nullptr; 16 17// static 18AppleKeychainV2& AppleKeychainV2::GetInstance() { 19 if (g_keychain_instance_override) { 20 return *g_keychain_instance_override; 21 } 22 static base::NoDestructor<AppleKeychainV2> k; 23 return *k; 24} 25 26// static 27void AppleKeychainV2::SetInstanceOverride(AppleKeychainV2* AppleKeychainV2) { 28 CHECK(!g_keychain_instance_override); 29 g_keychain_instance_override = AppleKeychainV2; 30} 31 32// static 33void AppleKeychainV2::ClearInstanceOverride() { 34 CHECK(g_keychain_instance_override); 35 g_keychain_instance_override = nullptr; 36} 37 38AppleKeychainV2::AppleKeychainV2() = default; 39AppleKeychainV2::~AppleKeychainV2() = default; 40 41NSArray* AppleKeychainV2::GetTokenIDs() { 42 return [[TKTokenWatcher alloc] init].tokenIDs; 43} 44 45base::apple::ScopedCFTypeRef<SecKeyRef> AppleKeychainV2::KeyCreateRandomKey( 46 CFDictionaryRef params, 47 CFErrorRef* error) { 48 return base::apple::ScopedCFTypeRef<SecKeyRef>( 49 SecKeyCreateRandomKey(params, error)); 50} 51 52base::apple::ScopedCFTypeRef<CFDataRef> AppleKeychainV2::KeyCreateSignature( 53 SecKeyRef key, 54 SecKeyAlgorithm algorithm, 55 CFDataRef data, 56 CFErrorRef* error) { 57 return base::apple::ScopedCFTypeRef<CFDataRef>( 58 SecKeyCreateSignature(key, algorithm, data, error)); 59} 60 61base::apple::ScopedCFTypeRef<SecKeyRef> AppleKeychainV2::KeyCopyPublicKey( 62 SecKeyRef key) { 63 return base::apple::ScopedCFTypeRef<SecKeyRef>(SecKeyCopyPublicKey(key)); 64} 65 66base::apple::ScopedCFTypeRef<CFDataRef> 67AppleKeychainV2::KeyCopyExternalRepresentation(SecKeyRef key, 68 CFErrorRef* error) { 69 return base::apple::ScopedCFTypeRef<CFDataRef>( 70 SecKeyCopyExternalRepresentation(key, error)); 71} 72 73base::apple::ScopedCFTypeRef<CFDictionaryRef> 74AppleKeychainV2::KeyCopyAttributes(SecKeyRef key) { 75 return base::apple::ScopedCFTypeRef<CFDictionaryRef>( 76 SecKeyCopyAttributes(key)); 77} 78 79OSStatus AppleKeychainV2::ItemCopyMatching( 80 CFDictionaryRef query, CFTypeRef* result) { 81 return SecItemCopyMatching(query, result); 82} 83 84OSStatus AppleKeychainV2::ItemDelete(CFDictionaryRef query) { 85 return SecItemDelete(query); 86} 87 88OSStatus AppleKeychainV2::ItemUpdate(CFDictionaryRef query, 89 CFDictionaryRef keychain_data) { 90 return SecItemUpdate(query, keychain_data); 91} 92 93#if !BUILDFLAG(IS_IOS) 94base::apple::ScopedCFTypeRef<CFTypeRef> 95AppleKeychainV2::TaskCopyValueForEntitlement(SecTaskRef task, 96 CFStringRef entitlement, 97 CFErrorRef* error) { 98 return base::apple::ScopedCFTypeRef<CFTypeRef>( 99 SecTaskCopyValueForEntitlement(task, entitlement, error)); 100} 101#endif // !BUILDFLAG(IS_IOS) 102 103BOOL AppleKeychainV2::LAContextCanEvaluatePolicy(LAPolicy policy, 104 NSError** error) { 105 LAContext* context = [[LAContext alloc] init]; 106 return [context canEvaluatePolicy:policy error:error]; 107} 108 109} // namespace crypto 110