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>
35 #include <AssertMacros.h>
38 /* Test whether the one true keychain pertains to the iOS keychain and only to the iOS keychain. */
39 static void tests(void)
42 CFNumberRef eighty
= CFNumberCreate(NULL
, kCFNumberSInt32Type
, &v_eighty
);
43 const char *v_data
= "test";
44 const char *v_data2
= "test";
45 CFDataRef pwdata
= CFDataCreate(NULL
, (UInt8
*)v_data
, strlen(v_data
));
46 CFDataRef pwdata2
= CFDataCreate(NULL
, (UInt8
*)v_data2
, strlen(v_data2
));
47 CFMutableDictionaryRef query
= CFDictionaryCreateMutable(NULL
, 0, NULL
, NULL
);
48 CFTypeRef result
= NULL
;
49 CFDictionaryAddValue(query
, kSecClass
, kSecClassInternetPassword
);
50 CFDictionaryAddValue(query
, kSecAttrServer
, CFSTR("members.spamcop.net"));
51 CFDictionaryAddValue(query
, kSecAttrAccount
, CFSTR("smith"));
52 CFDictionaryAddValue(query
, kSecAttrPort
, eighty
);
53 CFDictionaryAddValue(query
, kSecAttrProtocol
, kSecAttrProtocolHTTP
);
54 CFDictionaryAddValue(query
, kSecAttrAuthenticationType
, kSecAttrAuthenticationTypeDefault
);
56 CFMutableDictionaryRef noLegacyQuery
= CFDictionaryCreateMutableCopy(NULL
, 0, query
);
57 CFMutableDictionaryRef syncAnyQuery
= CFDictionaryCreateMutableCopy(NULL
, 0, query
);
58 CFMutableDictionaryRef syncQuery
= CFDictionaryCreateMutableCopy(NULL
, 0, query
);
60 CFDictionaryAddValue(noLegacyQuery
, kSecAttrNoLegacy
, kCFBooleanTrue
);
61 CFDictionaryAddValue(syncAnyQuery
, kSecAttrSynchronizable
, kSecAttrSynchronizableAny
);
62 CFDictionaryAddValue(syncQuery
, kSecAttrSynchronizable
, kCFBooleanTrue
);
65 SecItemDelete(noLegacyQuery
);
66 SecItemDelete(syncQuery
);
67 SecItemDelete(syncAnyQuery
);
69 CFDictionaryAddValue(query
, kSecValueData
, pwdata
);
70 ok_status(SecItemAdd(query
, NULL
), "add internet password in OS X keychain");
71 CFDictionaryRemoveValue(query
, kSecValueData
);
73 ok_status(SecItemCopyMatching(query
, &result
), "find the osx item");
74 CFReleaseNull(result
);
75 is_status(SecItemCopyMatching(noLegacyQuery
, &result
), errSecItemNotFound
, "do not find the osx item with noLegacy");
76 CFReleaseNull(result
);
77 ok_status(SecItemCopyMatching(syncAnyQuery
, &result
), "find the osx item with synchronizableAny");
78 CFReleaseNull(result
);
79 is_status(SecItemCopyMatching(syncQuery
, &result
), errSecItemNotFound
, "do not find the osx item with synchronizable");
80 CFReleaseNull(result
);
82 CFMutableDictionaryRef toUpdate
= CFDictionaryCreateMutable(NULL
, 1, NULL
, NULL
);
84 CFDictionaryAddValue(toUpdate
, kSecValueData
, pwdata2
);
86 ok_status(SecItemUpdate(query
, toUpdate
), "update the osx item");
87 is_status(SecItemUpdate(noLegacyQuery
, toUpdate
), errSecItemNotFound
, "do not update the osx item with noLegacy");
88 ok_status(SecItemUpdate(syncAnyQuery
, toUpdate
), "update the osx item with synchronizableAny");
90 is_status(SecItemDelete(noLegacyQuery
), errSecItemNotFound
, "do not delete the osx item with noLegacy");
91 ok_status(SecItemDelete(syncAnyQuery
), "delete the osx item with synchronizableAny");
96 CFDictionaryAddValue(noLegacyQuery
, kSecValueData
, pwdata
);
97 ok_status(SecItemAdd(noLegacyQuery
, &result
), "add internet password in iOS keychain");
98 CFDictionaryRemoveValue(noLegacyQuery
, kSecValueData
);
100 is_status(SecItemCopyMatching(query
, &result
), errSecItemNotFound
, "do not find the ios item");
101 CFReleaseNull(result
);
102 ok_status(SecItemCopyMatching(noLegacyQuery
, &result
), "find the ios item with noLegacy");
103 CFReleaseNull(result
);
104 is_status(SecItemCopyMatching(syncAnyQuery
, &result
), errSecItemNotFound
, "do not find the ios item with synchronizableAny");
105 CFReleaseNull(result
);
106 is_status(SecItemCopyMatching(syncQuery
, &result
), errSecItemNotFound
, "do not find the ios item with synchronizable");
107 CFReleaseNull(result
);
109 is_status(SecItemUpdate(query
, toUpdate
), errSecItemNotFound
, "do not update the ios item");
110 ok_status(SecItemUpdate(noLegacyQuery
, toUpdate
), "update the ios item with noLegacy");
111 ok_status(SecItemUpdate(syncAnyQuery
, toUpdate
), "update the ios item with synchronizableAny");
113 CFDictionaryRemoveValue(noLegacyQuery
, kSecValueData
);
115 ok_status(SecItemDelete(noLegacyQuery
), "delete the item with noLegacy");
119 int si_34_one_true_keychain(int argc
, char *const *argv
)