Decrypt keys of files within collections using the collectionKey

This commit is contained in:
Vishnu Mohandas 2020-10-11 05:47:31 +05:30
parent da8b69084b
commit 8f8594f07f
2 changed files with 31 additions and 15 deletions

View file

@ -15,7 +15,9 @@ class CollectionsService {
CollectionsDB _db;
Configuration _config;
Map<String, Collection> _localCollections;
final _localCollections = Map<String, Collection>();
final _collectionIDToCollection = Map<int, Collection>();
final _cachedKeys = Map<int, Uint8List>();
CollectionsService._privateConstructor() {
_db = CollectionsDB.instance;
@ -39,6 +41,7 @@ class CollectionsService {
Sodium.base642bin(collection.pathDecryptionNonce)));
_localCollections[path] = collection;
}
_collectionIDToCollection[collection.id] = collection;
}
}
@ -63,17 +66,23 @@ class CollectionsService {
});
}
Uint8List getCollectionKey(Collection collection) {
final encryptedKey = Sodium.base642bin(collection.encryptedKey);
if (collection.ownerID == _config.getUserID()) {
return CryptoUtil.decryptSync(encryptedKey, _config.getKey(),
Sodium.base642bin(collection.keyDecryptionNonce));
} else {
return CryptoUtil.openSealSync(
encryptedKey,
Sodium.base642bin(_config.getKeyAttributes().publicKey),
_config.getSecretKey());
Uint8List getCollectionKey(int collectionID) {
if (!_cachedKeys.containsKey(collectionID)) {
final collection = _collectionIDToCollection[collectionID];
final encryptedKey = Sodium.base642bin(collection.encryptedKey);
var key;
if (collection.ownerID == _config.getUserID()) {
key = CryptoUtil.decryptSync(encryptedKey, _config.getKey(),
Sodium.base642bin(collection.keyDecryptionNonce));
} else {
key = CryptoUtil.openSealSync(
encryptedKey,
Sodium.base642bin(_config.getKeyAttributes().publicKey),
_config.getSecretKey());
}
_cachedKeys[collection.id] = key;
}
return _cachedKeys[collectionID];
}
Future<List<Collection>> getCollections(int sinceTime) {

View file

@ -17,6 +17,7 @@ import 'package:photos/core/constants.dart';
import 'package:photos/db/files_db.dart';
import 'package:photos/models/file.dart';
import 'package:photos/models/file_type.dart';
import 'package:photos/services/collections_service.dart';
import 'crypto_util.dart';
@ -217,8 +218,14 @@ Future<io.File> _downloadAndDecryptThumbnail(File file) async {
}
Uint8List decryptFileKey(File file) {
return CryptoUtil.decryptSync(
Sodium.base642bin(file.encryptedKey),
Configuration.instance.getKey(),
Sodium.base642bin(file.keyDecryptionNonce));
final encryptedKey = Sodium.base642bin(file.encryptedKey);
final nonce = Sodium.base642bin(file.keyDecryptionNonce);
if (file.ownerID == Configuration.instance.getUserID()) {
return CryptoUtil.decryptSync(
encryptedKey, Configuration.instance.getKey(), nonce);
} else {
final collectionKey =
CollectionsService.instance.getCollectionKey(file.collectionID);
return CryptoUtil.decryptSync(encryptedKey, collectionKey, nonce);
}
}