]> git.saurik.com Git - apple/security.git/blob - OSX/sec/Security/SecuritydXPC.c
Security-58286.200.222.tar.gz
[apple/security.git] / OSX / sec / Security / SecuritydXPC.c
1 /*
2 * Copyright (c) 2012-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 <Security/SecuritydXPC.h>
26 #include <Security/SecCFAllocator.h>
27 #include <ipc/securityd_client.h>
28 #include <utilities/SecCFError.h>
29 #include <utilities/SecDb.h>
30 #include <utilities/SecCFWrappers.h>
31 #include <utilities/der_plist.h>
32
33 // TODO Shorten these string values to save ipc bandwidth.
34 const char *kSecXPCKeyOperation = "operation";
35 const char *kSecXPCKeyResult = "status";
36 const char *kSecXPCKeyEndpoint = "endpoint";
37 const char *kSecXPCKeyError = "error";
38 const char *kSecXPCKeyClientToken = "client";
39 const char *kSecXPCKeyPeerInfoArray = "peer-infos";
40 const char *kSecXPCKeyPeerInfo = "peer-info";
41 const char *kSecXPCKeyUserLabel = "userlabel";
42 const char *kSecXPCKeyBackup = "backup";
43 const char *kSecXPCKeyKeybag = "keybag";
44 const char *kSecXPCKeyUserPassword = "password";
45 const char *kSecXPCKeyEMCSBackup = "emcsbackup";
46 const char *kSecXPCKeyDSID = "dsid";
47 const char *kSecXPCKeyQuery = "query";
48 const char *kSecXPCKeyAttributesToUpdate = "attributesToUpdate";
49 const char *kSecXPCKeyDomain = "domain";
50 const char *kSecXPCKeyDigest = "digest";
51 const char *kSecXPCKeyCertificate = "cert";
52 const char *kSecXPCKeySettings = "settings";
53 const char *kSecXPCKeyOTAFileDirectory = "path";
54 const char *kSecXPCLimitInMinutes = "limitMinutes";
55 const char *kSecXPCPublicPeerId = "publicPeerId"; // Public peer id
56 const char *kSecXPCOTRSession = "otrsess"; // OTR session bytes
57 const char *kSecXPCData = "data"; // Data to process
58 const char *kSecXPCOTRReady = "otrrdy"; // OTR ready for messages
59 const char *kSecXPCKeyViewName = "viewname";
60 const char *kSecXPCKeyViewActionCode = "viewactioncode";
61 const char *kSecXPCKeyHSA2AutoAcceptInfo = "autoacceptinfo";
62 const char *kSecXPCKeyString = "cfstring";
63 const char *kSecXPCKeyArray = "cfarray";
64 const char *kSecXPCKeySet = "cfset";
65 const char *kSecXPCKeySet2 = "cfset2";
66 const char *kSecXPCKeyNewPublicBackupKey = "newPublicBackupKey";
67 const char *kSecXPCKeyRecoveryPublicKey = "RecoveryPublicKey";
68 const char *kSecXPCKeyIncludeV0 = "includeV0";
69 const char *kSecXPCKeyReason = "reason";
70 const char *kSecXPCKeyEnabledViewsKey = "enabledViews";
71 const char *kSecXPCKeyDisabledViewsKey = "disabledViews";
72 const char *kSecXPCKeyEscrowLabel = "escrow";
73 const char *kSecXPCKeyTriesLabel = "tries";
74 const char *kSecXPCKeyFileDescriptor = "fileDescriptor";
75 const char *kSecXPCKeyAccessGroups = "accessGroups";
76 const char *kSecXPCKeyClasses = "classes";
77 const char *kSecXPCKeyNormalizedIssuer = "normIssuer";
78 const char *kSecXPCKeySerialNumber = "serialNum";
79 const char *kSecXPCKeyBackupKeybagIdentifier = "backupKeybagID";
80 const char *kSecXPCKeyBackupKeybagPath = "backupKeybagPath";
81 const char *kSecXPCVersion = "version";
82 const char *kSecXPCKeySignInAnalytics = "signinanalytics";
83 //
84 // XPC Functions for both client and server.
85 //
86
87
88 CFStringRef SOSCCGetOperationDescription(enum SecXPCOperation op)
89 {
90 switch (op) {
91 case kSecXPCOpAccountSetToNew:
92 return CFSTR("AccountSetToNew");
93 case kSecXPCOpOTAGetEscrowCertificates:
94 return CFSTR("OTAGetEscrowCertificates");
95 case kSecXPCOpOTAPKIGetNewAsset:
96 return CFSTR("OTAPKIGetNewAsset");
97 case kSecXPCOpAcceptApplicants:
98 return CFSTR("AcceptApplicants");
99 case kSecXPCOpApplyToARing:
100 return CFSTR("ApplyToARing");
101 case kSecXPCOpBailFromCircle:
102 return CFSTR("BailFromCircle");
103 case kSecXPCOpCanAuthenticate:
104 return CFSTR("CanAuthenticate");
105 case kSecXPCOpCopyApplicantPeerInfo:
106 return CFSTR("CopyApplicantPeerInfo");
107 case kSecXPCOpCopyConcurringPeerPeerInfo:
108 return CFSTR("CopyConcurringPeerPeerInfo");
109 case kSecXPCOpCopyEngineState:
110 return CFSTR("CopyEngineState");
111 case kSecXPCOpCopyGenerationPeerInfo:
112 return CFSTR("CopyGenerationPeerInfo");
113 case kSecXPCOpCopyIncompatibilityInfo:
114 return CFSTR("CopyIncompatibilityInfo");
115 case kSecXPCOpCopyMyPeerInfo:
116 return CFSTR("CopyMyPeerInfo");
117 case kSecXPCOpCopyNotValidPeerPeerInfo:
118 return CFSTR("CopyNotValidPeerPeerInfo");
119 case kSecXPCOpCopyPeerPeerInfo:
120 return CFSTR("CopyPeerPeerInfo");
121 case kSecXPCOpCopyRetirementPeerInfo:
122 return CFSTR("CopyRetirementPeerInfo");
123 case kSecXPCOpCopyValidPeerPeerInfo:
124 return CFSTR("CopyValidPeerPeerInfo");
125 case kSecXPCOpCopyViewUnawarePeerInfo:
126 return CFSTR("CopyViewUnawarePeerInfo");
127 case kSecXPCOpDeviceInCircle:
128 return CFSTR("DeviceInCircle");
129 case kSecXPCOpEnableRing:
130 return CFSTR("EnableRing");
131 case kSecXPCOpGetAllTheRings:
132 return CFSTR("GetAllTheRings");
133 case kSecXPCOpGetLastDepartureReason:
134 return CFSTR("GetLastDepartureReason");
135 case kSecXPCOpSyncWithKVSPeer:
136 return CFSTR("SyncKVSPeer");
137 case kSecXPCOpClearKVSPeerMessage:
138 return CFSTR("kSecXPCOpClearKVSPeerMessage");
139 case kSecXPCOpLoggedOutOfAccount:
140 return CFSTR("LoggedOutOfAccount");
141 case kSecXPCOpProcessSyncWithAllPeers:
142 return CFSTR("ProcessSyncWithAllPeers");
143 case kSecXPCOpProcessSyncWithPeers:
144 return CFSTR("ProcessSyncWithPeers");
145 case kSecXPCOpProcessUnlockNotification:
146 return CFSTR("ProcessUnlockNotification");
147 case kSecXPCOpPurgeUserCredentials:
148 return CFSTR("PurgeUserCredentials");
149 case kSecXPCOpRejectApplicants:
150 return CFSTR("RejectApplicants");
151 case kSecXPCOpRemoveThisDeviceFromCircle:
152 return CFSTR("RemoveThisDeviceFromCircle");
153 case kSecXPCOpRemoveThisDeviceFromCircleWithAnalytics:
154 return CFSTR("RemoveThisDeviceFromCircleWithAnalytics");
155 case kSecXPCOpRemovePeersFromCircle:
156 return CFSTR("RemovePeersFromCircle");
157 case kSecXPCOpRemovePeersFromCircleWithAnalytics:
158 return CFSTR("RemovePeersFromCircleWithAnalytics");
159 case kSecXPCOpRequestEnsureFreshParameters:
160 return CFSTR("RequestEnsureFreshParameters");
161 case kSecXPCOpRequestToJoin:
162 return CFSTR("RequestToJoin");
163 case kSecXPCOpRequestToJoinWithAnalytics:
164 return CFSTR("RequestToJoinWithAnalytics");
165 case kSecXPCOpRequestToJoinAfterRestore:
166 return CFSTR("RequestToJoinAfterRestore");
167 case kSecXPCOpRequestToJoinAfterRestoreWithAnalytics:
168 return CFSTR("RequestToJoinAfterRestoreWithAnalytics");
169 case kSecXPCOpResetToEmpty:
170 return CFSTR("ResetToEmpty");
171 case kSecXPCOpResetToEmptyWithAnalytics:
172 return CFSTR("ResetToEmptyWithAnalytics");
173 case kSecXPCOpResetToOffering:
174 return CFSTR("ResetToOffering");
175 case kSecXPCOpRingStatus:
176 return CFSTR("RingStatus");
177 case kSecXPCOpRollKeys:
178 return CFSTR("RollKeys");
179 case kSecXPCOpSetBagForAllSlices:
180 return CFSTR("SetBagForAllSlices");
181 case kSecXPCOpSetLastDepartureReason:
182 return CFSTR("SetLastDepartureReason");
183 case kSecXPCOpSetNewPublicBackupKey:
184 return CFSTR("SetNewPublicBackupKey");
185 case kSecXPCOpSetUserCredentials:
186 return CFSTR("SetUserCredentials");
187 case kSecXPCOpSetUserCredentialsAndDSID:
188 return CFSTR("SetUserCredentialsAndDSID");
189 case kSecXPCOpSetUserCredentialsAndDSIDWithAnalytics:
190 return CFSTR("SetUserCredentialsAndDSIDWithAnalytics");
191 case kSecXPCOpTryUserCredentials:
192 return CFSTR("TryUserCredentials");
193 case kSecXPCOpValidateUserPublic:
194 return CFSTR("ValidateUserPublic");
195 case kSecXPCOpView:
196 return CFSTR("View");
197 case kSecXPCOpWithdrawlFromARing:
198 return CFSTR("WithdrawlFromARing");
199 case sec_add_shared_web_credential_id:
200 return CFSTR("add_shared_web_credential");
201 case sec_copy_shared_web_credential_id:
202 return CFSTR("copy_shared_web_credential");
203 case sec_delete_all_id:
204 return CFSTR("delete_all");
205 case sec_get_log_settings_id:
206 return CFSTR("get_log_settings");
207 case sec_item_add_id:
208 return CFSTR("add");
209 case sec_item_backup_copy_names_id:
210 return CFSTR("backup_copy_names");
211 case sec_item_backup_handoff_fd_id:
212 return CFSTR("backup_handoff_fd");
213 case sec_item_backup_restore_id:
214 return CFSTR("backup_restore");
215 case sec_item_backup_set_confirmed_manifest_id:
216 return CFSTR("backup_set_confirmed_manifest");
217 case sec_item_copy_matching_id:
218 return CFSTR("copy_matching");
219 case sec_item_delete_id:
220 return CFSTR("delete");
221 case sec_item_update_id:
222 return CFSTR("update");
223 case sec_keychain_backup_id:
224 return CFSTR("keychain_backup");
225 case sec_keychain_backup_syncable_id:
226 return CFSTR("keychain_backup_syncable");
227 case sec_keychain_restore_id:
228 return CFSTR("keychain_restore");
229 case sec_keychain_restore_syncable_id:
230 return CFSTR("keychain_restore_syncable");
231 case sec_keychain_sync_update_message_id:
232 return CFSTR("keychain_sync_update_message");
233 case sec_ota_pki_trust_store_version_id:
234 return CFSTR("ota_pki_trust_store_version");
235 case sec_otr_session_create_remote_id:
236 return CFSTR("otr_session_create_remote");
237 case sec_otr_session_process_packet_remote_id:
238 return CFSTR("otr_session_process_packet_remote");
239 case sec_set_circle_log_settings_id:
240 return CFSTR("set_circle_log_settings");
241 case sec_set_xpc_log_settings_id:
242 return CFSTR("set_xpc_log_settings");
243 case sec_trust_evaluate_id:
244 return CFSTR("trust_evaluate");
245 case sec_trust_store_contains_id:
246 return CFSTR("trust_store_contains");
247 case sec_trust_store_remove_certificate_id:
248 return CFSTR("trust_store_remove_certificate");
249 case sec_trust_store_set_trust_settings_id:
250 return CFSTR("trust_store_set_trust_settings");
251 case sec_trust_store_copy_all_id:
252 return CFSTR("trust_store_copy_all");
253 case sec_trust_store_copy_usage_constraints_id:
254 return CFSTR("trust_store_copy_usage_constraints");
255 case sec_ocsp_cache_flush_id:
256 return CFSTR("ocsp_cache_flush");
257 case soscc_EnsurePeerRegistration_id:
258 return CFSTR("EnsurePeerRegistration");
259 case kSecXPCOpSetEscrowRecord:
260 return CFSTR("SetEscrowRecord");
261 case kSecXPCOpGetEscrowRecord:
262 return CFSTR("GetEscrowRecord");
263 case kSecXPCOpWhoAmI:
264 return CFSTR("WhoAmI");
265 case kSecXPCOpTransmogrifyToSyncBubble:
266 return CFSTR("TransmogrifyToSyncBubble");
267 case kSecXPCOpWrapToBackupSliceKeyBagForView:
268 return CFSTR("WrapToBackupSliceKeyBagForView");
269 case kSecXPCOpCopyAccountData:
270 return CFSTR("CopyAccountDataFromKeychain");
271 case kSecXPCOpDeleteAccountData:
272 return CFSTR("DeleteAccountDataFromKeychain");
273 case kSecXPCOpCopyEngineData:
274 return CFSTR("CopyEngineDataFromKeychain");
275 case kSecXPCOpDeleteEngineData:
276 return CFSTR("DeleteEngineDataFromKeychain");
277 case sec_item_update_token_items_id:
278 return CFSTR("UpdateTokenItems");
279 case sec_delete_items_with_access_groups_id:
280 return CFSTR("sec_delete_items_with_access_groups_id");
281 case kSecXPCOpPeersHaveViewsEnabled:
282 return CFSTR("kSecXPCOpPeersHaveViewsEnabled");
283 case kSecXPCOpRegisterRecoveryPublicKey:
284 return CFSTR("RegisterRecoveryPublicKey");
285 case kSecXPCOpGetRecoveryPublicKey:
286 return CFSTR("GetRecoveryPublicKey");
287 case kSecXPCOpCopyBackupInformation:
288 return CFSTR("CopyBackupInformation");
289 case kSecXPCOpMessageFromPeerIsPending:
290 return CFSTR("MessageFromPeerIsPending");
291 case kSecXPCOpSendToPeerIsPending:
292 return CFSTR("SendToPeerIsPending");
293 case sec_item_copy_parent_certificates_id:
294 return CFSTR("copy_parent_certificates");
295 case sec_item_certificate_exists_id:
296 return CFSTR("certificate_exists");
297 case kSecXPCOpBackupKeybagAdd:
298 return CFSTR("KeybagAdd");
299 case kSecXPCOpBackupKeybagDelete:
300 return CFSTR("KeybagDelete");
301 case kSecXPCOpKeychainControlEndpoint:
302 return CFSTR("KeychainControlEndpoint");
303 case kSecXPCOpTLSAnaltyicsReport:
304 return CFSTR("TLSAnalyticsReport");
305 default:
306 return CFSTR("Unknown xpc operation");
307 }
308 }
309
310 bool SecXPCDictionarySetPList(xpc_object_t message, const char *key, CFTypeRef object, CFErrorRef *error)
311 {
312 if (!object)
313 return SecError(errSecParam, error, CFSTR("object for key %s is NULL"), key);
314
315 size_t size = der_sizeof_plist(object, error);
316 if (!size)
317 return false;
318 uint8_t *der = malloc(size);
319 uint8_t *der_end = der + size;
320 uint8_t *der_start = der_encode_plist(object, error, der, der_end);
321 if (!der_start) {
322 free(der);
323 return false;
324 }
325
326 assert(der == der_start);
327 xpc_dictionary_set_data(message, key, der_start, der_end - der_start);
328 free(der);
329 return true;
330 }
331
332 bool SecXPCDictionarySetPListOptional(xpc_object_t message, const char *key, CFTypeRef object, CFErrorRef *error) {
333 return !object || SecXPCDictionarySetPList(message, key, object, error);
334 }
335
336 bool SecXPCDictionarySetData(xpc_object_t message, const char *key, CFDataRef data, CFErrorRef *error)
337 {
338 if (!data)
339 return SecError(errSecParam, error, CFSTR("data for key %s is NULL"), key);
340
341 xpc_dictionary_set_data(message, key, CFDataGetBytePtr(data), CFDataGetLength(data));
342 return true;
343 }
344
345 bool SecXPCDictionarySetBool(xpc_object_t message, const char *key, bool value, CFErrorRef *error)
346 {
347 xpc_dictionary_set_bool(message, key, value);
348 return true;
349 }
350
351 bool SecXPCDictionarySetString(xpc_object_t message, const char *key, CFStringRef string, CFErrorRef *error)
352 {
353 if (!string)
354 return SecError(errSecParam, error, CFSTR("string for key %s is NULL"), key);
355
356 __block bool ok = true;
357 CFStringPerformWithCString(string, ^(const char *utf8Str) {
358 if (utf8Str)
359 xpc_dictionary_set_string(message, key, utf8Str);
360 else
361 ok = SecError(errSecParam, error, CFSTR("failed to convert string for key %s to utf8"), key);
362 });
363 return ok;
364 }
365
366 bool SecXPCDictionarySetStringOptional(xpc_object_t message, const char *key, CFStringRef string, CFErrorRef *error) {
367 return !string || SecXPCDictionarySetString(message, key, string, error);
368 }
369
370 bool SecXPCDictionarySetDataOptional(xpc_object_t message, const char *key, CFDataRef data, CFErrorRef *error) {
371 return !data || SecXPCDictionarySetData(message, key, data, error);
372 }
373
374 bool SecXPCDictionarySetInt64(xpc_object_t message, const char *key, int64_t value, CFErrorRef *error) {
375 xpc_dictionary_set_int64(message, key, value);
376 return true;
377 }
378
379 bool SecXPCDictionarySetFileDescriptor(xpc_object_t message, const char *key, int fd, CFErrorRef *error) {
380 xpc_dictionary_set_fd(message, key, fd);
381 return true;
382 }
383
384 int SecXPCDictionaryDupFileDescriptor(xpc_object_t message, const char *key, CFErrorRef *error) {
385 int fd = xpc_dictionary_dup_fd(message, key);
386 if (fd < 0)
387 SecError(errSecParam, error, CFSTR("missing fd for key %s"), key);
388
389 return fd;
390 }
391
392 CFSetRef SecXPCDictionaryCopySet(xpc_object_t message, const char *key, CFErrorRef *error) {
393 CFTypeRef obj = SecXPCDictionaryCopyPList(message, key, error);
394 CFSetRef set = copyIfSet(obj, error);
395 if (obj && !set) {
396 CFStringRef description = CFCopyTypeIDDescription(CFGetTypeID(obj));
397 SecError(errSecParam, error, CFSTR("object for key %s not set but %@"), key, description);
398 CFReleaseNull(description);
399 }
400 CFReleaseNull(obj);
401 return set;
402 }
403
404 CFArrayRef SecXPCDictionaryCopyArray(xpc_object_t message, const char *key, CFErrorRef *error) {
405 CFTypeRef array = SecXPCDictionaryCopyPList(message, key, error);
406 if (array) {
407 CFTypeID type_id = CFGetTypeID(array);
408 if (type_id != CFArrayGetTypeID()) {
409 CFStringRef description = CFCopyTypeIDDescription(type_id);
410 SecError(errSecParam, error, CFSTR("object for key %s not array but %@"), key, description);
411 CFReleaseNull(description);
412 CFReleaseNull(array);
413 }
414 }
415 return (CFArrayRef)array;
416 }
417
418 bool SecXPCDictionaryCopyArrayOptional(xpc_object_t message, const char *key, CFArrayRef *parray, CFErrorRef *error) {
419 if (!xpc_dictionary_get_value(message, key)) {
420 *parray = NULL;
421 return true;
422 }
423 *parray = SecXPCDictionaryCopyArray(message, key, error);
424 return *parray;
425 }
426
427 CFDataRef SecXPCDictionaryCopyData(xpc_object_t message, const char *key, CFErrorRef *error) {
428 CFDataRef data = NULL;
429 size_t size = 0;
430 const uint8_t *bytes = xpc_dictionary_get_data(message, key, &size);
431 if (!bytes) {
432 SecError(errSecParam, error, CFSTR("no data for key %s"), key);
433 return NULL;
434 }
435
436 data = CFDataCreate(kCFAllocatorDefault, bytes, size);
437 if (!data)
438 SecError(errSecParam, error, CFSTR("failed to create data for key %s"), key);
439
440 return data;
441 }
442
443 bool SecXPCDictionaryGetBool(xpc_object_t message, const char *key, CFErrorRef *__unused error) {
444 return xpc_dictionary_get_bool(message, key);
445 }
446
447 bool SecXPCDictionaryGetDouble(xpc_object_t message, const char *key, double *pvalue, CFErrorRef *error) {
448 *pvalue = xpc_dictionary_get_double(message, key);
449 if (*pvalue == NAN) {
450 return SecError(errSecParam, error, CFSTR("object for key %s bad double"), key);
451 }
452 return true;
453 }
454
455 bool SecXPCDictionaryCopyDataOptional(xpc_object_t message, const char *key, CFDataRef *pdata, CFErrorRef *error) {
456 size_t size = 0;
457 if (!xpc_dictionary_get_data(message, key, &size)) {
458 *pdata = NULL;
459 return true;
460 }
461 *pdata = SecXPCDictionaryCopyData(message, key, error);
462 return *pdata;
463 }
464
465 bool SecXPCDictionaryCopyURLOptional(xpc_object_t message, const char *key, CFURLRef *purl, CFErrorRef *error) {
466 size_t size = 0;
467 if (!xpc_dictionary_get_data(message, key, &size)) {
468 *purl = NULL;
469 return true;
470 }
471 CFDataRef data = SecXPCDictionaryCopyData(message, key, error);
472 if (data) {
473 *purl = CFURLCreateWithBytes(kCFAllocatorDefault, CFDataGetBytePtr(data), CFDataGetLength(data), kCFStringEncodingUTF8, NULL);
474 }
475 CFReleaseNull(data);
476 return *purl;
477 }
478
479 CFDictionaryRef SecXPCDictionaryCopyDictionary(xpc_object_t message, const char *key, CFErrorRef *error) {
480 CFTypeRef dict = SecXPCDictionaryCopyPList(message, key, error);
481 if (dict) {
482 CFTypeID type_id = CFGetTypeID(dict);
483 if (type_id != CFDictionaryGetTypeID()) {
484 CFStringRef description = CFCopyTypeIDDescription(type_id);
485 SecError(errSecParam, error, CFSTR("object for key %s not dictionary but %@"), key, description);
486 CFReleaseNull(description);
487 CFReleaseNull(dict);
488 }
489 }
490 return (CFDictionaryRef)dict;
491 }
492
493 bool SecXPCDictionaryCopyDictionaryOptional(xpc_object_t message, const char *key, CFDictionaryRef *pdictionary, CFErrorRef *error) {
494 if (!xpc_dictionary_get_value(message, key)) {
495 *pdictionary = NULL;
496 return true;
497 }
498 *pdictionary = SecXPCDictionaryCopyDictionary(message, key, error);
499 return *pdictionary;
500 }
501
502 CFTypeRef SecXPCDictionaryCopyPList(xpc_object_t message, const char *key, CFErrorRef *error)
503 {
504 CFTypeRef cfobject = NULL;
505 size_t size = 0;
506 const uint8_t *der = xpc_dictionary_get_data(message, key, &size);
507 if (!der) {
508 SecError(errSecParam, error, CFSTR("no object for key %s"), key);
509 return NULL;
510 }
511
512 const uint8_t *der_end = der + size;
513 /* use the sensitive allocator so that the dictionary is zeroized upon deallocation */
514 const uint8_t *decode_end = der_decode_plist(SecCFAllocatorZeroize(), kCFPropertyListImmutable,
515 &cfobject, error, der, der_end);
516 if (decode_end != der_end) {
517 SecError(errSecParam, error, CFSTR("trailing garbage after der decoded object for key %s"), key);
518 CFReleaseNull(cfobject);
519 }
520
521 /* zeroize xpc value as it may have contained raw key material */
522 cc_clear(size, (void *)der);
523
524 return cfobject;
525 }
526
527 bool SecXPCDictionaryCopyPListOptional(xpc_object_t message, const char *key, CFTypeRef *pobject, CFErrorRef *error) {
528 size_t size = 0;
529 if (!xpc_dictionary_get_data(message, key, &size)) {
530 *pobject = NULL;
531 return true;
532 }
533 *pobject = SecXPCDictionaryCopyPList(message, key, error);
534 return *pobject;
535 }
536
537 CFStringRef SecXPCDictionaryCopyString(xpc_object_t message, const char *key, CFErrorRef *error) {
538 const char *string = xpc_dictionary_get_string(message, key);
539 if (string) {
540 CFStringRef result = CFStringCreateWithCString(kCFAllocatorDefault, string, kCFStringEncodingUTF8);
541 if (!result) {
542 SecError(errSecAllocate, error, CFSTR("object for key %s failed to convert %s to CFString"), key, string);
543 }
544 return result;
545 } else {
546 SecError(errSecParam, error, CFSTR("object for key %s not string"), key);
547 return NULL;
548 }
549 }
550
551 bool SecXPCDictionaryCopyStringOptional(xpc_object_t message, const char *key, CFStringRef *pstring, CFErrorRef *error) {
552 if (!xpc_dictionary_get_value(message, key)) {
553 *pstring = NULL;
554 return true;
555 }
556 *pstring = SecXPCDictionaryCopyString(message, key, error);
557 return *pstring;
558 }
559
560 static CFDataRef CFDataCreateWithXPCArrayAtIndex(xpc_object_t xpc_data_array, size_t index, CFErrorRef *error) {
561 CFDataRef data = NULL;
562 size_t length = 0;
563 const uint8_t *bytes = xpc_array_get_data(xpc_data_array, index, &length);
564 if (bytes) {
565 data = CFDataCreate(kCFAllocatorDefault, bytes, length);
566 }
567 if (!data)
568 SecError(errSecParam, error, CFSTR("data_array[%zu] failed to decode"), index);
569
570 return data;
571 }
572
573 static CFArrayRef CFDataXPCArrayCopyArray(xpc_object_t xpc_data_array, CFErrorRef *error) {
574 CFMutableArrayRef data_array = NULL;
575 require_action_quiet(xpc_get_type(xpc_data_array) == XPC_TYPE_ARRAY, exit,
576 SecError(errSecParam, error, CFSTR("data_array xpc value is not an array")));
577 size_t count = xpc_array_get_count(xpc_data_array);
578 require_action_quiet(data_array = CFArrayCreateMutable(kCFAllocatorDefault, count, &kCFTypeArrayCallBacks), exit,
579 SecError(errSecAllocate, error, CFSTR("failed to create CFArray of capacity %zu"), count));
580
581 size_t ix;
582 for (ix = 0; ix < count; ++ix) {
583 CFDataRef data = CFDataCreateWithXPCArrayAtIndex(xpc_data_array, ix, error);
584 if (!data) {
585 CFRelease(data_array);
586 return NULL;
587 }
588 CFArraySetValueAtIndex(data_array, ix, data);
589 CFRelease(data);
590 }
591
592 exit:
593 return data_array;
594 }
595
596 bool SecXPCDictionaryCopyCFDataArrayOptional(xpc_object_t message, const char *key, CFArrayRef *data_array, CFErrorRef *error) {
597 xpc_object_t xpc_data_array = xpc_dictionary_get_value(message, key);
598 if (!xpc_data_array) {
599 if (data_array)
600 *data_array = NULL;
601 return true;
602 }
603 *data_array = CFDataXPCArrayCopyArray(xpc_data_array, error);
604 return *data_array != NULL;
605 }