2 * Copyright (c) 2015 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 * This is to fool os services to not provide the Keychain manager
26 * interface tht doens't work since we don't have unified headers
27 * between iOS and OS X. rdar://23405418/
29 #define __KEYCHAINCORE__ 1
32 #import <Foundation/Foundation.h>
33 #import <CoreFoundation/CoreFoundation.h>
34 #import <Security/SecBase.h>
35 #import <Security/SecItem.h>
36 #import <Security/SecItemPriv.h>
37 #import <Security/SecInternal.h>
38 #import <utilities/SecCFWrappers.h>
39 #import <utilities/SecFileLocations.h>
40 #import <securityd/SecItemServer.h>
44 #include "secd_regressions.h"
45 #include "SecdTestKeychainUtilities.h"
47 void SecAccessGroupsSetCurrent(CFArrayRef accessGroups);
48 CFArrayRef SecAccessGroupsGetCurrent(void);
51 secd_21_transmogrify(int argc, char *const *argv)
53 plan_tests(kSecdTestSetupTestCount + 14);
56 CFErrorRef error = NULL;
57 CFDictionaryRef result = NULL;
60 CFArrayRef currentACL = SecAccessGroupsGetCurrent();
62 NSMutableArray *newACL = [NSMutableArray arrayWithArray:(__bridge NSArray *)currentACL];
63 [newACL addObjectsFromArray:@[
64 @"com.apple.private.system-keychain",
65 @"com.apple.private.syncbubble-keychain",
66 @"com.apple.private.migrate-musr-system-keychain",
67 @"com.apple.ProtectedCloudStorage",
70 SecAccessGroupsSetCurrent((__bridge CFArrayRef)newACL);
73 secd_test_setup_temp_keychain("secd_21_transmogrify", NULL);
76 * Add to user keychain
79 res = SecItemAdd((CFDictionaryRef)@{
80 (id)kSecClass : (id)kSecClassGenericPassword,
81 (id)kSecAttrAccount : @"user-label-me",
82 (id)kSecValueData : [NSData dataWithBytes:"password" length:8]
84 is(res, 0, "SecItemAdd(user)");
86 SecurityClient client = {
88 .accessGroups = (__bridge CFArrayRef)@[
89 @"com.apple.ProtectedCloudStorage"
91 .allowSystemKeychain = true,
92 .allowSyncBubbleKeychain = true,
98 is(_SecServerTransmogrifyToSystemKeychain(&client, &error), true, "_SecServerTransmogrifyToSystemKeychain: %@", error);
100 CFDataRef musr = SecMUSRCreateActiveUserUUID(502);
102 client.inMultiUser = true;
105 SecSecuritySetMusrMode(true, 502, 502);
107 res = SecItemCopyMatching((CFDictionaryRef)@{
108 (id)kSecClass : (id)kSecClassGenericPassword,
109 (id)kSecAttrAccount : @"user-label-me",
110 (id)kSecUseSystemKeychain : (id)kCFBooleanTrue,
111 (id)kSecReturnAttributes : (id)kCFBooleanTrue,
112 (id)kSecReturnData : @(YES)
113 }, (CFTypeRef *)&result);
114 is(res, 0, "SecItemCopyMatching(system)");
116 ok(isDictionary(result), "found item");
117 if (isDictionary(result)) {
118 NSData *data = ((__bridge NSDictionary *)result)[@"musr"];
119 ok([data isEqual:(__bridge id)SecMUSRGetSystemKeychainUUID()], "item is system keychain");
121 NSData* passwordData = [(__bridge NSDictionary*)result valueForKey:(id)kSecValueData];
122 ok([passwordData isEqual:[NSData dataWithBytes:"password" length:8]], "no data found in transmogrified item");
124 ok(0, "returned item is: %@", result);
126 CFReleaseNull(result);
132 res = _SecItemAdd((__bridge CFDictionaryRef)@{
133 (id)kSecClass : (id)kSecClassGenericPassword,
134 (id)kSecAttrAccessGroup : @"com.apple.ProtectedCloudStorage",
135 (id)kSecAttrAccessible : (id)kSecAttrAccessibleAfterFirstUnlock,
136 (id)kSecAttrAccount : @"pcs-label-me",
137 (id)kSecValueData : [NSData dataWithBytes:"some data" length:9],
138 }, &client, NULL, NULL);
139 is(res, true, "SecItemAdd(user)");
141 res = _SecItemCopyMatching((__bridge CFDictionaryRef)@{
142 (id)kSecClass : (id)kSecClassGenericPassword,
143 (id)kSecAttrAccount : @"pcs-label-me",
144 (id)kSecReturnAttributes : (id)kCFBooleanTrue,
145 (id)kSecReturnData : @(YES),
146 }, &client, (CFTypeRef *)&result, &error);
147 is(res, true, "SecItemCopyMatching(system): %@", error);
149 ok(isDictionary(result), "result is dictionary");
150 ok([[(__bridge NSDictionary*)result valueForKey:(__bridge id)kSecValueData] isEqual:[NSData dataWithBytes:"some data" length:9]], "retrieved data matches stored data");
152 /* Check that data are in 502 active user keychain */
153 ok (CFEqualSafe(((__bridge CFDataRef)((__bridge NSDictionary *)result)[@"musr"]), musr), "not in msr 502");
155 CFReleaseNull(result);
158 ok(_SecServerTransmogrifyToSyncBubble((__bridge CFArrayRef)@[@"com.apple.mailq.sync.xpc" ], client.uid, &client, &error),
159 "_SecServerTransmogrifyToSyncBubble: %@", error);
161 CFReleaseNull(error);
164 * first check normal keychain
167 res = _SecItemCopyMatching((__bridge CFDictionaryRef)@{
168 (id)kSecClass : (id)kSecClassGenericPassword,
169 (id)kSecAttrAccount : @"pcs-label-me",
170 (id)kSecReturnAttributes : (id)kCFBooleanTrue,
171 }, &client, (CFTypeRef *)&result, &error);
172 is(res, true, "SecItemCopyMatching(active): %@", error);
174 ok(isDictionary(result), "result is dictionary");
175 CFReleaseNull(result);
177 SecSecuritySetMusrMode(true, 503, 503);
180 * then syncbubble keychain
183 res = _SecItemCopyMatching((__bridge CFDictionaryRef)@{
184 (id)kSecClass : (id)kSecClassGenericPassword,
185 (id)kSecAttrAccount : @"pcs-label-me",
186 (id)kSecReturnAttributes : (id)kCFBooleanTrue,
187 (id)kSecUseSyncBubbleKeychain : @502,
188 }, &client, (CFTypeRef *)&result, &error);
189 is(res, true, "SecItemCopyMatching(syncbubble): %@", error);
190 ok(isDictionary(result), "result is dictionary");
192 CFReleaseNull(result);
194 SecSecuritySetMusrMode(false, 501, -1);
196 SecAccessGroupsSetCurrent(currentACL);
200 plan_skip_all("not support on non TARGET_OS_IOS");