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/CKKSGroupOperation.h"
32 #import "keychain/ckks/CKKSLockStateTracker.h"
34 @interface CKKSLockStateTracker ()
35 @property bool isLocked;
36 @property dispatch_queue_t queue;
37 @property NSOperationQueue* operationQueue;
40 @implementation CKKSLockStateTracker
42 - (instancetype)init {
43 if((self = [super init])) {
44 _queue = dispatch_queue_create("lock-state-tracker", DISPATCH_QUEUE_SERIAL);
45 _operationQueue = [[NSOperationQueue alloc] init];
48 [self resetUnlockDependency];
50 __weak __typeof(self) weakSelf = self;
52 // If this is a live server, register with notify
53 if(!SecCKKSTestsEnabled()) {
55 notify_register_dispatch(kUserKeybagStateChangeNotification, &token, _queue, ^(int t) {
56 [weakSelf _onqueueRecheck];
60 dispatch_async(_queue, ^{
61 [weakSelf _onqueueRecheck];
67 -(NSString*)description {
68 return [NSString stringWithFormat: @"<CKKSLockStateTracker: %@>", self.isLocked ? @"locked" : @"unlocked"];
71 -(void)resetUnlockDependency {
72 if(self.unlockDependency == nil || ![self.unlockDependency isPending]) {
73 self.unlockDependency = [NSBlockOperation blockOperationWithBlock: ^{
74 secinfo("ckks", "Keybag unlocked");
76 self.unlockDependency.name = @"keybag-unlocked-dependency";
80 +(bool)queryAKSLocked {
81 CFErrorRef aksError = NULL;
84 if(!SecAKSGetIsLocked(&locked, &aksError)) {
85 secerror("ckks: error querying lock state: %@", aksError);
86 CFReleaseNull(aksError);
92 -(void)_onqueueRecheck {
93 dispatch_assert_queue(self.queue);
95 bool wasLocked = self.isLocked;
96 self.isLocked = [CKKSLockStateTracker queryAKSLocked];
98 if(wasLocked != self.isLocked) {
101 [self resetUnlockDependency];
103 [self.operationQueue addOperation: self.unlockDependency];
104 self.unlockDependency = nil;
110 dispatch_sync(self.queue, ^{
111 [self _onqueueRecheck];
115 -(bool)isLockedError:(NSError *)error {
116 return [error.domain isEqualToString:@"securityd"] && error.code == errSecInteractionNotAllowed;