From eef2f190f21b16b3dc982e8806edee0172a76edf Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Wed, 11 Oct 2023 21:02:04 +0200 Subject: [PATCH] New format for applet selection. AID comparison is performed by the kernel. If it is the same as previously loaded, do nothing. If not found, do nothing. If found and is different, unload old and load the new one. All other applets shall be migrated to new format. Signed-off-by: Pol Henarejos --- src/apdu.c | 15 +++++++++++---- src/apdu.h | 4 ++-- src/main.c | 3 ++- src/usb/hid/hid.c | 11 +++++------ 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/apdu.c b/src/apdu.c index 0de3139..be44045 100644 --- a/src/apdu.c +++ b/src/apdu.c @@ -27,11 +27,18 @@ extern uint32_t timeout; int process_apdu() { led_set_blink(BLINK_PROCESSING); if (INS(apdu) == 0xA4 && P1(apdu) == 0x04 && (P2(apdu) == 0x00 || P2(apdu) == 0x4)) { //select by AID - if (current_app && current_app->unload) { - current_app->unload(); - } for (int a = 0; a < num_apps; a++) { - if ((current_app = apps[a].select_aid(&apps[a], apdu.data, apdu.nc))) { + if (!memcmp(apps[a].aid + 1, apdu.data, MIN(apdu.nc, apps[a].aid[0]))) { + if (current_app) { + if (current_app->aid && !memcmp(current_app->aid + 1, apdu.data, apdu.nc)) { + return set_res_sw(0x90, 0x00); + } + if (current_app->unload) { + current_app->unload(); + } + } + current_app = &apps[a]; + current_app->select_aid(current_app); return set_res_sw(0x90, 0x00); } } diff --git a/src/apdu.h b/src/apdu.h index fe88fb5..1154e94 100644 --- a/src/apdu.h +++ b/src/apdu.h @@ -28,11 +28,11 @@ typedef struct app { const uint8_t *aid; int (*process_apdu)(); - struct app * (*select_aid)(struct app *, const uint8_t *, uint8_t); + int (*select_aid)(struct app *); int (*unload)(); } app_t; -extern int register_app(app_t *(*)(app_t *, const uint8_t *, uint8_t)); +extern int register_app(int (*)(app_t *), const uint8_t *); typedef struct cmd { uint8_t ins; diff --git a/src/main.c b/src/main.c index 79dec33..d06c1c0 100644 --- a/src/main.c +++ b/src/main.c @@ -113,9 +113,10 @@ app_t *current_app = NULL; const uint8_t *ccid_atr = NULL; -int register_app(app_t *(*select_aid)(app_t *, const uint8_t *, uint8_t)) { +int register_app(int (*select_aid)(app_t *), const uint8_t *aid) { if (num_apps < sizeof(apps) / sizeof(app_t)) { apps[num_apps].select_aid = select_aid; + apps[num_apps].aid = aid; num_apps++; return 1; } diff --git a/src/usb/hid/hid.c b/src/usb/hid/hid.c index 9f34663..fbea0ef 100644 --- a/src/usb/hid/hid.c +++ b/src/usb/hid/hid.c @@ -550,13 +550,12 @@ int driver_process_usb_packet_hid(uint16_t read) { } else if (current_app == NULL || - memcmp(current_app->aid, fido_aid + 1, - MIN(current_app->aid[0], fido_aid[0])) != 0) { - for (int a = 0; a < num_apps; a++) { - if ((current_app = apps[a].select_aid(&apps[a], fido_aid + 1, fido_aid[0]))) { - break; + current_app->aid != fido_aid) { + if (current_app && current_app->unload) { + current_app->unload(); } - } + current_app = &apps[a]; + current_app->select_aid(current_app); } //if (thread_type != 1) #ifndef ENABLE_EMULATION