From 997cd7577ff9b0f38a8ad05ce68d9cbd48310bc5 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Tue, 4 May 2021 15:59:21 +0200 Subject: [PATCH] added 'data asn1' - decodes asn1 byte arrays --- client/src/cmddata.c | 29 +++++++++++++++++++++++++++++ client/src/crypto/asn1utils.c | 4 ++-- client/src/emv/emvcore.c | 6 +++--- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/client/src/cmddata.c b/client/src/cmddata.c index 1e96e92d6..1f10ceb18 100644 --- a/client/src/cmddata.c +++ b/client/src/cmddata.c @@ -28,6 +28,7 @@ #include "mifare/ndef.h" #include "cliparser.h" #include "cmdlft55xx.h" // print... +#include "crypto/asn1utils.h" // ASN1 decode / print uint8_t DemodBuffer[MAX_DEMOD_BUF_LEN]; size_t DemodBufferLen = 0; @@ -2882,6 +2883,33 @@ static int CmdDataModulationSearch(const char *Cmd) { return try_detect_modulation(); } +static int CmdAsn1Decoder(const char* Cmd) { + + CLIParserContext *ctx; + CLIParserInit(&ctx, "data asn1", + "Decode ASN1 bytearray\n" + "", + "data asn1 -d 303381050186922305a5020500a6088101010403030008a7188516eeee4facacf4fbde5e5c49d95e55bfbca74267b02407a9020500\n" + ); + + void *argtable[] = { + arg_param_begin, + arg_str1("d", NULL, "", "ASN1 encoded byte array"), + arg_param_end + }; + CLIExecWithReturn(ctx, Cmd, argtable, false); + int dlen = 256; + uint8_t data[256]; + CLIGetHexWithReturn(ctx, 1, data, &dlen); + CLIParserFree(ctx); + + // print ASN1 decoded array in TLV view + PrintAndLogEx(INFO, "---------------- " _CYAN_("ASN1 TLV") " -----------------"); + asn1_print(data, dlen, " "); + PrintAndLogEx(NORMAL, ""); + return PM3_SUCCESS; +} + static command_t CommandTable[] = { {"help", CmdHelp, AlwaysAvailable, "This help"}, @@ -2916,6 +2944,7 @@ static command_t CommandTable[] = { {"getbitstream", CmdGetBitStream, AlwaysAvailable, "Convert GraphBuffer's >=1 values to 1 and <1 to 0"}, {"-----------", CmdHelp, AlwaysAvailable, "------------------------- " _CYAN_("General") "-------------------------"}, + {"asn1", CmdAsn1Decoder, AlwaysAvailable, "asn1 decoder"}, {"bin2hex", Cmdbin2hex, AlwaysAvailable, "Converts binary to hexadecimal"}, {"bitsamples", CmdBitsamples, IfPm3Present, "Get raw samples as bitstring"}, {"clear", CmdBuffClear, AlwaysAvailable, "Clears bigbuf on deviceside and graph window"}, diff --git a/client/src/crypto/asn1utils.c b/client/src/crypto/asn1utils.c index a076f207d..9f2dfd5b9 100644 --- a/client/src/crypto/asn1utils.c +++ b/client/src/crypto/asn1utils.c @@ -61,7 +61,7 @@ exit: return res; } -static void print_cb(void *data, const struct tlv *tlv, int level, bool is_leaf) { +static void asn1_print_cb(void *data, const struct tlv *tlv, int level, bool is_leaf) { bool candump = true; asn1_tag_dump(tlv, level, &candump); if (is_leaf && candump) { @@ -73,7 +73,7 @@ int asn1_print(uint8_t *asn1buf, size_t asn1buflen, const char *indent) { struct tlvdb *t = tlvdb_parse_multi(asn1buf, asn1buflen); if (t) { - tlvdb_visit(t, print_cb, NULL, 0); + tlvdb_visit(t, asn1_print_cb, NULL, 0); tlvdb_free(t); } else { PrintAndLogEx(ERR, "Can't parse data as TLV tree"); diff --git a/client/src/emv/emvcore.c b/client/src/emv/emvcore.c index 4ab081fff..9073d086a 100644 --- a/client/src/emv/emvcore.c +++ b/client/src/emv/emvcore.c @@ -157,7 +157,7 @@ enum CardPSVendor GetCardPSVendor(uint8_t *AID, size_t AIDlen) { return CV_NA; } -static void print_cb(void *data, const struct tlv *tlv, int level, bool is_leaf) { +static void emv_print_cb(void *data, const struct tlv *tlv, int level, bool is_leaf) { emv_tag_dump(tlv, level); if (is_leaf) { print_buffer(tlv->value, tlv->len, level); @@ -169,7 +169,7 @@ bool TLVPrintFromBuffer(uint8_t *data, int datalen) { if (t) { PrintAndLogEx(INFO, "-------------------- " _CYAN_("TLV decoded") " --------------------"); - tlvdb_visit(t, print_cb, NULL, 0); + tlvdb_visit(t, emv_print_cb, NULL, 0); tlvdb_free(t); return true; } else { @@ -182,7 +182,7 @@ void TLVPrintFromTLVLev(struct tlvdb *tlv, int level) { if (!tlv) return; - tlvdb_visit(tlv, print_cb, NULL, level); + tlvdb_visit(tlv, emv_print_cb, NULL, level); } void TLVPrintFromTLV(struct tlvdb *tlv) {