2 * Copyright (c) 2016 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 "secd_regressions.h"
27 #include <Security/Security.h>
29 #include <utilities/SecCFWrappers.h>
30 #include "SecDbKeychainItem.h"
32 #include <TargetConditionals.h>
35 #include "OSX/utilities/SecAKSWrappers.h"
37 #include "SecdTestKeychainUtilities.h"
39 int secd_36_ks_encrypt(int argc, char *const *argv)
43 secd_test_setup_temp_keychain("secd_36_ks_encrypt", NULL);
45 keybag_handle_t keybag;
47 CFDictionaryRef data = NULL;
49 CFErrorRef error = NULL;
50 SecAccessControlRef ac = NULL;
53 char passcode[] = "password";
54 int passcode_len = sizeof(passcode) - 1;
57 /* Create and lock custom keybag */
58 is(kAKSReturnSuccess, aks_create_bag(passcode, passcode_len, kAppleKeyStoreDeviceBag, &keybag), "create keybag");
59 is(kAKSReturnSuccess, aks_get_lock_state(keybag, &state), "get keybag state");
60 is(0, (int)(state&keybag_state_locked), "keybag unlocked");
62 data = (__bridge CFDictionaryRef)@{
63 (id)kSecValueData : @"secret here",
66 ok(ac = SecAccessControlCreate(NULL, &error), "SecAccessControlCreate: %@", error);
67 ok(SecAccessControlSetProtection(ac, kSecAttrAccessibleWhenUnlocked, &error), "SecAccessControlSetProtection: %@", error);
69 ret = ks_encrypt_data(keybag, ac, NULL, data, (__bridge CFDictionaryRef)@{@"persistref" : @"aaa-bbb-ccc"}, NULL, &enc, true, &error);
75 CFMutableDictionaryRef attributes = NULL;
78 ret = ks_decrypt_data(keybag, kAKSKeyOpDecrypt, &ac, NULL, enc, NULL, NULL, &attributes, &version, true, NULL, &error);
79 is(true, ret, "ks_decrypt_data: %@", error);
81 CFTypeRef aclProtection = ac ? SecAccessControlGetProtection(ac) : NULL;
82 ok(aclProtection && CFEqual(aclProtection, kSecAttrAccessibleWhenUnlocked), "AccessControl protection is: %@", aclProtection);
93 #else /* !USE_KEYSTORE */
95 int secd_36_ks_encrypt(int argc, char *const *argv)
101 #endif /* USE_KEYSTORE */