|
| 1 | +package org.wordpress.android.fluxc |
| 2 | + |
| 3 | +import android.util.Base64 |
| 4 | +import android.util.Base64.DEFAULT |
| 5 | +import com.goterl.lazycode.lazysodium.interfaces.SecretStream |
| 6 | +import com.goterl.lazycode.lazysodium.utils.KeyPair |
| 7 | +import org.json.JSONObject |
| 8 | +import org.junit.Assert.assertEquals |
| 9 | +import org.junit.Assert.assertNotNull |
| 10 | +import org.junit.Before |
| 11 | +import org.junit.Test |
| 12 | +import org.wordpress.android.fluxc.model.encryptedlogging.EncryptedLoggingKey |
| 13 | +import org.wordpress.android.fluxc.model.encryptedlogging.EncryptedSecretStreamKey |
| 14 | +import org.wordpress.android.fluxc.model.encryptedlogging.EncryptionUtils |
| 15 | +import org.wordpress.android.fluxc.model.encryptedlogging.LogEncrypter |
| 16 | +import org.wordpress.android.fluxc.model.encryptedlogging.SecretStreamKey |
| 17 | +import java.util.UUID |
| 18 | +import kotlin.random.Random.Default.nextInt |
| 19 | + |
| 20 | +class LogEncrypterTest { |
| 21 | + private lateinit var keypair: KeyPair |
| 22 | + private val logDecrypter: LogDecrypter = LogDecrypter() |
| 23 | + |
| 24 | + @Before |
| 25 | + fun setup() { |
| 26 | + keypair = EncryptionUtils.sodium.cryptoBoxKeypair() |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + @Throws |
| 31 | + fun testThatEncryptedLogsMatchV1FileFormat() { |
| 32 | + val testLogString = UUID.randomUUID().toString() |
| 33 | + val encryptedLog = encryptContent(testLogString) |
| 34 | + |
| 35 | + val json = JSONObject(encryptedLog) |
| 36 | + assertEquals( |
| 37 | + "`keyedWith` must ALWAYS be v1 in this version of the file format", |
| 38 | + "v1", |
| 39 | + json.getString("keyedWith") |
| 40 | + ) |
| 41 | + |
| 42 | + assertNotNull( |
| 43 | + "The UUID must be valid", |
| 44 | + UUID.fromString(json.getString("uuid")) |
| 45 | + ) |
| 46 | + |
| 47 | + assertEquals( |
| 48 | + "The header must be 32 bytes long", |
| 49 | + 32, |
| 50 | + json.getString("header").count() |
| 51 | + ) |
| 52 | + |
| 53 | + assertEquals( |
| 54 | + "The encrypted key should be 108 bytes long", |
| 55 | + 108, |
| 56 | + json.getString("encryptedKey").count() |
| 57 | + ) |
| 58 | + |
| 59 | + assertEquals( |
| 60 | + "There should be one message and the closing tag", |
| 61 | + 2, |
| 62 | + json.getJSONArray("messages").length() |
| 63 | + ) |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + fun testThatLogsCanBeDecrypted() { |
| 68 | + val testLogString = UUID.randomUUID().toString() |
| 69 | + assertEquals(testLogString, decryptContent(encryptContent(testLogString))) |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + fun testThatMultilineLogsCanBeDecrypted() { |
| 74 | + val testLogString = (0..(nextInt(100) + 2)).joinToString(separator = "\n") { UUID.randomUUID().toString() } |
| 75 | + assertEquals(testLogString, decryptContent(encryptContent(testLogString))) |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + fun testThatEmptyLogsCanBeEncrypted() { |
| 80 | + val testLogString = "" |
| 81 | + assertEquals(testLogString, decryptContent(encryptContent(testLogString))) |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + fun testThatExplicitUUIDsCanBeRetrievedFromEncryptedLogs() { |
| 86 | + val testUuid = UUID.randomUUID().toString() |
| 87 | + |
| 88 | + val (_, uuid) = logDecrypter.decrypt(encryptContent("", testUuid), keypair) |
| 89 | + assertEquals(uuid, testUuid) |
| 90 | + } |
| 91 | + |
| 92 | + // Helpers |
| 93 | + |
| 94 | + private fun encryptContent(content: String, uuid: String = UUID.randomUUID().toString()): String { |
| 95 | + return LogEncrypter(EncryptedLoggingKey(keypair.publicKey)).encrypt(content, uuid) |
| 96 | + } |
| 97 | + |
| 98 | + private fun decryptContent(encryptedText: String): String { |
| 99 | + return logDecrypter.decrypt(encryptedText, keypair).first |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * EncryptedStream represents the encrypted stream once the key has been decrypted. It exists to separate |
| 105 | + * the key decryption from the stream decryption while decoding. |
| 106 | + * |
| 107 | + * @param key An unencrypted SecretStreamKey used to decrypt the remainder of the log. |
| 108 | + * @param header A `ByteArray` representing the stream header – it's used to initialize the decryption stream. |
| 109 | + * @param messages A `List<ByteArray>` of encrypted messages |
| 110 | + */ |
| 111 | +private class EncryptedStream(val key: SecretStreamKey, val header: ByteArray, val messages: List<ByteArray>) |
| 112 | + |
| 113 | +private const val JSON_KEYED_WITH_KEY = "keyedWith" |
| 114 | +private const val JSON_UUID_KEY = "uuid" |
| 115 | +private const val JSON_HEADER_KEY = "header" |
| 116 | +private const val JSON_ENCRYPTED_KEY_KEY = "encryptedKey" |
| 117 | +private const val JSON_MESSAGES_KEY = "messages" |
| 118 | + |
| 119 | +/** |
| 120 | + * LogDecrypter allows decrypting encrypted content. |
| 121 | + */ |
| 122 | +private class LogDecrypter { |
| 123 | + private val sodium = EncryptionUtils.sodium |
| 124 | + private val state = SecretStream.State.ByReference() |
| 125 | + |
| 126 | + private fun encryptedStream(encryptedText: String, keyPair: KeyPair): Pair<EncryptedStream, String> { |
| 127 | + val json = JSONObject(encryptedText) |
| 128 | + |
| 129 | + require(json.getString(JSON_KEYED_WITH_KEY) == "v1") { |
| 130 | + "This class can only parse files keyedWith the v1 implementation" |
| 131 | + } |
| 132 | + |
| 133 | + val uuid = json.getString(JSON_UUID_KEY) |
| 134 | + val header = json.getString(JSON_HEADER_KEY).base64Decode() |
| 135 | + val encryptedKey = EncryptedSecretStreamKey(json.getString(JSON_ENCRYPTED_KEY_KEY).base64Decode()) |
| 136 | + val messagesJson = json.getJSONArray(JSON_MESSAGES_KEY) |
| 137 | + |
| 138 | + val messages = (0 until messagesJson.length()).map { messagesJson.getString(it).base64Decode() } |
| 139 | + |
| 140 | + val encryptedStream = EncryptedStream(encryptedKey.decrypt(keyPair), header, messages) |
| 141 | + check(sodium.cryptoSecretStreamInitPull(state, encryptedStream.header, encryptedStream.key.bytes)) |
| 142 | + return Pair(encryptedStream, uuid) |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Decrypts and returns the log file as a String. |
| 147 | + * |
| 148 | + * @param encryptedText The encrypted text to decrypt. |
| 149 | + * @param keyPair The public and secret key pair associated with this file. Both are required to decrypt the file. |
| 150 | + */ |
| 151 | + fun decrypt(encryptedText: String, keyPair: KeyPair): Pair<String, String> { |
| 152 | + val (encryptedStream, uuid) = encryptedStream(encryptedText, keyPair) |
| 153 | + val decryptedText = encryptedStream.messages.fold("") { accumulated: String, cipherBytes: ByteArray -> |
| 154 | + String |
| 155 | + val plainBytes = ByteArray(cipherBytes.size - SecretStream.ABYTES) |
| 156 | + |
| 157 | + val tag = ByteArray(1) // Stores the extracted tag. This implementation doesn't do anything with it. |
| 158 | + check(sodium.cryptoSecretStreamPull(state, plainBytes, tag, cipherBytes, cipherBytes.size.toLong())) |
| 159 | + |
| 160 | + accumulated + String(plainBytes) |
| 161 | + } |
| 162 | + return Pair(decryptedText, uuid) |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +// On Android base64 has lots of options, so define an extension to make it easier to avoid decoding issues. |
| 167 | +private fun String.base64Decode(): ByteArray { |
| 168 | + return Base64.decode(this, DEFAULT) |
| 169 | +} |
0 commit comments