-
Notifications
You must be signed in to change notification settings - Fork 271
/
AndroidBackup.java
505 lines (437 loc) · 20.3 KB
/
AndroidBackup.java
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
package org.nick.abe;
import java.io.ByteArrayOutputStream;
import java.io.Console;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.Key;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.crypto.PBEParametersGenerator;
import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator;
import org.bouncycastle.crypto.params.KeyParameter;
// mostly lifted off com.android.server.BackupManagerService.java
public class AndroidBackup {
private static final int BACKUP_MANIFEST_VERSION = 1;
private static final String BACKUP_FILE_HEADER_MAGIC = "ANDROID BACKUP\n";
private static final int BACKUP_FILE_V1 = 1;
private static final int BACKUP_FILE_V2 = 2;
private static final int BACKUP_FILE_V3 = 3;
private static final int BACKUP_FILE_V4 = 4;
private static final int BACKUP_FILE_V5 = 5;
private static final String ENCRYPTION_MECHANISM = "AES/CBC/PKCS5Padding";
private static final int PBKDF2_HASH_ROUNDS = 10000;
private static final int PBKDF2_KEY_SIZE = 256; // bits
private static final int MASTER_KEY_SIZE = 256; // bits
private static final int PBKDF2_SALT_SIZE = 512; // bits
private static final String ENCRYPTION_ALGORITHM_NAME = "AES-256";
private static final boolean DEBUG = false;
private static final SecureRandom random = new SecureRandom();
private AndroidBackup() {
}
public static void extractAsTar(String backupFilename, String filename,
String password) {
try {
InputStream rawInStream = getInputStream(backupFilename);
CipherInputStream cipherStream = null;
// To prevent the NumberFormatException when trying to figure out the backup version
if (Files.size(Paths.get(backupFilename)) == 0) {
throw new IllegalStateException("File too small in size");
}
String magic = readHeaderLine(rawInStream); // 1
if (DEBUG) {
System.err.println("Magic: " + magic);
}
String versionStr = readHeaderLine(rawInStream); // 2
if (DEBUG) {
System.err.println("Version: " + versionStr);
}
int version = Integer.parseInt(versionStr);
if (version < BACKUP_FILE_V1 || version > BACKUP_FILE_V5) {
throw new IllegalArgumentException(
"Don't know how to process version " + versionStr);
}
String compressed = readHeaderLine(rawInStream); // 3
boolean isCompressed = Integer.parseInt(compressed) == 1;
if (DEBUG) {
System.err.println("Compressed: " + compressed);
}
String encryptionAlg = readHeaderLine(rawInStream); // 4
if (DEBUG) {
System.err.println("Algorithm: " + encryptionAlg);
}
boolean isEncrypted = false;
if (encryptionAlg.equals(ENCRYPTION_ALGORITHM_NAME)) {
isEncrypted = true;
if (Cipher.getMaxAllowedKeyLength("AES") < MASTER_KEY_SIZE) {
System.err.println("WARNING: Maximum allowed key-length seems smaller than needed. " +
"Please check that unlimited strength cryptography is available, see README.md for details");
}
if (password == null || "".equals(password)) {
Console console = System.console();
if (console != null) {
System.err.println("This backup is encrypted, please provide the password");
password = new String(console.readPassword("Password: "));
} else {
throw new IllegalArgumentException(
"Backup encrypted but password not specified");
}
}
String userSaltHex = readHeaderLine(rawInStream); // 5
byte[] userSalt = hexToByteArray(userSaltHex);
if (userSalt.length != PBKDF2_SALT_SIZE / 8) {
throw new IllegalArgumentException("Invalid salt length: "
+ userSalt.length);
}
String ckSaltHex = readHeaderLine(rawInStream); // 6
byte[] ckSalt = hexToByteArray(ckSaltHex);
int rounds = Integer.parseInt(readHeaderLine(rawInStream)); // 7
String userIvHex = readHeaderLine(rawInStream); // 8
String masterKeyBlobHex = readHeaderLine(rawInStream); // 9
// decrypt the master key blob
Cipher c = Cipher.getInstance(ENCRYPTION_MECHANISM);
// XXX we don't support non-ASCII passwords
SecretKey userKey = buildPasswordKey(password, userSalt, rounds, false);
byte[] IV = hexToByteArray(userIvHex);
IvParameterSpec ivSpec = new IvParameterSpec(IV);
c.init(Cipher.DECRYPT_MODE,
new SecretKeySpec(userKey.getEncoded(), "AES"), ivSpec);
byte[] mkCipher = hexToByteArray(masterKeyBlobHex);
byte[] mkBlob = c.doFinal(mkCipher);
// first, the master key IV
int offset = 0;
int len = mkBlob[offset++];
IV = Arrays.copyOfRange(mkBlob, offset, offset + len);
if (DEBUG) {
System.err.println("IV: " + toHex(IV));
}
offset += len;
// then the master key itself
len = mkBlob[offset++];
byte[] mk = Arrays.copyOfRange(mkBlob, offset, offset + len);
if (DEBUG) {
System.err.println("MK: " + toHex(mk));
}
offset += len;
// and finally the master key checksum hash
len = mkBlob[offset++];
byte[] mkChecksum = Arrays.copyOfRange(mkBlob, offset, offset
+ len);
if (DEBUG) {
System.err.println("MK checksum: " + toHex(mkChecksum));
}
// now validate the decrypted master key against the checksum
// first try the algorithm matching the archive version
boolean useUtf = version >= BACKUP_FILE_V2;
byte[] calculatedCk = makeKeyChecksum(mk, ckSalt, rounds, useUtf);
System.err.printf("Calculated MK checksum (use UTF-8: %s): %s\n", useUtf, toHex(calculatedCk));
if (!Arrays.equals(calculatedCk, mkChecksum)) {
System.err.println("Checksum does not match.");
// try the reverse
calculatedCk = makeKeyChecksum(mk, ckSalt, rounds, !useUtf);
System.err.printf("Calculated MK checksum (use UTF-8: %s): %s\n", useUtf, toHex(calculatedCk));
}
if (Arrays.equals(calculatedCk, mkChecksum)) {
ivSpec = new IvParameterSpec(IV);
c.init(Cipher.DECRYPT_MODE, new SecretKeySpec(mk, "AES"),
ivSpec);
// Only if all of the above worked properly will 'result' be
// assigned
cipherStream = new CipherInputStream(rawInStream, c);
}
}
if (isEncrypted && cipherStream == null) {
throw new IllegalStateException(
"Invalid password or master key checksum.");
}
//Get input file size for percentage printing
double fileSize = new File(backupFilename).length();
double percentDone = -1;
OutputStream out = null;
InputStream baseStream = isEncrypted ? cipherStream : rawInStream;
Inflater inf = null;
InputStream in;
if (isCompressed) {
// The Inflater is needed to get the correct percentage because of compression
inf = new Inflater();
in = new InflaterInputStream(baseStream, inf);
} else
in = baseStream;
try {
out = getOutputStream(filename);
byte[] buff = new byte[10 * 1024];
int read;
long totalRead = 0; // of the input file decompressed
double currentPercent; // of the input file
long bytesRead; // of the input file compressed
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
totalRead += read;
if (DEBUG && (totalRead % 100 * 1024 == 0)) {
System.err.printf("%d bytes read\n", totalRead);
}
//Log the percentage extracted
bytesRead = inf == null ? totalRead : inf.getBytesRead();
currentPercent = Math.round(bytesRead / fileSize * 100);
if (currentPercent != percentDone) {
System.err.printf("%.0f%% ", currentPercent);
percentDone = currentPercent;
}
}
System.err.printf("\n%d bytes written to %s.\n",
totalRead, filename);
} finally {
in.close();
if (out != null) {
out.flush();
out.close();
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void packTar(String tarFilename, String backupFilename,
String password, boolean isKitKat) {
boolean encrypting = password != null && !"".equals(password);
boolean compressing = true;
StringBuilder headerbuf = new StringBuilder(1024);
headerbuf.append(BACKUP_FILE_HEADER_MAGIC);
// integer, no trailing \n
headerbuf.append(isKitKat ? BACKUP_FILE_V2 : BACKUP_FILE_V1);
headerbuf.append(compressing ? "\n1\n" : "\n0\n");
OutputStream out = null;
try {
InputStream in = getInputStream(tarFilename);
OutputStream ofstream = getOutputStream(backupFilename);
OutputStream finalOutput = ofstream;
// Set up the encryption stage if appropriate, and emit the correct
// header
if (encrypting) {
finalOutput = emitAesBackupHeader(headerbuf, finalOutput,
password, isKitKat);
} else {
headerbuf.append("none\n");
}
byte[] header = headerbuf.toString().getBytes(StandardCharsets.UTF_8);
ofstream.write(header);
// Set up the compression stage feeding into the encryption stage
// (if any)
if (compressing) {
Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
// requires Java 7
finalOutput = new DeflaterOutputStream(finalOutput, deflater,
true);
}
out = finalOutput;
byte[] buff = new byte[10 * 1024];
int read;
int totalRead = 0;
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
totalRead += read;
if (DEBUG && (totalRead % 100 * 1024 == 0)) {
System.err.printf("%d bytes written\n", totalRead);
}
}
System.err.printf("%d bytes written to %s.\n", totalRead,
backupFilename);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (out != null) {
try {
out.flush();
out.close();
} catch (IOException ignored) {
}
}
}
}
private static InputStream getInputStream(String filename) throws IOException {
if (filename.equals("-")) {
return System.in;
} else {
return new FileInputStream(filename);
}
}
private static OutputStream getOutputStream(String filename) throws IOException {
if (filename.equals("-")) {
return System.out;
} else {
return new FileOutputStream(filename);
}
}
private static byte[] randomBytes(int bits) {
byte[] array = new byte[bits / 8];
random.nextBytes(array);
return array;
}
private static OutputStream emitAesBackupHeader(StringBuilder headerbuf,
OutputStream ofstream, String encryptionPassword, boolean useUtf8) throws Exception {
// User key will be used to encrypt the master key.
byte[] newUserSalt = randomBytes(PBKDF2_SALT_SIZE);
SecretKey userKey = buildPasswordKey(encryptionPassword, newUserSalt,
PBKDF2_HASH_ROUNDS, useUtf8);
// the master key is random for each backup
byte[] masterPw = new byte[MASTER_KEY_SIZE / 8];
random.nextBytes(masterPw);
byte[] checksumSalt = randomBytes(PBKDF2_SALT_SIZE);
// primary encryption of the datastream with the random key
Cipher c = Cipher.getInstance(ENCRYPTION_MECHANISM);
SecretKeySpec masterKeySpec = new SecretKeySpec(masterPw, "AES");
c.init(Cipher.ENCRYPT_MODE, masterKeySpec);
OutputStream finalOutput = new CipherOutputStream(ofstream, c);
// line 4: name of encryption algorithm
headerbuf.append(ENCRYPTION_ALGORITHM_NAME);
headerbuf.append('\n');
// line 5: user password salt [hex]
headerbuf.append(toHex(newUserSalt));
headerbuf.append('\n');
// line 6: master key checksum salt [hex]
headerbuf.append(toHex(checksumSalt));
headerbuf.append('\n');
// line 7: number of PBKDF2 rounds used [decimal]
headerbuf.append(PBKDF2_HASH_ROUNDS);
headerbuf.append('\n');
// line 8: IV of the user key [hex]
Cipher mkC = Cipher.getInstance(ENCRYPTION_MECHANISM);
mkC.init(Cipher.ENCRYPT_MODE, userKey);
byte[] IV = mkC.getIV();
headerbuf.append(toHex(IV));
headerbuf.append('\n');
// line 9: master IV + key blob, encrypted by the user key [hex]. Blob
// format:
// [byte] IV length = Niv
// [array of Niv bytes] IV itself
// [byte] master key length = Nmk
// [array of Nmk bytes] master key itself
// [byte] MK checksum hash length = Nck
// [array of Nck bytes] master key checksum hash
//
// The checksum is the (master key + checksum salt), run through the
// stated number of PBKDF2 rounds
IV = c.getIV();
byte[] mk = masterKeySpec.getEncoded();
byte[] checksum = makeKeyChecksum(masterKeySpec.getEncoded(),
checksumSalt, PBKDF2_HASH_ROUNDS, useUtf8);
ByteArrayOutputStream blob = new ByteArrayOutputStream(IV.length
+ mk.length + checksum.length + 3);
DataOutputStream mkOut = new DataOutputStream(blob);
mkOut.writeByte(IV.length);
mkOut.write(IV);
mkOut.writeByte(mk.length);
mkOut.write(mk);
mkOut.writeByte(checksum.length);
mkOut.write(checksum);
mkOut.flush();
byte[] encryptedMk = mkC.doFinal(blob.toByteArray());
headerbuf.append(toHex(encryptedMk));
headerbuf.append('\n');
return finalOutput;
}
public static String toHex(byte[] bytes) {
StringBuilder buff = new StringBuilder();
for (byte b : bytes) {
buff.append(String.format("%02X", b));
}
return buff.toString();
}
private static String readHeaderLine(InputStream in) throws IOException {
int c;
StringBuilder buffer = new StringBuilder(80);
while ((c = in.read()) >= 0) {
if (c == '\n')
break; // consume and discard the newlines
buffer.append((char) c);
}
return buffer.toString();
}
public static byte[] hexToByteArray(String digits) {
final int bytes = digits.length() / 2;
if (2 * bytes != digits.length()) {
throw new IllegalArgumentException(
"Hex string must have an even number of digits");
}
byte[] result = new byte[bytes];
for (int i = 0; i < digits.length(); i += 2) {
result[i / 2] = (byte) Integer.parseInt(digits.substring(i, i + 2),
16);
}
return result;
}
public static byte[] makeKeyChecksum(byte[] pwBytes, byte[] salt, int rounds, boolean useUtf8) {
if (DEBUG) {
System.err.println("key bytes: " + toHex(pwBytes));
System.err.println("salt bytes: " + toHex(salt));
}
char[] mkAsChar = new char[pwBytes.length];
for (int i = 0; i < pwBytes.length; i++) {
mkAsChar[i] = (char) pwBytes[i];
}
if (DEBUG) {
System.err.printf("MK as string: [%s]\n", new String(mkAsChar));
}
Key checksum = buildCharArrayKey(mkAsChar, salt, rounds, useUtf8);
if (DEBUG) {
System.err.println("Key format: " + checksum.getFormat());
}
return checksum.getEncoded();
}
public static SecretKey buildCharArrayKey(char[] pwArray, byte[] salt,
int rounds, boolean useUtf8) {
// Original code from BackupManagerService
// this produces different results when run with Sun/Oracale Java SE
// which apparently treats password bytes as UTF-8 (16?)
// (the encoding is left unspecified in PKCS#5)
// try {
// SecretKeyFactory keyFactory = SecretKeyFactory
// .getInstance("PBKDF2WithHmacSHA1");
// KeySpec ks = new PBEKeySpec(pwArray, salt, rounds, PBKDF2_KEY_SIZE);
// return keyFactory.generateSecret(ks);
// } catch (InvalidKeySpecException e) {
// throw new RuntimeException(e);
// } catch (NoSuchAlgorithmException e) {
// throw new RuntimeException(e);
// } catch (NoSuchProviderException e) {
// throw new RuntimeException(e);
// }
// return null;
return androidPBKDF2(pwArray, salt, rounds, useUtf8);
}
public static SecretKey androidPBKDF2(char[] pwArray, byte[] salt,
int rounds, boolean useUtf8) {
PBEParametersGenerator generator = new PKCS5S2ParametersGenerator();
// Android treats password bytes as ASCII, which is obviously
// not the case when an AES key is used as a 'password'.
// Use the same method for compatibility.
// Android 4.4 however uses all char bytes
// useUtf8 needs to be true for KitKat
byte[] pwBytes = useUtf8 ? PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(pwArray)
: PBEParametersGenerator.PKCS5PasswordToBytes(pwArray);
generator.init(pwBytes, salt, rounds);
KeyParameter params = (KeyParameter) generator
.generateDerivedParameters(PBKDF2_KEY_SIZE);
return new SecretKeySpec(params.getKey(), "AES");
}
private static SecretKey buildPasswordKey(String pw, byte[] salt, int rounds, boolean useUtf8) {
return buildCharArrayKey(pw.toCharArray(), salt, rounds, useUtf8);
}
}