Use only the primary address key when uploading attachments for sending

The code was using all the address keys instead of only the primary key
to encrypt the attachments to upload.
This caused the key packet to contain several key packets (since the change in gopenpgp v2.4.3),
which was not accepted by the backend.
This commit is contained in:
M. Thiercelin 2022-04-14 17:05:00 +02:00
parent 1e573c0c1a
commit b2dc0d154a
No known key found for this signature in database
GPG Key ID: 29581E7E24EBEC0A
3 changed files with 16 additions and 6 deletions

View File

@ -92,7 +92,7 @@ class AttachmentsRepository @Inject constructor(
return@withContext Result.Failure("This attachment name / type is invalid. Please retry")
}
val encryptedAttachment = crypto.encrypt(fileContent, filename)
val encryptedAttachment = crypto.encryptWithPrimary(fileContent, filename)
val signedFileContent = armorer.unarmor(crypto.sign(fileContent))
val attachmentMimeType = mimeType.toMediaType()

View File

@ -167,8 +167,8 @@ class AddressCrypto @AssistedInject constructor(
/**
* Encrypt for Attachment
*/
fun encrypt(data: ByteArray, filename: String): CipherText {
val keyRing = createAndUnlockKeyRing()
fun encryptWithPrimary(data: ByteArray, filename: String): CipherText {
val keyRing = createAndUnlockPrimaryKeyRing().getOrThrow()
val pgpSplitMessage = keyRing.encryptAttachment(PlainMessage(data), filename)
keyRing.clearPrivateParams()
return CipherText(pgpSplitMessage.keyPacket, pgpSplitMessage.dataPacket)
@ -295,4 +295,14 @@ class AddressCrypto @AssistedInject constructor(
else -> IllegalStateException("$errorMessage. Caused by ${errors.joinToString { it.message!! }}")
}
}
private fun createAndUnlockPrimaryKeyRing(): Result<KeyRing> = runCatching {
val primaryAddressKey = currentKeys.first()
val addressKeyPassphrase = checkNotNull(
passphraseFor(primaryAddressKey)
) { "Could not get the address key passphrase" }
val lockedAddressKey = GoOpenPgpCrypto.newKeyFromArmored(primaryAddressKey.privateKey.string)
val unlockedAddressKey = lockedAddressKey.unlock(addressKeyPassphrase)
GoOpenPgpCrypto.newKeyRing(unlockedAddressKey)
}
}

View File

@ -120,7 +120,7 @@ class AttachmentsRepositoryTest : CoroutinesTest {
every { this@mockk.mimeType } returns mimeType
every { this@mockk.getFileContent() } returns fileContent
}
every { crypto.encrypt(fileContent, fileName) } returns mockCipherText
every { crypto.encryptWithPrimary(fileContent, fileName) } returns mockCipherText
every { crypto.sign(fileContent) } returns signedFileContent
every { armorer.unarmor(signedFileContent) } returns unarmoredSignedFileContent
@ -170,7 +170,7 @@ class AttachmentsRepositoryTest : CoroutinesTest {
every { this@mockk.mimeType } returns mimeType
every { this@mockk.getFileContent() } returns fileContent
}
every { crypto.encrypt(fileContent, fileName) } returns mockCipherText
every { crypto.encryptWithPrimary(fileContent, fileName) } returns mockCipherText
every { crypto.sign(fileContent) } returns signedFileContent
every { armorer.unarmor(signedFileContent) } returns unarmoredSignedFileContent
@ -206,7 +206,7 @@ class AttachmentsRepositoryTest : CoroutinesTest {
every { this@mockk.mimeType } returns mimeType
every { this@mockk.getFileContent() } returns fileContent
}
every { crypto.encrypt(fileContent, fileName) } returns mockCipherText
every { crypto.encryptWithPrimary(fileContent, fileName) } returns mockCipherText
every { crypto.sign(fileContent) } returns signedFileContent
every { armorer.unarmor(signedFileContent) } returns unarmoredSignedFileContent