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())