Fix to open challenge-response dynamically / refactoring methods #8

This commit is contained in:
J-Jamet committed 2022-04-25 21:47:43 +02:00
1 parent ecbee73eae
commit e8f79ae467
5 files changed
+36 -29

No files matched your search

@@ -153,7 +153,7 @@ class MainCredentialActivity : DatabaseModeActivity(), AdvancedUnlockFragment.Bu
// Build elements to manage hardware key
mHardwareKeyResponseHelper = HardwareKeyResponseHelper(this)
mHardwareKeyResponseHelper?.buildHardwareKeyResponse { responseData ->
mainCredentialView?.challengeResponse = responseData
mainCredentialView?.validateCredential(responseData)
}
mainCredentialView?.onRequestHardwareKeyResponse = { hardwareKey ->
try {
@@ -116,15 +116,15 @@ class DatabaseKDB : DatabaseVersioned<Int, UUID, GroupKDB, EntryKDB>() {
}
@Throws(IOException::class)
override fun getMasterKey(passwordKey: String?,
keyFileData: ByteArray?,
hardwareKey: ByteArray?): ByteArray {
override fun deriveMasterKey(passwordKey: String?,
keyFileData: ByteArray?,
hardwareKey: ByteArray?): ByteArray {
return if (passwordKey != null && keyFileData != null) {
getCompositeKey(passwordKey, keyFileData, null) ?: byteArrayOf()
retrieveCompositeKey(passwordKey, keyFileData, null) ?: byteArrayOf()
} else if (passwordKey != null) { // key.length() >= 0
getPasswordKey(passwordKey)
retrievePasswordKey(passwordKey)
} else if (keyFileData != null) { // key == null
getFileKey(keyFileData)
retrieveFileKey(keyFileData)
} else {
throw IllegalArgumentException("Key cannot be empty.")
}
@@ -529,10 +529,10 @@ class DatabaseKDBX : DatabaseVersioned<UUID, UUID, GroupKDBX, EntryKDBX> {
}
@Throws(IOException::class)
public override fun getMasterKey(passwordKey: String?,
keyFileData: ByteArray?,
hardwareKey: ByteArray?): ByteArray {
return getCompositeKey(passwordKey, keyFileData, hardwareKey)
public override fun deriveMasterKey(passwordKey: String?,
keyFileData: ByteArray?,
hardwareKey: ByteArray?): ByteArray {
return retrieveCompositeKey(passwordKey, keyFileData, hardwareKey)
?: HashManager.hashSha256(byteArrayOf())
}
@@ -92,34 +92,35 @@ abstract class DatabaseVersioned<
}
@Throws(IOException::class)
protected abstract fun getMasterKey(passwordKey: String?,
keyFileData: ByteArray?,
hardwareKey: ByteArray?): ByteArray
protected abstract fun deriveMasterKey(passwordKey: String?,
keyFileData: ByteArray?,
hardwareKey: ByteArray?): ByteArray
@Throws(IOException::class)
fun retrieveMasterKey(key: String?,
keyFileData: ByteArray?,
hardwareKeyData: ByteArray?) {
masterKey = getMasterKey(key, keyFileData, hardwareKeyData)
masterKey = deriveMasterKey(key, keyFileData, hardwareKeyData)
}
@Throws(IOException::class)
protected fun getCompositeKey(passwordKey: String?,
keyFileData: ByteArray?,
hardwareKeyData: ByteArray?): ByteArray? {
protected fun retrieveCompositeKey(passwordKey: String?,
keyFileData: ByteArray?,
hardwareKeyData: ByteArray?): ByteArray? {
if (passwordKey == null && keyFileData == null && hardwareKeyData == null)
return null
val passwordBytes = if (passwordKey != null) getPasswordKey(passwordKey) else null
val keyFileBytes = if (keyFileData != null) getFileKey(keyFileData) else null
val passwordBytes = if (passwordKey != null) retrievePasswordKey(passwordKey) else null
val keyFileBytes = if (keyFileData != null) retrieveFileKey(keyFileData) else null
val hardwareKeyBytes = if (hardwareKeyData != null) retrieveHardwareKey(hardwareKeyData) else null
return HashManager.hashSha256(
passwordBytes,
keyFileBytes,
hardwareKeyData
hardwareKeyBytes
)
}
@Throws(IOException::class)
protected fun getPasswordKey(key: String): ByteArray {
protected fun retrievePasswordKey(key: String): ByteArray {
val bKey: ByteArray = try {
key.toByteArray(charset(passwordEncoding))
} catch (e: UnsupportedEncodingException) {
@@ -129,7 +130,7 @@ abstract class DatabaseVersioned<
}
@Throws(IOException::class)
protected fun getFileKey(keyData: ByteArray): ByteArray {
protected fun retrieveFileKey(keyData: ByteArray): ByteArray {
try {
// Check XML key file
val xmlKeyByteArray = loadXmlKeyFile(ByteArrayInputStream(keyData))
@@ -153,6 +154,11 @@ abstract class DatabaseVersioned<
}
}
@Throws(IOException::class)
protected fun retrieveHardwareKey(keyData: ByteArray): ByteArray {
return HashManager.hashSha256(keyData)
}
protected open fun loadXmlKeyFile(keyInputStream: InputStream): ByteArray? {
return null
}
@@ -57,6 +57,8 @@ class MainCredentialView @JvmOverloads constructor(context: Context,
var onValidateListener: (() -> Unit)? = null
var onRequestHardwareKeyResponse: ((HardwareKey)-> Unit)? = null
private var mChallengeResponse: ByteArray? = null
private var mCredentialStorage: CredentialStorage = CredentialStorage.PASSWORD
init {
@@ -117,12 +119,14 @@ class MainCredentialView @JvmOverloads constructor(context: Context,
}
}
fun validateCredential() {
fun validateCredential(hardwareKeyData: ByteArray? = null) {
val hardwareKey = hardwareKeySelectionView.hardwareKey
if (checkboxHardwareView.isChecked) {
if (hardwareKeyData == null && checkboxHardwareView.isChecked) {
onRequestHardwareKeyResponse?.invoke(hardwareKey)
} else {
mChallengeResponse = hardwareKeyData
onValidateListener?.invoke()
mChallengeResponse = null
}
}
@@ -160,9 +164,6 @@ class MainCredentialView @JvmOverloads constructor(context: Context,
|| checkboxHardwareView.isChecked
}
// TODO Challenge response
var challengeResponse: ByteArray? = null
fun getMainCredential(): MainCredential {
return MainCredential().apply {
this.masterPassword = if (checkboxPasswordView.isChecked)
@@ -170,7 +171,7 @@ class MainCredentialView @JvmOverloads constructor(context: Context,
this.keyFileUri = if (checkboxKeyFileView.isChecked)
keyFileSelectionView.uri else null
this.hardwareKeyData = if (checkboxHardwareView.isChecked)
challengeResponse else null
mChallengeResponse else null
}
}