From eb0e496cfd7180bf732e7e486179cbb12db28106 Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Sat, 30 Nov 2019 17:02:09 +0100 Subject: [PATCH] Try to decrypt DatabaseV1 by InputStream --- .../tests/DatabaseInputOutputUtilsTest.kt | 14 +- .../com/kunzisoft/keepass/tests/PwDateTest.kt | 2 +- .../keepass/database/element/Database.kt | 111 +++--- .../database/file/DatabaseHeaderKDB.kt | 35 +- .../database/file/DatabaseHeaderKDBX.kt | 6 +- .../database/file/input/DatabaseInputKDB.kt | 342 ++++++++++-------- .../database/file/input/DatabaseInputKDBX.kt | 6 +- .../database/file/output/EntryOutputKDB.kt | 8 +- .../database/file/output/GroupOutputKDB.kt | 8 +- .../keepass/stream/HashedBlockInputStream.kt | 4 +- .../keepass/stream/HmacBlockInputStream.kt | 6 +- .../keepass/stream/HmacBlockOutputStream.java | 4 +- .../stream/LittleEndianDataInputStream.kt | 8 +- .../{ReadBytes.kt => StreamBytesUtils.kt} | 92 +++-- .../keepass/utils/DatabaseInputOutputUtils.kt | 22 +- .../keepass/utils/VariantDictionary.java | 13 +- 16 files changed, 377 insertions(+), 304 deletions(-) rename app/src/main/java/com/kunzisoft/keepass/stream/{ReadBytes.kt => StreamBytesUtils.kt} (69%) diff --git a/app/src/androidTest/java/com/kunzisoft/keepass/tests/DatabaseInputOutputUtilsTest.kt b/app/src/androidTest/java/com/kunzisoft/keepass/tests/DatabaseInputOutputUtilsTest.kt index a00b3933c..c3f223b9a 100644 --- a/app/src/androidTest/java/com/kunzisoft/keepass/tests/DatabaseInputOutputUtilsTest.kt +++ b/app/src/androidTest/java/com/kunzisoft/keepass/tests/DatabaseInputOutputUtilsTest.kt @@ -58,7 +58,7 @@ class DatabaseInputOutputUtilsTest : TestCase() { setArray(orig, value, 0, 8) - val one = readLong(orig, 0) + val one = bytes64ToLong(orig, 0) writeLong(one, dest, 0) assertArrayEquals(orig, dest) @@ -87,7 +87,7 @@ class DatabaseInputOutputUtilsTest : TestCase() { setArray(orig, value, 0, 4) - val one = readInt(orig, 0) + val one = bytes4ToInt(orig, 0) writeInt(one, dest, 0) @@ -107,7 +107,7 @@ class DatabaseInputOutputUtilsTest : TestCase() { orig[0] = 0 orig[1] = 1 - val one = readUShort(orig, 0) + val one = bytes2ToUShort(orig, 0) val dest = writeUShortBuf(one) assertArrayEquals(orig, dest) @@ -128,7 +128,7 @@ class DatabaseInputOutputUtilsTest : TestCase() { setArray(orig, value, 0, 2) - val one = readUShort(orig, 0) + val one = bytes2ToUShort(orig, 0) writeUShort(one, dest, 0) assertArrayEquals(orig, dest) @@ -167,8 +167,8 @@ class DatabaseInputOutputUtilsTest : TestCase() { expected.set(2008, 1, 2, 3, 4, 5) val actual = Calendar.getInstance() - DatabaseInputOutputUtils.writeCDate(expected.time, cal)?.let { buf -> - actual.time = DatabaseInputOutputUtils.readCDate(buf, 0, cal).date + DatabaseInputOutputUtils.dateToBytes(expected.time, cal)?.let { buf -> + actual.time = DatabaseInputOutputUtils.bytes5ToDate(buf, 0, cal).date } assertEquals("Year mismatch: ", 2008, actual.get(Calendar.YEAR)) @@ -186,7 +186,7 @@ class DatabaseInputOutputUtilsTest : TestCase() { val uuid = DatabaseInputOutputUtils.bytesToUuid(bUUID) val eUUID = DatabaseInputOutputUtils.uuidToBytes(uuid) - val lUUID = readUuid(bUUID, 0) + val lUUID = bytes16ToUuid(bUUID, 0) val leUUID = DatabaseInputOutputUtils.uuidToBytes(lUUID) assertArrayEquals("UUID match failed", bUUID, eUUID) diff --git a/app/src/androidTest/java/com/kunzisoft/keepass/tests/PwDateTest.kt b/app/src/androidTest/java/com/kunzisoft/keepass/tests/PwDateTest.kt index eadfbc4eb..4216c1bb3 100644 --- a/app/src/androidTest/java/com/kunzisoft/keepass/tests/PwDateTest.kt +++ b/app/src/androidTest/java/com/kunzisoft/keepass/tests/PwDateTest.kt @@ -30,7 +30,7 @@ class PwDateTest : TestCase() { fun testDate() { val jDate = DateInstant(System.currentTimeMillis()) val intermediate = DateInstant(jDate) - val cDate = DatabaseInputOutputUtils.readCDate(DatabaseInputOutputUtils.writeCDate(intermediate.date)!!, 0) + val cDate = DatabaseInputOutputUtils.bytes5ToDate(DatabaseInputOutputUtils.dateToBytes(intermediate.date)!!, 0) Assert.assertTrue("jDate and intermediate not equal", jDate == intermediate) Assert.assertTrue("jDate $jDate and cDate $cDate not equal", cDate == jDate) diff --git a/app/src/main/java/com/kunzisoft/keepass/database/element/Database.kt b/app/src/main/java/com/kunzisoft/keepass/database/element/Database.kt index ef78a0f64..2acdbaf48 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/element/Database.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/element/Database.kt @@ -48,7 +48,7 @@ import com.kunzisoft.keepass.database.file.output.DatabaseOutputKDB import com.kunzisoft.keepass.database.file.output.DatabaseOutputKDBX import com.kunzisoft.keepass.database.search.SearchHelper import com.kunzisoft.keepass.icons.IconDrawableFactory -import com.kunzisoft.keepass.stream.readInt +import com.kunzisoft.keepass.stream.readBytes4ToInt import com.kunzisoft.keepass.tasks.ProgressTaskUpdater import com.kunzisoft.keepass.utils.SingletonHolder import com.kunzisoft.keepass.utils.UriUtil @@ -327,66 +327,65 @@ class Database { isReadOnly = !file.canWrite() } - // Pass Uris as InputStreams - val inputStream: InputStream? + // Pass KeyFile Uri as InputStreams + var databaseInputStream: InputStream? = null + var keyFileInputStream: InputStream? = null try { - inputStream = UriUtil.getUriInputStream(contentResolver, uri) + // Get keyFile inputStream + keyfile?.let { + keyFileInputStream = UriUtil.getUriInputStream(contentResolver, keyfile) + } + + // Load Data, pass Uris as InputStreams + databaseInputStream = BufferedInputStream(UriUtil.getUriInputStream(contentResolver, uri)) + if (!databaseInputStream.markSupported()) { + throw IOException("Input stream does not support mark.") + } + + // We'll end up reading 8 bytes to identify the header. Might as well use two extra. + databaseInputStream.mark(10) + + // Get the file directory to save the attachments + val sig1 = databaseInputStream.readBytes4ToInt() + val sig2 = databaseInputStream.readBytes4ToInt() + + // Return to the start + databaseInputStream.reset() + + when { + // Header of database KDB + DatabaseHeaderKDB.matchesHeader(sig1, sig2) -> setDatabaseKDB(DatabaseInputKDB() + .openDatabase(databaseInputStream, + password, + keyFileInputStream, + progressTaskUpdater)) + + // Header of database KDBX + DatabaseHeaderKDBX.matchesHeader(sig1, sig2) -> setDatabaseKDBX(DatabaseInputKDBX( + cacheDirectory, + fixDuplicateUUID) + .openDatabase(databaseInputStream, + password, + keyFileInputStream, + progressTaskUpdater)) + + // Header not recognized + else -> throw SignatureDatabaseException() + } + + this.mSearchHelper = SearchHelper(omitBackup) + loaded = true + + } catch (e: LoadDatabaseException) { + Log.e("KPD", "Database::loadData", e) + throw e } catch (e: Exception) { Log.e("KPD", "Database::loadData", e) throw FileNotFoundDatabaseException() + } finally { + keyFileInputStream?.close() + databaseInputStream?.close() } - - // Pass KeyFile Uri as InputStreams - var keyFileInputStream: InputStream? = null - keyfile?.let { - try { - keyFileInputStream = UriUtil.getUriInputStream(contentResolver, keyfile) - } catch (e: Exception) { - Log.e("KPD", "Database::loadData", e) - throw FileNotFoundDatabaseException() - } - } - - // Load Data - - val bufferedInputStream = BufferedInputStream(inputStream) - if (!bufferedInputStream.markSupported()) { - throw IOException("Input stream does not support mark.") - } - - // We'll end up reading 8 bytes to identify the header. Might as well use two extra. - bufferedInputStream.mark(10) - - // Get the file directory to save the attachments - val sig1 = readInt(bufferedInputStream) - val sig2 = readInt(bufferedInputStream) - - // Return to the start - bufferedInputStream.reset() - - when { - // Header of database KDB - DatabaseHeaderKDB.matchesHeader(sig1, sig2) -> setDatabaseKDB(DatabaseInputKDB() - .openDatabase(bufferedInputStream, - password, - keyFileInputStream, - progressTaskUpdater)) - - // Header of database KDBX - DatabaseHeaderKDBX.matchesHeader(sig1, sig2) -> setDatabaseKDBX(DatabaseInputKDBX( - cacheDirectory, - fixDuplicateUUID) - .openDatabase(bufferedInputStream, - password, - keyFileInputStream, - progressTaskUpdater)) - - // Header not recognized - else -> throw SignatureDatabaseException() - } - - this.mSearchHelper = SearchHelper(omitBackup) - loaded = true } fun isGroupSearchable(group: Group, isOmitBackup: Boolean): Boolean { diff --git a/app/src/main/java/com/kunzisoft/keepass/database/file/DatabaseHeaderKDB.kt b/app/src/main/java/com/kunzisoft/keepass/database/file/DatabaseHeaderKDB.kt index 7d2e81ceb..7863c3e0d 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/file/DatabaseHeaderKDB.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/file/DatabaseHeaderKDB.kt @@ -21,8 +21,11 @@ package com.kunzisoft.keepass.database.file -import com.kunzisoft.keepass.stream.readInt +import com.kunzisoft.keepass.stream.bytes4ToInt +import com.kunzisoft.keepass.stream.readBytesLength +import com.kunzisoft.keepass.stream.readBytes4ToInt import java.io.IOException +import java.io.InputStream class DatabaseHeaderKDB : DatabaseHeader() { @@ -51,26 +54,20 @@ class DatabaseHeaderKDB : DatabaseHeader() { /** * Parse given buf, as read from file. - * @param buf - * @throws IOException */ @Throws(IOException::class) - fun loadFromFile(buf: ByteArray, offset: Int) { - signature1 = readInt(buf, offset) - signature2 = readInt(buf, offset + 4) - flags = readInt(buf, offset + 8) - version = readInt(buf, offset + 12) - - System.arraycopy(buf, offset + 16, masterSeed, 0, 16) - System.arraycopy(buf, offset + 32, encryptionIV, 0, 16) - - numGroups = readInt(buf, offset + 48) - numEntries = readInt(buf, offset + 52) - - System.arraycopy(buf, offset + 56, contentsHash, 0, 32) - - System.arraycopy(buf, offset + 88, transformSeed, 0, 32) - numKeyEncRounds = readInt(buf, offset + 120) + fun loadFromFile(inputStream: InputStream) { + signature1 = inputStream.readBytes4ToInt() // 4 bytes + signature2 = inputStream.readBytes4ToInt() // 4 bytes + flags = inputStream.readBytes4ToInt() // 4 bytes + version = inputStream.readBytes4ToInt() // 4 bytes + masterSeed = inputStream.readBytesLength(16) // 16 bytes + encryptionIV = inputStream.readBytesLength(16) // 16 bytes + numGroups = inputStream.readBytes4ToInt() // 4 bytes + numEntries = inputStream.readBytes4ToInt() // 4 bytes + contentsHash = inputStream.readBytesLength(32) // 32 bytes + transformSeed = inputStream.readBytesLength(32) // 32 bytes + numKeyEncRounds = inputStream.readBytes4ToInt() if (numKeyEncRounds < 0) { // TODO: Really treat this like an unsigned integer throw IOException("Does not support more than " + Integer.MAX_VALUE + " rounds.") diff --git a/app/src/main/java/com/kunzisoft/keepass/database/file/DatabaseHeaderKDBX.kt b/app/src/main/java/com/kunzisoft/keepass/database/file/DatabaseHeaderKDBX.kt index 09261465b..1880d604b 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/file/DatabaseHeaderKDBX.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/file/DatabaseHeaderKDBX.kt @@ -246,7 +246,7 @@ class DatabaseHeaderKDBX(private val databaseV4: DatabaseKDBX) : DatabaseHeader( private fun setTransformRound(roundsByte: ByteArray?) { assignAesKdfEngineIfNotExists() - val rounds = readLong(roundsByte!!, 0) + val rounds = bytes64ToLong(roundsByte!!, 0) databaseV4.kdfParameters?.setUInt64(AesKdf.PARAM_ROUNDS, rounds) databaseV4.numberKeyEncryptionRounds = rounds } @@ -257,7 +257,7 @@ class DatabaseHeaderKDBX(private val databaseV4: DatabaseKDBX) : DatabaseHeader( throw IOException("Invalid compression flags.") } - val flag = readInt(pbFlags, 0) + val flag = bytes4ToInt(pbFlags, 0) if (flag < 0 || flag >= CompressionAlgorithm.values().size) { throw IOException("Unrecognized compression flag.") } @@ -273,7 +273,7 @@ class DatabaseHeaderKDBX(private val databaseV4: DatabaseKDBX) : DatabaseHeader( throw IOException("Invalid stream id.") } - val id = readInt(streamID, 0) + val id = bytes4ToInt(streamID, 0) if (id < 0 || id >= CrsAlgorithm.values().size) { throw IOException("Invalid stream id.") } diff --git a/app/src/main/java/com/kunzisoft/keepass/database/file/input/DatabaseInputKDB.kt b/app/src/main/java/com/kunzisoft/keepass/database/file/input/DatabaseInputKDB.kt index 8976ec8bf..5f0e505f5 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/file/input/DatabaseInputKDB.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/file/input/DatabaseInputKDB.kt @@ -45,7 +45,6 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA package com.kunzisoft.keepass.database.file.input -import android.util.Log import com.kunzisoft.keepass.R import com.kunzisoft.keepass.crypto.CipherFactory import com.kunzisoft.keepass.database.element.database.DatabaseKDB @@ -57,21 +56,18 @@ import com.kunzisoft.keepass.database.element.security.EncryptionAlgorithm import com.kunzisoft.keepass.database.exception.* import com.kunzisoft.keepass.database.file.DatabaseHeader import com.kunzisoft.keepass.database.file.DatabaseHeaderKDB -import com.kunzisoft.keepass.stream.NullOutputStream -import com.kunzisoft.keepass.stream.readInt -import com.kunzisoft.keepass.stream.readUShort -import com.kunzisoft.keepass.stream.readUuid +import com.kunzisoft.keepass.stream.* import com.kunzisoft.keepass.tasks.ProgressTaskUpdater -import com.kunzisoft.keepass.utils.DatabaseInputOutputUtils -import java.io.IOException -import java.io.InputStream -import java.io.UnsupportedEncodingException +import java.io.* import java.security.* import java.util.* -import javax.crypto.* +import javax.crypto.Cipher +import javax.crypto.CipherInputStream +import javax.crypto.NoSuchPaddingException import javax.crypto.spec.IvParameterSpec import javax.crypto.spec.SecretKeySpec + /** * Load a KDB database file. */ @@ -88,17 +84,19 @@ class DatabaseInputKDB : DatabaseInput() { try { // Load entire file, most of it's encrypted. val fileSize = databaseInputStream.available() - val filebuf = ByteArray(fileSize + 16) // Pad with a blocksize (Twofish uses 128 bits), since Android 4.3 tries to write more to the buffer - databaseInputStream.read(filebuf, 0, fileSize) // TODO remove - databaseInputStream.close() // Parse header (unencrypted) if (fileSize < DatabaseHeaderKDB.BUF_SIZE) throw IOException("File too short for header") val header = DatabaseHeaderKDB() - header.loadFromFile(filebuf, 0) + header.loadFromFile(databaseInputStream) - if (header.signature1 != DatabaseHeader.PWM_DBSIG_1 || header.signature2 != DatabaseHeaderKDB.DBSIG_2) { + val contentSize = databaseInputStream.available() + if (fileSize != (contentSize + DatabaseHeaderKDB.BUF_SIZE)) + throw IOException("Header corrupted") + + if (header.signature1 != DatabaseHeader.PWM_DBSIG_1 + || header.signature2 != DatabaseHeaderKDB.DBSIG_2) { throw SignatureDatabaseException() } @@ -112,28 +110,35 @@ class DatabaseInputKDB : DatabaseInput() { // Select algorithm when { - header.flags and DatabaseHeaderKDB.FLAG_RIJNDAEL != 0 -> mDatabaseToOpen.encryptionAlgorithm = EncryptionAlgorithm.AESRijndael - header.flags and DatabaseHeaderKDB.FLAG_TWOFISH != 0 -> mDatabaseToOpen.encryptionAlgorithm = EncryptionAlgorithm.Twofish + header.flags and DatabaseHeaderKDB.FLAG_RIJNDAEL != 0 -> { + mDatabaseToOpen.encryptionAlgorithm = EncryptionAlgorithm.AESRijndael + } + header.flags and DatabaseHeaderKDB.FLAG_TWOFISH != 0 -> { + mDatabaseToOpen.encryptionAlgorithm = EncryptionAlgorithm.Twofish + } else -> throw InvalidAlgorithmDatabaseException() } mDatabaseToOpen.numberKeyEncryptionRounds = header.numKeyEncRounds.toLong() // Generate transformedMasterKey from masterKey - mDatabaseToOpen.makeFinalKey(header.masterSeed, header.transformSeed, mDatabaseToOpen.numberKeyEncryptionRounds) + mDatabaseToOpen.makeFinalKey( + header.masterSeed, + header.transformSeed, + mDatabaseToOpen.numberKeyEncryptionRounds) progressTaskUpdater?.updateMessage(R.string.decrypting_db) // Initialize Rijndael algorithm - val cipher: Cipher - try { - if (mDatabaseToOpen.encryptionAlgorithm === EncryptionAlgorithm.AESRijndael) { - cipher = CipherFactory.getInstance("AES/CBC/PKCS5Padding") - } else if (mDatabaseToOpen.encryptionAlgorithm === EncryptionAlgorithm.Twofish) { - cipher = CipherFactory.getInstance("Twofish/CBC/PKCS7PADDING") - } else { - throw IOException("Encryption algorithm is not supported") + val cipher: Cipher = try { + when { + mDatabaseToOpen.encryptionAlgorithm === EncryptionAlgorithm.AESRijndael -> { + CipherFactory.getInstance("AES/CBC/PKCS5Padding") + } + mDatabaseToOpen.encryptionAlgorithm === EncryptionAlgorithm.Twofish -> { + CipherFactory.getInstance("Twofish/CBC/PKCS7PADDING") + } + else -> throw IOException("Encryption algorithm is not supported") } - } catch (e1: NoSuchAlgorithmException) { throw IOException("No such algorithm") } catch (e1: NoSuchPaddingException) { @@ -141,98 +146,143 @@ class DatabaseInputKDB : DatabaseInput() { } try { - cipher.init(Cipher.DECRYPT_MODE, SecretKeySpec(mDatabaseToOpen.finalKey, "AES"), IvParameterSpec(header.encryptionIV)) + cipher.init(Cipher.DECRYPT_MODE, + SecretKeySpec(mDatabaseToOpen.finalKey, "AES"), + IvParameterSpec(header.encryptionIV)) } catch (e1: InvalidKeyException) { throw IOException("Invalid key") } catch (e1: InvalidAlgorithmParameterException) { throw IOException("Invalid algorithm parameter.") } - // Decrypt! The first bytes aren't encrypted (that's the header) - val encryptedPartSize: Int + val messageDigest: MessageDigest try { - encryptedPartSize = cipher.doFinal(filebuf, DatabaseHeaderKDB.BUF_SIZE, fileSize - DatabaseHeaderKDB.BUF_SIZE, filebuf, DatabaseHeaderKDB.BUF_SIZE) - } catch (e1: ShortBufferException) { - throw IOException("Buffer too short") - } catch (e1: IllegalBlockSizeException) { - throw IOException("Invalid block size") - } catch (e1: BadPaddingException) { - throw InvalidCredentialsDatabaseException() - } - - val md: MessageDigest - try { - md = MessageDigest.getInstance("SHA-256") + messageDigest = MessageDigest.getInstance("SHA-256") } catch (e: NoSuchAlgorithmException) { throw IOException("No SHA-256 algorithm") } - val nos = NullOutputStream() - val dos = DigestOutputStream(nos, md) - dos.write(filebuf, DatabaseHeaderKDB.BUF_SIZE, encryptedPartSize) - dos.close() - val hash = md.digest() + // Decrypt content + BufferedInputStream( + DigestInputStream( + CipherInputStream(databaseInputStream, cipher), + messageDigest + ) + ).use { cipherInputStream -> + /* + TODO + // Add a mark to the content start + if (!cipherInputStream.markSupported()) { + throw IOException("Input stream does not support mark.") + } + cipherInputStream.mark(cipherInputStream.available() +1) + // Consume all data to get the digest + var numberRead = 0 + while (numberRead > -1) { + numberRead = cipherInputStream.read(ByteArray(1024)) + } - if (!Arrays.equals(hash, header.contentsHash)) { + // Check sum + if (!Arrays.equals(messageDigest.digest(), header.contentsHash)) { + throw InvalidCredentialsDatabaseException() + } + // Back to the content start + cipherInputStream.reset() + */ - Log.w(TAG, "Database file did not decrypt correctly. (checksum code is broken)") - throw InvalidCredentialsDatabaseException() - } + // New manual root because KDB contains multiple root groups (here available with getRootGroups()) + val newRoot = mDatabaseToOpen.createGroup() + newRoot.level = -1 + mDatabaseToOpen.rootGroup = newRoot - // New manual root because KDB contains multiple root groups (here available with getRootGroups()) - val newRoot = mDatabaseToOpen.createGroup() - newRoot.level = -1 - mDatabaseToOpen.rootGroup = newRoot + // Import all nodes + var newGroup: GroupKDB? = null + var newEntry: EntryKDB? = null + var currentGroupNumber = 0 + var currentEntryNumber = 0 + while (currentGroupNumber < header.numGroups + || currentEntryNumber < header.numEntries) { + val fieldType = cipherInputStream.readBytes2ToUShort() + val fieldSize = cipherInputStream.readBytes4ToUInt().toInt() - // Import all groups - var pos = DatabaseHeaderKDB.BUF_SIZE - var newGrp = mDatabaseToOpen.createGroup() - run { - var i = 0 - while (i < header.numGroups) { - val fieldType = readUShort(filebuf, pos) - pos += 2 - val fieldSize = readInt(filebuf, pos) - pos += 4 - - if (fieldType == 0xFFFF) { - // End-Group record. Save group and count it. - mDatabaseToOpen.addGroupIndex(newGrp) - newGrp = mDatabaseToOpen.createGroup() - i++ - } else { - readGroupField(mDatabaseToOpen, newGrp, fieldType, filebuf, pos) + when (fieldType) { + 0x0000 -> { + cipherInputStream.readBytesLength(fieldSize) + } + 0x0001 -> { + // Create new node depending on byte number + when (fieldSize) { + 4 -> { + newGroup = mDatabaseToOpen.createGroup() + readGroupField(mDatabaseToOpen, + newGroup, + fieldType, + fieldSize, + cipherInputStream) + } + 16 -> { + newEntry = mDatabaseToOpen.createEntry() + readEntryField(mDatabaseToOpen, + newEntry, + fieldType, + fieldSize, + cipherInputStream) + } + } + } + 0xFFFF -> { + // End record. Save node and count it. + newGroup?.let { group -> + mDatabaseToOpen.addGroupIndex(group) + currentGroupNumber++ + newGroup = null + } + newEntry?.let { entry -> + mDatabaseToOpen.addEntryIndex(entry) + currentEntryNumber++ + newEntry = null + } + cipherInputStream.readBytesLength(fieldSize) + } + else -> { + newGroup?.let { group -> + readGroupField(mDatabaseToOpen, + group, + fieldType, + fieldSize, + cipherInputStream) + } ?: + newEntry?.let { entry -> + readEntryField(mDatabaseToOpen, + entry, + fieldType, + fieldSize, + cipherInputStream) + } ?: + cipherInputStream.readBytesLength(fieldSize) + } } - pos += fieldSize } + cipherInputStream.close() + // Check sum + if (!Arrays.equals(messageDigest.digest(), header.contentsHash)) { + throw InvalidCredentialsDatabaseException() + } + + constructTreeFromIndex() } - // Import all entries - var newEnt = mDatabaseToOpen.createEntry() - var i = 0 - while (i < header.numEntries) { - val fieldType = readUShort(filebuf, pos) - val fieldSize = readInt(filebuf, pos + 2) - - if (fieldType == 0xFFFF) { - // End-Group record. Save group and count it. - mDatabaseToOpen.addEntryIndex(newEnt) - newEnt = mDatabaseToOpen.createEntry() - i++ - } else { - readEntryField(mDatabaseToOpen, newEnt, filebuf, pos) - } - pos += 2 + 4 + fieldSize - } - - constructTreeFromIndex() } catch (e: LoadDatabaseException) { + mDatabaseToOpen.clearCache() throw e } catch (e: IOException) { + mDatabaseToOpen.clearCache() throw IODatabaseException(e) } catch (e: OutOfMemoryError) { + mDatabaseToOpen.clearCache() throw NoMemoryDatabaseException(e) } catch (e: Exception) { + mDatabaseToOpen.clearCache() throw LoadDatabaseException(e) } @@ -281,68 +331,66 @@ class DatabaseInputKDB : DatabaseInput() { } /** - * Parse and save one record from binary file. - * @param buf - * @param offset - * @return If >0, - * @throws UnsupportedEncodingException + * Parse and save one record from binary file */ @Throws(UnsupportedEncodingException::class) - private fun readGroupField(db: DatabaseKDB, grp: GroupKDB, fieldType: Int, buf: ByteArray, offset: Int) { - when (fieldType) { - 0x0000 -> { + private fun readGroupField(database: DatabaseKDB, + group: GroupKDB, + fieldType: Int, + fieldSize: Int, + inputStream: InputStream) { + if (fieldSize > 0) { + when (fieldType) { + 0x0001 -> group.setGroupId(inputStream.readBytes4ToInt()) + 0x0002 -> group.title = inputStream.readBytesToString(fieldSize) + 0x0003 -> group.creationTime = inputStream.readBytes5ToDate() + 0x0004 -> group.lastModificationTime = inputStream.readBytes5ToDate() + 0x0005 -> group.lastAccessTime = inputStream.readBytes5ToDate() + 0x0006 -> group.expiryTime = inputStream.readBytes5ToDate() + 0x0007 -> group.icon = database.iconFactory.getIcon(inputStream.readBytes4ToInt()) + 0x0008 -> group.level = inputStream.readBytes2ToUShort() + 0x0009 -> group.flags = inputStream.readBytes4ToInt() + else -> throw UnsupportedEncodingException("Field type $fieldType") } - 0x0001 -> grp.setGroupId(readInt(buf, offset)) - 0x0002 -> grp.title = DatabaseInputOutputUtils.readCString(buf, offset) - 0x0003 -> grp.creationTime = DatabaseInputOutputUtils.readCDate(buf, offset) - 0x0004 -> grp.lastModificationTime = DatabaseInputOutputUtils.readCDate(buf, offset) - 0x0005 -> grp.lastAccessTime = DatabaseInputOutputUtils.readCDate(buf, offset) - 0x0006 -> grp.expiryTime = DatabaseInputOutputUtils.readCDate(buf, offset) - 0x0007 -> grp.icon = db.iconFactory.getIcon(readInt(buf, offset)) - 0x0008 -> grp.level = readUShort(buf, offset) - 0x0009 -> grp.flags = readInt(buf, offset) - }// Ignore field + } } @Throws(UnsupportedEncodingException::class) - private fun readEntryField(db: DatabaseKDB, ent: EntryKDB, buf: ByteArray, offset: Int) { - var offsetMutable = offset - val fieldType = readUShort(buf, offsetMutable) - offsetMutable += 2 - val fieldSize = readInt(buf, offsetMutable) - offsetMutable += 4 - - when (fieldType) { - 0x0000 -> { - } - 0x0001 -> ent.nodeId = NodeIdUUID(readUuid(buf, offsetMutable)) - 0x0002 -> { - val groupKDB = mDatabaseToOpen.createGroup() - groupKDB.nodeId = NodeIdInt(readInt(buf, offsetMutable)) - ent.parent = groupKDB - } - 0x0003 -> { - var iconId = readInt(buf, offsetMutable) - - // Clean up after bug that set icon ids to -1 - if (iconId == -1) { - iconId = 0 + private fun readEntryField(database: DatabaseKDB, + entry: EntryKDB, + fieldType: Int, + fieldSize: Int, + inputStream: InputStream) { + if (fieldSize > 0) { + when (fieldType) { + 0x0001 -> entry.nodeId = NodeIdUUID(inputStream.readBytes16ToUuid()) + 0x0002 -> { + val groupKDB = mDatabaseToOpen.createGroup() + groupKDB.nodeId = NodeIdInt(inputStream.readBytes4ToInt()) + entry.parent = groupKDB } - - ent.icon = db.iconFactory.getIcon(iconId) + 0x0003 -> { + var iconId = inputStream.readBytes4ToInt() + // Clean up after bug that set icon ids to -1 + if (iconId == -1) { + iconId = 0 + } + entry.icon = database.iconFactory.getIcon(iconId) + } + 0x0004 -> entry.title = inputStream.readBytesToString(fieldSize) + 0x0005 -> entry.url = inputStream.readBytesToString(fieldSize) + 0x0006 -> entry.username = inputStream.readBytesToString(fieldSize) + 0x0007 -> entry.password = inputStream.readBytesToString(fieldSize,false) + 0x0008 -> entry.notes = inputStream.readBytesToString(fieldSize) + 0x0009 -> entry.creationTime = inputStream.readBytes5ToDate() + 0x000A -> entry.lastModificationTime = inputStream.readBytes5ToDate() + 0x000B -> entry.lastAccessTime = inputStream.readBytes5ToDate() + 0x000C -> entry.expiryTime = inputStream.readBytes5ToDate() + 0x000D -> entry.binaryDesc = inputStream.readBytesToString(fieldSize) + 0x000E -> entry.binaryData = inputStream.readBytesLength(fieldSize) + else -> throw UnsupportedEncodingException("Field type $fieldType") } - 0x0004 -> ent.title = DatabaseInputOutputUtils.readCString(buf, offsetMutable) - 0x0005 -> ent.url = DatabaseInputOutputUtils.readCString(buf, offsetMutable) - 0x0006 -> ent.username = DatabaseInputOutputUtils.readCString(buf, offsetMutable) - 0x0007 -> ent.password = DatabaseInputOutputUtils.readPassword(buf, offsetMutable) - 0x0008 -> ent.notes = DatabaseInputOutputUtils.readCString(buf, offsetMutable) - 0x0009 -> ent.creationTime = DatabaseInputOutputUtils.readCDate(buf, offsetMutable) - 0x000A -> ent.lastModificationTime = DatabaseInputOutputUtils.readCDate(buf, offsetMutable) - 0x000B -> ent.lastAccessTime = DatabaseInputOutputUtils.readCDate(buf, offsetMutable) - 0x000C -> ent.expiryTime = DatabaseInputOutputUtils.readCDate(buf, offsetMutable) - 0x000D -> ent.binaryDesc = DatabaseInputOutputUtils.readCString(buf, offsetMutable) - 0x000E -> ent.binaryData = DatabaseInputOutputUtils.readBytes(buf, offsetMutable, fieldSize) - }// Ignore field + } } companion object { 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 307f424af..86dc3c141 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 @@ -136,7 +136,7 @@ class DatabaseInputKDBX(private val streamDir: File, val storedStartBytes: ByteArray? try { storedStartBytes = dataDecrypted.readBytes(32) - if (storedStartBytes == null || storedStartBytes.size != 32) { + if (storedStartBytes.size != 32) { throw InvalidCredentialsDatabaseException() } } catch (e: IOException) { @@ -158,7 +158,7 @@ class DatabaseInputKDBX(private val streamDir: File, val hmacKey = mDatabase.hmacKey ?: throw LoadDatabaseException() val headerHmac = DatabaseHeaderKDBX.computeHeaderHmac(pbHeader, hmacKey) val storedHmac = isData.readBytes(32) - if (storedHmac == null || storedHmac.size != 32) { + if (storedHmac.size != 32) { throw InvalidCredentialsDatabaseException() } // Mac doesn't match @@ -822,7 +822,7 @@ class DatabaseInputKDBX(private val streamDir: File, buf = buf8 } - val seconds = readLong(buf, 0) + val seconds = bytes64ToLong(buf, 0) utcDate = DateKDBXUtil.convertKDBX4Time(seconds) } else { diff --git a/app/src/main/java/com/kunzisoft/keepass/database/file/output/EntryOutputKDB.kt b/app/src/main/java/com/kunzisoft/keepass/database/file/output/EntryOutputKDB.kt index bc9e994fa..cdcb191ab 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/file/output/EntryOutputKDB.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/file/output/EntryOutputKDB.kt @@ -81,16 +81,16 @@ class EntryOutputKDB length += DatabaseInputOutputUtils.writeCString(mEntry.notes, mOutputStream).toLong() // Create date - writeDate(CREATE_FIELD_TYPE, DatabaseInputOutputUtils.writeCDate(mEntry.creationTime.date)) + writeDate(CREATE_FIELD_TYPE, DatabaseInputOutputUtils.dateToBytes(mEntry.creationTime.date)) // Modification date - writeDate(MOD_FIELD_TYPE, DatabaseInputOutputUtils.writeCDate(mEntry.lastModificationTime.date)) + writeDate(MOD_FIELD_TYPE, DatabaseInputOutputUtils.dateToBytes(mEntry.lastModificationTime.date)) // Access date - writeDate(ACCESS_FIELD_TYPE, DatabaseInputOutputUtils.writeCDate(mEntry.lastAccessTime.date)) + writeDate(ACCESS_FIELD_TYPE, DatabaseInputOutputUtils.dateToBytes(mEntry.lastAccessTime.date)) // Expiration date - writeDate(EXPIRE_FIELD_TYPE, DatabaseInputOutputUtils.writeCDate(mEntry.expiryTime.date)) + writeDate(EXPIRE_FIELD_TYPE, DatabaseInputOutputUtils.dateToBytes(mEntry.expiryTime.date)) // Binary writeBinary(mEntry.binaryData) diff --git a/app/src/main/java/com/kunzisoft/keepass/database/file/output/GroupOutputKDB.kt b/app/src/main/java/com/kunzisoft/keepass/database/file/output/GroupOutputKDB.kt index 650794d42..2cae753c9 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/file/output/GroupOutputKDB.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/file/output/GroupOutputKDB.kt @@ -47,22 +47,22 @@ class GroupOutputKDB (private val mGroup: GroupKDB, private val mOutputStream: O // Create date mOutputStream.write(CREATE_FIELD_TYPE) mOutputStream.write(DATE_FIELD_SIZE) - mOutputStream.write(DatabaseInputOutputUtils.writeCDate(mGroup.creationTime.date)) + mOutputStream.write(DatabaseInputOutputUtils.dateToBytes(mGroup.creationTime.date)) // Modification date mOutputStream.write(MOD_FIELD_TYPE) mOutputStream.write(DATE_FIELD_SIZE) - mOutputStream.write(DatabaseInputOutputUtils.writeCDate(mGroup.lastModificationTime.date)) + mOutputStream.write(DatabaseInputOutputUtils.dateToBytes(mGroup.lastModificationTime.date)) // Access date mOutputStream.write(ACCESS_FIELD_TYPE) mOutputStream.write(DATE_FIELD_SIZE) - mOutputStream.write(DatabaseInputOutputUtils.writeCDate(mGroup.lastAccessTime.date)) + mOutputStream.write(DatabaseInputOutputUtils.dateToBytes(mGroup.lastAccessTime.date)) // Expiration date mOutputStream.write(EXPIRE_FIELD_TYPE) mOutputStream.write(DATE_FIELD_SIZE) - mOutputStream.write(DatabaseInputOutputUtils.writeCDate(mGroup.expiryTime.date)) + mOutputStream.write(DatabaseInputOutputUtils.dateToBytes(mGroup.expiryTime.date)) // Image ID mOutputStream.write(IMAGEID_FIELD_TYPE) diff --git a/app/src/main/java/com/kunzisoft/keepass/stream/HashedBlockInputStream.kt b/app/src/main/java/com/kunzisoft/keepass/stream/HashedBlockInputStream.kt index 82314b782..2a7f930f3 100644 --- a/app/src/main/java/com/kunzisoft/keepass/stream/HashedBlockInputStream.kt +++ b/app/src/main/java/com/kunzisoft/keepass/stream/HashedBlockInputStream.kt @@ -87,11 +87,11 @@ class HashedBlockInputStream(inputStream: InputStream) : InputStream() { bufferIndex++ val storedHash = baseStream.readBytes(32) - if (storedHash == null || storedHash.size != HASH_SIZE) { + if (storedHash.size != HASH_SIZE) { throw IOException("Invalid data format") } - val bufferSize = readInt(baseStream) + val bufferSize = baseStream.readBytes4ToInt() if (bufferSize < 0) { throw IOException("Invalid data format") } diff --git a/app/src/main/java/com/kunzisoft/keepass/stream/HmacBlockInputStream.kt b/app/src/main/java/com/kunzisoft/keepass/stream/HmacBlockInputStream.kt index d4b51454f..d9eb3ae8c 100644 --- a/app/src/main/java/com/kunzisoft/keepass/stream/HmacBlockInputStream.kt +++ b/app/src/main/java/com/kunzisoft/keepass/stream/HmacBlockInputStream.kt @@ -89,16 +89,16 @@ class HmacBlockInputStream(baseStream: InputStream, private val verify: Boolean, if (endOfStream) return false val storedHmac = baseStream.readBytes(32) - if (storedHmac == null || storedHmac.size != 32) { + if (storedHmac.size != 32) { throw IOException("File corrupted") } val pbBlockIndex = writeLongBuf(blockIndex) val pbBlockSize = baseStream.readBytes(4) - if (pbBlockSize == null || pbBlockSize.size != 4) { + if (pbBlockSize.size != 4) { throw IOException("File corrupted") } - val blockSize = readInt(pbBlockSize, 0) + val blockSize = bytes4ToInt(pbBlockSize, 0) bufferPos = 0 buffer = baseStream.readBytes(blockSize) diff --git a/app/src/main/java/com/kunzisoft/keepass/stream/HmacBlockOutputStream.java b/app/src/main/java/com/kunzisoft/keepass/stream/HmacBlockOutputStream.java index 215cac4cb..14ac24c2e 100644 --- a/app/src/main/java/com/kunzisoft/keepass/stream/HmacBlockOutputStream.java +++ b/app/src/main/java/com/kunzisoft/keepass/stream/HmacBlockOutputStream.java @@ -27,8 +27,8 @@ import java.security.NoSuchAlgorithmException; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; -import static com.kunzisoft.keepass.stream.ReadBytesKt.writeIntBuf; -import static com.kunzisoft.keepass.stream.ReadBytesKt.writeLongBuf; +import static com.kunzisoft.keepass.stream.StreamBytesUtilsKt.writeIntBuf; +import static com.kunzisoft.keepass.stream.StreamBytesUtilsKt.writeLongBuf; public class HmacBlockOutputStream extends OutputStream { private static final int DEFAULT_BUFFER_SIZE = 1024 * 1024; diff --git a/app/src/main/java/com/kunzisoft/keepass/stream/LittleEndianDataInputStream.kt b/app/src/main/java/com/kunzisoft/keepass/stream/LittleEndianDataInputStream.kt index 3aeb4a2bb..c864790a9 100644 --- a/app/src/main/java/com/kunzisoft/keepass/stream/LittleEndianDataInputStream.kt +++ b/app/src/main/java/com/kunzisoft/keepass/stream/LittleEndianDataInputStream.kt @@ -34,18 +34,18 @@ class LittleEndianDataInputStream(private val baseStream: InputStream) : InputSt */ @Throws(IOException::class) fun readUInt(): Long { - return readUInt(baseStream) + return baseStream.readBytes4ToUInt() } @Throws(IOException::class) fun readInt(): Int { - return readInt(baseStream) + return baseStream.readBytes4ToInt() } @Throws(IOException::class) fun readLong(): Long { val buf = readBytes(8) - return readLong(buf, 0) + return bytes64ToLong(buf, 0) } @Throws(IOException::class) @@ -53,7 +53,7 @@ class LittleEndianDataInputStream(private val baseStream: InputStream) : InputSt val buf = ByteArray(2) if (baseStream.read(buf, 0, 2) != 2) throw IOException("Unable to read UShort value") - return readUShort(buf, 0) + return bytes2ToUShort(buf, 0) } @Throws(IOException::class) diff --git a/app/src/main/java/com/kunzisoft/keepass/stream/ReadBytes.kt b/app/src/main/java/com/kunzisoft/keepass/stream/StreamBytesUtils.kt similarity index 69% rename from app/src/main/java/com/kunzisoft/keepass/stream/ReadBytes.kt rename to app/src/main/java/com/kunzisoft/keepass/stream/StreamBytesUtils.kt index 43f10057c..c3235391a 100644 --- a/app/src/main/java/com/kunzisoft/keepass/stream/ReadBytes.kt +++ b/app/src/main/java/com/kunzisoft/keepass/stream/StreamBytesUtils.kt @@ -19,10 +19,12 @@ */ package com.kunzisoft.keepass.stream +import com.kunzisoft.keepass.database.element.DateInstant +import com.kunzisoft.keepass.utils.DatabaseInputOutputUtils.bytes5ToDate +import com.kunzisoft.keepass.utils.DatabaseInputOutputUtils.bytesToString import java.io.IOException import java.io.InputStream import java.util.* -import kotlin.experimental.and /** * Read all data of stream and invoke [readBytes] each time the buffer is full or no more data to read. @@ -74,56 +76,90 @@ fun InputStream.readBytes(length: Int, bufferSize: Int, readBytes: (bytesRead: B } } +/** + * Read a 32-bit value and return it as a long, so that it can + * be interpreted as an unsigned integer. + */ +@Throws(IOException::class) +fun InputStream.readBytes4ToUInt(): Long { + return readBytes4ToInt().toLong()// and INT_TO_LONG_MASK // TODO +} + +@Throws(IOException::class) +fun InputStream.readBytes4ToInt(): Int { + return bytes4ToInt(readBytesLength(4), 0) +} + +@Throws(IOException::class) +fun InputStream.readBytes2ToUShort(): Int { + return bytes2ToUShort(readBytesLength(2), 0) +} + +@Throws(IOException::class) +fun InputStream.readBytes5ToDate(): DateInstant { + return bytes5ToDate(readBytesLength(5), 0) +} + +@Throws(IOException::class) +fun InputStream.readBytes16ToUuid(): UUID { + return bytes16ToUuid(readBytesLength(16), 0) +} + +@Throws(IOException::class) +fun InputStream.readBytesToString(length: Int, replaceCRLF: Boolean = true): String { + return bytesToString(this.readBytesLength(length), 0, replaceCRLF) +} + +@Throws(IOException::class) +fun InputStream.readBytesLength(length: Int): ByteArray { + val buf = ByteArray(length) + // WARNING this.read(buf, 0, length) Doesn't work + for (i in 0 until length) { + buf[i] = this.read().toByte() + } + return buf +} + /** * Read an unsigned 16-bit value. */ -fun readUShort(buf: ByteArray, offset: Int): Int { +fun bytes2ToUShort(buf: ByteArray, offset: Int): Int { return ((buf[offset].toInt() and 0xFF) + (buf[offset + 1].toInt() and 0xFF shl 8)) } -fun readLong(buf: ByteArray, offset: Int): Long { - return ((buf[offset].toLong() and 0xFF) + (buf[offset + 1].toLong() and 0xFF shl 8) - + (buf[offset + 2].toLong() and 0xFF shl 16) + (buf[offset + 3].toLong() and 0xFF shl 24) - + (buf[offset + 4].toLong() and 0xFF shl 32) + (buf[offset + 5].toLong() and 0xFF shl 40) - + (buf[offset + 6].toLong() and 0xFF shl 48) + (buf[offset + 7].toLong() and 0xFF shl 56)) +/** + * Read a 64 bit long + */ +fun bytes64ToLong(buf: ByteArray, offset: Int): Long { + return ((buf[offset].toLong() and 0xFF) + + (buf[offset + 1].toLong() and 0xFF shl 8) + + (buf[offset + 2].toLong() and 0xFF shl 16) + + (buf[offset + 3].toLong() and 0xFF shl 24) + + (buf[offset + 4].toLong() and 0xFF shl 32) + + (buf[offset + 5].toLong() and 0xFF shl 40) + + (buf[offset + 6].toLong() and 0xFF shl 48) + + (buf[offset + 7].toLong() and 0xFF shl 56)) } private const val INT_TO_LONG_MASK: Long = 0xffffffffL -fun readUInt(buf: ByteArray, offset: Int): Long { - return readInt(buf, offset).toLong()// and INT_TO_LONG_MASK // TODO -} - -/** - * Read a 32-bit value and return it as a long, so that it can - * be interpreted as an unsigned integer. - */ -@Throws(IOException::class) -fun readUInt(inputStream: InputStream): Long { - return readInt(inputStream).toLong()// and INT_TO_LONG_MASK // TODO -} - -@Throws(IOException::class) -fun readInt(inputStream: InputStream): Int { - val buf = ByteArray(4) - if (inputStream.read(buf, 0, 4) != 4) - throw IOException("Unable to read int value") - return readInt(buf, 0) +fun bytes4ToUInt(buf: ByteArray, offset: Int): Long { + return bytes4ToInt(buf, offset).toLong()// and INT_TO_LONG_MASK // TODO } /** * Read a 32-bit value. */ -fun readInt(buf: ByteArray, offset: Int): Int { +fun bytes4ToInt(buf: ByteArray, offset: Int): Int { return ((buf[offset].toInt() and 0xFF) + (buf[offset + 1].toInt() and 0xFF shl 8) + (buf[offset + 2].toInt() and 0xFF shl 16) + (buf[offset + 3].toInt() and 0xFF shl 24)) } -fun readUuid(buf: ByteArray, offset: Int): UUID { +fun bytes16ToUuid(buf: ByteArray, offset: Int): UUID { var lsb: Long = 0 for (i in 15 downTo 8) { lsb = lsb shl 8 or (buf[i + offset].toLong() and 0xff) diff --git a/app/src/main/java/com/kunzisoft/keepass/utils/DatabaseInputOutputUtils.kt b/app/src/main/java/com/kunzisoft/keepass/utils/DatabaseInputOutputUtils.kt index 8cf93c5d6..fe1977b2f 100644 --- a/app/src/main/java/com/kunzisoft/keepass/utils/DatabaseInputOutputUtils.kt +++ b/app/src/main/java/com/kunzisoft/keepass/utils/DatabaseInputOutputUtils.kt @@ -43,7 +43,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA package com.kunzisoft.keepass.utils import com.kunzisoft.keepass.database.element.DateInstant -import com.kunzisoft.keepass.stream.readUuid +import com.kunzisoft.keepass.stream.bytes16ToUuid import com.kunzisoft.keepass.stream.writeIntBuf import com.kunzisoft.keepass.stream.writeLong import java.io.IOException @@ -92,20 +92,18 @@ object DatabaseInputOutputUtils { * Return len of null-terminated string (i.e. distance to null) * within a byte buffer. */ - private fun strlen(buf: ByteArray, offset: Int): Int { + fun bytesStringLength(buf: ByteArray, offset: Int): Int { var len = 0 while (buf[offset + len].toInt() != 0) len++ return len } - fun readCString(buf: ByteArray, offset: Int): String { - var jstring = String(buf, offset, strlen(buf, offset), defaultCharset) - - if (REPLACE) { + fun bytesToString(buf: ByteArray, offset: Int, replaceCRLF: Boolean = true): String { + var jstring = String(buf, offset, bytesStringLength(buf, offset), defaultCharset) + if (replaceCRLF && REPLACE) { jstring = jstring.replace(CRLF, SEP!!) } - return jstring } @@ -137,7 +135,7 @@ object DatabaseInputOutputUtils { * Unpack date from 5 byte format. The five bytes at 'offset' are unpacked * to a java.util.Date instance. */ - fun readCDate(buf: ByteArray, offset: Int, calendar: Calendar = Calendar.getInstance()): DateInstant { + fun bytes5ToDate(buf: ByteArray, offset: Int, calendar: Calendar = Calendar.getInstance()): DateInstant { val dateSize = 5 val cDate = ByteArray(dateSize) System.arraycopy(buf, offset, cDate, 0, dateSize) @@ -165,7 +163,7 @@ object DatabaseInputOutputUtils { return DateInstant(calendar.time) } - fun writeCDate(date: Date?, calendar: Calendar = Calendar.getInstance()): ByteArray? { + fun dateToBytes(date: Date?, calendar: Calendar = Calendar.getInstance()): ByteArray? { if (date == null) { return null } @@ -192,10 +190,6 @@ object DatabaseInputOutputUtils { return buf } - fun readPassword(buf: ByteArray, offset: Int): String { - return String(buf, offset, strlen(buf, offset), defaultCharset) - } - @Throws(IOException::class) fun writePassword(str: String, os: OutputStream): Int { val initial = str.toByteArray(defaultCharset) @@ -222,7 +216,7 @@ object DatabaseInputOutputUtils { } fun bytesToUuid(buf: ByteArray): UUID { - return readUuid(buf, 0) + return bytes16ToUuid(buf, 0) } fun uuidToBytes(uuid: UUID): ByteArray { diff --git a/app/src/main/java/com/kunzisoft/keepass/utils/VariantDictionary.java b/app/src/main/java/com/kunzisoft/keepass/utils/VariantDictionary.java index 15d0783e8..9a23a1597 100644 --- a/app/src/main/java/com/kunzisoft/keepass/utils/VariantDictionary.java +++ b/app/src/main/java/com/kunzisoft/keepass/utils/VariantDictionary.java @@ -21,15 +21,14 @@ package com.kunzisoft.keepass.utils; import com.kunzisoft.keepass.stream.LittleEndianDataInputStream; import com.kunzisoft.keepass.stream.LittleEndianDataOutputStream; -import com.kunzisoft.keepass.stream.ReadBytesKt; +import com.kunzisoft.keepass.stream.StreamBytesUtilsKt; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map; -import static com.kunzisoft.keepass.stream.ReadBytesKt.readLong; -import static com.kunzisoft.keepass.stream.ReadBytesKt.readUInt; +import static com.kunzisoft.keepass.stream.StreamBytesUtilsKt.bytes64ToLong; public class VariantDictionary { private static final int VdVersion = 0x0100; @@ -125,12 +124,12 @@ public class VariantDictionary { switch (bType) { case VdType.UInt32: if (valueLen == 4) { - d.setUInt32(name, readUInt(valueBuf, 0)); + d.setUInt32(name, StreamBytesUtilsKt.bytes4ToUInt(valueBuf, 0)); } break; case VdType.UInt64: if (valueLen == 8) { - d.setUInt64(name, readLong(valueBuf, 0)); + d.setUInt64(name, bytes64ToLong(valueBuf, 0)); } break; case VdType.Bool: @@ -140,12 +139,12 @@ public class VariantDictionary { break; case VdType.Int32: if (valueLen == 4) { - d.setInt32(name, ReadBytesKt.readInt(valueBuf, 0)); + d.setInt32(name, StreamBytesUtilsKt.bytes4ToInt(valueBuf, 0)); } break; case VdType.Int64: if (valueLen == 8) { - d.setInt64(name, readLong(valueBuf, 0)); + d.setInt64(name, bytes64ToLong(valueBuf, 0)); } break; case VdType.String: