Blame SOURCES/coolkey-1.1.0-rhel7-alt-cac.patch

ed074c
diff -up ./src/coolkey/coolkey.cpp.alt-cac ./src/coolkey/coolkey.cpp
ed074c
--- ./src/coolkey/coolkey.cpp.alt-cac	2016-12-01 15:37:49.106167768 -0800
ed074c
+++ ./src/coolkey/coolkey.cpp	2016-12-01 15:37:49.113167892 -0800
ed074c
@@ -80,9 +80,16 @@ ecMechanismList[] = {
ed074c
     {CKM_ECDSA,{256,521,CKF_HW | CKF_SIGN | CKF_EC_F_P}},
ed074c
     {CKM_ECDH1_DERIVE,{256, 521, CKF_HW | CKF_DERIVE | CKF_EC_F_P} }
ed074c
 };
ed074c
+static const MechInfo
ed074c
+allMechanismList[] = {
ed074c
+    {CKM_RSA_PKCS, { 1024, 4096, CKF_HW | CKF_SIGN | CKF_DECRYPT } },
ed074c
+    {CKM_ECDSA,{256,521,CKF_HW | CKF_SIGN | CKF_EC_F_P}},
ed074c
+    {CKM_ECDH1_DERIVE,{256, 521, CKF_HW | CKF_DERIVE | CKF_EC_F_P} }
ed074c
+};
ed074c
 
ed074c
 unsigned int numRSAMechanisms = sizeof(rsaMechanismList)/sizeof(MechInfo);
ed074c
 unsigned int numECMechanisms = sizeof(ecMechanismList)/sizeof(MechInfo);
ed074c
+unsigned int numAllMechanisms = sizeof(allMechanismList)/sizeof(MechInfo);
ed074c
 
ed074c
 /* ------------------------------------------------------------ */
ed074c
 
ed074c
@@ -382,13 +389,22 @@ C_GetMechanismList(CK_SLOT_ID slotID, CK
ed074c
             return CKR_TOKEN_NOT_PRESENT;
ed074c
         }
ed074c
 
ed074c
-        if ( slot->getIsECC()) {
ed074c
+	switch (slot->getAlgs()) {
ed074c
+	case ALG_ECC|ALG_RSA:
ed074c
+            mechanismList = allMechanismList;
ed074c
+            numMechanisms = numAllMechanisms;
ed074c
+	    break;
ed074c
+	case ALG_ECC:
ed074c
             mechanismList = ecMechanismList;
ed074c
             numMechanisms = numECMechanisms;
ed074c
-        } else {
ed074c
+	    break;
ed074c
+	case ALG_NONE:
ed074c
+	case ALG_RSA:
ed074c
+	default:
ed074c
             mechanismList = rsaMechanismList;
ed074c
             numMechanisms = numRSAMechanisms;
ed074c
-        }
ed074c
+	    break;
ed074c
+	}
ed074c
   
