-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeychain-locked-process.cc
280 lines (215 loc) · 7.92 KB
/
keychain-locked-process.cc
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2013, Yingdi Yu
* Author: Yingdi Yu <[email protected]>
*/
#include <CoreFoundation/CoreFoundation.h>
#include <Security/Security.h>
#include <CoreServices/CoreServices.h>
#include <iostream>
using namespace std;
void
checkUserInteraction()
{
OSStatus res;
Boolean allowInteraction;
res = SecKeychainGetUserInteractionAllowed(&allowInteraction);
cerr << "Allow Interaction: " << boolalpha << (allowInteraction == true) << endl;
}
void
checkKeychainStatus(SecKeychainRef keychain)
{
OSStatus res;
SecKeychainStatus keychainStatus;
res = SecKeychainGetStatus (keychain, &keychainStatus);
cerr << "Keychain Unlocked: " << boolalpha << ((kSecUnlockStateStatus & keychainStatus) != 0) << endl;
cerr << "Keychain Readable: " << boolalpha << ((kSecReadPermStatus & keychainStatus) != 0) << endl;
cerr << "Keychain Writable: " << boolalpha << ((kSecWritePermStatus & keychainStatus) != 0) << endl;
}
void
createKeyPair()
{
OSStatus res;
SecKeyRef publicKey, privateKey;
int keySize = 2048;
CFStringRef keyLabel = CFStringCreateWithCString (NULL, "keychain-locked-process-key", kCFStringEncodingUTF8);
CFMutableDictionaryRef attrDict = CFDictionaryCreateMutable(NULL, 3, &kCFTypeDictionaryKeyCallBacks, NULL);
CFDictionaryAddValue(attrDict, kSecAttrKeyType, kSecAttrKeyTypeRSA);
CFDictionaryAddValue(attrDict, kSecAttrKeySizeInBits, CFNumberCreate(NULL, kCFNumberIntType, &keySize));
CFDictionaryAddValue(attrDict, kSecAttrLabel, keyLabel);
res = SecKeyGeneratePair(attrDict, &publicKey, &privateKey);
cerr << "SecKeyGeneratePair res: " << res << endl;
}
void
deleteKeyPair()
{
OSStatus res;
CFStringRef keyLabel = CFStringCreateWithCString (NULL, "keychain-locked-process-key", kCFStringEncodingUTF8);
CFMutableDictionaryRef attrDict = CFDictionaryCreateMutable(NULL, 4, &kCFTypeDictionaryKeyCallBacks, NULL);
CFDictionaryAddValue(attrDict, kSecClass, kSecClassKey);
CFDictionaryAddValue(attrDict, kSecAttrKeyType, kSecAttrKeyTypeRSA);
CFDictionaryAddValue(attrDict, kSecAttrLabel, keyLabel);
CFDictionaryAddValue(attrDict, kSecMatchLimit, kSecMatchLimitAll);
res = SecItemDelete(attrDict);
cerr << "SecItemDelete res: " << res << endl;
}
SecKeyRef
getPrivateKey()
{
OSStatus res;
SecKeyRef privateKey;
CFStringRef keyLabel = CFStringCreateWithCString (NULL, "keychain-locked-process-key", kCFStringEncodingUTF8);
CFMutableDictionaryRef attrDict = CFDictionaryCreateMutable(NULL, 5, &kCFTypeDictionaryKeyCallBacks, NULL);
CFDictionaryAddValue(attrDict, kSecClass, kSecClassKey);
CFDictionaryAddValue(attrDict, kSecAttrLabel, keyLabel);
CFDictionaryAddValue(attrDict, kSecAttrKeyClass, kSecAttrKeyClassPrivate);
CFDictionaryAddValue(attrDict, kSecReturnRef, kCFBooleanTrue);
res = SecItemCopyMatching(attrDict, (CFTypeRef*)&privateKey);
cerr << "SecItemCopyMatching res: " << res << endl;
return privateKey;
}
void
sign(SecKeyRef privateKey)
{
CFErrorRef error;
char data[10] = "encrypted";
CFDataRef dataRef = CFDataCreateWithBytesNoCopy(NULL, reinterpret_cast<uint8_t*>(data), strlen(data), kCFAllocatorNull);
SecTransformRef signer = SecSignTransformCreate(privateKey, &error);
if (error)
{
CFShow (error);
return;
}
SecTransformSetAttribute(signer, kSecTransformInputAttributeName, dataRef, &error);
if (error)
{
CFShow (error);
return;
}
SecTransformSetAttribute(signer, kSecPaddingKey, kSecPaddingPKCS1Key, &error);
if (error)
{
CFShow (error);
return;
}
SecTransformSetAttribute(signer, kSecDigestTypeAttribute, kSecDigestSHA2, &error);
if (error)
{
CFShow (error);
return;
}
long digestSize = 256;
SecTransformSetAttribute(signer, kSecDigestLengthAttribute, CFNumberCreate(NULL, kCFNumberLongType, &digestSize), &error);
if (error)
{
CFShow (error);
return;
}
CFDataRef signature = (CFDataRef) SecTransformExecute(signer, &error);
if (error) {
CFShow(error);
return;
}
}
CFDataRef
exportKey(SecKeyRef privateKey)
{
CFDataRef exportedKey;
OSStatus res;
res = SecItemExport((SecKeychainItemRef)privateKey,
kSecFormatOpenSSL,
0,
NULL,
&exportedKey);
cerr << "SecItemExport res: " << res << endl;
return exportedKey;
}
void
importKey(CFDataRef importedKey)
{
OSStatus res;
SecExternalFormat externalFormat = kSecFormatOpenSSL;
SecExternalItemType externalType = kSecItemTypePrivateKey;
SecKeyImportExportParameters keyParams;
memset(&keyParams, 0, sizeof(keyParams));
keyParams.version = SEC_KEY_IMPORT_EXPORT_PARAMS_VERSION;
keyParams.keyAttributes = CSSM_KEYATTR_EXTRACTABLE | CSSM_KEYATTR_PERMANENT;
SecAccessRef access;
CFStringRef keyLabel = CFStringCreateWithCString (NULL, "keychain-locked-process-key", kCFStringEncodingUTF8);
SecAccessCreate(keyLabel, NULL, &access);
keyParams.accessRef = access;
CFArrayRef outItems;
SecKeychainRef keychainRef;
SecKeychainCopyDefault(&keychainRef);
res = SecKeychainItemImport (importedKey,
NULL,
&externalFormat,
&externalType,
0,
&keyParams,
keychainRef,
&outItems);
cerr << "SecKeychainItemImport res: " << res << endl;
if(res != errSecSuccess)
return;
SecKeychainItemRef privateKey = (SecKeychainItemRef)CFArrayGetValueAtIndex(outItems, 0);
SecKeychainAttribute attrs[2]; // maximum number of attributes
SecKeychainAttributeList attrList = { 0, attrs };
string keyUri("keychain-locked-process-key");
{
attrs[attrList.count].tag = kSecKeyPrintName;
attrs[attrList.count].length = keyUri.size();
attrs[attrList.count].data = (void *)keyUri.c_str();
attrList.count++;
}
res = SecKeychainItemModifyAttributesAndData(privateKey,
&attrList,
0,
NULL);
}
int main (int argc, char** argv)
{
OSStatus res;
SecKeychainRef targetKeychain;
SecKeychainRef originKeychain;
checkUserInteraction ();
res = SecKeychainSetUserInteractionAllowed (false);
checkUserInteraction ();
res = SecKeychainCreate ("example.keychain", 4, "1234", false, NULL, &targetKeychain);
if(errSecDuplicateKeychain == res)
res = SecKeychainOpen ("example.keychain", &targetKeychain);
cerr << "Create/Open keychain res: " << res << endl;
res = SecKeychainCopyDefault (&originKeychain);
cerr << "Get default keychain res: " << res << endl;
res = SecKeychainSetDefault (targetKeychain);
cerr << "Set default keychain res: " << res << endl;
checkKeychainStatus (targetKeychain);
res = SecKeychainUnlock (targetKeychain, 4, "1234", true);
checkKeychainStatus (targetKeychain);
res = SecKeychainLock (targetKeychain);
checkKeychainStatus (targetKeychain);
createKeyPair(); // Locked keychain, should fail
res = SecKeychainUnlock (targetKeychain, 4, "1234", true);
checkKeychainStatus (targetKeychain);
createKeyPair();
res = SecKeychainLock (targetKeychain);
checkKeychainStatus (targetKeychain);
SecKeyRef privateKey = getPrivateKey();
sign(privateKey); // Locked keychain, should fail
exportKey(privateKey); // Locked keychain, should fail
res = SecKeychainUnlock (targetKeychain, 4, "1234", true);
checkKeychainStatus (targetKeychain);
sign(privateKey);
CFDataRef exportedKey = exportKey(privateKey);
deleteKeyPair();
res = SecKeychainLock (targetKeychain);
checkKeychainStatus (targetKeychain);
importKey(exportedKey);
res = SecKeychainUnlock (targetKeychain, 4, "1234", true);
checkKeychainStatus (targetKeychain);
importKey(exportedKey);
deleteKeyPair(); // Some bug here, we cannot delete the imported key, I guess it is related to the access control, let's figure it out later
res = SecKeychainSetDefault (originKeychain);
cerr << "Set default keychain back res: " << res << endl;
return 0;
}