-
Notifications
You must be signed in to change notification settings - Fork 40
/
logKextKeyGen.c
135 lines (122 loc) · 3.53 KB
/
logKextKeyGen.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <CoreFoundation/CoreFoundation.h>
#include <Security/Security.h>
#include <openssl/rand.h>
#include "logKextKeyGen.h"
void removeKey()
{
SecKeychainRef sysChain;
OSStatus secRes = SecKeychainOpen("/Library/Keychains/System.keychain", &sysChain);
if (secRes)
{
printf("Couldn't get system keychain: %d\n",secRes);
return;
}
SecKeychainItemRef itemRef=NULL;
secRes = SecKeychainFindGenericPassword(sysChain, strlen("logKextPassKey"), "logKextPassKey", 0, NULL, NULL, NULL, &itemRef);
if (secRes && secRes != errSecItemNotFound) {
printf("Error: couldn't find item: %d\n",secRes);
return;
}
else if (secRes == errSecItemNotFound) {
printf("Error: couldn't find passkey\n");
return;
}
else
printf("Successfully found item\n");
secRes = SecKeychainItemDelete(itemRef);
if (secRes)
printf("Couldn't delete item: %d\n",secRes);
else
printf("Deleted item\n");
}
SecAccessRef getAccessRef()
{
SecTrustedApplicationRef appRefs[3];
// corresponds to self
OSStatus secRes = SecTrustedApplicationCreateFromPath(NULL, &appRefs[0]);
if (secRes)
{
printf("Error: couldn't create trusted app from keygen path: %d\n",secRes);
return NULL;
}
secRes = SecTrustedApplicationCreateFromPath(DAEMON_PATH, &appRefs[1]);
if (secRes)
{
printf("Error: couldn't create trusted app from daemon path: %d\n",secRes);
return NULL;
}
secRes = SecTrustedApplicationCreateFromPath(CLIENT_PATH, &appRefs[2]);
if (secRes)
{
printf("Error: couldn't create trusted app from client path: %d\n",secRes);
return NULL;
}
CFArrayRef trustedList = CFArrayCreate(NULL, (void*)appRefs, sizeof(appRefs)/sizeof(*appRefs), NULL);
SecAccessRef accessRef;
secRes = SecAccessCreate(CFSTR("logKextPassKey"), trustedList, &accessRef);
if (secRes)
{
printf("Error: couldn't create secAccess %d\n",secRes);
return NULL;
}
return accessRef;
}
void generateKey()
{
SecKeychainRef sysChain;
OSStatus secRes = SecKeychainOpen("/Library/Keychains/System.keychain", &sysChain);
if (secRes)
{
printf("Couldn't get system keychain: %d\n",secRes);
return;
}
SecAccessRef accessRef = getAccessRef();
if (accessRef==NULL)
return;
SecKeychainItemRef itemRef=NULL;
char *passData;
UInt32 passLen=0;
secRes = SecKeychainFindGenericPassword(sysChain, strlen("logKextPassKey"), "logKextPassKey", 0, NULL, NULL, NULL, &itemRef);
if (secRes != errSecItemNotFound)
{
printf("Warning: item already exists\n");
secRes = SecKeychainItemCopyContent(itemRef, NULL, NULL, &passLen, (void**)&passData);
if (secRes)
{
printf("Error: Unable to copy keychain data: %d\n",secRes);
return;
}
secRes = SecKeychainItemDelete(itemRef);
if (secRes)
{
printf("Error: Unable to delete original keychain item: %d\n",secRes);
return;
}
}
else if (secRes == errSecItemNotFound)
{
passLen=16;
passData = malloc(passLen);
RAND_bytes((unsigned char*)passData, passLen);
}
else
{
printf("Error accessing keychain: %d\n",secRes);
return;
}
SecKeychainAttribute attrs[] = {
{ kSecLabelItemAttr, strlen("logKextPassKey"), "logKextPassKey" },
{ kSecServiceItemAttr, strlen("logKextPassKey"), "logKextPassKey" }
};
SecKeychainAttributeList attributes = { sizeof(attrs) / sizeof(attrs[0]), attrs };
secRes = SecKeychainItemCreateFromContent(kSecGenericPasswordItemClass, &attributes, passLen, passData, sysChain, accessRef, &itemRef);
if (secRes)
printf("Error creating keychain item: %d\n",secRes);
}
int main (int argc, const char * argv[]) {
if (argc>1 && !strcmp(argv[1], "remove"))
removeKey();
else
generateKey();
return 0;
}