summaryrefslogtreecommitdiffstats
path: root/src/lockdown.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lockdown.c')
-rw-r--r--src/lockdown.c891
1 files changed, 432 insertions, 459 deletions
diff --git a/src/lockdown.c b/src/lockdown.c
index 73a8bbc..63f9090 100644
--- a/src/lockdown.c
+++ b/src/lockdown.c
@@ -31,6 +31,9 @@
31#include <libtasn1.h> 31#include <libtasn1.h>
32#include <gnutls/x509.h> 32#include <gnutls/x509.h>
33 33
34#include <plist/plist.h>
35
36
34const ASN1_ARRAY_TYPE pkcs1_asn1_tab[] = { 37const ASN1_ARRAY_TYPE pkcs1_asn1_tab[] = {
35 {"PKCS1", 536872976, 0}, 38 {"PKCS1", 536872976, 0},
36 {0, 1073741836, 0}, 39 {0, 1073741836, 0},
@@ -40,35 +43,6 @@ const ASN1_ARRAY_TYPE pkcs1_asn1_tab[] = {
40 {0, 0, 0} 43 {0, 0, 0}
41}; 44};
42 45
43static int get_rand(int min, int max)
44{
45 int retval = (rand() % (max - min)) + min;
46 return retval;
47}
48
49/** Generates a valid HostID (which is actually a UUID).
50 *
51 * @param A null terminated string containing a valid HostID.
52 */
53char *lockdownd_generate_hostid(void)
54{
55 char *hostid = (char *) malloc(sizeof(char) * 37); // HostID's are just UUID's, and UUID's are 36 characters long
56 const char *chars = "ABCDEF0123456789";
57 srand(time(NULL));
58 int i = 0;
59
60 for (i = 0; i < 36; i++) {
61 if (i == 8 || i == 13 || i == 18 || i == 23) {
62 hostid[i] = '-';
63 continue;
64 } else {
65 hostid[i] = chars[get_rand(0, 16)];
66 }
67 }
68 hostid[36] = '\0'; // make it a real string
69 return hostid;
70}
71
72/** Creates a lockdownd client for the give iPhone. 46/** Creates a lockdownd client for the give iPhone.
73 * 47 *
74 * @param phone The iPhone to create a lockdownd client for 48 * @param phone The iPhone to create a lockdownd client for
@@ -100,57 +74,56 @@ iphone_lckd_client_t new_lockdownd_client(iphone_device_t phone)
100static void iphone_lckd_stop_session(iphone_lckd_client_t control) 74static void iphone_lckd_stop_session(iphone_lckd_client_t control)
101{ 75{
102 if (!control) 76 if (!control)
103 return; // IPHONE_E_INVALID_ARG; 77 return; //IPHONE_E_INVALID_ARG;
104 xmlDocPtr plist = new_plist(); 78
105 xmlNode *dict, *key;
106 char **dictionary;
107 int bytes = 0, i = 0;
108 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 79 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
109 80
110 log_debug_msg("lockdownd_stop_session() called\n"); 81 plist_t dict = plist_new_dict();
111 dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 82 plist_add_sub_key_el(dict, "Request");
112 key = add_key_str_dict_element(plist, dict, "Request", "StopSession", 1); 83 plist_add_sub_string_el(dict, "StopSession");
113 key = add_key_str_dict_element(plist, dict, "SessionID", control->session_id, 1); 84 plist_add_sub_key_el(dict, "SessionID");
85 plist_add_sub_string_el(dict, control->session_id);
114 86
115 char *XML_content; 87 log_dbg_msg(DBGMASK_LOCKDOWND, "iphone_lckd_stop_session() called\n");
116 uint32 length;
117 88
118 xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); 89 ret = iphone_lckd_send(control, dict);
119 ret = iphone_lckd_send(control, XML_content, length, &bytes);
120 90
121 xmlFree(XML_content); 91 plist_free(dict);
122 xmlFreeDoc(plist); 92 dict = NULL;
123 plist = NULL; 93
124 ret = iphone_lckd_recv(control, &XML_content, &bytes); 94 ret = iphone_lckd_recv(control, &dict);
125 95
126 plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0);
127 if (!plist) {
128 log_debug_msg("lockdownd_stop_session(): IPHONE_E_PLIST_ERROR\n");
129 return; //IPHONE_E_PLIST_ERROR;
130 }
131 dict = xmlDocGetRootElement(plist);
132 for (dict = dict->children; dict; dict = dict->next) {
133 if (!xmlStrcmp(dict->name, "dict"))
134 break;
135 }
136 if (!dict) { 96 if (!dict) {
137 log_debug_msg("lockdownd_stop_session(): IPHONE_E_DICT_ERROR\n"); 97 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_stop_session(): IPHONE_E_PLIST_ERROR\n");
138 return; //IPHONE_E_DICT_ERROR; 98 return; // IPHONE_E_PLIST_ERROR;
139 } 99 }
140 dictionary = read_dict_element_strings(dict);
141 xmlFreeDoc(plist);
142 free(XML_content);
143 100
144 for (i = 0; dictionary[i]; i += 2) { 101 plist_t query_node = plist_find_node_by_string(dict, "StopSession");
145 if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { 102 plist_t result_node = plist_get_next_sibling(query_node);
146 log_debug_msg("lockdownd_stop_session(): success\n"); 103 plist_t value_node = plist_get_next_sibling(result_node);
104
105 plist_type result_type = plist_get_node_type(result_node);
106 plist_type value_type = plist_get_node_type(value_node);
107
108 if (result_type == PLIST_KEY && value_type == PLIST_STRING) {
109
110 char *result_value = NULL;
111 char *value_value = NULL;
112
113 plist_get_key_val(result_node, &result_value);
114 plist_get_string_val(value_node, &value_value);
115
116 if (!strcmp(result_value, "Result") && !strcmp(value_value, "Success")) {
117 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_stop_session(): success\n");
147 ret = IPHONE_E_SUCCESS; 118 ret = IPHONE_E_SUCCESS;
148 break;
149 } 119 }
120 free(result_value);
121 free(value_value);
150 } 122 }
123 plist_free(dict);
124 dict = NULL;
151 125
152 free_dictionary(dictionary); 126 return; // ret;
153 return; //ret;
154} 127}
155 128
156/** 129/**
@@ -163,14 +136,14 @@ static void iphone_lckd_stop_session(iphone_lckd_client_t control)
163static void iphone_lckd_stop_SSL_session(iphone_lckd_client_t client) 136static void iphone_lckd_stop_SSL_session(iphone_lckd_client_t client)
164{ 137{
165 if (!client) { 138 if (!client) {
166 log_debug_msg("lockdownd_stop_SSL_session(): invalid argument!\n"); 139 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_stop_SSL_session(): invalid argument!\n");
167 return; 140 return;
168 } 141 }
169 142
170 if (client->in_SSL) { 143 if (client->in_SSL) {
171 log_debug_msg("Stopping SSL Session\n"); 144 log_dbg_msg(DBGMASK_LOCKDOWND, "Stopping SSL Session\n");
172 iphone_lckd_stop_session(client); 145 iphone_lckd_stop_session(client);
173 log_debug_msg("Sending SSL close notify\n"); 146 log_dbg_msg(DBGMASK_LOCKDOWND, "Sending SSL close notify\n");
174 gnutls_bye(*client->ssl_session, GNUTLS_SHUT_RDWR); 147 gnutls_bye(*client->ssl_session, GNUTLS_SHUT_RDWR);
175 } 148 }
176 if (client->ssl_session) { 149 if (client->ssl_session) {
@@ -215,13 +188,13 @@ iphone_error_t iphone_lckd_free_client(iphone_lckd_client_t client)
215 * 188 *
216 * @return The number of bytes received 189 * @return The number of bytes received
217 */ 190 */
218iphone_error_t iphone_lckd_recv(iphone_lckd_client_t client, char **dump_data, uint32_t * recv_bytes) 191iphone_error_t iphone_lckd_recv(iphone_lckd_client_t client, plist_t * plist)
219{ 192{
220 if (!client || !dump_data || !recv_bytes) 193 if (!client || !plist || (plist && *plist))
221 return IPHONE_E_INVALID_ARG; 194 return IPHONE_E_INVALID_ARG;
222 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 195 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
223 char *receive; 196 char *receive;
224 uint32 datalen = 0, bytes = 0; 197 uint32_t datalen = 0, bytes = 0;
225 198
226 if (!client->in_SSL) 199 if (!client->in_SSL)
227 ret = iphone_mux_recv(client->connection, (char *) &datalen, sizeof(datalen), &bytes); 200 ret = iphone_mux_recv(client->connection, (char *) &datalen, sizeof(datalen), &bytes);
@@ -240,8 +213,18 @@ iphone_error_t iphone_lckd_recv(iphone_lckd_client_t client, char **dump_data, u
240 if (bytes > 0) 213 if (bytes > 0)
241 ret = IPHONE_E_SUCCESS; 214 ret = IPHONE_E_SUCCESS;
242 } 215 }
243 *dump_data = receive; 216
244 *recv_bytes = bytes; 217 if (bytes <= 0) {
218 free(receive);
219 return IPHONE_E_NOT_ENOUGH_DATA;
220 }
221
222 plist_from_xml(receive, bytes, plist);
223 free(receive);
224
225 if (!*plist)
226 ret = IPHONE_E_PLIST_ERROR;
227
245 return ret; 228 return ret;
246} 229}
247 230
@@ -250,26 +233,31 @@ iphone_error_t iphone_lckd_recv(iphone_lckd_client_t client, char **dump_data, u
250 * @note This function is low-level and should only be used if you need to send 233 * @note This function is low-level and should only be used if you need to send
251 * a new type of message. 234 * a new type of message.
252 * 235 *
253 * @param control The lockdownd client 236 * @param client The lockdownd client
254 * @param raw_data The null terminated string buffer to send 237 * @param plist The plist to send
255 * @param length The length of data to send
256 * 238 *
257 * @return The number of bytes sent 239 * @return an error code (IPHONE_E_SUCCESS on success)
258 */ 240 */
259iphone_error_t iphone_lckd_send(iphone_lckd_client_t client, char *raw_data, uint32_t length, uint32_t * sent_bytes) 241iphone_error_t iphone_lckd_send(iphone_lckd_client_t client, plist_t plist)
260{ 242{
261 if (!client || !raw_data || length == 0 || !sent_bytes) 243 if (!client || !plist)
262 return IPHONE_E_INVALID_ARG; 244 return IPHONE_E_INVALID_ARG;
263 char *real_query; 245 char *real_query;
264 int bytes; 246 int bytes;
247 char *XMLContent = NULL;
248 uint32_t length = 0;
265 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 249 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
266 250
251 plist_to_xml(plist, &XMLContent, &length);
252 log_dbg_msg(DBGMASK_LOCKDOWND, "Send msg :\nsize : %i\nbuffer :\n%s\n", length, XMLContent);
253
254
267 real_query = (char *) malloc(sizeof(char) * (length + 4)); 255 real_query = (char *) malloc(sizeof(char) * (length + 4));
268 length = htonl(length); 256 length = htonl(length);
269 memcpy(real_query, &length, sizeof(length)); 257 memcpy(real_query, &length, sizeof(length));
270 memcpy(real_query + 4, raw_data, ntohl(length)); 258 memcpy(real_query + 4, XMLContent, ntohl(length));
271 log_debug_msg("lockdownd_send(): made the query, sending it along\n"); 259 free(XMLContent);
272 dump_debug_buffer("grpkt", real_query, ntohl(length) + 4); 260 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_send(): made the query, sending it along\n");
273 261
274 if (!client->in_SSL) 262 if (!client->in_SSL)
275 ret = iphone_mux_send(client->connection, real_query, ntohl(length) + sizeof(length), &bytes); 263 ret = iphone_mux_send(client->connection, real_query, ntohl(length) + sizeof(length), &bytes);
@@ -277,9 +265,9 @@ iphone_error_t iphone_lckd_send(iphone_lckd_client_t client, char *raw_data, uin
277 gnutls_record_send(*client->ssl_session, real_query, ntohl(length) + sizeof(length)); 265 gnutls_record_send(*client->ssl_session, real_query, ntohl(length) + sizeof(length));
278 ret = IPHONE_E_SUCCESS; 266 ret = IPHONE_E_SUCCESS;
279 } 267 }
280 log_debug_msg("lockdownd_send(): sent it!\n"); 268 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_send(): sent it!\n");
281 free(real_query); 269 free(real_query);
282 *sent_bytes = bytes; 270
283 return ret; 271 return ret;
284} 272}
285 273
@@ -295,49 +283,50 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control)
295{ 283{
296 if (!control) 284 if (!control)
297 return IPHONE_E_INVALID_ARG; 285 return IPHONE_E_INVALID_ARG;
298 xmlDocPtr plist = new_plist(); 286
299 xmlNode *dict, *key;
300 char **dictionary;
301 int bytes = 0, i = 0;
302 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 287 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
303 288
304 log_debug_msg("lockdownd_hello() called\n"); 289 plist_t dict = plist_new_dict();
305 dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 290 plist_add_sub_key_el(dict, "Request");
306 key = add_key_str_dict_element(plist, dict, "Request", "QueryType", 1); 291 plist_add_sub_string_el(dict, "QueryType");
307 char *XML_content;
308 uint32 length;
309 292
310 xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); 293 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_hello() called\n");
311 ret = iphone_lckd_send(control, XML_content, length, &bytes); 294 ret = iphone_lckd_send(control, dict);
312 295
313 xmlFree(XML_content); 296 plist_free(dict);
314 xmlFreeDoc(plist); 297 dict = NULL;
315 plist = NULL;
316 ret = iphone_lckd_recv(control, &XML_content, &bytes);
317 298
318 plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0); 299 ret = iphone_lckd_recv(control, &dict);
319 if (!plist) 300
320 return IPHONE_E_PLIST_ERROR; 301 if (IPHONE_E_SUCCESS != ret)
321 dict = xmlDocGetRootElement(plist); 302 return ret;
322 for (dict = dict->children; dict; dict = dict->next) { 303
323 if (!xmlStrcmp(dict->name, "dict")) 304 plist_t query_node = plist_find_node_by_string(dict, "QueryType");
324 break; 305 plist_t result_node = plist_get_next_sibling(query_node);
325 } 306 plist_t value_node = plist_get_next_sibling(result_node);
326 if (!dict) 307
327 return IPHONE_E_DICT_ERROR; 308 plist_type result_type = plist_get_node_type(result_node);
328 dictionary = read_dict_element_strings(dict); 309 plist_type value_type = plist_get_node_type(value_node);
329 xmlFreeDoc(plist); 310
330 free(XML_content); 311 if (result_type == PLIST_KEY && value_type == PLIST_STRING) {
331 312
332 for (i = 0; dictionary[i]; i += 2) { 313 char *result_value = NULL;
333 if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { 314 char *value_value = NULL;
334 log_debug_msg("lockdownd_hello(): success\n"); 315
316 plist_get_key_val(result_node, &result_value);
317 plist_get_string_val(value_node, &value_value);
318
319 if (!strcmp(result_value, "Result") && !strcmp(value_value, "Success")) {
320 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_hello(): success\n");
335 ret = IPHONE_E_SUCCESS; 321 ret = IPHONE_E_SUCCESS;
336 break;
337 } 322 }
323 free(result_value);
324 free(value_value);
338 } 325 }
339 326
340 free_dictionary(dictionary); 327 plist_free(dict);
328 dict = NULL;
329
341 return ret; 330 return ret;
342} 331}
343 332
@@ -349,74 +338,91 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control)
349 * 338 *
350 * @return IPHONE_E_SUCCESS on success. 339 * @return IPHONE_E_SUCCESS on success.
351 */ 340 */
352iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, const char *req_key, const char *req_string, 341iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, const char *req_key, char *req_string,
353 char **value) 342 gnutls_datum_t * value)
354{ 343{
355 if (!control || !req_key || !value || (value && *value)) 344 if (!control || !req_key || !value || value->data)
356 return IPHONE_E_INVALID_ARG; 345 return IPHONE_E_INVALID_ARG;
357 xmlDocPtr plist = new_plist(); 346
358 xmlNode *dict = NULL; 347 plist_t dict = NULL;
359 xmlNode *key = NULL;;
360 char **dictionary = NULL;
361 int bytes = 0, i = 0;
362 char *XML_content = NULL;
363 uint32 length = 0;
364 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 348 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
365 349
366 /* Setup DevicePublicKey request plist */ 350 /* Setup DevicePublicKey request plist */
367 dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 351 dict = plist_new_dict();
368 key = add_key_str_dict_element(plist, dict, req_key, req_string, 1); 352 plist_add_sub_key_el(dict, req_key);
369 key = add_key_str_dict_element(plist, dict, "Request", "GetValue", 1); 353 plist_add_sub_string_el(dict, req_string);
370 xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); 354 plist_add_sub_key_el(dict, "Request");
355 plist_add_sub_string_el(dict, "GetValue");
371 356
372 /* send to iPhone */ 357 /* send to iPhone */
373 ret = iphone_lckd_send(control, XML_content, length, &bytes); 358 ret = iphone_lckd_send(control, dict);
374 359
375 xmlFree(XML_content); 360 plist_free(dict);
376 xmlFreeDoc(plist); 361 dict = NULL;
377 plist = NULL;
378 362
379 if (ret != IPHONE_E_SUCCESS) 363 if (ret != IPHONE_E_SUCCESS)
380 return ret; 364 return ret;
381 365
382 /* Now get iPhone's answer */ 366 /* Now get iPhone's answer */
383 ret = iphone_lckd_recv(control, &XML_content, &bytes); 367 ret = iphone_lckd_recv(control, &dict);
384 368
385 if (ret != IPHONE_E_SUCCESS) 369 if (ret != IPHONE_E_SUCCESS)
386 return ret; 370 return ret;
387 371
388 plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0); 372 plist_t query_node = plist_find_node_by_string(dict, "GetValue");
389 if (!plist) 373 plist_t result_key_node = plist_get_next_sibling(query_node);
390 return IPHONE_E_PLIST_ERROR; 374 plist_t result_value_node = plist_get_next_sibling(result_key_node);
391 dict = xmlDocGetRootElement(plist);
392 for (dict = dict->children; dict; dict = dict->next) {
393 if (!xmlStrcmp(dict->name, "dict"))
394 break;
395 }
396 if (!dict)
397 return IPHONE_E_DICT_ERROR;
398 375
399 /* Parse xml to check success and to find public key */ 376 plist_type result_key_type = plist_get_node_type(result_key_node);
400 dictionary = read_dict_element_strings(dict); 377 plist_type result_value_type = plist_get_node_type(result_value_node);
401 xmlFreeDoc(plist);
402 free(XML_content);
403 378
404 int success = 0; 379 if (result_key_type == PLIST_KEY && result_value_type == PLIST_STRING) {
405 for (i = 0; dictionary[i]; i += 2) { 380
406 if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { 381 char *result_key = NULL;
407 success = 1; 382 char *result_value = NULL;
408 } 383 ret = IPHONE_E_DICT_ERROR;
409 if (!strcmp(dictionary[i], "Value")) { 384
410 *value = strdup(dictionary[i + 1]); 385 plist_get_key_val(result_key_node, &result_key);
386 plist_get_string_val(result_value_node, &result_value);
387
388 if (!strcmp(result_key, "Result") && !strcmp(result_value, "Success")) {
389 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_generic_get_value(): success\n");
390 ret = IPHONE_E_SUCCESS;
411 } 391 }
392 free(result_key);
393 free(result_value);
394 }
395 if (ret != IPHONE_E_SUCCESS) {
396 return ret;
412 } 397 }
413 398
414 if (dictionary) { 399 plist_t value_key_node = plist_get_next_sibling(result_key_node);
415 free_dictionary(dictionary); 400 plist_t value_value_node = plist_get_next_sibling(value_key_node);
416 dictionary = NULL; 401
402 plist_type value_key_type = plist_get_node_type(value_key_node);
403
404 if (value_key_type == PLIST_KEY) {
405
406 char *result_key = NULL;
407 plist_get_key_val(value_key_node, &result_key);
408
409 if (!strcmp(result_key, "Value")) {
410 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_generic_get_value(): success\n");
411
412 plist_type value_value_type = plist_get_node_type(value_value_node);
413 if (PLIST_STRING == value_value_type) {
414 char *value_value = NULL;
415 plist_get_string_val(value_value_node, &value_value);
416
417 value->data = value_value;
418 value->size = strlen(value_value);
419 ret = IPHONE_E_SUCCESS;
420 }
421 }
422 free(result_key);
417 } 423 }
418 if (success) 424
419 ret = IPHONE_E_SUCCESS; 425 plist_free(dict);
420 return ret; 426 return ret;
421} 427}
422 428
@@ -428,7 +434,9 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, const c
428 */ 434 */
429iphone_error_t lockdownd_get_device_uid(iphone_lckd_client_t control, char **uid) 435iphone_error_t lockdownd_get_device_uid(iphone_lckd_client_t control, char **uid)
430{ 436{
431 return lockdownd_generic_get_value(control, "Key", "UniqueDeviceID", uid); 437 gnutls_datum_t temp = { NULL, 0 };
438 return lockdownd_generic_get_value(control, "Key", "UniqueDeviceID", &temp);
439 *uid = temp.data;
432} 440}
433 441
434/** Askes for the device's public key. Part of the lockdownd handshake. 442/** Askes for the device's public key. Part of the lockdownd handshake.
@@ -437,7 +445,7 @@ iphone_error_t lockdownd_get_device_uid(iphone_lckd_client_t control, char **uid
437 * 445 *
438 * @return 1 on success and 0 on failure. 446 * @return 1 on success and 0 on failure.
439 */ 447 */
440iphone_error_t lockdownd_get_device_public_key(iphone_lckd_client_t control, char **public_key) 448iphone_error_t lockdownd_get_device_public_key(iphone_lckd_client_t control, gnutls_datum_t * public_key)
441{ 449{
442 return lockdownd_generic_get_value(control, "Key", "DevicePublicKey", public_key); 450 return lockdownd_generic_get_value(control, "Key", "DevicePublicKey", public_key);
443} 451}
@@ -513,107 +521,91 @@ iphone_error_t iphone_lckd_new_client(iphone_device_t device, iphone_lckd_client
513iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, char *host_id) 521iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, char *host_id)
514{ 522{
515 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 523 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
516 xmlDocPtr plist = new_plist(); 524 plist_t dict = NULL;
517 xmlNode *dict = NULL; 525 plist_t dict_record = NULL;
518 xmlNode *dictRecord = NULL; 526
519 char **dictionary = NULL; 527 gnutls_datum_t device_cert = { NULL, 0 };
520 int bytes = 0, i = 0; 528 gnutls_datum_t host_cert = { NULL, 0 };
521 char *XML_content = NULL; 529 gnutls_datum_t root_cert = { NULL, 0 };
522 uint32 length = 0; 530 gnutls_datum_t public_key = { NULL, 0 };
523 531
524 char *device_cert_b64 = NULL; 532 ret = lockdownd_get_device_public_key(control, &public_key);
525 char *host_cert_b64 = NULL;
526 char *root_cert_b64 = NULL;
527 char *public_key_b64 = NULL;
528
529 ret = lockdownd_get_device_public_key(control, &public_key_b64);
530 if (ret != IPHONE_E_SUCCESS) { 533 if (ret != IPHONE_E_SUCCESS) {
531 log_debug_msg("Device refused to send public key.\n"); 534 log_debug_msg("Device refused to send public key.\n");
532 return ret; 535 return ret;
533 } 536 }
534 537
535 ret = lockdownd_gen_pair_cert(public_key_b64, &device_cert_b64, &host_cert_b64, &root_cert_b64); 538 ret = lockdownd_gen_pair_cert(public_key, &device_cert, &host_cert, &root_cert);
536 if (ret != IPHONE_E_SUCCESS) { 539 if (ret != IPHONE_E_SUCCESS) {
537 free(public_key_b64); 540 free(public_key.data);
538 return ret; 541 return ret;
539 } 542 }
540 543
541 /* Setup Pair request plist */ 544 /* Setup Pair request plist */
542 dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 545 dict = plist_new_dict();
543 dictRecord = add_key_dict_node(plist, dict, "PairRecord", "\n", 1); 546 plist_add_sub_key_el(dict, "PairRecord");
544 //dictRecord = add_child_to_plist(plist, "dict", "\n", NULL, 1); 547 dict_record = plist_new_dict();
545 add_key_data_dict_element(plist, dictRecord, "DeviceCertificate", device_cert_b64, 2); 548 plist_add_sub_node(dict, dict_record);
546 add_key_data_dict_element(plist, dictRecord, "HostCertificate", host_cert_b64, 2); 549 plist_add_sub_key_el(dict_record, "DeviceCertificate");
547 add_key_str_dict_element(plist, dictRecord, "HostID", host_id, 2); 550 plist_add_sub_data_el(dict_record, device_cert.data, device_cert.size);
548 add_key_data_dict_element(plist, dictRecord, "RootCertificate", root_cert_b64, 2); 551 plist_add_sub_key_el(dict_record, "HostCertificate");
549 add_key_str_dict_element(plist, dict, "Request", "Pair", 1); 552 plist_add_sub_data_el(dict_record, host_cert.data, host_cert.size);
550 553 plist_add_sub_key_el(dict_record, "HostID");
551 xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); 554 plist_add_sub_string_el(dict_record, host_id);
552 555 plist_add_sub_key_el(dict_record, "RootCertificate");
553 printf("XML Pairing request : %s\n", XML_content); 556 plist_add_sub_data_el(dict_record, root_cert.data, root_cert.size);
557 plist_add_sub_key_el(dict_record, "Request");
558 plist_add_sub_string_el(dict_record, "Pair");
554 559
555 /* send to iPhone */ 560 /* send to iPhone */
556 ret = iphone_lckd_send(control, XML_content, length, &bytes); 561 ret = iphone_lckd_send(control, dict);
557 562 plist_free(dict);
558 xmlFree(XML_content); 563 dict = NULL;
559 xmlFreeDoc(plist);
560 plist = NULL;
561 564
562 if (ret != IPHONE_E_SUCCESS) 565 if (ret != IPHONE_E_SUCCESS)
563 return ret; 566 return ret;
564 567
565 /* Now get iPhone's answer */ 568 /* Now get iPhone's answer */
566 ret = iphone_lckd_recv(control, &XML_content, &bytes); 569 ret = iphone_lckd_recv(control, &dict);
567 570
568 if (ret != IPHONE_E_SUCCESS) 571 if (ret != IPHONE_E_SUCCESS)
569 return ret; 572 return ret;
570 573
571 log_debug_msg("lockdown_pair_device: iPhone's response to our pair request:\n"); 574 plist_t query_node = plist_find_node_by_string(dict, "Pair");
572 log_debug_msg(XML_content); 575 plist_t result_key_node = plist_get_next_sibling(query_node);
573 log_debug_msg("\n\n"); 576 plist_t result_value_node = plist_get_next_sibling(result_key_node);
574 577
575 plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0); 578 plist_type result_key_type = plist_get_node_type(result_key_node);
576 if (!plist) { 579 plist_type result_value_type = plist_get_node_type(result_value_node);
577 free(public_key_b64); 580
578 return IPHONE_E_PLIST_ERROR; 581 if (result_key_type == PLIST_KEY && result_value_type == PLIST_STRING) {
579 } 582
580 dict = xmlDocGetRootElement(plist); 583 char *result_key = NULL;
581 for (dict = dict->children; dict; dict = dict->next) { 584 char *result_value = NULL;
582 if (!xmlStrcmp(dict->name, "dict"))
583 break;
584 }
585 if (!dict) {
586 free(public_key_b64);
587 return IPHONE_E_DICT_ERROR;
588 }
589 585
590 /* Parse xml to check success and to find public key */ 586 plist_get_key_val(result_key_node, &result_key);
591 dictionary = read_dict_element_strings(dict); 587 plist_get_string_val(result_value_node, &result_value);
592 xmlFreeDoc(plist);
593 free(XML_content);
594 588
595 int success = 0; 589 if (!strcmp(result_key, "Result") && !strcmp(result_value, "Success")) {
596 for (i = 0; dictionary[i]; i += 2) { 590 ret = IPHONE_E_SUCCESS;
597 if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) {
598 success = 1;
599 } 591 }
600 }
601 592
602 if (dictionary) { 593 free(result_key);
603 free_dictionary(dictionary); 594 free(result_value);
604 dictionary = NULL;
605 } 595 }
596 plist_free(dict);
597 dict = NULL;
606 598
607 /* store public key in config if pairing succeeded */ 599 /* store public key in config if pairing succeeded */
608 if (success) { 600 if (ret == IPHONE_E_SUCCESS) {
609 log_debug_msg("lockdownd_pair_device: pair success\n"); 601 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_pair_device: pair success\n");
610 store_device_public_key(uid, public_key_b64); 602 store_device_public_key(uid, public_key);
611 ret = IPHONE_E_SUCCESS; 603 ret = IPHONE_E_SUCCESS;
612 } else { 604 } else {
613 log_debug_msg("lockdownd_pair_device: pair failure\n"); 605 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_pair_device: pair failure\n");
614 ret = IPHONE_E_PAIRING_FAILED; 606 ret = IPHONE_E_PAIRING_FAILED;
615 } 607 }
616 free(public_key_b64); 608 free(public_key.data);
617 return ret; 609 return ret;
618} 610}
619 611
@@ -626,55 +618,51 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch
626void lockdownd_close(iphone_lckd_client_t control) 618void lockdownd_close(iphone_lckd_client_t control)
627{ 619{
628 if (!control) 620 if (!control)
629 return; // IPHONE_E_INVALID_ARG; 621 return; //IPHONE_E_INVALID_ARG;
630 xmlDocPtr plist = new_plist(); 622
631 xmlNode *dict, *key;
632 char **dictionary;
633 int bytes = 0, i = 0;
634 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 623 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
635 624
636 log_debug_msg("lockdownd_close() called\n"); 625 plist_t dict = plist_new_dict();
637 dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 626 plist_add_sub_key_el(dict, "Request");
638 key = add_key_str_dict_element(plist, dict, "Request", "Goodbye", 1); 627 plist_add_sub_string_el(dict, "Goodbye");
639 char *XML_content;
640 uint32 length;
641 628
642 xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); 629 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_close() called\n");
643 ret = iphone_lckd_send(control, XML_content, length, &bytes);
644 630
645 xmlFree(XML_content); 631 ret = iphone_lckd_send(control, dict);
646 xmlFreeDoc(plist); 632 plist_free(dict);
647 plist = NULL; 633 dict = NULL;
648 ret = iphone_lckd_recv(control, &XML_content, &bytes); 634
635 ret = iphone_lckd_recv(control, &dict);
649 636
650 plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0);
651 if (!plist) {
652 log_debug_msg("lockdownd_close(): IPHONE_E_PLIST_ERROR\n");
653 return; //IPHONE_E_PLIST_ERROR;
654 }
655 dict = xmlDocGetRootElement(plist);
656 for (dict = dict->children; dict; dict = dict->next) {
657 if (!xmlStrcmp(dict->name, "dict"))
658 break;
659 }
660 if (!dict) { 637 if (!dict) {
661 log_debug_msg("lockdownd_close(): IPHONE_E_DICT_ERROR\n"); 638 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_close(): IPHONE_E_PLIST_ERROR\n");
662 return; //IPHONE_E_DICT_ERROR; 639 return; // IPHONE_E_PLIST_ERROR;
663 } 640 }
664 dictionary = read_dict_element_strings(dict);
665 xmlFreeDoc(plist);
666 free(XML_content);
667 641
668 for (i = 0; dictionary[i]; i += 2) { 642 plist_t query_node = plist_find_node_by_string(dict, "Goodbye");
669 if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { 643 plist_t result_node = plist_get_next_sibling(query_node);
670 log_debug_msg("lockdownd_close(): success\n"); 644 plist_t value_node = plist_get_next_sibling(result_node);
645
646 plist_type result_type = plist_get_node_type(result_node);
647 plist_type value_type = plist_get_node_type(value_node);
648
649 if (result_type == PLIST_KEY && value_type == PLIST_STRING) {
650 char *result_value = NULL;
651 char *value_value = NULL;
652
653 plist_get_key_val(result_node, &result_value);
654 plist_get_string_val(value_node, &value_value);
655
656 if (!strcmp(result_value, "Result") && !strcmp(value_value, "Success")) {
657 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_close(): success\n");
671 ret = IPHONE_E_SUCCESS; 658 ret = IPHONE_E_SUCCESS;
672 break;
673 } 659 }
660 free(result_value);
661 free(value_value);
674 } 662 }
675 663 plist_free(dict);
676 free_dictionary(dictionary); 664 dict = NULL;
677 return; //ret; 665 return; // ret;
678} 666}
679 667
680/** Generates the device certificate from the public key as well as the host 668/** Generates the device certificate from the public key as well as the host
@@ -682,25 +670,19 @@ void lockdownd_close(iphone_lckd_client_t control)
682 * 670 *
683 * @return IPHONE_E_SUCCESS on success. 671 * @return IPHONE_E_SUCCESS on success.
684 */ 672 */
685iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_b64, char **host_cert_b64, 673iphone_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t * odevice_cert,
686 char **root_cert_b64) 674 gnutls_datum_t * ohost_cert, gnutls_datum_t * oroot_cert)
687{ 675{
688 if (!public_key_b64 || !device_cert_b64 || !host_cert_b64 || !root_cert_b64) 676 if (!public_key.data || !odevice_cert || !ohost_cert || !oroot_cert)
689 return IPHONE_E_INVALID_ARG; 677 return IPHONE_E_INVALID_ARG;
690 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 678 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
691 679
692 gnutls_datum_t modulus = { NULL, 0 }; 680 gnutls_datum_t modulus = { NULL, 0 };
693 gnutls_datum_t exponent = { NULL, 0 }; 681 gnutls_datum_t exponent = { NULL, 0 };
694 682
695 /* first decode base64 public_key */
696 gnutls_datum_t pem_pub_key;
697 gsize decoded_size;
698 pem_pub_key.data = g_base64_decode(public_key_b64, &decoded_size);
699 pem_pub_key.size = decoded_size;
700
701 /* now decode the PEM encoded key */ 683 /* now decode the PEM encoded key */
702 gnutls_datum_t der_pub_key; 684 gnutls_datum_t der_pub_key;
703 if (GNUTLS_E_SUCCESS == gnutls_pem_base64_decode_alloc("RSA PUBLIC KEY", &pem_pub_key, &der_pub_key)) { 685 if (GNUTLS_E_SUCCESS == gnutls_pem_base64_decode_alloc("RSA PUBLIC KEY", &public_key, &der_pub_key)) {
704 686
705 /* initalize asn.1 parser */ 687 /* initalize asn.1 parser */
706 ASN1_TYPE pkcs1 = ASN1_TYPE_EMPTY; 688 ASN1_TYPE pkcs1 = ASN1_TYPE_EMPTY;
@@ -784,10 +766,18 @@ iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_
784 dev_pem.data = gnutls_malloc(dev_pem.size); 766 dev_pem.data = gnutls_malloc(dev_pem.size);
785 gnutls_x509_crt_export(dev_cert, GNUTLS_X509_FMT_PEM, dev_pem.data, &dev_pem.size); 767 gnutls_x509_crt_export(dev_cert, GNUTLS_X509_FMT_PEM, dev_pem.data, &dev_pem.size);
786 768
787 /* now encode certificates for output */ 769 /* copy buffer for output */
788 *device_cert_b64 = g_base64_encode(dev_pem.data, dev_pem.size); 770 odevice_cert->data = malloc(dev_pem.size);
789 *host_cert_b64 = g_base64_encode(pem_host_cert.data, pem_host_cert.size); 771 memcpy(odevice_cert->data, dev_pem.data, dev_pem.size);
790 *root_cert_b64 = g_base64_encode(pem_root_cert.data, pem_root_cert.size); 772 odevice_cert->size = dev_pem.size;
773
774 ohost_cert->data = malloc(pem_host_cert.size);
775 memcpy(ohost_cert->data, pem_host_cert.data, pem_host_cert.size);
776 ohost_cert->size = pem_host_cert.size;
777
778 oroot_cert->data = malloc(pem_root_cert.size);
779 memcpy(oroot_cert->data, pem_root_cert.data, pem_root_cert.size);
780 oroot_cert->size = pem_root_cert.size;
791 } 781 }
792 gnutls_free(pem_root_priv.data); 782 gnutls_free(pem_root_priv.data);
793 gnutls_free(pem_root_cert.data); 783 gnutls_free(pem_root_cert.data);
@@ -799,7 +789,6 @@ iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_
799 gnutls_free(exponent.data); 789 gnutls_free(exponent.data);
800 790
801 gnutls_free(der_pub_key.data); 791 gnutls_free(der_pub_key.data);
802 g_free(pem_pub_key.data);
803 792
804 return ret; 793 return ret;
805} 794}
@@ -813,129 +802,127 @@ iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_
813 */ 802 */
814iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const char *HostID) 803iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const char *HostID)
815{ 804{
816 xmlDocPtr plist = new_plist(); 805 plist_t dict = NULL;
817 xmlNode *dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 806 uint32_t return_me = 0;
818 xmlNode *key;
819 char *what2send = NULL, **dictionary = NULL;
820 uint32 len = 0, bytes = 0, return_me = 0, i = 0;
821 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
822 // end variables
823 807
808 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
824 control->session_id[0] = '\0'; 809 control->session_id[0] = '\0';
825 810
826 key = add_key_str_dict_element(plist, dict, "HostID", HostID, 1); 811 /* Setup DevicePublicKey request plist */
827 if (!key) { 812 dict = plist_new_dict();
828 log_debug_msg("Couldn't add a key.\n"); 813 plist_add_sub_key_el(dict, "HostID");
829 xmlFreeDoc(plist); 814 plist_add_sub_string_el(dict, HostID);
830 return IPHONE_E_DICT_ERROR; 815 plist_add_sub_key_el(dict, "Request");
831 } 816 plist_add_sub_string_el(dict, "StartSession");
832 key = add_key_str_dict_element(plist, dict, "Request", "StartSession", 1);
833 if (!key) {
834 log_debug_msg("Couldn't add a key.\n");
835 xmlFreeDoc(plist);
836 return IPHONE_E_DICT_ERROR;
837 }
838
839 xmlDocDumpMemory(plist, (xmlChar **) & what2send, &len);
840 ret = iphone_lckd_send(control, what2send, len, &bytes);
841 817
842 xmlFree(what2send); 818 ret = iphone_lckd_send(control, dict);
843 xmlFreeDoc(plist); 819 plist_free(dict);
820 dict = NULL;
844 821
845 if (ret != IPHONE_E_SUCCESS) 822 if (ret != IPHONE_E_SUCCESS)
846 return ret; 823 return ret;
847 824
848 if (bytes > 0) { 825 ret = iphone_lckd_recv(control, &dict);
849 ret = iphone_lckd_recv(control, &what2send, &len); 826
850 plist = xmlReadMemory(what2send, len, NULL, NULL, 0); 827 if (!dict)
851 dict = xmlDocGetRootElement(plist); 828 return IPHONE_E_PLIST_ERROR;
852 if (!dict) 829
853 return IPHONE_E_DICT_ERROR; 830 plist_t query_node = plist_find_node_by_string(dict, "StartSession");
854 for (dict = dict->children; dict; dict = dict->next) { 831 plist_t result_key_node = plist_get_next_sibling(query_node);
855 if (!xmlStrcmp(dict->name, "dict")) 832 plist_t result_value_node = plist_get_next_sibling(result_key_node);
856 break; 833
857 } 834 plist_type result_key_type = plist_get_node_type(result_key_node);
858 dictionary = read_dict_element_strings(dict); 835 plist_type result_value_type = plist_get_node_type(result_value_node);
859 xmlFreeDoc(plist); 836
860 free(what2send); 837 if (result_key_type == PLIST_KEY && result_value_type == PLIST_STRING) {
838 char *result_key = NULL;
839 char *result_value = NULL;
840
841 plist_get_key_val(result_key_node, &result_key);
842 plist_get_string_val(result_value_node, &result_value);
843
861 ret = IPHONE_E_SSL_ERROR; 844 ret = IPHONE_E_SSL_ERROR;
862 for (i = 0; dictionary[i]; i += 2) { 845 if (!strcmp(result_key, "Result") && !strcmp(result_value, "Success")) {
863 if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { 846 // Set up GnuTLS...
864 // Set up GnuTLS... 847 //gnutls_anon_client_credentials_t anoncred;
865 //gnutls_anon_client_credentials_t anoncred; 848 gnutls_certificate_credentials_t xcred;
866 gnutls_certificate_credentials_t xcred; 849
867 850 log_dbg_msg(DBGMASK_LOCKDOWND, "We started the session OK, now trying GnuTLS\n");
868 log_debug_msg("We started the session OK, now trying GnuTLS\n"); 851 errno = 0;
869 errno = 0; 852 gnutls_global_init();
870 gnutls_global_init(); 853 //gnutls_anon_allocate_client_credentials(&anoncred);
871 //gnutls_anon_allocate_client_credentials(&anoncred); 854 gnutls_certificate_allocate_credentials(&xcred);
872 gnutls_certificate_allocate_credentials(&xcred); 855 gnutls_certificate_set_x509_trust_file(xcred, "hostcert.pem", GNUTLS_X509_FMT_PEM);
873 gnutls_certificate_set_x509_trust_file(xcred, "hostcert.pem", GNUTLS_X509_FMT_PEM); 856 gnutls_init(control->ssl_session, GNUTLS_CLIENT);
874 gnutls_init(control->ssl_session, GNUTLS_CLIENT); 857 {
875 { 858 int protocol_priority[16] = { GNUTLS_SSL3, 0 };
876 int protocol_priority[16] = { GNUTLS_SSL3, 0 }; 859 int kx_priority[16] = { GNUTLS_KX_ANON_DH, GNUTLS_KX_RSA, 0 };
877 int kx_priority[16] = { GNUTLS_KX_ANON_DH, GNUTLS_KX_RSA, 0 }; 860 int cipher_priority[16] = { GNUTLS_CIPHER_AES_128_CBC, GNUTLS_CIPHER_AES_256_CBC, 0 };
878 int cipher_priority[16] = { GNUTLS_CIPHER_AES_128_CBC, GNUTLS_CIPHER_AES_256_CBC, 0 }; 861 int mac_priority[16] = { GNUTLS_MAC_SHA1, GNUTLS_MAC_MD5, 0 };
879 int mac_priority[16] = { GNUTLS_MAC_SHA1, GNUTLS_MAC_MD5, 0 }; 862 int comp_priority[16] = { GNUTLS_COMP_NULL, 0 };
880 int comp_priority[16] = { GNUTLS_COMP_NULL, 0 }; 863
881 864 gnutls_cipher_set_priority(*control->ssl_session, cipher_priority);
882 gnutls_cipher_set_priority(*control->ssl_session, cipher_priority); 865 gnutls_compression_set_priority(*control->ssl_session, comp_priority);
883 gnutls_compression_set_priority(*control->ssl_session, comp_priority); 866 gnutls_kx_set_priority(*control->ssl_session, kx_priority);
884 gnutls_kx_set_priority(*control->ssl_session, kx_priority); 867 gnutls_protocol_set_priority(*control->ssl_session, protocol_priority);
885 gnutls_protocol_set_priority(*control->ssl_session, protocol_priority); 868 gnutls_mac_set_priority(*control->ssl_session, mac_priority);
886 gnutls_mac_set_priority(*control->ssl_session, mac_priority); 869
887 870 }
888 } 871 gnutls_credentials_set(*control->ssl_session, GNUTLS_CRD_CERTIFICATE, xcred); // this part is killing me.
889 gnutls_credentials_set(*control->ssl_session, GNUTLS_CRD_CERTIFICATE, xcred); // this part is killing me. 872
890 873 log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS step 1...\n");
891 log_debug_msg("GnuTLS step 1...\n"); 874 gnutls_transport_set_ptr(*control->ssl_session, (gnutls_transport_ptr_t) control);
892 gnutls_transport_set_ptr(*control->ssl_session, (gnutls_transport_ptr_t) control); 875 log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS step 2...\n");
893 log_debug_msg("GnuTLS step 2...\n"); 876 gnutls_transport_set_push_function(*control->ssl_session, (gnutls_push_func) & lockdownd_secuwrite);
894 gnutls_transport_set_push_function(*control->ssl_session, (gnutls_push_func) & lockdownd_secuwrite); 877 log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS step 3...\n");
895 log_debug_msg("GnuTLS step 3...\n"); 878 gnutls_transport_set_pull_function(*control->ssl_session, (gnutls_pull_func) & lockdownd_securead);
896 gnutls_transport_set_pull_function(*control->ssl_session, (gnutls_pull_func) & lockdownd_securead); 879 log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS step 4 -- now handshaking...\n");
897 log_debug_msg("GnuTLS step 4 -- now handshaking...\n"); 880
898 881 if (errno)
899 if (errno) 882 log_dbg_msg(DBGMASK_LOCKDOWND, "WARN: errno says %s before handshake!\n", strerror(errno));
900 log_debug_msg("WARN: errno says %s before handshake!\n", strerror(errno)); 883 return_me = gnutls_handshake(*control->ssl_session);
901 return_me = gnutls_handshake(*control->ssl_session); 884 log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS handshake done...\n");
902 log_debug_msg("GnuTLS handshake done...\n"); 885
903 886 if (return_me != GNUTLS_E_SUCCESS) {
904 if (return_me != GNUTLS_E_SUCCESS) { 887 log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS reported something wrong.\n");
905 log_debug_msg("GnuTLS reported something wrong.\n"); 888 gnutls_perror(return_me);
906 gnutls_perror(return_me); 889 log_dbg_msg(DBGMASK_LOCKDOWND, "oh.. errno says %s\n", strerror(errno));
907 log_debug_msg("oh.. errno says %s\n", strerror(errno)); 890 return IPHONE_E_SSL_ERROR;
908 return IPHONE_E_SSL_ERROR; 891 } else {
909 } else { 892 control->in_SSL = 1;
910 control->in_SSL = 1; 893 ret = IPHONE_E_SUCCESS;
911 ret = IPHONE_E_SUCCESS;
912 }
913 } else if (!strcmp(dictionary[i], "SessionID")) {
914 // we need to store the session ID for StopSession
915 strcpy(control->session_id, dictionary[i + 1]);
916 log_debug_msg("SessionID: %s\n", control->session_id);
917 free_dictionary(dictionary);
918 return ret;
919 } 894 }
920 } 895 }
921 if (ret == IPHONE_E_SUCCESS) { 896 }
922 log_debug_msg("Failed to get SessionID!\n"); 897 //store session id
923 return ret; 898 plist_t session_node = plist_find_node_by_key(dict, "SessionID");
924 } 899 if (session_node) {
900
901 plist_t session_node_val = plist_get_next_sibling(session_node);
902 plist_type session_node_val_type = plist_get_node_type(session_node_val);
925 903
926 log_debug_msg("Apparently failed negotiating with lockdownd.\n"); 904 if (session_node_val_type == PLIST_STRING) {
927 log_debug_msg("Responding dictionary: \n"); 905
928 for (i = 0; dictionary[i]; i += 2) { 906 char *session_id = NULL;
929 log_debug_msg("\t%s: %s\n", dictionary[i], dictionary[i + 1]); 907 plist_get_string_val(session_node_val, &session_id);
908
909 if (session_node_val_type == PLIST_STRING && session_id) {
910 // we need to store the session ID for StopSession
911 strcpy(control->session_id, session_id);
912 log_dbg_msg(DBGMASK_LOCKDOWND, "SessionID: %s\n", control->session_id);
913 }
914 free(session_id);
930 } 915 }
916 } else
917 log_dbg_msg(DBGMASK_LOCKDOWND, "Failed to get SessionID!\n");
918 plist_free(dict);
919 dict = NULL;
931 920
921 if (ret == IPHONE_E_SUCCESS)
922 return ret;
932 923
933 free_dictionary(dictionary); 924 log_dbg_msg(DBGMASK_LOCKDOWND, "Apparently failed negotiating with lockdownd.\n");
934 return IPHONE_E_SSL_ERROR; 925 return IPHONE_E_SSL_ERROR;
935 } else {
936 log_debug_msg("Didn't get enough bytes.\n");
937 return IPHONE_E_NOT_ENOUGH_DATA;
938 }
939} 926}
940 927
941/** gnutls callback for writing data to the iPhone. 928/** gnutls callback for writing data to the iPhone.
@@ -951,10 +938,10 @@ ssize_t lockdownd_secuwrite(gnutls_transport_ptr_t transport, char *buffer, size
951 int bytes = 0; 938 int bytes = 0;
952 iphone_lckd_client_t control; 939 iphone_lckd_client_t control;
953 control = (iphone_lckd_client_t) transport; 940 control = (iphone_lckd_client_t) transport;
954 log_debug_msg("lockdownd_secuwrite() called\n"); 941 log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_secuwrite() called\n");
955 log_debug_msg("pre-send\nlength = %zi\n", length); 942 log_dbg_msg(DBGMASK_LOCKDOWND, "pre-send\nlength = %zi\n", length);
956 iphone_mux_send(control->connection, buffer, length, &bytes); 943 iphone_mux_send(control->connection, buffer, length, &bytes);
957 log_debug_msg("post-send\nsent %i bytes\n", bytes); 944 log_dbg_msg(DBGMASK_LOCKDOWND, "post-send\nsent %i bytes\n", bytes);
958 945
959 dump_debug_buffer("sslpacketwrite.out", buffer, length); 946 dump_debug_buffer("sslpacketwrite.out", buffer, length);
960 return bytes; 947 return bytes;
@@ -1038,86 +1025,72 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char
1038 if (!client->in_SSL && !lockdownd_start_SSL_session(client, host_id)) 1025 if (!client->in_SSL && !lockdownd_start_SSL_session(client, host_id))
1039 return IPHONE_E_SSL_ERROR; 1026 return IPHONE_E_SSL_ERROR;
1040 1027
1041 char *XML_query, **dictionary; 1028 plist_t dict = NULL;
1042 uint32 length, i = 0, port_loc = 0, bytes = 0; 1029 uint32_t port_loc = 0;
1043 uint8 result = 0;
1044 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 1030 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
1045 1031
1046 free(host_id); 1032 free(host_id);
1047 host_id = NULL; 1033 host_id = NULL;
1048 1034
1049 xmlDocPtr plist = new_plist(); 1035 dict = plist_new_dict();
1050 xmlNode *dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); 1036 plist_add_sub_key_el(dict, "Request");
1051 xmlNode *key; 1037 plist_add_sub_string_el(dict, "StartService");
1052 key = add_key_str_dict_element(plist, dict, "Request", "StartService", 1); 1038 plist_add_sub_key_el(dict, "Service");
1053 if (!key) { 1039 plist_add_sub_string_el(dict, service);
1054 xmlFreeDoc(plist);
1055 return IPHONE_E_UNKNOWN_ERROR;
1056 }
1057 key = add_key_str_dict_element(plist, dict, "Service", service, 1);
1058 if (!key) {
1059 xmlFreeDoc(plist);
1060 return IPHONE_E_UNKNOWN_ERROR;
1061 }
1062 1040
1063 xmlDocDumpMemory(plist, (xmlChar **) & XML_query, &length); 1041 /* send to iPhone */
1042 ret = iphone_lckd_send(client, dict);
1043 plist_free(dict);
1044 dict = NULL;
1064 1045
1065 ret = iphone_lckd_send(client, XML_query, length, &bytes);
1066 free(XML_query);
1067 if (IPHONE_E_SUCCESS != ret) 1046 if (IPHONE_E_SUCCESS != ret)
1068 return ret; 1047 return ret;
1069 1048
1070 ret = iphone_lckd_recv(client, &XML_query, &bytes); 1049 ret = iphone_lckd_recv(client, &dict);
1071 xmlFreeDoc(plist); 1050
1072 if (IPHONE_E_SUCCESS != ret) 1051 if (IPHONE_E_SUCCESS != ret)
1073 return ret; 1052 return ret;
1074 1053
1075 if (bytes <= 0) 1054 if (!dict)
1076 return IPHONE_E_NOT_ENOUGH_DATA; 1055 return IPHONE_E_PLIST_ERROR;
1077 else {
1078 plist = xmlReadMemory(XML_query, bytes, NULL, NULL, 0);
1079 if (!plist)
1080 return IPHONE_E_UNKNOWN_ERROR;
1081 dict = xmlDocGetRootElement(plist);
1082 if (!dict)
1083 return IPHONE_E_UNKNOWN_ERROR;
1084 for (dict = dict->children; dict; dict = dict->next) {
1085 if (!xmlStrcmp(dict->name, "dict"))
1086 break;
1087 }
1088 1056
1089 if (!dict) 1057 plist_t query_node = plist_find_node_by_string(dict, "StartService");
1090 return IPHONE_E_UNKNOWN_ERROR; 1058 plist_t result_key_node = plist_get_next_sibling(query_node);
1091 dictionary = read_dict_element_strings(dict); 1059 plist_t result_value_node = plist_get_next_sibling(result_key_node);
1092 1060
1093 for (i = 0; dictionary[i]; i += 2) { 1061 plist_t port_key_node = plist_find_node_by_key(dict, "Port");
1094 log_debug_msg("lockdownd_start_service() dictionary %s: %s\n", dictionary[i], dictionary[i + 1]); 1062 plist_t port_value_node = plist_get_next_sibling(port_key_node);
1095 1063
1096 if (!xmlStrcmp(dictionary[i], "Port")) { 1064 plist_type result_key_type = plist_get_node_type(result_key_node);
1097 port_loc = atoi(dictionary[i + 1]); 1065 plist_type result_value_type = plist_get_node_type(result_value_node);
1098 log_debug_msg("lockdownd_start_service() atoi'd port: %i\n", port); 1066 plist_type port_key_type = plist_get_node_type(port_key_node);
1099 } 1067 plist_type port_value_type = plist_get_node_type(port_value_node);
1100 1068
1101 if (!xmlStrcmp(dictionary[i], "Result")) { 1069 if (result_key_type == PLIST_KEY && result_value_type == PLIST_STRING && port_key_type == PLIST_KEY
1102 if (!xmlStrcmp(dictionary[i + 1], "Success")) { 1070 && port_value_type == PLIST_UINT) {
1103 result = 1; 1071
1104 } 1072 char *result_key = NULL;
1105 } 1073 char *result_value = NULL;
1106 } 1074 char *port_key = NULL;
1075 uint64_t port_value = 0;
1107 1076
1108 log_debug_msg("lockdownd_start_service(): DATA RECEIVED:\n\n"); 1077 plist_get_key_val(result_key_node, &result_key);
1109 log_debug_msg(XML_query); 1078 plist_get_string_val(result_value_node, &result_value);
1110 log_debug_msg("end data received by lockdownd_start_service()\n"); 1079 plist_get_key_val(port_key_node, &port_key);
1080 plist_get_uint_val(port_value_node, &port_value);
1111 1081
1112 free(XML_query); 1082 if (!strcmp(result_key, "Result") && !strcmp(result_value, "Success") && !strcmp(port_key, "Port")) {
1113 xmlFreeDoc(plist); 1083 port_loc = port_value;
1114 free_dictionary(dictionary); 1084 ret = IPHONE_E_SUCCESS;
1115 if (port && result) { 1085 }
1086
1087 if (port && ret == IPHONE_E_SUCCESS)
1116 *port = port_loc; 1088 *port = port_loc;
1117 return IPHONE_E_SUCCESS; 1089 else
1118 } else 1090 ret = IPHONE_E_UNKNOWN_ERROR;
1119 return IPHONE_E_UNKNOWN_ERROR;
1120 } 1091 }
1121 1092
1122 return IPHONE_E_UNKNOWN_ERROR; 1093 plist_free(dict);
1094 dict = NULL;
1095 return ret;
1123} 1096}