From 97f7b9256a7c5b3f8471d38d2e4aeb36c10c1cb2 Mon Sep 17 00:00:00 2001 From: Michele Balistreri Date: Mon, 4 Nov 2019 15:55:05 +0300 Subject: [PATCH] draft of key loading --- .../keycard/connect/card/CardCommand.kt | 2 +- .../connect/card/CardScriptExecutor.kt | 3 ++- .../connect/card/CheckMasterKeyCommand.kt | 7 ++++++ .../keycard/connect/card/LoadKeyCommand.kt | 25 +++++++++++++++++++ .../im/status/keycard/connect/card/Script.kt | 2 +- 5 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/im/status/keycard/connect/card/CheckMasterKeyCommand.kt create mode 100644 app/src/main/java/im/status/keycard/connect/card/LoadKeyCommand.kt diff --git a/app/src/main/java/im/status/keycard/connect/card/CardCommand.kt b/app/src/main/java/im/status/keycard/connect/card/CardCommand.kt index fd366a4..b526a32 100644 --- a/app/src/main/java/im/status/keycard/connect/card/CardCommand.kt +++ b/app/src/main/java/im/status/keycard/connect/card/CardCommand.kt @@ -4,7 +4,7 @@ import android.content.Intent interface CardCommand { enum class Result { - OK, CANCEL, RETRY, UX_ONGOING + OK, CANCEL, RETRY, UX_ONGOING, STOP } fun run(context: CardScriptExecutor.ScriptContext): Result diff --git a/app/src/main/java/im/status/keycard/connect/card/CardScriptExecutor.kt b/app/src/main/java/im/status/keycard/connect/card/CardScriptExecutor.kt index 8b96162..6c49841 100644 --- a/app/src/main/java/im/status/keycard/connect/card/CardScriptExecutor.kt +++ b/app/src/main/java/im/status/keycard/connect/card/CardScriptExecutor.kt @@ -35,7 +35,8 @@ class CardScriptExecutor(private val activity: Activity, private val listener: S script@for (cmd in runningScript) { when (cmd.run(executionContext)) { CardCommand.Result.OK -> {} - CardCommand.Result.CANCEL -> { success = false; break@script} + CardCommand.Result.STOP -> { break@script } + CardCommand.Result.CANCEL -> { success = false; break@script } CardCommand.Result.UX_ONGOING -> { waitingCmd = cmd; return } CardCommand.Result.RETRY -> { return } } diff --git a/app/src/main/java/im/status/keycard/connect/card/CheckMasterKeyCommand.kt b/app/src/main/java/im/status/keycard/connect/card/CheckMasterKeyCommand.kt new file mode 100644 index 0000000..e34faad --- /dev/null +++ b/app/src/main/java/im/status/keycard/connect/card/CheckMasterKeyCommand.kt @@ -0,0 +1,7 @@ +package im.status.keycard.connect.card + +class CheckMasterKeyCommand : CardCommand { + override fun run(context: CardScriptExecutor.ScriptContext): CardCommand.Result { + return if (context.cmdSet.applicationInfo.hasMasterKey()) CardCommand.Result.STOP else CardCommand.Result.OK + } +} \ No newline at end of file diff --git a/app/src/main/java/im/status/keycard/connect/card/LoadKeyCommand.kt b/app/src/main/java/im/status/keycard/connect/card/LoadKeyCommand.kt new file mode 100644 index 0000000..6e75f9f --- /dev/null +++ b/app/src/main/java/im/status/keycard/connect/card/LoadKeyCommand.kt @@ -0,0 +1,25 @@ +package im.status.keycard.connect.card + +import java.io.IOException +import java.lang.Exception + +class LoadKeyCommand : CardCommand { + + override fun run(context: CardScriptExecutor.ScriptContext): CardCommand.Result { + /* TODO: this should instead prompt and ask if + * 1. You want to generate keys on card with no backup (most secure) + * 2. You want to generate a new key with backup phrase + * 3. You want to import an existing key + */ + + try { + context.cmdSet.generateKey().checkOK() + } catch(e: IOException) { + return CardCommand.Result.RETRY + } catch (e: Exception) { + return CardCommand.Result.CANCEL + } + + return CardCommand.Result.OK + } +} \ No newline at end of file diff --git a/app/src/main/java/im/status/keycard/connect/card/Script.kt b/app/src/main/java/im/status/keycard/connect/card/Script.kt index 065ed75..9010a08 100644 --- a/app/src/main/java/im/status/keycard/connect/card/Script.kt +++ b/app/src/main/java/im/status/keycard/connect/card/Script.kt @@ -2,5 +2,5 @@ package im.status.keycard.connect.card fun scriptWithSecureChannel(): List = listOf(SelectCommand(), InitCommand(), OpenSecureChannelCommand()) fun scriptWithAuthentication(): List = scriptWithSecureChannel().plus(VerifyPINCommand()) -fun cardCheckupScript(): List = scriptWithAuthentication() +fun cardCheckupScript(): List = scriptWithSecureChannel().plus(CheckMasterKeyCommand()).plus(VerifyPINCommand()).plus(LoadKeyCommand())