]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_keychain/regressions/kc-item-helpers.h
Security-59754.80.3.tar.gz
[apple/security.git] / OSX / libsecurity_keychain / regressions / kc-item-helpers.h
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 #include "kc-helpers.h"
25 #include "utilities/SecCFRelease.h"
26
27 #ifndef kc_item_helpers_h
28 #define kc_item_helpers_h
29
30 #if TARGET_OS_MAC
31
32 #pragma clang diagnostic push
33 #pragma clang diagnostic ignored "-Wunused-variable"
34 #pragma clang diagnostic ignored "-Wunused-function"
35
36 static CFMutableDictionaryRef makeBaseDictionary(CFStringRef itemclass) {
37 CFMutableDictionaryRef query = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
38 CFDictionaryAddValue(query, kSecReturnRef, kCFBooleanTrue);
39 CFDictionarySetValue(query, kSecClass, itemclass);
40
41 return query;
42 }
43
44 static CFMutableDictionaryRef convertToQuery(CFMutableDictionaryRef query, SecKeychainRef kc) {
45 CFMutableArrayRef searchList = (CFMutableArrayRef) CFArrayCreateMutable(kCFAllocatorDefault, 1, &kCFTypeArrayCallBacks);
46 CFArrayAppendValue((CFMutableArrayRef)searchList, kc);
47 CFDictionarySetValue(query, kSecMatchSearchList, searchList);
48
49 CFDictionarySetValue(query, kSecMatchLimit, kSecMatchLimitAll);
50
51 return query;
52 }
53
54 static CFMutableDictionaryRef addLabel(CFMutableDictionaryRef query, CFStringRef label) {
55 CFDictionarySetValue(query, kSecAttrLabel, label);
56 return query;
57 }
58
59 static CFMutableDictionaryRef createBaseItemDictionary(CFStringRef itemclass, CFStringRef service) {
60 CFMutableDictionaryRef query = makeBaseDictionary(itemclass);
61
62 if(CFEqual(itemclass, kSecClassInternetPassword)) {
63 CFDictionarySetValue(query, kSecAttrServer, service == NULL ? CFSTR("test_service") : service);
64 CFDictionarySetValue(query, kSecAttrAuthenticationType, CFSTR("dflt")); // Default, I guess?
65 } else {
66 // Generic passwords have services
67 CFDictionarySetValue(query, kSecAttrService, service == NULL ? CFSTR("test_service") : service);
68 }
69 return query;
70 }
71
72 static CFMutableDictionaryRef createQueryItemDictionaryWithService(SecKeychainRef kc, CFStringRef itemclass, CFStringRef service) {
73 return convertToQuery(createBaseItemDictionary(itemclass, service), kc);
74 }
75 static CFMutableDictionaryRef createQueryItemDictionary(SecKeychainRef kc, CFStringRef itemclass) {
76 return createQueryItemDictionaryWithService(kc, itemclass, NULL);
77 }
78
79 static CFMutableDictionaryRef makeBaseQueryDictionary(SecKeychainRef kc, CFStringRef itemclass) {
80 return convertToQuery(makeBaseDictionary(itemclass), kc);
81 }
82
83 static CFMutableDictionaryRef createQueryCustomItemDictionaryWithService(SecKeychainRef kc, CFStringRef itemclass, CFStringRef label, CFStringRef service) {
84 CFMutableDictionaryRef query = createQueryItemDictionaryWithService(kc, itemclass, service);
85 CFDictionarySetValue(query, kSecAttrLabel, label);
86 return query;
87 }
88 static CFMutableDictionaryRef createQueryCustomItemDictionary(SecKeychainRef kc, CFStringRef itemclass, CFStringRef label) {
89 return createQueryCustomItemDictionaryWithService(kc, itemclass, label, NULL);
90 }
91
92 static CFMutableDictionaryRef createAddCustomItemDictionaryWithService(SecKeychainRef kc, CFStringRef itemclass, CFStringRef label, CFStringRef account, CFStringRef service) {
93 CFMutableDictionaryRef query = createBaseItemDictionary(itemclass, service);
94
95 CFDictionaryAddValue(query, kSecUseKeychain, kc);
96 CFDictionarySetValue(query, kSecAttrAccount, account);
97 CFDictionarySetValue(query, kSecAttrComment, CFSTR("a comment"));
98 CFDictionarySetValue(query, kSecAttrLabel, label);
99 CFDataRef data = CFDataCreate(NULL, (void*)"data", 4);
100 CFDictionarySetValue(query, kSecValueData, data);
101 CFReleaseNull(data);
102 return query;
103 }
104 static CFMutableDictionaryRef createAddCustomItemDictionary(SecKeychainRef kc, CFStringRef itemclass, CFStringRef label, CFStringRef account) {
105 return createAddCustomItemDictionaryWithService(kc, itemclass, label, account, NULL);
106 }
107
108 static CFMutableDictionaryRef createAddItemDictionary(SecKeychainRef kc, CFStringRef itemclass, CFStringRef label) {
109 return createAddCustomItemDictionary(kc, itemclass, label, CFSTR("test_account"));
110 }
111
112 static SecKeychainItemRef createCustomItem(const char* name, SecKeychainRef kc, CFDictionaryRef CF_CONSUMED addDictionary) {
113 CFTypeRef result = NULL;
114 ok_status(SecItemAdd(addDictionary, &result), "%s: SecItemAdd", name);
115 ok(result != NULL, "%s: SecItemAdd returned a result", name);
116
117 SecKeychainItemRef item = (SecKeychainItemRef) result;
118 ok(item != NULL, "%s: Couldn't convert into SecKeychainItemRef", name);
119
120 return item;
121 }
122 #define createCustomItemTests 3
123
124 static SecKeychainItemRef makeItem(const char* name, SecKeychainRef kc, CFStringRef itemclass, CFStringRef label) {
125 CFMutableDictionaryRef query = createAddItemDictionary(kc, itemclass, label);
126
127 SecKeychainItemRef item = createCustomItem(name, kc, query);
128
129 CFReleaseNull(query);
130 return item;
131 }
132 #define makeItemTests createCustomItemTests
133
134 static void makeCustomDuplicateItem(const char* name, SecKeychainRef kc, CFStringRef itemclass, CFStringRef label) {
135 CFMutableDictionaryRef query = createAddItemDictionary(kc, itemclass, label);
136
137 CFTypeRef result = NULL;
138 is(SecItemAdd(query, &result), errSecDuplicateItem, "%s: SecItemAdd (duplicate)", name);
139
140 CFReleaseNull(query);
141 }
142 #define makeCustomDuplicateItemTests 1
143
144 static void makeDuplicateItem(const char* name, SecKeychainRef kc, CFStringRef itemclass) {
145 return makeCustomDuplicateItem(name, kc, itemclass, CFSTR("test_label"));
146 }
147 #define makeDuplicateItemTests makeCustomDuplicateItemTests
148
149
150 #pragma clang diagnostic pop
151 #else
152
153 #endif /* TARGET_OS_MAC */
154
155 #endif /* kc_item_helpers_h */