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@
24 #import "CKKSKeychainView.h"
25 #import "CKKSIncomingQueueOperation.h"
26 #import "CKKSIncomingQueueEntry.h"
27 #import "CKKSItemEncrypter.h"
28 #import "CKKSOutgoingQueueEntry.h"
30 #import "CKKSManifest.h"
31 #import "CKKSAnalytics.h"
32 #import "CKKSPowerCollection.h"
33 #import "keychain/ckks/CKKSCurrentItemPointer.h"
35 #include <securityd/SecItemServer.h>
36 #include <securityd/SecItemDb.h>
37 #include <Security/SecItemPriv.h>
39 #include <utilities/SecADWrapper.h>
43 @interface CKKSIncomingQueueOperation ()
44 @property bool newOutgoingEntries;
45 @property bool pendingClassAEntries;
46 @property bool missingKey;
49 @implementation CKKSIncomingQueueOperation
51 - (instancetype)init {
54 - (instancetype)initWithCKKSKeychainView:(CKKSKeychainView*)ckks errorOnClassAFailure:(bool)errorOnClassAFailure {
55 if(self = [super init]) {
58 // Can't process unless we have a reasonable key hierarchy.
59 if(ckks.keyStateReadyDependency) {
60 [self addDependency: ckks.keyStateReadyDependency];
63 [self addNullableDependency: ckks.holdIncomingQueueOperation];
65 _errorOnClassAFailure = errorOnClassAFailure;
66 _pendingClassAEntries = false;
68 [self linearDependencies:ckks.incomingQueueOperations];
70 if ([CKKSManifest shouldSyncManifests]) {
71 __weak __typeof(self) weakSelf = self;
72 __weak CKKSKeychainView* weakCKKS = ckks;
73 CKKSResultOperation* updateManifestOperation = [CKKSResultOperation operationWithBlock:^{
74 __strong __typeof(self) strongSelf = weakSelf;
75 __strong CKKSKeychainView* strongCKKS = weakCKKS;
76 __block NSError* error = nil;
77 if (!strongCKKS || !strongSelf) {
78 ckkserror("ckksincoming", strongCKKS, "update manifest operation fired for released object");
82 [strongCKKS dispatchSyncWithAccountKeys:^bool{
83 strongCKKS.latestManifest = [CKKSManifest latestTrustedManifestForZone:strongCKKS.zoneName error:&error];
85 strongSelf.error = error;
86 ckkserror("ckksincoming", strongCKKS, "failed to get latest manifest: %@", error);
94 updateManifestOperation.name = @"update-manifest-operation";
96 [ckks scheduleOperation:updateManifestOperation];
97 [self addSuccessDependency:updateManifestOperation];
103 - (bool)processNewCurrentItemPointers:(NSArray<CKKSCurrentItemPointer*>*)queueEntries withManifest:(CKKSManifest*)manifest egoManifest:(CKKSEgoManifest*)egoManifest
105 CKKSKeychainView* ckks = self.ckks;
107 NSError* error = nil;
108 for(CKKSCurrentItemPointer* p in queueEntries) {
110 if ([CKKSManifest shouldSyncManifests]) {
111 if (![manifest validateCurrentItem:p withError:&error]) {
112 ckkserror("ckksincoming", ckks, "Unable to validate current item pointer (%@) against manifest (%@)", p, manifest);
113 if ([CKKSManifest shouldEnforceManifests]) {
119 p.state = SecCKKSProcessedStateLocal;
121 [p saveToDatabase:&error];
122 ckksnotice("ckkspointer", ckks, "Saving new current item pointer: %@", p);
124 ckkserror("ckksincoming", ckks, "Error saving new current item pointer: %@ %@", error, p);
127 // Schedule a view change notification
128 [ckks.notifyViewChangedScheduler trigger];
132 if(queueEntries.count > 0) {
133 // Schedule a view change notification
134 [ckks.notifyViewChangedScheduler trigger];
137 return (error == nil);
140 - (bool)processQueueEntries:(NSArray<CKKSIncomingQueueEntry*>*)queueEntries withManifest:(CKKSManifest*)manifest egoManifest:(CKKSEgoManifest*)egoManifest
142 CKKSKeychainView* ckks = self.ckks;
144 NSMutableArray* newOrChangedRecords = [[NSMutableArray alloc] init];
145 NSMutableArray* deletedRecordIDs = [[NSMutableArray alloc] init];
147 for(id entry in queueEntries) {
150 ckksnotice("ckksincoming", ckks, "CKKSIncomingQueueOperation cancelled, quitting");
154 NSError* error = nil;
156 CKKSIncomingQueueEntry* iqe = (CKKSIncomingQueueEntry*) entry;
157 ckksnotice("ckksincoming", ckks, "ready to process an incoming queue entry: %@ %@ %@", iqe, iqe.uuid, iqe.action);
159 // Note that we currently unencrypt the item before deleting it, instead of just deleting it
160 // This finds the class, which is necessary for the deletion process. We could just try to delete
161 // across all classes, though...
162 NSMutableDictionary* attributes = [[CKKSItemEncrypter decryptItemToDictionary: iqe.item error:&error] mutableCopy];
163 if(!attributes || error) {
164 if([ckks.lockStateTracker isLockedError:error]) {
165 NSError* localerror = nil;
166 ckkserror("ckksincoming", ckks, "Keychain is locked; can't decrypt IQE %@", iqe);
167 CKKSKey* key = [CKKSKey tryFromDatabase:iqe.item.parentKeyUUID zoneID:ckks.zoneID error:&localerror];
168 if(localerror || ([key.keyclass isEqualToString:SecCKKSKeyClassA] && self.errorOnClassAFailure)) {
172 // If this isn't an error, make sure it gets processed later.
173 if([key.keyclass isEqualToString:SecCKKSKeyClassA] && !self.errorOnClassAFailure) {
174 self.pendingClassAEntries = true;
177 } else if ([error.domain isEqualToString:@"securityd"] && error.code == errSecItemNotFound) {
178 ckkserror("ckksincoming", ckks, "Coudn't find key in keychain; will attempt to poke key hierarchy: %@", error)
179 self.missingKey = true;
182 ckkserror("ckksincoming", ckks, "Couldn't decrypt IQE %@ for some reason: %@", iqe, error);
185 self.errorItemsProcessed += 1;
189 // Add the UUID (which isn't stored encrypted)
190 [attributes setValue: iqe.item.uuid forKey: (__bridge NSString*) kSecAttrUUID];
192 // Add the PCS plaintext fields, if they exist
193 if(iqe.item.plaintextPCSServiceIdentifier) {
194 [attributes setValue: iqe.item.plaintextPCSServiceIdentifier forKey: (__bridge NSString*) kSecAttrPCSPlaintextServiceIdentifier];
196 if(iqe.item.plaintextPCSPublicKey) {
197 [attributes setValue: iqe.item.plaintextPCSPublicKey forKey: (__bridge NSString*) kSecAttrPCSPlaintextPublicKey];
199 if(iqe.item.plaintextPCSPublicIdentity) {
200 [attributes setValue: iqe.item.plaintextPCSPublicIdentity forKey: (__bridge NSString*) kSecAttrPCSPlaintextPublicIdentity];
203 // This item is also synchronizable (by definition)
204 [attributes setValue: @(YES) forKey: (__bridge NSString*) kSecAttrSynchronizable];
206 NSString* classStr = [attributes objectForKey: (__bridge NSString*) kSecClass];
207 if(![classStr isKindOfClass: [NSString class]]) {
208 self.error = [NSError errorWithDomain:@"securityd"
209 code:errSecInternalError
210 userInfo:@{NSLocalizedDescriptionKey : [NSString stringWithFormat:@"Item did not have a reasonable class: %@", classStr]}];
211 ckkserror("ckksincoming", ckks, "Synced item seems wrong: %@", self.error);
212 self.errorItemsProcessed += 1;
216 const SecDbClass * classP = !classStr ? NULL : kc_class_with_name((__bridge CFStringRef) classStr);
219 ckkserror("ckksincoming", ckks, "unknown class in object: %@ %@", classStr, iqe);
220 iqe.state = SecCKKSStateError;
221 [iqe saveToDatabase:&error];
223 ckkserror("ckksincoming", ckks, "Couldn't save errored IQE to database: %@", error);
226 self.errorItemsProcessed += 1;
230 if([iqe.action isEqualToString: SecCKKSActionAdd] || [iqe.action isEqualToString: SecCKKSActionModify]) {
231 BOOL requireManifestValidation = [CKKSManifest shouldEnforceManifests];
232 BOOL manifestValidatesItem = [manifest validateItem:iqe.item withError:&error];
234 if (!requireManifestValidation || manifestValidatesItem) {
235 [self _onqueueHandleIQEChange: iqe attributes:attributes class:classP];
236 [newOrChangedRecords addObject:[iqe.item CKRecordWithZoneID:ckks.zoneID]];
239 ckkserror("ckksincoming", ckks, "could not validate incoming item against manifest with error: %@", error);
240 if (![self _onqueueUpdateIQE:iqe withState:SecCKKSStateUnauthenticated error:&error]) {
241 ckkserror("ckksincoming", ckks, "failed to save incoming item back to database in unauthenticated state with error: %@", error);
244 self.errorItemsProcessed += 1;
247 } else if ([iqe.action isEqualToString: SecCKKSActionDelete]) {
248 BOOL requireManifestValidation = [CKKSManifest shouldEnforceManifests];
249 BOOL manifestValidatesDelete = ![manifest itemUUIDExistsInManifest:iqe.uuid];
251 if (!requireManifestValidation || manifestValidatesDelete) {
252 // if the item does not exist in the latest manifest, we're good to delete it
253 [self _onqueueHandleIQEDelete: iqe class:classP];
254 [deletedRecordIDs addObject:[[CKRecordID alloc] initWithRecordName:iqe.uuid zoneID:ckks.zoneID]];
257 // if the item DOES exist in the manifest, we can't trust the deletion
258 ckkserror("ckksincoming", ckks, "could not validate incoming item deletion against manifest");
259 if (![self _onqueueUpdateIQE:iqe withState:SecCKKSStateUnauthenticated error:&error]) {
260 ckkserror("ckksincoming", ckks, "failed to save incoming item deletion back to database in unauthenticated state with error: %@", error);
262 self.errorItemsProcessed += 1;
270 if(newOrChangedRecords.count > 0 || deletedRecordIDs > 0) {
271 // Schedule a view change notification
272 [ckks.notifyViewChangedScheduler trigger];
275 if(self.missingKey) {
276 [ckks.pokeKeyStateMachineScheduler trigger];
279 if ([CKKSManifest shouldSyncManifests]) {
280 [egoManifest updateWithNewOrChangedRecords:newOrChangedRecords deletedRecordIDs:deletedRecordIDs];
285 - (bool)_onqueueUpdateIQE:(CKKSIncomingQueueEntry*)iqe withState:(NSString*)newState error:(NSError**)error
287 if (![iqe.state isEqualToString:newState]) {
288 NSMutableDictionary* oldWhereClause = iqe.whereClauseToFindSelf.mutableCopy;
289 oldWhereClause[@"state"] = iqe.state;
290 iqe.state = newState;
291 if ([iqe saveToDatabase:error]) {
292 if (![CKKSSQLDatabaseObject deleteFromTable:[iqe.class sqlTable] where:oldWhereClause connection:NULL error:error]) {
305 // Synchronous, on some thread. Get back on the CKKS queue for thread-safety.
306 CKKSKeychainView* ckks = self.ckks;
308 ckkserror("ckksincoming", ckks, "no CKKS object");
312 __weak __typeof(self) weakSelf = self;
313 self.completionBlock = ^(void) {
314 __strong __typeof(self) strongSelf = weakSelf;
316 ckkserror("ckksincoming", ckks, "received callback for released object");
320 CKKSAnalytics* logger = [CKKSAnalytics logger];
322 if (!strongSelf.error) {
323 [logger logSuccessForEvent:CKKSEventProcessIncomingQueueClassC inView:ckks];
324 if (!strongSelf.pendingClassAEntries) {
325 [logger logSuccessForEvent:CKKSEventProcessIncomingQueueClassA inView:ckks];
328 [logger logRecoverableError:strongSelf.error
329 forEvent:strongSelf.errorOnClassAFailure ? CKKSEventProcessIncomingQueueClassA : CKKSEventProcessIncomingQueueClassC
331 withAttributes:NULL];
335 __block bool errored = false;
336 [ckks dispatchSync: ^bool{
338 ckksnotice("ckksincoming", ckks, "CKKSIncomingQueueOperation cancelled, quitting");
341 ckks.lastIncomingQueueOperation = self;
343 ckksnotice("ckksincoming", ckks, "Processing incoming queue");
345 if ([CKKSManifest shouldSyncManifests]) {
346 if (!ckks.latestManifest) {
347 // Until we can make manifests in our unit tests, we can't abort here
348 ckkserror("ckksincoming", ckks, "no manifest in ckks");
350 if (!ckks.egoManifest) {
351 ckkserror("ckksincoming", ckks, "no ego manifest in ckks");
355 bool ok = true; // Should commit transaction?
356 __block NSError* error = nil;
358 if ([CKKSManifest shouldSyncManifests]) {
359 NSInteger unauthenticatedItemCount = [CKKSIncomingQueueEntry countByState:SecCKKSStateUnauthenticated zone:ckks.zoneID error:&error];
360 if (error || unauthenticatedItemCount < 0) {
361 ckkserror("ckksincoming", ckks, "Error fetching incoming queue state counts: %@", error);
366 // take any existing unauthenticated entries and put them back in the new state
367 NSArray<CKKSIncomingQueueEntry*>* unauthenticatedEntries = nil;
368 NSString* lastMaxUUID = nil;
369 NSInteger numEntriesProcessed = 0;
370 while (numEntriesProcessed < unauthenticatedItemCount && (unauthenticatedEntries == nil || unauthenticatedEntries.count == SecCKKSIncomingQueueItemsAtOnce)) {
372 ckksnotice("ckksincoming", ckks, "CKKSIncomingQueueOperation cancelled, quitting");
376 unauthenticatedEntries = [CKKSIncomingQueueEntry fetch:SecCKKSIncomingQueueItemsAtOnce
377 startingAtUUID:lastMaxUUID
378 state:SecCKKSStateUnauthenticated
382 ckkserror("ckksincoming", ckks, "Error fetching unauthenticated queue records: %@", error);
387 if (unauthenticatedEntries.count == 0) {
388 ckksinfo("ckksincoming", ckks, "No unauthenticated entries in incoming queue to process");
392 for (CKKSIncomingQueueEntry* unauthenticatedEntry in unauthenticatedEntries) {
393 if (![self _onqueueUpdateIQE:unauthenticatedEntry withState:SecCKKSStateNew error:&error]) {
394 ckkserror("ckksincoming", ckks, "Error saving unauthenticated entry back to new state: %@", error);
399 lastMaxUUID = ([lastMaxUUID compare:unauthenticatedEntry.uuid] == NSOrderedDescending) ? lastMaxUUID : unauthenticatedEntry.uuid;
408 ckksnotice("ckksincoming", ckks, "Early-exiting from IncomingQueueOperation");
412 // Now for the tricky bit: take and drop the account queue for each batch of queue entries
413 // This is for peak memory concerns, but also to allow keychain API clients to make changes while we're processing many items
414 // Note that IncomingQueueOperations are no longer transactional: they can partially succeed. This might make them harder to reason about.
415 __block NSUInteger lastCount = SecCKKSIncomingQueueItemsAtOnce;
416 __block NSString* lastMaxUUID = nil;
418 while(lastCount == SecCKKSIncomingQueueItemsAtOnce) {
419 [ckks dispatchSync: ^bool{
420 NSArray<CKKSIncomingQueueEntry*> * queueEntries = nil;
422 ckksnotice("ckksincoming", ckks, "CKKSIncomingQueueOperation cancelled, quitting");
427 NSError* error = nil;
429 queueEntries = [CKKSIncomingQueueEntry fetch: SecCKKSIncomingQueueItemsAtOnce
430 startingAtUUID:lastMaxUUID
431 state:SecCKKSStateNew
436 ckkserror("ckksincoming", ckks, "Error fetching incoming queue records: %@", error);
442 lastCount = queueEntries.count;
444 if([queueEntries count] == 0) {
445 // Nothing to do! exit.
446 ckksnotice("ckksincoming", ckks, "Nothing in incoming queue to process");
450 [CKKSPowerCollection CKKSPowerEvent:kCKKSPowerEventOutgoingQueue zone:ckks.zoneName count:[queueEntries count]];
452 if (![self processQueueEntries:queueEntries withManifest:ckks.latestManifest egoManifest:ckks.egoManifest]) {
453 ckksnotice("ckksincoming", ckks, "processQueueEntries didn't complete successfully");
458 // Find the highest UUID for the next fetch.
459 for(CKKSIncomingQueueEntry* iqe in queueEntries) {
460 lastMaxUUID = ([lastMaxUUID compare:iqe.uuid] == NSOrderedDescending) ? lastMaxUUID : iqe.uuid;
466 ckksnotice("ckksincoming", ckks, "Early-exiting from IncomingQueueOperation");
471 ckksnotice("ckksincoming", ckks, "Processed %lu items in incoming queue (%lu errors)", (unsigned long)self.successfulItemsProcessed, (unsigned long)self.errorItemsProcessed);
473 [ckks dispatchSync: ^bool{
474 NSError* error = nil;
476 NSArray<CKKSCurrentItemPointer*>* newCIPs = [CKKSCurrentItemPointer remoteItemPointers:ckks.zoneID error:&error];
477 if(error || !newCIPs) {
478 ckkserror("ckksincoming", ckks, "Could not load remote item pointers: %@", error);
480 if (![self processNewCurrentItemPointers:newCIPs withManifest:ckks.latestManifest egoManifest:ckks.egoManifest]) {
483 ckksnotice("ckksincoming", ckks, "Processed %lu items in CIP queue", (unsigned long)newCIPs.count);
486 if(self.newOutgoingEntries) {
487 // No operation group
488 [ckks processOutgoingQueue:nil];
491 if(self.pendingClassAEntries) {
492 [self.ckks processIncomingQueueAfterNextUnlock];
499 - (void)_onqueueHandleIQEChange: (CKKSIncomingQueueEntry*) iqe attributes:(NSDictionary*)attributes class:(const SecDbClass *)classP {
500 CKKSKeychainView* ckks = self.ckks;
502 ckkserror("ckksincoming", ckks, "no CKKS object");
506 dispatch_assert_queue(ckks.queue);
509 __block CFErrorRef cferror = NULL;
510 __block NSError* error = NULL;
512 SecDbItemRef item = SecDbItemCreateWithAttributes(NULL, classP, (__bridge CFDictionaryRef) attributes, KEYBAG_DEVICE, &cferror);
514 __block NSDate* moddate = (__bridge NSDate*) CFDictionaryGetValue(item->attributes, kSecAttrModificationDate);
516 ok = kc_with_dbt(true, &cferror, ^(SecDbConnectionRef dbt){
517 bool replaceok = SecDbItemInsertOrReplace(item, dbt, &cferror, ^(SecDbItemRef olditem, SecDbItemRef *replace) {
518 // If the UUIDs do not match, then select the item with the 'lower' UUID, and tell CKKS to
519 // delete the item with the 'higher' UUID.
520 // Otherwise, the cloud wins.
522 SecADAddValueForScalarKey((__bridge CFStringRef) SecCKKSAggdPrimaryKeyConflict,1);
524 // Note that SecDbItemInsertOrReplace CFReleases any replace pointer it's given, so, be careful
526 if(!CFDictionaryContainsKey(olditem->attributes, kSecAttrUUID)) {
527 // No UUID -> no good.
528 ckksnotice("ckksincoming", ckks, "Replacing item (it doesn't have a UUID) for %@", iqe.uuid);
530 *replace = CFRetainSafe(item);
535 CFStringRef itemUUID = CFDictionaryGetValue(item->attributes, kSecAttrUUID);
536 CFStringRef olditemUUID = CFDictionaryGetValue(olditem->attributes, kSecAttrUUID);
538 CFComparisonResult compare = CFStringCompare(itemUUID, olditemUUID, 0);
539 CKKSOutgoingQueueEntry* oqe = nil;
541 case kCFCompareLessThan:
542 // item wins; delete olditem
543 ckksnotice("ckksincoming", ckks, "Primary key conflict; replacing %@ with CK item %@", olditem, item);
545 *replace = CFRetainSafe(item);
546 moddate = (__bridge NSDate*) CFDictionaryGetValue(item->attributes, kSecAttrModificationDate);
549 oqe = [CKKSOutgoingQueueEntry withItem:olditem action:SecCKKSActionDelete ckks:ckks error:&error];
550 [oqe saveToDatabase: &error];
551 self.newOutgoingEntries = true;
553 case kCFCompareGreaterThan:
554 // olditem wins; don't change olditem; delete item
555 ckksnotice("ckksincoming", ckks, "Primary key conflict; dropping CK item %@", item);
557 oqe = [CKKSOutgoingQueueEntry withItem:item action:SecCKKSActionDelete ckks:ckks error:&error];
558 [oqe saveToDatabase: &error];
559 self.newOutgoingEntries = true;
563 case kCFCompareEqualTo:
564 // remote item wins; this is the normal update case
565 ckksnotice("ckksincoming", ckks, "Primary key conflict; replacing %@ with CK item %@", olditem, item);
567 *replace = CFRetainSafe(item);
568 moddate = (__bridge NSDate*) CFDictionaryGetValue(item->attributes, kSecAttrModificationDate);
574 // SecDbItemInsertOrReplace returns an error even when it succeeds.
575 if(!replaceok && SecErrorIsSqliteDuplicateItemError(cferror)) {
576 CFReleaseNull(cferror);
585 ckkserror("ckksincoming", ckks, "couldn't process item from IncomingQueue: %@", cferror);
586 SecTranslateError(&error, cferror);
589 iqe.state = SecCKKSStateError;
590 [iqe saveToDatabase:&error];
592 ckkserror("ckksincoming", ckks, "Couldn't save errored IQE to database: %@", error);
599 ckkserror("ckksincoming", ckks, "Couldn't handle IQE, but why?: %@", error);
605 ckksinfo("ckksincoming", ckks, "Correctly processed an IQE; deleting");
606 [iqe deleteFromDatabase: &error];
609 ckkserror("ckksincoming", ckks, "couldn't delete CKKSIncomingQueueEntry: %@", error);
611 self.errorItemsProcessed += 1;
613 self.successfulItemsProcessed += 1;
617 // Log the number of seconds it took to propagate this change
618 uint64_t secondsDelay = (uint64_t) ([[NSDate date] timeIntervalSinceDate:moddate]);
619 SecADClientPushValueForDistributionKey((__bridge CFStringRef) SecCKKSAggdPropagationDelay, secondsDelay);
623 ckksnotice("ckksincoming", ckks, "IQE not correctly processed, but why? %@ %@", error, cferror);
626 iqe.state = SecCKKSStateError;
627 [iqe saveToDatabase:&error];
629 ckkserror("ckksincoming", ckks, "Couldn't save errored IQE to database: %@", error);
633 self.errorItemsProcessed += 1;
637 - (void)_onqueueHandleIQEDelete: (CKKSIncomingQueueEntry*) iqe class:(const SecDbClass *)classP {
638 CKKSKeychainView* ckks = self.ckks;
640 ckkserror("ckksincoming", ckks, "no CKKS object");
644 dispatch_assert_queue(ckks.queue);
647 __block CFErrorRef cferror = NULL;
648 NSError* error = NULL;
649 NSDictionary* queryAttributes = @{(__bridge NSString*) kSecClass: (__bridge NSString*) classP->name,
650 (__bridge NSString*) kSecAttrUUID: iqe.uuid,
651 (__bridge NSString*) kSecAttrSyncViewHint: ckks.zoneID.zoneName,
652 (__bridge NSString*) kSecAttrSynchronizable: @(YES)};
653 ckksnotice("ckksincoming", ckks, "trying to delete with query: %@", queryAttributes);
654 Query *q = query_create_with_limit( (__bridge CFDictionaryRef) queryAttributes, NULL, kSecMatchUnlimited, &cferror);
658 ckkserror("ckksincoming", ckks, "couldn't create query: %@", cferror);
659 SecTranslateError(&error, cferror);
664 ok = kc_with_dbt(true, &cferror, ^(SecDbConnectionRef dbt) {
665 return s3dl_query_delete(dbt, q, NULL, &cferror);
669 if(CFErrorGetCode(cferror) == errSecItemNotFound) {
670 ckkserror("ckksincoming", ckks, "couldn't delete item (as it's already gone); this is okay: %@", cferror);
672 CFReleaseNull(cferror);
674 ckkserror("ckksincoming", ckks, "couldn't delete item: %@", cferror);
675 SecTranslateError(&error, cferror);
677 query_destroy(q, NULL);
683 ok = query_notify_and_destroy(q, ok, &cferror);
686 ckkserror("ckksincoming", ckks, "couldn't delete query: %@", cferror);
687 SecTranslateError(&error, cferror);
693 ckksnotice("ckksincoming", ckks, "Correctly processed an IQE; deleting");
694 [iqe deleteFromDatabase: &error];
697 ckkserror("ckksincoming", ckks, "couldn't delete CKKSIncomingQueueEntry: %@", error);
699 self.errorItemsProcessed += 1;
701 self.successfulItemsProcessed += 1;
704 ckkserror("ckksincoming", ckks, "IQE not correctly processed, but why? %@ %@", error, cferror);
706 self.errorItemsProcessed += 1;