added compatibility with windows

git-svn-id: https://vsmartcard.svn.sourceforge.net/svnroot/vsmartcard@254 96b47cad-a561-4643-ad3b-153ac7d7599c
This commit is contained in:
frankmorgner
2010-08-30 13:18:43 +00:00
parent d4b42dc293
commit a6cd9c651c

View File

@@ -16,17 +16,30 @@
* You should have received a copy of the GNU General Public License along with * You should have received a copy of the GNU General Public License along with
* ifdnfc. If not, see <http://www.gnu.org/licenses/>. * ifdnfc. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <pcsclite.h> //#include <pcsclite.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <string.h>
#include <winscard.h> #include <winscard.h>
#include <reader.h>
/* XXX IOCTL_FEATURE_IFD_PIN_PROPERTIES */ #ifndef FEATURE_EXECUTE_PACE
const static DWORD feature_execute_pace = 0x42330020; #define FEATURE_EXECUTE_PACE 0x20
#endif
static void #ifndef CM_IOCTL_GET_FEATURE_REQUEST
#define CM_IOCTL_GET_FEATURE_REQUEST SCARD_CTL_CODE(3400)
#endif
typedef struct
{
uint8_t tag;
uint8_t length;
uint32_t value;
} PCSC_TLV_STRUCTURE;
static void
printb(unsigned char *buf, size_t len) printb(unsigned char *buf, size_t len)
{ {
size_t i = 0; size_t i = 0;
@@ -41,15 +54,16 @@ printb(unsigned char *buf, size_t len)
printf("\n"); printf("\n");
} }
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
LONG rv; LONG rv;
SCARDCONTEXT hContext; SCARDCONTEXT hContext;
SCARDHANDLE hCard; SCARDHANDLE hCard;
LPSTR mszReaders = NULL; LPSTR mszReaders = NULL;
int num; int num, l;
char *reader; char *reader;
DWORD pace_ioctl = 0;
BYTE pbSendBufferCapabilities [] = { BYTE pbSendBufferCapabilities [] = {
0x01, /* idxFunction = GetReadersPACECapabilities */ 0x01, /* idxFunction = GetReadersPACECapabilities */
0x00, /* lengthInputData */ 0x00, /* lengthInputData */
@@ -62,15 +76,24 @@ main(int argc, char *argv[])
0x03, /* PACE with PIN */ 0x03, /* PACE with PIN */
0x00, /* length CHAT */ 0x00, /* length CHAT */
0x00, /* length PIN */ 0x00, /* length PIN */
/*0x06, [> length PIN <]*/
/*'2',*/
/*'6',*/
/*'3',*/
/*'4',*/
/*'1',*/
/*'1',*/
0x00, /* length certificate description */ 0x00, /* length certificate description */
0x00, /* length certificate description */ 0x00, /* length certificate description */
}; };
BYTE pbRecvBuffer[1024]; BYTE pbRecvBuffer[1024];
DWORD dwActiveProtocol, dwRecvLength, dwReaders; DWORD dwActiveProtocol, dwRecvLength, dwReaders, i;
PCSC_TLV_STRUCTURE *pcsc_tlv;
uint16_t lengthInputData = 5; uint16_t lengthInputData = sizeof(pbSendBufferEstablish) - 3;
memcpy(pbSendBufferEstablish + 1, &lengthInputData, 2); memcpy(pbSendBufferEstablish + 1, &lengthInputData, 2);
if (argc > 1) { if (argc > 1) {
if (argc > 2 || sscanf(argv[1], "%d", &num) != 1) { if (argc > 2 || sscanf(argv[1], "%d", &num) != 1) {
fprintf(stderr, "Usage: %s [reader_num]\n", argv[0]); fprintf(stderr, "Usage: %s [reader_num]\n", argv[0]);
@@ -79,16 +102,21 @@ main(int argc, char *argv[])
} else } else
num = 0; num = 0;
rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext); rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
if (rv < 0) if (rv != SCARD_S_SUCCESS) {
fprintf(stderr, "Could not connect to PC/SC Service");
goto err; goto err;
}
dwReaders = SCARD_AUTOALLOCATE; dwReaders = SCARD_AUTOALLOCATE;
rv = SCardListReaders(hContext, NULL, (LPSTR)&mszReaders, &dwReaders); rv = SCardListReaders(hContext, NULL, (LPSTR)&mszReaders, &dwReaders);
if (rv < 0) if (rv != SCARD_S_SUCCESS) {
fprintf(stderr, "Could not get readers");
goto err; goto err;
}
int l, i;
for (reader = mszReaders, i = 0; for (reader = mszReaders, i = 0;
dwReaders > 0; dwReaders > 0;
l = strlen(reader) + 1, dwReaders -= l, reader += l, i++) { l = strlen(reader) + 1, dwReaders -= l, reader += l, i++) {
@@ -104,43 +132,73 @@ main(int argc, char *argv[])
rv = SCardConnect(hContext, reader, SCARD_SHARE_DIRECT, 0, &hCard, rv = SCardConnect(hContext, reader, SCARD_SHARE_DIRECT, 0, &hCard,
&dwActiveProtocol); &dwActiveProtocol);
if (rv < 0) if (rv != SCARD_S_SUCCESS) {
fprintf(stderr, "Could not connect to %s\n", reader);
goto err; goto err;
}
printf("Connected to %s\n", reader);
/* does the reader support PACE? */
rv = SCardControl(hCard, CM_IOCTL_GET_FEATURE_REQUEST, NULL, 0,
pbRecvBuffer, sizeof(pbRecvBuffer), &dwRecvLength);
if (rv != SCARD_S_SUCCESS) {
fprintf(stderr, "Could not request the reader's features\n");
goto err;
}
rv = SCardControl(hCard, feature_execute_pace, /* get the number of elements instead of the complete size */
dwRecvLength /= sizeof(PCSC_TLV_STRUCTURE);
pcsc_tlv = (PCSC_TLV_STRUCTURE *)pbRecvBuffer;
for (i = 0; i < dwRecvLength; i++) {
if (pcsc_tlv[i].tag == FEATURE_EXECUTE_PACE) {
pace_ioctl = pcsc_tlv[i].value;
break;
}
}
if (0 == pace_ioctl) {
printf("Reader does not support PACE\n");
goto err;
}
rv = SCardControl(hCard, pace_ioctl,
pbSendBufferCapabilities, sizeof(pbSendBufferCapabilities), pbSendBufferCapabilities, sizeof(pbSendBufferCapabilities),
pbRecvBuffer, sizeof(pbRecvBuffer), &dwRecvLength); pbRecvBuffer, sizeof(pbRecvBuffer), &dwRecvLength);
if (rv < 0) if (rv != SCARD_S_SUCCESS) {
fprintf(stderr, "Could not get reader's PACE capabilities\n");
goto err; goto err;
}
printf("GetReadersPACECapabilities successfull, received %d bytes\n", printf("GetReadersPACECapabilities successfull, received %d bytes\n",
dwRecvLength); dwRecvLength);
printb(pbRecvBuffer, dwRecvLength); printb(pbRecvBuffer, dwRecvLength);
rv = SCardControl(hCard, feature_execute_pace, dwRecvLength = 0;
rv = SCardControl(hCard, pace_ioctl,
pbSendBufferEstablish, sizeof(pbSendBufferEstablish), pbSendBufferEstablish, sizeof(pbSendBufferEstablish),
pbRecvBuffer, sizeof(pbRecvBuffer), &dwRecvLength); pbRecvBuffer, sizeof(pbRecvBuffer), &dwRecvLength);
if (rv < 0) if (rv != SCARD_S_SUCCESS) {
fprintf(stderr, "Could not establish PACE channel\n");
goto err; goto err;
}
printf("EstablishPACEChannel successfull, received %d bytes\n", printf("EstablishPACEChannel successfull, received %d bytes\n",
dwRecvLength); dwRecvLength);
printb(pbRecvBuffer, dwRecvLength); printb(pbRecvBuffer, dwRecvLength);
rv = SCardDisconnect(hCard, SCARD_LEAVE_CARD); rv = SCardDisconnect(hCard, SCARD_LEAVE_CARD);
if (rv < 0) if (rv != SCARD_S_SUCCESS)
goto err; goto err;
rv = SCardFreeMemory(hContext, mszReaders); rv = SCardFreeMemory(hContext, mszReaders);
if (rv < 0) if (rv != SCARD_S_SUCCESS)
goto err; goto err;
exit(0); exit(0);
err: err:
puts(pcsc_stringify_error(rv)); /*puts(pcsc_stringify_error(rv));*/
if (mszReaders) if (mszReaders)
SCardFreeMemory(hContext, mszReaders); SCardFreeMemory(hContext, mszReaders);