1/*
2 *  si-31-keychain-bad.c
3 *  Security
4 *
5 *  Created by Michael Brouwer on 5/23/08.
6 *  Copyright (c) 2008,2010 Apple Inc.. All Rights Reserved.
7 *
8 */
9
10#include <CoreFoundation/CoreFoundation.h>
11#include <Security/SecBase.h>
12#include <Security/SecItem.h>
13
14#include <stdlib.h>
15#include <fcntl.h>
16#include <unistd.h>
17#include <sys/stat.h>
18#include <sqlite3.h>
19
20#include "Security_regressions.h"
21
22const uint8_t keychain_data[] = {
23    0x62, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x30, 0x30, 0xd2, 0x01, 0x02, 0x03,
24    0x04, 0x5f, 0x10, 0x1b, 0x4e, 0x53, 0x57, 0x69, 0x6e, 0x64, 0x6f, 0x77,
25    0x20, 0x46, 0x72, 0x61, 0x6d, 0x65, 0x20, 0x50, 0x72, 0x6f, 0x63, 0x65,
26    0x73, 0x73, 0x50, 0x61, 0x6e, 0x65, 0x6c, 0x5f, 0x10, 0x1d, 0x4e, 0x53,
27    0x57, 0x69, 0x6e, 0x64, 0x6f, 0x77, 0x20, 0x46, 0x72, 0x61, 0x6d, 0x65,
28    0x20, 0x41, 0x62, 0x6f, 0x75, 0x74, 0x20, 0x54, 0x68, 0x69, 0x73, 0x20,
29    0x4d, 0x61, 0x63, 0x5f, 0x10, 0x1c, 0x32, 0x38, 0x20, 0x33, 0x37, 0x33,
30    0x20, 0x33, 0x34, 0x36, 0x20, 0x32, 0x39, 0x30, 0x20, 0x30, 0x20, 0x30,
31    0x20, 0x31, 0x34, 0x34, 0x30, 0x20, 0x38, 0x37, 0x38, 0x20, 0x5f, 0x10,
32    0x1d, 0x35, 0x36, 0x38, 0x20, 0x33, 0x39, 0x35, 0x20, 0x33, 0x30, 0x37,
33    0x20, 0x33, 0x37, 0x39, 0x20, 0x30, 0x20, 0x30, 0x20, 0x31, 0x34, 0x34,
34    0x30, 0x20, 0x38, 0x37, 0x38, 0x20, 0x08, 0x0d, 0x2b, 0x4b, 0x6a, 0x00,
35    0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
36    0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
37    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8a
38};
39
40void kc_dbhandle_reset(void);
41
42/* Test basic add delete update copy matching stuff. */
43static void tests(void)
44{
45#ifndef NO_SERVER
46    plan_skip_all("No testing against server.");
47#else
48    const char *home_dir = getenv("HOME");
49    char keychain_dir[1000];
50    char keychain_name[1000];
51    sprintf(keychain_dir, "%s/Library/Keychains", home_dir);
52    sprintf(keychain_name, "%s/keychain-2-debug.db", keychain_dir);
53    int fd;
54    ok_unix(fd = open(keychain_name, O_RDWR | O_CREAT | O_TRUNC, 0644),
55        "create keychain file");
56    is(write(fd, keychain_data, sizeof(keychain_data)),
57        (ssize_t)sizeof(keychain_data), "write garbage to keychain file");
58    ok_unix(close(fd), "close keychain file");
59
60    kc_dbhandle_reset();
61
62    int v_eighty = 80;
63    CFNumberRef eighty = CFNumberCreate(NULL, kCFNumberSInt32Type, &v_eighty);
64    const char *v_data = "test";
65    CFDataRef pwdata = CFDataCreate(NULL, (UInt8 *)v_data, strlen(v_data));
66    CFMutableDictionaryRef query = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
67    CFDictionaryAddValue(query, kSecClass, kSecClassInternetPassword);
68    CFDictionaryAddValue(query, kSecAttrServer, CFSTR("members.spamcop.net"));
69    CFDictionaryAddValue(query, kSecAttrAccount, CFSTR("smith"));
70    CFDictionaryAddValue(query, kSecAttrPort, eighty);
71    CFDictionaryAddValue(query, kSecAttrProtocol, kSecAttrProtocolHTTP);
72    CFDictionaryAddValue(query, kSecAttrAuthenticationType, kSecAttrAuthenticationTypeDefault);
73    CFDictionaryAddValue(query, kSecValueData, pwdata);
74    ok_status(SecItemAdd(query, NULL), "add internet password");
75    is_status(SecItemAdd(query, NULL), errSecDuplicateItem,
76	"add internet password again");
77
78    ok_status(SecItemCopyMatching(query, NULL), "Found the item we added");
79
80    ok_status(SecItemDelete(query),"Deleted the item we added");
81
82    CFRelease(query);
83    CFRelease(eighty);
84    CFRelease(pwdata);
85#endif
86}
87
88int si_31_keychain_bad(int argc, char *const *argv)
89{
90	plan_tests(7);
91
92	tests();
93
94	return 0;
95}
96