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>
37 #include "SecdTestKeychainUtilities.h"
39 int secd_36_ks_encrypt(int argc, char *const *argv)
43 keybag_handle_t keybag;
45 CFDictionaryRef data = NULL;
47 CFErrorRef error = NULL;
48 SecAccessControlRef ac = NULL;
51 char passcode[] = "password";
52 int passcode_len = sizeof(passcode) - 1;
55 /* Create and lock custom keybag */
56 is(kIOReturnSuccess, aks_create_bag(passcode, passcode_len, kAppleKeyStoreDeviceBag, &keybag), "create keybag");
57 is(kIOReturnSuccess, aks_get_lock_state(keybag, &state), "get keybag state");
58 is(0, (int)(state&keybag_state_locked), "keybag unlocked");
60 data = (__bridge CFDictionaryRef)@{
61 (id)kSecValueData : @"secret here",
64 ok(ac = SecAccessControlCreate(NULL, &error), "SecAccessControlCreate: %@", error);
65 ok(SecAccessControlSetProtection(ac, kSecAttrAccessibleWhenUnlocked, &error), "SecAccessControlSetProtection: %@", error);
67 ret = ks_encrypt_data(keybag, ac, NULL, data, NULL, &enc, true, &error);
73 CFMutableDictionaryRef attributes = NULL;
76 ret = ks_decrypt_data(keybag, kAKSKeyOpDecrypt, &ac, NULL, enc, NULL, NULL, &attributes, &version, &error);
77 is(true, ret, "ks_decrypt_data: %@", error);
79 ok(CFEqual(SecAccessControlGetProtection(ac), kSecAttrAccessibleWhenUnlocked), "AccessControl protection is: %@", SecAccessControlGetProtection(ac));
90 #else /* !USE_KEYSTORE */
92 int secd_36_ks_encrypt(int argc, char *const *argv)
98 #endif /* USE_KEYSTORE */