1 /* Copyright (c) 2012-2013 Apple Inc. All Rights Reserved. */
3 #include "authutilities.h"
4 #include "authd_private.h"
7 #include <AssertMacros.h>
12 SerializeItemSet(const AuthorizationItemSet
* itemSet
)
14 xpc_object_t set
= NULL
;
15 require_quiet(itemSet
!= NULL
, done
);
16 require_quiet(itemSet
->count
!= 0, done
);
18 set
= xpc_array_create(NULL
, 0);
19 require(set
!= NULL
, done
);
21 for (uint32_t i
= 0; i
< itemSet
->count
; i
++) {
22 xpc_object_t item
= xpc_dictionary_create(NULL
, NULL
, 0);
23 require(item
!= NULL
, done
);
25 xpc_dictionary_set_string(item
, AUTH_XPC_ITEM_NAME
, itemSet
->items
[i
].name
);
26 xpc_dictionary_set_uint64(item
, AUTH_XPC_ITEM_FLAGS
, itemSet
->items
[i
].flags
);
27 xpc_dictionary_set_data(item
, AUTH_XPC_ITEM_VALUE
, itemSet
->items
[i
].value
, itemSet
->items
[i
].valueLength
);
28 xpc_array_set_value(set
, XPC_ARRAY_APPEND
, item
);
36 AuthorizationItemSet
*
37 DeserializeItemSet(const xpc_object_t data
)
39 AuthorizationItemSet
* set
= NULL
;
40 require_quiet(data
!= NULL
, done
);
42 require(xpc_get_type(data
) == XPC_TYPE_ARRAY
, done
);
44 set
= (AuthorizationItemSet
*)calloc(1u, sizeof(AuthorizationItemSet
));
45 require(set
!= NULL
, done
);
47 set
->count
= (uint32_t)xpc_array_get_count(data
);
49 set
->items
= (AuthorizationItem
*)calloc(set
->count
, sizeof(AuthorizationItem
));
50 require_action(set
->items
!= NULL
, done
, set
->count
= 0);
52 xpc_array_apply(data
, ^bool(size_t index
, xpc_object_t value
) {
54 require(xpc_get_type(value
) == XPC_TYPE_DICTIONARY
, done
);
56 const char * name
= xpc_dictionary_get_string(value
, AUTH_XPC_ITEM_NAME
);
58 nameLen
= strlen(name
) + 1;
59 set
->items
[index
].name
= calloc(1u, nameLen
);
60 require(set
->items
[index
].name
!= NULL
, done
);
62 strlcpy((char*)set
->items
[index
].name
, name
, nameLen
);
64 set
->items
[index
].flags
= (uint32_t)xpc_dictionary_get_uint64(value
, AUTH_XPC_ITEM_FLAGS
);
66 const void * valueData
= xpc_dictionary_get_data(value
, AUTH_XPC_ITEM_VALUE
, &len
);
68 // <rdar://problem/13033889> authd is holding on to multiple copies of my password in the clear
69 if (xpc_dictionary_get_value(value
, AUTH_XPC_ITEM_SENSITIVE_VALUE_LENGTH
) != NULL
) {
70 size_t sensitiveLength
= (size_t)xpc_dictionary_get_uint64(value
, AUTH_XPC_ITEM_SENSITIVE_VALUE_LENGTH
);
71 dataCopy
= malloc(sensitiveLength
);
72 require(dataCopy
!= NULL
, done
);
73 memcpy(dataCopy
, valueData
, sensitiveLength
);
74 memset_s((void *)valueData
, len
, 0, sensitiveLength
); // clear the sensitive data, memset_s is never optimized away
75 len
= sensitiveLength
;
77 dataCopy
= malloc(len
);
78 require(dataCopy
!= NULL
, done
);
79 memcpy(dataCopy
, valueData
, len
);
82 set
->items
[index
].valueLength
= len
;
84 set
->items
[index
].value
= calloc(1u, len
);
85 require(set
->items
[index
].value
!= NULL
, done
);
87 memcpy(set
->items
[index
].value
, dataCopy
, len
);
104 void FreeItemSet(AuthorizationItemSet
* itemSet
)
106 if (!itemSet
) { return; }
108 for(uint32_t i
= 0; i
< itemSet
->count
; i
++ ) {
109 if (itemSet
->items
[i
].name
) {
110 free((void*)itemSet
->items
[i
].name
);
112 if (itemSet
->items
[i
].value
) {
113 free(itemSet
->items
[i
].value
);
116 if (itemSet
->items
) {
117 free(itemSet
->items
);
124 _copy_cf_string(CFTypeRef str
, const char * defaultValue
)
126 char * result
= NULL
;
127 require(str
!= NULL
, done
);
128 require(CFGetTypeID(str
) == CFStringGetTypeID(), done
);
130 CFIndex length
= CFStringGetLength(str
);
131 CFIndex size
= CFStringGetMaximumSizeForEncoding(length
, kCFStringEncodingUTF8
) + 1;
133 result
= (char*)calloc(1u, (size_t)size
);
134 check(result
!= NULL
);
136 if (!CFStringGetCString(str
, result
, size
, kCFStringEncodingUTF8
)) {
141 if (result
== NULL
&& defaultValue
) {
142 size_t len
= strlen(defaultValue
);
143 result
= (char*)calloc(1u, len
);
144 check(result
!= NULL
);
146 strlcpy(result
, defaultValue
, len
);
153 _get_cf_int(CFTypeRef num
, int64_t defaultValue
)
155 int64_t result
= defaultValue
;
156 require(num
!= NULL
, done
);
157 require(CFGetTypeID(num
) == CFNumberGetTypeID(), done
);
159 if (!CFNumberGetValue(num
, kCFNumberSInt64Type
, &result
)) {
160 result
= defaultValue
;
168 _get_cf_bool(CFTypeRef value
, bool defaultValue
)
170 bool result
= defaultValue
;
171 require(value
!= NULL
, done
);
172 require(CFGetTypeID(value
) == CFBooleanGetTypeID(), done
);
174 result
= CFBooleanGetValue(value
);
181 _compare_string(const char * str1
, const char * str2
)
183 if (!(str1
== str2
)) { // compare null or same pointer
184 if (str1
&& str2
) { // check both are non null
185 if (strcasecmp(str1
, str2
) != 0) { // compare strings
186 return false; // return false if not equal
189 return false; // return false if one null
197 _copy_string(const char * str
)
199 char * result
= NULL
;
200 require(str
!= NULL
, done
);
202 size_t len
= strlen(str
) + 1;
203 result
= calloc(1u, len
);
204 require(result
!= NULL
, done
);
206 strlcpy(result
, str
, len
);
213 _copy_data(const void * data
, size_t dataLen
)
215 void * result
= NULL
;
216 require(data
!= NULL
, done
);
218 result
= calloc(1u, dataLen
);
219 require(result
!= NULL
, done
);
221 memcpy(result
, data
, dataLen
);
227 bool _cf_set_iterate(CFSetRef set
, bool(^iterator
)(CFTypeRef value
))
230 CFTypeRef
* values
= NULL
;
232 require(set
!= NULL
, done
);
234 CFIndex count
= CFSetGetCount(set
);
235 values
= calloc((size_t)count
, sizeof(CFTypeRef
));
236 require(values
!= NULL
, done
);
238 CFSetGetValues(set
, values
);
239 for (CFIndex i
= 0; i
< count
; i
++) {
240 result
= iterator(values
[i
]);
251 bool _cf_bag_iterate(CFBagRef bag
, bool(^iterator
)(CFTypeRef value
))
254 CFTypeRef
* values
= NULL
;
256 require(bag
!= NULL
, done
);
258 CFIndex count
= CFBagGetCount(bag
);
259 values
= calloc((size_t)count
, sizeof(CFTypeRef
));
260 require(values
!= NULL
, done
);
262 CFBagGetValues(bag
, values
);
263 for (CFIndex i
= 0; i
< count
; i
++) {
264 result
= iterator(values
[i
]);
275 bool _cf_dictionary_iterate(CFDictionaryRef dict
, bool(^iterator
)(CFTypeRef key
, CFTypeRef value
))
278 CFTypeRef
* keys
= NULL
;
279 CFTypeRef
* values
= NULL
;
281 require(dict
!= NULL
, done
);
283 CFIndex count
= CFDictionaryGetCount(dict
);
284 keys
= calloc((size_t)count
, sizeof(CFTypeRef
));
285 require(keys
!= NULL
, done
);
287 values
= calloc((size_t)count
, sizeof(CFTypeRef
));
288 require(values
!= NULL
, done
);
290 CFDictionaryGetKeysAndValues(dict
, keys
, values
);
291 for (CFIndex i
= 0; i
< count
; i
++) {
292 result
= iterator(keys
[i
], values
[i
]);