Fix opening database with bad attachment #691

This commit is contained in:
J-Jamet
2020-09-17 15:59:48 +02:00
parent e4445949c8
commit d9da1ef085
2 changed files with 22 additions and 10 deletions

View File

@@ -30,9 +30,9 @@ import com.kunzisoft.keepass.crypto.keyDerivation.KdfEngine
import com.kunzisoft.keepass.crypto.keyDerivation.KdfFactory import com.kunzisoft.keepass.crypto.keyDerivation.KdfFactory
import com.kunzisoft.keepass.crypto.keyDerivation.KdfParameters import com.kunzisoft.keepass.crypto.keyDerivation.KdfParameters
import com.kunzisoft.keepass.database.action.node.NodeHandler import com.kunzisoft.keepass.database.action.node.NodeHandler
import com.kunzisoft.keepass.database.element.Attachment
import com.kunzisoft.keepass.database.element.DateInstant import com.kunzisoft.keepass.database.element.DateInstant
import com.kunzisoft.keepass.database.element.DeletedObject import com.kunzisoft.keepass.database.element.DeletedObject
import com.kunzisoft.keepass.database.element.Attachment
import com.kunzisoft.keepass.database.element.database.DatabaseKDB.Companion.BACKUP_FOLDER_TITLE import com.kunzisoft.keepass.database.element.database.DatabaseKDB.Companion.BACKUP_FOLDER_TITLE
import com.kunzisoft.keepass.database.element.entry.EntryKDBX import com.kunzisoft.keepass.database.element.entry.EntryKDBX
import com.kunzisoft.keepass.database.element.group.GroupKDBX import com.kunzisoft.keepass.database.element.group.GroupKDBX
@@ -42,7 +42,6 @@ import com.kunzisoft.keepass.database.element.node.NodeVersioned
import com.kunzisoft.keepass.database.element.security.EncryptionAlgorithm import com.kunzisoft.keepass.database.element.security.EncryptionAlgorithm
import com.kunzisoft.keepass.database.element.security.MemoryProtectionConfig import com.kunzisoft.keepass.database.element.security.MemoryProtectionConfig
import com.kunzisoft.keepass.database.exception.UnknownKDF import com.kunzisoft.keepass.database.exception.UnknownKDF
import com.kunzisoft.keepass.database.file.DatabaseHeaderKDBX
import com.kunzisoft.keepass.database.file.DatabaseHeaderKDBX.Companion.FILE_VERSION_32_3 import com.kunzisoft.keepass.database.file.DatabaseHeaderKDBX.Companion.FILE_VERSION_32_3
import com.kunzisoft.keepass.database.file.DatabaseHeaderKDBX.Companion.FILE_VERSION_32_4 import com.kunzisoft.keepass.database.file.DatabaseHeaderKDBX.Companion.FILE_VERSION_32_4
import com.kunzisoft.keepass.utils.UnsignedInt import com.kunzisoft.keepass.utils.UnsignedInt
@@ -633,7 +632,7 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
private const val KeyElementName = "Key" private const val KeyElementName = "Key"
private const val KeyDataElementName = "Data" private const val KeyDataElementName = "Data"
const val BASE_64_FLAG = Base64.NO_WRAP const val BASE_64_FLAG = Base64.NO_PADDING or Base64.NO_WRAP
const val BUFFER_SIZE_BYTES = 3 * 128 const val BUFFER_SIZE_BYTES = 3 * 128
} }

View File

@@ -20,6 +20,7 @@
package com.kunzisoft.keepass.database.file.input package com.kunzisoft.keepass.database.file.input
import android.util.Base64 import android.util.Base64
import android.util.Log
import com.kunzisoft.keepass.R import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.crypto.CipherFactory import com.kunzisoft.keepass.crypto.CipherFactory
import com.kunzisoft.keepass.crypto.StreamCipherFactory import com.kunzisoft.keepass.crypto.StreamCipherFactory
@@ -742,8 +743,7 @@ class DatabaseInputKDBX(cacheDirectory: File,
if (entryInHistory) { if (entryInHistory) {
ctxEntry = ctxHistoryBase ctxEntry = ctxHistoryBase
return KdbContext.EntryHistory return KdbContext.EntryHistory
} } else if (ctxEntry != null) {
else if (ctxEntry != null) {
// Add entry to the index only when close the XML element // Add entry to the index only when close the XML element
mDatabase.addEntryIndex(ctxEntry!!) mDatabase.addEntryIndex(ctxEntry!!)
} }
@@ -879,9 +879,14 @@ class DatabaseInputKDBX(cacheDirectory: File,
if (encoded.isEmpty()) { if (encoded.isEmpty()) {
return DatabaseVersioned.UUID_ZERO return DatabaseVersioned.UUID_ZERO
} }
val buf = Base64.decode(encoded, BASE_64_FLAG)
return bytes16ToUuid(buf) return try {
val buf = Base64.decode(encoded, BASE_64_FLAG)
bytes16ToUuid(buf)
} catch (e: Exception) {
Log.e(TAG, "Unable to read base 64 UUID, create a random one", e)
UUID.randomUUID()
}
} }
@Throws(IOException::class, XmlPullParserException::class) @Throws(IOException::class, XmlPullParserException::class)
@@ -981,12 +986,18 @@ class DatabaseInputKDBX(cacheDirectory: File,
val base64 = readString(xpp) val base64 = readString(xpp)
if (base64.isEmpty()) if (base64.isEmpty())
return null return null
val data = Base64.decode(base64, BASE_64_FLAG)
// Build the new binary and compress // Build the new binary and compress
val binaryAttachment = mDatabase.buildNewBinary(cacheDirectory, protected, compressed, binaryId) val binaryAttachment = mDatabase.buildNewBinary(cacheDirectory, protected, compressed, binaryId)
binaryAttachment.getOutputDataStream().use { outputStream -> try {
outputStream.write(data) binaryAttachment.getOutputDataStream().use { outputStream ->
outputStream.write(Base64.decode(base64, BASE_64_FLAG))
}
} catch (e: Exception) {
Log.e(TAG, "Unable to read base 64 attachment", e)
binaryAttachment.getOutputDataStream().use { outputStream ->
outputStream.write(base64.toByteArray())
}
} }
return binaryAttachment return binaryAttachment
} }
@@ -1045,6 +1056,8 @@ class DatabaseInputKDBX(cacheDirectory: File,
companion object { companion object {
private val TAG = DatabaseInputKDBX::class.java.name
private val DEFAULT_HISTORY_DAYS = UnsignedInt(365) private val DEFAULT_HISTORY_DAYS = UnsignedInt(365)
@Throws(XmlPullParserException::class) @Throws(XmlPullParserException::class)