From eb75ad4efa36703ed5dc7aaed1779e97497febb1 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Sun, 25 May 2025 19:07:20 +0200 Subject: [PATCH] Add app_exists() to check if an AID is loaded. --- src/apdu.h | 1 + src/main.c | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/apdu.h b/src/apdu.h index b0d7f70..a486bd3 100644 --- a/src/apdu.h +++ b/src/apdu.h @@ -33,6 +33,7 @@ typedef struct app { int (*unload)(); } app_t; +extern bool app_exists(const uint8_t *aid, size_t aid_len); extern int register_app(int (*)(app_t *, uint8_t), const uint8_t *); extern int select_app(const uint8_t *aid, size_t aid_len); diff --git a/src/main.c b/src/main.c index 4df4647..1d866ae 100644 --- a/src/main.c +++ b/src/main.c @@ -54,7 +54,22 @@ app_t *current_app = NULL; const uint8_t *ccid_atr = NULL; +bool app_exists(const uint8_t *aid, size_t aid_len) { + if (current_app && current_app->aid && (current_app->aid + 1 == aid || !memcmp(current_app->aid + 1, aid, aid_len))) { + return true; + } + for (int a = 0; a < num_apps; a++) { + if (!memcmp(apps[a].aid + 1, aid, MIN(aid_len, apps[a].aid[0]))) { + return true; + } + } + return false; +} + int register_app(int (*select_aid)(app_t *, uint8_t), const uint8_t *aid) { + if (app_exists(aid + 1, aid[0])) { + return 1; + } if (num_apps < sizeof(apps) / sizeof(app_t)) { apps[num_apps].select_aid = select_aid; apps[num_apps].aid = aid;