Skip to content

Commit

Permalink
Added asking a missed passphrase if we trying to download encrypted a…
Browse files Browse the repository at this point in the history
…ttachments.| #1255
  • Loading branch information
DenBond7 committed Jun 10, 2021
1 parent 34ff921 commit bc13231
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.flowcrypt.email.database.FlowCryptRoomDatabase
import com.flowcrypt.email.database.entity.AccountEntity
import com.flowcrypt.email.extensions.org.bouncycastle.openpgp.toPgpKeyDetails
import com.flowcrypt.email.security.model.PgpKeyDetails
import com.flowcrypt.email.security.pgp.PgpDecrypt
import com.flowcrypt.email.security.pgp.PgpKey
import com.flowcrypt.email.security.pgp.PgpPwd
import com.flowcrypt.email.util.exception.DifferentPassPhrasesException
Expand Down Expand Up @@ -161,5 +162,13 @@ class SecurityUtils {
fun generateRandomUUID(): String {
return String(Hex.encodeHex(DigestUtils.sha1(UUID.randomUUID().toString())))
}

/**
* Check if the file extension fits the encrypted pattern.
* If yes - it can mean the file is encrypted
*/
fun isEncryptedData(fileName: String?): Boolean {
return PgpDecrypt.DETECT_SEPARATE_ENCRYPTED_ATTACHMENTS_PATTERN.find(fileName ?: "") != null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import com.flowcrypt.email.database.entity.AccountEntity
import com.flowcrypt.email.extensions.kotlin.toHex
import com.flowcrypt.email.jetpack.viewmodel.AccountViewModel
import com.flowcrypt.email.security.KeysStorageImpl
import com.flowcrypt.email.security.SecurityUtils
import com.flowcrypt.email.security.pgp.PgpDecrypt
import com.flowcrypt.email.util.FileAndDirectoryUtils
import com.flowcrypt.email.util.GeneralUtil
Expand Down Expand Up @@ -653,8 +654,7 @@ class AttachmentDownloadManagerService : Service() {
throw NullPointerException("Error. The file is missing")
}

val regex = PgpDecrypt.DETECT_SEPARATE_ENCRYPTED_ATTACHMENTS_PATTERN
if (regex.find(att.name ?: "") == null) {
if (!SecurityUtils.isEncryptedData(att.name)) {
return file
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,22 @@ class AttachmentNotificationManager(context: Context) : CustomNotificationManage
fun errorHappened(context: Context, attInfo: AttachmentInfo, e: Exception) {
val builder = genDefBuilder(context, attInfo)

val bitText = StringBuilder()
val bigText = StringBuilder()
val contentText = StringBuilder()
when {
TextUtils.isEmpty(e.message) -> {
contentText.append(context.getString(R.string.error_occurred_please_try_again))
bitText.append(e.javaClass.simpleName)
bigText.append(e.javaClass.simpleName)
}

e.cause != null -> {
contentText.append(e.cause?.message ?: "")
bitText.append(e.javaClass.simpleName + ": " + e.message)
bigText.append(e.javaClass.simpleName + ": " + e.message)
}

else -> {
contentText.append(e.message)
bitText.append(e.javaClass.simpleName + ": " + e.message)
bigText.append(e.javaClass.simpleName + ": " + e.message)
}
}

Expand All @@ -159,7 +159,7 @@ class AttachmentNotificationManager(context: Context) : CustomNotificationManage
R.plurals.please_provide_passphrase_for_following_keys,
1
) + "\n" + GeneralUtil.doSectionsInText(" ", it, 4) + "\n\n"
bitText.insert(0, additionalText)
bigText.insert(0, additionalText)
}
}
}
Expand All @@ -175,8 +175,8 @@ class AttachmentNotificationManager(context: Context) : CustomNotificationManage
.setGroup(GROUP_NAME_ATTACHMENTS)
.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN)

if (bitText.isNotEmpty()) {
builder.setStyle(NotificationCompat.BigTextStyle().bigText(bitText))
if (bigText.isNotEmpty()) {
builder.setStyle(NotificationCompat.BigTextStyle().bigText(bigText))
}

notificationManagerCompat.notify(attInfo.id, attInfo.uid.toInt(), builder.build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import com.flowcrypt.email.jetpack.viewmodel.factory.MsgDetailsViewModelFactory
import com.flowcrypt.email.model.MessageEncryptionType
import com.flowcrypt.email.model.MessageType
import com.flowcrypt.email.model.PgpContact
import com.flowcrypt.email.security.SecurityUtils
import com.flowcrypt.email.service.attachment.AttachmentDownloadManagerService
import com.flowcrypt.email.ui.activity.CreateMessageActivity
import com.flowcrypt.email.ui.activity.ImportPrivateKeyActivity
Expand Down Expand Up @@ -129,6 +130,21 @@ class MessageDetailsFragment : BaseFragment(), ProgressBehaviour, View.OnClickLi
override fun onDownloadClick(attachmentInfo: AttachmentInfo) {
lastClickedAtt = attachmentInfo
lastClickedAtt?.orderNumber = GeneralUtil.genAttOrderId(requireContext())

if (SecurityUtils.isEncryptedData(attachmentInfo.name)) {
for (block in msgInfo?.msgBlocks ?: emptyList()) {
if (block.type == MsgBlock.Type.DECRYPT_ERROR) {
val decryptErrorMsgBlock = block as? DecryptErrorMsgBlock ?: continue
val decryptErrorDetails = decryptErrorMsgBlock.error?.details ?: continue
if (decryptErrorDetails.type == DecryptErrorDetails.Type.NEED_PASSPHRASE) {
val fingerprints = decryptErrorMsgBlock.error.longIds?.needPassphrase ?: continue
showNeedPassphraseDialog(fingerprints)
return
}
}
}
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q ||
ContextCompat.checkSelfPermission(
requireContext(),
Expand Down

0 comments on commit bc13231

Please sign in to comment.