]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-20-keychain_upgrade.m
Security-57337.50.23.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-20-keychain_upgrade.m
1 /*
2 * Copyright (c) 2015 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
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/
28 */
29 #define __KEYCHAINCORE__ 1
30
31
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>
41
42 #import <stdlib.h>
43
44 #include "secd_regressions.h"
45 #include "SecdTestKeychainUtilities.h"
46
47
48
49 static void
50 keychain_upgrade(bool musr, const char *dbname)
51 {
52 OSStatus res;
53
54 secd_test_setup_temp_keychain(dbname, NULL);
55
56 #if TARGET_OS_IOS
57 if (musr)
58 SecSecuritySetMusrMode(true, 502, 502);
59 #endif
60
61 #if TARGET_OS_IPHONE
62 /*
63 * Check system keychain migration
64 */
65
66 res = SecItemAdd((CFDictionaryRef)@{
67 (id)kSecClass : (id)kSecClassGenericPassword,
68 (id)kSecAttrAccount : @"system-label-me",
69 (id)kSecUseSystemKeychain : (id)kCFBooleanTrue,
70 }, NULL);
71 is(res, 0, "SecItemAdd(system)");
72 #endif
73
74 /*
75 * Check user keychain
76 */
77
78 res = SecItemAdd((CFDictionaryRef)@{
79 (id)kSecClass : (id)kSecClassGenericPassword,
80 (id)kSecAttrAccount : @"user-label-me",
81 }, NULL);
82 is(res, 0, "SecItemAdd(user)");
83
84 SecKeychainDbReset(^{
85 NSString *keychain_path = (NSString *)__SecKeychainCopyPath();
86 [keychain_path autorelease];
87
88 /* Create a new keychain sqlite db */
89 sqlite3 *db;
90
91 is(sqlite3_open([keychain_path UTF8String], &db), SQLITE_OK, "create keychain");
92 is(sqlite3_exec(db, "UPDATE tversion SET version = version - 1", NULL, NULL, NULL), SQLITE_OK,
93 "\"downgrade\" keychain");
94 is(sqlite3_close(db), SQLITE_OK, "close db");
95
96 });
97
98 #if TARGET_OS_IPHONE
99 res = SecItemCopyMatching((CFDictionaryRef)@{
100 (id)kSecClass : (id)kSecClassGenericPassword,
101 (id)kSecAttrAccount : @"system-label-me",
102 (id)kSecUseSystemKeychain : (id)kCFBooleanTrue,
103 }, NULL);
104 is(res, 0, "SecItemCopyMatching(system)");
105 #endif
106
107 res = SecItemCopyMatching((CFDictionaryRef)@{
108 (id)kSecClass : (id)kSecClassGenericPassword,
109 (id)kSecAttrAccount : @"user-label-me",
110 }, NULL);
111 is(res, 0, "SecItemCopyMatching(user)");
112
113 #if TARGET_OS_IOS
114 if (musr)
115 SecSecuritySetMusrMode(false, 501, -1);
116 #endif
117 }
118
119 void SecAccessGroupsSetCurrent(CFArrayRef accessGroups);
120 CFArrayRef SecAccessGroupsGetCurrent();
121
122 int
123 secd_20_keychain_upgrade(int argc, char *const *argv)
124 {
125 #if TARGET_OS_IPHONE
126 #define have_system_keychain_tests 2
127 #else
128 #define have_system_keychain_tests 0
129 #endif
130
131 plan_tests((kSecdTestSetupTestCount + 5 + have_system_keychain_tests) * 2);
132
133 CFArrayRef currentACL = SecAccessGroupsGetCurrent();
134
135 NSMutableArray *newACL = [NSMutableArray arrayWithArray:(__bridge NSArray *)currentACL];
136 [newACL addObjectsFromArray:@[
137 @"com.apple.private.system-keychain",
138 @"com.apple.private.syncbubble-keychain",
139 @"com.apple.private.migrate-musr-system-keychain",
140 ]];
141
142 SecAccessGroupsSetCurrent((__bridge CFArrayRef)newACL);
143
144 keychain_upgrade(false, "secd_20_keychain_upgrade");
145 keychain_upgrade(true, "secd_20_keychain_upgrade-musr");
146
147 SecAccessGroupsSetCurrent(currentACL);
148
149 return 0;
150 }