Check-in [d85f1a762d]
Overview
Comment:Updated to allow use of protocol T=0 or protocol T=1

Fixed minor warning in debug mode

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:d85f1a762d99da440d996896b757be22b21f19d1
User & Date: rkeene on 2010-07-21 21:21:38
Other Links: manifest | tags
Context
2010-07-21
21:33
Updated to leave build directory intact on snapshot check-in: 1fed88b8b1 user: rkeene tags: trunk
21:21
Updated to allow use of protocol T=0 or protocol T=1

Fixed minor warning in debug mode check-in: d85f1a762d user: rkeene tags: trunk

2010-07-14
03:59
Corrected typo in version dependency output message. Will be fixed in released packages in next minor release of CACKey. check-in: 63b337f367 user: kvanals tags: trunk
Changes

Modified cackey.c from [ecdd31141d] to [5676179bd7].

   538    538   	int transaction_depth;
   539    539   
   540    540   	int slot_reset;
   541    541   
   542    542   	CK_FLAGS token_flags;
   543    543   
   544    544   	unsigned char *label;
          545  +
          546  +	DWORD protocol;
   545    547   };
   546    548   
   547    549   typedef enum {
   548    550   	CACKEY_TLV_APP_GENERIC = 0x01,
   549    551   	CACKEY_TLV_APP_SKI     = 0x02,
   550    552   	CACKEY_TLV_APP_PKI     = 0x04
   551    553   } cackey_tlv_apptype;