ed074c
         if( pMechanismList != NULL ) {
ed074c
             if( *pulCount < numMechanisms ) {
ed074c
@@ -438,13 +454,22 @@ C_GetMechanismInfo(CK_SLOT_ID slotID, CK
ed074c
             return CKR_TOKEN_NOT_PRESENT;
ed074c
         }
ed074c
 
ed074c
-        if ( slot->getIsECC()) {
ed074c
+	switch (slot->getAlgs()) {
ed074c
+	case ALG_ECC|ALG_RSA:
ed074c
+            mechanismList = allMechanismList;
ed074c
+            numMechanisms = numAllMechanisms;
ed074c
+	    break;
ed074c
+	case ALG_ECC:
ed074c
             mechanismList = ecMechanismList;
ed074c
             numMechanisms = numECMechanisms;
ed074c
-        } else {
ed074c
+	    break;
ed074c
+	case ALG_NONE:
ed074c
+	case ALG_RSA:
ed074c
+	default:
ed074c
             mechanismList = rsaMechanismList;
ed074c
             numMechanisms = numRSAMechanisms;
ed074c
-        }
ed074c
+	    break;
ed074c
+	}
ed074c
 
ed074c
         for(unsigned int i=0; i < numMechanisms; ++i ) {
ed074c
             if( mechanismList[i].mech == type ) {
ed074c
diff -up ./src/coolkey/object.cpp.alt-cac ./src/coolkey/object.cpp
ed074c
--- ./src/coolkey/object.cpp.alt-cac	2016-12-01 15:37:49.097167608 -0800
ed074c
+++ ./src/coolkey/object.cpp	2016-12-01 15:37:49.114167910 -0800
ed074c
@@ -1232,7 +1232,7 @@ Reader::Reader(unsigned long muscleObjID
ed074c
 }
ed074c
 
ed074c
 
ed074c
-CACPrivKey::CACPrivKey(CKYByte instance, const PKCS11Object &cert) : 
ed074c
+CACPrivKey::CACPrivKey(CKYByte instance, const PKCS11Object &cert,bool isPIV) : 
ed074c
         PKCS11Object( ((int)'k') << 24 | ((int)instance+'0') << 16,
ed074c
                          instance | 0x400)
ed074c
 {
ed074c
@@ -1242,7 +1242,9 @@ CACPrivKey::CACPrivKey(CKYByte instance,
ed074c
 
ed074c
     /* So we know what the key is supposed to be used for based on
ed074c
      * the instance */
ed074c
-    if (instance == 2) {
ed074c
+    /* instance 2 is usually a decryption cert. >2 are usually old decryption 
ed074c
+     * certs */
ed074c
+    if (instance == 2 || (instance > (isPIV ? 3 : 2))) {
ed074c
         decrypt = TRUE;
ed074c
     }
ed074c
 
ed074c
@@ -1305,8 +1307,8 @@ CACPrivKey::CACPrivKey(CKYByte instance,
ed074c
      CKYBuffer_FreeData(&param2);
ed074c
 }
ed074c
 
ed074c
-CACPubKey::CACPubKey(CKYByte instance, const PKCS11Object &cert) : 
ed074c
-        PKCS11Object( ((int)'k') << 24 | ((int)(instance+'5')) << 16,
ed074c
+CACPubKey::CACPubKey(CKYByte instance, const PKCS11Object &cert, bool isPIV) : 
ed074c
+        PKCS11Object( ((int)'k') << 24 | ((int)(instance+'a')) << 16,
ed074c
                        instance | 0x500)
ed074c
 {
ed074c
     CKYBuffer id;
ed074c
@@ -1315,7 +1317,7 @@ CACPubKey::CACPubKey(CKYByte instance, c
ed074c
 
ed074c
     /* So we know what the key is supposed to be used for based on
ed074c
      * the instance */
ed074c
-    if (instance == 2) {
ed074c
+    if (instance == 2 || (instance > (isPIV ? 3 : 2))) {
ed074c
         encrypt = TRUE;
ed074c
     }
ed074c
 
ed074c
@@ -1359,6 +1361,9 @@ CACPubKey::CACPubKey(CKYByte instance, c
ed074c
             setAttribute(CKA_EC_POINT, &param1);
ed074c
             setAttribute(CKA_EC_PARAMS, &param2);
ed074c
 	    setAttributeULong(CKA_KEY_TYPE, CKK_EC);
ed074c
+    	    setAttributeBool(CKA_VERIFY_RECOVER, FALSE);
ed074c
+    	    setAttributeBool(CKA_ENCRYPT, FALSE);
ed074c
+    	    setAttributeBool(CKA_DERIVE, encrypt);
ed074c
             break;
ed074c
         default:
ed074c
             break;
ed074c
@@ -1376,6 +1381,26 @@ static const char *CAC_Label[] = {
ed074c
         "CAC ID Certificate",
ed074c
         "CAC Email Signature Certificate",
ed074c
         "CAC Email Encryption Certificate",
ed074c
+        "CAC Cert 3",
ed074c
+        "CAC Cert 4",
ed074c
+        "CAC Cert 5",
ed074c
+        "CAC Cert 6",
ed074c
+        "CAC Cert 7",
ed074c
+        "CAC Cert 8",
ed074c
+        "CAC Cert 9",
ed074c
+};
ed074c
+
ed074c
+static const char *PIV_Label[] = {
ed074c
+        "PIV ID Certificate",
ed074c
+        "PIV Email Signature Certificate",
ed074c
+        "PIV Email Encryption Certificate",
ed074c
+        "PIV Card Authentication Certificate",
ed074c
+        "PIV Cert 4",
ed074c
+        "PIV Cert 5",
ed074c
+        "PIV Cert 6",
ed074c
+        "PIV Cert 7",
ed074c
+        "PIV Cert 8",
ed074c
+        "PIV Cert 9",
ed074c
 };
ed074c
 
ed074c
 static const unsigned char CN_DATA[] = { 0x55, 0x4, 0x3 };
ed074c
@@ -1454,7 +1479,7 @@ GetUserName(const CKYBuffer *dn)
ed074c
     return string;
ed074c
 }
ed074c
 
ed074c
-CACCert::CACCert(CKYByte instance, const CKYBuffer *derCert) : 
ed074c
+CACCert::CACCert(CKYByte instance, const CKYBuffer *derCert, bool isPIV) : 
ed074c
         PKCS11Object( ((int)'c') << 24 | ((int)instance+'0') << 16, 
ed074c
                         instance | 0x600)
ed074c
 {
ed074c
@@ -1471,7 +1496,7 @@ CACCert::CACCert(CKYByte instance, const
ed074c
     setAttribute(CKA_ID, &id;;
ed074c
     CKYBuffer_FreeData(&id;;
ed074c
     setAttributeULong(CKA_CERTIFICATE_TYPE, CKC_X_509);
ed074c
-    setAttribute(CKA_LABEL, CAC_Label[instance]);
ed074c
+    setAttribute(CKA_LABEL, isPIV ? PIV_Label[instance] : CAC_Label[instance]);
ed074c
 
ed074c
     CKYBuffer derSerial; CKYBuffer_InitEmpty(&derSerial);
ed074c
     CKYBuffer derSubject; CKYBuffer_InitEmpty(&derSubject);
ed074c
diff -up ./src/coolkey/object.h.alt-cac ./src/coolkey/object.h
ed074c
--- ./src/coolkey/object.h.alt-cac	2016-12-01 15:37:49.087167430 -0800
ed074c
+++ ./src/coolkey/object.h	2016-12-01 15:37:49.115167928 -0800
ed074c
@@ -219,17 +219,17 @@ class Cert : public PKCS11Object {
ed074c
 
ed074c
 class CACPrivKey : public PKCS11Object {
ed074c
   public:
ed074c
-    CACPrivKey(CKYByte instance, const PKCS11Object &cert);
ed074c
+    CACPrivKey(CKYByte instance, const PKCS11Object &cert, bool isPIV);
ed074c
 };
ed074c
 
ed074c
 class CACPubKey : public PKCS11Object {
ed074c
   public:
ed074c
-    CACPubKey(CKYByte instance, const PKCS11Object &cert);
ed074c
+    CACPubKey(CKYByte instance, const PKCS11Object &cert, bool isPIV);
ed074c
 };
ed074c
 
ed074c
 class CACCert : public PKCS11Object {
ed074c
   public:
ed074c
-    CACCert(CKYByte instance, const CKYBuffer *derCert);
ed074c
+    CACCert(CKYByte instance, const CKYBuffer *derCert, bool isPIV);
ed074c
 };
ed074c
 
ed074c
 typedef enum { PK15StateInit, PK15StateNeedObject, 
ed074c
diff -up ./src/coolkey/slot.cpp.alt-cac ./src/coolkey/slot.cpp
ed074c
--- ./src/coolkey/slot.cpp.alt-cac	2016-12-01 15:37:49.110167839 -0800
ed074c
+++ ./src/coolkey/slot.cpp	2016-12-01 15:57:37.307994776 -0800
ed074c
@@ -415,8 +415,9 @@ Slot::Slot(const char *readerName_, Log
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), mECC(false), p15aid(0), p15odfAddr(0),
ed074c
-	p15tokenInfoAddr(0), p15Instance(0),
ed074c
+	pivContainer(-1), pivKey(-1), maxCacCerts(MAX_CERT_SLOTS), 
ed074c
+	algs(ALG_NONE), p15aid(0), p15odfAddr(0), p15tokenInfoAddr(0),
ed074c
+	p15Instance(0),
ed074c
 #ifdef USE_SHMEM
ed074c
 	shmem(readerName_),
ed074c
 #endif
ed074c
@@ -776,6 +777,7 @@ Slot::connectToToken()
ed074c
 	 state |= PIV_CARD | APPLET_SELECTABLE | APPLET_PERSONALIZED;
ed074c
 	 isVersion1Key = 0;
ed074c
 	 needLogin = true;
ed074c
+	 maxCacCerts = MAX_CERT_SLOTS;
ed074c
          mCoolkey = 0;
ed074c
 	 mOldCAC = 0;
ed074c
 	 mCACLocalLogin = getPIVLoginType();
ed074c
@@ -927,8 +929,12 @@ Slot::getCACAid()
ed074c
 	}
ed074c
 	/* yes, fill in the old applets */
ed074c
 	mOldCAC = true;
ed074c
+	maxCacCerts = 1;
ed074c
 	for (i=1; i< MAX_CERT_SLOTS; i++) {
ed074c
-	    CACApplet_SelectPKI(conn, &cardAID[i], i, NULL);
ed074c
+	    status = CACApplet_SelectPKI(conn, &cardAID[i], i, NULL);
ed074c
+	    if (status == CKYSUCCESS) {
ed074c
+		maxCacCerts = i+1;
ed074c
+	    }
ed074c
 	}
ed074c
 	return CKYSUCCESS;
ed074c
     }
ed074c
@@ -986,6 +992,7 @@ Slot::getCACAid()
ed074c
     if (certSlot == 0) {
ed074c
 	status = CKYAPDUFAIL; /* probably neeed a beter error code */
ed074c
     }
ed074c
+    maxCacCerts = certSlot;
ed074c
 
ed074c
 done:
ed074c
     CKYBuffer_FreeData(&tBuf);
ed074c
@@ -2168,12 +2175,11 @@ Slot::addKeyObject(list<PKCS11Object>& o
ed074c
         }
ed074c
         keyObj.completeKey(*iter);
ed074c
 
ed074c
-        /* For now this is how we determine what type of key.
ed074c
-           Also for now, allow only one or the other */
ed074c
+        /*  use key object to determine what algorithms we support */
ed074c
         if ( keyObj.getKeyType() == PKCS11Object::ecc) {
ed074c
-            mECC = true;
ed074c
+            algs = (SlotAlgs) (algs | ALG_ECC);
ed074c
         } else {
ed074c
-            mECC = false;
ed074c
+            algs = (SlotAlgs) (algs | ALG_RSA);
ed074c
         }
ed074c
        
ed074c
     }
ed074c
@@ -2205,7 +2211,7 @@ Slot::addCertObject(list<PKCS11Object>&
ed074c
 void
ed074c
 Slot::unloadObjects()
ed074c
 {
ed074c
-    mECC = false;
ed074c
+    algs = ALG_NONE;
ed074c
     tokenObjects.clear();
ed074c
     free(personName);
ed074c
     personName = NULL;
ed074c
@@ -2269,29 +2275,42 @@ Slot::unloadObjects()
ed074c
 // Shared memory segments are fixed size (equal to the object memory size of
ed074c
 // the token). 
ed074c
 //
ed074c
+//
ed074c
+//
ed074c
+
ed074c
+struct SlotDataPair {
ed074c
+    unsigned long dataOffset;
ed074c
+    unsigned long  dataSize;
ed074c
+};
ed074c
 
ed074c
 struct SlotSegmentHeader {
ed074c
     unsigned short version;
ed074c
     unsigned short headerSize;
ed074c
     unsigned char  valid;
ed074c
-    unsigned char  reserved;
ed074c
+    unsigned char  firstCacCert;
ed074c
     unsigned char  cuid[10];
ed074c
-    unsigned short reserved2;
ed074c
+
ed074c
+    unsigned short reserved; 
ed074c
     unsigned short dataVersion;
ed074c
     unsigned short dataHeaderOffset;
ed074c
     unsigned short dataOffset;
ed074c
     unsigned long  dataHeaderSize;
ed074c
     unsigned long  dataSize;
ed074c
-    unsigned long  cert2Offset;
ed074c
-    unsigned long  cert2Size;
ed074c
+    unsigned long  nextDataOffset;
ed074c
+    SlotDataPair cacCerts[MAX_CERT_SLOTS];
ed074c
 };
ed074c
 
ed074c
+const unsigned char NOT_A_CAC=0xff; /* place in firstCacCert field */
ed074c
+const unsigned short CAC_DATA_VERSION=2;
ed074c
+
ed074c
+
ed074c
 #define MAX_OBJECT_STORE_SIZE 15000
ed074c
 //
ed074c
 // previous development versions used a segment prefix of
ed074c
 // "coolkeypk11s"
ed074c
 //
ed074c
-#define SEGMENT_PREFIX "coolkeypk11s"
ed074c
+#define SEGMENT_PREFIX "coolkeypk11t" // update segment since the old cache was
ed074c
+                                      // incompatible
ed074c
 #define CAC_FAKE_CUID "CAC Certs"
ed074c
 SlotMemSegment::SlotMemSegment(const char *readerName): 
ed074c
 	segmentAddr(NULL),  segmentSize(0), segment(NULL)
ed074c
@@ -2320,9 +2339,8 @@ SlotMemSegment::SlotMemSegment(const cha
ed074c
 	return;
ed074c
     }
ed074c
 
ed074c
-    SlotSegmentHeader *segmentHeader = (SlotSegmentHeader *)segmentAddr;
ed074c
     if (needInit) {
ed074c
-	segmentHeader->valid = 0;
ed074c
+	clearValid(0);
ed074c
     }
ed074c
     segmentSize = segment->getSHMemSize();
ed074c
 }
ed074c
@@ -2396,6 +2414,18 @@ SlotMemSegment::getDataVersion() const
ed074c
     return segmentHeader->dataVersion;
ed074c
 }
ed074c
 
ed074c
+unsigned char
ed074c
+SlotMemSegment::getFirstCacCert() const
ed074c
+{
ed074c
+    if (!segment) {
ed074c
+	return NOT_A_CAC;
ed074c
+    }
ed074c
+
ed074c
+    SlotSegmentHeader *segmentHeader = (SlotSegmentHeader *)segmentAddr;
ed074c
+
ed074c
+    return segmentHeader->firstCacCert;
ed074c
+}
ed074c
+
ed074c
 void
ed074c
 SlotMemSegment::setVersion(unsigned short version)
ed074c
 {
ed074c
@@ -2419,6 +2449,18 @@ SlotMemSegment::setDataVersion(unsigned
ed074c
     segmentHeader->dataVersion = version;
ed074c
 }
ed074c
 
ed074c
+void
ed074c
+SlotMemSegment::setFirstCacCert(unsigned char firstCacCert)
ed074c
+{
ed074c
+    if (!segment) {
ed074c
+	return;
ed074c
+    }
ed074c
+
ed074c
+    SlotSegmentHeader *segmentHeader = (SlotSegmentHeader *)segmentAddr;
ed074c
+
ed074c
+    segmentHeader->firstCacCert = firstCacCert;
ed074c
+}
ed074c
+
ed074c
 bool
ed074c
 SlotMemSegment::isValid() const
ed074c
 {
ed074c
@@ -2493,23 +2535,13 @@ SlotMemSegment::readCACCert(CKYBuffer *o
ed074c
     int size;
ed074c
     CKYByte *data;
ed074c
 
ed074c
-    switch (instance) {
ed074c
-    case 0:
ed074c
-	data  = (CKYByte *) &segmentAddr[segmentHeader->dataHeaderOffset];
ed074c
-	size = segmentHeader->dataHeaderSize;
ed074c
-	break;
ed074c
-    case 1:
ed074c
-	data  = (CKYByte *) &segmentAddr[segmentHeader->dataOffset];
ed074c
-	size = segmentHeader->dataSize;
ed074c
-	break;
ed074c
-    case 2:
ed074c
-	data  = (CKYByte *) &segmentAddr[segmentHeader->cert2Offset];
ed074c
-	size = segmentHeader->cert2Size;
ed074c
-	break;
ed074c
-    default:
ed074c
+    if (instance >= MAX_CERT_SLOTS) {
ed074c
 	CKYBuffer_Resize(objData, 0);
ed074c
 	return;
ed074c
     }
ed074c
+    data = (CKYByte *) &segmentAddr[segmentHeader->cacCerts[instance]
ed074c
+								.dataOffset];
ed074c
+    size = segmentHeader->cacCerts[instance].dataSize;
ed074c
     CKYBuffer_Replace(objData, 0, data, size);
ed074c
 }
ed074c
 
ed074c
@@ -2523,30 +2555,20 @@ SlotMemSegment::writeCACCert(const CKYBu
ed074c
     SlotSegmentHeader *segmentHeader = (SlotSegmentHeader *)segmentAddr;
ed074c
     int size = CKYBuffer_Size(data);
ed074c
     CKYByte *shmData;
ed074c
-    switch (instance) {
ed074c
-    case 0:
ed074c
-	segmentHeader->headerSize = sizeof *segmentHeader;
ed074c
-	segmentHeader->dataHeaderOffset = sizeof *segmentHeader;
ed074c
-	segmentHeader->dataHeaderSize = size;
ed074c
-	segmentHeader->dataOffset = segmentHeader->dataHeaderOffset + size;
ed074c
-	segmentHeader->dataSize = 0;
ed074c
-	segmentHeader->cert2Offset = segmentHeader->dataOffset;
ed074c
-	segmentHeader->cert2Size = 0;
ed074c
-	shmData = (CKYByte *) &segmentAddr[segmentHeader->dataHeaderOffset];
ed074c
-	break;
ed074c
-    case 1:
ed074c
-	segmentHeader->dataSize = size;
ed074c
-	segmentHeader->cert2Offset = segmentHeader->dataOffset + size;
ed074c
-	segmentHeader->cert2Size = 0;
ed074c
-	shmData = (CKYByte *) &segmentAddr[segmentHeader->dataOffset];
ed074c
-	break;
ed074c
-    case 2:
ed074c
-	segmentHeader->cert2Size = size;
ed074c
-	shmData = (CKYByte *) &segmentAddr[segmentHeader->cert2Offset];
ed074c
-	break;
ed074c
-    default:
ed074c
+
ed074c
+    if (instance >= MAX_CERT_SLOTS) {
ed074c
 	return;
ed074c
     }
ed074c
+
ed074c
+    if (segmentHeader->firstCacCert == NOT_A_CAC) {
ed074c
+	segmentHeader->firstCacCert = instance;
ed074c
+    }
ed074c
+    unsigned long dataOffset = segmentHeader->nextDataOffset;
ed074c
+    segmentHeader->cacCerts[instance].dataOffset = dataOffset;
ed074c
+    segmentHeader->nextDataOffset += size;
ed074c
+    segmentHeader->cacCerts[instance].dataSize = size;
ed074c
+    shmData = (CKYByte *) &segmentAddr[dataOffset];
ed074c
+
ed074c
     memcpy(shmData, CKYBuffer_Data(data), size);
ed074c
 }
ed074c
 
ed074c
@@ -2558,15 +2580,18 @@ SlotMemSegment::clearValid(CKYByte insta
ed074c
 	return;
ed074c
     }
ed074c
     SlotSegmentHeader *segmentHeader = (SlotSegmentHeader *)segmentAddr;
ed074c
-    switch (instance) {
ed074c
-    case 0:
ed074c
-	segmentHeader->headerSize = 0;
ed074c
-	segmentHeader->dataHeaderSize = 0;
ed074c
-	/* fall through */
ed074c
-    case 1:
ed074c
-	segmentHeader->dataSize = 0;
ed074c
+
ed074c
+    segmentHeader->headerSize = sizeof *segmentHeader;
ed074c
+    segmentHeader->dataHeaderOffset = sizeof *segmentHeader;
ed074c
+    segmentHeader->dataHeaderSize = 0;
ed074c
+    segmentHeader->dataSize = 0;
ed074c
+    for (int i=0; i < MAX_CERT_SLOTS; i++) {
ed074c
+	segmentHeader->cacCerts[i].dataSize = 0;
ed074c
     }
ed074c
+    segmentHeader->dataOffset = sizeof *segmentHeader;
ed074c
+    segmentHeader->nextDataOffset = sizeof *segmentHeader;
ed074c
     segmentHeader->valid = 0;
ed074c
+    segmentHeader->firstCacCert = NOT_A_CAC;
ed074c
 }
ed074c
 
ed074c
 void
ed074c
@@ -2882,8 +2907,7 @@ berProcess(CKYBuffer *buf, int matchTag,
ed074c
 
ed074c
 
ed074c
 CKYStatus
ed074c
-Slot::readCACCertificateFirst(CKYBuffer *cert, CKYSize *nextSize, 
ed074c
-			      bool throwException)
ed074c
+Slot::readCACCertificateFirst(CKYBuffer *cert, CKYSize *nextSize)
ed074c
 {
ed074c
     CKYStatus status;
ed074c
     CKYISOStatus apduRC;
ed074c
@@ -2897,9 +2921,6 @@ Slot::readCACCertificateFirst(CKYBuffer
ed074c
 	CKYBuffer_InitEmpty(&certInfo);
ed074c
 	CKYBuffer_Resize(cert, 0);
ed074c
 	status = PIVApplet_GetCertificate(conn, cert, pivContainer, &apduRC);
ed074c
-	if (throwException && (status != CKYSUCCESS)) {
ed074c
-	    handleConnectionError();
ed074c
-	}
ed074c
 	/* actually, on success, we need to parse the certificate and find the
ed074c
 	 * propper tag */
ed074c
 	if (status == CKYSUCCESS) {
ed074c
@@ -2940,10 +2961,10 @@ Slot::readCACCertificateFirst(CKYBuffer
ed074c
     if (mOldCAC) {
ed074c
 	/* get the first 100 bytes of the cert */
ed074c
 	status = CACApplet_GetCertificateFirst(conn, cert, nextSize, &apduRC);
ed074c
-	if (throwException && (status != CKYSUCCESS)) {
ed074c
-	    handleConnectionError();
ed074c
+	if (status == CKYSUCCESS) {
ed074c
+	    return status;
ed074c
 	}
ed074c
-	return status;
ed074c
+	/* try to use CACApplet_ReadFile before we give up */
ed074c
     }
ed074c
 
ed074c
     CKYBuffer tBuf;
ed074c
@@ -2959,11 +2980,11 @@ Slot::readCACCertificateFirst(CKYBuffer
ed074c
 
ed074c
     /* handle the new CAC card read */
ed074c
     /* read the TLV */
ed074c
-    status = CACApplet_ReadFile(conn, CAC_TAG_FILE, &tBuf, NULL);
ed074c
+    status = CACApplet_ReadFile(conn, CAC_TAG_FILE, &tBuf, &apduRC);
ed074c
     if (status != CKYSUCCESS) {
ed074c
 	goto done;
ed074c
     }
ed074c
-    status = CACApplet_ReadFile(conn, CAC_VALUE_FILE, &vBuf, NULL);
ed074c
+    status = CACApplet_ReadFile(conn, CAC_VALUE_FILE, &vBuf, &apduRC);
ed074c
     if (status != CKYSUCCESS) {
ed074c
 	goto done;
ed074c
     }
ed074c
@@ -3199,14 +3220,12 @@ Slot::loadCACCert(CKYByte instance)
ed074c
     CKYStatus status = CKYSUCCESS;
ed074c
     CKYBuffer cert;
ed074c
     CKYBuffer rawCert;
ed074c
-    CKYBuffer shmCert;
ed074c
     CKYSize  nextSize;
ed074c
 
ed074c
     OSTime time = OSTimeNow();
ed074c
 
ed074c
     CKYBuffer_InitEmpty(&cert);
ed074c
     CKYBuffer_InitEmpty(&rawCert);
ed074c
-    CKYBuffer_InitEmpty(&shmCert);
ed074c
 
ed074c
     //
ed074c
     // not all CAC cards have all the PKI instances
ed074c
@@ -3215,78 +3234,24 @@ Slot::loadCACCert(CKYByte instance)
ed074c
     try {
ed074c
         selectCACApplet(instance, false);
ed074c
     } catch(PKCS11Exception& e) {
ed074c
-	// all CAC's must have instance '0', throw the error it
ed074c
-	// they don't.
ed074c
-	if (instance == 0) throw e;
ed074c
-	// If the CAC doesn't have instance '2', and we were updating
ed074c
-	// the shared memory, set it to valid now.
ed074c
-	if ((instance == 2) && !shmem.isValid()) {
ed074c
-	    shmem.setValid();
ed074c
-	}
ed074c
 	return;
ed074c
     }
ed074c
 
ed074c
     log->log("CAC Cert %d: select CAC applet:  %d ms\n",
ed074c
 						 instance, OSTimeNow() - time);
ed074c
 
ed074c
-    if (instance == 0) {
ed074c
-	readCACCertificateFirst(&rawCert, &nextSize, true);
ed074c
-
ed074c
-        if(CKYBuffer_Size(&rawCert) <= 1) {
ed074c
-             handleConnectionError();
ed074c
-        }
ed074c
-	log->log("CAC Cert %d: fetch CAC Cert:  %d ms\n", 
ed074c
-						instance, OSTimeNow() - time);
ed074c
-    }
ed074c
-
ed074c
-    unsigned short dataVersion = 1;
ed074c
-    CKYBool needRead = 1;
ed074c
-
ed074c
     /* see if it matches the shared memory */
ed074c
-    if (shmem.isValid() &&  shmem.getDataVersion() == dataVersion) {
ed074c
-	shmem.readCACCert(&shmCert, instance);
ed074c
-	CKYSize certSize = CKYBuffer_Size(&rawCert);
ed074c
-	CKYSize shmCertSize = CKYBuffer_Size(&shmCert);
ed074c
-	const CKYByte *shmData = CKYBuffer_Data(&shmCert);
ed074c
-
ed074c
-	if (instance != 0) {
ed074c
-	    needRead = 0;
ed074c
-	}
ed074c
-
ed074c
-	if (shmCertSize >= certSize) {
ed074c
-	    if (memcmp(shmData, CKYBuffer_Data(&rawCert), certSize) == 0) {
ed074c
-		/* yes it does, no need to read the rest of the cert, use
ed074c
-		 * the cache */
ed074c
-		CKYBuffer_Replace(&rawCert, 0, shmData, shmCertSize);
ed074c
-		needRead = 0;
ed074c
-	    }
ed074c
-	}
ed074c
-	if (!needRead && (shmCertSize == 0)) {	
ed074c
+    if (shmem.isValid() &&  shmem.getDataVersion() == CAC_DATA_VERSION) {
ed074c
+	shmem.readCACCert(&rawCert, instance);
ed074c
+	if (CKYBuffer_Size(&rawCert) == 0) {
ed074c
 	    /* no cert of this type, just return */
ed074c
 	    return;
ed074c
 	}
ed074c
-    }
ed074c
-    CKYBuffer_FreeData(&shmCert);
ed074c
-
ed074c
-    if (needRead) {
ed074c
-	/* it doesn't, read the new cert and update the cache */
ed074c
-	if (instance == 0) {
ed074c
-	    shmem.clearValid(0);
ed074c
-	    shmem.setVersion(SHMEM_VERSION);
ed074c
-	    shmem.setDataVersion(dataVersion);
ed074c
-	} else {
ed074c
-	    status = readCACCertificateFirst(&rawCert, &nextSize, false);
ed074c
-	
ed074c
-	    if ((status != CKYSUCCESS) || (CKYBuffer_Size(&rawCert) <= 1)) {
ed074c
-		/* CAC only requires the Certificate in pki '0' */
ed074c
-		/* if pki '1' or '2' are empty, treat it as a non-fatal error*/
ed074c
-		if (instance == 2) {
ed074c
-		    /* we've attempted to read all the certs, shared memory
ed074c
-		     * is now valid */
ed074c
-		    shmem.setValid();
ed074c
-		}
ed074c
-		return;
ed074c
-	    }
ed074c
+    } else {
ed074c
+	status = readCACCertificateFirst(&rawCert, &nextSize);
ed074c
+	if ((status != CKYSUCCESS) || (CKYBuffer_Size(&rawCert) <= 1)) {
ed074c
+	    /*this cert doesn't exist, go to the next one */
ed074c
+	    return;
ed074c
 	}
ed074c
 
ed074c
 	if (nextSize) {
ed074c
@@ -3298,9 +3263,6 @@ Slot::loadCACCert(CKYByte instance)
ed074c
 	    handleConnectionError();
ed074c
 	}
ed074c
 	shmem.writeCACCert(&rawCert, instance);
ed074c
-	if (instance == 2) {
ed074c
-	    shmem.setValid();
ed074c
-	}
ed074c
     }
ed074c
 
ed074c
 
ed074c
@@ -3368,14 +3330,17 @@ Slot::loadCACCert(CKYByte instance)
ed074c
     log->log("CAC Cert %d: Cert has been uncompressed:  %d ms\n",
ed074c
 						instance, OSTimeNow() - time);
ed074c
 
ed074c
-    CACCert certObj(instance, &cert);
ed074c
-    CACPrivKey privKey(instance, certObj);
ed074c
-    CACPubKey pubKey(instance, certObj);
ed074c
+    bool isPIV = (bool)((state & PIV_CARD) == PIV_CARD);
ed074c
+    CACCert certObj(instance, &cert, isPIV);
ed074c
+    CACPrivKey privKey(instance, certObj, isPIV);
ed074c
+    CACPubKey pubKey(instance, certObj, isPIV);
ed074c
     tokenObjects.push_back(privKey);
ed074c
     tokenObjects.push_back(pubKey);
ed074c
     tokenObjects.push_back(certObj);
ed074c
     if ( pubKey.getKeyType() == PKCS11Object::ecc) {
ed074c
-	mECC = 1;
ed074c
+        algs = (SlotAlgs) (algs | ALG_ECC);
ed074c
+    } else {
ed074c
+        algs = (SlotAlgs) (algs | ALG_RSA);
ed074c
     }
ed074c
 
ed074c
     if (personName == NULL) {
ed074c
@@ -3388,6 +3353,94 @@ Slot::loadCACCert(CKYByte instance)
ed074c
 }
ed074c
 
ed074c
 void
ed074c
+Slot::initCACShMem(void)
ed074c
+{
ed074c
+    bool failed = false;
ed074c
+
ed074c
+    unsigned char firstCert = shmem.getFirstCacCert();
ed074c
+
ed074c
+    log->log("init CACShMem: \n");
ed074c
+    /* check to make sure the shared memory is initialized with a CAC card */
ed074c
+    if (shmem.isValid() && shmem.getDataVersion() ==  CAC_DATA_VERSION
ed074c
+				&& firstCert != NOT_A_CAC) {
ed074c
+	CKYBuffer rawCert;
ed074c
+	CKYBuffer shmCert;
ed074c
+	CKYSize  nextSize;
ed074c
+
ed074c
+        log->log("init CACShMem: valid CAC cache found firstCert = %d\n",
ed074c
+						 firstCert);
ed074c
+	CKYBuffer_InitEmpty(&rawCert);
ed074c
+	CKYBuffer_InitEmpty(&shmCert);
ed074c
+
ed074c
+
ed074c
+	/* yes, see if it's this cac card by comparing the first cert 
ed074c
+	 * in the chain */
ed074c
+
ed074c
+	/* see if the first cert is in the expected slot */
ed074c
+	try {
ed074c
+	    selectCACApplet(firstCert, false);
ed074c
+ 	} catch(PKCS11Exception& e) {
ed074c
+	    failed = true;
ed074c
+            log->log("init CACShMem: applet select failed firstCert = %d\n",
ed074c
+							firstCert);
ed074c
+	}
ed074c
+	if (!failed) {
ed074c
+	    CKYStatus status = readCACCertificateFirst(&rawCert, &nextSize);
ed074c
+	    if ((status != CKYSUCCESS) || CKYBuffer_Size(&rawCert) <= 1) {
ed074c
+		failed = true;
ed074c
+                log->log("init CACShMem: read Cert failed firstCert = %d\n",
ed074c
+			 				firstCert);
ed074c
+	    }
ed074c
+	}
ed074c
+	if (!failed) {
ed074c
+	    shmem.readCACCert(&shmCert, firstCert);
ed074c
+	    CKYSize certSize = CKYBuffer_Size(&rawCert);
ed074c
+	    CKYSize shmCertSize = CKYBuffer_Size(&shmCert);
ed074c
+	    const CKYByte *shmData = CKYBuffer_Data(&shmCert);
ed074c
+
ed074c
+	    if (shmCertSize >= certSize) {
ed074c
+		if (memcmp(shmData, CKYBuffer_Data(&rawCert), certSize) == 0) {
ed074c
+		    /* this card is cached, go on and use the cache */
ed074c
+            	    log->log("init CACShMem: entries match, using cache\n");
ed074c
+		    CKYBuffer_FreeData(&rawCert);
ed074c
+		    CKYBuffer_FreeData(&shmCert);
ed074c
+		    return;
ed074c
+		}
ed074c
+            }		
ed074c
+            log->log("init CACShMem: no entry match certSize=%d"
ed074c
+				" shmCertSize=%d\n",certSize, shmCertSize);
ed074c
+	}
ed074c
+	CKYBuffer_FreeData(&rawCert);
ed074c
+	CKYBuffer_FreeData(&shmCert);
ed074c
+    }
ed074c
+
ed074c
+    log->log("init CACShMem: starting new cache valid=%d version=%d "
ed074c
+		" firstCert=%d\n",shmem.isValid(), shmem.getDataVersion(), 
ed074c
+				firstCert);
ed074c
+    /* cache is either invalid or for another card, start initializing it */
ed074c
+    shmem.clearValid(0);
ed074c
+    shmem.setVersion(SHMEM_VERSION);
ed074c
+    shmem.setDataVersion(CAC_DATA_VERSION);
ed074c
+}
ed074c
+
ed074c
+void
ed074c
+Slot::verifyCACShMem(void)
ed074c
+{
ed074c
+    /* if the memory is valid, then nothing to do */
ed074c
+    if (shmem.isValid()) {
ed074c
+	return;
ed074c
+    }
ed074c
+    /* if we didn't find any cert fail */
ed074c
+    if (shmem.getFirstCacCert() == NOT_A_CAC) {
ed074c
+	shmem.clearValid(0);
ed074c
+	disconnect();
ed074c
+        throw PKCS11Exception(CKR_DEVICE_REMOVED);
ed074c
+    }
ed074c
+    /* we're all set, let others see our results */
ed074c
+    shmem.setValid(); 
ed074c
+}
ed074c
+
ed074c
+void
ed074c
 Slot::loadObjects()
ed074c
 {
ed074c
     // throw away all token objects!
ed074c
@@ -3406,9 +3459,11 @@ Slot::loadObjects()
ed074c
     std::list<ListObjectInfo>::iterator iter;
ed074c
 
ed074c
     if (state & GOV_CARD) {
ed074c
-	loadCACCert(0);
ed074c
-	loadCACCert(1);
ed074c
-	loadCACCert(2);
ed074c
+	initCACShMem();
ed074c
+	for (int i=0; i < maxCacCerts; i++) {
ed074c
+	   loadCACCert(i);
ed074c
+	}
ed074c
+	verifyCACShMem();
ed074c
 	status = trans.end();
ed074c
 	loadReaderObject();
ed074c
 	return;
ed074c
@@ -4720,10 +4775,6 @@ Slot::performECCSignature(CKYBuffer *out
ed074c
     CKYStatus status = trans.begin(conn);
ed074c
     if( status != CKYSUCCESS ) handleConnectionError();
ed074c
 
ed074c
-    if (!mECC) {
ed074c
-        throw PKCS11Exception(CKR_FUNCTION_NOT_SUPPORTED);
ed074c
-    }
ed074c
-
ed074c
     CKYISOStatus result;
ed074c
     bool loginAttempted = false;
ed074c
 
ed074c
@@ -4790,9 +4841,6 @@ Slot::performRSAOp(CKYBuffer *output, co
ed074c
 		unsigned int keySize, const PKCS11Object *key, 
ed074c
 		CKYByte direction)
ed074c
 {
ed074c
-    if ( mECC ) {
ed074c
-        throw PKCS11Exception(CKR_FUNCTION_NOT_SUPPORTED);
ed074c
-    }
ed074c
 
ed074c
     //
ed074c
     // establish a transaction
ed074c
@@ -5145,10 +5193,6 @@ Slot::performECCKeyAgreement(CK_MECHANIS
ed074c
 	CKYBuffer *publicDataBuffer, CKYBuffer *secretKeyBuffer, 
ed074c
 	const PKCS11Object *key, unsigned int keySize)
ed074c
 {
ed074c
-    if (!mECC) {
ed074c
-       throw PKCS11Exception(CKR_FUNCTION_NOT_SUPPORTED);
ed074c
-    }
ed074c
-
ed074c
     Transaction trans;
ed074c
     CKYStatus status = trans.begin(conn);
ed074c
     if( status != CKYSUCCESS ) handleConnectionError();
ed074c
diff -up ./src/coolkey/slot.h.alt-cac ./src/coolkey/slot.h
ed074c
--- ./src/coolkey/slot.h.alt-cac	2016-12-01 15:37:49.104167732 -0800
ed074c
+++ ./src/coolkey/slot.h	2016-12-01 15:37:49.117167964 -0800
ed074c
@@ -79,9 +79,11 @@ public:
ed074c
     bool CUIDIsEqual(const CKYBuffer *cuid) const;
ed074c
     unsigned short getVersion() const;
ed074c
     unsigned short getDataVersion() const;
ed074c
+    unsigned char  getFirstCacCert() const;
ed074c
     void setCUID(const CKYBuffer *cuid);
ed074c
     void setVersion(unsigned short version);
ed074c
     void setDataVersion(unsigned short version);
ed074c
+    void setFirstCacCert(unsigned char firstCacCert);
ed074c
     bool isValid() const;
ed074c
     int size() const;
ed074c
     const unsigned char *getCUID() const;
ed074c
@@ -90,6 +92,7 @@ public:
ed074c
     void setSize(int size);
ed074c
     void readData(CKYBuffer *data) const;
ed074c
     void writeData(const CKYBuffer *data);
ed074c
+    void initCACHeaders(void);
ed074c
     void readCACCert(CKYBuffer *data, CKYByte instance) const;
ed074c
     void writeCACCert(const CKYBuffer *data, CKYByte instance);
ed074c
     void clearValid(CKYByte instance);
ed074c
@@ -294,7 +297,13 @@ class CryptParams {
ed074c
 				 const CKYBuffer *paddedOutput) const = 0;
ed074c
 };
ed074c
 
ed074c
-#define MAX_CERT_SLOTS 3
ed074c
+#define MAX_CERT_SLOTS 10
ed074c
+typedef enum {
ed074c
+	ALG_NONE= 0x0,
ed074c
+	ALG_ECC = 0x1,
ed074c
+	ALG_RSA = 0x2
ed074c
+} SlotAlgs;
ed074c
+
ed074c
 #define MAX_AUTH_USERS 3
ed074c
 class Slot {
ed074c
 
ed074c
@@ -349,7 +358,8 @@ class Slot {
ed074c
     bool mCACLocalLogin;
ed074c
     int pivContainer;
ed074c
     int pivKey;
ed074c
-    bool mECC;
ed074c
+    int maxCacCerts;
ed074c
+    SlotAlgs algs;
ed074c
     unsigned short p15aid;
ed074c
     unsigned short p15odfAddr;
ed074c
     unsigned short p15tokenInfoAddr;
ed074c
@@ -424,8 +434,7 @@ class Slot {
ed074c
     list<ListObjectInfo> fetchSeparateObjects();
ed074c
 
ed074c
     CKYStatus getCACAid();
ed074c
-    CKYStatus readCACCertificateFirst(CKYBuffer *cert, CKYSize *nextSize,
ed074c
-                              bool throwException);
ed074c
+    CKYStatus readCACCertificateFirst(CKYBuffer *cert, CKYSize *nextSize);
ed074c
     CKYStatus readCACCertificateAppend(CKYBuffer *cert, CKYSize nextSize);
ed074c
 
ed074c
     CKYStatus getP15Params();
ed074c
@@ -485,6 +494,8 @@ class Slot {
ed074c
     void processComputeCrypt(CKYBuffer *result, const CKYAPDU *apdu);
ed074c
 
ed074c
     CKYByte objectToKeyNum(const PKCS11Object *key);
ed074c
+    void initCACShMem(void);
ed074c
+    void verifyCACShMem(void);
ed074c
     Slot(const Slot &cpy)
ed074c
 #ifdef USE_SHMEM
ed074c
 	: shmem(readerName)
ed074c
@@ -580,7 +591,7 @@ class Slot {
ed074c
        CK_OBJECT_HANDLE hBaseKey, CK_ATTRIBUTE_PTR pTemplate, 
ed074c
        CK_ULONG ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey, CryptParams& params);
ed074c
 
ed074c
-    bool getIsECC() { return mECC; }
ed074c
+    SlotAlgs getAlgs() { return algs; }
ed074c
 };
ed074c
 
ed074c
 class SlotList {