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