fix: Clear sensitive data #2482 #2480

This commit is contained in:
J-Jamet committed 2026-04-15 17:07:54 +02:00
1 parent f28a3cf6c1
commit faa74585af
9 files changed
+46 -9

No files matched your search

@@ -2,9 +2,11 @@ package com.kunzisoft.keepass.database.element
import com.kunzisoft.keepass.hardware.HardwareKey
data class CompositeKey(var passwordData: ByteArray? = null,
var keyFileData: ByteArray? = null,
var hardwareKey: HardwareKey? = null) {
data class CompositeKey(
var passwordData: ByteArray? = null,
var keyFileData: ByteArray? = null,
var hardwareKey: HardwareKey? = null
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
@@ -31,4 +33,12 @@ data class CompositeKey(var passwordData: ByteArray? = null,
result = 31 * result + (hardwareKey?.hashCode() ?: 0)
return result
}
fun clear() {
passwordData?.fill(0)
keyFileData?.fill(0)
passwordData = null
keyFileData = null
hardwareKey = null
}
}
@@ -973,6 +973,8 @@ open class Database {
open fun clearAndClose(filesDirectory: File? = null) {
clearIndexesAndBinaries(filesDirectory)
this.mDatabaseKDB?.clearSensitiveData()
this.mDatabaseKDBX?.clearSensitiveData()
this.mDatabaseKDB = null
this.mDatabaseKDBX = null
this.loaded = false
@@ -118,7 +118,9 @@ data class MasterCredential(
} catch (_: UnsupportedEncodingException) {
key.toByteArray()
}
return HashManager.hashSha256(bKey)
val hash = HashManager.hashSha256(bKey)
bKey.fill(0)
return hash
}
@Throws(IOException::class)
@@ -125,6 +125,7 @@ class DatabaseKDB : DatabaseVersioned<Int, UUID, GroupKDB, EntryKDB>() {
val transformedKey = AESTransformer.transformKey(transformSeed, masterKey, numRounds) ?: ByteArray(0)
// Write checksum Checksum
finalKey = HashManager.hashSha256(masterSeed, transformedKey)
transformedKey.fill(0)
}
fun deriveMasterKey(
@@ -155,6 +156,8 @@ class DatabaseKDB : DatabaseVersioned<Int, UUID, GroupKDB, EntryKDB>() {
passwordBytes,
keyFileBytes
)
passwordBytes.fill(0)
keyFileBytes.fill(0)
} else {
this.masterKey = passwordBytes ?: keyFileBytes ?: byteArrayOf(0)
}
@@ -265,6 +265,11 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
// Build check key
this.checkKey = masterCredential.getCheckKey()
// Clear temporary sensitive data
passwordBytes?.fill(0)
keyFileBytes?.fill(0)
hardwareKeyBytes?.fill(0)
}
@Throws(DatabaseOutputException::class)
@@ -289,6 +294,7 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
keyFileBytes,
hardwareKeyBytes
)
hardwareKeyBytes.fill(0)
}
}
@@ -651,6 +657,7 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
throw IOException("No SHA-512 implementation")
} finally {
Arrays.fill(cmpKey, 0.toByte())
transformedMasterKey.fill(0)
}
}
}
@@ -897,6 +904,12 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
}
}
override fun clearSensitiveData() {
super.clearSensitiveData()
hmacKey?.fill(0)
mCompositeKey.clear()
}
companion object {
val TYPE = DatabaseKDBX::class.java
private val TAG = DatabaseKDBX::class.java.name
@@ -31,7 +31,6 @@ import com.kunzisoft.keepass.database.element.icon.IconsManager
import com.kunzisoft.keepass.database.element.node.NodeId
import com.kunzisoft.keepass.database.element.node.Type
import com.kunzisoft.keepass.database.exception.DuplicateUuidDatabaseException
import java.io.InputStream
import java.io.UnsupportedEncodingException
import java.nio.charset.Charset
import java.util.UUID
@@ -276,6 +275,13 @@ abstract class DatabaseVersioned<
abstract fun isInRecycleBin(group: Group): Boolean
open fun clearSensitiveData() {
masterKey.fill(0)
finalKey?.fill(0)
checkKey.fill(0)
transformSeed?.fill(0)
}
fun clearIconsCache() {
iconsManager.doForEachCustomIcon { _, binary ->
try {
@@ -303,6 +309,7 @@ abstract class DatabaseVersioned<
}
fun clearAll() {
clearSensitiveData()
clearIndexes()
clearIconsCache()
clearAttachmentsCache()