seos_native_peripheral.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include "seos_native_peripheral_i.h"
  2. #define TAG "SeosNativePeripheral"
  3. static uint8_t standard_seos_aid[] = {0xa0, 0x00, 0x00, 0x04, 0x40, 0x00, 0x01, 0x01, 0x00, 0x01};
  4. static uint8_t cd02[] = {0xcd, 0x02};
  5. static uint8_t general_authenticate_1[] =
  6. {0x00, 0x87, 0x00, 0x01, 0x04, 0x7c, 0x02, 0x81, 0x00, 0x00};
  7. static uint8_t ga1_response[] = {0x7c, 0x0a, 0x81, 0x08};
  8. static void seos_ble_connection_status_callback(BtStatus status, void* context) {
  9. furi_assert(context);
  10. SeosNativePeripheral* seos_native_peripheral = context;
  11. FURI_LOG_D(TAG, "seos_ble_connection_status_callback %d", (status == BtStatusConnected));
  12. if(status == BtStatusConnected) {
  13. view_dispatcher_send_custom_event(
  14. seos_native_peripheral->seos->view_dispatcher, SeosCustomEventConnected);
  15. } else if(status == BtStatusAdvertising) {
  16. view_dispatcher_send_custom_event(
  17. seos_native_peripheral->seos->view_dispatcher, SeosCustomEventAdvertising);
  18. }
  19. }
  20. static uint16_t seos_svc_callback(SeosServiceEvent event, void* context) {
  21. SeosNativePeripheral* seos_native_peripheral = context;
  22. UNUSED(seos_native_peripheral);
  23. BitBuffer* response = bit_buffer_alloc(128); // TODO: MTU
  24. if(event.event == SeosServiceEventTypeDataReceived) {
  25. uint8_t* data = event.data.buffer;
  26. uint8_t* rx_data = data + 1; // Match name to nfc version for easier copying
  27. seos_log_buffer(TAG, "seos_svc_callback", data, event.data.size);
  28. if(data[0] != BLE_START && data[0] != 0xe1) {
  29. FURI_LOG_W(TAG, "Unexpected start of BLE packet");
  30. }
  31. if(memcmp(data + 5, standard_seos_aid, sizeof(standard_seos_aid)) ==
  32. 0) { // response to select
  33. FURI_LOG_I(TAG, "Select ADF");
  34. uint8_t select_adf_header[] = {
  35. 0x80,
  36. 0xa5,
  37. 0x04,
  38. 0x00,
  39. (uint8_t)SEOS_ADF_OID_LEN + 2,
  40. 0x06,
  41. (uint8_t)SEOS_ADF_OID_LEN};
  42. bit_buffer_append_bytes(response, select_adf_header, sizeof(select_adf_header));
  43. bit_buffer_append_bytes(response, SEOS_ADF_OID, SEOS_ADF_OID_LEN);
  44. seos_native_peripheral->phase = SELECT_ADF;
  45. } else if(memcmp(data + 1, cd02, sizeof(cd02)) == 0) {
  46. BitBuffer* attribute_value = bit_buffer_alloc(event.data.size);
  47. bit_buffer_append_bytes(attribute_value, event.data.buffer, event.data.size);
  48. if(seos_reader_select_adf_response(
  49. attribute_value,
  50. 1,
  51. seos_native_peripheral->credential,
  52. &seos_native_peripheral->params)) {
  53. // Craft response
  54. general_authenticate_1[3] = seos_native_peripheral->params.key_no;
  55. bit_buffer_append_bytes(
  56. response, general_authenticate_1, sizeof(general_authenticate_1));
  57. seos_native_peripheral->phase = GENERAL_AUTHENTICATION_1;
  58. }
  59. bit_buffer_free(attribute_value);
  60. } else if(memcmp(data + 1, ga1_response, sizeof(ga1_response)) == 0) {
  61. memcpy(seos_native_peripheral->params.rndICC, data + 5, 8);
  62. // Craft response
  63. uint8_t cryptogram[32 + 8];
  64. memset(cryptogram, 0, sizeof(cryptogram));
  65. seos_reader_generate_cryptogram(
  66. seos_native_peripheral->credential, &seos_native_peripheral->params, cryptogram);
  67. uint8_t ga_header[] = {
  68. 0x00,
  69. 0x87,
  70. 0x00,
  71. seos_native_peripheral->params.key_no,
  72. sizeof(cryptogram) + 4,
  73. 0x7c,
  74. sizeof(cryptogram) + 2,
  75. 0x82,
  76. sizeof(cryptogram)};
  77. bit_buffer_append_bytes(response, ga_header, sizeof(ga_header));
  78. bit_buffer_append_bytes(response, cryptogram, sizeof(cryptogram));
  79. seos_native_peripheral->phase = GENERAL_AUTHENTICATION_2;
  80. } else if(rx_data[0] == 0x7C && rx_data[2] == 0x82) { // ga2 response
  81. if(rx_data[3] == 40) {
  82. if(!seos_reader_verify_cryptogram(&seos_native_peripheral->params, rx_data + 4)) {
  83. FURI_LOG_W(TAG, "Card cryptogram failed verification");
  84. return 0;
  85. }
  86. FURI_LOG_I(TAG, "Authenticated");
  87. view_dispatcher_send_custom_event(
  88. seos_native_peripheral->seos->view_dispatcher, SeosCustomEventAuthenticated);
  89. } else {
  90. FURI_LOG_W(TAG, "Unhandled card cryptogram size %d", rx_data[3]);
  91. }
  92. seos_native_peripheral->secure_messaging =
  93. secure_messaging_alloc(&seos_native_peripheral->params);
  94. SecureMessaging* secure_messaging = seos_native_peripheral->secure_messaging;
  95. uint8_t message[] = {0x5c, 0x02, 0xff, 0x00};
  96. secure_messaging_wrap_apdu(secure_messaging, message, sizeof(message), response);
  97. seos_native_peripheral->phase = REQUEST_SIO;
  98. view_dispatcher_send_custom_event(
  99. seos_native_peripheral->seos->view_dispatcher, SeosCustomEventSIORequested);
  100. } else if(seos_native_peripheral->phase == REQUEST_SIO) {
  101. SecureMessaging* secure_messaging = seos_native_peripheral->secure_messaging;
  102. BitBuffer* rx_buffer = bit_buffer_alloc(event.data.size - 1);
  103. bit_buffer_append_bytes(rx_buffer, rx_data, event.data.size - 1);
  104. seos_log_bitbuffer(TAG, "BLE response(wrapped)", rx_buffer);
  105. secure_messaging_unwrap_rapdu(secure_messaging, rx_buffer);
  106. seos_log_bitbuffer(TAG, "BLE response(clear)", rx_buffer);
  107. // Skip fileId
  108. seos_native_peripheral->credential->sio_len = bit_buffer_get_byte(rx_buffer, 2);
  109. if(seos_native_peripheral->credential->sio_len >
  110. sizeof(seos_native_peripheral->credential->sio)) {
  111. FURI_LOG_W(TAG, "SIO too long to save");
  112. return 0;
  113. }
  114. memcpy(
  115. seos_native_peripheral->credential->sio,
  116. bit_buffer_get_data(rx_buffer) + 3,
  117. seos_native_peripheral->credential->sio_len);
  118. FURI_LOG_I(TAG, "SIO Captured, %d bytes", seos_native_peripheral->credential->sio_len);
  119. Seos* seos = seos_native_peripheral->seos;
  120. view_dispatcher_send_custom_event(seos->view_dispatcher, SeosCustomEventReaderSuccess);
  121. bit_buffer_free(rx_buffer);
  122. seos_native_peripheral->phase = SELECT_AID;
  123. } else if(data[0] == 0xe1) {
  124. //ignore
  125. } else {
  126. FURI_LOG_W(TAG, "No match for write request");
  127. }
  128. } else {
  129. FURI_LOG_D(TAG, "seos_svc_callback: Unhandled event");
  130. }
  131. if(bit_buffer_get_size_bytes(response) > 0) {
  132. BitBuffer* tx = bit_buffer_alloc(1 + 2 + 1 + bit_buffer_get_size_bytes(response));
  133. bit_buffer_append_byte(tx, BLE_START);
  134. bit_buffer_append_bytes(
  135. tx, bit_buffer_get_data(response), bit_buffer_get_size_bytes(response));
  136. ble_profile_seos_tx(
  137. seos_native_peripheral->ble_profile,
  138. (uint8_t*)bit_buffer_get_data(tx),
  139. bit_buffer_get_size_bytes(tx));
  140. bit_buffer_free(tx);
  141. }
  142. bit_buffer_free(response);
  143. // NOTE: I'm not sure if I'm supposed to use ble_gatt_characteristic_update via ble_profile_seos_tx, or if I should be setting the seos_native_peripheral->event_buffer and returning a length.
  144. return 0;
  145. }
  146. SeosNativePeripheral* seos_native_peripheral_alloc(Seos* seos) {
  147. SeosNativePeripheral* seos_native_peripheral = malloc(sizeof(SeosNativePeripheral));
  148. memset(seos_native_peripheral, 0, sizeof(SeosNativePeripheral));
  149. seos_native_peripheral->seos = seos;
  150. seos_native_peripheral->credential = &seos->credential;
  151. seos_native_peripheral->bt = furi_record_open(RECORD_BT);
  152. seos_native_peripheral->phase = SELECT_AID;
  153. seos_native_peripheral->secure_messaging = NULL;
  154. seos_native_peripheral->params.key_no = 1;
  155. memset(
  156. seos_native_peripheral->params.cNonce,
  157. 0x0c,
  158. sizeof(seos_native_peripheral->params.cNonce));
  159. memset(seos_native_peripheral->params.UID, 0x0d, sizeof(seos_native_peripheral->params.UID));
  160. return seos_native_peripheral;
  161. }
  162. void seos_native_peripheral_free(SeosNativePeripheral* seos_native_peripheral) {
  163. furi_assert(seos_native_peripheral);
  164. furi_record_close(RECORD_BT);
  165. free(seos_native_peripheral);
  166. }
  167. void seos_native_peripheral_start(SeosNativePeripheral* seos_native_peripheral, FlowMode mode) {
  168. UNUSED(mode);
  169. bt_disconnect(seos_native_peripheral->bt);
  170. // Wait 2nd core to update nvm storage
  171. furi_delay_ms(200);
  172. seos_native_peripheral->ble_profile =
  173. bt_profile_start(seos_native_peripheral->bt, ble_profile_seos, NULL);
  174. furi_check(seos_native_peripheral->ble_profile);
  175. bt_set_status_changed_callback(
  176. seos_native_peripheral->bt, seos_ble_connection_status_callback, seos_native_peripheral);
  177. ble_profile_seos_set_event_callback(
  178. seos_native_peripheral->ble_profile,
  179. sizeof(seos_native_peripheral->event_buffer),
  180. seos_svc_callback,
  181. seos_native_peripheral);
  182. furi_hal_bt_start_advertising();
  183. view_dispatcher_send_custom_event(
  184. seos_native_peripheral->seos->view_dispatcher, SeosCustomEventAdvertising);
  185. }
  186. void seos_native_peripheral_stop(SeosNativePeripheral* seos_native_peripheral) {
  187. furi_hal_bt_stop_advertising();
  188. bt_set_status_changed_callback(seos_native_peripheral->bt, NULL, NULL);
  189. bt_disconnect(seos_native_peripheral->bt);
  190. // Wait 2nd core to update nvm storage
  191. furi_delay_ms(200);
  192. bt_keys_storage_set_default_path(seos_native_peripheral->bt);
  193. furi_check(bt_profile_restore_default(seos_native_peripheral->bt));
  194. }