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
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
|
/*
* lockdown.c
* com.apple.mobile.lockdownd service implementation.
*
* Copyright (c) 2010 Bryan Forbes All Rights Reserved.
* 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
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <stdlib.h>
#define _GNU_SOURCE 1
#define __USE_GNU 1
#include <stdio.h>
#include <ctype.h>
#ifdef HAVE_OPENSSL
#include <openssl/pem.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#else
#include <libtasn1.h>
#include <gnutls/x509.h>
#endif
#include <plist/plist.h>
#include "property_list_service.h"
#include "lockdown.h"
#include "idevice.h"
#include "common/debug.h"
#include "common/userpref.h"
#include "asprintf.h"
#define RESULT_SUCCESS 0
#define RESULT_FAILURE 1
#ifndef HAVE_OPENSSL
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}
};
#endif
/**
* Internally used function for checking the result from lockdown's answer
* plist to a previously sent request.
*
* @param dict The plist to evaluate.
* @param query_match Name of the request to match or NULL if no match is
* required.
*
* @return RESULT_SUCCESS when the result is 'Success',
* RESULT_FAILURE when the result is 'Failure',
* or a negative value if an error occured during evaluation.
*/
static int lockdown_check_result(plist_t dict, const char *query_match)
{
int ret = -1;
plist_t query_node = plist_dict_get_item(dict, "Request");
if (!query_node) {
return ret;
}
if (plist_get_node_type(query_node) != PLIST_STRING) {
return ret;
} else {
char *query_value = NULL;
plist_get_string_val(query_node, &query_value);
if (!query_value) {
return ret;
}
if (query_match && (strcmp(query_value, query_match) != 0)) {
free(query_value);
return ret;
}
free(query_value);
}
plist_t result_node = plist_dict_get_item(dict, "Result");
if (!result_node) {
/* iOS 5: the 'Result' key is not present anymore.
But we need to check for the 'Error' key. */
plist_t err_node = plist_dict_get_item(dict, "Error");
if (err_node) {
if (plist_get_node_type(err_node) == PLIST_STRING) {
char *err_value = NULL;
plist_get_string_val(err_node, &err_value);
if (err_value) {
debug_info("ERROR: %s", err_value);
free(err_value);
} else {
debug_info("ERROR: unknown error occured");
}
}
return RESULT_FAILURE;
}
return RESULT_SUCCESS;
}
plist_type result_type = plist_get_node_type(result_node);
if (result_type == PLIST_STRING) {
char *result_value = NULL;
plist_get_string_val(result_node, &result_value);
if (result_value) {
if (!strcmp(result_value, "Success")) {
ret = RESULT_SUCCESS;
} else if (!strcmp(result_value, "Failure")) {
ret = RESULT_FAILURE;
} else {
debug_info("ERROR: unknown result value '%s'", result_value);
}
}
if (result_value)
free(result_value);
}
return ret;
}
/**
* Adds a label key with the passed value to a plist dict node.
*
* @param plist The plist to add the key to
* @param label The value for the label key
*
*/
static void plist_dict_add_label(plist_t plist, const char *label)
{
if (plist && label) {
if (plist_get_node_type(plist) == PLIST_DICT)
plist_dict_insert_item(plist, "Label", plist_new_string(label));
}
}
/**
* Closes the lockdownd session by sending the StopSession request.
*
* @see lockdownd_start_session
*
* @param client The lockdown client
* @param session_id The id of a running session
*
* @return LOCKDOWN_E_SUCCESS on success, NP_E_INVALID_ARG when client is NULL
*/
lockdownd_error_t lockdownd_stop_session(lockdownd_client_t client, const char *session_id)
{
if (!client)
return LOCKDOWN_E_INVALID_ARG;
if (!session_id) {
debug_info("no session_id given, cannot stop session");
return LOCKDOWN_E_INVALID_ARG;
}
lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
plist_t dict = plist_new_dict();
plist_dict_add_label(dict, client->label);
plist_dict_insert_item(dict,"Request", plist_new_string("StopSession"));
plist_dict_insert_item(dict,"SessionID", plist_new_string(session_id));
debug_info("stopping session %s", session_id);
ret = lockdownd_send(client, dict);
plist_free(dict);
dict = NULL;
ret = lockdownd_receive(client, &dict);
if (!dict) {
debug_info("LOCKDOWN_E_PLIST_ERROR");
return LOCKDOWN_E_PLIST_ERROR;
}
ret = LOCKDOWN_E_UNKNOWN_ERROR;
if (lockdown_check_result(dict, "StopSession") == RESULT_SUCCESS) {
debug_info("success");
ret = LOCKDOWN_E_SUCCESS;
}
plist_free(dict);
dict = NULL;
if (client->ssl_enabled) {
property_list_service_disable_ssl(client->parent);
}
return ret;
}
/**
* Closes the lockdownd client session if one is running and frees up the
* lockdownd_client struct.
*
* @param client The lockdown client
*
* @return LOCKDOWN_E_SUCCESS on success, NP_E_INVALID_ARG when client is NULL
*/
lockdownd_error_t lockdownd_client_free(lockdownd_client_t client)
{
if (!client)
return LOCKDOWN_E_INVALID_ARG;
lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
if (client->session_id) {
lockdownd_stop_session(client, client->session_id);
free(client->session_id);
}
if (client->parent) {
lockdownd_goodbye(client);
if (property_list_service_client_free(client->parent) == PROPERTY_LIST_SERVICE_E_SUCCESS) {
ret = LOCKDOWN_E_SUCCESS;
}
}
if (client->udid) {
free(client->udid);
}
if (client->label) {
free(client->label);
}
free(client);
return ret;
}
/**
* Sets the label to send for requests to lockdownd.
*
* @param client The lockdown client
* @param label The label to set or NULL to disable sending a label
*
*/
void lockdownd_client_set_label(lockdownd_client_t client, const char *label)
{
if (client) {
if (client->label)
free(client->label);
client->label = (label != NULL) ? strdup(label): NULL;
}
}
/**
* Receives a plist from lockdownd.
*
* @param client The lockdownd client
* @param plist The plist to store the received data
*
* @return LOCKDOWN_E_SUCCESS on success, NP_E_INVALID_ARG when client or
* plist is NULL
*/
lockdownd_error_t lockdownd_receive(lockdownd_client_t client, plist_t *plist)
{
if (!client || !plist || (plist && *plist))
return LOCKDOWN_E_INVALID_ARG;
lockdownd_error_t ret = LOCKDOWN_E_SUCCESS;
property_list_service_error_t err;
err = property_list_service_receive_plist(client->parent, plist);
if (err != PROPERTY_LIST_SERVICE_E_SUCCESS) {
ret = LOCKDOWN_E_UNKNOWN_ERROR;
}
if (!*plist)
ret = LOCKDOWN_E_PLIST_ERROR;
return ret;
}
/**
* Sends a plist to lockdownd.
*
* @note This function is low-level and should only be used if you need to send
* a new type of message.
*
* @param client The lockdownd client
* @param plist The plist to send
*
* @return LOCKDOWN_E_SUCCESS on success, NP_E_INVALID_ARG when client or
* plist is NULL
*/
lockdownd_error_t lockdownd_send(lockdownd_client_t client, plist_t plist)
{
if (!client || !plist)
return LOCKDOWN_E_INVALID_ARG;
lockdownd_error_t ret = LOCKDOWN_E_SUCCESS;
idevice_error_t err;
err = property_list_service_send_xml_plist(client->parent, plist);
if (err != PROPERTY_LIST_SERVICE_E_SUCCESS) {
ret = LOCKDOWN_E_UNKNOWN_ERROR;
}
return ret;
}
/**
* Query the type of the service daemon. Depending on whether the device is
* queried in normal mode or restore mode, different types will be returned.
*
* @param client The lockdownd client
* @param type The type returned by the service daemon. Pass NULL to ignore.
*
* @return LOCKDOWN_E_SUCCESS on success, NP_E_INVALID_ARG when client is NULL
*/
lockdownd_error_t lockdownd_query_type(lockdownd_client_t client, char **type)
{
if (!client)
return LOCKDOWN_E_INVALID_ARG;
lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
plist_t dict = plist_new_dict();
plist_dict_add_label(dict, client->label);
plist_dict_insert_item(dict,"Request", plist_new_string("QueryType"));
debug_info("called");
ret = lockdownd_send(client, dict);
plist_free(dict);
dict = NULL;
ret = lockdownd_receive(client, &dict);
if (LOCKDOWN_E_SUCCESS != ret)
return ret;
ret = LOCKDOWN_E_UNKNOWN_ERROR;
if (lockdown_check_result(dict, "QueryType") == RESULT_SUCCESS) {
/* return the type if requested */
if (type != NULL) {
plist_t type_node = plist_dict_get_item(dict, "Type");
plist_get_string_val(type_node, type);
}
debug_info("success with type %s", *type);
ret = LOCKDOWN_E_SUCCESS;
}
plist_free(dict);
dict = NULL;
return ret;
}
/**
* Retrieves a preferences plist using an optional domain and/or key name.
*
* @param client An initialized lockdownd client.
* @param domain The domain to query on or NULL for global domain
* @param key The key name to request or NULL to query for all keys
* @param value A plist node representing the result value node
*
* @return LOCKDOWN_E_SUCCESS on success, NP_E_INVALID_ARG when client is NULL
*/
lockdownd_error_t lockdownd_get_value(lockdownd_client_t client, const char *domain, const char *key, plist_t *value)
{
if (!client)
return LOCKDOWN_E_INVALID_ARG;
plist_t dict = NULL;
lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
/* setup request plist */
dict = plist_new_dict();
plist_dict_add_label(dict, client->label);
if (domain) {
plist_dict_insert_item(dict,"Domain", plist_new_string(domain));
}
if (key) {
plist_dict_insert_item(dict,"Key", plist_new_string(key));
}
plist_dict_insert_item(dict,"Request", plist_new_string("GetValue"));
/* send to device */
ret = lockdownd_send(client, dict);
plist_free(dict);
dict = NULL;
if (ret != LOCKDOWN_E_SUCCESS)
return ret;
/* Now get device's answer */
ret = lockdownd_receive(client, &dict);
if (ret != LOCKDOWN_E_SUCCESS)
return ret;
if (lockdown_check_result(dict, "GetValue") == RESULT_SUCCESS) {
debug_info("success");
ret = LOCKDOWN_E_SUCCESS;
}
if (ret != LOCKDOWN_E_SUCCESS) {
plist_free(dict);
return ret;
}
plist_t value_node = plist_dict_get_item(dict, "Value");
if (value_node) {
debug_info("has a value");
*value = plist_copy(value_node);
}
plist_free(dict);
return ret;
}
/**
* Sets a preferences value using a plist and optional by domain and/or key name.
*
* @param client an initialized lockdownd client.
* @param domain the domain to query on or NULL for global domain
* @param key the key name to set the value or NULL to set a value dict plist
* @param value a plist node of any node type representing the value to set
*
* @return LOCKDOWN_E_SUCCESS on success, NP_E_INVALID_ARG when client or
* value is NULL
*/
lockdownd_error_t lockdownd_set_value(lockdownd_client_t client, const char *domain, const char *key, plist_t value)
{
if (!client || !value)
return LOCKDOWN_E_INVALID_ARG;
plist_t dict = NULL;
lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
/* setup request plist */
dict = plist_new_dict();
plist_dict_add_label(dict, client->label);
if (domain) {
plist_dict_insert_item(dict,"Domain", plist_new_string(domain));
}
if (key) {
plist_dict_insert_item(dict,"Key", plist_new_string(key));
}
plist_dict_insert_item(dict,"Request", plist_new_string("SetValue"));
plist_dict_insert_item(dict,"Value", value);
/* send to device */
ret = lockdownd_send(client, dict);
plist_free(dict);
dict = NULL;
if (ret != LOCKDOWN_E_SUCCESS)
return ret;
/* Now get device's answer */
ret = lockdownd_receive(client, &dict);
if (ret != LOCKDOWN_E_SUCCESS)
return ret;
if (lockdown_check_result(dict, "SetValue") == RESULT_SUCCESS) {
debug_info("success");
ret = LOCKDOWN_E_SUCCESS;
}
if (ret != LOCKDOWN_E_SUCCESS) {
plist_free(dict);
return ret;
}
plist_free(dict);
return ret;
}
/**
* Removes a preference node by domain and/or key name.
*
* @note: Use with caution as this could remove vital information on the device
*
* @param client An initialized lockdownd client.
* @param domain The domain to query on or NULL for global domain
* @param key The key name to remove or NULL remove all keys for the current domain
*
* @return LOCKDOWN_E_SUCCESS on success, NP_E_INVALID_ARG when client is NULL
*/
lockdownd_error_t lockdownd_remove_value(lockdownd_client_t client, const char *domain, const char *key)
{
if (!client)
return LOCKDOWN_E_INVALID_ARG;
plist_t dict = NULL;
lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
/* setup request plist */
dict = plist_new_dict();
plist_dict_add_label(dict, client->label);
if (domain) {
plist_dict_insert_item(dict,"Domain", plist_new_string(domain));
}
if (key) {
plist_dict_insert_item(dict,"Key", plist_new_string(key));
}
plist_dict_insert_item(dict,"Request", plist_new_string("RemoveValue"));
/* send to device */
ret = lockdownd_send(client, dict);
plist_free(dict);
dict = NULL;
if (ret != LOCKDOWN_E_SUCCESS)
return ret;
/* Now get device's answer */
ret = lockdownd_receive(client, &dict);
if (ret != LOCKDOWN_E_SUCCESS)
return ret;
if (lockdown_check_result(dict, "RemoveValue") == RESULT_SUCCESS) {
debug_info("success");
ret = LOCKDOWN_E_SUCCESS;
}
if (ret != LOCKDOWN_E_SUCCESS) {
plist_free(dict);
return ret;
}
plist_free(dict);
return ret;
}
/**
* Returns the unique id of the device from lockdownd.
*
* @param client An initialized lockdownd client.
* @param udid Holds the unique id of the device. The caller is responsible
* for freeing the memory.
*
* @return LOCKDOWN_E_SUCCESS on success
*/
lockdownd_error_t lockdownd_get_device_udid(lockdownd_client_t client, char **udid)
{
lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
plist_t value = NULL;
ret = lockdownd_get_value(client, NULL, "UniqueDeviceID", &value);
if (ret != LOCKDOWN_E_SUCCESS) {
return ret;
}
plist_get_string_val(value, udid);
plist_free(value);
value = NULL;
return ret;
}
/**
* Retrieves the public key of the device from lockdownd.
*
* @param client An initialized lockdownd client.
* @param public_key Holds the public key of the device. The caller is
* responsible for freeing the memory.
*
* @return LOCKDOWN_E_SUCCESS on success
*/
lockdownd_error_t lockdownd_get_device_public_key(lockdownd_client_t client, key_data_t * public_key)
{
lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
plist_t value = NULL;
char *value_value = NULL;
uint64_t size = 0;
ret = lockdownd_get_value(client, NULL, "DevicePublicKey", &value);
if (ret != LOCKDOWN_E_SUCCESS) {
return ret;
}
plist_get_data_val(value, &value_value, &size);
public_key->data = (unsigned char*)value_value;
public_key->size = size;
plist_free(value);
value = NULL;
return ret;
}
/**
* Retrieves the name of the device from lockdownd set by the user.
*
* @param client An initialized lockdownd client.
* @param device_name Holds the name of the device. The caller is
* responsible for freeing the memory.
*
* @return LOCKDOWN_E_SUCCESS on success
*/
lockdownd_error_t lockdownd_get_device_name(lockdownd_client_t client, char **device_name)
{
lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
plist_t value = NULL;
ret = lockdownd_get_value(client, NULL, "DeviceName", &value);
if (ret != LOCKDOWN_E_SUCCESS) {
return ret;
}
plist_get_string_val(value, device_name);
plist_free(value);
value = NULL;
return ret;
}
/**
* Creates a new lockdownd client for the device.
*
* @note This function does not pair with the device or start a session. This
* has to be done manually by the caller after the client is created.
* The device disconnects automatically if the lockdown connection idles
* for more than 10 seconds. Make sure to call lockdownd_client_free() as soon
* as the connection is no longer needed.
*
* @param device The device to create a lockdownd client for
* @param client The pointer to the location of the new lockdownd_client
* @param label The label to use for communication. Usually the program name.
*
* @return LOCKDOWN_E_SUCCESS on success, NP_E_INVALID_ARG when client is NULL
*/
lockdownd_error_t lockdownd_client_new(idevice_t device, lockdownd_client_t *client, const char *label)
{
if (!client)
return LOCKDOWN_E_INVALID_ARG;
static struct lockdownd_service_descriptor service = {
.port = 0xf27e,
.ssl_enabled = 0
};
property_list_service_client_t plistclient = NULL;
if (property_list_service_client_new(device, (lockdownd_service_descriptor_t)&service, &plistclient) != PROPERTY_LIST_SERVICE_E_SUCCESS) {
debug_info("could not connect to lockdownd (device %s)", device->udid);
return LOCKDOWN_E_MUX_ERROR;
}
lockdownd_client_t client_loc = (lockdownd_client_t) malloc(sizeof(struct lockdownd_client_private));
client_loc->parent = plistclient;
client_loc->ssl_enabled = 0;
client_loc->session_id = NULL;
if (idevice_get_udid(device, &client_loc->udid) != IDEVICE_E_SUCCESS) {
debug_info("failed to get device udid.");
}
debug_info("device udid: %s", client_loc->udid);
client_loc->label = label ? strdup(label) : NULL;
*client = client_loc;
return LOCKDOWN_E_SUCCESS;
}
/**
* Creates a new lockdownd client for the device and starts initial handshake.
* The handshake consists out of query_type, validate_pair, pair and
* start_session calls. It uses the internal pairing record management.
*
* @note The device disconnects automatically if the lockdown connection idles
* for more than 10 seconds. Make sure to call lockdownd_client_free() as soon
* as the connection is no longer needed.
*
* @param device The device to create a lockdownd client for
* @param client The pointer to the location of the new lockdownd_client
* @param label The label to use for communication. Usually the program name.
* Pass NULL to disable sending the label in requests to lockdownd.
*
* @return LOCKDOWN_E_SUCCESS on success, NP_E_INVALID_ARG when client is NULL,
* LOCKDOWN_E_INVALID_CONF if configuration data is wrong
*/
lockdownd_error_t lockdownd_client_new_with_handshake(idevice_t device, lockdownd_client_t *client, const char *label)
{
if (!client)
return LOCKDOWN_E_INVALID_ARG;
lockdownd_error_t ret = LOCKDOWN_E_SUCCESS;
lockdownd_client_t client_loc = NULL;
char *host_id = NULL;
char *type = NULL;
ret = lockdownd_client_new(device, &client_loc, label);
if (LOCKDOWN_E_SUCCESS != ret) {
debug_info("failed to create lockdownd client.");
return ret;
}
/* perform handshake */
if (LOCKDOWN_E_SUCCESS != lockdownd_query_type(client_loc, &type)) {
debug_info("QueryType failed in the lockdownd client.");
ret = LOCKDOWN_E_NOT_ENOUGH_DATA;
} else {
if (strcmp("com.apple.mobile.lockdown", type)) {
debug_info("Warning QueryType request returned \"%s\".", type);
}
if (type)
free(type);
}
userpref_get_host_id(&host_id);
if (LOCKDOWN_E_SUCCESS == ret && !host_id) {
ret = LOCKDOWN_E_INVALID_CONF;
}
if (LOCKDOWN_E_SUCCESS == ret && !userpref_has_device_public_key(client_loc->udid))
ret = lockdownd_pair(client_loc, NULL);
/* in any case, we need to validate pairing to receive trusted host status */
ret = lockdownd_validate_pair(client_loc, NULL);
/* if not paired yet, let's do it now */
if (LOCKDOWN_E_INVALID_HOST_ID == ret) {
ret = lockdownd_pair(client_loc, NULL);
if (LOCKDOWN_E_SUCCESS == ret) {
ret = lockdownd_validate_pair(client_loc, NULL);
}
}
if (LOCKDOWN_E_SUCCESS == ret) {
ret = lockdownd_start_session(client_loc, host_id, NULL, NULL);
if (LOCKDOWN_E_SUCCESS != ret) {
debug_info("Session opening failed.");
}
if (host_id) {
free(host_id);
host_id = NULL;
}
}
if (LOCKDOWN_E_SUCCESS == ret) {
*client = client_loc;
} else {
lockdownd_client_free(client_loc);
}
return ret;
}
/**
* Returns a new plist from the supplied lockdownd pair record. The caller is
* responsible for freeing the plist.
*
* @param pair_record The pair record to create a plist from.
*
* @return A pair record plist from the device, NULL if pair_record is not set
*/
static plist_t lockdownd_pair_record_to_plist(lockdownd_pair_record_t pair_record)
{
if (!pair_record)
return NULL;
char *host_id_loc = pair_record->host_id;
/* setup request plist */
plist_t dict = plist_new_dict();
plist_dict_insert_item(dict, "DeviceCertificate", plist_new_data(pair_record->device_certificate, strlen(pair_record->device_certificate)));
plist_dict_insert_item(dict, "HostCertificate", plist_new_data(pair_record->host_certificate, strlen(pair_record->host_certificate)));
if (!pair_record->host_id)
userpref_get_host_id(&host_id_loc);
plist_dict_insert_item(dict, "HostID", plist_new_string(host_id_loc));
plist_dict_insert_item(dict, "RootCertificate", plist_new_data(pair_record->root_certificate, strlen(pair_record->root_certificate)));
if (!pair_record->host_id)
free(host_id_loc);
return dict;
}
/**
* Generates a new pairing record plist and required certificates for the
* supplied public key of the device and the host_id of the caller's host
* computer.
*
* @param public_key The public key of the device.
* @param host_id The HostID to use for the pair record plist.
* @param pair_record_plist Holds the generated pair record.
*
* @return LOCKDOWN_E_SUCCESS on success
*/
static lockdownd_error_t generate_pair_record_plist(key_data_t public_key, char *host_id, plist_t *pair_record_plist)
{
lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
key_data_t device_cert = { NULL, 0 };
key_data_t host_cert = { NULL, 0 };
key_data_t root_cert = { NULL, 0 };
ret = lockdownd_gen_pair_cert(public_key, &device_cert, &host_cert, &root_cert);
if (ret != LOCKDOWN_E_SUCCESS) {
return ret;
}
char *host_id_loc = host_id;
if (!host_id)
userpref_get_host_id(&host_id_loc);
/* setup request plist */
*pair_record_plist = plist_new_dict();
plist_dict_insert_item(*pair_record_plist, "DeviceCertificate", plist_new_data((const char*)device_cert.data, device_cert.size));
plist_dict_insert_item(*pair_record_plist, "HostCertificate", plist_new_data((const char*)host_cert.data, host_cert.size));
plist_dict_insert_item(*pair_record_plist, "HostID", plist_new_string(host_id_loc));
plist_dict_insert_item(*pair_record_plist, "RootCertificate", plist_new_data((const char*)root_cert.data, root_cert.size));
if (!host_id)
free(host_id_loc);
if (device_cert.data)
free(device_cert.data);
if (host_cert.data)
free(host_cert.data);
if (root_cert.data)
free(root_cert.data);
return ret;
}
/**
* Function used internally by lockdownd_pair() and lockdownd_validate_pair()
*
* @param client The lockdown client to pair with.
* @param pair_record The pair record to use for pairing. If NULL is passed, then
* the pair records from the current machine are used. New records will be
* generated automatically when pairing is done for the first time.
* @param verb This is either "Pair", "ValidatePair" or "Unpair".
*
* @return LOCKDOWN_E_SUCCESS on success, NP_E_INVALID_ARG when client is NULL,
* LOCKDOWN_E_PLIST_ERROR if the pair_record certificates are wrong,
* LOCKDOWN_E_PAIRING_FAILED if the pairing failed,
* LOCKDOWN_E_PASSWORD_PROTECTED if the device is password protected,
* LOCKDOWN_E_INVALID_HOST_ID if the device does not know the caller's host id
*/
static lockdownd_error_t lockdownd_do_pair(lockdownd_client_t client, lockdownd_pair_record_t pair_record, const char *verb)
{
if (!client)
return LOCKDOWN_E_INVALID_ARG;
lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
plist_t dict = NULL;
plist_t dict_record = NULL;
key_data_t public_key = { NULL, 0 };
int pairing_mode = 0; /* 0 = libimobiledevice, 1 = external */
if (pair_record && pair_record->host_id) {
/* valid pair_record passed? */
if (!pair_record->device_certificate || !pair_record->host_certificate || !pair_record->root_certificate) {
return LOCKDOWN_E_PLIST_ERROR;
}
/* use passed pair_record */
dict_record = lockdownd_pair_record_to_plist(pair_record);
pairing_mode = 1;
} else {
ret = lockdownd_get_device_public_key(client, &public_key);
if (ret != LOCKDOWN_E_SUCCESS) {
if (public_key.data)
free(public_key.data);
debug_info("device refused to send public key.");
return ret;
}
debug_info("device public key follows:\n%.*s", public_key.size, public_key.data);
/* get libimobiledevice pair_record */
ret = generate_pair_record_plist(public_key, NULL, &dict_record);
if (ret != LOCKDOWN_E_SUCCESS) {
if (dict_record)
plist_free(dict_record);
return ret;
}
}
/* Setup Pair request plist */
dict = plist_new_dict();
plist_dict_add_label(dict, client->label);
plist_dict_insert_item(dict,"PairRecord", dict_record);
plist_dict_insert_item(dict, "Request", plist_new_string(verb));
/* send to device */
ret = lockdownd_send(client, dict);
plist_free(dict);
dict = NULL;
if (ret != LOCKDOWN_E_SUCCESS)
return ret;
/* Now get device's answer */
ret = lockdownd_receive(client, &dict);
if (ret != LOCKDOWN_E_SUCCESS)
return ret;
if (strcmp(verb, "Unpair") == 0) {
/* workaround for Unpair giving back ValidatePair,
* seems to be a bug in the device's fw */
if (lockdown_check_result(dict, NULL) != RESULT_SUCCESS) {
ret = LOCKDOWN_E_PAIRING_FAILED;
}
} else {
if (lockdown_check_result(dict, verb) != RESULT_SUCCESS) {
ret = LOCKDOWN_E_PAIRING_FAILED;
}
}
/* if pairing succeeded */
if (ret == LOCKDOWN_E_SUCCESS) {
debug_info("%s success", verb);
if (!pairing_mode) {
if (!strcmp("Unpair", verb)) {
/* remove public key from config */
userpref_remove_device_public_key(client->udid);
} else {
/* store public key in config */
userpref_set_device_public_key(client->udid, public_key);
}
}
} else {
debug_info("%s failure", verb);
plist_t error_node = NULL;
/* verify error condition */
error_node = plist_dict_get_item(dict, "Error");
if (error_node) {
char *value = NULL;
plist_get_string_val(error_node, &value);
if (value) {
/* the first pairing fails if the device is password protected */
if (!strcmp(value, "PasswordProtected")) {
ret = LOCKDOWN_E_PASSWORD_PROTECTED;
} else if (!strcmp(value, "InvalidHostID")) {
ret = LOCKDOWN_E_INVALID_HOST_ID;
}
free(value);
}
plist_free(error_node);
error_node = NULL;
}
}
plist_free(dict);
dict = NULL;
if (public_key.data)
free(public_key.data);
return ret;
}
/**
* Pairs the device using the supplied pair record.
*
* @param client The lockdown client to pair with.
* @param pair_record The pair record to use for pairing. If NULL is passed, then
* the pair records from the current machine are used. New records will be
* generated automatically when pairing is done for the first time.
*
* @return LOCKDOWN_E_SUCCESS on success, NP_E_INVALID_ARG when client is NULL,
* LOCKDOWN_E_PLIST_ERROR if the pair_record certificates are wrong,
* LOCKDOWN_E_PAIRING_FAILED if the pairing failed,
* LOCKDOWN_E_PASSWORD_PROTECTED if the device is password protected,
* LOCKDOWN_E_INVALID_HOST_ID if the device does not know the caller's host id
*/
lockdownd_error_t lockdownd_pair(lockdownd_client_t client, lockdownd_pair_record_t pair_record)
{
return lockdownd_do_pair(client, pair_record, "Pair");
}
/**
* Validates if the device is paired with the given HostID. If succeeded them
* specified host will become trusted host of the device indicated by the
* lockdownd preference named TrustedHostAttached. Otherwise the host must because
* paired using lockdownd_pair() first.
*
* @param client The lockdown client to pair with.
* @param pair_record The pair record to validate pairing with. If NULL is
* passed, then the pair record is read from the internal pairing record
* management.
*
* @return LOCKDOWN_E_SUCCESS on success, NP_E_INVALID_ARG when client is NULL,
* LOCKDOWN_E_PLIST_ERROR if the pair_record certificates are wrong,
* LOCKDOWN_E_PAIRING_FAILED if the pairing failed,
* LOCKDOWN_E_PASSWORD_PROTECTED if the device is password protected,
* LOCKDOWN_E_INVALID_HOST_ID if the device does not know the caller's host id
*/
lockdownd_error_t lockdownd_validate_pair(lockdownd_client_t client, lockdownd_pair_record_t pair_record)
{
return lockdownd_do_pair(client, pair_record, "ValidatePair");
}
/**
* Unpairs the device with the given HostID and removes the pairing records
* from the device and host if the internal pairing record management is used.
*
* @param client The lockdown client to pair with.
* @param pair_record The pair record to use for unpair. If NULL is passed, then
* the pair records from the current machine are used.
*
* @return LOCKDOWN_E_SUCCESS on success, NP_E_INVALID_ARG when client is NULL,
* LOCKDOWN_E_PLIST_ERROR if the pair_record certificates are wrong,
* LOCKDOWN_E_PAIRING_FAILED if the pairing failed,
* LOCKDOWN_E_PASSWORD_PROTECTED if the device is password protected,
* LOCKDOWN_E_INVALID_HOST_ID if the device does not know the caller's host id
*/
lockdownd_error_t lockdownd_unpair(lockdownd_client_t client, lockdownd_pair_record_t pair_record)
{
return lockdownd_do_pair(client, pair_record, "Unpair");
}
/**
* Tells the device to immediately enter recovery mode.
*
* @param client The lockdown client
*
* @return LOCKDOWN_E_SUCCESS on success, NP_E_INVALID_ARG when client is NULL
*/
lockdownd_error_t lockdownd_enter_recovery(lockdownd_client_t client)
{
if (!client)
return LOCKDOWN_E_INVALID_ARG;
lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
plist_t dict = plist_new_dict();
plist_dict_add_label(dict, client->label);
plist_dict_insert_item(dict,"Request", plist_new_string("EnterRecovery"));
debug_info("telling device to enter recovery mode");
ret = lockdownd_send(client, dict);
plist_free(dict);
dict = NULL;
ret = lockdownd_receive(client, &dict);
if (lockdown_check_result(dict, "EnterRecovery") == RESULT_SUCCESS) {
debug_info("success");
ret = LOCKDOWN_E_SUCCESS;
}
plist_free(dict);
dict = NULL;
return ret;
}
/**
* Sends the Goodbye request to lockdownd signaling the end of communication.
*
* @param client The lockdown client
*
* @return LOCKDOWN_E_SUCCESS on success, LOCKDOWN_E_INVALID_ARG when client
* is NULL, LOCKDOWN_E_PLIST_ERROR if the device did not acknowledge the
* request
*/
lockdownd_error_t lockdownd_goodbye(lockdownd_client_t client)
{
if (!client)
return LOCKDOWN_E_INVALID_ARG;
lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
plist_t dict = plist_new_dict();
plist_dict_add_label(dict, client->label);
plist_dict_insert_item(dict,"Request", plist_new_string("Goodbye"));
debug_info("called");
ret = lockdownd_send(client, dict);
plist_free(dict);
dict = NULL;
ret = lockdownd_receive(client, &dict);
if (!dict) {
debug_info("did not get goodbye response back");
return LOCKDOWN_E_PLIST_ERROR;
}
if (lockdown_check_result(dict, "Goodbye") == RESULT_SUCCESS) {
debug_info("success");
ret = LOCKDOWN_E_SUCCESS;
}
plist_free(dict);
dict = NULL;
return ret;
}
/**
* Generates the device certificate from the public key as well as the host
* and root certificates.
*
* @param public_key The public key of the device to use for generation.
* @param odevice_cert Holds the generated device certificate.
* @param ohost_cert Holds the generated host certificate.
* @param oroot_cert Holds the generated root certificate.
*
* @return LOCKDOWN_E_SUCCESS on success, LOCKDOWN_E_INVALID_ARG when a
* parameter is NULL, LOCKDOWN_E_INVALID_CONF if the internal configuration
* system failed, LOCKDOWN_E_SSL_ERROR if the certificates could not be
* generated
*/
lockdownd_error_t lockdownd_gen_pair_cert(key_data_t public_key, key_data_t * odevice_cert,
key_data_t * ohost_cert, key_data_t * oroot_cert)
{
if (!public_key.data || !odevice_cert || !ohost_cert || !oroot_cert)
return LOCKDOWN_E_INVALID_ARG;
lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
userpref_error_t uret = USERPREF_E_UNKNOWN_ERROR;
#ifdef HAVE_OPENSSL
BIO *membio = BIO_new_mem_buf(public_key.data, public_key.size);
RSA *pubkey = NULL;
if (!PEM_read_bio_RSAPublicKey(membio, &pubkey, NULL, NULL)) {
debug_info("Could not read public key");
}
BIO_free(membio);
/* now generate certificates */
key_data_t root_privkey, host_privkey;
key_data_t root_cert, host_cert;
X509* dev_cert;
root_cert.data = NULL;
root_cert.size = 0;
host_cert.data = NULL;
host_cert.size = 0;
dev_cert = X509_new();
root_privkey.data = NULL;
root_privkey.size = 0;
host_privkey.data = NULL;
host_privkey.size = 0;
uret = userpref_get_keys_and_certs(&root_privkey, &root_cert, &host_privkey, &host_cert);
if (USERPREF_E_SUCCESS == uret) {
/* generate device certificate */
ASN1_INTEGER* sn = ASN1_INTEGER_new();
ASN1_INTEGER_set(sn, 0);
X509_set_serialNumber(dev_cert, sn);
ASN1_INTEGER_free(sn);
X509_set_version(dev_cert, 2);
X509_EXTENSION* ext;
if (!(ext = X509V3_EXT_conf_nid(NULL, NULL, NID_basic_constraints, (char*)"critical,CA:FALSE"))) {
debug_info("ERROR: X509V3_EXT_conf_nid failed");
}
X509_add_ext(dev_cert, ext, -1);
ASN1_TIME* asn1time = ASN1_TIME_new();
ASN1_TIME_set(asn1time, time(NULL));
X509_set_notBefore(dev_cert, asn1time);
ASN1_TIME_set(asn1time, time(NULL) + (60 * 60 * 24 * 365 * 10));
X509_set_notAfter(dev_cert, asn1time);
ASN1_TIME_free(asn1time);
BIO* membp;
X509* rootCert = NULL;
membp = BIO_new_mem_buf(root_cert.data, root_cert.size);
PEM_read_bio_X509(membp, &rootCert, NULL, NULL);
BIO_free(membp);
if (!rootCert) {
debug_info("Could not read RootCertificate");
} else {
debug_info("RootCertificate loaded");
EVP_PKEY* pkey = EVP_PKEY_new();
EVP_PKEY_assign_RSA(pkey, pubkey);
X509_set_pubkey(dev_cert, pkey);
EVP_PKEY_free(pkey);
X509_free(rootCert);
}
EVP_PKEY* rootPriv = NULL;
membp = BIO_new_mem_buf(root_privkey.data, root_privkey.size);
PEM_read_bio_PrivateKey(membp, &rootPriv, NULL, NULL);
BIO_free(membp);
if (!rootPriv) {
debug_info("Could not read RootPrivateKey");
} else {
debug_info("RootPrivateKey loaded");
if (X509_sign(dev_cert, rootPriv, EVP_sha1())) {
ret = LOCKDOWN_E_SUCCESS;
} else {
debug_info("signing failed");
}
EVP_PKEY_free(rootPriv);
}
if (LOCKDOWN_E_SUCCESS == ret) {
/* if everything went well, export in PEM format */
key_data_t pem_root_cert = { NULL, 0 };
key_data_t pem_host_cert = { NULL, 0 };
uret = userpref_get_certs_as_pem(&pem_root_cert, &pem_host_cert);
if (USERPREF_E_SUCCESS == uret) {
/* copy buffer for output */
membp = BIO_new(BIO_s_mem());
if (membp && PEM_write_bio_X509(membp, dev_cert) > 0) {
odevice_cert->size = BIO_get_mem_data(membp, &odevice_cert->data);
}
if (membp)
free(membp);
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;
free(pem_root_cert.data);
free(pem_host_cert.data);
}
}
}
X509V3_EXT_cleanup();
X509_free(dev_cert);
switch(uret) {
case USERPREF_E_INVALID_ARG:
ret = LOCKDOWN_E_INVALID_ARG;
break;
case USERPREF_E_INVALID_CONF:
ret = LOCKDOWN_E_INVALID_CONF;
break;
case USERPREF_E_SSL_ERROR:
ret = LOCKDOWN_E_SSL_ERROR;
default:
break;
}
if (root_cert.data)
free(root_cert.data);
if (host_cert.data)
free(host_cert.data);
if (root_privkey.data)
free(root_privkey.data);
if (host_privkey.data)
free(host_privkey.data);
#else
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, (int*)&modulus.size);
int ret2 = asn1_read_value(asn1_pub_key, "publicExponent", NULL, (int*)&exponent.size);
modulus.data = gnutls_malloc(modulus.size);
exponent.data = gnutls_malloc(exponent.size);
ret1 = asn1_read_value(asn1_pub_key, "modulus", modulus.data, (int*)&modulus.size);
ret2 = asn1_read_value(asn1_pub_key, "publicExponent", exponent.data, (int*)&exponent.size);
if (ASN1_SUCCESS == ret1 && ASN1_SUCCESS == ret2)
ret = LOCKDOWN_E_SUCCESS;
}
if (asn1_pub_key)
asn1_delete_structure(&asn1_pub_key);
}
if (pkcs1)
asn1_delete_structure(&pkcs1);
}
/* now generate certificates */
if (LOCKDOWN_E_SUCCESS == ret && 0 != modulus.size && 0 != exponent.size) {
gnutls_global_init();
gnutls_datum_t essentially_null = { (unsigned char*)strdup("abababababababab"), strlen("abababababababab") };
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);
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);
gnutls_x509_privkey_init(&host_privkey);
uret = userpref_get_keys_and_certs(root_privkey, root_cert, host_privkey, host_cert);
if (USERPREF_E_SUCCESS == uret) {
/* 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 (LOCKDOWN_E_SUCCESS == ret) {
/* if everything went well, export in PEM format */
size_t export_size = 0;
gnutls_datum_t dev_pem = { NULL, 0 };
gnutls_x509_crt_export(dev_cert, GNUTLS_X509_FMT_PEM, NULL, &export_size);
dev_pem.data = gnutls_malloc(export_size);
gnutls_x509_crt_export(dev_cert, GNUTLS_X509_FMT_PEM, dev_pem.data, &export_size);
dev_pem.size = export_size;
gnutls_datum_t pem_root_cert = { NULL, 0 };
gnutls_datum_t pem_host_cert = { NULL, 0 };
uret = userpref_get_certs_as_pem(&pem_root_cert, &pem_host_cert);
if (USERPREF_E_SUCCESS == uret) {
/* 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_cert.data);
gnutls_free(pem_host_cert.data);
if (dev_pem.data)
gnutls_free(dev_pem.data);
}
}
}
switch(uret) {
case USERPREF_E_INVALID_ARG:
ret = LOCKDOWN_E_INVALID_ARG;
break;
case USERPREF_E_INVALID_CONF:
ret = LOCKDOWN_E_INVALID_CONF;
break;
case USERPREF_E_SSL_ERROR:
ret = LOCKDOWN_E_SSL_ERROR;
default:
break;
}
}
if (essentially_null.data)
free(essentially_null.data);
gnutls_x509_crt_deinit(dev_cert);
gnutls_x509_crt_deinit(root_cert);
gnutls_x509_crt_deinit(host_cert);
gnutls_x509_privkey_deinit(fake_privkey);
gnutls_x509_privkey_deinit(root_privkey);
gnutls_x509_privkey_deinit(host_privkey);
}
gnutls_free(modulus.data);
gnutls_free(exponent.data);
gnutls_free(der_pub_key.data);
#endif
return ret;
}
/**
* Opens a session with lockdownd and switches to SSL mode if device wants it.
*
* @param client The lockdownd client
* @param host_id The HostID of the computer
* @param session_id The new session_id of the created session
* @param ssl_enabled Whether SSL communication is used in the session
*
* @return LOCKDOWN_E_SUCCESS on success, LOCKDOWN_E_INVALID_ARG when a client
* or host_id is NULL, LOCKDOWN_E_PLIST_ERROR if the response plist had errors,
* LOCKDOWN_E_INVALID_HOST_ID if the device does not know the supplied HostID,
* LOCKDOWN_E_SSL_ERROR if enabling SSL communication failed
*/
lockdownd_error_t lockdownd_start_session(lockdownd_client_t client, const char *host_id, char **session_id, int *ssl_enabled)
{
lockdownd_error_t ret = LOCKDOWN_E_SUCCESS;
plist_t dict = NULL;
if (!client || !host_id)
ret = LOCKDOWN_E_INVALID_ARG;
/* if we have a running session, stop current one first */
if (client->session_id) {
lockdownd_stop_session(client, client->session_id);
free(client->session_id);
}
/* setup request plist */
dict = plist_new_dict();
plist_dict_add_label(dict, client->label);
plist_dict_insert_item(dict,"HostID", plist_new_string(host_id));
plist_dict_insert_item(dict,"Request", plist_new_string("StartSession"));
ret = lockdownd_send(client, dict);
plist_free(dict);
dict = NULL;
if (ret != LOCKDOWN_E_SUCCESS)
return ret;
ret = lockdownd_receive(client, &dict);
if (!dict)
return LOCKDOWN_E_PLIST_ERROR;
if (lockdown_check_result(dict, "StartSession") == RESULT_FAILURE) {
plist_t error_node = plist_dict_get_item(dict, "Error");
if (error_node && PLIST_STRING == plist_get_node_type(error_node)) {
char *error = NULL;
plist_get_string_val(error_node, &error);
if (!strcmp(error, "InvalidHostID")) {
ret = LOCKDOWN_E_INVALID_HOST_ID;
}
free(error);
}
} else {
uint8_t use_ssl = 0;
plist_t enable_ssl = plist_dict_get_item(dict, "EnableSessionSSL");
if (enable_ssl && (plist_get_node_type(enable_ssl) == PLIST_BOOLEAN)) {
plist_get_bool_val(enable_ssl, &use_ssl);
}
debug_info("Session startup OK");
if (ssl_enabled != NULL)
*ssl_enabled = use_ssl;
/* store session id, we need it for StopSession */
plist_t session_node = plist_dict_get_item(dict, "SessionID");
if (session_node && (plist_get_node_type(session_node) == PLIST_STRING)) {
plist_get_string_val(session_node, &client->session_id);
}
if (client->session_id) {
debug_info("SessionID: %s", client->session_id);
if (session_id != NULL)
*session_id = strdup(client->session_id);
} else {
debug_info("Failed to get SessionID!");
}
debug_info("Enable SSL Session: %s", (use_ssl?"true":"false"));
if (use_ssl) {
ret = property_list_service_enable_ssl(client->parent);
if (ret == PROPERTY_LIST_SERVICE_E_SUCCESS) {
client->ssl_enabled = 1;
} else {
ret = LOCKDOWN_E_SSL_ERROR;
client->ssl_enabled = 0;
}
} else {
client->ssl_enabled = 0;
ret = LOCKDOWN_E_SUCCESS;
}
}
plist_free(dict);
dict = NULL;
return ret;
}
/**
* Requests to start a service and retrieve it's port on success.
*
* @param client The lockdownd client
* @param identifier The identifier of the service to start
* @param descriptor The service descriptor on success or NULL on failure
* @return LOCKDOWN_E_SUCCESS on success, NP_E_INVALID_ARG if a parameter
* is NULL, LOCKDOWN_E_INVALID_SERVICE if the requested service is not known
* by the device, LOCKDOWN_E_START_SERVICE_FAILED if the service could not because
* started by the device
*/
lockdownd_error_t lockdownd_start_service(lockdownd_client_t client, const char *identifier, lockdownd_service_descriptor_t *service)
{
if (!client || !identifier || !service)
return LOCKDOWN_E_INVALID_ARG;
if (*service) {
// reset fields if service descriptor is reused
(*service)->port = 0;
(*service)->ssl_enabled = 0;
}
char *host_id = NULL;
userpref_get_host_id(&host_id);
if (!host_id)
return LOCKDOWN_E_INVALID_CONF;
if (!client->session_id)
return LOCKDOWN_E_NO_RUNNING_SESSION;
plist_t dict = NULL;
uint16_t port_loc = 0;
lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
free(host_id);
host_id = NULL;
dict = plist_new_dict();
plist_dict_add_label(dict, client->label);
plist_dict_insert_item(dict,"Request", plist_new_string("StartService"));
plist_dict_insert_item(dict,"Service", plist_new_string(identifier));
/* send to device */
ret = lockdownd_send(client, dict);
plist_free(dict);
dict = NULL;
if (LOCKDOWN_E_SUCCESS != ret)
return ret;
ret = lockdownd_receive(client, &dict);
if (LOCKDOWN_E_SUCCESS != ret)
return ret;
if (!dict)
return LOCKDOWN_E_PLIST_ERROR;
ret = LOCKDOWN_E_UNKNOWN_ERROR;
if (lockdown_check_result(dict, "StartService") == RESULT_SUCCESS) {
if (*service == NULL)
*service = (lockdownd_service_descriptor_t)malloc(sizeof(struct lockdownd_service_descriptor));
(*service)->port = 0;
(*service)->ssl_enabled = 0;
/* read service port number */
plist_t node = plist_dict_get_item(dict, "Port");
if (node && (plist_get_node_type(node) == PLIST_UINT)) {
uint64_t port_value = 0;
plist_get_uint_val(node, &port_value);
if (port_value) {
port_loc = port_value;
ret = LOCKDOWN_E_SUCCESS;
}
if (port_loc && ret == LOCKDOWN_E_SUCCESS) {
(*service)->port = port_loc;
}
}
/* check if the service requires SSL */
node = plist_dict_get_item(dict, "EnableServiceSSL");
if (node && (plist_get_node_type(node) == PLIST_BOOLEAN)) {
uint8_t b = 0;
plist_get_bool_val(node, &b);
(*service)->ssl_enabled = b;
}
} else {
ret = LOCKDOWN_E_START_SERVICE_FAILED;
plist_t error_node = plist_dict_get_item(dict, "Error");
if (error_node && PLIST_STRING == plist_get_node_type(error_node)) {
char *error = NULL;
plist_get_string_val(error_node, &error);
if (!strcmp(error, "InvalidService")) {
ret = LOCKDOWN_E_INVALID_SERVICE;
}
free(error);
}
}
plist_free(dict);
dict = NULL;
return ret;
}
/**
* Activates the device. Only works within an open session.
* The ActivationRecord plist dictionary must be obtained using the
* activation protocol requesting from Apple's https webservice.
*
* @see http://iphone-docs.org/doku.php?id=docs:protocols:activation
*
* @param client The lockdown client
* @param activation_record The activation record plist dictionary
*
* @return LOCKDOWN_E_SUCCESS on success, NP_E_INVALID_ARG when client or
* activation_record is NULL, LOCKDOWN_E_NO_RUNNING_SESSION if no session is
* open, LOCKDOWN_E_PLIST_ERROR if the received plist is broken,
* LOCKDOWN_E_ACTIVATION_FAILED if the activation failed,
* LOCKDOWN_E_INVALID_ACTIVATION_RECORD if the device reports that the
* activation_record is invalid
*/
lockdownd_error_t lockdownd_activate(lockdownd_client_t client, plist_t activation_record)
{
if (!client)
return LOCKDOWN_E_INVALID_ARG;
if (!client->session_id)
return LOCKDOWN_E_NO_RUNNING_SESSION;
if (!activation_record)
return LOCKDOWN_E_INVALID_ARG;
lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
plist_t dict = plist_new_dict();
plist_dict_add_label(dict, client->label);
plist_dict_insert_item(dict,"Request", plist_new_string("Activate"));
plist_dict_insert_item(dict,"ActivationRecord", plist_copy(activation_record));
ret = lockdownd_send(client, dict);
plist_free(dict);
dict = NULL;
ret = lockdownd_receive(client, &dict);
if (!dict) {
debug_info("LOCKDOWN_E_PLIST_ERROR");
return LOCKDOWN_E_PLIST_ERROR;
}
ret = LOCKDOWN_E_ACTIVATION_FAILED;
if (lockdown_check_result(dict, "Activate") == RESULT_SUCCESS) {
debug_info("success");
ret = LOCKDOWN_E_SUCCESS;
} else {
plist_t error_node = plist_dict_get_item(dict, "Error");
if (error_node && PLIST_STRING == plist_get_node_type(error_node)) {
char *error = NULL;
plist_get_string_val(error_node, &error);
if (!strcmp(error, "InvalidActivationRecord")) {
ret = LOCKDOWN_E_INVALID_ACTIVATION_RECORD;
}
free(error);
}
}
plist_free(dict);
dict = NULL;
return ret;
}
/**
* Deactivates the device, returning it to the locked “Activate with iTunes”
* screen.
*
* @param client The lockdown client
*
* @return LOCKDOWN_E_SUCCESS on success, NP_E_INVALID_ARG when client is NULL,
* LOCKDOWN_E_NO_RUNNING_SESSION if no session is open,
* LOCKDOWN_E_PLIST_ERROR if the received plist is broken
*/
lockdownd_error_t lockdownd_deactivate(lockdownd_client_t client)
{
if (!client)
return LOCKDOWN_E_INVALID_ARG;
if (!client->session_id)
return LOCKDOWN_E_NO_RUNNING_SESSION;
lockdownd_error_t ret = LOCKDOWN_E_UNKNOWN_ERROR;
plist_t dict = plist_new_dict();
plist_dict_add_label(dict, client->label);
plist_dict_insert_item(dict,"Request", plist_new_string("Deactivate"));
ret = lockdownd_send(client, dict);
plist_free(dict);
dict = NULL;
ret = lockdownd_receive(client, &dict);
if (!dict) {
debug_info("LOCKDOWN_E_PLIST_ERROR");
return LOCKDOWN_E_PLIST_ERROR;
}
ret = LOCKDOWN_E_UNKNOWN_ERROR;
if (lockdown_check_result(dict, "Deactivate") == RESULT_SUCCESS) {
debug_info("success");
ret = LOCKDOWN_E_SUCCESS;
}
plist_free(dict);
dict = NULL;
return ret;
}
static void str_remove_spaces(char *source)
{
char *dest = source;
while (*source != 0) {
if (!isspace(*source)) {
*dest++ = *source; /* copy */
}
source++;
}
*dest = 0;
}
/**
* Calculates and returns the data classes the device supports from lockdownd.
*
* @param client An initialized lockdownd client.
* @param classes A pointer to store an array of class names. The caller is responsible
* for freeing the memory which can be done using mobilesync_data_classes_free().
* @param count The number of items in the classes array.
*
* @return LOCKDOWN_E_SUCCESS on success,
* LOCKDOWN_E_INVALID_ARG when client is NULL,
* LOCKDOWN_E_NO_RUNNING_SESSION if no session is open,
* LOCKDOWN_E_PLIST_ERROR if the received plist is broken
*/
lockdownd_error_t lockdownd_get_sync_data_classes(lockdownd_client_t client, char ***classes, int *count)
{
if (!client)
return LOCKDOWN_E_INVALID_ARG;
if (!client->session_id)
return LOCKDOWN_E_NO_RUNNING_SESSION;
plist_t dict = NULL;
lockdownd_error_t err = LOCKDOWN_E_UNKNOWN_ERROR;
plist_t value = NULL;
char **newlist = NULL;
char *val = NULL;
*classes = NULL;
*count = 0;
err = lockdownd_get_value(client, "com.apple.mobile.iTunes", "SyncDataClasses", &dict);
if (err != LOCKDOWN_E_SUCCESS) {
if (dict) {
plist_free(dict);
}
return err;
}
if (plist_get_node_type(dict) != PLIST_ARRAY) {
plist_free(dict);
return LOCKDOWN_E_PLIST_ERROR;
}
while((value = plist_array_get_item(dict, *count)) != NULL) {
plist_get_string_val(value, &val);
newlist = realloc(*classes, sizeof(char*) * (*count+1));
str_remove_spaces(val);
asprintf(&newlist[*count], "com.apple.%s", val);
free(val);
val = NULL;
*classes = newlist;
*count = *count+1;
}
newlist = realloc(*classes, sizeof(char*) * (*count+1));
newlist[*count] = NULL;
*classes = newlist;
if (dict) {
plist_free(dict);
}
return LOCKDOWN_E_SUCCESS;
}
/**
* Frees memory of an allocated array of data classes as returned by lockdownd_get_sync_data_classes()
*
* @param classes An array of class names to free.
*
* @return LOCKDOWN_E_SUCCESS on success
*/
lockdownd_error_t lockdownd_data_classes_free(char **classes)
{
if (classes) {
int i = 0;
while (classes[i++]) {
free(classes[i]);
}
free(classes);
}
return LOCKDOWN_E_SUCCESS;
}
/**
* Frees memory of a service descriptor as returned by lockdownd_start_service()
*
* @param sevice A service descriptor instance to free.
*
* @return LOCKDOWN_E_SUCCESS on success
*/
lockdownd_error_t lockdownd_service_descriptor_free(lockdownd_service_descriptor_t service)
{
if (service)
free(service);
return LOCKDOWN_E_SUCCESS;
}
|