emv_decoder.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 const uint8_t select_ppse_ans[] = {
  38. 0x6F, 0x29, 0x84, 0x0E, 0x32, 0x50, 0x41, 0x59, 0x2E,
  39. 0x53, 0x59, 0x53, 0x2E, 0x44, 0x44, 0x46, 0x30, 0x31,
  40. 0xA5, 0x17, 0xBF, 0x0C, 0x14, 0x61, 0x12, 0x4F, 0x07,
  41. 0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10, 0x50, 0x04,
  42. 0x56, 0x49, 0x53, 0x41, 0x87, 0x01, 0x01, 0x90, 0x00};
  43. static const uint8_t select_app_ans[] = {
  44. 0x6F, 0x20, 0x84, 0x07, 0xA0, 0x00, 0x00, 0x00, 0x03,
  45. 0x10, 0x10, 0xA5, 0x15, 0x50, 0x04, 0x56, 0x49, 0x53,
  46. 0x41, 0x9F, 0x38, 0x0C, 0x9F, 0x66, 0x04, 0x9F, 0x02,
  47. 0x06, 0x9F, 0x37, 0x04, 0x5F, 0x2A, 0x02, 0x90, 0x00};
  48. static const uint8_t pdol_ans[] = {
  49. 0x77, 0x40, 0x82, 0x02, 0x20, 0x00, 0x57, 0x13, 0x55, 0x70, 0x73, 0x83,
  50. 0x85, 0x87, 0x73, 0x31, 0xD1, 0x80, 0x22, 0x01, 0x38, 0x84, 0x77, 0x94,
  51. 0x00, 0x00, 0x1F, 0x5F, 0x34, 0x01, 0x00, 0x9F, 0x10, 0x07, 0x06, 0x01,
  52. 0x11, 0x03, 0x80, 0x00, 0x00, 0x9F, 0x26, 0x08, 0x7A, 0x65, 0x7F, 0xD3,
  53. 0x52, 0x96, 0xC9, 0x85, 0x9F, 0x27, 0x01, 0x00, 0x9F, 0x36, 0x02, 0x06,
  54. 0x0C, 0x9F, 0x6C, 0x02, 0x10, 0x00, 0x90, 0x00};
  55. static uint16_t emv_parse_TLV(uint8_t* dest, uint8_t* src, uint16_t* idx) {
  56. uint8_t len = src[*idx + 1];
  57. memcpy(dest, &src[*idx + 2], len);
  58. *idx = *idx + len + 1;
  59. return len;
  60. }
  61. static bool emv_decode_search_tag_u16_r(uint16_t tag, uint8_t* buff, uint16_t* idx) {
  62. if((buff[*idx] << 8 | buff[*idx + 1]) == tag) {
  63. *idx = *idx + 3;
  64. return true;
  65. }
  66. return false;
  67. }
  68. uint16_t emv_prepare_select_ppse(uint8_t* dest) {
  69. const uint8_t emv_select_ppse[] = {
  70. 0x00, 0xA4, // SELECT ppse
  71. 0x04, 0x00, // P1:By name, P2: empty
  72. 0x0e, // Lc: Data length
  73. 0x32, 0x50, 0x41, 0x59, 0x2e, 0x53, 0x59, // Data string:
  74. 0x53, 0x2e, 0x44, 0x44, 0x46, 0x30, 0x31, // 2PAY.SYS.DDF01 (PPSE)
  75. 0x00 // Le
  76. };
  77. memcpy(dest, emv_select_ppse, sizeof(emv_select_ppse));
  78. return sizeof(emv_select_ppse);
  79. }
  80. bool emv_decode_ppse_response(uint8_t* buff, uint16_t len, EmvApplication* app) {
  81. uint16_t i = 0;
  82. bool app_aid_found = false;
  83. while(i < len) {
  84. if(buff[i] == EMV_TAG_APP_TEMPLATE) {
  85. uint8_t app_len = buff[++i];
  86. for(uint16_t j = i; j < i + app_len; j++) {
  87. if(buff[j] == EMV_TAG_AID) {
  88. app_aid_found = true;
  89. app->aid_len = buff[j + 1];
  90. emv_parse_TLV(app->aid, buff, &j);
  91. } else if(buff[j] == EMV_TAG_PRIORITY) {
  92. emv_parse_TLV(&app->priority, buff, &j);
  93. }
  94. }
  95. i += app_len;
  96. }
  97. i++;
  98. }
  99. return app_aid_found;
  100. }
  101. uint16_t emv_prepare_select_app(uint8_t* dest, EmvApplication* app) {
  102. const uint8_t emv_select_header[] = {
  103. 0x00,
  104. 0xA4, // SELECT application
  105. 0x04,
  106. 0x00 // P1:By name, P2:First or only occurence
  107. };
  108. uint16_t size = sizeof(emv_select_header);
  109. // Copy header
  110. memcpy(dest, emv_select_header, size);
  111. // Copy AID
  112. dest[size++] = app->aid_len;
  113. memcpy(&dest[size], app->aid, app->aid_len);
  114. size += app->aid_len;
  115. dest[size++] = 0;
  116. return size;
  117. }
  118. bool emv_decode_select_app_response(uint8_t* buff, uint16_t len, EmvApplication* app) {
  119. uint16_t i = 0;
  120. bool found_name = false;
  121. while(i < len) {
  122. if(buff[i] == EMV_TAG_CARD_NAME) {
  123. uint8_t name_len = buff[i + 1];
  124. emv_parse_TLV((uint8_t*)app->name, buff, &i);
  125. app->name[name_len] = '\0';
  126. found_name = true;
  127. } else if(((buff[i] << 8) | buff[i + 1]) == EMV_TAG_PDOL) {
  128. i++;
  129. app->pdol.size = emv_parse_TLV(app->pdol.data, buff, &i);
  130. }
  131. i++;
  132. }
  133. return found_name;
  134. }
  135. static uint16_t emv_prepare_pdol(APDU* dest, APDU* src) {
  136. bool tag_found;
  137. for(uint16_t i = 0; i < src->size; i++) {
  138. tag_found = false;
  139. for(uint8_t j = 0; j < sizeof(pdol_values) / sizeof(PDOLValue*); j++) {
  140. if(src->data[i] == pdol_values[j]->tag) {
  141. // Found tag with 1 byte length
  142. uint8_t len = src->data[++i];
  143. memcpy(dest->data + dest->size, pdol_values[j]->data, len);
  144. dest->size += len;
  145. tag_found = true;
  146. break;
  147. } else if(((src->data[i] << 8) | src->data[i + 1]) == pdol_values[j]->tag) {
  148. // Found tag with 2 byte length
  149. i += 2;
  150. uint8_t len = src->data[i];
  151. memcpy(dest->data + dest->size, pdol_values[j]->data, len);
  152. dest->size += len;
  153. tag_found = true;
  154. break;
  155. }
  156. }
  157. if(!tag_found) {
  158. // Unknown tag, fill zeros
  159. i += 2;
  160. uint8_t len = src->data[i];
  161. memset(dest->data + dest->size, 0, len);
  162. dest->size += len;
  163. }
  164. }
  165. return dest->size;
  166. }
  167. uint16_t emv_prepare_get_proc_opt(uint8_t* dest, EmvApplication* app) {
  168. // Get processing option header
  169. const uint8_t emv_gpo_header[] = {0x80, 0xA8, 0x00, 0x00};
  170. uint16_t size = sizeof(emv_gpo_header);
  171. // Copy header
  172. memcpy(dest, emv_gpo_header, size);
  173. APDU pdol_data = {0, {0}};
  174. // Prepare and copy pdol parameters
  175. emv_prepare_pdol(&pdol_data, &app->pdol);
  176. dest[size++] = 0x02 + pdol_data.size;
  177. dest[size++] = 0x83;
  178. dest[size++] = pdol_data.size;
  179. memcpy(dest + size, pdol_data.data, pdol_data.size);
  180. size += pdol_data.size;
  181. dest[size++] = 0;
  182. return size;
  183. }
  184. bool emv_decode_get_proc_opt(uint8_t* buff, uint16_t len, EmvApplication* app) {
  185. for(uint16_t i = 0; i < len; i++) {
  186. if(buff[i] == EMV_TAG_CARD_NUM) {
  187. app->card_number_len = 8;
  188. memcpy(app->card_number, &buff[i + 2], app->card_number_len);
  189. return true;
  190. } else if(buff[i] == EMV_TAG_AFL) {
  191. app->afl.size = emv_parse_TLV(app->afl.data, buff, &i);
  192. }
  193. }
  194. return false;
  195. }
  196. uint16_t emv_prepare_read_sfi_record(uint8_t* dest, uint8_t sfi, uint8_t record_num) {
  197. const uint8_t sfi_param = (sfi << 3) | (1 << 2);
  198. const uint8_t emv_sfi_header[] = {
  199. 0x00,
  200. 0xB2, // READ RECORD
  201. record_num,
  202. sfi_param, // P1:record_number and P2:SFI
  203. 0x00 // Le
  204. };
  205. uint16_t size = sizeof(emv_sfi_header);
  206. memcpy(dest, emv_sfi_header, size);
  207. return size;
  208. }
  209. bool emv_decode_read_sfi_record(uint8_t* buff, uint16_t len, EmvApplication* app) {
  210. bool pan_parsed = false;
  211. for(uint16_t i = 0; i < len; i++) {
  212. if(buff[i] == EMV_TAG_PAN) {
  213. if(buff[i + 1] == 8 || buff[i + 1] == 10) {
  214. app->card_number_len = buff[i + 1];
  215. memcpy(app->card_number, &buff[i + 2], app->card_number_len);
  216. pan_parsed = true;
  217. }
  218. } else if(emv_decode_search_tag_u16_r(EMV_TAG_EXP_DATE, buff, &i)) {
  219. app->exp_year = buff[i++];
  220. app->exp_month = buff[i++];
  221. } else if(emv_decode_search_tag_u16_r(EMV_TAG_CURRENCY_CODE, buff, &i)) {
  222. app->currency_code = (buff[i] << 8) | buff[i + 1];
  223. i += 2;
  224. } else if(emv_decode_search_tag_u16_r(EMV_TAG_COUNTRY_CODE, buff, &i)) {
  225. app->country_code = (buff[i] << 8) | buff[i + 1];
  226. i += 2;
  227. }
  228. }
  229. return pan_parsed;
  230. }
  231. uint16_t emv_select_ppse_ans(uint8_t* buff) {
  232. memcpy(buff, select_ppse_ans, sizeof(select_ppse_ans));
  233. return sizeof(select_ppse_ans);
  234. }
  235. uint16_t emv_select_app_ans(uint8_t* buff) {
  236. memcpy(buff, select_app_ans, sizeof(select_app_ans));
  237. return sizeof(select_app_ans);
  238. }
  239. uint16_t emv_get_proc_opt_ans(uint8_t* buff) {
  240. memcpy(buff, pdol_ans, sizeof(pdol_ans));
  241. return sizeof(pdol_ans);
  242. }