Skip to content

Commit

Permalink
fix master key alias
Browse files Browse the repository at this point in the history
Signed-off-by: phuoc <[email protected]>
  • Loading branch information
phuocbitmark committed Oct 3, 2024
1 parent 44f4088 commit 23dad98
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import java.io.File
import java.security.KeyStore
import java.util.*
import android.os.Build
import android.util.Log

internal interface SecureFileStorage {

Expand All @@ -30,13 +31,8 @@ internal class SecureFileStorageImpl(
) : SecureFileStorage {

private val keyStore: KeyStore = KeyStore.getInstance(ANDROID_KEY_STORE).apply { load(null) }
private val sharedPreferences = context.getSharedPreferences("beaconsdk", Context.MODE_PRIVATE)

private var masterKeyAlias: String?
get() = sharedPreferences.getString(KEY_MASTER_KEY_ALIAS, null)
set(value) {
value?.let { sharedPreferences.edit().putString(KEY_MASTER_KEY_ALIAS, it).apply() }
}
private fun getFileName(name: String) = "$alias-${name}-default_alias"

private fun write(path: String, name: String, data: ByteArray) {
val file = getEncryptedFile("$path/$name", false)
Expand All @@ -48,7 +44,7 @@ internal class SecureFileStorageImpl(
}

override fun writeOnFilesDir(name: String, data: ByteArray) {
write(context.filesDir.absolutePath, "$alias-$name", data)
write(context.filesDir.absolutePath, getFileName(name), data)
}

private fun read(path: String): ByteArray {
Expand All @@ -65,14 +61,15 @@ internal class SecureFileStorageImpl(
}

override fun readOnFilesDir(name: String): ByteArray =
read(File(context.filesDir, "$alias-$name").absolutePath)
read(File(context.filesDir, getFileName(name)).absolutePath)

private fun isExisting(path: String): Boolean = File(path).exists()

override fun isExistingOnFilesDir(name: String): Boolean =
isExisting(File(context.filesDir, "$alias-$name").absolutePath)
isExisting(File(context.filesDir, getFileName(name)).absolutePath)

private fun delete(path: String): Boolean = File(path).let { file ->
Log.d("delete path", "path to delete: $path")
if (!file.exists()) true
else if (file.isDirectory) {
file.deleteRecursively()
Expand All @@ -82,7 +79,7 @@ internal class SecureFileStorageImpl(
}

override fun deleteOnFilesDir(name: String): Boolean =
delete(File(context.filesDir, "$alias-$name").absolutePath)
delete(File(context.filesDir, getFileName(name)).absolutePath)

private fun getEncryptedFile(path: String, read: Boolean) = File(path).let { f ->
if (f.isDirectory) throw IllegalArgumentException("do not support directory")
Expand All @@ -104,10 +101,8 @@ internal class SecureFileStorageImpl(
private fun getMasterKey(): MasterKey {
keyStore.load(null)

val keyAlias = masterKeyAlias ?: UUID.randomUUID().toString().also { masterKeyAlias = it }

val parameterSpec = KeyGenParameterSpec.Builder(
keyAlias,
DEFAULT_MASTER_KEY_ALIAS,
KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT
).apply {
setKeySize(256)
Expand All @@ -121,14 +116,15 @@ internal class SecureFileStorageImpl(
setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
}.build()

return MasterKey.Builder(context, keyAlias)
return MasterKey.Builder(context, DEFAULT_MASTER_KEY_ALIAS)
.setKeyGenParameterSpec(parameterSpec)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build()
}

companion object {
private const val ANDROID_KEY_STORE = "AndroidKeyStore"
private const val KEY_MASTER_KEY_ALIAS = "masterKeyAlias"
private const val DEFAULT_MASTER_KEY_ALIAS = "default_master_key_alias"
}
}

Expand Down
13 changes: 9 additions & 4 deletions libauk/src/main/java/com/bitmark/libauk/storage/WalletStorage.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bitmark.libauk.storage

import android.util.Log
import at.favre.lib.hkdf.HKDF
import com.bitmark.libauk.Const.ACCOUNT_DERIVATION_PATH
import com.bitmark.libauk.Const.ENCRYPT_KEY_DERIVATION_PATH
Expand Down Expand Up @@ -38,7 +39,8 @@ interface WalletStorage {
words: List<String>,
passphrase: String? = "",
name: String,
creationDate: Date?
creationDate: Date?,
override: Boolean = false
): Completable

fun isWalletCreated(): Single<Boolean>
Expand Down Expand Up @@ -120,15 +122,16 @@ internal class WalletStorageImpl(private val secureFileStorage: SecureFileStorag
words: List<String>,
passphrase: String?,
name: String,
creationDate: Date?
creationDate: Date?,
override: Boolean
): Completable =
secureFileStorage.rxSingle { storage ->
storage.isExistingOnFilesDir(SEED_FILE_NAME) && storage.isExistingOnFilesDir(
ETH_KEY_INFO_FILE_NAME
)
}
.map { isExisting ->
if (!isExisting) {
if (!isExisting || override) {
val mnemonic = words.joinToString(separator = " ")
val entropy = MnemonicUtils.generateEntropy(mnemonic)
val seed = Seed(entropy, Date(), name, passphrase ?: "")
Expand Down Expand Up @@ -404,18 +407,20 @@ internal class WalletStorageImpl(private val secureFileStorage: SecureFileStorag
}

override fun removeKeys(): Completable = secureFileStorage.rxSingle { storage ->
storage.isExistingOnFilesDir(SEED_FILE_NAME) && storage.isExistingOnFilesDir(
storage.isExistingOnFilesDir(SEED_FILE_NAME) || storage.isExistingOnFilesDir(
ETH_KEY_INFO_FILE_NAME
)
}
.map { isExisting ->
Log.d("removeKeys", "isExisting $isExisting")
if (isExisting) {
true
} else {
throw Throwable("Wallet is not created!")
}
}
.flatMapCompletable {
Log.d("removeKeys", "removing")
secureFileStorage.rxCompletable { storage ->
storage.deleteOnFilesDir(SEED_FILE_NAME)
storage.deleteOnFilesDir(ETH_KEY_INFO_FILE_NAME)
Expand Down

0 comments on commit 23dad98

Please sign in to comment.