xref: /aosp_15_r20/external/cronet/crypto/apple_keychain_util.mm (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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#include "crypto/apple_keychain_util.h"
6
7#include <string>
8
9#import <Security/Security.h>
10
11#include "base/apple/bridging.h"
12#include "base/apple/foundation_util.h"
13#include "base/apple/scoped_cftyperef.h"
14#include "base/strings/sys_string_conversions.h"
15#include "crypto/apple_keychain_v2.h"
16
17namespace crypto {
18
19#if !BUILDFLAG(IS_IOS)
20bool ExecutableHasKeychainAccessGroupEntitlement(
21    const std::string& keychain_access_group) {
22  base::apple::ScopedCFTypeRef<SecTaskRef> task(SecTaskCreateFromSelf(nullptr));
23  if (!task) {
24    return false;
25  }
26
27  base::apple::ScopedCFTypeRef<CFTypeRef> entitlement_value_cftype(
28      AppleKeychainV2::GetInstance().TaskCopyValueForEntitlement(
29          task.get(), CFSTR("keychain-access-groups"), nullptr));
30  if (!entitlement_value_cftype) {
31    return false;
32  }
33
34  NSArray* entitlement_value_nsarray = base::apple::CFToNSPtrCast(
35      base::apple::CFCast<CFArrayRef>(entitlement_value_cftype.get()));
36  if (!entitlement_value_nsarray) {
37    return false;
38  }
39
40  return [entitlement_value_nsarray
41      containsObject:base::SysUTF8ToNSString(keychain_access_group)];
42}
43#endif  // !BUILDFLAG(IS_IOS)
44
45}  // namespace crypto
46