From 1b3e34c928d48e4f4a02ffa831fe6d10212bdc3f Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Tue, 13 Jul 2021 18:13:29 +0300 Subject: [PATCH] createapp fully works --- client/src/cmdhfmfdes.c | 27 +++++++++++++++++---------- client/src/mifare/desfirecore.c | 9 +++++++++ client/src/mifare/desfirecore.h | 1 + 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/client/src/cmdhfmfdes.c b/client/src/cmdhfmfdes.c index 09b9974f7..fe07ab53e 100644 --- a/client/src/cmdhfmfdes.c +++ b/client/src/cmdhfmfdes.c @@ -5034,7 +5034,7 @@ static int CmdHF14ADesDefault(const char *Cmd) { static int CmdHF14ADesCreateApp(const char *Cmd) { CLIParserContext *ctx; - CLIParserInit(&ctx, "hf mfdes createid", + CLIParserInit(&ctx, "hf mfdes createaid", "Create application. Master key needs to be provided.", "option rawdata have priority over the rest settings, and options ks1 and ks2 have priority over corresponded key settings\n" "\n"\ @@ -5065,9 +5065,9 @@ static int CmdHF14ADesCreateApp(const char *Cmd) { " 6E = with FID, 3TDEA, 14 keys\n"\ " AE = with FID, AES, 14 keys\n"\ "\n"\ - "hf mfdes createid --rawdata 123456 -> execute create by rawdata\n"\ - "hf mfdes createid --aid 123456 --fid 2345 --dfname aid123456 -> app aid, iso file id, and iso df name is specified\n" - "hf mfdes createid --aid 123456 --fid 2345 --dfname aid123456 --dstalgo aes -> with algorithm for key AES"); + "hf mfdes createaid --rawdata 123456 -> execute create by rawdata\n"\ + "hf mfdes createaid --aid 123456 --fid 2345 --dfname aid123456 -> app aid, iso file id, and iso df name is specified\n" + "hf mfdes createaid --aid 123456 --fid 2345 --dfname aid123456 --dstalgo aes -> with algorithm for key AES"); void *argtable[] = { arg_param_begin, @@ -5156,9 +5156,14 @@ static int CmdHF14ADesCreateApp(const char *Cmd) { } if (keycount > 0x0e || keycount < 1) { - PrintAndLogEx(ERR, "Key count must be in the range 0x01..0x0e"); + PrintAndLogEx(ERR, "Key count must be in the range 1..14"); return PM3_ESOFT; } + + if (dfnamelen > 16) { + PrintAndLogEx(ERR, "DF name must be a maximum of 16 bytes in length"); + return PM3_EINVARG; + } res = DesfireSelectAndAuthenticate(&dctx, securechann, 0x000000, verbose); if (res != PM3_SUCCESS) { @@ -5178,11 +5183,13 @@ static int CmdHF14ADesCreateApp(const char *Cmd) { if (!ks2present) { if (keycount > 0) { - //data[4] keycount + data[4] &= 0xf0; + data[4] |= keycount & 0x0f; } - //data[4] dstalgo - } - + uint8_t kt = DesfireKeyAlgoToType(dstalgo); + data[4] &= 0x3f; + data[4] |= (kt & 0x03) << 6; + } datalen = 5; if (fileidpresent || (data[4] & 0x20) != 0) { @@ -5190,7 +5197,7 @@ static int CmdHF14ADesCreateApp(const char *Cmd) { data[6] = (fileid >> 8) & 0xff; data[4] |= 0x20; // set bit FileID in the ks2 memcpy(&data[7], dfname, dfnamelen); - datalen = 7 + 16; + datalen = 7 + dfnamelen; } } diff --git a/client/src/mifare/desfirecore.c b/client/src/mifare/desfirecore.c index df9427941..216a62a1a 100644 --- a/client/src/mifare/desfirecore.c +++ b/client/src/mifare/desfirecore.c @@ -988,6 +988,15 @@ int DesfireChangeKeySettings(DesfireContext *dctx, uint8_t *data, size_t len) { return DesfireCommandTxData(dctx, MFDES_CHANGE_KEY_SETTINGS, data, len); } +uint8_t DesfireKeyAlgoToType(DesfireCryptoAlgorythm keyType) { + switch(keyType) { + case T_DES: return 0x00; + case T_3DES: return 0x00; + case T_3K3DES: return 0x01; + case T_AES: return 0x02; + } + return 0; +} static void PrintKeyType(uint8_t keytype) { switch (keytype) { case 00: diff --git a/client/src/mifare/desfirecore.h b/client/src/mifare/desfirecore.h index acc7bd7af..c6b092b59 100644 --- a/client/src/mifare/desfirecore.h +++ b/client/src/mifare/desfirecore.h @@ -53,5 +53,6 @@ int DesfireGetKeyVersion(DesfireContext *dctx, uint8_t *data, size_t len, uint8_ int DesfireGetKeySettings(DesfireContext *dctx, uint8_t *resp, size_t *resplen); int DesfireChangeKeySettings(DesfireContext *dctx, uint8_t *data, size_t len); void PrintKeySettings(uint8_t keysettings, uint8_t numkeys, bool applevel, bool print2ndbyte); +uint8_t DesfireKeyAlgoToType(DesfireCryptoAlgorythm keyType); #endif // __DESFIRECORE_H