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

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