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