adding dedicated error for missing room keys cipher

This commit is contained in:
Adam Brown 2022-10-09 15:09:56 +01:00
parent ef25d01c6d
commit 3a8c4c11ce
3 changed files with 18 additions and 8 deletions

View File

@ -156,6 +156,7 @@ internal fun SettingsScreen(viewModel: SettingsViewModel, onSignOut: () -> Unit,
ImportResult.Error.Type.UnexpectedDecryptionOutput -> "Unable to decrypt file, double check your passphrase"
is ImportResult.Error.Type.Unknown -> "${type.cause::class.java.simpleName}: ${type.cause.message}"
ImportResult.Error.Type.UnableToOpenFile -> "Unable to open file"
ImportResult.Error.Type.InvalidFile -> "Unable to process file"
}
Text(text = "Import failed\n$message", textAlign = TextAlign.Center)

View File

@ -184,6 +184,7 @@ sealed interface ImportResult {
object NoKeysFound : Type
object UnexpectedDecryptionOutput : Type
object UnableToOpenFile : Type
object InvalidFile : Type
}
}

View File

@ -86,21 +86,27 @@ class RoomKeyImporter(
val line = it.joinToString(separator = "").replace("\n", "")
val toByteArray = base64.decode(line)
if (index == 0) {
decryptCipher.initialize(toByteArray, password)
toByteArray
.copyOfRange(37, toByteArray.size)
.decrypt(decryptCipher)
.also {
if (!it.startsWith("[{")) {
throw ImportException(ImportResult.Error.Type.UnexpectedDecryptionOutput)
}
toByteArray.ensureHasCipherPayloadOrThrow()
val initializer = toByteArray.copyOfRange(0, 37)
decryptCipher.initialize(initializer, password)
val content = toByteArray.copyOfRange(37, toByteArray.size)
content.decrypt(decryptCipher).also {
if (!it.startsWith("[{")) {
throw ImportException(ImportResult.Error.Type.UnexpectedDecryptionOutput)
}
}
} else {
toByteArray.decrypt(decryptCipher)
}
}
}
private fun ByteArray.ensureHasCipherPayloadOrThrow() {
if (this.size < 37) {
throw ImportException(ImportResult.Error.Type.InvalidFile)
}
}
private fun Cipher.initialize(payload: ByteArray, passphrase: String) {
val salt = payload.copyOfRange(1, 1 + 16)
val iv = payload.copyOfRange(17, 17 + 16)
@ -176,6 +182,7 @@ private class JsonAccumulator {
jsonSegment = withLatest
null
}
else -> {
val string = withLatest.substring(objectRange)
importJson.decodeFromString(ElementMegolmExportObject.serializer(), string).also {
@ -200,6 +207,7 @@ private class JsonAccumulator {
}
opens++
}
c == '}' -> {
opens--
if (opens == 0) {