prepare to merge with pcsc_to_picc

git-svn-id: https://vsmartcard.svn.sourceforge.net/svnroot/vsmartcard@299 96b47cad-a561-4643-ad3b-153ac7d7599c
This commit is contained in:
frankmorgner
2010-10-06 20:01:44 +00:00
parent 5651019177
commit 320cca0731
4 changed files with 128 additions and 39 deletions

View File

@@ -8,7 +8,7 @@ ccid_emulator_SOURCES = ccid.c usbstring.c usb.c binutil.c
ccid_emulator_LDADD = $(OPENSC_LIBS) $(OPENSSL_LIBS) $(PTHREAD_LIBS) ccid_emulator_LDADD = $(OPENSC_LIBS) $(OPENSSL_LIBS) $(PTHREAD_LIBS)
ccid_emulator_CFLAGS = $(OPENSC_CFLAGS) $(OPENSSL_CFLAGS) $(PTHREAD_CFLAGS) ccid_emulator_CFLAGS = $(OPENSC_CFLAGS) $(OPENSSL_CFLAGS) $(PTHREAD_CFLAGS)
cats_test_SOURCES = cats-test.c cats_test_SOURCES = cats-test.c pcscutil.c
cats_test_LDADD = $(PCSC_LIBS) cats_test_LDADD = $(PCSC_LIBS)
cats_test_CFLAGS = $(PCSC_CFLAGS) cats_test_CFLAGS = $(PCSC_CFLAGS)
@@ -20,6 +20,7 @@ bin_PROGRAMS =
noinst_HEADERS = \ noinst_HEADERS = \
binutil.h \ binutil.h \
pcscutil.h \
ccid.h \ ccid.h \
usbstring.h usbstring.h

View File

@@ -39,6 +39,8 @@
#include <reader.h> #include <reader.h>
#endif #endif
#include "pcscutil.h"
#ifndef FEATURE_EXECUTE_PACE #ifndef FEATURE_EXECUTE_PACE
#define FEATURE_EXECUTE_PACE 0x20 #define FEATURE_EXECUTE_PACE 0x20
@@ -185,9 +187,8 @@ main(int argc, char *argv[])
SCARDCONTEXT hContext; SCARDCONTEXT hContext;
SCARDHANDLE hCard; SCARDHANDLE hCard;
LPSTR readers = NULL; LPSTR readers = NULL;
char *reader;
BYTE sendbuf[16], recvbuf[1024]; BYTE sendbuf[16], recvbuf[1024];
DWORD ctl, recvlen, readerslen; DWORD ctl, recvlen;
time_t t_start, t_end; time_t t_start, t_end;
size_t l, pinlen = 0; size_t l, pinlen = 0;
char *pin = NULL; char *pin = NULL;
@@ -214,39 +215,10 @@ main(int argc, char *argv[])
} }
r = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext); r = pcsc_connect(readernum, SCARD_SHARE_DIRECT, 0, &hContext, &readers,
if (r != SCARD_S_SUCCESS) { &hCard);
fprintf(stderr, "Could not connect to PC/SC Service\n"); if (r != SCARD_S_SUCCESS)
goto err; goto err;
}
readerslen = SCARD_AUTOALLOCATE;
r = SCardListReaders(hContext, NULL, (LPSTR) &readers, &readerslen);
if (r != SCARD_S_SUCCESS) {
fprintf(stderr, "Could not get readers\n");
goto err;
}
for (reader = readers, i = 0; readerslen > 0;
l = strlen(reader) + 1, readerslen -= l, reader += l, i++) {
if (i == readernum)
break;
}
if (readerslen <= 0) {
fprintf(stderr, "Could not find reader number %u\n", readernum);
r = SCARD_E_UNKNOWN_READER;
goto err;
}
r = SCardConnect(hContext, reader, SCARD_SHARE_DIRECT, 0, &hCard, &ctl);
if (r != SCARD_S_SUCCESS) {
fprintf(stderr, "Could not connect to %s\n", reader);
goto err;
}
printf("Connected to %s\n", reader);
#define SIMULATE_BUERGERCLIENT 1 #define SIMULATE_BUERGERCLIENT 1
@@ -254,7 +226,7 @@ main(int argc, char *argv[])
r = SCardReconnect(hCard, SCARD_SHARE_EXCLUSIVE, SCARD_PROTOCOL_T0, r = SCardReconnect(hCard, SCARD_SHARE_EXCLUSIVE, SCARD_PROTOCOL_T0,
SCARD_LEAVE_CARD, &ctl); SCARD_LEAVE_CARD, &ctl);
if (r != SCARD_S_SUCCESS) { if (r != SCARD_S_SUCCESS) {
fprintf(stderr, "Could not reconnect to %s\n", reader); fprintf(stderr, "Could not reconnect\n");
goto err; goto err;
} }
BYTE bufs0[] = { BYTE bufs0[] = {
@@ -291,7 +263,7 @@ main(int argc, char *argv[])
} }
r = SCardReconnect(hCard, SCARD_SHARE_DIRECT, 0, SCARD_LEAVE_CARD, &ctl); r = SCardReconnect(hCard, SCARD_SHARE_DIRECT, 0, SCARD_LEAVE_CARD, &ctl);
if (r != SCARD_S_SUCCESS) { if (r != SCARD_S_SUCCESS) {
fprintf(stderr, "Could not reconnect to %s\n", reader); fprintf(stderr, "Could not reconnect\n");
goto err; goto err;
} }
#endif #endif
@@ -369,7 +341,7 @@ main(int argc, char *argv[])
r = SCardReconnect(hCard, SCARD_SHARE_EXCLUSIVE, SCARD_PROTOCOL_T0, r = SCardReconnect(hCard, SCARD_SHARE_EXCLUSIVE, SCARD_PROTOCOL_T0,
SCARD_LEAVE_CARD, &ctl); SCARD_LEAVE_CARD, &ctl);
if (r != SCARD_S_SUCCESS) { if (r != SCARD_S_SUCCESS) {
fprintf(stderr, "Could not reconnect to %s\n", reader); fprintf(stderr, "Could not reconnect\n");
goto err; goto err;
} }
BYTE bufs1[] = { BYTE bufs1[] = {
@@ -418,7 +390,7 @@ main(int argc, char *argv[])
} }
r = SCardReconnect(hCard, SCARD_SHARE_DIRECT, 0, SCARD_LEAVE_CARD, &ctl); r = SCardReconnect(hCard, SCARD_SHARE_DIRECT, 0, SCARD_LEAVE_CARD, &ctl);
if (r != SCARD_S_SUCCESS) { if (r != SCARD_S_SUCCESS) {
fprintf(stderr, "Could not reconnect to %s\n", reader); fprintf(stderr, "Could not reconnect\n");
goto err; goto err;
} }
#endif #endif

