-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbinding_darwin.c
More file actions
348 lines (291 loc) · 10.7 KB
/
Copy pathbinding_darwin.c
File metadata and controls
348 lines (291 loc) · 10.7 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#if defined(__APPLE__)
#include <CoreFoundation/CoreFoundation.h>
#include <Security/Security.h>
#include "binding.h"
const char *SIGNER_DEFAULT_SERVICE = "bare-signer-service";
const char *SIGNER_DEFAULT_ACCOUNT = "mnemonic";
static int
copy_osstatus_message(OSStatus status, char *dest, size_t *dest_size) {
if (dest == NULL || dest_size == NULL || *dest_size == 0) {
return -1;
}
CFStringRef message = SecCopyErrorMessageString(status, NULL);
if (message != NULL) {
if (CFStringGetCString(message, dest, *dest_size, kCFStringEncodingUTF8)) {
*dest_size = strlen(dest) + 1;
CFRelease(message);
return 0;
}
CFRelease(message);
}
char fallback[64];
snprintf(fallback, sizeof(fallback), "keychain error (%d)", (int) status);
return safe_copy_string(fallback, dest, dest_size);
}
// Map access control string to iOS SecAccessControlCreateFlags
static SecAccessControlCreateFlags
map_access_control_string(const char *access_control_str) {
if (access_control_str == NULL) {
// Default: UserPresence
return kSecAccessControlUserPresence;
}
if (strcmp(access_control_str, "UserPresence") == 0) {
return kSecAccessControlUserPresence;
} else if (strcmp(access_control_str, "BiometryAny") == 0) {
return kSecAccessControlBiometryAny;
} else if (strcmp(access_control_str, "BiometryCurrentSet") == 0) {
return kSecAccessControlBiometryCurrentSet;
}
// Default if unknown
return kSecAccessControlUserPresence;
}
int
store_data_in_apple_keychain(
const char *data,
size_t data_len,
const char *name,
const char *service_param,
const char *access_control_str,
char *error_msg,
size_t *error_size
) {
int rc = -1;
if (data == NULL || data_len == 0) {
safe_copy_string("data is NULL or empty", error_msg, error_size);
return -1;
}
// Use defaults if parameters are NULL
const char *service_name = (service_param != NULL && strlen(service_param) > 0)
? service_param
: SIGNER_DEFAULT_SERVICE;
const char *account_name = (name != NULL && strlen(name) > 0)
? name
: SIGNER_DEFAULT_ACCOUNT;
SecAccessControlCreateFlags access_flags = map_access_control_string(access_control_str);
CFStringRef service = NULL;
CFStringRef account = NULL;
CFDataRef value = NULL;
CFErrorRef access_error = NULL;
SecAccessControlRef access_control = NULL;
CFMutableDictionaryRef add_query = NULL;
OSStatus status;
service = CFStringCreateWithCString(kCFAllocatorDefault, service_name, kCFStringEncodingUTF8);
if (service == NULL) {
safe_copy_string("CFStringCreateWithCString failed for service", error_msg, error_size);
goto cleanup;
}
account = CFStringCreateWithCString(kCFAllocatorDefault, account_name, kCFStringEncodingUTF8);
if (account == NULL) {
safe_copy_string("CFStringCreateWithCString failed for account", error_msg, error_size);
goto cleanup;
}
value = CFDataCreate(kCFAllocatorDefault, (const UInt8 *)data, data_len);
if (value == NULL) {
safe_copy_string("CFDataCreate failed", error_msg, error_size);
goto cleanup;
}
access_control = SecAccessControlCreateWithFlags(
kCFAllocatorDefault,
kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly,
access_flags,
&access_error
);
if (access_control == NULL) {
if (access_error == NULL) {
safe_copy_string("SecAccessControlCreateWithFlags failed", error_msg, error_size);
goto cleanup;
}
if (error_msg != NULL && error_size != NULL) {
CFStringRef desc = CFErrorCopyDescription(access_error);
if (desc != NULL) {
if (!CFStringGetCString(desc, error_msg, *error_size, kCFStringEncodingUTF8)) {
safe_copy_string("SecAccessControlCreateWithFlags failed", error_msg, error_size);
} else {
*error_size = strlen(error_msg) + 1;
}
CFRelease(desc);
} else {
safe_copy_string("SecAccessControlCreateWithFlags failed", error_msg, error_size);
}
}
goto cleanup;
}
add_query = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
if (add_query == NULL) {
safe_copy_string("CFDictionaryCreateMutable failed", error_msg, error_size);
goto cleanup;
}
CFMutableDictionaryRef del_query = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
if (del_query != NULL) {
CFDictionarySetValue(del_query, kSecClass, kSecClassGenericPassword);
CFDictionarySetValue(del_query, kSecAttrService, service);
CFDictionarySetValue(del_query, kSecAttrAccount, account);
SecItemDelete(del_query);
CFRelease(del_query);
}
CFDictionarySetValue(add_query, kSecClass, kSecClassGenericPassword);
CFDictionarySetValue(add_query, kSecAttrService, service);
CFDictionarySetValue(add_query, kSecAttrAccount, account);
CFDictionarySetValue(add_query, kSecValueData, value);
CFDictionarySetValue(add_query, kSecAttrAccessControl, access_control);
status = SecItemAdd(add_query, NULL);
if (status != errSecSuccess) {
copy_osstatus_message(status, error_msg, error_size);
goto cleanup;
}
rc = 0;
cleanup:
if (add_query != NULL) CFRelease(add_query);
if (access_control != NULL) CFRelease(access_control);
if (access_error != NULL) CFRelease(access_error);
if (value != NULL) CFRelease(value);
if (account != NULL) CFRelease(account);
if (service != NULL) CFRelease(service);
return rc;
}
// Returns 0 on success, -1 on error
// On input: *data_size and *error_size contain buffer sizes
// On output: *data_size contains actual bytes written, *error_size includes null terminator
int
get_data_from_apple_keychain(
char *out_data,
size_t *data_size,
const char *name,
const char *service_param,
const char *title_param,
char *error_msg,
size_t *error_size
) {
int rc = -1;
OSStatus status;
CFTypeRef result = NULL;
CFMutableDictionaryRef read_query = NULL;
CFStringRef service = NULL;
CFStringRef account = NULL;
CFStringRef title = NULL;
if (out_data == NULL || data_size == NULL) {
safe_copy_string("Invalid arguments", error_msg, error_size);
goto cleanup;
}
// Use defaults if parameters are NULL
const char *service_name = (service_param != NULL && strlen(service_param) > 0)
? service_param
: SIGNER_DEFAULT_SERVICE;
const char *account_name = (name != NULL && strlen(name) > 0)
? name
: SIGNER_DEFAULT_ACCOUNT;
service = CFStringCreateWithCString(kCFAllocatorDefault, service_name, kCFStringEncodingUTF8);
if (service == NULL) {
safe_copy_string("CFStringCreateWithCString failed for service", error_msg, error_size);
goto cleanup;
}
account = CFStringCreateWithCString(kCFAllocatorDefault, account_name, kCFStringEncodingUTF8);
if (account == NULL) {
safe_copy_string("CFStringCreateWithCString failed for account", error_msg, error_size);
goto cleanup;
}
read_query = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
if (read_query == NULL) {
safe_copy_string("CFDictionaryCreateMutable failed (read)", error_msg, error_size);
goto cleanup;
}
CFDictionarySetValue(read_query, kSecClass, kSecClassGenericPassword);
CFDictionarySetValue(read_query, kSecAttrService, service);
CFDictionarySetValue(read_query, kSecAttrAccount, account);
CFDictionarySetValue(read_query, kSecReturnData, kCFBooleanTrue);
CFDictionarySetValue(read_query, kSecMatchLimit, kSecMatchLimitOne);
if (title_param != NULL && strlen(title_param) > 0) {
title = CFStringCreateWithCString(kCFAllocatorDefault, title_param, kCFStringEncodingUTF8);
if (title != NULL) {
CFDictionarySetValue(read_query, kSecUseOperationPrompt, title);
}
}
status = SecItemCopyMatching(read_query, &result);
if (status != errSecSuccess) {
copy_osstatus_message(status, error_msg, error_size);
goto cleanup;
}
if (result == NULL || CFGetTypeID(result) != CFDataGetTypeID()) {
safe_copy_string("SecItemCopyMatching returned unexpected result", error_msg, error_size);
goto cleanup;
}
CFDataRef data = (CFDataRef) result;
CFIndex len = CFDataGetLength(data);
const UInt8 *bytes = CFDataGetBytePtr(data);
if ((size_t) len > *data_size) {
safe_copy_string("Output buffer too small", error_msg, error_size);
goto cleanup;
}
memcpy(out_data, bytes, len);
*data_size = len;
// printf("[C] keychain item read (%ld bytes): %.*s\n", (long) len, (int) len, (const char *) bytes);
rc = 0;
cleanup:
if (result != NULL) CFRelease(result);
if (read_query != NULL) CFRelease(read_query);
if (title != NULL) CFRelease(title);
if (account != NULL) CFRelease(account);
if (service != NULL) CFRelease(service);
return rc;
}
// Returns 0 on success, -1 on error
int
delete_data_from_apple_keychain(
const char *name,
const char *service_param,
char *error_msg,
size_t *error_size
) {
int rc = -1;
OSStatus status;
CFMutableDictionaryRef delete_query = NULL;
CFStringRef service = NULL;
CFStringRef account = NULL;
// Use defaults if parameters are NULL
const char *service_name = (service_param != NULL && strlen(service_param) > 0)
? service_param
: SIGNER_DEFAULT_SERVICE;
const char *account_name = (name != NULL && strlen(name) > 0)
? name
: SIGNER_DEFAULT_ACCOUNT;
service = CFStringCreateWithCString(kCFAllocatorDefault, service_name, kCFStringEncodingUTF8);
if (service == NULL) {
safe_copy_string("CFStringCreateWithCString failed for service", error_msg, error_size);
goto cleanup;
}
account = CFStringCreateWithCString(kCFAllocatorDefault, account_name, kCFStringEncodingUTF8);
if (account == NULL) {
safe_copy_string("CFStringCreateWithCString failed for account", error_msg, error_size);
goto cleanup;
}
delete_query = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
if (delete_query == NULL) {
safe_copy_string("CFDictionaryCreateMutable failed (delete)", error_msg, error_size);
goto cleanup;
}
CFDictionarySetValue(delete_query, kSecClass, kSecClassGenericPassword);
CFDictionarySetValue(delete_query, kSecAttrService, service);
CFDictionarySetValue(delete_query, kSecAttrAccount, account);
status = SecItemDelete(delete_query);
if (status != errSecSuccess) {
// Treat "item not found" as a success
if (status == errSecItemNotFound) { // The error code for item not found
rc = 0;
goto cleanup;
}
// For all other errors, report and fail
copy_osstatus_message(status, error_msg, error_size);
goto cleanup;
}
rc = 0;
cleanup:
if (delete_query != NULL) CFRelease(delete_query);
if (account != NULL) CFRelease(account);
if (service != NULL) CFRelease(service);
return rc;
}
#endif