2 * Copyright (c) 2016 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@
26 #import "keychain/ckks/OctagonAPSReceiver.h"
27 #import "keychain/ckks/CKKS.h"
28 #import "keychain/ckks/CKKSCondition.h"
29 #import "keychain/ckks/CKKSNearFutureScheduler.h"
30 #import "keychain/ckks/CKKSAnalytics.h"
31 #import "keychain/analytics/SecMetrics.h"
32 #import "keychain/analytics/SecEventMetric.h"
33 #import "keychain/ot/ObjCImprovements.h"
34 #import <CloudKit/CloudKit_Private.h>
35 #include <utilities/SecAKSWrappers.h>
36 #include <utilities/debugging.h>
38 @implementation CKRecordZoneNotification (CKKSPushTracing)
39 - (void)setCkksPushTracingEnabled:(BOOL)ckksPushTracingEnabled {
40 objc_setAssociatedObject(self, "ckksPushTracingEnabled", ckksPushTracingEnabled ? @YES : @NO, OBJC_ASSOCIATION_RETAIN);
43 - (BOOL)ckksPushTracingEnabled {
44 return !![objc_getAssociatedObject(self, "ckksPushTracingEnabled") boolValue];
47 - (void)setCkksPushTracingUUID:(NSString*)ckksPushTracingUUID {
48 objc_setAssociatedObject(self, "ckksPushTracingUUID", ckksPushTracingUUID, OBJC_ASSOCIATION_RETAIN);
51 - (NSString*)ckksPushTracingUUID {
52 return objc_getAssociatedObject(self, "ckksPushTracingUUID");
55 - (void)setCkksPushReceivedDate:(NSDate*)ckksPushReceivedDate {
56 objc_setAssociatedObject(self, "ckksPushReceivedDate", ckksPushReceivedDate, OBJC_ASSOCIATION_RETAIN);
59 - (NSDate*)ckksPushReceivedDate {
60 return objc_getAssociatedObject(self, "ckksPushReceivedDate");
65 @interface OctagonAPSReceiver()
67 @property CKKSNearFutureScheduler *clearStalePushNotifications;
69 // If we receive notifications for a record zone that hasn't been registered yet, send them a their updates when they register
70 @property NSMutableDictionary<NSString*, NSMutableSet<CKRecordZoneNotification*>*>* undeliveredUpdates;
72 // Same, but for cuttlefish containers (and only remember that a push was received; don't remember the pushes themselves)
73 @property NSMutableSet<NSString*>* undeliveredCuttlefishUpdates;
75 @property NSMapTable<NSString*, id<CKKSZoneUpdateReceiver>>* zoneMap;
76 @property NSMapTable<NSString*, id<OctagonCuttlefishUpdateReceiver>>* octagonContainerMap;
79 @implementation OctagonAPSReceiver
81 + (instancetype)receiverForEnvironment:(NSString *)environmentName
82 namedDelegatePort:(NSString*)namedDelegatePort
83 apsConnectionClass:(Class<OctagonAPSConnection>)apsConnectionClass
85 if(environmentName == nil) {
86 secnotice("octagonpush", "No push environment; not bringing up APS.");
90 @synchronized([self class]) {
91 NSMutableDictionary<NSString*, OctagonAPSReceiver*>* environmentMap = [self synchronizedGlobalEnvironmentMap];
93 OctagonAPSReceiver* recv = [environmentMap valueForKey: environmentName];
96 recv = [[OctagonAPSReceiver alloc] initWithEnvironmentName: environmentName namedDelegatePort:namedDelegatePort apsConnectionClass: apsConnectionClass];
97 [environmentMap setValue: recv forKey: environmentName];
104 + (void)resetGlobalEnviornmentMap
106 @synchronized (self) {
107 [self resettableSynchronizedGlobalEnvironmentMap:YES];
111 + (NSMutableDictionary<NSString*, OctagonAPSReceiver*>*)synchronizedGlobalEnvironmentMap
113 return [self resettableSynchronizedGlobalEnvironmentMap:NO];
116 + (NSMutableDictionary<NSString*, OctagonAPSReceiver*>*)resettableSynchronizedGlobalEnvironmentMap:(BOOL)reset
118 static NSMutableDictionary<NSString*, OctagonAPSReceiver*>* environmentMap = nil;
120 if(environmentMap == nil || reset) {
121 environmentMap = [[NSMutableDictionary alloc] init];
124 return environmentMap;
127 + (dispatch_queue_t)apsDeliveryQueue {
128 static dispatch_queue_t aps_dispatch_queue;
129 static dispatch_once_t onceToken;
130 dispatch_once(&onceToken, ^{
131 aps_dispatch_queue = dispatch_queue_create("aps-callback-queue", DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL);
133 return aps_dispatch_queue;
136 - (BOOL) haveStalePushes
138 __block BOOL haveStalePushes = NO;
139 dispatch_sync([OctagonAPSReceiver apsDeliveryQueue], ^{
140 haveStalePushes = (self.undeliveredUpdates.count || self.undeliveredCuttlefishUpdates.count);
142 return haveStalePushes;
145 - (NSSet<NSString*>*)cuttlefishPushTopics
147 NSString* cuttlefishTopic = [kCKPushTopicPrefix stringByAppendingString:@"com.apple.security.cuttlefish"];
149 // Currently cuttlefish pushes are sent to TPH. System XPC services can't properly register to be woken
150 // at push time, so receive them for it.
151 NSString* tphTopic = [kCKPushTopicPrefix stringByAppendingString:@"com.apple.TrustedPeersHelper"];
152 NSString* securitydTopic = [kCKPushTopicPrefix stringByAppendingString:@"com.apple.securityd"];
154 return [NSSet setWithArray:@[cuttlefishTopic, tphTopic, securitydTopic]];
157 - (instancetype)initWithEnvironmentName:(NSString*)environmentName
158 namedDelegatePort:(NSString*)namedDelegatePort
159 apsConnectionClass:(Class<OctagonAPSConnection>)apsConnectionClass
161 return [self initWithEnvironmentName:environmentName
162 namedDelegatePort:namedDelegatePort
163 apsConnectionClass:apsConnectionClass
164 stalePushTimeout:5*60*NSEC_PER_SEC];
167 - (instancetype)initWithEnvironmentName:(NSString*)environmentName
168 namedDelegatePort:(NSString*)namedDelegatePort
169 apsConnectionClass:(Class<OctagonAPSConnection>)apsConnectionClass
170 stalePushTimeout:(uint64_t)stalePushTimeout
172 if(self = [super init]) {
173 _apsConnectionClass = apsConnectionClass;
174 _apsConnection = NULL;
176 _undeliveredUpdates = [NSMutableDictionary dictionary];
177 _undeliveredCuttlefishUpdates = [[NSMutableSet alloc] init];
179 // APS might be slow. This doesn't need to happen immediately, so let it happen later.
181 dispatch_async([OctagonAPSReceiver apsDeliveryQueue], ^{
186 self.apsConnection = [[self.apsConnectionClass alloc] initWithEnvironmentName:environmentName namedDelegatePort:namedDelegatePort queue:[OctagonAPSReceiver apsDeliveryQueue]];
187 self.apsConnection.delegate = self;
189 // The following string should match: [[NSBundle mainBundle] bundleIdentifier]
190 NSString* ckksTopic = [kCKPushTopicPrefix stringByAppendingString:@"com.apple.securityd"];
192 NSArray* topics = [@[ckksTopic] arrayByAddingObjectsFromArray:[self cuttlefishPushTopics].allObjects];
193 [self.apsConnection setEnabledTopics:topics];
195 [self.apsConnection setDarkWakeTopics:topics];
199 _zoneMap = [NSMapTable strongToWeakObjectsMapTable];
200 _octagonContainerMap = [NSMapTable strongToWeakObjectsMapTable];
202 void (^clearPushBlock)(void) = ^{
203 dispatch_async([OctagonAPSReceiver apsDeliveryQueue], ^{
204 NSDictionary<NSString*, NSMutableSet<CKRecordZoneNotification*>*> *droppedUpdates;
210 droppedUpdates = self.undeliveredUpdates;
212 self.undeliveredUpdates = [NSMutableDictionary dictionary];
213 [self.undeliveredCuttlefishUpdates removeAllObjects];
215 [self reportDroppedPushes:droppedUpdates];
219 _clearStalePushNotifications = [[CKKSNearFutureScheduler alloc] initWithName: @"clearStalePushNotifications"
220 delay:stalePushTimeout
221 keepProcessAlive:false
222 dependencyDescriptionCode:CKKSResultDescriptionNone
223 block:clearPushBlock];
228 // Report that pushes we are dropping
229 - (void)reportDroppedPushes:(NSDictionary<NSString*, NSMutableSet<CKRecordZoneNotification*>*>*)notifications
231 bool hasBeenUnlocked = false;
232 CFErrorRef error = NULL;
235 * Let server know that device is not unlocked yet
238 (void)SecAKSGetHasBeenUnlocked(&hasBeenUnlocked, &error);
239 CFReleaseNull(error);
241 NSString *eventName = @"CKKS APNS Push Dropped";
242 if (!hasBeenUnlocked) {
243 eventName = @"CKKS APNS Push Dropped - never unlocked";
246 for (NSString *zone in notifications) {
247 for (CKRecordZoneNotification *notification in notifications[zone]) {
248 if (notification.ckksPushTracingEnabled) {
249 secnotice("apsnotification", "Submitting initial CKEventMetric due to notification %@", notification);
251 SecEventMetric *metric = [[SecEventMetric alloc] initWithEventName:@"APNSPushMetrics"];
252 metric[@"push_token_uuid"] = notification.ckksPushTracingUUID;
253 metric[@"push_received_date"] = notification.ckksPushReceivedDate;
255 metric[@"push_event_name"] = eventName;
257 [[SecMetrics managerObject] submitEvent:metric];
265 - (CKKSCondition*)registerReceiver:(id<CKKSZoneUpdateReceiver>)receiver forZoneID:(CKRecordZoneID *)zoneID {
266 CKKSCondition* finished = [[CKKSCondition alloc] init];
269 dispatch_async([OctagonAPSReceiver apsDeliveryQueue], ^{
272 secerror("ckks: received registration for released OctagonAPSReceiver");
276 [self.zoneMap setObject:receiver forKey: zoneID.zoneName];
278 NSMutableSet<CKRecordZoneNotification*>* currentPendingMessages = self.undeliveredUpdates[zoneID.zoneName];
279 [self.undeliveredUpdates removeObjectForKey:zoneID.zoneName];
281 for(CKRecordZoneNotification* message in currentPendingMessages.allObjects) {
282 // Now, send the receiver its notification!
283 secerror("ckks: sending stored push(%@) to newly-registered zone(%@): %@", message, zoneID.zoneName, receiver);
284 [receiver notifyZoneChange:message];
293 - (CKKSCondition*)registerCuttlefishReceiver:(id<OctagonCuttlefishUpdateReceiver>)receiver
294 forContainerName:(NSString*)containerName
296 CKKSCondition* finished = [[CKKSCondition alloc] init];
299 dispatch_async([OctagonAPSReceiver apsDeliveryQueue], ^{
302 secerror("octagon: received registration for released OctagonAPSReceiver");
306 [self.octagonContainerMap setObject:receiver forKey:containerName];
307 if([self.undeliveredCuttlefishUpdates containsObject:containerName]) {
308 [self.undeliveredCuttlefishUpdates removeObject:containerName];
310 // Now, send the receiver its fake notification!
311 secerror("octagon: sending fake push to newly-registered cuttlefish receiver(%@): %@", containerName, receiver);
312 [receiver notifyContainerChange:nil];
321 #pragma mark - APS Delegate callbacks
323 - (void)connection:(APSConnection *)connection didReceivePublicToken:(NSData *)publicToken {
325 secnotice("octagonpush", "OctagonAPSDelegate initiated: %@", connection);
328 - (void)connection:(APSConnection *)connection didReceiveToken:(NSData *)token forTopic:(NSString *)topic identifier:(NSString *)identifier {
329 secnotice("octagonpush", "Received per-topic push token \"%@\" for topic \"%@\" identifier \"%@\" on connection %@", token, topic, identifier, connection);
332 - (void)connection:(APSConnection *)connection didReceiveIncomingMessage:(APSIncomingMessage *)message {
333 secnotice("octagonpush", "OctagonAPSDelegate received a message(%@): %@ ", message.topic, message.userInfo);
335 // Report back through APS that we received a message
336 if(message.tracingEnabled) {
337 [connection confirmReceiptForMessage:message];
340 NSSet<NSString*>* cuttlefishTopics = [self cuttlefishPushTopics];
342 // Separate and handle cuttlefish notifications
343 if([cuttlefishTopics containsObject:message.topic] && [message.userInfo objectForKey:@"cf"]) {
344 NSDictionary* cfInfo = message.userInfo[@"cf"];
345 NSString* container = cfInfo[@"c"];
347 secnotice("octagonpush", "Received a cuttlefish push to container %@", container);
348 [[CKKSAnalytics logger] setDateProperty:[NSDate date] forKey:CKKSAnalyticsLastOctagonPush];
351 id<OctagonCuttlefishUpdateReceiver> receiver = [self.octagonContainerMap objectForKey:container];
354 [receiver notifyContainerChange:message];
356 secerror("octagonpush: received cuttlefish push for unregistered container: %@", container);
357 [self.undeliveredCuttlefishUpdates addObject:container];
358 [self.clearStalePushNotifications trigger];
361 // APS stripped the container. Send a push to all registered containers.
362 @synchronized(self.octagonContainerMap) {
363 for(id<OctagonCuttlefishUpdateReceiver> receiver in [self.octagonContainerMap objectEnumerator]) {
364 [receiver notifyContainerChange:nil];
372 CKNotification* notification = [CKNotification notificationFromRemoteNotificationDictionary:message.userInfo];
374 if(notification.notificationType == CKNotificationTypeRecordZone) {
375 CKRecordZoneNotification* rznotification = (CKRecordZoneNotification*) notification;
376 rznotification.ckksPushTracingEnabled = message.tracingEnabled;
377 rznotification.ckksPushTracingUUID = message.tracingUUID ? [[[NSUUID alloc] initWithUUIDBytes:message.tracingUUID.bytes] UUIDString] : nil;
378 rznotification.ckksPushReceivedDate = [NSDate date];
380 [[CKKSAnalytics logger] setDateProperty:[NSDate date] forKey:CKKSAnalyticsLastCKKSPush];
382 // Find receiever in map
383 id<CKKSZoneUpdateReceiver> recv = [self.zoneMap objectForKey:rznotification.recordZoneID.zoneName];
385 [recv notifyZoneChange:rznotification];
387 secerror("ckks: received push for unregistered zone: %@", rznotification);
388 if(rznotification.recordZoneID) {
389 NSMutableSet<CKRecordZoneNotification*>* currentPendingMessages = self.undeliveredUpdates[rznotification.recordZoneID.zoneName];
390 if(currentPendingMessages) {
391 [currentPendingMessages addObject:rznotification];
393 self.undeliveredUpdates[rznotification.recordZoneID.zoneName] = [NSMutableSet setWithObject:rznotification];
394 [self.clearStalePushNotifications trigger];
399 secerror("ckks: unexpected notification: %@", notification);