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"
35 @interface CKKSLockStateTracker ()
36 @property (readwrite) bool isLocked;
37 @property dispatch_queue_t queue;
38 @property NSOperationQueue* operationQueue;
39 @property NSHashTable<id<CKKSLockStateNotification>> *observers;
41 @property (nullable) NSDate* lastUnlockedTime;
45 @implementation CKKSLockStateTracker
47 - (instancetype)init {
48 if((self = [super init])) {
49 _queue = dispatch_queue_create("lock-state-tracker", DISPATCH_QUEUE_SERIAL);
50 _operationQueue = [[NSOperationQueue alloc] init];
53 _observers = [NSHashTable weakObjectsHashTable];
54 [self resetUnlockDependency];
56 __weak __typeof(self) weakSelf = self;
58 // If this is a live server, register with notify
59 if(!SecCKKSTestsEnabled()) {
61 notify_register_dispatch(kUserKeybagStateChangeNotification, &token, _queue, ^(int t) {
62 [weakSelf _onqueueRecheck];
66 dispatch_async(_queue, ^{
67 [weakSelf _onqueueRecheck];
73 - (NSDate*)lastUnlockTime {
74 // If unlocked, the last unlock time is now. Otherwise, used the cached value.
75 __block NSDate* date = nil;
76 dispatch_sync(self.queue, ^{
78 date = self.lastUnlockedTime;
81 self.lastUnlockedTime = date;
87 -(NSString*)description {
88 return [NSString stringWithFormat: @"<CKKSLockStateTracker: %@ last:%@>",
89 self.isLocked ? @"locked" : @"unlocked",
90 self.isLocked ? self.lastUnlockedTime : @"now"];
93 -(void)resetUnlockDependency {
94 if(self.unlockDependency == nil || ![self.unlockDependency isPending]) {
95 CKKSResultOperation* op = [CKKSResultOperation named:@"keybag-unlocked-dependency" withBlock: ^{
96 secinfo("ckks", "Keybag unlocked");
98 op.descriptionErrorCode = CKKSResultDescriptionPendingUnlock;
99 self.unlockDependency = op;
103 +(bool)queryAKSLocked {
104 CFErrorRef aksError = NULL;
107 if(!SecAKSGetIsLocked(&locked, &aksError)) {
108 secerror("ckks: error querying lock state: %@", aksError);
109 CFReleaseNull(aksError);
115 -(void)_onqueueRecheck {
116 dispatch_assert_queue(self.queue);
118 static bool first = true;
119 bool wasLocked = self.isLocked;
120 self.isLocked = [CKKSLockStateTracker queryAKSLocked];
122 if(wasLocked != self.isLocked || first) {
126 [self resetUnlockDependency];
129 self.lastUnlockedTime = [NSDate date];
132 [self.operationQueue addOperation: self.unlockDependency];
133 self.unlockDependency = nil;
134 self.lastUnlockedTime = [NSDate date];
137 bool isUnlocked = (self.isLocked == false);
138 for (id<CKKSLockStateNotification> observer in _observers) {
139 __strong typeof(observer) strongObserver = observer;
140 if (strongObserver == NULL)
142 dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^{
143 [strongObserver lockStateChangeNotification:isUnlocked];
150 dispatch_sync(self.queue, ^{
151 [self _onqueueRecheck];
155 -(bool)isLockedError:(NSError *)error {
156 return ([error.domain isEqualToString:@"securityd"] || [error.domain isEqualToString:(__bridge NSString*)kSecErrorDomain])
157 && error.code == errSecInteractionNotAllowed;
160 -(void)addLockStateObserver:(id<CKKSLockStateNotification>)object
162 dispatch_async(self.queue, ^{
163 [self->_observers addObject:object];
164 bool isUnlocked = (self.isLocked == false);
165 dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^{
166 [object lockStateChangeNotification:isUnlocked];