summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2021-07-27 03:36:45 +0200
committerGravatar Nikias Bassen2021-07-27 03:36:45 +0200
commitab1c815340afc2883c9ef08795eebd9bed1858c5 (patch)
treea188e92dfdff14e501bf42b5d880155468cffbdd /tools
parent19eca85f90d9950596e1c6021d7b0f89407f5d7f (diff)
downloadlibimobiledevice-ab1c815340afc2883c9ef08795eebd9bed1858c5.tar.gz
libimobiledevice-ab1c815340afc2883c9ef08795eebd9bed1858c5.tar.bz2
Add support for MbedTLS
Diffstat (limited to 'tools')
-rw-r--r--tools/Makefile.am16
-rw-r--r--tools/idevicebackup.c137
2 files changed, 65 insertions, 88 deletions
diff --git a/tools/Makefile.am b/tools/Makefile.am
index 04a5faa..b78f3f2 100644
--- a/tools/Makefile.am
+++ b/tools/Makefile.am
@@ -4,18 +4,10 @@ AM_CPPFLAGS = \
4 4
5AM_CFLAGS = \ 5AM_CFLAGS = \
6 $(GLOBAL_CFLAGS) \ 6 $(GLOBAL_CFLAGS) \
7 $(libgnutls_CFLAGS) \
8 $(libtasn1_CFLAGS) \
9 $(libgcrypt_CFLAGS) \
10 $(openssl_CFLAGS) \
11 $(libplist_CFLAGS) \ 7 $(libplist_CFLAGS) \
12 $(LFS_CFLAGS) 8 $(LFS_CFLAGS)
13 9
14AM_LDFLAGS = \ 10AM_LDFLAGS = \
15 $(libgnutls_LIBS) \
16 $(libtasn1_LIBS) \
17 $(libgcrypt_LIBS) \
18 $(openssl_LIBS) \
19 $(libplist_LIBS) 11 $(libplist_LIBS)
20 12
21bin_PROGRAMS = \ 13bin_PROGRAMS = \
@@ -49,8 +41,8 @@ idevicename_LDFLAGS = $(AM_LDFLAGS)
49idevicename_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la 41idevicename_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la
50 42
51idevicepair_SOURCES = idevicepair.c 43idevicepair_SOURCES = idevicepair.c
52idevicepair_CFLAGS = -I$(top_srcdir) $(AM_CFLAGS) 44idevicepair_CFLAGS = -I$(top_srcdir) $(AM_CFLAGS) $(ssl_lib_CFLAGS)
53idevicepair_LDFLAGS = $(AM_LDFLAGS) $(libusbmuxd_LIBS) 45idevicepair_LDFLAGS = $(AM_LDFLAGS) $(libusbmuxd_LIBS) $(ssl_lib_LIBS)
54idevicepair_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la $(top_builddir)/common/libinternalcommon.la 46idevicepair_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la $(top_builddir)/common/libinternalcommon.la
55 47
56idevicesyslog_SOURCES = idevicesyslog.c 48idevicesyslog_SOURCES = idevicesyslog.c
@@ -64,8 +56,8 @@ idevice_id_LDFLAGS = $(AM_LDFLAGS)
64idevice_id_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la 56idevice_id_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la
65 57
66idevicebackup_SOURCES = idevicebackup.c 58idevicebackup_SOURCES = idevicebackup.c
67idevicebackup_CFLAGS = $(AM_CFLAGS) 59idevicebackup_CFLAGS = $(AM_CFLAGS) $(ssl_lib_CFLAGS)
68idevicebackup_LDFLAGS = $(AM_LDFLAGS) 60idevicebackup_LDFLAGS = $(AM_LDFLAGS) $(ssl_lib_LIBS)
69idevicebackup_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la $(top_builddir)/common/libinternalcommon.la 61idevicebackup_LDADD = $(top_builddir)/src/libimobiledevice-1.0.la $(top_builddir)/common/libinternalcommon.la
70 62
71idevicebackup2_SOURCES = idevicebackup2.c 63idevicebackup2_SOURCES = idevicebackup2.c
diff --git a/tools/idevicebackup.c b/tools/idevicebackup.c
index dfd7b8b..8d0f74b 100644
--- a/tools/idevicebackup.c
+++ b/tools/idevicebackup.c
@@ -31,10 +31,20 @@
31#include <errno.h> 31#include <errno.h>
32#include <stdlib.h> 32#include <stdlib.h>
33#include <signal.h> 33#include <signal.h>
34#ifdef HAVE_OPENSSL 34#if defined(HAVE_OPENSSL)
35#include <openssl/sha.h> 35#include <openssl/sha.h>
36#else 36#elif defined(HAVE_GNUTLS)
37#include <gcrypt.h> 37#include <gcrypt.h>
38#elif defined(HAVE_MBEDTLS)
39#include <mbedtls/sha1.h>
40#if MBEDTLS_VERSION_NUMBER < 0x03000000
41#define mbedtls_sha1 mbedtls_sha1_ret
42#define mbedtls_sha1_starts mbedtls_sha1_starts_ret
43#define mbedtls_sha1_update mbedtls_sha1_update_ret
44#define mbedtls_sha1_finish mbedtls_sha1_finish_ret
45#endif
46#else
47#error No supported crypto library enabled
38#endif 48#endif
39#include <unistd.h> 49#include <unistd.h>
40#include <ctype.h> 50#include <ctype.h>
@@ -78,10 +88,12 @@ enum device_link_file_status_t {
78 88
79static void sha1_of_data(const char *input, uint32_t size, unsigned char *hash_out) 89static void sha1_of_data(const char *input, uint32_t size, unsigned char *hash_out)
80{ 90{
81#ifdef HAVE_OPENSSL 91#if defined(HAVE_OPENSSL)
82 SHA1((const unsigned char*)input, size, hash_out); 92 SHA1((const unsigned char*)input, size, hash_out);
83#else 93#elif defined(HAVE_GNUTLS)
84 gcry_md_hash_buffer(GCRY_MD_SHA1, hash_out, input, size); 94 gcry_md_hash_buffer(GCRY_MD_SHA1, hash_out, input, size);
95#elif defined(HAVE_MBEDTLS)
96 mbedtls_sha1((unsigned char*)input, size, hash_out);
85#endif 97#endif
86} 98}
87 99
@@ -96,12 +108,24 @@ static int compare_hash(const unsigned char *hash1, const unsigned char *hash2,
96 return 1; 108 return 1;
97} 109}
98 110
111static void _sha1_update(void* context, const char* data, size_t len)
112{
113#if defined(HAVE_OPENSSL)
114 SHA1_Update(context, data, len);
115#elif defined(HAVE_GNUTLS)
116 gcry_md_write(context, data, len);
117#elif defined(HAVE_MBEDTLS)
118 mbedtls_sha1_update(context, (const unsigned char*)data, len);
119#endif
120}
121
99static 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) 122static 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)
100{ 123{
101#ifdef HAVE_OPENSSL 124#if defined(HAVE_OPENSSL)
102 SHA_CTX sha1; 125 SHA_CTX sha1;
103 SHA1_Init(&sha1); 126 SHA1_Init(&sha1);
104#else 127 void* psha1 = &sha1;
128#elif defined(HAVE_GNUTLS)
105 gcry_md_hd_t hd = NULL; 129 gcry_md_hd_t hd = NULL;
106 gcry_md_open(&hd, GCRY_MD_SHA1, 0); 130 gcry_md_open(&hd, GCRY_MD_SHA1, 0);
107 if (!hd) { 131 if (!hd) {
@@ -109,102 +133,63 @@ static void compute_datahash(const char *path, const char *destpath, uint8_t gre
109 return; 133 return;
110 } 134 }
111 gcry_md_reset(hd); 135 gcry_md_reset(hd);
136 void* psha1 = hd;
137#elif defined(HAVE_MBEDTLS)
138 mbedtls_sha1_context sha1;
139 mbedtls_sha1_init(&sha1);
140 mbedtls_sha1_starts(&sha1);
141 void* psha1 = &sha1;
112#endif 142#endif
113 FILE *f = fopen(path, "rb"); 143 FILE *f = fopen(path, "rb");
114 if (f) { 144 if (f) {
115 unsigned char buf[16384]; 145 unsigned char buf[16384];
116 size_t len; 146 size_t len;
117 while ((len = fread(buf, 1, 16384, f)) > 0) { 147 while ((len = fread(buf, 1, 16384, f)) > 0) {
118#ifdef HAVE_OPENSSL 148 _sha1_update(psha1, (const char*)buf, len);
119 SHA1_Update(&sha1, buf, len);
120#else
121 gcry_md_write(hd, buf, len);
122#endif
123 } 149 }
124 fclose(f); 150 fclose(f);
125#ifdef HAVE_OPENSSL 151 _sha1_update(psha1, destpath, strlen(destpath));
126 SHA1_Update(&sha1, destpath, strlen(destpath)); 152 _sha1_update(psha1, ";", 1);
127 SHA1_Update(&sha1, ";", 1); 153
128#else
129 gcry_md_write(hd, destpath, strlen(destpath));
130 gcry_md_write(hd, ";", 1);
131#endif
132 if (greylist == 1) { 154 if (greylist == 1) {
133#ifdef HAVE_OPENSSL 155 _sha1_update(psha1, "true", 4);
134 SHA1_Update(&sha1, "true", 4);
135#else
136 gcry_md_write(hd, "true", 4);
137#endif
138 } else { 156 } else {
139#ifdef HAVE_OPENSSL 157 _sha1_update(psha1, "false", 5);
140 SHA1_Update(&sha1, "false", 5);
141#else
142 gcry_md_write(hd, "false", 5);
143#endif
144 } 158 }
145#ifdef HAVE_OPENSSL 159 _sha1_update(psha1, ";", 1);
146 SHA1_Update(&sha1, ";", 1); 160
147#else
148 gcry_md_write(hd, ";", 1);
149#endif
150 if (domain) { 161 if (domain) {
151#ifdef HAVE_OPENSSL 162 _sha1_update(psha1, domain, strlen(domain));
152 SHA1_Update(&sha1, domain, strlen(domain));
153#else
154 gcry_md_write(hd, domain, strlen(domain));
155#endif
156 } else { 163 } else {
157#ifdef HAVE_OPENSSL 164 _sha1_update(psha1, "(null)", 6);
158 SHA1_Update(&sha1, "(null)", 6);
159#else
160 gcry_md_write(hd, "(null)", 6);
161#endif
162 } 165 }
163#ifdef HAVE_OPENSSL 166 _sha1_update(psha1, ";", 1);
164 SHA1_Update(&sha1, ";", 1); 167
165#else
166 gcry_md_write(hd, ";", 1);
167#endif
168 if (appid) { 168 if (appid) {
169#ifdef HAVE_OPENSSL 169 _sha1_update(psha1, appid, strlen(appid));
170 SHA1_Update(&sha1, appid, strlen(appid));
171#else
172 gcry_md_write(hd, appid, strlen(appid));
173#endif
174 } else { 170 } else {
175#ifdef HAVE_OPENSSL 171 _sha1_update(psha1, "(null)", 6);
176 SHA1_Update(&sha1, "(null)", 6);
177#else
178 gcry_md_write(hd, "(null)", 6);
179#endif
180 } 172 }
181#ifdef HAVE_OPENSSL 173 _sha1_update(psha1, ";", 1);
182 SHA1_Update(&sha1, ";", 1); 174
183#else
184 gcry_md_write(hd, ";", 1);
185#endif
186 if (version) { 175 if (version) {
187#ifdef HAVE_OPENSSL 176 _sha1_update(psha1, version, strlen(version));
188 SHA1_Update(&sha1, version, strlen(version));
189#else
190 gcry_md_write(hd, version, strlen(version));
191#endif
192 } else { 177 } else {
193#ifdef HAVE_OPENSSL 178 _sha1_update(psha1, "(null)", 6);
194 SHA1_Update(&sha1, "(null)", 6);
195#else
196 gcry_md_write(hd, "(null)", 6);
197#endif
198 } 179 }
199#ifdef HAVE_OPENSSL 180#if defined(HAVE_OPENSSL)
200 SHA1_Final(hash_out, &sha1); 181 SHA1_Final(hash_out, &sha1);
201#else 182#elif defined(HAVE_GNUTLS)
202 unsigned char *newhash = gcry_md_read(hd, GCRY_MD_SHA1); 183 unsigned char *newhash = gcry_md_read(hd, GCRY_MD_SHA1);
203 memcpy(hash_out, newhash, 20); 184 memcpy(hash_out, newhash, 20);
185#elif defined(HAVE_MBEDTLS)
186 mbedtls_sha1_finish(&sha1, hash_out);
204#endif 187#endif
205 } 188 }
206#ifndef HAVE_OPENSSL 189#if defined(HAVE_GNUTLS)
207 gcry_md_close(hd); 190 gcry_md_close(hd);
191#elif defined(HAVE_MBEDTLS)
192 mbedtls_sha1_free(&sha1);
208#endif 193#endif
209} 194}
210 195