summaryrefslogtreecommitdiffstats
path: root/src/lockdown.c
blob: 872b7b043598e9d601d95a1ea6e4c9fcfe43aa67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
/*
 * 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 
 */

#include "usbmux.h"
#include "utils.h"
#include "iphone.h"
#include "lockdown.h"
#include "userpref.h"
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <glib.h>
#include <libtasn1.h>
#include <gnutls/x509.h>

#include <plist/plist.h>

const ASN1_ARRAY_TYPE pkcs1_asn1_tab[] = {
	{"PKCS1", 536872976, 0},
	{0, 1073741836, 0},
	{"RSAPublicKey", 536870917, 0},
	{"modulus", 1073741827, 0},
	{"publicExponent", 3, 0},
	{0, 0, 0}
};

/** Creates a lockdownd client for the give iPhone.
 *
 * @param phone The iPhone to create a lockdownd client for
 *
 * @return The lockdownd client.
 */
iphone_lckd_client_t new_lockdownd_client(iphone_device_t phone)
{
	if (!phone)
		return NULL;
	iphone_lckd_client_t control = (iphone_lckd_client_t) malloc(sizeof(struct iphone_lckd_client_int));

	if (IPHONE_E_SUCCESS != iphone_mux_new_client(phone, 0x0a00, 0xf27e, &control->connection)) {
		free(control);
		return NULL;
	}

	control->ssl_session = (gnutls_session_t *) malloc(sizeof(gnutls_session_t));
	control->in_SSL = 0;
	control->gtls_buffer_hack_len = 0;
	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;

	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 *) "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));

	log_debug_msg("iphone_lckd_stop_session() 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);

	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);

	if (!dict) {
		log_debug_msg("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 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;

	char *result_value = NULL;
	char *value_value = NULL;
	uint64_t result_length = 0;
	uint64_t value_length = 0;

	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")) {
		log_debug_msg("lockdownd_stop_session(): success\n");
		ret = IPHONE_E_SUCCESS;
	}

	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.
 *
 * @param control The lockdown client
 */
iphone_error_t iphone_lckd_free_client(iphone_lckd_client_t client)
{
	if (!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);
	}

	free(client);
	return ret;
}

/** Polls the iPhone for lockdownd data.
 *
 * @param control The lockdownd client
 * @param dump_data The pointer to the location of the buffer in which to store
 *                  the received data
 *
 * @return The number of bytes received
 */
iphone_error_t iphone_lckd_recv(iphone_lckd_client_t client, char **dump_data, uint32_t * recv_bytes)
{
	if (!client || !dump_data || !recv_bytes)
		return IPHONE_E_INVALID_ARG;
	iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
	char *receive;
	uint32_t datalen = 0, bytes = 0;

	if (!client->in_SSL)
		ret = iphone_mux_recv(client->connection, (char *) &datalen, sizeof(datalen), &bytes);
	else {
		bytes = gnutls_record_recv(*client->ssl_session, &datalen, sizeof(datalen));
		if (bytes > 0)
			ret = IPHONE_E_SUCCESS;
	}
	datalen = ntohl(datalen);

	receive = (char *) malloc(sizeof(char) * datalen);
	if (!client->in_SSL)
		ret = iphone_mux_recv(client->connection, receive, datalen, &bytes);
	else {
		bytes = gnutls_record_recv(*client->ssl_session, receive, datalen);
		if (bytes > 0)
			ret = IPHONE_E_SUCCESS;
	}
	*dump_data = receive;
	*recv_bytes = bytes;
	return ret;
}

/** 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.
 *
 * @param control The lockdownd client
 * @param raw_data The null terminated string buffer to send
 * @param length The length of data to send
 *
 * @return The number of bytes sent
 */
iphone_error_t iphone_lckd_send(iphone_lckd_client_t client, char *raw_data, uint32_t length, uint32_t * sent_bytes)
{
	if (!client || !raw_data || length == 0 || !sent_bytes)
		return IPHONE_E_INVALID_ARG;
	char *real_query;
	int bytes;
	iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;

	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);

	if (!client->in_SSL)
		ret = iphone_mux_send(client->connection, real_query, ntohl(length) + sizeof(length), &bytes);
	else {
		gnutls_record_send(*client->ssl_session, real_query, ntohl(length) + sizeof(length));
		ret = IPHONE_E_SUCCESS;
	}
	log_debug_msg("lockdownd_send(): sent it!\n");
	free(real_query);
	*sent_bytes = bytes;
	return ret;
}

