Improve Android OpenPGP, drafts and release builds
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -426,7 +426,7 @@ internal object AndroidMailService {
|
||||
}
|
||||
}
|
||||
|
||||
private fun validateOpenPgpMime(message: MimeMessage) {
|
||||
internal fun validateOpenPgpMime(message: MimeMessage) {
|
||||
val contentType = message.contentType.lowercase(Locale.ROOT)
|
||||
val multipart = message.content as? Multipart
|
||||
?: throw IllegalArgumentException("The OpenPGP MIME message is malformed.")
|
||||
@@ -434,8 +434,8 @@ internal object AndroidMailService {
|
||||
if (contentType.startsWith("multipart/encrypted")) {
|
||||
val versionPart = multipart.getBodyPart(0)
|
||||
val encryptedPart = multipart.getBodyPart(1)
|
||||
val version = runCatching { versionPart.content.toString().trim() }.getOrDefault("")
|
||||
val armor = runCatching { String(encryptedPart.inputStream.readBytes(), Charsets.US_ASCII) }.getOrDefault("")
|
||||
val version = versionPart.inputStream.bufferedReader(Charsets.US_ASCII).use { it.readText().trim() }
|
||||
val armor = encryptedPart.inputStream.bufferedReader(Charsets.US_ASCII).use { it.readText() }
|
||||
require(versionPart.isMimeType("application/pgp-encrypted") && version == "Version: 1" &&
|
||||
encryptedPart.isMimeType("application/octet-stream") &&
|
||||
armor.contains("-----BEGIN PGP MESSAGE-----") && armor.contains("-----END PGP MESSAGE-----")) {
|
||||
@@ -443,7 +443,7 @@ internal object AndroidMailService {
|
||||
}
|
||||
} else if (contentType.startsWith("multipart/signed")) {
|
||||
val signaturePart = multipart.getBodyPart(1)
|
||||
val armor = runCatching { String(signaturePart.inputStream.readBytes(), Charsets.US_ASCII) }.getOrDefault("")
|
||||
val armor = signaturePart.inputStream.bufferedReader(Charsets.US_ASCII).use { it.readText() }
|
||||
require(signaturePart.isMimeType("application/pgp-signature") &&
|
||||
armor.contains("-----BEGIN PGP SIGNATURE-----") && armor.contains("-----END PGP SIGNATURE-----")) {
|
||||
"The OpenPGP signature is missing or malformed."
|
||||
|
||||
@@ -6,12 +6,15 @@ import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.app.Activity
|
||||
import android.app.AlertDialog
|
||||
import androidx.activity.result.ActivityResult
|
||||
import androidx.biometric.BiometricPrompt
|
||||
import com.getcapacitor.JSObject
|
||||
import com.getcapacitor.Plugin
|
||||
import com.getcapacitor.PluginCall
|
||||
import com.getcapacitor.PluginMethod
|
||||
import com.getcapacitor.annotation.ActivityCallback
|
||||
import com.getcapacitor.annotation.CapacitorPlugin
|
||||
import java.util.Locale
|
||||
import java.util.UUID
|
||||
@@ -78,6 +81,55 @@ class EpMailPlugin : Plugin() {
|
||||
call.resolve(JSObject().put("version", BuildConfig.VERSION_NAME))
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
fun pickPgpPrivateKey(call: PluginCall) {
|
||||
try {
|
||||
if (AndroidAccountVault.appLockEnabled()) {
|
||||
call.reject("File import is unavailable while app lock is enabled. Paste the key instead, or temporarily disable app lock and re-enable it after importing.")
|
||||
return
|
||||
}
|
||||
} catch (error: Exception) {
|
||||
call.reject(error.message ?: "Could not check the app lock setting.")
|
||||
return
|
||||
}
|
||||
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
|
||||
addCategory(Intent.CATEGORY_OPENABLE)
|
||||
type = "*/*"
|
||||
}
|
||||
try {
|
||||
startActivityForResult(call, intent, "pgpPrivateKeyPicked")
|
||||
} catch (_: android.content.ActivityNotFoundException) {
|
||||
call.reject("No file picker is available on this device.")
|
||||
}
|
||||
}
|
||||
|
||||
@ActivityCallback
|
||||
private fun pgpPrivateKeyPicked(call: PluginCall?, result: ActivityResult) {
|
||||
if (call == null) return
|
||||
if (result.resultCode != Activity.RESULT_OK) {
|
||||
call.resolve(JSObject().put("canceled", true))
|
||||
return
|
||||
}
|
||||
val uri = result.data?.data
|
||||
if (uri == null || uri.scheme != "content") {
|
||||
call.reject("Could not access the selected key file.")
|
||||
return
|
||||
}
|
||||
mailboxExecutor.execute {
|
||||
try {
|
||||
val bytes = context.contentResolver.openInputStream(uri)?.use {
|
||||
BoundedInputReader.read(it, 200001)
|
||||
} ?: throw IllegalArgumentException("Could not open the selected key file.")
|
||||
if (bytes.isEmpty() || bytes.size > 200000) {
|
||||
throw IllegalArgumentException("That key file is empty or too large (maximum 200 KB).")
|
||||
}
|
||||
call.resolve(JSObject().put("armoredKey", String(bytes, Charsets.UTF_8)))
|
||||
} catch (error: Exception) {
|
||||
call.reject(error.message ?: "Could not read the selected key file.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
fun getNotificationPrivacy(call: PluginCall) {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user