update to versioned desktop secure storage and login key blob

This commit is contained in:
julian 2023-01-26 09:18:07 -06:00
parent ffda21f513
commit b49a1942a6
4 changed files with 58 additions and 9 deletions

@ -1 +1 @@
Subproject commit c1b403ccf6f4fffc9f7c233038c3df40f997c2b3
Subproject commit af88796d5e4988c03422320c3842af5cf6c049ef

View file

@ -4,9 +4,12 @@ import 'package:stackwallet/hive/db.dart';
import 'package:stackwallet/utilities/logger.dart';
const String _kKeyBlobKey = "swbKeyBlobKeyStringID";
const String _kKeyBlobVersionKey = "swbKeyBlobVersionKeyStringID";
const int kLatestBlobVersion = 2;
String _getMessageFromException(Object exception) {
if (exception is IncorrectPassphrase) {
if (exception is IncorrectPassphraseOrVersion) {
return exception.errMsg();
}
if (exception is BadDecryption) {
@ -18,6 +21,9 @@ String _getMessageFromException(Object exception) {
if (exception is EncodingError) {
return exception.errMsg();
}
if (exception is VersionError) {
return exception.errMsg();
}
return exception.toString();
}
@ -41,7 +47,10 @@ class DPS {
}
try {
_handler = await StorageCryptoHandler.fromNewPassphrase(passphrase);
_handler = await StorageCryptoHandler.fromNewPassphrase(
passphrase,
kLatestBlobVersion,
);
final box = await Hive.openBox<String>(DB.boxNameDesktopData);
await DB.instance.put<String>(
@ -49,6 +58,7 @@ class DPS {
key: _kKeyBlobKey,
value: await _handler!.getKeyBlob(),
);
await _updateStoredKeyBlobVersion(kLatestBlobVersion);
await box.close();
} catch (e, s) {
Logging.instance.log(
@ -78,7 +88,24 @@ class DPS {
}
try {
_handler = await StorageCryptoHandler.fromExisting(passphrase, keyBlob);
final blobVersion = await _getStoredKeyBlobVersion();
_handler = await StorageCryptoHandler.fromExisting(
passphrase,
keyBlob,
blobVersion,
);
if (blobVersion < kLatestBlobVersion) {
// update blob
await _handler!.resetPassphrase(passphrase, kLatestBlobVersion);
final box = await Hive.openBox<String>(DB.boxNameDesktopData);
await DB.instance.put<String>(
boxName: DB.boxNameDesktopData,
key: _kKeyBlobKey,
value: await _handler!.getKeyBlob(),
);
await _updateStoredKeyBlobVersion(kLatestBlobVersion);
await box.close();
}
} catch (e, s) {
Logging.instance.log(
"${_getMessageFromException(e)}\n$s",
@ -102,7 +129,8 @@ class DPS {
}
try {
await StorageCryptoHandler.fromExisting(passphrase, keyBlob);
final blobVersion = await _getStoredKeyBlobVersion();
await StorageCryptoHandler.fromExisting(passphrase, keyBlob, blobVersion);
// existing passphrase matches key blob
return true;
} catch (e, s) {
@ -135,8 +163,10 @@ class DPS {
return false;
}
final blobVersion = await _getStoredKeyBlobVersion();
try {
await _handler!.resetPassphrase(passphraseNew);
await _handler!.resetPassphrase(passphraseNew, blobVersion);
final box = await Hive.openBox<String>(DB.boxNameDesktopData);
await DB.instance.put<String>(
@ -144,6 +174,7 @@ class DPS {
key: _kKeyBlobKey,
value: await _handler!.getKeyBlob(),
);
await _updateStoredKeyBlobVersion(blobVersion);
await box.close();
// successfully updated passphrase
@ -164,4 +195,22 @@ class DPS {
);
return keyBlob != null;
}
Future<int> _getStoredKeyBlobVersion() async {
final box = await Hive.openBox<String>(DB.boxNameDesktopData);
final keyBlobVersionString = DB.instance.get<String>(
boxName: DB.boxNameDesktopData,
key: _kKeyBlobVersionKey,
);
await box.close();
return int.tryParse(keyBlobVersionString ?? "1") ?? 1;
}
Future<void> _updateStoredKeyBlobVersion(int version) async {
await DB.instance.put<String>(
boxName: DB.boxNameDesktopData,
key: _kKeyBlobVersionKey,
value: version.toString(),
);
}
}

View file

@ -1408,8 +1408,8 @@ packages:
dependency: "direct main"
description:
path: "."
ref: "6ada1204a4e0cf84d932b568e6150550478db69b"
resolved-ref: "6ada1204a4e0cf84d932b568e6150550478db69b"
ref: "93e2687bcc10fc7258c7dab038c363fc9ff8ba5d"
resolved-ref: "93e2687bcc10fc7258c7dab038c363fc9ff8ba5d"
url: "https://github.com/cypherstack/stack_wallet_backup.git"
source: git
version: "0.0.1"

View file

@ -54,7 +54,7 @@ dependencies:
stack_wallet_backup:
git:
url: https://github.com/cypherstack/stack_wallet_backup.git
ref: 6ada1204a4e0cf84d932b568e6150550478db69b
ref: 93e2687bcc10fc7258c7dab038c363fc9ff8ba5d
bip47:
git: