emv_decoder.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "emv_decoder.h"
  2. const PDOLValue pdol_term_info = {0x9F59, {0xC8, 0x80, 0x00}}; // Terminal transaction information
  3. const PDOLValue pdol_term_type = {0x9F5A, {0x00}}; // Terminal transaction type
  4. const PDOLValue pdol_merchant_type = {0x9F58, {0x01}}; // Merchant type indicator
  5. const PDOLValue pdol_term_trans_qualifies = {
  6. 0x9F66,
  7. {0x79, 0x00, 0x40, 0x80}}; // Terminal transaction qualifiers
  8. const PDOLValue pdol_amount_authorise = {
  9. 0x9F02,
  10. {0x00, 0x00, 0x00, 0x10, 0x00, 0x00}}; // Amount, authorised
  11. const PDOLValue pdol_amount = {0x9F03, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}; // Amount
  12. const PDOLValue pdol_country_code = {0x9F1A, {0x01, 0x24}}; // Terminal country code
  13. const PDOLValue pdol_currency_code = {0x5F2A, {0x01, 0x24}}; // Transaction currency code
  14. const PDOLValue pdol_term_verification = {
  15. 0x95,
  16. {0x00, 0x00, 0x00, 0x00, 0x00}}; // Terminal verification results
  17. const PDOLValue pdol_transaction_date = {0x9A, {0x19, 0x01, 0x01}}; // Transaction date
  18. const PDOLValue pdol_transaction_type = {0x9C, {0x00}}; // Transaction type
  19. const PDOLValue pdol_transaction_cert = {0x98, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  20. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}; // Transaction cert
  21. const PDOLValue pdol_unpredict_number = {0x9F37, {0x82, 0x3D, 0xDE, 0x7A}}; // Unpredictable number
  22. const PDOLValue* pdol_values[] = {
  23. &pdol_term_info,
  24. &pdol_term_type,
  25. &pdol_merchant_type,
  26. &pdol_term_trans_qualifies,
  27. &pdol_amount_authorise,
  28. &pdol_amount,
  29. &pdol_country_code,
  30. &pdol_currency_code,
  31. &pdol_term_verification,
  32. &pdol_transaction_date,
  33. &pdol_transaction_type,
  34. &pdol_transaction_cert,
  35. &pdol_unpredict_number,
  36. };
  37. static uint16_t emv_parse_TLV(uint8_t* dest, uint8_t* src, uint16_t* idx) {
  38. uint8_t len = src[*idx + 1];
  39. memcpy(dest, &src[*idx + 2], len);
  40. *idx = *idx + len + 1;
  41. return len;
  42. }
  43. uint16_t emv_prepare_select_ppse(uint8_t* dest) {
  44. const uint8_t emv_select_ppse[] = {
  45. 0x00, 0xA4, // SELECT ppse
  46. 0x04, 0x00, // P1:By name, P2: empty
  47. 0x0e, // Lc: Data length
  48. 0x32, 0x50, 0x41, 0x59, 0x2e, 0x53, 0x59, // Data string:
  49. 0x53, 0x2e, 0x44, 0x44, 0x46, 0x30, 0x31, // 2PAY.SYS.DDF01 (PPSE)
  50. 0x00 // Le
  51. };
  52. memcpy(dest, emv_select_ppse, sizeof(emv_select_ppse));
  53. return sizeof(emv_select_ppse);
  54. }
  55. bool emv_decode_ppse_response(uint8_t* buff, uint16_t len, EmvApplication* app) {
  56. uint16_t i = 0;
  57. bool app_aid_found = false;
  58. while(i < len) {
  59. if(buff[i] == EMV_TAG_APP_TEMPLATE) {
  60. uint8_t app_len = buff[++i];
  61. for(uint16_t j = i; j < i + app_len; j++) {
  62. if(buff[j] == EMV_TAG_AID) {
  63. app_aid_found = true;
  64. app->aid_len = buff[j + 1];
  65. emv_parse_TLV(app->aid, buff, &j);
  66. } else if(buff[j] == EMV_TAG_PRIORITY) {
  67. emv_parse_TLV(&app->priority, buff, &j);
  68. }
  69. }
  70. i += app_len;
  71. }
  72. i++;
  73. }
  74. return app_aid_found;
  75. }
  76. uint16_t emv_prepare_select_app(uint8_t* dest, EmvApplication* app) {
  77. const uint8_t emv_select_header[] = {
  78. 0x00,
  79. 0xA4, // SELECT application
  80. 0x04,
  81. 0x00 // P1:By name, P2:First or only occurence
  82. };
  83. uint16_t size = sizeof(emv_select_header);
  84. // Copy header
  85. memcpy(dest, emv_select_header, size);
  86. // Copy AID
  87. dest[size++] = app->aid_len;
  88. memcpy(&dest[size], app->aid, app->aid_len);
  89. size += app->aid_len;
  90. dest[size++] = 0;
  91. return size;
  92. }
  93. bool emv_decode_select_app_response(uint8_t* buff, uint16_t len, EmvApplication* app) {
  94. uint16_t i = 0;
  95. bool found_name = false;
  96. while(i < len) {
  97. if(buff[i] == EMV_TAG_CARD_NAME) {
  98. uint8_t name_len = buff[i + 1];
  99. emv_parse_TLV((uint8_t*)app->name, buff, &i);
  100. app->name[name_len] = '\0';
  101. found_name = true;
  102. } else if(((buff[i] << 8) | buff[i + 1]) == EMV_TAG_PDOL) {
  103. i++;
  104. app->pdol.size = emv_parse_TLV(app->pdol.data, buff, &i);
  105. }
  106. i++;
  107. }
  108. return found_name;
  109. }
  110. static uint16_t emv_prepare_pdol(APDU* dest, APDU* src) {
  111. bool tag_found;
  112. for(uint16_t i = 0; i < src->size; i++) {
  113. tag_found = false;
  114. for(uint8_t j = 0; j < sizeof(pdol_values) / sizeof(PDOLValue*); j++) {
  115. if(src->data[i] == pdol_values[j]->tag) {
  116. // Found tag with 1 byte length
  117. uint8_t len = src->data[++i];
  118. memcpy(dest->data + dest->size, pdol_values[j]->data, len);
  119. dest->size += len;
  120. tag_found = true;
  121. break;
  122. } else if(((src->data[i] << 8) | src->data[i + 1]) == pdol_values[j]->tag) {
  123. // Found tag with 2 byte length
  124. i += 2;
  125. uint8_t len = src->data[i];
  126. memcpy(dest->data + dest->size, pdol_values[j]->data, len);
  127. dest->size += len;
  128. tag_found = true;
  129. break;
  130. }
  131. }
  132. if(!tag_found) {
  133. // Unknown tag, fill zeros
  134. i += 2;
  135. uint8_t len = src->data[i];
  136. memset(dest->data + dest->size, 0, len);
  137. dest->size += len;
  138. }
  139. }
  140. return dest->size;
  141. }
  142. uint16_t emv_prepare_get_proc_opt(uint8_t* dest, EmvApplication* app) {
  143. // Get processing option header
  144. const uint8_t emv_gpo_header[] = {0x80, 0xA8, 0x00, 0x00};
  145. uint16_t size = sizeof(emv_gpo_header);
  146. // Copy header
  147. memcpy(dest, emv_gpo_header, size);
  148. APDU pdol_data = {0, {0}};
  149. // Prepare and copy pdol parameters
  150. emv_prepare_pdol(&pdol_data, &app->pdol);
  151. dest[size++] = 0x02 + pdol_data.size;
  152. dest[size++] = 0x83;
  153. dest[size++] = pdol_data.size;
  154. memcpy(dest + size, pdol_data.data, pdol_data.size);
  155. size += pdol_data.size;
  156. dest[size++] = 0;
  157. return size;
  158. }
  159. bool emv_decode_get_proc_opt(uint8_t* buff, uint16_t len, EmvApplication* app) {
  160. for(uint16_t i = 0; i < len; i++) {
  161. if(buff[i] == EMV_TAG_CARD_NUM) {
  162. memcpy(app->card_number, &buff[i + 2], 8);
  163. return true;
  164. } else if(buff[i] == EMV_TAG_AFL) {
  165. app->afl.size = emv_parse_TLV(app->afl.data, buff, &i);
  166. }
  167. }
  168. return false;
  169. }
  170. uint16_t emv_prepare_read_sfi_record(uint8_t* dest, uint8_t sfi, uint8_t record_num) {
  171. const uint8_t sfi_param = (sfi << 3) | (1 << 2);
  172. const uint8_t emv_sfi_header[] = {
  173. 0x00,
  174. 0xB2, // READ RECORD
  175. record_num,
  176. sfi_param, // P1:record_number and P2:SFI
  177. 0x00 // Le
  178. };
  179. uint16_t size = sizeof(emv_sfi_header);
  180. memcpy(dest, emv_sfi_header, size);
  181. return size;
  182. }
  183. bool emv_decode_read_sfi_record(uint8_t* buff, uint16_t len, EmvApplication* app) {
  184. for(uint16_t i = 0; i < len; i++) {
  185. if(buff[i] == EMV_TAG_PAN) {
  186. memcpy(app->card_number, &buff[i + 2], 8);
  187. return true;
  188. }
  189. }
  190. return false;
  191. }