]>
Commit | Line | Data |
---|---|---|
d64be36e A |
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 | #import <os/feature_private.h> | |
25 | #import <sys/codesign.h> | |
26 | ||
27 | #import "LegacyAPICounts.h" | |
28 | #import "utilities/SecCoreAnalytics.h" | |
29 | #import "SecEntitlements.h" | |
30 | #import "debugging.h" | |
31 | #import "SecInternalReleasePriv.h" | |
32 | ||
33 | #pragma mark - File-Private | |
34 | ||
35 | static NSString* applicationIdentifierForSelf() { | |
36 | NSString* identifier = nil; | |
37 | SecTaskRef task = SecTaskCreateFromSelf(kCFAllocatorDefault); | |
38 | ||
39 | if (task) { | |
40 | CFStringRef val = (CFStringRef)SecTaskCopyValueForEntitlement(task, kSecEntitlementApplicationIdentifier, NULL); | |
41 | if (val && CFGetTypeID(val) != CFStringGetTypeID()) { | |
42 | CFRelease(val); | |
43 | } else { | |
44 | identifier = CFBridgingRelease(val); | |
45 | } | |
46 | ||
47 | // security tool doesn't have entitlements, I wonder if there are more. | |
48 | if (!identifier) { | |
49 | identifier = CFBridgingRelease(SecTaskCopySigningIdentifier(task, NULL)); | |
50 | } | |
51 | ||
52 | CFRelease(task); | |
53 | } | |
54 | ||
55 | return identifier; | |
56 | } | |
57 | ||
58 | static BOOL countLegacyAPIEnabledForThread() { | |
59 | NSNumber* value = [[NSThread currentThread] threadDictionary][@"countLegacyAPIEnabled"]; | |
60 | ||
61 | // No value means not set at all, so not disabled by SecItem* | |
62 | if (!value || (value && [value isKindOfClass:[NSNumber class]] && [value boolValue])) { | |
63 | return YES; | |
64 | } | |
65 | return NO; | |
66 | } | |
67 | ||
68 | #pragma mark - SPI | |
69 | ||
70 | void setCountLegacyAPIEnabledForThread(bool value) { | |
71 | [[NSThread currentThread] threadDictionary][@"countLegacyAPIEnabled"] = value ? @YES : @NO; | |
72 | } | |
73 | ||
74 | void countLegacyAPI(dispatch_once_t* token, const char* api) { | |
75 | static NSString* identifier; | |
76 | static BOOL shouldCount; | |
77 | static dispatch_once_t onceToken; | |
78 | dispatch_once(&onceToken, ^{ | |
79 | identifier = applicationIdentifierForSelf() ?: @"unknown"; | |
80 | shouldCount = os_feature_enabled(Security, LegacyAPICounts); | |
81 | }); | |
82 | ||
83 | if (api == nil) { | |
84 | secerror("LegacyAPICounts: Attempt to count API without name"); | |
85 | return; | |
86 | } | |
87 | ||
88 | if (!shouldCount || !countLegacyAPIEnabledForThread()) { | |
89 | return; | |
90 | } | |
91 | ||
92 | dispatch_once(token, ^{ | |
93 | NSString* apiStringObject = [NSString stringWithCString:api encoding:NSUTF8StringEncoding]; | |
94 | if (!apiStringObject) { | |
95 | secerror("LegacyAPICounts: Surprisingly, char* for api name \"%s\" did not turn into NSString", api); | |
96 | return; | |
97 | } | |
98 | ||
99 | [SecCoreAnalytics sendEventLazy:@"com.apple.security.LegacyAPICounts" builder:^NSDictionary<NSString *,NSObject *> * _Nonnull{ | |
100 | return @{ | |
101 | @"app" : identifier, | |
102 | @"api" : apiStringObject, | |
103 | }; | |
104 | }]; | |
105 | }); | |
106 | } |