2 * Copyright (c) 2008-2010,2012-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@
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>
37 #include "Security_regressions.h"
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
);
45 CFReleaseNull(results
);
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)
54 plan_skip_all("No testing against server.");
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
);
62 ensureKeychainExists();
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
);
69 SecKeychainDbReset(NULL
);
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");
87 ok_status(SecItemCopyMatching(query
, NULL
), "Found the item we added");
89 ok_status(SecItemDelete(query
),"Deleted the item we added");
91 CFReleaseSafe(eighty
);
92 CFReleaseSafe(pwdata
);
97 int si_31_keychain_unreadable(int argc
, char *const *argv
)