]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_keychain/regressions/si-34-one-true-keychain.c
Security-58286.200.222.tar.gz
[apple/security.git] / OSX / libsecurity_keychain / regressions / si-34-one-true-keychain.c
1 /*
2 * Copyright (c) 2013-2014 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 #include <CoreFoundation/CoreFoundation.h>
25 #include <TargetConditionals.h>
26 #include <stdio.h>
27
28 #include "keychain_regressions.h"
29 #include <utilities/SecCFRelease.h>
30
31 #include <Security/SecBase.h>
32 #include <Security/SecItem.h>
33 #include <Security/SecItemPriv.h>
34 #include <utilities/SecCFRelease.h>
35 #include <libaks.h>
36 #include <AssertMacros.h>
37
38
39 /* Test whether the one true keychain pertains to the iOS keychain and only to the iOS keychain. */
40 static void tests(void)
41 {
42 int v_eighty = 80;
43 CFNumberRef eighty = CFNumberCreate(NULL, kCFNumberSInt32Type, &v_eighty);
44 const char *v_data = "test";
45 const char *v_data2 = "test";
46 CFDataRef pwdata = CFDataCreate(NULL, (UInt8 *)v_data, strlen(v_data));
47 CFDataRef pwdata2 = CFDataCreate(NULL, (UInt8 *)v_data2, strlen(v_data2));
48 CFMutableDictionaryRef query = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
49 CFTypeRef result = NULL;
50 CFDictionaryAddValue(query, kSecClass, kSecClassInternetPassword);
51 CFDictionaryAddValue(query, kSecAttrServer, CFSTR("members.spamcop.net"));
52 CFDictionaryAddValue(query, kSecAttrAccount, CFSTR("smith"));
53 CFDictionaryAddValue(query, kSecAttrPort, eighty); CFReleaseNull(eighty);
54 CFDictionaryAddValue(query, kSecAttrProtocol, kSecAttrProtocolHTTP);
55 CFDictionaryAddValue(query, kSecAttrAuthenticationType, kSecAttrAuthenticationTypeDefault);
56
57 CFMutableDictionaryRef noLegacyQuery = CFDictionaryCreateMutableCopy(NULL, 0, query);
58 CFMutableDictionaryRef syncAnyQuery = CFDictionaryCreateMutableCopy(NULL, 0, query);
59 CFMutableDictionaryRef syncQuery = CFDictionaryCreateMutableCopy(NULL, 0, query);
60
61 CFDictionaryAddValue(noLegacyQuery, kSecAttrNoLegacy, kCFBooleanTrue);
62 CFDictionaryAddValue(syncAnyQuery, kSecAttrSynchronizable, kSecAttrSynchronizableAny);
63 CFDictionaryAddValue(syncQuery, kSecAttrSynchronizable, kCFBooleanTrue);
64
65 SecItemDelete(query);
66 SecItemDelete(noLegacyQuery);
67 SecItemDelete(syncQuery);
68 SecItemDelete(syncAnyQuery);
69
70 CFDictionaryAddValue(query, kSecValueData, pwdata);
71 ok_status(SecItemAdd(query, NULL), "add internet password in OS X keychain");
72 CFDictionaryRemoveValue(query, kSecValueData);
73
74 ok_status(SecItemCopyMatching(query, &result), "find the osx item");
75 CFReleaseNull(result);
76 is_status(SecItemCopyMatching(noLegacyQuery, &result), errSecItemNotFound, "do not find the osx item with noLegacy");
77 CFReleaseNull(result);
78 ok_status(SecItemCopyMatching(syncAnyQuery, &result), "find the osx item with synchronizableAny");
79 CFReleaseNull(result);
80 is_status(SecItemCopyMatching(syncQuery, &result), errSecItemNotFound, "do not find the osx item with synchronizable");
81 CFReleaseNull(result);
82
83 CFMutableDictionaryRef toUpdate = CFDictionaryCreateMutable(NULL, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
84
85 CFDictionaryAddValue(toUpdate, kSecValueData, pwdata2);
86 CFReleaseNull(pwdata2);
87
88 ok_status(SecItemUpdate(query, toUpdate), "update the osx item");
89 is_status(SecItemUpdate(noLegacyQuery, toUpdate), errSecItemNotFound, "do not update the osx item with noLegacy");
90 ok_status(SecItemUpdate(syncAnyQuery, toUpdate), "update the osx item with synchronizableAny");
91
92 is_status(SecItemDelete(noLegacyQuery), errSecItemNotFound, "do not delete the osx item with noLegacy");
93 ok_status(SecItemDelete(syncAnyQuery), "delete the osx item with synchronizableAny");
94
95
96
97
98 CFDictionaryAddValue(noLegacyQuery, kSecValueData, pwdata);
99 CFReleaseNull(pwdata);
100 ok_status(SecItemAdd(noLegacyQuery, &result), "add internet password in iOS keychain");
101 CFDictionaryRemoveValue(noLegacyQuery, kSecValueData);
102
103 ok_status(SecItemCopyMatching(query, &result), "find the ios item with generic query");
104 CFReleaseNull(result);
105 ok_status(SecItemCopyMatching(noLegacyQuery, &result), "find the ios item with noLegacy");
106 CFReleaseNull(result);
107 ok_status(SecItemCopyMatching(syncAnyQuery, &result), "find the ios item with synchronizableAny");
108 CFReleaseNull(result);
109 is_status(SecItemCopyMatching(syncQuery, &result), errSecItemNotFound, "do not find the ios item with synchronizable");
110 CFReleaseNull(result);
111
112 ok_status(SecItemUpdate(query, toUpdate), "update the ios item without any flags");
113 ok_status(SecItemUpdate(noLegacyQuery, toUpdate), "update the ios item with noLegacy");
114 ok_status(SecItemUpdate(syncAnyQuery, toUpdate), "update the ios item with synchronizableAny");
115
116 CFDictionaryRemoveValue(noLegacyQuery, kSecValueData);
117
118 ok_status(SecItemDelete(noLegacyQuery), "delete the item with noLegacy");
119
120 CFReleaseNull(toUpdate);
121 CFReleaseNull(query);
122 CFReleaseNull(noLegacyQuery);
123 CFReleaseNull(syncQuery);
124 CFReleaseNull(syncAnyQuery);
125 }
126
127 int si_34_one_true_keychain(int argc, char *const *argv)
128 {
129 plan_tests(19);
130
131
132 tests();
133
134 return 0;
135 }