]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - client/emv/cmdemv.c
added new functions to `hf emv`
[proxmark3-svn] / client / emv / cmdemv.c
index c53b02af1a1dd1e4e58225a0f2dc905332e312ae..a4018649621383379048400583faae644cdc3b2e 100644 (file)
 #include <ctype.h>
 #include "cmdemv.h"
 #include "test/cryptotest.h"
-
-int UsageCmdHFEMVSelect(void) {
-       PrintAndLog("HELP :  Executes select applet command:\n");
-       PrintAndLog("Usage:  hf emv select [-s][-k][-a][-t] <HEX applet AID>\n");
-       PrintAndLog("  Options:");
-       PrintAndLog("  -s       : select card");
-       PrintAndLog("  -k       : keep field for next command");
-       PrintAndLog("  -a       : show APDU reqests and responses\n");
-       PrintAndLog("  -t       : TLV decode results\n");
-       PrintAndLog("Samples:");
-       PrintAndLog(" hf emv select -s a00000000101 -> select card, select applet");
-       PrintAndLog(" hf emv select -s -t a00000000101 -> select card, select applet, show result in TLV");
-       return 0;
-}
+#include "cliparser/cliparser.h"
 
 int CmdHFEMVSelect(const char *cmd) {
        uint8_t data[APDU_AID_LEN] = {0};
        int datalen = 0;
-       bool activateField = false;
-       bool leaveSignalON = false;
-       bool decodeTLV = false;
-
-       if (strlen(cmd) < 1) {
-               UsageCmdHFEMVSelect();
-               return 0;
-       }
-       
-       SetAPDULogging(false);
-       
-       int cmdp = 0;
-       while(param_getchar(cmd, cmdp) != 0x00) {
-               char c = param_getchar(cmd, cmdp);
-               if ((c == '-') && (param_getlength(cmd, cmdp) == 2))
-                       switch (param_getchar_indx(cmd, 1, cmdp)) {
-                               case 'h':
-                               case 'H':
-                                       UsageCmdHFEMVSelect();
-                                       return 0;
-                               case 's':
-                               case 'S':
-                                       activateField = true;
-                                       break;
-                               case 'k':
-                               case 'K':
-                                       leaveSignalON = true;
-                                       break;
-                               case 'a':
-                               case 'A':
-                                       SetAPDULogging(true);
-                                       break;
-                               case 't':
-                               case 'T':
-                                       decodeTLV = true;
-                                       break;
-                               default:
-                                       PrintAndLog("Unknown parameter '%c'", param_getchar_indx(cmd, 1, cmdp));
-                                       return 1;
-               }
 
-               if (isxdigit((unsigned char)c)) {
-                       switch(param_gethex_to_eol(cmd, cmdp, data, sizeof(data), &datalen)) {
-                       case 1:
-                               PrintAndLog("Invalid HEX value.");
-                               return 1;
-                       case 2:
-                               PrintAndLog("AID too large.");
-                               return 1;
-                       case 3:
-                               PrintAndLog("Hex must have even number of digits.");
-                               return 1;
-                       }
-                       
-                       // we get all the hex to end of line with spaces
-                       break;
-               }
 
+       CLIParserInit("hf 14a select", 
+               "Executes select applet command", 
+               "Usage:\n\thf emv select -s a00000000101 -> select card, select applet\n\thf emv select -st a00000000101 -> select card, select applet, show result in TLV\n");
 
-               cmdp++;
-       }
+       void* argtable[] = {
+               arg_param_begin,
+               arg_lit0("sS",  "select",  "activate field and select card"),
+               arg_lit0("kK",  "keep",    "keep field for next command"),
+               arg_lit0("aA",  "apdu",    "show APDU reqests and responses"),
+               arg_lit0("tT",  "tlv",     "TLV decode results"),
+               arg_str0(NULL,  NULL,      "<HEX applet AID>", NULL),
+               arg_param_end
+       };
+       CLIExecWithReturn(cmd, argtable, true);
+       
+       bool activateField = arg_get_lit(1);
+       bool leaveSignalON = arg_get_lit(2);
+       bool APDULogging = arg_get_lit(3);
+       bool decodeTLV = arg_get_lit(4);
+       CLIGetStrBLessWithReturn(5, data, &datalen, 0);
+       CLIParserFree();
+       
+       SetAPDULogging(APDULogging);
        
        // exec
        uint8_t buf[APDU_RES_LEN] = {0};
@@ -109,65 +60,30 @@ int CmdHFEMVSelect(const char *cmd) {
        return 0;
 }
 
-int UsageCmdHFEMVSearch(void) {
-       PrintAndLog("HELP :  Tries to select all applets from applet list:\n");
-       PrintAndLog("Usage:  hf emv search [-s][-k][-a][-t]\n");
-       PrintAndLog("  Options:");
-       PrintAndLog("  -s       : select card");
-       PrintAndLog("  -k       : keep field for next command");
-       PrintAndLog("  -a       : show APDU reqests and responses\n");
-       PrintAndLog("  -t       : TLV decode results of selected applets\n");
-       PrintAndLog("Samples:");
-       PrintAndLog(" hf emv search -s -> select card and search");
-       PrintAndLog(" hf emv search -s -t -> select card, search and show result in TLV");
-       return 0;
-}
-
 int CmdHFEMVSearch(const char *cmd) {
 
-       bool activateField = false;
-       bool leaveSignalON = false;
-       bool decodeTLV = false;
+       CLIParserInit("hf 14a select", 
+               "Tries to select all applets from applet list:\n", 
+               "Usage:\n\thf emv search -s -> select card and search\n\thf emv search -st -> select card, search and show result in TLV\n");
 
-       if (strlen(cmd) < 1) {
-               UsageCmdHFEMVSearch();
-               return 0;
-       }
+       void* argtable[] = {
+               arg_param_begin,
+               arg_lit0("sS",  "select",  "activate field and select card"),
+               arg_lit0("kK",  "keep",    "keep field ON for next command"),
+               arg_lit0("aA",  "apdu",    "show APDU reqests and responses"),
+               arg_lit0("tT",  "tlv",     "TLV decode results of selected applets"),
+               arg_param_end
+       };
+       CLIExecWithReturn(cmd, argtable, true);
        
-       SetAPDULogging(false);
+       bool activateField = arg_get_lit(1);
+       bool leaveSignalON = arg_get_lit(2);
+       bool APDULogging = arg_get_lit(3);
+       bool decodeTLV = arg_get_lit(4);
+       CLIParserFree();
+       
+       SetAPDULogging(APDULogging);
        
-       int cmdp = 0;
-       while(param_getchar(cmd, cmdp) != 0x00) {
-               char c = param_getchar(cmd, cmdp);
-               if ((c == '-') && (param_getlength(cmd, cmdp) == 2))
-                       switch (param_getchar_indx(cmd, 1, cmdp)) {
-                               case 'h':
-                               case 'H':
-                                       UsageCmdHFEMVSearch();
-                                       return 0;
-                               case 's':
-                               case 'S':
-                                       activateField = true;
-                                       break;
-                               case 'k':
-                               case 'K':
-                                       leaveSignalON = true;
-                                       break;
-                               case 'a':
-                               case 'A':
-                                       SetAPDULogging(true);
-                                       break;
-                               case 't':
-                               case 'T':
-                                       decodeTLV = true;
-                                       break;
-                               default:
-                                       PrintAndLog("Unknown parameter '%c'", param_getchar_indx(cmd, 1, cmdp));
-                                       return 1;
-               }
-               cmdp++;
-       }
-
        struct tlvdb *t = NULL;
        const char *al = "Applets list";
        t = tlvdb_fixed(1, strlen(al), (const unsigned char *)al);
@@ -189,74 +105,37 @@ int CmdHFEMVSearch(const char *cmd) {
        return 0;
 }
 
-int UsageCmdHFEMVPPSE(void) {
-       PrintAndLog("HELP :  Executes PSE/PPSE select command. It returns list of applet on the card:\n");
-       PrintAndLog("Usage:  hf emv pse [-s][-k][-1][-2][-a][-t]\n");
-       PrintAndLog("  Options:");
-       PrintAndLog("  -s       : select card");
-       PrintAndLog("  -k       : keep field for next command");
-       PrintAndLog("  -1       : ppse (1PAY.SYS.DDF01)");
-       PrintAndLog("  -2       : pse (2PAY.SYS.DDF01)");
-       PrintAndLog("  -a       : show APDU reqests and responses\n");
-       PrintAndLog("  -t       : TLV decode results\n");
-       PrintAndLog("Samples:");
-       PrintAndLog(" hf emv pse -s -1 -> select, get pse");
-       PrintAndLog(" hf emv pse -s -k -2 -> select, get ppse, keep field");
-       PrintAndLog(" hf emv pse -s -t -2 -> select, get ppse, show result in TLV");
-       return 0;
-}
-
 int CmdHFEMVPPSE(const char *cmd) {
        
+       
+       CLIParserInit("hf 14a pse", 
+               "Executes PSE/PPSE select command. It returns list of applet on the card:\n", 
+               "Usage:\n\thf emv pse -s1 -> select, get pse\n\thf emv pse -st2 -> select, get ppse, show result in TLV\n");
+
+       void* argtable[] = {
+               arg_param_begin,
+               arg_lit0("sS",  "select",  "activate field and select card"),
+               arg_lit0("kK",  "keep",    "keep field ON for next command"),
+               arg_lit0("1",   "pse",     "pse (1PAY.SYS.DDF01) mode"),
+               arg_lit0("2",   "ppse",    "ppse (2PAY.SYS.DDF01) mode (default mode)"),
+               arg_lit0("aA",  "apdu",    "show APDU reqests and responses"),
+               arg_lit0("tT",  "tlv",     "TLV decode results of selected applets"),
+               arg_param_end
+       };
+       CLIExecWithReturn(cmd, argtable, true);
+       
+       bool activateField = arg_get_lit(1);
+       bool leaveSignalON = arg_get_lit(2);
        uint8_t PSENum = 2;
-       bool activateField = false;
-       bool leaveSignalON = false;
-       bool decodeTLV = false;
-
-       if (strlen(cmd) < 1) {
-               UsageCmdHFEMVPPSE();
-               return 0;
-       }
-
-       SetAPDULogging(false);
+       if (arg_get_lit(3))
+               PSENum = 1;
+       if (arg_get_lit(4))
+               PSENum = 2;
+       bool APDULogging = arg_get_lit(5);
+       bool decodeTLV = arg_get_lit(6);
+       CLIParserFree();        
        
-       int cmdp = 0;
-       while(param_getchar(cmd, cmdp) != 0x00) {
-               char c = param_getchar(cmd, cmdp);
-               if ((c == '-') && (param_getlength(cmd, cmdp) == 2))
-                       switch (param_getchar_indx(cmd, 1, cmdp)) {
-                               case 'h':
-                               case 'H':
-                                       UsageCmdHFEMVPPSE();
-                                       return 0;
-                               case 's':
-                               case 'S':
-                                       activateField = true;
-                                       break;
-                               case 'k':
-                               case 'K':
-                                       leaveSignalON = true;
-                                       break;
-                               case 'a':
-                               case 'A':
-                                       SetAPDULogging(true);
-                                       break;
-                               case 't':
-                               case 'T':
-                                       decodeTLV = true;
-                                       break;
-                               case '1':
-                                       PSENum = 1;
-                                       break;
-                               case '2':
-                                       PSENum = 2;
-                                       break;
-                               default:
-                                       PrintAndLog("Unknown parameter '%c'", param_getchar_indx(cmd, 1, cmdp));
-                                       return 1;
-               }
-               cmdp++;
-       }
+       SetAPDULogging(APDULogging);
        
        // exec
        uint8_t buf[APDU_RES_LEN] = {0};
@@ -277,6 +156,31 @@ int CmdHFEMVPPSE(const char *cmd) {
        return 0;
 }
 
+int CmdHFEMVGPO(const char *cmd) {
+
+       return 0;
+}
+
+int CmdHFEMVReadRecord(const char *cmd) {
+
+       return 0;
+}
+
+int CmdHFEMVAC(const char *cmd) {
+
+       return 0;
+}
+
+int CmdHFEMVGenerateChallenge(const char *cmd) {
+
+       return 0;
+}
+
+int CmdHFEMVInternalAuthenticate(const char *cmd) {
+
+       return 0;
+}
+
 int UsageCmdHFEMVExec(void) {
        PrintAndLog("HELP :  Executes EMV contactless transaction:\n");
        PrintAndLog("Usage:  hf emv exec [-s][-a][-t][-f][-v][-c][-x][-g]\n");
@@ -834,12 +738,17 @@ int CmdHFEMVTest(const char *cmd) {
 
 int CmdHelp(const char *Cmd);
 static command_t CommandTable[] =  {
-       {"help",        CmdHelp,                1,      "This help"},
-       {"exec",        CmdHFEMVExec,   0,      "Executes EMV contactless transaction."},
-       {"pse",         CmdHFEMVPPSE,   0,      "Execute PPSE. It selects 2PAY.SYS.DDF01 or 1PAY.SYS.DDF01 directory."},
-       {"search",      CmdHFEMVSearch, 0,      "Try to select all applets from applets list and print installed applets."},
-       {"select",      CmdHFEMVSelect, 0,      "Select applet."},
-       {"test",        CmdHFEMVTest,   0,      "Crypto logic test."},
+       {"help",                CmdHelp,                                                1,      "This help"},
+       {"exec",                CmdHFEMVExec,                                   0,      "Executes EMV contactless transaction."},
+       {"pse",                 CmdHFEMVPPSE,                                   0,      "Execute PPSE. It selects 2PAY.SYS.DDF01 or 1PAY.SYS.DDF01 directory."},
+       {"search",              CmdHFEMVSearch,                                 0,      "Try to select all applets from applets list and print installed applets."},
+       {"select",              CmdHFEMVSelect,                                 0,      "Select applet."},
+       {"gpo",                 CmdHFEMVGPO,                                    0,      "Execute GetProcessingOptions."},
+       {"readrec",             CmdHFEMVReadRecord,                             0,      "Read files from card."},
+       {"genac",               CmdHFEMVAC,                                             0,      "Generate ApplicationCryptogram."},
+       {"challenge",   CmdHFEMVGenerateChallenge,              0,      "Generate challenge."},
+       {"intauth",             CmdHFEMVInternalAuthenticate,   0,      "Internal authentication."},
+       {"test",                CmdHFEMVTest,   0,      "Crypto logic test."},
        {NULL, NULL, 0, NULL}
 };
 
Impressum, Datenschutz