1 #import <CloudKit/CKContainer_Private.h>
2 #import <CloudKit/CloudKit.h>
3 #import <CoreFoundation/CFPriv.h>
4 #import <Foundation/Foundation.h>
5 #import <Foundation/NSURLSession.h>
6 #import <dispatch/dispatch.h>
7 #import <os/variant_private.h>
9 #include "policy_dryrun.h"
15 #include <Security/SecItem.h>
16 #include <Security/SecItemPriv.h>
17 #include <Security/Security.h>
18 #include <keychain/SecureObjectSync/SOSCloudCircle.h>
19 #include <keychain/SecureObjectSync/SOSViews.h>
20 #import "TrustedPeers/TrustedPeers.h"
22 #include "tool_errors.h"
24 // Depends on a modern enough TrustedPeers framework.
25 // If the machine doesn't have one installed, set DYLD_FRAMEWORK_PATH
27 // Stolen from keychain/SecureObjectSync/SOSEngine.c
29 static NSString* getSOSView(id object, NSString* itemClass) {
30 if (![object isKindOfClass:[NSDictionary class]]) {
34 NSString *viewHint = object[(NSString*)kSecAttrSyncViewHint];
35 if (viewHint != nil) {
38 NSString *ag = object[(NSString*)kSecAttrAccessGroup];
40 if ([itemClass isEqualToString: (NSString*)kSecClassKey] && [ag isEqualToString: @"com.apple.security.sos"]) {
41 return (NSString*)kSOSViewiCloudIdentity;
42 } else if ([ag isEqualToString: @"com.apple.cfnetwork"]) {
43 return (NSString*)kSOSViewAutofillPasswords;
44 } else if ([ag isEqualToString: @"com.apple.safari.credit-cards"]) {
45 return (NSString*)kSOSViewSafariCreditCards;
46 } else if ([itemClass isEqualToString: (NSString*)kSecClassGenericPassword]) {
47 if ([ag isEqualToString: @"apple"] &&
48 [object[(NSString*)kSecAttrService] isEqualToString: @"AirPort"]) {
49 return (NSString*)kSOSViewWiFi;
50 } else if ([ag isEqualToString: @"com.apple.sbd"]) {
51 return (NSString*)kSOSViewBackupBagV0;
53 return (NSString*)kSOSViewOtherSyncable; // (genp)
56 return (NSString*)kSOSViewOtherSyncable; // (inet || keys)
61 static inline NSNumber *now_msecs() {
62 return @(((long)[[NSDate date] timeIntervalSince1970] * 1000));
65 static NSString* cloudKitDeviceID() {
66 __block NSString* ret = nil;
68 if (!os_variant_has_internal_diagnostics("com.apple.security")) {
71 CKContainer *container = [CKContainer containerWithIdentifier:@"com.apple.security.keychain"];
72 dispatch_semaphore_t sem = dispatch_semaphore_create(0);
73 [container fetchCurrentDeviceIDWithCompletionHandler:^(NSString* deviceID, NSError* error) {
75 NSLog(@"failed to fetch CK deviceID: %@", error);
79 dispatch_semaphore_signal(sem);
81 dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
85 static void reportStats(unsigned expected_mismatches, unsigned real_mismatches, NSArray<NSDictionary*>* reportedMismatches) {
87 NSURLSessionConfiguration *defaultConfiguration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
88 NSURLSession *session = [NSURLSession sessionWithConfiguration:defaultConfiguration];
89 NSURL *endpoint = [NSURL URLWithString:@"https://xp.apple.com/report/2/xp_sear_keysync"];
90 NSMutableURLRequest *req = [[NSMutableURLRequest alloc] init];
92 req.HTTPMethod = @"POST";
93 NSMutableDictionary *dict = [@{
94 @"expectedMismatches": @(expected_mismatches),
95 @"realMismatches": @(real_mismatches),
96 @"eventTime": now_msecs(),
97 @"topic": @"xp_sear_keysync",
98 @"eventType": @"policy-dryrun",
100 NSDictionary *version = CFBridgingRelease(_CFCopySystemVersionDictionary());
101 NSString *build = version[(__bridge NSString *)_kCFSystemVersionBuildVersionKey];
103 dict[@"build"] = build;
105 NSLog(@"Unable to find out build version");
107 NSString *product = version[(__bridge NSString *)_kCFSystemVersionProductNameKey];
108 if (product != nil) {
109 dict[@"product"] = product;
111 NSLog(@"Unable to find out build product");
113 NSString* ckDeviceID = cloudKitDeviceID();
115 dict[@"SFAnalyticsDeviceID"] = ckDeviceID;
117 NSLog(@"Unable to fetch CK device ID");
120 dict[@"mismatches"] = reportedMismatches;
122 NSArray<NSDictionary*>* events = @[dict];
123 NSDictionary *wrapper = @{
124 @"postTime": @([[NSDate date] timeIntervalSince1970] * 1000),
127 NSData* data = [NSJSONSerialization dataWithJSONObject:wrapper options:0 error:&error];
128 if (data == nil || error != nil) {
129 NSLog(@"failed to encode data: %@", error);
132 NSLog(@"logging %@, %@", wrapper, data);
134 dispatch_semaphore_t sem = dispatch_semaphore_create(0);
135 NSURLSessionDataTask *task = [session dataTaskWithRequest:req completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
137 NSLog(@"splunk upload failed: %@", error);
140 NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*)response;
141 if (httpResp.statusCode != 200) {
142 NSLog(@"HTTP Post error: %@", httpResp);
144 NSLog(@"%@", httpResp);
146 size_t length = [data length];
147 char *buf = malloc(length);
148 [data getBytes:buf length:length];
149 NSLog(@"%.*s", (int)length, buf);
152 dispatch_semaphore_signal(sem);
155 dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
158 int policy_dryrun(int argc, char * const *argv) {
160 NSError* error = nil;
161 // From Swift.policy, policy v7
163 TPPolicyDocument *tpd = [TPPolicyDocument policyDocWithHash:@"SHA256:dL8Qujqzprhp6FdH5GzNMtPlnZtLWMwfiiF7aykr8WU="
164 data:[[NSData alloc] initWithBase64EncodedString:
165 @"CAcSDgoGaVBob25lEgRmdWxsEgwKBGlQYWQSBGZ1bGwSDAoEaVBvZBIEZnVsbBILCgNNYWMSBGZ1bGwSDAoEaU1hYxIEZnVsbBINCgdBcHBsZVRWEgJ0dhIOCgVXYXRjaBIFd2F0Y2gSFwoOQXVkaW9BY2Nlc3NvcnkSBWF1ZGlvGh4KBEhvbWUSBGZ1bGwSBXdhdGNoEgJ0dhIFYXVkaW8aJAoVUHJvdGVjdGVkQ2xvdWRTdG9yYWdlEgRmdWxsEgV3YXRjaBoYCglQYXNzd29yZHMSBGZ1bGwSBXdhdGNoGh8KEFNlY3VyZU9iamVjdFN5bmMSBGZ1bGwSBXdhdGNoGh4KBFdpRmkSBGZ1bGwSBXdhdGNoEgJ0dhIFYXVkaW8aGgoLQ3JlZGl0Q2FyZHMSBGZ1bGwSBXdhdGNoGhcKCEFwcGxlUGF5E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cAAISHgAEIhoCBHZ3aHQKEl5BY2Nlc3NvcnlQYWlyaW5nJBIaAAQiFgIEdndodAoOXk5hbm9SZWdpc3RyeSQSHAAEIhgCBHZ3aHQKEF5XYXRjaE1pZ3JhdGlvbiQSDURldmljZVBhaXJpbmcyDgoCAAYSCEJhY2tzdG9w" options:0]];
166 TPPolicy *policy = [tpd policyWithSecrets:@{} decrypter:nil error:&error];
168 NSLog(@"policy error: %@", error);
172 NSLog(@"policy is nil");
176 TPSyncingPolicy* syncingPolicy = [policy syncingPolicyForModel:@"iPhone"
177 syncUserControllableViews:TPPBPeerStableInfo_UserControllableViewStatus_UNKNOWN
179 if(syncingPolicy == nil || error != nil) {
180 NSLog(@"syncing policy is nil: %@", error);
184 unsigned real_mismatches = 0;
185 unsigned expected_mismatches = 0;
186 NSMutableArray<NSDictionary*>* reportedMismatches = [[NSMutableArray<NSDictionary*> alloc] init];
188 NSArray<NSString*>* keychainClasses = @[(id)kSecClassInternetPassword,
189 (id)kSecClassGenericPassword,
191 (id)kSecClassCertificate];
193 for(NSString* itemClass in keychainClasses) {
194 NSLog(@"itemClass: %@", itemClass);
195 NSDictionary *query = @{ (id)kSecMatchLimit : (id)kSecMatchLimitAll,
196 (id)kSecClass : (id)itemClass,
197 (id)kSecReturnAttributes : @YES,
198 (id)kSecAttrSynchronizable: @YES,
199 (id)kSecUseDataProtectionKeychain: @YES,
200 (id)kSecUseAuthenticationUI : (id)kSecUseAuthenticationUISkip,
205 OSStatus status = SecItemCopyMatching((CFDictionaryRef)query, (void*)&result);
207 if (status == errSecItemNotFound) {
208 NSLog(@"no items found matching: %@", query);
211 NSLog(@"SecItemCopyMatching(%@) failed: %d", query, (int)status);
216 if (![result isKindOfClass:[NSArray class]]) {
217 NSLog(@"expected NSArray result from SecItemCopyMatching");
221 for (id a in result) {
223 NSString *oldView = getSOSView(a, itemClass);
224 if (oldView != nil) {
225 NSLog(@"old: %@", oldView);
228 NSMutableDictionary* mutA = [a mutableCopy];
229 mutA[(id)kSecClass] = (id)itemClass;
231 NSString* newView = [syncingPolicy mapDictionaryToView:mutA];
232 if (newView != nil) {
233 NSLog(@"new: %@", newView);
235 if(oldView == nil ^ newView == nil) {
236 NSLog(@"real mismatch: old view (%@) != new view (%@)", oldView, newView);
238 [reportedMismatches addObject: a];
240 } else if (oldView && newView && ![oldView isEqualToString: newView]) {
241 if ([oldView hasPrefix:@"PCS-"] && [newView isEqualToString: @"ProtectedCloudStorage"]) {
242 NSLog(@"(expected PCS mismatch): old view (%@) != new view (%@)", oldView, newView);
243 ++expected_mismatches;
245 } else if([oldView isEqualToString:@"OtherSyncable"] && [newView isEqualToString: @"Applications"]) {
246 NSLog(@"(expected 3rd party mismatch): old view (%@) != new view (%@)", oldView, newView);
247 ++expected_mismatches;
249 } else if([oldView isEqualToString:@"OtherSyncable"] && [newView isEqualToString: @"Backstop"]) {
250 NSLog(@"(expected backstop mismatch): old view (%@) != new view (%@)", oldView, newView);
251 ++expected_mismatches;
253 } else if([newView isEqualToString:@"NotSynced"]) {
254 NSLog(@"(expected NotSynced mismatch): old view (%@) != new view (%@)", oldView, newView);
255 ++expected_mismatches;
257 } else if(([oldView isEqualToString:@"BackupBagV0"] || [oldView isEqualToString:@"iCloudIdentity"]) && [newView isEqualToString:@"SecureObjectSync"]) {
258 NSLog(@"(expected BackupBag - SecureObjectSync mismatch): old view (%@) != new view (%@)", oldView, newView);
259 ++expected_mismatches;
261 } else if(([oldView isEqualToString:@"AccessoryPairing"]
262 || [oldView isEqualToString:@"NanoRegistry"]
263 || [oldView isEqualToString:@"WatchMigration"]) && [newView isEqualToString:@"DevicePairing"]) {
264 NSLog(@"(expected DevicePairing mismatch): old view (%@) != new view (%@)", oldView, newView);
265 ++expected_mismatches;
268 NSLog(@"real mismatch: old view (%@) != new view (%@)", oldView, newView);
270 [reportedMismatches addObject: a];
276 NSLog(@"%d expected_mismatches", expected_mismatches);
277 NSLog(@"%d real_mismatches", real_mismatches);
278 reportStats(expected_mismatches, real_mismatches, reportedMismatches);