2017-08-06 16:03:36 +02:00
|
|
|
package me.impy.aegis.db;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2017-12-10 19:19:48 +01:00
|
|
|
import android.os.Environment;
|
2017-08-06 16:03:36 +02:00
|
|
|
|
2017-12-10 19:19:48 +01:00
|
|
|
import java.io.DataInputStream;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
2017-08-06 16:03:36 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
2017-12-12 12:25:55 +01:00
|
|
|
import me.impy.aegis.BuildConfig;
|
2017-08-06 16:03:36 +02:00
|
|
|
import me.impy.aegis.crypto.CryptParameters;
|
|
|
|
import me.impy.aegis.crypto.CryptResult;
|
|
|
|
import me.impy.aegis.crypto.MasterKey;
|
|
|
|
|
|
|
|
public class DatabaseManager {
|
2017-12-10 19:19:48 +01:00
|
|
|
private static final String FILENAME = "aegis.db";
|
|
|
|
private static final String FILENAME_EXPORT = "aegis_export.db";
|
|
|
|
private static final String FILENAME_EXPORT_PLAIN = "aegis_export.json";
|
2017-12-04 22:08:50 +01:00
|
|
|
|
2017-08-06 16:03:36 +02:00
|
|
|
private MasterKey _key;
|
|
|
|
private DatabaseFile _file;
|
|
|
|
private Database _db;
|
|
|
|
private Context _context;
|
|
|
|
|
|
|
|
public DatabaseManager(Context context) {
|
|
|
|
_context = context;
|
|
|
|
}
|
|
|
|
|
2017-12-25 00:16:24 +01:00
|
|
|
public boolean fileExists() {
|
|
|
|
File file = new File(_context.getFilesDir(), FILENAME);
|
|
|
|
return file.exists() && file.isFile();
|
|
|
|
}
|
|
|
|
|
2017-08-06 16:03:36 +02:00
|
|
|
public void load() throws Exception {
|
2017-12-24 22:29:32 +01:00
|
|
|
assertState(true, false);
|
|
|
|
|
2017-12-10 19:19:48 +01:00
|
|
|
byte[] fileBytes;
|
|
|
|
FileInputStream file = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
file = _context.openFileInput(FILENAME);
|
|
|
|
fileBytes = new byte[(int) file.getChannel().size()];
|
|
|
|
DataInputStream stream = new DataInputStream(file);
|
|
|
|
stream.readFully(fileBytes);
|
|
|
|
stream.close();
|
|
|
|
} finally {
|
|
|
|
// always close the file stream
|
|
|
|
// there is no need to close the DataInputStream
|
|
|
|
if (file != null) {
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_file = new DatabaseFile();
|
|
|
|
_file.deserialize(fileBytes);
|
|
|
|
|
2017-08-06 16:03:36 +02:00
|
|
|
if (!_file.isEncrypted()) {
|
2017-12-10 19:19:48 +01:00
|
|
|
byte[] contentBytes = _file.getContent();
|
2017-08-06 16:03:36 +02:00
|
|
|
_db = new Database();
|
2017-12-10 19:19:48 +01:00
|
|
|
_db.deserialize(contentBytes);
|
2017-08-06 16:03:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-23 22:33:32 +01:00
|
|
|
public void lock() throws Exception {
|
2017-12-24 22:29:32 +01:00
|
|
|
assertState(false, true);
|
2017-12-23 22:33:32 +01:00
|
|
|
// TODO: properly clear everything
|
|
|
|
_key = null;
|
|
|
|
_db = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void unlock(MasterKey key) throws Exception {
|
2017-12-24 22:29:32 +01:00
|
|
|
assertState(true, true);
|
2017-08-06 16:03:36 +02:00
|
|
|
byte[] encrypted = _file.getContent();
|
|
|
|
CryptParameters params = _file.getCryptParameters();
|
|
|
|
CryptResult result = key.decrypt(encrypted, params);
|
|
|
|
_db = new Database();
|
|
|
|
_db.deserialize(result.Data);
|
|
|
|
_key = key;
|
|
|
|
}
|
|
|
|
|
2017-12-10 19:19:48 +01:00
|
|
|
public static void save(Context context, DatabaseFile file) throws IOException {
|
|
|
|
byte[] bytes = file.serialize();
|
|
|
|
|
|
|
|
FileOutputStream stream = null;
|
|
|
|
try {
|
|
|
|
stream = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
|
|
|
|
stream.write(bytes);
|
|
|
|
} finally {
|
|
|
|
// always close the file stream
|
|
|
|
if (stream != null) {
|
|
|
|
stream.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-06 16:03:36 +02:00
|
|
|
public void save() throws Exception {
|
2017-12-24 22:29:32 +01:00
|
|
|
assertState(false, true);
|
2017-12-10 19:19:48 +01:00
|
|
|
byte[] dbBytes = _db.serialize();
|
2017-08-06 21:45:27 +02:00
|
|
|
if (!_file.isEncrypted()) {
|
2017-12-10 19:19:48 +01:00
|
|
|
_file.setContent(dbBytes);
|
2017-08-06 21:45:27 +02:00
|
|
|
} else {
|
2017-12-10 19:19:48 +01:00
|
|
|
CryptResult result = _key.encrypt(dbBytes);
|
|
|
|
_file.setContent(result.Data);
|
|
|
|
_file.setCryptParameters(result.Parameters);
|
|
|
|
}
|
|
|
|
save(_context, _file);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String export(boolean encrypt) throws Exception {
|
2017-12-24 22:29:32 +01:00
|
|
|
assertState(false, true);
|
2017-12-27 23:23:05 +01:00
|
|
|
byte[] bytes = _db.serialize(!encrypt);
|
2017-12-10 19:19:48 +01:00
|
|
|
encrypt = encrypt && getFile().isEncrypted();
|
|
|
|
if (encrypt) {
|
2017-08-06 21:45:27 +02:00
|
|
|
CryptResult result = _key.encrypt(bytes);
|
|
|
|
_file.setContent(result.Data);
|
|
|
|
_file.setCryptParameters(result.Parameters);
|
2017-12-10 19:19:48 +01:00
|
|
|
bytes = _file.serialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
File file;
|
|
|
|
FileOutputStream stream = null;
|
|
|
|
try {
|
2017-12-12 12:25:55 +01:00
|
|
|
String dirName = !BuildConfig.DEBUG ? "Aegis" : "AegisDebug";
|
|
|
|
File dir = new File(Environment.getExternalStorageDirectory(), dirName);
|
2017-12-10 19:19:48 +01:00
|
|
|
if (!dir.exists() && !dir.mkdirs()) {
|
|
|
|
throw new IOException("error creating external storage directory");
|
|
|
|
}
|
|
|
|
|
|
|
|
file = new File(dir.getAbsolutePath(), encrypt ? FILENAME_EXPORT : FILENAME_EXPORT_PLAIN);
|
|
|
|
stream = new FileOutputStream(file);
|
|
|
|
stream.write(bytes);
|
|
|
|
} finally {
|
|
|
|
// always close the file stream
|
|
|
|
if (stream != null) {
|
|
|
|
stream.close();
|
|
|
|
}
|
2017-08-06 21:45:27 +02:00
|
|
|
}
|
2017-12-10 19:19:48 +01:00
|
|
|
|
|
|
|
return file.getAbsolutePath();
|
2017-08-06 16:03:36 +02:00
|
|
|
}
|
|
|
|
|
2017-08-26 21:15:53 +02:00
|
|
|
public void addKey(DatabaseEntry entry) throws Exception {
|
2017-12-24 22:29:32 +01:00
|
|
|
assertState(false, true);
|
2017-08-26 21:15:53 +02:00
|
|
|
_db.addKey(entry);
|
2017-08-06 16:03:36 +02:00
|
|
|
}
|
|
|
|
|
2017-08-26 21:15:53 +02:00
|
|
|
public void removeKey(DatabaseEntry entry) throws Exception {
|
2017-12-24 22:29:32 +01:00
|
|
|
assertState(false, true);
|
2017-08-26 21:15:53 +02:00
|
|
|
_db.removeKey(entry);
|
2017-08-06 16:03:36 +02:00
|
|
|
}
|
|
|
|
|
2017-12-27 22:04:22 +01:00
|
|
|
public void replaceKey(DatabaseEntry entry) throws Exception {
|
|
|
|
assertState(false, true);
|
|
|
|
_db.replaceKey(entry);
|
|
|
|
}
|
|
|
|
|
2017-12-12 01:50:00 +01:00
|
|
|
public void swapKeys(DatabaseEntry entry1, DatabaseEntry entry2) throws Exception {
|
2017-12-24 22:29:32 +01:00
|
|
|
assertState(false, true);
|
2017-12-12 01:50:00 +01:00
|
|
|
_db.swapKeys(entry1, entry2);
|
|
|
|
}
|
|
|
|
|
2017-08-26 21:15:53 +02:00
|
|
|
public List<DatabaseEntry> getKeys() throws Exception {
|
2017-12-24 22:29:32 +01:00
|
|
|
assertState(false, true);
|
2017-08-06 16:03:36 +02:00
|
|
|
return _db.getKeys();
|
|
|
|
}
|
|
|
|
|
2017-08-06 18:15:47 +02:00
|
|
|
public DatabaseFile getFile() {
|
|
|
|
return _file;
|
|
|
|
}
|
|
|
|
|
2017-08-06 16:03:36 +02:00
|
|
|
public boolean isLoaded() {
|
|
|
|
return _file != null;
|
|
|
|
}
|
|
|
|
|
2017-12-24 22:29:32 +01:00
|
|
|
public boolean isLocked() {
|
|
|
|
return _db == null;
|
2017-08-06 16:03:36 +02:00
|
|
|
}
|
|
|
|
|
2017-12-24 22:29:32 +01:00
|
|
|
private void assertState(boolean locked, boolean loaded) throws Exception {
|
|
|
|
if (isLoaded() && !loaded) {
|
2017-08-06 16:03:36 +02:00
|
|
|
throw new Exception("database file has not been loaded yet");
|
2017-12-24 22:29:32 +01:00
|
|
|
} else if (!isLoaded() && loaded) {
|
|
|
|
throw new Exception("database file has is already been loaded");
|
2017-08-06 16:03:36 +02:00
|
|
|
}
|
|
|
|
|
2017-12-24 22:29:32 +01:00
|
|
|
if (isLocked() && !locked) {
|
2017-12-23 22:33:32 +01:00
|
|
|
throw new Exception("database file has not been unlocked yet");
|
2017-12-24 22:29:32 +01:00
|
|
|
} else if (!isLocked() && locked) {
|
|
|
|
throw new Exception("database file has is already been unlocked");
|
2017-08-06 16:03:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|