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
Changes from 1 commit
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,29 +6,41 @@ 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) {

@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.Builder(
context,
file,
masterKey,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB,
).build()
try {
EncryptedFile.Builder(
context,
file,
masterKey,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB,
).build()
} catch (e: AEADBadTagException) {
// Known issue: https://issuetracker.google.com/issues/164901843
throw EncryptionInitializationException(e)
}

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

class EncryptionInitializationException(cause: Throwable) : Exception(cause)
sdsantos marked this conversation as resolved.
Show resolved Hide resolved

companion object {
private const val MASTER_KEY_ALIAS = "_awaladroid_master_key_"
}
Expand Down