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: Wrap internal Android encryption exception #343

Merged
merged 3 commits into from
Sep 27, 2023
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 @@ -6,22 +6,38 @@ import androidx.security.crypto.MasterKey
import tech.relaycorp.awala.keystores.file.FileKeystoreRoot
import tech.relaycorp.awala.keystores.file.FilePrivateKeyStore
import java.io.File
import javax.crypto.AEADBadTagException

internal class AndroidPrivateKeyStore(
root: FileKeystoreRoot,
private val context: Context,
) : FilePrivateKeyStore(root) {
override fun makeEncryptedInputStream(file: File) = buildEncryptedFile(file).openFileInput()

override fun makeEncryptedOutputStream(file: File) = buildEncryptedFile(file).openFileOutput()

private fun buildEncryptedFile(file: File) =
private val encryptedFileBuilder: (File, MasterKey) -> EncryptedFile = { file, masterKey ->
EncryptedFile.Builder(
context,
file,
masterKey,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB,
).build()
},
) : FilePrivateKeyStore(root) {

@Throws(EncryptionInitializationException::class)
override fun makeEncryptedInputStream(file: File) = buildEncryptedFile(file).openFileInput()

@Throws(EncryptionInitializationException::class)
override fun makeEncryptedOutputStream(file: File) = buildEncryptedFile(file).openFileOutput()

@Throws(EncryptionInitializationException::class)
private fun buildEncryptedFile(file: File): EncryptedFile =
try {
encryptedFileBuilder(file, masterKey)
} catch (exception: AEADBadTagException) {
// Known issue: https://issuetracker.google.com/issues/164901843
throw EncryptionInitializationException(
"Could not build encrypted file due to internal issue",
exception,
)
}

private val masterKey by lazy {
MasterKey.Builder(context, MASTER_KEY_ALIAS)
Expand All @@ -33,3 +49,6 @@ internal class AndroidPrivateKeyStore(
private const val MASTER_KEY_ALIAS = "_awaladroid_master_key_"
}
}

public class EncryptionInitializationException(message: String, cause: Throwable) :
AwaladroidException(message, cause)
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import tech.relaycorp.awaladroid.test.FakeAndroidKeyStore
import tech.relaycorp.relaynet.testing.pki.KeyPairSet
import tech.relaycorp.relaynet.testing.pki.PDACertPath
import java.io.File
import javax.crypto.AEADBadTagException

@RunWith(RobolectricTestRunner::class)
public class AndroidPrivateKeyStoreTest {
Expand All @@ -33,4 +34,14 @@ public class AndroidPrivateKeyStoreTest {
val retrievedId = store.retrieveIdentityKey(certificate.subjectId)
assertEquals(id, retrievedId)
}

@Test(expected = EncryptionInitializationException::class)
public fun failWithAEADBadTagException(): Unit = runTest {
val androidContext = RuntimeEnvironment.getApplication()
val root = FileKeystoreRoot(File(androidContext.filesDir, "tmp-keystore"))
val store = AndroidPrivateKeyStore(root, androidContext) { _, _ ->
throw AEADBadTagException("")
}
store.saveIdentityKey(KeyPairSet.PRIVATE_ENDPOINT.private)
}
}