2 * Copyright (c) 2013-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@
26 #include "secd_regressions.h"
28 #include "keychain/securityd/SecDbItem.h"
29 #include "keychain/securityd/SecItemServer.h"
31 #include <utilities/array_size.h>
32 #include <utilities/SecFileLocations.h>
36 #include "SecdTestKeychainUtilities.h"
39 #include "OSX/utilities/SecAKSWrappers.h"
41 int secd_01_items(int argc, char *const *argv)
43 plan_tests(24 + kSecdTestSetupTestCount);
45 secd_test_setup_testviews(); // if running all tests get the test views setup first
46 /* custom keychain dir */
47 secd_test_setup_temp_keychain("secd_01_items", NULL);
50 keybag_handle_t keybag;
52 char *passcode="password";
53 int passcode_len=(int)strlen(passcode);
55 ok(kAKSReturnSuccess==aks_create_bag(passcode, passcode_len, kAppleKeyStoreDeviceBag, &keybag), "create keybag");
56 ok(kAKSReturnSuccess==aks_get_lock_state(keybag, &state), "get keybag state");
57 ok(!(state&keybag_state_locked), "keybag unlocked");
58 SecItemServerSetKeychainKeybag(keybag);
61 ok(kAKSReturnSuccess==aks_lock_bag(keybag), "lock keybag");
62 ok(kAKSReturnSuccess==aks_get_lock_state(keybag, &state), "get keybag state");
63 ok(state&keybag_state_locked, "keybag locked");
66 SecKeychainDbReset(NULL);
68 /* Creating a password */
70 CFNumberRef eighty = CFNumberCreate(NULL, kCFNumberSInt32Type, &v_eighty);
71 const char *v_data = "test";
72 CFDataRef pwdata = CFDataCreate(NULL, (UInt8 *)v_data, strlen(v_data));
73 const void *keys[] = {
79 kSecAttrAuthenticationType,
83 const void *values[] = {
84 kSecClassInternetPassword,
85 CFSTR("members.spamcop.net"),
94 CFDictionaryRef item = CFDictionaryCreate(NULL, keys, values,
95 array_size(keys), NULL, NULL);
98 is_status(SecItemAdd(item, NULL), errSecInteractionNotAllowed, "add internet password while locked");
101 ok(kAKSReturnSuccess==aks_unlock_bag(keybag, passcode, passcode_len), "unlock keybag");
102 ok(kAKSReturnSuccess==aks_get_lock_state(keybag, &state), "get keybag state");
103 ok(!(state&keybag_state_locked), "keybag unlocked");
105 ok_status(SecItemAdd(item, NULL), "add internet password, while unlocked");
109 ok(kAKSReturnSuccess==aks_lock_bag(keybag), "lock keybag");
110 ok(kAKSReturnSuccess==aks_get_lock_state(keybag, &state), "get keybag state");
111 ok(state&keybag_state_locked, "keybag locked");
113 is_status(SecItemAdd(item, NULL), errSecInteractionNotAllowed,
114 "add internet password again, while locked");
117 ok(kAKSReturnSuccess==aks_unlock_bag(keybag, passcode, passcode_len), "unlock keybag");
118 ok(kAKSReturnSuccess==aks_get_lock_state(keybag, &state), "get keybag state");
119 ok(!(state&keybag_state_locked), "keybag unlocked");
121 is_status(SecItemAdd(item, NULL), errSecDuplicateItem,
122 "add internet password again, while unlocked");
124 CFTypeRef results = NULL;
125 /* Create a dict with all attrs except the data. */
126 CFDictionaryRef query = CFDictionaryCreate(NULL, keys, values,
127 (array_size(keys)) - 1, NULL, NULL);
128 ok_status(SecItemCopyMatching(query, &results), "find internet password, while unlocked ");
135 ok(kAKSReturnSuccess==aks_lock_bag(keybag), "lock keybag");
136 ok(kAKSReturnSuccess==aks_get_lock_state(keybag, &state), "get keybag state");
137 ok(state&keybag_state_locked, "keybag locked");
139 is_status(SecItemCopyMatching(query, &results), errSecInteractionNotAllowed, "find internet password, while locked ");
141 /* Reset keybag and custom $HOME */
142 SecItemServerResetKeychainKeybag();
143 SetCustomHomePath(NULL);
144 SecKeychainDbReset(NULL);
147 CFReleaseNull(pwdata);
148 CFReleaseNull(eighty);
150 CFReleaseSafe(query);
157 int secd_01_items(int argc, char *const *argv)
161 todo("Not yet working in simulator");
167 /* not implemented in simulator (no keybag) */