]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-31-keychain-bad.c
Security-57336.1.9.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-31-keychain-bad.c
1 /*
2 * Copyright (c) 2008,2010,2013-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 <utilities/SecFileLocations.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 "secd_regressions.h"
37
38 const uint8_t keychain_data[] = {
39 0x62, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x30, 0x30, 0xd2, 0x01, 0x02, 0x03,
40 0x04, 0x5f, 0x10, 0x1b, 0x4e, 0x53, 0x57, 0x69, 0x6e, 0x64, 0x6f, 0x77,
41 0x20, 0x46, 0x72, 0x61, 0x6d, 0x65, 0x20, 0x50, 0x72, 0x6f, 0x63, 0x65,
42 0x73, 0x73, 0x50, 0x61, 0x6e, 0x65, 0x6c, 0x5f, 0x10, 0x1d, 0x4e, 0x53,
43 0x57, 0x69, 0x6e, 0x64, 0x6f, 0x77, 0x20, 0x46, 0x72, 0x61, 0x6d, 0x65,
44 0x20, 0x41, 0x62, 0x6f, 0x75, 0x74, 0x20, 0x54, 0x68, 0x69, 0x73, 0x20,
45 0x4d, 0x61, 0x63, 0x5f, 0x10, 0x1c, 0x32, 0x38, 0x20, 0x33, 0x37, 0x33,
46 0x20, 0x33, 0x34, 0x36, 0x20, 0x32, 0x39, 0x30, 0x20, 0x30, 0x20, 0x30,
47 0x20, 0x31, 0x34, 0x34, 0x30, 0x20, 0x38, 0x37, 0x38, 0x20, 0x5f, 0x10,
48 0x1d, 0x35, 0x36, 0x38, 0x20, 0x33, 0x39, 0x35, 0x20, 0x33, 0x30, 0x37,
49 0x20, 0x33, 0x37, 0x39, 0x20, 0x30, 0x20, 0x30, 0x20, 0x31, 0x34, 0x34,
50 0x30, 0x20, 0x38, 0x37, 0x38, 0x20, 0x08, 0x0d, 0x2b, 0x4b, 0x6a, 0x00,
51 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
52 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8a
54 };
55
56 #include "SecdTestKeychainUtilities.h"
57
58 #include <securityd/SecItemServer.h>
59 #include <utilities/SecCFWrappers.h>
60
61 /* Test basic add delete update copy matching stuff. */
62 static void tests(void)
63 {
64 /* custom keychain dir */
65 secd_test_setup_temp_keychain("secd_31_keychain_bad", ^{
66 CFStringRef keychain_path_cf = __SecKeychainCopyPath();
67
68 CFStringPerformWithCString(keychain_path_cf, ^(const char *keychain_path) {
69 int fd;
70 ok_unix(fd = open(keychain_path, O_RDWR | O_CREAT | O_TRUNC, 0644),
71 "create keychain file");
72 is(write(fd, keychain_data, sizeof(keychain_data)),
73 (ssize_t)sizeof(keychain_data), "write garbage to keychain file");
74 ok_unix(close(fd), "close keychain file");
75
76 });
77
78 CFReleaseSafe(keychain_path_cf);
79 });
80 __security_simulatecrash_enable(false);
81
82 int v_eighty = 80;
83 CFNumberRef eighty = CFNumberCreate(NULL, kCFNumberSInt32Type, &v_eighty);
84 const char *v_data = "test";
85 CFDataRef pwdata = CFDataCreate(NULL, (UInt8 *)v_data, strlen(v_data));
86 CFMutableDictionaryRef query = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
87 CFDictionaryAddValue(query, kSecClass, kSecClassInternetPassword);
88 CFDictionaryAddValue(query, kSecAttrServer, CFSTR("members.spamcop.net"));
89 CFDictionaryAddValue(query, kSecAttrAccount, CFSTR("smith"));
90 CFDictionaryAddValue(query, kSecAttrPort, eighty);
91 CFDictionaryAddValue(query, kSecAttrProtocol, kSecAttrProtocolHTTP);
92 CFDictionaryAddValue(query, kSecAttrAuthenticationType, kSecAttrAuthenticationTypeDefault);
93 CFDictionaryAddValue(query, kSecValueData, pwdata);
94 ok_status(SecItemAdd(query, NULL), "add internet password");
95 is_status(SecItemAdd(query, NULL), errSecDuplicateItem,
96 "add internet password again");
97
98 ok_status(SecItemCopyMatching(query, NULL), "Found the item we added");
99
100 ok_status(SecItemDelete(query),"Deleted the item we added");
101
102 is(__security_simulatecrash_enable(true), 3, "expecting 3 simcrashes from opening DB connection");
103
104 CFRelease(query);
105 CFRelease(eighty);
106 CFRelease(pwdata);
107 }
108
109 int secd_31_keychain_bad(int argc, char *const *argv)
110 {
111 plan_tests(8 + kSecdTestSetupTestCount);
112
113 tests();
114
115 return 0;
116 }