................................................................................
   841    843   		CACKEY_DEBUG_PRINTF("Connection to PC/SC failed, returning in failure");
   842    844   
   843    845   		return(CACKEY_PCSC_E_GENERIC);
   844    846   	}
   845    847   
   846    848   	/* Connect to reader, if needed */
   847    849   	if (!slot->pcsc_card_connected) {
          850  +		slot->protocol = 0;
          851  +
   848    852   		CACKEY_DEBUG_PRINTF("SCardConnect(%s) called", slot->pcsc_reader);
   849         -		scard_conn_ret = SCardConnect(*cackey_pcsc_handle, slot->pcsc_reader, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0, &slot->pcsc_card, &protocol);
          853  +		scard_conn_ret = SCardConnect(*cackey_pcsc_handle, slot->pcsc_reader, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, &slot->pcsc_card, &protocol);
   850    854   
   851    855   		if (scard_conn_ret == SCARD_W_UNPOWERED_CARD) {
   852         -			scard_conn_ret = SCardConnect(*cackey_pcsc_handle, slot->pcsc_reader, SCARD_SHARE_DIRECT, SCARD_PROTOCOL_T0, &slot->pcsc_card, &protocol);
   853         -			scard_conn_ret = SCardReconnect(slot->pcsc_card, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0, SCARD_RESET_CARD, &protocol);
          856  +			scard_conn_ret = SCardConnect(*cackey_pcsc_handle, slot->pcsc_reader, SCARD_SHARE_DIRECT, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, &slot->pcsc_card, &protocol);
          857  +			scard_conn_ret = SCardReconnect(slot->pcsc_card, SCARD_SHARE_SHARED, protocol, SCARD_RESET_CARD, &protocol);
   854    858   		}
   855    859   
   856    860   		if (scard_conn_ret != SCARD_S_SUCCESS) {
   857    861   			CACKEY_DEBUG_PRINTF("Connection to card failed, returning in failure (SCardConnect() = %s/%li)", CACKEY_DEBUG_FUNC_SCARDERR_TO_STR(scard_conn_ret), (long) scard_conn_ret);
   858    862   
   859    863   			return(CACKEY_PCSC_E_GENERIC);
   860    864   		}
   861    865   
   862    866   		slot->pcsc_card_connected = 1;
   863    867   		slot->transaction_depth = 0;
          868  +		slot->protocol = protocol;
   864    869   	}
   865    870   
   866    871   	return(CACKEY_PCSC_S_OK);
   867    872   }
   868    873   
   869    874   /*
   870    875    * SYNPOSIS
................................................................................
  1036   1041    *     specified.  It will reconnect to the card if the connection
  1037   1042    *     goes away.
  1038   1043    *
  1039   1044    */
  1040   1045   static cackey_ret cackey_send_apdu(struct cackey_slot *slot, unsigned char class, unsigned char instruction, unsigned char p1, unsigned char p2, unsigned char lc, unsigned char *data, unsigned char le, uint16_t *respcode, unsigned char *respdata, size_t *respdata_len) {
  1041   1046   	uint8_t major_rc, minor_rc;
  1042   1047   	size_t bytes_to_copy, tmp_respdata_len;
         1048  +	LPCSCARD_IO_REQUEST pioSendPci;
  1043   1049   	DWORD protocol;
  1044   1050   	DWORD xmit_len, recv_len;
  1045   1051   	LONG scard_xmit_ret, scard_reconn_ret;
  1046   1052   	BYTE xmit_buf[1024], recv_buf[1024];
  1047   1053   	int pcsc_connect_ret, pcsc_getresp_ret;
  1048   1054   	int idx;
  1049   1055   
................................................................................
  1057   1063   
  1058   1064   	pcsc_connect_ret = cackey_connect_card(slot);
  1059   1065   	if (pcsc_connect_ret != CACKEY_PCSC_S_OK) {
  1060   1066   		CACKEY_DEBUG_PRINTF("Unable to connect to card, returning in failure");
  1061   1067   
  1062   1068   		return(CACKEY_PCSC_E_GENERIC);
  1063   1069   	}
         1070  +
         1071  +	/* Determine which protocol to send using */
         1072  +	switch (slot->protocol) {
         1073  +		case SCARD_PROTOCOL_T0:
         1074  +			pioSendPci = SCARD_PCI_T0;
         1075  +
         1076  +			break;
         1077  +		case SCARD_PROTOCOL_T1:
         1078  +			pioSendPci = SCARD_PCI_T1;
         1079  +
         1080  +			break;
         1081  +		default:
         1082  +			CACKEY_DEBUG_PRINTF("Invalid protocol found, aborting.");
         1083  +
         1084  +			return(CACKEY_PCSC_E_GENERIC);
         1085  +	}
  1064   1086   
  1065   1087   	/* Transmit */
  1066   1088   	xmit_len = 0;
  1067   1089   	xmit_buf[xmit_len++] = class;
  1068   1090   	xmit_buf[xmit_len++] = instruction;
  1069   1091   	xmit_buf[xmit_len++] = p1;
  1070   1092   	xmit_buf[xmit_len++] = p2;
................................................................................
  1085   1107   	if (class == GSCIS_CLASS_ISO7816 && instruction == GSCIS_INSTR_VERIFY && p1 == 0x00 && p2 == 0x00) {
  1086   1108   		CACKEY_DEBUG_PRINTF("Sending APDU: <<censored>>");
  1087   1109   	} else {
  1088   1110   		CACKEY_DEBUG_PRINTBUF("Sending APDU:", xmit_buf, xmit_len);
  1089   1111   	}
  1090   1112   
  1091   1113   	recv_len = sizeof(recv_buf);
  1092         -	scard_xmit_ret = SCardTransmit(slot->pcsc_card, SCARD_PCI_T0, xmit_buf, xmit_len, NULL, recv_buf, &recv_len);
         1114  +	scard_xmit_ret = SCardTransmit(slot->pcsc_card, pioSendPci, xmit_buf, xmit_len, NULL, recv_buf, &recv_len);
  1093   1115   	if (scard_xmit_ret != SCARD_S_SUCCESS) {
  1094   1116   		CACKEY_DEBUG_PRINTF("Failed to send APDU to card (SCardTransmit() = %s/%lx)", CACKEY_DEBUG_FUNC_SCARDERR_TO_STR(scard_xmit_ret), (unsigned long) scard_xmit_ret);
  1095   1117   		CACKEY_DEBUG_PRINTF("Marking slot as having been reset");
  1096   1118   
  1097   1119   		slot->transaction_depth = 0;
  1098   1120   		slot->slot_reset = 1;
  1099   1121   
  1100   1122   		if (scard_xmit_ret == SCARD_W_RESET_CARD) {
  1101   1123   			CACKEY_DEBUG_PRINTF("Reset required, please hold...");
  1102   1124   
  1103         -			scard_reconn_ret = SCardReconnect(slot->pcsc_card, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0, SCARD_RESET_CARD, &protocol);
         1125  +			scard_reconn_ret = SCardReconnect(slot->pcsc_card, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, SCARD_RESET_CARD, &protocol);
  1104   1126   			if (scard_reconn_ret == SCARD_S_SUCCESS) {
         1127  +				/* Update protocol */
         1128  +				slot->protocol = protocol;
         1129  +				switch (slot->protocol) {
         1130  +					case SCARD_PROTOCOL_T0:
         1131  +						pioSendPci = SCARD_PCI_T0;
         1132  +
         1133  +						break;
         1134  +					case SCARD_PROTOCOL_T1:
         1135  +						pioSendPci = SCARD_PCI_T1;
         1136  +
         1137  +						break;
         1138  +					default:
         1139  +						CACKEY_DEBUG_PRINTF("Invalid protocol found, but too late to do anything about it now -- trying anyway.");
         1140  +
         1141  +						break;
         1142  +				}
         1143  +
  1105   1144   				/* Re-establish transaction, if it was present */
  1106   1145   				if (slot->transaction_depth > 0) {
  1107   1146   					slot->transaction_depth--;
  1108   1147   					cackey_begin_transaction(slot);
  1109   1148   				}
  1110   1149   
  1111   1150   				CACKEY_DEBUG_PRINTF("Reset successful, retransmitting");
  1112   1151   
  1113   1152   				recv_len = sizeof(recv_buf);
  1114         -				scard_xmit_ret = SCardTransmit(slot->pcsc_card, SCARD_PCI_T0, xmit_buf, xmit_len, NULL, recv_buf, &recv_len);
         1153  +				scard_xmit_ret = SCardTransmit(slot->pcsc_card, pioSendPci, xmit_buf, xmit_len, NULL, recv_buf, &recv_len);
  1115   1154   
  1116   1155   				if (scard_xmit_ret != SCARD_S_SUCCESS) {
  1117   1156   					CACKEY_DEBUG_PRINTF("Retransmit failed, returning in failure after disconnecting the card (SCardTransmit = %s/%li)", CACKEY_DEBUG_FUNC_SCARDERR_TO_STR(scard_xmit_ret), (long) scard_xmit_ret);
  1118   1157   
  1119   1158   					SCardDisconnect(slot->pcsc_card, SCARD_LEAVE_CARD);
  1120   1159   					slot->pcsc_card_connected = 0;
  1121   1160   
................................................................................
  2233   2272   	if (status_ret != SCARD_S_SUCCESS) {
  2234   2273   		slot->slot_reset = 1;
  2235   2274   		slot->token_flags = CKF_LOGIN_REQUIRED;
  2236   2275   
  2237   2276   		if (status_ret == SCARD_W_RESET_CARD) {
  2238   2277   			CACKEY_DEBUG_PRINTF("Reset required, please hold...");
  2239   2278   
  2240         -			scard_reconn_ret = SCardReconnect(slot->pcsc_card, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0, SCARD_RESET_CARD, &protocol);
         2279  +			scard_reconn_ret = SCardReconnect(slot->pcsc_card, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1, SCARD_RESET_CARD, &protocol);
  2241   2280   			if (scard_reconn_ret == SCARD_S_SUCCESS) {
         2281  +				/* Update protocol */
         2282  +				slot->protocol = protocol;
         2283  +
  2242   2284   				/* Re-establish transaction, if it was present */
  2243   2285   				if (slot->transaction_depth > 0) {
  2244   2286   					slot->transaction_depth--;
  2245   2287   					cackey_begin_transaction(slot);
  2246   2288   				}
  2247   2289   
  2248   2290   				CACKEY_DEBUG_PRINTF("Reset successful, requerying");
................................................................................
  5185   5227   
  5186   5228   		CACKEY_DEBUG_PRINTF("Error.  SignFinal() returned failure (rv = %lu).", (unsigned long) sign_ret);
  5187   5229   
  5188   5230   		return(sign_ret);
  5189   5231   	}
  5190   5232   
  5191   5233   	if (pSignature == NULL) {
  5192         -		CACKEY_DEBUG_PRINTF("pSignature specified as NULL, undoing C_SignUpdate()", (unsigned long) sign_ret);
         5234  +		CACKEY_DEBUG_PRINTF("pSignature specified as NULL, undoing C_SignUpdate()");
  5193   5235   
  5194   5236   		cackey_sessions[hSession].sign_bufused = start_sign_bufused;
  5195   5237   
  5196   5238   		return(sign_ret);
  5197   5239   	}
  5198   5240   
  5199   5241   	CACKEY_DEBUG_PRINTF("Returning CKR_OK (%i)", CKR_OK);