Diff

Differences From Artifact [5afb8e1152]:

To Artifact [b0e4dd691b]:


     1         -void C_GetFunctionList(void);
     2         -int main(int argc, char **argv) {
     3         -	C_GetFunctionList();
            1  +#ifdef __cplusplus
            2  +extern "C" {
            3  +#endif
            4  +
            5  +#include <unistd.h>
            6  +#include <stdlib.h>
            7  +
            8  +#include "mypkcs11.h"
            9  +#include "cackey-chrome.h"
           10  +
           11  +struct cackey_chrome_id {
           12  +	unsigned char *id;
           13  +	size_t idLen;
           14  +};
           15  +
           16  +static CK_FUNCTION_LIST_PTR moduleFunctionList = NULL;
           17  +
           18  +static CK_RV cackey_chrome_init(void) {
           19  +	CK_C_INITIALIZE_ARGS initargs;
           20  +	CK_RV chk_rv;
           21  +
           22  +	if (moduleFunctionList != NULL) {
           23  +		return(CKR_OK);
           24  +	}
           25  +
           26  +	chk_rv = C_GetFunctionList(&moduleFunctionList);
           27  +	if (chk_rv != CKR_OK) {
           28  +		return(chk_rv);
           29  +	}
           30  +
           31  +	initargs.CreateMutex = NULL;
           32  +	initargs.DestroyMutex = NULL;
           33  +	initargs.LockMutex = NULL;
           34  +	initargs.UnlockMutex = NULL;
           35  +	initargs.flags = CKF_OS_LOCKING_OK;
           36  +	initargs.pReserved = NULL;
           37  +
           38  +	chk_rv = moduleFunctionList->C_Initialize(&initargs);
           39  +	if (chk_rv != CKR_OK) {
           40  +		return(chk_rv);
           41  +	}
           42  +
           43  +	return(CKR_OK);
           44  +}
           45  +
           46  +int cackey_chrome_listCertificates(struct cackey_certificate **certificates) {
           47  +	CK_RV chk_rv;
           48  +	CK_ULONG numSlots, currSlot;
           49  +	CK_SLOT_ID_PTR slots;
           50  +	CK_SLOT_INFO slotInfo;
           51  +	CK_SESSION_HANDLE hSession;
           52  +	CK_OBJECT_HANDLE hObject;
           53  +	CK_ULONG ulObjectCount;
           54  +	CK_ATTRIBUTE searchTemplatePrivateKeys[] = {
           55  +		{CKA_CLASS, NULL, sizeof(CK_OBJECT_CLASS)}
           56  +	};
           57  +	CK_ATTRIBUTE searchTemplateCertificates[] = {
           58  +		{CKA_CLASS, NULL, sizeof(CK_OBJECT_CLASS)},
           59  +		{CKA_ID, NULL, 0}
           60  +	};
           61  +	CK_ATTRIBUTE attrTemplate[] = {
           62  +		{CKA_ID, NULL, 0}
           63  +	}, *currAttr;
           64  +	CK_ULONG currAttrIndex;
           65  +	CK_OBJECT_CLASS objectClassPrivateKey = CKO_PRIVATE_KEY;
           66  +
           67  +	chk_rv = cackey_chrome_init();
           68  +	if (chk_rv != CKR_OK) {
           69  +		return(0);
           70  +	}
           71  +
           72  +	chk_rv = moduleFunctionList->C_GetSlotList(FALSE, NULL, &numSlots);
           73  +	if (chk_rv != CKR_OK) {
           74  +		return(0);
           75  +	}
           76  +
           77  +	slots = malloc(sizeof(*slots) * numSlots);
           78  +
           79  +	chk_rv = moduleFunctionList->C_GetSlotList(FALSE, slots, &numSlots);
           80  +	if (chk_rv != CKR_OK) {
           81  +		return(0);
           82  +	}
           83  +
           84  +	searchTemplatePrivateKeys[0].pValue = &objectClassPrivateKey;
           85  +
           86  +	for (currSlot = 0; currSlot < numSlots; currSlot++) {
           87  +		chk_rv = moduleFunctionList->C_GetSlotInfo(slots[currSlot], &slotInfo);
           88  +		if (chk_rv != CKR_OK) {
           89  +			continue;
           90  +		}
           91  +
           92  +		if ((slotInfo.flags & CKF_TOKEN_PRESENT) != CKF_TOKEN_PRESENT) {
           93  +			continue;
           94  +		}
           95  +
           96  +		chk_rv = moduleFunctionList->C_OpenSession(slots[currSlot], CKF_SERIAL_SESSION, NULL, NULL, &hSession);
           97  +		if (chk_rv != CKR_OK) {
           98  +			continue;
           99  +		}
          100  +
          101  +		chk_rv = moduleFunctionList->C_FindObjectsInit(hSession, searchTemplatePrivateKeys, sizeof(searchTemplatePrivateKeys) / sizeof(searchTemplatePrivateKeys[0])); 
          102  +		if (chk_rv != CKR_OK) {
          103  +			moduleFunctionList->C_CloseSession(hSession);
          104  +
          105  +			continue;
          106  +		}
          107  +
          108  +		while (1) {
          109  +			chk_rv = moduleFunctionList->C_FindObjects(hSession, &hObject, 1, &ulObjectCount);
          110  +			if (chk_rv != CKR_OK) {
          111  +				break;
          112  +			}
          113  +
          114  +			if (ulObjectCount == 0) {
          115  +				break;
          116  +			}
          117  +
          118  +			if (ulObjectCount != 1) {
          119  +				break;
          120  +			}
          121  +
          122  +			for (currAttrIndex = 0; currAttrIndex < (sizeof(attrTemplate) / sizeof(attrTemplate[0])); currAttrIndex++) {
          123  +				currAttr = &attrTemplate[currAttrIndex];
          124  +
          125  +				currAttr->pValue = NULL;
          126  +				currAttr->ulValueLen = 0;
          127  +			}
          128  +
          129  +			chk_rv = C_GetAttributeValue(hSession, hObject, attrTemplate, sizeof(attrTemplate) / sizeof(attrTemplate[0]));
          130  +			if (chk_rv == CKR_ATTRIBUTE_TYPE_INVALID || chk_rv == CKR_ATTRIBUTE_SENSITIVE || chk_rv == CKR_BUFFER_TOO_SMALL) {
          131  +				chk_rv = CKR_OK;
          132  +			}
          133  +
          134  +			if (chk_rv != CKR_OK) {
          135  +				continue;
          136  +			}
          137  +
          138  +			for (currAttrIndex = 0; currAttrIndex < (sizeof(attrTemplate) / sizeof(attrTemplate[0])); currAttrIndex++) {
          139  +				currAttr = &attrTemplate[currAttrIndex];
          140  +
          141  +				if (currAttr->ulValueLen == 0) {
          142  +					continue;
          143  +				}
          144  +
          145  +				if (((CK_LONG) currAttr->ulValueLen) == ((CK_LONG) -1)) {
          146  +					continue;
          147  +				}
          148  +
          149  +				currAttr->pValue = malloc(currAttr->ulValueLen);
          150  +			}
          151  +
          152  +			chk_rv = C_GetAttributeValue(hSession, hObject, attrTemplate, sizeof(attrTemplate) / sizeof(attrTemplate[0]));
          153  +			if (chk_rv != CKR_OK) {
          154  +				continue;
          155  +			}
          156  +
          157  +		}
          158  +
          159  +		moduleFunctionList->C_FindObjectsFinal(hSession);
          160  +
          161  +		moduleFunctionList->C_CloseSession(hSession);
          162  +	}
          163  +
          164  +	return(0);
          165  +}
          166  +
          167  +#ifdef __cplusplus
     4    168   }
          169  +#endif