2 * Copyright (c) 2017 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@
27 #include <dispatch/dispatch.h>
28 #include <utilities/SecAKSWrappers.h>
30 #import "keychain/ckks/CKKS.h"
31 #import "keychain/ckks/CKKSResultOperation.h"
32 #import "keychain/ckks/CKKSGroupOperation.h"
33 #import "keychain/ckks/CKKSLockStateTracker.h"
34 #import "keychain/ot/ObjCImprovements.h"
36 @interface CKKSLockStateTracker ()
37 @property bool queueIsLocked;
38 @property dispatch_queue_t queue;
39 @property NSOperationQueue* operationQueue;
40 @property NSHashTable<id<CKKSLockStateNotification>> *observers;
41 @property (assign) int notify_token;
43 @property (nullable) NSDate* lastUnlockedTime;
47 @implementation CKKSLockStateTracker
49 - (instancetype)init {
50 if((self = [super init])) {
51 _queue = dispatch_queue_create("lock-state-tracker", DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL);
52 _operationQueue = [[NSOperationQueue alloc] init];
54 _notify_token = NOTIFY_TOKEN_INVALID;
55 _queueIsLocked = true;
56 _observers = [NSHashTable weakObjectsHashTable];
57 [self resetUnlockDependency];
61 // If this is a live server, register with notify
62 if(!SecCKKSTestsEnabled()) {
63 notify_register_dispatch(kUserKeybagStateChangeNotification, &_notify_token, _queue, ^(int t) {
65 [self _onqueueRecheck];
69 dispatch_async(_queue, ^{
71 [self _onqueueRecheck];
78 if (_notify_token != NOTIFY_TOKEN_INVALID) {
79 notify_cancel(_notify_token);
84 // if checking close after launch ``isLocked'' needs to be blocked on the initial fetch operation
86 dispatch_sync(_queue, ^{
87 locked = self->_queueIsLocked;
92 - (NSDate*)lastUnlockTime {
93 // If unlocked, the last unlock time is now. Otherwise, used the cached value.
94 __block NSDate* date = nil;
95 dispatch_sync(self.queue, ^{
96 if(self.queueIsLocked) {
97 date = self.lastUnlockedTime;
100 self.lastUnlockedTime = date;
106 -(NSString*)description {
107 bool isLocked = self.isLocked;
108 return [NSString stringWithFormat: @"<CKKSLockStateTracker: %@ last:%@>",
109 isLocked ? @"locked" : @"unlocked",
110 isLocked ? self.lastUnlockedTime : @"now"];
113 -(void)resetUnlockDependency {
114 if(self.unlockDependency == nil || ![self.unlockDependency isPending]) {
115 CKKSResultOperation* op = [CKKSResultOperation named:@"keybag-unlocked-dependency" withBlock: ^{
116 ckksinfo_global("ckks", "Keybag unlocked");
118 op.descriptionErrorCode = CKKSResultDescriptionPendingUnlock;
119 self.unlockDependency = op;
123 +(bool)queryAKSLocked {
124 CFErrorRef aksError = NULL;
127 if(!SecAKSGetIsLocked(&locked, &aksError)) {
128 ckkserror_global("ckks", "error querying lock state: %@", aksError);
129 CFReleaseNull(aksError);
135 -(void)_onqueueRecheck {
136 dispatch_assert_queue(self.queue);
138 static bool first = true;
139 bool wasLocked = self.queueIsLocked;
140 self.queueIsLocked = [CKKSLockStateTracker queryAKSLocked];
142 if(wasLocked != self.queueIsLocked || first) {
144 if(self.queueIsLocked) {
146 [self resetUnlockDependency];
149 self.lastUnlockedTime = [NSDate date];
152 [self.operationQueue addOperation: self.unlockDependency];
153 self.unlockDependency = nil;
154 self.lastUnlockedTime = [NSDate date];
157 bool isUnlocked = (self.queueIsLocked == false);
158 for (id<CKKSLockStateNotification> observer in _observers) {
159 __strong typeof(observer) strongObserver = observer;
160 if (strongObserver == nil) {
163 dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^{
164 [strongObserver lockStateChangeNotification:isUnlocked];
171 dispatch_sync(self.queue, ^{
172 [self _onqueueRecheck];
176 - (bool)lockedError:(NSError *)error
178 bool isLockedError = error.code == errSecInteractionNotAllowed &&
179 ([error.domain isEqualToString:@"securityd"] || [error.domain isEqualToString:(__bridge NSString*)kSecErrorDomain]);
180 return isLockedError;
183 - (bool)checkErrorChainForLockState:(NSError*)error
185 while(error != nil) {
186 if([self lockedError:error]) {
190 error = error.userInfo[NSUnderlyingErrorKey];
196 -(bool)isLockedError:(NSError *)error {
197 bool isLockedError = [self checkErrorChainForLockState:error];
200 * If we are locked, and the the current lock state track disagree, lets double check
201 * if we are actually locked since we might have missed a lock state notification,
202 * and cause spinning to happen.
204 * We don't update the local variable, since the error code was a locked error.
207 dispatch_sync(self.queue, ^{
208 if (self.queueIsLocked == false) {
209 [self _onqueueRecheck];
213 return isLockedError;
216 -(void)addLockStateObserver:(id<CKKSLockStateNotification>)object
218 dispatch_async(self.queue, ^{
219 [self->_observers addObject:object];
220 bool isUnlocked = (self.queueIsLocked == false);
221 dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^{
222 [object lockStateChangeNotification:isUnlocked];
227 + (CKKSLockStateTracker*)globalTracker
229 static CKKSLockStateTracker* tracker;
230 static dispatch_once_t onceToken;
231 dispatch_once(&onceToken, ^{
232 tracker = [[CKKSLockStateTracker alloc] init];