nfc: DESFire fixes (#1334)

* nfc: don't give up on reading DESFire card if GET_KEY_SETTINGS fails
  Some cards are configured to refuse to provide key settings, but still
  provide other info.  For example, Ubiquiti UniFi Protect access cards
  won't list keys or applications, but will still answer GET_FREE_MEMORY.
* nfc: don't show error when saving DESFire card with no applications
* nfc: fix DESFire load with 0 applications or no PICC key settings

Co-authored-by: Kevin Wallace <git+flipperzero@kevin.wallace.seattle.wa.us>
Co-authored-by: gornekich <n.gorbadey@gmail.com>
Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
Kevin Wallace 2022-06-21 07:58:22 -07:00 committed by GitHub
parent 92f763e553
commit 88facf20c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 52 deletions

View File

@ -495,7 +495,7 @@ static bool nfc_device_save_mifare_df_data(FlipperFormat* file, NfcDevice* dev)
n_apps++;
}
if(!flipper_format_write_uint32(file, "Application Count", &n_apps, 1)) break;
if(n_apps == 0) break;
if(n_apps) {
tmp = malloc(n_apps * 3);
int i = 0;
for(MifareDesfireApplication* app = data->app_head; app; app = app->next) {
@ -506,6 +506,7 @@ static bool nfc_device_save_mifare_df_data(FlipperFormat* file, NfcDevice* dev)
for(MifareDesfireApplication* app = data->app_head; app; app = app->next) {
if(!nfc_device_save_mifare_df_app(file, app)) break;
}
}
saved = true;
} while(false);
@ -532,6 +533,7 @@ bool nfc_device_load_mifare_df_data(FlipperFormat* file, NfcDevice* dev) {
break;
}
}
if(flipper_format_key_exist(file, "PICC Change Key ID")) {
data->master_key_settings = malloc(sizeof(MifareDesfireKeySettings));
memset(data->master_key_settings, 0, sizeof(MifareDesfireKeySettings));
if(!nfc_device_load_mifare_df_key_settings(file, data->master_key_settings, "PICC")) {
@ -539,8 +541,10 @@ bool nfc_device_load_mifare_df_data(FlipperFormat* file, NfcDevice* dev) {
data->master_key_settings = NULL;
break;
}
}
uint32_t n_apps;
if(!flipper_format_read_uint32(file, "Application Count", &n_apps, 1)) break;
if(n_apps) {
tmp = malloc(n_apps * 3);
if(!flipper_format_read_hex(file, "Application IDs", tmp, n_apps * 3)) break;
bool parsed_apps = true;
@ -558,6 +562,7 @@ bool nfc_device_load_mifare_df_data(FlipperFormat* file, NfcDevice* dev) {
app_head = &app->next;
}
if(!parsed_apps) break;
}
parsed = true;
} while(false);

View File

@ -576,9 +576,7 @@ void nfc_worker_read_mifare_desfire(NfcWorker* nfc_worker) {
FURI_LOG_W(TAG, "Bad DESFire GET_KEY_SETTINGS response");
free(data->master_key_settings);
data->master_key_settings = NULL;
continue;
}
} else {
MifareDesfireKeyVersion** key_version_head =
&data->master_key_settings->key_version_head;
for(uint8_t key_id = 0; key_id < data->master_key_settings->max_keys; key_id++) {
@ -600,6 +598,7 @@ void nfc_worker_read_mifare_desfire(NfcWorker* nfc_worker) {
key_version_head = &key_version->next;
}
}
}
tx_rx.tx_bits = 8 * mf_df_prepare_get_application_ids(tx_rx.tx_data);
if(!furi_hal_nfc_tx_rx_full(&tx_rx)) {