70
ccid/src/pcscutil.c Normal file
View File

@@ -0,0 +1,70 @@
/*
* Copyright (C) 2010 Frank Morgner
*
* This file is part of ccid.
*
* ccid is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* ccid is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* ccid. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include "pcscutil.h"
LONG
pcsc_connect(unsigned int readernum, DWORD dwShareMode, DWORD dwPreferredProtocol,
SCARDCONTEXT *hContext, LPSTR *readers, LPSCARDHANDLE phCard)
{
LONG r;
DWORD ctl, readerslen;
char *reader;
size_t l, i;
r = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
if (r != SCARD_S_SUCCESS) {
fprintf(stderr, "Could not connect to PC/SC Service\n");
goto err;
}
readerslen = SCARD_AUTOALLOCATE;
r = SCardListReaders(hContext, NULL, (LPSTR) readers, &readerslen);
if (r != SCARD_S_SUCCESS) {
fprintf(stderr, "Could not get readers\n");
goto err;
}
for (reader = *readers, i = 0; readerslen > 0;
l = strlen(reader) + 1, readerslen -= l, reader += l, i++) {
if (i == readernum)
break;
}
if (readerslen <= 0) {
fprintf(stderr, "Could not find reader number %u\n", readernum);
r = SCARD_E_UNKNOWN_READER;
goto err;
}
r = SCardConnect(hContext, reader, dwShareMode, dwPreferredProtocol,
phCard, &ctl);
if (r != SCARD_S_SUCCESS) {
fprintf(stderr, "Could not connect to %s\n", reader);
goto err;
}
printf("Connected to %s\n", reader);
err:
return r;
}

46
ccid/src/pcscutil.h Normal file
View File

@@ -0,0 +1,46 @@
/*
* Copyright (C) 2010 Frank Morgner
*
* This file is part of ccid.
*
* ccid is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* ccid is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* ccid. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _PCSCUTIL_H
#define _PCSCUTIL_H
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_PCSCLITE_H
#include <pcsclite.h>
#endif
#ifdef HAVE_READER_H
#include <reader.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
LONG
pcsc_connect(unsigned int readernum, DWORD dwShareMode, DWORD dwPreferredProtocol,
SCARDCONTEXT *hContext, LPSTR *readers, LPSCARDHANDLE phCard);
#ifdef __cplusplus
}
#endif
#endif