summaryrefslogtreecommitdiffstats
path: root/src/lockdown.c
diff options
context:
space:
mode:
authorGravatar Matt Colyer2008-08-05 23:25:35 -0700
committerGravatar Matt Colyer2008-08-05 23:28:10 -0700
commit25e85bf5362b6f4c1878665c6037fdbfdc25c155 (patch)
tree437979cdb4b9a7958a3d891d6a39a7be2bf61d17 /src/lockdown.c
parent4b558a53f61005b0ca49665d2da92303f6e14872 (diff)
downloadlibimobiledevice-25e85bf5362b6f4c1878665c6037fdbfdc25c155.tar.gz
libimobiledevice-25e85bf5362b6f4c1878665c6037fdbfdc25c155.tar.bz2
Zack's final changes to the pairing.
Diffstat (limited to 'src/lockdown.c')
-rw-r--r--src/lockdown.c46
1 files changed, 39 insertions, 7 deletions
diff --git a/src/lockdown.c b/src/lockdown.c
index 095b2b4..830866d 100644
--- a/src/lockdown.c
+++ b/src/lockdown.c
@@ -39,7 +39,28 @@ const ASN1_ARRAY_TYPE pkcs1_asn1_tab[]={
39 {0,0,0} 39 {0,0,0}
40}; 40};
41 41
42int get_rand(int min, int max) {
43 int retval = (rand() % (max - min)) + min;
44 return retval;
45}
42 46
47char *lockdownd_generate_hostid() {
48 char *hostid = (char*)malloc(sizeof(char) * 37); // HostID's are just UUID's, and UUID's are 36 characters long
49 const char *chars = "ABCDEF0123456789";
50 srand(time(NULL));
51 int i = 0;
52
53 for (i = 0; i < 36; i++) {
54 if (i == 8 || i == 13 || i == 18 || i == 23) {
55 hostid[i] = '-';
56 continue;
57 } else {
58 hostid[i] = chars[get_rand(0,16)];
59 }
60 }
61 hostid[36] = '\0'; // make it a real string
62 return hostid;
63}
43 64
44lockdownd_client *new_lockdownd_client(iPhone *phone) { 65lockdownd_client *new_lockdownd_client(iPhone *phone) {
45 if (!phone) return NULL; 66 if (!phone) return NULL;
@@ -72,7 +93,7 @@ int lockdownd_recv(lockdownd_client *control, char **dump_data) {
72 char *receive; 93 char *receive;
73 uint32 datalen = 0, bytes = 0; 94 uint32 datalen = 0, bytes = 0;
74 95
75 if (!control->in_SSL) bytes = mux_recv(control->iphone, control->connection, (char*)&datalen, sizeof(datalen)); 96 if (!control->in_SSL) bytes = mux_recv(control->connection, (char *)&datalen, sizeof(datalen));
76 else bytes = gnutls_record_recv(*control->ssl_session, &datalen, sizeof(datalen)); 97 else bytes = gnutls_record_recv(*control->ssl_session, &datalen, sizeof(datalen));
77 datalen = ntohl(datalen); 98 datalen = ntohl(datalen);
78 99
@@ -120,8 +141,7 @@ int lockdownd_hello(lockdownd_client *control) {
120 char *XML_content; 141 char *XML_content;
121 uint32 length; 142 uint32 length;
122 143
123 xmlDocDumpMemory(plist, (xmlChar**)&XML_content, &length); 144 xmlDocDumpMemory(plist, (xmlChar **)&XML_content, &length);
124
125 bytes = lockdownd_send(control, XML_content, length); 145 bytes = lockdownd_send(control, XML_content, length);
126 146
127 xmlFree(XML_content); 147 xmlFree(XML_content);
@@ -135,7 +155,6 @@ int lockdownd_hello(lockdownd_client *control) {
135 if (!xmlStrcmp(dict->name, "dict")) break; 155 if (!xmlStrcmp(dict->name, "dict")) break;
136 } 156 }
137 if (!dict) return 0; 157 if (!dict) return 0;
138
139 dictionary = read_dict_element_strings(dict); 158 dictionary = read_dict_element_strings(dict);
140 xmlFreeDoc(plist); 159 xmlFreeDoc(plist);
141 free(XML_content); 160 free(XML_content);
@@ -226,6 +245,8 @@ int lockdownd_init(iPhone *phone, lockdownd_client **control)
226 } 245 }
227 246
228 host_id = get_host_id(); 247 host_id = get_host_id();
248 if (!host_id) host_id = lockdownd_generate_hostid();
249
229 if (!is_device_known(public_key)){ 250 if (!is_device_known(public_key)){
230 ret = lockdownd_pair_device(*control, public_key, host_id); 251 ret = lockdownd_pair_device(*control, public_key, host_id);
231 } 252 }
@@ -284,6 +305,12 @@ int lockdownd_pair_device(lockdownd_client *control, char *public_key_b64, char
284 /* Now get iPhone's answer */ 305 /* Now get iPhone's answer */
285 bytes = lockdownd_recv(control, &XML_content); 306 bytes = lockdownd_recv(control, &XML_content);
286 307
308 if (debug) {
309 printf("lockdown_pair_device: iPhone's response to our pair request:\n");
310 fwrite(XML_content, 1, bytes, stdout);
311 printf("\n\n");
312 }
313
287 plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0); 314 plist = xmlReadMemory(XML_content, bytes, NULL, NULL, 0);
288 if (!plist) return 0; 315 if (!plist) return 0;
289 dict = xmlDocGetRootElement(plist); 316 dict = xmlDocGetRootElement(plist);
@@ -303,15 +330,20 @@ int lockdownd_pair_device(lockdownd_client *control, char *public_key_b64, char
303 success = 1; 330 success = 1;
304 } 331 }
305 } 332 }
306 333
307 if (dictionary) { 334 if (dictionary) {
308 free_dictionary(dictionary); 335 free_dictionary(dictionary);
309 dictionary = NULL; 336 dictionary = NULL;
310 } 337 }
311 338
312 /* store public key in config if pairing succeeded */ 339 /* store public key in config if pairing succeeded */
313 if (success) 340 if (success) {
341 if (debug) printf("lockdownd_pair_device: pair success\n");
314 store_device_public_key(public_key_b64); 342 store_device_public_key(public_key_b64);
343 ret = 1;
344 } else {
345 if (debug) printf("lockdownd_pair_device: pair failure\n");
346 }
315 return ret; 347 return ret;
316} 348}
317 349
@@ -480,6 +512,7 @@ int lockdownd_start_SSL_session(lockdownd_client *control, const char *HostID) {
480 // Set up GnuTLS... 512 // Set up GnuTLS...
481 //gnutls_anon_client_credentials_t anoncred; 513 //gnutls_anon_client_credentials_t anoncred;
482 gnutls_certificate_credentials_t xcred; 514 gnutls_certificate_credentials_t xcred;
515
483 if (debug) printf("We started the session OK, now trying GnuTLS\n"); 516 if (debug) printf("We started the session OK, now trying GnuTLS\n");
484 errno = 0; 517 errno = 0;
485 gnutls_global_init(); 518 gnutls_global_init();
@@ -703,4 +736,3 @@ int lockdownd_start_service(lockdownd_client *control, const char *service) {
703 736
704 return 0; 737 return 0;
705} 738}
706