mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2026-11-21 17:41:50 -08:00
fix: Clear MasterCredential when necessary #2485
This commit is contained in:
6 files changed
+66
-25
No files matched your search
@@ -173,7 +173,10 @@ abstract class DatabaseLockActivity : DatabaseModeActivity(),
|
||||
mDatabase?.let { database ->
|
||||
database.fileUri?.let { databaseUri ->
|
||||
// Show the progress dialog now or after dialog confirmation
|
||||
if (database.isValidCredential(mainCredential.toMasterCredential(contentResolver))) {
|
||||
val masterCredential = mainCredential.toMasterCredential(contentResolver)
|
||||
val validCredential = database.isValidCredential(masterCredential)
|
||||
masterCredential.clear()
|
||||
if (validCredential) {
|
||||
mDatabaseViewModel.assignMainCredential(databaseUri, mainCredential)
|
||||
} else {
|
||||
PasswordEncodingDialogFragment.getInstance(databaseUri, mainCredential)
|
||||
|
||||
@@ -23,6 +23,7 @@ import android.content.Context
|
||||
import android.net.Uri
|
||||
import com.kunzisoft.keepass.database.ContextualDatabase
|
||||
import com.kunzisoft.keepass.database.MainCredential
|
||||
import com.kunzisoft.keepass.database.element.MasterCredential
|
||||
import com.kunzisoft.keepass.database.element.binary.BinaryData
|
||||
import com.kunzisoft.keepass.database.exception.DatabaseInputException
|
||||
import com.kunzisoft.keepass.database.exception.UnknownDatabaseLocationException
|
||||
@@ -44,6 +45,7 @@ class LoadDatabaseRunnable(
|
||||
private val progressTaskUpdater: ProgressTaskUpdater?
|
||||
) : ActionRunnable() {
|
||||
|
||||
private var mMasterCredential: MasterCredential? = null
|
||||
var afterLoadDatabase : ((Result) -> Unit)? = null
|
||||
|
||||
private val binaryDir = context.getBinaryDir()
|
||||
@@ -58,19 +60,20 @@ class LoadDatabaseRunnable(
|
||||
val contentResolver = context.contentResolver
|
||||
// Save database URI
|
||||
mDatabase.fileUri = mDatabaseUri
|
||||
mMasterCredential = mMainCredential.toMasterCredential(contentResolver)
|
||||
mDatabase.loadData(
|
||||
contentResolver.getUriInputStream(mDatabaseUri)
|
||||
databaseStream = contentResolver.getUriInputStream(mDatabaseUri)
|
||||
?: throw UnknownDatabaseLocationException(),
|
||||
mMainCredential.toMasterCredential(contentResolver),
|
||||
mChallengeResponseRetriever,
|
||||
mReadonly,
|
||||
mAllowUserVerification,
|
||||
binaryDir,
|
||||
{ memoryWanted ->
|
||||
masterCredential = mMasterCredential!!,
|
||||
challengeResponseRetriever = mChallengeResponseRetriever,
|
||||
readOnly = mReadonly,
|
||||
allowUserVerification = mAllowUserVerification,
|
||||
cacheDirectory = binaryDir,
|
||||
isRAMSufficient = { memoryWanted ->
|
||||
BinaryData.canMemoryBeAllocatedInRAM(context, memoryWanted)
|
||||
},
|
||||
mFixDuplicateUUID,
|
||||
progressTaskUpdater
|
||||
fixDuplicateUUID = mFixDuplicateUUID,
|
||||
progressTaskUpdater = progressTaskUpdater
|
||||
)
|
||||
} catch (e: DatabaseInputException) {
|
||||
setError(e)
|
||||
@@ -82,6 +85,7 @@ class LoadDatabaseRunnable(
|
||||
}
|
||||
|
||||
override fun onFinishRun() {
|
||||
mMasterCredential?.clear()
|
||||
afterLoadDatabase?.invoke(result)
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,7 @@ import android.content.Context
|
||||
import android.net.Uri
|
||||
import com.kunzisoft.keepass.database.ContextualDatabase
|
||||
import com.kunzisoft.keepass.database.MainCredential
|
||||
import com.kunzisoft.keepass.database.element.MasterCredential
|
||||
import com.kunzisoft.keepass.database.element.binary.BinaryData
|
||||
import com.kunzisoft.keepass.database.exception.DatabaseException
|
||||
import com.kunzisoft.keepass.database.exception.UnknownDatabaseLocationException
|
||||
@@ -46,6 +47,9 @@ class MergeDatabaseRunnable(
|
||||
null,
|
||||
challengeResponseRetriever
|
||||
) {
|
||||
|
||||
private var mMasterCredential: MasterCredential? = null
|
||||
|
||||
override fun onStartRun() {
|
||||
database.wasReloaded = true
|
||||
super.onStartRun()
|
||||
@@ -54,16 +58,17 @@ class MergeDatabaseRunnable(
|
||||
override fun onActionRun() {
|
||||
try {
|
||||
val contentResolver = context.contentResolver
|
||||
mMasterCredential = mDatabaseToMergeMainCredential?.toMasterCredential(contentResolver)
|
||||
database.mergeData(
|
||||
context.contentResolver.getUriInputStream(
|
||||
databaseToMergeStream = contentResolver.getUriInputStream(
|
||||
mDatabaseToMergeUri ?: database.fileUri
|
||||
) ?: throw UnknownDatabaseLocationException(),
|
||||
mDatabaseToMergeMainCredential?.toMasterCredential(contentResolver),
|
||||
mDatabaseToMergeChallengeResponseRetriever,
|
||||
{ memoryWanted ->
|
||||
databaseToMergeMasterCredential = mMasterCredential,
|
||||
databaseToMergeChallengeResponseRetriever = mDatabaseToMergeChallengeResponseRetriever,
|
||||
isRAMSufficient = { memoryWanted ->
|
||||
BinaryData.canMemoryBeAllocatedInRAM(context, memoryWanted)
|
||||
},
|
||||
progressTaskUpdater
|
||||
progressTaskUpdater = progressTaskUpdater
|
||||
)
|
||||
} catch (e: DatabaseException) {
|
||||
setError(e)
|
||||
@@ -71,4 +76,9 @@ class MergeDatabaseRunnable(
|
||||
|
||||
super.onActionRun()
|
||||
}
|
||||
|
||||
override fun onFinishRun() {
|
||||
mMasterCredential?.clear()
|
||||
super.onFinishRun()
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,7 @@ import android.content.Context
|
||||
import android.net.Uri
|
||||
import com.kunzisoft.keepass.database.ContextualDatabase
|
||||
import com.kunzisoft.keepass.database.MainCredential
|
||||
import com.kunzisoft.keepass.database.element.MasterCredential
|
||||
import com.kunzisoft.keepass.database.exception.DatabaseException
|
||||
import com.kunzisoft.keepass.hardware.HardwareKey
|
||||
import com.kunzisoft.keepass.tasks.ActionRunnable
|
||||
@@ -38,6 +39,7 @@ open class SaveDatabaseRunnable(
|
||||
private var databaseCopyUri: Uri? = null
|
||||
) : ActionRunnable() {
|
||||
|
||||
private var mMasterCredential: MasterCredential? = null
|
||||
var afterSaveDatabase: ((Result) -> Unit)? = null
|
||||
|
||||
override fun onStartRun() {}
|
||||
@@ -48,6 +50,7 @@ open class SaveDatabaseRunnable(
|
||||
if ((databaseCopyUri != null || saveDatabase) && result.isSuccess) {
|
||||
try {
|
||||
val contentResolver = context.contentResolver
|
||||
mMasterCredential = mainCredential?.toMasterCredential(contentResolver)
|
||||
// Build temp database file to avoid file corruption if error
|
||||
database.saveData(
|
||||
cacheFile = File(context.cacheDir, databaseCopyUri.hashCode().toString()),
|
||||
@@ -56,8 +59,9 @@ open class SaveDatabaseRunnable(
|
||||
.getUriOutputStream(databaseCopyUri ?: database.fileUri)
|
||||
},
|
||||
isNewLocation = databaseCopyUri == null,
|
||||
mainCredential?.toMasterCredential(contentResolver),
|
||||
challengeResponseRetriever)
|
||||
masterCredential = mMasterCredential,
|
||||
challengeResponseRetriever = challengeResponseRetriever
|
||||
)
|
||||
} catch (e: DatabaseException) {
|
||||
setError(e)
|
||||
}
|
||||
@@ -66,6 +70,7 @@ open class SaveDatabaseRunnable(
|
||||
|
||||
override fun onFinishRun() {
|
||||
// Need to call super.onFinishRun() in child class
|
||||
mMasterCredential?.clear()
|
||||
afterSaveDatabase?.invoke(result)
|
||||
}
|
||||
}
|
||||
@@ -45,17 +45,36 @@ import javax.xml.parsers.ParserConfigurationException
|
||||
import kotlin.math.min
|
||||
|
||||
data class MasterCredential(
|
||||
var password: CharArray? = null,
|
||||
var keyFileData: ByteArray? = null,
|
||||
private var mPassword: CharArray? = null,
|
||||
private var mKeyFileData: ByteArray? = null,
|
||||
var hardwareKey: HardwareKey? = null
|
||||
): Parcelable {
|
||||
|
||||
constructor(parcel: Parcel) : this() {
|
||||
password = parcel.createCharArray()
|
||||
keyFileData = parcel.readByteArrayCompat()
|
||||
hardwareKey = parcel.readEnum<HardwareKey>()
|
||||
var password: CharArray?
|
||||
get() = mPassword
|
||||
set(value) {
|
||||
mPassword?.clear()
|
||||
mPassword = value?.copyOf()
|
||||
}
|
||||
|
||||
var keyFileData: ByteArray?
|
||||
get() = mKeyFileData
|
||||
set(value) {
|
||||
mKeyFileData?.fill(0)
|
||||
mKeyFileData = value?.copyOf()
|
||||
}
|
||||
|
||||
init {
|
||||
mPassword = mPassword?.copyOf()
|
||||
mKeyFileData = mKeyFileData?.copyOf()
|
||||
}
|
||||
|
||||
constructor(parcel: Parcel) : this(
|
||||
parcel.createCharArray(),
|
||||
parcel.readByteArrayCompat(),
|
||||
parcel.readEnum<HardwareKey>()
|
||||
)
|
||||
|
||||
override fun writeToParcel(parcel: Parcel, flags: Int) {
|
||||
parcel.writeCharArray(password)
|
||||
parcel.writeByteArrayCompat(keyFileData)
|
||||
|
||||
@@ -12,7 +12,7 @@ enum class HardwareKey(val value: String) {
|
||||
val DEFAULT = CHALLENGE_RESPONSE_YUBIKEY
|
||||
|
||||
fun getStringValues(): List<String> {
|
||||
return values().map { it.value }
|
||||
return HardwareKey.entries.map { it.value }
|
||||
}
|
||||
|
||||
fun fromPosition(position: Int): HardwareKey {
|
||||
@@ -26,7 +26,7 @@ enum class HardwareKey(val value: String) {
|
||||
fun getHardwareKeyFromString(text: String?): HardwareKey? {
|
||||
if (text == null)
|
||||
return null
|
||||
values().find { it.value == text }?.let {
|
||||
HardwareKey.entries.find { it.value == text }?.let {
|
||||
return it
|
||||
}
|
||||
return DEFAULT
|
||||
|
||||
Reference in new issue
Block a user