diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/database/DatabaseKDBX.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/database/DatabaseKDBX.kt index ba71b716a..574ecce6a 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/database/DatabaseKDBX.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/database/DatabaseKDBX.kt @@ -30,9 +30,9 @@ import com.kunzisoft.keepass.crypto.keyDerivation.KdfEngine import com.kunzisoft.keepass.crypto.keyDerivation.KdfFactory import com.kunzisoft.keepass.crypto.keyDerivation.KdfParameters 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.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.entry.EntryKDBX 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.MemoryProtectionConfig 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_4 import com.kunzisoft.keepass.utils.UnsignedInt @@ -633,7 +632,7 @@ class DatabaseKDBX : DatabaseVersioned { private const val KeyElementName = "Key" 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 } diff --git a/app/src/main/java/com/kunzisoft/keepass/database/file/input/DatabaseInputKDBX.kt b/app/src/main/java/com/kunzisoft/keepass/database/file/input/DatabaseInputKDBX.kt index aecb72d4b..74654e0ec 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/file/input/DatabaseInputKDBX.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/file/input/DatabaseInputKDBX.kt @@ -20,6 +20,7 @@ package com.kunzisoft.keepass.database.file.input import android.util.Base64 +import android.util.Log import com.kunzisoft.keepass.R import com.kunzisoft.keepass.crypto.CipherFactory import com.kunzisoft.keepass.crypto.StreamCipherFactory @@ -742,8 +743,7 @@ class DatabaseInputKDBX(cacheDirectory: File, if (entryInHistory) { ctxEntry = ctxHistoryBase return KdbContext.EntryHistory - } - else if (ctxEntry != null) { + } else if (ctxEntry != null) { // Add entry to the index only when close the XML element mDatabase.addEntryIndex(ctxEntry!!) } @@ -879,9 +879,14 @@ class DatabaseInputKDBX(cacheDirectory: File, if (encoded.isEmpty()) { 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) @@ -981,12 +986,18 @@ class DatabaseInputKDBX(cacheDirectory: File, val base64 = readString(xpp) if (base64.isEmpty()) return null - val data = Base64.decode(base64, BASE_64_FLAG) // Build the new binary and compress val binaryAttachment = mDatabase.buildNewBinary(cacheDirectory, protected, compressed, binaryId) - binaryAttachment.getOutputDataStream().use { outputStream -> - outputStream.write(data) + try { + 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 } @@ -1045,6 +1056,8 @@ class DatabaseInputKDBX(cacheDirectory: File, companion object { + private val TAG = DatabaseInputKDBX::class.java.name + private val DEFAULT_HISTORY_DAYS = UnsignedInt(365) @Throws(XmlPullParserException::class)