Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class SetupEncryptionDialogFragment :

// Setup layout
viewThemeUtils.material.colorTextInputLayout(binding.encryptionPasswordInputContainer)
viewThemeUtils.material.colorProgressBar(binding.progressBar)

val builder = buildMaterialAlertDialog(binding.root)
viewThemeUtils.dialog.colorMaterialAlertDialogBackground(requireContext(), builder)
Expand Down Expand Up @@ -158,70 +159,79 @@ class SetupEncryptionDialogFragment :
private fun decryptPrivateKey(dialog: DialogInterface) {
Log_OC.d(TAG, "Decrypt private key")
binding.encryptionStatus.setText(R.string.end_to_end_encryption_decrypting)
binding.progressBar.visibility = View.VISIBLE

try {
if (downloadKeyResult !is DownloadKeyResult.Success) {
Log_OC.d(TAG, "DownloadKeyResult is not success")
return
}
lifecycleScope.launch(Dispatchers.IO) {
try {
if (downloadKeyResult !is DownloadKeyResult.Success) {
Log_OC.d(TAG, "DownloadKeyResult is not success")
return@launch
}

val privateKey = (downloadKeyResult as DownloadKeyResult.Success).privateKey
if (privateKey.isNullOrEmpty()) {
Log_OC.e(TAG, "privateKey is null or empty")
return
}
val mnemonicUnchanged = binding.encryptionPasswordInput.text.toString().trim()
val mnemonic =
binding.encryptionPasswordInput.text.toString().replace("\\s".toRegex(), "")
.lowercase()
val decryptedPrivateKey = CryptoHelper.decryptPrivateKey(
privateKey,
mnemonic
)
val accountName = user?.accountName ?: return
val privateKey = (downloadKeyResult as DownloadKeyResult.Success).privateKey
if (privateKey.isNullOrEmpty()) {
Log_OC.e(TAG, "privateKey is null or empty")
return@launch
}
val mnemonicUnchanged = binding.encryptionPasswordInput.text.toString().trim()
val mnemonic =
binding.encryptionPasswordInput.text.toString().replace("\\s".toRegex(), "")
.lowercase()
val decryptedPrivateKey = CryptoHelper.decryptPrivateKey(
privateKey,
mnemonic
)
val accountName = user?.accountName ?: return@launch

arbitraryDataProvider?.storeOrUpdateKeyValue(
accountName,
EncryptionUtils.PRIVATE_KEY,
decryptedPrivateKey
)
dialog.dismiss()
arbitraryDataProvider?.storeOrUpdateKeyValue(
accountName,
EncryptionUtils.PRIVATE_KEY,
decryptedPrivateKey
)

Log_OC.d(TAG, "Private key successfully decrypted and stored")
Log_OC.d(TAG, "Private key successfully decrypted and stored")

arbitraryDataProvider?.storeOrUpdateKeyValue(
accountName,
EncryptionUtils.MNEMONIC,
mnemonicUnchanged
)
arbitraryDataProvider?.storeOrUpdateKeyValue(
accountName,
EncryptionUtils.MNEMONIC,
mnemonicUnchanged
)

// check if private key and public key match
val publicKey = arbitraryDataProvider?.getValue(
accountName,
EncryptionUtils.PUBLIC_KEY
)
// check if private key and public key match
val publicKey = arbitraryDataProvider?.getValue(
accountName,
EncryptionUtils.PUBLIC_KEY
)

val firstKey = EncryptionUtils.generateKey()
val base64encodedKey = EncryptionUtils.encodeBytesToBase64String(firstKey)
val encryptedString = EncryptionUtils.encryptStringAsymmetric(
base64encodedKey,
publicKey
)
val decryptedString = EncryptionUtils.decryptStringAsymmetric(
encryptedString,
decryptedPrivateKey
)
val secondKey = EncryptionUtils.decodeStringToBase64Bytes(decryptedString)
val firstKey = EncryptionUtils.generateKey()
val base64encodedKey = EncryptionUtils.encodeBytesToBase64String(firstKey)
val encryptedString = EncryptionUtils.encryptStringAsymmetric(
base64encodedKey,
publicKey
)
val decryptedString = EncryptionUtils.decryptStringAsymmetric(
encryptedString,
decryptedPrivateKey
)
val secondKey = EncryptionUtils.decodeStringToBase64Bytes(decryptedString)

if (!firstKey.contentEquals(secondKey)) {
EncryptionUtils.reportE2eError(arbitraryDataProvider, user)
throw Exception("Keys do not match")
}
if (!firstKey.contentEquals(secondKey)) {
EncryptionUtils.reportE2eError(arbitraryDataProvider, user)
throw Exception("Keys do not match")
}

notifyResult()
} catch (e: Exception) {
binding.encryptionStatus.setText(R.string.end_to_end_encryption_wrong_password)
Log_OC.e(TAG, "Error while decrypting private key: " + e.message)
withContext(Dispatchers.Main) {
dialog.dismiss()
notifyResult()
}
} catch (e: Exception) {
Log_OC.e(TAG, "Error while decrypting private key: " + e.message)

withContext(Dispatchers.Main) {
binding.encryptionStatus.setText(R.string.end_to_end_encryption_wrong_password)
binding.progressBar.visibility = View.GONE
}
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/layout/setup_encryption_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:gravity="clip_horizontal"
android:orientation="vertical"
android:padding="@dimen/dialog_padding">
Expand All @@ -35,6 +36,17 @@
tools:text="@string/placeholder_passphrase"
tools:visibility="visible" />

<com.google.android.material.progressindicator.LinearProgressIndicator
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/standard_margin"
android:visibility="gone"
tools:visibility="visible"
android:indeterminate="true"
app:indicatorColor="?attr/colorPrimary"
app:trackColor="?attr/colorSurfaceContainerHighest" />

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/encryption_passwordInput_container"
android:layout_width="match_parent"
Expand Down
Loading