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" 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}" is ImportResult.Error.Type.Unknown -> "${type.cause::class.java.simpleName}: ${type.cause.message}"
ImportResult.Error.Type.UnableToOpenFile -> "Unable to open file" 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) Text(text = "Import failed\n$message", textAlign = TextAlign.Center)

View File

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

View File

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