From 78b7b260d31a12ce5a44f33226c8a257916cd564 Mon Sep 17 00:00:00 2001 From: CentOS Buildsys Date: Nov 07 2013 00:17:29 +0000 Subject: import coolkey-1.1.0-25.el7.src.rpm --- diff --git a/.coolkey.metadata b/.coolkey.metadata new file mode 100644 index 0000000..9c8cdc5 --- /dev/null +++ b/.coolkey.metadata @@ -0,0 +1 @@ +54136decf9dfd091c8b231cb77dac97db95e1866 SOURCES/coolkey-1.1.0.tar.gz diff --git a/README.md b/README.md deleted file mode 100644 index 0e7897f..0000000 --- a/README.md +++ /dev/null @@ -1,5 +0,0 @@ -The master branch has no content - -Look at the c7 branch if you are working with CentOS-7, or the c4/c5/c6 branch for CentOS-4, 5 or 6 - -If you find this file in a distro specific branch, it means that no content has been checked in yet diff --git a/SOURCES/coolkey-cac-1.patch b/SOURCES/coolkey-cac-1.patch new file mode 100644 index 0000000..ea70187 --- /dev/null +++ b/SOURCES/coolkey-cac-1.patch @@ -0,0 +1,28 @@ +diff -up ./src/coolkey/object.cpp.cac-1 ./src/coolkey/object.cpp +--- ./src/coolkey/object.cpp.cac-1 2010-06-23 04:46:35.726198827 -0700 ++++ ./src/coolkey/object.cpp 2010-06-23 04:47:28.073827862 -0700 +@@ -505,6 +505,10 @@ dataStart(const CKYByte *buf, unsigned i + unsigned char tag; + unsigned int used_length= 0; + ++ if(!buf) { ++ return NULL; ++ } ++ + tag = buf[used_length++]; + + /* blow out when we come to the end */ +diff -up ./src/coolkey/slot.cpp.cac-1 ./src/coolkey/slot.cpp +--- ./src/coolkey/slot.cpp.cac-1 2010-06-23 04:46:22.718371631 -0700 ++++ ./src/coolkey/slot.cpp 2010-06-23 04:57:04.417774402 -0700 +@@ -2192,6 +2192,10 @@ Slot::readCACCertificateFirst(CKYBuffer + if (throwException && (status != CKYSUCCESS)) { + handleConnectionError(); + } ++ ++ if(CKYBuffer_Size(cert) == 0) { ++ handleConnectionError(); ++ } + return status; + } + diff --git a/SOURCES/coolkey-cac.patch b/SOURCES/coolkey-cac.patch new file mode 100644 index 0000000..a2a9732 --- /dev/null +++ b/SOURCES/coolkey-cac.patch @@ -0,0 +1,1013 @@ +diff -up ./src/coolkey/slot.cpp.cac ./src/coolkey/slot.cpp +--- ./src/coolkey/slot.cpp.cac 2010-06-16 13:43:51.477181000 -0700 ++++ ./src/coolkey/slot.cpp 2010-06-16 13:43:51.535179000 -0700 +@@ -372,7 +372,7 @@ Slot::Slot(const char *readerName_, Log + : log(log_), readerName(NULL), personName(NULL), manufacturer(NULL), + slotInfoFound(false), context(context_), conn(NULL), state(UNKNOWN), + isVersion1Key(false), needLogin(false), fullTokenName(false), +- mCoolkey(false), ++ mCoolkey(false), mOldCAC(false), + #ifdef USE_SHMEM + shmem(readerName_), + #endif +@@ -412,6 +412,9 @@ Slot::Slot(const char *readerName_, Log + } + CKYBuffer_InitEmpty(&cardATR); + CKYBuffer_InitEmpty(&mCUID); ++ for (int i=0; i < MAX_CERT_SLOTS; i++) { ++ CKYBuffer_InitEmpty(&cardAID[i]); ++ } + } catch(PKCS11Exception &) { + if (conn) { + CKYCardConnection_Destroy(conn); +@@ -479,6 +482,9 @@ Slot::~Slot() + CKYBuffer_FreeData(&nonce); + CKYBuffer_FreeData(&cardATR); + CKYBuffer_FreeData(&mCUID); ++ for (int i=0; i < MAX_CERT_SLOTS; i++) { ++ CKYBuffer_FreeData(&cardAID[i]); ++ } + } + + template +@@ -671,15 +677,9 @@ Slot::connectToToken() + status = CKYApplet_SelectCoolKeyManager(conn, NULL); + if (status != CKYSUCCESS) { + log->log("CoolKey Select failed 0x%x\n", status); +- status = CACApplet_SelectPKI(conn, 0, NULL); ++ status = getCACAid(); + if (status != CKYSUCCESS) { +- log->log("CAC Select failed 0x%x\n", status); +- if (status == CKYSCARDERR) { +- log->log("CAC Card Failure 0x%x\n", +- CKYCardConnection_GetLastError(conn)); +- disconnect(); +- } +- return; ++ goto loser; + } + state |= CAC_CARD | APPLET_SELECTABLE | APPLET_PERSONALIZED; + /* skip the read of the cuid. We really don't need it and, +@@ -690,6 +690,15 @@ Slot::connectToToken() + needLogin = 1; + mCoolkey = 0; + return; ++ ++loser: ++ log->log("CAC Select failed 0x%x\n", status); ++ if (status == CKYSCARDERR) { ++ log->log("CAC Card Failure 0x%x\n", ++ CKYCardConnection_GetLastError(conn)); ++ disconnect(); ++ } ++ return; + } + mCoolkey = 1; + log->log("time connect: Select Applet %d ms\n", OSTimeNow() - time); +@@ -771,17 +780,111 @@ Slot::disconnect() + invalidateLogin(false); + } + ++CKYStatus ++Slot::getCACAid() ++{ ++ CKYBuffer tBuf; ++ CKYBuffer vBuf; ++ CKYSize tlen, vlen; ++ CKYOffset toffset, voffset; ++ int certSlot = 0; ++ int i,length = 0; ++ CKYStatus status; ++ ++ CKYBuffer_InitEmpty(&tBuf); ++ CKYBuffer_InitEmpty(&vBuf); ++ ++ /* clear out the card AID's */ ++ for (i=0; i < MAX_CERT_SLOTS; i++) { ++ CKYBuffer_Resize(&cardAID[i],0); ++ } ++ ++ status = CACApplet_SelectCCC(conn,NULL); ++ if (status != CKYSUCCESS) { ++ /* are we an old CAC */ ++ status = CACApplet_SelectPKI(conn, &cardAID[0], 0, NULL); ++ if (status != CKYSUCCESS) { ++ /* no, just fail */ ++ return status; ++ } ++ /* yes, fill in the old applets */ ++ mOldCAC = true; ++ for (i=1; i< MAX_CERT_SLOTS; i++) { ++ CACApplet_SelectPKI(conn, &cardAID[i], i, NULL); ++ } ++ return CKYSUCCESS; ++ } ++ /* definately not an old CAC */ ++ mOldCAC = false; ++ ++ /* read the TLV */ ++ status = CACApplet_ReadFile(conn, CAC_TAG_FILE, &tBuf, NULL); ++ if (status != CKYSUCCESS) { ++ goto done; ++ } ++ status = CACApplet_ReadFile(conn, CAC_VALUE_FILE, &vBuf, NULL); ++ if (status != CKYSUCCESS) { ++ goto done; ++ } ++ tlen = CKYBuffer_Size(&tBuf); ++ vlen = CKYBuffer_Size(&vBuf); ++ ++ for(toffset = 2, voffset=2; ++ certSlot < MAX_CERT_SLOTS && toffset < tlen && voffset < vlen ; ++ voffset += length) { ++ ++ CKYByte tag = CKYBuffer_GetChar(&tBuf, toffset); ++ length = CKYBuffer_GetChar(&tBuf, toffset+1); ++ toffset += 2; ++ if (length == 0xff) { ++ length = CKYBuffer_GetShortLE(&tBuf, toffset); ++ toffset +=2; ++ } ++ if (tag != CAC_TAG_CARDURL) { ++ continue; ++ } ++ /* CARDURL tags must be at least 10 bytes long */ ++ if (length < 10) { ++ continue; ++ } ++ /* check the app type, should be TLV_APP_PKI */ ++ if (CKYBuffer_GetChar(&vBuf, voffset+5) != CAC_TLV_APP_PKI) { ++ continue; ++ } ++ status = CKYBuffer_AppendBuffer(&cardAID[certSlot], &vBuf, voffset, 5); ++ if (status != CKYSUCCESS) { ++ goto done; ++ } ++ status = CKYBuffer_AppendBuffer(&cardAID[certSlot], &vBuf, ++ voffset+8, 2); ++ if (status != CKYSUCCESS) { ++ goto done; ++ } ++ cardEF[certSlot] = CKYBuffer_GetShortLE(&vBuf, voffset+6); ++ ++ certSlot++; ++ } ++ status = CKYSUCCESS; ++ if (certSlot == 0) { ++ status = CKYAPDUFAIL; /* probably neeed a beter error code */ ++ } ++ ++done: ++ CKYBuffer_FreeData(&tBuf); ++ CKYBuffer_FreeData(&vBuf); ++ return status; ++} ++ + void + Slot::refreshTokenState() + { + if( cardStateMayHaveChanged() ) { +-log->log("card changed\n"); ++ log->log("card changed\n"); + invalidateLogin(true); + closeAllSessions(); + unloadObjects(); + connectToToken(); + +- + if( state & APPLET_PERSONALIZED ) { + try { + loadObjects(); +@@ -1019,7 +1122,7 @@ Slot::makeModelString(char *model, int m + + struct _manList { + unsigned short type; +- char *string; ++ const char *string; + }; + + static const struct _manList manList[] = { +@@ -1280,13 +1383,30 @@ void + Slot::selectCACApplet(CKYByte instance) + { + CKYStatus status; +- status = CACApplet_SelectPKI(conn, instance, NULL); ++ CKYBuffer *aid = &cardAID[instance]; ++ ++ if (CKYBuffer_Size(aid) == 0) { ++ disconnect(); ++ throw PKCS11Exception(CKR_DEVICE_REMOVED); ++ return; ++ } ++ ++ status = CKYApplet_SelectFile(conn, aid, NULL); + if ( status == CKYSCARDERR ) handleConnectionError(); + if ( status != CKYSUCCESS) { + // could not select applet: this just means it's not there + disconnect(); + throw PKCS11Exception(CKR_DEVICE_REMOVED); + } ++ if (mOldCAC) { ++ return; ++ } ++ status = CACApplet_SelectFile(conn, cardEF[instance], NULL); ++ if ( status == CKYSCARDERR ) handleConnectionError(); ++ if ( status != CKYSUCCESS) { ++ disconnect(); ++ throw PKCS11Exception(CKR_DEVICE_REMOVED); ++ } + } + // assume we are already in a transaction + void +@@ -2059,10 +2179,85 @@ Slot::fetchCombinedObjects(const CKYBuff + return objInfoList; + } + ++CKYStatus ++Slot::readCACCertificateFirst(CKYBuffer *cert, CKYSize *nextSize, ++ bool throwException) ++{ ++ CKYStatus status; ++ CKYISOStatus apduRC; ++ ++ if (mOldCAC) { ++ /* get the first 100 bytes of the cert */ ++ status = CACApplet_GetCertificateFirst(conn, cert, nextSize, &apduRC); ++ if (throwException && (status != CKYSUCCESS)) { ++ handleConnectionError(); ++ } ++ return status; ++ } ++ ++ CKYBuffer tBuf; ++ CKYBuffer vBuf; ++ CKYSize tlen, vlen; ++ CKYOffset toffset, voffset; ++ int length = 0; ++ ++ CKYBuffer_InitEmpty(&tBuf); ++ CKYBuffer_InitEmpty(&vBuf); ++ CKYBuffer_Resize(cert, 0); ++ ++ /* handle the new CAC card read */ ++ /* read the TLV */ ++ status = CACApplet_ReadFile(conn, CAC_TAG_FILE, &tBuf, NULL); ++ if (status != CKYSUCCESS) { ++ goto done; ++ } ++ status = CACApplet_ReadFile(conn, CAC_VALUE_FILE, &vBuf, NULL); ++ if (status != CKYSUCCESS) { ++ goto done; ++ } ++ tlen = CKYBuffer_Size(&tBuf); ++ vlen = CKYBuffer_Size(&vBuf); ++ ++ /* look for the Cert out of the TLV */ ++ for(toffset = 2, voffset=2; toffset < tlen && voffset < vlen ; ++ voffset += length) { ++ ++ CKYByte tag = CKYBuffer_GetChar(&tBuf, toffset); ++ length = CKYBuffer_GetChar(&tBuf, toffset+1); ++ toffset += 2; ++ if (length == 0xff) { ++ length = CKYBuffer_GetShortLE(&tBuf, toffset); ++ toffset +=2; ++ } ++ if (tag != CAC_TAG_CERTIFICATE) { ++ continue; ++ } ++ CKYBuffer_AppendBuffer(cert, &vBuf, voffset, length); ++ break; ++ } ++ status = CKYSUCCESS; ++ ++done: ++ CKYBuffer_FreeData(&tBuf); ++ CKYBuffer_FreeData(&vBuf); ++ return status; ++} ++ ++/* ++ * only necessary for old CAC cards. New CAC cards have to read the ++ * whole cert in anyway above.... ++ */ ++CKYStatus ++Slot::readCACCertificateAppend(CKYBuffer *cert, CKYSize nextSize) ++{ ++ CKYISOStatus apduRC; ++ assert(mOldCAC); ++ return CACApplet_GetCertificateAppend(conn, cert, nextSize, &apduRC); ++} ++ + void + Slot::loadCACCert(CKYByte instance) + { +- CKYISOStatus apduRC; + CKYStatus status = CKYSUCCESS; + CKYBuffer cert; + CKYBuffer rawCert; +@@ -2097,12 +2292,7 @@ Slot::loadCACCert(CKYByte instance) + instance, OSTimeNow() - time); + + if (instance == 0) { +- /* get the first 100 bytes of the cert */ +- status = CACApplet_GetCertificateFirst(conn, &rawCert, +- &nextSize, &apduRC); +- if (status != CKYSUCCESS) { +- handleConnectionError(); +- } ++ readCACCertificateFirst(&rawCert, &nextSize, true); + log->log("CAC Cert %d: fetch CAC Cert: %d ms\n", + instance, OSTimeNow() - time); + } +@@ -2143,8 +2333,7 @@ Slot::loadCACCert(CKYByte instance) + shmem.setVersion(SHMEM_VERSION); + shmem.setDataVersion(dataVersion); + } else { +- status = CACApplet_GetCertificateFirst(conn, &rawCert, +- &nextSize, &apduRC); ++ status = readCACCertificateFirst(&rawCert, &nextSize, false); + + if (status != CKYSUCCESS) { + /* CAC only requires the Certificate in pki '0' */ +@@ -2159,8 +2348,7 @@ Slot::loadCACCert(CKYByte instance) + } + + if (nextSize) { +- status = CACApplet_GetCertificateAppend(conn, &rawCert, +- nextSize, &apduRC); ++ status = readCACCertificateAppend(&rawCert, nextSize); + } + log->log("CAC Cert %d: Fetch rest : %d ms\n", + instance, OSTimeNow() - time); +@@ -2176,9 +2364,10 @@ Slot::loadCACCert(CKYByte instance) + + log->log("CAC Cert %d: Cert has been read: %d ms\n", + instance, OSTimeNow() - time); +- if (CKYBuffer_GetChar(&rawCert,0) == 1) { ++ if (!mOldCAC || CKYBuffer_GetChar(&rawCert,0) == 1) { + CKYSize guessFinalSize = CKYBuffer_Size(&rawCert); + CKYSize certSize = 0; ++ CKYOffset offset = mOldCAC ? 1 : 0; + int zret = Z_MEM_ERROR; + + do { +@@ -2189,7 +2378,8 @@ Slot::loadCACCert(CKYByte instance) + } + certSize = guessFinalSize; + zret = uncompress((Bytef *)CKYBuffer_Data(&cert),&certSize, +- CKYBuffer_Data(&rawCert)+1, CKYBuffer_Size(&rawCert)-1); ++ CKYBuffer_Data(&rawCert)+offset, ++ CKYBuffer_Size(&rawCert)-offset); + } while (zret == Z_BUF_ERROR); + + if (zret != Z_OK) { +@@ -2526,7 +2716,7 @@ Slot::attemptCACLogin() + switch( result ) { + case CKYISO_SUCCESS: + break; +- case 6981: ++ case 0x6981: + throw PKCS11Exception(CKR_PIN_LOCKED); + default: + if ((result & 0xff00) == 0x6300) { +diff -up ./src/coolkey/slot.h.cac ./src/coolkey/slot.h +--- ./src/coolkey/slot.h.cac 2010-06-16 13:43:51.344185000 -0700 ++++ ./src/coolkey/slot.h 2010-06-16 13:43:51.546179000 -0700 +@@ -294,6 +294,7 @@ class CryptParams { + const CKYBuffer *paddedOutput) const = 0; + }; + ++#define MAX_CERT_SLOTS 3 + class Slot { + + public: +@@ -328,6 +329,8 @@ class Slot { + CKYBuffer nonce; + CKYBuffer cardATR; + CKYBuffer mCUID; ++ CKYBuffer cardAID[MAX_CERT_SLOTS]; ++ unsigned short cardEF[MAX_CERT_SLOTS]; + bool isVersion1Key; + bool needLogin; + long publicFree; +@@ -335,6 +338,7 @@ class Slot { + long privateFree; + bool fullTokenName; + bool mCoolkey; ++ bool mOldCAC; + + //enum { RW_SESSION_HANDLE = 1, RO_SESSION_HANDLE = 2 }; + +@@ -398,6 +402,11 @@ class Slot { + list fetchCombinedObjects(const CKYBuffer *header); + list fetchSeparateObjects(); + ++ CKYStatus getCACAid(); ++ CKYStatus readCACCertificateFirst(CKYBuffer *cert, CKYSize *nextSize, ++ bool throwException); ++ CKYStatus readCACCertificateAppend(CKYBuffer *cert, CKYSize nextSize); ++ + void selectApplet(); + void selectCACApplet(CKYByte instance); + void unloadObjects(); +diff -up ./src/libckyapplet/cky_applet.c.cac ./src/libckyapplet/cky_applet.c +--- ./src/libckyapplet/cky_applet.c.cac 2010-06-16 13:43:51.357181000 -0700 ++++ ./src/libckyapplet/cky_applet.c 2010-06-16 14:47:41.305529000 -0700 +@@ -41,7 +41,13 @@ + CKYStatus + CKYAppletFactory_SelectFile(CKYAPDU *apdu, const void *param) + { +- return CKYAPDUFactory_SelectFile(apdu,(const CKYBuffer *)param); ++ return CKYAPDUFactory_SelectFile(apdu, 4, 0, (const CKYBuffer *)param); ++} ++ ++CKYStatus ++CACAppletFactory_SelectFile(CKYAPDU *apdu, const void *param) ++{ ++ return CKYAPDUFactory_SelectFile(apdu, 2, 12, (const CKYBuffer *)param); + } + + CKYStatus +@@ -225,10 +231,17 @@ CKYAppletFactory_GetBuiltinACL(CKYAPDU * + } + + CKYStatus +-CACAppletFactory_SignDecrypt(CKYAPDU *apdu, const void *param) ++CACAppletFactory_SignDecryptStep(CKYAPDU *apdu, const void *param) ++{ ++ const CKYBuffer *buf=(CKYBuffer *)param; ++ return CACAPDUFactory_SignDecrypt(apdu, CAC_P1_STEP, buf); ++} ++ ++CKYStatus ++CACAppletFactory_SignDecryptFinal(CKYAPDU *apdu, const void *param) + { + const CKYBuffer *buf=(CKYBuffer *)param; +- return CACAPDUFactory_SignDecrypt(apdu, buf); ++ return CACAPDUFactory_SignDecrypt(apdu, CAC_P1_FINAL, buf); + } + + CKYStatus +@@ -246,6 +259,13 @@ CACAppletFactory_GetCertificate(CKYAPDU + } + + CKYStatus ++CACAppletFactory_ReadFile(CKYAPDU *apdu, const void *param) ++{ ++ const CACAppletArgReadFile *rfs = (const CACAppletArgReadFile *)param; ++ return CACAPDUFactory_ReadFile(apdu, rfs->offset, rfs->type, rfs->count); ++} ++ ++CKYStatus + CACAppletFactory_GetProperties(CKYAPDU *apdu, const void *param) + { + return CACAPDUFactory_GetProperties(apdu); +@@ -457,7 +477,7 @@ CKYApplet_SelectFile(CKYCardConnection * + CKYISOStatus *apduRC) + { + return CKYApplet_HandleAPDU(conn, CKYAppletFactory_SelectFile, AID, NULL, +- 0, CKYAppletFill_Null, NULL, apduRC); ++ CKY_SIZE_UNKNOWN, CKYAppletFill_Null, NULL, apduRC); + } + + static CKYByte coolkeyid[] = {0x62, 0x76, 0x01, 0xff, 0x00, 0x00, 0x00 }; +@@ -477,22 +497,23 @@ CKYApplet_SelectCoolKeyManager(CKYCardCo + return ret; + } + +-static CKYByte CACPKIid[] = {0xa0, 0x00, 0x00, 0x00, 0x79, 0x01, 0x00 }; ++static CKYByte CACPKIid[] = { 0xa0, 0x00, 0x00, 0x00, 0x79, 0x01 }; + /* + * Select the CoolKey applet. Must happen after we start a transaction and + * before we issue any applet specific command. + */ + CKYStatus +-CACApplet_SelectPKI(CKYCardConnection *conn, CKYByte instance, +- CKYISOStatus *apduRC) ++CACApplet_SelectPKI(CKYCardConnection *conn, CKYBuffer *cacAID, ++ CKYByte instance, CKYISOStatus *apduRC) + { + CKYStatus ret; +- CKYBuffer CACPKIAID; +- CKYBuffer_InitFromData(&CACPKIAID, CACPKIid, sizeof(CACPKIid)); +- CKYBuffer_SetChar(&CACPKIAID, 6, instance); +- ret = CKYApplet_HandleAPDU(conn, CKYAppletFactory_SelectFile, &CACPKIAID, ++ CKYBuffer_AppendData(cacAID, CACPKIid, sizeof(CACPKIid)); ++ CKYBuffer_AppendChar(cacAID, instance); ++ ret = CKYApplet_HandleAPDU(conn, CKYAppletFactory_SelectFile, cacAID, + NULL, CKY_SIZE_UNKNOWN, CKYAppletFill_Null, NULL, apduRC); +- CKYBuffer_FreeData(&CACPKIAID); ++ if (ret != CKYSUCCESS) { ++ CKYBuffer_Resize(cacAID, 0); ++ } + return ret; + } + +@@ -515,11 +536,38 @@ CACApplet_SelectCardManager(CKYCardConne + CKYBuffer CAC_CM_AID; + CKYBuffer_InitFromData(&CAC_CM_AID, cacmgrid, sizeof(cacmgrid)); + ret = CKYApplet_HandleAPDU(conn, CKYAppletFactory_SelectFile, &CAC_CM_AID, +- NULL, 0, CKYAppletFill_Null, NULL, apduRC); ++ NULL, CKY_SIZE_UNKNOWN, CKYAppletFill_Null, NULL, apduRC); + CKYBuffer_FreeData(&CAC_CM_AID); + return ret; + } + ++static CKYByte cacCCCid[] = {0xa0, 0x00, 0x00, 0x01, 0x16, 0xdb, 0x00 }; ++CKYStatus ++CACApplet_SelectCCC(CKYCardConnection *conn, CKYISOStatus *apduRC) ++{ ++ CKYStatus ret; ++ CKYBuffer CAC_CM_AID; ++ CKYBuffer_InitFromData(&CAC_CM_AID, cacCCCid, sizeof(cacCCCid)); ++ ret = CKYApplet_HandleAPDU(conn, CKYAppletFactory_SelectFile, &CAC_CM_AID, ++ NULL, CKY_SIZE_UNKNOWN, CKYAppletFill_Null, NULL, apduRC); ++ CKYBuffer_FreeData(&CAC_CM_AID); ++ return ret; ++} ++ ++CKYStatus ++CACApplet_SelectFile(CKYCardConnection *conn, unsigned short ef, ++ CKYISOStatus *apduRC) ++{ ++ CKYStatus ret; ++ CKYBuffer efBuf; ++ CKYBuffer_InitEmpty(&efBuf); ++ CKYBuffer_AppendShortLE(&efBuf, ef); ++ ret = CKYApplet_HandleAPDU(conn, CACAppletFactory_SelectFile, &efBuf, ++ NULL, CKY_SIZE_UNKNOWN, CKYAppletFill_Null, NULL, apduRC); ++ CKYBuffer_FreeData(&efBuf); ++ return ret; ++} ++ + /* + * GetCPLC cluster -- must be called with CM selected + */ +@@ -673,8 +721,8 @@ CKYApplet_ComputeCryptProcess(CKYCardCon + ccd.keyNumber = keyNumber; + ccd.location = location; + ccd.data = data; +- return CKYApplet_HandleAPDU(conn, CKYAppletFactory_ComputeCryptProcess, &ccd, +- nonce, 0, CKYAppletFill_Null, NULL, apduRC); ++ return CKYApplet_HandleAPDU(conn, CKYAppletFactory_ComputeCryptProcess, ++ &ccd, nonce, 0, CKYAppletFill_Null, NULL, apduRC); + } + + /* computeCrypt returns data in the form : +@@ -832,11 +880,39 @@ CACApplet_SignDecrypt(CKYCardConnection + CKYBuffer *result, CKYISOStatus *apduRC) + { + CKYStatus ret; +- +- ret = CKYApplet_HandleAPDU(conn, +- CACAppletFactory_SignDecrypt, data, NULL, +- CKYBuffer_Size(data), CKYAppletFill_ReplaceBuffer, ++ CKYSize dataSize = CKYBuffer_Size(data); ++ CKYOffset offset = 0; ++ CKYBuffer tmp; ++ ++ CKYBuffer_InitEmpty(&tmp); ++ ++ CKYBuffer_Resize(result, 0); ++ for(offset = 0; (dataSize-offset) > CKY_MAX_WRITE_CHUNK_SIZE; ++ offset += CKY_MAX_WRITE_CHUNK_SIZE) { ++ CKYBuffer_Resize(&tmp,0); ++ CKYBuffer_AppendBuffer(&tmp, data, offset, CKY_MAX_WRITE_CHUNK_SIZE); ++ ret = CKYApplet_HandleAPDU(conn, CACAppletFactory_SignDecryptStep, ++ &tmp, NULL, CKY_SIZE_UNKNOWN, ++ CKYAppletFill_AppendBuffer, + result, apduRC); ++ if (ret != CKYSUCCESS) { ++ goto done; ++ } ++ } ++ CKYBuffer_Resize(&tmp,0); ++ CKYBuffer_AppendBuffer(&tmp, data, offset, dataSize - offset); ++ ret = CKYApplet_HandleAPDU(conn, CACAppletFactory_SignDecryptFinal, ++ &tmp, NULL, CKY_SIZE_UNKNOWN, ++ CKYAppletFill_AppendBuffer, ++ result, apduRC); ++ ++ if ((ret == CKYSUCCESS) && (CKYBuffer_Size(result) != dataSize)) { ++ /* RSA returns the same data size as input, didn't happen, so ++ * something is wrong. */ ++ } ++ ++done: ++ CKYBuffer_FreeData(&tmp); + return ret; + } + +@@ -895,6 +971,63 @@ CACApplet_GetCertificate(CKYCardConnecti + } + return ret; + } ++ ++/* ++ * Read a CAC Tag/Value file ++ */ ++CKYStatus ++CACApplet_ReadFile(CKYCardConnection *conn, CKYByte type, CKYBuffer *buffer, ++ CKYISOStatus *apduRC) ++{ ++ CKYStatus ret; ++ CKYISOStatus status; ++ CKYByte maxtransfer; ++ unsigned short offset = 0; ++ unsigned short size; ++ CACAppletArgReadFile rfs; ++ ++ CKYBuffer_Resize(buffer,0); ++ if (apduRC == NULL) { ++ apduRC = &status; ++ } ++ rfs.offset = 0; ++ rfs.count = 2; ++ rfs.type = type; ++ ++ /* APDU's are expensive, Grab a big chunk of the file first if possible */ ++ ret = CKYApplet_HandleAPDU(conn, ++ CACAppletFactory_ReadFile, &rfs, NULL, ++ rfs.count, CKYAppletFill_AppendBuffer, ++ buffer, apduRC); ++ /* file is probably smaller than 100 bytes, get the actual size first */ ++ if (ret != CKYSUCCESS) { ++ return ret; ++ } ++ size = CKYBuffer_GetShortLE(buffer, 0) + 2 /* include the length itself */; ++ maxtransfer = CKY_MAX_READ_CHUNK_SIZE; ++ /* get the rest of the buffer if necessary */ ++ for (offset = CKYBuffer_Size(buffer); size > offset; ++ offset = CKYBuffer_Size(buffer)) { ++ rfs.offset = offset; ++ rfs.count = MIN(size - offset, maxtransfer); ++ ret = CKYApplet_HandleAPDU(conn, ++ CACAppletFactory_ReadFile, &rfs, NULL, ++ rfs.count, CKYAppletFill_AppendBuffer, ++ buffer, apduRC); ++ if (ret != CKYSUCCESS) { ++ if (*apduRC == CAC_INVALID_PARAMS) { ++ maxtransfer = maxtransfer/2; ++ if (maxtransfer == 0) { ++ return ret; ++ } ++ } else { ++ return ret; ++ } ++ } ++ } ++ return ret; ++} ++ + CKYStatus + CACApplet_GetCertificateFirst(CKYCardConnection *conn, CKYBuffer *cert, + CKYSize *nextSize, CKYISOStatus *apduRC) +diff -up ./src/libckyapplet/cky_applet.h.cac ./src/libckyapplet/cky_applet.h +--- ./src/libckyapplet/cky_applet.h.cac 2010-06-16 13:43:51.370181000 -0700 ++++ ./src/libckyapplet/cky_applet.h 2010-06-16 13:43:51.572179000 -0700 +@@ -71,6 +71,15 @@ typedef unsigned short CKYISOStatus; /* + #define CKYISO_INTERNAL_ERROR 0x9cff /* Reserved for debugging, + * shouldn't happen */ + ++#define CAC_INVALID_PARAMS 0x6a83 ++#define CAC_TAG_FILE 1 ++#define CAC_VALUE_FILE 2 ++ ++ ++#define CAC_TAG_CARDURL 0xf3 ++#define CAC_TAG_CERTIFICATE 0x70 ++#define CAC_TLV_APP_PKI 0x04 ++ + /* + * Pin Constants as used by our applet + */ +@@ -209,6 +218,12 @@ typedef struct _CKYAppletArgComputeCrypt + const CKYBuffer *sig; + } CKYAppletArgComputeCrypt; + ++typedef struct _CACAppletArgReadFile { ++ CKYByte type; ++ CKYByte count; ++ unsigned short offset; ++} CACAppletArgReadFile; ++ + /* fills in an APDU from a structure -- form of all the generic factories*/ + typedef CKYStatus (*CKYAppletFactory)(CKYAPDU *apdu, const void *param); + /* fills in an a structure from a response -- form of all the fill structures*/ +@@ -451,9 +466,17 @@ CKYStatus CKYApplet_DeleteObject(CKYCard + /* Select the CAC card manager. Can happen with either applet selected */ + CKYStatus CACApplet_SelectCardManager(CKYCardConnection *conn, + CKYISOStatus *apduRC); +-/* Can happen with either applet selected */ +-CKYStatus CACApplet_SelectPKI(CKYCardConnection *conn, CKYByte instance, +- CKYISOStatus *apduRC); ++/* Select the CAC CC container. Can happen with either applet selected */ ++CKYStatus CACApplet_SelectCCC(CKYCardConnection *conn, CKYISOStatus *apduRC); ++/* Select an old CAC applet and fill in the cardAID */ ++CKYStatus CACApplet_SelectPKI(CKYCardConnection *conn, CKYBuffer *cardAid, ++ CKYByte instance, CKYISOStatus *apduRC); ++/* read a TLV file */ ++CKYStatus CACApplet_ReadFile(CKYCardConnection *conn, CKYByte type, ++ CKYBuffer *buffer, CKYISOStatus *apduRC); ++CKYStatus CACApplet_SelectFile(CKYCardConnection *conn, unsigned short ef, ++ CKYISOStatus *apduRC); ++ + /* must happen with PKI applet selected */ + CKYStatus CACApplet_SignDecrypt(CKYCardConnection *conn, const CKYBuffer *data, + CKYBuffer *result, CKYISOStatus *apduRC); +diff -up ./src/libckyapplet/cky_base.c.cac ./src/libckyapplet/cky_base.c +--- ./src/libckyapplet/cky_base.c.cac 2006-06-09 11:44:17.000000000 -0700 ++++ ./src/libckyapplet/cky_base.c 2010-06-16 13:43:51.583179000 -0700 +@@ -220,6 +220,22 @@ CKYBuffer_AppendShort(CKYBuffer *buf, un + return CKYSUCCESS; + } + ++/* append a short in network order */ ++CKYStatus ++CKYBuffer_AppendShortLE(CKYBuffer *buf, unsigned short val) ++{ ++ CKYStatus ret; ++ ++ ret = CKYBuffer_Reserve(buf, buf->len + 2); ++ if (ret != CKYSUCCESS) { ++ return ret; ++ } ++ buf->data[buf->len+1] = (CKYByte) ((val >> 8) & 0xff); ++ buf->data[buf->len+0] = (CKYByte) ((val >> 0) & 0xff); ++ buf->len += 2; ++ return CKYSUCCESS; ++} ++ + /* append a long in applet order */ + CKYStatus + CKYBuffer_AppendLong(CKYBuffer *buf, unsigned long val) +@@ -238,6 +254,24 @@ CKYBuffer_AppendLong(CKYBuffer *buf, uns + return CKYSUCCESS; + } + ++/* append a long in applet order */ ++CKYStatus ++CKYBuffer_AppendLongLE(CKYBuffer *buf, unsigned long val) ++{ ++ CKYStatus ret; ++ ++ ret = CKYBuffer_Reserve(buf, buf->len + 4); ++ if (ret != CKYSUCCESS) { ++ return ret; ++ } ++ buf->data[buf->len+3] = (CKYByte) ((val >> 24) & 0xff); ++ buf->data[buf->len+2] = (CKYByte) ((val >> 16) & 0xff); ++ buf->data[buf->len+1] = (CKYByte) ((val >> 8) & 0xff); ++ buf->data[buf->len+0] = (CKYByte) ((val >> 0) & 0xff); ++ buf->len += 4; ++ return CKYSUCCESS; ++} ++ + CKYStatus + CKYBuffer_Replace(CKYBuffer *buf, CKYOffset offset, const CKYByte *data, CKYSize len) + { +@@ -351,6 +385,22 @@ CKYBuffer_SetShort(CKYBuffer *buf, CKYOf + } + + CKYStatus ++CKYBuffer_SetShortLE(CKYBuffer *buf, CKYOffset offset, unsigned short val) ++{ ++ CKYStatus ret; ++ ++ if (buf->len < offset+2) { ++ ret = CKYBuffer_Resize(buf,offset+2); ++ if (ret != CKYSUCCESS) { ++ return ret; ++ } ++ } ++ buf->data[offset+1] = (CKYByte) ((val >> 8) & 0xff); ++ buf->data[offset+0] = (CKYByte) ((val >> 0) & 0xff); ++ return CKYSUCCESS; ++} ++ ++CKYStatus + CKYBuffer_SetLong(CKYBuffer *buf, CKYOffset offset, unsigned long val) + { + CKYStatus ret; +@@ -368,6 +418,24 @@ CKYBuffer_SetLong(CKYBuffer *buf, CKYOff + return CKYSUCCESS; + } + ++CKYStatus ++CKYBuffer_SetLongLE(CKYBuffer *buf, CKYOffset offset, unsigned long val) ++{ ++ CKYStatus ret; ++ ++ if (buf->len < offset+4) { ++ ret = CKYBuffer_Resize(buf,offset+4); ++ if (ret != CKYSUCCESS) { ++ return ret; ++ } ++ } ++ buf->data[offset+3] = (CKYByte) ((val >> 24) & 0xff); ++ buf->data[offset+2] = (CKYByte) ((val >> 16) & 0xff); ++ buf->data[offset+1] = (CKYByte) ((val >> 8) & 0xff); ++ buf->data[offset+0] = (CKYByte) ((val >> 0) & 0xff); ++ return CKYSUCCESS; ++} ++ + CKYByte + CKYBuffer_GetChar(const CKYBuffer *buf, CKYOffset offset) + { +@@ -388,6 +456,18 @@ CKYBuffer_GetShort(const CKYBuffer *buf, + val |= ((unsigned short)buf->data[offset+1]) << 0; + return val; + } ++ ++unsigned short ++CKYBuffer_GetShortLE(const CKYBuffer *buf, CKYOffset offset) ++{ ++ unsigned short val; ++ if (buf->len < offset+2) { ++ return 0; ++ } ++ val = ((unsigned short)buf->data[offset+1]) << 8; ++ val |= ((unsigned short)buf->data[offset+0]) << 0; ++ return val; ++} + + unsigned long + CKYBuffer_GetLong(const CKYBuffer *buf, CKYOffset offset) +@@ -402,6 +482,20 @@ CKYBuffer_GetLong(const CKYBuffer *buf, + val |= ((unsigned long)buf->data[offset+3]) << 0; + return val; + } ++ ++unsigned long ++CKYBuffer_GetLongLE(const CKYBuffer *buf, CKYOffset offset) ++{ ++ unsigned long val; ++ if (buf->len < offset+4) { ++ return 0; ++ } ++ val = ((unsigned long)buf->data[offset+3]) << 24; ++ val |= ((unsigned long)buf->data[offset+2]) << 16; ++ val |= ((unsigned long)buf->data[offset+1]) << 8; ++ val |= ((unsigned long)buf->data[offset+0]) << 0; ++ return val; ++} + + CKYStatus + CKYBuffer_Resize(CKYBuffer *buf, CKYSize newLen) +diff -up ./src/libckyapplet/cky_base.h.cac ./src/libckyapplet/cky_base.h +--- ./src/libckyapplet/cky_base.h.cac 2006-06-09 11:44:17.000000000 -0700 ++++ ./src/libckyapplet/cky_base.h 2010-06-16 13:43:51.592179000 -0700 +@@ -170,9 +170,15 @@ CKYStatus CKYBuffer_AppendChar(CKYBuffer + /* append a short in applet order */ + CKYStatus CKYBuffer_AppendShort(CKYBuffer *buf, unsigned short val); + ++/* append a short in little endian order */ ++CKYStatus CKYBuffer_AppendShortLE(CKYBuffer *buf, unsigned short val); ++ + /* append a long in applet order */ + CKYStatus CKYBuffer_AppendLong(CKYBuffer *buf, unsigned long val); + ++/* append a long in little endian order */ ++CKYStatus CKYBuffer_AppendLongLE(CKYBuffer *buf, unsigned long val); ++ + /* append data. the data starts at data and extends len bytes */ + CKYStatus CKYBuffer_AppendData(CKYBuffer *buf, const CKYByte *data, CKYSize len); + +@@ -210,12 +216,18 @@ CKYStatus CKYBuffer_SetChars(CKYBuffer * + CKYStatus CKYBuffer_SetShort(CKYBuffer *buf, CKYOffset offset, unsigned short val); + CKYStatus CKYBuffer_SetLong(CKYBuffer *buf, CKYOffset offset, unsigned long val); + ++/* These functions work in little endian order */ ++CKYStatus CKYBuffer_SetShortLE(CKYBuffer *buf, CKYOffset offset, unsigned short val); ++CKYStatus CKYBuffer_SetLongLE(CKYBuffer *buf, CKYOffset offset, unsigned long val); + /* read a character from offset. If offset is beyond the end of the buffer, + * then the function returns '0' */ + CKYByte CKYBuffer_GetChar(const CKYBuffer *buf, CKYOffset offset); + /* These functions work in applet order */ + unsigned short CKYBuffer_GetShort(const CKYBuffer *buf, CKYOffset offset); + unsigned long CKYBuffer_GetLong(const CKYBuffer *buf, CKYOffset offset); ++/* These functions work in little endian order */ ++unsigned short CKYBuffer_GetShortLE(const CKYBuffer *buf, CKYOffset offset); ++unsigned long CKYBuffer_GetLongLE(const CKYBuffer *buf, CKYOffset offset); + + /* clear out all the data in a buffer */ + void CKYBuffer_Zero(CKYBuffer *buf); +diff -up ./src/libckyapplet/cky_factory.c.cac ./src/libckyapplet/cky_factory.c +--- ./src/libckyapplet/cky_factory.c.cac 2010-06-16 13:43:51.393185000 -0700 ++++ ./src/libckyapplet/cky_factory.c 2010-06-16 14:48:08.885473000 -0700 +@@ -25,12 +25,13 @@ + * special commands can be issued at any time + */ + CKYStatus +-CKYAPDUFactory_SelectFile(CKYAPDU *apdu, const CKYBuffer *AID) ++CKYAPDUFactory_SelectFile(CKYAPDU *apdu, CKYByte p1, CKYByte p2, ++ const CKYBuffer *AID) + { + CKYAPDU_SetCLA(apdu, CKY_CLASS_ISO7816); + CKYAPDU_SetINS(apdu, CKY_INS_SELECT_FILE); +- CKYAPDU_SetP1(apdu, 0x04); +- CKYAPDU_SetP2(apdu, 0x00); ++ CKYAPDU_SetP1(apdu, p1); ++ CKYAPDU_SetP2(apdu, p2); + return CKYAPDU_SetSendDataBuffer(apdu, AID); + } + +@@ -131,6 +132,7 @@ fail: + return ret; + } + ++ + CKYStatus + CKYAPDUFactory_ComputeCryptFinal(CKYAPDU *apdu, CKYByte keyNumber, + CKYByte location, const CKYBuffer *data, const CKYBuffer *sig) +@@ -572,11 +574,11 @@ CKYAPDUFactory_GetBuiltinACL(CKYAPDU *ap + } + + CKYStatus +-CACAPDUFactory_SignDecrypt(CKYAPDU *apdu, const CKYBuffer *data) ++CACAPDUFactory_SignDecrypt(CKYAPDU *apdu, CKYByte type, const CKYBuffer *data) + { + CKYAPDU_SetCLA(apdu, CKY_CLASS_ISO7816); + CKYAPDU_SetINS(apdu, CAC_INS_SIGN_DECRYPT); +- CKYAPDU_SetP1(apdu, 0x00); ++ CKYAPDU_SetP1(apdu, type); + CKYAPDU_SetP2(apdu, 0x00); + return CKYAPDU_SetSendDataBuffer(apdu, data); + } +@@ -592,6 +594,36 @@ CACAPDUFactory_GetCertificate(CKYAPDU *a + } + + CKYStatus ++CACAPDUFactory_ReadFile(CKYAPDU *apdu, unsigned short offset, ++ CKYByte type, CKYByte count) ++{ ++ CKYStatus ret; ++ CKYBuffer buf; ++ ++ CKYBuffer_InitEmpty(&buf); ++ CKYAPDU_SetCLA(apdu, CKY_CLASS_GLOBAL_PLATFORM); ++ CKYAPDU_SetINS(apdu, CAC_INS_READ_FILE); ++ CKYAPDU_SetP1(apdu, (offset >> 8) & 0xff); ++ CKYAPDU_SetP2(apdu, offset & 0xff); ++ ret = CKYBuffer_Reserve(&buf, 2); ++ if (ret != CKYSUCCESS) { ++ goto fail; ++ } ++ ret = CKYBuffer_AppendChar(&buf, type); ++ if (ret != CKYSUCCESS) { ++ goto fail; ++ } ++ ret = CKYBuffer_AppendChar(&buf, count); ++ if (ret != CKYSUCCESS) { ++ goto fail; ++ } ++ ret = CKYAPDU_SetSendDataBuffer(apdu, &buf); ++fail: ++ CKYBuffer_FreeData(&buf); ++ return ret; ++} ++ ++CKYStatus + CACAPDUFactory_GetProperties(CKYAPDU *apdu) + { + CKYAPDU_SetCLA(apdu, CKY_CLASS_ISO7816); +diff -up ./src/libckyapplet/cky_factory.h.cac ./src/libckyapplet/cky_factory.h +--- ./src/libckyapplet/cky_factory.h.cac 2010-06-16 13:43:51.402181000 -0700 ++++ ./src/libckyapplet/cky_factory.h 2010-06-16 14:43:20.867049000 -0700 +@@ -86,7 +86,11 @@ + #define CAC_INS_SIGN_DECRYPT 0x42 + #define CAC_INS_VERIFY_PIN 0x20 + #define CAC_INS_GET_PROPERTIES 0x56 ++#define CAC_INS_READ_FILE 0x52 ++ + #define CAC_SIZE_GET_PROPERTIES 48 ++#define CAC_P1_STEP 0x80 ++#define CAC_P1_FINAL 0x00 + + /* + * Fixed return sized from various commands +@@ -169,7 +173,8 @@ + CKY_BEGIN_PROTOS + + /* function based factorys */ +-CKYStatus CKYAPDUFactory_SelectFile(CKYAPDU *apdu, const CKYBuffer *AID); ++CKYStatus CKYAPDUFactory_SelectFile(CKYAPDU *apdu, CKYByte p1, CKYByte p2, ++ const CKYBuffer *AID); + CKYStatus CKYAPDUFactory_SelectCardManager(CKYAPDU *apdu); + CKYStatus CKYAPDUFactory_GetCPLCData(CKYAPDU *apdu); + CKYStatus CKYAPDUFactory_ListKeys(CKYAPDU *apdu, CKYByte sequence); +@@ -211,9 +216,12 @@ CKYStatus CKYAPDUFactory_SeedRandom(CKYA + CKYStatus CKYAPDUFactory_GetIssuerInfo(CKYAPDU *apdu); + CKYStatus CKYAPDUFactory_GetBuiltinACL(CKYAPDU *apdu); + +-CKYStatus CACAPDUFactory_SignDecrypt(CKYAPDU *apdu, const CKYBuffer *data); ++CKYStatus CACAPDUFactory_SignDecrypt(CKYAPDU *apdu, CKYByte type, ++ const CKYBuffer *data); + CKYStatus CACAPDUFactory_VerifyPIN(CKYAPDU *apdu, const char *pin); + CKYStatus CACAPDUFactory_GetCertificate(CKYAPDU *apdu, CKYSize size); ++CKYStatus CACAPDUFactory_ReadFile(CKYAPDU *apdu, unsigned short offset, ++ CKYByte type, CKYByte count); + CKYStatus CACAPDUFactory_GetProperties(CKYAPDU *apdu); + + CKY_END_PROTOS diff --git a/SOURCES/coolkey-cache-dir-move.patch b/SOURCES/coolkey-cache-dir-move.patch new file mode 100644 index 0000000..1b5eaff --- /dev/null +++ b/SOURCES/coolkey-cache-dir-move.patch @@ -0,0 +1,177 @@ +Index: src/coolkey/machdep.cpp +=================================================================== +RCS file: /cvs/dirsec/coolkey/src/coolkey/machdep.cpp,v +retrieving revision 1.4 +diff -u -r1.4 machdep.cpp +--- src/coolkey/machdep.cpp 14 Feb 2007 00:46:28 -0000 1.4 ++++ src/coolkey/machdep.cpp 15 Aug 2007 01:41:11 -0000 +@@ -185,12 +185,20 @@ + #define MAP_INHERIT 0 + #endif + ++#ifndef BASEPATH ++#ifdef MAC ++#define BASEPATH "/var" ++#else ++#define BASEPATH "/var/cache" ++#endif ++#endif ++ + #ifdef FULL_CLEANUP + #define RESERVED_OFFSET 256 +-#define MEMSEGPATH "/tmp/.pk11ipc" ++#define MEMSEGPATH BASEPATH"/coolkey-lock" + #else + #define RESERVED_OFFSET 0 +-#define MEMSEGPATH "/tmp/.pk11ipc1" ++#define MEMSEGPATH BASEPATH"/coolkey" + #endif + + struct SHMemData { +@@ -208,11 +216,6 @@ + #ifdef FULL_CLEANUP + flock(fd,LOCK_EX); + unsigned long ref = --(*(unsigned long *)addr); +-#ifdef notdef +- if (ref == 0) { +- unlink(path); +- } +-#endif + flock(fd, LOCK_UN); + #endif + munmap(addr,size+RESERVED_OFFSET); +@@ -225,6 +228,73 @@ + } + } + ++/* ++ * The cache directory is shared and accessible by anyone, make ++ * sure the cache file we are opening is really a valid cache file. ++ */ ++int safe_open(char *path, int flags, int mode, int size) ++{ ++ struct stat buf; ++ int fd, ret; ++ ++ fd = open (path, flags|O_NOFOLLOW, mode); ++ ++ if (fd < 0) { ++ return fd; ++ } ++ ++ ret = fstat(fd, &buf); ++ if (ret < 0) { ++ close (fd); ++ return ret; ++ } ++ ++ /* our cache files are pretty specific, make sure we are looking ++ * at the correct one */ ++ ++ /* first, we should own the file ourselves, don't open a file ++ * that someone else wanted us to see. */ ++ if (buf.st_uid != getuid()) { ++ close(fd); ++ errno = EACCES; ++ return -1; ++ } ++ ++ /* next, there should only be one link in this file. Don't ++ * use this code to trash another file */ ++ if (buf.st_nlink != 1) { ++ close(fd); ++ errno = EMLINK; ++ return -1; ++ } ++ ++ /* next, This better be a regular file */ ++ if (!S_ISREG(buf.st_mode)) { ++ close(fd); ++ errno = EACCES; ++ return -1; ++ } ++ ++ /* if the permissions don't match, something is wrong */ ++ if ((buf.st_mode & 03777) != mode) { ++ close(fd); ++ errno = EACCES; ++ return -1; ++ } ++ ++ /* finally the file should be the correct size. This ++ * check isn't so much to protect from an attack, as it is to ++ * detect a corrupted cache file */ ++ if (buf.st_size != size) { ++ close(fd); ++ errno = EACCES; ++ return -1; ++ } ++ ++ /* OK, the file checked out, ok to continue */ ++ return fd; ++} ++ + SHMem::SHMem(): shmemData(0) {} + + SHMem * +@@ -248,7 +318,7 @@ + return NULL; + } + int mask = umask(0); +- int ret = mkdir (MEMSEGPATH, 0777); ++ int ret = mkdir (MEMSEGPATH, 01777); + umask(mask); + if ((ret == -1) && (errno != EEXIST)) { + delete shmemData; +@@ -264,21 +334,16 @@ + shmemData->path[sizeof(MEMSEGPATH)-1] = '/'; + strcpy(&shmemData->path[sizeof(MEMSEGPATH)],name); + +- int mode = 0777; +- if (strcmp(name,"token_names") != 0) { +- /* each user gets his own uid array */ +- sprintf(uid_str, "-%u",getuid()); +- strcat(shmemData->path,uid_str); +- mode = 0700; +- } ++ sprintf(uid_str, "-%u",getuid()); ++ strcat(shmemData->path,uid_str); ++ int mode = 0600; ++ + shmemData->fd = open(shmemData->path, + O_CREAT|O_RDWR|O_EXCL|O_APPEND|O_EXLOCK, mode); +- if (shmemData->fd < 0) { +- needInit = false; +- shmemData->fd = open(shmemData->path,O_RDWR|O_EXLOCK, mode); +- } else { ++ if (shmemData->fd >= 0) { + char *buf; + int len = size+RESERVED_OFFSET; ++ int ret; + + buf = (char *)calloc(1,len); + if (!buf) { +@@ -289,8 +354,22 @@ + delete shmemData; + return NULL; + } +- write(shmemData->fd,buf,len); ++ ret = write(shmemData->fd,buf,len); ++ if (ret != len) { ++ unlink(shmemData->path); ++#ifdef FULL_CLEANUP ++ flock(shmemData->fd, LOCK_UN); ++#endif ++ delete shmemData; ++ return NULL; ++ } ++ + free(buf); ++ } else if (errno == EEXIST) { ++ needInit = false; ++ ++ shmemData->fd = safe_open(shmemData->path,O_RDWR|O_EXLOCK, mode, ++ size+RESERVED_OFFSET); + } + if (shmemData->fd < 0) { + delete shmemData; diff --git a/SOURCES/coolkey-fix-token-removal-failure.patch b/SOURCES/coolkey-fix-token-removal-failure.patch new file mode 100644 index 0000000..7f8a954 --- /dev/null +++ b/SOURCES/coolkey-fix-token-removal-failure.patch @@ -0,0 +1,86 @@ +Fix insertion/removal detection + +pcsc now errors out of the SCardGetStatusChange call with +SCARD_E_UNKNOWN_READER if any of the passed readers aren't known. +This includes readers that were very recently forgotton about because +a user just disconnected them. + +(See + http://anonscm.debian.org/viewvc/pcsclite/trunk/PCSC/src/winscard_clnt.c?r1=5858&r2=5881 +for the change to pcsc) + +Unfortunately, this means SECMOD_WaitForAnyTokenEvent will fail with a +SC_NO_EVENT error if a user removes their smartcard at the wrong time. + +This patch changes coolkey to detect removed readers before calling +SCardGetStatusChange, so that it can handle the removal itself. + +diff -up coolkey-1.1.0/src/coolkey/slot.cpp.fix coolkey-1.1.0/src/coolkey/slot.cpp +--- coolkey-1.1.0/src/coolkey/slot.cpp.fix 2013-05-22 16:23:41.728846957 -0400 ++++ coolkey-1.1.0/src/coolkey/slot.cpp 2013-05-22 17:09:59.813958927 -0400 +@@ -279,24 +279,22 @@ SlotList::updateReaderList() + * don't recognize. + */ + +- /* first though, let's check to see if any previously removed readers have +- * come back from the dead. If the ignored bit has been set, we do not need +- * it any more. +- */ ++ /* Iterate through all the readers to see if we need to make unavailable any ++ * freshly removed readers. Also, see if any previously removed ++ * readers have come back from the dead and don't need to be ignored. ++ */ + + const char *curReaderName = NULL; + unsigned long knownState = 0; + for(int ri = 0 ; ri < numReaders; ri ++) { +- + knownState = CKYReader_GetKnownState(&readerStates[ri]); +- if( !(knownState & SCARD_STATE_IGNORE)) { +- continue; +- } +- ++ + curReaderName = CKYReader_GetReaderName(&readerStates[ri]); + if(readerNameExistsInList(curReaderName,&readerNames)) { + CKYReader_SetKnownState(&readerStates[ri], knownState & ~SCARD_STATE_IGNORE); +- ++ } else { ++ if (!(knownState & SCARD_STATE_UNAVAILABLE)) ++ CKYReader_SetKnownState(&readerStates[ri], knownState | SCARD_STATE_UNAVAILABLE | SCARD_STATE_CHANGED); + } + } + +@@ -1238,6 +1236,32 @@ SlotList::waitForSlotEvent(CK_FLAGS flag + throw; + } + ++ /* Before round-tripping to the daemon for the duration of the ++ * timeout, first see if we lost any readers, and pick a slot ++ * from that set to return ++ */ ++ for (i=0; i < numReaders; i++) { ++ unsigned long knownState = CKYReader_GetKnownState(&readerStates[i]); ++ ++ if ((knownState & SCARD_STATE_UNAVAILABLE) && ++ (knownState & SCARD_STATE_CHANGED)) { ++ CKYReader_SetKnownState(&readerStates[i], knownState & ~SCARD_STATE_CHANGED); ++ readerListLock.releaseLock(); ++ *slotp = slotIndexToID(i); ++ found = TRUE; ++ break; ++ } ++ } ++ ++ if (found) { ++ break; ++ } ++ ++ if (shuttingDown) { ++ readerListLock.releaseLock(); ++ break; ++ } ++ + if (myNumReaders != numReaders) { + if (myReaderStates) { + delete [] myReaderStates; diff --git a/SOURCES/coolkey-gcc43.patch b/SOURCES/coolkey-gcc43.patch new file mode 100644 index 0000000..5e41b5a --- /dev/null +++ b/SOURCES/coolkey-gcc43.patch @@ -0,0 +1,54 @@ +diff -up ./src/coolkey/slot.cpp.coolkey-gcc43 ./src/coolkey/slot.cpp +--- ./src/coolkey/slot.cpp.coolkey-gcc43 2008-02-13 18:01:45.000000000 -0800 ++++ ./src/coolkey/slot.cpp 2008-02-13 18:03:05.000000000 -0800 +@@ -25,7 +25,6 @@ + #include "PKCS11Exception.h" + #include + #include "slot.h" +-#include + #include "zlib.h" + #include "params.h" + +@@ -33,7 +32,6 @@ + + #define MIN(x, y) ((x) < (y) ? (x) : (y)) + +-using std::auto_ptr; + + + #ifdef DEBUG +diff -up ./src/coolkey/machdep.cpp.coolkey-gcc43 ./src/coolkey/machdep.cpp +--- ./src/coolkey/machdep.cpp.coolkey-gcc43 2008-02-13 18:02:06.000000000 -0800 ++++ ./src/coolkey/machdep.cpp 2008-02-13 18:04:04.000000000 -0800 +@@ -33,6 +33,8 @@ + #include + #include + #include ++#include ++#include + #endif + + #ifdef _WIN32 +diff -up ./src/coolkey/log.cpp.coolkey-gcc43 ./src/coolkey/log.cpp +--- ./src/coolkey/log.cpp.coolkey-gcc43 2008-02-13 18:01:55.000000000 -0800 ++++ ./src/coolkey/log.cpp 2008-02-13 18:03:37.000000000 -0800 +@@ -18,6 +18,8 @@ + * ***** END COPYRIGHT BLOCK *****/ + + #include ++#include ++#include + #include "mypkcs11.h" + #include + #include +diff -up ./src/coolkey/object.cpp.coolkey-gcc43 ./src/coolkey/object.cpp +--- ./src/coolkey/object.cpp.coolkey-gcc43 2008-02-13 18:02:20.000000000 -0800 ++++ ./src/coolkey/object.cpp 2008-02-13 18:04:22.000000000 -0800 +@@ -21,6 +21,7 @@ + #include "PKCS11Exception.h" + #include "object.h" + #include ++#include + + using std::find_if; + diff --git a/SOURCES/coolkey-latest.patch b/SOURCES/coolkey-latest.patch new file mode 100644 index 0000000..bc10eb8 --- /dev/null +++ b/SOURCES/coolkey-latest.patch @@ -0,0 +1,685 @@ +diff -up ./src/coolkey/slot.cpp.coolkey-latest ./src/coolkey/slot.cpp +--- ./src/coolkey/slot.cpp.coolkey-latest 2009-09-11 13:58:24.423487305 -0700 ++++ ./src/coolkey/slot.cpp 2009-09-11 14:04:30.813488220 -0700 +@@ -203,6 +203,29 @@ SlotList::readerExists(const char *reade + return FALSE; + } + ++bool ++SlotList::readerNameExistsInList(const char *readerName,CKYReaderNameList *readerNameList) ++{ ++ if( !readerName || !readerNameList) { ++ return FALSE; ++ } ++ ++ int i = 0; ++ int readerNameCnt = CKYReaderNameList_GetCount(*readerNameList); ++ ++ const char *curReaderName = NULL; ++ for(i=0; i < readerNameCnt; i++) { ++ curReaderName = CKYReaderNameList_GetValue(*readerNameList,i); ++ ++ if(!strcmp(curReaderName,readerName)) { ++ return TRUE; ++ } ++ ++ } ++ ++ return FALSE; ++} ++ + /* + * you need to hold the ReaderList Lock before you can update the ReaderList + */ +@@ -256,6 +279,27 @@ SlotList::updateReaderList() + * don't recognize. + */ + ++ /* first though, let's check to see if any previously removed readers have ++ * come back from the dead. If the ignored bit has been set, we do not need ++ * it any more. ++ */ ++ ++ const char *curReaderName = NULL; ++ unsigned long knownState = 0; ++ for(int ri = 0 ; ri < numReaders; ri ++) { ++ ++ knownState = CKYReader_GetKnownState(&readerStates[ri]); ++ if( !(knownState & SCARD_STATE_IGNORE)) { ++ continue; ++ } ++ ++ curReaderName = CKYReader_GetReaderName(&readerStates[ri]); ++ if(readerNameExistsInList(curReaderName,&readerNames)) { ++ CKYReader_SetKnownState(&readerStates[ri], knownState & ~SCARD_STATE_IGNORE); ++ ++ } ++ } ++ + const char *newReadersData[MAX_READER_DELTA]; + const char **newReaders = &newReadersData[0]; + unsigned int newReaderCount = 0; +@@ -528,7 +572,7 @@ SlotList::getSlotList(CK_BBOOL tokenPres + void + Slot::connectToToken() + { +- CKYStatus status; ++ CKYStatus status = CKYSCARDERR; + OSTime time = OSTimeNow(); + + mCoolkey = 0; +@@ -537,13 +581,31 @@ Slot::connectToToken() + + // try to connect to the card + if( ! CKYCardConnection_IsConnected(conn) ) { +- status = CKYCardConnection_Connect(conn, readerName); +- if( status != CKYSUCCESS ) { +- log->log("Unable to connect to token\n"); ++ int i = 0; ++ //for cranky readers try again a few more times ++ while( i++ < 5 && status != CKYSUCCESS ) ++ { ++ status = CKYCardConnection_Connect(conn, readerName); ++ if( status != CKYSUCCESS && ++ CKYCardConnection_GetLastError(conn) == SCARD_E_PROTO_MISMATCH ) ++ { ++ log->log("Unable to connect to token status %d ConnGetGetLastError %x .\n",status,CKYCardConnection_GetLastError(conn)); ++ ++ } ++ else ++ { ++ break; ++ } ++ OSSleep(100000); ++ } ++ ++ if( status != CKYSUCCESS) ++ { + state = UNKNOWN; + return; + } + } ++ + log->log("time connect: Connect Time %d ms\n", OSTimeNow() - time); + if (!slotInfoFound) { + readSlotInfo(); +@@ -562,15 +624,10 @@ Slot::connectToToken() + state = CARD_PRESENT; + } + +- if ( CKYBuffer_DataIsEqual(&cardATR, ATR, sizeof (ATR)) || +- CKYBuffer_DataIsEqual(&cardATR, ATR1, sizeof(ATR1)) || +- CKYBuffer_DataIsEqual(&cardATR, ATR2, sizeof(ATR2)) ) { +- +- if (Params::hasParam("noAppletOK")) +- { +- state |= APPLET_SELECTABLE; +- mCoolkey = 1; +- } ++ if (Params::hasParam("noAppletOK")) ++ { ++ state |= APPLET_SELECTABLE; ++ mCoolkey = 1; + } + + /* support CAC card. identify the card based on applets, not the ATRS */ +@@ -631,7 +688,7 @@ Slot::connectToToken() + * unfriendly */ + isVersion1Key = 0; + needLogin = 1; +- ++ mCoolkey = 0; + return; + } + mCoolkey = 1; +@@ -1077,6 +1134,7 @@ SlotList::waitForSlotEvent(CK_FLAGS flag + } + throw; + } ++ + if (myNumReaders != numReaders) { + if (myReaderStates) { + delete [] myReaderStates; +@@ -1103,6 +1161,7 @@ SlotList::waitForSlotEvent(CK_FLAGS flag + } + } + } ++ + if (found || (flag == CKF_DONT_BLOCK) || shuttingDown) { + break; + } +@@ -1272,6 +1331,19 @@ class ObjectHandleMatch { + } + }; + ++class KeyNumMatch { ++ private: ++ CKYByte keyNum; ++ const Slot &slot; ++ public: ++ KeyNumMatch(CKYByte keyNum_, const Slot &s) : keyNum(keyNum_), slot(s) { } ++ bool operator() (const PKCS11Object& obj) { ++ unsigned long objID = obj.getMuscleObjID(); ++ return (slot.getObjectClass(objID) == 'k') ++ && (slot.getObjectIndex(objID) == keyNum); ++ } ++}; ++ + class ObjectCertCKAIDMatch { + private: + CKYByte cka_id; +@@ -3007,8 +3079,9 @@ Slot::sign(SessionHandleSuffix suffix, C + CK_ULONG ulDataLen, CK_BYTE_PTR pSignature, + CK_ULONG_PTR pulSignatureLen) + { ++ RSASignatureParams params(CryptParams::DEFAULT_KEY_SIZE); + cryptRSA(suffix, pData, ulDataLen, pSignature, pulSignatureLen, +- RSASignatureParams(CryptParams::FIXED_KEY_SIZE)); ++ params); + } + + void +@@ -3016,14 +3089,15 @@ Slot::decrypt(SessionHandleSuffix suffix + CK_ULONG ulDataLen, CK_BYTE_PTR pDecryptedData, + CK_ULONG_PTR pulDecryptedDataLen) + { ++ RSADecryptParams params(CryptParams::DEFAULT_KEY_SIZE); + cryptRSA(suffix, pData, ulDataLen, pDecryptedData, pulDecryptedDataLen, +- RSADecryptParams(CryptParams::FIXED_KEY_SIZE)); ++ params); + } + + void + Slot::cryptRSA(SessionHandleSuffix suffix, CK_BYTE_PTR pInput, + CK_ULONG ulInputLen, CK_BYTE_PTR pOutput, +- CK_ULONG_PTR pulOutputLen, const CryptParams& params) ++ CK_ULONG_PTR pulOutputLen, CryptParams& params) + { + refreshTokenState(); + SessionIter session = findSession(suffix); +@@ -3041,6 +3115,11 @@ Slot::cryptRSA(SessionHandleSuffix suffi + CKYBuffer *result = &opState.result; + CKYByte keyNum = opState.keyNum; + ++ unsigned int keySize = getKeySize(keyNum); ++ ++ if(keySize != CryptParams::DEFAULT_KEY_SIZE) ++ params.setKeySize(keySize); ++ + if( CKYBuffer_Size(result) == 0 ) { + // we haven't already peformed the decryption, so do it now. + if( pInput == NULL || ulInputLen == 0) { +@@ -3243,3 +3322,36 @@ Slot::generateRandom(SessionHandleSuffix + throw PKCS11Exception(CKR_DEVICE_ERROR); + } + } ++ ++#define MAX_NUM_KEYS 8 ++unsigned int ++Slot::getKeySize(CKYByte keyNum) ++{ ++ unsigned int keySize = CryptParams::DEFAULT_KEY_SIZE; ++ int modSize = 0; ++ ++ if(keyNum >= MAX_NUM_KEYS) { ++ return keySize; ++ } ++ ++ ObjectConstIter iter; ++ iter = find_if(tokenObjects.begin(), tokenObjects.end(), ++ KeyNumMatch(keyNum,*this)); ++ ++ if( iter == tokenObjects.end() ) { ++ return keySize; ++ } ++ ++ CKYBuffer const *modulus = iter->getAttribute(CKA_MODULUS); ++ ++ if(modulus) { ++ modSize = CKYBuffer_Size(modulus); ++ if(CKYBuffer_GetChar(modulus,0) == 0x0) { ++ modSize--; ++ } ++ if(modSize > 0) ++ keySize = modSize * 8; ++ } ++ ++ return keySize; ++} +diff -up ./src/coolkey/slot.h.coolkey-latest ./src/coolkey/slot.h +--- ./src/coolkey/slot.h.coolkey-latest 2006-06-09 11:39:11.000000000 -0700 ++++ ./src/coolkey/slot.h 2009-09-11 13:58:24.462488099 -0700 +@@ -270,10 +270,9 @@ class CryptParams { + protected: + unsigned int getKeySize() const { return keySize; } + public: +- // !!!XXX hack. The right way to get the key size is to get all the +- // key information from the token with MSCListKeys, the same way +- // we get all the object information with MSCListObjects. +- enum { FIXED_KEY_SIZE = 1024 }; ++ // set the actual key size obtained from the card ++ void setKeySize(unsigned int newKeySize) { keySize = newKeySize; } ++ enum { DEFAULT_KEY_SIZE = 1024 }; + + + CryptParams(unsigned int keySize_) : keySize(keySize_) { } +@@ -422,7 +421,7 @@ class Slot { + + void cryptRSA(SessionHandleSuffix suffix, CK_BYTE_PTR pInput, + CK_ULONG ulInputLen, CK_BYTE_PTR pOutput, +- CK_ULONG_PTR pulOutputLen, const CryptParams& params); ++ CK_ULONG_PTR pulOutputLen, CryptParams& params); + + void performRSAOp(CKYBuffer *out, const CKYBuffer *input, CKYByte keyNum, + CKYByte direction); +@@ -460,6 +459,8 @@ class Slot { + return (char )((objectID >> 16) & 0xff) - '0'; + } + ++ // actually get the size of a key in bits from the card ++ unsigned int getKeySize(CKYByte keyNum); + + SessionHandleSuffix openSession(Session::Type type); + void closeSession(SessionHandleSuffix handleSuffix); +@@ -527,6 +528,8 @@ class SlotList { + * has called 'C_GetSlotList' with a NULL parameter */ + void updateReaderList(); + ++ /* see if a reader name exists in a caller provided reader name list. */ ++ bool readerNameExistsInList(const char *readerName,CKYReaderNameList *readerNameList ); + bool readerExists(const char *readerName, unsigned int *hint = 0); + public: + SlotList(Log *log); +diff -up ./src/libckyapplet/cky_applet.c.coolkey-latest ./src/libckyapplet/cky_applet.c +--- ./src/libckyapplet/cky_applet.c.coolkey-latest 2006-06-09 11:44:17.000000000 -0700 ++++ ./src/libckyapplet/cky_applet.c 2009-09-11 13:58:24.464487796 -0700 +@@ -134,6 +134,13 @@ CKYAppletFactory_Logout(CKYAPDU *apdu, c + /* Future add WriteObject */ + + CKYStatus ++CKYAppletFactory_WriteObject(CKYAPDU *apdu, const void *param) ++{ ++ const CKYAppletArgWriteObject *wos = (const CKYAppletArgWriteObject *)param; ++ return CKYAPDUFactory_WriteObject(apdu,wos->objectID,wos->offset,wos->size,wos->data); ++} ++ ++CKYStatus + CKYAppletFactory_CreateObject(CKYAPDU *apdu, const void *param) + { + const CKYAppletArgCreateObject *cos=(const CKYAppletArgCreateObject *)param; +@@ -192,7 +199,6 @@ CKYAppletFactory_GetLifeCycleV2(CKYAPDU + { + return CKYAPDUFactory_GetLifeCycleV2(apdu); + } +- + CKYStatus + CKYAppletFactory_GetRandom(CKYAPDU *apdu, const void *param) + { +@@ -725,24 +731,48 @@ CKYApplet_ComputeCrypt(CKYCardConnection + CKYAppletArgComputeCrypt ccd; + CKYBuffer empty; + CKYISOStatus status; ++ short dataSize = 0; + int use2APDUs = 0; ++ int use_dl_object = CKYBuffer_Size(data) > 200 ; + + CKYBuffer_InitEmpty(&empty); + ccd.keyNumber = keyNumber; + ccd.mode = mode; + ccd.direction = direction; +- ccd.location = CKY_DL_APDU; ++ ccd.location = use_dl_object ? CKY_DL_OBJECT : CKY_DL_APDU; + + if (!apduRC) + apduRC = &status; + ++ if (use_dl_object) { ++ CKYBuffer sizeBuf; ++ ++ CKYBuffer_InitEmpty(&sizeBuf); ++ CKYBuffer_AppendShort(&sizeBuf, CKYBuffer_Size(data)); ++ ++ ret = CKYApplet_WriteObjectFull(conn, 0xffffffff, ++ 0, CKYBuffer_Size(&sizeBuf), nonce, ++ &sizeBuf, apduRC); ++ ++ CKYBuffer_FreeData(&sizeBuf); ++ if( ret != CKYSUCCESS) ++ goto fail; ++ ++ ret = CKYApplet_WriteObjectFull(conn, 0xffffffff, ++ 2, CKYBuffer_Size(data), nonce, ++ data, apduRC); ++ ++ if(ret != CKYSUCCESS) ++ goto fail; ++ } ++ + if (mode == CKY_RSA_NO_PAD) { +- ccd.data = data; ++ ccd.data = use_dl_object ? &empty : data; + ccd.sig = sig; + ret = CKYApplet_HandleAPDU(conn, + CKYAppletFactory_ComputeCryptOneStep, &ccd, nonce, + CKY_SIZE_UNKNOWN, ckyAppletFill_ComputeCryptFinal, +- result, apduRC); ++ use_dl_object ? NULL : result, apduRC); + if (ret == CKYAPDUFAIL && *apduRC == CKYISO_INCORRECT_P2) { + use2APDUs = 1; /* maybe it's an old applet */ + } +@@ -759,13 +789,38 @@ CKYApplet_ComputeCrypt(CKYCardConnection + CKYAppletFactory_ComputeCryptInit, &ccd, nonce, + 0, CKYAppletFill_Null, NULL, apduRC); + if (ret == CKYSUCCESS) { +- ccd.data = data; ++ ccd.data = use_dl_object ? &empty : data; + ret = CKYApplet_HandleAPDU(conn, + CKYAppletFactory_ComputeCryptFinal, &ccd, nonce, + CKY_SIZE_UNKNOWN, ckyAppletFill_ComputeCryptFinal, +- result, apduRC); ++ use_dl_object ? NULL : result, apduRC); + } + } ++ ++ if (use_dl_object && ret == CKYSUCCESS) { ++ CKYBuffer sizeOutBuf; ++ CKYBuffer_InitEmpty(&sizeOutBuf); ++ ++ ret = CKYApplet_ReadObjectFull(conn,0xffffffff, ++ 0, 2, ++ nonce,&sizeOutBuf,apduRC); ++ ++ if(ret != CKYSUCCESS) { ++ CKYBuffer_FreeData(&sizeOutBuf); ++ goto fail; ++ } ++ ++ dataSize = CKYBuffer_GetShort(&sizeOutBuf, 0); ++ ++ CKYBuffer_FreeData(&sizeOutBuf); ++ ++ ret = CKYApplet_ReadObjectFull(conn,0xffffffff, ++ 2, dataSize, ++ nonce,result,apduRC); ++ } ++ ++fail: ++ + return ret; + } + +@@ -1036,6 +1091,44 @@ CKYApplet_ReadObjectFull(CKYCardConnecti + } + + /* ++ * Write Object ++ * This makes multiple APDU calls to write the entire object. ++ * ++ */ ++ ++CKYStatus ++CKYApplet_WriteObjectFull(CKYCardConnection *conn, unsigned long objectID, ++ CKYOffset offset, CKYSize size, const CKYBuffer *nonce, ++ const CKYBuffer *data, CKYISOStatus *apduRC) ++{ ++ ++ CKYBuffer chunk; ++ CKYOffset srcOffset = 0; ++ CKYAppletArgWriteObject wod; ++ CKYStatus ret = CKYSUCCESS; ++ ++ wod.objectID = objectID; ++ wod.offset = offset; ++ do { ++ wod.size = (CKYByte) MIN(size, 220); ++ ret = CKYBuffer_InitFromBuffer(&chunk, data, ++ srcOffset, wod.size); ++ if(ret == CKYSUCCESS) { ++ wod.data = &chunk; ++ ret = CKYApplet_HandleAPDU(conn, CKYAppletFactory_WriteObject, &wod, ++ nonce, 0, CKYAppletFill_Null, NULL, apduRC); ++ size -= wod.size; ++ wod.offset += wod.size; ++ srcOffset += wod.size; ++ CKYBuffer_FreeData(&chunk); ++ } ++ ++ } while ((size > 0) && (ret == CKYSUCCESS)); ++ ++ return ret; ++} ++ ++/* + * List Object cluster + */ + static CKYStatus +diff -up ./src/libckyapplet/cky_applet.h.coolkey-latest ./src/libckyapplet/cky_applet.h +--- ./src/libckyapplet/cky_applet.h.coolkey-latest 2006-06-09 11:44:17.000000000 -0700 ++++ ./src/libckyapplet/cky_applet.h 2009-09-11 13:58:24.466487772 -0700 +@@ -192,6 +192,14 @@ typedef struct _CKYAppletArgReadObject { + CKYByte size; + } CKYAppletArgReadObject; + ++typedef struct _CKYAppletArgWriteObject { ++ unsigned long objectID; ++ CKYOffset offset; ++ CKYByte size; ++ CKYBuffer *data; ++ ++} CKYAppletArgWriteObject; ++ + typedef struct _CKYAppletArgComputeCrypt { + CKYByte keyNumber; + CKYByte mode; +@@ -250,6 +258,8 @@ CKYStatus CKYAppletFactory_ListPINs(CKYA + /* param == CKYByte * (pointer to pinNumber) */ + CKYStatus CKYAppletFactory_Logout(CKYAPDU *apdu, const void *param); + /* Future add WriteObject */ ++/* parm == CKYAppletArgWriteObject */ ++CKYStatus CKYAppletFactory_WriteObject(CKYAPDU *apdu, const void *param); + /* param == CKYAppletArgCreateObject */ + CKYStatus CKYAppletFactory_CreateObject(CKYAPDU *apdu, const void *param); + /* param == CKYAppletArgDeleteObject */ +@@ -482,6 +492,17 @@ CKYStatus CKYApplet_ReadObjectAppend(CKY + CKYStatus CKYApplet_ReadObjectFull(CKYCardConnection *conn, + unsigned long objectID, CKYOffset offset, CKYSize size, + const CKYBuffer *nonce, CKYBuffer *data, CKYISOStatus *apduRC); ++/* ++ * There is 1 write command: ++ * CKYApplet_WriteObjectFull can write an entire data object. It makes multiple ++ * apdu calls in order to write the full amount into the buffer. The buffer is ++ * overwritten. ++*/ ++ ++CKYStatus CKYApplet_WriteObjectFull(CKYCardConnection *conn, ++ unsigned long objectID, CKYOffset offset, CKYSize size, ++ const CKYBuffer *nonce, const CKYBuffer *data, CKYISOStatus *apduRC); ++ + CKYStatus CKYApplet_ListObjects(CKYCardConnection *conn, CKYByte seq, + CKYAppletRespListObjects *lop, CKYISOStatus *apduRC); + CKYStatus CKYApplet_GetStatus(CKYCardConnection *conn, +diff -up ./src/libckyapplet/cky_card.c.coolkey-latest ./src/libckyapplet/cky_card.c +--- ./src/libckyapplet/cky_card.c.coolkey-latest 2006-06-09 11:44:17.000000000 -0700 ++++ ./src/libckyapplet/cky_card.c 2009-09-11 13:58:24.468487469 -0700 +@@ -129,6 +129,7 @@ typedef struct _SCard { + SCardGetStatusChangeFn SCardGetStatusChange; + SCardCancelFn SCardCancel; + SCARD_IO_REQUEST *SCARD_PCI_T0_; ++ SCARD_IO_REQUEST *SCARD_PCI_T1_; + } SCard; + + #define GET_ADDRESS(library, scard, name) \ +@@ -195,6 +196,12 @@ ckySCard_Init(void) + if( status != CKYSUCCESS ) { + goto fail; + } ++ ++ status = ckyShLibrary_getAddress( library, ++ (void**) &scard->SCARD_PCI_T1_, MAKE_DLL_SYMBOL(g_rgSCardT1Pci)); ++ if( status != CKYSUCCESS ) { ++ goto fail; ++ } + return scard; + + fail: +@@ -884,6 +891,7 @@ struct _CKYCardConnection { + SCARDHANDLE cardHandle; + unsigned long lastError; + CKYBool inTransaction; ++ unsigned long protocol; + }; + + static void +@@ -894,6 +902,7 @@ ckyCardConnection_init(CKYCardConnection + conn->cardHandle = 0; + conn->lastError = 0; + conn->inTransaction = 0; ++ conn->protocol = SCARD_PROTOCOL_T0; + } + + CKYCardConnection * +@@ -934,14 +943,13 @@ CKYCardConnection_Connect(CKYCardConnect + { + CKYStatus ret; + unsigned long rv; +- unsigned long protocol; + + ret = CKYCardConnection_Disconnect(conn); + if (ret != CKYSUCCESS) { + return ret; + } + rv = conn->scard->SCardConnect( conn->ctx->context, readerName, +- SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0, &conn->cardHandle, &protocol); ++ SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, &conn->cardHandle, &conn->protocol); + if (rv != SCARD_S_SUCCESS) { + conn->lastError = rv; + return CKYSCARDERR; +@@ -978,7 +986,7 @@ ckyCardConnection_reconnectRaw(CKYCardCo + unsigned long protocol; + + rv = conn->scard->SCardReconnect(conn->cardHandle, +- SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0, init, &protocol); ++ SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1 , init, &protocol); + if (rv != SCARD_S_SUCCESS) { + conn->lastError = rv; + return CKYSCARDERR; +@@ -1039,10 +1047,17 @@ CKYCardConnection_TransmitAPDU(CKYCardCo + return ret; + } + +- rv = conn->scard->SCardTransmit(conn->cardHandle, +- conn->scard->SCARD_PCI_T0_, +- CKYBuffer_Data(&apdu->apduBuf), CKYBuffer_Size(&apdu->apduBuf), +- NULL, response->data, &response->len); ++ if( conn->protocol == SCARD_PROTOCOL_T0 ) { ++ rv = conn->scard->SCardTransmit(conn->cardHandle, ++ conn->scard->SCARD_PCI_T0_, ++ CKYBuffer_Data(&apdu->apduBuf), CKYBuffer_Size(&apdu->apduBuf), ++ NULL, response->data, &response->len); ++ } else { ++ rv = conn->scard->SCardTransmit(conn->cardHandle, ++ conn->scard->SCARD_PCI_T1_, ++ CKYBuffer_Data(&apdu->apduBuf), CKYBuffer_Size(&apdu->apduBuf), ++ NULL, response->data, &response->len); ++ } + + if (rv != SCARD_S_SUCCESS) { + conn->lastError =rv; +diff -up ./src/libckyapplet/cky_factory.c.coolkey-latest ./src/libckyapplet/cky_factory.c +--- ./src/libckyapplet/cky_factory.c.coolkey-latest 2006-06-09 11:44:17.000000000 -0700 ++++ ./src/libckyapplet/cky_factory.c 2009-09-11 13:58:24.470495267 -0700 +@@ -190,8 +190,11 @@ CKYAPDUFactory_ComputeCryptOneStep(CKYAP + CKYSize len; + CKYBuffer buf; + +- if (!idata || !(len = CKYBuffer_Size(idata)) || location != CKY_DL_APDU) +- return ret; ++ if (!idata) ++ return ret; ++ ++ if (!(len = CKYBuffer_Size(idata)) && location != CKY_DL_OBJECT) ++ return ret; + + CKYAPDU_SetCLA(apdu, CKY_CLASS_COOLKEY); + CKYAPDU_SetINS(apdu, CKY_INS_COMPUTE_CRYPT); +@@ -314,8 +317,6 @@ CKYAPDUFactory_Logout(CKYAPDU *apdu, CKY + return CKYSUCCESS; + } + +-/* Future add WriteObject */ +- + CKYStatus + CKYAPDUFactory_CreateObject(CKYAPDU *apdu, unsigned long objectID, CKYSize size, + unsigned short readACL, unsigned short writeACL, unsigned short deleteACL) +@@ -419,6 +420,58 @@ fail: + } + + CKYStatus ++CKYAPDUFactory_WriteObject(CKYAPDU *apdu, unsigned long objectID, ++ CKYOffset offset,CKYSize size,CKYBuffer *data) ++{ ++ CKYBuffer buf; ++ CKYStatus ret = CKYSUCCESS; ++ unsigned short dataSize = 0; ++ ++ CKYAPDU_SetCLA(apdu, CKY_CLASS_COOLKEY); ++ CKYAPDU_SetINS(apdu, CKY_INS_WRITE_OBJ); ++ CKYAPDU_SetP1(apdu, 0x00); ++ CKYAPDU_SetP2(apdu, 0x00); ++ CKYBuffer_InitEmpty(&buf); ++ ++ dataSize = (unsigned short) CKYBuffer_Size(data); ++ ++ if(!dataSize) { ++ ret = CKYINVALIDARGS; ++ goto fail; ++ } ++ ++ ret = CKYBuffer_AppendLong(&buf,objectID); ++ if (ret != CKYSUCCESS) { ++ goto fail; ++ } ++ ret = CKYBuffer_AppendLong(&buf,offset); ++ if (ret != CKYSUCCESS) { ++ goto fail; ++ } ++ ret = CKYBuffer_AppendChar(&buf, size); ++ if (ret != CKYSUCCESS) { ++ goto fail; ++ } ++ ++ ret = CKYAPDU_SetSendDataBuffer(apdu,&buf); ++ ++ if (ret != CKYSUCCESS) { ++ goto fail; ++ } ++ ++ ret = CKYAPDU_AppendSendDataBuffer(apdu, data); ++ ++ if (ret != CKYSUCCESS) { ++ goto fail; ++ } ++ ++fail: ++ CKYBuffer_FreeData(&buf); ++ return ret; ++ ++} ++ ++CKYStatus + CKYAPDUFactory_ListObjects(CKYAPDU *apdu, CKYByte sequence) + { + CKYAPDU_SetCLA(apdu, CKY_CLASS_COOLKEY); +diff -up ./src/libckyapplet/cky_factory.h.coolkey-latest ./src/libckyapplet/cky_factory.h +--- ./src/libckyapplet/cky_factory.h.coolkey-latest 2006-06-09 11:44:17.000000000 -0700 ++++ ./src/libckyapplet/cky_factory.h 2009-09-11 13:58:24.472487421 -0700 +@@ -190,7 +190,8 @@ CKYStatus CKYAPDUFactory_ChangePIN(CKYAP + const char *oldPin, const char *newPin); + CKYStatus CKYAPDUFactory_ListPINs(CKYAPDU *apdu); + CKYStatus CKYAPDUFactory_Logout(CKYAPDU *apdu, CKYByte pinNumber); +- ++CKYStatus CKYAPDUFactory_WriteObject(CKYAPDU *apdu, unsigned long objectID, ++ CKYOffset offset,CKYSize size,CKYBuffer *data); + /* Future add WriteObject */ + CKYStatus CKYAPDUFactory_CreateObject(CKYAPDU *apdu, unsigned long objectID, + CKYSize size, unsigned short readACL, unsigned short writeACL, diff --git a/SOURCES/coolkey-pcsc-lite-fix.patch b/SOURCES/coolkey-pcsc-lite-fix.patch new file mode 100644 index 0000000..f528676 --- /dev/null +++ b/SOURCES/coolkey-pcsc-lite-fix.patch @@ -0,0 +1,69 @@ +diff -up ./src/coolkey/slot.cpp.reader-state-fix ./src/coolkey/slot.cpp +--- ./src/coolkey/slot.cpp.reader-state-fix 2010-09-08 13:25:14.479109000 -0700 ++++ ./src/coolkey/slot.cpp 2010-09-08 13:25:14.506109000 -0700 +@@ -2185,6 +2185,7 @@ Slot::readCACCertificateFirst(CKYBuffer + { + CKYStatus status; + CKYISOStatus apduRC; ++ *nextSize = 0; + + if (mOldCAC) { + /* get the first 100 bytes of the cert */ +diff -up ./src/libckyapplet/cky_card.c.reader-state-fix ./src/libckyapplet/cky_card.c +--- ./src/libckyapplet/cky_card.c.reader-state-fix 2010-09-08 14:05:10.859321000 -0700 ++++ ./src/libckyapplet/cky_card.c 2010-09-08 14:05:42.792257000 -0700 +@@ -27,7 +27,6 @@ + + #ifndef WINAPI + #define WINAPI +-typedef SCARD_READERSTATE *LPSCARD_READERSTATE; + #endif + + #ifndef SCARD_E_NO_READERS_AVAILABLE +diff -up ./src/libckyapplet/cky_card.h.reader-state-fix ./src/libckyapplet/cky_card.h +--- ./src/libckyapplet/cky_card.h.reader-state-fix 2006-06-09 11:44:17.000000000 -0700 ++++ ./src/libckyapplet/cky_card.h 2010-09-08 13:25:14.518109000 -0700 +@@ -41,23 +41,23 @@ CKYLIST_DECLARE(CKYReaderName, char *) + CKYLIST_DECLARE(CKYCardConnection, CKYCardConnection *) + + CKY_BEGIN_PROTOS +-void CKYReader_Init(SCARD_READERSTATE_A *reader); +-void CKYReader_FreeData(SCARD_READERSTATE_A *reader); ++void CKYReader_Init(SCARD_READERSTATE *reader); ++void CKYReader_FreeData(SCARD_READERSTATE *reader); + + /* +- * "Accessors": for SCARD_READERSTATE_A structure as a class. +- * These functions take an SCARD_READERSTATE_A which can also be referenced ++ * "Accessors": for SCARD_READERSTATE structure as a class. ++ * These functions take an SCARD_READERSTATE which can also be referenced + * directly. + */ +-CKYStatus CKYReader_SetReaderName(SCARD_READERSTATE_A *reader, const char *name); +-const char *CKYReader_GetReaderName(const SCARD_READERSTATE_A *reader); +-CKYStatus CKYReader_SetKnownState(SCARD_READERSTATE_A *reader, ++CKYStatus CKYReader_SetReaderName(SCARD_READERSTATE *reader, const char *name); ++const char *CKYReader_GetReaderName(const SCARD_READERSTATE *reader); ++CKYStatus CKYReader_SetKnownState(SCARD_READERSTATE *reader, + unsigned long state); +-unsigned long CKYReader_GetKnownState(const SCARD_READERSTATE_A *reader); +-unsigned long CKYReader_GetEventState(const SCARD_READERSTATE_A *reader); +-CKYStatus CKYReader_GetATR(const SCARD_READERSTATE_A *reader, CKYBuffer *buf); ++unsigned long CKYReader_GetKnownState(const SCARD_READERSTATE *reader); ++unsigned long CKYReader_GetEventState(const SCARD_READERSTATE *reader); ++CKYStatus CKYReader_GetATR(const SCARD_READERSTATE *reader, CKYBuffer *buf); + /* create an array of READERSTATEs from a LIST of Readers */ +-SCARD_READERSTATE_A *CKYReader_CreateArray(const CKYReaderNameList readerNames, ++SCARD_READERSTATE *CKYReader_CreateArray(const CKYReaderNameList readerNames, + unsigned long *readerCount); + /* frees the reader, then the full array */ + void CKYReader_DestroyArray(SCARD_READERSTATE *reader, unsigned long count); +@@ -88,7 +88,7 @@ CKYStatus CKYCardContext_FindReadersByAT + const CKYBuffer *targetATR); + /* return if any of the readers in our array has changed in status */ + CKYStatus CKYCardContext_WaitForStatusChange(CKYCardContext *context, +- SCARD_READERSTATE_A *readers, ++ SCARD_READERSTATE *readers, + unsigned long readerCount, + unsigned long timeout); + /* cancel any current operation (such as wait for status change) on this diff --git a/SOURCES/coolkey-piv-ecc-el7.patch b/SOURCES/coolkey-piv-ecc-el7.patch new file mode 100644 index 0000000..1ca6f61 --- /dev/null +++ b/SOURCES/coolkey-piv-ecc-el7.patch @@ -0,0 +1,4792 @@ +diff -up ./src/coolkey/coolkey.cpp.piv-ecc ./src/coolkey/coolkey.cpp +--- ./src/coolkey/coolkey.cpp.piv-ecc 2013-09-08 15:50:33.085428102 -0700 ++++ ./src/coolkey/coolkey.cpp 2013-09-08 15:50:33.119428673 -0700 +@@ -34,7 +34,6 @@ + #include "cky_base.h" + #include "params.h" + +-#define NULL 0 + + /* static module data -------------------------------- */ + +@@ -70,11 +69,19 @@ typedef struct { + /********************************************************************** + ************************** MECHANISM TABLE *************************** + **********************************************************************/ +-static MechInfo +-mechanismList[] = { ++ ++static const MechInfo ++rsaMechanismList[] = { + {CKM_RSA_PKCS, { 1024, 4096, CKF_HW | CKF_SIGN | CKF_DECRYPT } } + }; +-static unsigned int numMechanisms = sizeof(mechanismList)/sizeof(MechInfo); ++ ++static const MechInfo ++ecMechanismList[] = { ++ {CKM_ECDSA,{256,521,CKF_HW | CKF_SIGN | CKF_EC_F_P}},{ CKM_ECDSA_SHA1, {256, 521, CKF_HW | CKF_SIGN | CKF_EC_F_P}},{ CKM_ECDH1_DERIVE,{256, 521, CKF_HW | CKF_DERIVE | CKF_EC_F_P} } ++}; ++ ++unsigned int numRSAMechanisms = sizeof(rsaMechanismList)/sizeof(MechInfo); ++unsigned int numECMechanisms = sizeof(ecMechanismList)/sizeof(MechInfo); + + /* ------------------------------------------------------------ */ + +@@ -166,7 +173,6 @@ NOTSUPPORTED(C_GenerateKey, (CK_SESSION_ + NOTSUPPORTED(C_GenerateKeyPair, (CK_SESSION_HANDLE,CK_MECHANISM_PTR,CK_ATTRIBUTE_PTR,CK_ULONG,CK_ATTRIBUTE_PTR,CK_ULONG,CK_OBJECT_HANDLE_PTR,CK_OBJECT_HANDLE_PTR)) + NOTSUPPORTED(C_WrapKey, (CK_SESSION_HANDLE,CK_MECHANISM_PTR,CK_OBJECT_HANDLE,CK_OBJECT_HANDLE,CK_BYTE_PTR,CK_ULONG_PTR)) + NOTSUPPORTED(C_UnwrapKey, (CK_SESSION_HANDLE,CK_MECHANISM_PTR,CK_OBJECT_HANDLE,CK_BYTE_PTR,CK_ULONG,CK_ATTRIBUTE_PTR,CK_ULONG,CK_OBJECT_HANDLE_PTR)) +-NOTSUPPORTED(C_DeriveKey, (CK_SESSION_HANDLE,CK_MECHANISM_PTR,CK_OBJECT_HANDLE,CK_ATTRIBUTE_PTR,CK_ULONG,CK_OBJECT_HANDLE_PTR)) + NOTSUPPORTED(C_GetFunctionStatus, (CK_SESSION_HANDLE)) + NOTSUPPORTED(C_CancelFunction, (CK_SESSION_HANDLE)) + +@@ -200,6 +206,10 @@ SUPPORTED(C_SeedRandom, seedRandom, + SUPPORTED(C_GenerateRandom, generateRandom, + (CK_SESSION_HANDLE hSession ,CK_BYTE_PTR data,CK_ULONG dataLen), + (hSession, data, dataLen)) ++SUPPORTED(C_DeriveKey,derive, ++ (CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, ++ CK_OBJECT_HANDLE hBaseKey, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey ), ++ (hSession, pMechanism, hBaseKey, pTemplate, ulAttributeCount, phKey)) + + /* non-specialized functions supported with the slot directly */ + +@@ -249,7 +259,7 @@ C_Initialize(CK_VOID_PTR pInitArgs) + log = new DummyLog(); + } + log->log("Initialize called, hello %d\n", 5); +- CKY_SetName("coolkey"); ++ CKY_SetName((char *) "coolkey"); + slotList = new SlotList(log); + initialized = TRUE; + return CKR_OK; +@@ -347,6 +357,11 @@ CK_RV + C_GetMechanismList(CK_SLOT_ID slotID, CK_MECHANISM_TYPE_PTR pMechanismList, + CK_ULONG_PTR pulCount) + { ++ ++ const MechInfo *mechanismList = NULL; ++ unsigned int numMechanisms = 0; ++ ++ + if( ! initialized ) { + return CKR_CRYPTOKI_NOT_INITIALIZED; + } +@@ -359,11 +374,21 @@ C_GetMechanismList(CK_SLOT_ID slotID, CK + } + + slotList->validateSlotID(slotID); +- if( ! slotList->getSlot( +- slotIDToIndex(slotID))->isTokenPresent() ) { ++ ++ Slot *slot = slotList->getSlot(slotIDToIndex(slotID)); ++ ++ if( ! slot || ! slot->isTokenPresent() ) { + return CKR_TOKEN_NOT_PRESENT; + } + ++ if ( slot->getIsECC()) { ++ mechanismList = ecMechanismList; ++ numMechanisms = numECMechanisms; ++ } else { ++ mechanismList = rsaMechanismList; ++ numMechanisms = numRSAMechanisms; ++ } ++ + if( pMechanismList != NULL ) { + if( *pulCount < numMechanisms ) { + rv = CKR_BUFFER_TOO_SMALL; +@@ -390,19 +415,36 @@ CK_RV + C_GetMechanismInfo(CK_SLOT_ID slotID, CK_MECHANISM_TYPE type, + CK_MECHANISM_INFO_PTR pInfo) + { ++ const MechInfo *mechanismList = NULL; ++ unsigned int numMechanisms = 0; ++ + if( ! initialized ) { + return CKR_CRYPTOKI_NOT_INITIALIZED; + } ++ ++ + try { + log->log("C_GetMechanismInfo called\n"); + if( pInfo == NULL ) { + throw PKCS11Exception(CKR_ARGUMENTS_BAD); + } + slotList->validateSlotID(slotID); +- if( ! slotList->getSlot(slotIDToIndex(slotID))->isTokenPresent() ) { ++ ++ ++ Slot *slot = slotList->getSlot(slotIDToIndex(slotID)); ++ ++ if( ! slot || ! slot->isTokenPresent() ) { + return CKR_TOKEN_NOT_PRESENT; + } + ++ if ( slot->getIsECC()) { ++ mechanismList = ecMechanismList; ++ numMechanisms = numECMechanisms; ++ } else { ++ mechanismList = rsaMechanismList; ++ numMechanisms = numRSAMechanisms; ++ } ++ + for(unsigned int i=0; i < numMechanisms; ++i ) { + if( mechanismList[i].mech == type ) { + *pInfo = mechanismList[i].info; +diff -up ./src/coolkey/machdep.cpp.piv-ecc ./src/coolkey/machdep.cpp +--- ./src/coolkey/machdep.cpp.piv-ecc 2013-09-08 15:50:33.085428102 -0700 ++++ ./src/coolkey/machdep.cpp 2013-09-08 15:50:33.119428673 -0700 +@@ -368,6 +368,7 @@ SHMem::initSegment(const char *name, int + #ifdef FULL_CLEANUP + flock(shmemData->fd, LOCK_UN); + #endif ++ free(buf); + delete shmemData; + return NULL; + } +diff -up ./src/coolkey/object.cpp.piv-ecc ./src/coolkey/object.cpp +--- ./src/coolkey/object.cpp.piv-ecc 2013-09-08 15:50:33.104428421 -0700 ++++ ./src/coolkey/object.cpp 2013-09-08 15:50:33.121428706 -0700 +@@ -25,12 +25,44 @@ + + using std::find_if; + ++const CKYByte rsaOID[] = {0x2A,0x86,0x48,0x86,0xF7,0x0D, 0x01, 0x01,0x1}; ++const CKYByte eccOID[] = {0x2a,0x86,0x48,0xce,0x3d,0x02,0x01}; ++ ++#ifdef DEBUG ++void dump(CKYBuffer *buf) ++{ ++ CKYSize i; ++ CKYSize size = CKYBuffer_Size(buf); ++#define ROW_LENGTH 60 ++ char string[ROW_LENGTH+1]; ++ char *bp = &string[0]; ++ CKYByte c; ++ ++ for (i=0; i < size; i++) { ++ if (i && ((i % (ROW_LENGTH-1)) == 0) ) { ++ *bp = 0; ++ printf(" %s\n",string); ++ bp = &string[0]; ++ } ++ c = CKYBuffer_GetChar(buf, i); ++ printf("%02x ",c); ++ *bp++ = (c < ' ') ? '.' : ((c & 0x80) ? '*' : c); ++ } ++ *bp = 0; ++ for (i= (i % (ROW_LENGTH-1)); i && (i < ROW_LENGTH); i++) { ++ printf(" "); ++ } ++ printf(" %s\n",string); ++ fflush(stdout); ++} ++#endif ++ + + bool AttributeMatch::operator()(const PKCS11Attribute& cmp) + { + return (attr->type == cmp.getType()) && +- CKYBuffer_DataIsEqual(cmp.getValue(), +- (const CKYByte *)attr->pValue, attr->ulValueLen); ++ CKYBuffer_DataIsEqual(cmp.getValue(), ++ (const CKYByte *)attr->pValue, attr->ulValueLen); + } + + class AttributeTypeMatch +@@ -45,14 +77,14 @@ class AttributeTypeMatch + }; + + PKCS11Object::PKCS11Object(unsigned long muscleObjID_,CK_OBJECT_HANDLE handle_) +- : muscleObjID(muscleObjID_), handle(handle_), label(NULL), name(NULL) ++ : muscleObjID(muscleObjID_), handle(handle_), label(NULL), name(NULL), keyType(unknown) + { + CKYBuffer_InitEmpty(&pubKey); + } + + PKCS11Object::PKCS11Object(unsigned long muscleObjID_, const CKYBuffer *data, + CK_OBJECT_HANDLE handle_) : muscleObjID(muscleObjID_), handle(handle_), +- label(NULL), name(NULL) ++ label(NULL), name(NULL), keyType(unknown) + { + CKYBuffer_InitEmpty(&pubKey); + +@@ -63,9 +95,98 @@ PKCS11Object::PKCS11Object(unsigned long + "PKCS #11 actual object id does not match stated id"); + } + if (type == 0) { +- parseOldObject(data); ++ parseOldObject(data); + } else if (type == 1) { +- parseNewObject(data); ++ parseNewObject(data); ++ } ++} ++ ++SecretKey::SecretKey(unsigned long muscleObjID_, CK_OBJECT_HANDLE handle_, CKYBuffer *secretKeyBuffer, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount) ++ : PKCS11Object(muscleObjID_, handle_) ++{ ++ static CK_OBJECT_CLASS objClass = CKO_SECRET_KEY; ++ static CK_KEY_TYPE keyType = CKK_GENERIC_SECRET; ++ static CK_BBOOL value = 0x1; ++ ++ if ( secretKeyBuffer == NULL) ++ return; ++ ++ /* Rifle through the input template */ ++ ++ CK_ATTRIBUTE_TYPE type; ++ CK_ATTRIBUTE attr; ++ CK_ULONG valueLength = 0; ++ ++ for(int i = 0; i < (int) ulAttributeCount; i++) { ++ attr = pTemplate[i]; ++ type = attr.type; ++ ++ if ( type == CKA_VALUE_LEN) { ++ //CK_ULONG ulValueLen = attr.ulValueLen; ++ valueLength = *((CK_ULONG *)attr.pValue); ++ } else { ++ ++ CKYBuffer val; ++ CKYBuffer_InitFromData(&val,(const CK_BYTE *) attr.pValue, attr.ulValueLen); ++ setAttribute( type, &val); ++ CKYBuffer_FreeData(&val); ++ } ++ } ++ ++ adjustToKeyValueLength( secretKeyBuffer, valueLength ); ++ ++ /* Fall backs. */ ++ ++ if(!attributeExists(CKA_CLASS)) ++ setAttributeULong(CKA_CLASS, objClass); ++ ++ if(!attributeExists(CKA_KEY_TYPE)) ++ setAttributeULong(CKA_KEY_TYPE, keyType); ++ ++ if(!attributeExists(CKA_TOKEN)) ++ setAttributeBool(CKA_TOKEN, value); ++ ++ if(!attributeExists(CKA_DERIVE)) ++ setAttributeBool(CKA_DERIVE, value); ++ ++ /* Actual value */ ++ setAttribute(CKA_VALUE, secretKeyBuffer); ++ ++} ++ ++void SecretKey::adjustToKeyValueLength(CKYBuffer * secretKeyBuffer,CK_ULONG valueLength) ++{ ++ const CK_LONG MAX_DIFF = 200; /* Put some bounds on this value */ ++ ++ if ( !secretKeyBuffer ) { ++ return; ++ } ++ ++ CKYBuffer scratch; ++ CK_ULONG actual_length = CKYBuffer_Size(secretKeyBuffer); ++ ++ CK_LONG diff = 0; ++ diff = (CK_LONG) valueLength - actual_length; ++ ++ if ( diff == 0 ) { ++ return; ++ } ++ ++ if ( diff > 0 && diff < MAX_DIFF ) { /*check for silly values */ ++ /* prepend with zeroes */ ++ CKYBuffer_InitFromLen(&scratch, diff); ++ CKYBuffer_AppendCopy(&scratch, secretKeyBuffer); ++ ++ CKYBuffer_FreeData(secretKeyBuffer); ++ CKYBuffer_InitFromCopy(secretKeyBuffer, &scratch); ++ CKYBuffer_FreeData(&scratch); ++ ++ } else if (diff < 0 ) { ++ /* truncate most significant bytes */ ++ CKYBuffer_InitFromData(&scratch, CKYBuffer_Data(secretKeyBuffer)-diff, valueLength); ++ CKYBuffer_FreeData(secretKeyBuffer); ++ CKYBuffer_InitFromCopy(secretKeyBuffer, &scratch); ++ CKYBuffer_FreeData(&scratch); + } + } + +@@ -95,29 +216,29 @@ PKCS11Object::parseOldObject(const CKYBu + attrib.setType(CKYBuffer_GetLong(data, idx)); + idx += 4; + unsigned int attrLen = CKYBuffer_GetShort(data, idx); +- idx += 2; ++ idx += 2; + if( attrLen > CKYBuffer_Size(data) +- || (idx + attrLen > CKYBuffer_Size(data)) ) { ++ || (idx + attrLen > CKYBuffer_Size(data)) ) { + throw PKCS11Exception(CKR_DEVICE_ERROR, + "Invalid attribute length %d\n", attrLen); + } +- /* these two types are ints, read them back from +- * the card in host order */ +- if ((attrib.getType() == CKA_CLASS) || +- (attrib.getType() == CKA_CERTIFICATE_TYPE) || +- (attrib.getType() == CKA_KEY_TYPE)) { +- /* ulongs are 4 bytes on the token, even if they are 8 or +- * more in the pkcs11 module */ +- if (attrLen != 4) { ++ /* these two types are ints, read them back from ++ * the card in host order */ ++ if ((attrib.getType() == CKA_CLASS) || ++ (attrib.getType() == CKA_CERTIFICATE_TYPE) || ++ (attrib.getType() == CKA_KEY_TYPE)) { ++ /* ulongs are 4 bytes on the token, even if they are 8 or ++ * more in the pkcs11 module */ ++ if (attrLen != 4) { + throw PKCS11Exception(CKR_DEVICE_ERROR, + "Invalid attribute length %d\n", attrLen); +- } +- CK_ULONG value = makeLEUInt(data,idx); ++ } ++ CK_ULONG value = makeLEUInt(data,idx); + +- attrib.setValue((const CKYByte *)&value, sizeof(CK_ULONG)); +- } else { +- attrib.setValue(CKYBuffer_Data(data)+idx, attrLen); +- } ++ attrib.setValue((const CKYByte *)&value, sizeof(CK_ULONG)); ++ } else { ++ attrib.setValue(CKYBuffer_Data(data)+idx, attrLen); ++ } + idx += attrLen; + attributes.push_back(attrib); + } +@@ -177,33 +298,33 @@ PKCS11Object::expandAttributes(unsigned + unsigned long i; + + if (!attributeExists(CKA_ID)) { +- PKCS11Attribute attrib; +- attrib.setType(CKA_ID); +- attrib.setValue(&cka_id, 1); ++ PKCS11Attribute attrib; ++ attrib.setType(CKA_ID); ++ attrib.setValue(&cka_id, 1); + attributes.push_back(attrib); + } + /* unpack the class */ + if (!attributeExists(CKA_CLASS)) { +- PKCS11Attribute attrib; +- attrib.setType(CKA_CLASS); +- attrib.setValue((CKYByte *)&objectType, sizeof(CK_ULONG)); ++ PKCS11Attribute attrib; ++ attrib.setType(CKA_CLASS); ++ attrib.setValue((CKYByte *)&objectType, sizeof(CK_ULONG)); + attributes.push_back(attrib); + } + + /* unpack the boolean flags. Note, the default mask is based on + * the class specified in fixedAttrs, not on the real class */ + for (i=1; i < sizeof(unsigned long)*8; i++) { +- unsigned long iMask = 1<< i; +- if ((mask & iMask) == 0) { +- continue; +- } +- if (attributeExists(boolType[i])) { +- continue; +- } +- PKCS11Attribute attrib; +- CKYByte bVal = (fixedAttrs & iMask) != 0; +- attrib.setType(boolType[i]); +- attrib.setValue(&bVal, 1); ++ unsigned long iMask = 1<< i; ++ if ((mask & iMask) == 0) { ++ continue; ++ } ++ if (attributeExists(boolType[i])) { ++ continue; ++ } ++ PKCS11Attribute attrib; ++ CKYByte bVal = (fixedAttrs & iMask) != 0; ++ attrib.setType(boolType[i]); ++ attrib.setValue(&bVal, 1); + attributes.push_back(attrib); + } + } +@@ -224,40 +345,40 @@ PKCS11Object::parseNewObject(const CKYBu + // load up the explicit attributes first + for (j=0, offset = 11; j < attributeCount && offset < size; j++) { + PKCS11Attribute attrib; +- CKYByte attributeDataType = CKYBuffer_GetChar(data, offset+4); +- unsigned int attrLen = 0; ++ CKYByte attributeDataType = CKYBuffer_GetChar(data, offset+4); ++ unsigned int attrLen = 0; + attrib.setType(CKYBuffer_GetLong(data, offset)); + offset += 5; + +- switch(attributeDataType) { +- case DATATYPE_STRING: +- attrLen = CKYBuffer_GetShort(data, offset); +- offset += 2; ++ switch(attributeDataType) { ++ case DATATYPE_STRING: ++ attrLen = CKYBuffer_GetShort(data, offset); ++ offset += 2; + if (attrLen > CKYBuffer_Size(data) +- || (offset + attrLen > CKYBuffer_Size(data)) ) { +- throw PKCS11Exception(CKR_DEVICE_ERROR, +- "Invalid attribute length %d\n", attrLen); ++ || (offset + attrLen > CKYBuffer_Size(data)) ) { ++ throw PKCS11Exception(CKR_DEVICE_ERROR, ++ "Invalid attribute length %d\n", attrLen); + } +- attrib.setValue(CKYBuffer_Data(data)+offset, attrLen); +- break; +- case DATATYPE_BOOL_FALSE: +- case DATATYPE_BOOL_TRUE: +- { +- CKYByte bval = attributeDataType & 1; +- attrib.setValue(&bval, 1); +- } +- break; +- case DATATYPE_INTEGER: +- { +- CK_ULONG value = CKYBuffer_GetLong(data, offset); +- attrLen = 4; +- attrib.setValue((const CKYByte *)&value, sizeof(CK_ULONG)); +- } +- break; +- default: +- throw PKCS11Exception(CKR_DEVICE_ERROR, +- "Invalid attribute Data Type %d\n", attributeDataType); +- } ++ attrib.setValue(CKYBuffer_Data(data)+offset, attrLen); ++ break; ++ case DATATYPE_BOOL_FALSE: ++ case DATATYPE_BOOL_TRUE: ++ { ++ CKYByte bval = attributeDataType & 1; ++ attrib.setValue(&bval, 1); ++ } ++ break; ++ case DATATYPE_INTEGER: ++ { ++ CK_ULONG value = CKYBuffer_GetLong(data, offset); ++ attrLen = 4; ++ attrib.setValue((const CKYByte *)&value, sizeof(CK_ULONG)); ++ } ++ break; ++ default: ++ throw PKCS11Exception(CKR_DEVICE_ERROR, ++ "Invalid attribute Data Type %d\n", attributeDataType); ++ } + offset += attrLen; + attributes.push_back(attrib); + } +@@ -276,7 +397,7 @@ static const CK_ATTRIBUTE rdr_templat + + bool + PKCS11Object::matchesTemplate(const CK_ATTRIBUTE_PTR pTemplate, +- CK_ULONG ulCount) ++ CK_ULONG ulCount) + const + { + unsigned int i; +@@ -285,10 +406,10 @@ PKCS11Object::matchesTemplate(const CK_A + + #if defined( NSS_HIDE_NONSTANDARD_OBJECTS ) + if (!ulCount) { +- // exclude MOZ reader objects from searches for all objects. +- // To find an MOZ reader object, one must search for it by +- // some matching attribute, such as class. +- iterator iter = find_if(attributes.begin(), attributes.end(), ++ // exclude MOZ reader objects from searches for all objects. ++ // To find an MOZ reader object, one must search for it by ++ // some matching attribute, such as class. ++ iterator iter = find_if(attributes.begin(), attributes.end(), + AttributeMatch(&rdr_template[0])); + return (iter == attributes.end()) ? true : false; + } +@@ -325,7 +446,7 @@ PKCS11Object::getAttribute(CK_ATTRIBUTE_ + AttributeTypeMatch(type)); + + if( iter == attributes.end() ) { +- return NULL; ++ return NULL; + } + return iter->getValue(); + } +@@ -349,8 +470,9 @@ PKCS11Object::getAttributeValue(CK_ATTRI + if( iter == attributes.end() ) { + // no attribute of this type + attrTypeInvalid = true; +- log->log("GetAttributeValue: invalid type 0x%08x on object %x\n", +- pTemplate[i].type, muscleObjID); ++ if ( log ) ++ log->log("GetAttributeValue: invalid type 0x%08x on object %x\n", ++ pTemplate[i].type, muscleObjID); + pTemplate[i].ulValueLen = (CK_ULONG)-1; + continue; + } +@@ -371,7 +493,7 @@ PKCS11Object::getAttributeValue(CK_ATTRI + // the buffer is large enough. return the value and set the exact + // length. + memcpy(pTemplate[i].pValue, CKYBuffer_Data(iter->getValue()), +- CKYBuffer_Size(iter->getValue())); ++ CKYBuffer_Size(iter->getValue())); + pTemplate[i].ulValueLen = CKYBuffer_Size(iter->getValue()); + } + +@@ -406,14 +528,14 @@ PKCS11Object::getLabel() + + // none found + if( iter == attributes.end() ) { +- return ""; ++ return ""; + } + + int size = CKYBuffer_Size(iter->getValue()); + + label = new char [ size + 1 ]; + if (!label) { +- return ""; ++ return ""; + } + memcpy(label, CKYBuffer_Data(iter->getValue()), size); + label[size] = 0; +@@ -431,13 +553,13 @@ PKCS11Object::getClass() + + // none found */ + if( iter == attributes.end() ) { +- return (CK_OBJECT_CLASS) -1; ++ return (CK_OBJECT_CLASS) -1; + } + + int size = CKYBuffer_Size(iter->getValue()); + + if (size != sizeof(objClass)) { +- return (CK_OBJECT_CLASS) -1; ++ return (CK_OBJECT_CLASS) -1; + } + + memcpy(&objClass, CKYBuffer_Data(iter->getValue()), size); +@@ -453,7 +575,7 @@ PKCS11Object::setAttribute(CK_ATTRIBUTE_ + iter = find_if(attributes.begin(), attributes.end(), + AttributeTypeMatch(type)); + if( iter != attributes.end() ) { +- iter->setValue( CKYBuffer_Data(value), CKYBuffer_Size(value)); ++ iter->setValue( CKYBuffer_Data(value), CKYBuffer_Size(value)); + } else { + attributes.push_back(PKCS11Attribute(type, value)); + } +@@ -505,9 +627,15 @@ dataStart(const CKYByte *buf, unsigned i + unsigned char tag; + unsigned int used_length= 0; + ++ *data_length = 0; /* make sure data_length is zero on failure */ ++ + if(!buf) { + return NULL; + } ++ /* there must be at least 2 bytes */ ++ if (length < 2) { ++ return NULL; ++ } + + tag = buf[used_length++]; + +@@ -521,15 +649,22 @@ dataStart(const CKYByte *buf, unsigned i + if (*data_length&0x80) { + int len_count = *data_length & 0x7f; + ++ if (len_count+used_length > length) { ++ return NULL; ++ } ++ + *data_length = 0; + + while (len_count-- > 0) { + *data_length = (*data_length << 8) | buf[used_length++]; + } + } ++ /* paranoia, can't happen */ ++ if (length < used_length) { ++ return NULL; ++ } + + if (*data_length > (length-used_length) ) { +- *data_length = length-used_length; + return NULL; + } + if (includeTag) *data_length += used_length; +@@ -542,16 +677,158 @@ unwrapBitString(const CKYByte *buf, unsi + { + /* for RSA, bit string always has byte number of bits */ + if (buf[0] != 0) { +- return NULL; ++ return NULL; + } + if (len < 1) { +- return NULL; ++ return NULL; + } + *retLen = len -1; + return buf+1; + } + + static SECStatus ++GetECKeyFieldItems(const CKYByte *spki_data,unsigned int spki_length, ++ CCItem *point, CCItem *params) ++{ ++ const CKYByte *buf = spki_data; ++ unsigned int buf_length = spki_length; ++ const CKYByte *algid; ++ unsigned int algidlen; ++ const CKYByte *dummy; ++ unsigned int dummylen; ++ ++ if (!point || !params || !buf) ++ return SECFailure; ++ ++ point->data = NULL; ++ point->len = 0; ++ params->data = NULL; ++ params->len = 0; ++ ++ /* unwrap the algorithm id */ ++ dummy = dataStart(buf,buf_length,&dummylen,false); ++ if (dummy == NULL) return SECFailure; ++ buf_length -= (dummy-buf) + dummylen; ++ buf = dummy + dummylen; ++ /* unwrpped value is in dummy */ ++ algid = dummy; ++ algidlen = dummylen; ++ /* skip past algid oid */ ++ dummy = dataStart(algid, algidlen, &dummylen, false); ++ if (dummy == NULL) return SECFailure; ++ algidlen -= (dummy-algid) + dummylen; ++ algid = dummy + dummylen; ++ params->data = algid; ++ params->len = algidlen; ++ ++ /* unwrap the public key info */ ++ buf = dataStart(buf,buf_length,&buf_length,false); ++ if (buf == NULL) return SECFailure; ++ buf = unwrapBitString(buf,buf_length,&buf_length); ++ if (buf == NULL) return SECFailure; ++ ++ point->data = buf; ++ point->len = buf_length; ++ ++ if(point->data == NULL) return SECFailure; ++ ++ return SECSuccess; ++} ++ ++static bool ++GetKeyOIDMatches(const CKYByte *spki_data, unsigned int length, const CKYByte *oid_data) ++{ ++ bool ret = TRUE; ++ ++ if( spki_data == NULL || oid_data == NULL) { ++ return FALSE; ++ } ++ ++ for ( int i = 0 ; i < (int) length ; i++) { ++ if (spki_data[i] != oid_data[i]) { ++ ret = FALSE; ++ break; ++ } ++ ++ } ++ ++ return ret; ++} ++ ++static SECStatus ++GetKeyAlgorithmId(const CKYByte *spki_data, unsigned int spki_length, ++ CCItem *algorithmId) ++{ ++ ++ const CKYByte *buf = spki_data; ++ unsigned int buf_length = spki_length; ++ ++ if ( algorithmId == NULL) return SECFailure; ++ ++ /* objtain the algorithm id */ ++ algorithmId->data = dataStart(buf,buf_length,&algorithmId->len,false); ++ if (algorithmId->data == NULL) return SECFailure; ++ ++ return SECSuccess; ++ ++} ++ ++static PKCS11Object::KeyType ++GetKeyTypeFromSPKI(const CKYBuffer *key) ++{ ++ CCItem algIdItem; ++ SECStatus ret = GetKeyAlgorithmId(CKYBuffer_Data(key), ++ CKYBuffer_Size(key),&algIdItem); ++ PKCS11Object::KeyType foundType = PKCS11Object::unknown; ++ ++ if ( ret != SECSuccess ) { ++ throw PKCS11Exception(CKR_FUNCTION_FAILED, ++ "Failed to decode key algorithm ID."); ++ } ++ ++ unsigned int length = 0; ++ const CKYByte *keyData = NULL; ++ ++ /* Get actual oid buffer */ ++ ++ keyData = dataStart(algIdItem.data,algIdItem.len,&length, false); ++ if (keyData == NULL) { ++ throw PKCS11Exception(CKR_FUNCTION_FAILED, ++ "Failed to decode key algorithm ID."); ++ } ++ ++ bool match = FALSE; ++ ++ /* Check for outrageous length */ ++ ++ if ( length <= 3 || length >= algIdItem.len) { ++ throw PKCS11Exception(CKR_FUNCTION_FAILED, ++ "Failed to decode key algorithm ID."); ++ } ++ /* check for RSA */ ++ ++ match = GetKeyOIDMatches(keyData, length, rsaOID); ++ ++ if ( match == TRUE ) { ++ foundType = PKCS11Object::rsa; ++ } else { ++ /* check for ECC */ ++ match = GetKeyOIDMatches(keyData, length, eccOID); ++ ++ if ( match == TRUE ) { ++ foundType = PKCS11Object::ecc; ++ } ++ ++ } ++ ++ if ( foundType == PKCS11Object::unknown) { ++ throw PKCS11Exception(CKR_FUNCTION_FAILED, ++ "Failed to decode key algorithm ID."); ++ } ++ return foundType; ++} ++ ++static SECStatus + GetKeyFieldItems(const CKYByte *spki_data,unsigned int spki_length, + CCItem *modulus, CCItem *exponent) + { +@@ -596,7 +873,7 @@ GetKeyFields(const CKYBuffer *spki, CKYB + CCItem modulusItem, exponentItem; + + rv = GetKeyFieldItems(CKYBuffer_Data(spki), CKYBuffer_Size(spki), +- &modulusItem, &exponentItem); ++ &modulusItem, &exponentItem); + + if( rv != SECSuccess ) { + throw PKCS11Exception(CKR_FUNCTION_FAILED, +@@ -607,6 +884,29 @@ GetKeyFields(const CKYBuffer *spki, CKYB + CKYBuffer_Replace(exponent, 0, exponentItem.data, exponentItem.len); + } + ++static void ++GetECKeyFields(const CKYBuffer *spki, CKYBuffer *point, CKYBuffer *params) ++{ ++ SECStatus rv; ++ CCItem pointItem, paramsItem; ++ ++ if (spki == NULL || point == NULL || params == NULL) { ++ throw PKCS11Exception(CKR_FUNCTION_FAILED, ++ "Failed to decode certificate Subject Public KeyInfo!"); ++ } ++ ++ rv = GetECKeyFieldItems(CKYBuffer_Data(spki), CKYBuffer_Size(spki), ++ &pointItem, ¶msItem); ++ ++ if( rv != SECSuccess ) { ++ throw PKCS11Exception(CKR_FUNCTION_FAILED, ++ "Failed to decode certificate Subject Public Key Info!"); ++ } ++ ++ CKYBuffer_Replace(point, 0, pointItem.data, pointItem.len); ++ CKYBuffer_Replace(params, 0, paramsItem.data, paramsItem.len); ++} ++ + Key::Key(unsigned long muscleObjID, const CKYBuffer *data, + CK_OBJECT_HANDLE handle) : PKCS11Object(muscleObjID, data, handle) + { +@@ -616,22 +916,41 @@ Key::Key(unsigned long muscleObjID, cons + CKYBuffer_InitEmpty(&empty); + + if ((objClass == CKO_PUBLIC_KEY) || (objClass == CKO_PRIVATE_KEY)) { +- /* only CKK_RSA is supported */ +- setAttributeULong(CKA_KEY_TYPE, CKK_RSA); ++ //we may know already what type of key this is. ++ if (attributeExists(CKA_KEY_TYPE)) { ++ CK_ULONG type = 0; ++ CK_ATTRIBUTE aTemplate = {CKA_KEY_TYPE, &type, sizeof(CK_ULONG)}; ++ ++ getAttributeValue(&aTemplate, 1, NULL); ++ ++ if (type == 0x3) { ++ setKeyType(ecc); ++ setAttributeULong(CKA_KEY_TYPE, CKK_EC); ++ } else { ++ setKeyType(rsa); ++ setAttributeULong(CKA_KEY_TYPE, CKK_RSA); ++ } ++ } else { ++ /* default to rsa */ ++ setKeyType(rsa); ++ setAttributeULong(CKA_KEY_TYPE, CKK_RSA); ++ } ++ ++ // Could be RSA or ECC + } else if (objClass == CKO_SECRET_KEY) { +- if (!attributeExists(CKA_LABEL)) { +- setAttribute(CKA_LABEL, &empty); +- } +- if (!attributeExists(CKA_KEY_TYPE)) { +- /* default to DES3 */ +- setAttributeULong(CKA_KEY_TYPE, CKK_DES3); +- } ++ if (!attributeExists(CKA_LABEL)) { ++ setAttribute(CKA_LABEL, &empty); ++ } ++ if (!attributeExists(CKA_KEY_TYPE)) { ++ /* default to DES3 */ ++ setAttributeULong(CKA_KEY_TYPE, CKK_DES3); ++ } + } + if (!attributeExists(CKA_START_DATE)) { +- setAttribute(CKA_START_DATE, &empty); ++ setAttribute(CKA_START_DATE, &empty); + } + if (!attributeExists(CKA_END_DATE)) { +- setAttribute(CKA_END_DATE, &empty); ++ setAttribute(CKA_END_DATE, &empty); + } + } + +@@ -640,32 +959,59 @@ Key::completeKey(const PKCS11Object &cer + { + // infer key attributes from cert + bool modulusExists, exponentExists; +- CKYBuffer modulus; CKYBuffer_InitEmpty(&modulus); +- CKYBuffer exponent; CKYBuffer_InitEmpty(&exponent); ++ bool pointExists, paramsExists; ++ ++ PKCS11Object::KeyType keyType; ++ const CKYBuffer *key = cert.getPubKey(); + + if (!attributeExists(CKA_LABEL)) { +- setAttribute(CKA_LABEL, cert.getAttribute(CKA_LABEL)); ++ setAttribute(CKA_LABEL, cert.getAttribute(CKA_LABEL)); + } ++ ++ CKYBuffer param1; CKYBuffer_InitEmpty(¶m1); ++ CKYBuffer param2; CKYBuffer_InitEmpty(¶m2); + try { +- modulusExists = attributeExists(CKA_MODULUS); +- exponentExists = attributeExists(CKA_PUBLIC_EXPONENT); +- if (!modulusExists || !exponentExists) { +- const CKYBuffer *key = cert.getPubKey(); +- GetKeyFields(key, &modulus, &exponent); +- if (!modulusExists) { +- setAttribute(CKA_MODULUS, &modulus); +- } +- if (!exponentExists) { +- setAttribute(CKA_PUBLIC_EXPONENT, &exponent); +- } +- } ++ keyType = GetKeyTypeFromSPKI(key); ++ setKeyType(keyType); ++ ++ switch (keyType) { ++ case rsa: ++ modulusExists = attributeExists(CKA_MODULUS); ++ exponentExists = attributeExists(CKA_PUBLIC_EXPONENT); ++ if (!modulusExists || !exponentExists) { ++ GetKeyFields(key, ¶m1, ¶m2); ++ if (!modulusExists) { ++ setAttribute(CKA_MODULUS, ¶m1); ++ } ++ if (!exponentExists) { ++ setAttribute(CKA_PUBLIC_EXPONENT, ¶m2); ++ } ++ } ++ break; ++ case ecc: ++ pointExists = attributeExists(CKA_EC_POINT); ++ paramsExists = attributeExists(CKA_EC_PARAMS); ++ ++ if (!pointExists || !paramsExists) { ++ GetECKeyFields(key, ¶m1, ¶m2); ++ if (!pointExists) { ++ setAttribute(CKA_EC_POINT, ¶m1); ++ } ++ if (!paramsExists) { ++ setAttribute(CKA_EC_PARAMS, ¶m2); ++ } ++ } ++ break; ++ default: ++ break; ++ } + } catch (PKCS11Exception &e) { +- CKYBuffer_FreeData(&modulus); +- CKYBuffer_FreeData(&exponent); +- throw e; ++ CKYBuffer_FreeData(¶m1); ++ CKYBuffer_FreeData(¶m2); ++ throw e; + } +- CKYBuffer_FreeData(&modulus); +- CKYBuffer_FreeData(&exponent); ++ CKYBuffer_FreeData(¶m1); ++ CKYBuffer_FreeData(¶m2); + } + + static SECStatus +@@ -737,14 +1083,14 @@ GetCertFieldItems(const CKYByte *dercert + + static void + GetCertFields(const CKYBuffer *derCert, CKYBuffer *derSerial, +- CKYBuffer *derSubject, CKYBuffer *derIssuer, CKYBuffer *subjectKey) ++ CKYBuffer *derSubject, CKYBuffer *derIssuer, CKYBuffer *subjectKey) + { + SECStatus rv; + CCItem issuerItem, serialItem, derSerialItem, subjectItem, + validityItem, subjectKeyItem; + + rv = GetCertFieldItems(CKYBuffer_Data(derCert), CKYBuffer_Size(derCert), +- &issuerItem, &serialItem, &derSerialItem, &subjectItem, &validityItem, ++ &issuerItem, &serialItem, &derSerialItem, &subjectItem, &validityItem, + &subjectKeyItem); + + if( rv != SECSuccess ) { +@@ -769,50 +1115,50 @@ Cert::Cert(unsigned long muscleObjID, co + CK_ULONG certTypeValue = CKC_X_509; + + CKYBuffer_InitFromData(&certType, (CKYByte *)&certTypeValue, +- sizeof(certTypeValue)); ++ sizeof(certTypeValue)); + CKYBuffer_Resize(&pubKey,0); + + try { +- setAttribute(CKA_CERTIFICATE_TYPE, &certType); ++ setAttribute(CKA_CERTIFICATE_TYPE, &certType); + +- if (!attributeExists(CKA_VALUE)) { +- if (derCert) { +- setAttribute(CKA_VALUE, derCert); +- } else { +- throw PKCS11Exception(CKR_DEVICE_ERROR, +- "Missing certificate data from token"); +- } +- } ++ if (!attributeExists(CKA_VALUE)) { ++ if (derCert) { ++ setAttribute(CKA_VALUE, derCert); ++ } else { ++ throw PKCS11Exception(CKR_DEVICE_ERROR, ++ "Missing certificate data from token"); ++ } ++ } + +- if (!derCert) { +- derCert = getAttribute(CKA_VALUE); +- if (!derCert) { +- // paranoia, should never happen since we verify the +- // attribute exists above +- throw PKCS11Exception(CKR_DEVICE_ERROR, +- "Missing certificate data from token"); +- } +- } ++ if (!derCert) { ++ derCert = getAttribute(CKA_VALUE); ++ if (!derCert) { ++ // paranoia, should never happen since we verify the ++ // attribute exists above ++ throw PKCS11Exception(CKR_DEVICE_ERROR, ++ "Missing certificate data from token"); ++ } ++ } + +- // infer cert attributes ++ // infer cert attributes + +- GetCertFields(derCert, &derSerial, &derSubject, &derIssuer, &pubKey); ++ GetCertFields(derCert, &derSerial, &derSubject, &derIssuer, &pubKey); + +- if (!attributeExists(CKA_SERIAL_NUMBER)) { +- setAttribute(CKA_SERIAL_NUMBER, &derSerial); +- } +- if (!attributeExists(CKA_SUBJECT)) { +- setAttribute(CKA_SUBJECT, &derSubject); +- } +- if (!attributeExists(CKA_ISSUER)) { +- setAttribute(CKA_ISSUER, &derIssuer); +- } ++ if (!attributeExists(CKA_SERIAL_NUMBER)) { ++ setAttribute(CKA_SERIAL_NUMBER, &derSerial); ++ } ++ if (!attributeExists(CKA_SUBJECT)) { ++ setAttribute(CKA_SUBJECT, &derSubject); ++ } ++ if (!attributeExists(CKA_ISSUER)) { ++ setAttribute(CKA_ISSUER, &derIssuer); ++ } + } catch (PKCS11Exception &e) { +- CKYBuffer_FreeData(&certType); +- CKYBuffer_FreeData(&derSerial); +- CKYBuffer_FreeData(&derSubject); +- CKYBuffer_FreeData(&derIssuer); +- throw e; ++ CKYBuffer_FreeData(&certType); ++ CKYBuffer_FreeData(&derSerial); ++ CKYBuffer_FreeData(&derSubject); ++ CKYBuffer_FreeData(&derIssuer); ++ throw e; + } + CKYBuffer_FreeData(&certType); + CKYBuffer_FreeData(&derSerial); +@@ -822,7 +1168,7 @@ Cert::Cert(unsigned long muscleObjID, co + + Reader::Reader(unsigned long muscleObjID, CK_OBJECT_HANDLE handle, + const char *reader, const CKYBuffer *cardATR, bool isCoolkey) : +- PKCS11Object(muscleObjID, handle) ++ PKCS11Object(muscleObjID, handle) + { + setAttributeULong(CKA_CLASS, CKO_MOZ_READER); + setAttribute(CKA_LABEL, reader); +@@ -833,9 +1179,10 @@ Reader::Reader(unsigned long muscleObjID + setAttribute(CKA_MOZ_ATR, cardATR); + } + ++ + CACPrivKey::CACPrivKey(CKYByte instance, const PKCS11Object &cert) : +- PKCS11Object( ((int)'k') << 24 | ((int)instance+'0') << 16, +- instance | 0x400) ++ PKCS11Object( ((int)'k') << 24 | ((int)instance+'0') << 16, ++ instance | 0x400) + { + CKYBuffer id; + CKYBuffer empty; +@@ -844,7 +1191,7 @@ CACPrivKey::CACPrivKey(CKYByte instance, + /* So we know what the key is supposed to be used for based on + * the instance */ + if (instance == 2) { +- decrypt = TRUE; ++ decrypt = TRUE; + } + + CKYBuffer_InitEmpty(&empty); +@@ -863,33 +1210,52 @@ CACPrivKey::CACPrivKey(CKYByte instance, + setAttributeBool(CKA_LOCAL, TRUE); + setAttributeULong(CKA_KEY_TYPE, CKK_RSA); + +- setAttributeBool(CKA_DECRYPT, decrypt); + setAttributeBool(CKA_SIGN, !decrypt); + setAttributeBool(CKA_SIGN_RECOVER, !decrypt); + setAttributeBool(CKA_UNWRAP, FALSE); + setAttributeBool(CKA_SENSITIVE, TRUE); + setAttributeBool(CKA_EXTRACTABLE, FALSE); + +- CKYBuffer modulus; CKYBuffer_InitEmpty(&modulus); +- CKYBuffer exponent; CKYBuffer_InitEmpty(&exponent); ++ CKYBuffer param1; CKYBuffer_InitEmpty(¶m1); ++ CKYBuffer param2; CKYBuffer_InitEmpty(¶m2); + + try { +- const CKYBuffer *key = cert.getPubKey(); +- GetKeyFields(key, &modulus, &exponent); +- setAttribute(CKA_MODULUS, &modulus); +- setAttribute(CKA_PUBLIC_EXPONENT, &exponent); +- } catch (PKCS11Exception &e) { +- CKYBuffer_FreeData(&modulus); +- CKYBuffer_FreeData(&exponent); +- throw e; +- } +- CKYBuffer_FreeData(&modulus); +- CKYBuffer_FreeData(&exponent); ++ const CKYBuffer *key = cert.getPubKey(); ++ keyType = GetKeyTypeFromSPKI(key); ++ setKeyType(keyType); ++ ++ switch (keyType) { ++ case rsa: ++ GetKeyFields(key, ¶m1, ¶m2); ++ setAttribute(CKA_MODULUS, ¶m1); ++ setAttribute(CKA_PUBLIC_EXPONENT, ¶m2); ++ setAttributeULong(CKA_KEY_TYPE, CKK_RSA); ++ setAttributeBool(CKA_DECRYPT, decrypt); ++ setAttributeBool(CKA_DERIVE, FALSE); ++ break; ++ case ecc: ++ GetECKeyFields(key, ¶m1, ¶m2); ++ setAttribute(CKA_EC_POINT, ¶m1); ++ setAttribute(CKA_EC_PARAMS, ¶m2); ++ setAttributeULong(CKA_KEY_TYPE, CKK_EC); ++ setAttributeBool(CKA_DECRYPT, FALSE); ++ setAttributeBool(CKA_DERIVE, decrypt); ++ break; ++ default: ++ break; ++ } ++ } catch (PKCS11Exception &e) { ++ CKYBuffer_FreeData(¶m1); ++ CKYBuffer_FreeData(¶m2); ++ throw e; ++ } ++ CKYBuffer_FreeData(¶m1); ++ CKYBuffer_FreeData(¶m2); + } + + CACPubKey::CACPubKey(CKYByte instance, const PKCS11Object &cert) : +- PKCS11Object( ((int)'k') << 24 | ((int)(instance+'5')) << 16, +- instance | 0x500) ++ PKCS11Object( ((int)'k') << 24 | ((int)(instance+'5')) << 16, ++ instance | 0x500) + { + CKYBuffer id; + CKYBuffer empty; +@@ -898,7 +1264,7 @@ CACPubKey::CACPubKey(CKYByte instance, c + /* So we know what the key is supposed to be used for based on + * the instance */ + if (instance == 2) { +- encrypt = TRUE; ++ encrypt = TRUE; + } + + CKYBuffer_InitEmpty(&empty); +@@ -915,34 +1281,49 @@ CACPubKey::CACPubKey(CKYByte instance, c + setAttribute(CKA_END_DATE, &empty); + setAttributeBool(CKA_DERIVE, FALSE); + setAttributeBool(CKA_LOCAL, TRUE); +- setAttributeULong(CKA_KEY_TYPE, CKK_RSA); + + setAttributeBool(CKA_ENCRYPT, encrypt); + setAttributeBool(CKA_VERIFY, !encrypt); + setAttributeBool(CKA_VERIFY_RECOVER, !encrypt); + setAttributeBool(CKA_WRAP, FALSE); + +- CKYBuffer modulus; CKYBuffer_InitEmpty(&modulus); +- CKYBuffer exponent; CKYBuffer_InitEmpty(&exponent); ++ CKYBuffer param1; CKYBuffer_InitEmpty(¶m1); ++ CKYBuffer param2; CKYBuffer_InitEmpty(¶m2); + + try { +- const CKYBuffer *key = cert.getPubKey(); +- GetKeyFields(key, &modulus, &exponent); +- setAttribute(CKA_MODULUS, &modulus); +- setAttribute(CKA_PUBLIC_EXPONENT, &exponent); +- } catch (PKCS11Exception &e) { +- CKYBuffer_FreeData(&modulus); +- CKYBuffer_FreeData(&exponent); +- throw e; +- } +- CKYBuffer_FreeData(&modulus); +- CKYBuffer_FreeData(&exponent); ++ const CKYBuffer *key = cert.getPubKey(); ++ keyType = GetKeyTypeFromSPKI(key); ++ setKeyType(keyType); ++ ++ switch (keyType) { ++ case rsa: ++ GetKeyFields(key, ¶m1, ¶m2); ++ setAttribute(CKA_MODULUS, ¶m1); ++ setAttribute(CKA_PUBLIC_EXPONENT, ¶m2); ++ setAttributeULong(CKA_KEY_TYPE, CKK_RSA); ++ break; ++ case ecc: ++ GetECKeyFields(key, ¶m1, ¶m2); ++ setAttribute(CKA_EC_POINT, ¶m1); ++ setAttribute(CKA_EC_PARAMS, ¶m2); ++ setAttributeULong(CKA_KEY_TYPE, CKK_EC); ++ break; ++ default: ++ break; ++ } ++ } catch (PKCS11Exception &e) { ++ CKYBuffer_FreeData(¶m1); ++ CKYBuffer_FreeData(¶m2); ++ throw e; ++ } ++ CKYBuffer_FreeData(¶m1); ++ CKYBuffer_FreeData(¶m2); + } + + static const char *CAC_Label[] = { +- "CAC ID Certificate", +- "CAC Email Signature Certificate", +- "CAC Email Encryption Certificate", ++ "CAC ID Certificate", ++ "CAC Email Signature Certificate", ++ "CAC Email Encryption Certificate", + }; + + static const unsigned char CN_DATA[] = { 0x55, 0x4, 0x3 }; +@@ -959,39 +1340,43 @@ GetCN(const CKYByte *dn, unsigned int dn + if (buf == NULL) return SECFailure; + + while (buf_length) { +- const CKYByte *name; +- unsigned int name_length; +- const CKYByte *oid; +- unsigned int oid_length; +- +- /* unwrap the set */ +- name = dataStart(buf, buf_length, &name_length, false); ++ const CKYByte *name; ++ unsigned int name_length; ++ const CKYByte *oid; ++ unsigned int oid_length; ++ ++ /* unwrap the set */ ++ name = dataStart(buf, buf_length, &name_length, false); ++ if (name == NULL) return SECFailure; + + /* advance to next set */ +- buf_length -= (name-buf) + name_length; +- buf = name + name_length; ++ buf_length -= (name-buf) + name_length; ++ buf = name + name_length; + +- /* unwrap the Sequence */ +- name = dataStart(name, name_length, &name_length, false); ++ /* unwrap the Sequence */ ++ name = dataStart(name, name_length, &name_length, false); ++ if (name == NULL) return SECFailure; + + /* unwrap the oid */ +- oid = dataStart(name, name_length, &oid_length, false); ++ oid = dataStart(name, name_length, &oid_length, false); ++ if (oid == NULL) return SECFailure; + +- /* test the oid */ +- if (oid_length != CN_LENGTH) { +- continue; +- } +- if (memcmp(oid, CN_DATA, CN_LENGTH) != 0) { +- continue; +- } ++ /* test the oid */ ++ if (oid_length != CN_LENGTH) { ++ continue; ++ } ++ if (memcmp(oid, CN_DATA, CN_LENGTH) != 0) { ++ continue; ++ } + +- /* advance to CN */ +- name_length -= (oid-name) + oid_length; +- name = oid + oid_length; +- +- /* unwrap the CN */ +- cn->data = dataStart(name, name_length, &cn->len, false); +- return SECSuccess; ++ /* advance to CN */ ++ name_length -= (oid-name) + oid_length; ++ name = oid + oid_length; ++ ++ /* unwrap the CN */ ++ cn->data = dataStart(name, name_length, &cn->len, false); ++ if (cn->data == NULL) return SECFailure; ++ return SECSuccess; + } + return SECFailure; + } +@@ -1006,11 +1391,11 @@ GetUserName(const CKYBuffer *dn) + rv = GetCN(CKYBuffer_Data(dn), CKYBuffer_Size(dn) , &cn); + + if( rv != SECSuccess ) { +- return NULL; ++ return NULL; + } + string = new char [ cn.len + 1 ]; + if (string == NULL) { +- return NULL; ++ return NULL; + } + memcpy(string, cn.data, cn.len); + string[cn.len] = 0; +@@ -1018,8 +1403,8 @@ GetUserName(const CKYBuffer *dn) + } + + CACCert::CACCert(CKYByte instance, const CKYBuffer *derCert) : +- PKCS11Object( ((int)'c') << 24 | ((int)instance+'0') << 16, +- instance | 0x600) ++ PKCS11Object( ((int)'c') << 24 | ((int)instance+'0') << 16, ++ instance | 0x600) + { + CKYBuffer id; + CKYBuffer empty; +@@ -1028,7 +1413,7 @@ CACCert::CACCert(CKYByte instance, const + /* So we know what the key is supposed to be used for based on + * the instance */ + if (instance == 2) { +- decrypt = TRUE; ++ decrypt = TRUE; + } + + CKYBuffer_InitEmpty(&empty); +@@ -1050,19 +1435,19 @@ CACCert::CACCert(CKYByte instance, const + CKYBuffer_Resize(&pubKey,0); + + try { +- setAttribute(CKA_VALUE, derCert); +- // infer cert attributes ++ setAttribute(CKA_VALUE, derCert); ++ // infer cert attributes + +- GetCertFields(derCert, &derSerial, &derSubject, &derIssuer, &pubKey); ++ GetCertFields(derCert, &derSerial, &derSubject, &derIssuer, &pubKey); + +- setAttribute(CKA_SERIAL_NUMBER, &derSerial); +- setAttribute(CKA_SUBJECT, &derSubject); +- setAttribute(CKA_ISSUER, &derIssuer); ++ setAttribute(CKA_SERIAL_NUMBER, &derSerial); ++ setAttribute(CKA_SUBJECT, &derSubject); ++ setAttribute(CKA_ISSUER, &derIssuer); + } catch (PKCS11Exception &e) { +- CKYBuffer_FreeData(&derSerial); +- CKYBuffer_FreeData(&derSubject); +- CKYBuffer_FreeData(&derIssuer); +- throw e; ++ CKYBuffer_FreeData(&derSerial); ++ CKYBuffer_FreeData(&derSubject); ++ CKYBuffer_FreeData(&derIssuer); ++ throw e; + } + + name = GetUserName(&derSubject); /* adopt */ +@@ -1070,3 +1455,100 @@ CACCert::CACCert(CKYByte instance, const + CKYBuffer_FreeData(&derSubject); + CKYBuffer_FreeData(&derIssuer); + } ++ ++DEREncodedSignature::DEREncodedSignature(const CKYBuffer *derSig) ++{ ++ ++ CKYBuffer_InitEmpty(&derEncodedSignature); ++ CKYBuffer_InitFromCopy(&derEncodedSignature, derSig); ++} ++ ++DEREncodedSignature::~DEREncodedSignature() ++{ ++ CKYBuffer_FreeData(&derEncodedSignature); ++} ++ ++int DEREncodedSignature::getRawSignature(CKYBuffer *rawSig, ++ unsigned int keySize) ++{ ++ const CKYByte *buf = NULL; ++ ++ if (rawSig == NULL) { ++ return -1; ++ } ++ ++ if (CKYBuffer_Size(&derEncodedSignature) == 0) { ++ return -1; ++ } ++ ++ CKYBuffer_Zero(rawSig); ++ ++ unsigned int seq_length = 0; ++ unsigned int expected_sig_len = ( (keySize + 7) / 8 ) * 2 ; ++ unsigned int expected_piece_size = expected_sig_len / 2 ; ++ ++ /* unwrap the sequence */ ++ buf = dataStart(CKYBuffer_Data(&derEncodedSignature), CKYBuffer_Size(&derEncodedSignature),&seq_length, false); ++ ++ if (buf == NULL) return -1; ++ ++ // unwrap first multi byte integer ++ ++ unsigned int int_length = 0; ++ const CKYByte *int1Buf = NULL; ++ const CKYByte *int2Buf = NULL; ++ ++ int1Buf = dataStart(buf, seq_length, &int_length, false ); ++ ++ if (int1Buf == NULL) return -1; ++ //advance to next entry ++ ++ if (int_length > expected_piece_size) { ++ ++ unsigned int diff = int_length - expected_piece_size ; ++ ++ /* Make sure we are chopping off zeroes ++ Otherwise give up. */ ++ ++ for (int i = 0 ; i < (int) diff ; i++) { ++ if ( int1Buf[i] != 0) ++ return -1; ++ } ++ ++ int_length -= diff; ++ int1Buf += diff; ++ ++ } ++ ++ seq_length -= (int1Buf -buf) + int_length; ++ buf = int1Buf + int_length; ++ ++ // unwrap second multi byte integer ++ ++ unsigned int second_int_length = 0; ++ ++ int2Buf = dataStart(buf, seq_length, &second_int_length, false); ++ ++ if (int2Buf == NULL) return -1; ++ ++ ++ if (second_int_length > expected_piece_size) { ++ unsigned int diff = second_int_length - expected_piece_size ; ++ ++ /* Make sure we are chopping off zeroes ++ Otherwise give up. */ ++ ++ for (int i = 0 ; i < (int) diff ; i++) { ++ if ( int2Buf[i] != 0) ++ return -1; ++ } ++ ++ second_int_length -= diff; ++ int2Buf += diff; ++ } ++ ++ CKYBuffer_AppendData(rawSig, int1Buf, int_length); ++ CKYBuffer_AppendData(rawSig, int2Buf, second_int_length); ++ ++ return CKYSUCCESS; ++} +diff -up ./src/coolkey/object.h.piv-ecc ./src/coolkey/object.h +--- ./src/coolkey/object.h.piv-ecc 2013-09-08 15:50:33.081428035 -0700 ++++ ./src/coolkey/object.h 2013-09-08 15:50:33.121428706 -0700 +@@ -49,7 +49,7 @@ class PKCS11Attribute { + CKYBuffer_Size(&cpy.value)); + return *this; + } +- PKCS11Attribute() { CKYBuffer_InitEmpty(&value); } ++ PKCS11Attribute() : type(0){ CKYBuffer_InitEmpty(&value); } + PKCS11Attribute(CK_ATTRIBUTE_TYPE type_, const CKYBuffer *value_) + : type(type_) { CKYBuffer_InitFromCopy(&value, value_); } + ~PKCS11Attribute() { CKYBuffer_FreeData(&value); } +@@ -57,6 +57,11 @@ class PKCS11Attribute { + + class PKCS11Object { + public: ++ enum KeyType { ++ rsa, ++ ecc, ++ unknown ++ }; + + typedef list AttributeList; + typedef AttributeList::iterator AttributeIter; +@@ -75,18 +80,20 @@ class PKCS11Object { + PKCS11Object &operator=(PKCS11Object &cpy) { return *this; } //Disallow + + protected : +- CKYBuffer pubKey; + char *name; ++ KeyType keyType; ++ CKYBuffer pubKey; + + public: + PKCS11Object(unsigned long muscleObjID, CK_OBJECT_HANDLE handle); + PKCS11Object(unsigned long muscleObjID, const CKYBuffer *data, + CK_OBJECT_HANDLE handle); +- ~PKCS11Object() { delete [] label; delete [] name; CKYBuffer_FreeData(&pubKey); } ++ ~PKCS11Object() { delete label; delete name; CKYBuffer_FreeData(&pubKey); ++ attributes.clear(); } + + PKCS11Object(const PKCS11Object& cpy) : + attributes(cpy.attributes), muscleObjID(cpy.muscleObjID), +- handle(cpy.handle), label(NULL), name(NULL) { ++ handle(cpy.handle), label(NULL), name(NULL), keyType(cpy.keyType) { + CKYBuffer_InitFromCopy(&pubKey,&cpy.pubKey); } + + +@@ -116,14 +123,15 @@ class PKCS11Object { + const CKYBuffer *getPubKey(void) const { + return &pubKey; + } ++ ++ KeyType getKeyType() const { return keyType;} ++ void setKeyType(KeyType theType) { keyType = theType; } + }; + + class Key : public PKCS11Object { +- + public: + Key(unsigned long muscleObjID, const CKYBuffer *data, CK_OBJECT_HANDLE handle); + void completeKey(const PKCS11Object &cert); +- + }; + + class Cert : public PKCS11Object { +@@ -153,6 +161,25 @@ class Reader : public PKCS11Object { + const char *reader, const CKYBuffer *cardATR, bool isCoolkey); + }; + ++class SecretKey : public PKCS11Object { ++ public: ++ SecretKey(unsigned long muscleObjID, CK_OBJECT_HANDLE handle, CKYBuffer *secretKeyBuffer, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount); ++ private: ++ void adjustToKeyValueLength(CKYBuffer * secretKeyBuffer,CK_ULONG valueLength); ++ ++}; ++ ++class DEREncodedSignature { ++ ++ protected : ++ CKYBuffer derEncodedSignature; ++ public: ++ DEREncodedSignature(const CKYBuffer *derSig); ++ ~DEREncodedSignature(); ++ int getRawSignature(CKYBuffer *rawSig, unsigned int keySize); ++ ++}; ++ + class AttributeMatch { + + private: +diff -up ./src/coolkey/pkcs11t.h.piv-ecc ./src/coolkey/pkcs11t.h +--- ./src/coolkey/pkcs11t.h.piv-ecc 2006-06-09 11:39:11.000000000 -0700 ++++ ./src/coolkey/pkcs11t.h 2013-09-08 15:50:33.122428723 -0700 +@@ -1351,4 +1351,41 @@ typedef struct CK_PKCS5_PBKD2_PARAMS { + + typedef CK_PKCS5_PBKD2_PARAMS CK_PTR CK_PKCS5_PBKD2_PARAMS_PTR; + ++/* The following EC Key Derivation Functions are defined */ ++ ++#define CKD_NULL 0x00000001 ++ ++#define CKD_SHA1_KDF 0x00000002 ++ ++/* CK_ECDH1_DERIVE_PARAMS is new for v2.11. */ ++ ++ typedef CK_ULONG CK_EC_KDF_TYPE; ++ ++/* ++ * CK_ECDH1_DERIVE_PARAMS provides the parameters to the ++ * ++ * CKM_ECDH1_DERIVE and CKM_ECDH1_COFACTOR_DERIVE mechanisms, ++ * ++ * where each party contributes one key pair. ++ * ++ */ ++ ++typedef struct CK_ECDH1_DERIVE_PARAMS { ++ ++ CK_EC_KDF_TYPE kdf; ++ ++ CK_ULONG ulSharedDataLen; ++ ++ CK_BYTE_PTR pSharedData; ++ ++ CK_ULONG ulPublicDataLen; ++ ++ CK_BYTE_PTR pPublicData; ++ ++} CK_ECDH1_DERIVE_PARAMS; ++ ++ ++ ++typedef CK_ECDH1_DERIVE_PARAMS CK_PTR CK_ECDH1_DERIVE_PARAMS_PTR; ++ + #endif +diff -up ./src/coolkey/slot.cpp.piv-ecc ./src/coolkey/slot.cpp +--- ./src/coolkey/slot.cpp.piv-ecc 2013-09-08 15:50:33.112428555 -0700 ++++ ./src/coolkey/slot.cpp 2013-09-08 15:50:33.124428757 -0700 +@@ -54,6 +54,34 @@ const CKYByte ATR2[] = + { 0x3B, 0x6F, 0x00, 0xFF, 0x52, 0x53, 0x41, 0x53, 0x65, 0x63, 0x75, 0x72, + 0x49, 0x44, 0x28, 0x52, 0x29, 0x31, 0x30 }; + ++ ++/* ECC curve information ++ * Provide information for the limited set of curves supported by our smart card(s). ++ * ++ */ ++ ++typedef struct curveBytes2Name { ++ const CKYByte * bytes; ++ const char *curveName; ++ unsigned int length; ++ ++} CurveBytes2Name; ++ ++/* First byte is length of oid byte array. */ ++ ++const CKYByte nistp256[] = { 0x8, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07}; ++const CKYByte nistp384[] = { 0x5, 0x2b, 0x81, 0x04, 0x00, 0x22 }; ++const CKYByte nistp521[] = { 0x05, 0x2b, 0x81, 0x04, 0x00, 0x23 }; ++ ++const int numECCurves = 3; ++ ++static CurveBytes2Name curveBytesNamePair[] = ++{ ++ { nistp256, "nistp256", 256 }, ++ { nistp384, "nistp384", 384 }, ++ { nistp521, "nistp521", 521 } ++}; ++ + SlotList::SlotList(Log *log_) : log(log_) + { + // initialize things to NULL so we can recover from an exception +@@ -136,7 +164,11 @@ SlotList::updateSlotList() + throw PKCS11Exception(CKR_HOST_MEMORY); + memset(newSlots, 0, numReaders*sizeof(Slot*)); + +- memcpy(newSlots, slots, sizeof(slots[0]) * numSlots); ++ /* keep coverity happy, even though slot == NULL implies that ++ * numSlots == 0 */ ++ if (slots) { ++ memcpy(newSlots, slots, sizeof(slots[0]) * numSlots); ++ } + + for (unsigned int i=numSlots; i < numReaders; i++) { + newSlots[i] = new +@@ -237,32 +269,19 @@ SlotList::updateReaderList() + + CKYStatus status = CKYCardContext_ListReaders(context, &readerNames); + if ( status != CKYSUCCESS ) { +- throw PKCS11Exception(CKR_GENERAL_ERROR, ++ /* if the service is stopped, treat it as if we have no readers */ ++ if ((CKYCardContext_GetLastError(context) != SCARD_E_NO_SERVICE) && ++ (CKYCardContext_GetLastError(context) != SCARD_E_SERVICE_STOPPED)) { ++ throw PKCS11Exception(CKR_GENERAL_ERROR, + "Failed to list readers: 0x%x\n", + CKYCardContext_GetLastError(context)); ++ } + } + +- if (!readerStates) { ++ if (readerStates == NULL && readerNames != NULL) { + /* fresh Reader State list, just create it */ + readerStates = CKYReader_CreateArray(readerNames, (CKYSize *)&numReaders); + +- /* if we have no readers, make sure we have at least one to keep things +- * happy */ +- if (readerStates == NULL && +- CKYReaderNameList_GetCount(readerNames) == 0) { +- readerStates = (SCARD_READERSTATE *) +- malloc(sizeof(SCARD_READERSTATE)); +- if (readerStates) { +- CKYReader_Init(readerStates); +- status = CKYReader_SetReaderName(readerStates, "E-Gate 0 0"); +- if (status != CKYSUCCESS) { +- CKYReader_DestroyArray(readerStates, 1); +- readerStates = NULL; +- } else { +- numReaders = 1; +- } +- } +- } + CKYReaderNameList_Destroy(readerNames); + + if (readerStates == NULL) { +@@ -272,6 +291,16 @@ SlotList::updateReaderList() + return; + } + ++ if (readerStates == NULL) { ++ /* if we didn't have any readers before and we did get new names, ++ * that is handled above. If we didn't have any readers before, and ++ * we didn't get any names, there is nothing to update. blow out now. ++ * This more efficient and makes coverity happy (since coverity doesn't ++ * know numReaders and readerStates are linked). */ ++ return; ++ } ++ ++ + /* it would be tempting at this point just to see if we have more readers + * then specified previously. The problem with this is it is possible that + * some readers have been deleted, so the only way to tell if we have +@@ -286,18 +315,26 @@ SlotList::updateReaderList() + + const char *curReaderName = NULL; + unsigned long knownState = 0; +- for(int ri = 0 ; ri < numReaders; ri ++) { ++ for(unsigned int ri = 0 ; ri < numReaders; ri ++) { + knownState = CKYReader_GetKnownState(&readerStates[ri]); +- ++ + curReaderName = CKYReader_GetReaderName(&readerStates[ri]); +- if(readerNameExistsInList(curReaderName,&readerNames)) { +- CKYReader_SetKnownState(&readerStates[ri], knownState & ~SCARD_STATE_IGNORE); ++ if(readerNames && readerNameExistsInList(curReaderName,&readerNames)) { ++ CKYReader_SetKnownState(&readerStates[ri], ++ knownState & ~SCARD_STATE_IGNORE); + } else { +- if (!(knownState & SCARD_STATE_UNAVAILABLE)) +- CKYReader_SetKnownState(&readerStates[ri], knownState | SCARD_STATE_UNAVAILABLE | SCARD_STATE_CHANGED); +- } ++ if (!(knownState & SCARD_STATE_UNAVAILABLE)) ++ CKYReader_SetKnownState(&readerStates[ri], ++ knownState | SCARD_STATE_UNAVAILABLE | SCARD_STATE_CHANGED); ++ } + } + ++ if (readerNames == NULL) { ++ /* OK we've marked everything unavailable, we clearly ++ * aren't adding any readers, so we can blow out here */ ++ return; ++ } ++ + const char *newReadersData[MAX_READER_DELTA]; + const char **newReaders = &newReadersData[0]; + unsigned int newReaderCount = 0; +@@ -370,7 +407,8 @@ Slot::Slot(const char *readerName_, Log + : log(log_), readerName(NULL), personName(NULL), manufacturer(NULL), + slotInfoFound(false), context(context_), conn(NULL), state(UNKNOWN), + isVersion1Key(false), needLogin(false), fullTokenName(false), +- mCoolkey(false), mOldCAC(false), ++ mCoolkey(false), mOldCAC(false),mCACLocalLogin(false), ++ pivContainer(-1), pivKey(-1), mECC(false), + #ifdef USE_SHMEM + shmem(readerName_), + #endif +@@ -573,6 +611,35 @@ SlotList::getSlotList(CK_BBOOL tokenPres + return rv; + } + ++bool ++Slot::getPIVLoginType(void) ++{ ++ CKYStatus status; ++ CKYISOStatus apduRC; ++ CKYBuffer buffer; ++ bool local = true; ++ ++ CKYBuffer_InitEmpty(&buffer); ++ ++ /* get the discovery object */ ++ status = PIVApplet_GetCertificate(conn, &buffer, 0x7e, &apduRC); ++ if (status != CKYSUCCESS) { ++ /* Discovery object optional, PIV defaults to local */ ++ goto done; ++ } ++ /* techically we probably should parse out the TLVs, but the PIV ++ * specifies exactly what they should be, so we know exactly which ++ * byte to look at */ ++ if ((CKYBuffer_Size(&buffer) >= 20) && ++ (CKYBuffer_GetChar(&buffer,17) == 0x60)) { ++ /* This tells us we should use global login for this piv card */ ++ local = false; ++ } ++done: ++ CKYBuffer_FreeData(&buffer); ++ return true; ++} ++ + void + Slot::connectToToken() + { +@@ -587,6 +654,7 @@ Slot::connectToToken() + if( ! CKYCardConnection_IsConnected(conn) ) { + int i = 0; + //for cranky readers try again a few more times ++ status = CKYSCARDERR; + while( i++ < 5 && status != CKYSUCCESS ) + { + status = CKYCardConnection_Connect(conn, readerName); +@@ -672,12 +740,36 @@ Slot::connectToToken() + // see if the applet is selectable + + log->log("time connnect: Begin transaction %d ms\n", OSTimeNow() - time); ++ status = PIVApplet_Select(conn, NULL); ++ if (status == CKYSUCCESS) { ++ /* CARD is a PIV card */ ++ state |= PIV_CARD | APPLET_SELECTABLE | APPLET_PERSONALIZED; ++ isVersion1Key = 0; ++ needLogin = 1; ++ mCoolkey = 0; ++ mOldCAC = 0; ++ mCACLocalLogin = getPIVLoginType(); ++ return; ++ } + status = CKYApplet_SelectCoolKeyManager(conn, NULL); + if (status != CKYSUCCESS) { + log->log("CoolKey Select failed 0x%x\n", status); + status = getCACAid(); + if (status != CKYSUCCESS) { +- goto loser; ++ log->log("CAC Select failed 0x%x\n", status); ++ if (status == CKYSCARDERR) { ++ log->log("Card Failure 0x%x\n", ++ CKYCardConnection_GetLastError(conn)); ++ disconnect(); ++ } ++ /* CARD is a PIV card */ ++ state |= PIV_CARD | APPLET_SELECTABLE | APPLET_PERSONALIZED; ++ isVersion1Key = 0; ++ needLogin = 1; ++ mCoolkey = 0; ++ mOldCAC = 0; ++ mCACLocalLogin = getPIVLoginType(); ++ return; + } + state |= CAC_CARD | APPLET_SELECTABLE | APPLET_PERSONALIZED; + /* skip the read of the cuid. We really don't need it and, +@@ -687,15 +779,7 @@ Slot::connectToToken() + isVersion1Key = 0; + needLogin = 1; + mCoolkey = 0; +- return; +- +-loser: +- log->log("CAC Select failed 0x%x\n", status); +- if (status == CKYSCARDERR) { +- log->log("CAC Card Failure 0x%x\n", +- CKYCardConnection_GetLastError(conn)); +- disconnect(); +- } ++ mCACLocalLogin = false; + return; + } + mCoolkey = 1; +@@ -762,8 +846,8 @@ Slot::invalidateLogin(bool hard) + } + } else { + loggedIn = false; ++ pinCache.invalidate(); + if (hard) { +- pinCache.invalidate(); + pinCache.clearPin(); + } + } +@@ -1202,6 +1286,7 @@ Slot::getTokenInfo(CK_TOKEN_INFO_PTR pTo + + + return CKR_OK; ++ + } + + void +@@ -1222,7 +1307,16 @@ SlotList::waitForSlotEvent(CK_FLAGS flag + bool found = FALSE; + CKYStatus status; + SCARD_READERSTATE *myReaderStates = NULL; ++ static SCARD_READERSTATE pnp = { 0 }; + unsigned int myNumReaders = 0; ++ ++ readerListLock.getLock(); ++ if (pnp.szReader == 0) { ++ CKYReader_Init(&pnp); ++ pnp.szReader = "\\\\?PnP?\\Notification"; ++ } ++ readerListLock.releaseLock(); ++ + #ifndef notdef + do { + readerListLock.getLock(); +@@ -1241,11 +1335,13 @@ SlotList::waitForSlotEvent(CK_FLAGS flag + * from that set to return + */ + for (i=0; i < numReaders; i++) { +- unsigned long knownState = CKYReader_GetKnownState(&readerStates[i]); ++ unsigned long knownState = ++ CKYReader_GetKnownState(&readerStates[i]); + + if ((knownState & SCARD_STATE_UNAVAILABLE) && + (knownState & SCARD_STATE_CHANGED)) { +- CKYReader_SetKnownState(&readerStates[i], knownState & ~SCARD_STATE_CHANGED); ++ CKYReader_SetKnownState(&readerStates[i], ++ knownState & ~SCARD_STATE_CHANGED); + readerListLock.releaseLock(); + *slotp = slotIndexToID(i); + found = TRUE; +@@ -1262,31 +1358,48 @@ SlotList::waitForSlotEvent(CK_FLAGS flag + break; + } + +- if (myNumReaders != numReaders) { ++ if (myNumReaders != numReaders + 1) { + if (myReaderStates) { + delete [] myReaderStates; + } +- myReaderStates = new SCARD_READERSTATE [numReaders]; ++ myReaderStates = new SCARD_READERSTATE [numReaders + 1]; ++ myNumReaders = numReaders + 1; + } +- memcpy(myReaderStates, readerStates, +- sizeof(SCARD_READERSTATE)*numReaders); +- myNumReaders = numReaders; ++ ++ memcpy(myReaderStates, readerStates, ++ sizeof(SCARD_READERSTATE) * numReaders); ++ memcpy(&myReaderStates[numReaders], &pnp, sizeof(pnp)); + readerListLock.releaseLock(); + status = CKYCardContext_WaitForStatusChange(context, +- myReaderStates, myNumReaders, timeout); ++ myReaderStates, myNumReaders, timeout); + if (status == CKYSUCCESS) { +- for (i=0; i < myNumReaders; i++) { +- SCARD_READERSTATE *rsp = &myReaderStates[i]; +- unsigned long eventState = CKYReader_GetEventState(rsp); ++ unsigned long eventState; ++ for (i=0; i < myNumReaders - 1; i++) { ++ eventState = CKYReader_GetEventState(&myReaderStates[i]); + if (eventState & SCARD_STATE_CHANGED) { + readerListLock.getLock(); +- CKYReader_SetKnownState(&readerStates[i], eventState & ~SCARD_STATE_CHANGED); ++ CKYReader_SetKnownState(&readerStates[i], ++ eventState & ~SCARD_STATE_CHANGED); + readerListLock.releaseLock(); + *slotp = slotIndexToID(i); + found = TRUE; + break; + } + } ++ /* No real need to check for an additional card, we already update ++ * the list when we iterate. */ ++ if (!found) { ++ eventState = CKYReader_GetEventState( ++ &myReaderStates[myNumReaders-1]); ++ if (eventState & SCARD_STATE_CHANGED) { ++ readerListLock.getLock(); ++ CKYReader_SetKnownState(&pnp, ++ eventState & ~SCARD_STATE_CHANGED); ++ readerListLock.releaseLock(); ++ log->log("Reader insertion/removal detected\n"); ++ continue; /* get the update */ ++ } ++ } + } + + if (found || (flag == CKF_DONT_BLOCK) || shuttingDown) { +@@ -1294,21 +1407,20 @@ SlotList::waitForSlotEvent(CK_FLAGS flag + } + + #ifndef WIN32 +- if (status != CKYSUCCESS) { +- +- if ( (CKYCardContext_GetLastError(context) == +- SCARD_E_READER_UNAVAILABLE) || +- (CKYCardContext_GetLastError(context) == SCARD_E_TIMEOUT)) +- { +- OSSleep(timeout*PKCS11_CARD_ERROR_LATENCY); +- } +- +- +- } ++ /* pcsc-lite needs to make progress or something */ ++ if (status != CKYSUCCESS) { ++ if ((CKYCardContext_GetLastError(context) == ++ SCARD_E_READER_UNAVAILABLE) || ++ (CKYCardContext_GetLastError(context) == SCARD_E_TIMEOUT)) { ++ OSSleep(timeout*PKCS11_CARD_ERROR_LATENCY); ++ } ++ } + #endif + } while ((status == CKYSUCCESS) || + (CKYCardContext_GetLastError(context) == SCARD_E_TIMEOUT) || +- ( CKYCardContext_GetLastError(context) == SCARD_E_READER_UNAVAILABLE)); ++ (CKYCardContext_GetLastError(context) == SCARD_E_READER_UNAVAILABLE) || ++ (CKYCardContext_GetLastError(context) == SCARD_E_NO_SERVICE) || ++ (CKYCardContext_GetLastError(context) == SCARD_E_SERVICE_STOPPED) ); + #else + do { + OSSleep(100); +@@ -1345,6 +1457,7 @@ Slot::handleConnectionError() + case SCARD_W_REMOVED_CARD: + ckrv = CKR_DEVICE_REMOVED; + break; ++ + default: + ckrv = CKR_DEVICE_ERROR; + break; +@@ -1404,13 +1517,46 @@ Slot::selectApplet() + } + + void +-Slot::selectCACApplet(CKYByte instance) ++Slot::selectCACApplet(CKYByte instance, bool doDisconnect) + { + CKYStatus status; ++ /* PIV containers and keys by instance */ ++ static const int container[] = { ++ 0x5fc105, 0x5fc10a, 0x5fc10b, 0x5fc101, ++ 0x5fc10d, 0x5fc10e, 0x5fc10f, 0x5fc110, ++ 0x5fc111, 0x5fc112, 0x5fc113, 0x5fc114, ++ 0x5fc115, 0x5fc116, 0x5fc117, 0x5fc118, ++ 0x5fc119, 0x5fc11a, 0x5fc11b, 0x5fc11c, ++ 0x5fc11d, 0x5fc11e, 0x5fc11f, 0x5fc120 ++ }; ++ static const int keyRef[] = { ++ 0x9a, 0x9c, 0x9d, 0x9e, ++ 0x82, 0x83, 0x84, 0x85, ++ 0x86, 0x87, 0x88, 0x89, ++ 0x8a, 0x8b, 0x8c, 0x8d, ++ 0x8e, 0x8f, 0x90, 0x91, ++ 0x92, 0x93, 0x94, 0x95 ++ }; ++ ++ if (state & PIV_CARD) { ++ status = PIVApplet_Select(conn, NULL); ++ if (status == CKYSCARDERR) handleConnectionError(); ++ if (status != CKYSUCCESS) { ++ if (doDisconnect) { ++ disconnect(); ++ } ++ throw PKCS11Exception(CKR_DEVICE_REMOVED); ++ } ++ pivContainer = container[instance]; ++ pivKey = keyRef[instance]; ++ return; ++ } + CKYBuffer *aid = &cardAID[instance]; + + if (CKYBuffer_Size(aid) == 0) { +- disconnect(); ++ if (doDisconnect) { ++ disconnect(); ++ } + throw PKCS11Exception(CKR_DEVICE_REMOVED); + return; + } +@@ -1419,7 +1565,9 @@ Slot::selectCACApplet(CKYByte instance) + if ( status == CKYSCARDERR ) handleConnectionError(); + if ( status != CKYSUCCESS) { + // could not select applet: this just means it's not there +- disconnect(); ++ if (doDisconnect) { ++ disconnect(); ++ } + throw PKCS11Exception(CKR_DEVICE_REMOVED); + } + if (mOldCAC) { +@@ -1428,7 +1576,9 @@ Slot::selectCACApplet(CKYByte instance) + status = CACApplet_SelectFile(conn, cardEF[instance], NULL); + if ( status == CKYSCARDERR ) handleConnectionError(); + if ( status != CKYSUCCESS) { +- disconnect(); ++ if (doDisconnect) { ++ disconnect(); ++ } + throw PKCS11Exception(CKR_DEVICE_REMOVED); + } + } +@@ -1521,6 +1671,29 @@ Slot::generateUnusedObjectHandle() + return handle; + } + ++/* Create a short lived Secret Key for ECC key derive. */ ++PKCS11Object * ++Slot::createSecretKeyObject(CK_OBJECT_HANDLE handle, CKYBuffer *secretKeyBuffer, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount) ++{ ++ ++ if (secretKeyBuffer == NULL ) { ++ throw PKCS11Exception(CKR_DEVICE_ERROR, ++ "Can't create secret key object for ECC."); ++ } ++ ++ unsigned long muscleID = 0xfff; ++ PKCS11Object *secret = new SecretKey(muscleID, handle, secretKeyBuffer, pTemplate, ulAttributeCount); ++ ++ if (secret == NULL) { ++ throw PKCS11Exception(CKR_DEVICE_ERROR, ++ "Can't create secret key object for ECC."); ++ } ++ ++ tokenObjects.push_back(*secret); ++ ++ return secret; ++} ++ + void + Slot::addKeyObject(list& objectList, const ListObjectInfo& info, + CK_OBJECT_HANDLE handle, bool isCombined) +@@ -1530,24 +1703,32 @@ Slot::addKeyObject(list& o + CK_OBJECT_CLASS objClass = keyObj.getClass(); + const CKYBuffer *id; + +- + if (isCombined && +- ((objClass == CKO_PUBLIC_KEY) || (objClass == CKO_PRIVATE_KEY))) { +- id = keyObj.getAttribute(CKA_ID); +- if ((!id) || (CKYBuffer_Size(id) != 1)) { +- throw PKCS11Exception(CKR_DEVICE_ERROR, +- "Missing or invalid CKA_ID value"); +- } +- iter = find_if(objectList.begin(), objectList.end(), +- ObjectCertCKAIDMatch(CKYBuffer_GetChar(id,0))); +- if ( iter == objectList.end() ) { ++ ((objClass == CKO_PUBLIC_KEY) || (objClass == CKO_PRIVATE_KEY))) { ++ id = keyObj.getAttribute(CKA_ID); ++ if ((!id) || (CKYBuffer_Size(id) != 1)) { ++ throw PKCS11Exception(CKR_DEVICE_ERROR, ++ "Missing or invalid CKA_ID value"); ++ } ++ iter = find_if(objectList.begin(), objectList.end(), ++ ObjectCertCKAIDMatch(CKYBuffer_GetChar(id,0))); ++ if ( iter == objectList.end() ) { + // We failed to find a cert with a matching CKA_ID. This + // can happen if the cert is not present on the token, or + // the der encoded cert stored on the token was corrupted. +- throw PKCS11Exception(CKR_DEVICE_ERROR, +- "Failed to find cert with matching CKA_ID value"); +- } +- keyObj.completeKey(*iter); ++ throw PKCS11Exception(CKR_DEVICE_ERROR, ++ "Failed to find cert with matching CKA_ID value"); ++ } ++ keyObj.completeKey(*iter); ++ ++ /* For now this is how we determine what type of key. ++ Also for now, allow only one or the other */ ++ if ( keyObj.getKeyType() == PKCS11Object::ecc) { ++ mECC = true; ++ } else { ++ mECC = false; ++ } ++ + } + objectList.push_back(keyObj); + +@@ -1577,6 +1758,7 @@ Slot::addCertObject(list& + void + Slot::unloadObjects() + { ++ mECC = false; + tokenObjects.clear(); + free(personName); + personName = NULL; +@@ -1970,7 +2152,7 @@ Slot::readCUID(void) + // shared memory is protected by our transaction call on the card + // + CKYStatus status; +- if (state & CAC_CARD) { ++ if (state & GOV_CARD) { + status = CACApplet_SelectCardManager(conn, NULL); + } else { + status = CKYApplet_SelectCardManager(conn, NULL); +@@ -2203,6 +2385,50 @@ Slot::fetchCombinedObjects(const CKYBuff + return objInfoList; + } + ++typedef enum { ++ BER_UNWRAP, ++ BER_NEXT ++} BERop; ++ ++static CKYStatus ++berProcess(CKYBuffer *buf, int matchTag, CKYBuffer *target, BERop type) ++{ ++ unsigned char tag; ++ unsigned int used_length= 0; ++ unsigned int data_length; ++ ++ tag = CKYBuffer_GetChar(buf,used_length++); ++ ++ /* blow out when we come to the end */ ++ if (matchTag && tag != matchTag) { ++ return CKYLIBFAIL; ++ } ++ ++ data_length = CKYBuffer_GetChar(buf,used_length++); ++ ++ if (data_length & 0x80) { ++ int len_count = data_length & 0x7f; ++ ++ data_length = 0; ++ ++ while (len_count-- > 0) { ++ data_length = (data_length << 8) | ++ CKYBuffer_GetChar(buf,used_length++); ++ } ++ } ++ ++ if (data_length > (CKYBuffer_Size(buf)-used_length) ) { ++ return CKYLIBFAIL; ++ } ++ ++ if (type == BER_UNWRAP) { ++ return CKYBuffer_AppendBuffer(target, buf, used_length, data_length); ++ } ++ return CKYBuffer_AppendBuffer(target, buf, used_length+data_length, ++ CKYBuffer_Size(buf)-(used_length+data_length)); ++} ++ ++ + CKYStatus + Slot::readCACCertificateFirst(CKYBuffer *cert, CKYSize *nextSize, + bool throwException) +@@ -2211,16 +2437,60 @@ Slot::readCACCertificateFirst(CKYBuffer + CKYISOStatus apduRC; + *nextSize = 0; + ++ if (state & PIV_CARD) { ++ CKYBuffer pivData; ++ CKYBuffer certInfo; ++ ++ CKYBuffer_InitEmpty(&pivData); ++ CKYBuffer_InitEmpty(&certInfo); ++ CKYBuffer_Resize(cert, 0); ++ status = PIVApplet_GetCertificate(conn, cert, pivContainer, &apduRC); ++ if (throwException && (status != CKYSUCCESS)) { ++ handleConnectionError(); ++ } ++ /* actually, on success, we need to parse the certificate and find the ++ * propper tag */ ++ if (status == CKYSUCCESS) { ++ status = berProcess(cert, 0x53, &pivData, BER_UNWRAP); ++ CKYBuffer_Resize(cert, 0); ++ CKYBuffer_AppendChar(cert,0); ++ do { ++ CKYByte tag = CKYBuffer_GetChar(&pivData,0); ++ if (tag == CAC_TAG_CERTIFICATE) { ++ status = berProcess(&pivData, CAC_TAG_CERTIFICATE, ++ cert, BER_UNWRAP); ++ } ++ if (tag == CAC_TAG_CERTINFO) { ++ CKYBuffer_Resize(&certInfo, 0); ++ status = berProcess(&pivData, CAC_TAG_CERTINFO, ++ &certInfo, BER_UNWRAP); ++ if (CKYBuffer_Size(&certInfo) == 1) { ++ CKYBuffer_SetChar(cert,0, ++ CKYBuffer_GetChar(&certInfo,0)); ++ } ++ } ++ if (status == CKYSUCCESS) { ++ CKYBuffer_Resize(&certInfo, 0); ++ status = berProcess(&pivData, 0, &certInfo, BER_NEXT); ++ if (status == CKYSUCCESS) { ++ CKYBuffer_Resize(&pivData,0); ++ status = CKYBuffer_AppendCopy(&pivData,&certInfo); ++ } ++ } ++ } while ((status == CKYSUCCESS) && (CKYBuffer_Size(&pivData) != 0)); ++ CKYBuffer_FreeData(&pivData); ++ CKYBuffer_FreeData(&certInfo); ++ } ++ ++ return status; ++ } ++ + if (mOldCAC) { + /* get the first 100 bytes of the cert */ + status = CACApplet_GetCertificateFirst(conn, cert, nextSize, &apduRC); + if (throwException && (status != CKYSUCCESS)) { + handleConnectionError(); + } +- +- if(CKYBuffer_Size(cert) == 0) { +- handleConnectionError(); +- } + return status; + } + +@@ -2233,6 +2503,7 @@ Slot::readCACCertificateFirst(CKYBuffer + CKYBuffer_InitEmpty(&tBuf); + CKYBuffer_InitEmpty(&vBuf); + CKYBuffer_Resize(cert, 0); ++ CKYBuffer_AppendChar(cert,0); + + /* handle the new CAC card read */ + /* read the TLV */ +@@ -2258,11 +2529,12 @@ Slot::readCACCertificateFirst(CKYBuffer + length = CKYBuffer_GetShortLE(&tBuf, toffset); + toffset +=2; + } +- if (tag != CAC_TAG_CERTIFICATE) { +- continue; ++ if (tag == CAC_TAG_CERTIFICATE) { ++ CKYBuffer_AppendBuffer(cert, &vBuf, voffset, length); ++ } ++ if (tag == CAC_TAG_CERTINFO) { ++ CKYBuffer_SetChar(cert,0,CKYBuffer_GetChar(&vBuf,voffset)); + } +- CKYBuffer_AppendBuffer(cert, &vBuf, voffset, length); +- break; + } + status = CKYSUCCESS; + +@@ -2272,6 +2544,191 @@ done: + return status; + } + ++ ++const static unsigned long crc_table[] = { ++0x00000000,0x77073096,0xee0e612c,0x990951ba, ++0x076dc419,0x706af48f,0xe963a535,0x9e6495a3, ++0x0edb8832,0x79dcb8a4,0xe0d5e91e,0x97d2d988, ++0x09b64c2b,0x7eb17cbd,0xe7b82d07,0x90bf1d91, ++0x1db71064,0x6ab020f2,0xf3b97148,0x84be41de, ++0x1adad47d,0x6ddde4eb,0xf4d4b551,0x83d385c7, ++0x136c9856,0x646ba8c0,0xfd62f97a,0x8a65c9ec, ++0x14015c4f,0x63066cd9,0xfa0f3d63,0x8d080df5, ++0x3b6e20c8,0x4c69105e,0xd56041e4,0xa2677172, ++0x3c03e4d1,0x4b04d447,0xd20d85fd,0xa50ab56b, ++0x35b5a8fa,0x42b2986c,0xdbbbc9d6,0xacbcf940, ++0x32d86ce3,0x45df5c75,0xdcd60dcf,0xabd13d59, ++0x26d930ac,0x51de003a,0xc8d75180,0xbfd06116, ++0x21b4f4b5,0x56b3c423,0xcfba9599,0xb8bda50f, ++0x2802b89e,0x5f058808,0xc60cd9b2,0xb10be924, ++0x2f6f7c87,0x58684c11,0xc1611dab,0xb6662d3d, ++0x76dc4190,0x01db7106,0x98d220bc,0xefd5102a, ++0x71b18589,0x06b6b51f,0x9fbfe4a5,0xe8b8d433, ++0x7807c9a2,0x0f00f934,0x9609a88e,0xe10e9818, ++0x7f6a0dbb,0x086d3d2d,0x91646c97,0xe6635c01, ++0x6b6b51f4,0x1c6c6162,0x856530d8,0xf262004e, ++0x6c0695ed,0x1b01a57b,0x8208f4c1,0xf50fc457, ++0x65b0d9c6,0x12b7e950,0x8bbeb8ea,0xfcb9887c, ++0x62dd1ddf,0x15da2d49,0x8cd37cf3,0xfbd44c65, ++0x4db26158,0x3ab551ce,0xa3bc0074,0xd4bb30e2, ++0x4adfa541,0x3dd895d7,0xa4d1c46d,0xd3d6f4fb, ++0x4369e96a,0x346ed9fc,0xad678846,0xda60b8d0, ++0x44042d73,0x33031de5,0xaa0a4c5f,0xdd0d7cc9, ++0x5005713c,0x270241aa,0xbe0b1010,0xc90c2086, ++0x5768b525,0x206f85b3,0xb966d409,0xce61e49f, ++0x5edef90e,0x29d9c998,0xb0d09822,0xc7d7a8b4, ++0x59b33d17,0x2eb40d81,0xb7bd5c3b,0xc0ba6cad, ++0xedb88320,0x9abfb3b6,0x03b6e20c,0x74b1d29a, ++0xead54739,0x9dd277af,0x04db2615,0x73dc1683, ++0xe3630b12,0x94643b84,0x0d6d6a3e,0x7a6a5aa8, ++0xe40ecf0b,0x9309ff9d,0x0a00ae27,0x7d079eb1, ++0xf00f9344,0x8708a3d2,0x1e01f268,0x6906c2fe, ++0xf762575d,0x806567cb,0x196c3671,0x6e6b06e7, ++0xfed41b76,0x89d32be0,0x10da7a5a,0x67dd4acc, ++0xf9b9df6f,0x8ebeeff9,0x17b7be43,0x60b08ed5, ++0xd6d6a3e8,0xa1d1937e,0x38d8c2c4,0x4fdff252, ++0xd1bb67f1,0xa6bc5767,0x3fb506dd,0x48b2364b, ++0xd80d2bda,0xaf0a1b4c,0x36034af6,0x41047a60, ++0xdf60efc3,0xa867df55,0x316e8eef,0x4669be79, ++0xcb61b38c,0xbc66831a,0x256fd2a0,0x5268e236, ++0xcc0c7795,0xbb0b4703,0x220216b9,0x5505262f, ++0xc5ba3bbe,0xb2bd0b28,0x2bb45a92,0x5cb36a04, ++0xc2d7ffa7,0xb5d0cf31,0x2cd99e8b,0x5bdeae1d, ++0x9b64c2b0,0xec63f226,0x756aa39c,0x026d930a, ++0x9c0906a9,0xeb0e363f,0x72076785,0x05005713, ++0x95bf4a82,0xe2b87a14,0x7bb12bae,0x0cb61b38, ++0x92d28e9b,0xe5d5be0d,0x7cdcefb7,0x0bdbdf21, ++0x86d3d2d4,0xf1d4e242,0x68ddb3f8,0x1fda836e, ++0x81be16cd,0xf6b9265b,0x6fb077e1,0x18b74777, ++0x88085ae6,0xff0f6a70,0x66063bca,0x11010b5c, ++0x8f659eff,0xf862ae69,0x616bffd3,0x166ccf45, ++0xa00ae278,0xd70dd2ee,0x4e048354,0x3903b3c2, ++0xa7672661,0xd06016f7,0x4969474d,0x3e6e77db, ++0xaed16a4a,0xd9d65adc,0x40df0b66,0x37d83bf0, ++0xa9bcae53,0xdebb9ec5,0x47b2cf7f,0x30b5ffe9, ++0xbdbdf21c,0xcabac28a,0x53b39330,0x24b4a3a6, ++0xbad03605,0xcdd70693,0x54de5729,0x23d967bf, ++0xb3667a2e,0xc4614ab8,0x5d681b02,0x2a6f2b94, ++0xb40bbe37,0xc30c8ea1,0x5a05df1b,0x2d02ef8d ++}; ++ ++static unsigned long ++calc_crc32(const unsigned char *buf, int len) ++{ ++ unsigned long crc = 0xffffffff; ++ int i; ++ ++ for (i=0; i < len; i++) { ++ unsigned char crc_low = crc & 0xff; ++ unsigned long crc_high = crc >> 8; ++ crc = crc_table[crc_low ^ buf[i]] ^ crc_high; ++ } ++ return crc ^ 0xffffffff; ++} ++ ++/* ++ * decompress, handles both gzip and zlib trailers ++ * it also automatically allocates the output buffer and expands it as ++ * necessary. ++ */ ++static int ++decompress(CKYBuffer *out, ++ CKYBuffer *in, CKYOffset offset, CKYSize len) ++{ ++ int zret; ++ CKYStatus status; ++ z_stream stream; ++ int chunk = len *2; ++ int outlen = 0; ++ ++ ++ /* allocate inflate state */ ++ stream.zalloc = Z_NULL; ++ stream.zfree = Z_NULL; ++ stream.opaque = Z_NULL; ++ stream.avail_in = 0; ++ stream.next_in = Z_NULL; ++ zret = inflateInit(&stream); ++ if (zret != Z_OK) ++ return zret; ++ ++ status = CKYBuffer_Reserve(out, outlen); ++ if (status != CKYSUCCESS) { ++ return Z_MEM_ERROR; ++ } ++ ++ stream.avail_in = len; ++ stream.next_in = (Bytef *)(CKYBuffer_Data(in) + offset); ++ ++ do { ++ CKYBuffer_Resize(out, outlen + chunk); ++ stream.avail_out = chunk; ++ ++ stream.next_out = (Bytef *)CKYBuffer_Data(out)+ outlen; ++ ++ zret= inflate(&stream, Z_NO_FLUSH); ++ ++ /* we need the length early so it can be used in error processing */ ++ outlen += chunk - stream.avail_out; ++ ++ /* proccess the error codes */ ++ switch (zret) { ++ case Z_DATA_ERROR: ++ /* a DATA error can occur on either corrupted data, or on gzip. ++ * data. This is because gzip uses CRC32 and zlib used ADLER32 ++ * checksums. We need to check to see if this failure is do to ++ * a gzip header. */ ++ /* 1) a gzip header includes 4 extra bytes containing the length ++ * of the gziped data. This means there must be 4 more bytes ++ * in our input buffer that have not been processed */ ++ if (stream.avail_in != 4) { ++ break; /* not a gzip header */ ++ } ++ /* The last 4 bytes of a gzip header include the uncompressed length ++ * modulo 2^32. Make sure the actual uncompressed length matches ++ * the header. */ ++ if ((outlen & 0xffffffffL) ++ != CKYBuffer_GetLongLE(in, offset+len-4)) { ++ break; /* didn't decode the full length */ ++ } ++ /* At this point it''s pretty likely we have a gzip trailer. Verify ++ * the crc32 values to make sure there hasn't been any corruption. ++ */ ++ if (calc_crc32(CKYBuffer_Data(out), outlen) != ++ CKYBuffer_GetLongLE(in,offset+len-8)) { ++ break; /* CRC didn't match */ ++ } ++ /* This was valid gzip data, and we've successfully uncompressed ++ * it. We're now done. */ ++ zret=Z_STREAM_END; ++ break; ++ case Z_NEED_DICT: ++ /* if we need the dict, it wasn't in the data, ++ * so it's a data error */ ++ zret = Z_DATA_ERROR; ++ break; ++ case Z_OK: ++ /* Z_OK means we need more data, expand the buffer and go again. ++ * if we don't need more buffer space, then the input must have ++ * been truncated, that's a data error */ ++ if (stream.avail_out != 0) { ++ zret = Z_DATA_ERROR; ++ } ++ break; ++ } ++ } while (zret == Z_OK); ++ ++ /* cleanup */ ++ if (zret == Z_STREAM_END) { ++ zret = Z_OK; ++ CKYBuffer_Resize(out, outlen); ++ } else { ++ CKYBuffer_Resize(out, 0); ++ } ++ (void)inflateEnd(&stream); ++ return zret; ++} ++ + /* + * only necessary for old CAC cards. New CAC cards have to read the + * whole cert in anyway above.... +@@ -2304,7 +2761,7 @@ Slot::loadCACCert(CKYByte instance) + // catch the applet selection errors if they don't + // + try { +- selectCACApplet(instance); ++ selectCACApplet(instance, false); + } catch(PKCS11Exception& e) { + // all CAC's must have instance '0', throw the error it + // they don't. +@@ -2322,6 +2779,10 @@ Slot::loadCACCert(CKYByte instance) + + if (instance == 0) { + readCACCertificateFirst(&rawCert, &nextSize, true); ++ ++ if(CKYBuffer_Size(&rawCert) <= 1) { ++ handleConnectionError(); ++ } + log->log("CAC Cert %d: fetch CAC Cert: %d ms\n", + instance, OSTimeNow() - time); + } +@@ -2364,7 +2825,7 @@ Slot::loadCACCert(CKYByte instance) + } else { + status = readCACCertificateFirst(&rawCert, &nextSize, false); + +- if (status != CKYSUCCESS) { ++ if ((status != CKYSUCCESS) || (CKYBuffer_Size(&rawCert) <= 1)) { + /* CAC only requires the Certificate in pki '0' */ + /* if pki '1' or '2' are empty, treat it as a non-fatal error*/ + if (instance == 2) { +@@ -2393,31 +2854,61 @@ Slot::loadCACCert(CKYByte instance) + + log->log("CAC Cert %d: Cert has been read: %d ms\n", + instance, OSTimeNow() - time); +- if (!mOldCAC || CKYBuffer_GetChar(&rawCert,0) == 1) { +- CKYSize guessFinalSize = CKYBuffer_Size(&rawCert); +- CKYSize certSize = 0; +- CKYOffset offset = mOldCAC ? 1 : 0; ++ /* new CACs, and old CACs with the high one bit are compressed, ++ * uncompress them */ ++ if ((CKYBuffer_GetChar(&rawCert,0) & 0x3) == 1) { ++ CKYOffset offset = 1; + int zret = Z_MEM_ERROR; + +- do { +- guessFinalSize *= 2; +- status = CKYBuffer_Resize(&cert, guessFinalSize); +- if (status != CKYSUCCESS) { +- break; ++ /* process the GZIP header if present */ ++ /* header_id = 0x1f, 0x8b. CM=8. If we ever support something other ++ * than CM=8, we need to change the zlib header below. Currently both ++ * gzip and zlib only support CM=8 (DEFLATE) compression */ ++ if ((CKYBuffer_GetChar(&rawCert,1) == 0x1f) && ++ (CKYBuffer_GetChar(&rawCert,2) == 0x8b) && ++ (CKYBuffer_GetChar(&rawCert,3) == 8)) { ++ CKYByte flags = CKYBuffer_GetChar(&rawCert,4); ++ /* this has a gzip header, not raw data. */ ++ offset += 10; /* base size of the gzip header */ ++ if (flags & 4) { /* FEXTRA */ ++ CKYSize len = CKYBuffer_GetShortLE(&rawCert,offset); ++ offset += len; + } +- certSize = guessFinalSize; +- zret = uncompress((Bytef *)CKYBuffer_Data(&cert),&certSize, +- CKYBuffer_Data(&rawCert)+offset, +- CKYBuffer_Size(&rawCert)-offset); +- } while (zret == Z_BUF_ERROR); ++ if (flags & 8) { /* FNAME */ ++ while (CKYBuffer_GetChar(&rawCert,offset) != 0) { ++ offset++; ++ } ++ offset++; ++ } ++ if (flags & 0x10) { /* FComment */ ++ while (CKYBuffer_GetChar(&rawCert,offset) != 0) { ++ offset++; ++ } ++ offset++; ++ } ++ if (flags & 2) { /* FHCRC */ ++ offset += 2; ++ } ++ offset -= 2; ++ ++ /* add zlib header, so libz will be happy */ ++ /* CINFO=7, CM=8, LEVEL=2, DICTFLAG=0, FCHECK= 1c */ ++ /* NOTE: the zlib will fail when procssing the trailer. this is ++ * ok because decompress automatically notices the failure and ++ * and checks the gzip trailer. */ ++ CKYBuffer_SetChar(&rawCert, offset, 0x78); ++ CKYBuffer_SetChar(&rawCert, offset+1, 0x9c); ++ } ++ /* uncompress. This expands cert as necessary. */ ++ zret = decompress(&cert, &rawCert, offset, ++ CKYBuffer_Size(&rawCert)-offset); + + if (zret != Z_OK) { + CKYBuffer_FreeData(&rawCert); + CKYBuffer_FreeData(&cert); + throw PKCS11Exception(CKR_DEVICE_ERROR, +- "Corrupted compressed CAC Cert"); ++ "Corrupted compressed CAC/PIV Cert"); + } +- CKYBuffer_Resize(&cert,certSize); + } else { + CKYBuffer_InitFromBuffer(&cert,&rawCert,1,CKYBuffer_Size(&rawCert)-1); + } +@@ -2431,6 +2922,9 @@ Slot::loadCACCert(CKYByte instance) + tokenObjects.push_back(privKey); + tokenObjects.push_back(pubKey); + tokenObjects.push_back(certObj); ++ if ( pubKey.getKeyType() == PKCS11Object::ecc) { ++ mECC = 1; ++ } + + if (personName == NULL) { + const char *name = certObj.getName(); +@@ -2459,7 +2953,7 @@ Slot::loadObjects() + list objInfoList; + std::list::iterator iter; + +- if (state & CAC_CARD) { ++ if (state & GOV_CARD) { + loadCACCert(0); + loadCACCert(1); + loadCACCert(2); +@@ -2704,6 +3198,7 @@ Slot::login(SessionHandleSuffix handleSu + } + + if (!isVersion1Key) { ++ pinCache.invalidate(); + pinCache.set((const char *)pPin, ulPinLen); + } else if (nonceValid) { + throw PKCS11Exception(CKR_USER_ALREADY_LOGGED_IN); +@@ -2713,15 +3208,15 @@ Slot::login(SessionHandleSuffix handleSu + CKYStatus status = trans.begin(conn); + if(status != CKYSUCCESS ) handleConnectionError(); + +- if (state & CAC_CARD) { +- selectCACApplet(0); ++ if (state & GOV_CARD) { ++ selectCACApplet(0, true); + } else { + selectApplet(); + } + + if (isVersion1Key) { + attemptLogin((const char *)pPin); +- } else if (state & CAC_CARD) { ++ } else if (state & GOV_CARD) { + attemptCACLogin(); + } else { + oldAttemptLogin(); +@@ -2738,7 +3233,8 @@ Slot::attemptCACLogin() + CKYISOStatus result; + + status = CACApplet_VerifyPIN(conn, +- (const char *)CKYBuffer_Data(pinCache.get()), &result); ++ (const char *)CKYBuffer_Data(pinCache.get()), ++ mCACLocalLogin, &result); + if( status == CKYSCARDERR ) { + handleConnectionError(); + } +@@ -2746,8 +3242,10 @@ Slot::attemptCACLogin() + case CKYISO_SUCCESS: + break; + case 0x6981: ++ pinCache.clearPin(); + throw PKCS11Exception(CKR_PIN_LOCKED); + default: ++ pinCache.clearPin(); + if ((result & 0xff00) == 0x6300) { + throw PKCS11Exception(CKR_PIN_INCORRECT); + } +@@ -2776,10 +3274,13 @@ Slot::oldAttemptLogin() + case CKYISO_SUCCESS: + break; + case CKYISO_AUTH_FAILED: ++ pinCache.clearPin(); + throw PKCS11Exception(CKR_PIN_INCORRECT); + case CKYISO_IDENTITY_BLOCKED: ++ pinCache.clearPin(); + throw PKCS11Exception(CKR_PIN_LOCKED); + default: ++ pinCache.clearPin(); + throw PKCS11Exception(CKR_DEVICE_ERROR, "Applet returned 0x%04x", + result); + } +@@ -2866,7 +3367,7 @@ Slot::logout(SessionHandleSuffix suffix) + throw PKCS11Exception(CKR_SESSION_HANDLE_INVALID); + } + +- if (state & CAC_CARD) { ++ if (state & GOV_CARD) { + CACLogout(); + return; + } +@@ -2993,7 +3494,7 @@ Slot::getAttributeValue(SessionHandleSuf + ObjectConstIter iter = find_if(tokenObjects.begin(), tokenObjects.end(), + ObjectHandleMatch(hObject)); + +- if( iter == tokenObjects.end() ) { ++ if ( iter == tokenObjects.end()) { + throw PKCS11Exception(CKR_OBJECT_HANDLE_INVALID); + } + +@@ -3077,6 +3578,21 @@ SlotList::generateRandom(CK_SESSION_HAND + } + + void ++SlotList::derive(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, ++ CK_OBJECT_HANDLE hBaseKey, CK_ATTRIBUTE_PTR pTemplate, ++ CK_ULONG ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey) ++{ ++ ++ CK_SLOT_ID slotID; ++ SessionHandleSuffix suffix; ++ ++ decomposeSessionHandle(hSession, slotID, suffix); ++ ++ slots[slotIDToIndex(slotID)]->derive(suffix, pMechanism, hBaseKey, pTemplate, ulAttributeCount, phKey); ++ ++} ++ ++void + Slot::ensureValidSession(SessionHandleSuffix suffix) + { + if( ! isValidSession(suffix) ) { +@@ -3110,6 +3626,23 @@ Slot::objectHandleToKeyNum(CK_OBJECT_HAN + return keyNum & 0xFF; + } + ++PKCS11Object::KeyType ++Slot::getKeyTypeFromHandle(CK_OBJECT_HANDLE hKey) ++{ ++ ObjectConstIter iter = find_if(tokenObjects.begin(), tokenObjects.end(), ++ ObjectHandleMatch(hKey)); ++ ++ if( iter == tokenObjects.end() ) { ++ throw PKCS11Exception(CKR_KEY_HANDLE_INVALID); ++ } ++ ++ if( getObjectClass(iter->getMuscleObjID()) != 'k' ) { ++ throw PKCS11Exception(CKR_KEY_HANDLE_INVALID); ++ } ++ ++ return iter->getKeyType(); ++} ++ + void + Slot::signInit(SessionHandleSuffix suffix, CK_MECHANISM_PTR pMechanism, + CK_OBJECT_HANDLE hKey) +@@ -3119,7 +3652,10 @@ Slot::signInit(SessionHandleSuffix suffi + if( session == sessions.end() ) { + throw PKCS11Exception(CKR_SESSION_HANDLE_INVALID); + } +- session->signatureState.initialize(objectHandleToKeyNum(hKey)); ++ ++ PKCS11Object::KeyType keyType = getKeyTypeFromHandle(hKey); ++ ++ session->signatureState.initialize(objectHandleToKeyNum(hKey), keyType); + } + + void +@@ -3131,7 +3667,10 @@ Slot::decryptInit(SessionHandleSuffix su + if( session == sessions.end() ) { + throw PKCS11Exception(CKR_SESSION_HANDLE_INVALID); + } +- session->decryptionState.initialize(objectHandleToKeyNum(hKey)); ++ ++ PKCS11Object::KeyType keyType = getKeyTypeFromHandle(hKey); ++ ++ session->decryptionState.initialize(objectHandleToKeyNum(hKey), keyType); + } + + /** +@@ -3240,54 +3779,141 @@ stripRSAPadding(CKYBuffer *stripped, con + } + } + +-class RSASignatureParams : public CryptParams { ++class ECCKeyAgreementParams : public CryptParams { + public: +- RSASignatureParams(unsigned int keysize) : CryptParams(keysize) { } ++ ECCKeyAgreementParams(unsigned int keysize) : CryptParams(keysize) { } + +- CKYByte getDirection() const { return CKY_DIR_ENCRYPT; } ++ CKYByte getDirection() const { return CKY_DIR_NONE;} + + CryptOpState& getOpState(Session& session) const { +- return session.signatureState; ++ return session.keyAgreementState; + } + + void padInput(CKYBuffer *paddedInput, const CKYBuffer *unpaddedInput) const { +- // RSA_NO_PAD requires RSA PKCS #1 Type 1 padding +- CKYStatus status = CKYBuffer_Resize(paddedInput,getKeySize()/8); +- if (status != CKYSUCCESS) { +- throw PKCS11Exception(CKR_HOST_MEMORY); +- } +- padRSAType1(unpaddedInput, paddedInput); + return; + } + + void + unpadOutput(CKYBuffer *unpaddedOutput, const CKYBuffer *paddedOutput) const { +- // no need to unpad ciphertext +- CKYBuffer_Replace(unpaddedOutput, 0, CKYBuffer_Data(paddedOutput), +- CKYBuffer_Size(paddedOutput)); +- ++ return; + } ++ + }; + +-class RSADecryptParams: public CryptParams { ++class SignatureParams : public CryptParams { + public: +- RSADecryptParams(unsigned int keysize) : CryptParams(keysize) { } ++ SignatureParams(unsigned int keysize) : CryptParams(keysize) { } + +- CKYByte getDirection() const { return CKY_DIR_DECRYPT; } ++ CKYByte getDirection() const { return CKY_DIR_NONE; } + + CryptOpState& getOpState(Session& session) const { +- return session.decryptionState; ++ return session.signatureState; + } + + void padInput(CKYBuffer *paddedInput, const CKYBuffer *unpaddedInput) const { +- // no need to unpad ciphertext +- CKYBuffer_Replace(paddedInput, 0, CKYBuffer_Data(unpaddedInput), +- CKYBuffer_Size(unpaddedInput)); ++ return; + } + + void + unpadOutput(CKYBuffer *unpaddedOutput, const CKYBuffer *paddedOutput) const { +- // strip off PKCS #1 padding ++ return; ++ } ++ ++}; ++ ++ ++ ++class ECCSignatureParams : public CryptParams { ++ public: ++ ECCSignatureParams(unsigned int keysize) : CryptParams(keysize) { } ++ ++ CKYByte getDirection() const { return CKY_DIR_NONE; } ++ ++ CryptOpState& getOpState(Session& session) const { ++ return session.signatureState; ++ } ++ ++ void padInput(CKYBuffer *paddedInput, const CKYBuffer *unpaddedInput) const { ++ return; ++ } ++ ++ void ++ unpadOutput(CKYBuffer *unpaddedOutput, const CKYBuffer *paddedOutput) const { ++ /* Here we will unpack the DER encoding of the signature */ ++ ++ if ( unpaddedOutput == NULL || paddedOutput == NULL) { ++ throw PKCS11Exception(CKR_ARGUMENTS_BAD); ++ } ++ ++ CKYBuffer rawSignature; ++ CKYBuffer_InitEmpty(&rawSignature); ++ ++ DEREncodedSignature sig(paddedOutput); ++ ++ int rv = sig.getRawSignature(&rawSignature, getKeySize() ); ++ ++ if (rv == CKYSUCCESS) { ++ CKYBuffer_Replace(unpaddedOutput, 0, CKYBuffer_Data(&rawSignature), ++ CKYBuffer_Size(&rawSignature)); ++ } else { ++ throw PKCS11Exception(CKR_DEVICE_ERROR); ++ } ++ ++ CKYBuffer_FreeData(&rawSignature); ++ ++ } ++ ++}; ++ ++ ++class RSASignatureParams : public CryptParams { ++ public: ++ RSASignatureParams(unsigned int keysize) : CryptParams(keysize) { } ++ ++ CKYByte getDirection() const { return CKY_DIR_ENCRYPT; } ++ ++ CryptOpState& getOpState(Session& session) const { ++ return session.signatureState; ++ } ++ ++ void padInput(CKYBuffer *paddedInput, const CKYBuffer *unpaddedInput) const { ++ // RSA_NO_PAD requires RSA PKCS #1 Type 1 padding ++ CKYStatus status = CKYBuffer_Resize(paddedInput,getKeySize()/8); ++ if (status != CKYSUCCESS) { ++ throw PKCS11Exception(CKR_HOST_MEMORY); ++ } ++ padRSAType1(unpaddedInput, paddedInput); ++ return; ++ } ++ ++ void ++ unpadOutput(CKYBuffer *unpaddedOutput, const CKYBuffer *paddedOutput) const { ++ // no need to unpad ciphertext ++ CKYBuffer_Replace(unpaddedOutput, 0, CKYBuffer_Data(paddedOutput), ++ CKYBuffer_Size(paddedOutput)); ++ ++ } ++}; ++ ++class RSADecryptParams: public CryptParams { ++ public: ++ RSADecryptParams(unsigned int keysize) : CryptParams(keysize) { } ++ ++ CKYByte getDirection() const { return CKY_DIR_DECRYPT; } ++ ++ CryptOpState& getOpState(Session& session) const { ++ return session.decryptionState; ++ } ++ ++ void padInput(CKYBuffer *paddedInput, const CKYBuffer *unpaddedInput) const { ++ // no need to unpad ciphertext ++ CKYBuffer_Replace(paddedInput, 0, CKYBuffer_Data(unpaddedInput), ++ CKYBuffer_Size(unpaddedInput)); ++ } ++ ++ void ++ unpadOutput(CKYBuffer *unpaddedOutput, const CKYBuffer *paddedOutput) const { ++ // strip off PKCS #1 padding + stripRSAPadding( unpaddedOutput, paddedOutput ); + return; + } +@@ -3298,9 +3924,38 @@ Slot::sign(SessionHandleSuffix suffix, C + CK_ULONG ulDataLen, CK_BYTE_PTR pSignature, + CK_ULONG_PTR pulSignatureLen) + { +- RSASignatureParams params(CryptParams::DEFAULT_KEY_SIZE); +- cryptRSA(suffix, pData, ulDataLen, pSignature, pulSignatureLen, +- params); ++ ++ refreshTokenState(); ++ SessionIter session = findSession(suffix); ++ if( session == sessions.end() ) { ++ throw PKCS11Exception(CKR_SESSION_HANDLE_INVALID); ++ } ++ ++ if (!isVersion1Key && ! isLoggedIn() ) { ++ throw PKCS11Exception(CKR_USER_NOT_LOGGED_IN); ++ } ++ ++ /* Create a default one just to get the sigState */ ++ SignatureParams dummyParams(CryptParams::DEFAULT_KEY_SIZE); ++ ++ CryptOpState sigState = dummyParams.getOpState(*session); ++ ++ PKCS11Object::KeyType keyType = sigState.keyType; ++ ++ if ( keyType == PKCS11Object::unknown) { ++ throw PKCS11Exception(CKR_DATA_INVALID); ++ } ++ ++ if( keyType == Key::ecc ) { ++ ECCSignatureParams params(CryptParams::ECC_DEFAULT_KEY_SIZE); ++ signECC(suffix, pData, ulDataLen, pSignature, pulSignatureLen, ++ params); ++ ++ } else if (keyType == Key::rsa) { ++ RSASignatureParams params(CryptParams::DEFAULT_KEY_SIZE); ++ cryptRSA(suffix, pData, ulDataLen, pSignature, pulSignatureLen, ++ params); ++ } + } + + void +@@ -3334,9 +3989,9 @@ Slot::cryptRSA(SessionHandleSuffix suffi + CKYBuffer *result = &opState.result; + CKYByte keyNum = opState.keyNum; + +- unsigned int keySize = getKeySize(keyNum); ++ unsigned int keySize = getRSAKeySize(keyNum); + +- if(keySize != CryptParams::DEFAULT_KEY_SIZE) ++ if (keySize != CryptParams::DEFAULT_KEY_SIZE) + params.setKeySize(keySize); + + if( CKYBuffer_Size(result) == 0 ) { +@@ -3358,7 +4013,8 @@ Slot::cryptRSA(SessionHandleSuffix suffi + } + try { + params.padInput(&inputPad, &input); +- performRSAOp(&output, &inputPad, keyNum, params.getDirection()); ++ performRSAOp(&output, &inputPad, params.getKeySize(), ++ keyNum, params.getDirection()); + params.unpadOutput(result, &output); + CKYBuffer_FreeData(&input); + CKYBuffer_FreeData(&inputPad); +@@ -3395,10 +4051,166 @@ Slot::getNonce() + return &nonce; + } + ++void Slot::signECC(SessionHandleSuffix suffix, CK_BYTE_PTR pInput, ++ CK_ULONG ulInputLen, CK_BYTE_PTR pOutput, ++ CK_ULONG_PTR pulOutputLen, CryptParams& params) ++{ ++ ++ if( pulOutputLen == NULL ) { ++ throw PKCS11Exception(CKR_DATA_INVALID, ++ "output length is NULL"); ++ } ++ ++ refreshTokenState(); ++ SessionIter session = findSession(suffix); ++ if( session == sessions.end() ) { ++ throw PKCS11Exception(CKR_SESSION_HANDLE_INVALID); ++ } ++ /* version 1 keys may not need login. We catch the error ++ on the operation. The token will not allow us to sign with ++ a protected key unless we are logged in. ++ can be removed when version 0 support is depricated. ++ */ ++ ++ if (!isVersion1Key && ! isLoggedIn() ) { ++ throw PKCS11Exception(CKR_USER_NOT_LOGGED_IN); ++ } ++ CryptOpState& opState = params.getOpState(*session); ++ CKYBuffer *result = &opState.result; ++ CKYByte keyNum = opState.keyNum; ++ ++ unsigned int keySize = getECCKeySize(keyNum); ++ ++ if(keySize != CryptParams::ECC_DEFAULT_KEY_SIZE) ++ params.setKeySize(keySize); ++ ++ if( CKYBuffer_Size(result) == 0 ) { ++ unsigned int maxSize = params.getKeySize()/8; ++ ++ if( pInput == NULL || ulInputLen == 0) { ++ throw PKCS11Exception(CKR_DATA_LEN_RANGE); ++ } ++ if (ulInputLen > maxSize) { ++ //pInput += ulInputLen - maxSize; ++ ulInputLen = maxSize; ++ } ++ ++ CKYBuffer input; ++ CKYBuffer output; ++ CKYBuffer_InitEmpty(&output); ++ CKYStatus status = CKYBuffer_InitFromData(&input, pInput, ulInputLen); ++ ++ if (status != CKYSUCCESS) { ++ CKYBuffer_FreeData(&output); ++ throw PKCS11Exception(CKR_HOST_MEMORY); ++ } ++ try { ++ performECCSignature(&output, &input, params.getKeySize(), keyNum); ++ params.unpadOutput(result, &output); ++ CKYBuffer_FreeData(&input); ++ CKYBuffer_FreeData(&output); ++ } catch(PKCS11Exception& e) { ++ CKYBuffer_FreeData(&input); ++ CKYBuffer_FreeData(&output); ++ throw(e); ++ } ++ } ++ ++ if( pOutput != NULL ) { ++ if( *pulOutputLen < CKYBuffer_Size(result) ) { ++ *pulOutputLen = CKYBuffer_Size(result); ++ throw PKCS11Exception(CKR_BUFFER_TOO_SMALL); ++ } ++ memcpy(pOutput, CKYBuffer_Data(result), CKYBuffer_Size(result)); ++ } ++ *pulOutputLen = CKYBuffer_Size(result); ++} ++ ++void ++Slot::performECCSignature(CKYBuffer *output, const CKYBuffer *input, ++ unsigned int keySize, CKYByte keyNum) ++{ ++ ++ /* establish a transaction */ ++ Transaction trans; ++ CKYStatus status = trans.begin(conn); ++ if( status != CKYSUCCESS ) handleConnectionError(); ++ ++ if (!mECC) { ++ throw PKCS11Exception(CKR_FUNCTION_NOT_SUPPORTED); ++ } ++ ++ if (state & GOV_CARD) { ++ selectCACApplet(keyNum, true); ++ } else { ++ selectApplet(); ++ } ++ ++ CKYISOStatus result; ++ int loginAttempted = 0; ++ ++retry: ++ if (state & PIV_CARD) { ++ status = PIVApplet_SignDecrypt(conn, pivKey, keySize/8, 0, input, output, &result); ++ } else if (state & CAC_CARD) { ++ status = CACApplet_SignDecrypt(conn, input, output, &result); ++ } else { ++ status = CKYApplet_ComputeECCSignature(conn, keyNum, input, NULL, output, getNonce(), &result); ++ } ++ /* map the ISO not logged in code to the coolkey one */ ++ if ((result == CKYISO_CONDITION_NOT_SATISFIED) || ++ (result == CKYISO_SECURITY_NOT_SATISFIED)) { ++ result = (CKYStatus) CKYISO_UNAUTHORIZED; ++ } ++ ++ if (status != CKYSUCCESS) { ++ if ( status == CKYSCARDERR ) { ++ handleConnectionError(); ++ } ++ ++ if (result == CKYISO_DATA_INVALID) { ++ throw PKCS11Exception(CKR_DATA_INVALID); ++ } ++ /* version0 keys could be logged out in the middle by someone else, ++ reauthenticate... This code can go away when we depricate. ++ version0 applets. ++ */ ++ if (!isVersion1Key && !loginAttempted && ++ (result == CKYISO_UNAUTHORIZED)) { ++ /* try to reauthenticate */ ++ try { ++ if (state & GOV_CARD) { ++ attemptCACLogin(); ++ } else { ++ oldAttemptLogin(); ++ } ++ } catch(PKCS11Exception& ) { ++ /* attemptLogin can throw things like CKR_PIN_INCORRECT ++ that don't make sense from a crypto operation. This is ++ a result of pin caching. We will reformat any login ++ exception to a CKR_DEVICE_ERROR. ++ */ ++ throw PKCS11Exception(CKR_DEVICE_ERROR); ++ } ++ loginAttempted = true; ++ goto retry; /* easier to understand than a while loop in this case. */ ++ } ++ throw PKCS11Exception( result == CKYISO_UNAUTHORIZED ? ++ CKR_USER_NOT_LOGGED_IN : CKR_DEVICE_ERROR); ++ ++ } ++ ++} ++ ++ + void +-Slot::performRSAOp(CKYBuffer *output, const CKYBuffer *input, ++Slot::performRSAOp(CKYBuffer *output, const CKYBuffer *input, unsigned int keySize, + CKYByte keyNum, CKYByte direction) + { ++ if ( mECC ) { ++ throw PKCS11Exception(CKR_FUNCTION_NOT_SUPPORTED); ++ } ++ + // + // establish a transaction + // +@@ -3409,8 +4221,8 @@ Slot::performRSAOp(CKYBuffer *output, co + // + // select the applet + // +- if (state & CAC_CARD) { +- selectCACApplet(keyNum); ++ if (state & GOV_CARD) { ++ selectCACApplet(keyNum, true); + } else { + selectApplet(); + } +@@ -3418,12 +4230,21 @@ Slot::performRSAOp(CKYBuffer *output, co + CKYISOStatus result; + int loginAttempted = 0; + retry: +- if (state & CAC_CARD) { ++ if (state & PIV_CARD) { ++ status = PIVApplet_SignDecrypt(conn, pivKey, keySize/8, 0, input, output, &result); ++ } else if (state & CAC_CARD) { + status = CACApplet_SignDecrypt(conn, input, output, &result); + } else { + status = CKYApplet_ComputeCrypt(conn, keyNum, CKY_RSA_NO_PAD, direction, + input, NULL, output, getNonce(), &result); + } ++ ++ /* map the ISO not logged in code to the coolkey one */ ++ if ((result == CKYISO_CONDITION_NOT_SATISFIED) || ++ (result == CKYISO_SECURITY_NOT_SATISFIED)) { ++ result = CKYISO_UNAUTHORIZED; ++ } ++ + if (status != CKYSUCCESS) { + if ( status == CKYSCARDERR ) { + handleConnectionError(); +@@ -3434,11 +4255,15 @@ retry: + // version0 keys could be logged out in the middle by someone else, + // reauthenticate... This code can go away when we depricate. + // version0 applets. +- if (!isVersion1Key && !loginAttempted && ++ if (!isVersion1Key && !loginAttempted && pinCache.isValid() && + (result == CKYISO_UNAUTHORIZED)) { + // try to reauthenticate + try { +- oldAttemptLogin(); ++ if (state & GOV_CARD) { ++ attemptCACLogin(); ++ } else { ++ oldAttemptLogin(); ++ } + } catch(PKCS11Exception& ) { + // attemptLogin can throw things like CKR_PIN_INCORRECT + // that don't make sense from a crypto operation. This is +@@ -3458,7 +4283,7 @@ void + Slot::seedRandom(SessionHandleSuffix suffix, CK_BYTE_PTR pData, + CK_ULONG ulDataLen) + { +- if (state & CAC_CARD) { ++ if (state & GOV_CARD) { + /* should throw unsupported */ + throw PKCS11Exception(CKR_DEVICE_ERROR); + } +@@ -3510,7 +4335,7 @@ void + Slot::generateRandom(SessionHandleSuffix suffix, const CK_BYTE_PTR pData, + CK_ULONG ulDataLen) + { +- if (state & CAC_CARD) { ++ if (state & GOV_CARD) { + /* should throw unsupported */ + throw PKCS11Exception(CKR_DEVICE_ERROR); + } +@@ -3544,7 +4369,7 @@ Slot::generateRandom(SessionHandleSuffix + + #define MAX_NUM_KEYS 8 + unsigned int +-Slot::getKeySize(CKYByte keyNum) ++Slot::getRSAKeySize(CKYByte keyNum) + { + unsigned int keySize = CryptParams::DEFAULT_KEY_SIZE; + int modSize = 0; +@@ -3574,3 +4399,238 @@ Slot::getKeySize(CKYByte keyNum) + + return keySize; + } ++ ++unsigned int ++Slot::getECCKeySize(CKYByte keyNum) ++{ ++ return calcECCKeySize(keyNum); ++} ++ ++unsigned int ++Slot::calcECCKeySize(CKYByte keyNum) ++{ ++ unsigned int keySize = CryptParams::ECC_DEFAULT_KEY_SIZE; ++ ++ if(keyNum >= MAX_NUM_KEYS) { ++ return keySize; ++ } ++ ++ ObjectConstIter iter; ++ iter = find_if(tokenObjects.begin(), tokenObjects.end(), ++ KeyNumMatch(keyNum,*this)); ++ ++ if( iter == tokenObjects.end() ) { ++ return keySize; ++ } ++ ++ CKYBuffer const *eccParams = iter->getAttribute(CKA_EC_PARAMS); ++ ++ if (eccParams == NULL) { ++ return keySize; ++ } ++ ++ /* Extract the oid from the params */ ++ ++ CKYByte ecParamsLen = CKYBuffer_GetChar(eccParams, 1); ++ ++ if ( ecParamsLen == 0 ) { ++ return keySize; ++ } ++ ++/* Now compare against the limited known list of oid byte info */ ++ ++ unsigned int oidByteLen = 0; ++ ++ CKYByte curByte = 0; ++ ++ for (int i = 0 ; i < numECCurves ; i++ ) { ++ ++ oidByteLen = curveBytesNamePair[i].bytes[0]; ++ ++ if ( oidByteLen != (unsigned int ) ecParamsLen ) { ++ continue; ++ } ++ ++ int match = 1; ++ for ( int j = 0 ; j < ecParamsLen ; j++ ) { ++ curByte = CKYBuffer_GetChar(eccParams, 2 + j ); ++ if ( curveBytesNamePair[i].bytes[ j + 1 ] != curByte ) { ++ match = 0; ++ break; ++ } ++ } ++ ++ if ( match == 1 ) { ++ keySize = curveBytesNamePair[i].length; ++ return keySize; ++ } ++ ++ } ++ ++ return keySize; ++} ++ ++void ++Slot::derive(SessionHandleSuffix suffix, CK_MECHANISM_PTR pMechanism, ++ CK_OBJECT_HANDLE hBaseKey, CK_ATTRIBUTE_PTR pTemplate, ++ CK_ULONG ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey) ++{ ++ ++ log->log("Inside of Slot::Derive! \n"); ++ ++ ECCKeyAgreementParams params(CryptParams::ECC_DEFAULT_KEY_SIZE); ++ SessionIter session = findSession(suffix); ++ ++ PKCS11Object::KeyType keyType = getKeyTypeFromHandle(hBaseKey); ++ ++ session->keyAgreementState.initialize(objectHandleToKeyNum(hBaseKey), keyType); ++ deriveECC(suffix, pMechanism, hBaseKey, pTemplate, ulAttributeCount, phKey, params); ++ ++} ++ ++void Slot::deriveECC(SessionHandleSuffix suffix, CK_MECHANISM_PTR pMechanism, ++ CK_OBJECT_HANDLE hBaseKey, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey, CryptParams& params) ++{ ++ if (pMechanism == NULL ) { ++ throw PKCS11Exception(CKR_ARGUMENTS_BAD); ++ } ++ ++ CK_ECDH1_DERIVE_PARAMS *mechParams = NULL; ++ ++ mechParams = (CK_ECDH1_DERIVE_PARAMS*) pMechanism->pParameter; ++ ++ if (mechParams == NULL || mechParams->kdf != CKD_NULL ) { ++ throw PKCS11Exception(CKR_ARGUMENTS_BAD); ++ } ++ ++ refreshTokenState(); ++ SessionIter session = findSession(suffix); ++ if( session == sessions.end() ) { ++ throw PKCS11Exception(CKR_SESSION_HANDLE_INVALID); ++ } ++ ++ /* version 1 keys may not need login. We catch the error ++ on the operation. The token will not allow us to sign with ++ a protected key unless we are logged in. ++ can be removed when version 0 support is depricated. */ ++ ++ if (!isVersion1Key && ! isLoggedIn() ) { ++ throw PKCS11Exception(CKR_USER_NOT_LOGGED_IN); ++ } ++ ++ CryptOpState& opState = params.getOpState(*session); ++ CKYBuffer *result = &opState.result; ++ CKYByte keyNum = opState.keyNum; ++ ++ unsigned int keySize = getECCKeySize(keyNum); ++ ++ if(keySize != CryptParams::ECC_DEFAULT_KEY_SIZE) ++ params.setKeySize(keySize); ++ ++ CK_MECHANISM_TYPE deriveMech = pMechanism->mechanism; ++ ++ CK_ULONG otherPublicLen = mechParams->ulPublicDataLen; ++ CK_BYTE_PTR otherPublicData = mechParams->pPublicData; ++ ++ CKYBuffer secretKeyBuffer; ++ CKYBuffer_InitEmpty(&secretKeyBuffer); ++ CKYBuffer publicDataBuffer; ++ CKYStatus status = CKYBuffer_InitFromData(&publicDataBuffer,otherPublicData, otherPublicLen); ++ ++ if (status != CKYSUCCESS) { ++ CKYBuffer_FreeData(&secretKeyBuffer); ++ throw PKCS11Exception(CKR_HOST_MEMORY); ++ } ++ ++ PKCS11Object *secret = NULL; ++ *phKey = 0; ++ ++ if( CKYBuffer_Size(result) == 0 ) { ++ try { ++ performECCKeyAgreement(deriveMech, &publicDataBuffer, &secretKeyBuffer, ++ keyNum, params.getKeySize()); ++ CK_OBJECT_HANDLE keyObjectHandle = generateUnusedObjectHandle(); ++ secret = createSecretKeyObject(keyObjectHandle, &secretKeyBuffer, pTemplate, ulAttributeCount); ++ } catch(PKCS11Exception& e) { ++ CKYBuffer_FreeData(&secretKeyBuffer); ++ CKYBuffer_FreeData(&publicDataBuffer); ++ throw(e); ++ } ++ } ++ ++ CKYBuffer_FreeData(&secretKeyBuffer); ++ CKYBuffer_FreeData(&publicDataBuffer); ++ ++ if ( secret ) { ++ ++ *phKey = secret->getHandle(); ++ delete secret; ++ } ++} ++ ++void ++Slot::performECCKeyAgreement(CK_MECHANISM_TYPE deriveMech, CKYBuffer *publicDataBuffer, ++ CKYBuffer *secretKeyBuffer, CKYByte keyNum, unsigned int keySize) ++{ ++ if (!mECC) { ++ throw PKCS11Exception(CKR_FUNCTION_NOT_SUPPORTED); ++ } ++ ++ Transaction trans; ++ CKYStatus status = trans.begin(conn); ++ if( status != CKYSUCCESS ) handleConnectionError(); ++ ++ if (state & GOV_CARD) { ++ selectCACApplet(keyNum, true); ++ } else { ++ selectApplet(); ++ } ++ ++ CKYISOStatus result; ++ int loginAttempted = 0; ++ ++retry: ++ ++ if (state & PIV_CARD) { ++ status = PIVApplet_SignDecrypt(conn, pivKey, keySize/8, 1, publicDataBuffer, ++ secretKeyBuffer, &result); ++ } else if (state & CAC_CARD) { ++ status = CACApplet_SignDecrypt(conn, publicDataBuffer, secretKeyBuffer, &result); ++ } else { ++ status = CKYApplet_ComputeECCKeyAgreement(conn, keyNum, ++ publicDataBuffer , NULL, secretKeyBuffer, getNonce(), &result); ++ } ++ /* map the ISO not logged in code to the coolkey one */ ++ if ((result == CKYISO_CONDITION_NOT_SATISFIED) || ++ (result == CKYISO_SECURITY_NOT_SATISFIED)) { ++ result = (CKYStatus) CKYISO_UNAUTHORIZED; ++ } ++ ++ if (status != CKYSUCCESS) { ++ if ( status == CKYSCARDERR ) { ++ handleConnectionError(); ++ } ++ ++ if (result == CKYISO_DATA_INVALID) { ++ throw PKCS11Exception(CKR_DATA_INVALID); ++ } ++ if (!isVersion1Key && !loginAttempted && ++ (result == CKYISO_UNAUTHORIZED)) { ++ try { ++ if (state & GOV_CARD) { ++ attemptCACLogin(); ++ } else { ++ oldAttemptLogin(); ++ } ++ } catch(PKCS11Exception& ) { ++ throw PKCS11Exception(CKR_DEVICE_ERROR); ++ } ++ loginAttempted = true; ++ goto retry; ++ } ++ ++ throw PKCS11Exception( result == CKYISO_UNAUTHORIZED ? ++ CKR_USER_NOT_LOGGED_IN : CKR_DEVICE_ERROR); ++ ++ } ++} +diff -up ./src/coolkey/slot.h.piv-ecc ./src/coolkey/slot.h +--- ./src/coolkey/slot.h.piv-ecc 2013-09-08 15:50:33.098428320 -0700 ++++ ./src/coolkey/slot.h 2013-09-08 15:50:33.125428774 -0700 +@@ -211,24 +211,27 @@ class CryptOpState { + State state; + CKYByte keyNum; + CKYBuffer result; ++ PKCS11Object::KeyType keyType; + +- CryptOpState() : state(NOT_INITIALIZED), keyNum(0) ++ CryptOpState() : state(NOT_INITIALIZED), keyNum(0), keyType(PKCS11Object::unknown) + { CKYBuffer_InitEmpty(&result); } + CryptOpState(const CryptOpState &cpy) : +- state(cpy.state), keyNum(cpy.keyNum) { ++ state(cpy.state), keyNum(cpy.keyNum), keyType(cpy.keyType) { + CKYBuffer_InitFromCopy(&result, &cpy.result); + } + CryptOpState &operator=(const CryptOpState &cpy) { + state = cpy.state, + keyNum = cpy.keyNum; ++ keyType = cpy.keyType; + CKYBuffer_Replace(&result, 0, CKYBuffer_Data(&cpy.result), + CKYBuffer_Size(&cpy.result)); + return *this; + } + ~CryptOpState() { CKYBuffer_FreeData(&result); } +- void initialize(CKYByte keyNum) { ++ void initialize(CKYByte keyNum, PKCS11Object::KeyType theKeyType) { + state = IN_PROCESS; + this->keyNum = keyNum; ++ this->keyType = theKeyType; + CKYBuffer_Resize(&result, 0); + } + }; +@@ -258,6 +261,7 @@ class Session { + + CryptOpState signatureState; + CryptOpState decryptionState; ++ CryptOpState keyAgreementState; + }; + + typedef list SessionList; +@@ -267,12 +271,11 @@ typedef SessionList::const_iterator Sess + class CryptParams { + private: + unsigned int keySize; // in bits +- protected: +- unsigned int getKeySize() const { return keySize; } + public: + // set the actual key size obtained from the card + void setKeySize(unsigned int newKeySize) { keySize = newKeySize; } +- enum { DEFAULT_KEY_SIZE = 1024 }; ++ unsigned int getKeySize() const { return keySize; } ++ enum { DEFAULT_KEY_SIZE = 1024, ECC_DEFAULT_KEY_SIZE=256 }; + + + CryptParams(unsigned int keySize_) : keySize(keySize_) { } +@@ -304,12 +307,15 @@ class Slot { + ATR_MATCH = 0x04, + APPLET_SELECTABLE = 0x08, + APPLET_PERSONALIZED = 0x10, +- CAC_CARD = 0x20 ++ CAC_CARD = 0x20, ++ PIV_CARD = 0x40 + }; + enum { + NONCE_SIZE = 8 + }; + ++ static const SlotState GOV_CARD = (SlotState)(CAC_CARD|PIV_CARD); ++ + private: + Log *log; + char *readerName; +@@ -339,7 +345,10 @@ class Slot { + bool fullTokenName; + bool mCoolkey; + bool mOldCAC; +- ++ bool mCACLocalLogin; ++ int pivContainer; ++ int pivKey; ++ bool mECC; + //enum { RW_SESSION_HANDLE = 1, RO_SESSION_HANDLE = 2 }; + + #ifdef USE_SHMEM +@@ -386,6 +395,7 @@ class Slot { + const CKYBuffer *getATR(); + bool isLoggedIn(); + bool needLoggedIn(); ++ bool getPIVLoginType(); + void testNonce(); + + void addKeyObject(list& objectList, +@@ -395,6 +405,7 @@ class Slot { + const CKYBuffer *derCert, CK_OBJECT_HANDLE handle); + void addObject(list& objectList, + const ListObjectInfo& info, CK_OBJECT_HANDLE handle); ++ PKCS11Object *createSecretKeyObject(CK_OBJECT_HANDLE handle, CKYBuffer *secretKeyBuffer,CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount); + + void ensureValidSession(SessionHandleSuffix suffix); + +@@ -408,7 +419,7 @@ class Slot { + CKYStatus readCACCertificateAppend(CKYBuffer *cert, CKYSize nextSize); + + void selectApplet(); +- void selectCACApplet(CKYByte instance); ++ void selectCACApplet(CKYByte instance,bool do_disconnect); + void unloadObjects(); + void loadCACObjects(); + void loadCACCert(CKYByte instance); +@@ -432,12 +443,23 @@ class Slot { + CK_ULONG ulInputLen, CK_BYTE_PTR pOutput, + CK_ULONG_PTR pulOutputLen, CryptParams& params); + +- void performRSAOp(CKYBuffer *out, const CKYBuffer *input, CKYByte keyNum, +- CKYByte direction); ++ void performRSAOp(CKYBuffer *out, const CKYBuffer *input, unsigned int keySize, ++ CKYByte keyNum, CKYByte direction); ++ ++ void signECC(SessionHandleSuffix suffix, CK_BYTE_PTR pInput, ++ CK_ULONG ulInputLen, CK_BYTE_PTR pOutput, ++ CK_ULONG_PTR pulOutputLen, CryptParams& params); ++ ++ void performECCSignature(CKYBuffer *out, const CKYBuffer *input, ++ unsigned int keySize, CKYByte keyNum); ++ void performECCKeyAgreement(CK_MECHANISM_TYPE deriveMech, ++ CKYBuffer *publicDataBuffer, ++ CKYBuffer *secretKeyBuffer, CKYByte keyNum, unsigned int keySize); + + void processComputeCrypt(CKYBuffer *result, const CKYAPDU *apdu); + + CKYByte objectHandleToKeyNum(CK_OBJECT_HANDLE hKey); ++ unsigned int calcECCKeySize(CKYByte keyNum); + Slot(const Slot &cpy) + #ifdef USE_SHMEM + : shmem(readerName) +@@ -469,7 +491,10 @@ class Slot { + } + + // actually get the size of a key in bits from the card +- unsigned int getKeySize(CKYByte keyNum); ++ unsigned int getRSAKeySize(CKYByte keyNum); ++ unsigned int getECCKeySize(CKYByte keyNum); ++ ++ PKCS11Object::KeyType getKeyTypeFromHandle(CK_OBJECT_HANDLE hKey); + + SessionHandleSuffix openSession(Session::Type type); + void closeSession(SessionHandleSuffix handleSuffix); +@@ -511,6 +536,16 @@ class Slot { + CK_ULONG len); + void generateRandom(SessionHandleSuffix suffix, CK_BYTE_PTR data, + CK_ULONG len); ++ ++ void derive(SessionHandleSuffix suffix, CK_MECHANISM_PTR pMechanism, ++ CK_OBJECT_HANDLE hKey, CK_ATTRIBUTE_PTR pTemplate, ++ CK_ULONG ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey); ++ ++ void deriveECC(SessionHandleSuffix suffix, CK_MECHANISM_PTR pMechanism, ++ CK_OBJECT_HANDLE hBaseKey, CK_ATTRIBUTE_PTR pTemplate, ++ CK_ULONG ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey, CryptParams& params); ++ ++ bool getIsECC() { return mECC; } + }; + + class SlotList { +@@ -604,6 +639,10 @@ class SlotList { + void seedRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, + CK_ULONG ulDataLen); + ++ void derive(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, ++ CK_OBJECT_HANDLE hKey, CK_ATTRIBUTE_PTR pTemplate, ++ CK_ULONG ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey); ++ + + }; + #endif +diff -up ./src/install/pk11install.c.piv-ecc ./src/install/pk11install.c +--- ./src/install/pk11install.c.piv-ecc 2007-02-06 11:40:36.000000000 -0800 ++++ ./src/install/pk11install.c 2013-09-08 15:50:33.126428790 -0700 +@@ -171,7 +171,7 @@ int dirListCount = sizeof(dirList)/sizeo + static void + usage(char *prog) + { +- fprintf(stderr,"usage: %s [-u][-v] [-p path] module\n", prog); ++ fprintf(stderr,"usage: %s [-u][-v][-s][-l] [-p path] module\n", prog); + return; + } + +@@ -181,9 +181,9 @@ usage(char *prog) + + #define CONFIG_TAG "configDir=" + int +-installPKCS11(char *dirPath, InstType type, char *module) ++installPKCS11(char *dirPath, char *dbType, InstType type, char *module) + { +- char *paramString = (char *)malloc(strlen(dirPath)+sizeof(CONFIG_TAG)+3); ++ char *paramString = (char *)malloc(strlen(dbType)+strlen(dirPath)+sizeof(CONFIG_TAG)+3); + char *cp; + char **rc; + +@@ -191,7 +191,7 @@ installPKCS11(char *dirPath, InstType ty + PINST_SET_ERROR(ERROR_NOT_ENOUGH_MEMORY); + return 0; + } +- sprintf(paramString,CONFIG_TAG"\"%s\" ",dirPath); ++ sprintf(paramString,CONFIG_TAG"\"%s%s\" ",dbType,dirPath); + + /* translate all the \'s to /'s */ + for (cp=paramString; *cp; cp++) { +@@ -214,7 +214,7 @@ installPKCS11(char *dirPath, InstType ty + + + int +-installAllPKCS11(char *dirPath, char *search, char *tail, ++installAllPKCS11(char *dirPath, char *dbType, char *search, char *tail, + InstType type, char *module) + { + char *searchString; +@@ -282,9 +282,9 @@ installAllPKCS11(char *dirPath, char *se + + myPath=PINST_FULLPATH(tempPath,path); + if (tail) { +- installAllPKCS11(myPath, tail, NULL, type, module); ++ installAllPKCS11(myPath, dbType, tail, NULL, type, module); + } else { +- installPKCS11(myPath, type, module); ++ installPKCS11(myPath, dbType, type, module); + } + } while (PINST_NEXT(iter, fileData)); + free(tempPath); +@@ -309,6 +309,7 @@ int main(int argc, char **argv) + int i; + InstType type = Install; + char * path = NULL; ++ char *dbType = ""; + #ifdef WIN32 + BOOL brc; + HKEY regKey; +@@ -333,6 +334,12 @@ int main(int argc, char **argv) + case 'v': + verbose = 1; + break; ++ case 'l': ++ dbType = "dbm:"; ++ break; ++ case 's': ++ dbType = "sql:"; ++ break; + case 'p': + path = *argv++; + if (path == NULL) { +@@ -359,7 +366,7 @@ int main(int argc, char **argv) + } + + if (path) { +- installAllPKCS11(path, "", NULL, type, module); ++ installAllPKCS11(path, dbType, "", NULL, type, module); + return 0; + } + +@@ -444,7 +451,7 @@ int main(int argc, char **argv) + if (!dirPath) { + continue; + } +- installAllPKCS11(dirPath, dirList[i].search, dirList[i].tail, ++ installAllPKCS11(dirPath, dbType, dirList[i].search, dirList[i].tail, + type, module); + } + +diff -up ./src/libckyapplet/cky_applet.c.piv-ecc ./src/libckyapplet/cky_applet.c +--- ./src/libckyapplet/cky_applet.c.piv-ecc 2013-09-08 15:50:33.099428337 -0700 ++++ ./src/libckyapplet/cky_applet.c 2013-09-08 15:50:33.126428790 -0700 +@@ -103,6 +103,22 @@ CKYAppletFactory_ComputeCryptOneStep(CKY + } + + CKYStatus ++CKYAppletFactory_ComputeECCSignatureOneStep(CKYAPDU *apdu, const void *param) ++{ ++ const CKYAppletArgComputeECCSignature *ccs=(const CKYAppletArgComputeECCSignature *)param; ++ return CKYAPDUFactory_ComputeECCSignatureOneStep(apdu, ccs->keyNumber, ++ ccs->location, ccs->data, ccs->sig); ++} ++ ++CKYStatus ++CKYAppletFactory_ComputeECCKeyAgreementOneStep(CKYAPDU *apdu, const void *param) ++{ ++ ++ const CKYAppletArgComputeECCKeyAgreement *ccs=(const CKYAppletArgComputeECCKeyAgreement *)param; ++ return CKYAPDUFactory_ComputeECCKeyAgreementOneStep(apdu, ccs->keyNumber, ccs->location, ccs->publicValue, ccs->secretKey); ++} ++ ++CKYStatus + CKYAppletFactory_CreatePIN(CKYAPDU *apdu, const void *param) + { + const CKYAppletArgCreatePIN *cps = (const CKYAppletArgCreatePIN *)param; +@@ -245,10 +261,25 @@ CACAppletFactory_SignDecryptFinal(CKYAPD + } + + CKYStatus ++PIVAppletFactory_SignDecrypt(CKYAPDU *apdu, const void *param) ++{ ++ const PIVAppletArgSignDecrypt *psd = (const PIVAppletArgSignDecrypt *)param; ++ return PIVAPDUFactory_SignDecrypt(apdu, psd->chain, psd->alg, psd->key, ++ psd->len, psd->buf); ++} ++ ++CKYStatus + CACAppletFactory_VerifyPIN(CKYAPDU *apdu, const void *param) + { + const char *pin=(const char *)param; +- return CACAPDUFactory_VerifyPIN(apdu, pin); ++ return CACAPDUFactory_VerifyPIN(apdu, CAC_LOGIN_GLOBAL, pin); ++} ++ ++CKYStatus ++PIVAppletFactory_VerifyPIN(CKYAPDU *apdu, const void *param) ++{ ++ const char *pin=(const char *)param; ++ return CACAPDUFactory_VerifyPIN(apdu, PIV_LOGIN_LOCAL, pin); + } + + CKYStatus +@@ -259,6 +290,13 @@ CACAppletFactory_GetCertificate(CKYAPDU + } + + CKYStatus ++PIVAppletFactory_GetCertificate(CKYAPDU *apdu, const void *param) ++{ ++ CKYBuffer *tag =(CKYBuffer*)param; ++ return PIVAPDUFactory_GetData(apdu, tag, 0); ++} ++ ++CKYStatus + CACAppletFactory_ReadFile(CKYAPDU *apdu, const void *param) + { + const CACAppletArgReadFile *rfs = (const CACAppletArgReadFile *)param; +@@ -325,6 +363,7 @@ CKYAppletFill_AppendBuffer(const CKYBuff + CKYBuffer_Size(response) -2); + } + ++ + CKYStatus + CKYAppletFill_Byte(const CKYBuffer *response, CKYSize size, void *param) + { +@@ -725,6 +764,32 @@ CKYApplet_ComputeCryptProcess(CKYCardCon + &ccd, nonce, 0, CKYAppletFill_Null, NULL, apduRC); + } + ++/* computeECCValue returns data in the form : ++ * len: short ++ * data: byte[len] ++ * This fill routine returns A buffer with a copy of data and a length of len */ ++static CKYStatus ++ckyAppletFill_ComputeECCValueFinal(const CKYBuffer *response, ++ CKYSize size, void *param) ++{ ++ CKYBuffer *cbuf = (CKYBuffer *)param; ++ CKYSize respSize = CKYBuffer_Size(response); ++ CKYSize dataLen; ++ ++ if (cbuf == 0) { ++ return CKYSUCCESS; /* app didn't want the result */ ++ } ++ /* data response code + length code */ ++ if (respSize < 4) { ++ return CKYAPDUFAIL; ++ } ++ dataLen = CKYBuffer_GetShort(response, 0); ++ if (dataLen > (respSize-4)) { ++ return CKYAPDUFAIL; ++ } ++ return CKYBuffer_Replace(cbuf, 0, CKYBuffer_Data(response)+2, dataLen); ++} ++ + /* computeCrypt returns data in the form : + * len: short + * data: byte[len] +@@ -872,6 +937,77 @@ fail: + return ret; + } + ++CKYStatus ++CKYApplet_ComputeECCKeyAgreement(CKYCardConnection *conn, CKYByte keyNumber, ++ const CKYBuffer *publicValue, CKYBuffer *sharedSecret, ++ CKYBuffer *result, const CKYBuffer *nonce, CKYISOStatus *apduRC) ++{ ++ CKYStatus ret = CKYAPDUFAIL; ++ CKYAppletArgComputeECCKeyAgreement ccd; ++ CKYBuffer empty; ++ CKYISOStatus status; ++ /* Routine creates a sym key, should easily fit in one apdu */ ++ ++ CKYBuffer_InitEmpty(&empty); ++ ccd.keyNumber = keyNumber; ++ ccd.location = CKY_DL_APDU; ++ ++ if (!apduRC) ++ apduRC = &status; ++ ++ if (ccd.location == CKY_DL_APDU) { ++ ccd.publicValue = publicValue; ++ ccd.secretKey = sharedSecret; ++ ret = CKYApplet_HandleAPDU(conn, ++ CKYAppletFactory_ComputeECCKeyAgreementOneStep, &ccd, nonce, ++ CKY_SIZE_UNKNOWN, ckyAppletFill_ComputeECCValueFinal, ++ result, apduRC); ++ if (ret == CKYAPDUFAIL && *apduRC == CKYISO_INCORRECT_P2) { ++ return ret; ++ } ++ } ++ ++ return ret; ++} ++ ++CKYStatus ++CKYApplet_ComputeECCSignature(CKYCardConnection *conn, CKYByte keyNumber, ++ const CKYBuffer *data, CKYBuffer *sig, ++ CKYBuffer *result, const CKYBuffer *nonce, CKYISOStatus *apduRC) ++{ ++ int use2APDUs = 0; ++ int use_dl_object = 0; ++ short dataSize = 0; ++ CKYStatus ret = CKYAPDUFAIL; ++ CKYAppletArgComputeECCSignature ccd; ++ CKYBuffer empty; ++ CKYISOStatus status; ++ ++ CKYBuffer_InitEmpty(&empty); ++ ccd.keyNumber = keyNumber; ++ ++ /* Assume APDU, the signature can only get so big with our key sizes, ~ 130 for 521 bit key. */ ++ ccd.location = CKY_DL_APDU; ++ ++ if (!apduRC) ++ apduRC = &status; ++ ++ if (ccd.location == CKY_DL_APDU) { ++ ccd.data = data; ++ ccd.sig = sig; ++ ret = CKYApplet_HandleAPDU(conn, ++ CKYAppletFactory_ComputeECCSignatureOneStep, &ccd, nonce, ++ CKY_SIZE_UNKNOWN, ckyAppletFill_ComputeECCValueFinal, ++ result, apduRC); ++ if (ret == CKYAPDUFAIL && *apduRC == CKYISO_INCORRECT_P2) { ++ return ret; ++ } ++ ++ } ++ ++ return ret; ++} ++ + /* + * do a CAC Sign/Decrypt + */ +@@ -920,7 +1056,7 @@ done: + * do a CAC VerifyPIN + */ + CKYStatus +-CACApplet_VerifyPIN(CKYCardConnection *conn, const char *pin, ++CACApplet_VerifyPIN(CKYCardConnection *conn, const char *pin, int local, + CKYISOStatus *apduRC) + { + CKYStatus ret; +@@ -929,7 +1065,7 @@ CACApplet_VerifyPIN(CKYCardConnection *c + apduRC = &status; + } + +- ret = CKYApplet_HandleAPDU(conn, ++ ret = CKYApplet_HandleAPDU(conn, local ? PIVAppletFactory_VerifyPIN : + CACAppletFactory_VerifyPIN, pin, NULL, + 0, CKYAppletFill_Null, + NULL, apduRC); +@@ -942,6 +1078,7 @@ CACApplet_VerifyPIN(CKYCardConnection *c + return ret; + } + ++ + /* + * Get a CAC Certificate + */ +@@ -1078,6 +1215,278 @@ CACApplet_GetCertificateAppend(CKYCardCo + return ret; + } + ++/* Select the PIV applet */ ++static CKYByte pivAid[] = {0xa0, 0x00, 0x00, 0x03, 0x08, 0x00, 0x00, ++ 0x10, 0x00}; ++CKYStatus ++PIVApplet_Select(CKYCardConnection *conn, CKYISOStatus *apduRC) ++{ ++ CKYStatus ret; ++ CKYBuffer PIV_Applet_AID,return_AID; ++ ++ CKYBuffer_InitEmpty(&return_AID); ++ CKYBuffer_InitFromData(&PIV_Applet_AID, pivAid, sizeof(pivAid)); ++ ret = CKYApplet_HandleAPDU(conn, CKYAppletFactory_SelectFile, ++ &PIV_Applet_AID, ++ NULL, CKY_SIZE_UNKNOWN, CKYAppletFill_AppendBuffer, ++ &return_AID, apduRC); ++ /* Some cards return OK, but don't switch to our applet */ ++ /* PIV has a well defined return for it's select, check to see if we have ++ * a PIV card here */ ++ if (CKYBuffer_GetChar(&return_AID,0) != 0x61) { ++ /* not an application property template, so not a PIV. We could ++ * check that the aid tag (0x4f) and theallocation authority tag (0x79) ++ * are present, but what we are really avoiding is broken cards that ++ * lie about being able to switch to a particular applet, so the first ++ * tag should be sufficient */ ++ ret = CKYAPDUFAIL; /* what we should have gotten */ ++ } ++ CKYBuffer_FreeData(&PIV_Applet_AID); ++ CKYBuffer_FreeData(&return_AID); ++ return ret; ++} ++ ++/* ++ * Get a PIV Certificate ++ */ ++CKYStatus ++PIVApplet_GetCertificate(CKYCardConnection *conn, CKYBuffer *cert, int tag, ++ CKYISOStatus *apduRC) ++{ ++ CKYStatus ret; ++ CKYISOStatus status; ++ CKYBuffer tagBuf; ++ ++ CKYBuffer_InitEmpty(&tagBuf); ++ CKYBuffer_Reserve(&tagBuf,4); /* can be up to 4 bytes */ ++ ++ CKYBuffer_Resize(cert,0); ++ if (apduRC == NULL) { ++ apduRC = &status; ++ } ++ if (tag >= 0x01000000) { ++ ret = CKYBuffer_AppendChar(&tagBuf, (tag >> 24) & 0xff); ++ if (ret != CKYSUCCESS) { goto loser; } ++ } ++ if (tag >= 0x010000) { ++ ret = CKYBuffer_AppendChar(&tagBuf, (tag >> 16) & 0xff); ++ if (ret != CKYSUCCESS) { goto loser; } ++ } ++ if (tag >= 0x0100) { ++ ret =CKYBuffer_AppendChar(&tagBuf, (tag >> 8) & 0xff); ++ if (ret != CKYSUCCESS) { goto loser; } ++ } ++ ret = CKYBuffer_AppendChar(&tagBuf, tag & 0xff); ++ if (ret != CKYSUCCESS) { goto loser; } ++ ++ ++ ret = CKYApplet_HandleAPDU(conn, ++ PIVAppletFactory_GetCertificate, &tagBuf, NULL, ++ CKY_SIZE_UNKNOWN, CKYAppletFill_AppendBuffer, cert, ++ apduRC); ++loser: ++ CKYBuffer_FreeData(&tagBuf); ++ ++ return ret; ++} ++ ++ ++/* ++ * record the next ber tag and length. NOTE: this is a state machine. ++ * we can handle the case where we are passed the data just one byte ++ * at a time. ++ */ ++static CKYStatus ++pivUnwrap(const CKYBuffer *buf, CKYOffset *offset, ++ CKYSize *dataSize, PIVUnwrapState *unwrap) ++{ ++ if (unwrap->tag == 0) { ++ unwrap->tag = CKYBuffer_GetChar(buf, *offset); ++ if (unwrap->tag == 0) unwrap->tag = 0xff; ++ (*offset)++; ++ (*dataSize)--; ++ } ++ if (*dataSize == 0) { ++ return CKYSUCCESS; ++ } ++ if (unwrap->length_bytes != 0) { ++ int len; ++ if (unwrap->length_bytes == -1) { ++ len = CKYBuffer_GetChar(buf, *offset); ++ unwrap->length_bytes = 0; ++ unwrap->length = len; ++ (*offset)++; ++ (*dataSize)--; ++ if (len & 0x80) { ++ unwrap->length = 0; ++ unwrap->length_bytes = len & 0x7f; ++ } ++ } ++ while ((*dataSize != 0) && (unwrap->length_bytes != 0)) { ++ len = CKYBuffer_GetChar(buf, *offset); ++ (*offset) ++; ++ (*dataSize) --; ++ unwrap->length = ((unwrap->length) << 8 | len); ++ unwrap->length_bytes--; ++ } ++ } ++ return CKYSUCCESS; ++} ++ ++/* ++ * Remove the BER wrapping first... ++ */ ++static CKYStatus ++pivAppletFill_AppendUnwrapBuffer(const CKYBuffer *response, ++ CKYSize size, void *param) ++{ ++ PIVAppletRespSignDecrypt *prsd = (PIVAppletRespSignDecrypt *)param; ++ CKYBuffer *buf = prsd->buf; ++ CKYSize dataSize = CKYBuffer_Size(response); ++ CKYOffset offset = 0; ++ ++ if (dataSize <= 2) { ++ return CKYSUCCESS; ++ } ++ dataSize -= 2; ++ /* remove the first tag */ ++ (void) pivUnwrap(response, &offset, &dataSize, &prsd->tag_1); ++ if (dataSize == 0) { ++ return CKYSUCCESS; ++ } ++ /* remove the second tag */ ++ (void) pivUnwrap(response, &offset, &dataSize, &prsd->tag_2); ++ if (dataSize == 0) { ++ return CKYSUCCESS; ++ } ++ /* the rest is real data */ ++ return CKYBuffer_AppendData(buf, CKYBuffer_Data(response) + offset, ++ dataSize); ++} ++ ++static CKYStatus ++piv_wrapEncodeLength(CKYBuffer *buf, int length, int ber_len) ++{ ++ if (ber_len== 1) { ++ CKYBuffer_AppendChar(buf,length); ++ } else { ++ ber_len--; ++ CKYBuffer_AppendChar(buf,0x80+ber_len); ++ while(ber_len--) { ++ CKYBuffer_AppendChar(buf,(length >> (8*ber_len)) & 0xff); ++ } ++ } ++ return CKYSUCCESS; ++} ++/* ++ * do a PIV Sign/Decrypt ++ */ ++CKYStatus ++PIVApplet_SignDecrypt(CKYCardConnection *conn, CKYByte key, unsigned int keySize, int derive, ++ const CKYBuffer *data, CKYBuffer *result, CKYISOStatus *apduRC) ++{ ++ CKYStatus ret; ++ CKYSize dataSize = CKYBuffer_Size(data); ++ CKYSize outputSize = keySize; ++ CKYOffset offset = 0; ++ CKYBuffer tmp; ++ CKYByte alg; ++ int ber_len_1; ++ int ber_len_2; ++ int length; ++ PIVAppletArgSignDecrypt pasd; ++ PIVAppletRespSignDecrypt prsd; ++ ++ /* PIV only defines RSA 1024 and 2048, ECC 256 and ECC 384!!! */ ++ if (keySize == 128) { /* 1024 bit == 128 bytes */ ++ ber_len_2 = 2; ++ ber_len_1 = 2; ++ alg = 0x6; ++ } else if (keySize == 256) { /* 2048 bits == 256 bytes */ ++ ber_len_2 = 3; ++ ber_len_1 = 3; ++ alg = 0x7; ++ } else if (keySize == 32) { /* 256 bits = 32 bytes */ ++ ber_len_2 = 1; ++ ber_len_1 = 1; ++ alg = 0x11; ++ if (!derive) outputSize = keySize*2; ++ } else if (keySize == 48) { /* 384 bits = 48 bytes */ ++ ber_len_2 = 1; ++ ber_len_1 = 1; ++ alg = 0x14; ++ if (!derive) outputSize = keySize*2; ++ } else { ++ return CKYINVALIDARGS; ++ } ++ ++ CKYBuffer_InitEmpty(&tmp); ++ ret = CKYBuffer_Reserve(&tmp, CKY_MAX_WRITE_CHUNK_SIZE); ++ if (ret != CKYSUCCESS) { ++ goto done; ++ } ++ CKYBuffer_AppendChar(&tmp,0x7c); ++ piv_wrapEncodeLength(&tmp,dataSize + ber_len_2 + 3,ber_len_1); ++ CKYBuffer_AppendChar(&tmp,0x82); ++ CKYBuffer_AppendChar(&tmp,0x0); ++ CKYBuffer_AppendChar(&tmp, derive ? 0x85 : 0x81); ++ piv_wrapEncodeLength(&tmp,dataSize,ber_len_2); ++ ++ /* now length == header length from here to the end*/ ++ length = CKYBuffer_Size(&tmp); ++ ++ if (length + dataSize > CKY_MAX_WRITE_CHUNK_SIZE) { ++ CKYBuffer_AppendBuffer(&tmp, data, 0, CKY_MAX_WRITE_CHUNK_SIZE-length); ++ } else { ++ CKYBuffer_AppendBuffer(&tmp, data, 0, dataSize); ++ } ++ ++ prsd.tag_1.tag = 0; ++ prsd.tag_1.length_bytes = -1; ++ prsd.tag_1.length = 0; ++ prsd.tag_2.tag = 0; ++ prsd.tag_2.length_bytes = -1; ++ prsd.tag_2.length = 0; ++ prsd.buf = result; ++ pasd.alg = alg; ++ pasd.key = key; ++ pasd.buf = &tmp; ++ ++ CKYBuffer_Resize(result,0); ++ for(offset = -length; (dataSize-offset) > CKY_MAX_WRITE_CHUNK_SIZE; ) { ++ pasd.chain = 1; ++ pasd.len = 0; ++ ret = CKYApplet_HandleAPDU(conn, PIVAppletFactory_SignDecrypt, ++ &pasd, NULL, CKY_SIZE_UNKNOWN, ++ pivAppletFill_AppendUnwrapBuffer, ++ &prsd, apduRC); ++ if (ret != CKYSUCCESS) { ++ goto done; ++ } ++ CKYBuffer_Resize(&tmp,0); ++ /* increment before we append the next tmp buffer */ ++ offset += CKY_MAX_WRITE_CHUNK_SIZE; ++ CKYBuffer_AppendBuffer(&tmp, data, offset, ++ MIN(dataSize-offset, CKY_MAX_WRITE_CHUNK_SIZE)); ++ } ++ ++ pasd.chain = 0; ++ pasd.len = outputSize; ++ ++ ret = CKYApplet_HandleAPDU(conn, PIVAppletFactory_SignDecrypt, ++ &pasd, NULL, CKY_SIZE_UNKNOWN, ++ pivAppletFill_AppendUnwrapBuffer, ++ &prsd, apduRC); ++ ++ if ((ret == CKYSUCCESS) && (CKYBuffer_Size(result) != outputSize)) { ++ /* RSA returns the same data size as input, didn't happen, so ++ * something is wrong. */ ++ } ++ ++done: ++ CKYBuffer_FreeData(&tmp); ++ return ret; ++} + + /* + * PIN cluster +diff -up ./src/libckyapplet/cky_applet.h.piv-ecc ./src/libckyapplet/cky_applet.h +--- ./src/libckyapplet/cky_applet.h.piv-ecc 2013-09-08 15:50:33.099428337 -0700 ++++ ./src/libckyapplet/cky_applet.h 2013-09-08 15:50:33.127428807 -0700 +@@ -43,6 +43,8 @@ typedef unsigned short CKYISOStatus; /* + #define CKYISO_MORE_MASK 0xff00 /* More data mask */ + #define CKYISO_MORE 0x6300 /* More data available */ + #define CKYISO_DATA_INVALID 0x6984 ++#define CKYISO_CONDITION_NOT_SATISFIED 0x6985 /* AKA not logged in (CAC)*/ ++#define CKYISO_SECURITY_NOT_SATISFIED 0x6982 /* AKA not logged in (PIV)*/ + /* Applet Defined Return codes */ + #define CKYISO_NO_MEMORY_LEFT 0x9c01 /* There have been memory + * problems on the card */ +@@ -78,6 +80,7 @@ typedef unsigned short CKYISOStatus; /* + + #define CAC_TAG_CARDURL 0xf3 + #define CAC_TAG_CERTIFICATE 0x70 ++#define CAC_TAG_CERTINFO 0x71 + #define CAC_TLV_APP_PKI 0x04 + + /* +@@ -218,12 +221,47 @@ typedef struct _CKYAppletArgComputeCrypt + const CKYBuffer *sig; + } CKYAppletArgComputeCrypt; + ++typedef struct _CKYAppletArgComputeECCSignature { ++ CKYByte keyNumber; ++ CKYByte location; ++ const CKYBuffer *data; ++ const CKYBuffer *sig; ++} CKYAppletArgComputeECCSignature; ++ ++typedef struct _CKYAppletArgComputeECCKeyAgreement { ++ CKYByte keyNumber; ++ CKYByte location; ++ const CKYBuffer *publicValue; ++ const CKYBuffer *secretKey; ++} CKYAppletArgComputeECCKeyAgreement; ++ ++ + typedef struct _CACAppletArgReadFile { + CKYByte type; + CKYByte count; + unsigned short offset; + } CACAppletArgReadFile; + ++typedef struct _PIVAppletArgSignDecrypt { ++ CKYByte alg; ++ CKYByte key; ++ CKYByte chain; ++ CKYSize len; ++ CKYBuffer *buf; ++} PIVAppletArgSignDecrypt; ++ ++typedef struct _pivUnwrapState { ++ CKYByte tag; ++ CKYByte length; ++ int length_bytes; ++} PIVUnwrapState; ++ ++typedef struct _PIVAppletRespSignDecrypt { ++ PIVUnwrapState tag_1; ++ PIVUnwrapState tag_2; ++ CKYBuffer *buf; ++} PIVAppletRespSignDecrypt; ++ + /* fills in an APDU from a structure -- form of all the generic factories*/ + typedef CKYStatus (*CKYAppletFactory)(CKYAPDU *apdu, const void *param); + /* fills in an a structure from a response -- form of all the fill structures*/ +@@ -335,7 +373,6 @@ CKYStatus CKYAppletFill_AppendBuffer(con + /* Single value fills: Byte, Short, & Long */ + /* param == CKYByte * */ + CKYStatus CKYAppletFill_Byte(const CKYBuffer *response, CKYSize size, void *param); +-/* param == CKYByte * */ + CKYStatus CKYAppletFill_Short(const CKYBuffer *response, CKYSize size, void *param); + CKYStatus CKYAppletFill_Long(const CKYBuffer *response, CKYSize size, void *param); + +@@ -361,7 +398,7 @@ CKYBool CKYApplet_VerifyResponse(const C + * Sends the ADPU to the card through the connection conn. + * Checks that the response was valid (returning the responce code in apduRC. + * Formats the response data into fillArg with fillFunc +- * nonce and apduRC can be NULL (no nonce is added, not status returned ++ * nonce and apduRC can be NULL (no nonce is added, no status returned + * legal values for afArg are depened on afFunc. + * legal values for fillArg are depened on fillFunc. + */ +@@ -377,7 +414,7 @@ CKYStatus CKYApplet_HandleAPDU(CKYCardCo + * into function calls, with input and output parameters. + * The application is still responsible for + * 1) creating a connection to the card, +- * 2) Getting a tranaction long, then ++ * 2) Getting a transaction lock, then + * 3) selecting the appropriate applet (or Card manager). + * Except for those calls that have been noted, the appropriate applet + * is the CoolKey applet. +@@ -490,9 +527,18 @@ CKYStatus CACApplet_GetCertificateAppend + CKYISOStatus *apduRC); + + /*CKYStatus CACApplet_GetProperties(); */ +-CKYStatus CACApplet_VerifyPIN(CKYCardConnection *conn, const char *pin, +- CKYISOStatus *apduRC); ++CKYStatus CACApplet_VerifyPIN(CKYCardConnection *conn, const char *pin, ++ int local, CKYISOStatus *apduRC); + ++/* Select a PIV applet */ ++CKYStatus PIVApplet_Select(CKYCardConnection *conn, CKYISOStatus *apduRC); ++ ++CKYStatus PIVApplet_GetCertificate(CKYCardConnection *conn, CKYBuffer *cert, ++ int tag, CKYISOStatus *apduRC); ++CKYStatus PIVApplet_SignDecrypt(CKYCardConnection *conn, CKYByte key, ++ unsigned int keySize, int derive, ++ const CKYBuffer *data, CKYBuffer *result, ++ CKYISOStatus *apduRC); + /* + * There are 3 read commands: + * +@@ -553,6 +599,18 @@ CKYStatus CKYApplet_GetIssuerInfo(CKYCar + CKYStatus CKYApplet_GetBuiltinACL(CKYCardConnection *conn, + CKYAppletRespGetBuiltinACL *gba, CKYISOStatus *apduRC); + ++/** ECC commands ++ * * */ ++ ++CKYStatus CKYApplet_ComputeECCSignature(CKYCardConnection *conn, CKYByte keyNumber, ++ const CKYBuffer *data, CKYBuffer *sig, ++ CKYBuffer *result, const CKYBuffer *nonce, CKYISOStatus *apduRC); ++ ++CKYStatus ++CKYApplet_ComputeECCKeyAgreement(CKYCardConnection *conn, CKYByte keyNumber, ++ const CKYBuffer *publicValue, CKYBuffer *sharedSecret, ++ CKYBuffer *result, const CKYBuffer *nonce, CKYISOStatus *apduRC); ++ + + /* + * deprecates 0.x functions +diff -up ./src/libckyapplet/cky_base.c.piv-ecc ./src/libckyapplet/cky_base.c +--- ./src/libckyapplet/cky_base.c.piv-ecc 2013-09-08 15:50:33.100428354 -0700 ++++ ./src/libckyapplet/cky_base.c 2013-09-08 15:50:33.128428824 -0700 +@@ -41,6 +41,7 @@ ckyBuffer_initBuffer(CKYBuffer *buf) + buf->data = NULL; + buf->size = 0; + buf->len = 0; ++ buf->reserved = NULL; /* make coverity happy */ + } + + /* +@@ -573,6 +574,7 @@ CKYAPDU_Init(CKYAPDU *apdu) + assert(sizeof(CKYAPDU) == sizeof(CKYAPDUPublic)); + #endif + ckyBuffer_initBuffer(&apdu->apduBuf); ++ apdu->reserved = NULL; + return CKYBuffer_Resize(&apdu->apduBuf, CKYAPDU_MIN_LEN); + } + +@@ -583,6 +585,7 @@ CKYAPDU_InitFromData(CKYAPDU *apdu, cons + assert(sizeof(CKYAPDU) == sizeof(CKYAPDUPublic)); + #endif + ckyBuffer_initBuffer(&apdu->apduBuf); ++ apdu->reserved = NULL; + if (len > CKYAPDU_MAX_DATA_LEN) { + return CKYDATATOOLONG; + } +@@ -710,8 +713,15 @@ CKYAPDU_SetReceiveLen(CKYAPDU *apdu, CKY + return CKYBuffer_SetChar(&apdu->apduBuf, CKY_LE_OFFSET, recvlen); + } + ++CKYStatus ++CKYAPDU_AppendReceiveLen(CKYAPDU *apdu, CKYByte recvlen) ++{ ++ return CKYBuffer_AppendChar(&apdu->apduBuf, recvlen); ++} ++ ++ + void +-CKY_SetName(char *p) ++CKY_SetName(const char *p) + { + } + +diff -up ./src/libckyapplet/cky_base.h.piv-ecc ./src/libckyapplet/cky_base.h +--- ./src/libckyapplet/cky_base.h.piv-ecc 2013-09-08 15:50:33.100428354 -0700 ++++ ./src/libckyapplet/cky_base.h 2013-09-08 15:50:33.128428824 -0700 +@@ -278,9 +278,10 @@ CKYStatus CKYAPDU_AppendSendDataBuffer(C + /* set Le in the APDU header to the amount of bytes expected to be + * returned. */ + CKYStatus CKYAPDU_SetReceiveLen(CKYAPDU *apdu, CKYByte recvlen); ++CKYStatus CKYAPDU_AppendReceiveLen(CKYAPDU *apdu, CKYByte recvlen); + + /* set the parent loadmodule name */ +-void CKY_SetName(char *name); ++void CKY_SetName(const char *name); + + CKY_END_PROTOS + +diff -up ./src/libckyapplet/cky_card.c.piv-ecc ./src/libckyapplet/cky_card.c +--- ./src/libckyapplet/cky_card.c.piv-ecc 2013-09-08 15:50:33.109428505 -0700 ++++ ./src/libckyapplet/cky_card.c 2013-09-08 15:50:33.128428824 -0700 +@@ -27,6 +27,7 @@ + + #ifndef WINAPI + #define WINAPI ++typedef SCARD_READERSTATE *LPSCARD_READERSTATE; + #endif + + #ifndef SCARD_E_NO_READERS_AVAILABLE +@@ -843,6 +844,11 @@ CKYCardContext_WaitForStatusChange(CKYCa + rv = ctx->scard->SCardGetStatusChange(ctx->context, timeout, + readers, readerCount); + if (rv != SCARD_S_SUCCESS) { ++ if ((rv == SCARD_E_NO_SERVICE) || (rv == SCARD_E_SERVICE_STOPPED)) { ++ /* if we were stopped, don't reuse the old context, ++ * pcsc-lite hangs */ ++ ckyCardContext_release(ctx); ++ } + ctx->lastError = rv; + return CKYSCARDERR; + } +@@ -1071,25 +1077,39 @@ CKYCardConnection_ExchangeAPDU(CKYCardCo + CKYBuffer *response) + { + CKYStatus ret; ++ CKYBuffer getResponse; ++ CKYSize size = 0; + + ret = CKYCardConnection_TransmitAPDU(conn, apdu, response); + if (ret != CKYSUCCESS) { + return ret; + } ++ CKYBuffer_InitEmpty(&getResponse); + +- if (CKYBuffer_Size(response) == 2 && CKYBuffer_GetChar(response,0) == 0x61) { ++ /* automatically handle the response data protocol */ ++ while ((ret == CKYSUCCESS) && ++ (size = CKYBuffer_Size(response)) >= 2 && ++ (CKYBuffer_GetChar(response,size-2) == 0x61)) { + /* get the response */ + CKYAPDU getResponseAPDU; + ++ CKYBuffer_Zero(&getResponse); + CKYAPDU_Init(&getResponseAPDU); + CKYAPDU_SetCLA(&getResponseAPDU, 0x00); + CKYAPDU_SetINS(&getResponseAPDU, 0xc0); + CKYAPDU_SetP1(&getResponseAPDU, 0x00); + CKYAPDU_SetP2(&getResponseAPDU, 0x00); +- CKYAPDU_SetReceiveLen(&getResponseAPDU, CKYBuffer_GetChar(response,1)); +- ret = CKYCardConnection_TransmitAPDU(conn, &getResponseAPDU, response); ++ CKYAPDU_SetReceiveLen(&getResponseAPDU, ++ CKYBuffer_GetChar(response,size-1)); ++ ret = CKYCardConnection_TransmitAPDU(conn, &getResponseAPDU, ++ &getResponse); + CKYAPDU_FreeData(&getResponseAPDU); ++ if ((ret == CKYSUCCESS) && (CKYBuffer_Size(&getResponse) >= 2)) { ++ CKYBuffer_Resize(response, size-2); ++ CKYBuffer_AppendCopy(response,&getResponse); ++ } + } ++ CKYBuffer_FreeData(&getResponse); + return ret; + } + +diff -up ./src/libckyapplet/cky_card.h.piv-ecc ./src/libckyapplet/cky_card.h +diff -up ./src/libckyapplet/cky_factory.c.piv-ecc ./src/libckyapplet/cky_factory.c +--- ./src/libckyapplet/cky_factory.c.piv-ecc 2013-09-08 15:50:33.101428370 -0700 ++++ ./src/libckyapplet/cky_factory.c 2013-09-08 15:50:33.129428841 -0700 +@@ -62,7 +62,6 @@ CKYAPDUFactory_GetCPLCData(CKYAPDU *apdu + CKYAPDU_SetP2(apdu, 0x7f); + return CKYAPDU_SetReceiveLen(apdu, CKY_SIZE_GET_CPLCDATA); + } +- + /* + * applet commands must be issued with the appplet selected. + */ +@@ -184,6 +183,49 @@ fail: + } + + CKYStatus ++CKYAPDUFactory_ComputeECCKeyAgreementOneStep(CKYAPDU *apdu, CKYByte keyNumber, ++ CKYByte location, ++ const CKYBuffer *publicData, const CKYBuffer *secretKey) ++{ ++ CKYStatus ret = CKYINVALIDARGS; ++ CKYSize len; ++ CKYBuffer buf; ++ ++ if (!publicData) ++ return ret; ++ ++ if (!(len = CKYBuffer_Size(publicData))) ++ return ret; ++ ++ CKYAPDU_SetCLA(apdu, CKY_CLASS_COOLKEY); ++ CKYAPDU_SetINS(apdu, CKY_INS_COMPUTE_ECC_KEY_AGREEMENT); ++ CKYAPDU_SetP1(apdu, keyNumber); ++ CKYAPDU_SetP2(apdu, CKY_CIPHER_ONE_STEP); ++ ++ CKYBuffer_InitEmpty(&buf); ++ ++ ret = CKYBuffer_Reserve(&buf, 3); ++ ++ if (ret == CKYSUCCESS) ++ ret = CKYBuffer_AppendChar(&buf, location); ++ if (ret == CKYSUCCESS) ++ ret = CKYBuffer_AppendShort(&buf, (unsigned short)len); ++ if (ret == CKYSUCCESS) ++ ret = CKYAPDU_SetSendDataBuffer(apdu, &buf); ++ if (ret == CKYSUCCESS) ++ ret = CKYAPDU_AppendSendDataBuffer(apdu, publicData); ++ if (ret == CKYSUCCESS && secretKey && 0 < (len = CKYBuffer_Size(secretKey))) { ++ CKYBuffer_Resize(&buf,2); ++ CKYBuffer_SetShort(&buf, 0, (unsigned short)len); ++ ret = CKYAPDU_AppendSendDataBuffer(apdu, &buf); ++ if (ret == CKYSUCCESS) ++ ret = CKYAPDU_AppendSendDataBuffer(apdu, secretKey); ++ } ++ CKYBuffer_FreeData(&buf); ++ return ret; ++} ++ ++CKYStatus + CKYAPDUFactory_ComputeCryptOneStep(CKYAPDU *apdu, CKYByte keyNumber, CKYByte mode, + CKYByte direction, CKYByte location, + const CKYBuffer *idata, const CKYBuffer *sig) +@@ -386,6 +428,49 @@ fail: + } + + CKYStatus ++CKYAPDUFactory_ComputeECCSignatureOneStep(CKYAPDU *apdu, CKYByte keyNumber, ++ CKYByte location, ++ const CKYBuffer *idata, const CKYBuffer *sig) ++{ ++ CKYStatus ret = CKYINVALIDARGS; ++ CKYSize len; ++ CKYBuffer buf; ++ ++ if (!idata) ++ return ret; ++ ++ if (!(len = CKYBuffer_Size(idata)) && location != CKY_DL_OBJECT) ++ return ret; ++ ++ CKYAPDU_SetCLA(apdu, CKY_CLASS_COOLKEY); ++ CKYAPDU_SetINS(apdu, CKY_INS_COMPUTE_ECC_SIGNATURE); ++ CKYAPDU_SetP1(apdu, keyNumber); ++ CKYAPDU_SetP2(apdu, CKY_CIPHER_ONE_STEP); ++ ++ CKYBuffer_InitEmpty(&buf); ++ ++ ret = CKYBuffer_Reserve(&buf, 3); ++ ++ if (ret == CKYSUCCESS) ++ ret = CKYBuffer_AppendChar(&buf, location); ++ if (ret == CKYSUCCESS) ++ ret = CKYBuffer_AppendShort(&buf, (unsigned short)len); ++ if (ret == CKYSUCCESS) ++ ret = CKYAPDU_SetSendDataBuffer(apdu, &buf); ++ if (ret == CKYSUCCESS) ++ ret = CKYAPDU_AppendSendDataBuffer(apdu, idata); ++ if (ret == CKYSUCCESS && sig && 0 < (len = CKYBuffer_Size(sig))) { ++ CKYBuffer_Resize(&buf,2); ++ CKYBuffer_SetShort(&buf, 0, (unsigned short)len); ++ ret = CKYAPDU_AppendSendDataBuffer(apdu, &buf); ++ if (ret == CKYSUCCESS) ++ ret = CKYAPDU_AppendSendDataBuffer(apdu, sig); ++ } ++ CKYBuffer_FreeData(&buf); ++ return ret; ++} ++ ++CKYStatus + CKYAPDUFactory_ReadObject(CKYAPDU *apdu, unsigned long objectID, + CKYOffset offset, CKYByte size) + { +@@ -622,7 +707,6 @@ fail: + CKYBuffer_FreeData(&buf); + return ret; + } +- + CKYStatus + CACAPDUFactory_GetProperties(CKYAPDU *apdu) + { +@@ -634,7 +718,7 @@ CACAPDUFactory_GetProperties(CKYAPDU *ap + } + + CKYStatus +-CACAPDUFactory_VerifyPIN(CKYAPDU *apdu, const char *pin) ++CACAPDUFactory_VerifyPIN(CKYAPDU *apdu, CKYByte keyRef, const char *pin) + { + CKYStatus ret; + CKYSize size; +@@ -642,7 +726,7 @@ CACAPDUFactory_VerifyPIN(CKYAPDU *apdu, + CKYAPDU_SetCLA(apdu, CKY_CLASS_ISO7816); + CKYAPDU_SetINS(apdu, CAC_INS_VERIFY_PIN); + CKYAPDU_SetP1(apdu, 0x00); +- CKYAPDU_SetP2(apdu, 0x00); ++ CKYAPDU_SetP2(apdu, keyRef); + /* no pin, send an empty buffer */ + if (!pin) { + return CKYAPDU_SetReceiveLen(apdu, 0); +@@ -663,3 +747,63 @@ CACAPDUFactory_VerifyPIN(CKYAPDU *apdu, + return ret; + + } ++ ++CKYStatus ++PIVAPDUFactory_SignDecrypt(CKYAPDU *apdu, CKYByte chain, CKYByte alg, ++ CKYByte key, int len, const CKYBuffer *data) ++{ ++ CKYStatus ret; ++ CKYAPDU_SetCLA(apdu, chain ? CKY_CLASS_ISO7816_CHAIN : ++ CKY_CLASS_ISO7816); ++ CKYAPDU_SetINS(apdu, PIV_INS_GEN_AUTHENTICATE); ++ CKYAPDU_SetP1(apdu, alg); ++ CKYAPDU_SetP2(apdu, key); ++ ret = CKYAPDU_SetSendDataBuffer(apdu, data); ++ if (ret == CKYSUCCESS && chain == 0 && len != 0) { ++ if (len >= 256) len = 0; ++ ret = CKYAPDU_AppendReceiveLen(apdu, len); ++ } ++ return ret; ++} ++ ++CKYStatus ++PIVAPDUFactory_GetData(CKYAPDU *apdu, const CKYBuffer *object, CKYByte count) ++{ ++ CKYStatus ret; ++ CKYBuffer buf; ++ CKYByte objectSize; ++ ++ CKYBuffer_InitEmpty(&buf); ++ CKYAPDU_SetCLA(apdu, CKY_CLASS_ISO7816); ++ CKYAPDU_SetINS(apdu, 0xcb); ++ CKYAPDU_SetP1(apdu, 0x3f); ++ CKYAPDU_SetP2(apdu, 0xff); ++ ++ objectSize = CKYBuffer_Size(object); ++ ++ ret = CKYBuffer_Reserve(&buf, 2+objectSize); ++ if (ret != CKYSUCCESS) { ++ goto fail; ++ } ++ ret = CKYBuffer_AppendChar(&buf, 0x5c); ++ if (ret != CKYSUCCESS) { ++ goto fail; ++ } ++ ret = CKYBuffer_AppendChar(&buf, objectSize); ++ if (ret != CKYSUCCESS) { ++ goto fail; ++ } ++ ret = CKYBuffer_AppendCopy(&buf, object); ++ if (ret != CKYSUCCESS) { ++ goto fail; ++ } ++ ret = CKYAPDU_SetSendDataBuffer(apdu, &buf); ++ if (ret != CKYSUCCESS) { ++ goto fail; ++ } ++ ret = CKYAPDU_AppendReceiveLen(apdu, count); ++fail: ++ CKYBuffer_FreeData(&buf); ++ return ret; ++} ++ +diff -up ./src/libckyapplet/cky_factory.h.piv-ecc ./src/libckyapplet/cky_factory.h +--- ./src/libckyapplet/cky_factory.h.piv-ecc 2013-09-08 15:50:33.101428370 -0700 ++++ ./src/libckyapplet/cky_factory.h 2013-09-08 15:50:33.130428858 -0700 +@@ -25,10 +25,11 @@ + /* + * Various Class bytes + */ +-#define CKY_CLASS_ISO7816 0x00 ++#define CKY_CLASS_ISO7816 0x00 ++#define CKY_CLASS_ISO7816_CHAIN 0x10 + #define CKY_CLASS_GLOBAL_PLATFORM 0x80 +-#define CKY_CLASS_SECURE 0x84 +-#define CKY_CLASS_COOLKEY 0xb0 ++#define CKY_CLASS_SECURE 0x84 ++#define CKY_CLASS_COOLKEY 0xb0 + + /* + * Applet Instruction Bytes +@@ -66,6 +67,8 @@ + /* nonce validated & Secure Channel */ + #define CKY_INS_IMPORT_KEY 0x32 + #define CKY_INS_COMPUTE_CRYPT 0x36 ++#define CKY_INS_COMPUTE_ECC_SIGNATURE 0x37 ++#define CKY_INS_COMPUTE_ECC_KEY_AGREEMENT 0x38 + #define CKY_INS_CREATE_PIN 0x40 + #define CKY_INS_CHANGE_PIN 0x44 + #define CKY_INS_CREATE_OBJ 0x5A +@@ -91,6 +94,12 @@ + #define CAC_SIZE_GET_PROPERTIES 48 + #define CAC_P1_STEP 0x80 + #define CAC_P1_FINAL 0x00 ++#define CAC_LOGIN_GLOBAL 0x00 ++ ++/* PIV */ ++#define PIV_LOGIN_LOCAL 0x80 ++#define PIV_LOGIN_GLOBAL CAC_LOGIN_GLOBAL ++#define PIV_INS_GEN_AUTHENTICATE 0x87 + + /* + * Fixed return sized from various commands +@@ -123,6 +132,7 @@ + #define CKY_DES_ECB_NOPAD 0x21 + + /* operations (Cipher Direction) */ ++#define CKY_DIR_NONE 0x00 + #define CKY_DIR_SIGN 0x01 + #define CKY_DIR_VERIFY 0x02 + #define CKY_DIR_ENCRYPT 0x03 +@@ -187,6 +197,12 @@ CKYStatus CKYAPDUFactory_ComputeCryptFin + CKYStatus CKYAPDUFactory_ComputeCryptOneStep(CKYAPDU *apdu, CKYByte keyNumber, + CKYByte mode, CKYByte direction, CKYByte location, + const CKYBuffer *data, const CKYBuffer *sig); ++CKYStatus CKYAPDUFactory_ComputeECCSignatureOneStep(CKYAPDU *apdu, CKYByte keyNumber, ++ CKYByte location, ++ const CKYBuffer *data, const CKYBuffer *sig); ++CKYStatus CKYAPDUFactory_ComputeECCKeyAgreementOneStep(CKYAPDU *apdu, CKYByte keyNumber, ++ CKYByte location, ++ const CKYBuffer *publicData, const CKYBuffer *secretKey); + CKYStatus CKYAPDUFactory_CreatePIN(CKYAPDU *apdu, CKYByte pinNumber, + CKYByte maxAttempts, const char *pinValue); + CKYStatus CKYAPDUFactory_VerifyPIN(CKYAPDU *apdu, CKYByte pinNumber, +@@ -218,11 +234,16 @@ CKYStatus CKYAPDUFactory_GetBuiltinACL(C + + CKYStatus CACAPDUFactory_SignDecrypt(CKYAPDU *apdu, CKYByte type, + const CKYBuffer *data); +-CKYStatus CACAPDUFactory_VerifyPIN(CKYAPDU *apdu, const char *pin); ++CKYStatus CACAPDUFactory_VerifyPIN(CKYAPDU *apdu, CKYByte keyRef, ++ const char *pin); + CKYStatus CACAPDUFactory_GetCertificate(CKYAPDU *apdu, CKYSize size); + CKYStatus CACAPDUFactory_ReadFile(CKYAPDU *apdu, unsigned short offset, + CKYByte type, CKYByte count); + CKYStatus CACAPDUFactory_GetProperties(CKYAPDU *apdu); ++CKYStatus PIVAPDUFactory_GetData(CKYAPDU *apdu, const CKYBuffer *object, ++ CKYByte count); ++CKYStatus PIVAPDUFactory_SignDecrypt(CKYAPDU *apdu, CKYByte chain, CKYByte alg, ++ CKYByte key, int len, const CKYBuffer *data); + + CKY_END_PROTOS + diff --git a/SOURCES/coolkey-simple-bugs.patch b/SOURCES/coolkey-simple-bugs.patch new file mode 100644 index 0000000..f8f3ab7 --- /dev/null +++ b/SOURCES/coolkey-simple-bugs.patch @@ -0,0 +1,71 @@ +diff -up ./configure.in.coolkey-simple-bugs ./configure.in +--- ./configure.in.coolkey-simple-bugs 2009-09-16 11:21:55.621493844 -0700 ++++ ./configure.in 2009-09-16 11:22:23.354492383 -0700 +@@ -124,9 +124,9 @@ then + if test $WINDOWS -ne 1; then + PKG_CHECK_MODULES(NSS, nss, true, [ AC_MSG_ERROR(could not find NSS Crypto libraries) ]) + fi +- enable_pk11install = "yes" ++ enable_pk11install="yes" + else +- enable_pk11install = "no" ++ enable_pk11install="no" + AC_MSG_WARN([skipping pk11install]) + fi + +diff -up ./Makefile.am.coolkey-simple-bugs ./Makefile.am +--- ./Makefile.am.coolkey-simple-bugs 2009-09-16 11:23:18.715515063 -0700 ++++ ./Makefile.am 2009-09-16 13:15:29.570492412 -0700 +@@ -25,7 +25,6 @@ if BUILD_PK11INSTALL + SUBDIRS += src/install + endif + +-ACLOCAL_AMFLAGS = -I m4 + + EXTRA_DIST = cookey.spec LICENSE + +diff -up ./src/coolkey/object.cpp.coolkey-simple-bugs ./src/coolkey/object.cpp +--- ./src/coolkey/object.cpp.coolkey-simple-bugs 2009-09-16 10:36:29.300516245 -0700 ++++ ./src/coolkey/object.cpp 2009-09-16 10:37:17.747492199 -0700 +@@ -397,7 +397,7 @@ PKCS11Object::getLabel() + { + // clean up old one + if (label) { +- delete label; ++ delete [] label; + label = NULL; + } + // find matching attribute +diff -up ./src/coolkey/object.h.coolkey-simple-bugs ./src/coolkey/object.h +--- ./src/coolkey/object.h.coolkey-simple-bugs 2009-09-16 16:05:27.233488140 -0700 ++++ ./src/coolkey/object.h 2009-09-16 16:05:54.161492421 -0700 +@@ -82,7 +82,7 @@ class PKCS11Object { + PKCS11Object(unsigned long muscleObjID, CK_OBJECT_HANDLE handle); + PKCS11Object(unsigned long muscleObjID, const CKYBuffer *data, + CK_OBJECT_HANDLE handle); +- ~PKCS11Object() { delete label; delete name; CKYBuffer_FreeData(&pubKey); } ++ ~PKCS11Object() { delete [] label; delete [] name; CKYBuffer_FreeData(&pubKey); } + + PKCS11Object(const PKCS11Object& cpy) : + attributes(cpy.attributes), muscleObjID(cpy.muscleObjID), +diff -up ./src/coolkey/slot.cpp.coolkey-simple-bugs ./src/coolkey/slot.cpp +--- ./src/coolkey/slot.cpp.coolkey-simple-bugs 2009-09-16 10:28:15.412492201 -0700 ++++ ./src/coolkey/slot.cpp 2009-09-16 10:57:27.692492487 -0700 +@@ -979,7 +979,7 @@ Slot::makeLabelString(char *label, int m + // + #define COOLKEY "CoolKey" + #define POSSESSION " for " +- if (!personName || personName == "") { ++ if (!personName || personName[0] == '\0' ) { + const int coolKeySize = sizeof(COOLKEY) ; + memcpy(label, COOLKEY, coolKeySize-1); + makeSerialString(&label[coolKeySize], maxSize-coolKeySize, cuid); +@@ -1528,7 +1528,7 @@ SlotMemSegment::SlotMemSegment(const cha + } + sprintf(segName,SEGMENT_PREFIX"%s",readerName); + segment = SHMem::initSegment(segName, MAX_OBJECT_STORE_SIZE, needInit); +- delete segName; ++ delete [] segName; + if (!segment) { + // just run without shared memory + return; diff --git a/SOURCES/coolkey-thread-fix.patch b/SOURCES/coolkey-thread-fix.patch new file mode 100644 index 0000000..e3b552a --- /dev/null +++ b/SOURCES/coolkey-thread-fix.patch @@ -0,0 +1,158 @@ +Index: src/coolkey/coolkey.cpp +=================================================================== +RCS file: /cvs/dirsec/coolkey/src/coolkey/coolkey.cpp,v +retrieving revision 1.2 +diff -u -r1.2 coolkey.cpp +--- src/coolkey/coolkey.cpp 14 Feb 2007 19:54:01 -0000 1.2 ++++ src/coolkey/coolkey.cpp 18 Dec 2009 23:22:58 -0000 +@@ -42,7 +42,9 @@ + + static SlotList *slotList = NULL; + +-static OSLock finalizeLock(false); ++static OSLock *finalizeLock = NULL; ++#define FINALIZE_GETLOCK() if (finalizeLock) finalizeLock->getLock(); ++#define FINALIZE_RELEASELOCK() if (finalizeLock) finalizeLock->releaseLock(); + + static CK_BBOOL initialized = FALSE; + static CK_BBOOL finalizing = FALSE; +@@ -208,11 +210,13 @@ + if( initialized ) { + return CKR_CRYPTOKI_ALREADY_INITIALIZED; + } +- if (!finalizeLock.isValid()) { ++ if (finalizeLock && !finalizeLock->isValid()) { + return CKR_CANT_LOCK; + } + CK_C_INITIALIZE_ARGS* initArgs = (CK_C_INITIALIZE_ARGS*) pInitArgs; ++ OSLock::setThreadSafe(0); + if( initArgs != NULL ) { ++ bool needThreads; + /* work around a bug in NSS where the library parameters are only + * send if locking is requested */ + if (initArgs->LibraryParameters) { +@@ -220,7 +224,17 @@ + } else { + Params::ClearParams(); + } +- if( (initArgs->flags & CKF_OS_LOCKING_OK) || initArgs->LockMutex ){ ++ needThreads = ((initArgs->flags & CKF_OS_LOCKING_OK) != 0); ++ OSLock::setThreadSafe(needThreads); ++ /* don't get a finalize lock unless someone initializes us asking ++ * us to use threads */ ++ if (needThreads && !finalizeLock) { ++ finalizeLock = new OSLock(true); ++ if (finalizeLock == NULL) return CKR_HOST_MEMORY; ++ } ++ /* only support OS LOCKING threads */ ++ if( ((initArgs->flags & CKF_OS_LOCKING_OK) == 0) ++ && initArgs->LockMutex ){ + throw PKCS11Exception(CKR_CANT_LOCK); + } + } +@@ -259,9 +273,9 @@ + // the finalizing call first, we know it will set waitEvent before + // we can get the lock, so we only need to protect setting finalizing + // to true. +- finalizeLock.getLock(); ++ FINALIZE_GETLOCK(); + finalizing = TRUE; +- finalizeLock.releaseLock(); ++ FINALIZE_RELEASELOCK(); + if (waitEvent) { + /* we're waiting on a slot event, shutdown first to allow + * the wait function to complete before we pull the rug out. +@@ -273,10 +287,10 @@ + } + delete slotList; + delete log; +- finalizeLock.getLock(); ++ FINALIZE_GETLOCK(); + finalizing = FALSE; + initialized = FALSE; +- finalizeLock.releaseLock(); ++ FINALIZE_RELEASELOCK(); + return CKR_OK; + } + +@@ -595,17 +609,17 @@ + CK_RV + C_WaitForSlotEvent(CK_FLAGS flags, CK_SLOT_ID_PTR pSlot, CK_VOID_PTR pReserved) + { +- finalizeLock.getLock(); ++ FINALIZE_GETLOCK(); + if( ! initialized ) { +- finalizeLock.releaseLock(); ++ FINALIZE_RELEASELOCK(); + return CKR_CRYPTOKI_NOT_INITIALIZED; + } + if (finalizing) { +- finalizeLock.releaseLock(); ++ FINALIZE_RELEASELOCK(); + return CKR_CRYPTOKI_NOT_INITIALIZED; + } + waitEvent = TRUE; +- finalizeLock.releaseLock(); ++ FINALIZE_RELEASELOCK(); + try { + log->log("C_WaitForSlotEvent called\n"); + slotList->waitForSlotEvent(flags, pSlot, pReserved); +Index: src/coolkey/machdep.cpp +=================================================================== +RCS file: /cvs/dirsec/coolkey/src/coolkey/machdep.cpp,v +retrieving revision 1.7 +diff -u -r1.7 machdep.cpp +--- src/coolkey/machdep.cpp 14 Feb 2008 23:48:19 -0000 1.7 ++++ src/coolkey/machdep.cpp 18 Dec 2009 23:22:58 -0000 +@@ -37,6 +37,8 @@ + #include + #endif + ++bool OSLock::needThread = 0; ++ + #ifdef _WIN32 + // + // Windows functions to grab a named shared memory segment of a specific size, +@@ -123,6 +125,10 @@ + + OSLock::OSLock(bool exceptionAllowed) + { ++ if (!needThread) { ++ lockData = NULL; ++ return; ++ } + lockData = new OSLockData; + if (lockData) { + InitializeCriticalSection(&lockData->mutex); +@@ -360,6 +366,9 @@ + int rc; + + lockData = NULL; ++ if (!needThread) { ++ return; ++ } + #ifdef MAC + if (!OSLock_attr_init) { + rc = pthread_mutexattr_init(&OSLock_attr); +Index: src/coolkey/machdep.h +=================================================================== +RCS file: /cvs/dirsec/coolkey/src/coolkey/machdep.h,v +retrieving revision 1.1 +diff -u -r1.1 machdep.h +--- src/coolkey/machdep.h 9 Jun 2006 18:39:11 -0000 1.1 ++++ src/coolkey/machdep.h 18 Dec 2009 23:22:58 -0000 +@@ -40,12 +40,14 @@ + class OSLock { + private: + OSLockData *lockData; ++ static bool needThread; + public: + OSLock(bool exceptionAllowed = true); + ~OSLock(); + bool isValid(); + void getLock(); + void releaseLock(); ++ static void setThreadSafe(bool thread) { needThread = thread; } + }; + + typedef unsigned long OSTime; diff --git a/SPECS/coolkey.spec b/SPECS/coolkey.spec new file mode 100644 index 0000000..771f49a --- /dev/null +++ b/SPECS/coolkey.spec @@ -0,0 +1,258 @@ +# BEGIN COPYRIGHT BLOCK +# Copyright (C) 2005 Red Hat, Inc. +# All rights reserved. +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation version +# 2.1 of the License. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +# END COPYRIGHT BLOCK + +%define coolkey_module "CoolKey PKCS #11 Module" +%define nssdb %{_sysconfdir}/pki/nssdb + +Name: coolkey +Version: 1.1.0 +Release: 25%{?dist} +Summary: CoolKey PKCS #11 module +License: LGPLv2 +URL: http://directory.fedora.redhat.com/wiki/CoolKey +Source: coolkey-%{version}.tar.gz +Patch1: coolkey-cache-dir-move.patch +Patch2: coolkey-gcc43.patch +Patch3: coolkey-latest.patch +Patch4: coolkey-simple-bugs.patch +Patch5: coolkey-thread-fix.patch +Patch6: coolkey-cac.patch +Patch7: coolkey-cac-1.patch +Patch8: coolkey-pcsc-lite-fix.patch +Patch9: coolkey-fix-token-removal-failure.patch +Patch10: coolkey-piv-ecc-el7.patch +Group: System Environment/Libraries +BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n) +BuildRequires: autoconf +BuildRequires: pcsc-lite-devel +BuildRequires: zlib-devel +BuildRequires: nss-devel +Requires: nss-tools +Requires: pcsc-lite +Requires: pcsc-lite-libs +Requires: ccid +Provides: CoolKey Openkey +Obsoletes: CoolKey Openkey +# 390 does not have libusb or smartCards +ExcludeArch: s390 s390x + +%description +Linux Driver support for the CoolKey and CAC products. + +%package devel +Summary: CoolKey Applet libraries +Group: System Environment/Libraries + +%description devel +Linux Driver support to access the CoolKey applet. + + +%prep +%setup -q +%patch1 -b .cache.dir.move +%patch2 -b .coolkey-gcc43 +%patch3 -b .coolkey-latest +%patch4 -b .coolkey-simple-bugs +%patch5 -b .coolkey-thread-fix +%patch6 -b .cac +%patch7 -b .cac-1 +%patch8 -b .reader-state-fix +%patch9 -p1 -b .fix-token-removal-failure +%patch10 -b .piv-ecc + +%build +autoconf +%configure --with-debug --disable-dependency-tracking --enable-pk11install +make %{?_smp_mflags} CFLAGS="$CFLAGS -g -O2 -fno-strict-aliasing $CFLAGS " CXXFLAGS="$CXXFLAGS -g -O2 -fno-strict-aliasing $CFLAGS" + +%install +rm -rf $RPM_BUILD_ROOT +make install DESTDIR=$RPM_BUILD_ROOT +ln -s pkcs11/libcoolkeypk11.so $RPM_BUILD_ROOT/%{_libdir} +mkdir -p $RPM_BUILD_ROOT/var/cache/coolkey + +%clean +rm -rf $RPM_BUILD_ROOT + +%post +/sbin/ldconfig +if [ -x %{_bindir}/pk11install ]; then + isThere=`modutil -rawlist -dbdir dbm:%{nssdb} | grep %{coolkey_module} || echo NO` + if [ "$isThere" == "NO" ]; then + pk11install -l -p %{nssdb} 'name=%{coolkey_module} library=libcoolkeypk11.so' ||: + fi + isThere=`modutil -rawlist -dbdir sql:%{nssdb} | grep %{coolkey_module} || echo NO` + if [ "$isThere" == "NO" ]; then + pk11install -s -p %{nssdb} 'name=%{coolkey_module} library=libcoolkeypk11.so' ||: + fi +fi + + +%postun +/sbin/ldconfig +if [ $1 -eq 0 ]; then + modutil -delete %{coolkey_module} -dbdir dbm:%{nssdb} -force || : + modutil -delete %{coolkey_module} -dbdir sql:%{nssdb} -force || : +fi + + +%files +%defattr(-,root,root,-) +%doc ChangeLog LICENSE +%{_bindir}/pk11install +%{_libdir}/libcoolkeypk11.so +%{_libdir}/pkcs11 +%{_libdir}/libckyapplet.so.1 +%{_libdir}/libckyapplet.so.1.0.0 + +%files devel +%{_libdir}/libckyapplet.so +%{_libdir}/pkgconfig/libckyapplet.pc +%{_includedir}/*.h + + +%changelog +* Wed Nov 6 2013 Robert Relyea - 1.1.0-25 +- delete coolkey from sql db as well as the old one. + +* Sun Sep 8 2013 Robert Relyea - 1.1.0-24 +- Add ECC and PIV support + +* Wed May 22 2013 Ray Strode 1.1.0-23 +- Fix token removal issue + +* Wed Feb 13 2013 Fedora Release Engineering - 1.1.0-22 +- Rebuilt for https://fedoraproject.org/wiki/Fedora_19_Mass_Rebuild + +* Wed Jul 18 2012 Fedora Release Engineering - 1.1.0-21 +- Rebuilt for https://fedoraproject.org/wiki/Fedora_18_Mass_Rebuild + +* Thu Jan 12 2012 Fedora Release Engineering - 1.1.0-20 +- Rebuilt for https://fedoraproject.org/wiki/Fedora_17_Mass_Rebuild + +* Tue Feb 08 2011 Fedora Release Engineering - 1.1.0-19 +- Rebuilt for https://fedoraproject.org/wiki/Fedora_15_Mass_Rebuild + +* Tue Oct 19 2010 Ville Skyttä - 1.1.0-18 +- Own the %%{_libdir}/pkcs11 dir. + +* Wed Sep 8 2010 Robert Relyea - 1.1.0-17 +- pscs-lite removed SCARD_READERSTATE_A definition. revert to the prefered + SCARD_READERSTATE + +* Wed Jun 23 2010 Jack Magne - 1.1.0-16 +- fix possible crash when loading cac certs. + +* Wed Jun 16 2010 Robert Relyea - 1.1.0-15 +- better cac support. + +* Mon Feb 22 2010 Robert Relyea - 1.1.0-14 +- remove dependency on ifd-egate + +* Tue Jan 5 2010 Robert Relyea - 1.1.0-13 +- bump the release number to rebuild + +* Wed Dec 18 2009 Robert Relyea - 1.1.0-12 +- Fix threading issue. Coolkey will now work with non-threaded applications +- that don't link with libpthread. + +* Wed Sep 16 2009 Jack magne - 1.1.0-11 +- Misc bug fixes. Resolves: 485032, #250738, #497758. + +* Fri Sep 11 2009 Jack Magne - 1.1.0-10 +- Include latest changes for Gemalto 64K and Safenet 330J. + +* Fri Jul 24 2009 Fedora Release Engineering - 1.1.0-9 +- Rebuilt for https://fedoraproject.org/wiki/Fedora_12_Mass_Rebuild + +* Tue Feb 24 2009 Fedora Release Engineering - 1.1.0-8 +- Rebuilt for https://fedoraproject.org/wiki/Fedora_11_Mass_Rebuild + +* Sun Sep 14 2008 Matt Domsch - 1.1.0-7 +- BR: nss-devel not mozilla-nss-devel (FTBFS BZ#440753) + +* Wed Feb 13 2008 Jack magne - 1.1.0-6 +- Clean up building with gcc 4.3. +* Thu Sep 27 2007 Jack Magne - 1.1.0-5 +- Include patch for moving the cache directory to a safe location. +- Bug #299481. +* Fri Aug 20 2007 Bob Relyea - 1.1.0-4 +- Update License description to the new Fedora standard + +* Thu Jun 21 2007 Kai Engert - 1.1.0-3.1 +- rebuild + +* Tue Jun 5 2007 Bob Relyea - 1.1.0-3 +- add build requires, bump version number for make tag. + +* Thu May 31 2007 Bob Relyea - 1.1.0-2 +- Back out RHEL-4 version of spec from CVS, add pcsc-lite-lib requires. + +* Tue Feb 20 2007 Bob Relyea - 1.1.0-1 +- Pick up lates release. + +* Wed Nov 1 2006 Bob Relyea - 1.0.1-15 +- Don't grab the CUID on cac's. Resting the card causes it to +- logout of other applications. + +* Wed Nov 1 2006 Bob Relyea - 1.0.1-14 +- Shared memory directory needs to be writeable by all so +- coolkey can create caches for any user. (lack of caches +- show up in screen savers reactly slowly). + +* Fri Oct 20 2006 Bob Relyea - 1.0.1-13 +- fix login hw race failures + +* Fri Oct 20 2006 Bob Relyea - 1.0.1-12 +- add the dist flag + +* Wed Oct 18 2006 Bob Relyea - 1.0.1-11 +- CAC cards sometimes need to reset before they can get their +- initial transaction (problem is noticed on insertion an removal) + +* Tue Oct 17 2006 Jesse Keating - 1.0.1-10 +- Only run pk11install if the binary is there (multilib fun) + +* Mon Oct 09 2006 Bob Relyea - 1.0.1-9 +- use pk11install which does not require loading the module to install it. + +* Mon Oct 09 2006 Bob Relyea - 1.0.1-8 +- pcscd must be running in order to add coolkey. + +* Thu Oct 4 2006 Bob Relyea - 1.0.1-7 +- silence modutil warnings + +* Thu Sep 30 2006 Bob Relyea - 1.0.1-5 +- install and uninstall coolkey in the system secmod.db + +* Thu Sep 7 2006 Bob Relyea - 1.0.1-4 +- make the coolkey token caches persist over application calls. +- make a separate cache for each user. + +* Sun Jul 16 2006 Florian La Roche - 1.0.1-2 +- fix excludearch line + +* Mon Jul 10 2006 Bob Relyea - 1.0.1-1 +- Don't require pthread library in coolkey + +* Mon Jul 10 2006 Bob Relyea - 1.0.0-2 +- remove s390 from the build + +* Mon Jun 5 2006 Bob Relyea - 1.0.0-1 +- Initial revision for fedora