fix(crypto): Handle null values when decrypting empty messages.

When decrypting an empty OpenPGP message, gopenpgp will return
null in the getBinary().
This null value was not properly handled by the crypto module and would
result in a NullPointerException.

CP-5565 MAILANDR-435
This commit is contained in:
M. Thiercelin 2023-03-10 17:04:49 +01:00
parent 847ae561c9
commit 6e8d2a4c8d
No known key found for this signature in database
GPG Key ID: 29581E7E24EBEC0A
1 changed files with 11 additions and 4 deletions

View File

@ -626,14 +626,14 @@ class GOpenPGPCrypto : PGPCrypto {
message: EncryptedMessage,
unlockedKey: Unarmored
): ByteArray = runCatching {
decryptMessage(message, unlockedKey) { it.binary }
decryptMessage(message, unlockedKey) { it.getBinaryOrEmpty() }
}.getOrElse { throw CryptoException("Message cannot be decrypted.", it) }
override fun decryptData(
data: DataPacket,
sessionKey: SessionKey
): ByteArray = runCatching {
decryptDataSessionKey(data, sessionKey).binary
decryptDataSessionKey(data, sessionKey).getBinaryOrEmpty()
}.getOrElse { throw CryptoException("Data cannot be decrypted.", it) }
override fun decryptFile(
@ -686,7 +686,7 @@ class GOpenPGPCrypto : PGPCrypto {
): DecryptedData = runCatching {
decryptAndVerifyMessage(message, publicKeys, unlockedKeys, time.toUtcSeconds()) {
DecryptedData(
it.message.binary,
it.message.getBinaryOrEmpty(),
it.signatureVerificationError.toVerificationStatus()
)
}
@ -700,7 +700,7 @@ class GOpenPGPCrypto : PGPCrypto {
): DecryptedData = runCatching {
decryptAndVerifyDataSessionKey(data, sessionKey, publicKeys, time.toUtcSeconds()).let {
DecryptedData(
it.message.binary,
it.message.getBinaryOrEmpty(),
it.signatureVerificationError.toVerificationStatus()
)
}
@ -1028,6 +1028,13 @@ class GOpenPGPCrypto : PGPCrypto {
predicate: () -> Boolean
): String = if (predicate.invoke()) trimLinesEnd() else this
/**
* When gopenpgp decrypts an empty message, getBinary() will return null.
* We map the null value to an empty ByteArray()
*/
private fun PlainMessage.getBinaryOrEmpty(): ByteArray = binary ?: ByteArray(0)
companion object {
// 32K is usually not far from the optimal buffer size on Android devices.
const val DEFAULT_BUFFER_SIZE = 32_768