]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_keychain/regressions/kc-30-xara.c
Security-57740.1.18.tar.gz
[apple/security.git] / OSX / libsecurity_keychain / regressions / kc-30-xara.c
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 "keychain_regressions.h"
25
26 #include <CoreFoundation/CoreFoundation.h>
27 #include <Security/Security.h>
28 #include <Security/SecBase.h>
29 #include <Security/SecBasePriv.h>
30 #include <Security/SecKeychainPriv.h>
31 #include <TargetConditionals.h>
32 #include <Security/cssmapi.h>
33 #include <stdlib.h>
34 #include <sys/stat.h>
35 #include <copyfile.h>
36 #include <unistd.h>
37
38 #include "kc-30-xara-item-helpers.h"
39 #include "kc-30-xara-key-helpers.h"
40 #include "kc-30-xara-upgrade-helpers.h"
41
42 #if TARGET_OS_MAC
43
44 #pragma clang diagnostic push
45 #pragma clang diagnostic ignored "-Wunused-variable"
46 #pragma clang diagnostic ignored "-Wunused-function"
47
48 /* Test basic add delete update copy matching stuff. */
49
50
51 /* Standard memory functions required by CSSM. */
52 static void *cssmMalloc(CSSM_SIZE size, void *allocRef) { return malloc(size); }
53 static void cssmFree(void *mem_ptr, void *allocRef) { free(mem_ptr); return; }
54 static void *cssmRealloc(void *ptr, CSSM_SIZE size, void *allocRef) { return realloc( ptr, size ); }
55 static void *cssmCalloc(uint32 num, CSSM_SIZE size, void *allocRef) { return calloc( num, size ); }
56 static CSSM_API_MEMORY_FUNCS memFuncs = { cssmMalloc, cssmFree, cssmRealloc, cssmCalloc, NULL };
57
58 static CSSM_DL_DB_HANDLE initializeDL() {
59 CSSM_VERSION version = { 2, 0 };
60 CSSM_DL_DB_HANDLE dldbHandle = { 0, 0 };
61 CSSM_GUID myGuid = { 0xFADE, 0, 0, { 1, 2, 3, 4, 5, 6, 7, 0 } };
62 CSSM_PVC_MODE pvcPolicy = CSSM_PVC_NONE;
63
64 ok_status(CSSM_Init(&version, CSSM_PRIVILEGE_SCOPE_NONE, &myGuid, CSSM_KEY_HIERARCHY_NONE, &pvcPolicy, NULL), "cssm_init");
65 ok_status(CSSM_ModuleLoad(&gGuidAppleFileDL, CSSM_KEY_HIERARCHY_NONE, NULL, NULL), "module_load");
66 ok_status(CSSM_ModuleAttach(&gGuidAppleFileDL, &version, &memFuncs, 0, CSSM_SERVICE_DL, 0, CSSM_KEY_HIERARCHY_NONE, NULL, 0, NULL, &dldbHandle.DLHandle), "module_attach");
67
68 return dldbHandle;
69 }
70 #define initializeDLTests 3
71
72 static void unloadDL(CSSM_DL_DB_HANDLE* dldbHandle) {
73 ok_status(CSSM_ModuleDetach(dldbHandle->DLHandle), "detach");
74 ok_status(CSSM_ModuleUnload(&gGuidAppleFileDL, NULL, NULL), "unload");
75 ok_status(CSSM_Terminate(), "terminate");
76 }
77 #define unloadDLTests 3
78
79 static void modifyAttributeInKeychain(char * name, CSSM_DL_DB_HANDLE dldbHandle, char * keychainName, CSSM_DB_RECORDTYPE recordType, char* attributeName, char* newValue, size_t len) {
80 CSSM_RETURN status = CSSM_OK;
81 ok_status(CSSM_DL_DbOpen(dldbHandle.DLHandle, keychainName,
82 NULL,
83 CSSM_DB_ACCESS_READ | CSSM_DB_ACCESS_WRITE,
84 NULL, /* Access cred? */
85 NULL, /* Open Parameters? */
86 &dldbHandle.DBHandle), "%s: CSSM_DL_DbOpen", name);
87
88 CSSM_QUERY queryAll = {};
89 queryAll.RecordType = recordType;
90
91 CSSM_HANDLE results = 0;
92 CSSM_DATA data = {};
93 CSSM_DB_UNIQUE_RECORD_PTR uniqueIdPtr = NULL;
94
95 CSSM_DB_RECORD_ATTRIBUTE_DATA attributes = {};
96 attributes.NumberOfAttributes = 1;
97 attributes.AttributeData = malloc(sizeof(CSSM_DB_ATTRIBUTE_DATA) * attributes.NumberOfAttributes);
98 attributes.AttributeData[0].Info.Label.AttributeName = attributeName;
99 attributes.AttributeData[0].Info.AttributeNameFormat = CSSM_DB_ATTRIBUTE_NAME_AS_STRING;
100
101 attributes.AttributeData[0].NumberOfValues = 1;
102 attributes.AttributeData[0].Value = malloc(sizeof(CSSM_DATA)*attributes.AttributeData[0].NumberOfValues);
103
104
105 status = CSSM_DL_DataGetFirst(dldbHandle, &queryAll, &results, &attributes, &data, &uniqueIdPtr);
106 while(status == CSSM_OK) {
107 // I'm sure it has one thing and that thing needs to change.
108 attributes.AttributeData[0].Value[0].Data = (void*)newValue;
109 attributes.AttributeData[0].Value[0].Length = strlen(newValue);
110
111 CSSM_DL_DataModify(dldbHandle,
112 attributes.DataRecordType,
113 uniqueIdPtr,
114 &attributes,
115 NULL, // no data modification
116 CSSM_DB_MODIFY_ATTRIBUTE_REPLACE);
117
118 CSSM_DL_FreeUniqueRecord(dldbHandle, uniqueIdPtr);
119 status = CSSM_DL_DataGetNext(dldbHandle, results, &attributes, &data, &uniqueIdPtr);
120 }
121 ok_status(CSSM_DL_DbClose(dldbHandle), "%s: CSSM_DL_DbClose", name);
122 }
123 #define modifyAttributeInKeychainTests 2
124
125 static void testAttackItem(CSSM_DL_DB_HANDLE dldbHandle) {
126 char * name = "testAttackItem";
127 secnotice("integrity", "************************************* %s", name);
128
129 SecKeychainRef kc = newKeychain(name);
130 checkN(name, makeQueryItemDictionary(kc, kSecClassGenericPassword), 0);
131
132 makeItemWithIntegrity(name, kc, kSecClassGenericPassword, CFSTR("265438ea6807b509c9c6962df3f5033fd1af118f76c5f550e3ed90cb0d3ffce4"));
133 SecKeychainItemRef item = checkN(name, makeQueryItemDictionary(kc, kSecClassGenericPassword), 1);
134 CFReleaseNull(item);
135 CFReleaseNull(kc);
136
137 char * modification = "evil_application";
138 modifyAttributeInKeychain(name, dldbHandle, keychainDbFile, CSSM_DL_DB_RECORD_GENERIC_PASSWORD, "PrintName", modification, strlen(modification));
139
140 kc = openKeychain(name);
141 item = checkN(name, makeQueryItemDictionary(kc, kSecClassGenericPassword), 1);
142 readPasswordContentsWithResult(item, errSecInvalidItemRef, NULL);
143
144 ok_status(SecKeychainDelete(kc), "%s: SecKeychainDelete", name);
145 CFReleaseNull(kc);
146 }
147 #define testAttackItemTests (newKeychainTests + checkNTests + makeItemWithIntegrityTests + checkNTests + modifyAttributeInKeychainTests + openKeychainTests + checkNTests + readPasswordContentsWithResultTests + 1)
148
149 static void testAttackKey(CSSM_DL_DB_HANDLE dldbHandle) {
150 char * name = "testAttackKey";
151 secnotice("integrity", "************************************* %s", name);
152
153 SecKeychainRef kc = newKeychain(name);
154 checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassSymmetric), 0);
155
156 makeKeyWithIntegrity(name, kc, CFSTR("44f10f6bb508d47f8905859efc06eaee500304bc4da408b1f4d2a58c6502147b"));
157 SecKeychainItemRef item = checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassSymmetric), 1);
158
159 checkKeyUse((SecKeyRef) item, errSecSuccess);
160
161 CFReleaseNull(item);
162 CFReleaseNull(kc);
163
164 char * modification = "evil_application";
165 modifyAttributeInKeychain(name, dldbHandle, keychainDbFile, CSSM_DL_DB_RECORD_SYMMETRIC_KEY, "Label", modification, strlen(modification));
166
167 kc = openKeychain(name);
168 item = checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassSymmetric), 1);
169 checkKeyUse((SecKeyRef) item, errSecInvalidItemRef);
170 ok_status(SecKeychainDelete(kc), "%s: SecKeychainDelete", name);
171 CFReleaseNull(kc);
172 }
173 #define testAttackKeyTests (newKeychainTests + checkNTests + makeKeyWithIntegrityTests + checkNTests + checkKeyUseTests + modifyAttributeInKeychainTests \
174 + openKeychainTests + checkNTests + checkKeyUseTests + 1)
175
176
177 static void testAddAfterCorruptItem(CSSM_DL_DB_HANDLE dldbHandle) {
178 char * name = "testAddAfterCorruptItem";
179 secnotice("integrity", "************************************* %s", name);
180
181 SecKeychainRef kc = newKeychain(name);
182 checkN(name, makeQueryItemDictionary(kc, kSecClassGenericPassword), 0);
183
184 makeCustomItemWithIntegrity(name, kc, kSecClassGenericPassword, CFSTR("test_label"), CFSTR("265438ea6807b509c9c6962df3f5033fd1af118f76c5f550e3ed90cb0d3ffce4"));
185 SecKeychainItemRef item = checkN(name, makeQueryItemDictionary(kc, kSecClassGenericPassword), 1);
186 CFReleaseNull(item);
187
188 makeDuplicateItem(name, kc, kSecClassGenericPassword);
189 CFReleaseNull(kc);
190
191 char * modification = "evil_application";
192 modifyAttributeInKeychain(name, dldbHandle, keychainDbFile, CSSM_DL_DB_RECORD_GENERIC_PASSWORD, "PrintName", modification, strlen(modification));
193
194 kc = openKeychain(name);
195 item = checkN(name, makeQueryItemDictionary(kc, kSecClassGenericPassword), 1);
196 deleteItem(item);
197 checkN(name, makeQueryItemDictionary(kc, kSecClassGenericPassword), 0);
198
199 makeCustomItemWithIntegrity(name, kc, kSecClassGenericPassword, CFSTR("evil_application"), CFSTR("d2aa97b30a1f96f9e61fcade2b00d9f4284976a83a5b68392251ee5ec827f8cc"));
200 checkN(name, makeQueryItemDictionary(kc, kSecClassGenericPassword), 1);
201 makeCustomDuplicateItem(name, kc, kSecClassGenericPassword, CFSTR("evil_application"));
202 ok_status(SecKeychainDelete(kc), "%s: SecKeychainDelete", name);
203 CFReleaseNull(kc);
204 }
205 #define testAddAfterCorruptItemTests (newKeychainTests + checkNTests + makeCustomItemWithIntegrityTests + checkNTests + makeDuplicateItemTests \
206 + modifyAttributeInKeychainTests + openKeychainTests + checkNTests + deleteItemTests \
207 + checkNTests + makeCustomItemWithIntegrityTests + checkNTests + makeCustomDuplicateItemTests + 1)
208
209 static void testAddAfterCorruptKey(CSSM_DL_DB_HANDLE dldbHandle) {
210 char * name = "testAddAfterCorruptKey";
211 secnotice("integrity", "************************************* %s", name);
212
213 SecKeychainRef kc = newKeychain(name);
214 checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassSymmetric), 0);
215 checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassPublic), 0);
216 checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassPrivate), 0);
217
218 // Make a symmetric key
219 makeCustomKeyWithIntegrity(name, kc, CFSTR("test_key"), CFSTR("44f10f6bb508d47f8905859efc06eaee500304bc4da408b1f4d2a58c6502147b"));
220
221 SecKeychainItemRef item = checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassSymmetric), 1);
222 makeCustomDuplicateKey(name, kc, CFSTR("test_key"));
223 CFReleaseNull(item);
224
225 // Make a key pair
226 SecKeyRef pub;
227 SecKeyRef priv;
228 makeCustomKeyPair(name, kc, CFSTR("test_key_pair"), &pub, &priv);
229 checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassPublic), 1);
230 checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassPrivate), 1);
231 CFReleaseNull(pub);
232 CFReleaseNull(priv);
233
234 ok_status(SecKeychainListRemoveKeychain(&kc), "%s: SecKeychainListRemoveKeychain", name);
235
236 char * modification = "evil_application";
237 modifyAttributeInKeychain(name, dldbHandle, keychainDbFile, CSSM_DL_DB_RECORD_SYMMETRIC_KEY, "PrintName", modification, strlen(modification));
238 modifyAttributeInKeychain(name, dldbHandle, keychainDbFile, CSSM_DL_DB_RECORD_PUBLIC_KEY, "PrintName", modification, strlen(modification));
239 modifyAttributeInKeychain(name, dldbHandle, keychainDbFile, CSSM_DL_DB_RECORD_PRIVATE_KEY, "PrintName", modification, strlen(modification));
240
241 kc = openKeychain(name);
242
243 item = checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassSymmetric), 1);
244 deleteItem(item);
245 checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassSymmetric), 0);
246
247 item = checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassPublic), 1);
248 deleteItem(item);
249 checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassPublic), 0);
250
251 item = checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassPrivate), 1);
252 deleteItem(item);
253 checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassPrivate), 0);
254
255 makeCustomKeyWithIntegrity(name, kc, CFSTR("evil_application"), CFSTR("ca6d90a0b053113e43bbb67f64030230c96537f77601f66bdf821d8684431dfc"));
256 item = checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassSymmetric), 1);
257
258 makeCustomDuplicateKey(name, kc, CFSTR("evil_application"));
259
260 makeCustomKeyPair(name, kc, CFSTR("evil_application"), &pub, &priv);
261 checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassPublic), 1);
262 checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassPrivate), 1);
263
264 // We cannot create a duplicate key pair, so don't try.
265
266 CFReleaseNull(item);
267 ok_status(SecKeychainDelete(kc), "%s: SecKeychainDelete", name);
268 CFReleaseNull(kc);
269 }
270 #define testAddAfterCorruptKeyTests (newKeychainTests \
271 + checkNTests + checkNTests + checkNTests \
272 + makeCustomKeyWithIntegrityTests + checkNTests + makeCustomDuplicateKeyTests \
273 + makeCustomKeyPairTests + checkNTests + checkNTests \
274 + 1 \
275 + modifyAttributeInKeychainTests \
276 + modifyAttributeInKeychainTests \
277 + modifyAttributeInKeychainTests \
278 + openKeychainTests \
279 + checkNTests + deleteItemTests + checkNTests \
280 + checkNTests + deleteItemTests + checkNTests \
281 + checkNTests + deleteItemTests + checkNTests \
282 + makeCustomKeyWithIntegrityTests + checkNTests \
283 + makeCustomDuplicateKeyTests \
284 + makeCustomKeyPairTests + checkNTests + checkNTests \
285 + 1)
286
287
288 // These constants are in CommonBlob, but we're in C and can't access them
289 #define version_MacOS_10_0 0x00000100
290 #define version_partition 0x00000200
291
292 static void testKeychainUpgrade() {
293 char name[100];
294 sprintf(name, "testKeychainUpgrade");
295 secnotice("integrity", "************************************* %s", name);
296 UInt32 version;
297 char* path = malloc(sizeof(char) * 400);
298 UInt32 len = 400;
299
300 // To test multi-threading, we want the upgrade to take a while. Add a bunch of passwords...
301 char oldkcFile[100];
302 sprintf(oldkcFile, "%s/Library/test.keychain", getenv("HOME"));
303 unlink(oldkcFile);
304 writeOldKeychain(name, oldkcFile);
305
306 SecKeychainRef kc = openCustomKeychain(name, oldkcFile, "password");
307
308 for(int i = 0; i < 200; i++) {
309 CFTypeRef result = NULL;
310 CFStringRef cflabel = CFStringCreateWithFormat(NULL, NULL, CFSTR("item%d"), i);
311 CFMutableDictionaryRef query = makeAddCustomItemDictionaryWithService(kc, kSecClassInternetPassword, cflabel, cflabel, CFSTR("no service"));
312 SecItemAdd(query, &result); // don't particuluarly care if this fails...
313 CFReleaseNull(query);
314 CFReleaseNull(cflabel);
315 CFReleaseNull(result);
316 }
317
318 CFReleaseNull(kc);
319
320 ok_status(copyfile(oldkcFile, keychainFile, NULL, COPYFILE_UNLINK | COPYFILE_ALL), "%s: copyfile", name);
321 unlink(oldkcFile);
322 unlink(keychainDbFile);
323
324 static dispatch_once_t onceToken = 0;
325 static dispatch_queue_t release_queue = NULL;
326 dispatch_once(&onceToken, ^{
327 release_queue = dispatch_queue_create("com.apple.security.keychain-upgrade-queue", DISPATCH_QUEUE_CONCURRENT);
328 });
329
330 dispatch_group_t g = dispatch_group_create();
331 SecKeychainItemRef item;
332
333 char* __block blockName = NULL;
334 asprintf(&blockName, "%s", name);
335
336 kc = openCustomKeychain(name, keychainName, "password");
337
338 // Directly after an upgrade, no items should have partition ID lists
339 dispatch_group_async(g, release_queue, ^() {
340 secerror("beginning 1\n");
341 SecKeychainRef blockKc;
342 SecKeychainOpen(keychainName, &blockKc);
343 SecKeychainItemRef item = checkN(blockName, makeQueryItemDictionary(blockKc, kSecClassGenericPassword), 1);
344 checkIntegrityHash(blockName, item, CFSTR("39c56eadd3e3b496b6099e5f3d5ff88eaee9ca2e3a50c1be8319807a72e451e5"));
345 checkPartitionIDs(blockName, item, 0);
346 CFReleaseSafe(blockKc);
347 CFReleaseSafe(item);
348 secerror("ending 1\n");
349 });
350
351 dispatch_group_async(g, release_queue, ^() {
352 usleep(0.1 * USEC_PER_SEC); // use different timings to try to find multithreaded upgrade bugs
353 secerror("beginning 2\n");
354 SecKeychainRef blockKc;
355 SecKeychainOpen(keychainName, &blockKc);
356 SecKeychainItemRef item = checkN(blockName, makeQueryItemDictionaryWithService(blockKc, kSecClassInternetPassword, CFSTR("test_service")), 1);
357 checkIntegrityHash(blockName, item, CFSTR("4f1b64e3c156968916e72d8ff3f1a8eb78b32abe0b2b43f0578eb07c722aaf03"));
358 checkPartitionIDs(blockName, item, 0);
359 CFReleaseSafe(blockKc);
360 CFReleaseSafe(item);
361 secerror("ending 2\n");
362 });
363
364 dispatch_group_async(g, release_queue, ^() {
365 usleep(0.3 * USEC_PER_SEC);
366 secerror("beginning 3\n");
367 SecKeychainRef blockKc;
368 SecKeychainOpen(keychainName, &blockKc);
369 SecKeychainItemRef item = checkN(blockName, makeQueryKeyDictionary(blockKc, kSecAttrKeyClassSymmetric), 1);
370 checkIntegrityHash(blockName, (SecKeychainItemRef) item, CFSTR("44f10f6bb508d47f8905859efc06eaee500304bc4da408b1f4d2a58c6502147b"));
371 checkPartitionIDs(blockName, (SecKeychainItemRef) item, 0);
372 CFReleaseSafe(blockKc);
373 CFReleaseSafe(item);
374 secerror("ending 3\n");
375 });
376
377 dispatch_group_async(g, release_queue, ^() {
378 usleep(0.5 * USEC_PER_SEC);
379 secerror("beginning 4\n");
380 SecKeychainRef blockKc;
381 SecKeychainOpen(keychainName, &blockKc);
382 SecKeychainItemRef item = checkN(blockName, makeQueryKeyDictionary(blockKc, kSecAttrKeyClassPublic), 1);
383 checkIntegrityHash(blockName, (SecKeychainItemRef) item, CFSTR("42d29fd5e9935edffcf6d0261eabddb00782ec775caa93716119e8e553ab5578"));
384 checkPartitionIDs(blockName, (SecKeychainItemRef) item, 0);
385 CFReleaseSafe(blockKc);
386 CFReleaseSafe(item);
387 secerror("ending 4\n");
388 });
389
390 dispatch_group_async(g, release_queue, ^() {
391 usleep(1 * USEC_PER_SEC);
392 secerror("beginning 5\n");
393 SecKeychainRef blockKc;
394 SecKeychainOpen(keychainName, &blockKc);
395 SecKeychainItemRef item = checkN(blockName, makeQueryKeyDictionary(blockKc, kSecAttrKeyClassPrivate), 1);
396 checkIntegrityHash(blockName, (SecKeychainItemRef) item, CFSTR("bdf219cdbc2dc6c4521cf39d1beda2e3491ef0330ba59eb41229dd909632f48d"));
397 checkPartitionIDs(blockName, (SecKeychainItemRef) item, 0);
398 CFReleaseSafe(blockKc);
399 CFReleaseSafe(item);
400 secerror("ending 5\n");
401 });
402
403 dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
404
405 // @@@ I'm worried that there are still some thread issues in AppleDatabase; if these are run in the blocks above
406 // you can sometimes get CSSMERR_DL_INVALID_RECORD_UID/errSecInvalidRecord instead of errSecDuplicateItem
407 // <rdar://problem/27085024> Multi-threading duplicate item creation sometimes returns -67701
408 makeCustomDuplicateItem(name, kc, kSecClassGenericPassword, CFSTR("test_generic"));
409 makeCustomDuplicateItem(name, kc, kSecClassInternetPassword, CFSTR("test_internet"));
410
411 // Check the keychain's version and path
412 ok_status(SecKeychainGetKeychainVersion(kc, &version), "%s: SecKeychainGetKeychainVersion", name);
413 is(version, version_partition, "%s: version of upgraded keychain is incorrect", name);
414 ok_status(SecKeychainGetPath(kc, &len, path), "%s: SecKeychainGetKeychainPath", name);
415 eq_stringn(path, len, keychainDbFile, strlen(keychainDbFile), "%s: paths do not match", name);
416 free(path);
417
418 // Now close the keychain and open it again
419 CFReleaseNull(kc);
420 kc = openCustomKeychain(name, keychainName, "password");
421
422 item = checkN(name, makeQueryItemDictionary(kc, kSecClassGenericPassword), 1);
423 checkIntegrityHash(name, item, CFSTR("39c56eadd3e3b496b6099e5f3d5ff88eaee9ca2e3a50c1be8319807a72e451e5"));
424 checkPartitionIDs(name, item, 0);
425 makeCustomDuplicateItem(name, kc, kSecClassGenericPassword, CFSTR("test_generic"));
426
427 item = checkN(name, makeQueryItemDictionary(kc, kSecClassInternetPassword), 1);
428 checkIntegrityHash(name, item, CFSTR("4f1b64e3c156968916e72d8ff3f1a8eb78b32abe0b2b43f0578eb07c722aaf03"));
429 checkPartitionIDs(name, item, 0);
430 makeCustomDuplicateItem(name, kc, kSecClassInternetPassword, CFSTR("test_internet"));
431
432 item = checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassSymmetric), 1);
433 checkIntegrityHash(name, (SecKeychainItemRef) item, CFSTR("44f10f6bb508d47f8905859efc06eaee500304bc4da408b1f4d2a58c6502147b"));
434 checkPartitionIDs(name, (SecKeychainItemRef) item, 0);
435
436 item = checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassPublic), 1);
437 checkIntegrityHash(name, (SecKeychainItemRef) item, CFSTR("42d29fd5e9935edffcf6d0261eabddb00782ec775caa93716119e8e553ab5578"));
438 checkPartitionIDs(name, (SecKeychainItemRef) item, 0);
439
440 item = checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassPrivate), 1);
441 checkIntegrityHash(name, (SecKeychainItemRef) item, CFSTR("bdf219cdbc2dc6c4521cf39d1beda2e3491ef0330ba59eb41229dd909632f48d"));
442 checkPartitionIDs(name, (SecKeychainItemRef) item, 0);
443
444 ok_status(SecKeychainDelete(kc), "%s: SecKeychainDelete", name);
445 CFReleaseNull(kc);
446
447 // make sure we clean up any files left over
448 unlink(keychainDbFile);
449 unlink(keychainFile);
450 unlink(oldkcFile);
451 }
452 #define testKeychainUpgradeTests (openCustomKeychainTests + 1 + openCustomKeychainTests + 4 \
453 + checkNTests + checkIntegrityHashTests + checkPartitionIDsTests + makeCustomDuplicateItemTests \
454 + checkNTests + checkIntegrityHashTests + checkPartitionIDsTests + makeCustomDuplicateItemTests \
455 + checkNTests + checkIntegrityHashTests + checkPartitionIDsTests \
456 + checkNTests + checkIntegrityHashTests + checkPartitionIDsTests \
457 + checkNTests + checkIntegrityHashTests + checkPartitionIDsTests \
458 + openCustomKeychainTests \
459 + checkNTests + checkIntegrityHashTests + checkPartitionIDsTests + makeCustomDuplicateItemTests \
460 + checkNTests + checkIntegrityHashTests + checkPartitionIDsTests + makeCustomDuplicateItemTests \
461 + checkNTests + checkIntegrityHashTests + checkPartitionIDsTests \
462 + checkNTests + checkIntegrityHashTests + checkPartitionIDsTests \
463 + checkNTests + checkIntegrityHashTests + checkPartitionIDsTests \
464 + 1)
465
466 // tests that SecKeychainCreate over an old .keychain file returns an empty keychain
467 static void testKeychainCreateOver() {
468 char name[100];
469 sprintf(name, "testKeychainCreateOver");
470 secnotice("integrity", "************************************* %s", name);
471 UInt32 version;
472 char* path = malloc(sizeof(char) * 400);
473 UInt32 len = 400;
474
475 writeOldKeychain(name, keychainFile);
476 unlink(keychainDbFile);
477
478 SecKeychainItemRef item = NULL;
479
480 // Check that we upgrade on SecKeychainOpen
481 SecKeychainRef kc = openCustomKeychain(name, keychainName, "password");
482
483 item = checkN(name, makeQueryItemDictionary(kc, kSecClassGenericPassword), 1);
484 checkIntegrityHash(name, item, CFSTR("39c56eadd3e3b496b6099e5f3d5ff88eaee9ca2e3a50c1be8319807a72e451e5"));
485
486 ok_status(SecKeychainDelete(kc));
487 CFReleaseNull(kc);
488
489 // the old file should still exist, but the -db file should not.
490 struct stat filebuf;
491 is(stat(keychainFile, &filebuf), 0, "%s: check %s exists", name, keychainFile);
492 isnt(stat(keychainDbFile, &filebuf), 0, "%s: check %s does not exist", name, keychainDbFile);
493
494 // Now create a new keychain over the old remnants.
495 ok_status(SecKeychainCreate(keychainFile, (UInt32) strlen("password"), "password", false, NULL, &kc), "%s: SecKeychainCreate", name);
496
497 // Directly after creating a keychain, there shouldn't be any items (even though an old keychain exists underneath)
498 item = checkN(name, makeQueryItemDictionary(kc, kSecClassGenericPassword), 0);
499 item = checkN(name, makeQueryItemDictionary(kc, kSecClassInternetPassword), 0);
500 item = checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassSymmetric), 0);
501 item = checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassPublic), 0);
502 item = checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassPrivate), 0);
503
504 // Check the keychain's version and path
505 ok_status(SecKeychainGetKeychainVersion(kc, &version), "%s: SecKeychainGetKeychainVersion", name);
506 is(version, version_partition, "%s: version of upgraded keychain is incorrect", name);
507 ok_status(SecKeychainGetPath(kc, &len, path), "%s: SecKeychainGetKeychainPath", name);
508 eq_stringn(path, len, keychainDbFile, strlen(keychainDbFile), "%s: paths do not match", name);
509 free(path);
510
511 ok_status(SecKeychainDelete(kc), "%s: SecKeychainDelete", name);
512 CFReleaseNull(kc);
513
514 // final check that the files on-disk are as we expect
515 is(stat(keychainFile, &filebuf), 0, "%s: check %s exists", name, keychainFile);
516 isnt(stat(keychainDbFile, &filebuf), 0, "%s: check %s does not exist", name, keychainDbFile);
517
518 // make sure we clean up any files left over
519 unlink(keychainDbFile);
520 unlink(keychainFile);
521 }
522 #define testKeychainCreateOverTests (openCustomKeychainTests + \
523 + checkNTests + checkIntegrityHashTests \
524 + 1 + 2 + 1 \
525 + checkNTests \
526 + checkNTests \
527 + checkNTests \
528 + checkNTests \
529 + checkNTests \
530 + 4 + 1 + 2)
531
532 static void testKeychainDowngrade() {
533 char *name = "testKeychainDowngrade";
534 secnotice("integrity", "************************************* %s", name);
535
536 // For now, don't worry about filenames
537 writeFullV512Keychain(name, keychainDbFile);
538 unlink(keychainFile);
539 writeFullV512Keyfile(name, keychainTempFile);
540
541 SecKeychainRef kc = openCustomKeychain(name, keychainName, "password");
542 UInt32 version;
543
544 ok_status(SecKeychainGetKeychainVersion(kc, &version), "%s: SecKeychainGetKeychainVersion", name);
545 is(version, version_partition, "%s: version of initial keychain is incorrect", name);
546
547 SecKeychainItemRef item;
548
549 item = checkN(name, makeQueryItemDictionary(kc, kSecClassGenericPassword), 1);
550 checkIntegrityHash(name, item, CFSTR("6ba8d9f77ddba54d9373b11ae5c8f7b55a5e81da27e05e86723eeceb0a9a8e0c"));
551 makeCustomDuplicateItem(name, kc, kSecClassGenericPassword, CFSTR("test_generic"));
552
553 item = checkN(name, makeQueryItemDictionary(kc, kSecClassInternetPassword), 1);
554 checkIntegrityHash(name, item, CFSTR("630a9fe4f0191db8a99d6e8455e7114f628ce8f0f9eb3559efa572a98877a2b2"));
555 makeCustomDuplicateItem(name, kc, kSecClassInternetPassword, CFSTR("test_internet"));
556
557 item = checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassSymmetric), 1);
558 checkIntegrityHash(name, (SecKeychainItemRef) item, CFSTR("44f10f6bb508d47f8905859efc06eaee500304bc4da408b1f4d2a58c6502147b"));
559
560 item = checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassPublic), 1);
561 checkIntegrityHash(name, (SecKeychainItemRef) item, CFSTR("d27ee2be4920d5b6f47f6b19696d09c9a6c1a5d80c6f148f778db27b4ba99d9a"));
562
563 item = checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassPrivate), 1);
564 checkIntegrityHash(name, (SecKeychainItemRef) item, CFSTR("4b3f7bd7f9e48dc71006ce670990aed9dba6d5089b84d4113121bab41d0a3228"));
565
566
567
568 ok_status(SecKeychainAttemptMigrationWithMasterKey(kc, version_MacOS_10_0, keychainTempFile), "%s: SecKeychainAttemptKeychainMigrationWithMasterKey", name);
569 ok_status(SecKeychainGetKeychainVersion(kc, &version), "%s: SecKeychainGetKeychainVersion", name);
570 is(version, version_MacOS_10_0, "%s: version of downgraded keychain is incorrect", name);
571
572 checkN(name, makeQueryItemDictionary(kc, kSecClassGenericPassword), 1);
573 makeCustomDuplicateItem(name, kc, kSecClassGenericPassword, CFSTR("test_generic"));
574 checkN(name, makeQueryItemDictionary(kc, kSecClassInternetPassword), 1);
575 makeCustomDuplicateItem(name, kc, kSecClassInternetPassword, CFSTR("test_internet"));
576
577 checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassSymmetric), 1);
578 checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassPublic), 1);
579 checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassPrivate), 1);
580
581 ok_status(SecKeychainDelete(kc), "%s: SecKeychainDelete", name);
582 CFReleaseNull(kc);
583
584 // make sure we clean up
585 unlink(keychainTempFile);
586 unlink(keychainDbFile);
587 unlink(keychainFile);
588 }
589 #define testKeychainDowngradeTests (openCustomKeychainTests + 2 \
590 + checkNTests + checkIntegrityHashTests + makeCustomDuplicateItemTests \
591 + checkNTests + checkIntegrityHashTests + makeCustomDuplicateItemTests \
592 + checkNTests + checkIntegrityHashTests +\
593 + checkNTests + checkIntegrityHashTests +\
594 + checkNTests + checkIntegrityHashTests +\
595 + 3 + \
596 + checkNTests + makeCustomDuplicateItemTests \
597 + checkNTests + makeCustomDuplicateItemTests \
598 + checkNTests \
599 + checkNTests \
600 + checkNTests \
601 + 1)\
602
603 // Test opening and upgrading a v256 keychain at a -db filename.
604 static void testKeychainWrongFile256() {
605 char name[100];
606 sprintf(name, "testKeychainWrongFile256");
607 secnotice("integrity", "************************************* %s", name);
608 UInt32 version;
609
610 unlink(keychainFile);
611 writeOldKeychain(name, keychainDbFile);
612
613 // Only keychainDb file should exist
614 struct stat filebuf;
615 isnt(stat(keychainFile, &filebuf), 0, "%s: %s exists and shouldn't", name, keychainFile);
616 is(stat(keychainDbFile, &filebuf), 0, "%s: %s does not exist", name, keychainDbFile);
617
618 SecKeychainRef kc = openCustomKeychain(name, keychainName, "password");
619
620 SecKeychainItemRef item;
621
622 // Iterate over the keychain to trigger upgrade
623 item = checkN(name, makeQueryItemDictionary(kc, kSecClassGenericPassword), 1);
624 makeCustomDuplicateItem(name, kc, kSecClassGenericPassword, CFSTR("test_generic"));
625
626 // We should have created keychainFile, check for it
627 is(stat(keychainFile, &filebuf), 0, "%s: %s does not exist", name, keychainFile);
628 is(stat(keychainDbFile, &filebuf), 0, "%s: %s does not exist", name, keychainDbFile);
629
630 // Check the keychain's version and path
631 char path[400];
632 UInt32 len = sizeof(path);
633
634 ok_status(SecKeychainGetKeychainVersion(kc, &version), "%s: SecKeychainGetKeychainVersion", name);
635 is(version, version_partition, "%s: version of re-upgraded keychain is incorrect", name);
636 ok_status(SecKeychainGetPath(kc, &len, path), "%s: SecKeychainGetPath", name);
637 eq_stringn(path, len, keychainDbFile, strlen(keychainDbFile), "%s: paths do not match", name);
638
639 item = checkN(name, makeQueryItemDictionary(kc, kSecClassGenericPassword), 1);
640 makeCustomDuplicateItem(name, kc, kSecClassGenericPassword, CFSTR("test_generic"));
641
642 item = checkN(name, makeQueryItemDictionary(kc, kSecClassInternetPassword), 1);
643 makeCustomDuplicateItem(name, kc, kSecClassInternetPassword, CFSTR("test_internet"));
644
645 item = checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassSymmetric), 1);
646 item = checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassPublic), 1);
647 item = checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassPrivate), 1);
648
649 ok_status(SecKeychainDelete(kc), "%s: SecKeychainDelete", name);
650 CFReleaseNull(kc);
651
652 // make sure we clean up any files left over
653 unlink(keychainDbFile);
654 unlink(keychainFile);
655 }
656 #define testKeychainWrongFile256Tests (2 + openCustomKeychainTests \
657 + checkNTests + makeCustomDuplicateItemTests \
658 + 2 + 4 \
659 + checkNTests + makeCustomDuplicateItemTests \
660 + checkNTests + makeCustomDuplicateItemTests \
661 + checkNTests \
662 + checkNTests \
663 + checkNTests \
664 + 1)
665
666 // Test opening and upgrading a v512 keychain at a .keychain filename.
667 static void testKeychainWrongFile512() {
668 char name[100];
669 sprintf(name, "testKeychainWrongFile512");
670 secnotice("integrity", "************************************* %s", name);
671 UInt32 version;
672
673 writeFullV512Keychain(name, keychainFile);
674 unlink(keychainDbFile);
675
676 // Only keychain file should exist
677 struct stat filebuf;
678 isnt(stat(keychainDbFile, &filebuf), 0, "%s: %s exists and shouldn't", name, keychainFile);
679 is(stat(keychainFile, &filebuf), 0, "%s: %s does not exist", name, keychainDbFile);
680
681 SecKeychainRef kc = openCustomKeychain(name, keychainName, "password");
682
683 SecKeychainItemRef item;
684
685 // Iterate over the keychain to trigger upgrade
686 item = checkN(name, makeQueryItemDictionary(kc, kSecClassGenericPassword), 1);
687 makeCustomDuplicateItem(name, kc, kSecClassGenericPassword, CFSTR("test_generic"));
688
689 // We should have move the keychain to keychainDbFile, check for it
690 isnt(stat(keychainFile, &filebuf), 0, "%s: %s still exists", name, keychainFile);
691 is(stat(keychainDbFile, &filebuf), 0, "%s: %s does not exist", name, keychainDbFile);
692
693 // Check the keychain's version and path
694 char path[400];
695 UInt32 len = sizeof(path);
696
697 ok_status(SecKeychainGetKeychainVersion(kc, &version), "%s: SecKeychainGetKeychainVersion", name);
698 is(version, version_partition, "%s: version of moved keychain is incorrect", name);
699 ok_status(SecKeychainGetPath(kc, &len, path), "%s: SecKeychainGetPath", name);
700 eq_stringn(path, len, keychainDbFile, strlen(keychainDbFile), "%s: paths do not match", name);
701
702 item = checkN(name, makeQueryItemDictionary(kc, kSecClassGenericPassword), 1);
703 makeCustomDuplicateItem(name, kc, kSecClassGenericPassword, CFSTR("test_generic"));
704
705 item = checkN(name, makeQueryItemDictionary(kc, kSecClassInternetPassword), 1);
706 makeCustomDuplicateItem(name, kc, kSecClassInternetPassword, CFSTR("test_internet"));
707
708 item = checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassSymmetric), 1);
709 item = checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassPublic), 1);
710 item = checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassPrivate), 1);
711
712 ok_status(SecKeychainDelete(kc), "%s: SecKeychainDelete", name);
713 CFReleaseNull(kc);
714
715 // make sure we clean up any files left over
716 unlink(keychainDbFile);
717 unlink(keychainFile);
718 }
719 #define testKeychainWrongFile512Tests (2 + openCustomKeychainTests \
720 + checkNTests + makeCustomDuplicateItemTests \
721 + 2 + 4 \
722 + checkNTests + makeCustomDuplicateItemTests \
723 + checkNTests + makeCustomDuplicateItemTests \
724 + checkNTests \
725 + checkNTests \
726 + checkNTests \
727 + 1)
728
729
730 #undef version_partition
731 #undef version_MacOS_10_0
732
733 static SecAccessRef makeUidAccess(uid_t uid)
734 {
735 // make the "uid/gid" ACL subject
736 // this is a CSSM_LIST_ELEMENT chain
737 CSSM_ACL_PROCESS_SUBJECT_SELECTOR selector = {
738 CSSM_ACL_PROCESS_SELECTOR_CURRENT_VERSION, // selector version
739 CSSM_ACL_MATCH_UID, // set mask: match uids (only)
740 uid, // uid to match
741 0 // gid (not matched here)
742 };
743 CSSM_LIST_ELEMENT subject2 = { NULL, 0 };
744 subject2.Element.Word.Data = (UInt8 *)&selector;
745 subject2.Element.Word.Length = sizeof(selector);
746 CSSM_LIST_ELEMENT subject1 = {
747 &subject2, CSSM_ACL_SUBJECT_TYPE_PROCESS, CSSM_LIST_ELEMENT_WORDID
748 };
749
750 // rights granted (replace with individual list if desired)
751 CSSM_ACL_AUTHORIZATION_TAG rights[] = {
752 CSSM_ACL_AUTHORIZATION_ANY // everything
753 };
754 // owner component (right to change ACL)
755 CSSM_ACL_OWNER_PROTOTYPE owner = {
756 // TypedSubject
757 { CSSM_LIST_TYPE_UNKNOWN, &subject1, &subject2 },
758 // Delegate
759 false
760 };
761 // ACL entries (any number, just one here)
762 CSSM_ACL_ENTRY_INFO acls[] = {
763 {
764 // prototype
765 {
766 // TypedSubject
767 { CSSM_LIST_TYPE_UNKNOWN, &subject1, &subject2 },
768 false, // Delegate
769 // rights for this entry
770 { sizeof(rights) / sizeof(rights[0]), rights },
771 // rest is defaulted
772 }
773 }
774 };
775
776 SecAccessRef access;
777 SecAccessCreateFromOwnerAndACL(&owner, sizeof(acls) / sizeof(acls[0]), acls, &access);
778 return access;
779 }
780
781 static void checkAccessLength(const char * name, SecAccessRef access, int expected) {
782 CFArrayRef acllist = NULL;
783 ok_status(SecAccessCopyACLList(access, &acllist), "%s: SecAccessCopyACLList", name);
784
785 // Count the number of non-integrity ACLs in this access
786 int aclsFound = 0;
787 CFStringRef output = NULL;
788
789 if(acllist) {
790 for(int i = 0; i < CFArrayGetCount(acllist); i++) {
791 SecACLRef acl = (SecACLRef) CFArrayGetValueAtIndex(acllist, i);
792
793 CFArrayRef auths = SecACLCopyAuthorizations(acl);
794 CFRange searchrange = CFRangeMake(0, CFArrayGetCount(auths));
795 if(!CFArrayContainsValue(auths, searchrange, kSecACLAuthorizationIntegrity) &&
796 !CFArrayContainsValue(auths, searchrange, kSecACLAuthorizationPartitionID)) {
797
798 aclsFound += 1;
799 }
800
801 CFReleaseNull(auths);
802 }
803
804 CFReleaseNull(acllist);
805 }
806 is(aclsFound, expected, "%s: ACL has correct number of entries", name);
807 }
808 #define checkAccessLengthTests 2
809
810 static void testUidAccess() {
811 char name[100];
812 sprintf(name, "testUidAccess");
813 secnotice("integrity", "************************************* %s", name);
814
815 SecAccessRef access = makeUidAccess(getuid());
816
817 SecKeychainRef kc = newKeychain(name);
818 CFMutableDictionaryRef query = makeAddItemDictionary(kc, kSecClassGenericPassword, CFSTR("test label"));
819 CFDictionarySetValue(query, kSecAttrAccess, access);
820
821 CFTypeRef result = NULL;
822 ok_status(SecItemAdd(query, &result), "%s: SecItemAdd", name);
823 ok(result != NULL, "%s: SecItemAdd returned a result", name);
824
825 SecKeychainItemRef item = checkN(name, makeQueryItemDictionary(kc, kSecClassGenericPassword), 1);
826
827 ok_status(SecKeychainItemSetAccess(item, access), "%s: SecKeychainItemSetAccess", name);
828 checkN(name, makeQueryItemDictionary(kc, kSecClassGenericPassword), 1);
829
830 // Check to make sure the ACL stays
831 access = NULL;
832 ok_status(SecKeychainItemCopyAccess(item, &access), "%s: SecKeychainItemCopyAccess", name);
833 checkAccessLength(name, access, 2);
834
835 const char * newPassword = "newPassword";
836 ok_status(SecKeychainItemModifyContent(item, NULL, (UInt32) strlen(newPassword), newPassword), "%s: SecKeychainItemModifyContent", name);
837
838 access = NULL;
839 ok_status(SecKeychainItemCopyAccess(item, &access), "%s: SecKeychainItemCopyAccess", name);
840 checkAccessLength(name, access, 2);
841
842 ok_status(SecKeychainDelete(kc), "%s: SecKeychainDelete", name);
843 CFReleaseNull(kc);
844 }
845 #define testUidAccessTests (newKeychainTests + 2 + checkNTests + 1 + checkNTests + 1 + checkAccessLengthTests \
846 + 2 + checkAccessLengthTests + 1)
847
848
849 static SecAccessRef makeMultipleUidAccess(uid_t* uids, uint32 count)
850 {
851 // rights granted (replace with individual list if desired)
852 CSSM_ACL_AUTHORIZATION_TAG rights[] =
853 {
854 CSSM_ACL_AUTHORIZATION_ANY // everything
855 };
856 size_t numRights = sizeof(rights) / sizeof(rights[0]);
857
858 // allocate the arrays of objects used to define the ACL
859 CSSM_ACL_PROCESS_SUBJECT_SELECTOR selectors[count];
860 CSSM_LIST_ELEMENT heads[count], tails[count];
861 CSSM_ACL_ENTRY_INFO acls[count];
862 // clear all the ACL objects
863 memset(heads, 0, sizeof(heads));
864 memset(acls, 0, sizeof(acls));
865
866 uint32 i = count;
867 while (i--)
868 {
869 // make the "uid/gid" ACL subject
870 selectors[i].version = CSSM_ACL_PROCESS_SELECTOR_CURRENT_VERSION;
871 selectors[i].mask = CSSM_ACL_MATCH_UID; // set mask: match uids (only)
872 selectors[i].uid = uids[i]; // uid to match
873 selectors[i].gid = 0; // gid (not matched here)
874
875 // this is a CSSM_LIST_ELEMENT chain
876 heads[i].NextElement = &(tails[i]);
877 heads[i].WordID = CSSM_ACL_SUBJECT_TYPE_PROCESS;
878 heads[i].ElementType = CSSM_LIST_ELEMENT_WORDID;
879 // Element is unused
880
881 tails[i].NextElement = NULL;
882 tails[i].WordID = CSSM_WORDID__NLU_;
883 tails[i].ElementType = CSSM_LIST_ELEMENT_DATUM;
884 tails[i].Element.Word.Data = (UInt8 *)&selectors[i];
885 tails[i].Element.Word.Length = sizeof(selectors[i]);
886
887 // ACL entry
888 acls[i].EntryPublicInfo.TypedSubject.ListType = CSSM_LIST_TYPE_UNKNOWN;
889 acls[i].EntryPublicInfo.TypedSubject.Head = &heads[i];
890 acls[i].EntryPublicInfo.TypedSubject.Tail = &tails[i];
891 acls[i].EntryPublicInfo.Delegate = CSSM_FALSE;
892 acls[i].EntryPublicInfo.Authorization.NumberOfAuthTags = (uint32) numRights;
893
894 acls[i].EntryPublicInfo.Authorization.AuthTags = rights;
895 acls[i].EntryHandle = i;
896 }
897
898 // owner component (right to change ACL)
899 CSSM_ACL_OWNER_PROTOTYPE owner;
900 owner.TypedSubject = acls[0].EntryPublicInfo.TypedSubject;
901 owner.Delegate = acls[0].EntryPublicInfo.Delegate;
902
903 SecAccessRef access;
904 SecAccessCreateFromOwnerAndACL(&owner, count, acls, &access);
905 return access;
906 }
907 static void testMultipleUidAccess() {
908 char name[100];
909 sprintf(name, "testMultipleUidAccess");
910 secnotice("integrity", "************************************* %s", name);
911
912 uid_t uids[5];
913 uids[0] = getuid();
914 uids[1] = 0;
915 uids[2] = 500;
916 uids[3] = 501;
917 uids[4] = 502;
918
919 SecAccessRef access = makeMultipleUidAccess(uids, 5);
920
921 SecKeychainRef kc = newKeychain(name);
922 CFMutableDictionaryRef query = makeAddItemDictionary(kc, kSecClassGenericPassword, CFSTR("test label"));
923 CFDictionarySetValue(query, kSecAttrAccess, access);
924
925 CFTypeRef result = NULL;
926 ok_status(SecItemAdd(query, &result), "%s: SecItemAdd", name);
927 ok(result != NULL, "%s: SecItemAdd returned a result", name);
928
929 checkN(name, makeQueryItemDictionary(kc, kSecClassGenericPassword), 1);
930
931 ok_status(SecKeychainDelete(kc), "%s: SecKeychainDelete", name);
932 CFReleaseNull(kc);
933 }
934 #define testMultipleUidAccessTests (newKeychainTests + checkNTests + 3)
935
936 static void testRootUidAccess() {
937 char name[100];
938 sprintf(name, "testRootUidAccess");
939 secnotice("integrity", "************************************* %s", name);
940
941 SecAccessRef access = SecAccessCreateWithOwnerAndACL(getuid(), 0, (kSecUseOnlyUID | kSecHonorRoot), NULL, NULL);
942
943 SecKeychainRef kc = newKeychain(name);
944 CFMutableDictionaryRef query = makeAddItemDictionary(kc, kSecClassGenericPassword, CFSTR("test label"));
945 CFDictionarySetValue(query, kSecAttrAccess, access);
946
947 CFTypeRef result = NULL;
948 ok_status(SecItemAdd(query, &result), "%s: SecItemAdd", name);
949 ok(result != NULL, "%s: SecItemAdd returned a result", name);
950
951 query = makeQueryItemDictionary(kc, kSecClassGenericPassword);
952
953 SecKeychainItemRef item = checkN(name, query, 1);
954
955 ok_status(SecKeychainItemSetAccess(item, access), "%s: SecKeychainItemSetAccess", name);
956 checkN(name, makeQueryItemDictionary(kc, kSecClassGenericPassword), 1);
957
958 ok_status(SecKeychainDelete(kc), "%s: SecKeychainDelete", name);
959 CFReleaseNull(kc);
960 }
961 #define testRootUidAccessTests (newKeychainTests + checkNTests + 4 + checkNTests)
962
963 static void testBadACL() {
964 char name[100];
965 sprintf(name, "testBadACL");
966 secnotice("integrity", "************************************* %s", name);
967
968 SecKeychainItemRef item = NULL;
969
970 unlink(keychainFile);
971 writeFullV512Keychain(name, keychainDbFile);
972
973 SecKeychainRef kc = openCustomKeychain(name, keychainName, "password");
974
975 // Check that these exist in this keychain...
976 checkN(name, makeQueryItemDictionary(kc, kSecClassGenericPassword), 1);
977 checkN(name, makeQueryItemDictionary(kc, kSecClassInternetPassword), 1);
978
979 ok_status(SecKeychainDelete(kc), "%s: SecKeychainDelete", name);
980 CFRelease(kc);
981
982 // Corrupt all the ACLs, by changing the partition id plist entry
983 uint8_t * fileBuffer = (uint8_t*) malloc(FULL_V512_SIZE);
984 memcpy(fileBuffer, full_v512, FULL_V512_SIZE);
985
986 void* p;
987 char * str = "<key>Partitions</key>";
988 while( (p = memmem(fileBuffer, FULL_V512_SIZE, (void*) str, strlen(str))) ) {
989 *(uint8_t*) p = 0;
990 }
991 writeFile(keychainDbFile, fileBuffer, FULL_V512_SIZE);
992 free(fileBuffer);
993
994 kc = openCustomKeychain(name, keychainName, "password");
995
996 // These items exist in this keychain, but their ACL is corrupted. We should be able to find them, but not fetch data.
997 item = checkN(name, makeQueryItemDictionary(kc, kSecClassGenericPassword), 1);
998 readPasswordContentsWithResult(item, errSecInvalidItemRef, NULL); // we don't expect to be able to read this
999 deleteItem(item);
1000 checkN(name, makeQueryItemDictionary(kc, kSecClassGenericPassword), 0);
1001
1002 item = checkN(name, makeQueryItemDictionary(kc, kSecClassInternetPassword), 1);
1003 readPasswordContentsWithResult(item, errSecInvalidItemRef, NULL); // we don't expect to be able to read this
1004 deleteItem(item);
1005 checkN(name, makeQueryItemDictionary(kc, kSecClassInternetPassword), 0);
1006
1007 // These should work
1008 makeItem(name, kc, kSecClassGenericPassword, CFSTR("test_generic"));
1009 makeItem(name, kc, kSecClassInternetPassword, CFSTR("test_internet"));
1010
1011 // And now the items should exist
1012 item = checkN(name, makeQueryItemDictionary(kc, kSecClassGenericPassword), 1);
1013 readPasswordContents(item, CFSTR("data"));
1014 item = checkN(name, makeQueryItemDictionary(kc, kSecClassInternetPassword), 1);
1015 readPasswordContents(item, CFSTR("data"));
1016
1017 ok_status(SecKeychainDelete(kc), "%s: SecKeychainDelete", name);
1018 CFReleaseNull(kc);
1019 }
1020 #define testBadACLTests (openCustomKeychainTests + checkNTests * 2 + 1 + openCustomKeychainTests \
1021 + 2*(checkNTests + readPasswordContentsWithResultTests + deleteItemTests + checkNTests) \
1022 + makeItemTests*2 + checkNTests*2 + readPasswordContentsTests*2 + 1)
1023
1024 static void testIterateLockedKeychain() {
1025 char name[100];
1026 sprintf(name, "testIterateLockedKeychain");
1027 secnotice("integrity", "************************************* %s", name);
1028
1029 SecKeychainItemRef item = NULL;
1030
1031 unlink(keychainFile);
1032 writeFullV512Keychain(name, keychainDbFile);
1033
1034 SecKeychainRef kc = openCustomKeychain(name, keychainName, "password");
1035
1036 ok_status(SecKeychainLock(kc), "%s: SecKeychainLock", name);
1037
1038 item = checkN(name, makeQueryItemDictionary(kc, kSecClassGenericPassword), 1);
1039 item = checkN(name, makeQueryItemDictionary(kc, kSecClassInternetPassword), 1);
1040
1041 item = checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassSymmetric), 1);
1042 item = checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassPublic), 1);
1043 item = checkN(name, makeQueryKeyDictionary(kc, kSecAttrKeyClassPrivate), 1);
1044
1045 ok_status(SecKeychainDelete(kc), "%s: SecKeychainDelete", name);
1046 CFReleaseNull(kc);
1047 }
1048 #define testIterateLockedKeychainTests (openCustomKeychainTests + 1 + checkNTests*5 + 1)
1049
1050 #define kTestCount (0 \
1051 + testAddItemTests \
1052 + testAddItemTests \
1053 + testCopyMatchingItemTests \
1054 + testCopyMatchingItemTests \
1055 + testUpdateItemTests \
1056 + testUpdateItemTests \
1057 + testAddDuplicateItemTests \
1058 + testAddDuplicateItemTests \
1059 + testDeleteItemTests \
1060 + testDeleteItemTests \
1061 + testUpdateRetainedItemTests \
1062 + testUpdateRetainedItemTests \
1063 \
1064 + testAddKeyTests \
1065 + testAddFreeKeyTests \
1066 + testCopyMatchingKeyTests \
1067 + testUpdateKeyTests \
1068 + testAddDuplicateKeyTests \
1069 + testKeyPairTests \
1070 + testExportImportKeyPairTests \
1071 \
1072 + initializeDLTests \
1073 + testAttackItemTests \
1074 + testAttackKeyTests \
1075 + testAddAfterCorruptItemTests \
1076 + testAddAfterCorruptKeyTests \
1077 + unloadDLTests \
1078 \
1079 + testKeychainUpgradeTests \
1080 + testKeychainCreateOverTests \
1081 + testKeychainDowngradeTests \
1082 + testKeychainWrongFile256Tests \
1083 + testKeychainWrongFile512Tests \
1084 + testUidAccessTests \
1085 + testMultipleUidAccessTests \
1086 + testRootUidAccessTests \
1087 + testBadACLTests \
1088 + testIterateLockedKeychainTests \
1089 )
1090
1091 static void tests(void)
1092 {
1093 initializeKeychainTests("kc-30-xara");
1094
1095 testAddItem(kSecClassGenericPassword, CFSTR("265438ea6807b509c9c6962df3f5033fd1af118f76c5f550e3ed90cb0d3ffce4"));
1096 testAddItem(kSecClassInternetPassword, CFSTR("be34c4562153063ce9cdefc2c34451d5e6e98a447f293d68a67349c1b5d1164f"));
1097
1098 testCopyMatchingItem(kSecClassGenericPassword, CFSTR("265438ea6807b509c9c6962df3f5033fd1af118f76c5f550e3ed90cb0d3ffce4"));
1099 testCopyMatchingItem(kSecClassInternetPassword, CFSTR("be34c4562153063ce9cdefc2c34451d5e6e98a447f293d68a67349c1b5d1164f"));
1100
1101 testUpdateItem(kSecClassGenericPassword, CFSTR("265438ea6807b509c9c6962df3f5033fd1af118f76c5f550e3ed90cb0d3ffce4"),
1102 CFSTR("7b7be2fd6ee9f81ba4c5575ea451f2c21117fc0f241625a6cf90c65180b8c9f5"));
1103 testUpdateItem(kSecClassInternetPassword, CFSTR("be34c4562153063ce9cdefc2c34451d5e6e98a447f293d68a67349c1b5d1164f"),
1104 CFSTR("d71af9e4d54127a5dbc10c5ec097b828065cfbaf2b775caf1a3c4e3410f80851"));
1105
1106 testAddDuplicateItem(kSecClassGenericPassword, CFSTR("265438ea6807b509c9c6962df3f5033fd1af118f76c5f550e3ed90cb0d3ffce4"));
1107 testAddDuplicateItem(kSecClassInternetPassword, CFSTR("be34c4562153063ce9cdefc2c34451d5e6e98a447f293d68a67349c1b5d1164f"));
1108
1109 testDeleteItem(kSecClassGenericPassword, CFSTR("265438ea6807b509c9c6962df3f5033fd1af118f76c5f550e3ed90cb0d3ffce4"));
1110 testDeleteItem(kSecClassInternetPassword, CFSTR("be34c4562153063ce9cdefc2c34451d5e6e98a447f293d68a67349c1b5d1164f"));
1111
1112 testUpdateRetainedItem(kSecClassGenericPassword);
1113 testUpdateRetainedItem(kSecClassInternetPassword);
1114
1115 testAddKey( CFSTR("44f10f6bb508d47f8905859efc06eaee500304bc4da408b1f4d2a58c6502147b"));
1116 testAddFreeKey( CFSTR("44f10f6bb508d47f8905859efc06eaee500304bc4da408b1f4d2a58c6502147b"));
1117 testCopyMatchingKey(CFSTR("44f10f6bb508d47f8905859efc06eaee500304bc4da408b1f4d2a58c6502147b"));
1118 testUpdateKey( CFSTR("44f10f6bb508d47f8905859efc06eaee500304bc4da408b1f4d2a58c6502147b"),
1119 CFSTR("a744ce6db8359ad264ed5f4a35ecfcc8b6599b89319e7ea316035acd3fb02c22"));
1120 testAddDuplicateKey(CFSTR("44f10f6bb508d47f8905859efc06eaee500304bc4da408b1f4d2a58c6502147b"));
1121 testAddDuplicateFreeKey(CFSTR("44f10f6bb508d47f8905859efc06eaee500304bc4da408b1f4d2a58c6502147b"));
1122
1123 testKeyPair();
1124 testExportImportKeyPair();
1125
1126 CSSM_DL_DB_HANDLE dldbHandle = initializeDL();
1127 testAttackItem(dldbHandle);
1128 testAttackKey(dldbHandle);
1129
1130 testAddAfterCorruptItem(dldbHandle);
1131 testAddAfterCorruptKey(dldbHandle);
1132 unloadDL(&dldbHandle);
1133
1134 testKeychainUpgrade();
1135 testKeychainCreateOver();
1136 testKeychainDowngrade();
1137 testKeychainWrongFile256();
1138 testKeychainWrongFile512();
1139 testUidAccess();
1140 testMultipleUidAccess();
1141 testRootUidAccess();
1142 testBadACL();
1143 testIterateLockedKeychain();
1144
1145 //makeOldKeychainBlob();
1146 }
1147
1148 #pragma clang pop
1149 #else
1150
1151 #define kTestCount (0)
1152
1153
1154 static void tests(void)
1155 {
1156 }
1157
1158 #endif /* TARGET_OS_MAC */
1159
1160
1161 int kc_30_xara(int argc, char *const *argv)
1162 {
1163 plan_tests(kTestCount);
1164
1165 tests();
1166
1167 deleteTestFiles();
1168 return 0;
1169 }