/** 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
 *
 * @return 1 on success and 0 on failure.
 */
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"));

	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);

	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);

	if (!dict)
		return IPHONE_E_PLIST_ERROR;

	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;

	char *result_value = NULL;
	char *value_value = NULL;
	uint64_t result_length = 0;
	uint64_t value_length = 0;

	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")) {
		log_debug_msg("lockdownd_hello(): success\n");
		ret = IPHONE_E_SUCCESS;
	}

	return ret;
}

/** Generic function to handle simple (key, value) requests.
 *
 * @param control an initialized lockdownd client.
 * @param key the key to request
 * @param value a pointer to the requested value
 *
 * @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,
										   gnutls_datum_t * value)
{
	if (!control || !req_key || !value || value->data)
		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);

	/* send to iPhone */
	log_debug_msg("Send msg :\nsize : %i\nxml : %s", length, XML_content);
	ret = iphone_lckd_send(control, XML_content, length, &bytes);

	free(XML_content);
	XML_content = NULL;
	plist_free(dict);
	dict = NULL;

	if (ret != IPHONE_E_SUCCESS)
		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);

	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 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_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")) {
		log_debug_msg("lockdownd_generic_get_value(): success\n");
		ret = IPHONE_E_SUCCESS;
	}

	if (ret != IPHONE_E_SUCCESS) {
		return IPHONE_E_DICT_ERROR;
	}

	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_free(dict);
	free(XML_content);
	return ret;
}

/** Askes for the device's unique id. Part of the lockdownd handshake.
 *
 * @note You most likely want lockdownd_init unless you are doing something special.
 *
 * @return 1 on success and 0 on failure.
 */
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);
	*uid = temp.data;
}

/** Askes for the device's public key. Part of the lockdownd handshake.
 *
 * @note You most likely want lockdownd_init unless you are doing something special.
 *
 * @return 1 on success and 0 on failure.
 */
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);
}

/** Completes the entire lockdownd handshake.
 *
 * @param phone The iPhone
 * @param lockdownd_client The pointer to the location of the lockdownd_client
 *
 * @return 1 on success and 0 on failure
 */
iphone_error_t iphone_lckd_new_client(iphone_device_t device, iphone_lckd_client_t * client)
{
	if (!device || !client || (client && *client))
		return IPHONE_E_INVALID_ARG;
	iphone_error_t ret = IPHONE_E_SUCCESS;
	char *host_id = NULL;

	iphone_lckd_client_t client_loc = new_lockdownd_client(device);
	if (IPHONE_E_SUCCESS != lockdownd_hello(client_loc)) {
		fprintf(stderr, "Hello failed in the lockdownd client.\n");
		ret = IPHONE_E_NOT_ENOUGH_DATA;
	}


	char *uid = NULL;
	ret = lockdownd_get_device_uid(client_loc, &uid);
	if (IPHONE_E_SUCCESS != ret) {
		fprintf(stderr, "Device refused to send uid.\n");
	}

	host_id = get_host_id();
	if (IPHONE_E_SUCCESS == ret && !host_id) {
		fprintf(stderr, "No HostID found, run libiphone-initconf.\n");
		ret = IPHONE_E_INVALID_CONF;
	}

	if (IPHONE_E_SUCCESS == ret && !is_device_known(uid))
		ret = lockdownd_pair_device(client_loc, uid, host_id);

	if (uid) {
		free(uid);
		uid = NULL;
	}

	ret = lockdownd_start_SSL_session(client_loc, host_id);
	if (IPHONE_E_SUCCESS != ret) {
		ret = IPHONE_E_SSL_ERROR;
		fprintf(stderr, "SSL Session opening failed.\n");
	}

	if (host_id) {
		free(host_id);
		host_id = NULL;
	}

	if (IPHONE_E_SUCCESS == ret)
		*client = client_loc;
	return ret;
}

