From f4c4b783c8dbe2fe8e7e6f6b5f19f0d44b489c9a Mon Sep 17 00:00:00 2001 From: Zach C Date: Sun, 31 Aug 2008 11:25:22 -0700 Subject: Added binary-plist support (tweaked slightly to move stuff around) Signed-off-by: Matt Colyer fix makefile to take correct main function into account --- src/lockdown.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/lockdown.c') diff --git a/src/lockdown.c b/src/lockdown.c index 65cbf90..6b8f298 100644 --- a/src/lockdown.c +++ b/src/lockdown.c @@ -127,7 +127,7 @@ iphone_error_t iphone_lckd_recv(iphone_lckd_client_t client, char **dump_data, u return IPHONE_E_INVALID_ARG; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; char *receive; - uint32 datalen = 0, bytes = 0; + uint32_t datalen = 0, bytes = 0; if (!client->in_SSL) ret = iphone_mux_recv(client->connection, (char *) &datalen, sizeof(datalen), &bytes); @@ -211,7 +211,7 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control) dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); key = add_key_str_dict_element(plist, dict, "Request", "QueryType", 1); char *XML_content; - uint32 length; + uint32_t length; xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); ret = iphone_lckd_send(control, XML_content, length, &bytes); @@ -265,7 +265,7 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, char *r char **dictionary = NULL; int bytes = 0, i = 0; char *XML_content = NULL; - uint32 length = 0; + uint32_t length = 0; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; /* Setup DevicePublicKey request plist */ @@ -420,7 +420,7 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch char **dictionary = NULL; int bytes = 0, i = 0; char *XML_content = NULL; - uint32 length = 0; + uint32_t length = 0; char *device_cert_b64 = NULL; char *host_cert_b64 = NULL; @@ -658,7 +658,7 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c xmlNode *dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); xmlNode *key; char *what2send = NULL, **dictionary = NULL; - uint32 len = 0, bytes = 0, return_me = 0, i = 0; + uint32_t len = 0, bytes = 0, return_me = 0, i = 0; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; // end variables @@ -893,8 +893,8 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char return IPHONE_E_SSL_ERROR; char *XML_query, **dictionary; - uint32 length, i = 0, port_loc = 0, bytes = 0; - uint8 result = 0; + uint32_t length, i = 0, port_loc = 0, bytes = 0; + uint8_t result = 0; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; free(host_id); -- cgit v1.1-32-gdbae From d560cf5a15d1aef74e95b208ed69b7d324d94354 Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Sun, 30 Nov 2008 21:49:56 +0100 Subject: complete xml plist abstraction and migrate lockdownd_hello to new plist API. --- src/lockdown.c | 62 +++++++++++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 27 deletions(-) (limited to 'src/lockdown.c') diff --git a/src/lockdown.c b/src/lockdown.c index 6b8f298..2906fdf 100644 --- a/src/lockdown.c +++ b/src/lockdown.c @@ -201,49 +201,57 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control) { if (!control) return IPHONE_E_INVALID_ARG; - xmlDocPtr plist = new_plist(); - xmlNode *dict, *key; - char **dictionary; + int bytes = 0, i = 0; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; + plist_t plist = NULL; + plist_new_plist(&plist); + + dict_t dict = NULL; + plist_new_dict_in_plist(plist, &dict); + + plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "QueryType"); + log_debug_msg("lockdownd_hello() called\n"); - dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); - key = add_key_str_dict_element(plist, dict, "Request", "QueryType", 1); - char *XML_content; - uint32_t length; + char *XML_content = NULL; + uint32_t length = 0; - xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); + plist_to_xml(plist, &XML_content, &length); + log_debug_msg("Send msg :\nsize : %i\nxml : %s", length, XML_content); ret = iphone_lckd_send(control, XML_content, length, &bytes); xmlFree(XML_content); - xmlFreeDoc(plist); + XML_content = NULL; + plist_free(plist); plist = NULL; + ret = iphone_lckd_recv(control, &XML_content, &bytes); + log_debug_msg("Receive msg :\nsize : %i\nxml : %s", bytes, XML_content); + xml_to_plist(XML_content, bytes, &plist); - plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0); if (!plist) return IPHONE_E_PLIST_ERROR; - dict = xmlDocGetRootElement(plist); - for (dict = dict->children; dict; dict = dict->next) { - if (!xmlStrcmp(dict->name, "dict")) - break; - } - if (!dict) - return IPHONE_E_DICT_ERROR; - dictionary = read_dict_element_strings(dict); - xmlFreeDoc(plist); - free(XML_content); - for (i = 0; dictionary[i]; i += 2) { - if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { - log_debug_msg("lockdownd_hello(): success\n"); - ret = IPHONE_E_SUCCESS; - break; - } + plist_t query_node = find_query_node(plist, "Request", "QueryType"); + plist_t result_node = g_node_next_sibling(query_node); + plist_t value_node = g_node_next_sibling(result_node); + + plist_type result_type; + plist_type value_type; + + char *result_value = NULL; + char *value_value = NULL; + + get_type_and_value(result_node, &result_type, (void *) (&result_value)); + get_type_and_value(value_node, &value_type, (void *) (&value_value)); + + if (result_type == PLIST_KEY && + value_type == PLIST_STRING && !strcmp(result_value, "Result") && !strcmp(value_value, "Success")) { + log_debug_msg("lockdownd_hello(): success\n"); + ret = IPHONE_E_SUCCESS; } - free_dictionary(dictionary); return ret; } -- cgit v1.1-32-gdbae From 3d08602c6e5c86538e447fccd774a0eac0391868 Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Sun, 30 Nov 2008 23:33:39 +0100 Subject: migrate lockdownd_generic_get_value to new plisy API. --- src/lockdown.c | 80 ++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 33 deletions(-) (limited to 'src/lockdown.c') diff --git a/src/lockdown.c b/src/lockdown.c index 2906fdf..1782d45 100644 --- a/src/lockdown.c +++ b/src/lockdown.c @@ -267,26 +267,27 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, char *r { if (!control || !req_key || !value || (value && *value)) return IPHONE_E_INVALID_ARG; - xmlDocPtr plist = new_plist(); - xmlNode *dict = NULL; - xmlNode *key = NULL;; - char **dictionary = NULL; + plist_t plist = NULL; + dict_t dict = NULL; int bytes = 0, i = 0; char *XML_content = NULL; uint32_t length = 0; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; /* Setup DevicePublicKey request plist */ - dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); - key = add_key_str_dict_element(plist, dict, req_key, req_string, 1); - key = add_key_str_dict_element(plist, dict, "Request", "GetValue", 1); - xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); + plist_new_plist(&plist); + plist_new_dict_in_plist(plist, &dict); + plist_add_dict_element(dict, req_key, PLIST_STRING, (void *) req_string); + plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "GetValue"); + plist_to_xml(plist, &XML_content, &length); /* send to iPhone */ + log_debug_msg("Send msg :\nsize : %i\nxml : %s", length, XML_content); ret = iphone_lckd_send(control, XML_content, length, &bytes); xmlFree(XML_content); - xmlFreeDoc(plist); + XML_content = NULL; + plist_free(plist); plist = NULL; if (ret != IPHONE_E_SUCCESS) @@ -294,42 +295,55 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, char *r /* Now get iPhone's answer */ ret = iphone_lckd_recv(control, &XML_content, &bytes); + log_debug_msg("Receive msg :\nsize : %i\nxml : %s", bytes, XML_content); if (ret != IPHONE_E_SUCCESS) return ret; - plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0); + xml_to_plist(XML_content, bytes, &plist); if (!plist) return IPHONE_E_PLIST_ERROR; - dict = xmlDocGetRootElement(plist); - for (dict = dict->children; dict; dict = dict->next) { - if (!xmlStrcmp(dict->name, "dict")) - break; - } - if (!dict) - return IPHONE_E_DICT_ERROR; - /* Parse xml to check success and to find public key */ - dictionary = read_dict_element_strings(dict); - xmlFreeDoc(plist); - free(XML_content); + plist_t query_node = find_query_node(plist, "Request", "GetValue"); + plist_t result_key_node = g_node_next_sibling(query_node); + plist_t result_value_node = g_node_next_sibling(result_key_node); - int success = 0; - for (i = 0; dictionary[i]; i += 2) { - if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { - success = 1; - } - if (!strcmp(dictionary[i], "Value")) { - *value = strdup(dictionary[i + 1]); - } + plist_type result_key_type; + plist_type result_value_type; + char *result_key = NULL; + char *result_value = NULL; + + get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key)); + get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value)); + + if (result_key_type == PLIST_KEY && + result_value_type == PLIST_STRING && !strcmp(result_key, "Result") && !strcmp(result_value, "Success")) { + log_debug_msg("lockdownd_generic_get_value(): success\n"); + ret = IPHONE_E_SUCCESS; } - if (dictionary) { - free_dictionary(dictionary); - dictionary = NULL; + if (ret != IPHONE_E_SUCCESS) { + return IPHONE_E_DICT_ERROR; } - if (success) + + plist_t value_key_node = g_node_next_sibling(result_key_node); + plist_t value_value_node = g_node_next_sibling(value_key_node); + plist_type value_key_type; + plist_type value_value_type; + char *value_key = NULL; + char *value_value = NULL; + + get_type_and_value(value_key_node, &value_key_type, (void *) (&value_key)); + get_type_and_value(value_value_node, &value_value_type, (void *) (&value_value)); + + if (value_key_type == PLIST_KEY && !strcmp(result_key, "Value")) { + log_debug_msg("lockdownd_generic_get_value(): success\n"); + *value = value_value; ret = IPHONE_E_SUCCESS; + } + + plist_free(plist); + free(XML_content); return ret; } -- cgit v1.1-32-gdbae From 505c97582b53ed406169f931a49ee6f678b19b52 Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Mon, 1 Dec 2008 20:25:12 +0100 Subject: continue migration to new plist API. --- src/lockdown.c | 184 +++++++++++++++++++++++++++------------------------------ 1 file changed, 87 insertions(+), 97 deletions(-) (limited to 'src/lockdown.c') diff --git a/src/lockdown.c b/src/lockdown.c index 1782d45..3d39fbd 100644 --- a/src/lockdown.c +++ b/src/lockdown.c @@ -91,7 +91,6 @@ iphone_lckd_client_t new_lockdownd_client(iphone_device_t phone) return control; } - /** Closes the lockdownd client and does the necessary housekeeping. * * @param control The lockdown client @@ -436,10 +435,9 @@ iphone_error_t iphone_lckd_new_client(iphone_device_t device, iphone_lckd_client iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, char *host_id) { iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; - xmlDocPtr plist = new_plist(); - xmlNode *dict = NULL; - xmlNode *dictRecord = NULL; - char **dictionary = NULL; + plist_t plist = NULL; + dict_t dict = NULL; + dict_t dict_record = NULL; int bytes = 0, i = 0; char *XML_content = NULL; uint32_t length = 0; @@ -462,24 +460,23 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch } /* Setup Pair request plist */ - dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); - dictRecord = add_key_dict_node(plist, dict, "PairRecord", "\n", 1); - //dictRecord = add_child_to_plist(plist, "dict", "\n", NULL, 1); - add_key_data_dict_element(plist, dictRecord, "DeviceCertificate", device_cert_b64, 2); - add_key_data_dict_element(plist, dictRecord, "HostCertificate", host_cert_b64, 2); - add_key_str_dict_element(plist, dictRecord, "HostID", host_id, 2); - add_key_data_dict_element(plist, dictRecord, "RootCertificate", root_cert_b64, 2); - add_key_str_dict_element(plist, dict, "Request", "Pair", 1); - - xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); - - printf("XML Pairing request : %s\n", XML_content); + plist_new_plist(&plist); + plist_new_dict_in_plist(plist, &dict); + plist_add_dict_element(dict, "PairRecord", PLIST_DICT, NULL); + dict_record = g_node_last_child(dict); + plist_add_dict_element(dict_record, "DeviceCertificate", PLIST_DATA, (void *) device_cert_b64); + plist_add_dict_element(dict_record, "HostCertificate", PLIST_DATA, (void *) host_cert_b64); + plist_add_dict_element(dict_record, "HostID", PLIST_STRING, (void *) host_id); + plist_add_dict_element(dict_record, "RootCertificate", PLIST_DATA, (void *) root_cert_b64); + plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "Pair"); + plist_to_xml(plist, &XML_content, &length); + log_debug_msg("XML Pairing request :\nsize : %i\nxml :\n %s", length, XML_content); /* send to iPhone */ ret = iphone_lckd_send(control, XML_content, length, &bytes); xmlFree(XML_content); - xmlFreeDoc(plist); + plist_free(plist); plist = NULL; if (ret != IPHONE_E_SUCCESS) @@ -495,40 +492,29 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch log_debug_msg(XML_content); log_debug_msg("\n\n"); - plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0); - if (!plist) { - free(public_key_b64); + xml_to_plist(XML_content, bytes, &plist); + if (!plist) return IPHONE_E_PLIST_ERROR; - } - dict = xmlDocGetRootElement(plist); - for (dict = dict->children; dict; dict = dict->next) { - if (!xmlStrcmp(dict->name, "dict")) - break; - } - if (!dict) { - free(public_key_b64); - return IPHONE_E_DICT_ERROR; - } - /* Parse xml to check success and to find public key */ - dictionary = read_dict_element_strings(dict); - xmlFreeDoc(plist); - free(XML_content); + plist_t query_node = find_query_node(plist, "Request", "Pair"); + plist_t result_key_node = g_node_next_sibling(query_node); + plist_t result_value_node = g_node_next_sibling(result_key_node); - int success = 0; - for (i = 0; dictionary[i]; i += 2) { - if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { - success = 1; - } - } + plist_type result_key_type; + plist_type result_value_type; + char *result_key = NULL; + char *result_value = NULL; - if (dictionary) { - free_dictionary(dictionary); - dictionary = NULL; + get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key)); + get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value)); + + if (result_key_type == PLIST_KEY && + result_value_type == PLIST_STRING && !strcmp(result_key, "Result") && !strcmp(result_value, "Success")) { + ret = IPHONE_E_SUCCESS; } /* store public key in config if pairing succeeded */ - if (success) { + if (ret == IPHONE_E_SUCCESS) { log_debug_msg("lockdownd_pair_device: pair success\n"); store_device_public_key(uid, public_key_b64); ret = IPHONE_E_SUCCESS; @@ -914,81 +900,85 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char if (!client->in_SSL && !lockdownd_start_SSL_session(client, host_id)) return IPHONE_E_SSL_ERROR; - char *XML_query, **dictionary; + + plist_t plist = NULL; + dict_t dict = NULL; + char *XML_content = NULL; uint32_t length, i = 0, port_loc = 0, bytes = 0; - uint8_t result = 0; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; free(host_id); host_id = NULL; - xmlDocPtr plist = new_plist(); - xmlNode *dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); - xmlNode *key; - key = add_key_str_dict_element(plist, dict, "Request", "StartService", 1); - if (!key) { - xmlFreeDoc(plist); - return IPHONE_E_UNKNOWN_ERROR; - } - key = add_key_str_dict_element(plist, dict, "Service", service, 1); - if (!key) { - xmlFreeDoc(plist); - return IPHONE_E_UNKNOWN_ERROR; - } + plist_new_plist(&plist); + plist_new_dict_in_plist(plist, &dict); + plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "StartService"); + plist_add_dict_element(dict, "Service", PLIST_STRING, (void *) service); + plist_to_xml(plist, &XML_content, &length); + + /* send to iPhone */ + log_debug_msg("Send msg :\nsize : %i\nxml : %s", length, XML_content); + ret = iphone_lckd_send(client, XML_content, length, &bytes); - xmlDocDumpMemory(plist, (xmlChar **) & XML_query, &length); + xmlFree(XML_content); + XML_content = NULL; + plist_free(plist); + plist = NULL; - ret = iphone_lckd_send(client, XML_query, length, &bytes); - free(XML_query); if (IPHONE_E_SUCCESS != ret) return ret; - ret = iphone_lckd_recv(client, &XML_query, &bytes); - xmlFreeDoc(plist); + ret = iphone_lckd_recv(client, &XML_content, &bytes); + if (IPHONE_E_SUCCESS != ret) return ret; + xml_to_plist(XML_content, bytes, &plist); + if (!plist) + return IPHONE_E_PLIST_ERROR; + + if (bytes <= 0) return IPHONE_E_NOT_ENOUGH_DATA; else { - plist = xmlReadMemory(XML_query, bytes, NULL, NULL, 0); - if (!plist) - return IPHONE_E_UNKNOWN_ERROR; - dict = xmlDocGetRootElement(plist); - if (!dict) - return IPHONE_E_UNKNOWN_ERROR; - for (dict = dict->children; dict; dict = dict->next) { - if (!xmlStrcmp(dict->name, "dict")) - break; - } - if (!dict) - return IPHONE_E_UNKNOWN_ERROR; - dictionary = read_dict_element_strings(dict); - - for (i = 0; dictionary[i]; i += 2) { - log_debug_msg("lockdownd_start_service() dictionary %s: %s\n", dictionary[i], dictionary[i + 1]); - - if (!xmlStrcmp(dictionary[i], "Port")) { - port_loc = atoi(dictionary[i + 1]); - log_debug_msg("lockdownd_start_service() atoi'd port: %i\n", port); - } - - if (!xmlStrcmp(dictionary[i], "Result")) { - if (!xmlStrcmp(dictionary[i + 1], "Success")) { - result = 1; - } - } + plist_t query_node = find_query_node(plist, "Request", "StartService"); + plist_t result_key_node = g_node_next_sibling(query_node); + plist_t result_value_node = g_node_next_sibling(result_key_node); + + plist_t port_key_node = find_node(plist, PLIST_KEY, "Port"); + plist_t port_value_node = g_node_next_sibling(port_key_node); + + plist_type result_key_type; + plist_type result_value_type; + plist_type port_key_type; + plist_type port_value_type; + char *result_key = NULL; + char *result_value = NULL; + char *port_key = NULL; + uint64_t port_value = 0; + + get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key)); + get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value)); + get_type_and_value(port_key_node, &port_key_type, (void *) (&port_key)); + get_type_and_value(port_value_node, &port_value_type, (void *) (&port_value)); + + if (result_key_type == PLIST_KEY && + result_value_type == PLIST_STRING && + port_key_type == PLIST_KEY && + port_value_type == PLIST_UINT64 && + !strcmp(result_key, "Result") && !strcmp(result_value, "Success") && !strcmp(port_key, "Port")) { + port_loc = port_value; + ret = IPHONE_E_SUCCESS; } log_debug_msg("lockdownd_start_service(): DATA RECEIVED:\n\n"); - log_debug_msg(XML_query); + log_debug_msg(XML_content); log_debug_msg("end data received by lockdownd_start_service()\n"); - free(XML_query); - xmlFreeDoc(plist); - free_dictionary(dictionary); - if (port && result) { + free(XML_content); + plist_free(plist); + if (port && ret == IPHONE_E_SUCCESS) { *port = port_loc; return IPHONE_E_SUCCESS; } else -- cgit v1.1-32-gdbae From 36eff97cba9049ce7ec194cb4f8926c7876368eb Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Mon, 1 Dec 2008 21:12:02 +0100 Subject: finish new plist API migration. --- src/lockdown.c | 183 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 90 insertions(+), 93 deletions(-) (limited to 'src/lockdown.c') diff --git a/src/lockdown.c b/src/lockdown.c index 3d39fbd..55e2e65 100644 --- a/src/lockdown.c +++ b/src/lockdown.c @@ -662,113 +662,110 @@ iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_ */ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const char *HostID) { - xmlDocPtr plist = new_plist(); - xmlNode *dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); - xmlNode *key; - char *what2send = NULL, **dictionary = NULL; - uint32_t len = 0, bytes = 0, return_me = 0, i = 0; + plist_t plist = NULL; + dict_t dict = NULL; + char *XML_content = NULL; + uint32_t length = 0, bytes = 0, return_me = 0; + iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; - // end variables - key = add_key_str_dict_element(plist, dict, "HostID", HostID, 1); - if (!key) { - log_debug_msg("Couldn't add a key.\n"); - xmlFreeDoc(plist); - return IPHONE_E_DICT_ERROR; - } - key = add_key_str_dict_element(plist, dict, "Request", "StartSession", 1); - if (!key) { - log_debug_msg("Couldn't add a key.\n"); - xmlFreeDoc(plist); - return IPHONE_E_DICT_ERROR; - } + /* Setup DevicePublicKey request plist */ + plist_new_plist(&plist); + plist_new_dict_in_plist(plist, &dict); + plist_add_dict_element(dict, "HostID", PLIST_STRING, (void *) HostID); + plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "StartSession"); + plist_to_xml(plist, &XML_content, &length); + log_debug_msg("Send msg :\nsize : %i\nxml : %s", length, XML_content); - xmlDocDumpMemory(plist, (xmlChar **) & what2send, &len); - ret = iphone_lckd_send(control, what2send, len, &bytes); + ret = iphone_lckd_send(control, XML_content, length, &bytes); - xmlFree(what2send); - xmlFreeDoc(plist); + xmlFree(XML_content); + XML_content = NULL; + plist_free(plist); + plist = NULL; if (ret != IPHONE_E_SUCCESS) return ret; if (bytes > 0) { - ret = iphone_lckd_recv(control, &what2send, &len); - plist = xmlReadMemory(what2send, len, NULL, NULL, 0); - dict = xmlDocGetRootElement(plist); - if (!dict) - return IPHONE_E_DICT_ERROR; - for (dict = dict->children; dict; dict = dict->next) { - if (!xmlStrcmp(dict->name, "dict")) - break; - } - dictionary = read_dict_element_strings(dict); - xmlFreeDoc(plist); - free(what2send); - for (i = 0; dictionary[i]; i += 2) { - if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { - // Set up GnuTLS... - //gnutls_anon_client_credentials_t anoncred; - gnutls_certificate_credentials_t xcred; - - log_debug_msg("We started the session OK, now trying GnuTLS\n"); - errno = 0; - gnutls_global_init(); - //gnutls_anon_allocate_client_credentials(&anoncred); - gnutls_certificate_allocate_credentials(&xcred); - gnutls_certificate_set_x509_trust_file(xcred, "hostcert.pem", GNUTLS_X509_FMT_PEM); - gnutls_init(control->ssl_session, GNUTLS_CLIENT); - { - int protocol_priority[16] = { GNUTLS_SSL3, 0 }; - int kx_priority[16] = { GNUTLS_KX_ANON_DH, GNUTLS_KX_RSA, 0 }; - int cipher_priority[16] = { GNUTLS_CIPHER_AES_128_CBC, GNUTLS_CIPHER_AES_256_CBC, 0 }; - int mac_priority[16] = { GNUTLS_MAC_SHA1, GNUTLS_MAC_MD5, 0 }; - int comp_priority[16] = { GNUTLS_COMP_NULL, 0 }; - - gnutls_cipher_set_priority(*control->ssl_session, cipher_priority); - gnutls_compression_set_priority(*control->ssl_session, comp_priority); - gnutls_kx_set_priority(*control->ssl_session, kx_priority); - gnutls_protocol_set_priority(*control->ssl_session, protocol_priority); - gnutls_mac_set_priority(*control->ssl_session, mac_priority); - - } - gnutls_credentials_set(*control->ssl_session, GNUTLS_CRD_CERTIFICATE, xcred); // this part is killing me. - - log_debug_msg("GnuTLS step 1...\n"); - gnutls_transport_set_ptr(*control->ssl_session, (gnutls_transport_ptr_t) control); - log_debug_msg("GnuTLS step 2...\n"); - gnutls_transport_set_push_function(*control->ssl_session, (gnutls_push_func) & lockdownd_secuwrite); - log_debug_msg("GnuTLS step 3...\n"); - gnutls_transport_set_pull_function(*control->ssl_session, (gnutls_pull_func) & lockdownd_securead); - log_debug_msg("GnuTLS step 4 -- now handshaking...\n"); - - if (errno) - log_debug_msg("WARN: errno says %s before handshake!\n", strerror(errno)); - return_me = gnutls_handshake(*control->ssl_session); - log_debug_msg("GnuTLS handshake done...\n"); - - free_dictionary(dictionary); - - if (return_me != GNUTLS_E_SUCCESS) { - log_debug_msg("GnuTLS reported something wrong.\n"); - gnutls_perror(return_me); - log_debug_msg("oh.. errno says %s\n", strerror(errno)); - return IPHONE_E_SSL_ERROR; - } else { - control->in_SSL = 1; - return IPHONE_E_SUCCESS; - } + ret = iphone_lckd_recv(control, &XML_content, &bytes); + log_debug_msg("Receive msg :\nsize : %i\nxml : %s", bytes, XML_content); + xml_to_plist(XML_content, bytes, &plist); + if (!plist) + return IPHONE_E_PLIST_ERROR; + + plist_t query_node = find_query_node(plist, "Request", "StartSession"); + plist_t result_key_node = g_node_next_sibling(query_node); + plist_t result_value_node = g_node_next_sibling(result_key_node); + + plist_type result_key_type; + plist_type result_value_type; + char *result_key = NULL; + char *result_value = NULL; + + get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key)); + get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value)); + + xmlFree(XML_content); + XML_content = NULL; + plist_free(plist); + plist = NULL; + + if (result_key_type == PLIST_KEY && + result_value_type == PLIST_STRING && !strcmp(result_key, "Result") && !strcmp(result_value, "Success")) { + // Set up GnuTLS... + //gnutls_anon_client_credentials_t anoncred; + gnutls_certificate_credentials_t xcred; + + log_debug_msg("We started the session OK, now trying GnuTLS\n"); + errno = 0; + gnutls_global_init(); + //gnutls_anon_allocate_client_credentials(&anoncred); + gnutls_certificate_allocate_credentials(&xcred); + gnutls_certificate_set_x509_trust_file(xcred, "hostcert.pem", GNUTLS_X509_FMT_PEM); + gnutls_init(control->ssl_session, GNUTLS_CLIENT); + { + int protocol_priority[16] = { GNUTLS_SSL3, 0 }; + int kx_priority[16] = { GNUTLS_KX_ANON_DH, GNUTLS_KX_RSA, 0 }; + int cipher_priority[16] = { GNUTLS_CIPHER_AES_128_CBC, GNUTLS_CIPHER_AES_256_CBC, 0 }; + int mac_priority[16] = { GNUTLS_MAC_SHA1, GNUTLS_MAC_MD5, 0 }; + int comp_priority[16] = { GNUTLS_COMP_NULL, 0 }; + + gnutls_cipher_set_priority(*control->ssl_session, cipher_priority); + gnutls_compression_set_priority(*control->ssl_session, comp_priority); + gnutls_kx_set_priority(*control->ssl_session, kx_priority); + gnutls_protocol_set_priority(*control->ssl_session, protocol_priority); + gnutls_mac_set_priority(*control->ssl_session, mac_priority); + + } + gnutls_credentials_set(*control->ssl_session, GNUTLS_CRD_CERTIFICATE, xcred); // this part is killing me. + + log_debug_msg("GnuTLS step 1...\n"); + gnutls_transport_set_ptr(*control->ssl_session, (gnutls_transport_ptr_t) control); + log_debug_msg("GnuTLS step 2...\n"); + gnutls_transport_set_push_function(*control->ssl_session, (gnutls_push_func) & lockdownd_secuwrite); + log_debug_msg("GnuTLS step 3...\n"); + gnutls_transport_set_pull_function(*control->ssl_session, (gnutls_pull_func) & lockdownd_securead); + log_debug_msg("GnuTLS step 4 -- now handshaking...\n"); + + if (errno) + log_debug_msg("WARN: errno says %s before handshake!\n", strerror(errno)); + return_me = gnutls_handshake(*control->ssl_session); + log_debug_msg("GnuTLS handshake done...\n"); + + if (return_me != GNUTLS_E_SUCCESS) { + log_debug_msg("GnuTLS reported something wrong.\n"); + gnutls_perror(return_me); + log_debug_msg("oh.. errno says %s\n", strerror(errno)); + return IPHONE_E_SSL_ERROR; + } else { + control->in_SSL = 1; + return IPHONE_E_SUCCESS; } } log_debug_msg("Apparently failed negotiating with lockdownd.\n"); log_debug_msg("Responding dictionary: \n"); - for (i = 0; dictionary[i]; i += 2) { - log_debug_msg("\t%s: %s\n", dictionary[i], dictionary[i + 1]); - } - - - free_dictionary(dictionary); return IPHONE_E_SSL_ERROR; } else { log_debug_msg("Didn't get enough bytes.\n"); -- cgit v1.1-32-gdbae From 1a06347d27ca51283de3a9ff21e138a3ea9ba9b6 Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Mon, 8 Dec 2008 22:47:02 +0100 Subject: cleanup binary parsing and move stuff around. --- src/lockdown.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/lockdown.c') diff --git a/src/lockdown.c b/src/lockdown.c index 55e2e65..ae077b7 100644 --- a/src/lockdown.c +++ b/src/lockdown.c @@ -963,7 +963,7 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char if (result_key_type == PLIST_KEY && result_value_type == PLIST_STRING && port_key_type == PLIST_KEY && - port_value_type == PLIST_UINT64 && + port_value_type == PLIST_UINT && !strcmp(result_key, "Result") && !strcmp(result_value, "Success") && !strcmp(port_key, "Port")) { port_loc = port_value; ret = IPHONE_E_SUCCESS; -- cgit v1.1-32-gdbae From 18d1ee3b0f17325fdffe0cf3e2770a3f0f45a1b9 Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Thu, 11 Dec 2008 23:03:21 +0100 Subject: move stuff around to make code more organized. --- src/lockdown.c | 27 --------------------------- 1 file changed, 27 deletions(-) (limited to 'src/lockdown.c') diff --git a/src/lockdown.c b/src/lockdown.c index ae077b7..0957fa2 100644 --- a/src/lockdown.c +++ b/src/lockdown.c @@ -39,34 +39,7 @@ const ASN1_ARRAY_TYPE pkcs1_asn1_tab[] = { {0, 0, 0} }; -int get_rand(int min, int max) -{ - int retval = (rand() % (max - min)) + min; - return retval; -} -/** Generates a valid HostID (which is actually a UUID). - * - * @param A null terminated string containing a valid HostID. - */ -char *lockdownd_generate_hostid() -{ - char *hostid = (char *) malloc(sizeof(char) * 37); // HostID's are just UUID's, and UUID's are 36 characters long - const char *chars = "ABCDEF0123456789"; - srand(time(NULL)); - int i = 0; - - for (i = 0; i < 36; i++) { - if (i == 8 || i == 13 || i == 18 || i == 23) { - hostid[i] = '-'; - continue; - } else { - hostid[i] = chars[get_rand(0, 16)]; - } - } - hostid[36] = '\0'; // make it a real string - return hostid; -} /** Creates a lockdownd client for the give iPhone. * -- cgit v1.1-32-gdbae From 9ca887308d59e6cb5bf684f9f3bd968118e8014f Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Fri, 12 Dec 2008 22:05:44 +0100 Subject: Fix some bugs in binary plist generation. --- src/lockdown.c | 105 ++++++++++++++++++++++++++------------------------------- 1 file changed, 48 insertions(+), 57 deletions(-) (limited to 'src/lockdown.c') diff --git a/src/lockdown.c b/src/lockdown.c index 0957fa2..4c96a7d 100644 --- a/src/lockdown.c +++ b/src/lockdown.c @@ -177,11 +177,8 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control) int bytes = 0, i = 0; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; - plist_t plist = NULL; - plist_new_plist(&plist); - - dict_t dict = NULL; - plist_new_dict_in_plist(plist, &dict); + plist_t dict = NULL; + plist_new_dict(&dict); plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "QueryType"); @@ -189,23 +186,23 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control) char *XML_content = NULL; uint32_t length = 0; - plist_to_xml(plist, &XML_content, &length); + plist_to_xml(dict, &XML_content, &length); log_debug_msg("Send msg :\nsize : %i\nxml : %s", length, XML_content); ret = iphone_lckd_send(control, XML_content, length, &bytes); xmlFree(XML_content); XML_content = NULL; - plist_free(plist); - plist = NULL; + plist_free(dict); + dict = NULL; ret = iphone_lckd_recv(control, &XML_content, &bytes); log_debug_msg("Receive msg :\nsize : %i\nxml : %s", bytes, XML_content); - xml_to_plist(XML_content, bytes, &plist); + xml_to_plist(XML_content, bytes, &dict); - if (!plist) + if (!dict) return IPHONE_E_PLIST_ERROR; - plist_t query_node = find_query_node(plist, "Request", "QueryType"); + plist_t query_node = find_query_node(dict, "Request", "QueryType"); plist_t result_node = g_node_next_sibling(query_node); plist_t value_node = g_node_next_sibling(result_node); @@ -239,19 +236,18 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, char *r { if (!control || !req_key || !value || (value && *value)) return IPHONE_E_INVALID_ARG; - plist_t plist = NULL; - dict_t dict = NULL; + + plist_t dict = NULL; int bytes = 0, i = 0; char *XML_content = NULL; uint32_t length = 0; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; /* Setup DevicePublicKey request plist */ - plist_new_plist(&plist); - plist_new_dict_in_plist(plist, &dict); + plist_new_dict(&dict); plist_add_dict_element(dict, req_key, PLIST_STRING, (void *) req_string); plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "GetValue"); - plist_to_xml(plist, &XML_content, &length); + plist_to_xml(dict, &XML_content, &length); /* send to iPhone */ log_debug_msg("Send msg :\nsize : %i\nxml : %s", length, XML_content); @@ -259,8 +255,8 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, char *r xmlFree(XML_content); XML_content = NULL; - plist_free(plist); - plist = NULL; + plist_free(dict); + dict = NULL; if (ret != IPHONE_E_SUCCESS) return ret; @@ -272,11 +268,11 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, char *r if (ret != IPHONE_E_SUCCESS) return ret; - xml_to_plist(XML_content, bytes, &plist); - if (!plist) + xml_to_plist(XML_content, bytes, &dict); + if (!dict) return IPHONE_E_PLIST_ERROR; - plist_t query_node = find_query_node(plist, "Request", "GetValue"); + plist_t query_node = find_query_node(dict, "Request", "GetValue"); plist_t result_key_node = g_node_next_sibling(query_node); plist_t result_value_node = g_node_next_sibling(result_key_node); @@ -314,7 +310,7 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, char *r ret = IPHONE_E_SUCCESS; } - plist_free(plist); + plist_free(dict); free(XML_content); return ret; } @@ -408,9 +404,8 @@ iphone_error_t iphone_lckd_new_client(iphone_device_t device, iphone_lckd_client iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, char *host_id) { iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; - plist_t plist = NULL; - dict_t dict = NULL; - dict_t dict_record = NULL; + plist_t dict = NULL; + plist_t dict_record = NULL; int bytes = 0, i = 0; char *XML_content = NULL; uint32_t length = 0; @@ -433,8 +428,7 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch } /* Setup Pair request plist */ - plist_new_plist(&plist); - plist_new_dict_in_plist(plist, &dict); + plist_new_dict(&dict); plist_add_dict_element(dict, "PairRecord", PLIST_DICT, NULL); dict_record = g_node_last_child(dict); plist_add_dict_element(dict_record, "DeviceCertificate", PLIST_DATA, (void *) device_cert_b64); @@ -442,15 +436,15 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch plist_add_dict_element(dict_record, "HostID", PLIST_STRING, (void *) host_id); plist_add_dict_element(dict_record, "RootCertificate", PLIST_DATA, (void *) root_cert_b64); plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "Pair"); - plist_to_xml(plist, &XML_content, &length); + plist_to_xml(dict, &XML_content, &length); log_debug_msg("XML Pairing request :\nsize : %i\nxml :\n %s", length, XML_content); /* send to iPhone */ ret = iphone_lckd_send(control, XML_content, length, &bytes); xmlFree(XML_content); - plist_free(plist); - plist = NULL; + plist_free(dict); + dict = NULL; if (ret != IPHONE_E_SUCCESS) return ret; @@ -465,11 +459,11 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch log_debug_msg(XML_content); log_debug_msg("\n\n"); - xml_to_plist(XML_content, bytes, &plist); - if (!plist) + xml_to_plist(XML_content, bytes, &dict); + if (!dict) return IPHONE_E_PLIST_ERROR; - plist_t query_node = find_query_node(plist, "Request", "Pair"); + plist_t query_node = find_query_node(dict, "Request", "Pair"); plist_t result_key_node = g_node_next_sibling(query_node); plist_t result_value_node = g_node_next_sibling(result_key_node); @@ -635,27 +629,25 @@ iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_ */ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const char *HostID) { - plist_t plist = NULL; - dict_t dict = NULL; + plist_t dict = NULL; char *XML_content = NULL; uint32_t length = 0, bytes = 0, return_me = 0; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; /* Setup DevicePublicKey request plist */ - plist_new_plist(&plist); - plist_new_dict_in_plist(plist, &dict); + plist_new_dict(&dict); plist_add_dict_element(dict, "HostID", PLIST_STRING, (void *) HostID); plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "StartSession"); - plist_to_xml(plist, &XML_content, &length); + plist_to_xml(dict, &XML_content, &length); log_debug_msg("Send msg :\nsize : %i\nxml : %s", length, XML_content); ret = iphone_lckd_send(control, XML_content, length, &bytes); xmlFree(XML_content); XML_content = NULL; - plist_free(plist); - plist = NULL; + plist_free(dict); + dict = NULL; if (ret != IPHONE_E_SUCCESS) return ret; @@ -663,11 +655,11 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c if (bytes > 0) { ret = iphone_lckd_recv(control, &XML_content, &bytes); log_debug_msg("Receive msg :\nsize : %i\nxml : %s", bytes, XML_content); - xml_to_plist(XML_content, bytes, &plist); - if (!plist) + xml_to_plist(XML_content, bytes, &dict); + if (!dict) return IPHONE_E_PLIST_ERROR; - plist_t query_node = find_query_node(plist, "Request", "StartSession"); + plist_t query_node = find_query_node(dict, "Request", "StartSession"); plist_t result_key_node = g_node_next_sibling(query_node); plist_t result_value_node = g_node_next_sibling(result_key_node); @@ -681,8 +673,8 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c xmlFree(XML_content); XML_content = NULL; - plist_free(plist); - plist = NULL; + plist_free(dict); + dict = NULL; if (result_key_type == PLIST_KEY && result_value_type == PLIST_STRING && !strcmp(result_key, "Result") && !strcmp(result_value, "Success")) { @@ -871,8 +863,7 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char return IPHONE_E_SSL_ERROR; - plist_t plist = NULL; - dict_t dict = NULL; + plist_t dict = NULL; char *XML_content = NULL; uint32_t length, i = 0, port_loc = 0, bytes = 0; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; @@ -880,11 +871,10 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char free(host_id); host_id = NULL; - plist_new_plist(&plist); - plist_new_dict_in_plist(plist, &dict); + plist_new_dict(&dict); plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "StartService"); plist_add_dict_element(dict, "Service", PLIST_STRING, (void *) service); - plist_to_xml(plist, &XML_content, &length); + plist_to_xml(dict, &XML_content, &length); /* send to iPhone */ log_debug_msg("Send msg :\nsize : %i\nxml : %s", length, XML_content); @@ -892,8 +882,8 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char xmlFree(XML_content); XML_content = NULL; - plist_free(plist); - plist = NULL; + plist_free(dict); + dict = NULL; if (IPHONE_E_SUCCESS != ret) return ret; @@ -903,8 +893,8 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char if (IPHONE_E_SUCCESS != ret) return ret; - xml_to_plist(XML_content, bytes, &plist); - if (!plist) + xml_to_plist(XML_content, bytes, &dict); + if (!dict) return IPHONE_E_PLIST_ERROR; @@ -912,11 +902,11 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char return IPHONE_E_NOT_ENOUGH_DATA; else { - plist_t query_node = find_query_node(plist, "Request", "StartService"); + plist_t query_node = find_query_node(dict, "Request", "StartService"); plist_t result_key_node = g_node_next_sibling(query_node); plist_t result_value_node = g_node_next_sibling(result_key_node); - plist_t port_key_node = find_node(plist, PLIST_KEY, "Port"); + plist_t port_key_node = find_node(dict, PLIST_KEY, "Port"); plist_t port_value_node = g_node_next_sibling(port_key_node); plist_type result_key_type; @@ -947,7 +937,8 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char log_debug_msg("end data received by lockdownd_start_service()\n"); free(XML_content); - plist_free(plist); + plist_free(dict); + dict = NULL; if (port && ret == IPHONE_E_SUCCESS) { *port = port_loc; return IPHONE_E_SUCCESS; -- cgit v1.1-32-gdbae From 3d8ba053deeacd74e621469d3d45d1db38ee411a Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Fri, 12 Dec 2008 23:39:33 +0100 Subject: Change from Base64 encoded buffers to real buffers. Base64 decoding/encoding only happens in xml plists. --- src/lockdown.c | 143 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 81 insertions(+), 62 deletions(-) (limited to 'src/lockdown.c') diff --git a/src/lockdown.c b/src/lockdown.c index 4c96a7d..e882128 100644 --- a/src/lockdown.c +++ b/src/lockdown.c @@ -180,7 +180,7 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control) plist_t dict = NULL; plist_new_dict(&dict); - plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "QueryType"); + plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "QueryType", strlen("QueryType")); log_debug_msg("lockdownd_hello() called\n"); char *XML_content = NULL; @@ -190,7 +190,7 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control) log_debug_msg("Send msg :\nsize : %i\nxml : %s", length, XML_content); ret = iphone_lckd_send(control, XML_content, length, &bytes); - xmlFree(XML_content); + free(XML_content); XML_content = NULL; plist_free(dict); dict = NULL; @@ -211,9 +211,11 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control) char *result_value = NULL; char *value_value = NULL; + uint64_t result_length = 0; + uint64_t value_length = 0; - get_type_and_value(result_node, &result_type, (void *) (&result_value)); - get_type_and_value(value_node, &value_type, (void *) (&value_value)); + get_type_and_value(result_node, &result_type, (void *) (&result_value), &result_length); + get_type_and_value(value_node, &value_type, (void *) (&value_value), &value_length); if (result_type == PLIST_KEY && value_type == PLIST_STRING && !strcmp(result_value, "Result") && !strcmp(value_value, "Success")) { @@ -232,9 +234,10 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control) * * @return IPHONE_E_SUCCESS on success. */ -iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, char *req_key, char *req_string, char **value) +iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, char *req_key, char *req_string, + gnutls_datum_t * value) { - if (!control || !req_key || !value || (value && *value)) + if (!control || !req_key || !value || value->data) return IPHONE_E_INVALID_ARG; plist_t dict = NULL; @@ -245,15 +248,15 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, char *r /* Setup DevicePublicKey request plist */ plist_new_dict(&dict); - plist_add_dict_element(dict, req_key, PLIST_STRING, (void *) req_string); - plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "GetValue"); + plist_add_dict_element(dict, req_key, PLIST_STRING, (void *) req_string, strlen(req_string)); + plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "GetValue", strlen("GetValue")); plist_to_xml(dict, &XML_content, &length); /* send to iPhone */ log_debug_msg("Send msg :\nsize : %i\nxml : %s", length, XML_content); ret = iphone_lckd_send(control, XML_content, length, &bytes); - xmlFree(XML_content); + free(XML_content); XML_content = NULL; plist_free(dict); dict = NULL; @@ -280,9 +283,11 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, char *r plist_type result_value_type; char *result_key = NULL; char *result_value = NULL; + uint64_t result_length = 0; + uint64_t value_length = 0; - get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key)); - get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value)); + get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key), &result_length); + get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value), &value_length); if (result_key_type == PLIST_KEY && result_value_type == PLIST_STRING && !strcmp(result_key, "Result") && !strcmp(result_value, "Success")) { @@ -300,13 +305,16 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, char *r plist_type value_value_type; char *value_key = NULL; char *value_value = NULL; + uint64_t key_length = 0; + uint64_t valval_length = 0; - get_type_and_value(value_key_node, &value_key_type, (void *) (&value_key)); - get_type_and_value(value_value_node, &value_value_type, (void *) (&value_value)); + get_type_and_value(value_key_node, &value_key_type, (void *) (&value_key), &key_length); + get_type_and_value(value_value_node, &value_value_type, (void *) (&value_value), &valval_length); if (value_key_type == PLIST_KEY && !strcmp(result_key, "Value")) { log_debug_msg("lockdownd_generic_get_value(): success\n"); - *value = value_value; + value->data = value_value; + value->size = valval_length; ret = IPHONE_E_SUCCESS; } @@ -323,7 +331,9 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, char *r */ iphone_error_t lockdownd_get_device_uid(iphone_lckd_client_t control, char **uid) { - return lockdownd_generic_get_value(control, "Key", "UniqueDeviceID", uid); + gnutls_datum_t temp = { NULL, 0 }; + return lockdownd_generic_get_value(control, "Key", "UniqueDeviceID", &temp); + *uid = temp.data; } /** Askes for the device's public key. Part of the lockdownd handshake. @@ -332,7 +342,7 @@ iphone_error_t lockdownd_get_device_uid(iphone_lckd_client_t control, char **uid * * @return 1 on success and 0 on failure. */ -iphone_error_t lockdownd_get_device_public_key(iphone_lckd_client_t control, char **public_key) +iphone_error_t lockdownd_get_device_public_key(iphone_lckd_client_t control, gnutls_datum_t * public_key) { return lockdownd_generic_get_value(control, "Key", "DevicePublicKey", public_key); } @@ -410,39 +420,39 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch char *XML_content = NULL; uint32_t length = 0; - char *device_cert_b64 = NULL; - char *host_cert_b64 = NULL; - char *root_cert_b64 = NULL; - char *public_key_b64 = NULL; + gnutls_datum_t device_cert = { NULL, 0 }; + gnutls_datum_t host_cert = { NULL, 0 }; + gnutls_datum_t root_cert = { NULL, 0 }; + gnutls_datum_t public_key = { NULL, 0 }; - ret = lockdownd_get_device_public_key(control, &public_key_b64); + ret = lockdownd_get_device_public_key(control, &public_key); if (ret != IPHONE_E_SUCCESS) { fprintf(stderr, "Device refused to send public key.\n"); return ret; } - ret = lockdownd_gen_pair_cert(public_key_b64, &device_cert_b64, &host_cert_b64, &root_cert_b64); + ret = lockdownd_gen_pair_cert(public_key, &device_cert, &host_cert, &root_cert); if (ret != IPHONE_E_SUCCESS) { - free(public_key_b64); + free(public_key.data); return ret; } /* Setup Pair request plist */ plist_new_dict(&dict); - plist_add_dict_element(dict, "PairRecord", PLIST_DICT, NULL); + plist_add_dict_element(dict, "PairRecord", PLIST_DICT, NULL, 0); dict_record = g_node_last_child(dict); - plist_add_dict_element(dict_record, "DeviceCertificate", PLIST_DATA, (void *) device_cert_b64); - plist_add_dict_element(dict_record, "HostCertificate", PLIST_DATA, (void *) host_cert_b64); - plist_add_dict_element(dict_record, "HostID", PLIST_STRING, (void *) host_id); - plist_add_dict_element(dict_record, "RootCertificate", PLIST_DATA, (void *) root_cert_b64); - plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "Pair"); + plist_add_dict_element(dict_record, "DeviceCertificate", PLIST_DATA, (void *) device_cert.data, device_cert.size); + plist_add_dict_element(dict_record, "HostCertificate", PLIST_DATA, (void *) host_cert.data, host_cert.size); + plist_add_dict_element(dict_record, "HostID", PLIST_STRING, (void *) host_id, strlen(host_id)); + plist_add_dict_element(dict_record, "RootCertificate", PLIST_DATA, (void *) root_cert.data, root_cert.size); + plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "Pair", strlen("Pair")); plist_to_xml(dict, &XML_content, &length); log_debug_msg("XML Pairing request :\nsize : %i\nxml :\n %s", length, XML_content); /* send to iPhone */ ret = iphone_lckd_send(control, XML_content, length, &bytes); - xmlFree(XML_content); + free(XML_content); plist_free(dict); dict = NULL; @@ -471,9 +481,11 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch plist_type result_value_type; char *result_key = NULL; char *result_value = NULL; + uint64_t key_length = 0; + uint64_t val_length = 0; - get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key)); - get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value)); + get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key), &key_length); + get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value), &val_length); if (result_key_type == PLIST_KEY && result_value_type == PLIST_STRING && !strcmp(result_key, "Result") && !strcmp(result_value, "Success")) { @@ -483,13 +495,13 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch /* store public key in config if pairing succeeded */ if (ret == IPHONE_E_SUCCESS) { log_debug_msg("lockdownd_pair_device: pair success\n"); - store_device_public_key(uid, public_key_b64); + store_device_public_key(uid, public_key); ret = IPHONE_E_SUCCESS; } else { log_debug_msg("lockdownd_pair_device: pair failure\n"); ret = IPHONE_E_PAIRING_FAILED; } - free(public_key_b64); + free(public_key.data); return ret; } @@ -498,25 +510,19 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch * * @return IPHONE_E_SUCCESS on success. */ -iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_b64, char **host_cert_b64, - char **root_cert_b64) +iphone_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t * odevice_cert, + gnutls_datum_t * ohost_cert, gnutls_datum_t * oroot_cert) { - if (!public_key_b64 || !device_cert_b64 || !host_cert_b64 || !root_cert_b64) + if (!public_key.data || !odevice_cert || !ohost_cert || !oroot_cert) return IPHONE_E_INVALID_ARG; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; gnutls_datum_t modulus = { NULL, 0 }; gnutls_datum_t exponent = { NULL, 0 }; - /* first decode base64 public_key */ - gnutls_datum_t pem_pub_key; - gsize decoded_size; - pem_pub_key.data = g_base64_decode(public_key_b64, &decoded_size); - pem_pub_key.size = decoded_size; - /* now decode the PEM encoded key */ gnutls_datum_t der_pub_key; - if (GNUTLS_E_SUCCESS == gnutls_pem_base64_decode_alloc("RSA PUBLIC KEY", &pem_pub_key, &der_pub_key)) { + if (GNUTLS_E_SUCCESS == gnutls_pem_base64_decode_alloc("RSA PUBLIC KEY", &public_key, &der_pub_key)) { /* initalize asn.1 parser */ ASN1_TYPE pkcs1 = ASN1_TYPE_EMPTY; @@ -600,10 +606,18 @@ iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_ dev_pem.data = gnutls_malloc(dev_pem.size); gnutls_x509_crt_export(dev_cert, GNUTLS_X509_FMT_PEM, dev_pem.data, &dev_pem.size); - /* now encode certificates for output */ - *device_cert_b64 = g_base64_encode(dev_pem.data, dev_pem.size); - *host_cert_b64 = g_base64_encode(pem_host_cert.data, pem_host_cert.size); - *root_cert_b64 = g_base64_encode(pem_root_cert.data, pem_root_cert.size); + /* copy buffer for output */ + odevice_cert->data = malloc(dev_pem.size); + memcpy(odevice_cert->data, dev_pem.data, dev_pem.size); + odevice_cert->size = dev_pem.size; + + ohost_cert->data = malloc(pem_host_cert.size); + memcpy(ohost_cert->data, pem_host_cert.data, pem_host_cert.size); + ohost_cert->size = pem_host_cert.size; + + oroot_cert->data = malloc(pem_root_cert.size); + memcpy(oroot_cert->data, pem_root_cert.data, pem_root_cert.size); + oroot_cert->size = pem_root_cert.size; } gnutls_free(pem_root_priv.data); gnutls_free(pem_root_cert.data); @@ -615,7 +629,6 @@ iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_ gnutls_free(exponent.data); gnutls_free(der_pub_key.data); - g_free(pem_pub_key.data); return ret; } @@ -637,14 +650,14 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c /* Setup DevicePublicKey request plist */ plist_new_dict(&dict); - plist_add_dict_element(dict, "HostID", PLIST_STRING, (void *) HostID); - plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "StartSession"); + plist_add_dict_element(dict, "HostID", PLIST_STRING, (void *) HostID, strlen(HostID)); + plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "StartSession", strlen("StartSession")); plist_to_xml(dict, &XML_content, &length); log_debug_msg("Send msg :\nsize : %i\nxml : %s", length, XML_content); ret = iphone_lckd_send(control, XML_content, length, &bytes); - xmlFree(XML_content); + free(XML_content); XML_content = NULL; plist_free(dict); dict = NULL; @@ -667,11 +680,13 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c plist_type result_value_type; char *result_key = NULL; char *result_value = NULL; + uint64_t key_length = 0; + uint64_t val_length = 0; - get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key)); - get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value)); + get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key), &key_length); + get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value), &val_length); - xmlFree(XML_content); + free(XML_content); XML_content = NULL; plist_free(dict); dict = NULL; @@ -872,15 +887,15 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char host_id = NULL; plist_new_dict(&dict); - plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "StartService"); - plist_add_dict_element(dict, "Service", PLIST_STRING, (void *) service); + plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "StartService", strlen("StartService")); + plist_add_dict_element(dict, "Service", PLIST_STRING, (void *) service, strlen(service)); plist_to_xml(dict, &XML_content, &length); /* send to iPhone */ log_debug_msg("Send msg :\nsize : %i\nxml : %s", length, XML_content); ret = iphone_lckd_send(client, XML_content, length, &bytes); - xmlFree(XML_content); + free(XML_content); XML_content = NULL; plist_free(dict); dict = NULL; @@ -916,12 +931,16 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char char *result_key = NULL; char *result_value = NULL; char *port_key = NULL; + uint64_t res_key_length = 0; + uint64_t res_val_length = 0; + uint64_t port_key_length = 0; + uint64_t port_val_length = 0; uint64_t port_value = 0; - get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key)); - get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value)); - get_type_and_value(port_key_node, &port_key_type, (void *) (&port_key)); - get_type_and_value(port_value_node, &port_value_type, (void *) (&port_value)); + get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key), &res_key_length); + get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value), &res_val_length); + get_type_and_value(port_key_node, &port_key_type, (void *) (&port_key), &port_key_length); + get_type_and_value(port_value_node, &port_value_type, (void *) (&port_value), &port_val_length); if (result_key_type == PLIST_KEY && result_value_type == PLIST_STRING && -- cgit v1.1-32-gdbae From 4301ef9bb8e9d06ffa4e9172191d58ede5e16f5d Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Sat, 13 Dec 2008 18:12:46 +0100 Subject: fork out plist stuff in libplist and migrate libiphone to use it. --- src/lockdown.c | 130 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 71 insertions(+), 59 deletions(-) (limited to 'src/lockdown.c') diff --git a/src/lockdown.c b/src/lockdown.c index e882128..5b83fb9 100644 --- a/src/lockdown.c +++ b/src/lockdown.c @@ -30,6 +30,8 @@ #include #include +#include + const ASN1_ARRAY_TYPE pkcs1_asn1_tab[] = { {"PKCS1", 536872976, 0}, {0, 1073741836, 0}, @@ -177,10 +179,9 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control) int bytes = 0, i = 0; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; - plist_t dict = NULL; - plist_new_dict(&dict); - - plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "QueryType", strlen("QueryType")); + plist_t dict = plist_new_dict(); + plist_add_sub_element(dict, PLIST_KEY, (void *) "Request", strlen("Request")); + plist_add_sub_element(dict, PLIST_STRING, (void *) "QueryType", strlen("QueryType")); log_debug_msg("lockdownd_hello() called\n"); char *XML_content = NULL; @@ -197,14 +198,14 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control) ret = iphone_lckd_recv(control, &XML_content, &bytes); log_debug_msg("Receive msg :\nsize : %i\nxml : %s", bytes, XML_content); - xml_to_plist(XML_content, bytes, &dict); + plist_from_xml(XML_content, bytes, &dict); if (!dict) return IPHONE_E_PLIST_ERROR; - plist_t query_node = find_query_node(dict, "Request", "QueryType"); - plist_t result_node = g_node_next_sibling(query_node); - plist_t value_node = g_node_next_sibling(result_node); + plist_t query_node = plist_find_node(dict, PLIST_STRING, "QueryType", strlen("QueryType")); + plist_t result_node = plist_get_next_sibling(query_node); + plist_t value_node = plist_get_next_sibling(result_node); plist_type result_type; plist_type value_type; @@ -214,8 +215,8 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control) uint64_t result_length = 0; uint64_t value_length = 0; - get_type_and_value(result_node, &result_type, (void *) (&result_value), &result_length); - get_type_and_value(value_node, &value_type, (void *) (&value_value), &value_length); + plist_get_type_and_value(result_node, &result_type, (void *) (&result_value), &result_length); + plist_get_type_and_value(value_node, &value_type, (void *) (&value_value), &value_length); if (result_type == PLIST_KEY && value_type == PLIST_STRING && !strcmp(result_value, "Result") && !strcmp(value_value, "Success")) { @@ -247,9 +248,11 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, char *r iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; /* Setup DevicePublicKey request plist */ - plist_new_dict(&dict); - plist_add_dict_element(dict, req_key, PLIST_STRING, (void *) req_string, strlen(req_string)); - plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "GetValue", strlen("GetValue")); + dict = plist_new_dict(); + plist_add_sub_element(dict, PLIST_KEY, (void *) req_key, strlen(req_key)); + plist_add_sub_element(dict, PLIST_STRING, (void *) req_string, strlen(req_string)); + plist_add_sub_element(dict, PLIST_KEY, (void *) "Request", strlen("Request")); + plist_add_sub_element(dict, PLIST_STRING, (void *) "GetValue", strlen("GetValue")); plist_to_xml(dict, &XML_content, &length); /* send to iPhone */ @@ -271,13 +274,13 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, char *r if (ret != IPHONE_E_SUCCESS) return ret; - xml_to_plist(XML_content, bytes, &dict); + plist_from_xml(XML_content, bytes, &dict); if (!dict) return IPHONE_E_PLIST_ERROR; - plist_t query_node = find_query_node(dict, "Request", "GetValue"); - plist_t result_key_node = g_node_next_sibling(query_node); - plist_t result_value_node = g_node_next_sibling(result_key_node); + plist_t query_node = plist_find_node(dict, PLIST_STRING, "GetValue", strlen("GetValue")); + plist_t result_key_node = plist_get_next_sibling(query_node); + plist_t result_value_node = plist_get_next_sibling(result_key_node); plist_type result_key_type; plist_type result_value_type; @@ -286,8 +289,8 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, char *r uint64_t result_length = 0; uint64_t value_length = 0; - get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key), &result_length); - get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value), &value_length); + plist_get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key), &result_length); + plist_get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value), &value_length); if (result_key_type == PLIST_KEY && result_value_type == PLIST_STRING && !strcmp(result_key, "Result") && !strcmp(result_value, "Success")) { @@ -299,8 +302,8 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, char *r return IPHONE_E_DICT_ERROR; } - plist_t value_key_node = g_node_next_sibling(result_key_node); - plist_t value_value_node = g_node_next_sibling(value_key_node); + plist_t value_key_node = plist_get_next_sibling(result_key_node); + plist_t value_value_node = plist_get_next_sibling(value_key_node); plist_type value_key_type; plist_type value_value_type; char *value_key = NULL; @@ -308,8 +311,8 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, char *r uint64_t key_length = 0; uint64_t valval_length = 0; - get_type_and_value(value_key_node, &value_key_type, (void *) (&value_key), &key_length); - get_type_and_value(value_value_node, &value_value_type, (void *) (&value_value), &valval_length); + plist_get_type_and_value(value_key_node, &value_key_type, (void *) (&value_key), &key_length); + plist_get_type_and_value(value_value_node, &value_value_type, (void *) (&value_value), &valval_length); if (value_key_type == PLIST_KEY && !strcmp(result_key, "Value")) { log_debug_msg("lockdownd_generic_get_value(): success\n"); @@ -438,14 +441,19 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch } /* Setup Pair request plist */ - plist_new_dict(&dict); - plist_add_dict_element(dict, "PairRecord", PLIST_DICT, NULL, 0); - dict_record = g_node_last_child(dict); - plist_add_dict_element(dict_record, "DeviceCertificate", PLIST_DATA, (void *) device_cert.data, device_cert.size); - plist_add_dict_element(dict_record, "HostCertificate", PLIST_DATA, (void *) host_cert.data, host_cert.size); - plist_add_dict_element(dict_record, "HostID", PLIST_STRING, (void *) host_id, strlen(host_id)); - plist_add_dict_element(dict_record, "RootCertificate", PLIST_DATA, (void *) root_cert.data, root_cert.size); - plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "Pair", strlen("Pair")); + dict = plist_new_dict(); + plist_add_sub_element(dict, PLIST_KEY, (void *) "PairRecord", strlen("PairRecord")); + dict_record = plist_add_sub_element(dict, PLIST_DICT, NULL, 0); + plist_add_sub_element(dict_record, PLIST_KEY, (void *) "DeviceCertificate", strlen("DeviceCertificate")); + plist_add_sub_element(dict_record, PLIST_DATA, (void *) device_cert.data, device_cert.size); + plist_add_sub_element(dict_record, PLIST_KEY, (void *) "HostCertificate", strlen("HostCertificate")); + plist_add_sub_element(dict_record, PLIST_DATA, (void *) host_cert.data, host_cert.size); + plist_add_sub_element(dict_record, PLIST_KEY, (void *) "HostID", strlen("HostID")); + plist_add_sub_element(dict_record, PLIST_STRING, (void *) host_id, strlen(host_id)); + plist_add_sub_element(dict_record, PLIST_KEY, (void *) "RootCertificate", strlen("RootCertificate")); + plist_add_sub_element(dict_record, PLIST_DATA, (void *) root_cert.data, root_cert.size); + plist_add_sub_element(dict_record, PLIST_KEY, (void *) "Request", strlen("Request")); + plist_add_sub_element(dict_record, PLIST_STRING, (void *) "Pair", strlen("Pair")); plist_to_xml(dict, &XML_content, &length); log_debug_msg("XML Pairing request :\nsize : %i\nxml :\n %s", length, XML_content); @@ -469,13 +477,13 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch log_debug_msg(XML_content); log_debug_msg("\n\n"); - xml_to_plist(XML_content, bytes, &dict); + plist_from_xml(XML_content, bytes, &dict); if (!dict) return IPHONE_E_PLIST_ERROR; - plist_t query_node = find_query_node(dict, "Request", "Pair"); - plist_t result_key_node = g_node_next_sibling(query_node); - plist_t result_value_node = g_node_next_sibling(result_key_node); + plist_t query_node = plist_find_node(dict, PLIST_STRING, "Pair", strlen("Pair")); + plist_t result_key_node = plist_get_next_sibling(query_node); + plist_t result_value_node = plist_get_next_sibling(result_key_node); plist_type result_key_type; plist_type result_value_type; @@ -484,8 +492,8 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch uint64_t key_length = 0; uint64_t val_length = 0; - get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key), &key_length); - get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value), &val_length); + plist_get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key), &key_length); + plist_get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value), &val_length); if (result_key_type == PLIST_KEY && result_value_type == PLIST_STRING && !strcmp(result_key, "Result") && !strcmp(result_value, "Success")) { @@ -649,9 +657,11 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; /* Setup DevicePublicKey request plist */ - plist_new_dict(&dict); - plist_add_dict_element(dict, "HostID", PLIST_STRING, (void *) HostID, strlen(HostID)); - plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "StartSession", strlen("StartSession")); + dict = plist_new_dict(); + plist_add_sub_element(dict, PLIST_KEY, (void *) "HostID", strlen("HostID")); + plist_add_sub_element(dict, PLIST_STRING, (void *) HostID, strlen(HostID)); + plist_add_sub_element(dict, PLIST_KEY, (void *) "Request", strlen("Request")); + plist_add_sub_element(dict, PLIST_STRING, (void *) "StartSession", strlen("StartSession")); plist_to_xml(dict, &XML_content, &length); log_debug_msg("Send msg :\nsize : %i\nxml : %s", length, XML_content); @@ -668,13 +678,13 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c if (bytes > 0) { ret = iphone_lckd_recv(control, &XML_content, &bytes); log_debug_msg("Receive msg :\nsize : %i\nxml : %s", bytes, XML_content); - xml_to_plist(XML_content, bytes, &dict); + plist_from_xml(XML_content, bytes, &dict); if (!dict) return IPHONE_E_PLIST_ERROR; - plist_t query_node = find_query_node(dict, "Request", "StartSession"); - plist_t result_key_node = g_node_next_sibling(query_node); - plist_t result_value_node = g_node_next_sibling(result_key_node); + plist_t query_node = plist_find_node(dict, PLIST_STRING, "StartSession", strlen("StartSession")); + plist_t result_key_node = plist_get_next_sibling(query_node); + plist_t result_value_node = plist_get_next_sibling(result_key_node); plist_type result_key_type; plist_type result_value_type; @@ -683,8 +693,8 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c uint64_t key_length = 0; uint64_t val_length = 0; - get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key), &key_length); - get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value), &val_length); + plist_get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key), &key_length); + plist_get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value), &val_length); free(XML_content); XML_content = NULL; @@ -886,9 +896,11 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char free(host_id); host_id = NULL; - plist_new_dict(&dict); - plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "StartService", strlen("StartService")); - plist_add_dict_element(dict, "Service", PLIST_STRING, (void *) service, strlen(service)); + dict = plist_new_dict(); + plist_add_sub_element(dict, PLIST_KEY, (void *) "Request", strlen("Request")); + plist_add_sub_element(dict, PLIST_STRING, (void *) "StartService", strlen("StartService")); + plist_add_sub_element(dict, PLIST_KEY, (void *) "Service", strlen("Service")); + plist_add_sub_element(dict, PLIST_STRING, (void *) service, strlen(service)); plist_to_xml(dict, &XML_content, &length); /* send to iPhone */ @@ -908,7 +920,7 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char if (IPHONE_E_SUCCESS != ret) return ret; - xml_to_plist(XML_content, bytes, &dict); + plist_from_xml(XML_content, bytes, &dict); if (!dict) return IPHONE_E_PLIST_ERROR; @@ -917,12 +929,12 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char return IPHONE_E_NOT_ENOUGH_DATA; else { - plist_t query_node = find_query_node(dict, "Request", "StartService"); - plist_t result_key_node = g_node_next_sibling(query_node); - plist_t result_value_node = g_node_next_sibling(result_key_node); + plist_t query_node = plist_find_node(dict, PLIST_STRING, "StartService", strlen("StartService")); + plist_t result_key_node = plist_get_next_sibling(query_node); + plist_t result_value_node = plist_get_next_sibling(result_key_node); - plist_t port_key_node = find_node(dict, PLIST_KEY, "Port"); - plist_t port_value_node = g_node_next_sibling(port_key_node); + plist_t port_key_node = plist_find_node(dict, PLIST_KEY, "Port", strlen("Port")); + plist_t port_value_node = plist_get_next_sibling(port_key_node); plist_type result_key_type; plist_type result_value_type; @@ -937,10 +949,10 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char uint64_t port_val_length = 0; uint64_t port_value = 0; - get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key), &res_key_length); - get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value), &res_val_length); - get_type_and_value(port_key_node, &port_key_type, (void *) (&port_key), &port_key_length); - get_type_and_value(port_value_node, &port_value_type, (void *) (&port_value), &port_val_length); + plist_get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key), &res_key_length); + plist_get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value), &res_val_length); + plist_get_type_and_value(port_key_node, &port_key_type, (void *) (&port_key), &port_key_length); + plist_get_type_and_value(port_value_node, &port_value_type, (void *) (&port_value), &port_val_length); if (result_key_type == PLIST_KEY && result_value_type == PLIST_STRING && -- cgit v1.1-32-gdbae From ef98ef7211bc6277e9a87349f0405957ab264936 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Thu, 8 Jan 2009 18:17:21 +0100 Subject: Perform proper goodby on lockdown shutdown. --- src/lockdown.c | 178 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 172 insertions(+), 6 deletions(-) (limited to 'src/lockdown.c') diff --git a/src/lockdown.c b/src/lockdown.c index cf0d99e..ab168a3 100644 --- a/src/lockdown.c +++ b/src/lockdown.c @@ -92,6 +92,97 @@ iphone_lckd_client_t new_lockdownd_client(iphone_device_t phone) return control; } +/** + * Closes the lockdownd communication session, by sending + * the StopSession Request to the device. + * + * @param control The lockdown client + */ +static void iphone_lckd_stop_session(iphone_lckd_client_t control) +{ + if (!control) + return; // IPHONE_E_INVALID_ARG; + xmlDocPtr plist = new_plist(); + xmlNode *dict, *key; + char **dictionary; + int bytes = 0, i = 0; + iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; + + log_debug_msg("lockdownd_stop_session() called\n"); + dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); + key = add_key_str_dict_element(plist, dict, "Request", "StopSession", 1); + key = add_key_str_dict_element(plist, dict, "SessionID", control->session_id, 1); + + char *XML_content; + uint32 length; + + xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); + ret = iphone_lckd_send(control, XML_content, length, &bytes); + + xmlFree(XML_content); + xmlFreeDoc(plist); + plist = NULL; + ret = iphone_lckd_recv(control, &XML_content, &bytes); + + plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0); + if (!plist) { + fprintf(stderr, "lockdownd_stop_session(): IPHONE_E_PLIST_ERROR\n"); + return; //IPHONE_E_PLIST_ERROR; + } + dict = xmlDocGetRootElement(plist); + for (dict = dict->children; dict; dict = dict->next) { + if (!xmlStrcmp(dict->name, "dict")) + break; + } + if (!dict) { + fprintf(stderr, "lockdownd_stop_session(): IPHONE_E_DICT_ERROR\n"); + return; //IPHONE_E_DICT_ERROR; + } + dictionary = read_dict_element_strings(dict); + xmlFreeDoc(plist); + free(XML_content); + + for (i = 0; dictionary[i]; i += 2) { + if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { + log_debug_msg("lockdownd_stop_session(): success\n"); + ret = IPHONE_E_SUCCESS; + break; + } + } + + free_dictionary(dictionary); + return; //ret; +} + +/** + * Shuts down the SSL session by first calling iphone_lckd_stop_session + * to cleanly close the lockdownd communication session, and then + * performing a close notify, which is done by "gnutls_bye". + * + * @param client The lockdown client + */ +static void iphone_lckd_stop_SSL_session(iphone_lckd_client_t client) +{ + if (!client) { + log_debug_msg("lockdownd_stop_SSL_session(): invalid argument!\n"); + return; + } + + if (client->in_SSL) { + log_debug_msg("Stopping SSL Session\n"); + iphone_lckd_stop_session(client); + log_debug_msg("Sending SSL close notify\n"); + gnutls_bye(*client->ssl_session, GNUTLS_SHUT_RDWR); + } + if (client->ssl_session) { + gnutls_deinit(*client->ssl_session); + free(client->ssl_session); + } + client->in_SSL = 0; + client->gtls_buffer_hack_len = 0; // dunno if required?! + + return; +} /** Closes the lockdownd client and does the necessary housekeeping. * @@ -103,13 +194,17 @@ iphone_error_t iphone_lckd_free_client(iphone_lckd_client_t client) return IPHONE_E_INVALID_ARG; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; + iphone_lckd_stop_SSL_session(client); + if (client->connection) { + lockdownd_close(client); + + // IMO, read of final "sessionUpcall connection closed" packet + // should come here instead of in iphone_free_device + ret = iphone_mux_free_client(client->connection); } - if (client->ssl_session) - gnutls_deinit(*client->ssl_session); - free(client->ssl_session); free(client); return ret; } @@ -520,6 +615,66 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch return ret; } +/** + * Performs the Goodbye Request to tell the device the communication + * session is now closed. + * + * @param control The lockdown client + */ +void lockdownd_close(iphone_lckd_client_t control) +{ + if (!control) + return; // IPHONE_E_INVALID_ARG; + xmlDocPtr plist = new_plist(); + xmlNode *dict, *key; + char **dictionary; + int bytes = 0, i = 0; + iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; + + log_debug_msg("lockdownd_close() called\n"); + dict = add_child_to_plist(plist, "dict", "\n", NULL, 0); + key = add_key_str_dict_element(plist, dict, "Request", "Goodbye", 1); + char *XML_content; + uint32 length; + + xmlDocDumpMemory(plist, (xmlChar **) & XML_content, &length); + ret = iphone_lckd_send(control, XML_content, length, &bytes); + + xmlFree(XML_content); + xmlFreeDoc(plist); + plist = NULL; + ret = iphone_lckd_recv(control, &XML_content, &bytes); + + plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0); + if (!plist) { + fprintf(stderr, "lockdownd_close(): IPHONE_E_PLIST_ERROR\n"); + return; //IPHONE_E_PLIST_ERROR; + } + dict = xmlDocGetRootElement(plist); + for (dict = dict->children; dict; dict = dict->next) { + if (!xmlStrcmp(dict->name, "dict")) + break; + } + if (!dict) { + fprintf(stderr, "lockdownd_close(): IPHONE_E_DICT_ERROR\n"); + return; //IPHONE_E_DICT_ERROR; + } + dictionary = read_dict_element_strings(dict); + xmlFreeDoc(plist); + free(XML_content); + + for (i = 0; dictionary[i]; i += 2) { + if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { + log_debug_msg("lockdownd_close(): success\n"); + ret = IPHONE_E_SUCCESS; + break; + } + } + + free_dictionary(dictionary); + return; //ret; +} + /** Generates the device certificate from the public key as well as the host * and root certificates. * @@ -664,6 +819,8 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; // end variables + control->session_id[0] = '\0'; + key = add_key_str_dict_element(plist, dict, "HostID", HostID, 1); if (!key) { log_debug_msg("Couldn't add a key.\n"); @@ -699,6 +856,7 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c dictionary = read_dict_element_strings(dict); xmlFreeDoc(plist); free(what2send); + ret = IPHONE_E_SSL_ERROR; for (i = 0; dictionary[i]; i += 2) { if (!strcmp(dictionary[i], "Result") && !strcmp(dictionary[i + 1], "Success")) { // Set up GnuTLS... @@ -741,8 +899,6 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c return_me = gnutls_handshake(*control->ssl_session); log_debug_msg("GnuTLS handshake done...\n"); - free_dictionary(dictionary); - if (return_me != GNUTLS_E_SUCCESS) { log_debug_msg("GnuTLS reported something wrong.\n"); gnutls_perror(return_me); @@ -750,10 +906,20 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c return IPHONE_E_SSL_ERROR; } else { control->in_SSL = 1; - return IPHONE_E_SUCCESS; + ret = IPHONE_E_SUCCESS; } + } else if (!strcmp(dictionary[i], "SessionID")) { + // we need to store the session ID for StopSession + strcpy(control->session_id, dictionary[i + 1]); + log_debug_msg("SessionID: %s\n", control->session_id); + free_dictionary(dictionary); + return ret; } } + if (ret == IPHONE_E_SUCCESS) { + log_debug_msg("Failed to get SessionID!\n"); + return ret; + } log_debug_msg("Apparently failed negotiating with lockdownd.\n"); log_debug_msg("Responding dictionary: \n"); -- cgit v1.1-32-gdbae From 8f239549c124d11eb8899aec6c048d6a496e3911 Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Thu, 8 Jan 2009 23:27:28 +0100 Subject: Implement skeleton of MobileSync protocol (handshake and goodbye). --- src/lockdown.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'src/lockdown.c') diff --git a/src/lockdown.c b/src/lockdown.c index 872b7b0..56a6f4e 100644 --- a/src/lockdown.c +++ b/src/lockdown.c @@ -845,6 +845,8 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c ret = iphone_lckd_recv(control, &XML_content, &bytes); log_debug_msg("Receive msg :\nsize : %i\nxml : %s", bytes, XML_content); plist_from_xml(XML_content, bytes, &dict); + free(XML_content); + XML_content = NULL; if (!dict) return IPHONE_E_PLIST_ERROR; @@ -862,10 +864,6 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c plist_get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key), &key_length); plist_get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value), &val_length); - free(XML_content); - XML_content = NULL; - plist_free(dict); - dict = NULL; ret = IPHONE_E_SSL_ERROR; if (result_key_type == PLIST_KEY && result_value_type == PLIST_STRING && !strcmp(result_key, "Result") && !strcmp(result_value, "Success")) { @@ -934,14 +932,14 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c // we need to store the session ID for StopSession strcpy(control->session_id, session_id); log_debug_msg("SessionID: %s\n", control->session_id); - return ret; } - } - - if (ret == IPHONE_E_SUCCESS) { + } else log_debug_msg("Failed to get SessionID!\n"); + plist_free(dict); + dict = NULL; + + if (ret == IPHONE_E_SUCCESS) return ret; - } log_debug_msg("Apparently failed negotiating with lockdownd.\n"); return IPHONE_E_SSL_ERROR; -- cgit v1.1-32-gdbae From 564aebf941f2f0c5fb57d2f86091b37d6331b9d9 Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Mon, 12 Jan 2009 20:07:06 +0100 Subject: Update lockdownd transfer function to take abstract plist as argument. Simplify code accordingly. Use new libplist API to make code cleaner. --- src/lockdown.c | 617 +++++++++++++++++++++++++++------------------------------ 1 file changed, 287 insertions(+), 330 deletions(-) (limited to 'src/lockdown.c') diff --git a/src/lockdown.c b/src/lockdown.c index 56a6f4e..b83b8cf 100644 --- a/src/lockdown.c +++ b/src/lockdown.c @@ -33,6 +33,7 @@ #include + const ASN1_ARRAY_TYPE pkcs1_asn1_tab[] = { {"PKCS1", 536872976, 0}, {0, 1073741836, 0}, @@ -80,53 +81,49 @@ static void iphone_lckd_stop_session(iphone_lckd_client_t control) iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; plist_t dict = plist_new_dict(); - plist_add_sub_element(dict, PLIST_KEY, (void *) "Request", strlen("Request")); - plist_add_sub_element(dict, PLIST_STRING, (void *) "StopSession", strlen("StopSession")); - plist_add_sub_element(dict, PLIST_KEY, (void *) "SessionID", strlen("SessionID")); - plist_add_sub_element(dict, PLIST_STRING, (void *) control->session_id, strlen(control->session_id)); + plist_add_sub_key_el(dict, "Request"); + plist_add_sub_string_el(dict, "StopSession"); + plist_add_sub_key_el(dict, "SessionID"); + plist_add_sub_string_el(dict, control->session_id); - log_debug_msg("iphone_lckd_stop_session() called\n"); - char *XML_content = NULL; - uint32_t length = 0; + log_dbg_msg(DBGMASK_LOCKDOWND, "iphone_lckd_stop_session() called\n"); - plist_to_xml(dict, &XML_content, &length); - log_debug_msg("Send msg :\nsize : %i\nxml : %s", length, XML_content); - ret = iphone_lckd_send(control, XML_content, length, &bytes); + ret = iphone_lckd_send(control, dict); - free(XML_content); - XML_content = NULL; plist_free(dict); dict = NULL; - ret = iphone_lckd_recv(control, &XML_content, &bytes); - log_debug_msg("Receive msg :\nsize : %i\nxml : %s", bytes, XML_content); - plist_from_xml(XML_content, bytes, &dict); + ret = iphone_lckd_recv(control, &dict); if (!dict) { - log_debug_msg("lockdownd_stop_session(): IPHONE_E_PLIST_ERROR\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_stop_session(): IPHONE_E_PLIST_ERROR\n"); return; // IPHONE_E_PLIST_ERROR; } - plist_t query_node = plist_find_node(dict, PLIST_STRING, "StopSession", strlen("StopSession")); + plist_t query_node = plist_find_node_by_string(dict, "StopSession"); plist_t result_node = plist_get_next_sibling(query_node); plist_t value_node = plist_get_next_sibling(result_node); - plist_type result_type; - plist_type value_type; + plist_type result_type = plist_get_node_type(result_node); + plist_type value_type = plist_get_node_type(value_node); + + if (result_type == PLIST_KEY && value_type == PLIST_STRING) { - char *result_value = NULL; - char *value_value = NULL; - uint64_t result_length = 0; - uint64_t value_length = 0; + char *result_value = NULL; + char *value_value = NULL; - plist_get_type_and_value(result_node, &result_type, (void *) (&result_value), &result_length); - plist_get_type_and_value(value_node, &value_type, (void *) (&value_value), &value_length); + plist_get_key_val(result_node, &result_value); + plist_get_string_val(value_node, &value_value); - if (result_type == PLIST_KEY && - value_type == PLIST_STRING && !strcmp(result_value, "Result") && !strcmp(value_value, "Success")) { - log_debug_msg("lockdownd_stop_session(): success\n"); - ret = IPHONE_E_SUCCESS; + if (!strcmp(result_value, "Result") && !strcmp(value_value, "Success")) { + log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_stop_session(): success\n"); + ret = IPHONE_E_SUCCESS; + } + free(result_value); + free(value_value); } + plist_free(dict); + dict = NULL; return; // ret; } @@ -142,14 +139,14 @@ static void iphone_lckd_stop_session(iphone_lckd_client_t control) static void iphone_lckd_stop_SSL_session(iphone_lckd_client_t client) { if (!client) { - log_debug_msg("lockdownd_stop_SSL_session(): invalid argument!\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_stop_SSL_session(): invalid argument!\n"); return; } if (client->in_SSL) { - log_debug_msg("Stopping SSL Session\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "Stopping SSL Session\n"); iphone_lckd_stop_session(client); - log_debug_msg("Sending SSL close notify\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "Sending SSL close notify\n"); gnutls_bye(*client->ssl_session, GNUTLS_SHUT_RDWR); } if (client->ssl_session) { @@ -196,9 +193,9 @@ iphone_error_t iphone_lckd_free_client(iphone_lckd_client_t client) * * @return The number of bytes received */ -iphone_error_t iphone_lckd_recv(iphone_lckd_client_t client, char **dump_data, uint32_t * recv_bytes) +iphone_error_t iphone_lckd_recv(iphone_lckd_client_t client, plist_t * plist) { - if (!client || !dump_data || !recv_bytes) + if (!client || !plist || (plist && *plist)) return IPHONE_E_INVALID_ARG; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; char *receive; @@ -221,8 +218,18 @@ iphone_error_t iphone_lckd_recv(iphone_lckd_client_t client, char **dump_data, u if (bytes > 0) ret = IPHONE_E_SUCCESS; } - *dump_data = receive; - *recv_bytes = bytes; + + if (bytes <= 0) { + free(receive); + return IPHONE_E_NOT_ENOUGH_DATA; + } + + plist_from_xml(receive, bytes, plist); + free(receive); + + if (!*plist) + ret = IPHONE_E_PLIST_ERROR; + return ret; } @@ -231,26 +238,31 @@ iphone_error_t iphone_lckd_recv(iphone_lckd_client_t client, char **dump_data, u * @note This function is low-level and should only be used if you need to send * a new type of message. * - * @param control The lockdownd client - * @param raw_data The null terminated string buffer to send - * @param length The length of data to send + * @param client The lockdownd client + * @param plist The plist to send * - * @return The number of bytes sent + * @return an error code (IPHONE_E_SUCCESS on success) */ -iphone_error_t iphone_lckd_send(iphone_lckd_client_t client, char *raw_data, uint32_t length, uint32_t * sent_bytes) +iphone_error_t iphone_lckd_send(iphone_lckd_client_t client, plist_t plist) { - if (!client || !raw_data || length == 0 || !sent_bytes) + if (!client || !plist) return IPHONE_E_INVALID_ARG; char *real_query; int bytes; + char *XMLContent = NULL; + uint32_t length = 0; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; + plist_to_xml(plist, &XMLContent, &length); + log_dbg_msg(DBGMASK_LOCKDOWND, "Send msg :\nsize : %i\nbuffer :\n%s\n", length, XMLContent); + + real_query = (char *) malloc(sizeof(char) * (length + 4)); length = htonl(length); memcpy(real_query, &length, sizeof(length)); - memcpy(real_query + 4, raw_data, ntohl(length)); - log_debug_msg("lockdownd_send(): made the query, sending it along\n"); - dump_debug_buffer("grpkt", real_query, ntohl(length) + 4); + memcpy(real_query + 4, XMLContent, ntohl(length)); + free(XMLContent); + log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_send(): made the query, sending it along\n"); if (!client->in_SSL) ret = iphone_mux_send(client->connection, real_query, ntohl(length) + sizeof(length), &bytes); @@ -258,9 +270,9 @@ iphone_error_t iphone_lckd_send(iphone_lckd_client_t client, char *raw_data, uin gnutls_record_send(*client->ssl_session, real_query, ntohl(length) + sizeof(length)); ret = IPHONE_E_SUCCESS; } - log_debug_msg("lockdownd_send(): sent it!\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_send(): sent it!\n"); free(real_query); - *sent_bytes = bytes; + return ret; } @@ -277,54 +289,49 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control) if (!control) return IPHONE_E_INVALID_ARG; - int bytes = 0, i = 0; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; plist_t dict = plist_new_dict(); - plist_add_sub_element(dict, PLIST_KEY, (void *) "Request", strlen("Request")); - plist_add_sub_element(dict, PLIST_STRING, (void *) "QueryType", strlen("QueryType")); + plist_add_sub_key_el(dict, "Request"); + plist_add_sub_string_el(dict, "QueryType"); - log_debug_msg("lockdownd_hello() called\n"); - char *XML_content = NULL; - uint32_t length = 0; - - plist_to_xml(dict, &XML_content, &length); - log_debug_msg("Send msg :\nsize : %i\nxml : %s", length, XML_content); - ret = iphone_lckd_send(control, XML_content, length, &bytes); + log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_hello() called\n"); + ret = iphone_lckd_send(control, dict); - free(XML_content); - XML_content = NULL; plist_free(dict); dict = NULL; - ret = iphone_lckd_recv(control, &XML_content, &bytes); - log_debug_msg("Receive msg :\nsize : %i\nxml : %s", bytes, XML_content); - plist_from_xml(XML_content, bytes, &dict); + ret = iphone_lckd_recv(control, &dict); - if (!dict) - return IPHONE_E_PLIST_ERROR; + if (IPHONE_E_SUCCESS != ret) + return ret; - plist_t query_node = plist_find_node(dict, PLIST_STRING, "QueryType", strlen("QueryType")); + plist_t query_node = plist_find_node_by_string(dict, "QueryType"); plist_t result_node = plist_get_next_sibling(query_node); plist_t value_node = plist_get_next_sibling(result_node); - plist_type result_type; - plist_type value_type; + plist_type result_type = plist_get_node_type(result_node); + plist_type value_type = plist_get_node_type(value_node); + + if (result_type == PLIST_KEY && value_type == PLIST_STRING) { - char *result_value = NULL; - char *value_value = NULL; - uint64_t result_length = 0; - uint64_t value_length = 0; + char *result_value = NULL; + char *value_value = NULL; - plist_get_type_and_value(result_node, &result_type, (void *) (&result_value), &result_length); - plist_get_type_and_value(value_node, &value_type, (void *) (&value_value), &value_length); + plist_get_key_val(result_node, &result_value); + plist_get_string_val(value_node, &value_value); - if (result_type == PLIST_KEY && - value_type == PLIST_STRING && !strcmp(result_value, "Result") && !strcmp(value_value, "Success")) { - log_debug_msg("lockdownd_hello(): success\n"); - ret = IPHONE_E_SUCCESS; + if (!strcmp(result_value, "Result") && !strcmp(value_value, "Success")) { + log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_hello(): success\n"); + ret = IPHONE_E_SUCCESS; + } + free(result_value); + free(value_value); } + plist_free(dict); + dict = NULL; + return ret; } @@ -343,25 +350,18 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, const c return IPHONE_E_INVALID_ARG; plist_t dict = NULL; - int bytes = 0, i = 0; - char *XML_content = NULL; - uint32_t length = 0; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; /* Setup DevicePublicKey request plist */ dict = plist_new_dict(); - plist_add_sub_element(dict, PLIST_KEY, (void *) req_key, strlen(req_key)); - plist_add_sub_element(dict, PLIST_STRING, (void *) req_string, strlen(req_string)); - plist_add_sub_element(dict, PLIST_KEY, (void *) "Request", strlen("Request")); - plist_add_sub_element(dict, PLIST_STRING, (void *) "GetValue", strlen("GetValue")); - plist_to_xml(dict, &XML_content, &length); + plist_add_sub_key_el(dict, req_key); + plist_add_sub_string_el(dict, req_string); + plist_add_sub_key_el(dict, "Request"); + plist_add_sub_string_el(dict, "GetValue"); /* send to iPhone */ - log_debug_msg("Send msg :\nsize : %i\nxml : %s", length, XML_content); - ret = iphone_lckd_send(control, XML_content, length, &bytes); + ret = iphone_lckd_send(control, dict); - free(XML_content); - XML_content = NULL; plist_free(dict); dict = NULL; @@ -369,61 +369,65 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, const c return ret; /* Now get iPhone's answer */ - ret = iphone_lckd_recv(control, &XML_content, &bytes); - log_debug_msg("Receive msg :\nsize : %i\nxml : %s", bytes, XML_content); + ret = iphone_lckd_recv(control, &dict); if (ret != IPHONE_E_SUCCESS) return ret; - plist_from_xml(XML_content, bytes, &dict); - if (!dict) - return IPHONE_E_PLIST_ERROR; - - plist_t query_node = plist_find_node(dict, PLIST_STRING, "GetValue", strlen("GetValue")); + plist_t query_node = plist_find_node_by_string(dict, "GetValue"); plist_t result_key_node = plist_get_next_sibling(query_node); plist_t result_value_node = plist_get_next_sibling(result_key_node); - plist_type result_key_type; - plist_type result_value_type; - char *result_key = NULL; - char *result_value = NULL; - uint64_t result_length = 0; - uint64_t value_length = 0; + plist_type result_key_type = plist_get_node_type(result_key_node); + plist_type result_value_type = plist_get_node_type(result_value_node); - plist_get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key), &result_length); - plist_get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value), &value_length); + if (result_key_type == PLIST_KEY && result_value_type == PLIST_STRING) { - if (result_key_type == PLIST_KEY && - result_value_type == PLIST_STRING && !strcmp(result_key, "Result") && !strcmp(result_value, "Success")) { - log_debug_msg("lockdownd_generic_get_value(): success\n"); - ret = IPHONE_E_SUCCESS; - } + char *result_key = NULL; + char *result_value = NULL; + ret = IPHONE_E_DICT_ERROR; + + plist_get_key_val(result_key_node, &result_key); + plist_get_string_val(result_value_node, &result_value); + if (!strcmp(result_key, "Result") && !strcmp(result_value, "Success")) { + log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_generic_get_value(): success\n"); + ret = IPHONE_E_SUCCESS; + } + free(result_key); + free(result_value); + } if (ret != IPHONE_E_SUCCESS) { - return IPHONE_E_DICT_ERROR; + return ret; } plist_t value_key_node = plist_get_next_sibling(result_key_node); plist_t value_value_node = plist_get_next_sibling(value_key_node); - plist_type value_key_type; - plist_type value_value_type; - char *value_key = NULL; - char *value_value = NULL; - uint64_t key_length = 0; - uint64_t valval_length = 0; - - plist_get_type_and_value(value_key_node, &value_key_type, (void *) (&value_key), &key_length); - plist_get_type_and_value(value_value_node, &value_value_type, (void *) (&value_value), &valval_length); - - if (value_key_type == PLIST_KEY && !strcmp(result_key, "Value")) { - log_debug_msg("lockdownd_generic_get_value(): success\n"); - value->data = value_value; - value->size = valval_length; - ret = IPHONE_E_SUCCESS; + + plist_type value_key_type = plist_get_node_type(value_key_node); + + if (value_key_type == PLIST_KEY) { + + char *result_key = NULL; + plist_get_key_val(value_key_node, &result_key); + + if (!strcmp(result_key, "Value")) { + log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_generic_get_value(): success\n"); + + plist_type value_value_type; + char *value_value = NULL; + uint64_t valval_length = 0; + + plist_get_type_and_value(value_value_node, &value_value_type, (void *) (&value_value), &valval_length); + + value->data = value_value; + value->size = valval_length; + ret = IPHONE_E_SUCCESS; + } + free(result_key); } plist_free(dict); - free(XML_content); return ret; } @@ -520,9 +524,6 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; plist_t dict = NULL; plist_t dict_record = NULL; - int bytes = 0, i = 0; - char *XML_content = NULL; - uint32_t length = 0; gnutls_datum_t device_cert = { NULL, 0 }; gnutls_datum_t host_cert = { NULL, 0 }; @@ -543,25 +544,22 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch /* Setup Pair request plist */ dict = plist_new_dict(); - plist_add_sub_element(dict, PLIST_KEY, (void *) "PairRecord", strlen("PairRecord")); - dict_record = plist_add_sub_element(dict, PLIST_DICT, NULL, 0); - plist_add_sub_element(dict_record, PLIST_KEY, (void *) "DeviceCertificate", strlen("DeviceCertificate")); - plist_add_sub_element(dict_record, PLIST_DATA, (void *) device_cert.data, device_cert.size); - plist_add_sub_element(dict_record, PLIST_KEY, (void *) "HostCertificate", strlen("HostCertificate")); - plist_add_sub_element(dict_record, PLIST_DATA, (void *) host_cert.data, host_cert.size); - plist_add_sub_element(dict_record, PLIST_KEY, (void *) "HostID", strlen("HostID")); - plist_add_sub_element(dict_record, PLIST_STRING, (void *) host_id, strlen(host_id)); - plist_add_sub_element(dict_record, PLIST_KEY, (void *) "RootCertificate", strlen("RootCertificate")); - plist_add_sub_element(dict_record, PLIST_DATA, (void *) root_cert.data, root_cert.size); - plist_add_sub_element(dict_record, PLIST_KEY, (void *) "Request", strlen("Request")); - plist_add_sub_element(dict_record, PLIST_STRING, (void *) "Pair", strlen("Pair")); - plist_to_xml(dict, &XML_content, &length); - log_debug_msg("XML Pairing request :\nsize : %i\nxml :\n %s", length, XML_content); + plist_add_sub_key_el(dict, "PairRecord"); + dict_record = plist_new_dict(); + plist_add_sub_node(dict, dict_record); + plist_add_sub_key_el(dict_record, "DeviceCertificate"); + plist_add_sub_data_el(dict_record, device_cert.data, device_cert.size); + plist_add_sub_key_el(dict_record, "HostCertificate"); + plist_add_sub_data_el(dict_record, host_cert.data, host_cert.size); + plist_add_sub_key_el(dict_record, "HostID"); + plist_add_sub_string_el(dict_record, host_id); + plist_add_sub_key_el(dict_record, "RootCertificate"); + plist_add_sub_data_el(dict_record, root_cert.data, root_cert.size); + plist_add_sub_key_el(dict_record, "Request"); + plist_add_sub_string_el(dict_record, "Pair"); /* send to iPhone */ - ret = iphone_lckd_send(control, XML_content, length, &bytes); - - free(XML_content); + ret = iphone_lckd_send(control, dict); plist_free(dict); dict = NULL; @@ -569,45 +567,43 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch return ret; /* Now get iPhone's answer */ - ret = iphone_lckd_recv(control, &XML_content, &bytes); + ret = iphone_lckd_recv(control, &dict); if (ret != IPHONE_E_SUCCESS) return ret; - log_debug_msg("lockdown_pair_device: iPhone's response to our pair request:\n"); - log_debug_msg(XML_content); - log_debug_msg("\n\n"); - - plist_from_xml(XML_content, bytes, &dict); - if (!dict) - return IPHONE_E_PLIST_ERROR; - - plist_t query_node = plist_find_node(dict, PLIST_STRING, "Pair", strlen("Pair")); + plist_t query_node = plist_find_node_by_string(dict, "Pair"); plist_t result_key_node = plist_get_next_sibling(query_node); plist_t result_value_node = plist_get_next_sibling(result_key_node); - plist_type result_key_type; - plist_type result_value_type; - char *result_key = NULL; - char *result_value = NULL; - uint64_t key_length = 0; - uint64_t val_length = 0; + plist_type result_key_type = plist_get_node_type(result_key_node); + plist_type result_value_type = plist_get_node_type(result_value_node); - plist_get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key), &key_length); - plist_get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value), &val_length); + if (result_key_type == PLIST_KEY && result_value_type == PLIST_STRING) { - if (result_key_type == PLIST_KEY && - result_value_type == PLIST_STRING && !strcmp(result_key, "Result") && !strcmp(result_value, "Success")) { - ret = IPHONE_E_SUCCESS; + char *result_key = NULL; + char *result_value = NULL; + + plist_get_key_val(result_key_node, &result_key); + plist_get_string_val(result_value_node, &result_value); + + if (!strcmp(result_key, "Result") && !strcmp(result_value, "Success")) { + ret = IPHONE_E_SUCCESS; + } + + free(result_key); + free(result_value); } + plist_free(dict); + dict = NULL; /* store public key in config if pairing succeeded */ if (ret == IPHONE_E_SUCCESS) { - log_debug_msg("lockdownd_pair_device: pair success\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_pair_device: pair success\n"); store_device_public_key(uid, public_key); ret = IPHONE_E_SUCCESS; } else { - log_debug_msg("lockdownd_pair_device: pair failure\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_pair_device: pair failure\n"); ret = IPHONE_E_PAIRING_FAILED; } free(public_key.data); @@ -625,56 +621,48 @@ void lockdownd_close(iphone_lckd_client_t control) if (!control) return; //IPHONE_E_INVALID_ARG; - int bytes = 0, i = 0; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; plist_t dict = plist_new_dict(); - plist_add_sub_element(dict, PLIST_KEY, (void *) "Request", strlen("Request")); - plist_add_sub_element(dict, PLIST_STRING, (void *) "Goodbye", strlen("Goodbye")); - - log_debug_msg("lockdownd_close() called\n"); - char *XML_content = NULL; - uint32_t length = 0; + plist_add_sub_key_el(dict, "Request"); + plist_add_sub_string_el(dict, "Goodbye"); - plist_to_xml(dict, &XML_content, &length); - log_debug_msg("Send msg :\nsize : %i\nxml : %s", length, XML_content); - ret = iphone_lckd_send(control, XML_content, length, &bytes); + log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_close() called\n"); - free(XML_content); - XML_content = NULL; + ret = iphone_lckd_send(control, dict); plist_free(dict); dict = NULL; - ret = iphone_lckd_recv(control, &XML_content, &bytes); - log_debug_msg("Receive msg :\nsize : %i\nxml : %s", bytes, XML_content); - plist_from_xml(XML_content, bytes, &dict); + ret = iphone_lckd_recv(control, &dict); if (!dict) { - log_debug_msg("lockdownd_close(): IPHONE_E_PLIST_ERROR\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_close(): IPHONE_E_PLIST_ERROR\n"); return; // IPHONE_E_PLIST_ERROR; } - plist_t query_node = plist_find_node(dict, PLIST_STRING, "Goodbye", strlen("Goodbye")); + plist_t query_node = plist_find_node_by_string(dict, "Goodbye"); plist_t result_node = plist_get_next_sibling(query_node); plist_t value_node = plist_get_next_sibling(result_node); - plist_type result_type; - plist_type value_type; + plist_type result_type = plist_get_node_type(result_node); + plist_type value_type = plist_get_node_type(value_node); - char *result_value = NULL; - char *value_value = NULL; - uint64_t result_length = 0; - uint64_t value_length = 0; + if (result_type == PLIST_KEY && value_type == PLIST_STRING) { + char *result_value = NULL; + char *value_value = NULL; - plist_get_type_and_value(result_node, &result_type, (void *) (&result_value), &result_length); - plist_get_type_and_value(value_node, &value_type, (void *) (&value_value), &value_length); + plist_get_key_val(result_node, &result_value); + plist_get_string_val(value_node, &value_value); - if (result_type == PLIST_KEY && - value_type == PLIST_STRING && !strcmp(result_value, "Result") && !strcmp(value_value, "Success")) { - log_debug_msg("lockdownd_close(): success\n"); - ret = IPHONE_E_SUCCESS; + if (!strcmp(result_value, "Result") && !strcmp(value_value, "Success")) { + log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_close(): success\n"); + ret = IPHONE_E_SUCCESS; + } + free(result_value); + free(value_value); } - + plist_free(dict); + dict = NULL; return; // ret; } @@ -824,54 +812,44 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c /* Setup DevicePublicKey request plist */ dict = plist_new_dict(); - plist_add_sub_element(dict, PLIST_KEY, (void *) "HostID", strlen("HostID")); - plist_add_sub_element(dict, PLIST_STRING, (void *) HostID, strlen(HostID)); - plist_add_sub_element(dict, PLIST_KEY, (void *) "Request", strlen("Request")); - plist_add_sub_element(dict, PLIST_STRING, (void *) "StartSession", strlen("StartSession")); - plist_to_xml(dict, &XML_content, &length); - log_debug_msg("Send msg :\nsize : %i\nxml : %s", length, XML_content); + plist_add_sub_key_el(dict, "HostID"); + plist_add_sub_string_el(dict, HostID); + plist_add_sub_key_el(dict, "Request"); + plist_add_sub_string_el(dict, "StartSession"); - ret = iphone_lckd_send(control, XML_content, length, &bytes); - - free(XML_content); - XML_content = NULL; + ret = iphone_lckd_send(control, dict); plist_free(dict); dict = NULL; if (ret != IPHONE_E_SUCCESS) return ret; - if (bytes > 0) { - ret = iphone_lckd_recv(control, &XML_content, &bytes); - log_debug_msg("Receive msg :\nsize : %i\nxml : %s", bytes, XML_content); - plist_from_xml(XML_content, bytes, &dict); - free(XML_content); - XML_content = NULL; - if (!dict) - return IPHONE_E_PLIST_ERROR; - - plist_t query_node = plist_find_node(dict, PLIST_STRING, "StartSession", strlen("StartSession")); - plist_t result_key_node = plist_get_next_sibling(query_node); - plist_t result_value_node = plist_get_next_sibling(result_key_node); - - plist_type result_key_type; - plist_type result_value_type; + ret = iphone_lckd_recv(control, &dict); + + if (!dict) + return IPHONE_E_PLIST_ERROR; + + plist_t query_node = plist_find_node(dict, PLIST_STRING, "StartSession", strlen("StartSession")); + plist_t result_key_node = plist_get_next_sibling(query_node); + plist_t result_value_node = plist_get_next_sibling(result_key_node); + + plist_type result_key_type = plist_get_node_type(result_key_node); + plist_type result_value_type = plist_get_node_type(result_value_node); + + if (result_key_type == PLIST_KEY && result_value_type == PLIST_STRING) { char *result_key = NULL; char *result_value = NULL; - uint64_t key_length = 0; - uint64_t val_length = 0; - plist_get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key), &key_length); - plist_get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value), &val_length); + plist_get_key_val(result_key_node, &result_key); + plist_get_string_val(result_value_node, &result_value); ret = IPHONE_E_SSL_ERROR; - if (result_key_type == PLIST_KEY && - result_value_type == PLIST_STRING && !strcmp(result_key, "Result") && !strcmp(result_value, "Success")) { + if (!strcmp(result_key, "Result") && !strcmp(result_value, "Success")) { // Set up GnuTLS... //gnutls_anon_client_credentials_t anoncred; gnutls_certificate_credentials_t xcred; - log_debug_msg("We started the session OK, now trying GnuTLS\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "We started the session OK, now trying GnuTLS\n"); errno = 0; gnutls_global_init(); //gnutls_anon_allocate_client_credentials(&anoncred); @@ -894,59 +872,59 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c } gnutls_credentials_set(*control->ssl_session, GNUTLS_CRD_CERTIFICATE, xcred); // this part is killing me. - log_debug_msg("GnuTLS step 1...\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS step 1...\n"); gnutls_transport_set_ptr(*control->ssl_session, (gnutls_transport_ptr_t) control); - log_debug_msg("GnuTLS step 2...\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS step 2...\n"); gnutls_transport_set_push_function(*control->ssl_session, (gnutls_push_func) & lockdownd_secuwrite); - log_debug_msg("GnuTLS step 3...\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS step 3...\n"); gnutls_transport_set_pull_function(*control->ssl_session, (gnutls_pull_func) & lockdownd_securead); - log_debug_msg("GnuTLS step 4 -- now handshaking...\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS step 4 -- now handshaking...\n"); if (errno) - log_debug_msg("WARN: errno says %s before handshake!\n", strerror(errno)); + log_dbg_msg(DBGMASK_LOCKDOWND, "WARN: errno says %s before handshake!\n", strerror(errno)); return_me = gnutls_handshake(*control->ssl_session); - log_debug_msg("GnuTLS handshake done...\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS handshake done...\n"); if (return_me != GNUTLS_E_SUCCESS) { - log_debug_msg("GnuTLS reported something wrong.\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "GnuTLS reported something wrong.\n"); gnutls_perror(return_me); - log_debug_msg("oh.. errno says %s\n", strerror(errno)); + log_dbg_msg(DBGMASK_LOCKDOWND, "oh.. errno says %s\n", strerror(errno)); return IPHONE_E_SSL_ERROR; } else { control->in_SSL = 1; ret = IPHONE_E_SUCCESS; } } - //store session id - plist_t session_node = plist_find_node(dict, PLIST_KEY, "SessionID", strlen("SessionID")); - if (session_node) { + } + //store session id + plist_t session_node = plist_find_node_by_key(dict, "SessionID"); + if (session_node) { + + plist_t session_node_val = plist_get_next_sibling(session_node); + plist_type session_node_val_type = plist_get_node_type(session_node_val); + + if (session_node_val_type == PLIST_STRING) { - plist_type session_node_val_type; char *session_id = NULL; - uint64_t session_id_length = 0; - plist_t session_node_val = plist_get_next_sibling(session_node); + plist_get_string_val(session_node_val, &session_id); - plist_get_type_and_value(session_node_val, &session_node_val_type, (void *) (&session_id), - &session_id_length); - if (session_node_val_type == PLIST_STRING && session_id_length > 0) { + if (session_node_val_type == PLIST_STRING && session_id) { // we need to store the session ID for StopSession strcpy(control->session_id, session_id); - log_debug_msg("SessionID: %s\n", control->session_id); + log_dbg_msg(DBGMASK_LOCKDOWND, "SessionID: %s\n", control->session_id); } - } else - log_debug_msg("Failed to get SessionID!\n"); - plist_free(dict); - dict = NULL; + free(session_id); + } + } else + log_dbg_msg(DBGMASK_LOCKDOWND, "Failed to get SessionID!\n"); + plist_free(dict); + dict = NULL; - if (ret == IPHONE_E_SUCCESS) - return ret; + if (ret == IPHONE_E_SUCCESS) + return ret; - log_debug_msg("Apparently failed negotiating with lockdownd.\n"); - return IPHONE_E_SSL_ERROR; - } else { - log_debug_msg("Didn't get enough bytes.\n"); - return IPHONE_E_NOT_ENOUGH_DATA; - } + log_dbg_msg(DBGMASK_LOCKDOWND, "Apparently failed negotiating with lockdownd.\n"); + return IPHONE_E_SSL_ERROR; } /** gnutls callback for writing data to the iPhone. @@ -962,10 +940,10 @@ ssize_t lockdownd_secuwrite(gnutls_transport_ptr_t transport, char *buffer, size int bytes = 0; iphone_lckd_client_t control; control = (iphone_lckd_client_t) transport; - log_debug_msg("lockdownd_secuwrite() called\n"); - log_debug_msg("pre-send\nlength = %zi\n", length); + log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_secuwrite() called\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "pre-send\nlength = %zi\n", length); iphone_mux_send(control->connection, buffer, length, &bytes); - log_debug_msg("post-send\nsent %i bytes\n", bytes); + log_dbg_msg(DBGMASK_LOCKDOWND, "post-send\nsent %i bytes\n", bytes); dump_debug_buffer("sslpacketwrite.out", buffer, length); return bytes; @@ -985,7 +963,7 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_ char *hackhackhack = NULL; iphone_lckd_client_t control; control = (iphone_lckd_client_t) transport; - log_debug_msg("lockdownd_securead() called\nlength = %zi\n", length); + log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_securead() called\nlength = %zi\n", length); // Buffering hack! Throw what we've got in our "buffer" into the stream first, then get more. if (control->gtls_buffer_hack_len > 0) { if (length > control->gtls_buffer_hack_len) { // If it's asking for more than we got @@ -994,7 +972,7 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_ memcpy(buffer, control->gtls_buffer_hack, control->gtls_buffer_hack_len); // Fill their buffer partially free(control->gtls_buffer_hack); // free our memory, it's not chained anymore control->gtls_buffer_hack_len = 0; // we don't have a hack buffer anymore - log_debug_msg("Did a partial fill to help quench thirst for data\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "Did a partial fill to help quench thirst for data\n"); } else if (length < control->gtls_buffer_hack_len) { // If it's asking for less... control->gtls_buffer_hack_len -= length; // subtract what they're asking for memcpy(buffer, control->gtls_buffer_hack, length); // fill their buffer @@ -1003,33 +981,34 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_ free(control->gtls_buffer_hack); // Free the old one control->gtls_buffer_hack = hackhackhack; // And make it the new one. hackhackhack = NULL; - log_debug_msg("Quenched the thirst for data; new hack length is %i\n", control->gtls_buffer_hack_len); + log_dbg_msg(DBGMASK_LOCKDOWND, "Quenched the thirst for data; new hack length is %i\n", + control->gtls_buffer_hack_len); return length; // hand it over. } else { // length == hack length memcpy(buffer, control->gtls_buffer_hack, length); // copy our buffer into theirs free(control->gtls_buffer_hack); // free our "obligation" control->gtls_buffer_hack_len = 0; // free our "obligation" - log_debug_msg("Satiated the thirst for data; now we have to eventually receive again.\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "Satiated the thirst for data; now we have to eventually receive again.\n"); return length; // hand it over } } // End buffering hack! char *recv_buffer = (char *) malloc(sizeof(char) * (length * 1000)); // ensuring nothing stupid happens - log_debug_msg("pre-read\nclient wants %zi bytes\n", length); + log_dbg_msg(DBGMASK_LOCKDOWND, "pre-read\nclient wants %zi bytes\n", length); iphone_mux_recv(control->connection, recv_buffer, (length * 1000), &bytes); - log_debug_msg("post-read\nwe got %i bytes\n", bytes); + log_dbg_msg(DBGMASK_LOCKDOWND, "post-read\nwe got %i bytes\n", bytes); if (bytes < 0) { - log_debug_msg("lockdownd_securead(): uh oh\n"); - log_debug_msg - ("I believe what we have here is a failure to communicate... libusb says %s but strerror says %s\n", - usb_strerror(), strerror(errno)); + log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_securead(): uh oh\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, + "I believe what we have here is a failure to communicate... libusb says %s but strerror says %s\n", + usb_strerror(), strerror(errno)); return bytes + 28; // an errno } if (bytes >= length) { if (bytes > length) { - log_debug_msg - ("lockdownd_securead: Client deliberately read less data than was there; resorting to GnuTLS buffering hack.\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, + "lockdownd_securead: Client deliberately read less data than was there; resorting to GnuTLS buffering hack.\n"); if (!control->gtls_buffer_hack_len) { // if there's no hack buffer yet //control->gtls_buffer_hack = strndup(recv_buffer+length, bytes-length); // strndup is NOT a good solution! control->gtls_buffer_hack_len += bytes - length; @@ -1045,10 +1024,11 @@ ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_ memcpy(buffer + pos_start_fill, recv_buffer, length); free(recv_buffer); if (bytes == length) { - log_debug_msg("Returning how much we received.\n"); + log_dbg_msg(DBGMASK_LOCKDOWND, "Returning how much we received.\n"); return bytes; } else { - log_debug_msg("Returning what they want to hear.\nHack length: %i\n", control->gtls_buffer_hack_len); + log_dbg_msg(DBGMASK_LOCKDOWND, "Returning what they want to hear.\nHack length: %i\n", + control->gtls_buffer_hack_len); return length; } } @@ -1073,95 +1053,72 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char if (!client->in_SSL && !lockdownd_start_SSL_session(client, host_id)) return IPHONE_E_SSL_ERROR; - plist_t dict = NULL; - char *XML_content = NULL; - uint32_t length, i = 0, port_loc = 0, bytes = 0; + uint32_t port_loc = 0; iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; free(host_id); host_id = NULL; dict = plist_new_dict(); - plist_add_sub_element(dict, PLIST_KEY, (void *) "Request", strlen("Request")); - plist_add_sub_element(dict, PLIST_STRING, (void *) "StartService", strlen("StartService")); - plist_add_sub_element(dict, PLIST_KEY, (void *) "Service", strlen("Service")); - plist_add_sub_element(dict, PLIST_STRING, (void *) service, strlen(service)); - plist_to_xml(dict, &XML_content, &length); + plist_add_sub_key_el(dict, "Request"); + plist_add_sub_string_el(dict, "StartService"); + plist_add_sub_key_el(dict, "Service"); + plist_add_sub_string_el(dict, service); /* send to iPhone */ - log_debug_msg("Send msg :\nsize : %i\nxml : %s", length, XML_content); - ret = iphone_lckd_send(client, XML_content, length, &bytes); - - free(XML_content); - XML_content = NULL; + ret = iphone_lckd_send(client, dict); plist_free(dict); dict = NULL; if (IPHONE_E_SUCCESS != ret) return ret; - ret = iphone_lckd_recv(client, &XML_content, &bytes); + ret = iphone_lckd_recv(client, &dict); if (IPHONE_E_SUCCESS != ret) return ret; - plist_from_xml(XML_content, bytes, &dict); if (!dict) return IPHONE_E_PLIST_ERROR; + plist_t query_node = plist_find_node_by_string(dict, "StartService"); + plist_t result_key_node = plist_get_next_sibling(query_node); + plist_t result_value_node = plist_get_next_sibling(result_key_node); - if (bytes <= 0) - return IPHONE_E_NOT_ENOUGH_DATA; - else { + plist_t port_key_node = plist_find_node_by_key(dict, "Port"); + plist_t port_value_node = plist_get_next_sibling(port_key_node); - plist_t query_node = plist_find_node(dict, PLIST_STRING, "StartService", strlen("StartService")); - plist_t result_key_node = plist_get_next_sibling(query_node); - plist_t result_value_node = plist_get_next_sibling(result_key_node); + plist_type result_key_type = plist_get_node_type(result_key_node); + plist_type result_value_type = plist_get_node_type(result_value_node); + plist_type port_key_type = plist_get_node_type(port_key_node); + plist_type port_value_type = plist_get_node_type(port_value_node); - plist_t port_key_node = plist_find_node(dict, PLIST_KEY, "Port", strlen("Port")); - plist_t port_value_node = plist_get_next_sibling(port_key_node); + if (result_key_type == PLIST_KEY && result_value_type == PLIST_STRING && port_key_type == PLIST_KEY + && port_value_type == PLIST_UINT) { - plist_type result_key_type; - plist_type result_value_type; - plist_type port_key_type; - plist_type port_value_type; char *result_key = NULL; char *result_value = NULL; char *port_key = NULL; - uint64_t res_key_length = 0; - uint64_t res_val_length = 0; - uint64_t port_key_length = 0; - uint64_t port_val_length = 0; uint64_t port_value = 0; - plist_get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key), &res_key_length); - plist_get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value), &res_val_length); - plist_get_type_and_value(port_key_node, &port_key_type, (void *) (&port_key), &port_key_length); - plist_get_type_and_value(port_value_node, &port_value_type, (void *) (&port_value), &port_val_length); + plist_get_key_val(result_key_node, &result_key); + plist_get_string_val(result_value_node, &result_value); + plist_get_key_val(port_key_node, &port_key); + plist_get_uint_val(port_value_node, &port_value); - if (result_key_type == PLIST_KEY && - result_value_type == PLIST_STRING && - port_key_type == PLIST_KEY && - port_value_type == PLIST_UINT && - !strcmp(result_key, "Result") && !strcmp(result_value, "Success") && !strcmp(port_key, "Port")) { + if (!strcmp(result_key, "Result") && !strcmp(result_value, "Success") && !strcmp(port_key, "Port")) { port_loc = port_value; ret = IPHONE_E_SUCCESS; } - log_debug_msg("lockdownd_start_service(): DATA RECEIVED:\n\n"); - log_debug_msg(XML_content); - log_debug_msg("end data received by lockdownd_start_service()\n"); - - free(XML_content); - plist_free(dict); - dict = NULL; - if (port && ret == IPHONE_E_SUCCESS) { + if (port && ret == IPHONE_E_SUCCESS) *port = port_loc; - return IPHONE_E_SUCCESS; - } else - return IPHONE_E_UNKNOWN_ERROR; + else + ret = IPHONE_E_UNKNOWN_ERROR; } - return IPHONE_E_UNKNOWN_ERROR; + plist_free(dict); + dict = NULL; + return ret; } -- cgit v1.1-32-gdbae From 0d05f8de79ee91e9be80c6296eff9ce216582ba4 Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Thu, 29 Jan 2009 22:30:16 +0100 Subject: Update to latest libplist API. --- src/lockdown.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'src/lockdown.c') diff --git a/src/lockdown.c b/src/lockdown.c index a02e6a8..2f48dfd 100644 --- a/src/lockdown.c +++ b/src/lockdown.c @@ -411,15 +411,15 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, const c if (!strcmp(result_key, "Value")) { log_dbg_msg(DBGMASK_LOCKDOWND, "lockdownd_generic_get_value(): success\n"); - plist_type value_value_type; - char *value_value = NULL; - uint64_t valval_length = 0; + plist_type value_value_type = plist_get_node_type(value_value_node); + if (PLIST_STRING == value_value_type) { + char *value_value = NULL; + plist_get_string_val(value_value_node, &value_value); - plist_get_type_and_value(value_value_node, &value_value_type, (void *) (&value_value), &valval_length); - - value->data = value_value; - value->size = valval_length; - ret = IPHONE_E_SUCCESS; + value->data = value_value; + value->size = strlen(value_value); + ret = IPHONE_E_SUCCESS; + } } free(result_key); } @@ -825,7 +825,7 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c if (!dict) return IPHONE_E_PLIST_ERROR; - plist_t query_node = plist_find_node(dict, PLIST_STRING, "StartSession", strlen("StartSession")); + plist_t query_node = plist_find_node_by_string(dict, "StartSession"); plist_t result_key_node = plist_get_next_sibling(query_node); plist_t result_value_node = plist_get_next_sibling(result_key_node); -- cgit v1.1-32-gdbae From f893e8a9e2cc197522f838b3f2bbec8862953c2f Mon Sep 17 00:00:00 2001 From: Jonathan Beck Date: Sun, 12 Apr 2009 16:08:06 +0200 Subject: Use less secure random number generation so we can generate private keys on the fly. Drop libiphone-initconf. --- src/lockdown.c | 160 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 85 insertions(+), 75 deletions(-) (limited to 'src/lockdown.c') diff --git a/src/lockdown.c b/src/lockdown.c index 63f9090..e720b29 100644 --- a/src/lockdown.c +++ b/src/lockdown.c @@ -1,22 +1,22 @@ /* * lockdown.c * libiphone built-in lockdownd client - * + * * Copyright (c) 2008 Zach C. All Rights Reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. - * + * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "usbmux.h" @@ -67,7 +67,7 @@ iphone_lckd_client_t new_lockdownd_client(iphone_device_t phone) /** * Closes the lockdownd communication session, by sending - * the StopSession Request to the device. + * the StopSession Request to the device. * * @param control The lockdown client */ @@ -128,7 +128,7 @@ static void iphone_lckd_stop_session(iphone_lckd_client_t control) /** * Shuts down the SSL session by first calling iphone_lckd_stop_session - * to cleanly close the lockdownd communication session, and then + * to cleanly close the lockdownd communication session, and then * performing a close notify, which is done by "gnutls_bye". * * @param client The lockdown client @@ -219,6 +219,7 @@ iphone_error_t iphone_lckd_recv(iphone_lckd_client_t client, plist_t * plist) return IPHONE_E_NOT_ENOUGH_DATA; } + log_dbg_msg(DBGMASK_LOCKDOWND, "Recv msg :\nsize : %i\nbuffer :\n%s\n", bytes, receive); plist_from_xml(receive, bytes, plist); free(receive); @@ -229,7 +230,7 @@ iphone_error_t iphone_lckd_recv(iphone_lckd_client_t client, plist_t * plist) } /** Sends lockdownd data to the iPhone - * + * * @note This function is low-level and should only be used if you need to send * a new type of message. * @@ -272,7 +273,7 @@ iphone_error_t iphone_lckd_send(iphone_lckd_client_t client, plist_t plist) } /** Initiates the handshake for the lockdown session. Part of the lockdownd handshake. - * + * * @note You most likely want lockdownd_init unless you are doing something special. * * @param control The lockdownd client @@ -338,7 +339,7 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control) * * @return IPHONE_E_SUCCESS on success. */ -iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, const char *req_key, char *req_string, +iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, const char *req_key, const char *req_string, gnutls_datum_t * value) { if (!control || !req_key || !value || value->data) @@ -396,7 +397,7 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, const c return ret; } - plist_t value_key_node = plist_get_next_sibling(result_key_node); + plist_t value_key_node = plist_find_node_by_key(dict, "Value");//plist_get_next_sibling(result_value_node); plist_t value_value_node = plist_get_next_sibling(value_key_node); plist_type value_key_type = plist_get_node_type(value_key_node); @@ -418,6 +419,16 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, const c value->size = strlen(value_value); ret = IPHONE_E_SUCCESS; } + + if (PLIST_DATA == value_value_type) { + char *value_value = NULL; + uint64_t size = 0; + plist_get_data_val(value_value_node, &value_value, &size); + + value->data = value_value; + value->size = size; + ret = IPHONE_E_SUCCESS; + } } free(result_key); } @@ -435,8 +446,9 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, const c iphone_error_t lockdownd_get_device_uid(iphone_lckd_client_t control, char **uid) { gnutls_datum_t temp = { NULL, 0 }; - return lockdownd_generic_get_value(control, "Key", "UniqueDeviceID", &temp); + iphone_error_t ret = lockdownd_generic_get_value(control, "Key", "UniqueDeviceID", &temp); *uid = temp.data; + return ret; } /** Askes for the device's public key. Part of the lockdownd handshake. @@ -480,6 +492,7 @@ iphone_error_t iphone_lckd_new_client(iphone_device_t device, iphone_lckd_client if (IPHONE_E_SUCCESS != ret) { log_debug_msg("Device refused to send uid.\n"); } + log_debug_msg("Device uid: %s\n", uid); host_id = get_host_id(); if (IPHONE_E_SUCCESS == ret && !host_id) { @@ -495,19 +508,22 @@ iphone_error_t iphone_lckd_new_client(iphone_device_t device, iphone_lckd_client uid = NULL; } - ret = lockdownd_start_SSL_session(client_loc, host_id); - if (IPHONE_E_SUCCESS != ret) { - ret = IPHONE_E_SSL_ERROR; - log_debug_msg("SSL Session opening failed.\n"); - } + if (IPHONE_E_SUCCESS == ret) { + ret = lockdownd_start_SSL_session(client_loc, host_id); + if (IPHONE_E_SUCCESS != ret) { + ret = IPHONE_E_SSL_ERROR; + log_debug_msg("SSL Session opening failed.\n"); + } - if (host_id) { - free(host_id); - host_id = NULL; + if (host_id) { + free(host_id); + host_id = NULL; + } + + if (IPHONE_E_SUCCESS == ret) + *client = client_loc; } - if (IPHONE_E_SUCCESS == ret) - *client = client_loc; return ret; } @@ -534,6 +550,7 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch log_debug_msg("Device refused to send public key.\n"); return ret; } + log_debug_msg("device public key :\n %s.\n", public_key.data); ret = lockdownd_gen_pair_cert(public_key, &device_cert, &host_cert, &root_cert); if (ret != IPHONE_E_SUCCESS) { @@ -547,15 +564,15 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch dict_record = plist_new_dict(); plist_add_sub_node(dict, dict_record); plist_add_sub_key_el(dict_record, "DeviceCertificate"); - plist_add_sub_data_el(dict_record, device_cert.data, device_cert.size); + plist_add_sub_data_el(dict_record, (const char*)device_cert.data, device_cert.size); plist_add_sub_key_el(dict_record, "HostCertificate"); - plist_add_sub_data_el(dict_record, host_cert.data, host_cert.size); + plist_add_sub_data_el(dict_record, (const char*)host_cert.data, host_cert.size); plist_add_sub_key_el(dict_record, "HostID"); plist_add_sub_string_el(dict_record, host_id); plist_add_sub_key_el(dict_record, "RootCertificate"); - plist_add_sub_data_el(dict_record, root_cert.data, root_cert.size); - plist_add_sub_key_el(dict_record, "Request"); - plist_add_sub_string_el(dict_record, "Pair"); + plist_add_sub_data_el(dict_record, (const char*)root_cert.data, root_cert.size); + plist_add_sub_key_el(dict, "Request"); + plist_add_sub_string_el(dict, "Pair"); /* send to iPhone */ ret = iphone_lckd_send(control, dict); @@ -667,7 +684,7 @@ void lockdownd_close(iphone_lckd_client_t control) /** Generates the device certificate from the public key as well as the host * and root certificates. - * + * * @return IPHONE_E_SUCCESS on success. */ iphone_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t * odevice_cert, @@ -718,7 +735,7 @@ iphone_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t gnutls_global_init(); gnutls_datum_t essentially_null = { strdup("abababababababab"), strlen("abababababababab") }; - gnutls_x509_privkey_t fake_privkey, root_privkey; + gnutls_x509_privkey_t fake_privkey, root_privkey, host_privkey; gnutls_x509_crt_t dev_cert, root_cert, host_cert; gnutls_x509_privkey_init(&fake_privkey); @@ -731,57 +748,50 @@ iphone_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t &essentially_null, &essentially_null)) { gnutls_x509_privkey_init(&root_privkey); + gnutls_x509_privkey_init(&host_privkey); - /* get root cert */ - gnutls_datum_t pem_root_cert = { NULL, 0 }; - get_root_certificate(&pem_root_cert); - if (GNUTLS_E_SUCCESS != gnutls_x509_crt_import(root_cert, &pem_root_cert, GNUTLS_X509_FMT_PEM)) - ret = IPHONE_E_SSL_ERROR; - - /* get host cert */ - gnutls_datum_t pem_host_cert = { NULL, 0 }; - get_host_certificate(&pem_host_cert); - if (GNUTLS_E_SUCCESS != gnutls_x509_crt_import(host_cert, &pem_host_cert, GNUTLS_X509_FMT_PEM)) - ret = IPHONE_E_SSL_ERROR; - - /* get root private key */ - gnutls_datum_t pem_root_priv = { NULL, 0 }; - get_root_private_key(&pem_root_priv); - if (GNUTLS_E_SUCCESS != gnutls_x509_privkey_import(root_privkey, &pem_root_priv, GNUTLS_X509_FMT_PEM)) - ret = IPHONE_E_SSL_ERROR; - - /* generate device certificate */ - gnutls_x509_crt_set_key(dev_cert, fake_privkey); - gnutls_x509_crt_set_serial(dev_cert, "\x00", 1); - gnutls_x509_crt_set_version(dev_cert, 3); - gnutls_x509_crt_set_ca_status(dev_cert, 0); - gnutls_x509_crt_set_activation_time(dev_cert, time(NULL)); - gnutls_x509_crt_set_expiration_time(dev_cert, time(NULL) + (60 * 60 * 24 * 365 * 10)); - gnutls_x509_crt_sign(dev_cert, root_cert, root_privkey); + ret = get_keys_and_certs( root_privkey, root_cert, host_privkey, host_cert); if (IPHONE_E_SUCCESS == ret) { - /* if everything went well, export in PEM format */ - gnutls_datum_t dev_pem = { NULL, 0 }; - gnutls_x509_crt_export(dev_cert, GNUTLS_X509_FMT_PEM, NULL, &dev_pem.size); - dev_pem.data = gnutls_malloc(dev_pem.size); - gnutls_x509_crt_export(dev_cert, GNUTLS_X509_FMT_PEM, dev_pem.data, &dev_pem.size); - - /* copy buffer for output */ - odevice_cert->data = malloc(dev_pem.size); - memcpy(odevice_cert->data, dev_pem.data, dev_pem.size); - odevice_cert->size = dev_pem.size; - - ohost_cert->data = malloc(pem_host_cert.size); - memcpy(ohost_cert->data, pem_host_cert.data, pem_host_cert.size); - ohost_cert->size = pem_host_cert.size; - - oroot_cert->data = malloc(pem_root_cert.size); - memcpy(oroot_cert->data, pem_root_cert.data, pem_root_cert.size); - oroot_cert->size = pem_root_cert.size; + + /* generate device certificate */ + gnutls_x509_crt_set_key(dev_cert, fake_privkey); + gnutls_x509_crt_set_serial(dev_cert, "\x00", 1); + gnutls_x509_crt_set_version(dev_cert, 3); + gnutls_x509_crt_set_ca_status(dev_cert, 0); + gnutls_x509_crt_set_activation_time(dev_cert, time(NULL)); + gnutls_x509_crt_set_expiration_time(dev_cert, time(NULL) + (60 * 60 * 24 * 365 * 10)); + gnutls_x509_crt_sign(dev_cert, root_cert, root_privkey); + + if (IPHONE_E_SUCCESS == ret) { + /* if everything went well, export in PEM format */ + gnutls_datum_t dev_pem = { NULL, 0 }; + gnutls_x509_crt_export(dev_cert, GNUTLS_X509_FMT_PEM, NULL, &dev_pem.size); + dev_pem.data = gnutls_malloc(dev_pem.size); + gnutls_x509_crt_export(dev_cert, GNUTLS_X509_FMT_PEM, dev_pem.data, &dev_pem.size); + + gnutls_datum_t pem_root_cert = { NULL, 0 }; + gnutls_datum_t pem_host_cert = { NULL, 0 }; + + if ( IPHONE_E_SUCCESS == get_certs_as_pem(&pem_root_cert, &pem_host_cert) ) { + /* copy buffer for output */ + odevice_cert->data = malloc(dev_pem.size); + memcpy(odevice_cert->data, dev_pem.data, dev_pem.size); + odevice_cert->size = dev_pem.size; + + ohost_cert->data = malloc(pem_host_cert.size); + memcpy(ohost_cert->data, pem_host_cert.data, pem_host_cert.size); + ohost_cert->size = pem_host_cert.size; + + oroot_cert->data = malloc(pem_root_cert.size); + memcpy(oroot_cert->data, pem_root_cert.data, pem_root_cert.size); + oroot_cert->size = pem_root_cert.size; + + g_free(pem_root_cert.data); + g_free(pem_host_cert.data); + } + } } - gnutls_free(pem_root_priv.data); - gnutls_free(pem_root_cert.data); - gnutls_free(pem_host_cert.data); } } -- cgit v1.1-32-gdbae