/ OSX / utilities / entitlements.c
entitlements.c
 1  /*
 2  * Copyright (c) 2020 Apple Inc. All Rights Reserved.
 3  *
 4  * @APPLE_LICENSE_HEADER_START@
 5  *
 6  * This file contains Original Code and/or Modifications of Original Code
 7  * as defined in and that are subject to the Apple Public Source License
 8  * Version 2.0 (the 'License'). You may not use this file except in
 9  * compliance with the License. Please obtain a copy of the License at
10  * http://www.opensource.apple.com/apsl/ and read it before using this
11  * file.
12  *
13  * The Original Code and all software distributed under the License are
14  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18  * Please see the License for the specific language governing rights and
19  * limitations under the License.
20  *
21  * @APPLE_LICENSE_HEADER_END@
22  */
23  
24  #include "entitlements.h"
25  
26  /// Moves the entitlement value from the original entitlement into the target entitlement.
27  static void transferEntitlement(CFMutableDictionaryRef entitlements, CFStringRef originalEntitlement, CFStringRef targetEntitlement)
28  {
29      CFTypeRef value = (CFStringRef)CFDictionaryGetValue(entitlements, originalEntitlement);
30      CFDictionaryAddValue(entitlements, targetEntitlement, value);
31  }
32  
33  /// Determines if an entitlement needs fixup, which means it has a value for the original entitlement and no value for the
34  /// target entitlement.
35  static bool entitlementNeedsFixup(CFDictionaryRef entitlements, CFStringRef originalEntitlement, CFStringRef targetEntitlement)
36  {
37      // Entitlements only need fixup on macOS running on Apple Silicon, so just always fall through to the default case otherwise.
38  #if TARGET_OS_OSX && TARGET_CPU_ARM64
39      CFTypeRef originalValue = (CFStringRef)CFDictionaryGetValue(entitlements, originalEntitlement);
40      CFTypeRef newValue = (CFStringRef)CFDictionaryGetValue(entitlements, targetEntitlement);
41      if (originalValue != NULL && newValue == NULL) {
42          return true;
43      }
44  #endif
45      return false;
46  }
47  
48  bool needsCatalystEntitlementFixup(CFDictionaryRef entitlements)
49  {
50      return entitlementNeedsFixup(entitlements, CFSTR("application-identifier"), CFSTR("com.apple.application-identifier")) ||
51             entitlementNeedsFixup(entitlements, CFSTR("aps-environment"), CFSTR("com.apple.developer.aps-environment"));
52  }
53  
54  bool updateCatalystEntitlements(CFMutableDictionaryRef entitlements)
55  {
56      bool updated = false;
57      if (entitlementNeedsFixup(entitlements, CFSTR("application-identifier"), CFSTR("com.apple.application-identifier"))) {
58          transferEntitlement(entitlements, CFSTR("application-identifier"), CFSTR("com.apple.application-identifier"));
59          updated = true;
60      }
61      if (entitlementNeedsFixup(entitlements, CFSTR("aps-environment"), CFSTR("com.apple.developer.aps-environment"))) {
62          transferEntitlement(entitlements, CFSTR("aps-environment"), CFSTR("com.apple.developer.aps-environment"));
63          updated = true;
64      }
65      return updated;
66  }