Skip to content

Commit

Permalink
Make EncryptionInitializationException public. Fix test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
sdsantos committed Sep 27, 2023
1 parent a788d74 commit 1cd6b3b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ import javax.crypto.AEADBadTagException
internal class AndroidPrivateKeyStore(
root: FileKeystoreRoot,
private val context: Context,
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)
Expand All @@ -20,17 +28,15 @@ internal class AndroidPrivateKeyStore(
override fun makeEncryptedOutputStream(file: File) = buildEncryptedFile(file).openFileOutput()

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

private val masterKey by lazy {
Expand All @@ -39,9 +45,10 @@ internal class AndroidPrivateKeyStore(
.build()
}

class EncryptionInitializationException(cause: Throwable) : Exception(cause)

companion object {
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)
}
}

0 comments on commit 1cd6b3b

Please sign in to comment.