|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 The Matrix.org Foundation C.I.C. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.matrix.android.sdk.internal.session.contentscanner |
| 18 | + |
| 19 | +import okio.ByteString.Companion.decodeBase64 |
| 20 | +import org.amshove.kluent.shouldBe |
| 21 | +import org.amshove.kluent.shouldBeEqualTo |
| 22 | +import org.amshove.kluent.shouldNotBe |
| 23 | +import org.junit.Test |
| 24 | +import org.matrix.android.sdk.api.session.crypto.attachments.ElementToDecrypt |
| 25 | +import org.matrix.android.sdk.api.session.crypto.model.EncryptedFileInfo |
| 26 | +import org.matrix.android.sdk.api.session.crypto.model.EncryptedFileKey |
| 27 | +import org.matrix.android.sdk.internal.crypto.tools.withOlmDecryption |
| 28 | +import org.matrix.android.sdk.internal.di.MoshiProvider |
| 29 | +import org.matrix.android.sdk.internal.session.contentscanner.model.DownloadBody |
| 30 | +import org.matrix.android.sdk.internal.session.contentscanner.model.EncryptedBody |
| 31 | +import org.matrix.olm.OlmPkMessage |
| 32 | + |
| 33 | +class ScanEncryptorUtilsTest { |
| 34 | + private val anMxcUrl = "mxc://matrix.org/123456" |
| 35 | + private val anElementToDecrypt = ElementToDecrypt( |
| 36 | + k = "key", |
| 37 | + iv = "iv", |
| 38 | + sha256 = "sha256" |
| 39 | + ) |
| 40 | + private val aPublicKey = "6n3l15JqsNhpM1OwRIoDCL/3c1B5idcwvy07Y5qFRyw=" |
| 41 | + private val aPrivateKey = "CLYwNaeA9d0KHE0DniO1bxGgmNsPJ/pyanF4b4tcK1M=" |
| 42 | + |
| 43 | + @Test |
| 44 | + fun whenNoServerKeyIsProvidedTheContentIsNotEncrypted() { |
| 45 | + val result = ScanEncryptorUtils.getDownloadBodyAndEncryptIfNeeded( |
| 46 | + publicServerKey = null, |
| 47 | + mxcUrl = anMxcUrl, |
| 48 | + elementToDecrypt = anElementToDecrypt |
| 49 | + ) |
| 50 | + result shouldBeEqualTo DownloadBody( |
| 51 | + file = EncryptedFileInfo( |
| 52 | + url = anMxcUrl, |
| 53 | + iv = anElementToDecrypt.iv, |
| 54 | + hashes = mapOf("sha256" to anElementToDecrypt.sha256), |
| 55 | + key = EncryptedFileKey( |
| 56 | + k = anElementToDecrypt.k, |
| 57 | + alg = "A256CTR", |
| 58 | + keyOps = listOf("encrypt", "decrypt"), |
| 59 | + kty = "oct", |
| 60 | + ext = true |
| 61 | + ), |
| 62 | + v = "v2" |
| 63 | + ), |
| 64 | + encryptedBody = null |
| 65 | + ) |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + fun whenServerKeyIsProvidedTheContentIsEncrypted() { |
| 70 | + System.loadLibrary("olm") |
| 71 | + val result = ScanEncryptorUtils.getDownloadBodyAndEncryptIfNeeded( |
| 72 | + publicServerKey = aPublicKey, |
| 73 | + mxcUrl = anMxcUrl, |
| 74 | + elementToDecrypt = anElementToDecrypt |
| 75 | + ) |
| 76 | + result.file shouldBe null |
| 77 | + // Note: we cannot check the members of EncryptedBody because they change on each call. |
| 78 | + result.encryptedBody shouldNotBe null |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + fun checkThatTheCodeIsAbleToDecryptContent() { |
| 83 | + System.loadLibrary("olm") |
| 84 | + val clearInfo = ScanEncryptorUtils.getDownloadBodyAndEncryptIfNeeded( |
| 85 | + publicServerKey = null, |
| 86 | + mxcUrl = anMxcUrl, |
| 87 | + elementToDecrypt = anElementToDecrypt |
| 88 | + ) |
| 89 | + // Uncomment to get a new encrypted body |
| 90 | + // val encryptedBody = ScanEncryptorUtils.getDownloadBodyAndEncryptIfNeeded( |
| 91 | + // publicServerKey = aPublicKey, |
| 92 | + // mxcUrl = anMxcUrl, |
| 93 | + // elementToDecrypt = anElementToDecrypt |
| 94 | + // ).encryptedBody!! |
| 95 | + // println("libolmEncryptedBody: $encryptedBody") |
| 96 | + val libolmEncryptedBody = EncryptedBody( |
| 97 | + cipherText = "GTnDhm6xe5fPe/QCr6fyGcZXheFhZlPG" + |
| 98 | + "nJZiCK8Xwq6qTg71vSUGWtLdt3uaTmK7" + |
| 99 | + "F7fB3PBKchHu2VVv6MMgo8fpUQ7KBbmu" + |
| 100 | + "NWTrNmf3QdhXuRwUwz/q4GxsbGR2zjSX" + |
| 101 | + "/UoE5S4ymVtOVhvSfXQfssN56wVIzC6S" + |
| 102 | + "dy57y6b1IXPihlCUdvb8LMkMvViHYeNf" + |
| 103 | + "beFrAfMlsyr1+jdZEXZF5Q7iruhsH2iu" + |
| 104 | + "k7+Ayl9rdILCD5tjE9pezwe1V6uc/Agb", |
| 105 | + mac = "Wk77HRg50oM", |
| 106 | + ephemeral = "rMTK6/CGASinfX4USFS5qmD3r4meffxKc/jCSFIBczw" |
| 107 | + ) |
| 108 | + // Try to decrypt the body |
| 109 | + val result = withOlmDecryption { olmPkDecryption -> |
| 110 | + olmPkDecryption.setPrivateKey(aPrivateKey.decodeBase64()!!.toByteArray()) |
| 111 | + olmPkDecryption.decrypt( |
| 112 | + OlmPkMessage().apply { |
| 113 | + mCipherText = libolmEncryptedBody.cipherText |
| 114 | + mMac = libolmEncryptedBody.mac |
| 115 | + mEphemeralKey = libolmEncryptedBody.ephemeral |
| 116 | + } |
| 117 | + ) |
| 118 | + } |
| 119 | + val parseResult = MoshiProvider.providesMoshi() |
| 120 | + .adapter(DownloadBody::class.java) |
| 121 | + .fromJson(result) |
| 122 | + parseResult shouldBeEqualTo clearInfo |
| 123 | + } |
| 124 | +} |
0 commit comments