From 12c2a6e99c53ebe35495c1390e37e26bb4e8db2b Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Fri, 29 Nov 2019 14:19:13 +0100 Subject: [PATCH] Refactor bytes utility methods --- .../tests/DatabaseInputOutputUtilsTest.kt | 21 +-- .../kunzisoft/keepass/crypto/CryptoUtil.kt | 8 +- .../keepass/database/element/Database.kt | 11 +- .../database/file/DatabaseHeaderKDB.kt | 17 +- .../database/file/DatabaseHeaderKDBX.kt | 10 +- .../database/file/input/DatabaseInputKDB.kt | 39 ++-- .../database/file/input/DatabaseInputKDBX.kt | 7 +- .../file/output/DatabaseHeaderOutputKDB.kt | 17 +- .../file/output/DatabaseHeaderOutputKDBX.kt | 16 +- .../file/output/DatabaseOutputKDBX.kt | 9 +- .../database/file/output/EntryOutputKDB.kt | 46 ++--- .../database/file/output/GroupOutputKDB.kt | 40 ++-- .../keepass/stream/HashedBlockInputStream.kt | 5 +- .../keepass/stream/HmacBlockInputStream.kt | 8 +- .../keepass/stream/HmacBlockOutputStream.java | 7 +- .../stream/LittleEndianDataInputStream.java | 176 ------------------ .../stream/LittleEndianDataInputStream.kt | 125 +++++++++++++ .../stream/LittleEndianDataOutputStream.java | 144 -------------- .../stream/LittleEndianDataOutputStream.kt | 86 +++++++++ .../com/kunzisoft/keepass/stream/ReadBytes.kt | 112 +++++++++++ .../keepass/utils/DatabaseInputOutputUtils.kt | 27 +-- .../keepass/utils/VariantDictionary.java | 12 +- 22 files changed, 474 insertions(+), 469 deletions(-) delete mode 100644 app/src/main/java/com/kunzisoft/keepass/stream/LittleEndianDataInputStream.java create mode 100644 app/src/main/java/com/kunzisoft/keepass/stream/LittleEndianDataInputStream.kt delete mode 100644 app/src/main/java/com/kunzisoft/keepass/stream/LittleEndianDataOutputStream.java create mode 100644 app/src/main/java/com/kunzisoft/keepass/stream/LittleEndianDataOutputStream.kt 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 1938435db..a00b3933c 100644 --- a/app/src/androidTest/java/com/kunzisoft/keepass/tests/DatabaseInputOutputUtilsTest.kt +++ b/app/src/androidTest/java/com/kunzisoft/keepass/tests/DatabaseInputOutputUtilsTest.kt @@ -19,6 +19,7 @@ */ package com.kunzisoft.keepass.tests +import com.kunzisoft.keepass.stream.* import org.junit.Assert.assertArrayEquals import java.io.ByteArrayOutputStream @@ -27,8 +28,6 @@ import java.util.Random import junit.framework.TestCase -import com.kunzisoft.keepass.stream.LittleEndianDataInputStream -import com.kunzisoft.keepass.stream.LittleEndianDataOutputStream import com.kunzisoft.keepass.utils.DatabaseInputOutputUtils class DatabaseInputOutputUtilsTest : TestCase() { @@ -59,8 +58,8 @@ class DatabaseInputOutputUtilsTest : TestCase() { setArray(orig, value, 0, 8) - val one = LittleEndianDataInputStream.readLong(orig, 0) - LittleEndianDataOutputStream.writeLong(one, dest, 0) + val one = readLong(orig, 0) + writeLong(one, dest, 0) assertArrayEquals(orig, dest) @@ -88,9 +87,9 @@ class DatabaseInputOutputUtilsTest : TestCase() { setArray(orig, value, 0, 4) - val one = LittleEndianDataInputStream.readInt(orig, 0) + val one = readInt(orig, 0) - LittleEndianDataOutputStream.writeInt(one, dest, 0) + writeInt(one, dest, 0) assertArrayEquals(orig, dest) @@ -108,8 +107,8 @@ class DatabaseInputOutputUtilsTest : TestCase() { orig[0] = 0 orig[1] = 1 - val one = LittleEndianDataInputStream.readUShort(orig, 0) - val dest = LittleEndianDataOutputStream.writeUShortBuf(one) + val one = readUShort(orig, 0) + val dest = writeUShortBuf(one) assertArrayEquals(orig, dest) @@ -129,8 +128,8 @@ class DatabaseInputOutputUtilsTest : TestCase() { setArray(orig, value, 0, 2) - val one = LittleEndianDataInputStream.readUShort(orig, 0) - LittleEndianDataOutputStream.writeUShort(one, dest, 0) + val one = readUShort(orig, 0) + writeUShort(one, dest, 0) assertArrayEquals(orig, dest) @@ -187,7 +186,7 @@ class DatabaseInputOutputUtilsTest : TestCase() { val uuid = DatabaseInputOutputUtils.bytesToUuid(bUUID) val eUUID = DatabaseInputOutputUtils.uuidToBytes(uuid) - val lUUID = LittleEndianDataInputStream.readUuid(bUUID, 0) + val lUUID = readUuid(bUUID, 0) val leUUID = DatabaseInputOutputUtils.uuidToBytes(lUUID) assertArrayEquals("UUID match failed", bUUID, eUUID) diff --git a/app/src/main/java/com/kunzisoft/keepass/crypto/CryptoUtil.kt b/app/src/main/java/com/kunzisoft/keepass/crypto/CryptoUtil.kt index eb0e397a6..5b6c7f0c2 100644 --- a/app/src/main/java/com/kunzisoft/keepass/crypto/CryptoUtil.kt +++ b/app/src/main/java/com/kunzisoft/keepass/crypto/CryptoUtil.kt @@ -19,15 +19,13 @@ */ package com.kunzisoft.keepass.crypto -import com.kunzisoft.keepass.stream.LittleEndianDataOutputStream import com.kunzisoft.keepass.stream.NullOutputStream - +import com.kunzisoft.keepass.stream.writeLongBuf import java.io.IOException import java.security.DigestOutputStream import java.security.MessageDigest import java.security.NoSuchAlgorithmException -import java.util.Arrays - +import java.util.* import javax.crypto.Mac import kotlin.math.min @@ -60,7 +58,7 @@ object CryptoUtil { throw RuntimeException(e) } - val pbR = LittleEndianDataOutputStream.writeLongBuf(r) + val pbR = writeLongBuf(r) val part = hmac.doFinal(pbR) val copy = min(cbOut - pos, part.size) 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 24d54fab9..ef78a0f64 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 @@ -36,7 +36,10 @@ import com.kunzisoft.keepass.database.element.node.NodeId import com.kunzisoft.keepass.database.element.node.NodeIdInt import com.kunzisoft.keepass.database.element.node.NodeIdUUID import com.kunzisoft.keepass.database.element.security.EncryptionAlgorithm -import com.kunzisoft.keepass.database.exception.* +import com.kunzisoft.keepass.database.exception.DatabaseOutputException +import com.kunzisoft.keepass.database.exception.FileNotFoundDatabaseException +import com.kunzisoft.keepass.database.exception.LoadDatabaseException +import com.kunzisoft.keepass.database.exception.SignatureDatabaseException import com.kunzisoft.keepass.database.file.DatabaseHeaderKDB import com.kunzisoft.keepass.database.file.DatabaseHeaderKDBX import com.kunzisoft.keepass.database.file.input.DatabaseInputKDB @@ -45,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.LittleEndianDataInputStream +import com.kunzisoft.keepass.stream.readInt import com.kunzisoft.keepass.tasks.ProgressTaskUpdater import com.kunzisoft.keepass.utils.SingletonHolder import com.kunzisoft.keepass.utils.UriUtil @@ -355,8 +358,8 @@ class Database { bufferedInputStream.mark(10) // Get the file directory to save the attachments - val sig1 = LittleEndianDataInputStream.readInt(bufferedInputStream) - val sig2 = LittleEndianDataInputStream.readInt(bufferedInputStream) + val sig1 = readInt(bufferedInputStream) + val sig2 = readInt(bufferedInputStream) // Return to the start bufferedInputStream.reset() 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 c433b1a9d..7d2e81ceb 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,7 @@ package com.kunzisoft.keepass.database.file -import com.kunzisoft.keepass.stream.LittleEndianDataInputStream - +import com.kunzisoft.keepass.stream.readInt import java.io.IOException class DatabaseHeaderKDB : DatabaseHeader() { @@ -57,21 +56,21 @@ class DatabaseHeaderKDB : DatabaseHeader() { */ @Throws(IOException::class) fun loadFromFile(buf: ByteArray, offset: Int) { - signature1 = LittleEndianDataInputStream.readInt(buf, offset) - signature2 = LittleEndianDataInputStream.readInt(buf, offset + 4) - flags = LittleEndianDataInputStream.readInt(buf, offset + 8) - version = LittleEndianDataInputStream.readInt(buf, offset + 12) + 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 = LittleEndianDataInputStream.readInt(buf, offset + 48) - numEntries = LittleEndianDataInputStream.readInt(buf, offset + 52) + 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 = LittleEndianDataInputStream.readInt(buf, offset + 120) + numKeyEncRounds = readInt(buf, offset + 120) 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 470a69a42..09261465b 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 @@ -30,9 +30,7 @@ import com.kunzisoft.keepass.database.element.entry.EntryKDBX import com.kunzisoft.keepass.database.element.group.GroupKDBX import com.kunzisoft.keepass.database.element.node.NodeKDBXInterface import com.kunzisoft.keepass.database.exception.VersionDatabaseException -import com.kunzisoft.keepass.stream.CopyInputStream -import com.kunzisoft.keepass.stream.HmacBlockStream -import com.kunzisoft.keepass.stream.LittleEndianDataInputStream +import com.kunzisoft.keepass.stream.* import com.kunzisoft.keepass.utils.DatabaseInputOutputUtils import java.io.ByteArrayOutputStream import java.io.IOException @@ -248,7 +246,7 @@ class DatabaseHeaderKDBX(private val databaseV4: DatabaseKDBX) : DatabaseHeader( private fun setTransformRound(roundsByte: ByteArray?) { assignAesKdfEngineIfNotExists() - val rounds = LittleEndianDataInputStream.readLong(roundsByte!!, 0) + val rounds = readLong(roundsByte!!, 0) databaseV4.kdfParameters?.setUInt64(AesKdf.PARAM_ROUNDS, rounds) databaseV4.numberKeyEncryptionRounds = rounds } @@ -259,7 +257,7 @@ class DatabaseHeaderKDBX(private val databaseV4: DatabaseKDBX) : DatabaseHeader( throw IOException("Invalid compression flags.") } - val flag = LittleEndianDataInputStream.readInt(pbFlags, 0) + val flag = readInt(pbFlags, 0) if (flag < 0 || flag >= CompressionAlgorithm.values().size) { throw IOException("Unrecognized compression flag.") } @@ -275,7 +273,7 @@ class DatabaseHeaderKDBX(private val databaseV4: DatabaseKDBX) : DatabaseHeader( throw IOException("Invalid stream id.") } - val id = LittleEndianDataInputStream.readInt(streamID, 0) + val id = readInt(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 70ca73379..8976ec8bf 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 @@ -57,19 +57,20 @@ 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.LittleEndianDataInputStream 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.tasks.ProgressTaskUpdater import com.kunzisoft.keepass.utils.DatabaseInputOutputUtils - -import javax.crypto.* -import javax.crypto.spec.IvParameterSpec -import javax.crypto.spec.SecretKeySpec import java.io.IOException import java.io.InputStream import java.io.UnsupportedEncodingException import java.security.* -import java.util.Arrays +import java.util.* +import javax.crypto.* +import javax.crypto.spec.IvParameterSpec +import javax.crypto.spec.SecretKeySpec /** * Load a KDB database file. @@ -189,9 +190,9 @@ class DatabaseInputKDB : DatabaseInput() { run { var i = 0 while (i < header.numGroups) { - val fieldType = LittleEndianDataInputStream.readUShort(filebuf, pos) + val fieldType = readUShort(filebuf, pos) pos += 2 - val fieldSize = LittleEndianDataInputStream.readInt(filebuf, pos) + val fieldSize = readInt(filebuf, pos) pos += 4 if (fieldType == 0xFFFF) { @@ -210,8 +211,8 @@ class DatabaseInputKDB : DatabaseInput() { var newEnt = mDatabaseToOpen.createEntry() var i = 0 while (i < header.numEntries) { - val fieldType = LittleEndianDataInputStream.readUShort(filebuf, pos) - val fieldSize = LittleEndianDataInputStream.readInt(filebuf, pos + 2) + val fieldType = readUShort(filebuf, pos) + val fieldSize = readInt(filebuf, pos + 2) if (fieldType == 0xFFFF) { // End-Group record. Save group and count it. @@ -291,37 +292,37 @@ class DatabaseInputKDB : DatabaseInput() { when (fieldType) { 0x0000 -> { } - 0x0001 -> grp.setGroupId(LittleEndianDataInputStream.readInt(buf, offset)) + 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(LittleEndianDataInputStream.readInt(buf, offset)) - 0x0008 -> grp.level = LittleEndianDataInputStream.readUShort(buf, offset) - 0x0009 -> grp.flags = LittleEndianDataInputStream.readInt(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 = LittleEndianDataInputStream.readUShort(buf, offsetMutable) + val fieldType = readUShort(buf, offsetMutable) offsetMutable += 2 - val fieldSize = LittleEndianDataInputStream.readInt(buf, offsetMutable) + val fieldSize = readInt(buf, offsetMutable) offsetMutable += 4 when (fieldType) { 0x0000 -> { } - 0x0001 -> ent.nodeId = NodeIdUUID(LittleEndianDataInputStream.readUuid(buf, offsetMutable)) + 0x0001 -> ent.nodeId = NodeIdUUID(readUuid(buf, offsetMutable)) 0x0002 -> { val groupKDB = mDatabaseToOpen.createGroup() - groupKDB.nodeId = NodeIdInt(LittleEndianDataInputStream.readInt(buf, offsetMutable)) + groupKDB.nodeId = NodeIdInt(readInt(buf, offsetMutable)) ent.parent = groupKDB } 0x0003 -> { - var iconId = LittleEndianDataInputStream.readInt(buf, offsetMutable) + var iconId = readInt(buf, offsetMutable) // Clean up after bug that set icon ids to -1 if (iconId == -1) { 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 7ef6aaa32..307f424af 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 @@ -24,7 +24,8 @@ import com.kunzisoft.keepass.R import com.kunzisoft.keepass.crypto.CipherFactory import com.kunzisoft.keepass.crypto.StreamCipherFactory import com.kunzisoft.keepass.crypto.engine.CipherEngine -import com.kunzisoft.keepass.database.element.* +import com.kunzisoft.keepass.database.element.DateInstant +import com.kunzisoft.keepass.database.element.DeletedObject import com.kunzisoft.keepass.database.element.database.CompressionAlgorithm import com.kunzisoft.keepass.database.element.database.DatabaseKDBX import com.kunzisoft.keepass.database.element.database.DatabaseKDBX.Companion.BASE_64_FLAG @@ -37,9 +38,9 @@ import com.kunzisoft.keepass.database.element.node.NodeKDBXInterface import com.kunzisoft.keepass.database.element.security.BinaryAttachment import com.kunzisoft.keepass.database.element.security.ProtectedString import com.kunzisoft.keepass.database.exception.* -import com.kunzisoft.keepass.database.file.DateKDBXUtil import com.kunzisoft.keepass.database.file.DatabaseHeaderKDBX import com.kunzisoft.keepass.database.file.DatabaseKDBXXML +import com.kunzisoft.keepass.database.file.DateKDBXUtil import com.kunzisoft.keepass.stream.* import com.kunzisoft.keepass.tasks.ProgressTaskUpdater import com.kunzisoft.keepass.utils.DatabaseInputOutputUtils @@ -821,7 +822,7 @@ class DatabaseInputKDBX(private val streamDir: File, buf = buf8 } - val seconds = LittleEndianDataInputStream.readLong(buf, 0) + val seconds = readLong(buf, 0) utcDate = DateKDBXUtil.convertKDBX4Time(seconds) } else { diff --git a/app/src/main/java/com/kunzisoft/keepass/database/file/output/DatabaseHeaderOutputKDB.kt b/app/src/main/java/com/kunzisoft/keepass/database/file/output/DatabaseHeaderOutputKDB.kt index 8bc6d77c0..924da7352 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/file/output/DatabaseHeaderOutputKDB.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/file/output/DatabaseHeaderOutputKDB.kt @@ -20,8 +20,7 @@ package com.kunzisoft.keepass.database.file.output import com.kunzisoft.keepass.database.file.DatabaseHeaderKDB -import com.kunzisoft.keepass.stream.LittleEndianDataOutputStream - +import com.kunzisoft.keepass.stream.writeIntBuf import java.io.IOException import java.io.OutputStream @@ -30,14 +29,14 @@ class DatabaseHeaderOutputKDB(private val mHeader: DatabaseHeaderKDB, @Throws(IOException::class) fun outputStart() { - mOutputStream.write(LittleEndianDataOutputStream.writeIntBuf(mHeader.signature1)) - mOutputStream.write(LittleEndianDataOutputStream.writeIntBuf(mHeader.signature2)) - mOutputStream.write(LittleEndianDataOutputStream.writeIntBuf(mHeader.flags)) - mOutputStream.write(LittleEndianDataOutputStream.writeIntBuf(mHeader.version)) + mOutputStream.write(writeIntBuf(mHeader.signature1)) + mOutputStream.write(writeIntBuf(mHeader.signature2)) + mOutputStream.write(writeIntBuf(mHeader.flags)) + mOutputStream.write(writeIntBuf(mHeader.version)) mOutputStream.write(mHeader.masterSeed) mOutputStream.write(mHeader.encryptionIV) - mOutputStream.write(LittleEndianDataOutputStream.writeIntBuf(mHeader.numGroups)) - mOutputStream.write(LittleEndianDataOutputStream.writeIntBuf(mHeader.numEntries)) + mOutputStream.write(writeIntBuf(mHeader.numGroups)) + mOutputStream.write(writeIntBuf(mHeader.numEntries)) } @Throws(IOException::class) @@ -48,7 +47,7 @@ class DatabaseHeaderOutputKDB(private val mHeader: DatabaseHeaderKDB, @Throws(IOException::class) fun outputEnd() { mOutputStream.write(mHeader.transformSeed) - mOutputStream.write(LittleEndianDataOutputStream.writeIntBuf(mHeader.numKeyEncRounds)) + mOutputStream.write(writeIntBuf(mHeader.numKeyEncRounds)) } @Throws(IOException::class) diff --git a/app/src/main/java/com/kunzisoft/keepass/database/file/output/DatabaseHeaderOutputKDBX.kt b/app/src/main/java/com/kunzisoft/keepass/database/file/output/DatabaseHeaderOutputKDBX.kt index ab364ae2b..57fa39b56 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/file/output/DatabaseHeaderOutputKDBX.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/file/output/DatabaseHeaderOutputKDBX.kt @@ -19,17 +19,14 @@ */ package com.kunzisoft.keepass.database.file.output -import com.kunzisoft.keepass.utils.VariantDictionary import com.kunzisoft.keepass.crypto.keyDerivation.KdfParameters import com.kunzisoft.keepass.database.element.database.DatabaseKDBX +import com.kunzisoft.keepass.database.exception.DatabaseOutputException import com.kunzisoft.keepass.database.file.DatabaseHeader import com.kunzisoft.keepass.database.file.DatabaseHeaderKDBX -import com.kunzisoft.keepass.database.exception.DatabaseOutputException -import com.kunzisoft.keepass.stream.HmacBlockStream -import com.kunzisoft.keepass.stream.LittleEndianDataOutputStream -import com.kunzisoft.keepass.stream.MacOutputStream +import com.kunzisoft.keepass.stream.* import com.kunzisoft.keepass.utils.DatabaseInputOutputUtils - +import com.kunzisoft.keepass.utils.VariantDictionary import java.io.ByteArrayOutputStream import java.io.IOException import java.io.OutputStream @@ -37,7 +34,6 @@ import java.security.DigestOutputStream import java.security.InvalidKeyException import java.security.MessageDigest import java.security.NoSuchAlgorithmException - import javax.crypto.Mac import javax.crypto.spec.SecretKeySpec @@ -87,12 +83,12 @@ constructor(private val db: DatabaseKDBX, private val header: DatabaseHeaderKDBX los.writeUInt(header.version) writeHeaderField(DatabaseHeaderKDBX.PwDbHeaderV4Fields.CipherID, DatabaseInputOutputUtils.uuidToBytes(db.dataCipher)) - writeHeaderField(DatabaseHeaderKDBX.PwDbHeaderV4Fields.CompressionFlags, LittleEndianDataOutputStream.writeIntBuf(DatabaseHeaderKDBX.getFlagFromCompression(db.compressionAlgorithm))) + writeHeaderField(DatabaseHeaderKDBX.PwDbHeaderV4Fields.CompressionFlags, writeIntBuf(DatabaseHeaderKDBX.getFlagFromCompression(db.compressionAlgorithm))) writeHeaderField(DatabaseHeaderKDBX.PwDbHeaderV4Fields.MasterSeed, header.masterSeed) if (header.version < DatabaseHeaderKDBX.FILE_VERSION_32_4) { writeHeaderField(DatabaseHeaderKDBX.PwDbHeaderV4Fields.TransformSeed, header.transformSeed) - writeHeaderField(DatabaseHeaderKDBX.PwDbHeaderV4Fields.TransformRounds, LittleEndianDataOutputStream.writeLongBuf(db.numberKeyEncryptionRounds)) + writeHeaderField(DatabaseHeaderKDBX.PwDbHeaderV4Fields.TransformRounds, writeLongBuf(db.numberKeyEncryptionRounds)) } else { writeHeaderField(DatabaseHeaderKDBX.PwDbHeaderV4Fields.KdfParameters, KdfParameters.serialize(db.kdfParameters!!)) } @@ -104,7 +100,7 @@ constructor(private val db: DatabaseKDBX, private val header: DatabaseHeaderKDBX if (header.version < DatabaseHeaderKDBX.FILE_VERSION_32_4) { writeHeaderField(DatabaseHeaderKDBX.PwDbHeaderV4Fields.InnerRandomstreamKey, header.innerRandomStreamKey) writeHeaderField(DatabaseHeaderKDBX.PwDbHeaderV4Fields.StreamStartBytes, header.streamStartBytes) - writeHeaderField(DatabaseHeaderKDBX.PwDbHeaderV4Fields.InnerRandomStreamID, LittleEndianDataOutputStream.writeIntBuf(header.innerRandomStream!!.id)) + writeHeaderField(DatabaseHeaderKDBX.PwDbHeaderV4Fields.InnerRandomStreamID, writeIntBuf(header.innerRandomStream!!.id)) } if (db.containsPublicCustomData()) { diff --git a/app/src/main/java/com/kunzisoft/keepass/database/file/output/DatabaseOutputKDBX.kt b/app/src/main/java/com/kunzisoft/keepass/database/file/output/DatabaseOutputKDBX.kt index c1ee0068a..a061bc46a 100644 --- a/app/src/main/java/com/kunzisoft/keepass/database/file/output/DatabaseOutputKDBX.kt +++ b/app/src/main/java/com/kunzisoft/keepass/database/file/output/DatabaseOutputKDBX.kt @@ -28,7 +28,7 @@ import com.kunzisoft.keepass.crypto.StreamCipherFactory import com.kunzisoft.keepass.crypto.engine.CipherEngine import com.kunzisoft.keepass.crypto.keyDerivation.KdfFactory import com.kunzisoft.keepass.database.action.node.NodeHandler -import com.kunzisoft.keepass.database.element.* +import com.kunzisoft.keepass.database.element.DeletedObject import com.kunzisoft.keepass.database.element.database.CompressionAlgorithm import com.kunzisoft.keepass.database.element.database.DatabaseKDBX import com.kunzisoft.keepass.database.element.database.DatabaseKDBX.Companion.BASE_64_FLAG @@ -46,7 +46,10 @@ import com.kunzisoft.keepass.database.exception.UnknownKDF import com.kunzisoft.keepass.database.file.DatabaseHeaderKDBX import com.kunzisoft.keepass.database.file.DatabaseKDBXXML import com.kunzisoft.keepass.database.file.DateKDBXUtil -import com.kunzisoft.keepass.stream.* +import com.kunzisoft.keepass.stream.HashedBlockOutputStream +import com.kunzisoft.keepass.stream.HmacBlockOutputStream +import com.kunzisoft.keepass.stream.readBytes +import com.kunzisoft.keepass.stream.writeLongBuf import com.kunzisoft.keepass.utils.DatabaseInputOutputUtils import org.joda.time.DateTime import org.spongycastle.crypto.StreamCipher @@ -390,7 +393,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX, } else { val dt = DateTime(value) val seconds = DateKDBXUtil.convertDateToKDBX4Time(dt) - val buf = LittleEndianDataOutputStream.writeLongBuf(seconds) + val buf = writeLongBuf(seconds) val b64 = String(Base64.encode(buf, BASE_64_FLAG)) writeObject(name, b64) } 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 e090520d4..bc9e994fa 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 @@ -20,9 +20,9 @@ package com.kunzisoft.keepass.database.file.output import com.kunzisoft.keepass.database.element.entry.EntryKDB -import com.kunzisoft.keepass.stream.LittleEndianDataOutputStream +import com.kunzisoft.keepass.stream.writeIntBuf +import com.kunzisoft.keepass.stream.writeUShortBuf import com.kunzisoft.keepass.utils.DatabaseInputOutputUtils - import java.io.IOException import java.io.OutputStream @@ -52,12 +52,12 @@ class EntryOutputKDB // Group ID mOutputStream.write(GROUPID_FIELD_TYPE) mOutputStream.write(LONG_FOUR) - mOutputStream.write(LittleEndianDataOutputStream.writeIntBuf(mEntry.parent!!.id)) + mOutputStream.write(writeIntBuf(mEntry.parent!!.id)) // Image ID mOutputStream.write(IMAGEID_FIELD_TYPE) mOutputStream.write(LONG_FOUR) - mOutputStream.write(LittleEndianDataOutputStream.writeIntBuf(mEntry.icon.iconId)) + mOutputStream.write(writeIntBuf(mEntry.icon.iconId)) // Title //byte[] title = mEntry.title.getBytes("UTF-8"); @@ -123,28 +123,28 @@ class EntryOutputKDB companion object { // Constants - val UUID_FIELD_TYPE:ByteArray = LittleEndianDataOutputStream.writeUShortBuf(1) - val GROUPID_FIELD_TYPE:ByteArray = LittleEndianDataOutputStream.writeUShortBuf(2) - val IMAGEID_FIELD_TYPE:ByteArray = LittleEndianDataOutputStream.writeUShortBuf(3) - val TITLE_FIELD_TYPE:ByteArray = LittleEndianDataOutputStream.writeUShortBuf(4) - val URL_FIELD_TYPE:ByteArray = LittleEndianDataOutputStream.writeUShortBuf(5) - val USERNAME_FIELD_TYPE:ByteArray = LittleEndianDataOutputStream.writeUShortBuf(6) - val PASSWORD_FIELD_TYPE:ByteArray = LittleEndianDataOutputStream.writeUShortBuf(7) - val ADDITIONAL_FIELD_TYPE:ByteArray = LittleEndianDataOutputStream.writeUShortBuf(8) - val CREATE_FIELD_TYPE:ByteArray = LittleEndianDataOutputStream.writeUShortBuf(9) - val MOD_FIELD_TYPE:ByteArray = LittleEndianDataOutputStream.writeUShortBuf(10) - val ACCESS_FIELD_TYPE:ByteArray = LittleEndianDataOutputStream.writeUShortBuf(11) - val EXPIRE_FIELD_TYPE:ByteArray = LittleEndianDataOutputStream.writeUShortBuf(12) - val BINARY_DESC_FIELD_TYPE:ByteArray = LittleEndianDataOutputStream.writeUShortBuf(13) - val BINARY_DATA_FIELD_TYPE:ByteArray = LittleEndianDataOutputStream.writeUShortBuf(14) - val END_FIELD_TYPE:ByteArray = LittleEndianDataOutputStream.writeUShortBuf(0xFFFF) - val LONG_FOUR:ByteArray = LittleEndianDataOutputStream.writeIntBuf(4) - val UUID_FIELD_SIZE:ByteArray = LittleEndianDataOutputStream.writeIntBuf(16) - val DATE_FIELD_SIZE:ByteArray = LittleEndianDataOutputStream.writeIntBuf(5) + val UUID_FIELD_TYPE:ByteArray = writeUShortBuf(1) + val GROUPID_FIELD_TYPE:ByteArray = writeUShortBuf(2) + val IMAGEID_FIELD_TYPE:ByteArray = writeUShortBuf(3) + val TITLE_FIELD_TYPE:ByteArray = writeUShortBuf(4) + val URL_FIELD_TYPE:ByteArray = writeUShortBuf(5) + val USERNAME_FIELD_TYPE:ByteArray = writeUShortBuf(6) + val PASSWORD_FIELD_TYPE:ByteArray = writeUShortBuf(7) + val ADDITIONAL_FIELD_TYPE:ByteArray = writeUShortBuf(8) + val CREATE_FIELD_TYPE:ByteArray = writeUShortBuf(9) + val MOD_FIELD_TYPE:ByteArray = writeUShortBuf(10) + val ACCESS_FIELD_TYPE:ByteArray = writeUShortBuf(11) + val EXPIRE_FIELD_TYPE:ByteArray = writeUShortBuf(12) + val BINARY_DESC_FIELD_TYPE:ByteArray = writeUShortBuf(13) + val BINARY_DATA_FIELD_TYPE:ByteArray = writeUShortBuf(14) + val END_FIELD_TYPE:ByteArray = writeUShortBuf(0xFFFF) + val LONG_FOUR:ByteArray = writeIntBuf(4) + val UUID_FIELD_SIZE:ByteArray = writeIntBuf(16) + val DATE_FIELD_SIZE:ByteArray = writeIntBuf(5) val IMAGEID_FIELD_SIZE:ByteArray = LONG_FOUR val LEVEL_FIELD_SIZE:ByteArray = LONG_FOUR val FLAGS_FIELD_SIZE:ByteArray = LONG_FOUR - val ZERO_FIELD_SIZE:ByteArray = LittleEndianDataOutputStream.writeIntBuf(0) + val ZERO_FIELD_SIZE:ByteArray = writeIntBuf(0) val ZERO_FIVE:ByteArray = byteArrayOf(0x00, 0x00, 0x00, 0x00, 0x00) } } 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 ccc0db8bf..650794d42 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 @@ -20,9 +20,9 @@ package com.kunzisoft.keepass.database.file.output import com.kunzisoft.keepass.database.element.group.GroupKDB -import com.kunzisoft.keepass.stream.LittleEndianDataOutputStream +import com.kunzisoft.keepass.stream.writeIntBuf +import com.kunzisoft.keepass.stream.writeUShortBuf import com.kunzisoft.keepass.utils.DatabaseInputOutputUtils - import java.io.IOException import java.io.OutputStream @@ -38,7 +38,7 @@ class GroupOutputKDB (private val mGroup: GroupKDB, private val mOutputStream: O // Group ID mOutputStream.write(GROUPID_FIELD_TYPE) mOutputStream.write(GROUPID_FIELD_SIZE) - mOutputStream.write(LittleEndianDataOutputStream.writeIntBuf(mGroup.id)) + mOutputStream.write(writeIntBuf(mGroup.id)) // Name mOutputStream.write(NAME_FIELD_TYPE) @@ -67,17 +67,17 @@ class GroupOutputKDB (private val mGroup: GroupKDB, private val mOutputStream: O // Image ID mOutputStream.write(IMAGEID_FIELD_TYPE) mOutputStream.write(IMAGEID_FIELD_SIZE) - mOutputStream.write(LittleEndianDataOutputStream.writeIntBuf(mGroup.icon.iconId)) + mOutputStream.write(writeIntBuf(mGroup.icon.iconId)) // Level mOutputStream.write(LEVEL_FIELD_TYPE) mOutputStream.write(LEVEL_FIELD_SIZE) - mOutputStream.write(LittleEndianDataOutputStream.writeUShortBuf(mGroup.level)) + mOutputStream.write(writeUShortBuf(mGroup.level)) // Flags mOutputStream.write(FLAGS_FIELD_TYPE) mOutputStream.write(FLAGS_FIELD_SIZE) - mOutputStream.write(LittleEndianDataOutputStream.writeIntBuf(mGroup.flags)) + mOutputStream.write(writeIntBuf(mGroup.flags)) // End mOutputStream.write(END_FIELD_TYPE) @@ -86,23 +86,23 @@ class GroupOutputKDB (private val mGroup: GroupKDB, private val mOutputStream: O companion object { // Constants - val GROUPID_FIELD_TYPE: ByteArray = LittleEndianDataOutputStream.writeUShortBuf(1) - val NAME_FIELD_TYPE:ByteArray = LittleEndianDataOutputStream.writeUShortBuf(2) - val CREATE_FIELD_TYPE:ByteArray = LittleEndianDataOutputStream.writeUShortBuf(3) - val MOD_FIELD_TYPE:ByteArray = LittleEndianDataOutputStream.writeUShortBuf(4) - val ACCESS_FIELD_TYPE:ByteArray = LittleEndianDataOutputStream.writeUShortBuf(5) - val EXPIRE_FIELD_TYPE:ByteArray = LittleEndianDataOutputStream.writeUShortBuf(6) - val IMAGEID_FIELD_TYPE:ByteArray = LittleEndianDataOutputStream.writeUShortBuf(7) - val LEVEL_FIELD_TYPE:ByteArray = LittleEndianDataOutputStream.writeUShortBuf(8) - val FLAGS_FIELD_TYPE:ByteArray = LittleEndianDataOutputStream.writeUShortBuf(9) - val END_FIELD_TYPE:ByteArray = LittleEndianDataOutputStream.writeUShortBuf(0xFFFF) - val LONG_FOUR:ByteArray = LittleEndianDataOutputStream.writeIntBuf(4) + val GROUPID_FIELD_TYPE: ByteArray = writeUShortBuf(1) + val NAME_FIELD_TYPE:ByteArray = writeUShortBuf(2) + val CREATE_FIELD_TYPE:ByteArray = writeUShortBuf(3) + val MOD_FIELD_TYPE:ByteArray = writeUShortBuf(4) + val ACCESS_FIELD_TYPE:ByteArray = writeUShortBuf(5) + val EXPIRE_FIELD_TYPE:ByteArray = writeUShortBuf(6) + val IMAGEID_FIELD_TYPE:ByteArray = writeUShortBuf(7) + val LEVEL_FIELD_TYPE:ByteArray = writeUShortBuf(8) + val FLAGS_FIELD_TYPE:ByteArray = writeUShortBuf(9) + val END_FIELD_TYPE:ByteArray = writeUShortBuf(0xFFFF) + val LONG_FOUR:ByteArray = writeIntBuf(4) val GROUPID_FIELD_SIZE:ByteArray = LONG_FOUR - val DATE_FIELD_SIZE:ByteArray = LittleEndianDataOutputStream.writeIntBuf(5) + val DATE_FIELD_SIZE:ByteArray = writeIntBuf(5) val IMAGEID_FIELD_SIZE:ByteArray = LONG_FOUR - val LEVEL_FIELD_SIZE:ByteArray = LittleEndianDataOutputStream.writeIntBuf(2) + val LEVEL_FIELD_SIZE:ByteArray = writeIntBuf(2) val FLAGS_FIELD_SIZE:ByteArray = LONG_FOUR - val ZERO_FIELD_SIZE:ByteArray = LittleEndianDataOutputStream.writeIntBuf(0) + val ZERO_FIELD_SIZE:ByteArray = writeIntBuf(0) } } 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 4b82e4323..82314b782 100644 --- a/app/src/main/java/com/kunzisoft/keepass/stream/HashedBlockInputStream.kt +++ b/app/src/main/java/com/kunzisoft/keepass/stream/HashedBlockInputStream.kt @@ -20,12 +20,11 @@ package com.kunzisoft.keepass.stream import com.kunzisoft.keepass.utils.DatabaseInputOutputUtils - import java.io.IOException import java.io.InputStream import java.security.MessageDigest import java.security.NoSuchAlgorithmException -import java.util.Arrays +import java.util.* class HashedBlockInputStream(inputStream: InputStream) : InputStream() { @@ -92,7 +91,7 @@ class HashedBlockInputStream(inputStream: InputStream) : InputStream() { throw IOException("Invalid data format") } - val bufferSize = LittleEndianDataInputStream.readInt(baseStream) + val bufferSize = readInt(baseStream) 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 3953a1be8..d4b51454f 100644 --- a/app/src/main/java/com/kunzisoft/keepass/stream/HmacBlockInputStream.kt +++ b/app/src/main/java/com/kunzisoft/keepass/stream/HmacBlockInputStream.kt @@ -20,13 +20,11 @@ package com.kunzisoft.keepass.stream import com.kunzisoft.keepass.utils.DatabaseInputOutputUtils - import java.io.IOException import java.io.InputStream import java.security.InvalidKeyException import java.security.NoSuchAlgorithmException -import java.util.Arrays - +import java.util.* import javax.crypto.Mac import javax.crypto.spec.SecretKeySpec @@ -95,12 +93,12 @@ class HmacBlockInputStream(baseStream: InputStream, private val verify: Boolean, throw IOException("File corrupted") } - val pbBlockIndex = LittleEndianDataOutputStream.writeLongBuf(blockIndex) + val pbBlockIndex = writeLongBuf(blockIndex) val pbBlockSize = baseStream.readBytes(4) if (pbBlockSize == null || pbBlockSize.size != 4) { throw IOException("File corrupted") } - val blockSize = LittleEndianDataInputStream.readInt(pbBlockSize, 0) + val blockSize = readInt(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 a84f83d93..215cac4cb 100644 --- a/app/src/main/java/com/kunzisoft/keepass/stream/HmacBlockOutputStream.java +++ b/app/src/main/java/com/kunzisoft/keepass/stream/HmacBlockOutputStream.java @@ -27,6 +27,9 @@ 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; + public class HmacBlockOutputStream extends OutputStream { private static final int DEFAULT_BUFFER_SIZE = 1024 * 1024; private LittleEndianDataOutputStream baseStream; @@ -89,8 +92,8 @@ public class HmacBlockOutputStream extends OutputStream { } private void WriteSafeBlock() throws IOException { - byte[] bufBlockIndex = LittleEndianDataOutputStream.writeLongBuf(blockIndex); - byte[] blockSizeBuf = LittleEndianDataOutputStream.writeIntBuf(bufferPos); + byte[] bufBlockIndex = writeLongBuf(blockIndex); + byte[] blockSizeBuf = writeIntBuf(bufferPos); byte[] blockHmac; byte[] blockKey = HmacBlockStream.GetHmacKey64(key, blockIndex); diff --git a/app/src/main/java/com/kunzisoft/keepass/stream/LittleEndianDataInputStream.java b/app/src/main/java/com/kunzisoft/keepass/stream/LittleEndianDataInputStream.java deleted file mode 100644 index 4b631b0a5..000000000 --- a/app/src/main/java/com/kunzisoft/keepass/stream/LittleEndianDataInputStream.java +++ /dev/null @@ -1,176 +0,0 @@ -/* - * Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft. - * - * This file is part of KeePass DX. - * - * KeePass DX is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * KeePass DX is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with KeePass DX. If not, see . - * - */ -package com.kunzisoft.keepass.stream; - -import java.io.IOException; -import java.io.InputStream; -import java.util.UUID; - - -/** - * Little endian version of the DataInputStream - * @author bpellin - */ -public class LittleEndianDataInputStream extends InputStream { - - private static final long INT_TO_LONG_MASK = 0xffffffffL; - - private InputStream baseStream; - - public LittleEndianDataInputStream(InputStream inputStream) { - baseStream = inputStream; - } - - /** - * Read a 32-bit value and return it as a long, so that it can - * be interpreted as an unsigned integer. - */ - public long readUInt() throws IOException { - return readInt(baseStream) & INT_TO_LONG_MASK; - } - - public int readInt() throws IOException { - return readInt(baseStream); - } - - public long readLong() throws IOException { - byte[] buf = readBytes(8); - return readLong(buf, 0); - } - - @Override - public int available() throws IOException { - return baseStream.available(); - } - - @Override - public void close() throws IOException { - baseStream.close(); - } - - @Override - public void mark(int readlimit) { - baseStream.mark(readlimit); - } - - @Override - public boolean markSupported() { - return baseStream.markSupported(); - } - - @Override - public int read() throws IOException { - return baseStream.read(); - } - - @Override - public int read(byte[] b, int offset, int length) throws IOException { - return baseStream.read(b, offset, length); - } - - @Override - public int read(byte[] b) throws IOException { - return baseStream.read(b); - } - - @Override - public synchronized void reset() throws IOException { - baseStream.reset(); - } - - @Override - public long skip(long n) throws IOException { - return baseStream.skip(n); - } - - public byte[] readBytes(int length) throws IOException { - // TODO Exception max length < buffer size - byte[] buf = new byte[length]; - - int count = 0; - while ( count < length ) { - int read = read(buf, count, length - count); - - // Reached end - if ( read == -1 ) { - // Stop early - byte[] early = new byte[count]; - System.arraycopy(buf, 0, early, 0, count); - return early; - } - - count += read; - } - - return buf; - } - - public int readUShort() throws IOException { - byte[] buf = new byte[2]; - baseStream.read(buf, 0, 2); - return readUShort(buf, 0); - } - - /** - * Read an unsigned 16-bit value. - */ - public static int readUShort( byte[] buf, int offset ) { - return (buf[offset] & 0xFF) + ((buf[offset + 1] & 0xFF) << 8); - } - - public static long readLong(byte[] buf, int offset ) { - return ((long)buf[offset] & 0xFF) + (((long)buf[offset + 1] & 0xFF) << 8) - + (((long)buf[offset + 2] & 0xFF) << 16) + (((long)buf[offset + 3] & 0xFF) << 24) - + (((long)buf[offset + 4] & 0xFF) << 32) + (((long)buf[offset + 5] & 0xFF) << 40) - + (((long)buf[offset + 6] & 0xFF) << 48) + (((long)buf[offset + 7] & 0xFF) << 56); - } - - public static long readUInt(byte[] buf, int offset ) { - return (readInt(buf, offset) & INT_TO_LONG_MASK); - } - - public static int readInt(InputStream is) throws IOException { - byte[] buf = new byte[4]; - is.read(buf, 0, 4); - return readInt(buf, 0); - } - - /** - * Read a 32-bit value. - */ - public static int readInt(byte[] buf, int offset ) { - return (buf[offset] & 0xFF) + ((buf[offset + 1] & 0xFF) << 8) + ((buf[offset + 2] & 0xFF) << 16) - + ((buf[offset + 3] & 0xFF) << 24); - } - - public static UUID readUuid( byte[] buf, int offset ) { - long lsb = 0; - for (int i = 15; i >= 8; i--) { - lsb = (lsb << 8) | (buf[i + offset] & 0xff); - } - - long msb = 0; - for (int i = 7; i >= 0; i--) { - msb = (msb << 8) | (buf[i + offset] & 0xff); - } - - return new UUID(msb, lsb); - } -} diff --git a/app/src/main/java/com/kunzisoft/keepass/stream/LittleEndianDataInputStream.kt b/app/src/main/java/com/kunzisoft/keepass/stream/LittleEndianDataInputStream.kt new file mode 100644 index 000000000..3aeb4a2bb --- /dev/null +++ b/app/src/main/java/com/kunzisoft/keepass/stream/LittleEndianDataInputStream.kt @@ -0,0 +1,125 @@ +/* + * Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft. + * + * This file is part of KeePass DX. + * + * KeePass DX is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * KeePass DX is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with KeePass DX. If not, see . + * + */ +package com.kunzisoft.keepass.stream + +import java.io.IOException +import java.io.InputStream + +/** + * Little endian version of the DataInputStream + * @author bpellin + */ +class LittleEndianDataInputStream(private val baseStream: InputStream) : InputStream() { + + /** + * 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(): Long { + return readUInt(baseStream) + } + + @Throws(IOException::class) + fun readInt(): Int { + return readInt(baseStream) + } + + @Throws(IOException::class) + fun readLong(): Long { + val buf = readBytes(8) + return readLong(buf, 0) + } + + @Throws(IOException::class) + fun readUShort(): Int { + val buf = ByteArray(2) + if (baseStream.read(buf, 0, 2) != 2) + throw IOException("Unable to read UShort value") + return readUShort(buf, 0) + } + + @Throws(IOException::class) + override fun available(): Int { + return baseStream.available() + } + + @Throws(IOException::class) + override fun close() { + baseStream.close() + } + + override fun mark(readlimit: Int) { + baseStream.mark(readlimit) + } + + override fun markSupported(): Boolean { + return baseStream.markSupported() + } + + @Throws(IOException::class) + override fun read(): Int { + return baseStream.read() + } + + @Throws(IOException::class) + override fun read(b: ByteArray, offset: Int, length: Int): Int { + return baseStream.read(b, offset, length) + } + + @Throws(IOException::class) + override fun read(b: ByteArray): Int { + return baseStream.read(b) + } + + @Synchronized + @Throws(IOException::class) + override fun reset() { + baseStream.reset() + } + + @Throws(IOException::class) + override fun skip(n: Long): Long { + return baseStream.skip(n) + } + + @Throws(IOException::class) + fun readBytes(length: Int): ByteArray { + // TODO Exception max length < buffer size + val buf = ByteArray(length) + + var count = 0 + while (count < length) { + val read = read(buf, count, length - count) + + // Reached end + if (read == -1) { + // Stop early + val early = ByteArray(count) + System.arraycopy(buf, 0, early, 0, count) + return early + } + + count += read + } + + return buf + } +} diff --git a/app/src/main/java/com/kunzisoft/keepass/stream/LittleEndianDataOutputStream.java b/app/src/main/java/com/kunzisoft/keepass/stream/LittleEndianDataOutputStream.java deleted file mode 100644 index cd83cea98..000000000 --- a/app/src/main/java/com/kunzisoft/keepass/stream/LittleEndianDataOutputStream.java +++ /dev/null @@ -1,144 +0,0 @@ -/* - * Copyright 2017 Brian Pellin, Jeremy Jamet / Kunzisoft. - * - * This file is part of KeePass DX. - * - * KeePass DX is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * KeePass DX is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with KeePass DX. If not, see . - * - */ -package com.kunzisoft.keepass.stream; - -import java.io.IOException; -import java.io.OutputStream; - - -/** Little Endian version of the DataOutputStream - * @author bpellin - * - */ -public class LittleEndianDataOutputStream extends OutputStream { - - private OutputStream baseStream; - - public LittleEndianDataOutputStream(OutputStream out) { - baseStream = out; - } - - public void writeUInt(long uint) throws IOException { - baseStream.write(LittleEndianDataOutputStream.writeIntBuf((int) uint)); - } - - @Override - public void close() throws IOException { - baseStream.close(); - } - - @Override - public void flush() throws IOException { - baseStream.flush(); - } - - @Override - public void write(byte[] buffer, int offset, int count) throws IOException { - baseStream.write(buffer, offset, count); - } - - @Override - public void write(byte[] buffer) throws IOException { - baseStream.write(buffer); - } - - @Override - public void write(int oneByte) throws IOException { - baseStream.write(oneByte); - } - - public void writeLong(long val) throws IOException { - byte[] buf = new byte[8]; - - writeLong(val, buf, 0); - baseStream.write(buf); - } - - public void writeInt(int val) throws IOException { - byte[] buf = new byte[4]; - writeInt(val, buf, 0); - - baseStream.write(buf); - } - - public void writeUShort(int val) throws IOException { - byte[] buf = new byte[2]; - writeUShort(val, buf, 0); - baseStream.write(buf); - } - - public static byte[] writeIntBuf(int val) { - byte[] buf = new byte[4]; - writeInt(val, buf, 0); - - return buf; - } - - public static byte[] writeUShortBuf(int val) { - byte[] buf = new byte[2]; - - writeUShort(val, buf, 0); - - return buf; - } - - /** Write an unsigned 16-bit value - * - * @param val - * @param buf - * @param offset - */ - public static void writeUShort(int val, byte[] buf, int offset) { - buf[offset + 0] = (byte)(val & 0x00FF); - buf[offset + 1] = (byte)((val & 0xFF00) >> 8); - } - - /** - * Write a 32-bit value. - * - * @param val - * @param buf - * @param offset - */ - public static void writeInt( int val, byte[] buf, int offset ) { - buf[offset + 0] = (byte)(val & 0xFF); - buf[offset + 1] = (byte)((val >>> 8) & 0xFF); - buf[offset + 2] = (byte)((val >>> 16) & 0xFF); - buf[offset + 3] = (byte)((val >>> 24) & 0xFF); - } - - public static byte[] writeLongBuf(long val) { - byte[] buf = new byte[8]; - writeLong(val, buf, 0); - return buf; - } - - public static void writeLong( long val, byte[] buf, int offset ) { - buf[offset + 0] = (byte)(val & 0xFF); - buf[offset + 1] = (byte)((val >>> 8) & 0xFF); - buf[offset + 2] = (byte)((val >>> 16) & 0xFF); - buf[offset + 3] = (byte)((val >>> 24) & 0xFF); - buf[offset + 4] = (byte)((val >>> 32) & 0xFF); - buf[offset + 5] = (byte)((val >>> 40) & 0xFF); - buf[offset + 6] = (byte)((val >>> 48) & 0xFF); - buf[offset + 7] = (byte)((val >>> 56) & 0xFF); - } - -} diff --git a/app/src/main/java/com/kunzisoft/keepass/stream/LittleEndianDataOutputStream.kt b/app/src/main/java/com/kunzisoft/keepass/stream/LittleEndianDataOutputStream.kt new file mode 100644 index 000000000..9c64c61df --- /dev/null +++ b/app/src/main/java/com/kunzisoft/keepass/stream/LittleEndianDataOutputStream.kt @@ -0,0 +1,86 @@ +/* + * Copyright 2019 Brian Pellin, Jeremy Jamet / Kunzisoft. + * + * This file is part of KeePass DX. + * + * KeePass DX is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * KeePass DX is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with KeePass DX. If not, see . + * + */ +package com.kunzisoft.keepass.stream + +import java.io.IOException +import java.io.OutputStream + +import com.kunzisoft.keepass.stream.writeIntBuf + + +/** + * Little Endian version of the DataOutputStream + * @author bpellin + */ +class LittleEndianDataOutputStream(private val baseStream: OutputStream) : OutputStream() { + + @Throws(IOException::class) + fun writeUInt(uint: Long) { + baseStream.write(writeIntBuf(uint.toInt())) + } + + @Throws(IOException::class) + override fun close() { + baseStream.close() + } + + @Throws(IOException::class) + override fun flush() { + baseStream.flush() + } + + @Throws(IOException::class) + override fun write(buffer: ByteArray, offset: Int, count: Int) { + baseStream.write(buffer, offset, count) + } + + @Throws(IOException::class) + override fun write(buffer: ByteArray) { + baseStream.write(buffer) + } + + @Throws(IOException::class) + override fun write(oneByte: Int) { + baseStream.write(oneByte) + } + + @Throws(IOException::class) + fun writeLong(value: Long) { + val buf = ByteArray(8) + + writeLong(value, buf, 0) + baseStream.write(buf) + } + + @Throws(IOException::class) + fun writeInt(value: Int) { + val buf = ByteArray(4) + writeInt(value, buf, 0) + + baseStream.write(buf) + } + + @Throws(IOException::class) + fun writeUShort(value: Int) { + val buf = ByteArray(2) + writeUShort(value, buf, 0) + baseStream.write(buf) + } +} diff --git a/app/src/main/java/com/kunzisoft/keepass/stream/ReadBytes.kt b/app/src/main/java/com/kunzisoft/keepass/stream/ReadBytes.kt index f1b0a150c..43f10057c 100644 --- a/app/src/main/java/com/kunzisoft/keepass/stream/ReadBytes.kt +++ b/app/src/main/java/com/kunzisoft/keepass/stream/ReadBytes.kt @@ -21,6 +21,8 @@ package com.kunzisoft.keepass.stream 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. @@ -71,3 +73,113 @@ fun InputStream.readBytes(length: Int, bufferSize: Int, readBytes: (bytesRead: B offset += read } } + +/** + * Read an unsigned 16-bit value. + */ +fun readUShort(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)) +} + + +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) +} + +/** + * Read a 32-bit value. + */ +fun readInt(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 { + var lsb: Long = 0 + for (i in 15 downTo 8) { + lsb = lsb shl 8 or (buf[i + offset].toLong() and 0xff) + } + + var msb: Long = 0 + for (i in 7 downTo 0) { + msb = msb shl 8 or (buf[i + offset].toLong() and 0xff) + } + + return UUID(msb, lsb) +} + +fun writeIntBuf(value: Int): ByteArray { + val buf = ByteArray(4) + writeInt(value, buf, 0) + return buf +} + +fun writeUShortBuf(value: Int): ByteArray { + val buf = ByteArray(2) + writeUShort(value, buf, 0) + return buf +} + +/** + * Write an unsigned 16-bit value + */ +fun writeUShort(value: Int, buf: ByteArray, offset: Int) { + buf[offset + 0] = (value and 0x00FF).toByte() + buf[offset + 1] = (value and 0xFF00 shr 8).toByte() +} + +/** + * Write a 32-bit value. + */ +fun writeInt(value: Int, buf: ByteArray, offset: Int) { + buf[offset + 0] = (value and 0xFF).toByte() + buf[offset + 1] = (value.ushr(8) and 0xFF).toByte() + buf[offset + 2] = (value.ushr(16) and 0xFF).toByte() + buf[offset + 3] = (value.ushr(24) and 0xFF).toByte() +} + +fun writeLongBuf(value: Long): ByteArray { + val buf = ByteArray(8) + writeLong(value, buf, 0) + return buf +} + +fun writeLong(value: Long, buf: ByteArray, offset: Int) { + buf[offset + 0] = (value and 0xFF).toByte() + buf[offset + 1] = (value.ushr(8) and 0xFF).toByte() + buf[offset + 2] = (value.ushr(16) and 0xFF).toByte() + buf[offset + 3] = (value.ushr(24) and 0xFF).toByte() + buf[offset + 4] = (value.ushr(32) and 0xFF).toByte() + buf[offset + 5] = (value.ushr(40) and 0xFF).toByte() + buf[offset + 6] = (value.ushr(48) and 0xFF).toByte() + buf[offset + 7] = (value.ushr(56) and 0xFF).toByte() +} 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 38dc5819d..8cf93c5d6 100644 --- a/app/src/main/java/com/kunzisoft/keepass/utils/DatabaseInputOutputUtils.kt +++ b/app/src/main/java/com/kunzisoft/keepass/utils/DatabaseInputOutputUtils.kt @@ -43,8 +43,9 @@ 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.LittleEndianDataInputStream -import com.kunzisoft.keepass.stream.LittleEndianDataOutputStream +import com.kunzisoft.keepass.stream.readUuid +import com.kunzisoft.keepass.stream.writeIntBuf +import com.kunzisoft.keepass.stream.writeLong import java.io.IOException import java.io.OutputStream import java.nio.charset.Charset @@ -75,14 +76,14 @@ object DatabaseInputOutputUtils { /** * Write an unsigned byte */ - fun writeUByte(`val`: Int, buf: ByteArray, offset: Int) { - buf[offset] = (`val` and 0xFF).toByte() + fun writeUByte(value: Int, buf: ByteArray, offset: Int) { + buf[offset] = (value and 0xFF).toByte() } - fun writeUByte(`val`: Int): Byte { + fun writeUByte(value: Int): Byte { val buf = ByteArray(1) - writeUByte(`val`, buf, 0) + writeUByte(value, buf, 0) return buf[0] } @@ -113,7 +114,7 @@ object DatabaseInputOutputUtils { var str = string if (str == null) { // Write out a null character - os.write(LittleEndianDataOutputStream.writeIntBuf(1)) + os.write(writeIntBuf(1)) os.write(0x00) return 0 } @@ -125,7 +126,7 @@ object DatabaseInputOutputUtils { val initial = str.toByteArray(defaultCharset) val length = initial.size + 1 - os.write(LittleEndianDataOutputStream.writeIntBuf(length)) + os.write(writeIntBuf(length)) os.write(initial) os.write(0x00) @@ -199,7 +200,7 @@ object DatabaseInputOutputUtils { fun writePassword(str: String, os: OutputStream): Int { val initial = str.toByteArray(defaultCharset) val length = initial.size + 1 - os.write(LittleEndianDataOutputStream.writeIntBuf(length)) + os.write(writeIntBuf(length)) os.write(initial) os.write(0x00) return length @@ -213,7 +214,7 @@ object DatabaseInputOutputUtils { @Throws(IOException::class) fun writeBytes(data: ByteArray?, dataLen: Int, os: OutputStream): Int { - os.write(LittleEndianDataOutputStream.writeIntBuf(dataLen)) + os.write(writeIntBuf(dataLen)) if (data != null) { os.write(data) } @@ -221,13 +222,13 @@ object DatabaseInputOutputUtils { } fun bytesToUuid(buf: ByteArray): UUID { - return LittleEndianDataInputStream.readUuid(buf, 0) + return readUuid(buf, 0) } fun uuidToBytes(uuid: UUID): ByteArray { val buf = ByteArray(16) - LittleEndianDataOutputStream.writeLong(uuid.mostSignificantBits, buf, 0) - LittleEndianDataOutputStream.writeLong(uuid.leastSignificantBits, buf, 8) + writeLong(uuid.mostSignificantBits, buf, 0) + writeLong(uuid.leastSignificantBits, buf, 8) return buf } } 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 bfd2fc3ad..15d0783e8 100644 --- a/app/src/main/java/com/kunzisoft/keepass/utils/VariantDictionary.java +++ b/app/src/main/java/com/kunzisoft/keepass/utils/VariantDictionary.java @@ -21,12 +21,16 @@ package com.kunzisoft.keepass.utils; import com.kunzisoft.keepass.stream.LittleEndianDataInputStream; import com.kunzisoft.keepass.stream.LittleEndianDataOutputStream; +import com.kunzisoft.keepass.stream.ReadBytesKt; 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; + public class VariantDictionary { private static final int VdVersion = 0x0100; private static final int VdmCritical = 0xFF00; @@ -121,12 +125,12 @@ public class VariantDictionary { switch (bType) { case VdType.UInt32: if (valueLen == 4) { - d.setUInt32(name, LittleEndianDataInputStream.readUInt(valueBuf, 0)); + d.setUInt32(name, readUInt(valueBuf, 0)); } break; case VdType.UInt64: if (valueLen == 8) { - d.setUInt64(name, LittleEndianDataInputStream.readLong(valueBuf, 0)); + d.setUInt64(name, readLong(valueBuf, 0)); } break; case VdType.Bool: @@ -136,12 +140,12 @@ public class VariantDictionary { break; case VdType.Int32: if (valueLen == 4) { - d.setInt32(name, LittleEndianDataInputStream.readInt(valueBuf, 0)); + d.setInt32(name, ReadBytesKt.readInt(valueBuf, 0)); } break; case VdType.Int64: if (valueLen == 8) { - d.setInt64(name, LittleEndianDataInputStream.readLong(valueBuf, 0)); + d.setInt64(name, readLong(valueBuf, 0)); } break; case VdType.String: