Diff

Differences From Artifact [c129398723]:

To Artifact [aa058371f0]:


   213    213   	static char buf_user[4096] = {0}; \
   214    214   	snprintf(buf_user, sizeof(buf_user), x); \
   215    215   	buf_user[sizeof(buf_user) - 1] = '\0'; \
   216    216   	fprintf(cackey_debug_fd(), "[%lu]: %s():%i: %s\n", CACKEY_DEBUG_GETTIME(), __func__, __LINE__, buf_user); \
   217    217   	fflush(cackey_debug_fd()); \
   218    218   }
   219    219   #  define CACKEY_DEBUG_PRINTBUF(f, x, y) { \
   220         -	static char buf_user[4096] = {0}, *buf_user_p; \
          220  +	static char buf_user[4096] = {0}, *buf_user_p, *buf_user_print; \
   221    221   	unsigned long buf_user_size; \
   222    222   	unsigned char *TMPBUF; \
   223    223   	unsigned long idx; \
   224    224   	int snprintf_ret; \
   225    225   	TMPBUF = (unsigned char *) (x); \
   226    226   	buf_user[0] = 0; \
   227    227   	buf_user_p = buf_user; \
   228    228   	buf_user_size = sizeof(buf_user); \
   229         -	for (idx = 1; idx < (y); idx++) { \
          229  +	for (idx = 0; idx < (y); idx++) { \
   230    230   		if (buf_user_size <= 0) { \
   231    231   			break; \
   232    232   		}; \
   233    233   		snprintf_ret = snprintf(buf_user_p, buf_user_size, ", %02x", TMPBUF[idx]); \
   234    234   		if (snprintf_ret <= 0) { \
   235    235   			break; \
   236    236   		}; \
   237    237   		buf_user_p += snprintf_ret; \
   238    238   		buf_user_size -= snprintf_ret; \
   239    239   	}; \
   240    240   	buf_user[sizeof(buf_user) - 1] = '\0'; \
   241         -	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); \
          241  +	buf_user_print = buf_user + 2; \
          242  +	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); \
   242    243   	fflush(cackey_debug_fd()); \
   243    244   }
   244    245   #  define free(x) { CACKEY_DEBUG_PRINTF("FREE(%p) (%s)", (void *) x, #x); free(x); }
   245    246   
   246    247   static FILE *cackey_debug_fd(void) {
   247    248   	static FILE *fd = NULL;
   248    249   	char *logfile;
................................................................................
   840    841   	CACKEY_PCSC_S_TOKENPRESENT    = 1,
   841    842   	CACKEY_PCSC_S_OK              = 0,
   842    843   	CACKEY_PCSC_E_GENERIC         = -1,
   843    844   	CACKEY_PCSC_E_BADPIN          = -2,
   844    845   	CACKEY_PCSC_E_LOCKED          = -3,
   845    846   	CACKEY_PCSC_E_NEEDLOGIN       = -4,
   846    847   	CACKEY_PCSC_E_TOKENABSENT     = -6,
   847         -	CACKEY_PCSC_E_RETRY           = -7
          848  +	CACKEY_PCSC_E_RETRY           = -7,
          849  +	CACKEY_PCSC_E_NODATA          = -8
   848    850   } cackey_ret;
   849    851   
   850    852   struct cackey_tlv_cardurl {
   851    853   	unsigned char        rid[5];
   852    854   	cackey_tlv_apptype   apptype;
   853    855   	cackey_tlv_objectid  objectid;
   854    856   	cackey_tlv_objectid  appid;
................................................................................
  1646   1648   	if (recv_len < 2) {
  1647   1649   		/* Minimal response length is 2 bytes, returning in failure */
  1648   1650   		CACKEY_DEBUG_PRINTF("Response too small, returning in failure (recv_len = %lu)", (unsigned long) recv_len);
  1649   1651   
  1650   1652   		/* End Smartcard Transaction */
  1651   1653   		cackey_end_transaction(slot);
  1652   1654   
  1653         -		return(CACKEY_PCSC_E_GENERIC);
         1655  +		/* Supply an invalid response code */
         1656  +		if (respcode) {
         1657  +			*respcode = 0;
         1658  +		}
         1659  +
         1660  +		return(CACKEY_PCSC_E_NODATA);
  1654   1661   	}
  1655   1662   
  1656   1663   	/* Determine result code */
  1657   1664   	major_rc = recv_buf[recv_len - 2];
  1658   1665   	minor_rc = recv_buf[recv_len - 1];
  1659   1666   	if (respcode) {
  1660   1667   		*respcode = (major_rc << 8) | minor_rc;
................................................................................
  2505   2512   	transaction_ret = cackey_begin_transaction(slot);
  2506   2513   	if (transaction_ret != CACKEY_PCSC_S_OK) {
  2507   2514   		CACKEY_DEBUG_PRINTF("Unable begin transaction, returning in failure");
  2508   2515   
  2509   2516   		return(NULL);
  2510   2517   	}
  2511   2518   
  2512         -	if (certs == NULL) {
  2513         -		certs = malloc(sizeof(*certs) * 5);
  2514         -		*count = 5;
  2515         -		certs_resizable = 1;
  2516         -	} else {
  2517         -		certs_resizable = 0;
  2518         -	}
  2519         -
  2520   2519   	/* Select the CCC Applet */
  2521   2520   	send_ret = cackey_select_applet(slot, ccc_aid, sizeof(ccc_aid));
  2522   2521   	if (send_ret != CACKEY_PCSC_S_OK) {
  2523   2522   		/* Try PIV application */
  2524   2523   		send_ret = cackey_select_applet(slot, piv_aid, sizeof(piv_aid));
  2525   2524   		if (send_ret == CACKEY_PCSC_S_OK) {
  2526   2525   			CACKEY_DEBUG_PRINTF("We have a PIV card -- not using the CCC, pulling pre-selected keys");
................................................................................
  2527   2526   
  2528   2527   			piv = 1;
  2529   2528   		} else {
  2530   2529   			CACKEY_DEBUG_PRINTF("Unable to select CCC Applet, returning in failure");
  2531   2530   
  2532   2531   			/* Terminate SmartCard Transaction */
  2533   2532   			cackey_end_transaction(slot);
         2533  +
         2534  +			if (certs == NULL) {
         2535  +				*count = 0;
         2536  +			}
  2534   2537   
  2535   2538   			return(NULL);
  2536   2539   		}
  2537   2540   	}
         2541  +
         2542  +	if (certs == NULL) {
         2543  +		certs = malloc(sizeof(*certs) * 5);
         2544  +		*count = 5;
         2545  +		certs_resizable = 1;
         2546  +	} else {
         2547  +		certs_resizable = 0;
         2548  +	}
  2538   2549   
  2539   2550   	if (piv) {
  2540   2551   		for (idx = 0; idx < 3; idx++) {
  2541   2552   			switch (idx) {
  2542   2553   				case 0:
  2543   2554   					piv_oid = piv_oid_pivauth;
  2544   2555   					piv_key = NISTSP800_78_3_KEY_PIVAUTH;
................................................................................
  7723   7734   
  7724   7735   	return(CKR_FUNCTION_NOT_PARALLEL);
  7725   7736   
  7726   7737   	hSession = hSession; /* Supress unused variable warning */
  7727   7738   }
  7728   7739   
  7729   7740   CK_DEFINE_FUNCTION(CK_RV, C_GetFunctionList)(CK_FUNCTION_LIST_PTR_PTR ppFunctionList) {
         7741  +	static CK_FUNCTION_LIST_PTR spFunctionList = NULL;
  7730   7742   	CK_FUNCTION_LIST_PTR pFunctionList;
  7731   7743   
  7732   7744   	CACKEY_DEBUG_PRINTF("Called.");
  7733   7745   
  7734   7746   	if (ppFunctionList == NULL) {
  7735   7747   		CACKEY_DEBUG_PRINTF("Error. ppFunctionList is NULL.");
  7736   7748   
  7737   7749   		return(CKR_ARGUMENTS_BAD);
  7738   7750   	}
         7751  +
         7752  +	if (spFunctionList != NULL) {
         7753  +		*ppFunctionList = spFunctionList;
         7754  +
         7755  +		CACKEY_DEBUG_PRINTF("Returning CKR_OK (%i)", CKR_OK);
         7756  +
         7757  +		return(CKR_OK);
         7758  +	}
  7739   7759   
  7740   7760   	pFunctionList = malloc(sizeof(*pFunctionList));
  7741   7761   
  7742   7762   	pFunctionList->version.major = ((CACKEY_CRYPTOKI_VERSION_CODE) >> 16) & 0xff;
  7743   7763   	pFunctionList->version.minor = ((CACKEY_CRYPTOKI_VERSION_CODE) >> 8) & 0xff;
  7744   7764   
  7745   7765   	pFunctionList->C_Initialize = C_Initialize;
................................................................................
  7807   7827   	pFunctionList->C_DeriveKey = C_DeriveKey;
  7808   7828   	pFunctionList->C_SeedRandom = C_SeedRandom;
  7809   7829   	pFunctionList->C_GenerateRandom = C_GenerateRandom;
  7810   7830   	pFunctionList->C_GetFunctionStatus = C_GetFunctionStatus;
  7811   7831   	pFunctionList->C_CancelFunction = C_CancelFunction;
  7812   7832   	pFunctionList->C_GetFunctionList = C_GetFunctionList;
  7813   7833   
         7834  +	spFunctionList  = pFunctionList;
  7814   7835   	*ppFunctionList = pFunctionList;
  7815   7836   
  7816   7837   	CACKEY_DEBUG_PRINTF("Returning CKR_OK (%i)", CKR_OK);
  7817   7838   
  7818   7839   	return(CKR_OK);
  7819   7840   }
  7820         -