Index: cackey.c ================================================================== --- cackey.c +++ cackey.c @@ -215,20 +215,20 @@ buf_user[sizeof(buf_user) - 1] = '\0'; \ fprintf(cackey_debug_fd(), "[%lu]: %s():%i: %s\n", CACKEY_DEBUG_GETTIME(), __func__, __LINE__, buf_user); \ fflush(cackey_debug_fd()); \ } # define CACKEY_DEBUG_PRINTBUF(f, x, y) { \ - static char buf_user[4096] = {0}, *buf_user_p; \ + static char buf_user[4096] = {0}, *buf_user_p, *buf_user_print; \ unsigned long buf_user_size; \ unsigned char *TMPBUF; \ unsigned long idx; \ int snprintf_ret; \ TMPBUF = (unsigned char *) (x); \ buf_user[0] = 0; \ buf_user_p = buf_user; \ buf_user_size = sizeof(buf_user); \ - for (idx = 1; idx < (y); idx++) { \ + for (idx = 0; idx < (y); idx++) { \ if (buf_user_size <= 0) { \ break; \ }; \ snprintf_ret = snprintf(buf_user_p, buf_user_size, ", %02x", TMPBUF[idx]); \ if (snprintf_ret <= 0) { \ @@ -236,11 +236,12 @@ }; \ buf_user_p += snprintf_ret; \ buf_user_size -= snprintf_ret; \ }; \ buf_user[sizeof(buf_user) - 1] = '\0'; \ - fprintf(cackey_debug_fd(), "[%lu]: %s():%i: %s (%s/%lu = {%02x%s})\n", CACKEY_DEBUG_GETTIME(), __func__, __LINE__, f, #x, (unsigned long) (y), TMPBUF[0], buf_user); \ + buf_user_print = buf_user + 2; \ + fprintf(cackey_debug_fd(), "[%lu]: %s():%i: %s (%s/%lu = {%s})\n", CACKEY_DEBUG_GETTIME(), __func__, __LINE__, f, #x, (unsigned long) (y), buf_user_print); \ fflush(cackey_debug_fd()); \ } # define free(x) { CACKEY_DEBUG_PRINTF("FREE(%p) (%s)", (void *) x, #x); free(x); } static FILE *cackey_debug_fd(void) { @@ -842,11 +843,12 @@ CACKEY_PCSC_E_GENERIC = -1, CACKEY_PCSC_E_BADPIN = -2, CACKEY_PCSC_E_LOCKED = -3, CACKEY_PCSC_E_NEEDLOGIN = -4, CACKEY_PCSC_E_TOKENABSENT = -6, - CACKEY_PCSC_E_RETRY = -7 + CACKEY_PCSC_E_RETRY = -7, + CACKEY_PCSC_E_NODATA = -8 } cackey_ret; struct cackey_tlv_cardurl { unsigned char rid[5]; cackey_tlv_apptype apptype; @@ -1648,11 +1650,16 @@ CACKEY_DEBUG_PRINTF("Response too small, returning in failure (recv_len = %lu)", (unsigned long) recv_len); /* End Smartcard Transaction */ cackey_end_transaction(slot); - return(CACKEY_PCSC_E_GENERIC); + /* Supply an invalid response code */ + if (respcode) { + *respcode = 0; + } + + return(CACKEY_PCSC_E_NODATA); } /* Determine result code */ major_rc = recv_buf[recv_len - 2]; minor_rc = recv_buf[recv_len - 1]; @@ -2507,18 +2514,10 @@ CACKEY_DEBUG_PRINTF("Unable begin transaction, returning in failure"); return(NULL); } - if (certs == NULL) { - certs = malloc(sizeof(*certs) * 5); - *count = 5; - certs_resizable = 1; - } else { - certs_resizable = 0; - } - /* Select the CCC Applet */ send_ret = cackey_select_applet(slot, ccc_aid, sizeof(ccc_aid)); if (send_ret != CACKEY_PCSC_S_OK) { /* Try PIV application */ send_ret = cackey_select_applet(slot, piv_aid, sizeof(piv_aid)); @@ -2529,14 +2528,26 @@ } else { CACKEY_DEBUG_PRINTF("Unable to select CCC Applet, returning in failure"); /* Terminate SmartCard Transaction */ cackey_end_transaction(slot); + + if (certs == NULL) { + *count = 0; + } return(NULL); } } + + if (certs == NULL) { + certs = malloc(sizeof(*certs) * 5); + *count = 5; + certs_resizable = 1; + } else { + certs_resizable = 0; + } if (piv) { for (idx = 0; idx < 3; idx++) { switch (idx) { case 0: @@ -7725,19 +7736,28 @@ hSession = hSession; /* Supress unused variable warning */ } CK_DEFINE_FUNCTION(CK_RV, C_GetFunctionList)(CK_FUNCTION_LIST_PTR_PTR ppFunctionList) { + static CK_FUNCTION_LIST_PTR spFunctionList = NULL; CK_FUNCTION_LIST_PTR pFunctionList; CACKEY_DEBUG_PRINTF("Called."); if (ppFunctionList == NULL) { CACKEY_DEBUG_PRINTF("Error. ppFunctionList is NULL."); return(CKR_ARGUMENTS_BAD); } + + if (spFunctionList != NULL) { + *ppFunctionList = spFunctionList; + + CACKEY_DEBUG_PRINTF("Returning CKR_OK (%i)", CKR_OK); + + return(CKR_OK); + } pFunctionList = malloc(sizeof(*pFunctionList)); pFunctionList->version.major = ((CACKEY_CRYPTOKI_VERSION_CODE) >> 16) & 0xff; pFunctionList->version.minor = ((CACKEY_CRYPTOKI_VERSION_CODE) >> 8) & 0xff; @@ -7809,12 +7829,12 @@ pFunctionList->C_GenerateRandom = C_GenerateRandom; pFunctionList->C_GetFunctionStatus = C_GetFunctionStatus; pFunctionList->C_CancelFunction = C_CancelFunction; pFunctionList->C_GetFunctionList = C_GetFunctionList; + spFunctionList = pFunctionList; *ppFunctionList = pFunctionList; CACKEY_DEBUG_PRINTF("Returning CKR_OK (%i)", CKR_OK); return(CKR_OK); } -