Skip to content

Commit

Permalink
Encrypt data for the content scanner using PkEncryption from the Rust…
Browse files Browse the repository at this point in the history
… SDK.
  • Loading branch information
bmarty committed Sep 11, 2024
1 parent fc68f1c commit 355621b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,14 @@

package org.matrix.android.sdk.internal.session.contentscanner

import okio.ByteString.Companion.decodeBase64
import org.amshove.kluent.shouldBe
import org.amshove.kluent.shouldBeEqualTo
import org.amshove.kluent.shouldNotBe
import org.junit.Test
import org.matrix.android.sdk.api.session.crypto.attachments.ElementToDecrypt
import org.matrix.android.sdk.api.session.crypto.model.EncryptedFileInfo
import org.matrix.android.sdk.api.session.crypto.model.EncryptedFileKey
import org.matrix.android.sdk.internal.crypto.tools.withOlmDecryption
import org.matrix.android.sdk.internal.di.MoshiProvider
import org.matrix.android.sdk.internal.session.contentscanner.model.DownloadBody
import org.matrix.android.sdk.internal.session.contentscanner.model.EncryptedBody
import org.matrix.olm.OlmPkMessage

class ScanEncryptorUtilsTest {
private val anMxcUrl = "mxc://matrix.org/123456"
Expand Down Expand Up @@ -67,7 +62,6 @@ class ScanEncryptorUtilsTest {

@Test
fun whenServerKeyIsProvidedTheContentIsEncrypted() {
System.loadLibrary("olm")
val result = ScanEncryptorUtils.getDownloadBodyAndEncryptIfNeeded(
publicServerKey = aPublicKey,
mxcUrl = anMxcUrl,
Expand All @@ -78,6 +72,8 @@ class ScanEncryptorUtilsTest {
result.encryptedBody shouldNotBe null
}

// Note: PkDecryption is not exposed in the FFI layer, so we cannot use this test.
/*
@Test
fun checkThatTheCodeIsAbleToDecryptContent() {
System.loadLibrary("olm")
Expand Down Expand Up @@ -121,4 +117,5 @@ class ScanEncryptorUtilsTest {
.fromJson(result)
parseResult shouldBeEqualTo clearInfo
}
*/
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ package org.matrix.android.sdk.internal.session.contentscanner
import org.matrix.android.sdk.api.session.crypto.attachments.ElementToDecrypt
import org.matrix.android.sdk.api.session.crypto.model.EncryptedFileInfo
import org.matrix.android.sdk.api.session.crypto.model.EncryptedFileKey
import org.matrix.android.sdk.internal.crypto.tools.withOlmEncryption
import org.matrix.android.sdk.internal.session.contentscanner.model.DownloadBody
import org.matrix.android.sdk.internal.session.contentscanner.model.EncryptedBody
import org.matrix.android.sdk.internal.session.contentscanner.model.toCanonicalJson
import org.matrix.rustcomponents.sdk.crypto.PkEncryption

internal object ScanEncryptorUtils {

Expand All @@ -43,22 +43,15 @@ internal object ScanEncryptorUtils {
v = "v2"
)
return if (publicServerKey != null) {
// We should encrypt
withOlmEncryption { //olm ->
// TODO BMA
error("Not supported anymore")
/*
olm.setRecipientKey(publicServerKey)
val olmResult = olm.encrypt(DownloadBody(encryptedInfo).toCanonicalJson())
DownloadBody(
encryptedBody = EncryptedBody(
cipherText = olmResult.mCipherText,
ephemeral = olmResult.mEphemeralKey,
mac = olmResult.mMac
)
)
*/
}
val pkEncryption = PkEncryption.fromBase64(key = publicServerKey)
val pkMessage = pkEncryption.encrypt(DownloadBody(encryptedInfo).toCanonicalJson())
DownloadBody(
encryptedBody = EncryptedBody(
cipherText = pkMessage.ciphertext,
ephemeral = pkMessage.ephemeralKey,
mac = pkMessage.mac
)
)
} else {
DownloadBody(encryptedInfo)
}
Expand Down

0 comments on commit 355621b

Please sign in to comment.