2 * Copyright (c) 2020 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 #import <os/feature_private.h>
25 #import <sys/codesign.h>
27 #import "LegacyAPICounts.h"
28 #import "utilities/SecCoreAnalytics.h"
29 #import "SecEntitlements.h"
31 #import "SecInternalReleasePriv.h"
33 #pragma mark - File-Private
35 static NSString* applicationIdentifierForSelf() {
36 NSString* identifier = nil;
37 SecTaskRef task = SecTaskCreateFromSelf(kCFAllocatorDefault);
40 CFStringRef val = (CFStringRef)SecTaskCopyValueForEntitlement(task, kSecEntitlementApplicationIdentifier, NULL);
41 if (val && CFGetTypeID(val) != CFStringGetTypeID()) {
44 identifier = CFBridgingRelease(val);
47 // security tool doesn't have entitlements, I wonder if there are more.
49 identifier = CFBridgingRelease(SecTaskCopySigningIdentifier(task, NULL));
58 static BOOL countLegacyAPIEnabledForThread() {
59 NSNumber* value = [[NSThread currentThread] threadDictionary][@"countLegacyAPIEnabled"];
61 // No value means not set at all, so not disabled by SecItem*
62 if (!value || (value && [value isKindOfClass:[NSNumber class]] && [value boolValue])) {
70 void setCountLegacyAPIEnabledForThread(bool value) {
71 [[NSThread currentThread] threadDictionary][@"countLegacyAPIEnabled"] = value ? @YES : @NO;
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);
84 secerror("LegacyAPICounts: Attempt to count API without name");
88 if (!shouldCount || !countLegacyAPIEnabledForThread()) {
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);
99 [SecCoreAnalytics sendEventLazy:@"com.apple.security.LegacyAPICounts" builder:^NSDictionary<NSString *,NSObject *> * _Nonnull{
102 @"api" : apiStringObject,