Blame SOURCES/coolkey-1.1.0-cardos-5-3.patch

255307
diff -up ./src/coolkey/object.cpp.cardos-5-3 ./src/coolkey/object.cpp
255307
--- ./src/coolkey/object.cpp.cardos-5-3	2017-03-16 17:14:02.415338726 -0700
255307
+++ ./src/coolkey/object.cpp	2017-03-16 17:14:02.419338794 -0700
255307
@@ -32,7 +32,7 @@ const CKYByte eccOID[] = {0x2a,0x86,0x48
255307
 void dump(const char *label, const CKYBuffer *buf)
255307
 {
255307
     CKYSize i;
255307
-    CKYSize size = CKYBuffer_Size(buf);
255307
+    CKYSize size = buf ? CKYBuffer_Size(buf) : 0;
255307
 #define ROW_LENGTH 60
255307
     char string[ROW_LENGTH+1];
255307
     char *bp = &string[0];
255307
diff -up ./src/coolkey/object.h.cardos-5-3 ./src/coolkey/object.h
255307
--- ./src/coolkey/object.h.cardos-5-3	2017-03-16 17:14:02.415338726 -0700
255307
+++ ./src/coolkey/object.h	2017-03-16 17:14:02.419338794 -0700
255307
@@ -200,9 +200,11 @@ class PKCS11Object {
255307
     CK_USER_TYPE getUser(void) const { return user; }
255307
     void setKeyType(KeyType theType) { keyType = theType; }
255307
     void setKeySize(unsigned int keySize_) { keySize = keySize_; }
255307
+    void setUser(CK_USER_TYPE user_) { user = user_; }
255307
     const CKYBuffer *getAuthId(void) const { return &authId; }
255307
     const CKYBuffer *getPinAuthId(void) const { return &pinAuthId; }
255307
     const PK15ObjectPath &getObjectPath() const { return objectPath; }
255307
+    void setObjectPath(const PK15ObjectPath &newPath) { objectPath = newPath; }
255307
     void completeKey(const PKCS11Object &cert);
255307
 };
255307
 
255307
@@ -308,6 +310,7 @@ class PK15Object : public PKCS11Object {
255307
     bool isLocal(void) const { return 
255307
 			(pinInfo.pinFlags & P15PinLocal) ? true : false; }
255307
     const P15PinInfo *getPinInfo(void) const { return &pinInfo; }
255307
+    void setPinRef(CK_BYTE pinRef) { pinInfo.pinRef = pinRef; }
255307
 };
255307
 
255307
 class Reader : public PKCS11Object {
255307
diff -up ./src/coolkey/slot.cpp.cardos-5-3 ./src/coolkey/slot.cpp
255307
--- ./src/coolkey/slot.cpp.cardos-5-3	2017-03-16 17:14:02.416338743 -0700
255307
+++ ./src/coolkey/slot.cpp	2017-03-17 13:48:26.661205327 -0700
255307
@@ -41,6 +41,7 @@
255307
 #define PRINTF(args)
255307
 #endif
255307
 // #define DISPLAY_WHOLE_GET_DATA 1
255307
+void dump(const char *label, const CKYBuffer *buf);
255307
 
255307
 
255307
 // The Cyberflex Access 32k egate ATR
255307
@@ -467,6 +468,8 @@ Slot::Slot(const char *readerName_, Log
255307
     }
255307
     CKYBuffer_InitEmpty(&cardATR);
255307
     CKYBuffer_InitEmpty(&mCUID);
255307
+    CKYBuffer_InitEmpty(&candidateUserAuthId);
255307
+    CKYBuffer_InitEmpty(&candidateContextSpecificAuthId);
255307
     for (int i=0; i < MAX_CERT_SLOTS; i++) {
255307
 	CKYBuffer_InitEmpty(&cardAID[i]);
255307
     }
255307
@@ -540,6 +543,8 @@ Slot::~Slot()
255307
     CKYBuffer_FreeData(&nonce);
255307
     CKYBuffer_FreeData(&cardATR);
255307
     CKYBuffer_FreeData(&mCUID);
255307
+    CKYBuffer_FreeData(&candidateUserAuthId);
255307
+    CKYBuffer_FreeData(&candidateContextSpecificAuthId);
255307
     CKYBuffer_FreeData(&p15AID);
255307
     CKYBuffer_FreeData(&p15odf);
255307
     CKYBuffer_FreeData(&p15tokenInfo);
255307
@@ -1272,6 +1277,41 @@ class ObjectKeyCKAIDMatch {
255307
     }
255307
 };
255307
 
255307
+#ifdef DEBUG
255307
+void
255307
+dumpPin(const char *label, const PK15Object *pin)
255307
+{
255307
+    const P15PinInfo *pinInfo = pin->getPinInfo();
255307
+    const PK15ObjectPath &pinPath=pin->getObjectPath(); 
255307
+    unsigned int pin_type = (unsigned int) pinInfo->pinType;
255307
+    const char *pin_name[3] = {"BCD", "ASCIINum", "UTF8" };
255307
+
255307
+    printf("Pin Object %s\n",label);
255307
+    printf(" Pin flags=0x%08lx\n",pinInfo->pinFlags);
255307
+    printf(" Pin type=%d (%s)\n",(int) pin_type, 
255307
+		pin_type < 3U ? pin_name[pin_type] :
255307
+		"Invalid");
255307
+    printf(" Pin length= %d (%d - %d)\n",(int)pinInfo->storedLength,
255307
+					(int)pinInfo->minLength, 
255307
+					(int)pinInfo->maxLength);
255307
+    printf(" Pin pad = 0x%02x,<%c>\n", pinInfo->padChar, pinInfo->padChar);
255307
+    printf(" Pin Ref = 0x%02x\n", pinInfo->pinRef);
255307
+    printf(" Pin Path index = %ld\n",(long)pinPath.getIndex());
255307
+    printf(" Pin Path size = %ld\n",(long)pinPath.getLength());
255307
+    dump(" Pin Path:",pinPath.getPath());
255307
+}
255307
+
255307
+void
255307
+dumpPath(const char *label, const PK15ObjectPath &path) 
255307
+{
255307
+    printf(" Path for %s\n", label);
255307
+    printf(" index = %ld\n",(long)path.getIndex());
255307
+    printf(" size = %ld\n",(long)path.getLength());
255307
+    dump(" objPath:",path.getPath());
255307
+}
255307
+#endif
255307
+
255307
+
255307
 CKYStatus
255307
 Slot::parseEF_Directory(const CKYByte *current, 
255307
 					CKYSize size, PK15ObjectType type)
255307
@@ -1326,7 +1366,22 @@ Slot::parseEF_Directory(const CKYByte *c
255307
 			auth[CKU_SO] = new PK15Object(obj);
255307
 		    }
255307
 		} else if (auth[CKU_USER] == NULL) {
255307
+		    const CKYBuffer *authid = obj.getPinAuthId();
255307
 		    auth[CKU_USER] = new PK15Object(obj);
255307
+		    if ((CKYBuffer_Size(&candidateUserAuthId) != 0) 
255307
+			&& !CKYBuffer_IsEqual(authid, &candidateUserAuthId)) {
255307
+			/* validate our candidates */
255307
+			if ((CKYBuffer_Size(&candidateContextSpecificAuthId)
255307
+			     == 0) || (CKYBuffer_IsEqual(
255307
+				&candidateContextSpecificAuthId, authid))) {
255307
+			    CKYBuffer_Replace(&candidateContextSpecificAuthId,0,
255307
+				CKYBuffer_Data(&candidateUserAuthId),
255307
+			        CKYBuffer_Size(&candidateUserAuthId));
255307
+			}
255307
+			CKYBuffer_Replace(&candidateUserAuthId, 0,
255307
+				CKYBuffer_Data(authid), CKYBuffer_Size(authid));
255307
+		    }
255307
+			
255307
 		} else if (auth[CKU_CONTEXT_SPECIFIC] == NULL) {
255307
 		    ObjectIter iter;
255307
 		    const CKYBuffer *authid = obj.getPinAuthId();
255307
@@ -1339,6 +1394,8 @@ Slot::parseEF_Directory(const CKYByte *c
255307
 			if( CKYBuffer_IsEqual(iter->getAuthId(),authid)) {
255307
 			    iter->setAttributeBool(CKA_ALWAYS_AUTHENTICATE,
255307
 						   TRUE);
255307
+			    iter->setUser(CKU_CONTEXT_SPECIFIC);
255307
+printf("Setting Context Specific pin on key\n");
255307
 			}
255307
 		    }
255307
 		}
255307
@@ -1349,7 +1406,19 @@ Slot::parseEF_Directory(const CKYByte *c
255307
 		{
255307
 		    ObjectConstIter iter;
255307
 		    const CKYBuffer *id;
255307
+		    const CKYBuffer *authid;
255307
 
255307
+		    authid = obj.getAuthId();
255307
+		    if (authid) {
255307
+			if (CKYBuffer_Size(&candidateUserAuthId) == 0) {
255307
+			    CKYBuffer_Replace(&candidateUserAuthId, 0,
255307
+				CKYBuffer_Data(authid), CKYBuffer_Size(authid));
255307
+			} else if (!CKYBuffer_IsEqual(&candidateUserAuthId, 
255307
+							authid)) {
255307
+			    CKYBuffer_Replace(&candidateContextSpecificAuthId,0,
255307
+				CKYBuffer_Data(authid), CKYBuffer_Size(authid));
255307
+			}
255307
+		    }
255307
 		    id = obj.getAttribute(CKA_ID);
255307
 		    if ((!id) || (CKYBuffer_Size(id) != 1)) {
255307
 			break;
255307
@@ -1386,6 +1455,31 @@ Slot::parseEF_Directory(const CKYByte *c
255307
     	    tokenObjects.push_back(obj);
255307
   	} while ( false );
255307
     }
255307
+
255307
+    /* handle the case where we have context specific with the same user pin */
255307
+    if ((type == PK15AuthObj) 
255307
+		&& (CKYBuffer_Size(&candidateContextSpecificAuthId) != 0)
255307
+		&& (auth[CKU_CONTEXT_SPECIFIC] == NULL)) {
255307
+	ObjectIter iter;
255307
+
255307
+	/* these should put on the individual keys */
255307
+	auth[CKU_CONTEXT_SPECIFIC] = new PK15Object(*auth[CKU_USER]);
255307
+	/* set the pin ref for the context specific auth */
255307
+	auth[CKU_CONTEXT_SPECIFIC]->setPinRef(
255307
+	    (CK_BYTE) CKYBuffer_GetChar(&candidateContextSpecificAuthId,0));
255307
+	for( iter = tokenObjects.begin(); iter != tokenObjects.end(); ++iter) {
255307
+	    const CKYBuffer *authid = iter->getAuthId();
255307
+	    if(authid && 
255307
+		CKYBuffer_IsEqual(authid,&candidateContextSpecificAuthId)) {
255307
+		 iter->setAttributeBool(CKA_ALWAYS_AUTHENTICATE, TRUE);
255307
+		 iter->setUser(CKU_CONTEXT_SPECIFIC);
255307
+		 /* auth[CKU_CONTEXT_SPECIFIC]->
255307
+				setObjectPath(iter->getObjectPath()); */
255307
+	    }
255307
+	}
255307
+	
255307
+
255307
+    }
255307
     CKYBuffer_FreeData(&file;;
255307
     return CKYSUCCESS;
255307
 }
255307
@@ -2221,6 +2315,12 @@ Slot::unloadObjects()
255307
 	tokenManufacturer = NULL;
255307
     }
255307
     CKYBuffer_Resize(&p15serialNumber,0);
255307
+    CKYBuffer_Resize(&candidateUserAuthId,0);
255307
+    CKYBuffer_Resize(&candidateContextSpecificAuthId,0);
255307
+    for (int i=0; i < MAX_AUTH_USERS; i++) {
255307
+	if (auth[i]) delete auth[i];
255307
+	auth[i]=NULL;
255307
+    }
255307
 }
255307
 
255307
 #ifdef USE_SHMEM
255307
@@ -3766,7 +3866,6 @@ Slot::attemptLogin(CK_USER_TYPE user, bo
255307
 	contextPinCache.clearPin();
255307
     }
255307
 }
