mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2026-12-06 08:42:30 -08:00
fix: Add main credential check method
This commit is contained in:
14 files changed
+418
-177
No files matched your search
@@ -42,6 +42,7 @@ 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
|
||||
@@ -618,6 +619,53 @@ 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)
|
||||
}
|
||||
}
|
||||
|
||||
fun isMergeDataAllowed(): Boolean {
|
||||
return mDatabaseKDBX != null
|
||||
}
|
||||
|
||||
@@ -42,9 +42,11 @@ import javax.xml.XMLConstants
|
||||
import javax.xml.parsers.DocumentBuilderFactory
|
||||
import javax.xml.parsers.ParserConfigurationException
|
||||
|
||||
data class MasterCredential(var password: String? = null,
|
||||
var keyFileData: ByteArray? = null,
|
||||
var hardwareKey: HardwareKey? = null): Parcelable {
|
||||
data class MasterCredential(
|
||||
var password: String? = null,
|
||||
var keyFileData: ByteArray? = null,
|
||||
var hardwareKey: HardwareKey? = null
|
||||
): Parcelable {
|
||||
|
||||
constructor(parcel: Parcel) : this() {
|
||||
password = parcel.readString()
|
||||
@@ -94,8 +96,9 @@ data class MasterCredential(var password: String? = null,
|
||||
private val TAG = MasterCredential::class.java.simpleName
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun retrievePasswordKey(key: String,
|
||||
encoding: Charset
|
||||
fun retrievePasswordKey(
|
||||
key: String,
|
||||
encoding: Charset
|
||||
): ByteArray {
|
||||
val bKey: ByteArray = try {
|
||||
key.toByteArray(encoding)
|
||||
|
||||
Reference in new issue
Block a user