1/* 2 * secd-31-keychain-unreadable.c 3 * Security 4 * 5 * Created by Michael Brouwer on 5/23/08. 6 * Copyright (c) 2008-2010,2013 Apple Inc. All Rights Reserved. 7 * 8 */ 9 10#include <CoreFoundation/CoreFoundation.h> 11#include <Security/SecBase.h> 12#include <Security/SecItem.h> 13#include <Security/SecInternal.h> 14#include <utilities/SecFileLocations.h> 15#include <utilities/SecCFWrappers.h> 16 17#include <stdlib.h> 18#include <fcntl.h> 19#include <unistd.h> 20#include <sys/stat.h> 21#include <sqlite3.h> 22 23#include "secd_regressions.h" 24 25#include <securityd/SecItemServer.h> 26 27#include "SecdTestKeychainUtilities.h" 28 29 30/* Create an empty keychain file that can't be read or written and make sure 31 securityd can deal with it. */ 32static void tests(void) 33{ 34 /* custom keychain dir */ 35 secd_test_setup_temp_keychain("secd_31_keychain_unreadable", ^{ 36 CFStringRef keychain_path_cf = __SecKeychainCopyPath(); 37 38 CFStringPerformWithCString(keychain_path_cf, ^(const char *keychain_path) { 39 int fd; 40 ok_unix(fd = open(keychain_path, O_RDWR | O_CREAT | O_TRUNC, 0644), 41 "create keychain file '%s'", keychain_path); 42 ok_unix(fchmod(fd, 0), " keychain file '%s'", keychain_path); 43 ok_unix(close(fd), "close keychain file '%s'", keychain_path); 44 45 }); 46 47 CFReleaseSafe(keychain_path_cf); 48 }); 49 50 int v_eighty = 80; 51 CFNumberRef eighty = CFNumberCreate(NULL, kCFNumberSInt32Type, &v_eighty); 52 const char *v_data = "test"; 53 CFDataRef pwdata = CFDataCreate(NULL, (UInt8 *)v_data, strlen(v_data)); 54 CFMutableDictionaryRef query = CFDictionaryCreateMutable(NULL, 0, NULL, NULL); 55 CFDictionaryAddValue(query, kSecClass, kSecClassInternetPassword); 56 CFDictionaryAddValue(query, kSecAttrServer, CFSTR("members.spamcop.net")); 57 CFDictionaryAddValue(query, kSecAttrAccount, CFSTR("smith")); 58 CFDictionaryAddValue(query, kSecAttrPort, eighty); 59 CFDictionaryAddValue(query, kSecAttrProtocol, kSecAttrProtocolHTTP); 60 CFDictionaryAddValue(query, kSecAttrAuthenticationType, kSecAttrAuthenticationTypeDefault); 61 CFDictionaryAddValue(query, kSecValueData, pwdata); 62 ok_status(SecItemAdd(query, NULL), "add internet password"); 63 is_status(SecItemAdd(query, NULL), errSecDuplicateItem, 64 "add internet password again"); 65 66 ok_status(SecItemCopyMatching(query, NULL), "Found the item we added"); 67 68 ok_status(SecItemDelete(query),"Deleted the item we added"); 69 70 CFReleaseSafe(eighty); 71 CFReleaseSafe(pwdata); 72 CFReleaseSafe(query); 73} 74 75int secd_31_keychain_unreadable(int argc, char *const *argv) 76{ 77 plan_tests(7 + kSecdTestSetupTestCount); 78 tests(); 79 80 return 0; 81} 82