Blame SOURCES/coolkey-1.1.0-alt-tokens-2.patch

ed074c
diff -up ./src/coolkey/slot.cpp.alt-tokens-2 ./src/coolkey/slot.cpp
ed074c
--- ./src/coolkey/slot.cpp.alt-tokens-2	2018-06-25 17:58:23.472185284 -0700
ed074c
+++ ./src/coolkey/slot.cpp	2018-06-25 18:02:29.714918126 -0700
ed074c
@@ -415,8 +415,9 @@ Slot::Slot(const char *readerName_, Log
ed074c
 	tokenManufacturer(NULL),
ed074c
 	slotInfoFound(false), context(context_), conn(NULL), state(UNKNOWN), 
ed074c
 	isVersion1Key(false), needLogin(false), fullTokenName(false), 
ed074c
-	mCoolkey(false), mOldCAC(false), mCACLocalLogin(false),
ed074c
-	pivContainer(-1), pivKey(-1), maxCacCerts(MAX_CERT_SLOTS), 
ed074c
+	mCoolkey(false), mOldCAC(false), mCACLocalLogin(false), mCAC_ACA(false),
ed074c
+	pivContainer(-1), pivKey(-1),
ed074c
+	minCacCerts(0), maxCacCerts(MAX_CERT_SLOTS),
ed074c
 	algs(ALG_NONE), p15aid(0), p15odfAddr(0), p15tokenInfoAddr(0),
ed074c
 	p15Instance(0),
ed074c
 #ifdef USE_SHMEM
ed074c
@@ -782,9 +783,11 @@ Slot::connectToToken()
ed074c
 	 state |= PIV_CARD | APPLET_SELECTABLE | APPLET_PERSONALIZED;
ed074c
 	 isVersion1Key = 0;
ed074c
 	 needLogin = true;
ed074c
+	 minCacCerts = 0;
ed074c
 	 maxCacCerts = MAX_CERT_SLOTS;
ed074c
          mCoolkey = 0;
ed074c
 	 mOldCAC = 0;
ed074c
+	 mCAC_ACA = 0;
ed074c
 	 mCACLocalLogin = getPIVLoginType();
ed074c
 	return;
ed074c
     } 
ed074c
@@ -924,23 +927,29 @@ Slot::getCACAid()
ed074c
 	CKYBuffer_Resize(&cardAID[i],0);
ed074c
     }
ed074c
 
ed074c
+    mCAC_ACA=false;
ed074c
     status = CACApplet_SelectCCC(conn,NULL);
