Revert "fix: Base64 encoder as external library"

This reverts commit 1b96747187.
This commit is contained in:
J-Jamet 2023-05-14 21:56:11 +02:00
parent c2cc0a77ab
commit 111a77d984
5 changed files with 42 additions and 33 deletions

View file

@ -25,13 +25,13 @@ import android.content.IntentFilter
import android.content.ServiceConnection import android.content.ServiceConnection
import android.net.Uri import android.net.Uri
import android.os.IBinder import android.os.IBinder
import android.util.Base64
import android.util.Log import android.util.Log
import com.kunzisoft.keepass.model.CipherEncryptDatabase import com.kunzisoft.keepass.model.CipherEncryptDatabase
import com.kunzisoft.keepass.services.AdvancedUnlockNotificationService import com.kunzisoft.keepass.services.AdvancedUnlockNotificationService
import com.kunzisoft.keepass.settings.PreferencesUtil import com.kunzisoft.keepass.settings.PreferencesUtil
import com.kunzisoft.keepass.utils.IOActionTask import com.kunzisoft.keepass.utils.IOActionTask
import com.kunzisoft.keepass.utils.SingletonHolderParameter import com.kunzisoft.keepass.utils.SingletonHolderParameter
import org.apache.commons.codec.binary.Base64
import java.util.LinkedList import java.util.LinkedList
class CipherDatabaseAction(context: Context) { class CipherDatabaseAction(context: Context) {
@ -136,11 +136,13 @@ class CipherDatabaseAction(context: Context) {
mBinder?.getCipherDatabase(databaseUri)?.let { cipherDatabaseEntity -> mBinder?.getCipherDatabase(databaseUri)?.let { cipherDatabaseEntity ->
cipherDatabase = CipherEncryptDatabase().apply { cipherDatabase = CipherEncryptDatabase().apply {
this.databaseUri = Uri.parse(cipherDatabaseEntity.databaseUri) this.databaseUri = Uri.parse(cipherDatabaseEntity.databaseUri)
this.encryptedValue = Base64.decodeBase64( this.encryptedValue = Base64.decode(
cipherDatabaseEntity.encryptedValue cipherDatabaseEntity.encryptedValue,
Base64.NO_WRAP
) )
this.specParameters = Base64.decodeBase64( this.specParameters = Base64.decode(
cipherDatabaseEntity.specParameters cipherDatabaseEntity.specParameters,
Base64.NO_WRAP
) )
} }
} }
@ -153,11 +155,13 @@ class CipherDatabaseAction(context: Context) {
?.let { cipherDatabaseEntity -> ?.let { cipherDatabaseEntity ->
CipherEncryptDatabase().apply { CipherEncryptDatabase().apply {
this.databaseUri = Uri.parse(cipherDatabaseEntity.databaseUri) this.databaseUri = Uri.parse(cipherDatabaseEntity.databaseUri)
this.encryptedValue = Base64.decodeBase64( this.encryptedValue = Base64.decode(
cipherDatabaseEntity.encryptedValue cipherDatabaseEntity.encryptedValue,
Base64.NO_WRAP
) )
this.specParameters = Base64.decodeBase64( this.specParameters = Base64.decode(
cipherDatabaseEntity.specParameters cipherDatabaseEntity.specParameters,
Base64.NO_WRAP
) )
} }
} }
@ -182,8 +186,8 @@ class CipherDatabaseAction(context: Context) {
val cipherDatabaseEntity = CipherDatabaseEntity( val cipherDatabaseEntity = CipherDatabaseEntity(
databaseUri.toString(), databaseUri.toString(),
Base64.encodeBase64String(cipherEncryptDatabase.encryptedValue), Base64.encodeToString(cipherEncryptDatabase.encryptedValue, Base64.NO_WRAP),
Base64.encodeBase64String(cipherEncryptDatabase.specParameters), Base64.encodeToString(cipherEncryptDatabase.specParameters, Base64.NO_WRAP),
) )
if (useTempDao) { if (useTempDao) {

View file

@ -20,8 +20,10 @@ package com.kunzisoft.keepass.database.element
import android.os.Parcel import android.os.Parcel
import android.os.Parcelable import android.os.Parcelable
import android.util.Base64
import android.util.Log import android.util.Log
import com.kunzisoft.encrypt.HashManager import com.kunzisoft.encrypt.HashManager
import com.kunzisoft.keepass.database.element.database.DatabaseKDBX
import com.kunzisoft.keepass.hardware.HardwareKey import com.kunzisoft.keepass.hardware.HardwareKey
import com.kunzisoft.keepass.utils.StringUtil.removeSpaceChars import com.kunzisoft.keepass.utils.StringUtil.removeSpaceChars
import com.kunzisoft.keepass.utils.StringUtil.toHexString import com.kunzisoft.keepass.utils.StringUtil.toHexString
@ -29,7 +31,6 @@ import com.kunzisoft.keepass.utils.readByteArrayCompat
import com.kunzisoft.keepass.utils.readEnum import com.kunzisoft.keepass.utils.readEnum
import com.kunzisoft.keepass.utils.writeByteArrayCompat import com.kunzisoft.keepass.utils.writeByteArrayCompat
import com.kunzisoft.keepass.utils.writeEnum import com.kunzisoft.keepass.utils.writeEnum
import org.apache.commons.codec.binary.Base64
import org.apache.commons.codec.binary.Hex import org.apache.commons.codec.binary.Hex
import org.w3c.dom.Node import org.w3c.dom.Node
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
@ -211,7 +212,9 @@ data class MasterCredential(var password: String? = null,
when (xmlKeyFileVersion) { when (xmlKeyFileVersion) {
1F -> { 1F -> {
// No hash in KeyFile XML version 1 // No hash in KeyFile XML version 1
return Base64.decodeBase64(dataString) return Base64.decode(dataString,
DatabaseKDBX.BASE_64_FLAG
)
} }
2F -> { 2F -> {
return if (hashString != null return if (hashString != null

View file

@ -19,6 +19,7 @@
*/ */
package com.kunzisoft.keepass.database.element.database package com.kunzisoft.keepass.database.element.database
import android.util.Base64
import android.util.Log import android.util.Log
import com.kunzisoft.encrypt.HashManager import com.kunzisoft.encrypt.HashManager
import com.kunzisoft.keepass.database.crypto.EncryptionAlgorithm import com.kunzisoft.keepass.database.crypto.EncryptionAlgorithm
@ -885,9 +886,12 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
} }
companion object { companion object {
val TYPE = DatabaseKDBX::class.java
private val TAG = DatabaseKDBX::class.java.name private val TAG = DatabaseKDBX::class.java.name
private const val DEFAULT_HISTORY_MAX_ITEMS = 10 // -1 unlimited private const val DEFAULT_HISTORY_MAX_ITEMS = 10 // -1 unlimited
private const val DEFAULT_HISTORY_MAX_SIZE = (6 * 1024 * 1024).toLong() // -1 unlimited private const val DEFAULT_HISTORY_MAX_SIZE = (6 * 1024 * 1024).toLong() // -1 unlimited
const val BASE_64_FLAG = Base64.NO_WRAP
} }
} }

View file

@ -19,6 +19,7 @@
*/ */
package com.kunzisoft.keepass.database.file.input package com.kunzisoft.keepass.database.file.input
import android.util.Base64
import android.util.Log import android.util.Log
import com.kunzisoft.encrypt.StreamCipher import com.kunzisoft.encrypt.StreamCipher
import com.kunzisoft.keepass.database.crypto.CipherEngine import com.kunzisoft.keepass.database.crypto.CipherEngine
@ -28,6 +29,7 @@ import com.kunzisoft.keepass.database.element.*
import com.kunzisoft.keepass.database.element.binary.BinaryData import com.kunzisoft.keepass.database.element.binary.BinaryData
import com.kunzisoft.keepass.database.element.database.CompressionAlgorithm import com.kunzisoft.keepass.database.element.database.CompressionAlgorithm
import com.kunzisoft.keepass.database.element.database.DatabaseKDBX import com.kunzisoft.keepass.database.element.database.DatabaseKDBX
import com.kunzisoft.keepass.database.element.database.DatabaseKDBX.Companion.BASE_64_FLAG
import com.kunzisoft.keepass.database.element.database.DatabaseVersioned import com.kunzisoft.keepass.database.element.database.DatabaseVersioned
import com.kunzisoft.keepass.database.element.entry.EntryKDBX import com.kunzisoft.keepass.database.element.entry.EntryKDBX
import com.kunzisoft.keepass.database.element.group.GroupKDBX import com.kunzisoft.keepass.database.element.group.GroupKDBX
@ -45,7 +47,6 @@ import com.kunzisoft.keepass.stream.HashedBlockInputStream
import com.kunzisoft.keepass.stream.HmacBlockInputStream import com.kunzisoft.keepass.stream.HmacBlockInputStream
import com.kunzisoft.keepass.tasks.ProgressTaskUpdater import com.kunzisoft.keepass.tasks.ProgressTaskUpdater
import com.kunzisoft.keepass.utils.* import com.kunzisoft.keepass.utils.*
import org.apache.commons.codec.binary.Base64
import org.xmlpull.v1.XmlPullParser import org.xmlpull.v1.XmlPullParser
import org.xmlpull.v1.XmlPullParserException import org.xmlpull.v1.XmlPullParserException
import org.xmlpull.v1.XmlPullParserFactory import org.xmlpull.v1.XmlPullParserFactory
@ -54,10 +55,7 @@ import java.io.InputStream
import java.io.UnsupportedEncodingException import java.io.UnsupportedEncodingException
import java.nio.charset.Charset import java.nio.charset.Charset
import java.text.ParseException import java.text.ParseException
import java.util.Arrays import java.util.*
import java.util.Date
import java.util.Stack
import java.util.UUID
import java.util.zip.GZIPInputStream import java.util.zip.GZIPInputStream
import javax.crypto.Cipher import javax.crypto.Cipher
import javax.crypto.CipherInputStream import javax.crypto.CipherInputStream
@ -348,7 +346,7 @@ class DatabaseInputKDBX(database: DatabaseKDBX)
} else if (name.equals(DatabaseKDBXXML.ElemHeaderHash, ignoreCase = true)) { } else if (name.equals(DatabaseKDBXXML.ElemHeaderHash, ignoreCase = true)) {
val encodedHash = readString(xpp) val encodedHash = readString(xpp)
if (encodedHash.isNotEmpty() && hashOfHeader != null) { if (encodedHash.isNotEmpty() && hashOfHeader != null) {
val hash = Base64.decodeBase64(encodedHash) val hash = Base64.decode(encodedHash, BASE_64_FLAG)
if (!Arrays.equals(hash, hashOfHeader)) { if (!Arrays.equals(hash, hashOfHeader)) {
throw DatabaseInputException() throw DatabaseInputException()
} }
@ -434,7 +432,7 @@ class DatabaseInputKDBX(database: DatabaseKDBX)
} else if (name.equals(DatabaseKDBXXML.ElemCustomIconItemData, ignoreCase = true)) { } else if (name.equals(DatabaseKDBXXML.ElemCustomIconItemData, ignoreCase = true)) {
val strData = readString(xpp) val strData = readString(xpp)
if (strData.isNotEmpty()) { if (strData.isNotEmpty()) {
customIconData = Base64.decodeBase64(strData) customIconData = Base64.decode(strData, BASE_64_FLAG)
} }
} else if (name.equals(DatabaseKDBXXML.ElemName, ignoreCase = true)) { } else if (name.equals(DatabaseKDBXXML.ElemName, ignoreCase = true)) {
customIconName = readString(xpp) customIconName = readString(xpp)
@ -838,7 +836,7 @@ class DatabaseInputKDBX(database: DatabaseKDBX)
// Catch with null test below // Catch with null test below
} }
} else { } else {
var buf = Base64.decodeBase64(sDate) var buf = Base64.decode(sDate, BASE_64_FLAG)
if (buf.size != 8) { if (buf.size != 8) {
val buf8 = ByteArray(8) val buf8 = ByteArray(8)
System.arraycopy(buf, 0, buf8, 0, min(buf.size, 8)) System.arraycopy(buf, 0, buf8, 0, min(buf.size, 8))
@ -904,7 +902,7 @@ class DatabaseInputKDBX(database: DatabaseKDBX)
} }
return try { return try {
val buf = Base64.decodeBase64(encoded) val buf = Base64.decode(encoded, BASE_64_FLAG)
bytes16ToUuid(buf) bytes16ToUuid(buf)
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "Unable to read base 64 UUID, create a random one", e) Log.e(TAG, "Unable to read base 64 UUID, create a random one", e)
@ -1024,7 +1022,7 @@ class DatabaseInputKDBX(database: DatabaseKDBX)
isRAMSufficient.invoke(base64.length.toLong()), compressed, protected, binaryId) isRAMSufficient.invoke(base64.length.toLong()), compressed, protected, binaryId)
try { try {
binaryAttachment.getOutputDataStream(mDatabase.binaryCache).use { outputStream -> binaryAttachment.getOutputDataStream(mDatabase.binaryCache).use { outputStream ->
outputStream.write(Base64.decodeBase64(base64)) outputStream.write(Base64.decode(base64, BASE_64_FLAG))
} }
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "Unable to read base 64 attachment", e) Log.e(TAG, "Unable to read base 64 attachment", e)
@ -1057,7 +1055,7 @@ class DatabaseInputKDBX(database: DatabaseKDBX)
if (xpp.attributeCount > 0) { if (xpp.attributeCount > 0) {
val protect = xpp.getAttributeValue(null, DatabaseKDBXXML.AttrProtected) val protect = xpp.getAttributeValue(null, DatabaseKDBXXML.AttrProtected)
if (protect != null && protect.equals(DatabaseKDBXXML.ValTrue, ignoreCase = true)) { if (protect != null && protect.equals(DatabaseKDBXXML.ValTrue, ignoreCase = true)) {
Base64.decodeBase64(xpp.safeNextText())?.let { data -> Base64.decode(xpp.safeNextText(), BASE_64_FLAG)?.let { data ->
return randomStream?.processBytes(data) return randomStream?.processBytes(data)
} }
return ByteArray(0) return ByteArray(0)

View file

@ -19,6 +19,7 @@
*/ */
package com.kunzisoft.keepass.database.file.output package com.kunzisoft.keepass.database.file.output
import android.util.Base64
import android.util.Log import android.util.Log
import android.util.Xml import android.util.Xml
import com.kunzisoft.encrypt.StreamCipher import com.kunzisoft.encrypt.StreamCipher
@ -27,6 +28,7 @@ import com.kunzisoft.keepass.database.crypto.kdf.KdfFactory
import com.kunzisoft.keepass.database.element.* import com.kunzisoft.keepass.database.element.*
import com.kunzisoft.keepass.database.element.database.CompressionAlgorithm import com.kunzisoft.keepass.database.element.database.CompressionAlgorithm
import com.kunzisoft.keepass.database.element.database.DatabaseKDBX import com.kunzisoft.keepass.database.element.database.DatabaseKDBX
import com.kunzisoft.keepass.database.element.database.DatabaseKDBX.Companion.BASE_64_FLAG
import com.kunzisoft.keepass.database.element.database.DatabaseVersioned import com.kunzisoft.keepass.database.element.database.DatabaseVersioned
import com.kunzisoft.keepass.database.element.entry.AutoType import com.kunzisoft.keepass.database.element.entry.AutoType
import com.kunzisoft.keepass.database.element.entry.EntryKDBX import com.kunzisoft.keepass.database.element.entry.EntryKDBX
@ -43,13 +45,11 @@ import com.kunzisoft.keepass.database.file.DateKDBXUtil
import com.kunzisoft.keepass.stream.HashedBlockOutputStream import com.kunzisoft.keepass.stream.HashedBlockOutputStream
import com.kunzisoft.keepass.stream.HmacBlockOutputStream import com.kunzisoft.keepass.stream.HmacBlockOutputStream
import com.kunzisoft.keepass.utils.* import com.kunzisoft.keepass.utils.*
import org.apache.commons.codec.binary.Base64
import org.xmlpull.v1.XmlSerializer import org.xmlpull.v1.XmlSerializer
import java.io.IOException import java.io.IOException
import java.io.OutputStream import java.io.OutputStream
import java.security.SecureRandom import java.security.SecureRandom
import java.util.Stack import java.util.*
import java.util.UUID
import java.util.zip.GZIPOutputStream import java.util.zip.GZIPOutputStream
import javax.crypto.Cipher import javax.crypto.Cipher
import javax.crypto.CipherOutputStream import javax.crypto.CipherOutputStream
@ -216,7 +216,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX)
writeString(DatabaseKDBXXML.ElemGenerator, mDatabaseKDBX.localizedAppName) writeString(DatabaseKDBXXML.ElemGenerator, mDatabaseKDBX.localizedAppName)
if (hashOfHeader != null) { if (hashOfHeader != null) {
writeString(DatabaseKDBXXML.ElemHeaderHash, String(Base64.encodeBase64(hashOfHeader!!))) writeString(DatabaseKDBXXML.ElemHeaderHash, String(Base64.encode(hashOfHeader!!, BASE_64_FLAG)))
} }
if (!header!!.version.isBefore(FILE_VERSION_40)) { if (!header!!.version.isBefore(FILE_VERSION_40)) {
@ -417,7 +417,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX)
writeString(name, DatabaseKDBXXML.DateFormatter.format(date)) writeString(name, DatabaseKDBXXML.DateFormatter.format(date))
} else { } else {
val buf = longTo8Bytes(DateKDBXUtil.convertDateToKDBX4Time(date)) val buf = longTo8Bytes(DateKDBXUtil.convertDateToKDBX4Time(date))
val b64 = String(Base64.encodeBase64(buf)) val b64 = String(Base64.encode(buf, BASE_64_FLAG))
writeString(name, b64) writeString(name, b64)
} }
} }
@ -441,7 +441,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX)
@Throws(IllegalArgumentException::class, IllegalStateException::class, IOException::class) @Throws(IllegalArgumentException::class, IllegalStateException::class, IOException::class)
private fun writeUuid(name: String, uuid: UUID) { private fun writeUuid(name: String, uuid: UUID) {
val data = uuidTo16Bytes(uuid) val data = uuidTo16Bytes(uuid)
writeString(name, String(Base64.encodeBase64(data))) writeString(name, String(Base64.encode(data, BASE_64_FLAG)))
} }
/* /*
@ -490,7 +490,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX)
// Write the XML // Write the XML
binary.getInputDataStream(binaryCache).use { inputStream -> binary.getInputDataStream(binaryCache).use { inputStream ->
inputStream.readAllBytes { buffer -> inputStream.readAllBytes { buffer ->
xml.text(String(Base64.encodeBase64(buffer))) xml.text(String(Base64.encode(buffer, BASE_64_FLAG)))
} }
} }
} catch (e: Exception) { } catch (e: Exception) {
@ -562,7 +562,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX)
xml.attribute(null, DatabaseKDBXXML.AttrProtected, DatabaseKDBXXML.ValTrue) xml.attribute(null, DatabaseKDBXXML.AttrProtected, DatabaseKDBXXML.ValTrue)
val data = value.toString().toByteArray() val data = value.toString().toByteArray()
val encoded = randomStream?.processBytes(data) ?: ByteArray(0) val encoded = randomStream?.processBytes(data) ?: ByteArray(0)
xml.text(String(Base64.encodeBase64(encoded))) xml.text(String(Base64.encode(encoded, BASE_64_FLAG)))
} else { } else {
xml.text(value.toString()) xml.text(value.toString())
} }
@ -723,7 +723,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX)
Log.e(TAG, "Unable to write custom icon", e) Log.e(TAG, "Unable to write custom icon", e)
} finally { } finally {
writeString(DatabaseKDBXXML.ElemCustomIconItemData, writeString(DatabaseKDBXXML.ElemCustomIconItemData,
String(Base64.encodeBase64(customImageData))) String(Base64.encode(customImageData, BASE_64_FLAG)))
} }
if (iconCustom.name.isNotEmpty()) { if (iconCustom.name.isNotEmpty()) {
writeString(DatabaseKDBXXML.ElemName, iconCustom.name) writeString(DatabaseKDBXXML.ElemName, iconCustom.name)