Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Drop encryption of non-sensitive data #164

Merged
merged 1 commit into from
Nov 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,19 @@ import kotlinx.coroutines.test.runBlockingTest
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
internal class EncryptedDiskPersistenceTest {
internal class DiskPersistenceTest {

private val context: Context = ApplicationProvider.getApplicationContext()
private val coroutineScope = TestCoroutineScope()
private val rootFolder = "relaydroid_test"
private val subject = EncryptedDiskPersistence(
private val subject = DiskPersistence(
context,
coroutineScope.coroutineContext,
rootFolder
Expand Down Expand Up @@ -58,15 +57,15 @@ internal class EncryptedDiskPersistenceTest {
}

@Test
fun setEncryptsContent() = coroutineScope.runBlockingTest {
fun setContent() = coroutineScope.runBlockingTest {
val location = "file"
val data = "test"
subject.set(location, data.toByteArray())
val fileContent =
File(context.filesDir, "$rootFolder${File.separator}$location")
.readBytes()
.toString(Charset.defaultCharset())
assertNotEquals(data, fileContent)
assertEquals(data, fileContent)
}

@Test
Expand Down
4 changes: 2 additions & 2 deletions lib/src/main/java/tech/relaycorp/awaladroid/Awala.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import tech.relaycorp.awala.keystores.file.FileKeystoreRoot
import tech.relaycorp.awala.keystores.file.FileSessionPublicKeystore
import tech.relaycorp.awaladroid.background.ServiceInteractor
import tech.relaycorp.awaladroid.storage.StorageImpl
import tech.relaycorp.awaladroid.storage.persistence.EncryptedDiskPersistence
import tech.relaycorp.awaladroid.storage.persistence.DiskPersistence
import tech.relaycorp.relaynet.nodes.EndpointManager

public object Awala {
Expand All @@ -31,7 +31,7 @@ public object Awala {
val androidPrivateKeyStore = AndroidPrivateKeyStore(keystoreRoot, context)
val fileSessionPublicKeystore = FileSessionPublicKeystore(keystoreRoot)
this.context = AwalaContext(
StorageImpl(EncryptedDiskPersistence(context)),
StorageImpl(DiskPersistence(context)),
GatewayClientImpl(
serviceInteractorBuilder = { ServiceInteractor(context) }
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package tech.relaycorp.awaladroid.storage.persistence

import android.content.Context
import androidx.security.crypto.EncryptedFile
import androidx.security.crypto.MasterKey
import java.io.File
import java.io.IOException
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

internal class EncryptedDiskPersistence(
internal class DiskPersistence(
private val context: Context,
private val coroutineContext: CoroutineContext = Dispatchers.IO,
private val rootFolder: String = "awaladroid"
Expand All @@ -21,8 +19,8 @@ internal class EncryptedDiskPersistence(
withContext(coroutineContext) {
deleteIfExists(location)
try {
buildEncryptedFile(location)
.openFileOutput()
buildFile(location)
.outputStream()
.use { it.write(data) }
} catch (exception: IOException) {
throw PersistenceException("Failed to write to file at $location", exception)
Expand All @@ -34,8 +32,8 @@ internal class EncryptedDiskPersistence(
@Throws(PersistenceException::class)
override suspend fun get(location: String): ByteArray? = withContext(coroutineContext) {
try {
buildEncryptedFile(location)
.openFileInput()
buildFile(location)
.inputStream()
.use { it.readBytes() }
} catch (exception: IOException) {
if (buildFile(location).exists()) {
Expand Down Expand Up @@ -82,22 +80,4 @@ internal class EncryptedDiskPersistence(
File(context.filesDir, "$rootFolder${File.separator}$location").also {
it.parentFile?.mkdirs()
}

private fun buildEncryptedFile(location: String) =
EncryptedFile.Builder(
context,
buildFile(location),
masterKey,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
).build()

private val masterKey by lazy {
MasterKey.Builder(context, MASTER_KEY_ALIAS)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build()
}

companion object {
private const val MASTER_KEY_ALIAS = "_relaydroid_master_key_"
}
}