/** Generates the appropriate keys and pairs the device. It's part of the
 *  lockdownd handshake.
 *
 * @note You most likely want lockdownd_init unless you are doing something special.
 *
 * @return 1 on success and 0 on failure
 */
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 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 };
	gnutls_datum_t root_cert = { NULL, 0 };
	gnutls_datum_t public_key = { NULL, 0 };

	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, &device_cert, &host_cert, &root_cert);
	if (ret != IPHONE_E_SUCCESS) {
		free(public_key.data);
		return ret;
	}

	/* 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);

	/* send to iPhone */
	ret = iphone_lckd_send(control, XML_content, length, &bytes);

	free(XML_content);
	plist_free(dict);
	dict = NULL;

	if (ret != IPHONE_E_SUCCESS)
		return ret;

	/* Now get iPhone's answer */
	ret = iphone_lckd_recv(control, &XML_content, &bytes);

	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 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_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")) {
		ret = IPHONE_E_SUCCESS;
	}

	/* 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);
		ret = IPHONE_E_SUCCESS;
	} else {
		log_debug_msg("lockdownd_pair_device: pair failure\n");
		ret = IPHONE_E_PAIRING_FAILED;
	}
	free(public_key.data);
	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;

	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_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);

	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);

	if (!dict) {
		log_debug_msg("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 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;

	char *result_value = NULL;
	char *value_value = NULL;
	uint64_t result_length = 0;
	uint64_t value_length = 0;

	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")) {
		log_debug_msg("lockdownd_close(): success\n");
		ret = IPHONE_E_SUCCESS;
	}

	return;						// ret;
}

/** 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,
									   gnutls_datum_t * ohost_cert, gnutls_datum_t * oroot_cert)
{
	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 };

	/* now decode the PEM encoded key */
	gnutls_datum_t 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;
		if (ASN1_SUCCESS == asn1_array2tree(pkcs1_asn1_tab, &pkcs1, NULL)) {

			ASN1_TYPE asn1_pub_key = ASN1_TYPE_EMPTY;
			asn1_create_element(pkcs1, "PKCS1.RSAPublicKey", &asn1_pub_key);

			if (ASN1_SUCCESS == asn1_der_decoding(&asn1_pub_key, der_pub_key.data, der_pub_key.size, NULL)) {

				/* get size to read */
				int ret1 = asn1_read_value(asn1_pub_key, "modulus", NULL, &modulus.size);
				int ret2 = asn1_read_value(asn1_pub_key, "publicExponent", NULL, &exponent.size);

				modulus.data = gnutls_malloc(modulus.size);
				exponent.data = gnutls_malloc(exponent.size);

				ret1 = asn1_read_value(asn1_pub_key, "modulus", modulus.data, &modulus.size);
				ret2 = asn1_read_value(asn1_pub_key, "publicExponent", exponent.data, &exponent.size);
				if (ASN1_SUCCESS == ret1 && ASN1_SUCCESS == ret2)
					ret = IPHONE_E_SUCCESS;
			}
			if (asn1_pub_key)
				asn1_delete_structure(&asn1_pub_key);
		}
		if (pkcs1)
			asn1_delete_structure(&pkcs1);
	}

	/* now generate certifcates */
	if (IPHONE_E_SUCCESS == ret && 0 != modulus.size && 0 != exponent.size) {

		gnutls_global_init();
		gnutls_datum_t essentially_null = { strdup("abababababababab"), strlen("abababababababab") };

		gnutls_x509_privkey_t fake_privkey, root_privkey;
		gnutls_x509_crt_t dev_cert, root_cert, host_cert;

		gnutls_x509_privkey_init(&fake_privkey);
		gnutls_x509_crt_init(&dev_cert);
		gnutls_x509_crt_init(&root_cert);
		gnutls_x509_crt_init(&host_cert);

		if (GNUTLS_E_SUCCESS ==
			gnutls_x509_privkey_import_rsa_raw(fake_privkey, &modulus, &exponent, &essentially_null, &essentially_null,
											   &essentially_null, &essentially_null)) {

			gnutls_x509_privkey_init(&root_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);

			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;
			}
			gnutls_free(pem_root_priv.data);
			gnutls_free(pem_root_cert.data);
			gnutls_free(pem_host_cert.data);
		}
	}

	gnutls_free(modulus.data);
	gnutls_free(exponent.data);

	gnutls_free(der_pub_key.data);

	return ret;
}

/** Starts SSL communication with lockdownd after the iPhone has been paired.
 *
 * @param control The lockdownd client
 * @param HostID The HostID used with this phone
 *
 * @return 1 on success and 0 on failure
 */
iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const char *HostID)
{
	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;
	control->session_id[0] = '\0';

	/* 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);

	ret = iphone_lckd_send(control, XML_content, length, &bytes);

	free(XML_content);
	XML_content = NULL;
	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);
		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;
		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);

		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")) {
			// 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;
				ret = IPHONE_E_SUCCESS;
			}
		}
		//store session id
		plist_t session_node = plist_find_node(dict, PLIST_KEY, "SessionID", strlen("SessionID"));
		if (session_node) {

			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_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) {
				// 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) {
			log_debug_msg("Failed to get SessionID!\n");
			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;
	}
}

/** gnutls callback for writing data to the iPhone.
 *
 * @param transport It's really the lockdownd client, but the method signature has to match
 * @param buffer The data to send
 * @param length The length of data to send in bytes
 *
 * @return The number of bytes sent
 */
ssize_t lockdownd_secuwrite(gnutls_transport_ptr_t transport, char *buffer, size_t length)
{
	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);
	iphone_mux_send(control->connection, buffer, length, &bytes);
	log_debug_msg("post-send\nsent %i bytes\n", bytes);

	dump_debug_buffer("sslpacketwrite.out", buffer, length);
	return bytes;
}

/** gnutls callback for reading data from the iPhone
 *
 * @param transport It's really the lockdownd client, but the method signature has to match
 * @param buffer The buffer to store data in
 * @param length The length of data to read in bytes
 *
 * @return The number of bytes read
 */
ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_t length)
{
	int bytes = 0, pos_start_fill = 0;
	char *hackhackhack = NULL;
	iphone_lckd_client_t control;
	control = (iphone_lckd_client_t) transport;
	log_debug_msg("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
			length -= control->gtls_buffer_hack_len;	// Subtract what we have from their requested length
			pos_start_fill = control->gtls_buffer_hack_len;	// set the pos to start filling at
			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");
		} 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
			hackhackhack = (char *) malloc(sizeof(char) * control->gtls_buffer_hack_len);	// strndup is NOT a good solution -- concatenates \0!!!! Anyway, make a new "hack" buffer.
			memcpy(hackhackhack, control->gtls_buffer_hack + length, control->gtls_buffer_hack_len);	// Move what's left into the new one
			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);
			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");
			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);
	iphone_mux_recv(control->connection, recv_buffer, (length * 1000), &bytes);
	log_debug_msg("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));
		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");
			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;
				control->gtls_buffer_hack = (char *) malloc(sizeof(char) * control->gtls_buffer_hack_len);
				memcpy(control->gtls_buffer_hack, recv_buffer + length, control->gtls_buffer_hack_len);
			} else {			// if there is. 
				control->gtls_buffer_hack =
					realloc(control->gtls_buffer_hack, control->gtls_buffer_hack_len + (bytes - length));
				memcpy(control->gtls_buffer_hack + control->gtls_buffer_hack_len, recv_buffer + length, bytes - length);
				control->gtls_buffer_hack_len += bytes - length;
			}
		}
		memcpy(buffer + pos_start_fill, recv_buffer, length);
		free(recv_buffer);
		if (bytes == length) {
			log_debug_msg("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);
			return length;
		}
	}
	return bytes;
}

/** Command to start the desired service
 *
 * @param control The lockdownd client
 * @param service The name of the service to start
 *
 * @return The port number the service was started on or 0 on failure.
 */
iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char *service, int *port)
{
	if (!client || !service || !port)
		return IPHONE_E_INVALID_ARG;

	char *host_id = get_host_id();
	if (!host_id)
		return IPHONE_E_INVALID_CONF;
	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;
	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);

	/* 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;
	plist_free(dict);
	dict = NULL;

	if (IPHONE_E_SUCCESS != ret)
		return ret;

	ret = iphone_lckd_recv(client, &XML_content, &bytes);

	if (IPHONE_E_SUCCESS != ret)
		return ret;

	plist_from_xml(XML_content, bytes, &dict);
	if (!dict)
		return IPHONE_E_PLIST_ERROR;


	if (bytes <= 0)
		return IPHONE_E_NOT_ENOUGH_DATA;
	else {

		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 = 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;
		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);

		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")) {
			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) {
			*port = port_loc;
			return IPHONE_E_SUCCESS;
		} else
			return IPHONE_E_UNKNOWN_ERROR;
	}

	return IPHONE_E_UNKNOWN_ERROR;
}