255307
-void dump(const char *label, const CKYBuffer *buf);
255307
 
255307
 void
255307
 Slot::attemptP15Login(CK_USER_TYPE user)
255307
@@ -3794,7 +3893,6 @@ Slot::attemptP15Login(CK_USER_TYPE user)
255307
 	throw PKCS11Exception(CKR_DEVICE_ERROR, "Applet select return 0x%04x",
255307
 								result);
255307
     }
255307
-
255307
     status = P15Applet_VerifyPIN(conn, 
255307
 		(const char *)CKYBuffer_Data(pinCachePtr->get()), 
255307
 		auth[user]->getPinInfo(), &result);
255307
@@ -4636,7 +4734,14 @@ Slot::cryptRSA(SessionHandleSuffix suffi
255307
 	    params.padInput(&inputPad, &input);
255307
             performRSAOp(&output, &inputPad, params.getKeySize(), key, 
255307
 							params.getDirection());
255307
-	    params.unpadOutput(result, &output);
255307
+	    if (CKYBuffer_Size(&output) < CKYBuffer_Size(&inputPad)) {
255307
+		/* if the size is smaller than the input, treat it as 
255307
+	         * unpadded */
255307
+		CKYBuffer_Replace(result, 0, CKYBuffer_Data(&output),
255307
+					CKYBuffer_Size(&output));
255307
+	    } else {
255307
+		params.unpadOutput(result, &output);
255307
+	    }
255307
 	    CKYBuffer_FreeData(&input);
255307
 	    CKYBuffer_FreeData(&inputPad);
255307
 	    CKYBuffer_FreeData(&output);
255307
@@ -4787,9 +4892,8 @@ retry:
255307
     } else if (state & CAC_CARD) {
255307
         status = CACApplet_SignDecrypt(conn, input, output, &result);
255307
     } else if (state & P15_CARD) {
255307
-	status = P15Applet_SignDecrypt(conn, key->getKeyRef(), keySize/8,
255307
+	status = P15Applet_SignDecrypt(conn, key->getKeyRef(), (keySize/8)*2,
255307
 				CKY_DIR_ENCRYPT, input, output, &result); 
255307
-	
255307
     } else {
255307
         status = CKYApplet_ComputeECCSignature(conn, objectToKeyNum(key), 					input, NULL, output, getNonce(), &result);
255307
     }
255307
@@ -4861,8 +4965,32 @@ retry:
255307
     } else if (state & CAC_CARD) {
255307
         status = CACApplet_SignDecrypt(conn, input, output, &result);
255307
     } else if (state & P15_CARD) {
255307
-	status = P15Applet_SignDecrypt(conn, key->getKeyRef(), keySize/8,
255307
-				direction, input, output, &result);
255307
+	if (direction == CKY_DIR_DECRYPT) {
255307
+	    status = P15Applet_SignDecrypt(conn, key->getKeyRef(), 
255307
+			keySize/8, direction, input, output, &result);
255307
+	} else {
255307
+	    CKYBuffer unpadInput;
255307
+	    CKYBuffer_InitEmpty(&unpadInput);
255307
+            stripRSAPadding(&unpadInput, input); /* will throw exception 
255307
+						  * on error */
255307
+	    status = P15Applet_SignDecrypt(conn, key->getKeyRef(), keySize/8,
255307
+				direction, &unpadInput, output, &result);
255307
+            CKYBuffer_FreeData(&unpadInput);
255307
+	    /* if it didn't work, try full padded the input first */
255307
+	    if ((status != CKYSUCCESS) 
255307
+		&& (result != CKYISO_CONDITION_NOT_SATISFIED)
255307
+		&& (result != CKYISO_SECURITY_NOT_SATISFIED))  {
255307
+		status = P15Applet_SignDecrypt(conn, key->getKeyRef(), 
255307
+			keySize/8, direction, input, output, &result);
255307
+	    }
255307
+	    /* finally just lie and try to "decrypt" the buffer */
255307
+	    if ((status != CKYSUCCESS) 
255307
+		&& (result != CKYISO_CONDITION_NOT_SATISFIED) 
255307
+		&& (result != CKYISO_SECURITY_NOT_SATISFIED))  {
255307
+		status = P15Applet_SignDecrypt(conn, key->getKeyRef(),
255307
+			 keySize/8, CKY_DIR_DECRYPT, input, output, &result);
255307
+	    }
255307
+	}
255307
     } else {
255307
         status = CKYApplet_ComputeCrypt(conn, objectToKeyNum(key), 
255307
 		CKY_RSA_NO_PAD, direction, input, NULL, output, 
255307
@@ -4883,8 +5011,7 @@ retry:
255307
             throw PKCS11Exception(CKR_DATA_INVALID);
255307
 	}
255307
 	// version0 keys could be logged out in the middle by someone else,
255307
-	// reauthenticate... This code can go away when we depricate.
255307
-        // version0 applets.
255307
+	// reauthenticate... 
255307
 	if (!isVersion1Key && !loginAttempted  && 
255307
 				userPinCache(key->getUser())->isValid() &&
255307
 					(result == CKYISO_UNAUTHORIZED)) {
255307
diff -up ./src/coolkey/slot.h.cardos-5-3 ./src/coolkey/slot.h
255307
--- ./src/coolkey/slot.h.cardos-5-3	2017-03-16 17:14:02.417338760 -0700
255307
+++ ./src/coolkey/slot.h	2017-03-16 17:14:02.421338828 -0700
255307
@@ -368,6 +368,8 @@ class Slot {
255307
     CKYBuffer p15tokenInfo;
255307
     CKYBuffer p15odf;
255307
     CKYBuffer p15serialNumber;
255307
+    CKYBuffer candidateUserAuthId;
255307
+    CKYBuffer candidateContextSpecificAuthId;
255307
     //enum { RW_SESSION_HANDLE = 1, RO_SESSION_HANDLE = 2 };
255307
 
255307
 #ifdef USE_SHMEM
255307
diff -up ./src/libckyapplet/cky_applet.c.cardos-5-3 ./src/libckyapplet/cky_applet.c
255307
--- ./src/libckyapplet/cky_applet.c.cardos-5-3	2017-03-16 17:14:02.404338539 -0700
255307
+++ ./src/libckyapplet/cky_applet.c	2017-03-16 17:14:02.422338845 -0700
255307
@@ -1316,8 +1316,6 @@ P15Applet_SignDecrypt(CKYCardConnection
255307
     int appendLength = length;
255307
     int hasPad = 0;
255307
 
255307
-    /* Hack, lie and say we are always doing encipherment */
255307
-    direction = CKY_DIR_DECRYPT;
255307
     CKYBuffer_Resize(result,0);
255307
     /*
255307
      * first set the security environment
255307
@@ -1367,7 +1365,7 @@ P15Applet_SignDecrypt(CKYCardConnection
255307
     }
255307
     CKYBuffer_AppendBuffer(&tmp, data, offset, appendLength);
255307
     pso.chain = 0;
255307
-    pso.retLen = dataSize;
255307
+    pso.retLen = keySize;
255307
 
255307
     ret = CKYApplet_HandleAPDU(conn, 
255307
 		P15AppletFactory_PerformSecurityOperation, &pso, NULL,