2 * Copyright (c) 2014 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 #include <Security/SecItem.h>
26 #include <Security/SecItemPriv.h>
27 #include <Security/SecAccessControl.h>
28 #include <Security/SecAccessControlPriv.h>
29 #include <Security/SecInternal.h>
30 #include <utilities/SecCFWrappers.h>
31 #include <utilities/array_size.h>
32 #include <utilities/der_plist.h>
33 #include <libaks_acl_cf_keys.h>
35 #include <ACMAclDefs.h>
37 #if TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_IPHONE_SIMULATOR)
38 #define USE_KEYSTORE 1
39 #elif TARGET_OS_EMBEDDED && !TARGET_IPHONE_SIMULATOR
40 #define USE_KEYSTORE 1
41 #else /* no keystore on this platform */
42 #define USE_KEYSTORE 0
46 #include <Security/SecRandom.h>
47 #include <securityd/SecDbItem.h>
48 #include <coreauthd_spi.h>
49 #include <corecrypto/ccder.h>
52 #include "Security_regressions.h"
54 #if LA_CONTEXT_IMPLEMENTED
55 static bool aks_consistency_test(bool currentAuthDataFormat
, kern_return_t expectedAksResult
, SecAccessControlRef access_control
, CFDataRef acm_context
);
56 static CFDataRef
kc_create_auth_data(SecAccessControlRef access_control
, CFDictionaryRef auth_attributes
);
57 static CFDataRef
kc_copy_constraints_data(SecAccessControlRef access_control
, CFDictionaryRef auth_attributes
);
58 static int aks_crypt_acl(CFTypeRef operation
, keybag_handle_t keybag
,
59 keyclass_t keyclass
, uint32_t textLength
, const uint8_t *source
,
60 CFMutableDataRef dest
, CFDataRef auth_data
, CFDataRef acm_context
, CFDataRef caller_access_groups
);
63 static void tests(void)
65 CFAllocatorRef allocator
= kCFAllocatorDefault
;
66 CFTypeRef protection
= kSecAttrAccessibleAlwaysPrivate
;
67 CFErrorRef error
= NULL
;
71 // ACL with protection only
72 SecAccessControlRef acl
= SecAccessControlCreateWithFlags(allocator
, protection
, 0, &error
);
73 ok(acl
!= NULL
, "SecAccessControlCreateWithFlags: %@", error
);
77 // ACL with flags only (not allowed)
78 #pragma clang diagnostic push
79 #pragma clang diagnostic ignored "-Wnonnull"
80 // NULL passed as 'protection' newly generates a warning, we need to suppress it in order to compile
81 acl
= SecAccessControlCreateWithFlags(allocator
, NULL
, kSecAccessControlUserPresence
, &error
);
82 #pragma clang diagnostic pop
83 ok(acl
== NULL
, "SecAccessControlCreateWithFlags");
87 // ACL with protection and kSecAccessControlTouchIDCurrentSet
88 acl
= SecAccessControlCreateWithFlags(allocator
, protection
, kSecAccessControlTouchIDCurrentSet
, &error
);
89 ok(acl
!= NULL
, "SecAccessControlCreateWithFlags: %@", error
);
93 // ACL with protection and flags
94 acl
= SecAccessControlCreateWithFlags(allocator
, protection
, kSecAccessControlTouchIDAny
| kSecAccessControlDevicePasscode
| kSecAccessControlOr
, &error
);
95 ok(acl
!= NULL
, "SecAccessControlCreateWithFlags: %@", error
);
99 // ACL with protection and flags
100 acl
= SecAccessControlCreateWithFlags(allocator
, protection
, kSecAccessControlTouchIDAny
| kSecAccessControlDevicePasscode
| kSecAccessControlAnd
, &error
);
101 ok(acl
!= NULL
, "SecAccessControlCreateWithFlags: %@", error
);
102 CFReleaseNull(error
);
105 // ACL with protection and flags
106 acl
= SecAccessControlCreateWithFlags(allocator
, protection
, kSecAccessControlTouchIDAny
| kSecAccessControlDevicePasscode
| kSecAccessControlAnd
| kSecAccessControlApplicationPassword
, &error
);
107 ok(acl
!= NULL
, "SecAccessControlCreateWithFlags: %@", error
);
108 CFReleaseNull(error
);
111 // ACL with protection and kSecAccessControlApplicationPassword
112 acl
= SecAccessControlCreateWithFlags(allocator
, protection
, kSecAccessControlApplicationPassword
, &error
);
113 ok(acl
!= NULL
, "SecAccessControlCreateWithFlags: %@", error
);
114 CFReleaseNull(error
);
117 // ACL with protection and, kSecAccessControlUserPresence can be in combination with kSecAccessControlApplicationPassword and kSecAccessControlPrivateKeyUsage
118 acl
= SecAccessControlCreateWithFlags(allocator
, protection
, kSecAccessControlUserPresence
| kSecAccessControlApplicationPassword
| kSecAccessControlPrivateKeyUsage
, &error
);
119 ok(acl
!= NULL
, "SecAccessControlCreateWithFlags: %@", error
);
120 CFReleaseNull(error
);
123 // negative test of ACL with protection and, kSecAccessControlUserPresence can be in combination with kSecAccessControlApplicationPassword and kSecAccessControlPrivateKeyUsage
124 acl
= SecAccessControlCreateWithFlags(allocator
, protection
, kSecAccessControlUserPresence
| kSecAccessControlTouchIDAny
, &error
);
125 ok(acl
== NULL
, "SecAccessControlCreateWithFlag wrong combination of flags");
126 CFReleaseNull(error
);
129 // ACL with protection and flags
130 acl
= SecAccessControlCreateWithFlags(allocator
, protection
, kSecAccessControlUserPresence
, &error
);
131 ok(acl
!= NULL
, "SecAccessControlCreateWithFlags: %@", error
);
132 CFReleaseNull(error
);
134 // Extended API tests:
137 CFTypeRef aclProtection
= SecAccessControlGetProtection(acl
);
138 is(aclProtection
, protection
, "SecAccessControlGetProtection");
140 SecAccessConstraintRef aclConstraint
= SecAccessControlGetConstraint(acl
, kAKSKeyOpDecrypt
);
142 ok(aclConstraint
!= NULL
&& isDictionary(aclConstraint
), "SecAccessControlGetConstraint");
143 eq_cf(CFDictionaryGetValue(aclConstraint
, CFSTR(kACMKeyAclConstraintPolicy
)), CFSTR(kACMPolicyDeviceOwnerAuthentication
), "SecAccessControlGetConstraint");
150 acl
= SecAccessControlCreate(allocator
, &error
);
151 ok(acl
!= NULL
, "SecAccessControlCreate: %@", error
);
152 CFReleaseNull(error
);
155 bool result
= SecAccessControlSetProtection(acl
, protection
, &error
);
156 ok(result
, "SecAccessControlSetProtection: %@", error
);
157 CFReleaseNull(error
);
159 aclProtection
= SecAccessControlGetProtection(acl
);
161 is(aclProtection
, protection
, "SecAccessControlGetProtection");
164 SecAccessConstraintRef policy
= SecAccessConstraintCreatePolicy(allocator
, CFSTR(kACMPolicyDeviceOwnerAuthentication
), &error
);
165 ok(policy
!= NULL
, "SecAccessConstraintCreatePolicy: %@", error
);
166 ok(isDictionary(policy
), "SecAccessConstraintCreatePolicy");
167 is(CFDictionaryGetValue(policy
, CFSTR(kACMKeyAclConstraintPolicy
)), CFSTR(kACMPolicyDeviceOwnerAuthentication
), "SecAccessConstraintCreatePolicy");
168 CFReleaseNull(error
);
169 CFReleaseNull(policy
);
171 // Passcode constraint
172 SecAccessConstraintRef passcode
= SecAccessConstraintCreatePasscode(allocator
);
173 ok(passcode
!= NULL
&& isDictionary(passcode
), "SecAccessConstraintCreatePasscode");
174 is(CFDictionaryGetValue(passcode
, CFSTR(kACMKeyAclConstraintUserPasscode
)), kCFBooleanTrue
, "SecAccessConstraintCreatePasscode");
175 // CFReleaseNull(passcode); passcode will be used in later tests
177 CFUUIDRef uuid
= CFUUIDCreate(allocator
);
178 CFStringRef uuidString
= CFUUIDCreateString(allocator
, uuid
);
179 CFDataRef uuidData
= CFStringCreateExternalRepresentation(allocator
, uuidString
, kCFStringEncodingUTF8
, 0);
180 SecAccessConstraintRef touchID
= SecAccessConstraintCreateTouchIDCurrentSet(allocator
, uuidData
, uuidData
);
181 // TouchID constraint
182 ok(touchID
!= NULL
, "SecAccessConstraintCreateTouchID: %@", error
);
183 ok(isDictionary(touchID
), "SecAccessConstraintCreateTouchID");
184 ok(CFDictionaryGetValue(touchID
, CFSTR(kACMKeyAclConstraintBio
)), "SecAccessConstraintCreateTouchID");
185 CFDictionaryRef bioRef
= CFDictionaryGetValue(touchID
, CFSTR(kACMKeyAclConstraintBio
));
186 ok(isDictionary(bioRef
), "SecAccessConstraintCreateTouchID");
187 is(CFDictionaryGetValue(bioRef
, CFSTR(kACMKeyAclParamBioCatacombUUID
)), uuidData
, "SecAccessConstraintCreateTouchID");
188 is(CFDictionaryGetValue(bioRef
, CFSTR(kACMKeyAclParamBioDatabaseHash
)), uuidData
, "SecAccessConstraintCreateTouchID");
189 CFReleaseNull(error
);
190 CFReleaseNull(touchID
);
191 CFReleaseNull(uuidData
);
192 CFReleaseNull(uuidString
);
195 uuid
= CFUUIDCreate(allocator
);
196 uuidString
= CFUUIDCreateString(allocator
, uuid
);
197 uuidData
= CFStringCreateExternalRepresentation(allocator
, uuidString
, kCFStringEncodingUTF8
, 0);
198 touchID
= SecAccessConstraintCreateTouchIDAny(allocator
, uuidData
);
199 // TouchID constraint
200 ok(touchID
!= NULL
, "SecAccessConstraintCreateTouchID: %@", error
);
201 ok(isDictionary(touchID
), "SecAccessConstraintCreateTouchID");
202 ok(CFDictionaryGetValue(touchID
, CFSTR(kACMKeyAclConstraintBio
)), "SecAccessConstraintCreateTouchID");
203 bioRef
= CFDictionaryGetValue(touchID
, CFSTR(kACMKeyAclConstraintBio
));
204 ok(isDictionary(bioRef
), "SecAccessConstraintCreateTouchID");
205 is(CFDictionaryGetValue(bioRef
, CFSTR(kACMKeyAclParamBioCatacombUUID
)), uuidData
, "SecAccessConstraintCreateTouchID");
206 CFReleaseNull(error
);
207 // CFReleaseNull(touchID); touchID will be used in later tests
208 CFReleaseNull(uuidData
);
209 CFReleaseNull(uuidString
);
213 CFTypeRef constraints_array
[] = { passcode
, touchID
};
214 CFArrayRef constraintsArray
= CFArrayCreate(allocator
, constraints_array
, array_size(constraints_array
), &kCFTypeArrayCallBacks
);
215 SecAccessConstraintRef kofn
= SecAccessConstraintCreateKofN(allocator
, 1, constraintsArray
, &error
);
216 ok(kofn
!= NULL
, "SecAccessConstraintCreateKofN: %@", error
);
217 ok(isDictionary(kofn
), "SecAccessConstraintCreateKofN");
218 CFTypeRef kofnConstraint
= CFDictionaryGetValue(kofn
, CFSTR(kACMKeyAclConstraintKofN
));
219 ok(kofnConstraint
!= NULL
&& isDictionary(kofnConstraint
), "SecAccessConstraintCreateKofN");
220 CFNumberRef required
= CFNumberCreateWithCFIndex(allocator
, 1);
221 is(CFDictionaryGetValue(kofnConstraint
, CFSTR(kACMKeyAclParamKofN
)), required
, "SecAccessConstraintCreateKofN");
222 ok(CFDictionaryGetValue(kofnConstraint
, CFSTR(kACMKeyAclConstraintBio
)), "SecAccessConstraintCreateKofN");
223 is(CFDictionaryGetValue(kofnConstraint
, CFSTR(kACMKeyAclConstraintUserPasscode
)), kCFBooleanTrue
, "SecAccessConstraintCreateKofN");
224 CFReleaseNull(error
);
226 CFReleaseNull(required
);
227 CFReleaseNull(constraintsArray
);
228 CFReleaseNull(passcode
);
230 // Add ACL constraint for operation
231 result
= SecAccessControlAddConstraintForOperation(acl
, kAKSKeyOpDecrypt
, touchID
, &error
);
232 ok(result
, "SecAccessControlAddConstraintForOperation: %@", error
);
233 CFReleaseNull(error
);
235 // Get ACL operation constraint
236 SecAccessConstraintRef constraint
= SecAccessControlGetConstraint(acl
, kAKSKeyOpDecrypt
);
237 is(constraint
, touchID
, "SecAccessControlGetConstraint");
238 CFReleaseNull(touchID
);
240 // Add ACL constraint for operation (kCFBooleanTrue)
241 result
= SecAccessControlAddConstraintForOperation(acl
, kAKSKeyOpDecrypt
, kCFBooleanTrue
, &error
);
242 ok(result
, "SecAccessControlAddConstraintForOperation: %@", error
);
243 CFReleaseNull(error
);
245 // Get ACL operation constraint (kCFBooleanTrue)
246 constraint
= SecAccessControlGetConstraint(acl
, kAKSKeyOpDecrypt
);
247 is(constraint
, kCFBooleanTrue
, "SecAccessControlGetConstraint");
249 // Get ACL constraints
250 CFDictionaryRef constraints
= SecAccessControlGetConstraints(acl
);
251 ok(constraints
!= NULL
&& isDictionary(constraints
), "SecAccessControlGetConstraints");
252 // Get ACL constraints
253 is(CFDictionaryGetValue(constraints
, kAKSKeyOpDecrypt
), kCFBooleanTrue
, "SecAccessControlGetConstraints");
255 // ACL export and import
256 CFDataRef aclData
= SecAccessControlCopyData(acl
);
257 ok(aclData
!= NULL
, "SecAccessControlCopyData");
258 SecAccessControlRef aclCopy
= SecAccessControlCreateFromData(allocator
, aclData
, &error
);
259 ok(aclCopy
!= NULL
, "SecAccessControlCopyData: %@", error
);
260 ok(CFEqual(aclCopy
, acl
), "SecAccessControlCopyData");
261 CFReleaseNull(error
);
262 CFReleaseNull(aclCopy
);
263 CFReleaseNull(aclData
);
265 // Extended SPI tests:
267 // kAKSKeyDefaultOpAcl
268 result
= SecAccessControlAddConstraintForOperation(acl
, kAKSKeyOpDefaultAcl
, kCFBooleanTrue
, &error
);
269 ok(result
, "SecAccessControlAddConstraintForOperation: %@", error
);
270 constraint
= SecAccessControlGetConstraint(acl
, kAKSKeyOpDecrypt
);
271 is(constraint
, kCFBooleanTrue
, "SecAccessControlRemoveConstraintForOperation");
272 CFReleaseNull(error
);
276 #if LA_CONTEXT_IMPLEMENTED
277 // AKS consistency test:
279 acl
= SecAccessControlCreateWithFlags(allocator
, protection
, kSecAccessControlUserPresence
, &error
);
280 ok(acl
!= NULL
, "SecAccessControlCreateWithFlags: %@", error
);
281 CFReleaseNull(error
);
284 skip("SecAccessControlCreateWithFlags failed", 7, acl
!= NULL
);
286 CFDataRef acm_context
= NULL
;
287 CFTypeRef auth_handle
= NULL
;
289 auth_handle
= LACreateNewContextWithACMContext(NULL
, &error
);
290 ok(auth_handle
!= NULL
, "LACreateNewContextWithACMContext: %@", error
);
291 CFReleaseNull(error
);
294 skip("LACreateNewContextWithACMContext failed", 6, auth_handle
!= NULL
);
296 acm_context
= LACopyACMContext(auth_handle
, &error
);
297 ok(acm_context
!= NULL
, "LACopyACMContext: %@", error
);
298 CFReleaseNull(error
);
300 CFReleaseNull(auth_handle
);
303 skip("LACopyACMContext failed", 5, acm_context
!= NULL
);
305 ok(aks_consistency_test(true, kAKSReturnPolicyError
, acl
, acm_context
), "AKS consistency negative test (current auth_data format)");
306 ok(aks_consistency_test(false, kAKSReturnPolicyError
, acl
, acm_context
), "AKS consistency negative test (old auth_data format)");
308 bool decrypt_enabled
= false;
309 CFDictionaryRef constraints
= SecAccessControlGetConstraints(acl
);
311 CFMutableDictionaryRef new_constraints
= CFDictionaryCreateMutableCopy(NULL
, 0, constraints
);
312 if (new_constraints
) {
313 CFDictionarySetValue(new_constraints
, kAKSKeyOpDecrypt
, kCFBooleanTrue
);
314 SecAccessControlSetConstraints(acl
, new_constraints
);
315 CFReleaseSafe(new_constraints
);
316 decrypt_enabled
= true;
319 ok(decrypt_enabled
, "Cannot enable decrypt operation for AKS consistency positive tests");
321 ok(aks_consistency_test(true, kAKSReturnSuccess
, acl
, acm_context
), "AKS consistency positive test (current auth_data format)");
322 ok(aks_consistency_test(false, kAKSReturnSuccess
, acl
, acm_context
), "AKS consistency positive test (old auth_data format)");
324 CFReleaseNull(acm_context
);
332 // kSecAccessControlPrivateKeyUsage
333 acl
= SecAccessControlCreateWithFlags(kCFAllocatorDefault
, protection
, kSecAccessControlPrivateKeyUsage
| kSecAccessControlDevicePasscode
, NULL
);
334 ok(acl
, "kSecAccessControlPrivateKeyUsage ACL create with constraint");
335 ok(!SecAccessControlGetConstraint(acl
, kAKSKeyOpEncrypt
), "kAKSKeyOpEncrypt constraint");
336 ok(!SecAccessControlGetConstraint(acl
, kAKSKeyOpDecrypt
), "kAKSKeyOpDecrypt constraint");
337 ok(constraint
= SecAccessControlGetConstraint(acl
, kAKSKeyOpDelete
), "kAKSKeyOpDelete constraint");
338 is(constraint
, kCFBooleanTrue
, "kAKSKeyOpDelete constraint value");
339 ok(constraint
= SecAccessControlGetConstraint(acl
, kAKSKeyOpSign
), "kAKSKeyOpSign constraint");
340 ok(constraint
&& isDictionary(constraint
), "kAKSKeyOpSign constraint value");
343 acl
= SecAccessControlCreateWithFlags(kCFAllocatorDefault
, protection
, kSecAccessControlPrivateKeyUsage
, NULL
);
344 ok(acl
, "kSecAccessControlPrivateKeyUsage ACL create without constraint");
345 ok(!SecAccessControlGetConstraint(acl
, kAKSKeyOpEncrypt
), "kAKSKeyOpEncrypt constraint");
346 ok(!SecAccessControlGetConstraint(acl
, kAKSKeyOpDecrypt
), "kAKSKeyOpDecrypt constraint");
347 ok(constraint
= SecAccessControlGetConstraint(acl
, kAKSKeyOpDelete
), "kAKSKeyOpDelete constraint");
348 is(constraint
, kCFBooleanTrue
, "kAKSKeyOpDelete constraint value");
349 ok(constraint
= SecAccessControlGetConstraint(acl
, kAKSKeyOpSign
), "kAKSKeyOpSign constraint");
350 is(constraint
, kCFBooleanTrue
, "kAKSKeyOpSign constraint value");
354 #if LA_CONTEXT_IMPLEMENTED
356 static bool aks_consistency_test(bool currentAuthDataFormat
, kern_return_t expectedAksResult
, SecAccessControlRef access_control
, CFDataRef acm_context
)
360 const uint32_t bulkKeySize
= 32;
361 const uint32_t maxKeyWrapOverHead
= 8 + 32;
362 uint8_t bulkKey
[bulkKeySize
];
363 CFMutableDataRef bulkKeyWrapped
= CFDataCreateMutable(NULL
, 0);
364 CFDataSetLength(bulkKeyWrapped
, bulkKeySize
+ maxKeyWrapOverHead
);
365 CFMutableDataRef bulkKeyUnwrapped
= CFDataCreateMutable(NULL
, 0);
366 CFDataSetLength(bulkKeyUnwrapped
, bulkKeySize
);
368 CFDataRef auth_data
= NULL
;
369 CFMutableDictionaryRef auth_attribs
= NULL
;
371 require_noerr_string(SecRandomCopyBytes(kSecRandomDefault
, bulkKeySize
, bulkKey
), out
, "SecRandomCopyBytes failed");
373 auth_attribs
= CFDictionaryCreateMutable(NULL
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
374 if (currentAuthDataFormat
) {
375 auth_data
= kc_create_auth_data(access_control
, auth_attribs
);
378 auth_data
= kc_copy_constraints_data(access_control
, auth_attribs
);
381 require_string(aks_crypt_acl(kAKSKeyOpEncrypt
, KEYBAG_DEVICE
, key_class_dk
, bulkKeySize
, bulkKey
, bulkKeyWrapped
,
382 auth_data
, acm_context
, NULL
) == kAKSReturnSuccess
, out
, "kAKSKeyOpEncrypt failed");
384 uint32_t blobLenWrapped
= (uint32_t)CFDataGetLength(bulkKeyWrapped
);
385 const uint8_t *cursor
= CFDataGetBytePtr(bulkKeyWrapped
);
387 require_string(aks_crypt_acl(kAKSKeyOpDecrypt
, KEYBAG_DEVICE
, key_class_dk
, blobLenWrapped
, cursor
, bulkKeyUnwrapped
,
388 auth_data
, acm_context
, NULL
) == expectedAksResult
, out
, "kAKSKeyOpDecrypt finished with unexpected result");
393 CFReleaseSafe(bulkKeyUnwrapped
);
394 CFReleaseSafe(bulkKeyWrapped
);
395 CFReleaseSafe(auth_data
);
396 CFReleaseSafe(auth_attribs
);
401 static bool merge_der_in_to_data(const void *der1
, size_t der1_len
, const void *der2
, size_t der2_len
, CFMutableDataRef mergedData
)
404 CFPropertyListRef dict1
= NULL
;
405 CFPropertyListRef dict2
= NULL
;
407 der_decode_plist(NULL
, kCFPropertyListImmutable
, &dict1
, NULL
, der1
, der1
+ der1_len
);
408 der_decode_plist(NULL
, kCFPropertyListImmutable
, &dict2
, NULL
, der2
, der2
+ der2_len
);
409 if (dict1
&& dict2
) {
410 CFMutableDictionaryRef result_dict
= CFDictionaryCreateMutableCopy(kCFAllocatorDefault
, 0, dict1
);
411 CFDictionaryForEach(dict2
, ^(const void *key
, const void *value
) {
412 CFDictionaryAddValue(result_dict
, key
, value
);
415 CFDataSetLength(mergedData
, 0);
416 CFDataRef der_data
= CFPropertyListCreateDERData(kCFAllocatorDefault
, result_dict
, NULL
);
418 CFDataAppend(mergedData
, der_data
);
420 result
= CFDataGetLength(mergedData
) > 0;
422 CFRelease(result_dict
);
425 CFReleaseSafe(dict1
);
426 CFReleaseSafe(dict2
);
430 static int aks_crypt_acl(CFTypeRef operation
, keybag_handle_t keybag
,
431 keyclass_t keyclass
, uint32_t textLength
, const uint8_t *source
,
432 CFMutableDataRef dest
, CFDataRef auth_data
, CFDataRef acm_context
, CFDataRef caller_access_groups
)
434 int aks_return
= kAKSReturnSuccess
;
435 void *params
= NULL
, *der
= NULL
;
436 const uint8_t *access_groups
= caller_access_groups
?CFDataGetBytePtr(caller_access_groups
):NULL
;
437 size_t params_len
= 0, der_len
= 0, access_groups_len
= caller_access_groups
?CFDataGetLength(caller_access_groups
):0;
438 aks_ref_key_t key_handle
= NULL
;
440 if (CFEqual(operation
, kAKSKeyOpEncrypt
)) {
441 aks_operation_optional_params(0, 0, CFDataGetBytePtr(auth_data
), CFDataGetLength(auth_data
),
442 CFDataGetBytePtr(acm_context
), (int)CFDataGetLength(acm_context
), ¶ms
, ¶ms_len
);
444 require_noerr_quiet(aks_return
= aks_ref_key_create(keybag
, keyclass
, key_type_sym
, params
, params_len
, &key_handle
), out
);
445 require_noerr_quiet(aks_return
= aks_ref_key_encrypt(key_handle
, params
, params_len
, source
, textLength
, &der
, &der_len
), out
);
447 const void *key_blob
= aks_ref_key_get_blob(key_handle
, &key_blob_len
);
448 require_action_string(key_blob
, out
, aks_return
= kAKSReturnError
, "aks_ref_key_get_blob failed");
449 require_action_string(merge_der_in_to_data(der
, der_len
, key_blob
, key_blob_len
, dest
), out
, aks_return
= kAKSReturnError
, "merge_der_in_to_data failed");
451 } else if (CFEqual(operation
, kAKSKeyOpDecrypt
)) {
452 aks_operation_optional_params(access_groups
, access_groups_len
, 0, 0,
453 CFDataGetBytePtr(acm_context
), (int)CFDataGetLength(acm_context
), (void**)¶ms
, ¶ms_len
);
454 require_noerr_quiet(aks_return
= aks_ref_key_create_with_blob(keybag
, source
, textLength
, &key_handle
), out
);
455 require_noerr_quiet(aks_return
= aks_ref_key_decrypt(key_handle
, params
, params_len
, source
, textLength
, &der
, &der_len
), out
);
456 require_action_string(der
, out
, aks_return
= kAKSReturnError
, "aks_ref_key_decrypt failed");
458 CFPropertyListRef decoded_data
= NULL
;
459 der_decode_plist(kCFAllocatorDefault
, kCFPropertyListImmutable
, &decoded_data
, NULL
, der
, der
+ der_len
);
460 require_action_string(decoded_data
, out
, aks_return
= kAKSReturnError
, "der_decode_plist failed");
461 if (CFGetTypeID(decoded_data
) == CFDataGetTypeID()) {
462 CFDataSetLength(dest
, 0);
463 CFDataAppend(dest
, decoded_data
);
464 CFRelease(decoded_data
);
467 CFRelease(decoded_data
);
468 require_action_string(false, out
, aks_return
= kAKSReturnError
, "wrong decoded data type");
474 aks_ref_key_free(&key_handle
);
485 static CFDataRef
kc_create_auth_data(SecAccessControlRef access_control
, CFDictionaryRef auth_attributes
)
487 CFDictionaryRef constraints
= SecAccessControlGetConstraints(access_control
);
488 CFMutableDictionaryRef auth_data
= CFDictionaryCreateMutableCopy(NULL
, 0, auth_attributes
);
489 CFDictionarySetValue(auth_data
, kAKSKeyAcl
, constraints
);
490 CFDataRef encoded
= CFPropertyListCreateDERData(kCFAllocatorDefault
, auth_data
, NULL
);
491 CFReleaseSafe(auth_data
);
495 static CFDataRef
kc_copy_constraints_data(SecAccessControlRef access_control
, CFDictionaryRef auth_attributes
)
497 CFDictionaryRef constraints
= SecAccessControlGetConstraints(access_control
);
498 CFMutableDictionaryRef auth_data
= CFDictionaryCreateMutableCopy(NULL
, 0, constraints
);
499 if (auth_attributes
) {
500 CFDictionaryForEach(auth_attributes
, ^(const void *key
, const void *value
) {
501 CFDictionaryAddValue(auth_data
, key
, value
);
505 CFDataRef encoded
= CFPropertyListCreateDERData(kCFAllocatorDefault
, auth_data
, NULL
);
506 CFReleaseSafe(auth_data
);
512 int si_77_SecAccessControl(int argc
, char *const *argv
)
514 #if LA_CONTEXT_IMPLEMENTED