diff --git a/app/src/main/java/com/kunzisoft/keepass/app/database/CipherDatabaseAction.kt b/app/src/main/java/com/kunzisoft/keepass/app/database/CipherDatabaseAction.kt index d3af75f75..6b1d385b3 100644 --- a/app/src/main/java/com/kunzisoft/keepass/app/database/CipherDatabaseAction.kt +++ b/app/src/main/java/com/kunzisoft/keepass/app/database/CipherDatabaseAction.kt @@ -25,13 +25,13 @@ import android.content.IntentFilter import android.content.ServiceConnection import android.net.Uri import android.os.IBinder +import android.util.Base64 import android.util.Log import com.kunzisoft.keepass.model.CipherEncryptDatabase import com.kunzisoft.keepass.services.AdvancedUnlockNotificationService import com.kunzisoft.keepass.settings.PreferencesUtil import com.kunzisoft.keepass.utils.IOActionTask import com.kunzisoft.keepass.utils.SingletonHolderParameter -import org.apache.commons.codec.binary.Base64 import java.util.LinkedList class CipherDatabaseAction(context: Context) { @@ -136,11 +136,13 @@ class CipherDatabaseAction(context: Context) { mBinder?.getCipherDatabase(databaseUri)?.let { cipherDatabaseEntity -> cipherDatabase = CipherEncryptDatabase().apply { this.databaseUri = Uri.parse(cipherDatabaseEntity.databaseUri) - this.encryptedValue = Base64.decodeBase64( - cipherDatabaseEntity.encryptedValue + this.encryptedValue = Base64.decode( + cipherDatabaseEntity.encryptedValue, + Base64.NO_WRAP ) - this.specParameters = Base64.decodeBase64( - cipherDatabaseEntity.specParameters + this.specParameters = Base64.decode( + cipherDatabaseEntity.specParameters, + Base64.NO_WRAP ) } } @@ -153,11 +155,13 @@ class CipherDatabaseAction(context: Context) { ?.let { cipherDatabaseEntity -> CipherEncryptDatabase().apply { this.databaseUri = Uri.parse(cipherDatabaseEntity.databaseUri) - this.encryptedValue = Base64.decodeBase64( - cipherDatabaseEntity.encryptedValue + this.encryptedValue = Base64.decode( + cipherDatabaseEntity.encryptedValue, + Base64.NO_WRAP ) - this.specParameters = Base64.decodeBase64( - cipherDatabaseEntity.specParameters + this.specParameters = Base64.decode( + cipherDatabaseEntity.specParameters, + Base64.NO_WRAP ) } } @@ -182,8 +186,8 @@ class CipherDatabaseAction(context: Context) { val cipherDatabaseEntity = CipherDatabaseEntity( databaseUri.toString(), - Base64.encodeBase64String(cipherEncryptDatabase.encryptedValue), - Base64.encodeBase64String(cipherEncryptDatabase.specParameters), + Base64.encodeToString(cipherEncryptDatabase.encryptedValue, Base64.NO_WRAP), + Base64.encodeToString(cipherEncryptDatabase.specParameters, Base64.NO_WRAP), ) if (useTempDao) { diff --git a/database/src/main/java/com/kunzisoft/keepass/database/element/MasterCredential.kt b/database/src/main/java/com/kunzisoft/keepass/database/element/MasterCredential.kt index 3df7fe02d..2d0eb960f 100644 --- a/database/src/main/java/com/kunzisoft/keepass/database/element/MasterCredential.kt +++ b/database/src/main/java/com/kunzisoft/keepass/database/element/MasterCredential.kt @@ -20,8 +20,10 @@ package com.kunzisoft.keepass.database.element import android.os.Parcel import android.os.Parcelable +import android.util.Base64 import android.util.Log import com.kunzisoft.encrypt.HashManager +import com.kunzisoft.keepass.database.element.database.DatabaseKDBX import com.kunzisoft.keepass.hardware.HardwareKey import com.kunzisoft.keepass.utils.StringUtil.removeSpaceChars 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.writeByteArrayCompat import com.kunzisoft.keepass.utils.writeEnum -import org.apache.commons.codec.binary.Base64 import org.apache.commons.codec.binary.Hex import org.w3c.dom.Node import java.io.ByteArrayInputStream @@ -211,7 +212,9 @@ data class MasterCredential(var password: String? = null, when (xmlKeyFileVersion) { 1F -> { // No hash in KeyFile XML version 1 - return Base64.decodeBase64(dataString) + return Base64.decode(dataString, + DatabaseKDBX.BASE_64_FLAG + ) } 2F -> { return if (hashString != null diff --git a/database/src/main/java/com/kunzisoft/keepass/database/element/database/DatabaseKDBX.kt b/database/src/main/java/com/kunzisoft/keepass/database/element/database/DatabaseKDBX.kt index 88d3e84e9..6a372b661 100644 --- a/database/src/main/java/com/kunzisoft/keepass/database/element/database/DatabaseKDBX.kt +++ b/database/src/main/java/com/kunzisoft/keepass/database/element/database/DatabaseKDBX.kt @@ -19,6 +19,7 @@ */ package com.kunzisoft.keepass.database.element.database +import android.util.Base64 import android.util.Log import com.kunzisoft.encrypt.HashManager import com.kunzisoft.keepass.database.crypto.EncryptionAlgorithm @@ -885,9 +886,12 @@ class DatabaseKDBX : DatabaseVersioned { } companion object { + val TYPE = DatabaseKDBX::class.java private val TAG = DatabaseKDBX::class.java.name private const val DEFAULT_HISTORY_MAX_ITEMS = 10 // -1 unlimited private const val DEFAULT_HISTORY_MAX_SIZE = (6 * 1024 * 1024).toLong() // -1 unlimited + + const val BASE_64_FLAG = Base64.NO_WRAP } } diff --git a/database/src/main/java/com/kunzisoft/keepass/database/file/input/DatabaseInputKDBX.kt b/database/src/main/java/com/kunzisoft/keepass/database/file/input/DatabaseInputKDBX.kt index c85f56141..7d9e1867d 100644 --- a/database/src/main/java/com/kunzisoft/keepass/database/file/input/DatabaseInputKDBX.kt +++ b/database/src/main/java/com/kunzisoft/keepass/database/file/input/DatabaseInputKDBX.kt @@ -19,6 +19,7 @@ */ package com.kunzisoft.keepass.database.file.input +import android.util.Base64 import android.util.Log import com.kunzisoft.encrypt.StreamCipher 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.database.CompressionAlgorithm 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.entry.EntryKDBX 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.tasks.ProgressTaskUpdater import com.kunzisoft.keepass.utils.* -import org.apache.commons.codec.binary.Base64 import org.xmlpull.v1.XmlPullParser import org.xmlpull.v1.XmlPullParserException import org.xmlpull.v1.XmlPullParserFactory @@ -54,10 +55,7 @@ import java.io.InputStream import java.io.UnsupportedEncodingException import java.nio.charset.Charset import java.text.ParseException -import java.util.Arrays -import java.util.Date -import java.util.Stack -import java.util.UUID +import java.util.* import java.util.zip.GZIPInputStream import javax.crypto.Cipher import javax.crypto.CipherInputStream @@ -348,7 +346,7 @@ class DatabaseInputKDBX(database: DatabaseKDBX) } else if (name.equals(DatabaseKDBXXML.ElemHeaderHash, ignoreCase = true)) { val encodedHash = readString(xpp) if (encodedHash.isNotEmpty() && hashOfHeader != null) { - val hash = Base64.decodeBase64(encodedHash) + val hash = Base64.decode(encodedHash, BASE_64_FLAG) if (!Arrays.equals(hash, hashOfHeader)) { throw DatabaseInputException() } @@ -434,7 +432,7 @@ class DatabaseInputKDBX(database: DatabaseKDBX) } else if (name.equals(DatabaseKDBXXML.ElemCustomIconItemData, ignoreCase = true)) { val strData = readString(xpp) if (strData.isNotEmpty()) { - customIconData = Base64.decodeBase64(strData) + customIconData = Base64.decode(strData, BASE_64_FLAG) } } else if (name.equals(DatabaseKDBXXML.ElemName, ignoreCase = true)) { customIconName = readString(xpp) @@ -838,7 +836,7 @@ class DatabaseInputKDBX(database: DatabaseKDBX) // Catch with null test below } } else { - var buf = Base64.decodeBase64(sDate) + var buf = Base64.decode(sDate, BASE_64_FLAG) if (buf.size != 8) { val buf8 = ByteArray(8) System.arraycopy(buf, 0, buf8, 0, min(buf.size, 8)) @@ -904,7 +902,7 @@ class DatabaseInputKDBX(database: DatabaseKDBX) } return try { - val buf = Base64.decodeBase64(encoded) + val buf = Base64.decode(encoded, BASE_64_FLAG) bytes16ToUuid(buf) } catch (e: Exception) { Log.e(TAG, "Unable to read base 64 UUID, create a random one", e) @@ -1024,7 +1022,7 @@ class DatabaseInputKDBX(database: DatabaseKDBX) isRAMSufficient.invoke(base64.length.toLong()), compressed, protected, binaryId) try { binaryAttachment.getOutputDataStream(mDatabase.binaryCache).use { outputStream -> - outputStream.write(Base64.decodeBase64(base64)) + outputStream.write(Base64.decode(base64, BASE_64_FLAG)) } } catch (e: Exception) { Log.e(TAG, "Unable to read base 64 attachment", e) @@ -1057,7 +1055,7 @@ class DatabaseInputKDBX(database: DatabaseKDBX) if (xpp.attributeCount > 0) { val protect = xpp.getAttributeValue(null, DatabaseKDBXXML.AttrProtected) 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 ByteArray(0) diff --git a/database/src/main/java/com/kunzisoft/keepass/database/file/output/DatabaseOutputKDBX.kt b/database/src/main/java/com/kunzisoft/keepass/database/file/output/DatabaseOutputKDBX.kt index fb7289e47..2866ffaca 100644 --- a/database/src/main/java/com/kunzisoft/keepass/database/file/output/DatabaseOutputKDBX.kt +++ b/database/src/main/java/com/kunzisoft/keepass/database/file/output/DatabaseOutputKDBX.kt @@ -19,6 +19,7 @@ */ package com.kunzisoft.keepass.database.file.output +import android.util.Base64 import android.util.Log import android.util.Xml 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.database.CompressionAlgorithm 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.entry.AutoType 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.HmacBlockOutputStream import com.kunzisoft.keepass.utils.* -import org.apache.commons.codec.binary.Base64 import org.xmlpull.v1.XmlSerializer import java.io.IOException import java.io.OutputStream import java.security.SecureRandom -import java.util.Stack -import java.util.UUID +import java.util.* import java.util.zip.GZIPOutputStream import javax.crypto.Cipher import javax.crypto.CipherOutputStream @@ -216,7 +216,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX) writeString(DatabaseKDBXXML.ElemGenerator, mDatabaseKDBX.localizedAppName) 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)) { @@ -417,7 +417,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX) writeString(name, DatabaseKDBXXML.DateFormatter.format(date)) } else { val buf = longTo8Bytes(DateKDBXUtil.convertDateToKDBX4Time(date)) - val b64 = String(Base64.encodeBase64(buf)) + val b64 = String(Base64.encode(buf, BASE_64_FLAG)) writeString(name, b64) } } @@ -441,7 +441,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX) @Throws(IllegalArgumentException::class, IllegalStateException::class, IOException::class) private fun writeUuid(name: String, uuid: 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 binary.getInputDataStream(binaryCache).use { inputStream -> inputStream.readAllBytes { buffer -> - xml.text(String(Base64.encodeBase64(buffer))) + xml.text(String(Base64.encode(buffer, BASE_64_FLAG))) } } } catch (e: Exception) { @@ -562,7 +562,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX) xml.attribute(null, DatabaseKDBXXML.AttrProtected, DatabaseKDBXXML.ValTrue) val data = value.toString().toByteArray() val encoded = randomStream?.processBytes(data) ?: ByteArray(0) - xml.text(String(Base64.encodeBase64(encoded))) + xml.text(String(Base64.encode(encoded, BASE_64_FLAG))) } else { xml.text(value.toString()) } @@ -723,7 +723,7 @@ class DatabaseOutputKDBX(private val mDatabaseKDBX: DatabaseKDBX) Log.e(TAG, "Unable to write custom icon", e) } finally { writeString(DatabaseKDBXXML.ElemCustomIconItemData, - String(Base64.encodeBase64(customImageData))) + String(Base64.encode(customImageData, BASE_64_FLAG))) } if (iconCustom.name.isNotEmpty()) { writeString(DatabaseKDBXXML.ElemName, iconCustom.name)