refactor: Let bootstrap throw custom Exception InvalidPassphraseException so it is easier to catch

This commit is contained in:
Krille 2023-05-30 09:41:10 +02:00
parent c6b6c793d6
commit 07ceee4171
No known key found for this signature in database
1 changed files with 16 additions and 11 deletions

View File

@ -132,17 +132,17 @@ class SSSS {
final parity = result.fold<int>(0, (a, b) => a ^ b); final parity = result.fold<int>(0, (a, b) => a ^ b);
if (parity != 0) { if (parity != 0) {
throw Exception('Incorrect parity'); throw InvalidPassphraseException('Incorrect parity');
} }
for (var i = 0; i < olmRecoveryKeyPrefix.length; i++) { for (var i = 0; i < olmRecoveryKeyPrefix.length; i++) {
if (result[i] != olmRecoveryKeyPrefix[i]) { if (result[i] != olmRecoveryKeyPrefix[i]) {
throw Exception('Incorrect prefix'); throw InvalidPassphraseException('Incorrect prefix');
} }
} }
if (result.length != olmRecoveryKeyPrefix.length + ssssKeyLength + 1) { if (result.length != olmRecoveryKeyPrefix.length + ssssKeyLength + 1) {
throw Exception('Incorrect length'); throw InvalidPassphraseException('Incorrect length');
} }
return Uint8List.fromList(result.sublist(olmRecoveryKeyPrefix.length, return Uint8List.fromList(result.sublist(olmRecoveryKeyPrefix.length,
@ -163,13 +163,13 @@ class SSSS {
static Future<Uint8List> keyFromPassphrase( static Future<Uint8List> keyFromPassphrase(
String passphrase, PassphraseInfo info) async { String passphrase, PassphraseInfo info) async {
if (info.algorithm != AlgorithmTypes.pbkdf2) { if (info.algorithm != AlgorithmTypes.pbkdf2) {
throw Exception('Unknown algorithm'); throw InvalidPassphraseException('Unknown algorithm');
} }
if (info.iterations == null) { if (info.iterations == null) {
throw Exception('Passphrase info without iterations'); throw InvalidPassphraseException('Passphrase info without iterations');
} }
if (info.salt == null) { if (info.salt == null) {
throw Exception('Passphrase info without salt'); throw InvalidPassphraseException('Passphrase info without salt');
} }
return await uc.pbkdf2( return await uc.pbkdf2(
Uint8List.fromList(utf8.encode(passphrase)), Uint8List.fromList(utf8.encode(passphrase)),
@ -275,7 +275,7 @@ class SSSS {
return true; return true;
} }
} else { } else {
throw Exception('Unknown Algorithm'); throw InvalidPassphraseException('Unknown Algorithm');
} }
} }
@ -380,7 +380,7 @@ class SSSS {
// now remove all other keys // now remove all other keys
final content = client.accountData[type]?.content.copy(); final content = client.accountData[type]?.content.copy();
if (content == null) { if (content == null) {
throw Exception('Key has no content!'); throw InvalidPassphraseException('Key has no content!');
} }
final otherKeys = final otherKeys =
@ -666,7 +666,7 @@ class OpenSSSS {
return; return;
} else if (passphrase != null) { } else if (passphrase != null) {
if (!hasPassphrase) { if (!hasPassphrase) {
throw Exception( throw InvalidPassphraseException(
'Tried to unlock with passphrase while key does not have a passphrase'); 'Tried to unlock with passphrase while key does not have a passphrase');
} }
privateKey = await Future.value( privateKey = await Future.value(
@ -680,12 +680,12 @@ class OpenSSSS {
} else if (recoveryKey != null) { } else if (recoveryKey != null) {
privateKey = SSSS.decodeRecoveryKey(recoveryKey); privateKey = SSSS.decodeRecoveryKey(recoveryKey);
} else { } else {
throw Exception('Nothing specified'); throw InvalidPassphraseException('Nothing specified');
} }
// verify the validity of the key // verify the validity of the key
if (!await ssss.checkKey(privateKey!, keyData)) { if (!await ssss.checkKey(privateKey!, keyData)) {
privateKey = null; privateKey = null;
throw Exception('Inalid key'); throw InvalidPassphraseException('Inalid key');
} }
if (postUnlock) { if (postUnlock) {
await runInRoot(() => _postUnlock()); await runInRoot(() => _postUnlock());
@ -766,3 +766,8 @@ class KeyFromPassphraseArgs {
Future<Uint8List> generateKeyFromPassphrase(KeyFromPassphraseArgs args) async { Future<Uint8List> generateKeyFromPassphrase(KeyFromPassphraseArgs args) async {
return await SSSS.keyFromPassphrase(args.passphrase, args.info); return await SSSS.keyFromPassphrase(args.passphrase, args.info);
} }
class InvalidPassphraseException implements Exception {
String cause;
InvalidPassphraseException(this.cause);
}