2 * Copyright (c) 2018 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@
25 #import <libaks_ref_key.h>
26 #import <MobileKeyBag/MobileKeyBag.h>
27 #import <CryptoTokenKit/CryptoTokenKit.h>
29 #import <coreauthd_spi.h>
31 #import <LocalAuthentication/LAPublicDefines.h>
32 #import <LocalAuthentication/LAPrivateDefines.h>
33 #import <LocalAuthentication/LACFSupport.h>
34 #import <SecurityFoundation/SFEncryptionOperation.h>
37 @implementation SecMockAKS
39 + (bool)isLocked:(keyclass_t)key_class
49 + (bool)useGenerationCount
57 aks_load_bag(const void * data, int length, keybag_handle_t* handle)
63 kern_return_t aks_unlock_bag(keybag_handle_t handle, const void * passcode, int length)
69 aks_create_bag(const void * passcode, int length, keybag_type_t type, keybag_handle_t* handle)
76 aks_unload_bag(keybag_handle_t handle)
82 aks_save_bag(keybag_handle_t handle, void ** data, int * length)
88 aks_get_system(keybag_handle_t special_handle, keybag_handle_t *handle)
94 //static uint8_t staticAKSKey[32] = "1234567890123456789012";
99 aks_wrap_key(const void * key, int key_size, keyclass_t key_class, keybag_handle_t handle, void * wrapped_key, int * wrapped_key_size_inout, keyclass_t * class_out)
101 if ([SecMockAKS isLocked:key_class]) {
102 return kIOReturnNotPermitted;
104 if ([SecMockAKS isSEPDown]) {
105 return kIOReturnBusy;
108 *class_out = key_class;
109 if ([SecMockAKS useGenerationCount]) {
110 *class_out |= (key_class_last + 1);
113 if (key_size + PADDINGSIZE > *wrapped_key_size_inout) {
116 *wrapped_key_size_inout = key_size + PADDINGSIZE;
117 memcpy(wrapped_key, key, key_size);
118 memset(((uint8_t *)wrapped_key) + key_size, 0xff, PADDINGSIZE);
123 aks_unwrap_key(const void * wrapped_key, int wrapped_key_size, keyclass_t key_class, keybag_handle_t handle, void * key, int * key_size_inout)
125 if ([SecMockAKS isLocked:key_class]) {
126 return kIOReturnNotPermitted;
128 if ([SecMockAKS isSEPDown]) {
129 return kIOReturnBusy;
132 if (wrapped_key_size < 8) {
135 static const char expected_padding[PADDINGSIZE] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
136 if (memcmp(((const uint8_t *)wrapped_key) + (wrapped_key_size - PADDINGSIZE), expected_padding, PADDINGSIZE) != 0) {
139 if (*key_size_inout < wrapped_key_size - PADDINGSIZE) {
142 *key_size_inout = wrapped_key_size - PADDINGSIZE;
143 memcpy(key, wrapped_key, *key_size_inout);
149 aks_ref_key_create(keybag_handle_t handle, keyclass_t cls, aks_key_type_t type, const uint8_t *params, size_t params_len, aks_ref_key_t *ot)
155 aks_ref_key_encrypt(aks_ref_key_t handle,
156 const uint8_t *der_params, size_t der_params_len,
157 const void * data, size_t data_len,
158 void ** out_der, size_t * out_der_len)
164 aks_ref_key_decrypt(aks_ref_key_t handle,
165 const uint8_t *der_params, size_t der_params_len,
166 const void * data, size_t data_len,
167 void ** out_der, size_t * out_der_len)
173 aks_ref_key_delete(aks_ref_key_t handle, const uint8_t *der_params, size_t der_params_len)
179 aks_operation_optional_params(const uint8_t * access_groups, size_t access_groups_len, const uint8_t * external_data, size_t external_data_len, const void * acm_handle, int acm_handle_len, void ** out_der, size_t * out_der_len)
184 int aks_ref_key_create_with_blob(keybag_handle_t refkey, const uint8_t *ref_key_blob, size_t ref_key_blob_len, aks_ref_key_t* handle)
190 const uint8_t * aks_ref_key_get_blob(aks_ref_key_t refkey, size_t *out_blob_len)
193 return (const uint8_t *)"20";
196 aks_ref_key_free(aks_ref_key_t* key)
202 aks_ref_key_get_external_data(aks_ref_key_t refkey, size_t *out_external_data_len)
204 *out_external_data_len = 2;
205 return (const uint8_t *)"21";
210 aks_assert_hold(keybag_handle_t handle, uint32_t type, uint64_t timeout)
216 aks_assert_drop(keybag_handle_t handle, uint32_t type)
222 aks_generation(keybag_handle_t handle,
223 generation_option_t generation_option,
224 uint32_t * current_generation)
226 *current_generation = 0;
231 aks_get_bag_uuid(keybag_handle_t handle, uuid_t uuid)
233 memcpy(uuid, "0123456789abcdf", sizeof(uuid_t));
238 aks_get_lock_state(keybag_handle_t handle, keybag_state_t *state)
240 *state = keybag_state_been_unlocked;
244 static CFStringRef staticKeybagHandle = CFSTR("keybagHandle");
247 MKBKeyBagCreateWithData(CFDataRef keybagBlob, MKBKeyBagHandleRef* newHandle)
249 *newHandle = (MKBKeyBagHandleRef)staticKeybagHandle;
254 MKBKeyBagUnlock(MKBKeyBagHandleRef keybag, CFDataRef passcode)
256 if (keybag == NULL || !CFEqual(keybag, staticKeybagHandle)) {
262 int MKBKeyBagGetAKSHandle(MKBKeyBagHandleRef keybag, int32_t *handle)
264 if (keybag == NULL || !CFEqual(keybag, staticKeybagHandle)) {
273 const CFTypeRef kAKSKeyAcl = (CFTypeRef)CFSTR("kAKSKeyAcl");
274 const CFTypeRef kAKSKeyAclParamRequirePasscode = (CFTypeRef)CFSTR("kAKSKeyAclParamRequirePasscode");
276 const CFTypeRef kAKSKeyOpDefaultAcl = (CFTypeRef)CFSTR("kAKSKeyOpDefaultAcl");
277 const CFTypeRef kAKSKeyOpEncrypt = (CFTypeRef)CFSTR("kAKSKeyOpEncrypt");
278 const CFTypeRef kAKSKeyOpDecrypt = (CFTypeRef)CFSTR("kAKSKeyOpDecrypt");
279 const CFTypeRef kAKSKeyOpSync = (CFTypeRef)CFSTR("kAKSKeyOpSync");
280 const CFTypeRef kAKSKeyOpDelete = (CFTypeRef)CFSTR("kAKSKeyOpDelete");
281 const CFTypeRef kAKSKeyOpCreate = (CFTypeRef)CFSTR("kAKSKeyOpCreate");
282 const CFTypeRef kAKSKeyOpSign = (CFTypeRef)CFSTR("kAKSKeyOpSign");
283 const CFTypeRef kAKSKeyOpSetKeyClass = (CFTypeRef)CFSTR("kAKSKeyOpSetKeyClass");
284 const CFTypeRef kAKSKeyOpWrap = (CFTypeRef)CFSTR("kAKSKeyOpWrap");
285 const CFTypeRef kAKSKeyOpUnwrap = (CFTypeRef)CFSTR("kAKSKeyOpUnwrap");
286 const CFTypeRef kAKSKeyOpComputeKey = (CFTypeRef)CFSTR("kAKSKeyOpComputeKey");
287 const CFTypeRef kAKSKeyOpAttest = (CFTypeRef)CFSTR("kAKSKeyOpAttest");
288 const CFTypeRef kAKSKeyOpTranscrypt = (CFTypeRef)CFSTR("kAKSKeyOpTranscrypt");
291 TKTokenRef TKTokenCreate(CFDictionaryRef attributes, CFErrorRef *error)
296 CFTypeRef TKTokenCopyObjectData(TKTokenRef token, CFDataRef objectID, CFErrorRef *error)
301 CFDataRef TKTokenCopyObjectCreationAccessControl(TKTokenRef token, CFDictionaryRef objectAttributes, CFErrorRef *error)
306 CFDataRef TKTokenCreateOrUpdateObject(TKTokenRef token, CFDataRef objectID, CFMutableDictionaryRef attributes, CFErrorRef *error)
311 CFDataRef TKTokenCopyObjectAccessControl(TKTokenRef token, CFDataRef objectID, CFErrorRef *error)
315 bool TKTokenDeleteObject(TKTokenRef token, CFDataRef objectID, CFErrorRef *error)
320 CFDataRef TKTokenCopyPublicKeyData(TKTokenRef token, CFDataRef objectID, CFErrorRef *error)
325 CFTypeRef TKTokenCopyOperationResult(TKTokenRef token, CFDataRef objectID, CFIndex secKeyOperationType, CFArrayRef algorithm,
326 CFIndex secKeyOperationMode, CFTypeRef in1, CFTypeRef in2, CFErrorRef *error)
331 CF_RETURNS_RETAINED CFDictionaryRef TKTokenControl(TKTokenRef token, CFDictionaryRef attributes, CFErrorRef *error)
336 CFTypeRef LACreateNewContextWithACMContext(CFDataRef acmContext, CFErrorRef *error)
341 CFDataRef LACopyACMContext(CFTypeRef context, CFErrorRef *error)
346 bool LAEvaluateAndUpdateACL(CFTypeRef context, CFDataRef acl, CFTypeRef operation, CFDictionaryRef hints, CFDataRef *updatedACL, CFErrorRef *error)
352 ACMContextCreateWithExternalForm(const void *externalForm, size_t dataLength)
358 ACMContextDelete(ACMContextRef context, bool destroyContext)
364 ACMContextRemovePassphraseCredentialsByPurposeAndScope(const ACMContextRef context, ACMPassphrasePurpose purpose, ACMScope scope)