5 // Created by Mitch Adler on 6/5/13.
6 // Copyright (c) 2013 Apple Inc. All rights reserved.
9 #ifndef _SECAKSWRAPPERS_H_
10 #define _SECAKSWRAPPERS_H_
12 #include <utilities/SecCFError.h>
13 #include <AssertMacros.h>
14 #include <dispatch/dispatch.h>
16 #if TARGET_IPHONE_SIMULATOR
18 #include <IOKit/IOReturn.h>
20 // Make the compiler happy so this will compile.
21 #define device_keybag_handle 0
22 #define session_keybag_handle 0
25 keybag_state_unlocked
= 0,
26 keybag_state_locked
= 1 << 0,
27 keybag_state_no_pin
= 1 << 1,
28 keybag_state_been_unlocked
= 1 << 2,
30 typedef uint32_t keybag_state_t
;
31 typedef int32_t keybag_handle_t
;
33 static kern_return_t
aks_get_lock_state(keybag_handle_t handle
, keybag_state_t
*state
) {
34 if (state
) *state
= keybag_state_no_pin
& keybag_state_been_unlocked
;
35 return kIOReturnSuccess
;
45 // MARK: User lock state
49 user_keybag_handle
= TARGET_OS_EMBEDDED
? device_keybag_handle
: session_keybag_handle
,
52 extern const char * const kUserKeybagStateChangeNotification
;
54 static inline bool SecAKSGetLockedState(keybag_state_t
*state
, CFErrorRef
* error
)
56 kern_return_t status
= aks_get_lock_state(user_keybag_handle
, state
);
58 if (kIOReturnSuccess
!= status
) {
59 SecCFCreateError(status
, CFSTR("com.apple.kern_return_t"), CFSTR("Kern return error"), NULL
, error
);
66 // returns true if any of the bits in bits is set in the current state of the user bag
67 static inline bool SecAKSLockedAnyStateBitIsSet(bool* isSet
, keybag_state_t bits
, CFErrorRef
* error
)
70 bool success
= SecAKSGetLockedState(&state
, error
);
72 require_quiet(success
, exit
);
75 *isSet
= (state
& bits
);
82 static inline bool SecAKSGetIsLocked(bool* isLocked
, CFErrorRef
* error
)
84 return SecAKSLockedAnyStateBitIsSet(isLocked
, keybag_state_locked
, error
);
87 static inline bool SecAKSGetIsUnlocked(bool* isUnlocked
, CFErrorRef
* error
)
89 bool isLocked
= false;
90 bool success
= SecAKSGetIsLocked(&isLocked
, error
);
92 if (success
&& isUnlocked
)
93 *isUnlocked
= !isLocked
;
98 static inline bool SecAKSGetHasBeenUnlocked(bool* hasBeenUnlocked
, CFErrorRef
* error
)
100 return SecAKSLockedAnyStateBitIsSet(hasBeenUnlocked
, keybag_state_been_unlocked
, error
);
103 bool SecAKSDoWhileUserBagLocked(CFErrorRef
*error
, dispatch_block_t action
);