diff options
| author | 2011-09-14 18:55:59 +0200 | |
|---|---|---|
| committer | 2012-03-19 01:43:29 +0100 | |
| commit | 8b1af4cf80eff619d3465925dce7fe572fc09224 (patch) | |
| tree | 1fc167114f574c2ff3470c97d6ce74938778f9db /tools/idevicebackup.c | |
| parent | 294cf69b256419e407b1eac04634752412ee7756 (diff) | |
| download | libimobiledevice-8b1af4cf80eff619d3465925dce7fe572fc09224.tar.gz libimobiledevice-8b1af4cf80eff619d3465925dce7fe572fc09224.tar.bz2 | |
Add OpenSSL support
Diffstat (limited to 'tools/idevicebackup.c')
| -rw-r--r-- | tools/idevicebackup.c | 77 |
1 files changed, 76 insertions, 1 deletions
diff --git a/tools/idevicebackup.c b/tools/idevicebackup.c index 867eaad..f744e70 100644 --- a/tools/idevicebackup.c +++ b/tools/idevicebackup.c | |||
| @@ -20,13 +20,21 @@ | |||
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | */ | 21 | */ |
| 22 | 22 | ||
| 23 | #ifdef HAVE_CONFIG_H | ||
| 24 | #include <config.h> | ||
| 25 | #endif | ||
| 26 | |||
| 23 | #include <stdio.h> | 27 | #include <stdio.h> |
| 24 | #include <string.h> | 28 | #include <string.h> |
| 25 | #include <errno.h> | 29 | #include <errno.h> |
| 26 | #include <stdlib.h> | 30 | #include <stdlib.h> |
| 27 | #include <signal.h> | 31 | #include <signal.h> |
| 28 | #include <glib.h> | 32 | #include <glib.h> |
| 33 | #ifdef HAVE_OPENSSL | ||
| 34 | #include <openssl/sha.h> | ||
| 35 | #else | ||
| 29 | #include <gcrypt.h> | 36 | #include <gcrypt.h> |
| 37 | #endif | ||
| 30 | #include <unistd.h> | 38 | #include <unistd.h> |
| 31 | 39 | ||
| 32 | #include <libimobiledevice/libimobiledevice.h> | 40 | #include <libimobiledevice/libimobiledevice.h> |
| @@ -66,7 +74,11 @@ enum device_link_file_status_t { | |||
| 66 | 74 | ||
| 67 | static void sha1_of_data(const char *input, uint32_t size, unsigned char *hash_out) | 75 | static void sha1_of_data(const char *input, uint32_t size, unsigned char *hash_out) |
| 68 | { | 76 | { |
| 77 | #ifdef HAVE_OPENSSL | ||
| 78 | SHA1((const unsigned char*)input, size, hash_out); | ||
| 79 | #else | ||
| 69 | gcry_md_hash_buffer(GCRY_MD_SHA1, hash_out, input, size); | 80 | gcry_md_hash_buffer(GCRY_MD_SHA1, hash_out, input, size); |
| 81 | #endif | ||
| 70 | } | 82 | } |
| 71 | 83 | ||
| 72 | static int compare_hash(const unsigned char *hash1, const unsigned char *hash2, int hash_len) | 84 | static int compare_hash(const unsigned char *hash1, const unsigned char *hash2, int hash_len) |
| @@ -82,6 +94,10 @@ static int compare_hash(const unsigned char *hash1, const unsigned char *hash2, | |||
| 82 | 94 | ||
| 83 | 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) | 95 | 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) |
| 84 | { | 96 | { |
| 97 | #ifdef HAVE_OPENSSL | ||
| 98 | SHA_CTX sha1; | ||
| 99 | SHA1_Init(&sha1); | ||
| 100 | #else | ||
| 85 | gcry_md_hd_t hd = NULL; | 101 | gcry_md_hd_t hd = NULL; |
| 86 | gcry_md_open(&hd, GCRY_MD_SHA1, 0); | 102 | gcry_md_open(&hd, GCRY_MD_SHA1, 0); |
| 87 | if (!hd) { | 103 | if (!hd) { |
| @@ -89,44 +105,103 @@ static void compute_datahash(const char *path, const char *destpath, uint8_t gre | |||
| 89 | return; | 105 | return; |
| 90 | } | 106 | } |
| 91 | gcry_md_reset(hd); | 107 | gcry_md_reset(hd); |
| 92 | 108 | #endif | |
| 93 | FILE *f = fopen(path, "rb"); | 109 | FILE *f = fopen(path, "rb"); |
| 94 | if (f) { | 110 | if (f) { |
| 95 | unsigned char buf[16384]; | 111 | unsigned char buf[16384]; |
| 96 | size_t len; | 112 | size_t len; |
| 97 | while ((len = fread(buf, 1, 16384, f)) > 0) { | 113 | while ((len = fread(buf, 1, 16384, f)) > 0) { |
| 114 | #ifdef HAVE_OPENSSL | ||
| 115 | SHA1_Update(&sha1, buf, len); | ||
| 116 | #else | ||
| 98 | gcry_md_write(hd, buf, len); | 117 | gcry_md_write(hd, buf, len); |
| 118 | #endif | ||
| 99 | } | 119 | } |
| 100 | fclose(f); | 120 | fclose(f); |
| 121 | #ifdef HAVE_OPENSSL | ||
| 122 | SHA1_Update(&sha1, destpath, strlen(destpath)); | ||
| 123 | SHA1_Update(&sha1, ";", 1); | ||
| 124 | #else | ||
| 101 | gcry_md_write(hd, destpath, strlen(destpath)); | 125 | gcry_md_write(hd, destpath, strlen(destpath)); |
| 102 | gcry_md_write(hd, ";", 1); | 126 | gcry_md_write(hd, ";", 1); |
| 127 | #endif | ||
| 103 | if (greylist == 1) { | 128 | if (greylist == 1) { |
| 129 | #ifdef HAVE_OPENSSL | ||
| 130 | SHA1_Update(&sha1, "true", 4); | ||
| 131 | #else | ||
| 104 | gcry_md_write(hd, "true", 4); | 132 | gcry_md_write(hd, "true", 4); |
| 133 | #endif | ||
| 105 | } else { | 134 | } else { |
| 135 | #ifdef HAVE_OPENSSL | ||
| 136 | SHA1_Update(&sha1, "false", 5); | ||
| 137 | #else | ||
| 106 | gcry_md_write(hd, "false", 5); | 138 | gcry_md_write(hd, "false", 5); |
| 139 | #endif | ||
| 107 | } | 140 | } |
| 141 | #ifdef HAVE_OPENSSL | ||
| 142 | SHA1_Update(&sha1, ";", 1); | ||
| 143 | #else | ||
| 108 | gcry_md_write(hd, ";", 1); | 144 | gcry_md_write(hd, ";", 1); |
| 145 | #endif | ||
| 109 | if (domain) { | 146 | if (domain) { |
| 147 | #ifdef HAVE_OPENSSL | ||
| 148 | SHA1_Update(&sha1, domain, strlen(domain)); | ||
| 149 | #else | ||
| 110 | gcry_md_write(hd, domain, strlen(domain)); | 150 | gcry_md_write(hd, domain, strlen(domain)); |
| 151 | #endif | ||
| 111 | } else { | 152 | } else { |
| 153 | #ifdef HAVE_OPENSSL | ||
| 154 | SHA1_Update(&sha1, "(null)", 6); | ||
| 155 | #else | ||
| 112 | gcry_md_write(hd, "(null)", 6); | 156 | gcry_md_write(hd, "(null)", 6); |
| 157 | #endif | ||
| 113 | } | 158 | } |
| 159 | #ifdef HAVE_OPENSSL | ||
| 160 | SHA1_Update(&sha1, ";", 1); | ||
| 161 | #else | ||
| 114 | gcry_md_write(hd, ";", 1); | 162 | gcry_md_write(hd, ";", 1); |
| 163 | #endif | ||
| 115 | if (appid) { | 164 | if (appid) { |
| 165 | #ifdef HAVE_OPENSSL | ||
| 166 | SHA1_Update(&sha1, appid, strlen(appid)); | ||
| 167 | #else | ||
| 116 | gcry_md_write(hd, appid, strlen(appid)); | 168 | gcry_md_write(hd, appid, strlen(appid)); |
| 169 | #endif | ||
| 117 | } else { | 170 | } else { |
| 171 | #ifdef HAVE_OPENSSL | ||
| 172 | SHA1_Update(&sha1, "(null)", 6); | ||
| 173 | #else | ||
| 118 | gcry_md_write(hd, "(null)", 6); | 174 | gcry_md_write(hd, "(null)", 6); |
| 175 | #endif | ||
| 119 | } | 176 | } |
| 177 | #ifdef HAVE_OPENSSL | ||
| 178 | SHA1_Update(&sha1, ";", 1); | ||
| 179 | #else | ||
| 120 | gcry_md_write(hd, ";", 1); | 180 | gcry_md_write(hd, ";", 1); |
| 181 | #endif | ||
| 121 | if (version) { | 182 | if (version) { |
| 183 | #ifdef HAVE_OPENSSL | ||
| 184 | SHA1_Update(&sha1, version, strlen(version)); | ||
| 185 | #else | ||
| 122 | gcry_md_write(hd, version, strlen(version)); | 186 | gcry_md_write(hd, version, strlen(version)); |
| 187 | #endif | ||
| 123 | } else { | 188 | } else { |
| 189 | #ifdef HAVE_OPENSSL | ||
| 190 | SHA1_Update(&sha1, "(null)", 6); | ||
| 191 | #else | ||
| 124 | gcry_md_write(hd, "(null)", 6); | 192 | gcry_md_write(hd, "(null)", 6); |
| 193 | #endif | ||
| 125 | } | 194 | } |
| 195 | #ifdef HAVE_OPENSSL | ||
| 196 | SHA1_Final(hash_out, &sha1); | ||
| 197 | #else | ||
| 126 | unsigned char *newhash = gcry_md_read(hd, GCRY_MD_SHA1); | 198 | unsigned char *newhash = gcry_md_read(hd, GCRY_MD_SHA1); |
| 127 | memcpy(hash_out, newhash, 20); | 199 | memcpy(hash_out, newhash, 20); |
| 200 | #endif | ||
| 128 | } | 201 | } |
| 202 | #ifndef HAVE_OPENSSL | ||
| 129 | gcry_md_close(hd); | 203 | gcry_md_close(hd); |
| 204 | #endif | ||
| 130 | } | 205 | } |
| 131 | 206 | ||
| 132 | static void print_hash(const unsigned char *hash, int len) | 207 | static void print_hash(const unsigned char *hash, int len) |
