Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Fix proton import error handling

Prateek Sunal57108125144b
diff --git a/mobile/apps/auth/lib/l10n/arb/app_en.arb b/mobile/apps/auth/lib/l10n/arb/app_en.arb
index 2873deeacb..bd9ab39784 100644
--- a/mobile/apps/auth/lib/l10n/arb/app_en.arb
+++ b/mobile/apps/auth/lib/l10n/arb/app_en.arb
@@ -122,6 +122,8 @@
"import2FasGuide": "Use the \"Settings->Backup -Export\" option in 2FAS.\n\nIf your backup is encrypted, you will need to enter the password to decrypt the backup",
"importLastpassGuide": "Use the \"Transfer accounts\" option within Lastpass Authenticator Settings and press \"Export accounts to file\". Import the JSON downloaded.",
"importProtonAuthGuide": "Use the \"Export\" option in Proton Authenticator Settings to export your codes.\n\nIf your export is password protected, you will need to enter the password to decrypt it.",
+ "invalidProtonExportTitle": "Invalid Proton export",
+ "invalidProtonExportMessage": "The selected file is not a valid Proton Authenticator export.",
"exportCodes": "Export codes",
"importLabel": "Import",
"importInstruction": "Please select a file that contains a list of your codes in the following format",
diff --git a/mobile/apps/auth/lib/ui/settings/data/import/proton_import.dart b/mobile/apps/auth/lib/ui/settings/data/import/proton_import.dart
index b48119fec5..92b93c2a24 100644
--- a/mobile/apps/auth/lib/ui/settings/data/import/proton_import.dart
+++ b/mobile/apps/auth/lib/ui/settings/data/import/proton_import.dart
@@ -92,48 +92,37 @@ Future<int?> _processProtonExportFile(
await dialog.hide();
await showErrorDialog(
context,
- 'Invalid Proton export',
- 'The selected file is not a valid Proton Authenticator export.',
+ context.l10n.invalidProtonExportTitle,
+ context.l10n.invalidProtonExportMessage,
);
return null;
}
if (isEncryptedProtonExport(decodedJson)) {
await dialog.hide();
- String? password;
- try {
- await showTextInputDialog(
- context,
- title: context.l10n.passwordForDecryptingExport,
- submitButtonLabel: context.l10n.submit,
- isPasswordInput: true,
- onSubmit: (value) async {
- password = value;
- },
- );
- if (password == null) {
- return null;
- }
+ final password = await _promptForProtonExportPassword(context);
+ if (password == null) {
+ return null;
+ }
- await dialog.show();
+ await dialog.show();
+ try {
final decryptedJsonString = await compute(
_decryptProtonExportInBackground,
{
'jsonString': jsonString,
- 'password': password!,
+ 'password': password,
},
);
decodedJson = decodeProtonExportJson(decryptedJsonString);
} catch (e, s) {
Logger('ProtonImport').warning('Failed to decrypt Proton export', e, s);
await dialog.hide();
- if (password != null) {
- await showErrorDialog(
- context,
- context.l10n.incorrectPasswordTitle,
- context.l10n.pleaseCheckPasswordAndTryAgain,
- );
- }
+ await showErrorDialog(
+ context,
+ context.l10n.incorrectPasswordTitle,
+ context.l10n.pleaseCheckPasswordAndTryAgain,
+ );
return null;
}
}
@@ -148,6 +137,31 @@ Future<int?> _processProtonExportFile(
}
String _decryptProtonExportInBackground(Map<String, String> params) {
- final decodedJson = decodeProtonExportJson(params['jsonString']!);
- return decryptProtonExport(decodedJson, password: params['password']!);
+ final jsonString = params['jsonString'];
+ final password = params['password'];
+ if (jsonString == null || password == null) {
+ throw ArgumentError('Missing Proton export decryption params');
+ }
+
+ final decodedJson = decodeProtonExportJson(jsonString);
+ return decryptProtonExport(decodedJson, password: password);
+}
+
+Future<String?> _promptForProtonExportPassword(BuildContext context) async {
+ final password = Completer<String?>();
+ await showTextInputDialog(
+ context,
+ title: context.l10n.passwordForDecryptingExport,
+ submitButtonLabel: context.l10n.submit,
+ isPasswordInput: true,
+ onSubmit: (value) async {
+ if (!password.isCompleted) {
+ password.complete(value);
+ }
+ },
+ );
+ if (!password.isCompleted) {
+ return null;
+ }
+ return password.future;
}
diff --git a/mobile/apps/auth/lib/ui/settings/data/import/proton_import_parser.dart b/mobile/apps/auth/lib/ui/settings/data/import/proton_import_parser.dart
index 9a6f454a60..abce75763e 100644
--- a/mobile/apps/auth/lib/ui/settings/data/import/proton_import_parser.dart
+++ b/mobile/apps/auth/lib/ui/settings/data/import/proton_import_parser.dart
@@ -1,7 +1,7 @@
import 'dart:convert';
-import 'dart:typed_data';
import 'package:ente_auth/models/code.dart';
+import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';
import 'package:pointycastle/export.dart';
@@ -144,6 +144,14 @@ String decryptProtonExport(
return utf8.decode(decryptedBytes);
}
+@visibleForTesting
+Uint8List deriveProtonPasswordKeyForTesting(String password, Uint8List salt) {
+ return _deriveProtonPasswordKey(password, salt);
+}
+
+@visibleForTesting
+String get protonExportAadForTesting => _protonExportAad;
+
Uint8List _deriveProtonPasswordKey(String password, Uint8List salt) {
final generator = Argon2BytesGenerator()
..init(
diff --git a/mobile/apps/auth/test/ui/settings/data/import/proton_import_test.dart b/mobile/apps/auth/test/ui/settings/data/import/proton_import_test.dart
index b01b553225..2242a497b3 100644
--- a/mobile/apps/auth/test/ui/settings/data/import/proton_import_test.dart
+++ b/mobile/apps/auth/test/ui/settings/data/import/proton_import_test.dart
@@ -108,7 +108,7 @@ Map<String, dynamic> _createEncryptedExport(
final nonce = Uint8List.fromList(
List<int>.generate(12, (index) => index + 31),
);
- final derivedKey = _deriveProtonPasswordKey(password, salt);
+ final derivedKey = deriveProtonPasswordKeyForTesting(password, salt);
final cipher = GCMBlockCipher(AESEngine())
..init(
@@ -117,7 +117,7 @@ Map<String, dynamic> _createEncryptedExport(
KeyParameter(derivedKey),
128,
nonce,
- Uint8List.fromList(utf8.encode('proton.authenticator.export.v1')),
+ Uint8List.fromList(utf8.encode(protonExportAadForTesting)),
),
);
final encryptedBytes = cipher.process(
@@ -131,20 +131,3 @@ Map<String, dynamic> _createEncryptedExport(
'content': base64Encode(payload),
};
}
-
-Uint8List _deriveProtonPasswordKey(String password, Uint8List salt) {
- final generator = Argon2BytesGenerator()
- ..init(
- Argon2Parameters(
- Argon2Parameters.ARGON2_id,
- salt,
- desiredKeyLength: 32,
- iterations: 2,
- memory: 19 * 1024,
- lanes: 1,
- version: Argon2Parameters.ARGON2_VERSION_13,
- ),
- );
-
- return generator.process(Uint8List.fromList(utf8.encode(password)));
-}