Android security implementation Phase 3.1

This commit is contained in:
2026-11-03 18:05:14 +02:00
parent d165fdc4b4
commit e3b3a9bb52
14 changed files with 224 additions and 48 deletions
@@ -275,10 +275,7 @@ internal object AndroidMailService {
val bytes = Base64.decode(rawBase64, Base64.DEFAULT)
require(bytes.size <= 25 * 1024 * 1024) { "The encrypted message is too large to send on Android testing builds." }
message = MimeMessage(session, ByteArrayInputStream(bytes))
val contentType = message.contentType.lowercase(Locale.ROOT)
require(contentType.startsWith("multipart/encrypted") || contentType.startsWith("multipart/signed")) {
"Only an OpenPGP MIME message can use the encrypted send path."
}
validateOpenPgpMime(message)
require(message.from?.isNotEmpty() == true && message.allRecipients?.isNotEmpty() == true) {
"The encrypted message needs a sender and at least one recipient."
}
@@ -373,6 +370,33 @@ internal object AndroidMailService {
}
}
private 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.")
require(multipart.count == 2) { "The OpenPGP MIME message has an unexpected structure." }
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("")
require(versionPart.isMimeType("application/pgp-encrypted") && version == "Version: 1" &&
encryptedPart.isMimeType("application/octet-stream") &&
armor.contains("-----BEGIN PGP MESSAGE-----") && armor.contains("-----END PGP MESSAGE-----")) {
"The encrypted OpenPGP payload is missing or malformed."
}
} else if (contentType.startsWith("multipart/signed")) {
val signaturePart = multipart.getBodyPart(1)
val armor = runCatching { String(signaturePart.inputStream.readBytes(), Charsets.US_ASCII) }.getOrDefault("")
require(signaturePart.isMimeType("application/pgp-signature") &&
armor.contains("-----BEGIN PGP SIGNATURE-----") && armor.contains("-----END PGP SIGNATURE-----")) {
"The OpenPGP signature is missing or malformed."
}
} else {
throw IllegalArgumentException("Only an OpenPGP MIME message can use the protected send path.")
}
}
fun savePgpAttachment(context: Context, input: JSONObject): JSONObject {
val filename = input.optString("filename").replace(Regex("[\\\\/\\r\\n\\u0000]"), "").take(255).ifBlank { "attachment" }
val contentType = input.optString("contentType", "application/octet-stream").lowercase(Locale.ROOT)