mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2026-11-24 02:51:34 -08:00
Added a dialog to verify the password #2283
This commit is contained in:
21 files changed
+292
-263
No files matched your search
@@ -42,7 +42,6 @@ import com.kunzisoft.keepass.database.exception.DatabaseException
|
||||
import com.kunzisoft.keepass.database.exception.DatabaseInputException
|
||||
import com.kunzisoft.keepass.database.exception.DatabaseOutputException
|
||||
import com.kunzisoft.keepass.database.exception.FileNotFoundDatabaseException
|
||||
import com.kunzisoft.keepass.database.exception.InvalidCredentialsDatabaseException
|
||||
import com.kunzisoft.keepass.database.exception.MergeDatabaseKDBException
|
||||
import com.kunzisoft.keepass.database.exception.SignatureDatabaseException
|
||||
import com.kunzisoft.keepass.database.file.DatabaseHeaderKDB
|
||||
@@ -391,6 +390,9 @@ open class Database {
|
||||
val transformSeed: ByteArray?
|
||||
get() = mDatabaseKDB?.transformSeed ?: mDatabaseKDBX?.transformSeed
|
||||
|
||||
private val checkKey: ByteArray
|
||||
get() = mDatabaseKDB?.checkKey ?: mDatabaseKDBX?.checkKey ?: ByteArray(32)
|
||||
|
||||
var rootGroup: Group?
|
||||
get() {
|
||||
mDatabaseKDB?.rootGroup?.let {
|
||||
@@ -619,51 +621,11 @@ open class Database {
|
||||
}
|
||||
}
|
||||
|
||||
fun checkMasterKey(
|
||||
databaseStream: InputStream,
|
||||
masterCredential: MasterCredential,
|
||||
challengeResponseRetriever: (HardwareKey, ByteArray?) -> ByteArray,
|
||||
progressTaskUpdater: ProgressTaskUpdater?
|
||||
) {
|
||||
try {
|
||||
var masterKey = byteArrayOf()
|
||||
// Read database stream for the first time
|
||||
readDatabaseStream(databaseStream,
|
||||
{ databaseInputStream ->
|
||||
val databaseKDB = DatabaseKDB()
|
||||
DatabaseInputKDB(databaseKDB)
|
||||
.openDatabase(databaseInputStream,
|
||||
progressTaskUpdater
|
||||
) {
|
||||
databaseKDB.deriveMasterKey(
|
||||
masterCredential
|
||||
)
|
||||
}
|
||||
masterKey = databaseKDB.masterKey
|
||||
},
|
||||
{ databaseInputStream ->
|
||||
val databaseKDBX = DatabaseKDBX()
|
||||
DatabaseInputKDBX(databaseKDBX).apply {
|
||||
openDatabase(databaseInputStream,
|
||||
progressTaskUpdater) {
|
||||
databaseKDBX.deriveMasterKey(
|
||||
masterCredential,
|
||||
challengeResponseRetriever
|
||||
)
|
||||
}
|
||||
}
|
||||
masterKey = databaseKDBX.masterKey
|
||||
}
|
||||
)
|
||||
if (!this.masterKey.contentEquals(masterKey)) {
|
||||
throw InvalidCredentialsDatabaseException()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Unable to check the main credential")
|
||||
if (e is DatabaseInputException)
|
||||
throw e
|
||||
throw DatabaseInputException(e)
|
||||
}
|
||||
/**
|
||||
* Check if the key is valid
|
||||
*/
|
||||
fun checkKey(key: ByteArray): Boolean {
|
||||
return checkKey.contentEquals(key)
|
||||
}
|
||||
|
||||
fun isMergeDataAllowed(): Boolean {
|
||||
@@ -1268,7 +1230,7 @@ open class Database {
|
||||
}
|
||||
|
||||
fun undoRecycle(entry: Entry, parent: Group) {
|
||||
recycleBin?.let { it ->
|
||||
recycleBin?.let {
|
||||
removeEntryFrom(entry, it)
|
||||
}
|
||||
addEntryTo(entry, parent)
|
||||
|
||||
@@ -64,6 +64,10 @@ data class MasterCredential(
|
||||
return 0
|
||||
}
|
||||
|
||||
fun getCheckKey(): ByteArray {
|
||||
return getCheckKey(password)
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (javaClass != other?.javaClass) return false
|
||||
@@ -95,6 +99,15 @@ data class MasterCredential(
|
||||
|
||||
private val TAG = MasterCredential::class.java.simpleName
|
||||
|
||||
fun getCheckKey(password: String?): ByteArray {
|
||||
return retrievePasswordKey(
|
||||
try {
|
||||
password?.substring(0, 3) ?: ""
|
||||
} catch (_: Exception) { "" },
|
||||
Charsets.UTF_8
|
||||
)
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun retrievePasswordKey(
|
||||
key: String,
|
||||
@@ -102,7 +115,7 @@ data class MasterCredential(
|
||||
): ByteArray {
|
||||
val bKey: ByteArray = try {
|
||||
key.toByteArray(encoding)
|
||||
} catch (e: UnsupportedEncodingException) {
|
||||
} catch (_: UnsupportedEncodingException) {
|
||||
key.toByteArray()
|
||||
}
|
||||
return HashManager.hashSha256(bKey)
|
||||
@@ -128,7 +141,7 @@ data class MasterCredential(
|
||||
32 -> return keyFileData
|
||||
64 -> try {
|
||||
return Hex.decodeHex(String(keyFileData).toCharArray())
|
||||
} catch (ignoredException: Exception) {
|
||||
} catch (_: Exception) {
|
||||
// Key is not base 64, treat it as binary data
|
||||
}
|
||||
}
|
||||
@@ -151,7 +164,7 @@ data class MasterCredential(
|
||||
// Disable certain unsecure XML-Parsing DocumentBuilderFactory features
|
||||
try {
|
||||
documentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true)
|
||||
} catch (e : ParserConfigurationException) {
|
||||
} catch (_ : ParserConfigurationException) {
|
||||
Log.w(TAG, "Unable to add FEATURE_SECURE_PROCESSING to prevent XML eXternal Entity injection (XXE)")
|
||||
}
|
||||
|
||||
@@ -187,7 +200,7 @@ data class MasterCredential(
|
||||
xmlKeyFileVersion = versionText.toFloat()
|
||||
Log.i(TAG, "Reading XML KeyFile version : $xmlKeyFileVersion")
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "XML Keyfile version cannot be read : $versionText")
|
||||
Log.e(TAG, "XML Keyfile version cannot be read : $versionText", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -235,7 +248,7 @@ data class MasterCredential(
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
} catch (_: Exception) {
|
||||
return null
|
||||
}
|
||||
return null
|
||||
|
||||
+4
-1
@@ -36,7 +36,7 @@ import com.kunzisoft.keepass.database.exception.EmptyKeyDatabaseException
|
||||
import com.kunzisoft.keepass.database.exception.HardwareKeyDatabaseException
|
||||
import java.io.IOException
|
||||
import java.nio.charset.Charset
|
||||
import java.util.*
|
||||
import java.util.UUID
|
||||
|
||||
class DatabaseKDB : DatabaseVersioned<Int, UUID, GroupKDB, EntryKDB>() {
|
||||
|
||||
@@ -158,6 +158,9 @@ class DatabaseKDB : DatabaseVersioned<Int, UUID, GroupKDB, EntryKDB>() {
|
||||
} else {
|
||||
this.masterKey = passwordBytes ?: keyFileBytes ?: byteArrayOf(0)
|
||||
}
|
||||
|
||||
// Build check key
|
||||
this.checkKey = masterCredential.getCheckKey()
|
||||
}
|
||||
|
||||
override fun createGroup(): GroupKDB {
|
||||
|
||||
+17
-5
@@ -19,7 +19,6 @@
|
||||
*/
|
||||
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
|
||||
@@ -28,7 +27,12 @@ import com.kunzisoft.keepass.database.crypto.kdf.AesKdf
|
||||
import com.kunzisoft.keepass.database.crypto.kdf.KdfEngine
|
||||
import com.kunzisoft.keepass.database.crypto.kdf.KdfFactory
|
||||
import com.kunzisoft.keepass.database.crypto.kdf.KdfParameters
|
||||
import com.kunzisoft.keepass.database.element.*
|
||||
import com.kunzisoft.keepass.database.element.CompositeKey
|
||||
import com.kunzisoft.keepass.database.element.CustomData
|
||||
import com.kunzisoft.keepass.database.element.DateInstant
|
||||
import com.kunzisoft.keepass.database.element.DeletedObject
|
||||
import com.kunzisoft.keepass.database.element.MasterCredential
|
||||
import com.kunzisoft.keepass.database.element.Tags
|
||||
import com.kunzisoft.keepass.database.element.binary.BinaryData
|
||||
import com.kunzisoft.keepass.database.element.database.DatabaseKDB.Companion.BACKUP_FOLDER_TITLE
|
||||
import com.kunzisoft.keepass.database.element.entry.EntryKDBX
|
||||
@@ -36,7 +40,11 @@ import com.kunzisoft.keepass.database.element.entry.FieldReferencesEngine
|
||||
import com.kunzisoft.keepass.database.element.group.GroupKDBX
|
||||
import com.kunzisoft.keepass.database.element.icon.IconImageCustom
|
||||
import com.kunzisoft.keepass.database.element.icon.IconImageStandard
|
||||
import com.kunzisoft.keepass.database.element.node.*
|
||||
import com.kunzisoft.keepass.database.element.node.NodeHandler
|
||||
import com.kunzisoft.keepass.database.element.node.NodeId
|
||||
import com.kunzisoft.keepass.database.element.node.NodeIdUUID
|
||||
import com.kunzisoft.keepass.database.element.node.NodeKDBXInterface
|
||||
import com.kunzisoft.keepass.database.element.node.NodeVersioned
|
||||
import com.kunzisoft.keepass.database.element.security.MemoryProtectionConfig
|
||||
import com.kunzisoft.keepass.database.element.template.Template
|
||||
import com.kunzisoft.keepass.database.element.template.TemplateEngineCompatible
|
||||
@@ -51,7 +59,8 @@ import java.io.IOException
|
||||
import java.nio.charset.Charset
|
||||
import java.security.MessageDigest
|
||||
import java.security.NoSuchAlgorithmException
|
||||
import java.util.*
|
||||
import java.util.Arrays
|
||||
import java.util.UUID
|
||||
import javax.crypto.Mac
|
||||
import kotlin.math.min
|
||||
|
||||
@@ -253,6 +262,9 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
|
||||
keyFileBytes,
|
||||
hardwareKeyBytes
|
||||
)
|
||||
|
||||
// Build check key
|
||||
this.checkKey = masterCredential.getCheckKey()
|
||||
}
|
||||
|
||||
@Throws(DatabaseOutputException::class)
|
||||
@@ -635,7 +647,7 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
|
||||
messageDigest = MessageDigest.getInstance("SHA-512")
|
||||
cmpKey[64] = 1
|
||||
hmacKey = messageDigest.digest(cmpKey)
|
||||
} catch (e: NoSuchAlgorithmException) {
|
||||
} catch (_: NoSuchAlgorithmException) {
|
||||
throw IOException("No SHA-512 implementation")
|
||||
} finally {
|
||||
Arrays.fill(cmpKey, 0.toByte())
|
||||
|
||||
+6
-7
@@ -34,7 +34,7 @@ import com.kunzisoft.keepass.database.exception.DuplicateUuidDatabaseException
|
||||
import java.io.InputStream
|
||||
import java.io.UnsupportedEncodingException
|
||||
import java.nio.charset.Charset
|
||||
import java.util.*
|
||||
import java.util.UUID
|
||||
|
||||
abstract class DatabaseVersioned<
|
||||
GroupId,
|
||||
@@ -58,6 +58,8 @@ abstract class DatabaseVersioned<
|
||||
protected set
|
||||
var transformSeed: ByteArray? = null
|
||||
|
||||
var checkKey = ByteArray(32)
|
||||
|
||||
abstract val version: String
|
||||
abstract val defaultFileExtension: String
|
||||
|
||||
@@ -89,10 +91,6 @@ abstract class DatabaseVersioned<
|
||||
return getGroupIndexes().filter { it != rootGroup }
|
||||
}
|
||||
|
||||
protected open fun loadXmlKeyFile(keyInputStream: InputStream): ByteArray? {
|
||||
return null
|
||||
}
|
||||
|
||||
open fun isValidCredential(password: String?, containsKeyFile: Boolean): Boolean {
|
||||
if (password == null && !containsKeyFile)
|
||||
return false
|
||||
@@ -105,14 +103,14 @@ abstract class DatabaseVersioned<
|
||||
val bKey: ByteArray
|
||||
try {
|
||||
bKey = password.toByteArray(encoding)
|
||||
} catch (e: UnsupportedEncodingException) {
|
||||
} catch (_: UnsupportedEncodingException) {
|
||||
return false
|
||||
}
|
||||
|
||||
val reEncoded: String
|
||||
try {
|
||||
reEncoded = String(bKey, encoding)
|
||||
} catch (e: UnsupportedEncodingException) {
|
||||
} catch (_: UnsupportedEncodingException) {
|
||||
return false
|
||||
}
|
||||
return password == reEncoded
|
||||
@@ -121,6 +119,7 @@ abstract class DatabaseVersioned<
|
||||
fun copyMasterKeyFrom(databaseVersioned: DatabaseVersioned<GroupId, EntryId, Group, Entry>) {
|
||||
this.masterKey = databaseVersioned.masterKey
|
||||
this.transformSeed = databaseVersioned.transformSeed
|
||||
this.checkKey = databaseVersioned.checkKey
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in new issue
Block a user