From 6691487cd7433b4ffc3a99124b5ecf92361b8a76 Mon Sep 17 00:00:00 2001 From: Jakub Jelen Date: Tue, 9 Oct 2018 15:10:36 +0200 Subject: [PATCH 1/3] cac: These functions do not have to be exposed --- src/libopensc/card-cac.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libopensc/card-cac.c b/src/libopensc/card-cac.c index eeab07e4f..bd4e03362 100644 --- a/src/libopensc/card-cac.c +++ b/src/libopensc/card-cac.c @@ -211,7 +211,7 @@ typedef struct cac_private_data { #define CAC_DATA(card) ((cac_private_data_t*)card->drv_data) -int cac_list_compare_path(const void *a, const void *b) +static int cac_list_compare_path(const void *a, const void *b) { if (a == NULL || b == NULL) return 1; @@ -220,7 +220,7 @@ int cac_list_compare_path(const void *a, const void *b) } /* For SimCList autocopy, we need to know the size of the data elements */ -size_t cac_list_meter(const void *el) { +static size_t cac_list_meter(const void *el) { return sizeof(cac_object_t); } From fab79b70ff45d02d99bc05863be57f8ca8f0acda Mon Sep 17 00:00:00 2001 From: Jakub Jelen Date: Tue, 9 Oct 2018 15:58:12 +0200 Subject: [PATCH 2/3] coolkey: Improve card matching to avoid mismatches in muscle --- src/libopensc/card-coolkey.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/libopensc/card-coolkey.c b/src/libopensc/card-coolkey.c index b97559cc3..2cf2362c8 100644 --- a/src/libopensc/card-coolkey.c +++ b/src/libopensc/card-coolkey.c @@ -2224,14 +2224,32 @@ static int coolkey_initialize(sc_card_t *card) /* NOTE: returns a bool, 1 card matches, 0 it does not */ static int coolkey_match_card(sc_card_t *card) { + sc_apdu_t apdu; int r; + SC_FUNC_CALLED(card->ctx, SC_LOG_DEBUG_VERBOSE); /* Since we send an APDU, the card's logout function may be called... * however it may be in dirty memory */ card->ops->logout = NULL; r = coolkey_select_applet(card); - return (r >= SC_SUCCESS); + if (r == SC_SUCCESS) { + /* The GET STATUS INS with P1 = 1 returns invalid instruction (0x6D00) + * on Coolkey applet (reserved for GetMemory function), + * while incorrect P1 (0x9C10) on Muscle applets + */ + sc_format_apdu(card, &apdu, SC_APDU_CASE_1, COOLKEY_INS_GET_STATUS, 0x01, 0x00); + apdu.cla = COOLKEY_CLASS; + apdu.le = 0x00; + apdu.resplen = 0; + apdu.resp = NULL; + r = sc_transmit_apdu(card, &apdu); + if (r == SC_SUCCESS && apdu.sw1 == 0x6d && apdu.sw2 == 0x00) { + return 1; + } + return 0; + } + return 0; } From 98a1716768d11afd6d0e1e73bf8154dddfe915e9 Mon Sep 17 00:00:00 2001 From: Jakub Jelen Date: Tue, 9 Oct 2018 16:01:57 +0200 Subject: [PATCH 3/3] ctx: Move coolkey driver up after improving the matching Fixes #1483 --- src/libopensc/ctx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libopensc/ctx.c b/src/libopensc/ctx.c index f24a61ca0..98e6038a7 100644 --- a/src/libopensc/ctx.c +++ b/src/libopensc/ctx.c @@ -128,6 +128,7 @@ static const struct _sc_driver_entry internal_card_drivers[] = { /* Here should be placed drivers that need some APDU transactions in the * driver's `match_card()` function. */ + { "coolkey", (void *(*)(void)) sc_get_coolkey_driver }, /* MUSCLE card applet returns 9000 on whatever AID is selected, see * https://github.com/JavaCardOS/MuscleCard-Applet/blob/master/musclecard/src/com/musclecard/CardEdge/CardEdge.java#L326 * put the muscle driver first to cope with this bug. */ @@ -144,7 +145,6 @@ static const struct _sc_driver_entry internal_card_drivers[] = { #endif { "openpgp", (void *(*)(void)) sc_get_openpgp_driver }, { "jpki", (void *(*)(void)) sc_get_jpki_driver }, - { "coolkey", (void *(*)(void)) sc_get_coolkey_driver }, { "npa", (void *(*)(void)) sc_get_npa_driver }, /* The default driver should be last, as it handles all the * unrecognized cards. */