diff options
-rw-r--r-- | configure.ac | 2 | ||||
-rw-r--r-- | tools/idevicebackup.c | 129 |
2 files changed, 24 insertions, 107 deletions
diff --git a/configure.ac b/configure.ac index 9856ae2..f1b9c76 100644 --- a/configure.ac +++ b/configure.ac | |||
@@ -27,7 +27,7 @@ fi | |||
27 | dnl Minimum package versions | 27 | dnl Minimum package versions |
28 | LIBUSBMUXD_VERSION=2.0.2 | 28 | LIBUSBMUXD_VERSION=2.0.2 |
29 | LIBPLIST_VERSION=2.3.0 | 29 | LIBPLIST_VERSION=2.3.0 |
30 | LIMD_GLUE_VERSION=1.0.0 | 30 | LIMD_GLUE_VERSION=1.3.0 |
31 | 31 | ||
32 | AC_SUBST(LIBUSBMUXD_VERSION) | 32 | AC_SUBST(LIBUSBMUXD_VERSION) |
33 | AC_SUBST(LIBPLIST_VERSION) | 33 | AC_SUBST(LIBPLIST_VERSION) |
diff --git a/tools/idevicebackup.c b/tools/idevicebackup.c index 5694c12..c0537b8 100644 --- a/tools/idevicebackup.c +++ b/tools/idevicebackup.c | |||
@@ -32,24 +32,6 @@ | |||
32 | #include <stdlib.h> | 32 | #include <stdlib.h> |
33 | #include <signal.h> | 33 | #include <signal.h> |
34 | #include <getopt.h> | 34 | #include <getopt.h> |
35 | #if defined(HAVE_OPENSSL) | ||
36 | #include <openssl/sha.h> | ||
37 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L | ||
38 | #include <openssl/evp.h> | ||
39 | #endif | ||
40 | #elif defined(HAVE_GNUTLS) | ||
41 | #include <gcrypt.h> | ||
42 | #elif defined(HAVE_MBEDTLS) | ||
43 | #include <mbedtls/sha1.h> | ||
44 | #if MBEDTLS_VERSION_NUMBER < 0x03000000 | ||
45 | #define mbedtls_sha1 mbedtls_sha1_ret | ||
46 | #define mbedtls_sha1_starts mbedtls_sha1_starts_ret | ||
47 | #define mbedtls_sha1_update mbedtls_sha1_update_ret | ||
48 | #define mbedtls_sha1_finish mbedtls_sha1_finish_ret | ||
49 | #endif | ||
50 | #else | ||
51 | #error No supported crypto library enabled | ||
52 | #endif | ||
53 | #include <unistd.h> | 35 | #include <unistd.h> |
54 | #include <ctype.h> | 36 | #include <ctype.h> |
55 | #include <time.h> | 37 | #include <time.h> |
@@ -59,6 +41,7 @@ | |||
59 | #include <libimobiledevice/mobilebackup.h> | 41 | #include <libimobiledevice/mobilebackup.h> |
60 | #include <libimobiledevice/notification_proxy.h> | 42 | #include <libimobiledevice/notification_proxy.h> |
61 | #include <libimobiledevice/afc.h> | 43 | #include <libimobiledevice/afc.h> |
44 | #include <libimobiledevice-glue/sha.h> | ||
62 | #include <libimobiledevice-glue/utils.h> | 45 | #include <libimobiledevice-glue/utils.h> |
63 | #include <plist/plist.h> | 46 | #include <plist/plist.h> |
64 | 47 | ||
@@ -91,17 +74,6 @@ enum device_link_file_status_t { | |||
91 | DEVICE_LINK_FILE_STATUS_LAST_HUNK | 74 | DEVICE_LINK_FILE_STATUS_LAST_HUNK |
92 | }; | 75 | }; |
93 | 76 | ||
94 | static void sha1_of_data(const char *input, uint32_t size, unsigned char *hash_out) | ||
95 | { | ||
96 | #if defined(HAVE_OPENSSL) | ||
97 | SHA1((const unsigned char*)input, size, hash_out); | ||
98 | #elif defined(HAVE_GNUTLS) | ||
99 | gcry_md_hash_buffer(GCRY_MD_SHA1, hash_out, input, size); | ||
100 | #elif defined(HAVE_MBEDTLS) | ||
101 | mbedtls_sha1((unsigned char*)input, size, hash_out); | ||
102 | #endif | ||
103 | } | ||
104 | |||
105 | static int compare_hash(const unsigned char *hash1, const unsigned char *hash2, int hash_len) | 77 | static int compare_hash(const unsigned char *hash1, const unsigned char *hash2, int hash_len) |
106 | { | 78 | { |
107 | int i; | 79 | int i; |
@@ -113,104 +85,49 @@ static int compare_hash(const unsigned char *hash1, const unsigned char *hash2, | |||
113 | return 1; | 85 | return 1; |
114 | } | 86 | } |
115 | 87 | ||
116 | static void _sha1_update(void* context, const char* data, size_t len) | ||
117 | { | ||
118 | #if defined(HAVE_OPENSSL) | ||
119 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L | ||
120 | EVP_DigestUpdate(context, data, len); | ||
121 | #else | ||
122 | SHA1_Update(context, data, len); | ||
123 | #endif | ||
124 | #elif defined(HAVE_GNUTLS) | ||
125 | gcry_md_write(context, data, len); | ||
126 | #elif defined(HAVE_MBEDTLS) | ||
127 | mbedtls_sha1_update(context, (const unsigned char*)data, len); | ||
128 | #endif | ||
129 | } | ||
130 | |||
131 | static void compute_datahash(const char *path, const char *destpath, uint8_t greylist, const char *domain, const char *appid, const char *version, unsigned char *hash_out) | 88 | static void compute_datahash(const char *path, const char *destpath, uint8_t greylist, const char *domain, const char *appid, const char *version, unsigned char *hash_out) |
132 | { | 89 | { |
133 | #if defined(HAVE_OPENSSL) | 90 | sha1_context sha1; |
134 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L | 91 | sha1_init(&sha1); |
135 | EVP_MD_CTX* sha1 = EVP_MD_CTX_new(); | ||
136 | EVP_DigestInit(sha1, EVP_sha1()); | ||
137 | void* psha1 = sha1; | ||
138 | #else | ||
139 | SHA_CTX sha1; | ||
140 | SHA1_Init(&sha1); | ||
141 | void* psha1 = &sha1; | ||
142 | #endif | ||
143 | #elif defined(HAVE_GNUTLS) | ||
144 | gcry_md_hd_t hd = NULL; | ||
145 | gcry_md_open(&hd, GCRY_MD_SHA1, 0); | ||
146 | if (!hd) { | ||
147 | printf("ERROR: Could not initialize libgcrypt/SHA1\n"); | ||
148 | return; | ||
149 | } | ||
150 | gcry_md_reset(hd); | ||
151 | void* psha1 = hd; | ||
152 | #elif defined(HAVE_MBEDTLS) | ||
153 | mbedtls_sha1_context sha1; | ||
154 | mbedtls_sha1_init(&sha1); | ||
155 | mbedtls_sha1_starts(&sha1); | ||
156 | void* psha1 = &sha1; | ||
157 | #endif | ||
158 | FILE *f = fopen(path, "rb"); | 92 | FILE *f = fopen(path, "rb"); |
159 | if (f) { | 93 | if (f) { |
160 | unsigned char buf[16384]; | 94 | unsigned char buf[16384]; |
161 | size_t len; | 95 | size_t len; |
162 | while ((len = fread(buf, 1, 16384, f)) > 0) { | 96 | while ((len = fread(buf, 1, 16384, f)) > 0) { |
163 | _sha1_update(psha1, (const char*)buf, len); | 97 | sha1_update(&sha1, buf, len); |
164 | } | 98 | } |
165 | fclose(f); | 99 | fclose(f); |
166 | _sha1_update(psha1, destpath, strlen(destpath)); | 100 | sha1_update(&sha1, destpath, strlen(destpath)); |
167 | _sha1_update(psha1, ";", 1); | 101 | sha1_update(&sha1, ";", 1); |
168 | 102 | ||
169 | if (greylist == 1) { | 103 | if (greylist == 1) { |
170 | _sha1_update(psha1, "true", 4); | 104 | sha1_update(&sha1, "true", 4); |
171 | } else { | 105 | } else { |
172 | _sha1_update(psha1, "false", 5); | 106 | sha1_update(&sha1, "false", 5); |
173 | } | 107 | } |
174 | _sha1_update(psha1, ";", 1); | 108 | sha1_update(&sha1, ";", 1); |
175 | 109 | ||
176 | if (domain) { | 110 | if (domain) { |
177 | _sha1_update(psha1, domain, strlen(domain)); | 111 | sha1_update(&sha1, domain, strlen(domain)); |
178 | } else { | 112 | } else { |
179 | _sha1_update(psha1, "(null)", 6); | 113 | sha1_update(&sha1, "(null)", 6); |
180 | } | 114 | } |
181 | _sha1_update(psha1, ";", 1); | 115 | sha1_update(&sha1, ";", 1); |
182 | 116 | ||
183 | if (appid) { | 117 | if (appid) { |
184 | _sha1_update(psha1, appid, strlen(appid)); | 118 | sha1_update(&sha1, appid, strlen(appid)); |
185 | } else { | 119 | } else { |
186 | _sha1_update(psha1, "(null)", 6); | 120 | sha1_update(&sha1, "(null)", 6); |
187 | } | 121 | } |
188 | _sha1_update(psha1, ";", 1); | 122 | sha1_update(&sha1, ";", 1); |
189 | 123 | ||
190 | if (version) { | 124 | if (version) { |
191 | _sha1_update(psha1, version, strlen(version)); | 125 | sha1_update(&sha1, version, strlen(version)); |
192 | } else { | 126 | } else { |
193 | _sha1_update(psha1, "(null)", 6); | 127 | sha1_update(&sha1, "(null)", 6); |
194 | } | 128 | } |
195 | #if defined(HAVE_OPENSSL) | 129 | sha1_final(&sha1, hash_out); |
196 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L | ||
197 | EVP_DigestFinal(sha1, hash_out, NULL); | ||
198 | EVP_MD_CTX_destroy(sha1); | ||
199 | #else | ||
200 | SHA1_Final(hash_out, &sha1); | ||
201 | #endif | ||
202 | #elif defined(HAVE_GNUTLS) | ||
203 | unsigned char *newhash = gcry_md_read(hd, GCRY_MD_SHA1); | ||
204 | memcpy(hash_out, newhash, 20); | ||
205 | #elif defined(HAVE_MBEDTLS) | ||
206 | mbedtls_sha1_finish(&sha1, hash_out); | ||
207 | #endif | ||
208 | } | 130 | } |
209 | #if defined(HAVE_GNUTLS) | ||
210 | gcry_md_close(hd); | ||
211 | #elif defined(HAVE_MBEDTLS) | ||
212 | mbedtls_sha1_free(&sha1); | ||
213 | #endif | ||
214 | } | 131 | } |
215 | 132 | ||
216 | static void print_hash(const unsigned char *hash, int len) | 133 | static void print_hash(const unsigned char *hash, int len) |
@@ -547,7 +464,7 @@ static int mobilebackup_check_file_integrity(const char *backup_directory, const | |||
547 | unsigned char fnhash[20]; | 464 | unsigned char fnhash[20]; |
548 | char fnamehash[41]; | 465 | char fnamehash[41]; |
549 | char *p = fnamehash; | 466 | char *p = fnamehash; |
550 | sha1_of_data(fnstr, strlen(fnstr), fnhash); | 467 | sha1((const unsigned char*)fnstr, strlen(fnstr), fnhash); |
551 | free(fnstr); | 468 | free(fnstr); |
552 | int i; | 469 | int i; |
553 | for ( i = 0; i < 20; i++, p += 2 ) { | 470 | for ( i = 0; i < 20; i++, p += 2 ) { |
@@ -1285,14 +1202,14 @@ files_out: | |||
1285 | } | 1202 | } |
1286 | 1203 | ||
1287 | printf("Verifying backup integrity, please wait.\n"); | 1204 | printf("Verifying backup integrity, please wait.\n"); |
1288 | char *bin = NULL; | 1205 | unsigned char *bin = NULL; |
1289 | uint64_t binsize = 0; | 1206 | uint64_t binsize = 0; |
1290 | node = plist_dict_get_item(manifest_plist, "Data"); | 1207 | node = plist_dict_get_item(manifest_plist, "Data"); |
1291 | if (!node || (plist_get_node_type(node) != PLIST_DATA)) { | 1208 | if (!node || (plist_get_node_type(node) != PLIST_DATA)) { |
1292 | printf("Could not read Data key from Manifest.plist!\n"); | 1209 | printf("Could not read Data key from Manifest.plist!\n"); |
1293 | break; | 1210 | break; |
1294 | } | 1211 | } |
1295 | plist_get_data_val(node, &bin, &binsize); | 1212 | plist_get_data_val(node, (char**)&bin, &binsize); |
1296 | plist_t backup_data = NULL; | 1213 | plist_t backup_data = NULL; |
1297 | if (bin) { | 1214 | if (bin) { |
1298 | char *auth_ver = NULL; | 1215 | char *auth_ver = NULL; |
@@ -1309,7 +1226,7 @@ files_out: | |||
1309 | if (auth_sig && (auth_sig_len == 20)) { | 1226 | if (auth_sig && (auth_sig_len == 20)) { |
1310 | /* calculate the sha1, then compare */ | 1227 | /* calculate the sha1, then compare */ |
1311 | unsigned char data_sha1[20]; | 1228 | unsigned char data_sha1[20]; |
1312 | sha1_of_data(bin, binsize, data_sha1); | 1229 | sha1(bin, binsize, data_sha1); |
1313 | if (compare_hash(auth_sig, data_sha1, 20)) { | 1230 | if (compare_hash(auth_sig, data_sha1, 20)) { |
1314 | printf("AuthSignature is valid\n"); | 1231 | printf("AuthSignature is valid\n"); |
1315 | } else { | 1232 | } else { |
@@ -1322,7 +1239,7 @@ files_out: | |||
1322 | } else if (auth_ver) { | 1239 | } else if (auth_ver) { |
1323 | printf("Unknown AuthVersion '%s', cannot verify AuthSignature\n", auth_ver); | 1240 | printf("Unknown AuthVersion '%s', cannot verify AuthSignature\n", auth_ver); |
1324 | } | 1241 | } |
1325 | plist_from_bin(bin, (uint32_t)binsize, &backup_data); | 1242 | plist_from_bin((char*)bin, (uint32_t)binsize, &backup_data); |
1326 | free(bin); | 1243 | free(bin); |
1327 | } | 1244 | } |
1328 | if (!backup_data) { | 1245 | if (!backup_data) { |