ed074c
     if (status != CKYSUCCESS) {
ed074c
 	/* are we an old CAC */
ed074c
-	status = CACApplet_SelectPKI(conn, &cardAID[0], 0, NULL);
ed074c
-	if (status != CKYSUCCESS) {
ed074c
-	   /* no, just fail */
ed074c
-	   return status;
ed074c
-	}
ed074c
-	/* yes, fill in the old applets */
ed074c
-	mOldCAC = true;
ed074c
-	maxCacCerts = 1;
ed074c
-	for (i=1; i< MAX_CERT_SLOTS; i++) {
ed074c
+	maxCacCerts = 0;
ed074c
+	minCacCerts = -1;
ed074c
+        status = CACApplet_SelectACA(conn,NULL);
ed074c
+        if (status == CKYSUCCESS) {
ed074c
+	    mCAC_ACA = true;
ed074c
+        }
ed074c
+	for (i=0; i< MAX_CERT_SLOTS; i++) {
ed074c
 	    status = CACApplet_SelectPKI(conn, &cardAID[i], i, NULL);
ed074c
 	    if (status == CKYSUCCESS) {
ed074c
+		if (minCacCerts == -1) {
ed074c
+		    minCacCerts = i;
ed074c
+                }
ed074c
 		maxCacCerts = i+1;
ed074c
 	    }
ed074c
 	}
ed074c
+	if (minCacCerts == -1) {
ed074c
+	    return status;
ed074c
+        }
ed074c
+	mOldCAC = true;
ed074c
 	return CKYSUCCESS;
ed074c
     }
ed074c
     /* definately not an old CAC */
ed074c
@@ -997,6 +1006,7 @@ Slot::getCACAid()
ed074c
     if (certSlot == 0) {
ed074c
 	status = CKYAPDUFAIL; /* probably neeed a beter error code */
ed074c
     }
ed074c
+    minCacCerts = 0;
ed074c
     maxCacCerts = certSlot;
ed074c
 
ed074c
 done:
ed074c
@@ -3840,7 +3850,16 @@ Slot::login(SessionHandleSuffix handleSu
ed074c
     if(status != CKYSUCCESS ) handleConnectionError();
ed074c
 
ed074c
     if (state & GOV_CARD) {
ed074c
-	selectCACApplet(0, true);
ed074c
+	if (mCAC_ACA) {
ed074c
+            status = CACApplet_SelectACA(conn,NULL);
ed074c
+	    if ( status == CKYSCARDERR ) handleConnectionError();
ed074c
+	    if ( status != CKYSUCCESS) {
ed074c
+		disconnect();
ed074c
+        	throw PKCS11Exception(CKR_DEVICE_REMOVED);
ed074c
+	    }
ed074c
+	} else {
ed074c
+	    selectCACApplet(minCacCerts, true);
ed074c
+	}
ed074c
     } else if ((state & P15_CARD)== 0) {
ed074c
 	/* p15 does the select in attemptLogin */
ed074c
 	selectApplet();
ed074c
diff -up ./src/coolkey/slot.h.alt-tokens-2 ./src/coolkey/slot.h
ed074c
--- ./src/coolkey/slot.h.alt-tokens-2	2018-06-25 17:58:23.473185283 -0700
ed074c
+++ ./src/coolkey/slot.h	2018-06-25 17:58:23.475185280 -0700
ed074c
@@ -356,8 +356,10 @@ class Slot {
ed074c
     bool mCoolkey;
ed074c
     bool mOldCAC;
ed074c
     bool mCACLocalLogin;
ed074c
+    bool mCAC_ACA;
ed074c
     int pivContainer;
ed074c
     int pivKey;
ed074c
+    int minCacCerts;
ed074c
     int maxCacCerts;
ed074c
     SlotAlgs algs;
ed074c
     unsigned short p15aid;
ed074c
diff -up ./src/libckyapplet/cky_applet.c.alt-tokens-2 ./src/libckyapplet/cky_applet.c
ed074c
--- ./src/libckyapplet/cky_applet.c.alt-tokens-2	2018-06-25 17:58:23.473185283 -0700
ed074c
+++ ./src/libckyapplet/cky_applet.c	2018-06-25 17:58:23.475185280 -0700
ed074c
@@ -626,6 +626,19 @@ CACApplet_SelectCCC(CKYCardConnection *c
ed074c
     return ret;
ed074c
 }
ed074c
 
ed074c
+static CKYByte cacACAid[] = {0xa0, 0x00, 0x00, 0x00, 0x79, 0x10, 0x00 };
ed074c
+CKYStatus
ed074c
+CACApplet_SelectACA(CKYCardConnection *conn, CKYISOStatus *apduRC)
ed074c
+{
ed074c
+    CKYStatus ret;
ed074c
+    CKYBuffer CAC_CM_AID;
ed074c
+    CKYBuffer_InitFromData(&CAC_CM_AID, cacACAid, sizeof(cacACAid));
ed074c
+    ret = CKYApplet_HandleAPDU(conn, CKYAppletFactory_SelectFile, &CAC_CM_AID,
ed074c
+		 NULL, CKY_SIZE_UNKNOWN, CKYAppletFill_Null, NULL, apduRC);
ed074c
+    CKYBuffer_FreeData(&CAC_CM_AID);
ed074c
+    return ret;
ed074c
+}
ed074c
+
ed074c
 CKYStatus
ed074c
 CACApplet_SelectFile(CKYCardConnection *conn, unsigned short ef,
ed074c
 						 CKYISOStatus *apduRC)
ed074c
diff -up ./src/libckyapplet/cky_applet.h.alt-tokens-2 ./src/libckyapplet/cky_applet.h
ed074c
--- ./src/libckyapplet/cky_applet.h.alt-tokens-2	2018-06-25 17:58:23.457185300 -0700
ed074c
+++ ./src/libckyapplet/cky_applet.h	2018-06-25 17:58:23.475185280 -0700
ed074c
@@ -539,6 +539,8 @@ CKYStatus CACApplet_SelectCardManager(CK
ed074c
 							CKYISOStatus *apduRC);
ed074c
 /* Select the CAC CC container. Can happen with either applet selected */
ed074c
 CKYStatus CACApplet_SelectCCC(CKYCardConnection *conn, CKYISOStatus *apduRC);
ed074c
+/* Select the CAC ACA container. Can happen with either applet selected */
ed074c
+CKYStatus CACApplet_SelectACA(CKYCardConnection *conn, CKYISOStatus *apduRC);
ed074c
 /* Select an old CAC applet and fill in the cardAID */
ed074c
 CKYStatus CACApplet_SelectPKI(CKYCardConnection *conn, CKYBuffer *cardAid,
ed074c
 			      CKYByte instance, CKYISOStatus *apduRC);