1 /* Copyright (c) 2012-2013 Apple Inc. All Rights Reserved. */
3 #include "authutilities.h"
4 #include "authd_private.h"
7 #include <AssertMacros.h>
11 SerializeItemSet(const AuthorizationItemSet
* itemSet
)
13 xpc_object_t set
= NULL
;
14 require_quiet(itemSet
!= NULL
, done
);
15 require_quiet(itemSet
->count
!= 0, done
);
17 set
= xpc_array_create(NULL
, 0);
18 require(set
!= NULL
, done
);
20 for (uint32_t i
= 0; i
< itemSet
->count
; i
++) {
21 xpc_object_t item
= xpc_dictionary_create(NULL
, NULL
, 0);
22 require(item
!= NULL
, done
);
24 xpc_dictionary_set_string(item
, AUTH_XPC_ITEM_NAME
, itemSet
->items
[i
].name
);
25 xpc_dictionary_set_uint64(item
, AUTH_XPC_ITEM_FLAGS
, itemSet
->items
[i
].flags
);
26 xpc_dictionary_set_data(item
, AUTH_XPC_ITEM_VALUE
, itemSet
->items
[i
].value
, itemSet
->items
[i
].valueLength
);
27 xpc_array_set_value(set
, XPC_ARRAY_APPEND
, item
);
35 AuthorizationItemSet
*
36 DeserializeItemSet(const xpc_object_t data
)
38 AuthorizationItemSet
* set
= NULL
;
39 require_quiet(data
!= NULL
, done
);
41 require(xpc_get_type(data
) == XPC_TYPE_ARRAY
, done
);
43 set
= (AuthorizationItemSet
*)calloc(1u, sizeof(AuthorizationItemSet
));
44 require(set
!= NULL
, done
);
46 set
->count
= (uint32_t)xpc_array_get_count(data
);
48 set
->items
= (AuthorizationItem
*)calloc(set
->count
, sizeof(AuthorizationItem
));
49 require_action(set
->items
!= NULL
, done
, set
->count
= 0);
51 xpc_array_apply(data
, ^bool(size_t index
, xpc_object_t value
) {
53 require(xpc_get_type(value
) == XPC_TYPE_DICTIONARY
, done
);
55 const char * name
= xpc_dictionary_get_string(value
, AUTH_XPC_ITEM_NAME
);
57 nameLen
= strlen(name
) + 1;
58 set
->items
[index
].name
= calloc(1u, nameLen
);
59 require(set
->items
[index
].name
!= NULL
, done
);
61 strlcpy((char*)set
->items
[index
].name
, name
, nameLen
);
63 set
->items
[index
].flags
= (uint32_t)xpc_dictionary_get_uint64(value
, AUTH_XPC_ITEM_FLAGS
);
65 const void * valueData
= xpc_dictionary_get_data(value
, AUTH_XPC_ITEM_VALUE
, &len
);
67 // <rdar://problem/13033889> authd is holding on to multiple copies of my password in the clear
68 if (xpc_dictionary_get_value(value
, AUTH_XPC_ITEM_SENSITIVE_VALUE_LENGTH
) != NULL
) {
69 size_t sensitiveLength
= (size_t)xpc_dictionary_get_uint64(value
, AUTH_XPC_ITEM_SENSITIVE_VALUE_LENGTH
);
70 dataCopy
= malloc(sensitiveLength
);
71 require(dataCopy
!= NULL
, done
);
72 memcpy(dataCopy
, valueData
, sensitiveLength
);
73 memset_s((void *)valueData
, len
, 0, sensitiveLength
); // clear the sensitive data, memset_s is never optimized away
74 len
= sensitiveLength
;
76 dataCopy
= malloc(len
);
77 require(dataCopy
!= NULL
, done
);
78 memcpy(dataCopy
, valueData
, len
);
81 set
->items
[index
].valueLength
= len
;
83 set
->items
[index
].value
= calloc(1u, len
);
84 require(set
->items
[index
].value
!= NULL
, done
);
86 memcpy(set
->items
[index
].value
, dataCopy
, len
);
103 void FreeItemSet(AuthorizationItemSet
* itemSet
)
105 if (!itemSet
) { return; }
107 for(uint32_t i
= 0; i
< itemSet
->count
; i
++ ) {
108 if (itemSet
->items
[i
].name
) {
109 free((void*)itemSet
->items
[i
].name
);
111 if (itemSet
->items
[i
].value
) {
112 free(itemSet
->items
[i
].value
);
115 if (itemSet
->items
) {
116 free(itemSet
->items
);
123 _copy_cf_string(CFTypeRef str
, const char * defaultValue
)
125 char * result
= NULL
;
126 require(str
!= NULL
, done
);
127 require(CFGetTypeID(str
) == CFStringGetTypeID(), done
);
129 CFIndex length
= CFStringGetLength(str
);
130 CFIndex size
= CFStringGetMaximumSizeForEncoding(length
, kCFStringEncodingUTF8
) + 1;
132 result
= (char*)calloc(1u, (size_t)size
);
133 check(result
!= NULL
);
135 if (!CFStringGetCString(str
, result
, size
, kCFStringEncodingUTF8
)) {
140 if (result
== NULL
&& defaultValue
) {
141 size_t len
= strlen(defaultValue
);
142 result
= (char*)calloc(1u, len
);
143 check(result
!= NULL
);
145 strlcpy(result
, defaultValue
, len
);
152 _get_cf_int(CFTypeRef num
, int64_t defaultValue
)
154 int64_t result
= defaultValue
;
155 require(num
!= NULL
, done
);
156 require(CFGetTypeID(num
) == CFNumberGetTypeID(), done
);
158 if (!CFNumberGetValue(num
, kCFNumberSInt64Type
, &result
)) {
159 result
= defaultValue
;
167 _get_cf_bool(CFTypeRef value
, bool defaultValue
)
169 bool result
= defaultValue
;
170 require(value
!= NULL
, done
);
171 require(CFGetTypeID(value
) == CFBooleanGetTypeID(), done
);
173 result
= CFBooleanGetValue(value
);
180 _compare_string(const char * str1
, const char * str2
)
182 if (!(str1
== str2
)) { // compare null or same pointer
183 if (str1
&& str2
) { // check both are non null
184 if (strcasecmp(str1
, str2
) != 0) { // compare strings
185 return false; // return false if not equal
188 return false; // return false if one null
196 _copy_string(const char * str
)
198 char * result
= NULL
;
199 require(str
!= NULL
, done
);
201 size_t len
= strlen(str
) + 1;
202 result
= calloc(1u, len
);
203 require(result
!= NULL
, done
);
205 strlcpy(result
, str
, len
);
212 _copy_data(const void * data
, size_t dataLen
)
214 void * result
= NULL
;
215 require(data
!= NULL
, done
);
217 result
= calloc(1u, dataLen
);
218 require(result
!= NULL
, done
);
220 memcpy(result
, data
, dataLen
);
226 bool _cf_set_iterate(CFSetRef set
, bool(^iterator
)(CFTypeRef value
))
229 CFTypeRef
* values
= NULL
;
231 require(set
!= NULL
, done
);
233 CFIndex count
= CFSetGetCount(set
);
234 values
= calloc((size_t)count
, sizeof(CFTypeRef
));
235 require(values
!= NULL
, done
);
237 CFSetGetValues(set
, values
);
238 for (CFIndex i
= 0; i
< count
; i
++) {
239 result
= iterator(values
[i
]);
250 bool _cf_bag_iterate(CFBagRef bag
, bool(^iterator
)(CFTypeRef value
))
253 CFTypeRef
* values
= NULL
;
255 require(bag
!= NULL
, done
);
257 CFIndex count
= CFBagGetCount(bag
);
258 values
= calloc((size_t)count
, sizeof(CFTypeRef
));
259 require(values
!= NULL
, done
);
261 CFBagGetValues(bag
, values
);
262 for (CFIndex i
= 0; i
< count
; i
++) {
263 result
= iterator(values
[i
]);
274 bool _cf_dictionary_iterate(CFDictionaryRef dict
, bool(^iterator
)(CFTypeRef key
, CFTypeRef value
))
277 CFTypeRef
* keys
= NULL
;
278 CFTypeRef
* values
= NULL
;
280 require(dict
!= NULL
, done
);
282 CFIndex count
= CFDictionaryGetCount(dict
);
283 keys
= calloc((size_t)count
, sizeof(CFTypeRef
));
284 require(keys
!= NULL
, done
);
286 values
= calloc((size_t)count
, sizeof(CFTypeRef
));
287 require(values
!= NULL
, done
);
289 CFDictionaryGetKeysAndValues(dict
, keys
, values
);
290 for (CFIndex i
= 0; i
< count
; i
++) {
291 result
= iterator(keys
[i
], values
[i
]);