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@
24 #include <CoreFoundation/CoreFoundation.h>
25 #include <TargetConditionals.h>
28 #include "keychain_regressions.h"
29 #include <utilities/SecCFRelease.h>
31 #include <Security/SecBase.h>
32 #include <Security/SecItem.h>
33 #include <Security/SecItemPriv.h>
34 #include <utilities/SecCFRelease.h>
36 #include <AssertMacros.h>
39 /* Test whether the one true keychain pertains to the iOS keychain and only to the iOS keychain. */
40 static void tests(void)
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
);
57 CFMutableDictionaryRef noLegacyQuery
= CFDictionaryCreateMutableCopy(NULL
, 0, query
);
58 CFMutableDictionaryRef syncAnyQuery
= CFDictionaryCreateMutableCopy(NULL
, 0, query
);
59 CFMutableDictionaryRef syncQuery
= CFDictionaryCreateMutableCopy(NULL
, 0, query
);
61 CFDictionaryAddValue(noLegacyQuery
, kSecAttrNoLegacy
, kCFBooleanTrue
);
62 CFDictionaryAddValue(syncAnyQuery
, kSecAttrSynchronizable
, kSecAttrSynchronizableAny
);
63 CFDictionaryAddValue(syncQuery
, kSecAttrSynchronizable
, kCFBooleanTrue
);
66 SecItemDelete(noLegacyQuery
);
67 SecItemDelete(syncQuery
);
68 SecItemDelete(syncAnyQuery
);
70 CFDictionaryAddValue(query
, kSecValueData
, pwdata
);
71 ok_status(SecItemAdd(query
, NULL
), "add internet password in OS X keychain");
72 CFDictionaryRemoveValue(query
, kSecValueData
);
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
);
83 CFMutableDictionaryRef toUpdate
= CFDictionaryCreateMutable(NULL
, 1, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
85 CFDictionaryAddValue(toUpdate
, kSecValueData
, pwdata2
);
86 CFReleaseNull(pwdata2
);
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");
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");
98 CFDictionaryAddValue(noLegacyQuery
, kSecValueData
, pwdata
);
99 CFReleaseNull(pwdata
);
100 ok_status(SecItemAdd(noLegacyQuery
, &result
), "add internet password in iOS keychain");
101 CFDictionaryRemoveValue(noLegacyQuery
, kSecValueData
);
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
);
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");
116 CFDictionaryRemoveValue(noLegacyQuery
, kSecValueData
);
118 ok_status(SecItemDelete(noLegacyQuery
), "delete the item with noLegacy");
120 CFReleaseNull(toUpdate
);
121 CFReleaseNull(query
);
122 CFReleaseNull(noLegacyQuery
);
123 CFReleaseNull(syncQuery
);
124 CFReleaseNull(syncAnyQuery
);
127 int si_34_one_true_keychain(int argc
, char *const *argv
)