From c24be5a6319c98cc95ed410807d5c7679782cecf Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Sat, 28 Oct 2023 20:53:06 +0200 Subject: [PATCH] Adapted to new selection AID method. Signed-off-by: Pol Henarejos --- src/fido/cmd_register.c | 14 +++------ src/fido/fido.c | 22 +++++++------ src/fido/management.c | 22 ++++++------- src/fido/oath.c | 70 +++++++++++++++++++---------------------- src/fido/otp.c | 34 +++++++++----------- 5 files changed, 75 insertions(+), 87 deletions(-) diff --git a/src/fido/cmd_register.c b/src/fido/cmd_register.c index 837cb90..877f5b0 100644 --- a/src/fido/cmd_register.c +++ b/src/fido/cmd_register.c @@ -32,18 +32,14 @@ const uint8_t u2f_aid[] = { int u2f_unload(); int u2f_process_apdu(); -app_t *u2f_select(app_t *a, const uint8_t *aid, uint8_t aid_len) { - if (!memcmp(aid, u2f_aid + 1, MIN(aid_len, u2f_aid[0])) && cap_supported(CAP_U2F)) { - a->aid = u2f_aid; - a->process_apdu = u2f_process_apdu; - a->unload = u2f_unload; - return a; - } - return NULL; +int u2f_select(app_t *a) { + a->process_apdu = u2f_process_apdu; + a->unload = u2f_unload; + return CCID_OK; } void __attribute__((constructor)) u2f_ctor() { - register_app(u2f_select); + register_app(u2f_select, u2f_aid); } int u2f_unload() { diff --git a/src/fido/fido.c b/src/fido/fido.c index dc70e86..32db925 100644 --- a/src/fido/fido.c +++ b/src/fido/fido.c @@ -33,6 +33,7 @@ #include #include "management.h" #include "ctap_hid.h" +#include "version.h" int fido_process_apdu(); int fido_unload(); @@ -53,27 +54,30 @@ const uint8_t atr_fido[] = { 0x75, 0x62, 0x69, 0x4b, 0x65, 0x79, 0x40 }; -app_t *fido_select(app_t *a, const uint8_t *aid, uint8_t aid_len) { - if (!memcmp(aid, fido_aid + 1, MIN(aid_len, fido_aid[0])) && cap_supported(CAP_FIDO2)) { - a->aid = fido_aid; - a->process_apdu = fido_process_apdu; - a->unload = fido_unload; - return a; - } - return NULL; +int fido_select(app_t *a) { + a->process_apdu = fido_process_apdu; + a->unload = fido_unload; + return CCID_OK; } void __attribute__((constructor)) fido_ctor() { #if defined(USB_ITF_CCID) || defined(ENABLE_EMULATION) ccid_atr = atr_fido; #endif - register_app(fido_select); + register_app(fido_select, fido_aid); } int fido_unload() { return CCID_OK; } +uint8_t get_version_major() { + return PICO_FIDO_VERSION_MAJOR; +} +uint8_t get_version_minor() { + return PICO_FIDO_VERSION_MINOR; +} + mbedtls_ecp_group_id fido_curve_to_mbedtls(int curve) { if (curve == FIDO2_CURVE_P256) { return MBEDTLS_ECP_DP_SECP256R1; diff --git a/src/fido/management.c b/src/fido/management.c index f2f8df7..5d4eefb 100644 --- a/src/fido/management.c +++ b/src/fido/management.c @@ -31,22 +31,18 @@ const uint8_t man_aid[] = { 0xa0, 0x00, 0x00, 0x05, 0x27, 0x47, 0x11, 0x17 }; extern void scan_all(); -app_t *man_select(app_t *a, const uint8_t *aid, uint8_t aid_len) { - if (!memcmp(aid, man_aid + 1, MIN(aid_len, man_aid[0]))) { - a->aid = man_aid; - a->process_apdu = man_process_apdu; - a->unload = man_unload; - sprintf((char *) res_APDU, "%d.%d.0", PICO_FIDO_VERSION_MAJOR, PICO_FIDO_VERSION_MINOR); - res_APDU_size = strlen((char *) res_APDU); - apdu.ne = res_APDU_size; - scan_all(); - return a; - } - return NULL; +int man_select(app_t *a) { + a->process_apdu = man_process_apdu; + a->unload = man_unload; + sprintf((char *) res_APDU, "%d.%d.0", PICO_FIDO_VERSION_MAJOR, PICO_FIDO_VERSION_MINOR); + res_APDU_size = strlen((char *) res_APDU); + apdu.ne = res_APDU_size; + scan_all(); + return CCID_OK; } void __attribute__((constructor)) man_ctor() { - register_app(man_select); + register_app(man_select, man_aid); } int man_unload() { diff --git a/src/fido/oath.c b/src/fido/oath.c index e3d25bc..a0713c9 100644 --- a/src/fido/oath.c +++ b/src/fido/oath.c @@ -68,50 +68,46 @@ const uint8_t oath_aid[] = { 0xa0, 0x00, 0x00, 0x05, 0x27, 0x21, 0x01 }; -app_t *oath_select(app_t *a, const uint8_t *aid, uint8_t aid_len) { - if (!memcmp(aid, oath_aid + 1, MIN(aid_len, oath_aid[0])) && cap_supported(CAP_OATH)) { - a->aid = oath_aid; - a->process_apdu = oath_process_apdu; - a->unload = oath_unload; - res_APDU_size = 0; - res_APDU[res_APDU_size++] = TAG_T_VERSION; - res_APDU[res_APDU_size++] = 3; - res_APDU[res_APDU_size++] = PICO_FIDO_VERSION_MAJOR; - res_APDU[res_APDU_size++] = PICO_FIDO_VERSION_MINOR; - res_APDU[res_APDU_size++] = 0; - res_APDU[res_APDU_size++] = TAG_NAME; - res_APDU[res_APDU_size++] = 8; +int oath_select(app_t *a) { + a->process_apdu = oath_process_apdu; + a->unload = oath_unload; + res_APDU_size = 0; + res_APDU[res_APDU_size++] = TAG_T_VERSION; + res_APDU[res_APDU_size++] = 3; + res_APDU[res_APDU_size++] = PICO_FIDO_VERSION_MAJOR; + res_APDU[res_APDU_size++] = PICO_FIDO_VERSION_MINOR; + res_APDU[res_APDU_size++] = 0; + res_APDU[res_APDU_size++] = TAG_NAME; + res_APDU[res_APDU_size++] = 8; #ifndef ENABLE_EMULATION - pico_get_unique_board_id((pico_unique_board_id_t *) (res_APDU + res_APDU_size)); - res_APDU_size += 8; + pico_get_unique_board_id((pico_unique_board_id_t *) (res_APDU + res_APDU_size)); + res_APDU_size += 8; #else - memset(res_APDU + res_APDU_size, 0, 8); res_APDU_size += 8; + memset(res_APDU + res_APDU_size, 0, 8); res_APDU_size += 8; #endif - if (file_has_data(search_dynamic_file(EF_OATH_CODE)) == true) { - random_gen(NULL, challenge, sizeof(challenge)); - res_APDU[res_APDU_size++] = TAG_CHALLENGE; - res_APDU[res_APDU_size++] = sizeof(challenge); - memcpy(res_APDU + res_APDU_size, challenge, sizeof(challenge)); - res_APDU_size += sizeof(challenge); - } - file_t *ef_otp_pin = search_by_fid(EF_OTP_PIN, NULL, SPECIFY_EF); - if (file_has_data(ef_otp_pin)) { - const uint8_t *pin_data = file_get_data(ef_otp_pin); - res_APDU[res_APDU_size++] = TAG_PIN_COUNTER; - res_APDU[res_APDU_size++] = 1; - res_APDU[res_APDU_size++] = *pin_data; - } - res_APDU[res_APDU_size++] = TAG_ALGO; - res_APDU[res_APDU_size++] = 1; - res_APDU[res_APDU_size++] = ALG_HMAC_SHA1; - apdu.ne = res_APDU_size; - return a; + if (file_has_data(search_dynamic_file(EF_OATH_CODE)) == true) { + random_gen(NULL, challenge, sizeof(challenge)); + res_APDU[res_APDU_size++] = TAG_CHALLENGE; + res_APDU[res_APDU_size++] = sizeof(challenge); + memcpy(res_APDU + res_APDU_size, challenge, sizeof(challenge)); + res_APDU_size += sizeof(challenge); } - return NULL; + file_t *ef_otp_pin = search_by_fid(EF_OTP_PIN, NULL, SPECIFY_EF); + if (file_has_data(ef_otp_pin)) { + const uint8_t *pin_data = file_get_data(ef_otp_pin); + res_APDU[res_APDU_size++] = TAG_PIN_COUNTER; + res_APDU[res_APDU_size++] = 1; + res_APDU[res_APDU_size++] = *pin_data; + } + res_APDU[res_APDU_size++] = TAG_ALGO; + res_APDU[res_APDU_size++] = 1; + res_APDU[res_APDU_size++] = ALG_HMAC_SHA1; + apdu.ne = res_APDU_size; + return CCID_OK; } void __attribute__((constructor)) oath_ctor() { - register_app(oath_select); + register_app(oath_select, oath_aid); } int oath_unload() { diff --git a/src/fido/otp.c b/src/fido/otp.c index a38c287..3bfce0d 100644 --- a/src/fido/otp.c +++ b/src/fido/otp.c @@ -116,25 +116,21 @@ const uint8_t otp_aid[] = { 0xa0, 0x00, 0x00, 0x05, 0x27, 0x20, 0x01 }; -app_t *otp_select(app_t *a, const uint8_t *aid, uint8_t aid_len) { - if (!memcmp(aid, otp_aid + 1, MIN(aid_len, otp_aid[0])) && cap_supported(CAP_OTP)) { - a->aid = otp_aid; - a->process_apdu = otp_process_apdu; - a->unload = otp_unload; - if (file_has_data(search_dynamic_file(EF_OTP_SLOT1)) || - file_has_data(search_dynamic_file(EF_OTP_SLOT2))) { - config_seq = 1; - } - else { - config_seq = 0; - } - otp_status(); - memmove(res_APDU, res_APDU + 1, 6); - res_APDU_size = 6; - apdu.ne = res_APDU_size; - return a; +int otp_select(app_t *a) { + a->process_apdu = otp_process_apdu; + a->unload = otp_unload; + if (file_has_data(search_dynamic_file(EF_OTP_SLOT1)) || + file_has_data(search_dynamic_file(EF_OTP_SLOT2))) { + config_seq = 1; } - return NULL; + else { + config_seq = 0; + } + otp_status(); + memmove(res_APDU, res_APDU + 1, 6); + res_APDU_size = 6; + apdu.ne = res_APDU_size; + return CCID_OK; } uint8_t modhex_tab[] = @@ -308,7 +304,7 @@ int otp_button_pressed(uint8_t slot) { } void __attribute__((constructor)) otp_ctor() { - register_app(otp_select); + register_app(otp_select, otp_aid); button_pressed_cb = otp_button_pressed; }