Skip to content

Commit 6c08565

Browse files
committed
Implement ATR debug event
* Add ATR debug event content type * Add debug macros for trace data and parsed ATR info * Move logging of parsed ATR to emv_atr_parse()
1 parent 23bacea commit 6c08565

File tree

6 files changed

+39
-14
lines changed

6 files changed

+39
-14
lines changed

src/emv.c

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ int emv_atr_parse(const void* atr, size_t atr_len)
7777
return EMV_OUTCOME_CARD_ERROR;
7878
}
7979
}
80+
emv_debug_atr_info(&atr_info);
8081

8182
// The intention of this function is to validate the ATR in accordance with
8283
// EMV Level 1 Contact Interface Specification v1.0, 8.3. Some of the

src/emv_debug.h

+24-1
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ enum emv_debug_level_t {
4949

5050
/// Debug event content type
5151
enum emv_debug_type_t {
52-
EMV_DEBUG_TYPE_MSG, ///< Debug event contains only a string message and no data
52+
EMV_DEBUG_TYPE_MSG = 1, ///< Debug event contains only a string message and no data
5353
EMV_DEBUG_TYPE_DATA, ///< Debug event contains binary data
5454
EMV_DEBUG_TYPE_TLV, ///< Debug event contains ISO 8825-1 BER encoded data
55+
EMV_DEBUG_TYPE_ATR, ///< Debug event contains ISO 7816 Answer To Reset (ATR) data
5556
EMV_DEBUG_TYPE_CAPDU, ///< Debug event contains ISO 7816 C-APDU (command APDU) data
5657
EMV_DEBUG_TYPE_RAPDU, ///< Debug event contains ISO 7816 R-APDU (response APDU) data
5758
EMV_DEBUG_TYPE_CTPDU, ///< Debug event contains ISO 7816 C-TPDU (request TPDU) data
@@ -161,6 +162,18 @@ void emv_debug_internal(
161162
#define emv_debug_info_tlv(fmt, buf, buf_len, ...) do {} while (0)
162163
#endif // EMV_DEBUG_ENABLED
163164

165+
#if defined(EMV_DEBUG_ENABLED) && !defined(EMV_DEBUG_CARD_DISABLED)
166+
167+
/**
168+
* Emit debug event with decoded ISO 7816 ATR
169+
* @param atr_info Pointer to parsed ATR info (@ref iso7816_atr_info_t)
170+
*/
171+
#define emv_debug_atr_info(atr_info) do { emv_debug_internal(EMV_DEBUG_SOURCE, EMV_DEBUG_CARD, EMV_DEBUG_TYPE_ATR, "ATR", atr_info, sizeof(*atr_info)); } while (0)
172+
173+
#else // EMV_DEBUG_ENABLED && !EMV_DEBUG_CARD_DISABLED
174+
#define emv_debug_atr_info(atr_info) do {} while (0)
175+
#endif // EMV_DEBUG_ENABLED && !EMV_DEBUG_CARD_DISABLED
176+
164177
#if defined(EMV_DEBUG_ENABLED) && !defined(EMV_DEBUG_CARD_DISABLED) && !defined(EMV_DEBUG_APDU_DISABLED)
165178

166179
/**
@@ -220,8 +233,18 @@ void emv_debug_internal(
220233
*/
221234
#define emv_debug_trace_msg(fmt, ...) do { emv_debug_internal(EMV_DEBUG_SOURCE, EMV_DEBUG_TRACE, EMV_DEBUG_TYPE_MSG, "%s[%u]: "fmt, NULL, 0, __FILE__, __LINE__, ##__VA_ARGS__); } while (0)
222235

236+
/**
237+
* Emit debug trace data
238+
* @param fmt Format string (printf-style)
239+
* @param buf Debug event data
240+
* @param buf_len Length of debug event data in bytes
241+
* @param ... Variable arguments for @c fmt
242+
*/
243+
#define emv_debug_trace_data(fmt, buf, buf_len, ...) do { emv_debug_internal(EMV_DEBUG_SOURCE, EMV_DEBUG_TRACE, EMV_DEBUG_TYPE_DATA, fmt, buf, buf_len, ##__VA_ARGS__); } while (0)
244+
223245
#else // EMV_DEBUG_ENABLED && !EMV_DEBUG_TRACE_DISABLED
224246
#define emv_debug_trace_msg(...) do {} while (0)
247+
#define emv_debug_trace_data(...) do {} while (0)
225248
#endif // EMV_DEBUG_ENABLED && !EMV_DEBUG_TRACE_DISABLED
226249

227250
__END_DECLS

tests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ if (BUILD_TESTING)
1313
add_test(emv_debug_test emv_debug_test)
1414
string(CONCAT emv_debug_test_regex
1515
"\\[APP\\] asdf=1337[\r\n]"
16+
"\\[APP\\] This is a trace message: DEADBEEF[\r\n]"
1617
)
1718
set_tests_properties(emv_debug_test
1819
PROPERTIES

tests/emv_debug_test.c

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ int main(void)
3030
{
3131
int r;
3232
size_t asdf = 1337;
33+
uint8_t foo[] = { 0xde, 0xad, 0xbe, 0xef };
3334

3435
// Enable debug output
3536
r = emv_debug_init(EMV_DEBUG_SOURCE_ALL, EMV_DEBUG_ALL, &print_emv_debug);
@@ -40,4 +41,7 @@ int main(void)
4041

4142
// Test whether compiler supports length modifier z both at compile-time and runtime
4243
emv_debug_error("asdf=%zu", asdf);
44+
45+
// Test trace logging and data output
46+
emv_debug_trace_data("This is a trace message", foo, sizeof(foo));
4347
}

tools/emv-tool.c

+1-10
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
#include "emv.h"
2323
#include "pcsc.h"
24-
#include "iso7816.h"
2524
#include "print_helpers.h"
2625
#include "emv_tags.h"
2726
#include "emv_fields.h"
@@ -386,7 +385,6 @@ int main(int argc, char** argv)
386385
size_t reader_idx;
387386
uint8_t atr[PCSC_MAX_ATR_SIZE];
388387
size_t atr_len = 0;
389-
struct iso7816_atr_info_t atr_info;
390388

391389
if (argc == 1) {
392390
// No command line arguments
@@ -492,14 +490,7 @@ int main(int argc, char** argv)
492490
printf("Failed to retrieve ATR\n");
493491
goto pcsc_exit;
494492
}
495-
496-
r = iso7816_atr_parse(atr, atr_len, &atr_info);
497-
if (r) {
498-
printf("Failed to parse ATR\n");
499-
goto pcsc_exit;
500-
}
501-
502-
print_atr(&atr_info);
493+
emv_debug_trace_data("ATR", atr, atr_len);
503494

504495
r = emv_atr_parse(atr, atr_len);
505496
if (r < 0) {

tools/print_helpers.c

+8-3
Original file line numberDiff line numberDiff line change
@@ -510,15 +510,20 @@ static void print_emv_debug_internal(
510510
printf("%s\n", str);
511511
return;
512512
} else {
513-
printf("%s: ", str);
514-
print_buf(NULL, buf, buf_len);
515-
516513
switch (debug_type) {
517514
case EMV_DEBUG_TYPE_TLV:
515+
printf("%s: ", str);
516+
print_buf(NULL, buf, buf_len);
518517
print_emv_buf(buf, buf_len, " ", 1);
519518
return;
520519

520+
case EMV_DEBUG_TYPE_ATR:
521+
print_atr(buf);
522+
return;
523+
521524
default:
525+
printf("%s: ", str);
526+
print_buf(NULL, buf, buf_len);
522527
return;
523528
}
524529
}

0 commit comments

Comments
 (0)