2020-07-17 16:35:39 +02:00
|
|
|
package sushi.hardcore.droidfs
|
|
|
|
|
2022-03-05 12:51:02 +01:00
|
|
|
import android.annotation.SuppressLint
|
2020-07-17 16:35:39 +02:00
|
|
|
import android.os.Build
|
|
|
|
import android.os.Bundle
|
2022-03-05 12:51:02 +01:00
|
|
|
import android.view.MenuItem
|
|
|
|
import android.view.View
|
2020-07-17 16:35:39 +02:00
|
|
|
import android.widget.Toast
|
2020-07-28 22:25:10 +02:00
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2021-06-11 20:23:54 +02:00
|
|
|
import sushi.hardcore.droidfs.databinding.ActivityChangePasswordBinding
|
2021-11-09 11:12:09 +01:00
|
|
|
import sushi.hardcore.droidfs.widgets.CustomAlertDialogBuilder
|
2020-07-17 16:35:39 +02:00
|
|
|
import java.util.*
|
|
|
|
|
2022-03-05 12:51:02 +01:00
|
|
|
class ChangePasswordActivity: BaseActivity() {
|
|
|
|
|
2021-06-11 20:23:54 +02:00
|
|
|
private lateinit var binding: ActivityChangePasswordBinding
|
2022-03-05 12:51:02 +01:00
|
|
|
private lateinit var volume: Volume
|
|
|
|
private lateinit var volumeDatabase: VolumeDatabase
|
|
|
|
private var fingerprintProtector: FingerprintProtector? = null
|
|
|
|
private var usfFingerprint: Boolean = false
|
2021-06-11 20:23:54 +02:00
|
|
|
|
2020-07-17 16:35:39 +02:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
2022-03-05 12:51:02 +01:00
|
|
|
volume = intent.getParcelableExtra("volume")!!
|
2021-06-11 20:23:54 +02:00
|
|
|
binding = ActivityChangePasswordBinding.inflate(layoutInflater)
|
|
|
|
setContentView(binding.root)
|
2022-03-05 12:51:02 +01:00
|
|
|
title = getString(R.string.change_password)
|
|
|
|
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
|
|
|
binding.textVolumeName.text = volume.name
|
|
|
|
volumeDatabase = VolumeDatabase(this)
|
|
|
|
usfFingerprint = sharedPrefs.getBoolean("usf_fingerprint", false)
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
|
fingerprintProtector = FingerprintProtector.new(this, themeValue, volumeDatabase)
|
|
|
|
if (fingerprintProtector != null && volume.encryptedHash != null) {
|
|
|
|
binding.textCurrentPasswordLabel.visibility = View.GONE
|
|
|
|
binding.editCurrentPassword.visibility = View.GONE
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
}
|
2022-03-05 12:51:02 +01:00
|
|
|
if (!usfFingerprint || fingerprintProtector == null) {
|
|
|
|
binding.checkboxSavePassword.visibility = View.GONE
|
2020-11-04 18:42:04 +01:00
|
|
|
}
|
2022-03-05 12:51:02 +01:00
|
|
|
binding.editPasswordConfirm.setOnEditorActionListener { _, _, _ ->
|
|
|
|
changeVolumePassword()
|
2020-07-17 16:35:39 +02:00
|
|
|
true
|
|
|
|
}
|
2022-03-05 12:51:02 +01:00
|
|
|
binding.button.setOnClickListener { changeVolumePassword() }
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
|
2022-03-05 12:51:02 +01:00
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
|
|
|
return if (item.itemId == android.R.id.home) {
|
|
|
|
finish()
|
|
|
|
true
|
|
|
|
} else super.onOptionsItemSelected(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun showCurrentPasswordInput() {
|
|
|
|
binding.textCurrentPasswordLabel.visibility = View.VISIBLE
|
|
|
|
binding.editCurrentPassword.visibility = View.VISIBLE
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
|
2022-03-05 12:51:02 +01:00
|
|
|
private fun changeVolumePassword() {
|
|
|
|
val newPassword = CharArray(binding.editNewPassword.text.length)
|
|
|
|
binding.editNewPassword.text.getChars(0, newPassword.size, newPassword, 0)
|
|
|
|
val newPasswordConfirm = CharArray(binding.editPasswordConfirm.text.length)
|
|
|
|
binding.editPasswordConfirm.text.getChars(0, newPasswordConfirm.size, newPasswordConfirm, 0)
|
|
|
|
@SuppressLint("NewApi")
|
2020-08-25 14:10:46 +02:00
|
|
|
if (!newPassword.contentEquals(newPasswordConfirm)) {
|
|
|
|
Toast.makeText(this, R.string.passwords_mismatch, Toast.LENGTH_SHORT).show()
|
2022-03-05 12:51:02 +01:00
|
|
|
Arrays.fill(newPassword, 0.toChar())
|
2020-08-25 14:10:46 +02:00
|
|
|
} else {
|
2022-03-05 12:51:02 +01:00
|
|
|
var changeWithCurrentPassword = true
|
|
|
|
volume.encryptedHash?.let { encryptedHash ->
|
|
|
|
volume.iv?.let { iv ->
|
|
|
|
fingerprintProtector?.let {
|
|
|
|
changeWithCurrentPassword = false
|
|
|
|
it.listener = object : FingerprintProtector.Listener {
|
|
|
|
override fun onHashStorageReset() {
|
|
|
|
showCurrentPasswordInput()
|
|
|
|
volume.encryptedHash = null
|
|
|
|
volume.iv = null
|
2020-11-04 18:42:04 +01:00
|
|
|
}
|
2022-03-05 12:51:02 +01:00
|
|
|
override fun onPasswordHashDecrypted(hash: ByteArray) {
|
|
|
|
changeVolumePassword(newPassword, hash)
|
|
|
|
}
|
|
|
|
override fun onPasswordHashSaved() {}
|
|
|
|
override fun onFailed(pending: Boolean) {
|
|
|
|
Arrays.fill(newPassword, 0.toChar())
|
2020-07-28 22:25:10 +02:00
|
|
|
}
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
2022-03-05 12:51:02 +01:00
|
|
|
it.loadPasswordHash(volume.name, encryptedHash, iv)
|
2020-07-28 22:25:10 +02:00
|
|
|
}
|
2022-03-05 12:51:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (changeWithCurrentPassword) {
|
|
|
|
changeVolumePassword(newPassword)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Arrays.fill(newPasswordConfirm, 0.toChar())
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun changeVolumePassword(newPassword: CharArray, givenHash: ByteArray? = null) {
|
|
|
|
object : LoadingTask(this, themeValue, R.string.loading_msg_change_password) {
|
|
|
|
override fun doTask(activity: AppCompatActivity) {
|
|
|
|
var returnedHash: ByteArray? = null
|
|
|
|
if (binding.checkboxSavePassword.isChecked) {
|
|
|
|
returnedHash = ByteArray(GocryptfsVolume.KeyLen)
|
|
|
|
}
|
|
|
|
var currentPassword: CharArray? = null
|
|
|
|
if (givenHash == null) {
|
|
|
|
currentPassword = CharArray(binding.editCurrentPassword.text.length)
|
|
|
|
binding.editCurrentPassword.text.getChars(0, currentPassword.size, currentPassword, 0)
|
|
|
|
}
|
|
|
|
if (GocryptfsVolume.changePassword(volume.getFullPath(filesDir.path), currentPassword, givenHash, newPassword, returnedHash)) {
|
|
|
|
if (volumeDatabase.isHashSaved(volume.name)) {
|
|
|
|
volumeDatabase.removeHash(volume)
|
|
|
|
}
|
|
|
|
stopTask {
|
|
|
|
@SuppressLint("NewApi") // if fingerprintProtector is null checkboxSavePassword is hidden
|
|
|
|
if (binding.checkboxSavePassword.isChecked && returnedHash != null) {
|
|
|
|
fingerprintProtector!!.let {
|
|
|
|
it.listener = object : FingerprintProtector.Listener {
|
|
|
|
override fun onHashStorageReset() {
|
|
|
|
// retry
|
|
|
|
it.savePasswordHash(volume, returnedHash)
|
|
|
|
}
|
|
|
|
override fun onPasswordHashDecrypted(hash: ByteArray) {}
|
|
|
|
override fun onPasswordHashSaved() {
|
|
|
|
Arrays.fill(returnedHash, 0)
|
|
|
|
finish()
|
|
|
|
}
|
|
|
|
override fun onFailed(pending: Boolean) {
|
|
|
|
if (!pending) {
|
|
|
|
Arrays.fill(returnedHash, 0)
|
|
|
|
finish()
|
|
|
|
}
|
2020-10-22 22:14:22 +02:00
|
|
|
}
|
2020-10-15 17:43:13 +02:00
|
|
|
}
|
2022-03-05 12:51:02 +01:00
|
|
|
it.savePasswordHash(volume, returnedHash)
|
2020-07-28 22:25:10 +02:00
|
|
|
}
|
|
|
|
} else {
|
2022-03-05 12:51:02 +01:00
|
|
|
finish()
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
}
|
2022-03-05 12:51:02 +01:00
|
|
|
} else {
|
|
|
|
stopTask {
|
|
|
|
CustomAlertDialogBuilder(activity, themeValue)
|
|
|
|
.setTitle(R.string.error)
|
|
|
|
.setMessage(R.string.change_password_failed)
|
|
|
|
.setPositiveButton(R.string.ok, null)
|
|
|
|
.show()
|
|
|
|
}
|
2020-08-25 14:10:46 +02:00
|
|
|
}
|
2022-03-05 12:51:02 +01:00
|
|
|
if (currentPassword != null)
|
|
|
|
Arrays.fill(currentPassword, 0.toChar())
|
|
|
|
Arrays.fill(newPassword, 0.toChar())
|
|
|
|
if (givenHash != null)
|
|
|
|
Arrays.fill(givenHash, 0)
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
2020-07-28 22:25:10 +02:00
|
|
|
}
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
}
|