style
[RRG-proxmark3.git] / client / src / cmdhfthinfilm.c
blob74a76e78808c653469387771f03380f6caf96fd0
1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // See LICENSE.txt for the text of the license.
15 //-----------------------------------------------------------------------------
16 // Thinfilm commands
17 //-----------------------------------------------------------------------------
18 #include "cmdhfthinfilm.h"
20 #include <stdio.h>
21 #include <string.h>
22 #include <ctype.h>
23 #include "cliparser.h"
24 #include "cmdparser.h" // command_t
25 #include "comms.h"
26 #include "cmdtrace.h"
27 #include "crc16.h"
28 #include "ui.h"
29 #include "cmdhf14a.h" // manufacture
31 static int CmdHelp(const char *Cmd);
33 // Printing function based upon the code in libnfc
34 // ref
35 // https://github.com/nfc-tools/libnfc/blob/master/utils/nfc-barcode.c
36 static int print_barcode(uint8_t *barcode, const size_t barcode_len, bool verbose) {
38 PrintAndLogEx(NORMAL, "");
39 // remove start bit
40 uint8_t mb = barcode[0] & ~0x80;
41 PrintAndLogEx(SUCCESS, " Manufacturer : "_YELLOW_("%s") "[0x%02X]", getTagInfo(mb), mb);
43 if (verbose) {
44 PrintAndLogEx(SUCCESS, " Data format : "_YELLOW_("%02X"), barcode[1]);
45 if (barcode_len > 2) {
46 uint8_t b1 = 0, b2 = 0;
47 compute_crc(CRC_14443_A, barcode, barcode_len - 2, &b1, &b2);
48 bool isok = (barcode[barcode_len - 1] == b1 && barcode[barcode_len - 2] == b2);
50 PrintAndLogEx(SUCCESS, " Checksum : "_YELLOW_("%02X %02X")" ( %s )", b2, b1, (isok) ? _GREEN_("ok") : _RED_("fail"));
51 } else {
52 PrintAndLogEx(SUCCESS, " Checksum : "_YELLOW_("too few data for checksum")" - " _RED_("fail"));
54 PrintAndLogEx(SUCCESS, " Data len (bits) : "_YELLOW_("%zu")" ( %s )", barcode_len * 8, (barcode_len == 16 || barcode_len == 32) ? _GREEN_("ok") : _YELLOW_("warning"));
55 PrintAndLogEx(SUCCESS, " Raw data : "_YELLOW_("%s"), sprint_hex(barcode, barcode_len));
56 if (barcode_len < 4) // too few to go to next decoding stages
57 return PM3_ESOFT;
60 char s[45];
61 memset(s, 0x00, sizeof(s));
63 switch (barcode[1]) {
64 case 0:
65 PrintAndLogEx(SUCCESS, " Data format : Reserved for allocation by tag manufacturer");
66 return PM3_SUCCESS;
67 case 1:
68 snprintf(s, sizeof(s), "http://www.");
69 break;
70 case 2:
71 snprintf(s, sizeof(s), "https://www.");
72 break;
73 case 3:
74 snprintf(s, sizeof(s), "http://");
75 break;
76 case 4:
77 snprintf(s, sizeof(s), "https://");
78 break;
79 case 5:
80 if (barcode_len < 16) {
81 PrintAndLogEx(WARNING, "EPC: (partial data) %s", sprint_hex(barcode + 2, barcode_len - 2));
82 return PM3_ESOFT;
84 PrintAndLogEx(SUCCESS, "EPC: %s", sprint_hex(barcode + 2, 12));
85 return PM3_SUCCESS;
86 default:
87 PrintAndLogEx(SUCCESS, " Data format : RFU Reserved for future use (%02X)", barcode[1]);
88 if (!verbose)
89 PrintAndLogEx(SUCCESS, "Raw data with CRC: "_YELLOW_("%s"), sprint_hex(barcode, barcode_len));
90 return PM3_SUCCESS;
93 snprintf(s + strlen(s), barcode_len - 3, (const char *)&barcode[2], barcode_len - 4);
95 for (size_t i = 0; i < strlen(s); i++) {
97 // terminate string
98 if ((uint8_t) s[i] == 0xFE) {
99 s[i] = 0;
100 break;
103 PrintAndLogEx(SUCCESS, " Decoded NFC URL : "_YELLOW_("%s"), s);
104 return PM3_SUCCESS;
107 int CmdHfThinFilmInfo(const char *Cmd) {
108 CLIParserContext *ctx;
109 CLIParserInit(&ctx, "hf thinfilm info",
110 "Get info from Thinfilm tags",
111 "hf thinfilm info");
113 void *argtable[] = {
114 arg_param_begin,
115 arg_param_end
117 CLIExecWithReturn(ctx, Cmd, argtable, true);
118 CLIParserFree(ctx);
119 return infoThinFilm(true);
122 int infoThinFilm(bool verbose) {
124 clearCommandBuffer();
125 SendCommandNG(CMD_HF_THINFILM_READ, NULL, 0);
127 PacketResponseNG resp;
128 if (!WaitForResponseTimeout(CMD_HF_THINFILM_READ, &resp, 1500)) {
129 PrintAndLogEx(WARNING, "timeout while waiting for reply.");
130 return PM3_ETIMEOUT;
133 if (resp.status == PM3_SUCCESS) {
134 if (resp.length == 16 || resp.length == 32) {
135 PrintAndLogEx(NORMAL, "");
136 PrintAndLogEx(INFO, "--- " _CYAN_("Tag Information") " ---------------------------");
137 print_barcode(resp.data.asBytes, resp.length, verbose);
138 } else {
139 if (verbose)
140 PrintAndLogEx(WARNING, "Response is wrong length. (%d)", resp.length);
142 return PM3_ESOFT;
146 return resp.status;
149 int CmdHfThinFilmSim(const char *Cmd) {
150 CLIParserContext *ctx;
151 CLIParserInit(&ctx, "hf thinfilm sim",
152 "Simulate Thinfilm tag",
153 "hf thinfilm sim -d B70470726f786d61726b2e636f6d");
155 void *argtable[] = {
156 arg_param_begin,
157 arg_str1("d", "data", "<hex>", "bytes to send"),
158 arg_lit0(NULL, "raw", "raw, provided bytes should include CRC"),
159 arg_param_end
161 CLIExecWithReturn(ctx, Cmd, argtable, false);
163 int data_len = 0;
164 uint8_t data[512] = {0};
165 CLIGetHexWithReturn(ctx, 1, data, &data_len);
167 bool addcrc = true;
169 if (arg_get_lit(ctx, 2)) {
170 addcrc = false;
173 CLIParserFree(ctx);
175 if (addcrc && data_len <= 510) {
176 uint8_t b1 = 0, b2 = 0;
177 compute_crc(CRC_14443_A, data, data_len, &b1, &b2);
178 data[data_len++] = b2;
179 data[data_len++] = b1;
182 clearCommandBuffer();
183 SendCommandNG(CMD_HF_THINFILM_SIMULATE, (uint8_t *)&data, data_len);
184 PacketResponseNG resp;
185 PrintAndLogEx(SUCCESS, "press " _GREEN_("pm3 button") " to abort simulation");
187 int ret;
188 while (!(ret = kbd_enter_pressed())) {
189 if (WaitForResponseTimeout(CMD_HF_THINFILM_SIMULATE, &resp, 500) == 0) continue;
190 if (resp.status != PM3_SUCCESS) break;
192 if (ret) {
193 PrintAndLogEx(INFO, "Client side interrupted");
194 PrintAndLogEx(WARNING, "Simulation still running on Proxmark3 till next command or button press");
195 } else {
196 PrintAndLogEx(INFO, "Done!");
198 return PM3_SUCCESS;
201 static int CmdHfThinFilmList(const char *Cmd) {
202 return CmdTraceListAlias(Cmd, "hf thinfilm", "thinfilm");
205 static command_t CommandTable[] = {
206 {"help", CmdHelp, AlwaysAvailable, "This help"},
207 {"info", CmdHfThinFilmInfo, IfPm3NfcBarcode, "Tag information"},
208 {"list", CmdHfThinFilmList, AlwaysAvailable, "List NFC Barcode / Thinfilm history - not correct"},
209 {"sim", CmdHfThinFilmSim, IfPm3NfcBarcode, "Fake Thinfilm tag"},
210 {NULL, NULL, NULL, NULL}
213 static int CmdHelp(const char *Cmd) {
214 (void)Cmd; // Cmd is not used so far
215 CmdsHelp(CommandTable);
216 return PM3_SUCCESS;
219 int CmdHFThinfilm(const char *Cmd) {
220 clearCommandBuffer();
221 return CmdsParse(CommandTable, Cmd);