Improve Android OpenPGP, drafts and release builds

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2026-11-17 22:11:05 +02:00
co-authored by Copilot
parent d0e2ce26bc
commit 96c7b6966b
28 changed files with 747 additions and 187 deletions
@@ -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 {
@@ -0,0 +1,52 @@
package studio.daybreak.mail
import java.io.ByteArrayInputStream
import java.io.File
import java.util.Properties
import javax.mail.Session
import javax.mail.internet.MimeMessage
import org.junit.Assert.assertThrows
import org.junit.Assume.assumeTrue
import org.junit.Test
class AndroidPgpMimeValidationTest {
private fun message(contentType: String, parts: String): MimeMessage {
val source = "MIME-Version: 1.0\r\nContent-Type: $contentType; boundary=\"test\"\r\n\r\n$parts"
return MimeMessage(Session.getInstance(Properties()), ByteArrayInputStream(source.toByteArray(Charsets.UTF_8)))
}
private fun encrypted(version: String = "Version: 1", partType: String = "application/octet-stream", armor: String = "-----BEGIN PGP MESSAGE-----\r\nabc\r\n-----END PGP MESSAGE-----"): MimeMessage =
message("multipart/encrypted; protocol=\"application/pgp-encrypted\"",
"--test\r\nContent-Type: application/pgp-encrypted\r\n\r\n$version\r\n" +
"--test\r\nContent-Type: $partType\r\n\r\n$armor\r\n--test--\r\n")
private fun signed(armor: String = "-----BEGIN PGP SIGNATURE-----\r\nabc\r\n-----END PGP SIGNATURE-----"): MimeMessage =
message("multipart/signed; protocol=\"application/pgp-signature\"",
"--test\r\nContent-Type: text/plain\r\n\r\nhello\r\n" +
"--test\r\nContent-Type: application/pgp-signature\r\n\r\n$armor\r\n--test--\r\n")
@Test
fun acceptsValidEncryptedAndSignedMime() {
AndroidMailService.validateOpenPgpMime(encrypted())
AndroidMailService.validateOpenPgpMime(signed())
}
@Test
fun rejectsMalformedEncryptedAndSignedMime() {
assertThrows(IllegalArgumentException::class.java) { AndroidMailService.validateOpenPgpMime(encrypted(version = "Version: 2")) }
assertThrows(IllegalArgumentException::class.java) { AndroidMailService.validateOpenPgpMime(encrypted(partType = "text/plain")) }
assertThrows(IllegalArgumentException::class.java) { AndroidMailService.validateOpenPgpMime(encrypted(armor = "missing armor")) }
assertThrows(IllegalArgumentException::class.java) { AndroidMailService.validateOpenPgpMime(signed(armor = "missing signature")) }
}
@Test
fun acceptsMimeGeneratedByAndroidOpenPgpBuilder() {
val directory = System.getenv("EP_MAIL_PGP_MIME_FIXTURE_DIR")
assumeTrue("Set EP_MAIL_PGP_MIME_FIXTURE_DIR to run the JavaScript-to-JavaMail integration test.", !directory.isNullOrBlank())
for (name in listOf("encrypted", "signed", "encrypted-signed")) {
val file = File(directory, "$name.eml")
val parsed = file.inputStream().use { MimeMessage(Session.getInstance(Properties()), it) }
AndroidMailService.validateOpenPgpMime(parsed)
}
}
}