]> git.saurik.com Git - apple/security.git/blob - OSX/sec/Security/Regressions/secitem/si-31-keychain-unreadable.c
Security-57336.10.29.tar.gz
[apple/security.git] / OSX / sec / Security / Regressions / secitem / si-31-keychain-unreadable.c
1 /*
2 * Copyright (c) 2008-2010,2012-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
25 #include <CoreFoundation/CoreFoundation.h>
26 #include <Security/SecBase.h>
27 #include <Security/SecItem.h>
28 #include <Security/SecInternal.h>
29
30 #include <stdlib.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <sys/stat.h>
34 #include <sqlite3.h>
35
36 #include "Security_regressions.h"
37
38 #ifdef NO_SERVER
39 static void ensureKeychainExists(void) {
40 CFDictionaryRef query = CFDictionaryCreate(0, (const void **)&kSecClass, (const void **)&kSecClassInternetPassword, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
41 CFTypeRef results = NULL;
42 is_status(SecItemCopyMatching(query, &results), errSecItemNotFound, "expected nothing got %@", results);
43 CFReleaseNull(query);
44 CFReleaseNull(results);
45 }
46 #endif
47
48 void kc_dbhandle_reset(void);
49
50 /* Create an empty keychain file that can't be read or written and make sure
51 securityd can deal with it. */
52 static void tests(void)
53 {
54 #ifndef NO_SERVER
55 plan_skip_all("No testing against server.");
56 #else
57 const char *home_dir = getenv("HOME");
58 char keychain_dir[1000];
59 char keychain_name[1000];
60 sprintf(keychain_dir, "%s/Library/Keychains", home_dir);
61 sprintf(keychain_name, "%s/keychain-2-debug.db", keychain_dir);
62
63 ensureKeychainExists();
64 int fd;
65 ok_unix(fd = open(keychain_name, O_RDWR | O_CREAT | O_TRUNC, 0644),
66 "create keychain file '%s'", keychain_name);
67 ok_unix(fchmod(fd, 0), " keychain file '%s'", keychain_name);
68 ok_unix(close(fd), "close keychain file '%s'", keychain_name);
69
70 kc_dbhandle_reset();
71
72 int v_eighty = 80;
73 CFNumberRef eighty = CFNumberCreate(NULL, kCFNumberSInt32Type, &v_eighty);
74 const char *v_data = "test";
75 CFDataRef pwdata = CFDataCreate(NULL, (UInt8 *)v_data, strlen(v_data));
76 CFMutableDictionaryRef query = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
77 CFDictionaryAddValue(query, kSecClass, kSecClassInternetPassword);
78 CFDictionaryAddValue(query, kSecAttrServer, CFSTR("members.spamcop.net"));
79 CFDictionaryAddValue(query, kSecAttrAccount, CFSTR("smith"));
80 CFDictionaryAddValue(query, kSecAttrPort, eighty);
81 CFDictionaryAddValue(query, kSecAttrProtocol, kSecAttrProtocolHTTP);
82 CFDictionaryAddValue(query, kSecAttrAuthenticationType, kSecAttrAuthenticationTypeDefault);
83 CFDictionaryAddValue(query, kSecValueData, pwdata);
84 ok_status(SecItemAdd(query, NULL), "add internet password");
85 is_status(SecItemAdd(query, NULL), errSecDuplicateItem,
86 "add internet password again");
87
88 ok_status(SecItemCopyMatching(query, NULL), "Found the item we added");
89
90 ok_status(SecItemDelete(query),"Deleted the item we added");
91
92 CFReleaseSafe(eighty);
93 CFReleaseSafe(pwdata);
94 CFReleaseSafe(query);
95 #endif
96 }
97
98 int si_31_keychain_unreadable(int argc, char *const *argv)
99 {
100 plan_tests(8);
101 tests();
102
103 return 0;
104 }