t_1.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #include "t_1.h"
  2. #define TAG "Seader:T=1"
  3. // http://www.sat-digest.com/SatXpress/SmartCard/ISO7816-4.htm
  4. /* I know my T=1 is terrible, but I'm also only targetting one specific 'card' */
  5. #define MORE_BIT 0x20
  6. #define IFSD_VALUE 0xfe
  7. #define IFSC_VALUE 0xfe // Fom the SAM ATR
  8. #define R_BLOCK 0x80
  9. #define R_SEQUENCE_NUMBER_MASK 0x10
  10. // TODO: T1 struct
  11. uint8_t NAD = 0x00;
  12. uint8_t dPCB = 0x40; // Init to 0x40 so first call to next_pcb will return 0x00
  13. uint8_t cPCB = 0x00; // Init to 0x40 so first call to next_pcb will return 0x00
  14. uint8_t seader_next_dpcb() {
  15. uint8_t next_pcb = dPCB ^ 0x40;
  16. //FURI_LOG_D(TAG, "dPCB was: %02X, current dPCB: %02X", dPCB, next_pcb);
  17. dPCB = next_pcb;
  18. return dPCB;
  19. }
  20. uint8_t seader_next_cpcb() {
  21. uint8_t next_pcb = cPCB ^ 0x40;
  22. //FURI_LOG_D(TAG, "cPCB was: %02X, current cPCB: %02X", cPCB, next_pcb);
  23. cPCB = next_pcb;
  24. return cPCB;
  25. }
  26. void seader_t_1_set_IFSD(Seader* seader) {
  27. SeaderWorker* seader_worker = seader->worker;
  28. SeaderUartBridge* seader_uart = seader_worker->uart;
  29. uint8_t frame[5];
  30. uint8_t frame_len = 0;
  31. frame[0] = NAD;
  32. frame[1] = 0xC1; // S(IFS request)
  33. frame[2] = 0x01;
  34. frame[3] = IFSD_VALUE;
  35. frame_len = 4;
  36. frame_len = seader_add_lrc(frame, frame_len);
  37. seader_ccid_XfrBlock(seader_uart, frame, frame_len);
  38. }
  39. void seader_t_1_send_ack(Seader* seader) {
  40. SeaderWorker* seader_worker = seader->worker;
  41. SeaderUartBridge* seader_uart = seader_worker->uart;
  42. uint8_t frame[4];
  43. uint8_t frame_len = 0;
  44. frame[0] = NAD;
  45. frame[1] = R_BLOCK | (seader_next_cpcb() >> 2);
  46. frame[2] = 0x00;
  47. frame_len = 3;
  48. frame_len = seader_add_lrc(frame, frame_len);
  49. //FURI_LOG_D(TAG, "Sending R-Block ACK: PCB: %02x", frame[1]);
  50. seader_ccid_XfrBlock(seader_uart, frame, frame_len);
  51. }
  52. BitBuffer* seader_t_1_tx_buffer;
  53. size_t seader_t_1_tx_buffer_offset = 0;
  54. void seader_send_t1_chunk(SeaderUartBridge* seader_uart, uint8_t PCB, uint8_t* chunk, size_t len) {
  55. uint8_t* frame = malloc(3 + len + 1);
  56. uint8_t frame_len = 0;
  57. frame[0] = NAD;
  58. frame[1] = PCB;
  59. frame[2] = len;
  60. frame_len = 3;
  61. if(len > 0) {
  62. memcpy(frame + frame_len, chunk, len);
  63. frame_len += len;
  64. }
  65. frame_len = seader_add_lrc(frame, frame_len);
  66. seader_ccid_XfrBlock(seader_uart, frame, frame_len);
  67. free(frame);
  68. }
  69. void seader_send_t1(SeaderUartBridge* seader_uart, uint8_t* apdu, size_t len) {
  70. if(len > IFSC_VALUE) {
  71. if(seader_t_1_tx_buffer == NULL) {
  72. seader_t_1_tx_buffer = bit_buffer_alloc(768);
  73. bit_buffer_copy_bytes(seader_t_1_tx_buffer, apdu, len);
  74. }
  75. size_t remaining =
  76. (bit_buffer_get_size_bytes(seader_t_1_tx_buffer) - seader_t_1_tx_buffer_offset);
  77. size_t copy_length = remaining > IFSC_VALUE ? IFSC_VALUE : remaining;
  78. uint8_t* chunk =
  79. (uint8_t*)bit_buffer_get_data(seader_t_1_tx_buffer) + seader_t_1_tx_buffer_offset;
  80. if(remaining > IFSC_VALUE) {
  81. uint8_t PCB = seader_next_dpcb() | MORE_BIT;
  82. seader_send_t1_chunk(seader_uart, PCB, chunk, copy_length);
  83. } else {
  84. uint8_t PCB = seader_next_dpcb();
  85. seader_send_t1_chunk(seader_uart, PCB, chunk, copy_length);
  86. }
  87. seader_t_1_tx_buffer_offset += copy_length;
  88. if(seader_t_1_tx_buffer_offset >= bit_buffer_get_size_bytes(seader_t_1_tx_buffer)) {
  89. bit_buffer_free(seader_t_1_tx_buffer);
  90. seader_t_1_tx_buffer = NULL;
  91. seader_t_1_tx_buffer_offset = 0;
  92. }
  93. return;
  94. }
  95. seader_send_t1_chunk(seader_uart, seader_next_dpcb(), apdu, len);
  96. }
  97. BitBuffer* seader_t_1_rx_buffer;
  98. bool seader_recv_t1(Seader* seader, CCID_Message* message) {
  99. // remove/validate NAD, PCB, LEN, LRC
  100. if(message->dwLength < 4) {
  101. FURI_LOG_W(TAG, "Invalid T=1 frame: too short");
  102. return false;
  103. }
  104. //uint8_t NAD = message->payload[0];
  105. uint8_t rPCB = message->payload[1];
  106. uint8_t LEN = message->payload[2];
  107. //uint8_t LRC = message->payload[3 + LEN];
  108. //FURI_LOG_D(TAG, "NAD: %02X, rPCB: %02X, LEN: %02X, LRC: %02X", NAD, rPCB, LEN, LRC);
  109. if(rPCB == 0xE1) {
  110. // S(IFS response)
  111. seader_worker_send_version(seader);
  112. SeaderWorker* seader_worker = seader->worker;
  113. if(seader_worker->callback) {
  114. seader_worker->callback(SeaderWorkerEventSamPresent, seader_worker->context);
  115. }
  116. return false;
  117. }
  118. if(rPCB == cPCB) {
  119. seader_next_cpcb();
  120. if(seader_t_1_rx_buffer != NULL) {
  121. bit_buffer_append_bytes(seader_t_1_rx_buffer, message->payload + 3, LEN);
  122. // TODO: validate LRC
  123. seader_worker_process_sam_message(
  124. seader,
  125. (uint8_t*)bit_buffer_get_data(seader_t_1_rx_buffer),
  126. bit_buffer_get_size_bytes(seader_t_1_rx_buffer));
  127. bit_buffer_free(seader_t_1_rx_buffer);
  128. seader_t_1_rx_buffer = NULL;
  129. return true;
  130. }
  131. if(seader_validate_lrc(message->payload, message->dwLength) == false) {
  132. return false;
  133. }
  134. // Skip NAD, PCB, LEN
  135. message->payload = message->payload + 3;
  136. message->dwLength = LEN;
  137. if(message->dwLength == 0) {
  138. //FURI_LOG_D(TAG, "Received T=1 frame with no data");
  139. return true;
  140. }
  141. return seader_worker_process_sam_message(seader, message->payload, message->dwLength);
  142. } else if(rPCB == (cPCB | MORE_BIT)) {
  143. //FURI_LOG_D(TAG, "Received T=1 frame with more bit set");
  144. if(seader_t_1_rx_buffer == NULL) {
  145. seader_t_1_rx_buffer = bit_buffer_alloc(512);
  146. }
  147. bit_buffer_append_bytes(seader_t_1_rx_buffer, message->payload + 3, LEN);
  148. seader_t_1_send_ack(seader);
  149. return false;
  150. } else if((rPCB & R_BLOCK) == R_BLOCK) {
  151. uint8_t R_SEQ = (rPCB & R_SEQUENCE_NUMBER_MASK) >> 4;
  152. uint8_t I_SEQ = (dPCB ^ 0x40) >> 6;
  153. if(R_SEQ != I_SEQ) {
  154. /*
  155. FURI_LOG_D(
  156. TAG,
  157. "Received R-Block: Incorrect sequence. Expected: %02X, Received: %02X",
  158. I_SEQ,
  159. R_SEQ);
  160. */
  161. // When this happens, the flipper freezes if it is doing NFC and my attempts to do events to stop that have failed
  162. return false;
  163. }
  164. if(seader_t_1_tx_buffer != NULL) {
  165. // Send more data, re-using the buffer to trigger the code path that sends the next block
  166. SeaderWorker* seader_worker = seader->worker;
  167. SeaderUartBridge* seader_uart = seader_worker->uart;
  168. seader_send_t1(
  169. seader_uart,
  170. (uint8_t*)bit_buffer_get_data(seader_t_1_tx_buffer),
  171. bit_buffer_get_size_bytes(seader_t_1_tx_buffer));
  172. return false;
  173. }
  174. } else {
  175. FURI_LOG_W(
  176. TAG, "Invalid T=1 frame: PCB mismatch. Expected: %02X, Received: %02X", cPCB, rPCB);
  177. }
  178. return false;
  179. }