seos_native_peripheral.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #include "seos_native_peripheral_i.h"
  2. #define TAG "SeosNativePeripheral"
  3. #define MESSAGE_QUEUE_SIZE 10
  4. static uint8_t standard_seos_aid[] = {0xa0, 0x00, 0x00, 0x04, 0x40, 0x00, 0x01, 0x01, 0x00, 0x01};
  5. static uint8_t cd02[] = {0xcd, 0x02};
  6. static uint8_t general_authenticate_1[] =
  7. {0x00, 0x87, 0x00, 0x01, 0x04, 0x7c, 0x02, 0x81, 0x00, 0x00};
  8. static uint8_t ga1_response[] = {0x7c, 0x0a, 0x81, 0x08};
  9. int32_t seos_native_peripheral_task(void* context);
  10. typedef struct {
  11. size_t len;
  12. uint8_t buf[BLE_SVC_SEOS_CHAR_VALUE_LEN_MAX];
  13. } NativePeripheralMessage;
  14. static void seos_ble_connection_status_callback(BtStatus status, void* context) {
  15. furi_assert(context);
  16. SeosNativePeripheral* seos_native_peripheral = context;
  17. FURI_LOG_D(TAG, "seos_ble_connection_status_callback %d", (status == BtStatusConnected));
  18. if(status == BtStatusConnected) {
  19. view_dispatcher_send_custom_event(
  20. seos_native_peripheral->seos->view_dispatcher, SeosCustomEventConnected);
  21. } else if(status == BtStatusAdvertising) {
  22. view_dispatcher_send_custom_event(
  23. seos_native_peripheral->seos->view_dispatcher, SeosCustomEventAdvertising);
  24. }
  25. }
  26. static uint16_t seos_svc_callback(SeosServiceEvent event, void* context) {
  27. SeosNativePeripheral* seos_native_peripheral = context;
  28. uint16_t bytes_available = 0;
  29. if(event.event == SeosServiceEventTypeDataReceived) {
  30. uint32_t space = furi_message_queue_get_space(seos_native_peripheral->messages);
  31. if(space > 0) {
  32. NativePeripheralMessage message = {.len = event.data.size};
  33. memcpy(message.buf, event.data.buffer, event.data.size);
  34. if(furi_mutex_acquire(seos_native_peripheral->mq_mutex, FuriWaitForever) ==
  35. FuriStatusOk) {
  36. furi_message_queue_put(
  37. seos_native_peripheral->messages, &message, FuriWaitForever);
  38. furi_mutex_release(seos_native_peripheral->mq_mutex);
  39. }
  40. if(space < MESSAGE_QUEUE_SIZE / 2) {
  41. FURI_LOG_D(TAG, "Queue message. %ld remaining", space);
  42. }
  43. bytes_available = (space - 1) * sizeof(NativePeripheralMessage);
  44. } else {
  45. FURI_LOG_E(TAG, "No space in message queue");
  46. }
  47. }
  48. return bytes_available;
  49. }
  50. SeosNativePeripheral* seos_native_peripheral_alloc(Seos* seos) {
  51. SeosNativePeripheral* seos_native_peripheral = malloc(sizeof(SeosNativePeripheral));
  52. memset(seos_native_peripheral, 0, sizeof(SeosNativePeripheral));
  53. seos_native_peripheral->seos = seos;
  54. seos_native_peripheral->credential = &seos->credential;
  55. seos_native_peripheral->bt = furi_record_open(RECORD_BT);
  56. seos_native_peripheral->phase = SELECT_AID;
  57. seos_native_peripheral->secure_messaging = NULL;
  58. seos_native_peripheral->params.key_no = 1;
  59. memset(
  60. seos_native_peripheral->params.cNonce,
  61. 0x0c,
  62. sizeof(seos_native_peripheral->params.cNonce));
  63. memset(seos_native_peripheral->params.UID, 0x0d, sizeof(seos_native_peripheral->params.UID));
  64. seos_native_peripheral->thread = furi_thread_alloc_ex(
  65. "SeosNativePeripheralWorker",
  66. 5 * 1024,
  67. seos_native_peripheral_task,
  68. seos_native_peripheral);
  69. seos_native_peripheral->messages =
  70. furi_message_queue_alloc(MESSAGE_QUEUE_SIZE, sizeof(NativePeripheralMessage));
  71. seos_native_peripheral->mq_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  72. return seos_native_peripheral;
  73. }
  74. void seos_native_peripheral_free(SeosNativePeripheral* seos_native_peripheral) {
  75. furi_assert(seos_native_peripheral);
  76. furi_record_close(RECORD_BT);
  77. furi_message_queue_free(seos_native_peripheral->messages);
  78. furi_mutex_free(seos_native_peripheral->mq_mutex);
  79. furi_thread_free(seos_native_peripheral->thread);
  80. free(seos_native_peripheral);
  81. }
  82. void seos_native_peripheral_start(SeosNativePeripheral* seos_native_peripheral, FlowMode mode) {
  83. UNUSED(mode);
  84. bt_disconnect(seos_native_peripheral->bt);
  85. // Wait 2nd core to update nvm storage
  86. furi_delay_ms(200);
  87. seos_native_peripheral->ble_profile =
  88. bt_profile_start(seos_native_peripheral->bt, ble_profile_seos, NULL);
  89. furi_check(seos_native_peripheral->ble_profile);
  90. bt_set_status_changed_callback(
  91. seos_native_peripheral->bt, seos_ble_connection_status_callback, seos_native_peripheral);
  92. ble_profile_seos_set_event_callback(
  93. seos_native_peripheral->ble_profile,
  94. sizeof(seos_native_peripheral->event_buffer),
  95. seos_svc_callback,
  96. seos_native_peripheral);
  97. furi_hal_bt_start_advertising();
  98. view_dispatcher_send_custom_event(
  99. seos_native_peripheral->seos->view_dispatcher, SeosCustomEventAdvertising);
  100. furi_thread_start(seos_native_peripheral->thread);
  101. }
  102. void seos_native_peripheral_stop(SeosNativePeripheral* seos_native_peripheral) {
  103. furi_hal_bt_stop_advertising();
  104. bt_set_status_changed_callback(seos_native_peripheral->bt, NULL, NULL);
  105. bt_disconnect(seos_native_peripheral->bt);
  106. // Wait 2nd core to update nvm storage
  107. furi_delay_ms(200);
  108. bt_keys_storage_set_default_path(seos_native_peripheral->bt);
  109. furi_check(bt_profile_restore_default(seos_native_peripheral->bt));
  110. furi_thread_flags_set(furi_thread_get_id(seos_native_peripheral->thread), WorkerEvtStop);
  111. furi_thread_join(seos_native_peripheral->thread);
  112. }
  113. void seos_native_peripheral_process_message(
  114. SeosNativePeripheral* seos_native_peripheral,
  115. NativePeripheralMessage message) {
  116. BitBuffer* response = bit_buffer_alloc(128); // TODO: MTU
  117. uint8_t* data = message.buf;
  118. uint8_t* rx_data = data + 1; // Match name to nfc version for easier copying
  119. seos_log_buffer(TAG, "seos_svc_callback", data, message.len);
  120. if(data[0] != BLE_START && data[0] != 0xe1) {
  121. FURI_LOG_W(TAG, "Unexpected start of BLE packet");
  122. }
  123. if(memcmp(data + 5, standard_seos_aid, sizeof(standard_seos_aid)) == 0) { // response to select
  124. FURI_LOG_I(TAG, "Select ADF");
  125. uint8_t select_adf_header[] = {
  126. 0x80, 0xa5, 0x04, 0x00, (uint8_t)SEOS_ADF_OID_LEN + 2, 0x06, (uint8_t)SEOS_ADF_OID_LEN};
  127. bit_buffer_append_bytes(response, select_adf_header, sizeof(select_adf_header));
  128. bit_buffer_append_bytes(response, SEOS_ADF_OID, SEOS_ADF_OID_LEN);
  129. seos_native_peripheral->phase = SELECT_ADF;
  130. } else if(memcmp(data + 1, cd02, sizeof(cd02)) == 0) {
  131. BitBuffer* attribute_value = bit_buffer_alloc(message.len);
  132. bit_buffer_append_bytes(attribute_value, message.buf, message.len);
  133. if(seos_reader_select_adf_response(
  134. attribute_value,
  135. 1,
  136. seos_native_peripheral->credential,
  137. &seos_native_peripheral->params)) {
  138. // Craft response
  139. general_authenticate_1[3] = seos_native_peripheral->params.key_no;
  140. bit_buffer_append_bytes(
  141. response, general_authenticate_1, sizeof(general_authenticate_1));
  142. seos_native_peripheral->phase = GENERAL_AUTHENTICATION_1;
  143. }
  144. bit_buffer_free(attribute_value);
  145. } else if(memcmp(data + 1, ga1_response, sizeof(ga1_response)) == 0) {
  146. memcpy(seos_native_peripheral->params.rndICC, data + 5, 8);
  147. // Craft response
  148. uint8_t cryptogram[32 + 8];
  149. memset(cryptogram, 0, sizeof(cryptogram));
  150. seos_reader_generate_cryptogram(
  151. seos_native_peripheral->credential, &seos_native_peripheral->params, cryptogram);
  152. uint8_t ga_header[] = {
  153. 0x00,
  154. 0x87,
  155. 0x00,
  156. seos_native_peripheral->params.key_no,
  157. sizeof(cryptogram) + 4,
  158. 0x7c,
  159. sizeof(cryptogram) + 2,
  160. 0x82,
  161. sizeof(cryptogram)};
  162. bit_buffer_append_bytes(response, ga_header, sizeof(ga_header));
  163. bit_buffer_append_bytes(response, cryptogram, sizeof(cryptogram));
  164. seos_native_peripheral->phase = GENERAL_AUTHENTICATION_2;
  165. } else if(rx_data[0] == 0x7C && rx_data[2] == 0x82) { // ga2 response
  166. if(rx_data[3] == 40) {
  167. if(!seos_reader_verify_cryptogram(&seos_native_peripheral->params, rx_data + 4)) {
  168. FURI_LOG_W(TAG, "Card cryptogram failed verification");
  169. bit_buffer_free(response);
  170. return;
  171. }
  172. FURI_LOG_I(TAG, "Authenticated");
  173. view_dispatcher_send_custom_event(
  174. seos_native_peripheral->seos->view_dispatcher, SeosCustomEventAuthenticated);
  175. } else {
  176. FURI_LOG_W(TAG, "Unhandled card cryptogram size %d", rx_data[3]);
  177. }
  178. seos_native_peripheral->secure_messaging =
  179. secure_messaging_alloc(&seos_native_peripheral->params);
  180. SecureMessaging* secure_messaging = seos_native_peripheral->secure_messaging;
  181. uint8_t message[] = {0x5c, 0x02, 0xff, 0x00};
  182. secure_messaging_wrap_apdu(secure_messaging, message, sizeof(message), response);
  183. seos_native_peripheral->phase = REQUEST_SIO;
  184. view_dispatcher_send_custom_event(
  185. seos_native_peripheral->seos->view_dispatcher, SeosCustomEventSIORequested);
  186. } else if(seos_native_peripheral->phase == REQUEST_SIO) {
  187. SecureMessaging* secure_messaging = seos_native_peripheral->secure_messaging;
  188. BitBuffer* rx_buffer = bit_buffer_alloc(message.len - 1);
  189. bit_buffer_append_bytes(rx_buffer, rx_data, message.len - 1);
  190. seos_log_bitbuffer(TAG, "BLE response(wrapped)", rx_buffer);
  191. secure_messaging_unwrap_rapdu(secure_messaging, rx_buffer);
  192. seos_log_bitbuffer(TAG, "BLE response(clear)", rx_buffer);
  193. // Skip fileId
  194. seos_native_peripheral->credential->sio_len = bit_buffer_get_byte(rx_buffer, 2);
  195. if(seos_native_peripheral->credential->sio_len >
  196. sizeof(seos_native_peripheral->credential->sio)) {
  197. FURI_LOG_W(TAG, "SIO too long to save");
  198. bit_buffer_free(response);
  199. return;
  200. }
  201. memcpy(
  202. seos_native_peripheral->credential->sio,
  203. bit_buffer_get_data(rx_buffer) + 3,
  204. seos_native_peripheral->credential->sio_len);
  205. FURI_LOG_I(TAG, "SIO Captured, %d bytes", seos_native_peripheral->credential->sio_len);
  206. Seos* seos = seos_native_peripheral->seos;
  207. view_dispatcher_send_custom_event(seos->view_dispatcher, SeosCustomEventReaderSuccess);
  208. bit_buffer_free(rx_buffer);
  209. seos_native_peripheral->phase = SELECT_AID;
  210. } else if(data[0] == 0xe1) {
  211. //ignore
  212. } else {
  213. FURI_LOG_W(TAG, "No match for write request");
  214. }
  215. if(bit_buffer_get_size_bytes(response) > 0) {
  216. BitBuffer* tx = bit_buffer_alloc(1 + 2 + 1 + bit_buffer_get_size_bytes(response));
  217. bit_buffer_append_byte(tx, BLE_START);
  218. bit_buffer_append_bytes(
  219. tx, bit_buffer_get_data(response), bit_buffer_get_size_bytes(response));
  220. ble_profile_seos_tx(
  221. seos_native_peripheral->ble_profile,
  222. (uint8_t*)bit_buffer_get_data(tx),
  223. bit_buffer_get_size_bytes(tx));
  224. bit_buffer_free(tx);
  225. }
  226. bit_buffer_free(response);
  227. }
  228. int32_t seos_native_peripheral_task(void* context) {
  229. SeosNativePeripheral* seos_native_peripheral = (SeosNativePeripheral*)context;
  230. bool running = true;
  231. while(running) {
  232. uint32_t events = furi_thread_flags_get();
  233. if(events & WorkerEvtStop) {
  234. running = false;
  235. break;
  236. }
  237. if(furi_mutex_acquire(seos_native_peripheral->mq_mutex, 1) == FuriStatusOk) {
  238. uint32_t count = furi_message_queue_get_count(seos_native_peripheral->messages);
  239. if(count > 0) {
  240. if(count > MESSAGE_QUEUE_SIZE / 2) {
  241. FURI_LOG_I(TAG, "Dequeue message [%ld messages]", count);
  242. }
  243. NativePeripheralMessage message = {};
  244. FuriStatus status = furi_message_queue_get(
  245. seos_native_peripheral->messages, &message, FuriWaitForever);
  246. if(status != FuriStatusOk) {
  247. FURI_LOG_W(TAG, "furi_message_queue_get fail %d", status);
  248. }
  249. seos_native_peripheral_process_message(seos_native_peripheral, message);
  250. }
  251. furi_mutex_release(seos_native_peripheral->mq_mutex);
  252. } else {
  253. FURI_LOG_W(TAG, "Failed to acquire mutex");
  254. }
  255. // A beat for event flags
  256. furi_delay_ms(1);
  257. }
  258. return 0;
  259. }