diff --git a/test_iso7816_helpers.c b/test_iso7816_helpers.c index 292fefbf4..e433764f2 100644 --- a/test_iso7816_helpers.c +++ b/test_iso7816_helpers.c @@ -1,4 +1,5 @@ #include +#include #include "lib/nfc/helpers/iso7816.h" @@ -6,6 +7,8 @@ #define COLOR_GREEN "\033[0;32m" #define COLOR_RESET "\033[0;0m" +#define num_elements(A) (sizeof(A)/sizeof(A[0])) + //TODO: do something with ISO7816-4 Table 9 — Interindustry data objects for tag allocation authority //0x06 Object identifier (encoding specified in ISO/IEC 8825-1, see examples in annex A) //0x41 Country code (encoding specified in ISO 3166-1 [1] ) and optional national data @@ -24,6 +27,24 @@ void print_tlv(char* fmt, TlvInfo tlv) { printf("\n"); } +int tlv_number(TlvInfo tlv) { + //TODO: negative numbers? + const char* str = tlv.value; + size_t length = tlv.length; + + int value = 0; + while(length--) { + char c = *(str++); + + if(c >= '0' && c <= '9') { + value = value * 10 + (c - '0'); + } else { + //TODO: warning? return? crash? + } + } + return value; +} + void test_iso7816_tlv_parse(const uint8_t* input, size_t input_size, uint16_t exp_tag, size_t exp_length) { TlvInfo tlv = iso7816_tlv_parse(input); @@ -185,12 +206,56 @@ int main(int argc, char** argv) { size_t ef_com_data_len = 24; describe_tlv(ef_com_data, ef_com_data_len, 0); - printf("====\n"); + uint16_t lds_tag_path[] = {0x60, 0x5f01}; + uint16_t unicode_tag_path[] = {0x60, 0x5f36}; + uint16_t tags_tag_path[] = {0x60, 0x5c}; - TlvInfo tlv = iso7816_tlv_select(ef_com_data, ef_com_data_len, (uint16_t[]){0x60, 0x5f36}, 2); - print_tlv("0x60, 0x5f36:", tlv); + TlvInfo tlv_lds_version = iso7816_tlv_select(ef_com_data, ef_com_data_len, lds_tag_path, num_elements(lds_tag_path)); + if(tlv_lds_version.tag) { + int tlv_version = tlv_number(tlv_lds_version); + printf("LDS Version: %d.%d (%.4s)\n", tlv_version/100, tlv_version%100, tlv_lds_version.value); + } else { + printf("Error, LDS info not found!\n"); + } + + TlvInfo tlv_unicode_version = iso7816_tlv_select(ef_com_data, ef_com_data_len, unicode_tag_path, num_elements(unicode_tag_path)); + if(tlv_unicode_version.tag) { + int unicode_version = tlv_number(tlv_unicode_version); + printf("Unicode Version: %d.%d.%d (%.6s)\n", unicode_version/10000, unicode_version/100%100, unicode_version%100, tlv_unicode_version.value); + } else { + printf("Error, Unicode info not found!\n"); + } + + TlvInfo tlv_tag_list = iso7816_tlv_select(ef_com_data, ef_com_data_len, tags_tag_path, num_elements(tags_tag_path)); + if(tlv_tag_list.tag) { + printf("Tag List:\n"); + for(size_t i=0; i